xref: /aosp_15_r20/external/selinux/libselinux/src/Makefile (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
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 ?= python3
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 ?= /usr
12LIBDIR ?= $(PREFIX)/lib
13SHLIBDIR ?= /lib
14INCLUDEDIR ?= $(PREFIX)/include
15PYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
16PYLIBS ?= $(shell $(PKG_CONFIG) --libs $(PYPREFIX))
17PYTHONLIBDIR ?= $(shell $(PYTHON) -c "import sysconfig; print(sysconfig.get_path('platlib', vars={'platbase': '$(PREFIX)', 'base': '$(PREFIX)'}))")
18PYCEXT ?= $(shell $(PYTHON) -c 'import importlib.machinery;print(importlib.machinery.EXTENSION_SUFFIXES[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 ?= $(shell $(RUBY) -e 'puts RbConfig::CONFIG["vendorarchdir"]')
22
23VERSION = $(shell cat ../VERSION)
24LIBVERSION = 1
25
26OS ?= $(shell uname)
27
28ifeq ($(shell $(CC) -v 2>&1 | grep "clang"),)
29COMPILER ?= gcc
30else
31COMPILER ?= clang
32endif
33
34LIBA=libselinux.a
35TARGET=libselinux.so
36LIBPC=libselinux.pc
37SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
38SWIGRUBYIF= selinuxswig_ruby.i
39SWIGCOUT= selinuxswig_python_wrap.c
40SWIGPYOUT= selinux.py
41SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
42SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
43SWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT))
44SWIGSO=$(PYPREFIX)_selinux.so
45SWIGFILES=$(SWIGSO) $(SWIGPYOUT)
46SWIGRUBYSO=$(RUBYPREFIX)_selinux.so
47LIBSO=$(TARGET).$(LIBVERSION)
48AUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
49AUDIT2WHYSO=$(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
54ifeq ($(LIBSEPOLA),)
55        LDLIBS_LIBSEPOLA := -l:libsepol.a
56endif
57
58GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) $(SWIGCOUT) selinuxswig_python_exception.i
59SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
60
61MAX_STACK_SIZE=32768
62
63ifeq ($(COMPILER), gcc)
64EXTRA_CFLAGS = -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 -fno-semantic-interposition
69else
70EXTRA_CFLAGS = -Wunused-command-line-argument
71endif
72
73OBJS= $(patsubst %.c,%.o,$(SRCS))
74LOBJS= $(patsubst %.c,%.lo,$(SRCS))
75CFLAGS ?= -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          -Wframe-larger-than=$(MAX_STACK_SIZE) \
87          -fstack-protector-all --param=ssp-buffer-size=4 -fexceptions \
88          -fasynchronous-unwind-tables -fdiagnostics-show-option \
89          -Werror -Wno-aggregate-return \
90          $(EXTRA_CFLAGS)
91
92LD_SONAME_FLAGS=-soname,$(LIBSO),--version-script=libselinux.map,-z,defs,-z,relro
93
94ifeq ($(OS), Darwin)
95override CFLAGS += -I/opt/local/include
96override LDFLAGS += -L/opt/local/lib -undefined dynamic_lookup
97LD_SONAME_FLAGS=-install_name,$(LIBSO)
98endif
99
100# override with -lfts when building on Musl libc to use fts-standalone
101FTS_LDLIBS ?=
102
103override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
104
105# check for strlcpy(3) availability
106H := \#
107ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char d[2];const char *s="a";return (size_t)strlcpy(d,s,sizeof(d))>=sizeof(d);}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
108override CFLAGS += -DHAVE_STRLCPY
109endif
110
111# check for reallocarray(3) availability
112H := \#
113ifeq (yes,$(shell printf '${H}include <stdlib.h>\nint main(void){return reallocarray(NULL,0,0)==NULL;}' | $(CC) $(CFLAGS) $(LDFLAGS) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
114override CFLAGS += -DHAVE_REALLOCARRAY
115endif
116
117SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
118		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
119		-Wno-deprecated-declarations
120
121RANLIB ?= ranlib
122
123ARCH := $(patsubst i%86,i386,$(shell uname -m))
124ifneq (,$(filter i386,$(ARCH)))
125TLSFLAGS += -mno-tls-direct-seg-refs
126endif
127
128ifeq ($(ANDROID_HOST),y)
129DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
130	-DBUILD_HOST
131SRCS= callbacks.c freecon.c label.c label_file.c \
132	label_backends_android.c regex.c label_support.c \
133	matchpathcon.c setrans_client.c sha1.c booleans.c
134LABEL_BACKEND_ANDROID=y
135endif
136
137ifneq ($(LABEL_BACKEND_ANDROID),y)
138SRCS:= $(filter-out label_backends_android.c, $(SRCS))
139DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
140endif
141
142ifeq ($(DISABLE_X11),y)
143SRCS:= $(filter-out label_x.c, $(SRCS))
144endif
145
146SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
147
148all: $(LIBA) $(LIBSO) $(LIBPC)
149
150pywrap: all selinuxswig_python_exception.i
151	CFLAGS="$(CPPFLAGS) $(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext
152
153rubywrap: all $(SWIGRUBYSO)
154
155$(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
156	$(CC) $(CPPFLAGS) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
157
158$(SWIGRUBYSO): $(SWIGRUBYLOBJ)
159	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
160
161$(LIBA): $(OBJS)
162	$(AR) rcs $@ $^
163	$(RANLIB) $@
164
165$(LIBSO): $(LOBJS)
166	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ $(PCRE_LDLIBS) $(FTS_LDLIBS) -ldl -Wl,$(LD_SONAME_FLAGS)
167	ln -sf $@ $(TARGET)
168
169$(LIBPC): $(LIBPC).in ../VERSION
170	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBDIR):; s:@includedir@:$(INCLUDEDIR):; s:@PCRE_MODULE@:$(PCRE_MODULE):' < $< > $@
171
172selinuxswig_python_exception.i: exception.sh ../include/selinux/selinux.h
173	bash -e exception.sh > $@ || (rm -f $@ ; false)
174
175%.o:  %.c policy.h
176	$(CC) $(CPPFLAGS) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
177
178%.lo:  %.c policy.h
179	$(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
180
181$(SWIGRUBYCOUT): $(SWIGRUBYIF)
182	$(SWIGRUBY) $<
183
184install: all
185	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
186	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
187	test -d $(DESTDIR)$(SHLIBDIR) || install -m 755 -d $(DESTDIR)$(SHLIBDIR)
188	install -m 755 $(LIBSO) $(DESTDIR)$(SHLIBDIR)
189	test -d $(DESTDIR)$(LIBDIR)/pkgconfig || install -m 755 -d $(DESTDIR)$(LIBDIR)/pkgconfig
190	install -m 644 $(LIBPC) $(DESTDIR)$(LIBDIR)/pkgconfig
191	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
192
193install-pywrap: pywrap
194	CFLAGS="$(CPPFLAGS) $(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) -m pip install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR) --ignore-installed --no-deps` $(PYTHON_SETUP_ARGS) .
195	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
196	ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
197
198install-rubywrap: rubywrap
199	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL)
200	install -m 755 $(SWIGRUBYSO) $(DESTDIR)$(RUBYINSTALL)/selinux.so
201
202relabel:
203	/sbin/restorecon $(DESTDIR)$(SHLIBDIR)/$(LIBSO)
204
205clean-pywrap:
206	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
207	$(PYTHON) setup.py clean
208	-rm -rf build *~ \#* *pyc .#* selinux.egg-info/
209
210clean-rubywrap:
211	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
212
213clean: clean-pywrap clean-rubywrap
214	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
215
216distclean: clean
217	rm -f $(GENERATED) $(SWIGFILES)
218
219indent:
220	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
221
222.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
223