blob: 847beaf199b7eb2e0b1f950528c1b3c045a810cb [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
Julius Wernerc90d4062021-12-01 13:52:25 -080048TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type
Jan Dabrosef1c9682020-03-28 00:15:03 +010049
50# Path for Kconfig autoheader
51TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
52
Julius Werner25400372020-05-04 17:46:31 -070053TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010054
Jakub Czapiga084ad932021-03-25 13:10:31 +010055TEST_CFLAGS += -D__TEST__
56
Jan Dabros7f00dba2020-05-22 09:57:17 +020057TEST_CFLAGS += -I$(cmockasrc)/include
58
Jakub Czapiga29faa8a2021-09-09 12:11:39 +020059ifneq ($(filter-out 0,$(TEST_PRINT)),)
60TEST_CFLAGS += -DTEST_PRINT=1
61endif
62
Jan Dabrosef1c9682020-03-28 00:15:03 +010063# Link against Cmocka
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020064TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
Jan Dabros7f00dba2020-05-22 09:57:17 +020065TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010066
Julius Werner84446e62021-02-12 17:37:27 -080067# Some memlayout symbols don't work with userspace relocation -- disable it.
68TEST_CFLAGS += -fno-pie -fno-pic
69TEST_LDFLAGS += -no-pie
70
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060071# Enable code coverage if COV=1
72ifeq ($(COV),1)
73TEST_CFLAGS += --coverage
74TEST_LDFLAGS += --coverage
75endif
76
Jan Dabrosef1c9682020-03-28 00:15:03 +010077# Extra attributes for unit tests, declared per test
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020078attributes := srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010079
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020080stages := decompressor bootblock romstage smm verstage
81stages += ramstage rmodule postcar libagesa
Jan Dabrosef1c9682020-03-28 00:15:03 +010082
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020083alltests :=
84subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
85subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
86subdirs += tests/northbridge tests/security tests/soc tests/southbridge
87subdirs += tests/superio tests/vendorcode
Jan Dabrosef1c9682020-03-28 00:15:03 +010088
89define tests-handler
90alltests += $(1)$(2)
91$(foreach attribute,$(attributes),
92 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
93$(foreach attribute,$(attributes),
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020094 $(eval $(2)-$(attribute) := ))
Jan Dabrosef1c9682020-03-28 00:15:03 +010095
96# Sanity check for stage attribute value
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020097$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
Jan Dabrosef1c9682020-03-28 00:15:03 +010098$(if $(findstring $($(1)$(2)-stage), $(stages)),,
99 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
100 Check your $(dir $(1)$(2))Makefile.inc))
101endef
102
Jakub Czapiga9bf716e2021-08-09 15:00:43 +0200103# Copy attributes of one test to another.
104# $1 - input test name
105# $2 - output test name
106copy-test = $(foreach attr,$(attributes), \
107 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
108
Jan Dabrosef1c9682020-03-28 00:15:03 +0100109$(call add-special-class, tests)
110$(call evaluate_subdirs)
111
112# Create actual targets for unit test binaries
113# $1 - test name
114define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200115
Julius Wernerab914072021-08-17 19:50:22 -0700116# 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 Czapiga203698a2021-05-25 15:56:36 +0200123$(1)-config-file := $(testobj)/$(1)/config.h
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200124$$($(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 Wernerab914072021-08-17 19:50:22 -0700134 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 Czapiga5c3b05e2021-04-28 16:50:51 +0200142
143$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jakub Czapiga7c6081e2021-08-25 16:27:35 +0200144 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
Jakub Czapigac1e4c5a2021-07-23 13:35:14 +0200145 -D__TEST_NAME__=\"$(subst /,_,$(1))\" \
146 -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200147
Julius Wernerab914072021-08-17 19:50:22 -0700148# Give us a way to distinguish between coreboot source files and test files in code.
149$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200150
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 Fagerburgde6cbac2021-05-11 09:56:48 -0600154$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100155 mkdir -p $$(dir $$@)
Yu-Ping Wu0f88f6f2021-07-20 14:32:40 +0800156 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
157 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200158 objcopy_wrap_flags=''; \
159 for sym in $$($(1)-mocks); do \
Jakub Czapigab88d8452021-12-03 08:23:28 +0000160 sym_line="$$$$($(OBJDUMP) -t $$@.orig \
161 | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200162 if [ ! -z "$$$$sym_line" ] ; then \
Jakub Czapigab88d8452021-12-03 08:23:28 +0000163 addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \
164 section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200165 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
166 fi \
167 done ; \
Julius Wernerab914072021-08-17 19:50:22 -0700168 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
Jan Dabrosef1c9682020-03-28 00:15:03 +0100169
Jan Dabros7f00dba2020-05-22 09:57:17 +0200170$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100171 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
172
173endef
174
175$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200176 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +0200177 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200178 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100179 $(patsubst %.c,%.o,$($(test)-srcs)))))
180$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200181 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100182$(foreach test, $(alltests), \
183 $(eval $(call TEST_CC_template,$(test))))
184
185$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200186 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100187$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200188 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100189
190DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
191-include $(DEPENDENCIES)
192
Jan Dabros7f00dba2020-05-22 09:57:17 +0200193# 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 Dabrosef1c9682020-03-28 00:15:03 +0100200# 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 Czapigaeaf5ff32021-08-09 14:20:28 +0200207$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100208 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 Georgi53ea1d42019-11-22 16:55:58 +0100217 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
218 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
Jan Dabrosef1c9682020-03-28 00:15:03 +0100219
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 Czapiga4d9694e2021-10-07 09:06:42 +0000226# %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 Georgia0a198f2020-05-27 10:59:09 +0200229ifeq ($(JUNIT_OUTPUT),y)
230$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000231$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200232endif
233
Jan Dabrosef1c9682020-03-28 00:15:03 +0100234$(alltests): $$($$(@)-bin)
Jakub Czapiga4d9694e2021-10-07 09:06:42 +0000235 rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
236 rm -f $(testobj)/$(subst /,_,$^).failed
Raul E Rangel940953e2021-07-23 16:43:18 -0600237 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100238
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600239# 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 Fagerburg045fbf12021-04-16 11:36:25 -0600245
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600246.PHONY: coverage-report clean-coverage-report
247
248ifeq ($(COV),1)
249coverage-report:
250 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
251 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600252 -s $(testobj)/tests.info
253
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600254clean-coverage-report:
255 rm -Rf $(testobj)/$(coverage_dir)
256else
257coverage-report:
258 COV=1 V=$(V) $(MAKE) coverage-report
259
260clean-coverage-report:
261 COV=1 V=$(V) $(MAKE) clean-coverage-report
262endif
263
Jan Dabrosef1c9682020-03-28 00:15:03 +0100264unit-tests: build-unit-tests run-unit-tests
265
266build-unit-tests: $(test-bins)
267
268run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200269 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 Dabrosef1c9682020-03-28 00:15:03 +0100280
281$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600282 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100283
284clean-unit-tests:
285 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200286
287list-unit-tests:
288 @echo "unit-tests:"
289 for t in $(sort $(alltests)); do \
290 echo " $$t"; \
291 done
292
293help-unit-tests help::
294 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600295 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600296 @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 Fagerburgde6cbac2021-05-11 09:56:48 -0600301 @echo ' coverage-report - Generate a code coverage report'
302 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200303 @echo