hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/tools/testing/selftests/lib.mk
....@@ -7,8 +7,12 @@
77 endif
88
99 ifeq (0,$(MAKELEVEL))
10
-OUTPUT := $(shell pwd)
10
+ ifeq ($(OUTPUT),)
11
+ OUTPUT := $(shell pwd)
12
+ DEFAULT_INSTALL_HDR_PATH := 1
13
+ endif
1114 endif
15
+selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
1216
1317 # The following are built by lib.mk common compile rules.
1418 # TEST_CUSTOM_PROGS should be used by tests that require
....@@ -25,75 +29,68 @@
2529 include $(top_srcdir)/scripts/subarch.include
2630 ARCH ?= $(SUBARCH)
2731
32
+# set default goal to all, so make without a target runs all, even when
33
+# all isn't the first target in the file.
34
+.DEFAULT_GOAL := all
35
+
36
+# Invoke headers install with --no-builtin-rules to avoid circular
37
+# dependency in "make kselftest" case. In this case, second level
38
+# make inherits builtin-rules which will use the rule generate
39
+# Makefile.o and runs into
40
+# "Circular Makefile.o <- prepare dependency dropped."
41
+# and headers_install fails and test compile fails.
42
+# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
43
+# invokes them as sub-makes and --no-builtin-rules is not necessary,
44
+# but doesn't cause any failures. Keep it simple and use the same
45
+# flags in both cases.
46
+# Note that the support to install headers from lib.mk is necessary
47
+# when test Makefile is run directly with "make -C".
48
+# When local build is done, headers are installed in the default
49
+# INSTALL_HDR_PATH usr/include.
2850 .PHONY: khdr
2951 .NOTPARALLEL:
3052 khdr:
31
- make ARCH=$(ARCH) -C $(top_srcdir) headers_install
53
+ifndef KSFT_KHDR_INSTALL_DONE
54
+ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
55
+ $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
56
+else
57
+ $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
58
+ ARCH=$(ARCH) -C $(top_srcdir) headers_install
59
+endif
60
+endif
3261
3362 all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
3463 else
3564 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
3665 endif
3766
38
-.ONESHELL:
39
-define RUN_TEST_PRINT_RESULT
40
- TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \
41
- echo $$TEST_HDR_MSG; \
42
- echo "========================================"; \
43
- if [ ! -x $$TEST ]; then \
44
- echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\
45
- echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
46
- else \
47
- cd `dirname $$TEST` > /dev/null; \
48
- if [ "X$(summary)" != "X" ]; then \
49
- (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \
50
- echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
51
- (if [ $$? -eq $$skip ]; then \
52
- echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
53
- else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
54
- fi;) \
55
- else \
56
- (./$$BASENAME_TEST && \
57
- echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
58
- (if [ $$? -eq $$skip ]; then \
59
- echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
60
- else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
61
- fi;) \
62
- fi; \
63
- cd - > /dev/null; \
64
- fi;
65
-endef
66
-
6767 define RUN_TESTS
68
- @export KSFT_TAP_LEVEL=`echo 1`; \
69
- test_num=`echo 0`; \
70
- skip=`echo 4`; \
71
- echo "TAP version 13"; \
72
- for TEST in $(1); do \
73
- BASENAME_TEST=`basename $$TEST`; \
74
- test_num=`echo $$test_num+1 | bc`; \
75
- $(call RUN_TEST_PRINT_RESULT,$(TEST),$(BASENAME_TEST),$(test_num),$(skip)) \
76
- done;
68
+ BASE_DIR="$(selfdir)"; \
69
+ . $(selfdir)/kselftest/runner.sh; \
70
+ if [ "X$(summary)" != "X" ]; then \
71
+ per_test_logging=1; \
72
+ fi; \
73
+ run_many $(1)
7774 endef
7875
7976 run_tests: all
80
-ifneq ($(KBUILD_SRC),)
81
- @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
82
- @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
77
+ifdef building_out_of_srctree
78
+ @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
79
+ rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
8380 fi
84
- @if [ "X$(TEST_PROGS)" != "X" ]; then
85
- $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
86
- else
87
- $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
81
+ @if [ "X$(TEST_PROGS)" != "X" ]; then \
82
+ $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
83
+ $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
84
+ else \
85
+ $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
8886 fi
8987 else
90
- $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
88
+ @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
9189 endif
9290
9391 define INSTALL_SINGLE_RULE
9492 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
95
- $(if $(INSTALL_LIST),@echo rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
96
- $(if $(INSTALL_LIST),@rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
93
+ $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
9794 endef
9895
9996 define INSTALL_RULE
....@@ -113,24 +110,11 @@
113110 $(error Error: set INSTALL_PATH to use install)
114111 endif
115112
116
-define EMIT_TESTS
117
- @test_num=`echo 0`; \
113
+emit_tests:
118114 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
119115 BASENAME_TEST=`basename $$TEST`; \
120
- test_num=`echo $$test_num+1 | bc`; \
121
- TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \
122
- echo "echo $$TEST_HDR_MSG"; \
123
- if [ ! -x $$TEST ]; then \
124
- echo "echo \"$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.\""; \
125
- echo "echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\""; \
126
- else
127
- echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"ok 1..$$test_num $$TEST_HDR_MSG [PASS]\") || (if [ \$$? -eq \$$skip ]; then echo \"not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]\"; else echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\"; fi;)"; \
128
- fi; \
129
- done;
130
-endef
131
-
132
-emit_tests:
133
- $(EMIT_TESTS)
116
+ echo "$(COLLECTION):$$BASENAME_TEST"; \
117
+ done
134118
135119 # define if isn't already. It is undefined in make O= case.
136120 ifeq ($(RM),)
....@@ -147,7 +131,7 @@
147131 # When make O= with kselftest target from main level
148132 # the following aren't defined.
149133 #
150
-ifneq ($(KBUILD_SRC),)
134
+ifdef building_out_of_srctree
151135 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
152136 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
153137 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
....@@ -156,8 +140,9 @@
156140 # Selftest makefiles can override those targets by setting
157141 # OVERRIDE_TARGETS = 1.
158142 ifeq ($(OVERRIDE_TARGETS),)
159
-$(OUTPUT)/%:%.c
160
- $(LINK.c) $^ $(LDLIBS) -o $@
143
+LOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
144
+$(OUTPUT)/%:%.c $(LOCAL_HDRS)
145
+ $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
161146
162147 $(OUTPUT)/%.o:%.S
163148 $(COMPILE.S) $^ -o $@