blob: 530c3c6f0f29c674840d52da14cb777c9782d846 [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 Czapigae744ba62022-11-14 11:22:59 +010054 $(eval $(test)-sysobjs := $(addprefix $(testobj)/$(test)/, \
55 $(patsubst %.c,%.o,$($(test)-syssrcs)))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020056 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +010057 $(patsubst %.c,%.o,$($(test)-srcs)))))
58$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020059 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +010060$(foreach test, $(alltests), \
61 $(eval $(call TEST_CC_template,$(test))))
62
63$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020064 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +010065$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020066 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +010067
68DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
69-include $(DEPENDENCIES)
70
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020071.PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests))
72.PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests))
Jan Dabrosef1c9682020-03-28 00:15:03 +010073.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020074.PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests
Jan Dabrosef1c9682020-03-28 00:15:03 +010075
Jakub Czapiga4d9694e2021-10-07 09:06:42 +000076# %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
77# by macro cb_run_group_tests(), which should be used for running tests.
78# __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
Patrick Georgia0a198f2020-05-27 10:59:09 +020079ifeq ($(JUNIT_OUTPUT),y)
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020080$(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml
81$(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +020082endif
83
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020084$(addprefix run-,$(alltests)): run-%: $$(%-bin)
Jakub Czapiga4d9694e2021-10-07 09:06:42 +000085 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
86 rm -f $(testobj)/$(subst /,_,$^).failed
Raul E Rangel940953e2021-07-23 16:43:18 -060087 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +010088
Jakub Czapigaa0e36d82022-09-06 10:42:16 +020089$(addprefix build-,$(alltests)): build-%: $$(%-bin)
90
91$(alltests): run-$$(@)
92
93$(addprefix try-,$(alltests)): try-%: clean-% $(TEST_COMMON_DEPENDENCIES)
94 mkdir -p $(testobj)/$*
95 echo "<testcase classname='coreboot_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \
96 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \
97 && type="system-out" || type="failure"; \
98 if [ $$type = "failure" ]; then \
99 echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \
100 else \
101 echo "<$$type>" >> $(testobj)/$*.tmp; \
102 fi; \
103 echo '<![CDATA[' >> $(testobj)/$*.tmp; \
104 cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \
105 echo "]]></$$type>" >> $(testobj)/$*.tmp; \
106 rm -f $(testobj)/$*.tmp.2; \
107 echo "</testcase>" >> $(testobj)/$*.tmp; \
108 if [ $$type != 'failure' ]; then \
109 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \
110 fi
111
112
113TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml
114
115$(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests))
116 mkdir -p $(dir $@)
117 echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@
118 for tst in $(alltests); do \
119 cat $(testobj)/$$tst.tmp >> $@; \
120 done
121 echo "</testsuite>" >> $@
122
123junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE)
124
125clean-junit.xml-unit-tests:
126 rm -f $(TESTS_BUILD_XML_FILE)
127
128
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600129# Build a code coverage report by collecting all the gcov files into a single
130# report. If COV is not set, this might be a user error, and they're trying
131# to generate a coverage report without first having built and run the code
132# with code coverage. So instead of silently correcting it by adding COV=1,
133# let's flag it to the user so they can be sure they're doing the thing they
134# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600135
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600136.PHONY: coverage-report clean-coverage-report
137
138ifeq ($(COV),1)
139coverage-report:
140 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
141 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600142 -s $(testobj)/tests.info
143
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600144clean-coverage-report:
145 rm -Rf $(testobj)/$(coverage_dir)
146else
147coverage-report:
148 COV=1 V=$(V) $(MAKE) coverage-report
149
150clean-coverage-report:
151 COV=1 V=$(V) $(MAKE) clean-coverage-report
152endif
153
Jan Dabrosef1c9682020-03-28 00:15:03 +0100154unit-tests: build-unit-tests run-unit-tests
155
156build-unit-tests: $(test-bins)
157
158run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200159 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
160 echo "**********************"; \
161 echo " TESTS FAILED"; \
162 echo "**********************"; \
163 exit 1; \
164 else \
165 echo "**********************"; \
166 echo " ALL TESTS PASSED"; \
167 echo "**********************"; \
168 exit 0; \
169 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100170
171$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600172 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100173
174clean-unit-tests:
175 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200176
177list-unit-tests:
178 @echo "unit-tests:"
179 for t in $(sort $(alltests)); do \
180 echo " $$t"; \
181 done
182
183help-unit-tests help::
184 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600185 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Martin Roth96edc102022-09-27 18:30:55 -0600186 @echo ' Use "GDB_DEBUG=1 make [target]" to build with debug symbols'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600187 @echo ' unit-tests - Run all unit-tests from tests/'
188 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
189 @echo ' list-unit-tests - List all unit-tests'
190 @echo ' <unit-test> - Build and run single unit-test'
191 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600192 @echo ' coverage-report - Generate a code coverage report'
193 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200194 @echo