xref: /aosp_15_r20/external/selinux/libsepol/tests/Makefile (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1ENV ?= env
2M4 ?= m4 -E -E
3MKDIR ?= mkdir
4EXE ?= libsepol-tests
5
6CFLAGS += -g3 -gdwarf-2 -O0 \
7	-Werror -Wall -Wextra \
8	-Wfloat-equal \
9	-Wformat=2 \
10	-Winit-self \
11	-Wmissing-format-attribute \
12	-Wmissing-noreturn \
13	-Wmissing-prototypes \
14	-Wnull-dereference \
15	-Wpointer-arith \
16	-Wshadow \
17	-Wstrict-prototypes \
18	-Wundef \
19	-Wunused \
20	-Wwrite-strings \
21	-fno-common
22
23# Statically link libsepol on the assumption that we are going to
24# be testing internal functions.
25LIBSEPOL := ../src/libsepol.a
26
27# In order to load source policies we need to link in the checkpolicy/checkmodule parser and util code.
28# This is less than ideal, but it makes the tests easier to maintain by allowing source policies
29# to be loaded directly.
30CHECKPOLICY := ../../checkpolicy/
31override CPPFLAGS += -I../include/ -I$(CHECKPOLICY)
32
33# test program object files
34objs := $(patsubst %.c,%.o,$(sort $(wildcard *.c)))
35parserobjs := $(CHECKPOLICY)queue.o $(CHECKPOLICY)y.tab.o \
36	$(CHECKPOLICY)parse_util.o $(CHECKPOLICY)lex.yy.o \
37	$(CHECKPOLICY)policy_define.o $(CHECKPOLICY)module_compiler.o
38
39# test policy pieces
40m4support := $(wildcard policies/support/*.spt)
41testsuites := $(wildcard policies/test-*)
42policysrc := $(foreach path,$(testsuites),$(wildcard $(path)/*.conf))
43stdpol := $(addsuffix .std,$(policysrc))
44mlspol := $(addsuffix .mls,$(policysrc))
45policies := $(stdpol) $(mlspol)
46
47all: $(EXE) $(policies)
48policies: $(policies)
49
50$(EXE): $(objs) $(parserobjs) $(LIBSEPOL)
51	$(CC) $(LDFLAGS) $(objs) $(parserobjs) -lcunit $(LIBSEPOL) -o $@
52
53%.conf.std: $(m4support) %.conf
54	$(M4) $(M4PARAMS) $^ > $@
55
56%.conf.mls: $(m4support) %.conf
57	$(M4) $(M4PARAMS) -D enable_mls $^ > $@
58
59clean:
60	rm -f $(objs) $(EXE)
61	rm -f $(policies)
62	rm -f policies/test-downgrade/policy.hi policies/test-downgrade/policy.lo
63
64# mkdir is run in a clean environment created by env -i to avoid failing under ASan with:
65#
66#   ASan runtime does not come first in initial library list;
67#   you should either link runtime to your application or manually preload it with LD_PRELOAD
68#
69# when the source code is built with ASan
70test: $(EXE) $(policies)
71	$(ENV) -i $(MKDIR) -p policies/test-downgrade
72	../../checkpolicy/checkpolicy -M policies/test-cond/refpolicy-base.conf -o policies/test-downgrade/policy.hi
73	./$(EXE)
74
75.PHONY: all policies clean test
76