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 | e8bef61 | 2022-12-05 15:21:52 +0100 | [diff] [blame] | 11 | objutil := $(testobj)/util |
| 12 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 13 | include $(top)/tests/Makefile.common |
Julius Werner | 84446e6 | 2021-02-12 17:37:27 -0800 | [diff] [blame] | 14 | |
Martin Roth | 96edc10 | 2022-09-27 18:30:55 -0600 | [diff] [blame] | 15 | # Enable GDB debug build if requested |
| 16 | GDB_DEBUG ?= 0 |
| 17 | ifneq ($(GDB_DEBUG),0) |
| 18 | TEST_CFLAGS += -g -Og |
| 19 | endif |
| 20 | |
| 21 | # Enable code coverage if requested |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 22 | ifeq ($(COV),1) |
| 23 | TEST_CFLAGS += --coverage |
| 24 | TEST_LDFLAGS += --coverage |
| 25 | endif |
| 26 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 27 | stages := decompressor bootblock romstage smm verstage |
| 28 | stages += ramstage rmodule postcar libagesa |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 29 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 30 | alltests := |
| 31 | subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu |
Patrick Rudolph | 1ab8ad6 | 2021-04-21 10:02:55 +0200 | [diff] [blame] | 32 | subdirs += tests/device tests/drivers tests/ec tests/lib |
| 33 | subdirs += tests/mainboard tests/northbridge tests/security tests/soc |
| 34 | subdirs += tests/southbridge tests/superio tests/vendorcode |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 35 | |
| 36 | define tests-handler |
| 37 | alltests += $(1)$(2) |
| 38 | $(foreach attribute,$(attributes), |
| 39 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 40 | $(foreach attribute,$(attributes), |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 41 | $(eval $(2)-$(attribute) := )) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 42 | |
| 43 | # Sanity check for stage attribute value |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 44 | $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 45 | $(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 46 | $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
Martin Roth | ba3a719 | 2024-01-18 18:16:12 -0700 | [diff] [blame^] | 47 | Check your $(dir $(1)$(2))Makefile.mk)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 48 | endef |
| 49 | |
| 50 | $(call add-special-class, tests) |
| 51 | $(call evaluate_subdirs) |
| 52 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 53 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 54 | $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \ |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 55 | $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 56 | $(eval $(test)-sysobjs := $(addprefix $(testobj)/$(test)/, \ |
| 57 | $(patsubst %.c,%.o,$($(test)-syssrcs)))) \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 58 | $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 59 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 60 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 61 | $(eval $(test)-bin := $(testobj)/$(test)/run)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 62 | $(foreach test, $(alltests), \ |
| 63 | $(eval $(call TEST_CC_template,$(test)))) |
| 64 | |
| 65 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 66 | $(eval all-test-objs += $($(test)-objs))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 67 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 68 | $(eval test-bins += $($(test)-bin))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 69 | |
| 70 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 71 | -include $(DEPENDENCIES) |
| 72 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 73 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests)) |
| 74 | .PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 75 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 76 | .PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 77 | |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 78 | # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)" |
| 79 | # by macro cb_run_group_tests(), which should be used for running tests. |
| 80 | # __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] | 81 | ifeq ($(JUNIT_OUTPUT),y) |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 82 | $(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml |
| 83 | $(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 84 | endif |
| 85 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 86 | $(addprefix run-,$(alltests)): run-%: $$(%-bin) |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 87 | rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml |
| 88 | rm -f $(testobj)/$(subst /,_,$^).failed |
Raul E Rangel | 940953e | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 89 | -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 90 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 91 | $(addprefix build-,$(alltests)): build-%: $$(%-bin) |
| 92 | |
| 93 | $(alltests): run-$$(@) |
| 94 | |
| 95 | $(addprefix try-,$(alltests)): try-%: clean-% $(TEST_COMMON_DEPENDENCIES) |
| 96 | mkdir -p $(testobj)/$* |
| 97 | echo "<testcase classname='coreboot_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \ |
| 98 | $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \ |
| 99 | && type="system-out" || type="failure"; \ |
| 100 | if [ $$type = "failure" ]; then \ |
| 101 | echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \ |
| 102 | else \ |
| 103 | echo "<$$type>" >> $(testobj)/$*.tmp; \ |
| 104 | fi; \ |
| 105 | echo '<![CDATA[' >> $(testobj)/$*.tmp; \ |
| 106 | cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \ |
| 107 | echo "]]></$$type>" >> $(testobj)/$*.tmp; \ |
| 108 | rm -f $(testobj)/$*.tmp.2; \ |
| 109 | echo "</testcase>" >> $(testobj)/$*.tmp; \ |
| 110 | if [ $$type != 'failure' ]; then \ |
| 111 | $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \ |
| 112 | fi |
| 113 | |
| 114 | |
| 115 | TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml |
| 116 | |
| 117 | $(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests)) |
| 118 | mkdir -p $(dir $@) |
| 119 | echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@ |
| 120 | for tst in $(alltests); do \ |
| 121 | cat $(testobj)/$$tst.tmp >> $@; \ |
| 122 | done |
| 123 | echo "</testsuite>" >> $@ |
| 124 | |
| 125 | junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE) |
| 126 | |
| 127 | clean-junit.xml-unit-tests: |
| 128 | rm -f $(TESTS_BUILD_XML_FILE) |
| 129 | |
| 130 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 131 | # Build a code coverage report by collecting all the gcov files into a single |
| 132 | # report. If COV is not set, this might be a user error, and they're trying |
| 133 | # to generate a coverage report without first having built and run the code |
| 134 | # with code coverage. So instead of silently correcting it by adding COV=1, |
| 135 | # let's flag it to the user so they can be sure they're doing the thing they |
| 136 | # want to do. |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 137 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 138 | .PHONY: coverage-report clean-coverage-report |
| 139 | |
| 140 | ifeq ($(COV),1) |
| 141 | coverage-report: |
| 142 | lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*' |
| 143 | genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \ |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 144 | -s $(testobj)/tests.info |
| 145 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 146 | clean-coverage-report: |
| 147 | rm -Rf $(testobj)/$(coverage_dir) |
| 148 | else |
| 149 | coverage-report: |
| 150 | COV=1 V=$(V) $(MAKE) coverage-report |
| 151 | |
| 152 | clean-coverage-report: |
| 153 | COV=1 V=$(V) $(MAKE) clean-coverage-report |
| 154 | endif |
| 155 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 156 | unit-tests: build-unit-tests run-unit-tests |
| 157 | |
| 158 | build-unit-tests: $(test-bins) |
| 159 | |
| 160 | run-unit-tests: $(alltests) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 161 | if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \ |
| 162 | echo "**********************"; \ |
| 163 | echo " TESTS FAILED"; \ |
| 164 | echo "**********************"; \ |
| 165 | exit 1; \ |
| 166 | else \ |
| 167 | echo "**********************"; \ |
| 168 | echo " ALL TESTS PASSED"; \ |
| 169 | echo "**********************"; \ |
| 170 | exit 0; \ |
| 171 | fi |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 172 | |
| 173 | $(addprefix clean-,$(alltests)): clean-%: |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 174 | rm -rf $(testobj)/$* |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 175 | |
| 176 | clean-unit-tests: |
| 177 | rm -rf $(testobj) |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 178 | |
| 179 | list-unit-tests: |
| 180 | @echo "unit-tests:" |
| 181 | for t in $(sort $(alltests)); do \ |
| 182 | echo " $$t"; \ |
| 183 | done |
| 184 | |
| 185 | help-unit-tests help:: |
| 186 | @echo '*** coreboot unit-tests targets ***' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 187 | @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] | 188 | @echo ' Use "GDB_DEBUG=1 make [target]" to build with debug symbols' |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 189 | @echo ' unit-tests - Run all unit-tests from tests/' |
| 190 | @echo ' clean-unit-tests - Remove unit-tests build artifacts' |
| 191 | @echo ' list-unit-tests - List all unit-tests' |
| 192 | @echo ' <unit-test> - Build and run single unit-test' |
| 193 | @echo ' clean-<unit-test> - Remove single unit-test build artifacts' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 194 | @echo ' coverage-report - Generate a code coverage report' |
| 195 | @echo ' clean-coverage-report - Remove the code coverage report' |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 196 | @echo |