16cd6a6acSopenharmony_ci# Support building the Python bindings multiple times, against various Python
26cd6a6acSopenharmony_ci# runtimes (e.g. Python 2 vs Python 3) by optionally prefixing the build
36cd6a6acSopenharmony_ci# targets with "PYPREFIX":
46cd6a6acSopenharmony_ciPYTHON ?= python3
56cd6a6acSopenharmony_ciPYPREFIX ?= $(shell $(PYTHON) -c 'import sys;print("python-%d.%d" % sys.version_info[:2])')
66cd6a6acSopenharmony_ciRUBY ?= ruby
76cd6a6acSopenharmony_ciRUBYPREFIX ?= $(notdir $(RUBY))
86cd6a6acSopenharmony_ciPKG_CONFIG ?= pkg-config
96cd6a6acSopenharmony_ci
106cd6a6acSopenharmony_ci# Installation directories.
116cd6a6acSopenharmony_ciPREFIX ?= /usr
126cd6a6acSopenharmony_ciLIBDIR ?= $(PREFIX)/lib
136cd6a6acSopenharmony_ciSHLIBDIR ?= /lib
146cd6a6acSopenharmony_ciINCLUDEDIR ?= $(PREFIX)/include
156cd6a6acSopenharmony_ciPYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
166cd6a6acSopenharmony_ciPYLIBS ?= $(shell $(PKG_CONFIG) --libs $(PYPREFIX))
176cd6a6acSopenharmony_ciPYTHONLIBDIR ?= $(shell $(PYTHON) -c "import sysconfig; print(sysconfig.get_path('platlib', vars={'platbase': '$(PREFIX)', 'base': '$(PREFIX)'}))")
186cd6a6acSopenharmony_ciPYCEXT ?= $(shell $(PYTHON) -c 'import importlib.machinery;print(importlib.machinery.EXTENSION_SUFFIXES[0])')
196cd6a6acSopenharmony_ciRUBYINC ?= $(shell $(RUBY) -e 'puts "-I" + RbConfig::CONFIG["rubyarchhdrdir"] + " -I" + RbConfig::CONFIG["rubyhdrdir"]')
206cd6a6acSopenharmony_ciRUBYLIBS ?= $(shell $(RUBY) -e 'puts "-L" + RbConfig::CONFIG["libdir"] + " -L" + RbConfig::CONFIG["archlibdir"] + " " + RbConfig::CONFIG["LIBRUBYARG_SHARED"]')
216cd6a6acSopenharmony_ciRUBYINSTALL ?= $(shell $(RUBY) -e 'puts RbConfig::CONFIG["vendorarchdir"]')
226cd6a6acSopenharmony_ci
236cd6a6acSopenharmony_ciVERSION = $(shell cat ../VERSION)
246cd6a6acSopenharmony_ciLIBVERSION = 1
256cd6a6acSopenharmony_ci
266cd6a6acSopenharmony_ciOS ?= $(shell uname)
276cd6a6acSopenharmony_ci
286cd6a6acSopenharmony_ciifeq ($(shell $(CC) -v 2>&1 | grep "clang"),)
296cd6a6acSopenharmony_ciCOMPILER ?= gcc
306cd6a6acSopenharmony_cielse
316cd6a6acSopenharmony_ciCOMPILER ?= clang
326cd6a6acSopenharmony_ciendif
336cd6a6acSopenharmony_ci
346cd6a6acSopenharmony_ciLIBA=libselinux.a 
356cd6a6acSopenharmony_ciTARGET=libselinux.so
366cd6a6acSopenharmony_ciLIBPC=libselinux.pc
376cd6a6acSopenharmony_ciSWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
386cd6a6acSopenharmony_ciSWIGRUBYIF= selinuxswig_ruby.i
396cd6a6acSopenharmony_ciSWIGCOUT= selinuxswig_python_wrap.c
406cd6a6acSopenharmony_ciSWIGPYOUT= selinux.py
416cd6a6acSopenharmony_ciSWIGRUBYCOUT= selinuxswig_ruby_wrap.c
426cd6a6acSopenharmony_ciSWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
436cd6a6acSopenharmony_ciSWIGRUBYLOBJ:= $(patsubst %.c,%.lo,$(SWIGRUBYCOUT)) 
446cd6a6acSopenharmony_ciSWIGSO=$(PYPREFIX)_selinux.so
456cd6a6acSopenharmony_ciSWIGFILES=$(SWIGSO) $(SWIGPYOUT)
466cd6a6acSopenharmony_ciSWIGRUBYSO=$(RUBYPREFIX)_selinux.so
476cd6a6acSopenharmony_ciLIBSO=$(TARGET).$(LIBVERSION)
486cd6a6acSopenharmony_ciAUDIT2WHYLOBJ=$(PYPREFIX)audit2why.lo
496cd6a6acSopenharmony_ciAUDIT2WHYSO=$(PYPREFIX)audit2why.so
506cd6a6acSopenharmony_ci
516cd6a6acSopenharmony_ci# If no specific libsepol.a is specified, fall back on LDFLAGS search path
526cd6a6acSopenharmony_ci# Otherwise, as $(LIBSEPOLA) already appears in the dependencies, there
536cd6a6acSopenharmony_ci# is no need to define a value for LDLIBS_LIBSEPOLA
546cd6a6acSopenharmony_ciifeq ($(LIBSEPOLA),)
556cd6a6acSopenharmony_ci        LDLIBS_LIBSEPOLA := -l:libsepol.a
566cd6a6acSopenharmony_ciendif
576cd6a6acSopenharmony_ci
586cd6a6acSopenharmony_ciGENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) $(SWIGCOUT) selinuxswig_python_exception.i
596cd6a6acSopenharmony_ciSRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
606cd6a6acSopenharmony_ci
616cd6a6acSopenharmony_ciMAX_STACK_SIZE=32768
626cd6a6acSopenharmony_ci
636cd6a6acSopenharmony_ciifeq ($(COMPILER), gcc)
646cd6a6acSopenharmony_ciEXTRA_CFLAGS = -fipa-pure-const -Wlogical-op -Wpacked-bitfield-compat -Wsync-nand \
656cd6a6acSopenharmony_ci	-Wcoverage-mismatch -Wcpp -Wformat-contains-nul -Wnormalized=nfc -Wsuggest-attribute=const \
666cd6a6acSopenharmony_ci	-Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines -Wjump-misses-init \
676cd6a6acSopenharmony_ci	-Wno-suggest-attribute=pure -Wno-suggest-attribute=const -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
686cd6a6acSopenharmony_ci	-Wstrict-overflow=5 -fno-semantic-interposition
696cd6a6acSopenharmony_cielse
706cd6a6acSopenharmony_ciEXTRA_CFLAGS = -Wunused-command-line-argument
716cd6a6acSopenharmony_ciendif
726cd6a6acSopenharmony_ci
736cd6a6acSopenharmony_ciOBJS= $(patsubst %.c,%.o,$(SRCS))
746cd6a6acSopenharmony_ciLOBJS= $(patsubst %.c,%.lo,$(SRCS))
756cd6a6acSopenharmony_ciCFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissing-include-dirs \
766cd6a6acSopenharmony_ci          -Wunused -Wunknown-pragmas -Wstrict-aliasing -Wshadow -Wpointer-arith \
776cd6a6acSopenharmony_ci          -Wbad-function-cast -Wcast-align -Wwrite-strings -Waggregate-return \
786cd6a6acSopenharmony_ci          -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes \
796cd6a6acSopenharmony_ci          -Wmissing-declarations -Wmissing-noreturn -Wmissing-format-attribute \
806cd6a6acSopenharmony_ci          -Wredundant-decls -Wnested-externs -Winline -Winvalid-pch -Wvolatile-register-var \
816cd6a6acSopenharmony_ci          -Wdisabled-optimization -Wbuiltin-macro-redefined \
826cd6a6acSopenharmony_ci          -Wattributes -Wmultichar \
836cd6a6acSopenharmony_ci          -Wdeprecated-declarations -Wdiv-by-zero -Wdouble-promotion -Wendif-labels -Wextra \
846cd6a6acSopenharmony_ci          -Wformat-extra-args -Wformat-zero-length -Wformat=2 -Wmultichar \
856cd6a6acSopenharmony_ci          -Woverflow -Wpointer-to-int-cast -Wpragmas \
866cd6a6acSopenharmony_ci          -Wno-missing-field-initializers -Wno-sign-compare \
876cd6a6acSopenharmony_ci          -Wno-format-nonliteral -Wframe-larger-than=$(MAX_STACK_SIZE) \
886cd6a6acSopenharmony_ci          -fstack-protector-all --param=ssp-buffer-size=4 -fexceptions \
896cd6a6acSopenharmony_ci          -fasynchronous-unwind-tables -fdiagnostics-show-option \
906cd6a6acSopenharmony_ci          -Werror -Wno-aggregate-return -Wno-redundant-decls \
916cd6a6acSopenharmony_ci          $(EXTRA_CFLAGS)
926cd6a6acSopenharmony_ci
936cd6a6acSopenharmony_ciLD_SONAME_FLAGS=-soname,$(LIBSO),--version-script=libselinux.map,-z,defs,-z,relro
946cd6a6acSopenharmony_ci
956cd6a6acSopenharmony_ciifeq ($(OS), Darwin)
966cd6a6acSopenharmony_cioverride CFLAGS += -I/opt/local/include
976cd6a6acSopenharmony_cioverride LDFLAGS += -L/opt/local/lib -undefined dynamic_lookup
986cd6a6acSopenharmony_ciLD_SONAME_FLAGS=-install_name,$(LIBSO)
996cd6a6acSopenharmony_ciendif
1006cd6a6acSopenharmony_ci
1016cd6a6acSopenharmony_ci# override with -lfts when building on Musl libc to use fts-standalone
1026cd6a6acSopenharmony_ciFTS_LDLIBS ?=
1036cd6a6acSopenharmony_ci
1046cd6a6acSopenharmony_cioverride CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
1056cd6a6acSopenharmony_ci
1066cd6a6acSopenharmony_ci# check for strlcpy(3) availability
1076cd6a6acSopenharmony_ciH := \#
1086cd6a6acSopenharmony_ciifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
1096cd6a6acSopenharmony_cioverride CFLAGS += -DHAVE_STRLCPY
1106cd6a6acSopenharmony_ciendif
1116cd6a6acSopenharmony_ci
1126cd6a6acSopenharmony_ciSWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
1136cd6a6acSopenharmony_ci		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
1146cd6a6acSopenharmony_ci		-Wno-deprecated-declarations
1156cd6a6acSopenharmony_ci
1166cd6a6acSopenharmony_ciRANLIB ?= ranlib
1176cd6a6acSopenharmony_ci
1186cd6a6acSopenharmony_ciARCH := $(patsubst i%86,i386,$(shell uname -m))
1196cd6a6acSopenharmony_ciifneq (,$(filter i386,$(ARCH)))
1206cd6a6acSopenharmony_ciTLSFLAGS += -mno-tls-direct-seg-refs
1216cd6a6acSopenharmony_ciendif
1226cd6a6acSopenharmony_ci
1236cd6a6acSopenharmony_ciifeq ($(ANDROID_HOST),y)
1246cd6a6acSopenharmony_ciDISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
1256cd6a6acSopenharmony_ci	-DBUILD_HOST
1266cd6a6acSopenharmony_ciSRCS= callbacks.c freecon.c label.c label_file.c \
1276cd6a6acSopenharmony_ci	label_backends_android.c regex.c label_support.c \
1286cd6a6acSopenharmony_ci	matchpathcon.c setrans_client.c sha1.c booleans.c
1296cd6a6acSopenharmony_cielse
1306cd6a6acSopenharmony_ciLABEL_BACKEND_ANDROID=y
1316cd6a6acSopenharmony_ciendif
1326cd6a6acSopenharmony_ci
1336cd6a6acSopenharmony_ciifneq ($(LABEL_BACKEND_ANDROIDT),y)
1346cd6a6acSopenharmony_ciSRCS:= $(filter-out label_backends_android.c, $(SRCS))
1356cd6a6acSopenharmony_ciDISABLE_FLAGS+= -DNO_ANDROID_BACKEND
1366cd6a6acSopenharmony_ciendif
1376cd6a6acSopenharmony_ci
1386cd6a6acSopenharmony_ciifeq ($(DISABLE_X11),y)
1396cd6a6acSopenharmony_ciSRCS:= $(filter-out label_x.c, $(SRCS))
1406cd6a6acSopenharmony_ciendif
1416cd6a6acSopenharmony_ci
1426cd6a6acSopenharmony_ciSWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
1436cd6a6acSopenharmony_ci
1446cd6a6acSopenharmony_ciall: $(LIBA) $(LIBSO) $(LIBPC)
1456cd6a6acSopenharmony_ci
1466cd6a6acSopenharmony_cipywrap: all selinuxswig_python_exception.i
1476cd6a6acSopenharmony_ci	CFLAGS="$(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext
1486cd6a6acSopenharmony_ci
1496cd6a6acSopenharmony_cirubywrap: all $(SWIGRUBYSO)
1506cd6a6acSopenharmony_ci
1516cd6a6acSopenharmony_ci$(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
1526cd6a6acSopenharmony_ci	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
1536cd6a6acSopenharmony_ci
1546cd6a6acSopenharmony_ci$(SWIGRUBYSO): $(SWIGRUBYLOBJ)
1556cd6a6acSopenharmony_ci	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
1566cd6a6acSopenharmony_ci
1576cd6a6acSopenharmony_ci$(LIBA): $(OBJS)
1586cd6a6acSopenharmony_ci	$(AR) rcs $@ $^
1596cd6a6acSopenharmony_ci	$(RANLIB) $@
1606cd6a6acSopenharmony_ci
1616cd6a6acSopenharmony_ci$(LIBSO): $(LOBJS)
1626cd6a6acSopenharmony_ci	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ $(PCRE_LDLIBS) $(FTS_LDLIBS) -ldl -Wl,$(LD_SONAME_FLAGS)
1636cd6a6acSopenharmony_ci	ln -sf $@ $(TARGET)
1646cd6a6acSopenharmony_ci
1656cd6a6acSopenharmony_ci$(LIBPC): $(LIBPC).in ../VERSION
1666cd6a6acSopenharmony_ci	sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBDIR):; s:@includedir@:$(INCLUDEDIR):; s:@PCRE_MODULE@:$(PCRE_MODULE):' < $< > $@
1676cd6a6acSopenharmony_ci
1686cd6a6acSopenharmony_ciselinuxswig_python_exception.i: exception.sh ../include/selinux/selinux.h
1696cd6a6acSopenharmony_ci	bash -e exception.sh > $@ || (rm -f $@ ; false)
1706cd6a6acSopenharmony_ci
1716cd6a6acSopenharmony_ci%.o:  %.c policy.h
1726cd6a6acSopenharmony_ci	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
1736cd6a6acSopenharmony_ci
1746cd6a6acSopenharmony_ci%.lo:  %.c policy.h
1756cd6a6acSopenharmony_ci	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
1766cd6a6acSopenharmony_ci
1776cd6a6acSopenharmony_ci$(SWIGRUBYCOUT): $(SWIGRUBYIF)
1786cd6a6acSopenharmony_ci	$(SWIGRUBY) $<
1796cd6a6acSopenharmony_ci
1806cd6a6acSopenharmony_ciinstall: all 
1816cd6a6acSopenharmony_ci	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
1826cd6a6acSopenharmony_ci	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
1836cd6a6acSopenharmony_ci	test -d $(DESTDIR)$(SHLIBDIR) || install -m 755 -d $(DESTDIR)$(SHLIBDIR)
1846cd6a6acSopenharmony_ci	install -m 755 $(LIBSO) $(DESTDIR)$(SHLIBDIR)
1856cd6a6acSopenharmony_ci	test -d $(DESTDIR)$(LIBDIR)/pkgconfig || install -m 755 -d $(DESTDIR)$(LIBDIR)/pkgconfig
1866cd6a6acSopenharmony_ci	install -m 644 $(LIBPC) $(DESTDIR)$(LIBDIR)/pkgconfig
1876cd6a6acSopenharmony_ci	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
1886cd6a6acSopenharmony_ci
1896cd6a6acSopenharmony_ciinstall-pywrap: pywrap
1906cd6a6acSopenharmony_ci	$(PYTHON) -m pip install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR) --ignore-installed --no-deps` $(PYTHON_SETUP_ARGS) .
1916cd6a6acSopenharmony_ci	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
1926cd6a6acSopenharmony_ci	ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
1936cd6a6acSopenharmony_ci
1946cd6a6acSopenharmony_ciinstall-rubywrap: rubywrap
1956cd6a6acSopenharmony_ci	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL) 
1966cd6a6acSopenharmony_ci	install -m 755 $(SWIGRUBYSO) $(DESTDIR)$(RUBYINSTALL)/selinux.so
1976cd6a6acSopenharmony_ci
1986cd6a6acSopenharmony_cirelabel:
1996cd6a6acSopenharmony_ci	/sbin/restorecon $(DESTDIR)$(SHLIBDIR)/$(LIBSO)
2006cd6a6acSopenharmony_ci
2016cd6a6acSopenharmony_ciclean-pywrap:
2026cd6a6acSopenharmony_ci	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
2036cd6a6acSopenharmony_ci	$(PYTHON) setup.py clean
2046cd6a6acSopenharmony_ci	-rm -rf build *~ \#* *pyc .#* selinux.egg-info/
2056cd6a6acSopenharmony_ci
2066cd6a6acSopenharmony_ciclean-rubywrap:
2076cd6a6acSopenharmony_ci	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
2086cd6a6acSopenharmony_ci
2096cd6a6acSopenharmony_ciclean: clean-pywrap clean-rubywrap
2106cd6a6acSopenharmony_ci	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
2116cd6a6acSopenharmony_ci
2126cd6a6acSopenharmony_cidistclean: clean
2136cd6a6acSopenharmony_ci	rm -f $(GENERATED) $(SWIGFILES)
2146cd6a6acSopenharmony_ci
2156cd6a6acSopenharmony_ciindent:
2166cd6a6acSopenharmony_ci	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
2176cd6a6acSopenharmony_ci
2186cd6a6acSopenharmony_ci.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
219