xref: /linux/tools/testing/selftests/lib.mk (revision db10cb9b)
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 ("$(origin O)", "command line")
48  KBUILD_OUTPUT := $(O)
49endif
50
51ifneq ($(KBUILD_OUTPUT),)
52  # Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
53  # expand a shell special character '~'. We use a somewhat tedious way here.
54  abs_objtree := $(shell cd $(top_srcdir) && mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
55  $(if $(abs_objtree),, \
56    $(error failed to create output directory "$(KBUILD_OUTPUT)"))
57  # $(realpath ...) resolves symlinks
58  abs_objtree := $(realpath $(abs_objtree))
59  KHDR_DIR := ${abs_objtree}/usr/include
60else
61  abs_srctree := $(shell cd $(top_srcdir) && pwd)
62  KHDR_DIR := ${abs_srctree}/usr/include
63endif
64
65KHDR_INCLUDES := -isystem $(KHDR_DIR)
66
67# The following are built by lib.mk common compile rules.
68# TEST_CUSTOM_PROGS should be used by tests that require
69# custom build rule and prevent common build rule use.
70# TEST_PROGS are for test shell scripts.
71# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
72# and install targets. Common clean doesn't touch them.
73TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
74TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
75TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
76
77all: kernel_header_files $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) \
78     $(TEST_GEN_FILES)
79
80kernel_header_files:
81	@ls $(KHDR_DIR)/linux/*.h >/dev/null 2>/dev/null;                      \
82	if [ $$? -ne 0 ]; then                                                 \
83            RED='\033[1;31m';                                                  \
84            NOCOLOR='\033[0m';                                                 \
85            echo;                                                              \
86            echo -e "$${RED}error$${NOCOLOR}: missing kernel header files.";   \
87            echo "Please run this and try again:";                             \
88            echo;                                                              \
89            echo "    cd $(top_srcdir)";                                       \
90            echo "    make headers";                                           \
91            echo;                                                              \
92	    exit 1; \
93	fi
94
95.PHONY: kernel_header_files
96
97define RUN_TESTS
98	BASE_DIR="$(selfdir)";			\
99	. $(selfdir)/kselftest/runner.sh;	\
100	if [ "X$(summary)" != "X" ]; then       \
101		per_test_logging=1;		\
102	fi;                                     \
103	run_many $(1)
104endef
105
106run_tests: all
107ifdef building_out_of_srctree
108	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
109		rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
110	fi
111	@if [ "X$(TEST_PROGS)" != "X" ]; then \
112		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
113				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
114	else \
115		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
116	fi
117else
118	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
119endif
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_RULE
127	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
128	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
129	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
130	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
131	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
132	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
133	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
134	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
135endef
136
137install: all
138ifdef INSTALL_PATH
139	$(INSTALL_RULE)
140else
141	$(error Error: set INSTALL_PATH to use install)
142endif
143
144emit_tests:
145	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
146		BASENAME_TEST=`basename $$TEST`;	\
147		echo "$(COLLECTION):$$BASENAME_TEST";	\
148	done
149
150# define if isn't already. It is undefined in make O= case.
151ifeq ($(RM),)
152RM := rm -f
153endif
154
155define CLEAN
156	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
157endef
158
159clean:
160	$(CLEAN)
161
162# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
163# make USERCFLAGS=-Werror USERLDFLAGS=-static
164CFLAGS += $(USERCFLAGS)
165LDFLAGS += $(USERLDFLAGS)
166
167# When make O= with kselftest target from main level
168# the following aren't defined.
169#
170ifdef building_out_of_srctree
171LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
172COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
173LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
174endif
175
176# Selftest makefiles can override those targets by setting
177# OVERRIDE_TARGETS = 1.
178ifeq ($(OVERRIDE_TARGETS),)
179LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
180$(OUTPUT)/%:%.c $(LOCAL_HDRS)
181	$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
182
183$(OUTPUT)/%.o:%.S
184	$(COMPILE.S) $^ -o $@
185
186$(OUTPUT)/%:%.S
187	$(LINK.S) $^ $(LDLIBS) -o $@
188endif
189
190.PHONY: run_tests all clean install emit_tests
191