xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision a48c7709)
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))
6OUTPUT := $(shell pwd)
7endif
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.
15TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
16TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
17TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
18
19all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
20
21.ONESHELL:
22define RUN_TESTS
23	@export KSFT_TAP_LEVEL=`echo 1`;
24	@test_num=`echo 0`;
25	@echo "TAP version 13";
26	@for TEST in $(1); do				\
27		BASENAME_TEST=`basename $$TEST`;	\
28		test_num=`echo $$test_num+1 | bc`;	\
29		echo "selftests: $$BASENAME_TEST";	\
30		echo "========================================";	\
31		if [ ! -x $$TEST ]; then	\
32			echo "selftests: Warning: file $$BASENAME_TEST is not executable, correct this.";\
33			echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; \
34		else					\
35		if [ "X$(summary)" != "X" ]; then		\
36				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;\
37			else				\
38				cd `dirname $$TEST` > /dev/null; (./$$BASENAME_TEST && echo "ok 1..$$test_num selftests: $$BASENAME_TEST [PASS]") || echo "not ok 1..$$test_num selftests:  $$BASENAME_TEST [FAIL]"; cd - > /dev/null;\
39			fi;				\
40		fi;					\
41	done;
42endef
43
44run_tests: all
45ifneq ($(KBUILD_SRC),)
46	@if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
47		@rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
48	fi
49	@if [ "X$(TEST_PROGS)" != "X" ]; then
50		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
51	else
52		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
53	fi
54else
55	$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
56endif
57
58define INSTALL_RULE
59	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then					\
60		mkdir -p ${INSTALL_PATH};										\
61		echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";	\
62		rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;		\
63	fi
64	@if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then					\
65		mkdir -p ${INSTALL_PATH};										\
66		echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";	\
67		rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;		\
68	fi
69endef
70
71install: all
72ifdef INSTALL_PATH
73	$(INSTALL_RULE)
74else
75	$(error Error: set INSTALL_PATH to use install)
76endif
77
78define EMIT_TESTS
79	@for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
80		BASENAME_TEST=`basename $$TEST`;	\
81		echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"selftests: $$BASENAME_TEST [PASS]\") || echo \"selftests: $$BASENAME_TEST [FAIL]\""; \
82	done;
83endef
84
85emit_tests:
86	$(EMIT_TESTS)
87
88# define if isn't already. It is undefined in make O= case.
89ifeq ($(RM),)
90RM := rm -f
91endif
92
93define CLEAN
94	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
95endef
96
97clean:
98	$(CLEAN)
99
100# When make O= with kselftest target from main level
101# the following aren't defined.
102#
103ifneq ($(KBUILD_SRC),)
104LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
105COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
106LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
107endif
108
109$(OUTPUT)/%:%.c
110	$(LINK.c) $^ $(LDLIBS) -o $@
111
112$(OUTPUT)/%.o:%.S
113	$(COMPILE.S) $^ -o $@
114
115$(OUTPUT)/%:%.S
116	$(LINK.S) $^ $(LDLIBS) -o $@
117
118.PHONY: run_tests all clean install emit_tests
119