blob: 982f42baf7a48ddac9be506a3aefc005962f5a8a [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
Jakub Czapiga29faa8a2021-09-09 12:11:39 +020058ifneq ($(filter-out 0,$(TEST_PRINT)),)
59TEST_CFLAGS += -DTEST_PRINT=1
60endif
61
Jan Dabrosef1c9682020-03-28 00:15:03 +010062# Link against Cmocka
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020063TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
Jan Dabros7f00dba2020-05-22 09:57:17 +020064TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010065
Julius Werner84446e62021-02-12 17:37:27 -080066# Some memlayout symbols don't work with userspace relocation -- disable it.
67TEST_CFLAGS += -fno-pie -fno-pic
68TEST_LDFLAGS += -no-pie
69
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060070# Enable code coverage if COV=1
71ifeq ($(COV),1)
72TEST_CFLAGS += --coverage
73TEST_LDFLAGS += --coverage
74endif
75
Jan Dabrosef1c9682020-03-28 00:15:03 +010076# Extra attributes for unit tests, declared per test
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020077attributes := srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010078
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020079stages := decompressor bootblock romstage smm verstage
80stages += ramstage rmodule postcar libagesa
Jan Dabrosef1c9682020-03-28 00:15:03 +010081
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020082alltests :=
83subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
84subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
85subdirs += tests/northbridge tests/security tests/soc tests/southbridge
86subdirs += tests/superio tests/vendorcode
Jan Dabrosef1c9682020-03-28 00:15:03 +010087
88define tests-handler
89alltests += $(1)$(2)
90$(foreach attribute,$(attributes),
91 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
92$(foreach attribute,$(attributes),
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020093 $(eval $(2)-$(attribute) := ))
Jan Dabrosef1c9682020-03-28 00:15:03 +010094
95# Sanity check for stage attribute value
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020096$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
Jan Dabrosef1c9682020-03-28 00:15:03 +010097$(if $(findstring $($(1)$(2)-stage), $(stages)),,
98 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
99 Check your $(dir $(1)$(2))Makefile.inc))
100endef
101
Jakub Czapiga9bf716e2021-08-09 15:00:43 +0200102# Copy attributes of one test to another.
103# $1 - input test name
104# $2 - output test name
105copy-test = $(foreach attr,$(attributes), \
106 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
107
Jan Dabrosef1c9682020-03-28 00:15:03 +0100108$(call add-special-class, tests)
109$(call evaluate_subdirs)
110
111# Create actual targets for unit test binaries
112# $1 - test name
113define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200114
Julius Wernerab914072021-08-17 19:50:22 -0700115# 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.)
Jakub Czapiga203698a2021-05-25 15:56:36 +0200122$(1)-config-file := $(testobj)/$(1)/config.h
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200123$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
124 mkdir -p $$(dir $$@)
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" >> $$@; \
132 done
Julius Wernerab914072021-08-17 19:50:22 -0700133 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" >> $$@; \
139 done
140 printf '#endif\n' >> $$@;
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200141
142$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jakub Czapiga7c6081e2021-08-25 16:27:35 +0200143 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000144 -D__TEST_NAME__=\"$(subst /,_,$(1))\"
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200145
Julius Wernerab914072021-08-17 19:50:22 -0700146# Give us a way to distinguish between coreboot source files and test files in code.
147$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200148
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.
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600152$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100153 mkdir -p $$(dir $$@)
Yu-Ping Wu0f88f6f2021-07-20 14:32:40 +0800154 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
155 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200156 objcopy_wrap_flags=''; \
157 for sym in $$($(1)-mocks); do \
Julius Wernerab914072021-08-17 19:50:22 -0700158 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 +0200159 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"; \
163 fi \
164 done ; \
Julius Wernerab914072021-08-17 19:50:22 -0700165 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
Jan Dabrosef1c9682020-03-28 00:15:03 +0100166
Jan Dabros7f00dba2020-05-22 09:57:17 +0200167$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100168 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
169
170endef
171
172$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200173 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +0200174 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200175 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100176 $(patsubst %.c,%.o,$($(test)-srcs)))))
177$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200178 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100179$(foreach test, $(alltests), \
180 $(eval $(call TEST_CC_template,$(test))))
181
182$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200183 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100184$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200185 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100186
187DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
188-include $(DEPENDENCIES)
189
Jan Dabros7f00dba2020-05-22 09:57:17 +0200190# Build cmocka
191$(CMOCKA_LIB):
192 echo "*** Building CMOCKA ***"
193 mkdir -p $(cmockaobj)
194 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
195 $(MAKE) -C $(cmockaobj)
196
Jan Dabrosef1c9682020-03-28 00:15:03 +0100197# Kconfig targets
198$(TEST_DOTCONFIG):
199 mkdir -p $(dir $@)
200 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
201
202# Don't override default Kconfig variables, since this will affect all
203# Kconfig targets. Change them only when calling sub-make instead.
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200204$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100205 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)
211
212$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
213 mkdir -p $(dir $@)
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100214 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
215 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
Jan Dabrosef1c9682020-03-28 00:15:03 +0100216
217$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
218 true
219
220.PHONY: $(alltests) $(addprefix clean-,$(alltests))
221.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
222
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000223# %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
224# by macro cb_run_group_tests(), which should be used for running tests.
225# __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
Patrick Georgia0a198f2020-05-27 10:59:09 +0200226ifeq ($(JUNIT_OUTPUT),y)
227$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000228$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200229endif
230
Jan Dabrosef1c9682020-03-28 00:15:03 +0100231$(alltests): $$($$(@)-bin)
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000232 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
233 rm -f $(testobj)/$(subst /,_,$^).failed
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200234 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100235
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600236# Build a code coverage report by collecting all the gcov files into a single
237# report. If COV is not set, this might be a user error, and they're trying
238# to generate a coverage report without first having built and run the code
239# with code coverage. So instead of silently correcting it by adding COV=1,
240# let's flag it to the user so they can be sure they're doing the thing they
241# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600242
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600243.PHONY: coverage-report clean-coverage-report
244
245ifeq ($(COV),1)
246coverage-report:
247 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
248 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600249 -s $(testobj)/tests.info
250
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600251clean-coverage-report:
252 rm -Rf $(testobj)/$(coverage_dir)
253else
254coverage-report:
255 COV=1 V=$(V) $(MAKE) coverage-report
256
257clean-coverage-report:
258 COV=1 V=$(V) $(MAKE) clean-coverage-report
259endif
260
Jan Dabrosef1c9682020-03-28 00:15:03 +0100261unit-tests: build-unit-tests run-unit-tests
262
263build-unit-tests: $(test-bins)
264
265run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200266 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
267 echo "**********************"; \
268 echo " TESTS FAILED"; \
269 echo "**********************"; \
270 exit 1; \
271 else \
272 echo "**********************"; \
273 echo " ALL TESTS PASSED"; \
274 echo "**********************"; \
275 exit 0; \
276 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100277
278$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600279 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100280
281clean-unit-tests:
282 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200283
284list-unit-tests:
285 @echo "unit-tests:"
286 for t in $(sort $(alltests)); do \
287 echo " $$t"; \
288 done
289
290help-unit-tests help::
291 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600292 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600293 @echo ' unit-tests - Run all unit-tests from tests/'
294 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
295 @echo ' list-unit-tests - List all unit-tests'
296 @echo ' <unit-test> - Build and run single unit-test'
297 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600298 @echo ' coverage-report - Generate a code coverage report'
299 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200300 @echo