xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
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
102d43d30aSValentin ObstCLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
112d43d30aSValentin 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
232d43d30aSValentin Obst
242d43d30aSValentin Obst# Default to host architecture if ARCH is not explicitly given.
252d43d30aSValentin Obstifeq ($(ARCH),)
262d43d30aSValentin ObstCLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple)
272d43d30aSValentin Obstelse
28795285efSMark BrownCLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
292d43d30aSValentin 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
412d43d30aSValentin ObstCC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
4226e6dd10SYonghong Songelse
4384f887bfSMichael EllermanCC := $(CROSS_COMPILE)gcc
44795285efSMark Brownendif # LLVM
4584f887bfSMichael Ellerman
46e53aff45SShuah Khanifeq (0,$(MAKELEVEL))
4727d79a2bSShuah Khan    ifeq ($(OUTPUT),)
48e53aff45SShuah Khan	OUTPUT := $(shell pwd)
498ce72dc3SShuah Khan	DEFAULT_INSTALL_HDR_PATH := 1
508ce72dc3SShuah Khan    endif
518ce72dc3SShuah Khanendif
5242d46e57SKees Cookselfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
53bdbf0617SAxel Rasmussentop_srcdir = $(selfdir)/../../..
54e53aff45SShuah Khan
55d228e98dSJohn Hubbardifeq ($(KHDR_INCLUDES),)
56d228e98dSJohn HubbardKHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
57a5254052SMickaël Salaünendif
58a5254052SMickaël Salaün
59be16a244SShuah Khan# The following are built by lib.mk common compile rules.
60be16a244SShuah Khan# TEST_CUSTOM_PROGS should be used by tests that require
61be16a244SShuah Khan# custom build rule and prevent common build rule use.
62be16a244SShuah Khan# TEST_PROGS are for test shell scripts.
63be16a244SShuah Khan# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
64be16a244SShuah Khan# and install targets. Common clean doesn't touch them.
65d83c3ba0SMichael EllermanTEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
668050ef2bSShuah KhanTEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
67d83c3ba0SMichael EllermanTEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
68d83c3ba0SMichael Ellerman
69d228e98dSJohn Hubbardall: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
70b2d35fa5SAnders Roxell
717afed3dcSShuah Khan (Samsung OSG)define RUN_TESTS
7299aacebeSYauheni Kaliuta	BASE_DIR="$(selfdir)";			\
735c069b6dSKees Cook	. $(selfdir)/kselftest/runner.sh;	\
7442d46e57SKees Cook	if [ "X$(summary)" != "X" ]; then       \
75bf660782SKees Cook		per_test_logging=1;		\
7642d46e57SKees Cook	fi;                                     \
77bf660782SKees Cook	run_many $(1)
785e29a910SMichael Ellermanendef
795e29a910SMichael Ellerman
80*66c43605SBenjamin Poirierdefine INSTALL_INCLUDES
81*66c43605SBenjamin Poirier	$(if $(TEST_INCLUDES), \
82*66c43605SBenjamin Poirier		relative_files=""; \
83*66c43605SBenjamin Poirier		for entry in $(TEST_INCLUDES); do \
84*66c43605SBenjamin Poirier			entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \
85*66c43605SBenjamin Poirier			entry_name=$$(basename "$$entry"); \
86*66c43605SBenjamin Poirier			relative_dir=$${entry_dir#"$$SRC_PATH"/}; \
87*66c43605SBenjamin Poirier			if [ "$$relative_dir" = "$$entry_dir" ]; then \
88*66c43605SBenjamin Poirier				echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \
89*66c43605SBenjamin Poirier				exit 1; \
90*66c43605SBenjamin Poirier			fi; \
91*66c43605SBenjamin Poirier			relative_files="$$relative_files $$relative_dir/$$entry_name"; \
92*66c43605SBenjamin Poirier		done; \
93*66c43605SBenjamin Poirier		cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \
94*66c43605SBenjamin Poirier	)
95*66c43605SBenjamin Poirierendef
96*66c43605SBenjamin Poirier
975e29a910SMichael Ellermanrun_tests: all
98051f278eSMasahiro Yamadaifdef building_out_of_srctree
9999aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
1003f3f3841SBjörn Töpel		rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
1011a940687SShuah Khan	fi
102*66c43605SBenjamin Poirier	@$(INSTALL_INCLUDES)
10399aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS)" != "X" ]; then \
104cb4969e6SIlya Leoshkevich		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
105cb4969e6SIlya Leoshkevich				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
10699aacebeSYauheni Kaliuta	else \
10799aacebeSYauheni Kaliuta		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
1081a940687SShuah Khan	fi
1091a940687SShuah Khanelse
11099aacebeSYauheni Kaliuta	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
1111a940687SShuah Khanendif
1125e29a910SMichael Ellerman
113c363eb48SJiri Bencdefine INSTALL_SINGLE_RULE
114c363eb48SJiri Benc	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
1153f3f3841SBjörn Töpel	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
116c363eb48SJiri Bencendef
117c363eb48SJiri Benc
11832dcfba6SMichael Ellermandefine INSTALL_RULE
119c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
120c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
121c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
122c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
123c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
124c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
125c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
126de53fa9bSKees Cook	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
12732dcfba6SMichael Ellermanendef
12832dcfba6SMichael Ellerman
12932dcfba6SMichael Ellermaninstall: all
13032dcfba6SMichael Ellermanifdef INSTALL_PATH
13132dcfba6SMichael Ellerman	$(INSTALL_RULE)
132*66c43605SBenjamin Poirier	$(INSTALL_INCLUDES)
13332dcfba6SMichael Ellermanelse
13432dcfba6SMichael Ellerman	$(error Error: set INSTALL_PATH to use install)
13532dcfba6SMichael Ellermanendif
13632dcfba6SMichael Ellerman
137d4e59a53SKees Cookemit_tests:
1383df6131fSShuah Khan (Samsung OSG)	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
139a8ba798bSbamvor.zhangjian@huawei.com		BASENAME_TEST=`basename $$TEST`;	\
140f0f0a5dfSKees Cook		echo "$(COLLECTION):$$BASENAME_TEST";	\
141f0f0a5dfSKees Cook	done
14232dcfba6SMichael Ellerman
1438050ef2bSShuah Khan# define if isn't already. It is undefined in make O= case.
1448050ef2bSShuah Khanifeq ($(RM),)
1458050ef2bSShuah KhanRM := rm -f
1468050ef2bSShuah Khanendif
1478050ef2bSShuah Khan
148df6438f9SShuah Khandefine CLEAN
14980d443e8Sbamvor.zhangjian@huawei.com	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
150df6438f9SShuah Khanendef
151df6438f9SShuah Khan
152df6438f9SShuah Khanclean:
153df6438f9SShuah Khan	$(CLEAN)
15488baa78dSbamvor.zhangjian@huawei.com
155de3ee3f6SMickaël Salaün# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
156de3ee3f6SMickaël Salaün# make USERCFLAGS=-Werror USERLDFLAGS=-static
157de3ee3f6SMickaël SalaünCFLAGS += $(USERCFLAGS)
158de3ee3f6SMickaël SalaünLDFLAGS += $(USERLDFLAGS)
159de3ee3f6SMickaël Salaün
1608050ef2bSShuah Khan# When make O= with kselftest target from main level
1618050ef2bSShuah Khan# the following aren't defined.
1628050ef2bSShuah Khan#
163051f278eSMasahiro Yamadaifdef building_out_of_srctree
1648050ef2bSShuah KhanLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1658050ef2bSShuah KhanCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
1668050ef2bSShuah KhanLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1678050ef2bSShuah Khanendif
1688050ef2bSShuah Khan
1694e49ed2fSMathieu Desnoyers# Selftest makefiles can override those targets by setting
1704e49ed2fSMathieu Desnoyers# OVERRIDE_TARGETS = 1.
1714e49ed2fSMathieu Desnoyersifeq ($(OVERRIDE_TARGETS),)
172a738a4ceSAnders RoxellLOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
1731056d3d2SShuah Khan$(OUTPUT)/%:%.c $(LOCAL_HDRS)
174f825d3f7STommi Rantala	$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
175a8ba798bSbamvor.zhangjian@huawei.com
176a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%.o:%.S
177634ce97cSMichael Ellerman	$(COMPILE.S) $^ -o $@
178a8ba798bSbamvor.zhangjian@huawei.com
179a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%:%.S
180634ce97cSMichael Ellerman	$(LINK.S) $^ $(LDLIBS) -o $@
1814e49ed2fSMathieu Desnoyersendif
1827d758af2Sbamvor.zhangjian@huawei.com
18332dcfba6SMichael Ellerman.PHONY: run_tests all clean install emit_tests
184