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 ?= $(shell $(PYTHON) -c 'import sys;print("python-%d.%d" % sys.version_info[:2])')
      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"] + " -L" + RbConfig::CONFIG["archlibdir"] + " " + RbConfig::CONFIG["LIBRUBYARG_SHARED"]')
     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 ifeq ($(COMPILER), gcc)
     59 EXTRA_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
     63 else
     64 EXTRA_CFLAGS = -Wunused-command-line-argument
     65 endif
     66 
     67 OBJS= $(patsubst %.c,%.o,$(SRCS))
     68 LOBJS= $(patsubst %.c,%.lo,$(SRCS))
     69 CFLAGS ?= -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 
     87 LD_SONAME_FLAGS=-soname,$(LIBSO),-z,defs,-z,relro
     88 
     89 ifeq ($(OS), Darwin)
     90 override CFLAGS += -I/opt/local/include
     91 override LDFLAGS += -L/opt/local/lib -undefined dynamic_lookup
     92 LD_SONAME_FLAGS=-install_name,$(LIBSO)
     93 endif
     94 
     95 PCRE_LDLIBS ?= -lpcre
     96 
     97 override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
     98 
     99 SWIG_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 
    102 RANLIB ?= ranlib
    103 
    104 ARCH := $(patsubst i%86,i386,$(shell uname -m))
    105 ifneq (,$(filter i386,$(ARCH)))
    106 TLSFLAGS += -mno-tls-direct-seg-refs
    107 endif
    108 
    109 ifeq ($(ANDROID_HOST),y)
    110 DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
    111 	-DBUILD_HOST
    112 SRCS= 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
    115 else
    116 DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
    117 SRCS:= $(filter-out label_backends_android.c, $(SRCS))
    118 endif
    119 
    120 SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(DISABLE_FLAGS)
    121 
    122 SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
    123 
    124 all: $(LIBA) $(LIBSO) $(LIBPC)
    125 
    126 pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
    127 
    128 rubywrap: 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 
    153 selinuxswig_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 
    176 swigify: $(SWIGIF)
    177 	$(SWIG) $<
    178 
    179 install: 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 
    188 install-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 
    194 install-rubywrap: rubywrap
    195 	test -d $(RUBYINSTALL) || install -m 755 -d $(RUBYINSTALL) 
    196 	install -m 755 $(SWIGRUBYSO) $(RUBYINSTALL)/selinux.so
    197 
    198 relabel:
    199 	/sbin/restorecon $(SHLIBDIR)/$(LIBSO)
    200 
    201 clean-pywrap:
    202 	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
    203 
    204 clean-rubywrap:
    205 	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
    206 
    207 clean: clean-pywrap clean-rubywrap
    208 	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
    209 
    210 distclean: clean
    211 	rm -f $(GENERATED) $(SWIGFILES)
    212 
    213 indent:
    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