tests: Always run all unit tests
[coreboot.git] / tests / Makefile.inc
blobbfd18060abc8f19943f027921668fd751c9fc6a4
1 # SPDX-License-Identifier: GPL-2.0-only
3 testsrc = $(top)/tests
4 testobj = $(obj)/tests
5 cmockasrc = 3rdparty/cmocka
6 cmockaobj = $(objutil)/cmocka
8 CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
10 CMAKE:= cmake
12 TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
13 TEST_DOTCONFIG = $(testobj)/.config
14 TEST_KCONFIG_AUTOHEADER := $(testobj)/config.h
15 TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
16 TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
17 TEST_KCONFIG_SPLITCONFIG := $(testobj)/config
18 TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
20 TEST_CFLAGS = -include $(src)/include/kconfig.h \
21         -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
22         -include $(src)/include/rules.h
24 # Include generic test mock headers, before original ones
25 TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
27 TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
28         -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \
30 # Path for Kconfig autoheader
31 TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
33 TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
35 # Checkout Cmocka repository
36 forgetthis:=$(shell git submodule update --init --checkout 3rdparty/cmocka)
38 TEST_CFLAGS += -I$(cmockasrc)/include
40 # Link against Cmocka
41 TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
42 TEST_LDFLAGS += -Wl,--gc-sections
44 # Extra attributes for unit tests, declared per test
45 attributes:= srcs cflags mocks stage
47 stages:= decompressor bootblock romstage smm verstage
48 stages+= ramstage rmodule postcar libagesa
50 alltests:=
51 subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device
52 subdirs+= tests/drivers tests/ec tests/lib tests/mainboard
53 subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
54 subdirs+= tests/superio tests/vendorcode
56 define tests-handler
57 alltests += $(1)$(2)
58 $(foreach attribute,$(attributes),
59         $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
60 $(foreach attribute,$(attributes),
61         $(eval $(2)-$(attribute):=))
63 # Sanity check for stage attribute value
64 $(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
65 $(if $(findstring $($(1)$(2)-stage), $(stages)),,
66         $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
67                 Check your $(dir $(1)$(2))Makefile.inc))
68 endef
70 $(call add-special-class, tests)
71 $(call evaluate_subdirs)
73 # Create actual targets for unit test binaries
74 # $1 - test name
75 define TEST_CC_template
76 $($(1)-objs): TEST_CFLAGS+= \
77         -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
78 $($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $(TEST_KCONFIG_AUTOHEADER)
79         mkdir -p $$(dir $$@)
80         $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags)  -MMD \
81                 -MT $$@ -c $$< -o $$@
83 $($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
84 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
85         $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
87 endef
89 $(foreach test, $(alltests), \
90         $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \
91                 $(patsubst %.c,%.o,$($(test)-srcs)))))
92 $(foreach test, $(alltests), \
93         $(eval $(test)-bin:=$(obj)/$(test)/run))
94 $(foreach test, $(alltests), \
95         $(eval $(call TEST_CC_template,$(test))))
97 $(foreach test, $(alltests), \
98         $(eval all-test-objs+=$($(test)-objs)))
99 $(foreach test, $(alltests), \
100         $(eval test-bins+=$($(test)-bin)))
102 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
103 -include $(DEPENDENCIES)
105 # Build cmocka
106 $(CMOCKA_LIB):
107         echo "*** Building CMOCKA ***"
108         mkdir -p $(cmockaobj)
109         cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
110         $(MAKE) -C $(cmockaobj)
112 # Kconfig targets
113 $(TEST_DOTCONFIG):
114         mkdir -p $(dir $@)
115         cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
117 # Don't override default Kconfig variables, since this will affect all
118 # Kconfig targets. Change them only when calling sub-make instead.
119 $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
120         KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
121         KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
122         KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
123         KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
124         KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
125         KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
127 $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
128         mkdir -p $(dir $@)
129         +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
130         +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
132 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
133         true
135 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
136 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
138 ifeq ($(JUNIT_OUTPUT),y)
139 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
140 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^).xml
141 endif
143 $(alltests): $$($$(@)-bin)
144         rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
145         -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
147 unit-tests: build-unit-tests run-unit-tests
149 build-unit-tests: $(test-bins)
151 run-unit-tests: $(alltests)
152         if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
153                 echo "**********************"; \
154                 echo "     TESTS FAILED"; \
155                 echo "**********************"; \
156                 exit 1; \
157         else \
158                 echo "**********************"; \
159                 echo "   ALL TESTS PASSED"; \
160                 echo "**********************"; \
161                 exit 0; \
162         fi
164 $(addprefix clean-,$(alltests)): clean-%:
165         rm -rf $(obj)/$*
167 clean-unit-tests:
168         rm -rf $(testobj)