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