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 ?= $(shell $(PYTHON) -c 'import sys;print("python-%d.%d" % sys.version_info[:2])')
6RUBY ?= ruby
7RUBYPREFIX ?= $(notdir $(RUBY))
8PKG_CONFIG ?= pkg-config
9
10# Installation directories.
11PREFIX ?= $(DESTDIR)/usr
12LIBDIR ?= $(PREFIX)/lib
13SHLIBDIR ?= $(DESTDIR)/lib
14INCLUDEDIR ?= $(PREFIX)/include
15PYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
16PYLIBS ?= $(shell $(PKG_CONFIG) --libs $(PYPREFIX))
17PYSITEDIR ?= $(DESTDIR)$(shell $(PYTHON) -c 'import site; print(site.getsitepackages()[0])')
18PYCEXT ?= $(shell $(PYTHON) -c 'import imp;print([s for s,m,t in imp.get_suffixes() if t == imp.C_EXTENSION][0])')
19RUBYINC ?= $(shell $(RUBY) -e 'puts "-I" + RbConfig::CONFIG["rubyarchhdrdir"] + " -I" + RbConfig::CONFIG["rubyhdrdir"]')
20RUBYLIBS ?= $(shell $(RUBY) -e 'puts "-L" + RbConfig::CONFIG["libdir"] + " -L" + RbConfig::CONFIG["archlibdir"] + " " + RbConfig::CONFIG["LIBRUBYARG_SHARED"]')
21RUBYINSTALL ?= $(DESTDIR)$(shell $(RUBY) -e 'puts RbConfig::CONFIG["vendorarchdir"]')
22
23LIBBASE=$(shell basename $(LIBDIR))
24
25DEFAULT_SEMANAGE_CONF_LOCATION=$(DESTDIR)/etc/selinux/semanage.conf
26
27ifeq ($(DEBUG),1)
28	export CFLAGS = -g3 -O0 -gdwarf-2 -fno-strict-aliasing -Wall -Wshadow -Werror
29	export LDFLAGS = -g
30endif
31
32LEX = flex
33LFLAGS = -s
34YACC = bison
35YFLAGS = -d
36
37VERSION = $(shell cat ../VERSION)
38LIBVERSION = 1
39
40LIBA=libsemanage.a
41TARGET=libsemanage.so
42LIBPC=libsemanage.pc
43SWIGIF= semanageswig_python.i semanageswig_python_exception.i
44SWIGRUBYIF= semanageswig_ruby.i
45SWIGCOUT= semanageswig_wrap.c
46SWIGRUBYCOUT= semanageswig_ruby_wrap.c
47SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
48SWIGRUBYLOBJ:= $(patsubst %.c,$(RUBYPREFIX)%.lo,$(SWIGRUBYCOUT))
49SWIGSO=$(PYPREFIX)_semanage.so
50SWIGFILES=$(SWIGSO) semanage.py
51SWIGRUBYSO=$(RUBYPREFIX)_semanage.so
52LIBSO=$(TARGET).$(LIBVERSION)
53
54GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) semanageswig_python_exception.i $(sort $(wildcard conf-*.[ch]))
55SRCS= $(filter-out $(GENERATED),$(sort $(wildcard *.c)))
56
57OBJS= $(patsubst %.c,%.o,$(SRCS)) conf-scan.o conf-parse.o
58LOBJS= $(patsubst %.c,%.lo,$(SRCS)) conf-scan.lo conf-parse.lo
59CFLAGS ?= -Werror -Wall -W -Wundef -Wshadow -Wmissing-noreturn -Wmissing-format-attribute
60
61SWIG_CFLAGS += -Wno-error -Wno-unused-but-set-variable -Wno-unused-variable -Wno-shadow \
62		-Wno-unused-parameter
63
64override CFLAGS += -I../include -D_GNU_SOURCE
65RANLIB ?= ranlib
66
67SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./
68
69SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./
70
71all: $(LIBA) $(LIBSO) $(LIBPC)
72
73pywrap: all $(SWIGSO)
74
75rubywrap: all $(SWIGRUBYSO)
76
77$(SWIGLOBJ): $(SWIGCOUT)
78	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(PYINC) -fPIC -DSHARED -c -o $@ $<
79
80$(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
81	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
82
83$(SWIGSO): $(SWIGLOBJ)
84	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $< -lsemanage $(PYLIBS)
85
86$(SWIGRUBYSO): $(SWIGRUBYLOBJ)
87	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lsemanage $(RUBYLIBS)
88
89$(LIBA): $(OBJS)
90	$(AR) rcs $@ $^
91	$(RANLIB) $@
92
93$(LIBSO): $(LOBJS)
94	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ -lsepol -laudit -lselinux -lbz2 -Wl,-soname,$(LIBSO),--version-script=libsemanage.map,-z,defs
95	ln -sf $@ $(TARGET)
96
97$(LIBPC): $(LIBPC).in ../VERSION
98	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBBASE):; s:@includedir@:$(INCLUDEDIR):' < $< > $@
99
100semanageswig_python_exception.i: ../include/semanage/semanage.h
101	bash -e exception.sh > $@ || (rm -f $@ ; false)
102
103conf-scan.c: conf-scan.l conf-parse.h
104	$(LEX) $(LFLAGS) -o $@ $<
105
106conf-parse.c: conf-parse.y
107	$(YACC) $(YFLAGS) -o $@ $<
108
109conf-parse.h: conf-parse.c
110
111%.o:  %.c
112	$(CC) $(CFLAGS) -c -o $@ $<
113
114%.lo:  %.c
115	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
116
117conf-parse.o:  conf-parse.c
118	$(CC) $(filter-out -Werror, $(CFLAGS)) -c -o $@ $<
119
120conf-parse.lo:  conf-parse.c
121	$(CC) $(filter-out -Werror, $(CFLAGS)) -fPIC -DSHARED -c -o $@ $<
122
123conf-scan.o:  conf-scan.c
124	$(CC) $(filter-out -Werror, $(CFLAGS)) -c -o $@ $<
125
126conf-scan.lo:  conf-scan.c
127	$(CC) $(filter-out -Werror, $(CFLAGS)) -fPIC -DSHARED -c -o $@ $<
128
129$(SWIGCOUT): $(SWIGIF)
130	$(SWIG) $<
131
132$(SWIGRUBYCOUT): $(SWIGRUBYIF)
133	$(SWIGRUBY) $<
134
135swigify: $(SWIGIF)
136	$(SWIG) $<
137
138install: all
139	test -d $(LIBDIR) || install -m 755 -d $(LIBDIR)
140	install -m 644 $(LIBA) $(LIBDIR)
141	install -m 755 $(LIBSO) $(LIBDIR)
142	test -d $(LIBDIR)/pkgconfig || install -m 755 -d $(LIBDIR)/pkgconfig
143	install -m 644 $(LIBPC) $(LIBDIR)/pkgconfig
144	test -f $(DEFAULT_SEMANAGE_CONF_LOCATION) || install -m 644 -D semanage.conf $(DEFAULT_SEMANAGE_CONF_LOCATION)
145	cd $(LIBDIR) && ln -sf $(LIBSO) $(TARGET)
146
147install-pywrap: pywrap
148	test -d $(PYSITEDIR) || install -m 755 -d $(PYSITEDIR)
149	install -m 755 $(SWIGSO) $(PYSITEDIR)/_semanage$(PYCEXT)
150	install -m 644 semanage.py $(PYSITEDIR)
151
152
153install-rubywrap: rubywrap
154	test -d $(RUBYINSTALL) || install -m 755 -d $(RUBYINSTALL)
155	install -m 755 $(SWIGRUBYSO) $(RUBYINSTALL)/semanage.so
156
157relabel:
158	/sbin/restorecon $(LIBDIR)/$(LIBSO)
159
160clean:
161	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(SWIGLOBJ) $(SWIGSO) $(SWIGRUBYSO) $(TARGET) conf-parse.c conf-parse.h conf-scan.c *.o *.lo *~
162
163distclean: clean
164	rm -f $(GENERATED) $(SWIGFILES)
165
166indent:
167	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
168
169.PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
170