xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision a738a4ce)
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),)
426e6dd10SYonghong SongCC := clang
526e6dd10SYonghong Songelse
684f887bfSMichael EllermanCC := $(CROSS_COMPILE)gcc
726e6dd10SYonghong Songendif
884f887bfSMichael Ellerman
9e53aff45SShuah Khanifeq (0,$(MAKELEVEL))
1027d79a2bSShuah Khan    ifeq ($(OUTPUT),)
11e53aff45SShuah Khan	OUTPUT := $(shell pwd)
128ce72dc3SShuah Khan	DEFAULT_INSTALL_HDR_PATH := 1
138ce72dc3SShuah Khan    endif
148ce72dc3SShuah Khanendif
1542d46e57SKees Cookselfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
16e53aff45SShuah Khan
17be16a244SShuah Khan# The following are built by lib.mk common compile rules.
18be16a244SShuah Khan# TEST_CUSTOM_PROGS should be used by tests that require
19be16a244SShuah Khan# custom build rule and prevent common build rule use.
20be16a244SShuah Khan# TEST_PROGS are for test shell scripts.
21be16a244SShuah Khan# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
22be16a244SShuah Khan# and install targets. Common clean doesn't touch them.
23d83c3ba0SMichael EllermanTEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
248050ef2bSShuah KhanTEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
25d83c3ba0SMichael EllermanTEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
26d83c3ba0SMichael Ellerman
27211929fdSShuah Khanifdef KSFT_KHDR_INSTALL
28b2d35fa5SAnders Roxelltop_srcdir ?= ../../../..
29b2d35fa5SAnders Roxellinclude $(top_srcdir)/scripts/subarch.include
30b2d35fa5SAnders RoxellARCH		?= $(SUBARCH)
31b2d35fa5SAnders Roxell
328ce72dc3SShuah Khan# set default goal to all, so make without a target runs all, even when
338ce72dc3SShuah Khan# all isn't the first target in the file.
348ce72dc3SShuah Khan.DEFAULT_GOAL := all
358ce72dc3SShuah Khan
368ce72dc3SShuah Khan# Invoke headers install with --no-builtin-rules to avoid circular
378ce72dc3SShuah Khan# dependency in "make kselftest" case. In this case, second level
388ce72dc3SShuah Khan# make inherits builtin-rules which will use the rule generate
398ce72dc3SShuah Khan# Makefile.o and runs into
408ce72dc3SShuah Khan# "Circular Makefile.o <- prepare dependency dropped."
418ce72dc3SShuah Khan# and headers_install fails and test compile fails.
428ce72dc3SShuah Khan# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
438ce72dc3SShuah Khan# invokes them as sub-makes and --no-builtin-rules is not necessary,
448ce72dc3SShuah Khan# but doesn't cause any failures. Keep it simple and use the same
458ce72dc3SShuah Khan# flags in both cases.
468ce72dc3SShuah Khan# Note that the support to install headers from lib.mk is necessary
478ce72dc3SShuah Khan# when test Makefile is run directly with "make -C".
488ce72dc3SShuah Khan# When local build is done, headers are installed in the default
498ce72dc3SShuah Khan# INSTALL_HDR_PATH usr/include.
50b2d35fa5SAnders Roxell.PHONY: khdr
518914a7a2SLi Zhijian.NOTPARALLEL:
52b2d35fa5SAnders Roxellkhdr:
538ce72dc3SShuah Khanifndef KSFT_KHDR_INSTALL_DONE
548ce72dc3SShuah Khanifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
5575fa6772SDenys Vlasenko	$(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
568ce72dc3SShuah Khanelse
5775fa6772SDenys Vlasenko	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
588ce72dc3SShuah Khan		ARCH=$(ARCH) -C $(top_srcdir) headers_install
598ce72dc3SShuah Khanendif
608ce72dc3SShuah Khanendif
61b2d35fa5SAnders Roxell
62211929fdSShuah Khanall: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
63211929fdSShuah Khanelse
64211929fdSShuah Khanall: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
65b2d35fa5SAnders Roxellendif
66b2d35fa5SAnders Roxell
677afed3dcSShuah Khan (Samsung OSG)define RUN_TESTS
6899aacebeSYauheni Kaliuta	BASE_DIR="$(selfdir)";			\
695c069b6dSKees Cook	. $(selfdir)/kselftest/runner.sh;	\
7042d46e57SKees Cook	if [ "X$(summary)" != "X" ]; then       \
71bf660782SKees Cook		per_test_logging=1;		\
7242d46e57SKees Cook	fi;                                     \
73bf660782SKees Cook	run_many $(1)
745e29a910SMichael Ellermanendef
755e29a910SMichael Ellerman
765e29a910SMichael Ellermanrun_tests: all
77051f278eSMasahiro Yamadaifdef building_out_of_srctree
7899aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
7999aacebeSYauheni Kaliuta		rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
801a940687SShuah Khan	fi
8199aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS)" != "X" ]; then \
82cb4969e6SIlya Leoshkevich		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
83cb4969e6SIlya Leoshkevich				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
8499aacebeSYauheni Kaliuta	else \
8599aacebeSYauheni Kaliuta		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
861a940687SShuah Khan	fi
871a940687SShuah Khanelse
8899aacebeSYauheni Kaliuta	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
891a940687SShuah Khanendif
905e29a910SMichael Ellerman
91c363eb48SJiri Bencdefine INSTALL_SINGLE_RULE
92c363eb48SJiri Benc	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
9399aacebeSYauheni Kaliuta	$(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
94c363eb48SJiri Bencendef
95c363eb48SJiri Benc
9632dcfba6SMichael Ellermandefine INSTALL_RULE
97c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
98c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
99c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
100c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
101c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
102c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
103c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
104de53fa9bSKees Cook	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
10532dcfba6SMichael Ellermanendef
10632dcfba6SMichael Ellerman
10732dcfba6SMichael Ellermaninstall: all
10832dcfba6SMichael Ellermanifdef INSTALL_PATH
10932dcfba6SMichael Ellerman	$(INSTALL_RULE)
11032dcfba6SMichael Ellermanelse
11132dcfba6SMichael Ellerman	$(error Error: set INSTALL_PATH to use install)
11232dcfba6SMichael Ellermanendif
11332dcfba6SMichael Ellerman
114d4e59a53SKees Cookemit_tests:
1153df6131fSShuah Khan (Samsung OSG)	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
116a8ba798bSbamvor.zhangjian@huawei.com		BASENAME_TEST=`basename $$TEST`;	\
117f0f0a5dfSKees Cook		echo "$(COLLECTION):$$BASENAME_TEST";	\
118f0f0a5dfSKees Cook	done
11932dcfba6SMichael Ellerman
1208050ef2bSShuah Khan# define if isn't already. It is undefined in make O= case.
1218050ef2bSShuah Khanifeq ($(RM),)
1228050ef2bSShuah KhanRM := rm -f
1238050ef2bSShuah Khanendif
1248050ef2bSShuah Khan
125df6438f9SShuah Khandefine CLEAN
12680d443e8Sbamvor.zhangjian@huawei.com	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
127df6438f9SShuah Khanendef
128df6438f9SShuah Khan
129df6438f9SShuah Khanclean:
130df6438f9SShuah Khan	$(CLEAN)
13188baa78dSbamvor.zhangjian@huawei.com
1328050ef2bSShuah Khan# When make O= with kselftest target from main level
1338050ef2bSShuah Khan# the following aren't defined.
1348050ef2bSShuah Khan#
135051f278eSMasahiro Yamadaifdef building_out_of_srctree
1368050ef2bSShuah KhanLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1378050ef2bSShuah KhanCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
1388050ef2bSShuah KhanLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1398050ef2bSShuah Khanendif
1408050ef2bSShuah Khan
1414e49ed2fSMathieu Desnoyers# Selftest makefiles can override those targets by setting
1424e49ed2fSMathieu Desnoyers# OVERRIDE_TARGETS = 1.
1434e49ed2fSMathieu Desnoyersifeq ($(OVERRIDE_TARGETS),)
144*a738a4ceSAnders RoxellLOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
1451056d3d2SShuah Khan$(OUTPUT)/%:%.c $(LOCAL_HDRS)
146f825d3f7STommi Rantala	$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
147a8ba798bSbamvor.zhangjian@huawei.com
148a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%.o:%.S
149634ce97cSMichael Ellerman	$(COMPILE.S) $^ -o $@
150a8ba798bSbamvor.zhangjian@huawei.com
151a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%:%.S
152634ce97cSMichael Ellerman	$(LINK.S) $^ $(LDLIBS) -o $@
1534e49ed2fSMathieu Desnoyersendif
1547d758af2Sbamvor.zhangjian@huawei.com
15532dcfba6SMichael Ellerman.PHONY: run_tests all clean install emit_tests
156