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