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
9 ifneq ($(KBUILD_OUTPUT),)
10 OUTPUT := $(KBUILD_OUTPUT)
12 OUTPUT := $(shell pwd)
13 DEFAULT_INSTALL_HDR_PATH := 1
18 # The following are built by lib.mk common compile rules.
19 # TEST_CUSTOM_PROGS should be used by tests that require
20 # custom build rule and prevent common build rule use.
21 # TEST_PROGS are for test shell scripts.
22 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
23 # and install targets. Common clean doesn't touch them.
24 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
25 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
26 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
28 ifdef KSFT_KHDR_INSTALL
29 top_srcdir ?= ../../../..
30 include $(top_srcdir)/scripts/subarch.include
33 # set default goal to all, so make without a target runs all, even when
34 # all isn't the first target in the file.
37 # Invoke headers install with --no-builtin-rules to avoid circular
38 # dependency in "make kselftest" case. In this case, second level
39 # make inherits builtin-rules which will use the rule generate
40 # Makefile.o and runs into
41 # "Circular Makefile.o <- prepare dependency dropped."
42 # and headers_install fails and test compile fails.
43 # O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
44 # invokes them as sub-makes and --no-builtin-rules is not necessary,
45 # but doesn't cause any failures. Keep it simple and use the same
46 # flags in both cases.
47 # Note that the support to install headers from lib.mk is necessary
48 # when test Makefile is run directly with "make -C".
49 # When local build is done, headers are installed in the default
50 # INSTALL_HDR_PATH usr/include.
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
57 make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
58 ARCH=$(ARCH) -C $(top_srcdir) headers_install
62 all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
64 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
68 define RUN_TEST_PRINT_RESULT
69 TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \
70 echo $$TEST_HDR_MSG; \
71 echo "========================================"; \
72 if [ ! -x $$TEST ]; then \
73 echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\
74 echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
76 cd `dirname $$TEST` > /dev/null; \
77 if [ "X$(summary)" != "X" ]; then \
78 (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \
79 echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
80 (if [ $$? -eq $$skip ]; then \
81 echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
82 else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
85 (./$$BASENAME_TEST && \
86 echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
87 (if [ $$? -eq $$skip ]; then \
88 echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
89 else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
97 @export KSFT_TAP_LEVEL=`echo 1`; \
100 echo "TAP version 13"; \
101 for TEST in $(1); do \
102 BASENAME_TEST=`basename $$TEST`; \
103 test_num=`echo $$test_num+1 | bc`; \
104 $(call RUN_TEST_PRINT_RESULT,$(TEST),$(BASENAME_TEST),$(test_num),$(skip)) \
109 ifneq ($(KBUILD_SRC),)
110 @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
111 @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
113 @if [ "X$(TEST_PROGS)" != "X" ]; then
114 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
116 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
119 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
123 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
124 mkdir -p ${INSTALL_PATH}; \
125 echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/"; \
126 rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/; \
128 @if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then \
129 mkdir -p ${INSTALL_PATH}; \
130 echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/"; \
131 rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/; \
139 $(error Error: set INSTALL_PATH to use install)
143 @test_num=`echo 0`; \
144 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
145 BASENAME_TEST=`basename $$TEST`; \
146 test_num=`echo $$test_num+1 | bc`; \
147 TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \
148 echo "echo $$TEST_HDR_MSG"; \
149 if [ ! -x $$TEST ]; then \
150 echo "echo \"$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.\""; \
151 echo "echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\""; \
153 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;)"; \
161 # define if isn't already. It is undefined in make O= case.
167 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
173 # When make O= with kselftest target from main level
174 # the following aren't defined.
176 ifneq ($(KBUILD_SRC),)
177 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
178 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
179 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
182 # Selftest makefiles can override those targets by setting
183 # OVERRIDE_TARGETS = 1.
184 ifeq ($(OVERRIDE_TARGETS),)
186 $(LINK.c) $^ $(LDLIBS) -o $@
189 $(COMPILE.S) $^ -o $@
192 $(LINK.S) $^ $(LDLIBS) -o $@
195 .PHONY: run_tests all clean install emit_tests