blob: fa5faab973893af3f9eeeb760205b3bf4b84e820 [file] [log] [blame]
Jan Dabrosef1c9682020-03-28 00:15:03 +01001# SPDX-License-Identifier: GPL-2.0-only
Jan Dabrosef1c9682020-03-28 00:15:03 +01002
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +02003testsrc := $(top)/tests
Paul Fagerburgde6cbac2021-05-11 09:56:48 -06004
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.
7ifeq ($(COV),1)
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +02008testobj := $(obj)/coverage
Paul Fagerburgde6cbac2021-05-11 09:56:48 -06009else
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020010testobj := $(obj)/tests
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060011endif
12
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020013cmockasrc := 3rdparty/cmocka
14cmockaobj := $(objutil)/cmocka
15coverage_dir := coverage_reports
Jan Dabros7f00dba2020-05-22 09:57:17 +020016
17CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
18
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020019CMAKE := cmake
20OBJCOPY ?= objcopy
21OBJDUMP ?= objdump
Jan Dabrosef1c9682020-03-28 00:15:03 +010022
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020023TEST_DEFAULT_CONFIG := $(top)/configs/config.emulation_qemu_x86_i440fx
24TEST_DOTCONFIG := $(testobj)/.config
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020025TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010026TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
27TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
Jakub Czapiga21863e32021-07-15 12:54:27 +020028TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/
Jan Dabrosef1c9682020-03-28 00:15:03 +010029TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
30
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020031TEST_CFLAGS := -include $(src)/include/kconfig.h \
Julius Werner21744812020-05-04 17:44:04 -070032 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
33 -include $(src)/include/rules.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010034
35# Include generic test mock headers, before original ones
Julius Werner21744812020-05-04 17:44:04 -070036TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
Jan Dabrosef1c9682020-03-28 00:15:03 +010037
Jakub Czapigad0bd0122021-08-09 15:51:37 +020038TEST_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 Werner1e14de82020-06-10 15:35:08 -070041
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 Wernere54d7842021-02-16 19:32:24 -080047TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
Jan Dabrosef1c9682020-03-28 00:15:03 +010048
49# Path for Kconfig autoheader
50TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
51
Julius Werner25400372020-05-04 17:46:31 -070052TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010053
Jakub Czapiga084ad932021-03-25 13:10:31 +010054TEST_CFLAGS += -D__TEST__
55
Jan Dabros7f00dba2020-05-22 09:57:17 +020056TEST_CFLAGS += -I$(cmockasrc)/include
57
Jan Dabrosef1c9682020-03-28 00:15:03 +010058# Link against Cmocka
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020059TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
Jan Dabros7f00dba2020-05-22 09:57:17 +020060TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010061
Julius Werner84446e62021-02-12 17:37:27 -080062# Some memlayout symbols don't work with userspace relocation -- disable it.
63TEST_CFLAGS += -fno-pie -fno-pic
64TEST_LDFLAGS += -no-pie
65
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060066# Enable code coverage if COV=1
67ifeq ($(COV),1)
68TEST_CFLAGS += --coverage
69TEST_LDFLAGS += --coverage
70endif
71
Jan Dabrosef1c9682020-03-28 00:15:03 +010072# Extra attributes for unit tests, declared per test
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020073attributes := srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010074
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020075stages := decompressor bootblock romstage smm verstage
76stages += ramstage rmodule postcar libagesa
Jan Dabrosef1c9682020-03-28 00:15:03 +010077
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020078alltests :=
79subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
80subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
81subdirs += tests/northbridge tests/security tests/soc tests/southbridge
82subdirs += tests/superio tests/vendorcode
Jan Dabrosef1c9682020-03-28 00:15:03 +010083
84define tests-handler
85alltests += $(1)$(2)
86$(foreach attribute,$(attributes),
87 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
88$(foreach attribute,$(attributes),
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020089 $(eval $(2)-$(attribute) := ))
Jan Dabrosef1c9682020-03-28 00:15:03 +010090
91# Sanity check for stage attribute value
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020092$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
Jan Dabrosef1c9682020-03-28 00:15:03 +010093$(if $(findstring $($(1)$(2)-stage), $(stages)),,
94 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
95 Check your $(dir $(1)$(2))Makefile.inc))
96endef
97
Jakub Czapiga9bf716e2021-08-09 15:00:43 +020098# Copy attributes of one test to another.
99# $1 - input test name
100# $2 - output test name
101copy-test = $(foreach attr,$(attributes), \
102 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
103
Jan Dabrosef1c9682020-03-28 00:15:03 +0100104$(call add-special-class, tests)
105$(call evaluate_subdirs)
106
107# Create actual targets for unit test binaries
108# $1 - test name
109define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200110
Julius Wernerab914072021-08-17 19:50:22 -0700111# Generate custom config.h redefining given config symbols, and declaring mocked
112# functions weak. It is important that the compiler already sees that they are
113# weak (and they aren't just turned weak at a later stage) to prevent certain
114# optimizations that would break if the function gets replaced. (For clang this
115# file needs to be marked `system_header` to prevent it from warning about
116# #pragma weak entries without a matching function declaration, since there's no
117# -Wno-xxx command line option for that.)
Jakub Czapiga203698a2021-05-25 15:56:36 +0200118$(1)-config-file := $(testobj)/$(1)/config.h
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200119$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
120 mkdir -p $$(dir $$@)
121 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
122 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
123 for kv in $$($(1)-config); do \
124 key="`echo $$$$kv | cut -d '=' -f -1`"; \
125 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
126 printf '#undef %s\n' "$$$$key" >> $$@; \
127 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
128 done
Julius Wernerab914072021-08-17 19:50:22 -0700129 printf '#ifdef __clang__\n' >> $$@;
130 printf '#pragma clang system_header\n' >> $$@;
131 printf '#endif\n' >> $$@;
132 printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
133 for m in $$($(1)-mocks); do \
134 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
135 done
136 printf '#endif\n' >> $$@;
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200137
138$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jakub Czapiga7c6081e2021-08-25 16:27:35 +0200139 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
140 -D__TEST_NAME__=\"$(1)\"
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200141
Julius Wernerab914072021-08-17 19:50:22 -0700142# Give us a way to distinguish between coreboot source files and test files in code.
143$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200144
145# Compile sources and apply mocking/wrapping of selected symbols.
146# For each listed mock add new symbol with prefix `__real_`,
147# and pointing to the same section:address.
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600148$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100149 mkdir -p $$(dir $$@)
Yu-Ping Wu0f88f6f2021-07-20 14:32:40 +0800150 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
151 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200152 objcopy_wrap_flags=''; \
153 for sym in $$($(1)-mocks); do \
Julius Wernerab914072021-08-17 19:50:22 -0700154 sym_line="$$$$($(OBJDUMP) -t $$@.orig | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200155 if [ ! -z "$$$$sym_line" ] ; then \
156 addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
157 section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
158 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
159 fi \
160 done ; \
Julius Wernerab914072021-08-17 19:50:22 -0700161 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
Jan Dabrosef1c9682020-03-28 00:15:03 +0100162
Jan Dabros7f00dba2020-05-22 09:57:17 +0200163$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100164 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
165
166endef
167
168$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200169 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +0200170 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200171 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100172 $(patsubst %.c,%.o,$($(test)-srcs)))))
173$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200174 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100175$(foreach test, $(alltests), \
176 $(eval $(call TEST_CC_template,$(test))))
177
178$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200179 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100180$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200181 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100182
183DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
184-include $(DEPENDENCIES)
185
Jan Dabros7f00dba2020-05-22 09:57:17 +0200186# Build cmocka
187$(CMOCKA_LIB):
188 echo "*** Building CMOCKA ***"
189 mkdir -p $(cmockaobj)
190 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
191 $(MAKE) -C $(cmockaobj)
192
Jan Dabrosef1c9682020-03-28 00:15:03 +0100193# Kconfig targets
194$(TEST_DOTCONFIG):
195 mkdir -p $(dir $@)
196 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
197
198# Don't override default Kconfig variables, since this will affect all
199# Kconfig targets. Change them only when calling sub-make instead.
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200200$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100201 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
202 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
203 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
204 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
205 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
206 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
207
208$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
209 mkdir -p $(dir $@)
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100210 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
211 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
Jan Dabrosef1c9682020-03-28 00:15:03 +0100212
213$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
214 true
215
216.PHONY: $(alltests) $(addprefix clean-,$(alltests))
217.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
218
Patrick Georgia0a198f2020-05-27 10:59:09 +0200219ifeq ($(JUNIT_OUTPUT),y)
220$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga1add4832021-03-05 11:37:23 +0100221$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200222endif
223
Jan Dabrosef1c9682020-03-28 00:15:03 +0100224$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200225 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
226 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100227
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600228# Build a code coverage report by collecting all the gcov files into a single
229# report. If COV is not set, this might be a user error, and they're trying
230# to generate a coverage report without first having built and run the code
231# with code coverage. So instead of silently correcting it by adding COV=1,
232# let's flag it to the user so they can be sure they're doing the thing they
233# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600234
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600235.PHONY: coverage-report clean-coverage-report
236
237ifeq ($(COV),1)
238coverage-report:
239 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
240 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600241 -s $(testobj)/tests.info
242
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600243clean-coverage-report:
244 rm -Rf $(testobj)/$(coverage_dir)
245else
246coverage-report:
247 COV=1 V=$(V) $(MAKE) coverage-report
248
249clean-coverage-report:
250 COV=1 V=$(V) $(MAKE) clean-coverage-report
251endif
252
Jan Dabrosef1c9682020-03-28 00:15:03 +0100253unit-tests: build-unit-tests run-unit-tests
254
255build-unit-tests: $(test-bins)
256
257run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200258 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
259 echo "**********************"; \
260 echo " TESTS FAILED"; \
261 echo "**********************"; \
262 exit 1; \
263 else \
264 echo "**********************"; \
265 echo " ALL TESTS PASSED"; \
266 echo "**********************"; \
267 exit 0; \
268 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100269
270$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600271 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100272
273clean-unit-tests:
274 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200275
276list-unit-tests:
277 @echo "unit-tests:"
278 for t in $(sort $(alltests)); do \
279 echo " $$t"; \
280 done
281
282help-unit-tests help::
283 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600284 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600285 @echo ' unit-tests - Run all unit-tests from tests/'
286 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
287 @echo ' list-unit-tests - List all unit-tests'
288 @echo ' <unit-test> - Build and run single unit-test'
289 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600290 @echo ' coverage-report - Generate a code coverage report'
291 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200292 @echo