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 code coverage if COV=1
15 TEST_CFLAGS += --coverage
16 TEST_LDFLAGS += --coverage
19 stages := decompressor bootblock romstage smm verstage
20 stages += ramstage rmodule postcar libagesa
23 subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
24 subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
25 subdirs += tests/northbridge tests/security tests/soc tests/southbridge
26 subdirs += tests/superio tests/vendorcode
30 $(foreach attribute,$(attributes),
31 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
32 $(foreach attribute,$(attributes),
33 $(eval $(2)-$(attribute) := ))
35 # Sanity check for stage attribute value
36 $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
37 $(if $(findstring $($(1)$(2)-stage), $(stages)),,
38 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
39 Check your $(dir $(1)$(2))Makefile.inc))
42 $(call add-special-class, tests)
43 $(call evaluate_subdirs)
45 $(foreach test, $(alltests), \
46 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
47 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
48 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
49 $(patsubst %.c,%.o,$($(test)-srcs)))))
50 $(foreach test, $(alltests), \
51 $(eval $(test)-bin := $(testobj)/$(test)/run))
52 $(foreach test, $(alltests), \
53 $(eval $(call TEST_CC_template,$(test))))
55 $(foreach test, $(alltests), \
56 $(eval all-test-objs += $($(test)-objs)))
57 $(foreach test, $(alltests), \
58 $(eval test-bins += $($(test)-bin)))
60 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
61 -include $(DEPENDENCIES)
63 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
64 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
66 # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
67 # by macro cb_run_group_tests(), which should be used for running tests.
68 # __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
69 ifeq ($(JUNIT_OUTPUT),y)
70 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
71 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
74 $(alltests): $$($$(@)-bin)
75 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
76 rm -f $(testobj)/$(subst /,_,$^).failed
77 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
79 # Build a code coverage report by collecting all the gcov files into a single
80 # report. If COV is not set, this might be a user error, and they're trying
81 # to generate a coverage report without first having built and run the code
82 # with code coverage. So instead of silently correcting it by adding COV=1,
83 # let's flag it to the user so they can be sure they're doing the thing they
86 .PHONY: coverage-report clean-coverage-report
90 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
91 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
92 -s $(testobj)/tests.info
94 clean-coverage-report:
95 rm -Rf $(testobj)/$(coverage_dir)
98 COV=1 V=$(V) $(MAKE) coverage-report
100 clean-coverage-report:
101 COV=1 V=$(V) $(MAKE) clean-coverage-report
104 unit-tests: build-unit-tests run-unit-tests
106 build-unit-tests: $(test-bins)
108 run-unit-tests: $(alltests)
109 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
110 echo "**********************"; \
111 echo " TESTS FAILED"; \
112 echo "**********************"; \
115 echo "**********************"; \
116 echo " ALL TESTS PASSED"; \
117 echo "**********************"; \
121 $(addprefix clean-,$(alltests)): clean-%:
129 for t in $(sort $(alltests)); do \
133 help-unit-tests help::
134 @echo '*** coreboot unit-tests targets ***'
135 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
136 @echo ' unit-tests - Run all unit-tests from tests/'
137 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
138 @echo ' list-unit-tests - List all unit-tests'
139 @echo ' <unit-test> - Build and run single unit-test'
140 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
141 @echo ' coverage-report - Generate a code coverage report'
142 @echo ' clean-coverage-report - Remove the code coverage report'