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