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