blob: dd3fcc38705105e61498a58e78ffd771717f64b6 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001# Common makefile rules for tests
2#
3# Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
4#
5# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6
7ifeq ($(TESTS),)
8TESTS := $(patsubst %.c,%,$(wildcard *.c))
9endif
10ifneq ($(TESTS_DISABLED),)
11TESTS := $(filter-out $(TESTS_DISABLED),$(TESTS))
12endif
13ifeq ($(SHELL_TESTS),)
14SHELL_TESTS := $(patsubst %.sh,shell_%,$(wildcard *.sh))
15endif
16
17ifneq ($(filter-out test,$(strip $(TESTS))),$(strip $(TESTS)))
18$(error Sanity check: cannot have a test named "test.c")
19endif
20
21U_TARGETS := $(TESTS)
22G_TARGETS := $(addsuffix _glibc,$(U_TARGETS))
23
24ifneq ($(GLIBC_TESTS_DISABLED),)
25G_TARGETS := $(filter-out $(GLIBC_TESTS_DISABLED),$(G_TARGETS))
26endif
27
28ifeq ($(GLIBC_ONLY),)
29TARGETS += $(U_TARGETS)
30endif
31ifeq ($(UCLIBC_ONLY),)
32TARGETS += $(G_TARGETS)
33endif
34
35CLEAN_TARGETS := $(U_TARGETS) $(G_TARGETS)
36CLEAN_TARGETS += $(TESTS_DISABLED) $(addsuffix _glibc,$(TESTS_DISABLED)) $(GLIBC_TESTS_DISABLED)
37COMPILE_TARGETS := $(TARGETS)
38RUN_TARGETS := $(addsuffix .exe,$(TARGETS))
39# provide build rules even for disabled tests:
40U_TARGETS += $(TESTS_DISABLED)
41G_TARGETS += $(addsuffix _glibc,$(TESTS_DISABLED)) $(GLIBC_TESTS_DISABLED)
42TARGETS += $(SHELL_TESTS)
43CFLAGS += $(CFLAGS_$(notdir $(CURDIR)))
44
45define binary_name
46$(patsubst %.exe,%,$@)
47endef
48define tst_src_name
49$(patsubst %_glibc,%,$(binary_name))
50endef
51
52define diff_test
53 $(Q)\
54 for x in "$(binary_name).out" "$(tst_src_name).out" ; do \
55 test -e "$$x.good" && $(do_showdiff) "$(binary_name).out" "$$x.good" && exec diff -u "$(binary_name).out" "$$x.good" ; \
56 done ; \
57 true
58endef
59define uclibc_glibc_diff_test
60 $(Q)\
61 test -z "$(DODIFF_$(tst_src_name))" && exec true ; \
62 uclibc_out="$(binary_name).out" ; \
63 glibc_out="$(tst_src_name).out" ; \
64 $(do_showdiff) $$uclibc_out $$glibc_out ; \
65 exec diff -u "$$uclibc_out" "$$glibc_out"
66endef
67define exec_test
68 $(showtest)
69 $(Q)\
70 $(WRAPPER) $(WRAPPER_$(tst_src_name)) \
71 ./$(binary_name) $(OPTS) $(OPTS_$(tst_src_name)) > "$(binary_name).out" 2>&1 ; \
72 ret=$$? ; \
73 expected_ret="$(RET_$(tst_src_name))" ; \
74 test -z "$$expected_ret" && export expected_ret=0 ; \
75 if ! test $$ret -eq $$expected_ret ; then \
76 echo "ret == $$ret ; expected_ret == $$expected_ret" ; \
77 echo "The output of failed test is:"; \
78 cat "$(binary_name).out"; \
79 exit 1 ; \
80 fi
81 $(SCAT) "$(binary_name).out"
82endef
83
84test check all: run
85run: $(RUN_TARGETS)
86$(RUN_TARGETS):
87 $(exec_test)
88 $(diff_test)
89ifeq ($(UCLIBC_ONLY),)
90 $(uclibc_glibc_diff_test)
91endif
92
93compile: $(COMPILE_TARGETS)
94
95G_TARGET_SRCS := $(addsuffix .c,$(G_TARGETS))
96U_TARGET_SRCS := $(addsuffix .c,$(U_TARGETS))
97
98$(MAKE_SRCS): Makefile $(TESTDIR)Makefile $(TESTDIR)Rules.mak $(TESTDIR)Test.mak
99
100$(U_TARGETS): $(U_TARGET_SRCS) $(MAKE_SRCS)
101 $(showlink)
102 $(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(notdir $(CURDIR))) $(CFLAGS_$@) -c $@.c -o $@.o
103 $(Q)$(CC) $(LDFLAGS) $@.o -o $@ $(EXTRA_LDFLAGS) $(LDFLAGS_$@)
104
105$(G_TARGETS): $(U_TARGET_SRCS) $(MAKE_SRCS)
106 $(showlink)
107 $(Q)$(HOSTCC) $(HOST_CFLAGS) $(CFLAGS_$(notdir $(CURDIR))) $(CFLAGS_$(patsubst %_glibc,%,$@)) -c $(patsubst %_glibc,%,$@).c -o $@.o
108 $(Q)$(HOSTCC) $(HOST_LDFLAGS) $@.o -o $@ $(EXTRA_LDFLAGS) $(LDFLAGS_$(patsubst %_glibc,%,$@))
109
110
111shell_%:
112 $(showtest)
113 $(Q)$(if $(PRE_RUN_$(@)),$(PRE_RUN_$(@)))
114 $(Q)$(SHELL) $(patsubst shell_%,%.sh,$(binary_name))
115 $(Q)$(if $(POST_RUN_$(@)),$(POST_RUN_$(@)))
116
117%.so: %.c
118 $(showlink)
119 $(Q)$(CC) \
120 $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(patsubst %_glibc,%,$@)) \
121 -fPIC -shared $< -o $@ -Wl,-soname,$@ \
122 $(LDFLAGS) $(EXTRA_LIBS) $(LDFLAGS_$(patsubst %_glibc,%,$@))
123
124clean:
125 $(showclean)
126 $(Q)$(RM) *.a *.o *.so *~ core *.out *.gdb $(CLEAN_TARGETS) $(EXTRA_CLEAN)
127 $(Q)$(RM_R) $(EXTRA_DIRS)
128
129.PHONY: all check clean test run compile