Merge tag 'vfs-6.7.misc' of gitolite.kernel.org:pub/scm/linux/kernel/git/vfs/vfs
[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
795285ef
MB
10CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi
11CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu
12CLANG_TARGET_FLAGS_hexagon := hexagon-linux-musl
624c60f3 13CLANG_TARGET_FLAGS_i386 := i386-linux-gnu
795285ef
MB
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
624c60f3 20CLANG_TARGET_FLAGS_x86_64 := x86_64-linux-gnu
795285ef
MB
21CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
22
23ifeq ($(CROSS_COMPILE),)
24ifeq ($(CLANG_TARGET_FLAGS),)
9fdaca2c 25$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
795285ef
MB
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
26e6dd10 34else
84f887bf 35CC := $(CROSS_COMPILE)gcc
795285ef 36endif # LLVM
84f887bf 37
e53aff45 38ifeq (0,$(MAKELEVEL))
27d79a2b
SK
39 ifeq ($(OUTPUT),)
40 OUTPUT := $(shell pwd)
41 DEFAULT_INSTALL_HDR_PATH := 1
8ce72dc3 42 endif
e53aff45 43endif
42d46e57 44selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
bdbf0617 45top_srcdir = $(selfdir)/../../..
e53aff45 46
9fc96c7c
JH
47ifeq ("$(origin O)", "command line")
48 KBUILD_OUTPUT := $(O)
a5254052
MS
49endif
50
9fc96c7c
JH
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
be16a244
SK
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.
d83c3ba0 73TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
8050ef2b 74TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
d83c3ba0
ME
75TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
76
9fc96c7c
JH
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
b2d35fa5 96
5e29a910 97define RUN_TESTS
99aacebe 98 BASE_DIR="$(selfdir)"; \
5c069b6d 99 . $(selfdir)/kselftest/runner.sh; \
bf660782
KC
100 if [ "X$(summary)" != "X" ]; then \
101 per_test_logging=1; \
102 fi; \
103 run_many $(1)
5e29a910
ME
104endef
105
106run_tests: all
051f278e 107ifdef building_out_of_srctree
c9f75047 108 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
3f3f3841 109 rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
1a940687 110 fi
99aacebe 111 @if [ "X$(TEST_PROGS)" != "X" ]; then \
cb4969e6
IL
112 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
113 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
99aacebe
YK
114 else \
115 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
1a940687
SK
116 fi
117else
99aacebe 118 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
1a940687 119endif
5e29a910 120
c363eb48
JB
121define INSTALL_SINGLE_RULE
122 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
3f3f3841 123 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
c363eb48
JB
124endef
125
32dcfba6 126define INSTALL_RULE
c363eb48
JB
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)
de53fa9b 134 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
32dcfba6
ME
135endef
136
137install: all
138ifdef INSTALL_PATH
139 $(INSTALL_RULE)
140else
141 $(error Error: set INSTALL_PATH to use install)
142endif
143
d4e59a53 144emit_tests:
3df6131f 145 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
a8ba798b 146 BASENAME_TEST=`basename $$TEST`; \
f0f0a5df
KC
147 echo "$(COLLECTION):$$BASENAME_TEST"; \
148 done
32dcfba6 149
8050ef2b
SK
150# define if isn't already. It is undefined in make O= case.
151ifeq ($(RM),)
152RM := rm -f
153endif
154
df6438f9 155define CLEAN
80d443e8 156 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
df6438f9
SK
157endef
158
159clean:
160 $(CLEAN)
88baa78d 161
de3ee3f6
MS
162# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
163# make USERCFLAGS=-Werror USERLDFLAGS=-static
164CFLAGS += $(USERCFLAGS)
165LDFLAGS += $(USERLDFLAGS)
166
8050ef2b
SK
167# When make O= with kselftest target from main level
168# the following aren't defined.
169#
051f278e 170ifdef building_out_of_srctree
8050ef2b
SK
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
4e49ed2f
MD
176# Selftest makefiles can override those targets by setting
177# OVERRIDE_TARGETS = 1.
178ifeq ($(OVERRIDE_TARGETS),)
a738a4ce 179LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
1056d3d2 180$(OUTPUT)/%:%.c $(LOCAL_HDRS)
f825d3f7 181 $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
a8ba798b 182
183$(OUTPUT)/%.o:%.S
634ce97c 184 $(COMPILE.S) $^ -o $@
a8ba798b 185
186$(OUTPUT)/%:%.S
634ce97c 187 $(LINK.S) $^ $(LDLIBS) -o $@
4e49ed2f 188endif
7d758af2 189
32dcfba6 190.PHONY: run_tests all clean install emit_tests