]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/testing/selftests/lib.mk
9d2b3c303bfa7003183bf1426530ae6c52d6726c
[linux.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     ifneq ($(O),)
7         OUTPUT := $(O)
8     else
9         ifneq ($(KBUILD_OUTPUT),)
10                 OUTPUT := $(KBUILD_OUTPUT)
11         else
12                 OUTPUT := $(shell pwd)
13                 DEFAULT_INSTALL_HDR_PATH := 1
14         endif
15     endif
16 endif
17 selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
18
19 # The following are built by lib.mk common compile rules.
20 # TEST_CUSTOM_PROGS should be used by tests that require
21 # custom build rule and prevent common build rule use.
22 # TEST_PROGS are for test shell scripts.
23 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
24 # and install targets. Common clean doesn't touch them.
25 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
26 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
27 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
28
29 ifdef KSFT_KHDR_INSTALL
30 top_srcdir ?= ../../../..
31 include $(top_srcdir)/scripts/subarch.include
32 ARCH            ?= $(SUBARCH)
33
34 # set default goal to all, so make without a target runs all, even when
35 # all isn't the first target in the file.
36 .DEFAULT_GOAL := all
37
38 # Invoke headers install with --no-builtin-rules to avoid circular
39 # dependency in "make kselftest" case. In this case, second level
40 # make inherits builtin-rules which will use the rule generate
41 # Makefile.o and runs into
42 # "Circular Makefile.o <- prepare dependency dropped."
43 # and headers_install fails and test compile fails.
44 # O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
45 # invokes them as sub-makes and --no-builtin-rules is not necessary,
46 # but doesn't cause any failures. Keep it simple and use the same
47 # flags in both cases.
48 # Note that the support to install headers from lib.mk is necessary
49 # when test Makefile is run directly with "make -C".
50 # When local build is done, headers are installed in the default
51 # INSTALL_HDR_PATH usr/include.
52 .PHONY: khdr
53 khdr:
54 ifndef KSFT_KHDR_INSTALL_DONE
55 ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
56         make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
57 else
58         make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
59                 ARCH=$(ARCH) -C $(top_srcdir) headers_install
60 endif
61 endif
62
63 all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
64 else
65 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
66 endif
67
68 .ONESHELL:
69 define RUN_TESTS
70         @export KSFT_TAP_LEVEL=`echo 1`;                \
71         test_num=`echo 0`;                              \
72         . $(selfdir)/kselftest/runner.sh;               \
73         echo "TAP version 13";                          \
74         for TEST in $(1); do                            \
75                 BASENAME_TEST=`basename $$TEST`;        \
76                 test_num=`echo $$test_num+1 | bc`;      \
77                 if [ "X$(summary)" != "X" ]; then       \
78                         logfile="/tmp/$$BASENAME_TEST"; \
79                         cat /dev/null > "$$logfile";    \
80                 fi;                                     \
81                 run_one "$$BASENAME_TEST" "$$test_num"; \
82         done;
83 endef
84
85 run_tests: all
86 ifneq ($(KBUILD_SRC),)
87         @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
88                 @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
89         fi
90         @if [ "X$(TEST_PROGS)" != "X" ]; then
91                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
92         else
93                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
94         fi
95 else
96         $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
97 endif
98
99 define INSTALL_RULE
100         @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then                                        \
101                 mkdir -p ${INSTALL_PATH};                                                                               \
102                 echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";    \
103                 rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;           \
104         fi
105         @if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then                                        \
106                 mkdir -p ${INSTALL_PATH};                                                                               \
107                 echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";   \
108                 rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;          \
109         fi
110 endef
111
112 install: all
113 ifdef INSTALL_PATH
114         $(INSTALL_RULE)
115 else
116         $(error Error: set INSTALL_PATH to use install)
117 endif
118
119 define EMIT_TESTS
120         @test_num=`echo 0`;                             \
121         for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
122                 BASENAME_TEST=`basename $$TEST`;        \
123                 test_num=`echo $$test_num+1 | bc`;      \
124                 TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST";  \
125                 echo "echo $$TEST_HDR_MSG";     \
126                 if [ ! -x $$TEST ]; then        \
127                         echo "echo \"$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.\"";         \
128                         echo "echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\""; \
129                 else
130                         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;)"; \
131                 fi;             \
132         done;
133 endef
134
135 emit_tests:
136         $(EMIT_TESTS)
137
138 # define if isn't already. It is undefined in make O= case.
139 ifeq ($(RM),)
140 RM := rm -f
141 endif
142
143 define CLEAN
144         $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
145 endef
146
147 clean:
148         $(CLEAN)
149
150 # When make O= with kselftest target from main level
151 # the following aren't defined.
152 #
153 ifneq ($(KBUILD_SRC),)
154 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
155 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
156 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
157 endif
158
159 # Selftest makefiles can override those targets by setting
160 # OVERRIDE_TARGETS = 1.
161 ifeq ($(OVERRIDE_TARGETS),)
162 $(OUTPUT)/%:%.c
163         $(LINK.c) $^ $(LDLIBS) -o $@
164
165 $(OUTPUT)/%.o:%.S
166         $(COMPILE.S) $^ -o $@
167
168 $(OUTPUT)/%:%.S
169         $(LINK.S) $^ $(LDLIBS) -o $@
170 endif
171
172 .PHONY: run_tests all clean install emit_tests