]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - tools/testing/selftests/lib.mk
selftests: lib.mk: add TEST_CUSTOM_PROGS to allow custom test run/install
[mirror_ubuntu-eoan-kernel.git] / tools / testing / selftests / lib.mk
1 # This mimics the top-level Makefile. We do it explicitly here so that this
2 # Makefile can operate with or without the kbuild infrastructure.
3 CC := $(CROSS_COMPILE)gcc
4
5 ifeq (0,$(MAKELEVEL))
6 OUTPUT := $(shell pwd)
7 endif
8
9 # The following are built by lib.mk common compile rules.
10 # TEST_CUSTOM_PROGS should be used by tests that require
11 # custom build rule and prevent common build rule use.
12 # TEST_PROGS are for test shell scripts.
13 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
14 # and install targets. Common clean doesn't touch them.
15 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
16 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
17 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
18
19 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
20
21 .ONESHELL:
22 define RUN_TESTS
23 @test_num=`echo 0`;
24 @echo "TAP version 13";
25 @for TEST in $(1); do \
26 BASENAME_TEST=`basename $$TEST`; \
27 test_num=`echo $$test_num+1 | bc`; \
28 echo "selftests: $$BASENAME_TEST"; \
29 echo "========================================"; \
30 if [ ! -x $$TEST ]; then \
31 echo "selftests: Warning: file $$BASENAME_TEST is not executable, correct this.";\
32 echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; \
33 else \
34 cd `dirname $$TEST` > /dev/null; (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && echo "ok 1..$$test_num selftests: $$BASENAME_TEST [PASS]") || echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; cd - > /dev/null;\
35 fi; \
36 done;
37 endef
38
39 run_tests: all
40 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
41
42 define INSTALL_RULE
43 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
44 mkdir -p ${INSTALL_PATH}; \
45 echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/"; \
46 rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/; \
47 fi
48 @if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then \
49 mkdir -p ${INSTALL_PATH}; \
50 echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/"; \
51 rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/; \
52 fi
53 endef
54
55 install: all
56 ifdef INSTALL_PATH
57 $(INSTALL_RULE)
58 else
59 $(error Error: set INSTALL_PATH to use install)
60 endif
61
62 define EMIT_TESTS
63 @for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
64 BASENAME_TEST=`basename $$TEST`; \
65 echo "(./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && echo \"selftests: $$BASENAME_TEST [PASS]\") || echo \"selftests: $$BASENAME_TEST [FAIL]\""; \
66 done;
67 endef
68
69 emit_tests:
70 $(EMIT_TESTS)
71
72 # define if isn't already. It is undefined in make O= case.
73 ifeq ($(RM),)
74 RM := rm -f
75 endif
76
77 define CLEAN
78 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
79 endef
80
81 clean:
82 $(CLEAN)
83
84 # When make O= with kselftest target from main level
85 # the following aren't defined.
86 #
87 ifneq ($(KBUILD_SRC),)
88 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
89 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
90 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
91 endif
92
93 $(OUTPUT)/%:%.c
94 $(LINK.c) $^ $(LDLIBS) -o $@
95
96 $(OUTPUT)/%.o:%.S
97 $(COMPILE.S) $^ -o $@
98
99 $(OUTPUT)/%:%.S
100 $(LINK.S) $^ $(LDLIBS) -o $@
101
102 .PHONY: run_tests all clean install emit_tests