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