xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 1e70d57e)
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.
3ifneq ($(LLVM),)
4ifneq ($(filter %/,$(LLVM)),)
5LLVM_PREFIX := $(LLVM)
6else ifneq ($(filter -%,$(LLVM)),)
7LLVM_SUFFIX := $(LLVM)
8endif
9
10CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
11else
12CC := $(CROSS_COMPILE)gcc
13endif
14
15ifeq (0,$(MAKELEVEL))
16    ifeq ($(OUTPUT),)
17	OUTPUT := $(shell pwd)
18	DEFAULT_INSTALL_HDR_PATH := 1
19    endif
20endif
21selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
22
23# The following are built by lib.mk common compile rules.
24# TEST_CUSTOM_PROGS should be used by tests that require
25# custom build rule and prevent common build rule use.
26# TEST_PROGS are for test shell scripts.
27# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
28# and install targets. Common clean doesn't touch them.
29TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
30TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
31TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
32
33ifdef KSFT_KHDR_INSTALL
34top_srcdir ?= ../../../..
35include $(top_srcdir)/scripts/subarch.include
36ARCH		?= $(SUBARCH)
37
38# set default goal to all, so make without a target runs all, even when
39# all isn't the first target in the file.
40.DEFAULT_GOAL := all
41
42# Invoke headers install with --no-builtin-rules to avoid circular
43# dependency in "make kselftest" case. In this case, second level
44# make inherits builtin-rules which will use the rule generate
45# Makefile.o and runs into
46# "Circular Makefile.o <- prepare dependency dropped."
47# and headers_install fails and test compile fails.
48# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
49# invokes them as sub-makes and --no-builtin-rules is not necessary,
50# but doesn't cause any failures. Keep it simple and use the same
51# flags in both cases.
52# Note that the support to install headers from lib.mk is necessary
53# when test Makefile is run directly with "make -C".
54# When local build is done, headers are installed in the default
55# INSTALL_HDR_PATH usr/include.
56.PHONY: khdr
57.NOTPARALLEL:
58khdr:
59ifndef KSFT_KHDR_INSTALL_DONE
60ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
61	$(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
62else
63	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
64		ARCH=$(ARCH) -C $(top_srcdir) headers_install
65endif
66endif
67
68all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
69else
70all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
71endif
72
73define RUN_TESTS
74	BASE_DIR="$(selfdir)";			\
75	. $(selfdir)/kselftest/runner.sh;	\
76	if [ "X$(summary)" != "X" ]; then       \
77		per_test_logging=1;		\
78	fi;                                     \
79	run_many $(1)
80endef
81
82run_tests: all
83ifdef building_out_of_srctree
84	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
85		rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
86	fi
87	@if [ "X$(TEST_PROGS)" != "X" ]; then \
88		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
89				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
90	else \
91		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
92	fi
93else
94	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
95endif
96
97define INSTALL_SINGLE_RULE
98	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
99	$(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
100endef
101
102define INSTALL_RULE
103	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
104	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
105	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
106	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
107	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
108	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
109	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
110	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
111endef
112
113install: all
114ifdef INSTALL_PATH
115	$(INSTALL_RULE)
116else
117	$(error Error: set INSTALL_PATH to use install)
118endif
119
120emit_tests:
121	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
122		BASENAME_TEST=`basename $$TEST`;	\
123		echo "$(COLLECTION):$$BASENAME_TEST";	\
124	done
125
126# define if isn't already. It is undefined in make O= case.
127ifeq ($(RM),)
128RM := rm -f
129endif
130
131define CLEAN
132	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
133endef
134
135clean:
136	$(CLEAN)
137
138# When make O= with kselftest target from main level
139# the following aren't defined.
140#
141ifdef building_out_of_srctree
142LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
143COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
144LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
145endif
146
147# Selftest makefiles can override those targets by setting
148# OVERRIDE_TARGETS = 1.
149ifeq ($(OVERRIDE_TARGETS),)
150LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
151$(OUTPUT)/%:%.c $(LOCAL_HDRS)
152	$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
153
154$(OUTPUT)/%.o:%.S
155	$(COMPILE.S) $^ -o $@
156
157$(OUTPUT)/%:%.S
158	$(LINK.S) $^ $(LDLIBS) -o $@
159endif
160
161.PHONY: run_tests all clean install emit_tests
162