xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 42d46e57)
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))
68ce72dc3SShuah Khan    ifneq ($(O),)
78ce72dc3SShuah Khan	OUTPUT := $(O)
88ce72dc3SShuah Khan    else
98ce72dc3SShuah Khan	ifneq ($(KBUILD_OUTPUT),)
108ce72dc3SShuah Khan		OUTPUT := $(KBUILD_OUTPUT)
118ce72dc3SShuah Khan	else
12e53aff45SShuah Khan		OUTPUT := $(shell pwd)
138ce72dc3SShuah Khan		DEFAULT_INSTALL_HDR_PATH := 1
148ce72dc3SShuah Khan	endif
158ce72dc3SShuah Khan    endif
16e53aff45SShuah Khanendif
17*42d46e57SKees Cookselfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
18e53aff45SShuah Khan
19be16a244SShuah Khan# The following are built by lib.mk common compile rules.
20be16a244SShuah Khan# TEST_CUSTOM_PROGS should be used by tests that require
21be16a244SShuah Khan# custom build rule and prevent common build rule use.
22be16a244SShuah Khan# TEST_PROGS are for test shell scripts.
23be16a244SShuah Khan# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
24be16a244SShuah Khan# and install targets. Common clean doesn't touch them.
25d83c3ba0SMichael EllermanTEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
268050ef2bSShuah KhanTEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
27d83c3ba0SMichael EllermanTEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
28d83c3ba0SMichael Ellerman
29211929fdSShuah Khanifdef KSFT_KHDR_INSTALL
30b2d35fa5SAnders Roxelltop_srcdir ?= ../../../..
31b2d35fa5SAnders Roxellinclude $(top_srcdir)/scripts/subarch.include
32b2d35fa5SAnders RoxellARCH		?= $(SUBARCH)
33b2d35fa5SAnders Roxell
348ce72dc3SShuah Khan# set default goal to all, so make without a target runs all, even when
358ce72dc3SShuah Khan# all isn't the first target in the file.
368ce72dc3SShuah Khan.DEFAULT_GOAL := all
378ce72dc3SShuah Khan
388ce72dc3SShuah Khan# Invoke headers install with --no-builtin-rules to avoid circular
398ce72dc3SShuah Khan# dependency in "make kselftest" case. In this case, second level
408ce72dc3SShuah Khan# make inherits builtin-rules which will use the rule generate
418ce72dc3SShuah Khan# Makefile.o and runs into
428ce72dc3SShuah Khan# "Circular Makefile.o <- prepare dependency dropped."
438ce72dc3SShuah Khan# and headers_install fails and test compile fails.
448ce72dc3SShuah Khan# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
458ce72dc3SShuah Khan# invokes them as sub-makes and --no-builtin-rules is not necessary,
468ce72dc3SShuah Khan# but doesn't cause any failures. Keep it simple and use the same
478ce72dc3SShuah Khan# flags in both cases.
488ce72dc3SShuah Khan# Note that the support to install headers from lib.mk is necessary
498ce72dc3SShuah Khan# when test Makefile is run directly with "make -C".
508ce72dc3SShuah Khan# When local build is done, headers are installed in the default
518ce72dc3SShuah Khan# INSTALL_HDR_PATH usr/include.
52b2d35fa5SAnders Roxell.PHONY: khdr
53b2d35fa5SAnders Roxellkhdr:
548ce72dc3SShuah Khanifndef KSFT_KHDR_INSTALL_DONE
558ce72dc3SShuah Khanifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
568ce72dc3SShuah Khan	make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
578ce72dc3SShuah Khanelse
588ce72dc3SShuah Khan	make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
598ce72dc3SShuah Khan		ARCH=$(ARCH) -C $(top_srcdir) headers_install
608ce72dc3SShuah Khanendif
618ce72dc3SShuah Khanendif
62b2d35fa5SAnders Roxell
63211929fdSShuah Khanall: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
64211929fdSShuah Khanelse
65211929fdSShuah Khanall: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
66b2d35fa5SAnders Roxellendif
67b2d35fa5SAnders Roxell
681f87c7c1SShuah Khan.ONESHELL:
697afed3dcSShuah Khan (Samsung OSG)define RUN_TESTS
707afed3dcSShuah Khan (Samsung OSG)	@export KSFT_TAP_LEVEL=`echo 1`;		\
717afed3dcSShuah Khan (Samsung OSG)	test_num=`echo 0`;				\
72*42d46e57SKees Cook	. $(selfdir)/kselftest/runner.sh;		\
737afed3dcSShuah Khan (Samsung OSG)	echo "TAP version 13";				\
747afed3dcSShuah Khan (Samsung OSG)	for TEST in $(1); do				\
757afed3dcSShuah Khan (Samsung OSG)		BASENAME_TEST=`basename $$TEST`;	\
767afed3dcSShuah Khan (Samsung OSG)		test_num=`echo $$test_num+1 | bc`;	\
77*42d46e57SKees Cook		if [ "X$(summary)" != "X" ]; then	\
78*42d46e57SKees Cook		        logfile="/tmp/$$BASENAME_TEST";	\
79*42d46e57SKees Cook			cat /dev/null > "$$logfile";	\
80*42d46e57SKees Cook		fi;					\
81*42d46e57SKees Cook		run_one "$$BASENAME_TEST" "$$test_num";	\
825e29a910SMichael Ellerman	done;
835e29a910SMichael Ellermanendef
845e29a910SMichael Ellerman
855e29a910SMichael Ellermanrun_tests: all
861a940687SShuah Khanifneq ($(KBUILD_SRC),)
871a940687SShuah Khan	@if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
881a940687SShuah Khan		@rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
891a940687SShuah Khan	fi
901a940687SShuah Khan	@if [ "X$(TEST_PROGS)" != "X" ]; then
911a940687SShuah Khan		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
921a940687SShuah Khan	else
931a940687SShuah Khan		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
941a940687SShuah Khan	fi
951a940687SShuah Khanelse
96be16a244SShuah Khan	$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
971a940687SShuah Khanendif
985e29a910SMichael Ellerman
9932dcfba6SMichael Ellermandefine INSTALL_RULE
100a7d0f078SBamvor Jian Zhang	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then					\
101900d65eeSBamvor Jian Zhang		mkdir -p ${INSTALL_PATH};										\
1025fe59799Sbamvor.zhangjian@huawei.com		echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";	\
1035fe59799Sbamvor.zhangjian@huawei.com		rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;		\
104a7d0f078SBamvor Jian Zhang	fi
105be16a244SShuah Khan	@if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then					\
10688baa78dSbamvor.zhangjian@huawei.com		mkdir -p ${INSTALL_PATH};										\
107be16a244SShuah Khan		echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";	\
108be16a244SShuah Khan		rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;		\
10988baa78dSbamvor.zhangjian@huawei.com	fi
11032dcfba6SMichael Ellermanendef
11132dcfba6SMichael Ellerman
11232dcfba6SMichael Ellermaninstall: all
11332dcfba6SMichael Ellermanifdef INSTALL_PATH
11432dcfba6SMichael Ellerman	$(INSTALL_RULE)
11532dcfba6SMichael Ellermanelse
11632dcfba6SMichael Ellerman	$(error Error: set INSTALL_PATH to use install)
11732dcfba6SMichael Ellermanendif
11832dcfba6SMichael Ellerman
11932dcfba6SMichael Ellermandefine EMIT_TESTS
1203df6131fSShuah Khan (Samsung OSG)	@test_num=`echo 0`;				\
1213df6131fSShuah Khan (Samsung OSG)	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
122a8ba798bSbamvor.zhangjian@huawei.com		BASENAME_TEST=`basename $$TEST`;	\
1233df6131fSShuah Khan (Samsung OSG)		test_num=`echo $$test_num+1 | bc`;	\
1243df6131fSShuah Khan (Samsung OSG)		TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST";	\
1253df6131fSShuah Khan (Samsung OSG)		echo "echo $$TEST_HDR_MSG";	\
126a548de0fSShuah Khan (Samsung OSG)		if [ ! -x $$TEST ]; then	\
127a548de0fSShuah Khan (Samsung OSG)			echo "echo \"$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.\"";		\
128a548de0fSShuah Khan (Samsung OSG)			echo "echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\""; \
129a548de0fSShuah Khan (Samsung OSG)		else
1303df6131fSShuah Khan (Samsung OSG)			echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"ok 1..$$test_num $$TEST_HDR_MSG [PASS]\") || (if [ \$$? -eq \$$skip ]; then echo \"not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]\"; else echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\"; fi;)"; \
131a548de0fSShuah Khan (Samsung OSG)		fi;		\
13232dcfba6SMichael Ellerman	done;
13332dcfba6SMichael Ellermanendef
13432dcfba6SMichael Ellerman
13532dcfba6SMichael Ellermanemit_tests:
13632dcfba6SMichael Ellerman	$(EMIT_TESTS)
13732dcfba6SMichael Ellerman
1388050ef2bSShuah Khan# define if isn't already. It is undefined in make O= case.
1398050ef2bSShuah Khanifeq ($(RM),)
1408050ef2bSShuah KhanRM := rm -f
1418050ef2bSShuah Khanendif
1428050ef2bSShuah Khan
143df6438f9SShuah Khandefine CLEAN
14480d443e8Sbamvor.zhangjian@huawei.com	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
145df6438f9SShuah Khanendef
146df6438f9SShuah Khan
147df6438f9SShuah Khanclean:
148df6438f9SShuah Khan	$(CLEAN)
14988baa78dSbamvor.zhangjian@huawei.com
1508050ef2bSShuah Khan# When make O= with kselftest target from main level
1518050ef2bSShuah Khan# the following aren't defined.
1528050ef2bSShuah Khan#
1538050ef2bSShuah Khanifneq ($(KBUILD_SRC),)
1548050ef2bSShuah KhanLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1558050ef2bSShuah KhanCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
1568050ef2bSShuah KhanLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
1578050ef2bSShuah Khanendif
1588050ef2bSShuah Khan
1594e49ed2fSMathieu Desnoyers# Selftest makefiles can override those targets by setting
1604e49ed2fSMathieu Desnoyers# OVERRIDE_TARGETS = 1.
1614e49ed2fSMathieu Desnoyersifeq ($(OVERRIDE_TARGETS),)
162a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%:%.c
1632047f1d8SMichael Ellerman	$(LINK.c) $^ $(LDLIBS) -o $@
164a8ba798bSbamvor.zhangjian@huawei.com
165a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%.o:%.S
166634ce97cSMichael Ellerman	$(COMPILE.S) $^ -o $@
167a8ba798bSbamvor.zhangjian@huawei.com
168a8ba798bSbamvor.zhangjian@huawei.com$(OUTPUT)/%:%.S
169634ce97cSMichael Ellerman	$(LINK.S) $^ $(LDLIBS) -o $@
1704e49ed2fSMathieu Desnoyersendif
1717d758af2Sbamvor.zhangjian@huawei.com
17232dcfba6SMichael Ellerman.PHONY: run_tests all clean install emit_tests
173