• 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 imp;print([s for s,m,t in imp.get_suffixes() if t == imp.C_EXTENSION][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
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),-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
101PCRE_LDLIBS ?= -lpcre
102# override with -lfts when building on Musl libc to use fts-standalone
103FTS_LDLIBS ?=
104
105override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
106
107SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
108		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-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
124DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
125SRCS:= $(filter-out label_backends_android.c, $(SRCS))
126endif
127
128SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
129
130all: $(LIBA) $(LIBSO) $(LIBPC)
131
132pywrap: all selinuxswig_python_exception.i
133	CFLAGS="$(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext
134
135rubywrap: all $(SWIGRUBYSO)
136
137$(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
138	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
139
140$(SWIGRUBYSO): $(SWIGRUBYLOBJ)
141	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
142
143$(LIBA): $(OBJS)
144	$(AR) rcs $@ $^
145	$(RANLIB) $@
146
147$(LIBSO): $(LOBJS)
148	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ $(PCRE_LDLIBS) $(FTS_LDLIBS) -ldl -Wl,$(LD_SONAME_FLAGS)
149	ln -sf $@ $(TARGET)
150
151$(LIBPC): $(LIBPC).in ../VERSION
152	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBDIR):; s:@includedir@:$(INCLUDEDIR):; s:@PCRE_MODULE@:$(PCRE_MODULE):' < $< > $@
153
154selinuxswig_python_exception.i: exception.sh ../include/selinux/selinux.h
155	bash -e exception.sh > $@ || (rm -f $@ ; false)
156
157%.o:  %.c policy.h
158	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
159
160%.lo:  %.c policy.h
161	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
162
163$(SWIGRUBYCOUT): $(SWIGRUBYIF)
164	$(SWIGRUBY) $<
165
166install: all
167	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
168	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
169	test -d $(DESTDIR)$(SHLIBDIR) || install -m 755 -d $(DESTDIR)$(SHLIBDIR)
170	install -m 755 $(LIBSO) $(DESTDIR)$(SHLIBDIR)
171	test -d $(DESTDIR)$(LIBDIR)/pkgconfig || install -m 755 -d $(DESTDIR)$(LIBDIR)/pkgconfig
172	install -m 644 $(LIBPC) $(DESTDIR)$(LIBDIR)/pkgconfig
173	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
174
175install-pywrap: pywrap
176	$(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)`
177	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
178	ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
179
180install-rubywrap: rubywrap
181	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL)
182	install -m 755 $(SWIGRUBYSO) $(DESTDIR)$(RUBYINSTALL)/selinux.so
183
184relabel:
185	/sbin/restorecon $(DESTDIR)$(SHLIBDIR)/$(LIBSO)
186
187clean-pywrap:
188	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
189	$(PYTHON) setup.py clean
190	-rm -rf build *~ \#* *pyc .#*
191
192clean-rubywrap:
193	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
194
195clean: clean-pywrap clean-rubywrap
196	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
197
198distclean: clean
199	rm -f $(GENERATED) $(SWIGFILES)
200
201indent:
202	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
203
204.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
205