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 | |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 3 | testsrc = $(top)/tests |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 4 | |
| 5 | # Place the build output in one of two places depending on COV, so that code |
| 6 | # built with code coverage never mixes with code built without code coverage. |
| 7 | ifeq ($(COV),1) |
| 8 | testobj = $(obj)/coverage |
| 9 | else |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 10 | testobj = $(obj)/tests |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 11 | endif |
| 12 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 13 | cmockasrc = 3rdparty/cmocka |
Patrick Georgi | a340297 | 2020-05-27 10:53:01 +0200 | [diff] [blame] | 14 | cmockaobj = $(objutil)/cmocka |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 15 | coverage_dir = coverage_reports |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 16 | |
| 17 | CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so |
| 18 | |
| 19 | CMAKE:= cmake |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 20 | OBJCOPY?= objcopy |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 21 | |
| 22 | TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx |
| 23 | TEST_DOTCONFIG = $(testobj)/.config |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 24 | TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 25 | TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf |
| 26 | TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd |
Jakub Czapiga | 21863e3 | 2021-07-15 12:54:27 +0200 | [diff] [blame] | 27 | TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 28 | TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf |
| 29 | |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 30 | TEST_CFLAGS = -include $(src)/include/kconfig.h \ |
| 31 | -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \ |
| 32 | -include $(src)/include/rules.h |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 33 | |
| 34 | # Include generic test mock headers, before original ones |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 35 | TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 36 | |
| 37 | TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \ |
Julius Werner | 1e14de8 | 2020-06-10 15:35:08 -0700 | [diff] [blame] | 38 | -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include |
| 39 | |
| 40 | # Note: This is intentionally just a subset of the warnings in the toplevel |
| 41 | # Makefile.inc. We don't need to be as strict with test code, and things like |
| 42 | # -Wmissing-prototypes just make working with the test framework cumbersome. |
| 43 | # Only put conservative warnings here that really detect code that's obviously |
| 44 | # unintentional. |
Julius Werner | e54d784 | 2021-02-16 19:32:24 -0800 | [diff] [blame] | 45 | TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 46 | |
| 47 | # Path for Kconfig autoheader |
| 48 | TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) |
| 49 | |
Julius Werner | 2540037 | 2020-05-04 17:46:31 -0700 | [diff] [blame] | 50 | TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 51 | |
Jakub Czapiga | 084ad93 | 2021-03-25 13:10:31 +0100 | [diff] [blame] | 52 | TEST_CFLAGS += -D__TEST__ |
| 53 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 54 | TEST_CFLAGS += -I$(cmockasrc)/include |
| 55 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 56 | # Link against Cmocka |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 57 | TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src |
| 58 | TEST_LDFLAGS += -Wl,--gc-sections |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 59 | |
Julius Werner | 84446e6 | 2021-02-12 17:37:27 -0800 | [diff] [blame] | 60 | # Some memlayout symbols don't work with userspace relocation -- disable it. |
| 61 | TEST_CFLAGS += -fno-pie -fno-pic |
| 62 | TEST_LDFLAGS += -no-pie |
| 63 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 64 | # Enable code coverage if COV=1 |
| 65 | ifeq ($(COV),1) |
| 66 | TEST_CFLAGS += --coverage |
| 67 | TEST_LDFLAGS += --coverage |
| 68 | endif |
| 69 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 70 | # Extra attributes for unit tests, declared per test |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 71 | attributes:= srcs cflags config mocks stage |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 72 | |
| 73 | stages:= decompressor bootblock romstage smm verstage |
| 74 | stages+= ramstage rmodule postcar libagesa |
| 75 | |
| 76 | alltests:= |
Jakub Czapiga | 168b38b | 2021-02-11 12:59:51 +0100 | [diff] [blame] | 77 | subdirs:= tests/arch tests/acpi tests/commonlib tests/console tests/cpu |
| 78 | subdirs+= tests/device tests/drivers tests/ec tests/lib tests/mainboard |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 79 | subdirs+= tests/northbridge tests/security tests/soc tests/southbridge |
| 80 | subdirs+= tests/superio tests/vendorcode |
| 81 | |
| 82 | define tests-handler |
| 83 | alltests += $(1)$(2) |
| 84 | $(foreach attribute,$(attributes), |
| 85 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 86 | $(foreach attribute,$(attributes), |
| 87 | $(eval $(2)-$(attribute):=)) |
| 88 | |
| 89 | # Sanity check for stage attribute value |
| 90 | $(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
| 91 | $(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 92 | $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
| 93 | Check your $(dir $(1)$(2))Makefile.inc)) |
| 94 | endef |
| 95 | |
| 96 | $(call add-special-class, tests) |
| 97 | $(call evaluate_subdirs) |
| 98 | |
| 99 | # Create actual targets for unit test binaries |
| 100 | # $1 - test name |
| 101 | define TEST_CC_template |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 102 | |
| 103 | # Generate custom config.h redefining given symbols |
Jakub Czapiga | 203698a | 2021-05-25 15:56:36 +0200 | [diff] [blame] | 104 | $(1)-config-file := $(testobj)/$(1)/config.h |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 105 | $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER) |
| 106 | mkdir -p $$(dir $$@) |
| 107 | printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@ |
| 108 | printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@ |
| 109 | for kv in $$($(1)-config); do \ |
| 110 | key="`echo $$$$kv | cut -d '=' -f -1`"; \ |
| 111 | value="`echo $$$$kv | cut -d '=' -f 2-`"; \ |
| 112 | printf '#undef %s\n' "$$$$key" >> $$@; \ |
| 113 | printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \ |
| 114 | done |
| 115 | |
| 116 | $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 117 | -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 118 | $($(1)-srcobjs): OBJCOPY_FLAGS += $$(foreach mock,$$($(1)-mocks),--globalize-symbol=$$(mock) --weaken-symbol=$$(mock)) |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 119 | $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 120 | mkdir -p $$(dir $$@) |
Yu-Ping Wu | 0f88f6f | 2021-07-20 14:32:40 +0800 | [diff] [blame] | 121 | $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ |
| 122 | -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 123 | $(OBJCOPY) $$@.orig $$(OBJCOPY_FLAGS) $$@ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 124 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 125 | $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 126 | $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@ |
| 127 | |
| 128 | endef |
| 129 | |
| 130 | $(foreach test, $(alltests), \ |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 131 | $(eval $(test)-srcobjs:=$(addprefix $(testobj)/$(test)/, \ |
| 132 | $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 133 | $(eval $(test)-objs:=$(addprefix $(testobj)/$(test)/, \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 134 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 135 | $(foreach test, $(alltests), \ |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 136 | $(eval $(test)-bin:=$(testobj)/$(test)/run)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 137 | $(foreach test, $(alltests), \ |
| 138 | $(eval $(call TEST_CC_template,$(test)))) |
| 139 | |
| 140 | $(foreach test, $(alltests), \ |
| 141 | $(eval all-test-objs+=$($(test)-objs))) |
| 142 | $(foreach test, $(alltests), \ |
| 143 | $(eval test-bins+=$($(test)-bin))) |
| 144 | |
| 145 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 146 | -include $(DEPENDENCIES) |
| 147 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 148 | # Build cmocka |
| 149 | $(CMOCKA_LIB): |
| 150 | echo "*** Building CMOCKA ***" |
| 151 | mkdir -p $(cmockaobj) |
| 152 | cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc)) |
| 153 | $(MAKE) -C $(cmockaobj) |
| 154 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 155 | # Kconfig targets |
| 156 | $(TEST_DOTCONFIG): |
| 157 | mkdir -p $(dir $@) |
| 158 | cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG) |
| 159 | |
| 160 | # Don't override default Kconfig variables, since this will affect all |
| 161 | # Kconfig targets. Change them only when calling sub-make instead. |
| 162 | $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \ |
| 163 | KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \ |
| 164 | KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \ |
| 165 | KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \ |
| 166 | KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \ |
| 167 | KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \ |
| 168 | KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG) |
| 169 | |
| 170 | $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf |
| 171 | mkdir -p $(dir $@) |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 172 | $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig |
| 173 | $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 174 | |
| 175 | $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER) |
| 176 | true |
| 177 | |
| 178 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) |
| 179 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
| 180 | |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 181 | ifeq ($(JUNIT_OUTPUT),y) |
| 182 | $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml |
Jakub Czapiga | 1add483 | 2021-03-05 11:37:23 +0100 | [diff] [blame] | 183 | $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 184 | endif |
| 185 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 186 | $(alltests): $$($$(@)-bin) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 187 | rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed |
| 188 | -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 189 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 190 | # Build a code coverage report by collecting all the gcov files into a single |
| 191 | # report. If COV is not set, this might be a user error, and they're trying |
| 192 | # to generate a coverage report without first having built and run the code |
| 193 | # with code coverage. So instead of silently correcting it by adding COV=1, |
| 194 | # let's flag it to the user so they can be sure they're doing the thing they |
| 195 | # want to do. |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 196 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 197 | .PHONY: coverage-report clean-coverage-report |
| 198 | |
| 199 | ifeq ($(COV),1) |
| 200 | coverage-report: |
| 201 | lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*' |
| 202 | genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \ |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 203 | -s $(testobj)/tests.info |
| 204 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 205 | clean-coverage-report: |
| 206 | rm -Rf $(testobj)/$(coverage_dir) |
| 207 | else |
| 208 | coverage-report: |
| 209 | COV=1 V=$(V) $(MAKE) coverage-report |
| 210 | |
| 211 | clean-coverage-report: |
| 212 | COV=1 V=$(V) $(MAKE) clean-coverage-report |
| 213 | endif |
| 214 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 215 | unit-tests: build-unit-tests run-unit-tests |
| 216 | |
| 217 | build-unit-tests: $(test-bins) |
| 218 | |
| 219 | run-unit-tests: $(alltests) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 220 | if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \ |
| 221 | echo "**********************"; \ |
| 222 | echo " TESTS FAILED"; \ |
| 223 | echo "**********************"; \ |
| 224 | exit 1; \ |
| 225 | else \ |
| 226 | echo "**********************"; \ |
| 227 | echo " ALL TESTS PASSED"; \ |
| 228 | echo "**********************"; \ |
| 229 | exit 0; \ |
| 230 | fi |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 231 | |
| 232 | $(addprefix clean-,$(alltests)): clean-%: |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 233 | rm -rf $(testobj)/$* |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 234 | |
| 235 | clean-unit-tests: |
| 236 | rm -rf $(testobj) |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 237 | |
| 238 | list-unit-tests: |
| 239 | @echo "unit-tests:" |
| 240 | for t in $(sort $(alltests)); do \ |
| 241 | echo " $$t"; \ |
| 242 | done |
| 243 | |
| 244 | help-unit-tests help:: |
| 245 | @echo '*** coreboot unit-tests targets ***' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 246 | @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] | 247 | @echo ' unit-tests - Run all unit-tests from tests/' |
| 248 | @echo ' clean-unit-tests - Remove unit-tests build artifacts' |
| 249 | @echo ' list-unit-tests - List all unit-tests' |
| 250 | @echo ' <unit-test> - Build and run single unit-test' |
| 251 | @echo ' clean-<unit-test> - Remove single unit-test build artifacts' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 252 | @echo ' coverage-report - Generate a code coverage report' |
| 253 | @echo ' clean-coverage-report - Remove the code coverage report' |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 254 | @echo |