xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 99aacebe)
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.
384f887bfSMichael EllermanCC := $(CROSS_COMPILE)gcc
484f887bfSMichael Ellerman
5e53aff45SShuah Khanifeq (0,$(MAKELEVEL))
627d79a2bSShuah Khan    ifeq ($(OUTPUT),)
7e53aff45SShuah Khan	OUTPUT := $(shell pwd)
88ce72dc3SShuah Khan	DEFAULT_INSTALL_HDR_PATH := 1
98ce72dc3SShuah Khan    endif
108ce72dc3SShuah Khanendif
1142d46e57SKees Cookselfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
12e53aff45SShuah Khan
13be16a244SShuah Khan# The following are built by lib.mk common compile rules.
14be16a244SShuah Khan# TEST_CUSTOM_PROGS should be used by tests that require
15be16a244SShuah Khan# custom build rule and prevent common build rule use.
16be16a244SShuah Khan# TEST_PROGS are for test shell scripts.
17be16a244SShuah Khan# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
18be16a244SShuah Khan# and install targets. Common clean doesn't touch them.
19d83c3ba0SMichael EllermanTEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
208050ef2bSShuah KhanTEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
21d83c3ba0SMichael EllermanTEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
22d83c3ba0SMichael Ellerman
23211929fdSShuah Khanifdef KSFT_KHDR_INSTALL
24b2d35fa5SAnders Roxelltop_srcdir ?= ../../../..
25b2d35fa5SAnders Roxellinclude $(top_srcdir)/scripts/subarch.include
26b2d35fa5SAnders RoxellARCH		?= $(SUBARCH)
27b2d35fa5SAnders Roxell
288ce72dc3SShuah Khan# set default goal to all, so make without a target runs all, even when
298ce72dc3SShuah Khan# all isn't the first target in the file.
308ce72dc3SShuah Khan.DEFAULT_GOAL := all
318ce72dc3SShuah Khan
328ce72dc3SShuah Khan# Invoke headers install with --no-builtin-rules to avoid circular
338ce72dc3SShuah Khan# dependency in "make kselftest" case. In this case, second level
348ce72dc3SShuah Khan# make inherits builtin-rules which will use the rule generate
358ce72dc3SShuah Khan# Makefile.o and runs into
368ce72dc3SShuah Khan# "Circular Makefile.o <- prepare dependency dropped."
378ce72dc3SShuah Khan# and headers_install fails and test compile fails.
388ce72dc3SShuah Khan# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
398ce72dc3SShuah Khan# invokes them as sub-makes and --no-builtin-rules is not necessary,
408ce72dc3SShuah Khan# but doesn't cause any failures. Keep it simple and use the same
418ce72dc3SShuah Khan# flags in both cases.
428ce72dc3SShuah Khan# Note that the support to install headers from lib.mk is necessary
438ce72dc3SShuah Khan# when test Makefile is run directly with "make -C".
448ce72dc3SShuah Khan# When local build is done, headers are installed in the default
458ce72dc3SShuah Khan# INSTALL_HDR_PATH usr/include.
46b2d35fa5SAnders Roxell.PHONY: khdr
47b2d35fa5SAnders Roxellkhdr:
488ce72dc3SShuah Khanifndef KSFT_KHDR_INSTALL_DONE
498ce72dc3SShuah Khanifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
508ce72dc3SShuah Khan	make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
518ce72dc3SShuah Khanelse
528ce72dc3SShuah Khan	make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
538ce72dc3SShuah Khan		ARCH=$(ARCH) -C $(top_srcdir) headers_install
548ce72dc3SShuah Khanendif
558ce72dc3SShuah Khanendif
56b2d35fa5SAnders Roxell
57211929fdSShuah Khanall: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
58211929fdSShuah Khanelse
59211929fdSShuah Khanall: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
60b2d35fa5SAnders Roxellendif
61b2d35fa5SAnders Roxell
627afed3dcSShuah Khan (Samsung OSG)define RUN_TESTS
6399aacebeSYauheni Kaliuta	BASE_DIR="$(selfdir)";			\
645c069b6dSKees Cook	. $(selfdir)/kselftest/runner.sh;	\
6542d46e57SKees Cook	if [ "X$(summary)" != "X" ]; then       \
66bf660782SKees Cook		per_test_logging=1;		\
6742d46e57SKees Cook	fi;                                     \
68bf660782SKees Cook	run_many $(1)
695e29a910SMichael Ellermanendef
705e29a910SMichael Ellerman
715e29a910SMichael Ellermanrun_tests: all
72051f278eSMasahiro Yamadaifdef building_out_of_srctree
7399aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then \
7499aacebeSYauheni Kaliuta		rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
751a940687SShuah Khan	fi
7699aacebeSYauheni Kaliuta	@if [ "X$(TEST_PROGS)" != "X" ]; then \
7799aacebeSYauheni Kaliuta		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS)) ; \
7899aacebeSYauheni Kaliuta	else \
7999aacebeSYauheni Kaliuta		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
801a940687SShuah Khan	fi
811a940687SShuah Khanelse
8299aacebeSYauheni Kaliuta	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
831a940687SShuah Khanendif
845e29a910SMichael Ellerman
85c363eb48SJiri Bencdefine INSTALL_SINGLE_RULE
86c363eb48SJiri Benc	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
8799aacebeSYauheni Kaliuta	$(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
88c363eb48SJiri Bencendef
89c363eb48SJiri Benc
9032dcfba6SMichael Ellermandefine INSTALL_RULE
91c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
92c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
93c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
94c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
95c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
96c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
97c363eb48SJiri Benc	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
9832dcfba6SMichael Ellermanendef
9932dcfba6SMichael Ellerman
10032dcfba6SMichael Ellermaninstall: all
10132dcfba6SMichael Ellermanifdef INSTALL_PATH
10232dcfba6SMichael Ellerman	$(INSTALL_RULE)
10332dcfba6SMichael Ellermanelse
10432dcfba6SMichael Ellerman	$(error Error: set INSTALL_PATH to use install)
10532dcfba6SMichael Ellermanendif
10632dcfba6SMichael Ellerman
107d4e59a53SKees Cookemit_tests:
1083df6131fSShuah Khan (Samsung OSG)	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
109a8ba798bSbamvor.zhangjian@huawei.com		BASENAME_TEST=`basename $$TEST`;	\
110bf660782SKees Cook		echo "	\\";				\
111bf660782SKees Cook		echo -n "	\"$$BASENAME_TEST\"";	\
112bf660782SKees Cook	done;						\
11332dcfba6SMichael Ellerman
1148050ef2bSShuah Khan# define if isn't already. It is undefined in make O= case.
1158050ef2bSShuah Khanifeq ($(RM),)
1168050ef2bSShuah KhanRM := rm -f
1178050ef2bSShuah Khanendif
1188050ef2bSShuah Khan
119df6438f9SShuah Khandefine CLEAN
12080d443e8Sbamvor.zhangjian@huawei.com	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
121df6438f9SShuah Khanendef
122df6438f9SShuah Khan
123df6438f9SShuah Khanclean:
124df6438f9SShuah Khan	$(CLEAN)
12588baa78dSbamvor.zhangjian@huawei.com
1268050ef2bSShuah Khan# When make O= with kselftest target from main level
1278050ef2bSShuah Khan# the following aren't defined.
1288050ef2bSShuah Khan#
129051f278eSMasahiro Yamadaifdef building_out_of_srctree
1308050ef2bSShuah KhanLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1318050ef2bSShuah KhanCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
1328050ef2bSShuah KhanLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1338050ef2bSShuah Khanendif
1348050ef2bSShuah Khan
1354e49ed2fSMathieu Desnoyers# Selftest makefiles can override those targets by setting
1364e49ed2fSMathieu Desnoyers# OVERRIDE_TARGETS = 1.
1374e49ed2fSMathieu Desnoyersifeq ($(OVERRIDE_TARGETS),)
1381056d3d2SShuah KhanLOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
1391056d3d2SShuah Khan$(OUTPUT)/%:%.c $(LOCAL_HDRS)
1402047f1d8SMichael Ellerman	$(LINK.c) $^ $(LDLIBS) -o $@
141a8ba798bSbamvor.zhangjian@huawei.com
142a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%.o:%.S
143634ce97cSMichael Ellerman	$(COMPILE.S) $^ -o $@
144a8ba798bSbamvor.zhangjian@huawei.com
145a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%:%.S
146634ce97cSMichael Ellerman	$(LINK.S) $^ $(LDLIBS) -o $@
1474e49ed2fSMathieu Desnoyersendif
1487d758af2Sbamvor.zhangjian@huawei.com
14932dcfba6SMichael Ellerman.PHONY: run_tests all clean install emit_tests
150