blob: 2c1a30bb8dd12391401cea76573a655d0bfa1338 [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
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060013# Enable code coverage if COV=1
14ifeq ($(COV),1)
15TEST_CFLAGS += --coverage
16TEST_LDFLAGS += --coverage
17endif
18
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020019stages := decompressor bootblock romstage smm verstage
20stages += ramstage rmodule postcar libagesa
Jan Dabrosef1c9682020-03-28 00:15:03 +010021
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020022alltests :=
23subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
24subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
25subdirs += tests/northbridge tests/security tests/soc tests/southbridge
26subdirs += tests/superio tests/vendorcode
Jan Dabrosef1c9682020-03-28 00:15:03 +010027
28define tests-handler
29alltests += $(1)$(2)
30$(foreach attribute,$(attributes),
31 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
32$(foreach attribute,$(attributes),
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020033 $(eval $(2)-$(attribute) := ))
Jan Dabrosef1c9682020-03-28 00:15:03 +010034
35# Sanity check for stage attribute value
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020036$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
Jan Dabrosef1c9682020-03-28 00:15:03 +010037$(if $(findstring $($(1)$(2)-stage), $(stages)),,
38 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
39 Check your $(dir $(1)$(2))Makefile.inc))
40endef
41
42$(call add-special-class, tests)
43$(call evaluate_subdirs)
44
Jan Dabrosef1c9682020-03-28 00:15:03 +010045$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020046 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +020047 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020048 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +010049 $(patsubst %.c,%.o,$($(test)-srcs)))))
50$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020051 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +010052$(foreach test, $(alltests), \
53 $(eval $(call TEST_CC_template,$(test))))
54
55$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020056 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +010057$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020058 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +010059
60DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
61-include $(DEPENDENCIES)
62
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020063.PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests))
64.PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests))
Jan Dabrosef1c9682020-03-28 00:15:03 +010065.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020066.PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests
Jan Dabrosef1c9682020-03-28 00:15:03 +010067
Jakub Czapiga4d9694e2021-10-07 09:06:42 +000068# %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
69# by macro cb_run_group_tests(), which should be used for running tests.
70# __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
Patrick Georgia0a198f2020-05-27 10:59:09 +020071ifeq ($(JUNIT_OUTPUT),y)
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020072$(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml
73$(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +020074endif
75
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020076$(addprefix run-,$(alltests)): run-%: $$(%-bin)
Jakub Czapiga4d9694e2021-10-07 09:06:42 +000077 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
78 rm -f $(testobj)/$(subst /,_,$^).failed
Raul E Rangel940953e2021-07-23 16:43:18 -060079 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +010080
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020081$(addprefix build-,$(alltests)): build-%: $$(%-bin)
82
83$(alltests): run-$$(@)
84
85$(addprefix try-,$(alltests)): try-%: clean-% $(TEST_COMMON_DEPENDENCIES)
86 mkdir -p $(testobj)/$*
87 echo "<testcase classname='coreboot_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \
88 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \
89 && type="system-out" || type="failure"; \
90 if [ $$type = "failure" ]; then \
91 echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \
92 else \
93 echo "<$$type>" >> $(testobj)/$*.tmp; \
94 fi; \
95 echo '<![CDATA[' >> $(testobj)/$*.tmp; \
96 cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \
97 echo "]]></$$type>" >> $(testobj)/$*.tmp; \
98 rm -f $(testobj)/$*.tmp.2; \
99 echo "</testcase>" >> $(testobj)/$*.tmp; \
100 if [ $$type != 'failure' ]; then \
101 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \
102 fi
103
104
105TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml
106
107$(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests))
108 mkdir -p $(dir $@)
109 echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@
110 for tst in $(alltests); do \
111 cat $(testobj)/$$tst.tmp >> $@; \
112 done
113 echo "</testsuite>" >> $@
114
115junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE)
116
117clean-junit.xml-unit-tests:
118 rm -f $(TESTS_BUILD_XML_FILE)
119
120
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600121# Build a code coverage report by collecting all the gcov files into a single
122# report. If COV is not set, this might be a user error, and they're trying
123# to generate a coverage report without first having built and run the code
124# with code coverage. So instead of silently correcting it by adding COV=1,
125# let's flag it to the user so they can be sure they're doing the thing they
126# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600127
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600128.PHONY: coverage-report clean-coverage-report
129
130ifeq ($(COV),1)
131coverage-report:
132 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
133 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600134 -s $(testobj)/tests.info
135
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600136clean-coverage-report:
137 rm -Rf $(testobj)/$(coverage_dir)
138else
139coverage-report:
140 COV=1 V=$(V) $(MAKE) coverage-report
141
142clean-coverage-report:
143 COV=1 V=$(V) $(MAKE) clean-coverage-report
144endif
145
Jan Dabrosef1c9682020-03-28 00:15:03 +0100146unit-tests: build-unit-tests run-unit-tests
147
148build-unit-tests: $(test-bins)
149
150run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200151 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
152 echo "**********************"; \
153 echo " TESTS FAILED"; \
154 echo "**********************"; \
155 exit 1; \
156 else \
157 echo "**********************"; \
158 echo " ALL TESTS PASSED"; \
159 echo "**********************"; \
160 exit 0; \
161 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100162
163$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600164 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100165
166clean-unit-tests:
167 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200168
169list-unit-tests:
170 @echo "unit-tests:"
171 for t in $(sort $(alltests)); do \
172 echo " $$t"; \
173 done
174
175help-unit-tests help::
176 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600177 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600178 @echo ' unit-tests - Run all unit-tests from tests/'
179 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
180 @echo ' list-unit-tests - List all unit-tests'
181 @echo ' <unit-test> - Build and run single unit-test'
182 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600183 @echo ' coverage-report - Generate a code coverage report'
184 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200185 @echo