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 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 13 | # Enable code coverage if COV=1 |
| 14 | ifeq ($(COV),1) |
| 15 | TEST_CFLAGS += --coverage |
| 16 | TEST_LDFLAGS += --coverage |
| 17 | endif |
| 18 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 19 | stages := decompressor bootblock romstage smm verstage |
| 20 | stages += ramstage rmodule postcar libagesa |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 21 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 22 | alltests := |
| 23 | subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu |
| 24 | subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard |
| 25 | subdirs += tests/northbridge tests/security tests/soc tests/southbridge |
| 26 | subdirs += tests/superio tests/vendorcode |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 27 | |
| 28 | define tests-handler |
| 29 | alltests += $(1)$(2) |
| 30 | $(foreach attribute,$(attributes), |
| 31 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 32 | $(foreach attribute,$(attributes), |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 33 | $(eval $(2)-$(attribute) := )) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 34 | |
| 35 | # Sanity check for stage attribute value |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 36 | $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 37 | $(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 38 | $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
| 39 | Check your $(dir $(1)$(2))Makefile.inc)) |
| 40 | endef |
| 41 | |
| 42 | $(call add-special-class, tests) |
| 43 | $(call evaluate_subdirs) |
| 44 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 45 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 46 | $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \ |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 47 | $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 48 | $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 49 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 50 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 51 | $(eval $(test)-bin := $(testobj)/$(test)/run)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 52 | $(foreach test, $(alltests), \ |
| 53 | $(eval $(call TEST_CC_template,$(test)))) |
| 54 | |
| 55 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 56 | $(eval all-test-objs += $($(test)-objs))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 57 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 58 | $(eval test-bins += $($(test)-bin))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 59 | |
| 60 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 61 | -include $(DEPENDENCIES) |
| 62 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 63 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests)) |
| 64 | .PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 65 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 66 | .PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 67 | |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 68 | # %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 Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 71 | ifeq ($(JUNIT_OUTPUT),y) |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 72 | $(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml |
| 73 | $(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 74 | endif |
| 75 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 76 | $(addprefix run-,$(alltests)): run-%: $$(%-bin) |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 77 | rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml |
| 78 | rm -f $(testobj)/$(subst /,_,$^).failed |
Raul E Rangel | 940953e | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 79 | -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 80 | |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 81 | $(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 | |
| 105 | TESTS_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 | |
| 115 | junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE) |
| 116 | |
| 117 | clean-junit.xml-unit-tests: |
| 118 | rm -f $(TESTS_BUILD_XML_FILE) |
| 119 | |
| 120 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 121 | # 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 Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 127 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 128 | .PHONY: coverage-report clean-coverage-report |
| 129 | |
| 130 | ifeq ($(COV),1) |
| 131 | coverage-report: |
| 132 | lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*' |
| 133 | genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \ |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 134 | -s $(testobj)/tests.info |
| 135 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 136 | clean-coverage-report: |
| 137 | rm -Rf $(testobj)/$(coverage_dir) |
| 138 | else |
| 139 | coverage-report: |
| 140 | COV=1 V=$(V) $(MAKE) coverage-report |
| 141 | |
| 142 | clean-coverage-report: |
| 143 | COV=1 V=$(V) $(MAKE) clean-coverage-report |
| 144 | endif |
| 145 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 146 | unit-tests: build-unit-tests run-unit-tests |
| 147 | |
| 148 | build-unit-tests: $(test-bins) |
| 149 | |
| 150 | run-unit-tests: $(alltests) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 151 | 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 Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 162 | |
| 163 | $(addprefix clean-,$(alltests)): clean-%: |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 164 | rm -rf $(testobj)/$* |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 165 | |
| 166 | clean-unit-tests: |
| 167 | rm -rf $(testobj) |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 168 | |
| 169 | list-unit-tests: |
| 170 | @echo "unit-tests:" |
| 171 | for t in $(sort $(alltests)); do \ |
| 172 | echo " $$t"; \ |
| 173 | done |
| 174 | |
| 175 | help-unit-tests help:: |
| 176 | @echo '*** coreboot unit-tests targets ***' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 177 | @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests' |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 178 | @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 Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 183 | @echo ' coverage-report - Generate a code coverage report' |
| 184 | @echo ' clean-coverage-report - Remove the code coverage report' |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 185 | @echo |