xref: /linux/tools/testing/selftests/lib.mk (revision 6d9b262afe0ec1d6e0ef99321ca9d6b921310471)
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# The following are built by lib.mk common compile rules.
52# TEST_CUSTOM_PROGS should be used by tests that require
53# custom build rule and prevent common build rule use.
54# TEST_PROGS are for test shell scripts.
55# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
56# and install targets. Common clean doesn't touch them.
57TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
58TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
59TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
60
61all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \
62	$(if $(TEST_GEN_MODS_DIR),gen_mods_dir)
63
64define RUN_TESTS
65	BASE_DIR="$(selfdir)";			\
66	. $(selfdir)/kselftest/runner.sh;	\
67	if [ "X$(summary)" != "X" ]; then       \
68		per_test_logging=1;		\
69	fi;                                     \
70	run_many $(1)
71endef
72
73define INSTALL_INCLUDES
74	$(if $(TEST_INCLUDES), \
75		relative_files=""; \
76		for entry in $(TEST_INCLUDES); do \
77			entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \
78			entry_name=$$(basename "$$entry"); \
79			relative_dir=$${entry_dir#"$$SRC_PATH"/}; \
80			if [ "$$relative_dir" = "$$entry_dir" ]; then \
81				echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \
82				exit 1; \
83			fi; \
84			relative_files="$$relative_files $$relative_dir/$$entry_name"; \
85		done; \
86		cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \
87	)
88endef
89
90run_tests: all
91ifdef building_out_of_srctree
92	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)$(TEST_GEN_MODS_DIR)" != "X" ]; then \
93		rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(TEST_GEN_MODS_DIR) $(OUTPUT); \
94	fi
95	@$(INSTALL_INCLUDES)
96	@if [ "X$(TEST_PROGS)" != "X" ]; then \
97		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
98				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
99	else \
100		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
101	fi
102else
103	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
104endif
105
106gen_mods_dir:
107	$(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR)
108
109clean_mods_dir:
110	$(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) clean
111
112define INSTALL_SINGLE_RULE
113	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
114	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
115endef
116
117define INSTALL_MODS_RULE
118	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)/$(INSTALL_LIST))
119	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST)/*.ko $(INSTALL_PATH)/$(INSTALL_LIST))
120endef
121
122define INSTALL_RULE
123	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
124	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
125	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
126	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
127	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
128	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
129	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
130	$(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE)
131	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
132endef
133
134install: all
135ifdef INSTALL_PATH
136	$(INSTALL_RULE)
137	$(INSTALL_INCLUDES)
138else
139	$(error Error: set INSTALL_PATH to use install)
140endif
141
142emit_tests:
143	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
144		BASENAME_TEST=`basename $$TEST`;	\
145		echo "$(COLLECTION):$$BASENAME_TEST";	\
146	done
147
148# define if isn't already. It is undefined in make O= case.
149ifeq ($(RM),)
150RM := rm -f
151endif
152
153define CLEAN
154	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
155endef
156
157clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir)
158	$(CLEAN)
159
160# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
161# make USERCFLAGS=-Werror USERLDFLAGS=-static
162CFLAGS += $(USERCFLAGS)
163LDFLAGS += $(USERLDFLAGS)
164
165# When make O= with kselftest target from main level
166# the following aren't defined.
167#
168ifdef building_out_of_srctree
169LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
170COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
171LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
172endif
173
174# Selftest makefiles can override those targets by setting
175# OVERRIDE_TARGETS = 1.
176ifeq ($(OVERRIDE_TARGETS),)
177LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
178$(OUTPUT)/%:%.c $(LOCAL_HDRS)
179	$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
180
181$(OUTPUT)/%.o:%.S
182	$(COMPILE.S) $^ -o $@
183
184$(OUTPUT)/%:%.S
185	$(LINK.S) $^ $(LDLIBS) -o $@
186endif
187
188.PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir
189