xref: /linux/tools/testing/selftests/lib.mk (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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
9TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
10TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
11
12all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
13
14define RUN_TESTS
15	@for TEST in $(TEST_GEN_PROGS) $(TEST_PROGS); do \
16		BASENAME_TEST=`basename $$TEST`;	\
17		cd `dirname $$TEST`; (./$$BASENAME_TEST && echo "selftests: $$BASENAME_TEST [PASS]") || echo "selftests:  $$BASENAME_TEST [FAIL]"; cd -;\
18	done;
19endef
20
21run_tests: all
22	$(RUN_TESTS)
23
24define INSTALL_RULE
25	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then					\
26		mkdir -p ${INSTALL_PATH};										\
27		echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";	\
28		rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;		\
29	fi
30	@if [ "X$(TEST_GEN_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then					\
31		mkdir -p ${INSTALL_PATH};										\
32		echo "rsync -a $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";	\
33		rsync -a $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;		\
34	fi
35endef
36
37install: all
38ifdef INSTALL_PATH
39	$(INSTALL_RULE)
40else
41	$(error Error: set INSTALL_PATH to use install)
42endif
43
44define EMIT_TESTS
45	@for TEST in $(TEST_GEN_PROGS) $(TEST_PROGS); do \
46		BASENAME_TEST=`basename $$TEST`;	\
47		echo "(./$$BASENAME_TEST && echo \"selftests: $$BASENAME_TEST [PASS]\") || echo \"selftests: $$BASENAME_TEST [FAIL]\""; \
48	done;
49endef
50
51emit_tests:
52	$(EMIT_TESTS)
53
54clean:
55	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
56
57$(OUTPUT)/%:%.c
58	$(LINK.c) $^ $(LDLIBS) -o $@
59
60$(OUTPUT)/%.o:%.S
61	$(COMPILE.S) $^ -o $@
62
63$(OUTPUT)/%:%.S
64	$(LINK.S) $^ $(LDLIBS) -o $@
65
66.PHONY: run_tests all clean install emit_tests
67