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