Home | History | Annotate | Download | only in src
      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":
      4 PYTHON ?= python
      5 PYPREFIX ?= $(notdir $(PYTHON))
      6 RUBY ?= ruby
      7 RUBYPREFIX ?= $(notdir $(RUBY))
      8 PKG_CONFIG ?= pkg-config
      9 
     10 # Installation directories.
     11 PREFIX ?= $(DESTDIR)/usr
     12 LIBDIR ?= $(PREFIX)/lib
     13 SHLIBDIR ?= $(DESTDIR)/lib
     14 INCLUDEDIR ?= $(PREFIX)/include
     15 PYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
     16 PYLIBS ?= $(shell $(PKG_CONFIG) --libs $(PYPREFIX))
     17 PYSITEDIR ?= $(DESTDIR)$(shell $(PYTHON) -c 'import site; print(site.getsitepackages()[0])')
     18 PYCEXT ?= $(shell $(PYTHON) -c 'import imp;print([s for s,m,t in imp.get_suffixes() if t == imp.C_EXTENSION][0])')
     19 RUBYINC ?= $(shell $(RUBY) -e 'puts "-I" + RbConfig::CONFIG["rubyarchhdrdir"] + " -I" + RbConfig::CONFIG["rubyhdrdir"]')
     20 RUBYLIBS ?= $(shell $(RUBY) -e 'puts "-L" + RbConfig::CONFIG["libdir"] + " -lruby"')
     21 RUBYINSTALL ?= $(DESTDIR)$(shell $(RUBY) -e 'puts RbConfig::CONFIG["vendorarchdir"]')
     22 LIBBASE ?= $(shell basename $(LIBDIR))
     23 LIBSEPOLA ?= $(LIBDIR)/libsepol.a
     24 
     25 VERSION = $(shell cat ../VERSION)
     26 LIBVERSION = 1
     27 
     28 OS ?= $(shell uname)
     29 
     30 ifeq ($(shell $(CC) -v 2>&1 | grep "clang"),)
     31 COMPILER ?= gcc
     32 else
     33 COMPILER ?= clang
     34 endif
     35 
     36 LIBA=libselinux.a 
     37 TARGET=libselinux.so
     38 LIBPC=libselinux.pc
     39 SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
     40 SWIGRUBYIF= selinuxswig_ruby.i
     41 SWIGCOUT= selinuxswig_wrap.c
     42 SWIGPYOUT= selinux.py
     43 SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
     44 SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
     45 SWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT)) 
     46 SWIGSO=$(PYPREFIX)_selinux.so
     47 SWIGFILES=$(SWIGSO) $(SWIGPYOUT)
     48 SWIGRUBYSO=$(RUBYPREFIX)_selinux.so
     49 LIBSO=$(TARGET).$(LIBVERSION)
     50 AUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
     51 AUDIT2WHYSO=$(PYPREFIX)audit2why.so
     52 
     53 GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) selinuxswig_python_exception.i
     54 SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
     55 
     56 MAX_STACK_SIZE=32768
     57 
     58 OBJS= $(patsubst %.c,%.o,$(SRCS))
     59 LOBJS= $(patsubst %.c,%.lo,$(SRCS))
     60 CFLAGS ?= -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 
     77 LD_SONAME_FLAGS=-soname,$(LIBSO),-z,defs,-z,relro
     78 
     79 ifeq ($(COMPILER), gcc)
     80 CFLAGS += -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
     84 else
     85 CFLAGS += -Wunused-command-line-argument
     86 endif
     87 
     88 ifeq ($(OS), Darwin)
     89 override CFLAGS += -I/opt/local/include
     90 override LDFLAGS += -L/opt/local/lib -undefined dynamic_lookup
     91 LD_SONAME_FLAGS=-install_name,$(LIBSO)
     92 endif
     93 
     94 PCRE_LDFLAGS ?= -lpcre
     95 
     96 override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
     97 
     98 SWIG_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 
    101 RANLIB ?= ranlib
    102 
    103 ARCH := $(patsubst i%86,i386,$(shell uname -m))
    104 ifneq (,$(filter i386,$(ARCH)))
    105 TLSFLAGS += -mno-tls-direct-seg-refs
    106 endif
    107 
    108 ifeq ($(ANDROID_HOST),y)
    109 DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
    110 	-DBUILD_HOST
    111 SRCS= 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
    114 else
    115 DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
    116 SRCS:= $(filter-out label_backends_android.c, $(SRCS))
    117 endif
    118 
    119 SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(DISABLE_FLAGS)
    120 
    121 SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
    122 
    123 all: $(LIBA) $(LIBSO) $(LIBPC)
    124 
    125 pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
    126 
    127 rubywrap: 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 
    152 selinuxswig_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 
    175 swigify: $(SWIGIF)
    176 	$(SWIG) $<
    177 
    178 install: 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 
    187 install-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 
    193 install-rubywrap: rubywrap
    194 	test -d $(RUBYINSTALL) || install -m 755 -d $(RUBYINSTALL) 
    195 	install -m 755 $(SWIGRUBYSO) $(RUBYINSTALL)/selinux.so
    196 
    197 relabel:
    198 	/sbin/restorecon $(SHLIBDIR)/$(LIBSO)
    199 
    200 clean-pywrap:
    201 	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
    202 
    203 clean-rubywrap:
    204 	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
    205 
    206 clean: clean-pywrap clean-rubywrap
    207 	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
    208 
    209 distclean: clean
    210 	rm -f $(GENERATED) $(SWIGFILES)
    211 
    212 indent:
    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