• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Support building the Python bindings multiple times, against various Python
2# runtimes (e.g. Python 2 vs Python 3) by optionally prefixing the build
3# targets with "PYPREFIX":
4PYTHON ?= python3
5PYPREFIX ?= $(shell $(PYTHON) -c 'import sys;print("python-%d.%d" % sys.version_info[:2])')
6RUBY ?= ruby
7RUBYPREFIX ?= $(notdir $(RUBY))
8PKG_CONFIG ?= pkg-config
9
10# Installation directories.
11PREFIX ?= /usr
12LIBDIR ?= $(PREFIX)/lib
13SHLIBDIR ?= /lib
14INCLUDEDIR ?= $(PREFIX)/include
15PYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
16PYLIBS ?= $(shell $(PKG_CONFIG) --libs $(PYPREFIX))
17PYTHONLIBDIR ?= $(shell $(PYTHON) -c "from distutils.sysconfig import *; print(get_python_lib(plat_specific=1, prefix='$(PREFIX)'))")
18PYCEXT ?= $(shell $(PYTHON) -c 'import importlib.machinery;print(importlib.machinery.EXTENSION_SUFFIXES[0])')
19RUBYINC ?= $(shell $(RUBY) -e 'puts "-I" + RbConfig::CONFIG["rubyarchhdrdir"] + " -I" + RbConfig::CONFIG["rubyhdrdir"]')
20RUBYLIBS ?= $(shell $(RUBY) -e 'puts "-L" + RbConfig::CONFIG["libdir"] + " -L" + RbConfig::CONFIG["archlibdir"] + " " + RbConfig::CONFIG["LIBRUBYARG_SHARED"]')
21RUBYINSTALL ?= $(shell $(RUBY) -e 'puts RbConfig::CONFIG["vendorarchdir"]')
22
23VERSION = $(shell cat ../VERSION)
24LIBVERSION = 1
25
26OS ?= $(shell uname)
27
28ifeq ($(shell $(CC) -v 2>&1 | grep "clang"),)
29COMPILER ?= gcc
30else
31COMPILER ?= clang
32endif
33
34LIBA=libselinux.a
35TARGET=libselinux.so
36LIBPC=libselinux.pc
37SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
38SWIGRUBYIF= selinuxswig_ruby.i
39SWIGCOUT= selinuxswig_python_wrap.c
40SWIGPYOUT= selinux.py
41SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
42SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
43SWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT))
44SWIGSO=$(PYPREFIX)_selinux.so
45SWIGFILES=$(SWIGSO) $(SWIGPYOUT)
46SWIGRUBYSO=$(RUBYPREFIX)_selinux.so
47LIBSO=$(TARGET).$(LIBVERSION)
48AUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
49AUDIT2WHYSO=$(PYPREFIX)audit2why.so
50
51# If no specific libsepol.a is specified, fall back on LDFLAGS search path
52# Otherwise, as $(LIBSEPOLA) already appears in the dependencies, there
53# is no need to define a value for LDLIBS_LIBSEPOLA
54ifeq ($(LIBSEPOLA),)
55        LDLIBS_LIBSEPOLA := -l:libsepol.a
56endif
57
58GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) $(SWIGCOUT) selinuxswig_python_exception.i
59SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
60
61MAX_STACK_SIZE=32768
62
63ifeq ($(COMPILER), gcc)
64EXTRA_CFLAGS = -fipa-pure-const -Wlogical-op -Wpacked-bitfield-compat -Wsync-nand \
65	-Wcoverage-mismatch -Wcpp -Wformat-contains-nul -Wnormalized=nfc -Wsuggest-attribute=const \
66	-Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines -Wjump-misses-init \
67	-Wno-suggest-attribute=pure -Wno-suggest-attribute=const -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
68	-Wstrict-overflow=5 -fno-semantic-interposition
69else
70EXTRA_CFLAGS = -Wunused-command-line-argument
71endif
72
73OBJS= $(patsubst %.c,%.o,$(SRCS))
74LOBJS= $(patsubst %.c,%.lo,$(SRCS))
75CFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissing-include-dirs \
76          -Wunused -Wunknown-pragmas -Wstrict-aliasing -Wshadow -Wpointer-arith \
77          -Wbad-function-cast -Wcast-align -Wwrite-strings -Waggregate-return \
78          -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes \
79          -Wmissing-declarations -Wmissing-noreturn -Wmissing-format-attribute \
80          -Wredundant-decls -Wnested-externs -Winline -Winvalid-pch -Wvolatile-register-var \
81          -Wdisabled-optimization -Wbuiltin-macro-redefined \
82          -Wattributes -Wmultichar \
83          -Wdeprecated-declarations -Wdiv-by-zero -Wdouble-promotion -Wendif-labels -Wextra \
84          -Wformat-extra-args -Wformat-zero-length -Wformat=2 -Wmultichar \
85          -Woverflow -Wpointer-to-int-cast -Wpragmas \
86          -Wno-missing-field-initializers -Wno-sign-compare \
87          -Wno-format-nonliteral -Wframe-larger-than=$(MAX_STACK_SIZE) \
88          -fstack-protector-all --param=ssp-buffer-size=4 -fexceptions \
89          -fasynchronous-unwind-tables -fdiagnostics-show-option -funit-at-a-time \
90          -Werror -Wno-aggregate-return -Wno-redundant-decls \
91          $(EXTRA_CFLAGS)
92
93LD_SONAME_FLAGS=-soname,$(LIBSO),--version-script=libselinux.map,-z,defs,-z,relro
94
95ifeq ($(OS), Darwin)
96override CFLAGS += -I/opt/local/include
97override LDFLAGS += -L/opt/local/lib -undefined dynamic_lookup
98LD_SONAME_FLAGS=-install_name,$(LIBSO)
99endif
100
101# override with -lfts when building on Musl libc to use fts-standalone
102FTS_LDLIBS ?=
103
104override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
105
106SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
107		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
108		-Wno-deprecated-declarations
109
110RANLIB ?= ranlib
111
112ARCH := $(patsubst i%86,i386,$(shell uname -m))
113ifneq (,$(filter i386,$(ARCH)))
114TLSFLAGS += -mno-tls-direct-seg-refs
115endif
116
117ifeq ($(ANDROID_HOST),y)
118DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
119	-DBUILD_HOST
120SRCS= callbacks.c freecon.c label.c label_file.c \
121	label_backends_android.c regex.c label_support.c \
122	matchpathcon.c setrans_client.c sha1.c booleans.c
123else
124LABEL_BACKEND_ANDROID=y
125endif
126
127ifneq ($(LABEL_BACKEND_ANDROIDT),y)
128SRCS:= $(filter-out label_backends_android.c, $(SRCS))
129DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
130endif
131
132ifeq ($(DISABLE_X11),y)
133SRCS:= $(filter-out label_x.c, $(SRCS))
134endif
135
136SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
137
138all: $(LIBA) $(LIBSO) $(LIBPC)
139
140pywrap: all selinuxswig_python_exception.i
141	CFLAGS="$(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext
142
143rubywrap: all $(SWIGRUBYSO)
144
145$(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
146	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
147
148$(SWIGRUBYSO): $(SWIGRUBYLOBJ)
149	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
150
151$(LIBA): $(OBJS)
152	$(AR) rcs $@ $^
153	$(RANLIB) $@
154
155$(LIBSO): $(LOBJS)
156	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ $(PCRE_LDLIBS) $(FTS_LDLIBS) -ldl -Wl,$(LD_SONAME_FLAGS)
157	ln -sf $@ $(TARGET)
158
159$(LIBPC): $(LIBPC).in ../VERSION
160	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBDIR):; s:@includedir@:$(INCLUDEDIR):; s:@PCRE_MODULE@:$(PCRE_MODULE):' < $< > $@
161
162selinuxswig_python_exception.i: exception.sh ../include/selinux/selinux.h
163	bash -e exception.sh > $@ || (rm -f $@ ; false)
164
165%.o:  %.c policy.h
166	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
167
168%.lo:  %.c policy.h
169	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
170
171$(SWIGRUBYCOUT): $(SWIGRUBYIF)
172	$(SWIGRUBY) $<
173
174install: all
175	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
176	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
177	test -d $(DESTDIR)$(SHLIBDIR) || install -m 755 -d $(DESTDIR)$(SHLIBDIR)
178	install -m 755 $(LIBSO) $(DESTDIR)$(SHLIBDIR)
179	test -d $(DESTDIR)$(LIBDIR)/pkgconfig || install -m 755 -d $(DESTDIR)$(LIBDIR)/pkgconfig
180	install -m 644 $(LIBPC) $(DESTDIR)$(LIBDIR)/pkgconfig
181	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
182
183install-pywrap: pywrap
184	$(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)` $(PYTHON_SETUP_ARGS)
185	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
186	ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
187
188install-rubywrap: rubywrap
189	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL)
190	install -m 755 $(SWIGRUBYSO) $(DESTDIR)$(RUBYINSTALL)/selinux.so
191
192relabel:
193	/sbin/restorecon $(DESTDIR)$(SHLIBDIR)/$(LIBSO)
194
195clean-pywrap:
196	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
197	$(PYTHON) setup.py clean
198	-rm -rf build *~ \#* *pyc .#*
199
200clean-rubywrap:
201	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
202
203clean: clean-pywrap clean-rubywrap
204	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
205
206distclean: clean
207	rm -f $(GENERATED) $(SWIGFILES)
208
209indent:
210	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
211
212.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
213