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