Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0-only |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 2 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 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. |
| 5 | ifeq ($(COV),1) |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 6 | testobj := $(obj)/coverage |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 7 | else |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 8 | testobj := $(obj)/tests |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 9 | endif |
| 10 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 11 | include $(top)/tests/Makefile.common |
Julius Werner | 84446e6 | 2021-02-12 17:37:27 -0800 | [diff] [blame] | 12 | |
Martin Roth | 96edc10 | 2022-09-27 18:30:55 -0600 | [diff] [blame] | 13 | # Enable GDB debug build if requested |
| 14 | GDB_DEBUG ?= 0 |
| 15 | ifneq ($(GDB_DEBUG),0) |
| 16 | TEST_CFLAGS += -g -Og |
| 17 | endif |
| 18 | |
| 19 | # Enable code coverage if requested |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 20 | ifeq ($(COV),1) |
| 21 | TEST_CFLAGS += --coverage |
| 22 | TEST_LDFLAGS += --coverage |
| 23 | endif |
| 24 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 25 | stages := decompressor bootblock romstage smm verstage |
| 26 | stages += ramstage rmodule postcar libagesa |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 27 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 28 | alltests := |
| 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 |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 33 | |
| 34 | define tests-handler |
| 35 | alltests += $(1)$(2) |
| 36 | $(foreach attribute,$(attributes), |
| 37 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 38 | $(foreach attribute,$(attributes), |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 39 | $(eval $(2)-$(attribute) := )) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 40 | |
| 41 | # Sanity check for stage attribute value |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 42 | $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 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)) |
| 46 | endef |
| 47 | |
| 48 | $(call add-special-class, tests) |
| 49 | $(call evaluate_subdirs) |
| 50 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 51 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 52 | $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \ |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 53 | $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 54 | $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 55 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 56 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 57 | $(eval $(test)-bin := $(testobj)/$(test)/run)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 58 | $(foreach test, $(alltests), \ |
| 59 | $(eval $(call TEST_CC_template,$(test)))) |
| 60 | |
| 61 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 62 | $(eval all-test-objs += $($(test)-objs))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 63 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 64 | $(eval test-bins += $($(test)-bin))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 65 | |
| 66 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 67 | -include $(DEPENDENCIES) |
| 68 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 69 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests)) |
| 70 | .PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 71 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 72 | .PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 73 | |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 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 |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 77 | ifeq ($(JUNIT_OUTPUT),y) |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 78 | $(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml |
| 79 | $(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 80 | endif |
| 81 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 82 | $(addprefix run-,$(alltests)): run-%: $$(%-bin) |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 83 | rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml |
| 84 | rm -f $(testobj)/$(subst /,_,$^).failed |
Raul E Rangel | 940953e | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 85 | -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 86 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 87 | $(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 | |
| 111 | TESTS_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 | |
| 121 | junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE) |
| 122 | |
| 123 | clean-junit.xml-unit-tests: |
| 124 | rm -f $(TESTS_BUILD_XML_FILE) |
| 125 | |
| 126 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 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 |
| 132 | # want to do. |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 133 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 134 | .PHONY: coverage-report clean-coverage-report |
| 135 | |
| 136 | ifeq ($(COV),1) |
| 137 | coverage-report: |
| 138 | lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*' |
| 139 | genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \ |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 140 | -s $(testobj)/tests.info |
| 141 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 142 | clean-coverage-report: |
| 143 | rm -Rf $(testobj)/$(coverage_dir) |
| 144 | else |
| 145 | coverage-report: |
| 146 | COV=1 V=$(V) $(MAKE) coverage-report |
| 147 | |
| 148 | clean-coverage-report: |
| 149 | COV=1 V=$(V) $(MAKE) clean-coverage-report |
| 150 | endif |
| 151 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 152 | unit-tests: build-unit-tests run-unit-tests |
| 153 | |
| 154 | build-unit-tests: $(test-bins) |
| 155 | |
| 156 | run-unit-tests: $(alltests) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 157 | 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 Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 168 | |
| 169 | $(addprefix clean-,$(alltests)): clean-%: |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 170 | rm -rf $(testobj)/$* |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 171 | |
| 172 | clean-unit-tests: |
| 173 | rm -rf $(testobj) |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 174 | |
| 175 | list-unit-tests: |
| 176 | @echo "unit-tests:" |
| 177 | for t in $(sort $(alltests)); do \ |
| 178 | echo " $$t"; \ |
| 179 | done |
| 180 | |
| 181 | help-unit-tests help:: |
| 182 | @echo '*** coreboot unit-tests targets ***' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 183 | @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests' |
Martin Roth | 96edc10 | 2022-09-27 18:30:55 -0600 | [diff] [blame] | 184 | @echo ' Use "GDB_DEBUG=1 make [target]" to build with debug symbols' |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 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' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 190 | @echo ' coverage-report - Generate a code coverage report' |
| 191 | @echo ' clean-coverage-report - Remove the code coverage report' |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 192 | @echo |