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 ?= python3
      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 ?= /usr
     12 LIBDIR ?= $(PREFIX)/lib
     13 SHLIBDIR ?= /lib
     14 INCLUDEDIR ?= $(PREFIX)/include
     15 PYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
     16 PYLIBS ?= $(shell $(PKG_CONFIG) --libs $(PYPREFIX))
     17 PYTHONLIBDIR ?= $(shell $(PYTHON) -c "from distutils.sysconfig import *; print(get_python_lib(plat_specific=1, prefix='$(PREFIX)'))")
     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 ?= $(shell $(RUBY) -e 'puts RbConfig::CONFIG["vendorarchdir"]')
     22 
     23 VERSION = $(shell cat ../VERSION)
     24 LIBVERSION = 1
     25 
     26 OS ?= $(shell uname)
     27 
     28 ifeq ($(shell $(CC) -v 2>&1 | grep "clang"),)
     29 COMPILER ?= gcc
     30 else
     31 COMPILER ?= clang
     32 endif
     33 
     34 LIBA=libselinux.a 
     35 TARGET=libselinux.so
     36 LIBPC=libselinux.pc
     37 SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
     38 SWIGRUBYIF= selinuxswig_ruby.i
     39 SWIGCOUT= selinuxswig_wrap.c
     40 SWIGPYOUT= selinux.py
     41 SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
     42 SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
     43 SWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT)) 
     44 SWIGSO=$(PYPREFIX)_selinux.so
     45 SWIGFILES=$(SWIGSO) $(SWIGPYOUT)
     46 SWIGRUBYSO=$(RUBYPREFIX)_selinux.so
     47 LIBSO=$(TARGET).$(LIBVERSION)
     48 AUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
     49 AUDIT2WHYSO=$(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
     54 ifeq ($(LIBSEPOLA),)
     55         LDLIBS_LIBSEPOLA := -l:libsepol.a
     56 endif
     57 
     58 GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) selinuxswig_python_exception.i
     59 SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
     60 
     61 MAX_STACK_SIZE=32768
     62 
     63 ifeq ($(COMPILER), gcc)
     64 EXTRA_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
     69 else
     70 EXTRA_CFLAGS = -Wunused-command-line-argument
     71 endif
     72 
     73 OBJS= $(patsubst %.c,%.o,$(SRCS))
     74 LOBJS= $(patsubst %.c,%.lo,$(SRCS))
     75 CFLAGS ?= -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 
     93 LD_SONAME_FLAGS=-soname,$(LIBSO),-z,defs,-z,relro
     94 
     95 ifeq ($(OS), Darwin)
     96 override CFLAGS += -I/opt/local/include
     97 override LDFLAGS += -L/opt/local/lib -undefined dynamic_lookup
     98 LD_SONAME_FLAGS=-install_name,$(LIBSO)
     99 endif
    100 
    101 PCRE_LDLIBS ?= -lpcre
    102 # override with -lfts when building on Musl libc to use fts-standalone
    103 FTS_LDLIBS ?=
    104 
    105 override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
    106 
    107 SWIG_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 
    110 RANLIB ?= ranlib
    111 
    112 ARCH := $(patsubst i%86,i386,$(shell uname -m))
    113 ifneq (,$(filter i386,$(ARCH)))
    114 TLSFLAGS += -mno-tls-direct-seg-refs
    115 endif
    116 
    117 ifeq ($(ANDROID_HOST),y)
    118 DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
    119 	-DBUILD_HOST
    120 SRCS= 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
    123 else
    124 DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
    125 SRCS:= $(filter-out label_backends_android.c, $(SRCS))
    126 endif
    127 
    128 SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(DISABLE_FLAGS)
    129 
    130 SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
    131 
    132 all: $(LIBA) $(LIBSO) $(LIBPC)
    133 
    134 pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
    135 
    136 rubywrap: all $(SWIGRUBYSO)
    137 
    138 $(SWIGLOBJ): $(SWIGCOUT)
    139 	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(PYINC) -fPIC -DSHARED -c -o $@ $<
    140 
    141 $(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
    142 	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
    143 
    144 $(SWIGSO): $(SWIGLOBJ)
    145 	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $< -lselinux $(PYLIBS)
    146 
    147 $(SWIGRUBYSO): $(SWIGRUBYLOBJ)
    148 	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
    149 
    150 $(LIBA): $(OBJS)
    151 	$(AR) rcs $@ $^
    152 	$(RANLIB) $@
    153 
    154 $(LIBSO): $(LOBJS)
    155 	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ $(PCRE_LDLIBS) $(FTS_LDLIBS) -ldl -Wl,$(LD_SONAME_FLAGS)
    156 	ln -sf $@ $(TARGET)
    157 
    158 $(LIBPC): $(LIBPC).in ../VERSION
    159 	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBDIR):; s:@includedir@:$(INCLUDEDIR):; s:@PCRE_MODULE@:$(PCRE_MODULE):' < $< > $@
    160 
    161 selinuxswig_python_exception.i: ../include/selinux/selinux.h
    162 	bash -e exception.sh > $@ || (rm -f $@ ; false)
    163 
    164 $(AUDIT2WHYLOBJ): audit2why.c
    165 	$(CC) $(filter-out -Werror, $(CFLAGS)) $(PYINC) -fPIC -DSHARED -c -o $@ $<
    166 
    167 $(AUDIT2WHYSO): $(AUDIT2WHYLOBJ) $(LIBSEPOLA)
    168 	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(LDLIBS_LIBSEPOLA) $(PYLIBS) -Wl,-soname,audit2why.so,--version-script=audit2why.map,-z,defs
    169 
    170 %.o:  %.c policy.h
    171 	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
    172 
    173 %.lo:  %.c policy.h
    174 	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
    175 
    176 $(SWIGCOUT): $(SWIGIF)
    177 	$(SWIG) $<
    178 
    179 $(SWIGPYOUT): $(SWIGCOUT)
    180 
    181 $(SWIGRUBYCOUT): $(SWIGRUBYIF)
    182 	$(SWIGRUBY) $<
    183 
    184 swigify: $(SWIGIF)
    185 	$(SWIG) $<
    186 
    187 install: all 
    188 	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
    189 	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
    190 	test -d $(DESTDIR)$(SHLIBDIR) || install -m 755 -d $(DESTDIR)$(SHLIBDIR)
    191 	install -m 755 $(LIBSO) $(DESTDIR)$(SHLIBDIR)
    192 	test -d $(DESTDIR)$(LIBDIR)/pkgconfig || install -m 755 -d $(DESTDIR)$(LIBDIR)/pkgconfig
    193 	install -m 644 $(LIBPC) $(DESTDIR)$(LIBDIR)/pkgconfig
    194 	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
    195 
    196 install-pywrap: pywrap
    197 	test -d $(DESTDIR)$(PYTHONLIBDIR)/selinux || install -m 755 -d $(DESTDIR)$(PYTHONLIBDIR)/selinux
    198 	install -m 755 $(SWIGSO) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
    199 	install -m 755 $(AUDIT2WHYSO) $(DESTDIR)$(PYTHONLIBDIR)/selinux/audit2why$(PYCEXT)
    200 	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
    201 
    202 install-rubywrap: rubywrap
    203 	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL) 
    204 	install -m 755 $(SWIGRUBYSO) $(DESTDIR)$(RUBYINSTALL)/selinux.so
    205 
    206 relabel:
    207 	/sbin/restorecon $(DESTDIR)$(SHLIBDIR)/$(LIBSO)
    208 
    209 clean-pywrap:
    210 	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
    211 
    212 clean-rubywrap:
    213 	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
    214 
    215 clean: clean-pywrap clean-rubywrap
    216 	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
    217 
    218 distclean: clean
    219 	rm -f $(GENERATED) $(SWIGFILES)
    220 
    221 indent:
    222 	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
    223 
    224 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
    225