yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | ifeq ($(TESTS),) |
| 8 | TESTS := $(patsubst %.c,%,$(wildcard *.c)) |
| 9 | endif |
| 10 | ifneq ($(TESTS_DISABLED),) |
| 11 | TESTS := $(filter-out $(TESTS_DISABLED),$(TESTS)) |
| 12 | endif |
| 13 | ifeq ($(SHELL_TESTS),) |
| 14 | SHELL_TESTS := $(patsubst %.sh,shell_%,$(wildcard *.sh)) |
| 15 | endif |
| 16 | |
| 17 | ifneq ($(filter-out test,$(strip $(TESTS))),$(strip $(TESTS))) |
| 18 | $(error Sanity check: cannot have a test named "test.c") |
| 19 | endif |
| 20 | |
| 21 | U_TARGETS := $(TESTS) |
| 22 | G_TARGETS := $(addsuffix _glibc,$(U_TARGETS)) |
| 23 | |
| 24 | ifneq ($(GLIBC_TESTS_DISABLED),) |
| 25 | G_TARGETS := $(filter-out $(GLIBC_TESTS_DISABLED),$(G_TARGETS)) |
| 26 | endif |
| 27 | |
| 28 | ifeq ($(GLIBC_ONLY),) |
| 29 | TARGETS += $(U_TARGETS) |
| 30 | endif |
| 31 | ifeq ($(UCLIBC_ONLY),) |
| 32 | TARGETS += $(G_TARGETS) |
| 33 | endif |
| 34 | |
| 35 | CLEAN_TARGETS := $(U_TARGETS) $(G_TARGETS) |
| 36 | CLEAN_TARGETS += $(TESTS_DISABLED) $(addsuffix _glibc,$(TESTS_DISABLED)) $(GLIBC_TESTS_DISABLED) |
| 37 | COMPILE_TARGETS := $(TARGETS) |
| 38 | RUN_TARGETS := $(addsuffix .exe,$(TARGETS)) |
| 39 | # provide build rules even for disabled tests: |
| 40 | U_TARGETS += $(TESTS_DISABLED) |
| 41 | G_TARGETS += $(addsuffix _glibc,$(TESTS_DISABLED)) $(GLIBC_TESTS_DISABLED) |
| 42 | TARGETS += $(SHELL_TESTS) |
| 43 | CFLAGS += $(CFLAGS_$(notdir $(CURDIR))) |
| 44 | |
| 45 | define binary_name |
| 46 | $(patsubst %.exe,%,$@) |
| 47 | endef |
| 48 | define tst_src_name |
| 49 | $(patsubst %_glibc,%,$(binary_name)) |
| 50 | endef |
| 51 | |
| 52 | define 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 |
| 58 | endef |
| 59 | define 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" |
| 66 | endef |
| 67 | define 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" |
| 82 | endef |
| 83 | |
| 84 | test check all: run |
| 85 | run: $(RUN_TARGETS) |
| 86 | $(RUN_TARGETS): |
| 87 | $(exec_test) |
| 88 | $(diff_test) |
| 89 | ifeq ($(UCLIBC_ONLY),) |
| 90 | $(uclibc_glibc_diff_test) |
| 91 | endif |
| 92 | |
| 93 | compile: $(COMPILE_TARGETS) |
| 94 | |
| 95 | G_TARGET_SRCS := $(addsuffix .c,$(G_TARGETS)) |
| 96 | U_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 | |
| 111 | shell_%: |
| 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 | |
| 124 | clean: |
| 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 |