xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 5c069b6dedef1fab5420ca8658ed7f9ee4d26007)
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
17selfdir = $(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.
25TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
26TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
27TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
28
29ifdef KSFT_KHDR_INSTALL
30top_srcdir ?= ../../../..
31include $(top_srcdir)/scripts/subarch.include
32ARCH		?= $(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
53khdr:
54ifndef KSFT_KHDR_INSTALL_DONE
55ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
56	make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
57else
58	make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
59		ARCH=$(ARCH) -C $(top_srcdir) headers_install
60endif
61endif
62
63all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
64else
65all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
66endif
67
68.ONESHELL:
69define RUN_TESTS
70	@BASE_DIR="$(selfdir)";			\
71	. $(selfdir)/kselftest/runner.sh;	\
72	if [ "X$(summary)" != "X" ]; then       \
73		per_test_logging=1;		\
74	fi;                                     \
75	run_many $(1)
76endef
77
78run_tests: all
79ifneq ($(KBUILD_SRC),)
80	@if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
81		@rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
82	fi
83	@if [ "X$(TEST_PROGS)" != "X" ]; then
84		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
85	else
86		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
87	fi
88else
89	$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
90endif
91
92define INSTALL_RULE
93	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then					\
94		mkdir -p ${INSTALL_PATH};										\
95		echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";	\
96		rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;		\
97	fi
98	@if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then					\
99		mkdir -p ${INSTALL_PATH};										\
100		echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";	\
101		rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;		\
102	fi
103endef
104
105install: all
106ifdef INSTALL_PATH
107	$(INSTALL_RULE)
108else
109	$(error Error: set INSTALL_PATH to use install)
110endif
111
112emit_tests:
113	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
114		BASENAME_TEST=`basename $$TEST`;	\
115		echo "	\\";				\
116		echo -n "	\"$$BASENAME_TEST\"";	\
117	done;						\
118
119# define if isn't already. It is undefined in make O= case.
120ifeq ($(RM),)
121RM := rm -f
122endif
123
124define CLEAN
125	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
126endef
127
128clean:
129	$(CLEAN)
130
131# When make O= with kselftest target from main level
132# the following aren't defined.
133#
134ifneq ($(KBUILD_SRC),)
135LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
136COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
137LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
138endif
139
140# Selftest makefiles can override those targets by setting
141# OVERRIDE_TARGETS = 1.
142ifeq ($(OVERRIDE_TARGETS),)
143$(OUTPUT)/%:%.c
144	$(LINK.c) $^ $(LDLIBS) -o $@
145
146$(OUTPUT)/%.o:%.S
147	$(COMPILE.S) $^ -o $@
148
149$(OUTPUT)/%:%.S
150	$(LINK.S) $^ $(LDLIBS) -o $@
151endif
152
153.PHONY: run_tests all clean install emit_tests
154