xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 1056d3d2)
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
621f87c7c1SShuah Khan.ONESHELL:
637afed3dcSShuah Khan (Samsung OSG)define RUN_TESTS
645c069b6dSKees Cook	@BASE_DIR="$(selfdir)";			\
655c069b6dSKees Cook	. $(selfdir)/kselftest/runner.sh;	\
6642d46e57SKees Cook	if [ "X$(summary)" != "X" ]; then       \
67bf660782SKees Cook		per_test_logging=1;		\
6842d46e57SKees Cook	fi;                                     \
69bf660782SKees Cook	run_many $(1)
705e29a910SMichael Ellermanendef
715e29a910SMichael Ellerman
725e29a910SMichael Ellermanrun_tests: all
73051f278eSMasahiro Yamadaifdef building_out_of_srctree
741a940687SShuah Khan	@if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
751a940687SShuah Khan		@rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
761a940687SShuah Khan	fi
771a940687SShuah Khan	@if [ "X$(TEST_PROGS)" != "X" ]; then
781a940687SShuah Khan		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
791a940687SShuah Khan	else
801a940687SShuah Khan		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
811a940687SShuah Khan	fi
821a940687SShuah Khanelse
83be16a244SShuah Khan	$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
841a940687SShuah Khanendif
855e29a910SMichael Ellerman
8632dcfba6SMichael Ellermandefine INSTALL_RULE
87a7d0f078SBamvor Jian Zhang	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then					\
88900d65eeSBamvor Jian Zhang		mkdir -p ${INSTALL_PATH};										\
895fe59799Sbamvor.zhangjian@huawei.com		echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";	\
905fe59799Sbamvor.zhangjian@huawei.com		rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;		\
91a7d0f078SBamvor Jian Zhang	fi
92be16a244SShuah Khan	@if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then					\
9388baa78dSbamvor.zhangjian@huawei.com		mkdir -p ${INSTALL_PATH};										\
94be16a244SShuah Khan		echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";	\
95be16a244SShuah Khan		rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;		\
9688baa78dSbamvor.zhangjian@huawei.com	fi
9732dcfba6SMichael Ellermanendef
9832dcfba6SMichael Ellerman
9932dcfba6SMichael Ellermaninstall: all
10032dcfba6SMichael Ellermanifdef INSTALL_PATH
10132dcfba6SMichael Ellerman	$(INSTALL_RULE)
10232dcfba6SMichael Ellermanelse
10332dcfba6SMichael Ellerman	$(error Error: set INSTALL_PATH to use install)
10432dcfba6SMichael Ellermanendif
10532dcfba6SMichael Ellerman
106d4e59a53SKees Cookemit_tests:
1073df6131fSShuah Khan (Samsung OSG)	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
108a8ba798bSbamvor.zhangjian@huawei.com		BASENAME_TEST=`basename $$TEST`;	\
109bf660782SKees Cook		echo "	\\";				\
110bf660782SKees Cook		echo -n "	\"$$BASENAME_TEST\"";	\
111bf660782SKees Cook	done;						\
11232dcfba6SMichael Ellerman
1138050ef2bSShuah Khan# define if isn't already. It is undefined in make O= case.
1148050ef2bSShuah Khanifeq ($(RM),)
1158050ef2bSShuah KhanRM := rm -f
1168050ef2bSShuah Khanendif
1178050ef2bSShuah Khan
118df6438f9SShuah Khandefine CLEAN
11980d443e8Sbamvor.zhangjian@huawei.com	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
120df6438f9SShuah Khanendef
121df6438f9SShuah Khan
122df6438f9SShuah Khanclean:
123df6438f9SShuah Khan	$(CLEAN)
12488baa78dSbamvor.zhangjian@huawei.com
1258050ef2bSShuah Khan# When make O= with kselftest target from main level
1268050ef2bSShuah Khan# the following aren't defined.
1278050ef2bSShuah Khan#
128051f278eSMasahiro Yamadaifdef building_out_of_srctree
1298050ef2bSShuah KhanLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1308050ef2bSShuah KhanCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
1318050ef2bSShuah KhanLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1328050ef2bSShuah Khanendif
1338050ef2bSShuah Khan
1344e49ed2fSMathieu Desnoyers# Selftest makefiles can override those targets by setting
1354e49ed2fSMathieu Desnoyers# OVERRIDE_TARGETS = 1.
1364e49ed2fSMathieu Desnoyersifeq ($(OVERRIDE_TARGETS),)
137*1056d3d2SShuah KhanLOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
138*1056d3d2SShuah Khan$(OUTPUT)/%:%.c $(LOCAL_HDRS)
1392047f1d8SMichael Ellerman	$(LINK.c) $^ $(LDLIBS) -o $@
140a8ba798bSbamvor.zhangjian@huawei.com
141a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%.o:%.S
142634ce97cSMichael Ellerman	$(COMPILE.S) $^ -o $@
143a8ba798bSbamvor.zhangjian@huawei.com
144a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%:%.S
145634ce97cSMichael Ellerman	$(LINK.S) $^ $(LDLIBS) -o $@
1464e49ed2fSMathieu Desnoyersendif
1477d758af2Sbamvor.zhangjian@huawei.com
14832dcfba6SMichael Ellerman.PHONY: run_tests all clean install emit_tests
149