1 # SPDX-License-Identifier: GPL-2.0-only
3 testsrc := $(top)/tests
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.
8 testobj := $(obj)/coverage
10 testobj := $(obj)/tests
13 cmockasrc := 3rdparty/cmocka
14 cmockaobj := $(objutil)/cmocka
15 coverage_dir := coverage_reports
17 CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
23 TEST_DEFAULT_CONFIG := $(top)/configs/config.emulation_qemu_x86_i440fx
24 TEST_DOTCONFIG := $(testobj)/.config
25 TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
26 TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
27 TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
28 TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/
29 TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
31 TEST_CFLAGS := -include $(src)/include/kconfig.h \
32 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
33 -include $(src)/include/rules.h
35 # Include generic test mock headers, before original ones
36 TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
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
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
47 TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
49 # Path for Kconfig autoheader
50 TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
52 TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
54 TEST_CFLAGS += -D__TEST__
56 TEST_CFLAGS += -I$(cmockasrc)/include
58 ifneq ($(filter-out 0,$(TEST_PRINT)),)
59 TEST_CFLAGS += -DTEST_PRINT=1
63 TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
64 TEST_LDFLAGS += -Wl,--gc-sections
66 # Some memlayout symbols don't work with userspace relocation -- disable it.
67 TEST_CFLAGS += -fno-pie -fno-pic
68 TEST_LDFLAGS += -no-pie
70 # Enable code coverage if COV=1
72 TEST_CFLAGS += --coverage
73 TEST_LDFLAGS += --coverage
76 # Extra attributes for unit tests, declared per test
77 attributes := srcs cflags config mocks stage
79 stages := decompressor bootblock romstage smm verstage
80 stages += ramstage rmodule postcar libagesa
83 subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
84 subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
85 subdirs += tests/northbridge tests/security tests/soc tests/southbridge
86 subdirs += tests/superio tests/vendorcode
90 $(foreach attribute,$(attributes),
91 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
92 $(foreach attribute,$(attributes),
93 $(eval $(2)-$(attribute) := ))
95 # Sanity check for stage attribute value
96 $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
97 $(if $(findstring $($(1)$(2)-stage), $(stages)),,
98 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
99 Check your $(dir $(1)$(2))Makefile.inc))
102 # Copy attributes of one test to another.
103 # $1 - input test name
104 # $2 - output test name
105 copy-test = $(foreach attr,$(attributes), \
106 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
108 $(call add-special-class, tests)
109 $(call evaluate_subdirs)
111 # Create actual targets for unit test binaries
113 define TEST_CC_template
115 # Generate custom config.h redefining given config symbols, and declaring mocked
116 # functions weak. It is important that the compiler already sees that they are
117 # weak (and they aren't just turned weak at a later stage) to prevent certain
118 # optimizations that would break if the function gets replaced. (For clang this
119 # file needs to be marked `system_header` to prevent it from warning about
120 # #pragma weak entries without a matching function declaration, since there's no
121 # -Wno-xxx command line option for that.)
122 $(1)-config-file := $(testobj)/$(1)/config.h
123 $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
125 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
126 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
127 for kv in $$($(1)-config); do \
128 key="`echo $$$$kv | cut -d '=' -f -1`"; \
129 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
130 printf '#undef %s\n' "$$$$key" >> $$@; \
131 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
133 printf '#ifdef __clang__\n' >> $$@;
134 printf '#pragma clang system_header\n' >> $$@;
135 printf '#endif\n' >> $$@;
136 printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
137 for m in $$($(1)-mocks); do \
138 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
140 printf '#endif\n' >> $$@;
142 $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
143 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
144 -D__TEST_NAME__=\"$(1)\"
146 # Give us a way to distinguish between coreboot source files and test files in code.
147 $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
149 # Compile sources and apply mocking/wrapping of selected symbols.
150 # For each listed mock add new symbol with prefix `__real_`,
151 # and pointing to the same section:address.
152 $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
154 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
155 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
156 objcopy_wrap_flags=''; \
157 for sym in $$($(1)-mocks); do \
158 sym_line="$$$$($(OBJDUMP) -t $$@.orig | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
159 if [ ! -z "$$$$sym_line" ] ; then \
160 addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
161 section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
162 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
165 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
167 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
168 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
172 $(foreach test, $(alltests), \
173 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
174 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
175 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
176 $(patsubst %.c,%.o,$($(test)-srcs)))))
177 $(foreach test, $(alltests), \
178 $(eval $(test)-bin := $(testobj)/$(test)/run))
179 $(foreach test, $(alltests), \
180 $(eval $(call TEST_CC_template,$(test))))
182 $(foreach test, $(alltests), \
183 $(eval all-test-objs += $($(test)-objs)))
184 $(foreach test, $(alltests), \
185 $(eval test-bins += $($(test)-bin)))
187 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
188 -include $(DEPENDENCIES)
192 echo "*** Building CMOCKA ***"
193 mkdir -p $(cmockaobj)
194 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
195 $(MAKE) -C $(cmockaobj)
200 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
202 # Don't override default Kconfig variables, since this will affect all
203 # Kconfig targets. Change them only when calling sub-make instead.
204 $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
205 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
206 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
207 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
208 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
209 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
210 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
212 $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
214 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
215 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
217 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
220 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
221 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
223 ifeq ($(JUNIT_OUTPUT),y)
224 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
225 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
228 $(alltests): $$($$(@)-bin)
229 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
230 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
232 # Build a code coverage report by collecting all the gcov files into a single
233 # report. If COV is not set, this might be a user error, and they're trying
234 # to generate a coverage report without first having built and run the code
235 # with code coverage. So instead of silently correcting it by adding COV=1,
236 # let's flag it to the user so they can be sure they're doing the thing they
239 .PHONY: coverage-report clean-coverage-report
243 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
244 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
245 -s $(testobj)/tests.info
247 clean-coverage-report:
248 rm -Rf $(testobj)/$(coverage_dir)
251 COV=1 V=$(V) $(MAKE) coverage-report
253 clean-coverage-report:
254 COV=1 V=$(V) $(MAKE) clean-coverage-report
257 unit-tests: build-unit-tests run-unit-tests
259 build-unit-tests: $(test-bins)
261 run-unit-tests: $(alltests)
262 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
263 echo "**********************"; \
264 echo " TESTS FAILED"; \
265 echo "**********************"; \
268 echo "**********************"; \
269 echo " ALL TESTS PASSED"; \
270 echo "**********************"; \
274 $(addprefix clean-,$(alltests)): clean-%:
282 for t in $(sort $(alltests)); do \
286 help-unit-tests help::
287 @echo '*** coreboot unit-tests targets ***'
288 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
289 @echo ' unit-tests - Run all unit-tests from tests/'
290 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
291 @echo ' list-unit-tests - List all unit-tests'
292 @echo ' <unit-test> - Build and run single unit-test'
293 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
294 @echo ' coverage-report - Generate a code coverage report'
295 @echo ' clean-coverage-report - Remove the code coverage report'