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