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 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [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) |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 8 | testobj := $(obj)/coverage |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 9 | else |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 10 | testobj := $(obj)/tests |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 11 | endif |
| 12 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 13 | cmockasrc := 3rdparty/cmocka |
| 14 | cmockaobj := $(objutil)/cmocka |
| 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 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 19 | CMAKE := cmake |
| 20 | OBJCOPY ?= objcopy |
| 21 | OBJDUMP ?= objdump |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 22 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 23 | TEST_DEFAULT_CONFIG := $(top)/configs/config.emulation_qemu_x86_i440fx |
| 24 | TEST_DOTCONFIG := $(testobj)/.config |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 25 | TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 26 | TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf |
| 27 | TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd |
Jakub Czapiga | 21863e3 | 2021-07-15 12:54:27 +0200 | [diff] [blame] | 28 | TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 29 | TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf |
| 30 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 31 | TEST_CFLAGS := -include $(src)/include/kconfig.h \ |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 32 | -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \ |
| 33 | -include $(src)/include/rules.h |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 34 | |
| 35 | # Include generic test mock headers, before original ones |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 36 | TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 37 | |
Jakub Czapiga | d0bd012 | 2021-08-09 15:51:37 +0200 | [diff] [blame] | 38 | TEST_CFLAGS += -I$(src) -I$(src)/include -I$(src)/commonlib/include \ |
| 39 | -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \ |
| 40 | -I$(top)/3rdparty/vboot/firmware/include |
Julius Werner | 1e14de8 | 2020-06-10 15:35:08 -0700 | [diff] [blame] | 41 | |
| 42 | # Note: This is intentionally just a subset of the warnings in the toplevel |
| 43 | # Makefile.inc. We don't need to be as strict with test code, and things like |
| 44 | # -Wmissing-prototypes just make working with the test framework cumbersome. |
| 45 | # Only put conservative warnings here that really detect code that's obviously |
| 46 | # unintentional. |
Julius Werner | e54d784 | 2021-02-16 19:32:24 -0800 | [diff] [blame] | 47 | TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm |
Julius Werner | c90d406 | 2021-12-01 13:52:25 -0800 | [diff] [blame] | 48 | TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 49 | |
| 50 | # Path for Kconfig autoheader |
| 51 | TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) |
| 52 | |
Julius Werner | 2540037 | 2020-05-04 17:46:31 -0700 | [diff] [blame] | 53 | TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 54 | |
Jakub Czapiga | 084ad93 | 2021-03-25 13:10:31 +0100 | [diff] [blame] | 55 | TEST_CFLAGS += -D__TEST__ |
| 56 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 57 | TEST_CFLAGS += -I$(cmockasrc)/include |
| 58 | |
Jakub Czapiga | 29faa8a | 2021-09-09 12:11:39 +0200 | [diff] [blame] | 59 | ifneq ($(filter-out 0,$(TEST_PRINT)),) |
| 60 | TEST_CFLAGS += -DTEST_PRINT=1 |
| 61 | endif |
| 62 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 63 | # Link against Cmocka |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 64 | TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 65 | TEST_LDFLAGS += -Wl,--gc-sections |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 66 | |
Julius Werner | 84446e6 | 2021-02-12 17:37:27 -0800 | [diff] [blame] | 67 | # Some memlayout symbols don't work with userspace relocation -- disable it. |
| 68 | TEST_CFLAGS += -fno-pie -fno-pic |
| 69 | TEST_LDFLAGS += -no-pie |
| 70 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 71 | # Enable code coverage if COV=1 |
| 72 | ifeq ($(COV),1) |
| 73 | TEST_CFLAGS += --coverage |
| 74 | TEST_LDFLAGS += --coverage |
| 75 | endif |
| 76 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 77 | # Extra attributes for unit tests, declared per test |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 78 | attributes := srcs cflags config mocks stage |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 79 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 80 | stages := decompressor bootblock romstage smm verstage |
| 81 | stages += ramstage rmodule postcar libagesa |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 82 | |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 83 | alltests := |
| 84 | subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu |
| 85 | subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard |
| 86 | subdirs += tests/northbridge tests/security tests/soc tests/southbridge |
| 87 | subdirs += tests/superio tests/vendorcode |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 88 | |
| 89 | define tests-handler |
| 90 | alltests += $(1)$(2) |
| 91 | $(foreach attribute,$(attributes), |
| 92 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 93 | $(foreach attribute,$(attributes), |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 94 | $(eval $(2)-$(attribute) := )) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 95 | |
| 96 | # Sanity check for stage attribute value |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 97 | $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 98 | $(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 99 | $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
| 100 | Check your $(dir $(1)$(2))Makefile.inc)) |
| 101 | endef |
| 102 | |
Jakub Czapiga | 9bf716e | 2021-08-09 15:00:43 +0200 | [diff] [blame] | 103 | # Copy attributes of one test to another. |
| 104 | # $1 - input test name |
| 105 | # $2 - output test name |
| 106 | copy-test = $(foreach attr,$(attributes), \ |
| 107 | $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr)))) |
| 108 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 109 | $(call add-special-class, tests) |
| 110 | $(call evaluate_subdirs) |
| 111 | |
| 112 | # Create actual targets for unit test binaries |
| 113 | # $1 - test name |
| 114 | define TEST_CC_template |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 115 | |
Julius Werner | ab91407 | 2021-08-17 19:50:22 -0700 | [diff] [blame] | 116 | # Generate custom config.h redefining given config symbols, and declaring mocked |
| 117 | # functions weak. It is important that the compiler already sees that they are |
| 118 | # weak (and they aren't just turned weak at a later stage) to prevent certain |
| 119 | # optimizations that would break if the function gets replaced. (For clang this |
| 120 | # file needs to be marked `system_header` to prevent it from warning about |
| 121 | # #pragma weak entries without a matching function declaration, since there's no |
| 122 | # -Wno-xxx command line option for that.) |
Jakub Czapiga | 203698a | 2021-05-25 15:56:36 +0200 | [diff] [blame] | 123 | $(1)-config-file := $(testobj)/$(1)/config.h |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 124 | $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER) |
| 125 | mkdir -p $$(dir $$@) |
| 126 | printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@ |
| 127 | printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@ |
| 128 | for kv in $$($(1)-config); do \ |
| 129 | key="`echo $$$$kv | cut -d '=' -f -1`"; \ |
| 130 | value="`echo $$$$kv | cut -d '=' -f 2-`"; \ |
| 131 | printf '#undef %s\n' "$$$$key" >> $$@; \ |
| 132 | printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \ |
| 133 | done |
Julius Werner | ab91407 | 2021-08-17 19:50:22 -0700 | [diff] [blame] | 134 | printf '#ifdef __clang__\n' >> $$@; |
| 135 | printf '#pragma clang system_header\n' >> $$@; |
| 136 | printf '#endif\n' >> $$@; |
| 137 | printf '#ifdef __TEST_SRCOBJ__\n' >> $$@; |
| 138 | for m in $$($(1)-mocks); do \ |
| 139 | printf '#pragma weak %s\n' "$$$$m" >> $$@; \ |
| 140 | done |
| 141 | printf '#endif\n' >> $$@; |
Jakub Czapiga | 5c3b05e | 2021-04-28 16:50:51 +0200 | [diff] [blame] | 142 | |
| 143 | $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \ |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 144 | -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \ |
Jakub Czapiga | c1e4c5a | 2021-07-23 13:35:14 +0200 | [diff] [blame] | 145 | -D__TEST_NAME__=\"$(subst /,_,$(1))\" \ |
| 146 | -D__TEST_DATA_DIR__=\"$(testsrc)/data\" |
Jakub Czapiga | a7a49e6 | 2021-07-29 13:27:54 +0200 | [diff] [blame] | 147 | |
Julius Werner | ab91407 | 2021-08-17 19:50:22 -0700 | [diff] [blame] | 148 | # Give us a way to distinguish between coreboot source files and test files in code. |
| 149 | $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__ |
Jakub Czapiga | a7a49e6 | 2021-07-29 13:27:54 +0200 | [diff] [blame] | 150 | |
| 151 | # Compile sources and apply mocking/wrapping of selected symbols. |
| 152 | # For each listed mock add new symbol with prefix `__real_`, |
| 153 | # and pointing to the same section:address. |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 154 | $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 155 | mkdir -p $$(dir $$@) |
Yu-Ping Wu | 0f88f6f | 2021-07-20 14:32:40 +0800 | [diff] [blame] | 156 | $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ |
| 157 | -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig |
Jakub Czapiga | a7a49e6 | 2021-07-29 13:27:54 +0200 | [diff] [blame] | 158 | objcopy_wrap_flags=''; \ |
| 159 | for sym in $$($(1)-mocks); do \ |
Jakub Czapiga | b88d845 | 2021-12-03 08:23:28 +0000 | [diff] [blame] | 160 | sym_line="$$$$($(OBJDUMP) -t $$@.orig \ |
| 161 | | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \ |
Jakub Czapiga | a7a49e6 | 2021-07-29 13:27:54 +0200 | [diff] [blame] | 162 | if [ ! -z "$$$$sym_line" ] ; then \ |
Jakub Czapiga | b88d845 | 2021-12-03 08:23:28 +0000 | [diff] [blame] | 163 | addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \ |
| 164 | section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \ |
Jakub Czapiga | a7a49e6 | 2021-07-29 13:27:54 +0200 | [diff] [blame] | 165 | objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \ |
| 166 | fi \ |
| 167 | done ; \ |
Julius Werner | ab91407 | 2021-08-17 19:50:22 -0700 | [diff] [blame] | 168 | $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 169 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 170 | $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 171 | $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@ |
| 172 | |
| 173 | endef |
| 174 | |
| 175 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 176 | $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \ |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 177 | $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 178 | $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 179 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 180 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 181 | $(eval $(test)-bin := $(testobj)/$(test)/run)) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 182 | $(foreach test, $(alltests), \ |
| 183 | $(eval $(call TEST_CC_template,$(test)))) |
| 184 | |
| 185 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 186 | $(eval all-test-objs += $($(test)-objs))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 187 | $(foreach test, $(alltests), \ |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 188 | $(eval test-bins += $($(test)-bin))) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 189 | |
| 190 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 191 | -include $(DEPENDENCIES) |
| 192 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 193 | # Build cmocka |
| 194 | $(CMOCKA_LIB): |
| 195 | echo "*** Building CMOCKA ***" |
| 196 | mkdir -p $(cmockaobj) |
| 197 | cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc)) |
| 198 | $(MAKE) -C $(cmockaobj) |
| 199 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 200 | # Kconfig targets |
| 201 | $(TEST_DOTCONFIG): |
| 202 | mkdir -p $(dir $@) |
| 203 | cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG) |
| 204 | |
| 205 | # Don't override default Kconfig variables, since this will affect all |
| 206 | # Kconfig targets. Change them only when calling sub-make instead. |
Jakub Czapiga | eaf5ff3 | 2021-08-09 14:20:28 +0200 | [diff] [blame] | 207 | $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \ |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 208 | KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \ |
| 209 | KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \ |
| 210 | KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \ |
| 211 | KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \ |
| 212 | KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \ |
| 213 | KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG) |
| 214 | |
| 215 | $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf |
| 216 | mkdir -p $(dir $@) |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 217 | $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig |
| 218 | $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 219 | |
| 220 | $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER) |
| 221 | true |
| 222 | |
| 223 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) |
| 224 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
| 225 | |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 226 | # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)" |
| 227 | # by macro cb_run_group_tests(), which should be used for running tests. |
| 228 | # __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] | 229 | ifeq ($(JUNIT_OUTPUT),y) |
| 230 | $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 231 | $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 232 | endif |
| 233 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 234 | $(alltests): $$($$(@)-bin) |
Jakub Czapiga | 4d9694e | 2021-10-07 09:06:42 +0000 | [diff] [blame] | 235 | rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml |
| 236 | rm -f $(testobj)/$(subst /,_,$^).failed |
Raul E Rangel | 940953e | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 237 | -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 238 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 239 | # Build a code coverage report by collecting all the gcov files into a single |
| 240 | # report. If COV is not set, this might be a user error, and they're trying |
| 241 | # to generate a coverage report without first having built and run the code |
| 242 | # with code coverage. So instead of silently correcting it by adding COV=1, |
| 243 | # let's flag it to the user so they can be sure they're doing the thing they |
| 244 | # want to do. |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 245 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 246 | .PHONY: coverage-report clean-coverage-report |
| 247 | |
| 248 | ifeq ($(COV),1) |
| 249 | coverage-report: |
| 250 | lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*' |
| 251 | genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \ |
Paul Fagerburg | 045fbf1 | 2021-04-16 11:36:25 -0600 | [diff] [blame] | 252 | -s $(testobj)/tests.info |
| 253 | |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 254 | clean-coverage-report: |
| 255 | rm -Rf $(testobj)/$(coverage_dir) |
| 256 | else |
| 257 | coverage-report: |
| 258 | COV=1 V=$(V) $(MAKE) coverage-report |
| 259 | |
| 260 | clean-coverage-report: |
| 261 | COV=1 V=$(V) $(MAKE) clean-coverage-report |
| 262 | endif |
| 263 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 264 | unit-tests: build-unit-tests run-unit-tests |
| 265 | |
| 266 | build-unit-tests: $(test-bins) |
| 267 | |
| 268 | run-unit-tests: $(alltests) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 269 | if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \ |
| 270 | echo "**********************"; \ |
| 271 | echo " TESTS FAILED"; \ |
| 272 | echo "**********************"; \ |
| 273 | exit 1; \ |
| 274 | else \ |
| 275 | echo "**********************"; \ |
| 276 | echo " ALL TESTS PASSED"; \ |
| 277 | echo "**********************"; \ |
| 278 | exit 0; \ |
| 279 | fi |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 280 | |
| 281 | $(addprefix clean-,$(alltests)): clean-%: |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 282 | rm -rf $(testobj)/$* |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 283 | |
| 284 | clean-unit-tests: |
| 285 | rm -rf $(testobj) |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 286 | |
| 287 | list-unit-tests: |
| 288 | @echo "unit-tests:" |
| 289 | for t in $(sort $(alltests)); do \ |
| 290 | echo " $$t"; \ |
| 291 | done |
| 292 | |
| 293 | help-unit-tests help:: |
| 294 | @echo '*** coreboot unit-tests targets ***' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 295 | @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] | 296 | @echo ' unit-tests - Run all unit-tests from tests/' |
| 297 | @echo ' clean-unit-tests - Remove unit-tests build artifacts' |
| 298 | @echo ' list-unit-tests - List all unit-tests' |
| 299 | @echo ' <unit-test> - Build and run single unit-test' |
| 300 | @echo ' clean-<unit-test> - Remove single unit-test build artifacts' |
Paul Fagerburg | de6cbac | 2021-05-11 09:56:48 -0600 | [diff] [blame] | 301 | @echo ' coverage-report - Generate a code coverage report' |
| 302 | @echo ' clean-coverage-report - Remove the code coverage report' |
Jakub Czapiga | a4819b3 | 2021-04-13 16:07:05 +0200 | [diff] [blame] | 303 | @echo |