Merge tag 'media/v6.12-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-block.git] / tools / testing / selftests / lib.mk
CommitLineData
84f887bf
ME
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.
26e6dd10 3ifneq ($(LLVM),)
e9c28192
NC
4ifneq ($(filter %/,$(LLVM)),)
5LLVM_PREFIX := $(LLVM)
6else ifneq ($(filter -%,$(LLVM)),)
7LLVM_SUFFIX := $(LLVM)
8endif
9
d4e6fbd2
VO
10CLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
11
795285ef
MB
12CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi
13CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu
14CLANG_TARGET_FLAGS_hexagon := hexagon-linux-musl
624c60f3 15CLANG_TARGET_FLAGS_i386 := i386-linux-gnu
795285ef
MB
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
624c60f3 22CLANG_TARGET_FLAGS_x86_64 := x86_64-linux-gnu
d4e6fbd2
VO
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
795285ef
MB
30
31ifeq ($(CROSS_COMPILE),)
32ifeq ($(CLANG_TARGET_FLAGS),)
9fdaca2c 33$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
795285ef
MB
34else
35CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
36endif # CLANG_TARGET_FLAGS
37else
38CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
39endif # CROSS_COMPILE
40
a84a0c6a
JH
41# gcc defaults to silence (off) for the following warnings, but clang defaults
42# to the opposite. The warnings are not useful for the kernel itself, which is
43# why they have remained disabled in gcc for the main kernel build. And it is
44# only due to including kernel data structures in the selftests, that we get the
45# warnings from clang. Therefore, disable the warnings for clang builds.
46CFLAGS += -Wno-address-of-packed-member
47CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
48
d4e6fbd2 49CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
26e6dd10 50else
84f887bf 51CC := $(CROSS_COMPILE)gcc
795285ef 52endif # LLVM
84f887bf 53
e53aff45 54ifeq (0,$(MAKELEVEL))
27d79a2b
SK
55 ifeq ($(OUTPUT),)
56 OUTPUT := $(shell pwd)
57 DEFAULT_INSTALL_HDR_PATH := 1
8ce72dc3 58 endif
e53aff45 59endif
42d46e57 60selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
bdbf0617 61top_srcdir = $(selfdir)/../../..
e53aff45 62
6fc6d7f5
JK
63# msg: emit succinct information message describing current building step
64# $1 - generic step name (e.g., CC, LINK, etc);
65# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
66# $3 - target (assumed to be file); only file name will be emitted;
67# $4 - optional extra arg, emitted as-is, if provided.
68ifeq ($(V),1)
69Q =
70msg =
71else
72Q = @
73msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
74MAKEFLAGS += --no-print-directory
75endif
76
43e8832f 77ifeq ($(KHDR_INCLUDES),)
cee27ae5 78KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
a5254052
MS
79endif
80
e076eaca
JH
81# In order to use newer items that haven't yet been added to the user's system
82# header files, add $(TOOLS_INCLUDES) to the compiler invocation in each
83# each selftest.
84# You may need to add files to that location, or to refresh an existing file. In
85# order to do that, run "make headers" from $(top_srcdir), then copy the
86# header file that you want from $(top_srcdir)/usr/include/... , to the matching
87# subdir in $(TOOLS_INCLUDE).
88TOOLS_INCLUDES := -isystem $(top_srcdir)/tools/include/uapi
89
be16a244
SK
90# The following are built by lib.mk common compile rules.
91# TEST_CUSTOM_PROGS should be used by tests that require
92# custom build rule and prevent common build rule use.
93# TEST_PROGS are for test shell scripts.
94# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
95# and install targets. Common clean doesn't touch them.
d83c3ba0 96TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
8050ef2b 97TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
d83c3ba0
ME
98TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
99
6727980b
MPS
100all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \
101 $(if $(TEST_GEN_MODS_DIR),gen_mods_dir)
b2d35fa5 102
5e29a910 103define RUN_TESTS
99aacebe 104 BASE_DIR="$(selfdir)"; \
5c069b6d 105 . $(selfdir)/kselftest/runner.sh; \
bf660782
KC
106 if [ "X$(summary)" != "X" ]; then \
107 per_test_logging=1; \
108 fi; \
109 run_many $(1)
5e29a910
ME
110endef
111
2a0683be
BP
112define INSTALL_INCLUDES
113 $(if $(TEST_INCLUDES), \
114 relative_files=""; \
115 for entry in $(TEST_INCLUDES); do \
116 entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \
117 entry_name=$$(basename "$$entry"); \
118 relative_dir=$${entry_dir#"$$SRC_PATH"/}; \
119 if [ "$$relative_dir" = "$$entry_dir" ]; then \
120 echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \
121 exit 1; \
122 fi; \
123 relative_files="$$relative_files $$relative_dir/$$entry_name"; \
124 done; \
125 cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \
126 )
127endef
128
5e29a910 129run_tests: all
051f278e 130ifdef building_out_of_srctree
6727980b
MPS
131 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)$(TEST_GEN_MODS_DIR)" != "X" ]; then \
132 rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(TEST_GEN_MODS_DIR) $(OUTPUT); \
1a940687 133 fi
2a0683be 134 @$(INSTALL_INCLUDES)
99aacebe 135 @if [ "X$(TEST_PROGS)" != "X" ]; then \
cb4969e6
IL
136 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
137 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
99aacebe
YK
138 else \
139 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
1a940687
SK
140 fi
141else
99aacebe 142 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
1a940687 143endif
5e29a910 144
6727980b
MPS
145gen_mods_dir:
146 $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR)
147
148clean_mods_dir:
149 $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) clean
150
c363eb48
JB
151define INSTALL_SINGLE_RULE
152 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
3f3f3841 153 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
c363eb48
JB
154endef
155
6727980b
MPS
156define INSTALL_MODS_RULE
157 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)/$(INSTALL_LIST))
158 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST)/*.ko $(INSTALL_PATH)/$(INSTALL_LIST))
159endef
160
32dcfba6 161define INSTALL_RULE
c363eb48
JB
162 $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
163 $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
164 $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
165 $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
166 $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
167 $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
168 $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
6727980b 169 $(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE)
de53fa9b 170 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
32dcfba6
ME
171endef
172
173install: all
174ifdef INSTALL_PATH
175 $(INSTALL_RULE)
2a0683be 176 $(INSTALL_INCLUDES)
32dcfba6
ME
177else
178 $(error Error: set INSTALL_PATH to use install)
179endif
180
d4e59a53 181emit_tests:
3df6131f 182 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
a8ba798b 183 BASENAME_TEST=`basename $$TEST`; \
f0f0a5df
KC
184 echo "$(COLLECTION):$$BASENAME_TEST"; \
185 done
32dcfba6 186
8050ef2b
SK
187# define if isn't already. It is undefined in make O= case.
188ifeq ($(RM),)
189RM := rm -f
190endif
191
df6438f9 192define CLEAN
80d443e8 193 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
df6438f9
SK
194endef
195
6727980b 196clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir)
df6438f9 197 $(CLEAN)
88baa78d 198
cc937dad
EL
199# Build with _GNU_SOURCE by default
200CFLAGS += -D_GNU_SOURCE=
201
de3ee3f6
MS
202# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
203# make USERCFLAGS=-Werror USERLDFLAGS=-static
204CFLAGS += $(USERCFLAGS)
205LDFLAGS += $(USERLDFLAGS)
206
8050ef2b
SK
207# When make O= with kselftest target from main level
208# the following aren't defined.
209#
051f278e 210ifdef building_out_of_srctree
8050ef2b
SK
211LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
212COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
213LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
214endif
215
4e49ed2f
MD
216# Selftest makefiles can override those targets by setting
217# OVERRIDE_TARGETS = 1.
218ifeq ($(OVERRIDE_TARGETS),)
a738a4ce 219LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
1056d3d2 220$(OUTPUT)/%:%.c $(LOCAL_HDRS)
6fc6d7f5
JK
221 $(call msg,CC,,$@)
222 $(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
a8ba798b 223
224$(OUTPUT)/%.o:%.S
634ce97c 225 $(COMPILE.S) $^ -o $@
a8ba798b 226
227$(OUTPUT)/%:%.S
634ce97c 228 $(LINK.S) $^ $(LDLIBS) -o $@
4e49ed2f 229endif
7d758af2 230
6727980b 231.PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir