blob: 7a7ab1d03723f909276a4fe8b02e65ce07bac611 [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 Czapigac1e4c5a2021-07-23 13:35:14 +0200144 -D__TEST_NAME__=\"$(subst /,_,$(1))\" \
145 -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200146
Julius Wernerab914072021-08-17 19:50:22 -0700147# Give us a way to distinguish between coreboot source files and test files in code.
148$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200149
150# Compile sources and apply mocking/wrapping of selected symbols.
151# For each listed mock add new symbol with prefix `__real_`,
152# and pointing to the same section:address.
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600153$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100154 mkdir -p $$(dir $$@)
Yu-Ping Wu0f88f6f2021-07-20 14:32:40 +0800155 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
156 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200157 objcopy_wrap_flags=''; \
158 for sym in $$($(1)-mocks); do \
Julius Wernerab914072021-08-17 19:50:22 -0700159 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 +0200160 if [ ! -z "$$$$sym_line" ] ; then \
161 addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
162 section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
163 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
164 fi \
165 done ; \
Julius Wernerab914072021-08-17 19:50:22 -0700166 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
Jan Dabrosef1c9682020-03-28 00:15:03 +0100167
Jan Dabros7f00dba2020-05-22 09:57:17 +0200168$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100169 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
170
171endef
172
173$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200174 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +0200175 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200176 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100177 $(patsubst %.c,%.o,$($(test)-srcs)))))
178$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200179 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100180$(foreach test, $(alltests), \
181 $(eval $(call TEST_CC_template,$(test))))
182
183$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200184 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100185$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200186 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100187
188DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
189-include $(DEPENDENCIES)
190
Jan Dabros7f00dba2020-05-22 09:57:17 +0200191# Build cmocka
192$(CMOCKA_LIB):
193 echo "*** Building CMOCKA ***"
194 mkdir -p $(cmockaobj)
195 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
196 $(MAKE) -C $(cmockaobj)
197
Jan Dabrosef1c9682020-03-28 00:15:03 +0100198# Kconfig targets
199$(TEST_DOTCONFIG):
200 mkdir -p $(dir $@)
201 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
202
203# Don't override default Kconfig variables, since this will affect all
204# Kconfig targets. Change them only when calling sub-make instead.
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200205$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100206 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
207 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
208 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
209 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
210 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
211 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
212
213$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
214 mkdir -p $(dir $@)
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100215 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
216 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
Jan Dabrosef1c9682020-03-28 00:15:03 +0100217
218$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
219 true
220
221.PHONY: $(alltests) $(addprefix clean-,$(alltests))
222.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
223
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000224# %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
225# by macro cb_run_group_tests(), which should be used for running tests.
226# __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
Patrick Georgia0a198f2020-05-27 10:59:09 +0200227ifeq ($(JUNIT_OUTPUT),y)
228$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000229$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200230endif
231
Jan Dabrosef1c9682020-03-28 00:15:03 +0100232$(alltests): $$($$(@)-bin)
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000233 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
234 rm -f $(testobj)/$(subst /,_,$^).failed
Raul E Rangel940953e2021-07-23 16:43:18 -0600235 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100236
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600237# Build a code coverage report by collecting all the gcov files into a single
238# report. If COV is not set, this might be a user error, and they're trying
239# to generate a coverage report without first having built and run the code
240# with code coverage. So instead of silently correcting it by adding COV=1,
241# let's flag it to the user so they can be sure they're doing the thing they
242# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600243
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600244.PHONY: coverage-report clean-coverage-report
245
246ifeq ($(COV),1)
247coverage-report:
248 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
249 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600250 -s $(testobj)/tests.info
251
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600252clean-coverage-report:
253 rm -Rf $(testobj)/$(coverage_dir)
254else
255coverage-report:
256 COV=1 V=$(V) $(MAKE) coverage-report
257
258clean-coverage-report:
259 COV=1 V=$(V) $(MAKE) clean-coverage-report
260endif
261
Jan Dabrosef1c9682020-03-28 00:15:03 +0100262unit-tests: build-unit-tests run-unit-tests
263
264build-unit-tests: $(test-bins)
265
266run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200267 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
268 echo "**********************"; \
269 echo " TESTS FAILED"; \
270 echo "**********************"; \
271 exit 1; \
272 else \
273 echo "**********************"; \
274 echo " ALL TESTS PASSED"; \
275 echo "**********************"; \
276 exit 0; \
277 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100278
279$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600280 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100281
282clean-unit-tests:
283 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200284
285list-unit-tests:
286 @echo "unit-tests:"
287 for t in $(sort $(alltests)); do \
288 echo " $$t"; \
289 done
290
291help-unit-tests help::
292 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600293 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600294 @echo ' unit-tests - Run all unit-tests from tests/'
295 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
296 @echo ' list-unit-tests - List all unit-tests'
297 @echo ' <unit-test> - Build and run single unit-test'
298 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600299 @echo ' coverage-report - Generate a code coverage report'
300 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200301 @echo