1 # SPDX-License-Identifier: GPL-2.0-only
3 # Place the build output in one of two places depending on COV, so that code
4 # built with code coverage never mixes with code built without code coverage.
6 testobj := $(obj)/coverage
8 testobj := $(obj)/tests
11 include $(top)/tests/Makefile.common
13 # Enable GDB debug build if requested
15 ifneq ($(GDB_DEBUG),0)
19 # Enable code coverage if requested
21 TEST_CFLAGS += --coverage
22 TEST_LDFLAGS += --coverage
25 stages := decompressor bootblock romstage smm verstage
26 stages += ramstage rmodule postcar libagesa
29 subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
30 subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
31 subdirs += tests/northbridge tests/security tests/soc tests/southbridge
32 subdirs += tests/superio tests/vendorcode
36 $(foreach attribute,$(attributes),
37 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
38 $(foreach attribute,$(attributes),
39 $(eval $(2)-$(attribute) := ))
41 # Sanity check for stage attribute value
42 $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
43 $(if $(findstring $($(1)$(2)-stage), $(stages)),,
44 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
45 Check your $(dir $(1)$(2))Makefile.inc))
48 $(call add-special-class, tests)
49 $(call evaluate_subdirs)
51 $(foreach test, $(alltests), \
52 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
53 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
54 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
55 $(patsubst %.c,%.o,$($(test)-srcs)))))
56 $(foreach test, $(alltests), \
57 $(eval $(test)-bin := $(testobj)/$(test)/run))
58 $(foreach test, $(alltests), \
59 $(eval $(call TEST_CC_template,$(test))))
61 $(foreach test, $(alltests), \
62 $(eval all-test-objs += $($(test)-objs)))
63 $(foreach test, $(alltests), \
64 $(eval test-bins += $($(test)-bin)))
66 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
67 -include $(DEPENDENCIES)
69 .PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests))
70 .PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests))
71 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
72 .PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests
74 # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
75 # by macro cb_run_group_tests(), which should be used for running tests.
76 # __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
77 ifeq ($(JUNIT_OUTPUT),y)
78 $(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml
79 $(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
82 $(addprefix run-,$(alltests)): run-%: $$(%-bin)
83 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
84 rm -f $(testobj)/$(subst /,_,$^).failed
85 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
87 $(addprefix build-,$(alltests)): build-%: $$(%-bin)
89 $(alltests): run-$$(@)
91 $(addprefix try-,$(alltests)): try-%: clean-% $(TEST_COMMON_DEPENDENCIES)
92 mkdir -p $(testobj)/$*
93 echo "<testcase classname='coreboot_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \
94 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \
95 && type="system-out" || type="failure"; \
96 if [ $$type = "failure" ]; then \
97 echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \
99 echo "<$$type>" >> $(testobj)/$*.tmp; \
101 echo '<![CDATA[' >> $(testobj)/$*.tmp; \
102 cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \
103 echo "]]></$$type>" >> $(testobj)/$*.tmp; \
104 rm -f $(testobj)/$*.tmp.2; \
105 echo "</testcase>" >> $(testobj)/$*.tmp; \
106 if [ $$type != 'failure' ]; then \
107 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \
111 TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml
113 $(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests))
115 echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@
116 for tst in $(alltests); do \
117 cat $(testobj)/$$tst.tmp >> $@; \
119 echo "</testsuite>" >> $@
121 junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE)
123 clean-junit.xml-unit-tests:
124 rm -f $(TESTS_BUILD_XML_FILE)
127 # Build a code coverage report by collecting all the gcov files into a single
128 # report. If COV is not set, this might be a user error, and they're trying
129 # to generate a coverage report without first having built and run the code
130 # with code coverage. So instead of silently correcting it by adding COV=1,
131 # let's flag it to the user so they can be sure they're doing the thing they
134 .PHONY: coverage-report clean-coverage-report
138 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
139 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
140 -s $(testobj)/tests.info
142 clean-coverage-report:
143 rm -Rf $(testobj)/$(coverage_dir)
146 COV=1 V=$(V) $(MAKE) coverage-report
148 clean-coverage-report:
149 COV=1 V=$(V) $(MAKE) clean-coverage-report
152 unit-tests: build-unit-tests run-unit-tests
154 build-unit-tests: $(test-bins)
156 run-unit-tests: $(alltests)
157 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
158 echo "**********************"; \
159 echo " TESTS FAILED"; \
160 echo "**********************"; \
163 echo "**********************"; \
164 echo " ALL TESTS PASSED"; \
165 echo "**********************"; \
169 $(addprefix clean-,$(alltests)): clean-%:
177 for t in $(sort $(alltests)); do \
181 help-unit-tests help::
182 @echo '*** coreboot unit-tests targets ***'
183 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
184 @echo ' Use "GDB_DEBUG=1 make [target]" to build with debug symbols'
185 @echo ' unit-tests - Run all unit-tests from tests/'
186 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
187 @echo ' list-unit-tests - List all unit-tests'
188 @echo ' <unit-test> - Build and run single unit-test'
189 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
190 @echo ' coverage-report - Generate a code coverage report'
191 @echo ' clean-coverage-report - Remove the code coverage report'