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 
      9 # Installation directories.
     10 PREFIX ?= $(DESTDIR)/usr
     11 LIBDIR ?= $(PREFIX)/lib
     12 SHLIBDIR ?= $(DESTDIR)/lib
     13 INCLUDEDIR ?= $(PREFIX)/include
     14 PYLIBVER ?= $(shell $(PYTHON) -c 'import sys;print("python%d.%d" % sys.version_info[0:2])')
     15 PYINC ?= $(shell pkg-config --cflags $(PYPREFIX))
     16 PYLIBDIR ?= $(LIBDIR)/$(PYLIBVER)
     17 RUBYLIBVER ?= $(shell $(RUBY) -e 'print RUBY_VERSION.split(".")[0..1].join(".")')
     18 RUBYPLATFORM ?= $(shell $(RUBY) -e 'print RUBY_PLATFORM')
     19 RUBYINC ?= $(shell pkg-config --cflags ruby)
     20 RUBYINSTALL ?= $(LIBDIR)/ruby/site_ruby/$(RUBYLIBVER)/$(RUBYPLATFORM)
     21 LIBBASE ?= $(shell basename $(LIBDIR))
     22 
     23 VERSION = $(shell cat ../VERSION)
     24 LIBVERSION = 1
     25 
     26 LIBA=libselinux.a 
     27 TARGET=libselinux.so
     28 LIBPC=libselinux.pc
     29 SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
     30 SWIGRUBYIF= selinuxswig_ruby.i
     31 SWIGCOUT= selinuxswig_wrap.c
     32 SWIGPYOUT= selinux.py
     33 SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
     34 SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
     35 SWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT)) 
     36 SWIGSO=$(PYPREFIX)_selinux.so
     37 SWIGFILES=$(SWIGSO) $(SWIGPYOUT)
     38 SWIGRUBYSO=$(RUBYPREFIX)_selinux.so
     39 LIBSO=$(TARGET).$(LIBVERSION)
     40 AUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
     41 AUDIT2WHYSO=$(PYPREFIX)audit2why.so
     42 
     43 ifeq ($(DISABLE_AVC),y)
     44 	UNUSED_SRCS+=avc.c avc_internal.c avc_sidtab.c mapping.c stringrep.c checkAccess.c
     45 endif
     46 ifeq ($(DISABLE_BOOL),y)
     47 	UNUSED_SRCS+=booleans.c
     48 endif
     49 
     50 GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) selinuxswig_python_exception.i
     51 SRCS= $(filter-out $(UNUSED_SRCS) $(GENERATED) audit2why.c, $(wildcard *.c))
     52 
     53 MAX_STACK_SIZE=32768
     54 
     55 OBJS= $(patsubst %.c,%.o,$(SRCS))
     56 LOBJS= $(patsubst %.c,%.lo,$(SRCS))
     57 CFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissing-include-dirs \
     58           -Wunused -Wunknown-pragmas -Wstrict-aliasing -Wshadow -Wpointer-arith \
     59           -Wbad-function-cast -Wcast-align -Wwrite-strings -Wlogical-op -Waggregate-return \
     60           -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes \
     61           -Wmissing-declarations -Wmissing-noreturn -Wmissing-format-attribute \
     62           -Wredundant-decls -Wnested-externs -Winline -Winvalid-pch -Wvolatile-register-var \
     63           -Wdisabled-optimization -Wbuiltin-macro-redefined -Wpacked-bitfield-compat \
     64           -Wsync-nand -Wattributes -Wcoverage-mismatch -Wmultichar -Wcpp \
     65           -Wdeprecated-declarations -Wdiv-by-zero -Wdouble-promotion -Wendif-labels -Wextra \
     66           -Wformat-contains-nul -Wformat-extra-args -Wformat-zero-length -Wformat=2 -Wmultichar \
     67           -Wnormalized=nfc -Woverflow -Wpointer-to-int-cast -Wpragmas -Wsuggest-attribute=const \
     68           -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines \
     69           -Wno-missing-field-initializers -Wno-sign-compare -Wjump-misses-init \
     70           -Wno-format-nonliteral -Wframe-larger-than=$(MAX_STACK_SIZE) -Wp,-D_FORTIFY_SOURCE=2 \
     71           -fstack-protector-all --param=ssp-buffer-size=4 -fexceptions \
     72           -fasynchronous-unwind-tables -fdiagnostics-show-option -funit-at-a-time \
     73           -fipa-pure-const -Wno-suggest-attribute=pure -Wno-suggest-attribute=const \
     74           -Werror -Wno-aggregate-return -Wno-redundant-decls
     75 
     76 override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 $(EMFLAGS)
     77 
     78 SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
     79 		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations
     80 
     81 RANLIB ?= ranlib
     82 
     83 ARCH := $(patsubst i%86,i386,$(shell uname -m))
     84 ifneq (,$(filter i386,$(ARCH)))
     85 TLSFLAGS += -mno-tls-direct-seg-refs
     86 endif
     87 
     88 SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(EMFLAGS)
     89 
     90 SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(EMFLAGS)
     91 
     92 all: $(LIBA) $(LIBSO) $(LIBPC)
     93 
     94 pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
     95 
     96 rubywrap: all $(SWIGRUBYSO)
     97 
     98 $(SWIGLOBJ): $(SWIGCOUT)
     99 	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(PYINC) -fPIC -DSHARED -c -o $@ $<
    100 
    101 $(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
    102 	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
    103 
    104 $(SWIGSO): $(SWIGLOBJ)
    105 	$(CC) $(CFLAGS) -shared -o $@ $< -L. -lselinux $(LDFLAGS) -L$(LIBDIR)
    106 
    107 $(SWIGRUBYSO): $(SWIGRUBYLOBJ)
    108 	$(CC) $(CFLAGS) -shared -o $@ $^ -L. -lselinux $(LDFLAGS) -L$(LIBDIR)
    109 
    110 $(LIBA): $(OBJS)
    111 	$(AR) rcs $@ $^
    112 	$(RANLIB) $@
    113 
    114 $(LIBSO): $(LOBJS)
    115 	$(CC) $(CFLAGS) -shared -o $@ $^ -lpcre -ldl $(LDFLAGS) -L$(LIBDIR) -Wl,-soname,$(LIBSO),-z,defs,-z,relro
    116 	ln -sf $@ $(TARGET) 
    117 
    118 $(LIBPC): $(LIBPC).in ../VERSION
    119 	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBBASE):; s:@includedir@:$(INCLUDEDIR):' < $< > $@
    120 
    121 selinuxswig_python_exception.i: ../include/selinux/selinux.h
    122 	bash exception.sh > $@ 
    123 
    124 $(AUDIT2WHYLOBJ): audit2why.c
    125 	$(CC) $(filter-out -Werror, $(CFLAGS)) $(PYINC) -fPIC -DSHARED -c -o $@ $<
    126 
    127 $(AUDIT2WHYSO): $(AUDIT2WHYLOBJ)
    128 	$(CC) $(CFLAGS) -shared -o $@ $^ -L. $(LDFLAGS) -lselinux $(LIBDIR)/libsepol.a -L$(LIBDIR)
    129 
    130 %.o:  %.c policy.h
    131 	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
    132 
    133 %.lo:  %.c policy.h
    134 	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
    135 
    136 $(SWIGCOUT): $(SWIGIF)
    137 	$(SWIG) $<
    138 
    139 $(SWIGPYOUT): $(SWIGCOUT)
    140 
    141 $(SWIGRUBYCOUT): $(SWIGRUBYIF)
    142 	$(SWIGRUBY) $<
    143 
    144 swigify: $(SWIGIF)
    145 	$(SWIG) $<
    146 
    147 install: all 
    148 	test -d $(LIBDIR) || install -m 755 -d $(LIBDIR)
    149 	install -m 644 $(LIBA) $(LIBDIR)
    150 	test -d $(SHLIBDIR) || install -m 755 -d $(SHLIBDIR)
    151 	install -m 755 $(LIBSO) $(SHLIBDIR)
    152 	test -d $(LIBDIR)/pkgconfig || install -m 755 -d $(LIBDIR)/pkgconfig
    153 	install -m 644 $(LIBPC) $(LIBDIR)/pkgconfig
    154 	ln -sf --relative $(SHLIBDIR)/$(LIBSO) $(LIBDIR)/$(TARGET)
    155 
    156 install-pywrap: pywrap
    157 	test -d $(PYLIBDIR)/site-packages/selinux || install -m 755 -d $(PYLIBDIR)/site-packages/selinux
    158 	install -m 755 $(SWIGSO) $(PYLIBDIR)/site-packages/selinux/_selinux.so
    159 	install -m 755 $(AUDIT2WHYSO) $(PYLIBDIR)/site-packages/selinux/audit2why.so
    160 	install -m 644 $(SWIGPYOUT) $(PYLIBDIR)/site-packages/selinux/__init__.py
    161 
    162 install-rubywrap: rubywrap
    163 	test -d $(RUBYINSTALL) || install -m 755 -d $(RUBYINSTALL) 
    164 	install -m 755 $(SWIGRUBYSO) $(RUBYINSTALL)/selinux.so
    165 
    166 relabel:
    167 	/sbin/restorecon $(SHLIBDIR)/$(LIBSO)
    168 
    169 clean: 
    170 	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(SWIGLOBJ) $(SWIGRUBYLOBJ) $(SWIGSO) $(TARGET) $(AUDIT2WHYSO) *.o *.lo *~
    171 
    172 distclean: clean
    173 	rm -f $(GENERATED) $(SWIGFILES)
    174 
    175 indent:
    176 	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
    177 
    178 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
    179