blob: 90f01d159a733afec206ef590444f5e33bc97f02 [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
Paul Fagerburgde6cbac2021-05-11 09:56:48 -06003# 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.
5ifeq ($(COV),1)
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +02006testobj := $(obj)/coverage
Paul Fagerburgde6cbac2021-05-11 09:56:48 -06007else
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +02008testobj := $(obj)/tests
Paul Fagerburgde6cbac2021-05-11 09:56:48 -06009endif
10
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020011include $(top)/tests/Makefile.common
Julius Werner84446e62021-02-12 17:37:27 -080012
Martin Roth96edc102022-09-27 18:30:55 -060013# Enable GDB debug build if requested
14GDB_DEBUG ?= 0
15ifneq ($(GDB_DEBUG),0)
16TEST_CFLAGS += -g -Og
17endif
18
19# Enable code coverage if requested
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060020ifeq ($(COV),1)
21TEST_CFLAGS += --coverage
22TEST_LDFLAGS += --coverage
23endif
24
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020025stages := decompressor bootblock romstage smm verstage
26stages += ramstage rmodule postcar libagesa
Jan Dabrosef1c9682020-03-28 00:15:03 +010027
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020028alltests :=
29subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
30subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
31subdirs += tests/northbridge tests/security tests/soc tests/southbridge
32subdirs += tests/superio tests/vendorcode
Jan Dabrosef1c9682020-03-28 00:15:03 +010033
34define tests-handler
35alltests += $(1)$(2)
36$(foreach attribute,$(attributes),
37 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
38$(foreach attribute,$(attributes),
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020039 $(eval $(2)-$(attribute) := ))
Jan Dabrosef1c9682020-03-28 00:15:03 +010040
41# Sanity check for stage attribute value
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020042$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
Jan Dabrosef1c9682020-03-28 00:15:03 +010043$(if $(findstring $($(1)$(2)-stage), $(stages)),,
44 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
45 Check your $(dir $(1)$(2))Makefile.inc))
46endef
47
48$(call add-special-class, tests)
49$(call evaluate_subdirs)
50
Jan Dabrosef1c9682020-03-28 00:15:03 +010051$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020052 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +020053 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020054 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +010055 $(patsubst %.c,%.o,$($(test)-srcs)))))
56$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020057 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +010058$(foreach test, $(alltests), \
59 $(eval $(call TEST_CC_template,$(test))))
60
61$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020062 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +010063$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020064 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +010065
66DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
67-include $(DEPENDENCIES)
68
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020069.PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests))
70.PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests))
Jan Dabrosef1c9682020-03-28 00:15:03 +010071.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020072.PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests
Jan Dabrosef1c9682020-03-28 00:15:03 +010073
Jakub Czapiga4d9694e2021-10-07 09:06:42 +000074# %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
Patrick Georgia0a198f2020-05-27 10:59:09 +020077ifeq ($(JUNIT_OUTPUT),y)
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020078$(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml
79$(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +020080endif
81
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020082$(addprefix run-,$(alltests)): run-%: $$(%-bin)
Jakub Czapiga4d9694e2021-10-07 09:06:42 +000083 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
84 rm -f $(testobj)/$(subst /,_,$^).failed
Raul E Rangel940953e2021-07-23 16:43:18 -060085 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +010086
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020087$(addprefix build-,$(alltests)): build-%: $$(%-bin)
88
89$(alltests): run-$$(@)
90
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; \
98 else \
99 echo "<$$type>" >> $(testobj)/$*.tmp; \
100 fi; \
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-$*"; \
108 fi
109
110
111TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml
112
113$(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests))
114 mkdir -p $(dir $@)
115 echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@
116 for tst in $(alltests); do \
117 cat $(testobj)/$$tst.tmp >> $@; \
118 done
119 echo "</testsuite>" >> $@
120
121junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE)
122
123clean-junit.xml-unit-tests:
124 rm -f $(TESTS_BUILD_XML_FILE)
125
126
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600127# 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
132# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600133
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600134.PHONY: coverage-report clean-coverage-report
135
136ifeq ($(COV),1)
137coverage-report:
138 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
139 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600140 -s $(testobj)/tests.info
141
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600142clean-coverage-report:
143 rm -Rf $(testobj)/$(coverage_dir)
144else
145coverage-report:
146 COV=1 V=$(V) $(MAKE) coverage-report
147
148clean-coverage-report:
149 COV=1 V=$(V) $(MAKE) clean-coverage-report
150endif
151
Jan Dabrosef1c9682020-03-28 00:15:03 +0100152unit-tests: build-unit-tests run-unit-tests
153
154build-unit-tests: $(test-bins)
155
156run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200157 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
158 echo "**********************"; \
159 echo " TESTS FAILED"; \
160 echo "**********************"; \
161 exit 1; \
162 else \
163 echo "**********************"; \
164 echo " ALL TESTS PASSED"; \
165 echo "**********************"; \
166 exit 0; \
167 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100168
169$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600170 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100171
172clean-unit-tests:
173 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200174
175list-unit-tests:
176 @echo "unit-tests:"
177 for t in $(sort $(alltests)); do \
178 echo " $$t"; \
179 done
180
181help-unit-tests help::
182 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600183 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Martin Roth96edc102022-09-27 18:30:55 -0600184 @echo ' Use "GDB_DEBUG=1 make [target]" to build with debug symbols'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600185 @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'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600190 @echo ' coverage-report - Generate a code coverage report'
191 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200192 @echo