Loading...
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 := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
11
12CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi
13CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu
14CLANG_TARGET_FLAGS_hexagon := hexagon-linux-musl
15CLANG_TARGET_FLAGS_i386 := i386-linux-gnu
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
22CLANG_TARGET_FLAGS_x86_64 := x86_64-linux-gnu
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
30
31ifeq ($(CROSS_COMPILE),)
32ifeq ($(CLANG_TARGET_FLAGS),)
33$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
34else
35CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
36endif # CLANG_TARGET_FLAGS
37else
38CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
39endif # CROSS_COMPILE
40
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
49CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
50else
51CC := $(CROSS_COMPILE)gcc
52endif # LLVM
53
54ifeq (0,$(MAKELEVEL))
55 ifeq ($(OUTPUT),)
56 OUTPUT := $(shell pwd)
57 DEFAULT_INSTALL_HDR_PATH := 1
58 endif
59endif
60selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
61top_srcdir = $(selfdir)/../../..
62
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
77ifeq ($(KHDR_INCLUDES),)
78KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
79endif
80
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
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.
96TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
97TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
98TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
99
100all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \
101 $(if $(TEST_GEN_MODS_DIR),gen_mods_dir)
102
103define RUN_TESTS
104 BASE_DIR="$(selfdir)"; \
105 . $(selfdir)/kselftest/runner.sh; \
106 if [ "X$(summary)" != "X" ]; then \
107 per_test_logging=1; \
108 fi; \
109 run_many $(1)
110endef
111
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
129run_tests: all
130ifdef building_out_of_srctree
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); \
133 fi
134 @$(INSTALL_INCLUDES)
135 @if [ "X$(TEST_PROGS)" != "X" ]; then \
136 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
137 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
138 else \
139 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
140 fi
141else
142 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
143endif
144
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
151define INSTALL_SINGLE_RULE
152 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
153 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
154endef
155
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
161define INSTALL_RULE
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)
169 $(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE)
170 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
171endef
172
173install: all
174ifdef INSTALL_PATH
175 $(INSTALL_RULE)
176 $(INSTALL_INCLUDES)
177else
178 $(error Error: set INSTALL_PATH to use install)
179endif
180
181emit_tests:
182 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
183 BASENAME_TEST=`basename $$TEST`; \
184 echo "$(COLLECTION):$$BASENAME_TEST"; \
185 done
186
187# define if isn't already. It is undefined in make O= case.
188ifeq ($(RM),)
189RM := rm -f
190endif
191
192define CLEAN
193 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
194endef
195
196clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir)
197 $(CLEAN)
198
199# Build with _GNU_SOURCE by default
200CFLAGS += -D_GNU_SOURCE=
201
202# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
203# make USERCFLAGS=-Werror USERLDFLAGS=-static
204CFLAGS += $(USERCFLAGS)
205LDFLAGS += $(USERLDFLAGS)
206
207# When make O= with kselftest target from main level
208# the following aren't defined.
209#
210ifdef building_out_of_srctree
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
216# Selftest makefiles can override those targets by setting
217# OVERRIDE_TARGETS = 1.
218ifeq ($(OVERRIDE_TARGETS),)
219LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
220$(OUTPUT)/%:%.c $(LOCAL_HDRS)
221 $(call msg,CC,,$@)
222 $(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
223
224$(OUTPUT)/%.o:%.S
225 $(COMPILE.S) $^ -o $@
226
227$(OUTPUT)/%:%.S
228 $(LINK.S) $^ $(LDLIBS) -o $@
229endif
230
231.PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir
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.
3CC := $(CROSS_COMPILE)gcc
4
5ifeq (0,$(MAKELEVEL))
6 ifeq ($(OUTPUT),)
7 OUTPUT := $(shell pwd)
8 DEFAULT_INSTALL_HDR_PATH := 1
9 endif
10endif
11selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
12
13# The following are built by lib.mk common compile rules.
14# TEST_CUSTOM_PROGS should be used by tests that require
15# custom build rule and prevent common build rule use.
16# TEST_PROGS are for test shell scripts.
17# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
18# and install targets. Common clean doesn't touch them.
19TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
20TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
21TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
22
23ifdef KSFT_KHDR_INSTALL
24top_srcdir ?= ../../../..
25include $(top_srcdir)/scripts/subarch.include
26ARCH ?= $(SUBARCH)
27
28# set default goal to all, so make without a target runs all, even when
29# all isn't the first target in the file.
30.DEFAULT_GOAL := all
31
32# Invoke headers install with --no-builtin-rules to avoid circular
33# dependency in "make kselftest" case. In this case, second level
34# make inherits builtin-rules which will use the rule generate
35# Makefile.o and runs into
36# "Circular Makefile.o <- prepare dependency dropped."
37# and headers_install fails and test compile fails.
38# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
39# invokes them as sub-makes and --no-builtin-rules is not necessary,
40# but doesn't cause any failures. Keep it simple and use the same
41# flags in both cases.
42# Note that the support to install headers from lib.mk is necessary
43# when test Makefile is run directly with "make -C".
44# When local build is done, headers are installed in the default
45# INSTALL_HDR_PATH usr/include.
46.PHONY: khdr
47khdr:
48ifndef KSFT_KHDR_INSTALL_DONE
49ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
50 make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
51else
52 make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
53 ARCH=$(ARCH) -C $(top_srcdir) headers_install
54endif
55endif
56
57all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
58else
59all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
60endif
61
62define RUN_TESTS
63 BASE_DIR="$(selfdir)"; \
64 . $(selfdir)/kselftest/runner.sh; \
65 if [ "X$(summary)" != "X" ]; then \
66 per_test_logging=1; \
67 fi; \
68 run_many $(1)
69endef
70
71run_tests: all
72ifdef building_out_of_srctree
73 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
74 rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
75 fi
76 @if [ "X$(TEST_PROGS)" != "X" ]; then \
77 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS)) ; \
78 else \
79 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
80 fi
81else
82 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
83endif
84
85define INSTALL_SINGLE_RULE
86 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
87 $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
88endef
89
90define INSTALL_RULE
91 $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
92 $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
93 $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
94 $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
95 $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
96 $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
97 $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
98endef
99
100install: all
101ifdef INSTALL_PATH
102 $(INSTALL_RULE)
103else
104 $(error Error: set INSTALL_PATH to use install)
105endif
106
107emit_tests:
108 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
109 BASENAME_TEST=`basename $$TEST`; \
110 echo " \\"; \
111 echo -n " \"$$BASENAME_TEST\""; \
112 done; \
113
114# define if isn't already. It is undefined in make O= case.
115ifeq ($(RM),)
116RM := rm -f
117endif
118
119define CLEAN
120 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
121endef
122
123clean:
124 $(CLEAN)
125
126# When make O= with kselftest target from main level
127# the following aren't defined.
128#
129ifdef building_out_of_srctree
130LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
131COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
132LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
133endif
134
135# Selftest makefiles can override those targets by setting
136# OVERRIDE_TARGETS = 1.
137ifeq ($(OVERRIDE_TARGETS),)
138LOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
139$(OUTPUT)/%:%.c $(LOCAL_HDRS)
140 $(LINK.c) $^ $(LDLIBS) -o $@
141
142$(OUTPUT)/%.o:%.S
143 $(COMPILE.S) $^ -o $@
144
145$(OUTPUT)/%:%.S
146 $(LINK.S) $^ $(LDLIBS) -o $@
147endif
148
149.PHONY: run_tests all clean install emit_tests