xref: /linux-6.15/tools/testing/selftests/lib.mk (revision 1a59f5d3)
184f887bfSMichael Ellerman# This mimics the top-level Makefile. We do it explicitly here so that this
284f887bfSMichael Ellerman# Makefile can operate with or without the kbuild infrastructure.
326e6dd10SYonghong Songifneq ($(LLVM),)
4e9c28192SNathan Chancellorifneq ($(filter %/,$(LLVM)),)
5e9c28192SNathan ChancellorLLVM_PREFIX := $(LLVM)
6e9c28192SNathan Chancellorelse ifneq ($(filter -%,$(LLVM)),)
7e9c28192SNathan ChancellorLLVM_SUFFIX := $(LLVM)
8e9c28192SNathan Chancellorendif
9e9c28192SNathan Chancellor
10d4e6fbd2SValentin ObstCLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
11d4e6fbd2SValentin Obst
12795285efSMark BrownCLANG_TARGET_FLAGS_arm          := arm-linux-gnueabi
13795285efSMark BrownCLANG_TARGET_FLAGS_arm64        := aarch64-linux-gnu
14795285efSMark BrownCLANG_TARGET_FLAGS_hexagon      := hexagon-linux-musl
15624c60f3SGuillaume TuckerCLANG_TARGET_FLAGS_i386         := i386-linux-gnu
16795285efSMark BrownCLANG_TARGET_FLAGS_m68k         := m68k-linux-gnu
17795285efSMark BrownCLANG_TARGET_FLAGS_mips         := mipsel-linux-gnu
18795285efSMark BrownCLANG_TARGET_FLAGS_powerpc      := powerpc64le-linux-gnu
19795285efSMark BrownCLANG_TARGET_FLAGS_riscv        := riscv64-linux-gnu
20795285efSMark BrownCLANG_TARGET_FLAGS_s390         := s390x-linux-gnu
21795285efSMark BrownCLANG_TARGET_FLAGS_x86          := x86_64-linux-gnu
22624c60f3SGuillaume TuckerCLANG_TARGET_FLAGS_x86_64       := x86_64-linux-gnu
23d4e6fbd2SValentin Obst
24d4e6fbd2SValentin Obst# Default to host architecture if ARCH is not explicitly given.
25d4e6fbd2SValentin Obstifeq ($(ARCH),)
26d4e6fbd2SValentin ObstCLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple)
27d4e6fbd2SValentin Obstelse
28795285efSMark BrownCLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
29d4e6fbd2SValentin Obstendif
30795285efSMark Brown
31795285efSMark Brownifeq ($(CROSS_COMPILE),)
32795285efSMark Brownifeq ($(CLANG_TARGET_FLAGS),)
339fdaca2cSMark Brown$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
34795285efSMark Brownelse
35795285efSMark BrownCLANG_FLAGS     += --target=$(CLANG_TARGET_FLAGS)
36795285efSMark Brownendif # CLANG_TARGET_FLAGS
37795285efSMark Brownelse
38795285efSMark BrownCLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
39795285efSMark Brownendif # CROSS_COMPILE
40795285efSMark Brown
41a84a0c6aSJohn Hubbard# gcc defaults to silence (off) for the following warnings, but clang defaults
42a84a0c6aSJohn Hubbard# to the opposite. The warnings are not useful for the kernel itself, which is
43a84a0c6aSJohn Hubbard# why they have remained disabled in gcc for the main kernel build. And it is
44a84a0c6aSJohn Hubbard# only due to including kernel data structures in the selftests, that we get the
45a84a0c6aSJohn Hubbard# warnings from clang. Therefore, disable the warnings for clang builds.
46a84a0c6aSJohn HubbardCFLAGS += -Wno-address-of-packed-member
47a84a0c6aSJohn HubbardCFLAGS += -Wno-gnu-variable-sized-type-not-at-end
48a84a0c6aSJohn Hubbard
49d4e6fbd2SValentin ObstCC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
5026e6dd10SYonghong Songelse
5184f887bfSMichael EllermanCC := $(CROSS_COMPILE)gcc
52795285efSMark Brownendif # LLVM
5384f887bfSMichael Ellerman
54e53aff45SShuah Khanifeq (0,$(MAKELEVEL))
5527d79a2bSShuah Khan    ifeq ($(OUTPUT),)
56e53aff45SShuah Khan	OUTPUT := $(shell pwd)
578ce72dc3SShuah Khan	DEFAULT_INSTALL_HDR_PATH := 1
588ce72dc3SShuah Khan    endif
598ce72dc3SShuah Khanendif
6042d46e57SKees Cookselfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
61bdbf0617SAxel Rasmussentop_srcdir = $(selfdir)/../../..
62e53aff45SShuah Khan
636fc6d7f5SJakub Kicinski# msg: emit succinct information message describing current building step
646fc6d7f5SJakub Kicinski# $1 - generic step name (e.g., CC, LINK, etc);
656fc6d7f5SJakub Kicinski# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
666fc6d7f5SJakub Kicinski# $3 - target (assumed to be file); only file name will be emitted;
676fc6d7f5SJakub Kicinski# $4 - optional extra arg, emitted as-is, if provided.
686fc6d7f5SJakub Kicinskiifeq ($(V),1)
696fc6d7f5SJakub KicinskiQ =
706fc6d7f5SJakub Kicinskimsg =
716fc6d7f5SJakub Kicinskielse
726fc6d7f5SJakub KicinskiQ = @
736fc6d7f5SJakub Kicinskimsg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
746fc6d7f5SJakub KicinskiMAKEFLAGS += --no-print-directory
756fc6d7f5SJakub Kicinskiendif
766fc6d7f5SJakub Kicinski
7743e8832fSJohn Hubbardifeq ($(KHDR_INCLUDES),)
78cee27ae5SShuah KhanKHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
79a5254052SMickaël Salaünendif
80a5254052SMickaël Salaün
81e076eacaSJohn Hubbard# In order to use newer items that haven't yet been added to the user's system
82e076eacaSJohn Hubbard# header files, add $(TOOLS_INCLUDES) to the compiler invocation in each
83e076eacaSJohn Hubbard# each selftest.
84e076eacaSJohn Hubbard# You may need to add files to that location, or to refresh an existing file. In
85e076eacaSJohn Hubbard# order to do that, run "make headers" from $(top_srcdir), then copy the
86e076eacaSJohn Hubbard# header file that you want from $(top_srcdir)/usr/include/... , to the matching
87e076eacaSJohn Hubbard# subdir in $(TOOLS_INCLUDE).
88e076eacaSJohn HubbardTOOLS_INCLUDES := -isystem $(top_srcdir)/tools/include/uapi
89e076eacaSJohn Hubbard
90be16a244SShuah Khan# The following are built by lib.mk common compile rules.
91be16a244SShuah Khan# TEST_CUSTOM_PROGS should be used by tests that require
92be16a244SShuah Khan# custom build rule and prevent common build rule use.
93be16a244SShuah Khan# TEST_PROGS are for test shell scripts.
94be16a244SShuah Khan# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
95be16a244SShuah Khan# and install targets. Common clean doesn't touch them.
96d83c3ba0SMichael EllermanTEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
978050ef2bSShuah KhanTEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
98d83c3ba0SMichael EllermanTEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
99d83c3ba0SMichael Ellerman
1006727980bSMarcos Paulo de Souzaall: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \
1016727980bSMarcos Paulo de Souza	$(if $(TEST_GEN_MODS_DIR),gen_mods_dir)
102b2d35fa5SAnders Roxell
1037afed3dcSShuah Khan (Samsung OSG)define RUN_TESTS
10499aacebeSYauheni Kaliuta	BASE_DIR="$(selfdir)";			\
1055c069b6dSKees Cook	. $(selfdir)/kselftest/runner.sh;	\
10642d46e57SKees Cook	if [ "X$(summary)" != "X" ]; then       \
107bf660782SKees Cook		per_test_logging=1;		\
10842d46e57SKees Cook	fi;                                     \
109bf660782SKees Cook	run_many $(1)
1105e29a910SMichael Ellermanendef
1115e29a910SMichael Ellerman
1122a0683beSBenjamin Poirierdefine INSTALL_INCLUDES
1132a0683beSBenjamin Poirier	$(if $(TEST_INCLUDES), \
1142a0683beSBenjamin Poirier		relative_files=""; \
1152a0683beSBenjamin Poirier		for entry in $(TEST_INCLUDES); do \
1162a0683beSBenjamin Poirier			entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \
1172a0683beSBenjamin Poirier			entry_name=$$(basename "$$entry"); \
1182a0683beSBenjamin Poirier			relative_dir=$${entry_dir#"$$SRC_PATH"/}; \
1192a0683beSBenjamin Poirier			if [ "$$relative_dir" = "$$entry_dir" ]; then \
1202a0683beSBenjamin Poirier				echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \
1212a0683beSBenjamin Poirier				exit 1; \
1222a0683beSBenjamin Poirier			fi; \
1232a0683beSBenjamin Poirier			relative_files="$$relative_files $$relative_dir/$$entry_name"; \
1242a0683beSBenjamin Poirier		done; \
1252a0683beSBenjamin Poirier		cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \
1262a0683beSBenjamin Poirier	)
1272a0683beSBenjamin Poirierendef
1282a0683beSBenjamin Poirier
1295e29a910SMichael Ellermanrun_tests: all
130051f278eSMasahiro Yamadaifdef building_out_of_srctree
1316727980bSMarcos Paulo de Souza	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)$(TEST_GEN_MODS_DIR)" != "X" ]; then \
1326727980bSMarcos Paulo de Souza		rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(TEST_GEN_MODS_DIR) $(OUTPUT); \
1331a940687SShuah Khan	fi
1342a0683beSBenjamin Poirier	@$(INSTALL_INCLUDES)
13599aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS)" != "X" ]; then \
136cb4969e6SIlya Leoshkevich		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
137cb4969e6SIlya Leoshkevich				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
13899aacebeSYauheni Kaliuta	else \
13999aacebeSYauheni Kaliuta		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
1401a940687SShuah Khan	fi
1411a940687SShuah Khanelse
14299aacebeSYauheni Kaliuta	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
1431a940687SShuah Khanendif
1445e29a910SMichael Ellerman
1456727980bSMarcos Paulo de Souzagen_mods_dir:
1466727980bSMarcos Paulo de Souza	$(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR)
1476727980bSMarcos Paulo de Souza
1486727980bSMarcos Paulo de Souzaclean_mods_dir:
1496727980bSMarcos Paulo de Souza	$(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) clean
1506727980bSMarcos Paulo de Souza
151c363eb48SJiri Bencdefine INSTALL_SINGLE_RULE
152c363eb48SJiri Benc	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
1533f3f3841SBjörn Töpel	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
154c363eb48SJiri Bencendef
155c363eb48SJiri Benc
1566727980bSMarcos Paulo de Souzadefine INSTALL_MODS_RULE
1576727980bSMarcos Paulo de Souza	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)/$(INSTALL_LIST))
1586727980bSMarcos Paulo de Souza	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST)/*.ko $(INSTALL_PATH)/$(INSTALL_LIST))
1596727980bSMarcos Paulo de Souzaendef
1606727980bSMarcos Paulo de Souza
16132dcfba6SMichael Ellermandefine INSTALL_RULE
162c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
163c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
164c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
165c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
166c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
167c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
168c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
1696727980bSMarcos Paulo de Souza	$(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE)
170de53fa9bSKees Cook	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
17132dcfba6SMichael Ellermanendef
17232dcfba6SMichael Ellerman
17332dcfba6SMichael Ellermaninstall: all
17432dcfba6SMichael Ellermanifdef INSTALL_PATH
17532dcfba6SMichael Ellerman	$(INSTALL_RULE)
1762a0683beSBenjamin Poirier	$(INSTALL_INCLUDES)
17732dcfba6SMichael Ellermanelse
17832dcfba6SMichael Ellerman	$(error Error: set INSTALL_PATH to use install)
17932dcfba6SMichael Ellermanendif
18032dcfba6SMichael Ellerman
181d4e59a53SKees Cookemit_tests:
1823df6131fSShuah Khan (Samsung OSG)	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
183a8ba798bS[email protected]		BASENAME_TEST=`basename $$TEST`;	\
184f0f0a5dfSKees Cook		echo "$(COLLECTION):$$BASENAME_TEST";	\
185f0f0a5dfSKees Cook	done
18632dcfba6SMichael Ellerman
1878050ef2bSShuah Khan# define if isn't already. It is undefined in make O= case.
1888050ef2bSShuah Khanifeq ($(RM),)
1898050ef2bSShuah KhanRM := rm -f
1908050ef2bSShuah Khanendif
1918050ef2bSShuah Khan
192df6438f9SShuah Khandefine CLEAN
19380d443e8S[email protected]	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
194df6438f9SShuah Khanendef
195df6438f9SShuah Khan
1966727980bSMarcos Paulo de Souzaclean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir)
197df6438f9SShuah Khan	$(CLEAN)
19888baa78dS[email protected]
199cc937dadSEdward Liaw# Build with _GNU_SOURCE by default
200cc937dadSEdward LiawCFLAGS += -D_GNU_SOURCE=
201cc937dadSEdward Liaw
202de3ee3f6SMickaël Salaün# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
203de3ee3f6SMickaël Salaün# make USERCFLAGS=-Werror USERLDFLAGS=-static
204de3ee3f6SMickaël SalaünCFLAGS += $(USERCFLAGS)
205de3ee3f6SMickaël SalaünLDFLAGS += $(USERLDFLAGS)
206de3ee3f6SMickaël Salaün
2078050ef2bSShuah Khan# When make O= with kselftest target from main level
2088050ef2bSShuah Khan# the following aren't defined.
2098050ef2bSShuah Khan#
210051f278eSMasahiro Yamadaifdef building_out_of_srctree
2118050ef2bSShuah KhanLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
2128050ef2bSShuah KhanCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
2138050ef2bSShuah KhanLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
2148050ef2bSShuah Khanendif
2158050ef2bSShuah Khan
2164e49ed2fSMathieu Desnoyers# Selftest makefiles can override those targets by setting
2174e49ed2fSMathieu Desnoyers# OVERRIDE_TARGETS = 1.
2184e49ed2fSMathieu Desnoyersifeq ($(OVERRIDE_TARGETS),)
219a738a4ceSAnders RoxellLOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
2201056d3d2SShuah Khan$(OUTPUT)/%:%.c $(LOCAL_HDRS)
2216fc6d7f5SJakub Kicinski	$(call msg,CC,,$@)
2226fc6d7f5SJakub Kicinski	$(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
223a8ba798bS[email protected]
224a8ba798bS[email protected]$(OUTPUT)/%.o:%.S
225634ce97cSMichael Ellerman	$(COMPILE.S) $^ -o $@
226a8ba798bS[email protected]
227a8ba798bS[email protected]$(OUTPUT)/%:%.S
228634ce97cSMichael Ellerman	$(LINK.S) $^ $(LDLIBS) -o $@
2294e49ed2fSMathieu Desnoyersendif
2307d758af2S[email protected]
231*1a59f5d3SThomas Weißschuhheaders:
232*1a59f5d3SThomas Weißschuh	$(Q)$(MAKE) -C $(top_srcdir) headers
233*1a59f5d3SThomas Weißschuh
234*1a59f5d3SThomas Weißschuh.PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir headers
235