blob: debb2f9868c7355b6f3fc6bd78dee40c35f18d18 [file] [log] [blame]
Jan Dabrosef1c9682020-03-28 00:15:03 +01001# SPDX-License-Identifier: GPL-2.0-only
Jan Dabrosef1c9682020-03-28 00:15:03 +01002
3testobj = $(obj)/tests
4
5TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
6TEST_DOTCONFIG = $(testobj)/.config
7TEST_KCONFIG_AUTOHEADER := $(testobj)/config.h
8TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
9TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
10TEST_KCONFIG_SPLITCONFIG := $(testobj)/config
11TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
12
13TEST_CFLAGS = -include$(src)/include/kconfig.h \
14 -include$(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
15 -include $(src)/include/rules.h \
16
17# Include generic test mock headers, before original ones
18TEST_CFLAGS += -Itests/include/mocks
19
20TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
21 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \
22
23# Path for Kconfig autoheader
24TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
25
26TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections \
27 -Wl,--gc-sections -fno-builtin
28
29# Link against Cmocka
30TEST_LDFLAGS = -lcmocka
31
32# Extra attributes for unit tests, declared per test
33attributes:= srcs cflags mocks stage
34
35stages:= decompressor bootblock romstage smm verstage
36stages+= ramstage rmodule postcar libagesa
37
38alltests:=
39subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device
40subdirs+= tests/drivers tests/ec tests/lib tests/mainboard
41subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
42subdirs+= tests/superio tests/vendorcode
43
44define tests-handler
45alltests += $(1)$(2)
46$(foreach attribute,$(attributes),
47 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
48$(foreach attribute,$(attributes),
49 $(eval $(2)-$(attribute):=))
50
51# Sanity check for stage attribute value
52$(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
53$(if $(findstring $($(1)$(2)-stage), $(stages)),,
54 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
55 Check your $(dir $(1)$(2))Makefile.inc))
56endef
57
58$(call add-special-class, tests)
59$(call evaluate_subdirs)
60
61# Create actual targets for unit test binaries
62# $1 - test name
63define TEST_CC_template
64$($(1)-objs): TEST_CFLAGS+= \
65 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
66$($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $(TEST_KCONFIG_AUTOHEADER)
67 mkdir -p $$(dir $$@)
68 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
69 -MT $$@ -c $$< -o $$@
70
71$($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
72$($(1)-bin): $($(1)-objs)
73 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
74
75endef
76
77$(foreach test, $(alltests), \
78 $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \
79 $(patsubst %.c,%.o,$($(test)-srcs)))))
80$(foreach test, $(alltests), \
81 $(eval $(test)-bin:=$(obj)/$(test)/run))
82$(foreach test, $(alltests), \
83 $(eval $(call TEST_CC_template,$(test))))
84
85$(foreach test, $(alltests), \
86 $(eval all-test-objs+=$($(test)-objs)))
87$(foreach test, $(alltests), \
88 $(eval test-bins+=$($(test)-bin)))
89
90DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
91-include $(DEPENDENCIES)
92
93# Kconfig targets
94$(TEST_DOTCONFIG):
95 mkdir -p $(dir $@)
96 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
97
98# Don't override default Kconfig variables, since this will affect all
99# Kconfig targets. Change them only when calling sub-make instead.
100$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
101 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
102 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
103 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
104 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
105 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
106 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
107
108$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
109 mkdir -p $(dir $@)
110 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
111 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
112
113$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
114 true
115
116.PHONY: $(alltests) $(addprefix clean-,$(alltests))
117.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
118
119$(alltests): $$($$(@)-bin)
120 ./$^
121
122unit-tests: build-unit-tests run-unit-tests
123
124build-unit-tests: $(test-bins)
125
126run-unit-tests: $(alltests)
127 echo "**********************"
128 echo " ALL TESTS PASSED"
129 echo "**********************"
130
131$(addprefix clean-,$(alltests)): clean-%:
132 rm -rf $(obj)/$*
133
134clean-unit-tests:
135 rm -rf $(testobj)