• 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 "import sysconfig; print(sysconfig.get_path('platlib', vars={'platbase': '$(PREFIX)', 'base': '$(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 \
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
106# check for strlcpy(3) availability
107H := \#
108ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
109override CFLAGS += -DHAVE_STRLCPY
110endif
111
112SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
113		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
114		-Wno-deprecated-declarations
115
116RANLIB ?= ranlib
117
118ARCH := $(patsubst i%86,i386,$(shell uname -m))
119ifneq (,$(filter i386,$(ARCH)))
120TLSFLAGS += -mno-tls-direct-seg-refs
121endif
122
123ifeq ($(ANDROID_HOST),y)
124DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
125	-DBUILD_HOST
126SRCS= callbacks.c freecon.c label.c label_file.c \
127	label_backends_android.c regex.c label_support.c \
128	matchpathcon.c setrans_client.c sha1.c booleans.c
129else
130LABEL_BACKEND_ANDROID=y
131endif
132
133ifneq ($(LABEL_BACKEND_ANDROIDT),y)
134SRCS:= $(filter-out label_backends_android.c, $(SRCS))
135DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
136endif
137
138ifeq ($(DISABLE_X11),y)
139SRCS:= $(filter-out label_x.c, $(SRCS))
140endif
141
142SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
143
144all: $(LIBA) $(LIBSO) $(LIBPC)
145
146pywrap: all selinuxswig_python_exception.i
147	CFLAGS="$(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext
148
149rubywrap: all $(SWIGRUBYSO)
150
151$(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
152	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
153
154$(SWIGRUBYSO): $(SWIGRUBYLOBJ)
155	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
156
157$(LIBA): $(OBJS)
158	$(AR) rcs $@ $^
159	$(RANLIB) $@
160
161$(LIBSO): $(LOBJS)
162	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ $(PCRE_LDLIBS) $(FTS_LDLIBS) -ldl -Wl,$(LD_SONAME_FLAGS)
163	ln -sf $@ $(TARGET)
164
165$(LIBPC): $(LIBPC).in ../VERSION
166	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBDIR):; s:@includedir@:$(INCLUDEDIR):; s:@PCRE_MODULE@:$(PCRE_MODULE):' < $< > $@
167
168selinuxswig_python_exception.i: exception.sh ../include/selinux/selinux.h
169	bash -e exception.sh > $@ || (rm -f $@ ; false)
170
171%.o:  %.c policy.h
172	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
173
174%.lo:  %.c policy.h
175	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
176
177$(SWIGRUBYCOUT): $(SWIGRUBYIF)
178	$(SWIGRUBY) $<
179
180install: all
181	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
182	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
183	test -d $(DESTDIR)$(SHLIBDIR) || install -m 755 -d $(DESTDIR)$(SHLIBDIR)
184	install -m 755 $(LIBSO) $(DESTDIR)$(SHLIBDIR)
185	test -d $(DESTDIR)$(LIBDIR)/pkgconfig || install -m 755 -d $(DESTDIR)$(LIBDIR)/pkgconfig
186	install -m 644 $(LIBPC) $(DESTDIR)$(LIBDIR)/pkgconfig
187	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
188
189install-pywrap: pywrap
190	$(PYTHON) -m pip install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR) --ignore-installed --no-deps` $(PYTHON_SETUP_ARGS) .
191	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
192	ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
193
194install-rubywrap: rubywrap
195	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL)
196	install -m 755 $(SWIGRUBYSO) $(DESTDIR)$(RUBYINSTALL)/selinux.so
197
198relabel:
199	/sbin/restorecon $(DESTDIR)$(SHLIBDIR)/$(LIBSO)
200
201clean-pywrap:
202	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
203	$(PYTHON) setup.py clean
204	-rm -rf build *~ \#* *pyc .#* selinux.egg-info/
205
206clean-rubywrap:
207	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
208
209clean: clean-pywrap clean-rubywrap
210	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
211
212distclean: clean
213	rm -f $(GENERATED) $(SWIGFILES)
214
215indent:
216	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
217
218.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
219