xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 2c64e9cb)
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.
3CC := $(CROSS_COMPILE)gcc
4
5ifeq (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
16endif
17
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.
24TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
25TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
26TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
27
28ifdef KSFT_KHDR_INSTALL
29top_srcdir ?= ../../../..
30include $(top_srcdir)/scripts/subarch.include
31ARCH		?= $(SUBARCH)
32
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.
35.DEFAULT_GOAL := all
36
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.
51.PHONY: khdr
52khdr:
53ifndef KSFT_KHDR_INSTALL_DONE
54ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
55	make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
56else
57	make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
58		ARCH=$(ARCH) -C $(top_srcdir) headers_install
59endif
60endif
61
62all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
63else
64all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
65endif
66
67.ONESHELL:
68define 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]"; \
75	else					\
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]";					\
83			fi;)			\
84		else				\
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]";				\
90			fi;)		\
91		fi;				\
92		cd - > /dev/null;		\
93	fi;
94endef
95
96define RUN_TESTS
97	@export KSFT_TAP_LEVEL=`echo 1`;		\
98	test_num=`echo 0`;				\
99	skip=`echo 4`;					\
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))						\
105	done;
106endef
107
108run_tests: all
109ifneq ($(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)
112	fi
113	@if [ "X$(TEST_PROGS)" != "X" ]; then
114		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
115	else
116		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
117	fi
118else
119	$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
120endif
121
122define INSTALL_RULE
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)/;		\
127	fi
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)/;		\
132	fi
133endef
134
135install: all
136ifdef INSTALL_PATH
137	$(INSTALL_RULE)
138else
139	$(error Error: set INSTALL_PATH to use install)
140endif
141
142define EMIT_TESTS
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]\""; \
152		else
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;)"; \
154		fi;		\
155	done;
156endef
157
158emit_tests:
159	$(EMIT_TESTS)
160
161# define if isn't already. It is undefined in make O= case.
162ifeq ($(RM),)
163RM := rm -f
164endif
165
166define CLEAN
167	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
168endef
169
170clean:
171	$(CLEAN)
172
173# When make O= with kselftest target from main level
174# the following aren't defined.
175#
176ifneq ($(KBUILD_SRC),)
177LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
178COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
179LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
180endif
181
182# Selftest makefiles can override those targets by setting
183# OVERRIDE_TARGETS = 1.
184ifeq ($(OVERRIDE_TARGETS),)
185$(OUTPUT)/%:%.c
186	$(LINK.c) $^ $(LDLIBS) -o $@
187
188$(OUTPUT)/%.o:%.S
189	$(COMPILE.S) $^ -o $@
190
191$(OUTPUT)/%:%.S
192	$(LINK.S) $^ $(LDLIBS) -o $@
193endif
194
195.PHONY: run_tests all clean install emit_tests
196