xref: /linux/tools/testing/selftests/lib.mk (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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 := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
11
12CLANG_TARGET_FLAGS_arm          := arm-linux-gnueabi
13CLANG_TARGET_FLAGS_arm64        := aarch64-linux-gnu
14CLANG_TARGET_FLAGS_hexagon      := hexagon-linux-musl
15CLANG_TARGET_FLAGS_i386         := i386-linux-gnu
16CLANG_TARGET_FLAGS_m68k         := m68k-linux-gnu
17CLANG_TARGET_FLAGS_mips         := mipsel-linux-gnu
18CLANG_TARGET_FLAGS_powerpc      := powerpc64le-linux-gnu
19CLANG_TARGET_FLAGS_riscv        := riscv64-linux-gnu
20CLANG_TARGET_FLAGS_s390         := s390x-linux-gnu
21CLANG_TARGET_FLAGS_x86          := x86_64-linux-gnu
22CLANG_TARGET_FLAGS_x86_64       := x86_64-linux-gnu
23
24# Default to host architecture if ARCH is not explicitly given.
25ifeq ($(ARCH),)
26CLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple)
27else
28CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
29endif
30
31ifeq ($(CROSS_COMPILE),)
32ifeq ($(CLANG_TARGET_FLAGS),)
33$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
34else
35CLANG_FLAGS     += --target=$(CLANG_TARGET_FLAGS)
36endif # CLANG_TARGET_FLAGS
37else
38CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
39endif # CROSS_COMPILE
40
41CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
42else
43CC := $(CROSS_COMPILE)gcc
44endif # LLVM
45
46ifeq (0,$(MAKELEVEL))
47    ifeq ($(OUTPUT),)
48	OUTPUT := $(shell pwd)
49	DEFAULT_INSTALL_HDR_PATH := 1
50    endif
51endif
52selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
53top_srcdir = $(selfdir)/../../..
54
55# msg: emit succinct information message describing current building step
56# $1 - generic step name (e.g., CC, LINK, etc);
57# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
58# $3 - target (assumed to be file); only file name will be emitted;
59# $4 - optional extra arg, emitted as-is, if provided.
60ifeq ($(V),1)
61Q =
62msg =
63else
64Q = @
65msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
66MAKEFLAGS += --no-print-directory
67endif
68
69ifeq ($(KHDR_INCLUDES),)
70KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
71endif
72
73# In order to use newer items that haven't yet been added to the user's system
74# header files, add $(TOOLS_INCLUDES) to the compiler invocation in each
75# each selftest.
76# You may need to add files to that location, or to refresh an existing file. In
77# order to do that, run "make headers" from $(top_srcdir), then copy the
78# header file that you want from $(top_srcdir)/usr/include/... , to the matching
79# subdir in $(TOOLS_INCLUDE).
80TOOLS_INCLUDES := -isystem $(top_srcdir)/tools/include/uapi
81
82# The following are built by lib.mk common compile rules.
83# TEST_CUSTOM_PROGS should be used by tests that require
84# custom build rule and prevent common build rule use.
85# TEST_PROGS are for test shell scripts.
86# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
87# and install targets. Common clean doesn't touch them.
88TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
89TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
90TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
91
92all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \
93	$(if $(TEST_GEN_MODS_DIR),gen_mods_dir)
94
95define RUN_TESTS
96	BASE_DIR="$(selfdir)";			\
97	. $(selfdir)/kselftest/runner.sh;	\
98	if [ "X$(summary)" != "X" ]; then       \
99		per_test_logging=1;		\
100	fi;                                     \
101	run_many $(1)
102endef
103
104define INSTALL_INCLUDES
105	$(if $(TEST_INCLUDES), \
106		relative_files=""; \
107		for entry in $(TEST_INCLUDES); do \
108			entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \
109			entry_name=$$(basename "$$entry"); \
110			relative_dir=$${entry_dir#"$$SRC_PATH"/}; \
111			if [ "$$relative_dir" = "$$entry_dir" ]; then \
112				echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \
113				exit 1; \
114			fi; \
115			relative_files="$$relative_files $$relative_dir/$$entry_name"; \
116		done; \
117		cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \
118	)
119endef
120
121run_tests: all
122ifdef building_out_of_srctree
123	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)$(TEST_GEN_MODS_DIR)" != "X" ]; then \
124		rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(TEST_GEN_MODS_DIR) $(OUTPUT); \
125	fi
126	@$(INSTALL_INCLUDES)
127	@if [ "X$(TEST_PROGS)" != "X" ]; then \
128		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
129				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
130	else \
131		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
132	fi
133else
134	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
135endif
136
137gen_mods_dir:
138	$(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR)
139
140clean_mods_dir:
141	$(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) clean
142
143define INSTALL_SINGLE_RULE
144	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
145	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
146endef
147
148define INSTALL_MODS_RULE
149	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)/$(INSTALL_LIST))
150	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST)/*.ko $(INSTALL_PATH)/$(INSTALL_LIST))
151endef
152
153define INSTALL_RULE
154	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
155	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
156	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
157	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
158	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
159	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
160	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
161	$(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE)
162	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
163endef
164
165install: all
166ifdef INSTALL_PATH
167	$(INSTALL_RULE)
168	$(INSTALL_INCLUDES)
169else
170	$(error Error: set INSTALL_PATH to use install)
171endif
172
173emit_tests:
174	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
175		BASENAME_TEST=`basename $$TEST`;	\
176		echo "$(COLLECTION):$$BASENAME_TEST";	\
177	done
178
179# define if isn't already. It is undefined in make O= case.
180ifeq ($(RM),)
181RM := rm -f
182endif
183
184define CLEAN
185	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
186endef
187
188clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir)
189	$(CLEAN)
190
191# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
192# make USERCFLAGS=-Werror USERLDFLAGS=-static
193CFLAGS += $(USERCFLAGS)
194LDFLAGS += $(USERLDFLAGS)
195
196# When make O= with kselftest target from main level
197# the following aren't defined.
198#
199ifdef building_out_of_srctree
200LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
201COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
202LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
203endif
204
205# Selftest makefiles can override those targets by setting
206# OVERRIDE_TARGETS = 1.
207ifeq ($(OVERRIDE_TARGETS),)
208LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
209$(OUTPUT)/%:%.c $(LOCAL_HDRS)
210	$(call msg,CC,,$@)
211	$(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
212
213$(OUTPUT)/%.o:%.S
214	$(COMPILE.S) $^ -o $@
215
216$(OUTPUT)/%:%.S
217	$(LINK.S) $^ $(LDLIBS) -o $@
218endif
219
220.PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir
221