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":
4PYTHON ?= python
5PYPREFIX ?= $(notdir $(PYTHON))
6RUBY ?= ruby
7RUBYPREFIX ?= $(notdir $(RUBY))
8
9# Installation directories.
10PREFIX ?= $(DESTDIR)/usr
11LIBDIR ?= $(PREFIX)/lib
12SHLIBDIR ?= $(DESTDIR)/lib
13INCLUDEDIR ?= $(PREFIX)/include
14PYLIBVER ?= $(shell $(PYTHON) -c 'import sys;print("python%d.%d" % sys.version_info[0:2])')
15PYINC ?= $(shell pkg-config --cflags $(PYPREFIX))
16PYLIBDIR ?= $(LIBDIR)/$(PYLIBVER)
17RUBYLIBVER ?= $(shell $(RUBY) -e 'print RUBY_VERSION.split(".")[0..1].join(".")')
18RUBYPLATFORM ?= $(shell $(RUBY) -e 'print RUBY_PLATFORM')
19RUBYINC ?= $(shell pkg-config --cflags ruby)
20RUBYINSTALL ?= $(LIBDIR)/ruby/site_ruby/$(RUBYLIBVER)/$(RUBYPLATFORM)
21LIBBASE ?= $(shell basename $(LIBDIR))
22
23VERSION = $(shell cat ../VERSION)
24LIBVERSION = 1
25
26LIBA=libselinux.a 
27TARGET=libselinux.so
28LIBPC=libselinux.pc
29SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
30SWIGRUBYIF= selinuxswig_ruby.i
31SWIGCOUT= selinuxswig_wrap.c
32SWIGPYOUT= selinux.py
33SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
34SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
35SWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT)) 
36SWIGSO=$(PYPREFIX)_selinux.so
37SWIGFILES=$(SWIGSO) $(SWIGPYOUT)
38SWIGRUBYSO=$(RUBYPREFIX)_selinux.so
39LIBSO=$(TARGET).$(LIBVERSION)
40AUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
41AUDIT2WHYSO=$(PYPREFIX)audit2why.so
42
43ifeq ($(DISABLE_AVC),y)
44	UNUSED_SRCS+=avc.c avc_internal.c avc_sidtab.c mapping.c stringrep.c checkAccess.c
45endif
46ifeq ($(DISABLE_BOOL),y)
47	UNUSED_SRCS+=booleans.c
48endif
49
50GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) selinuxswig_python_exception.i
51SRCS= $(filter-out $(UNUSED_SRCS) $(GENERATED) audit2why.c, $(wildcard *.c))
52
53MAX_STACK_SIZE=32768
54
55OBJS= $(patsubst %.c,%.o,$(SRCS))
56LOBJS= $(patsubst %.c,%.lo,$(SRCS))
57CFLAGS ?= -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
76override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 $(EMFLAGS)
77
78SWIG_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
81RANLIB ?= ranlib
82
83ARCH := $(patsubst i%86,i386,$(shell uname -m))
84ifneq (,$(filter i386,$(ARCH)))
85TLSFLAGS += -mno-tls-direct-seg-refs
86endif
87
88SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(EMFLAGS)
89
90SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(EMFLAGS)
91
92all: $(LIBA) $(LIBSO) $(LIBPC)
93
94pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
95
96rubywrap: 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
121selinuxswig_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
144swigify: $(SWIGIF)
145	$(SWIG) $<
146
147install: 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
156install-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
162install-rubywrap: rubywrap
163	test -d $(RUBYINSTALL) || install -m 755 -d $(RUBYINSTALL) 
164	install -m 755 $(SWIGRUBYSO) $(RUBYINSTALL)/selinux.so
165
166relabel:
167	/sbin/restorecon $(SHLIBDIR)/$(LIBSO)
168
169clean: 
170	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(SWIGLOBJ) $(SWIGRUBYLOBJ) $(SWIGSO) $(TARGET) $(AUDIT2WHYSO) *.o *.lo *~
171
172distclean: clean
173	rm -f $(GENERATED) $(SWIGFILES)
174
175indent:
176	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
177
178.PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
179