blob: 85d30a2651089807dd5a45672addce51e5b9a54f [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
38TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
Julius Werner1e14de82020-06-10 15:35:08 -070039 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include
40
41# Note: This is intentionally just a subset of the warnings in the toplevel
42# Makefile.inc. We don't need to be as strict with test code, and things like
43# -Wmissing-prototypes just make working with the test framework cumbersome.
44# Only put conservative warnings here that really detect code that's obviously
45# unintentional.
Julius Wernere54d7842021-02-16 19:32:24 -080046TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
Jan Dabrosef1c9682020-03-28 00:15:03 +010047
48# Path for Kconfig autoheader
49TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
50
Julius Werner25400372020-05-04 17:46:31 -070051TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010052
Jakub Czapiga084ad932021-03-25 13:10:31 +010053TEST_CFLAGS += -D__TEST__
54
Jan Dabros7f00dba2020-05-22 09:57:17 +020055TEST_CFLAGS += -I$(cmockasrc)/include
56
Jan Dabrosef1c9682020-03-28 00:15:03 +010057# Link against Cmocka
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020058TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
Jan Dabros7f00dba2020-05-22 09:57:17 +020059TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010060
Julius Werner84446e62021-02-12 17:37:27 -080061# Some memlayout symbols don't work with userspace relocation -- disable it.
62TEST_CFLAGS += -fno-pie -fno-pic
63TEST_LDFLAGS += -no-pie
64
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060065# Enable code coverage if COV=1
66ifeq ($(COV),1)
67TEST_CFLAGS += --coverage
68TEST_LDFLAGS += --coverage
69endif
70
Jan Dabrosef1c9682020-03-28 00:15:03 +010071# Extra attributes for unit tests, declared per test
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020072attributes := srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010073
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020074stages := decompressor bootblock romstage smm verstage
75stages += ramstage rmodule postcar libagesa
Jan Dabrosef1c9682020-03-28 00:15:03 +010076
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020077alltests :=
78subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
79subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
80subdirs += tests/northbridge tests/security tests/soc tests/southbridge
81subdirs += tests/superio tests/vendorcode
Jan Dabrosef1c9682020-03-28 00:15:03 +010082
83define tests-handler
84alltests += $(1)$(2)
85$(foreach attribute,$(attributes),
86 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
87$(foreach attribute,$(attributes),
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020088 $(eval $(2)-$(attribute) := ))
Jan Dabrosef1c9682020-03-28 00:15:03 +010089
90# Sanity check for stage attribute value
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +020091$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
Jan Dabrosef1c9682020-03-28 00:15:03 +010092$(if $(findstring $($(1)$(2)-stage), $(stages)),,
93 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
94 Check your $(dir $(1)$(2))Makefile.inc))
95endef
96
97$(call add-special-class, tests)
98$(call evaluate_subdirs)
99
100# Create actual targets for unit test binaries
101# $1 - test name
102define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200103
104# Generate custom config.h redefining given symbols
Jakub Czapiga203698a2021-05-25 15:56:36 +0200105$(1)-config-file := $(testobj)/$(1)/config.h
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200106$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
107 mkdir -p $$(dir $$@)
108 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
109 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
110 for kv in $$($(1)-config); do \
111 key="`echo $$$$kv | cut -d '=' -f -1`"; \
112 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
113 printf '#undef %s\n' "$$$$key" >> $$@; \
114 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
115 done
116
117$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100118 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200119
120# Weaken symbols listed as mocks to enable overriding in the code
Patrick Georgice55ca22021-06-09 18:37:42 +0200121$($(1)-srcobjs): OBJCOPY_FLAGS += $$(foreach mock,$$($(1)-mocks),--globalize-symbol=$$(mock) --weaken-symbol=$$(mock))
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200122
123# Compile sources and apply mocking/wrapping of selected symbols.
124# For each listed mock add new symbol with prefix `__real_`,
125# and pointing to the same section:address.
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600126$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100127 mkdir -p $$(dir $$@)
Yu-Ping Wu0f88f6f2021-07-20 14:32:40 +0800128 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
129 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
Jakub Czapigaa7a49e62021-07-29 13:27:54 +0200130 $(OBJCOPY) $$@.orig $$(OBJCOPY_FLAGS) $$@.orig2
131 objcopy_wrap_flags=''; \
132 for sym in $$($(1)-mocks); do \
133 sym_line="$$$$($(OBJDUMP) -t $$@.orig2 | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
134 if [ ! -z "$$$$sym_line" ] ; then \
135 addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
136 section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
137 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
138 fi \
139 done ; \
140 $(OBJCOPY) $$@.orig2 $$$$objcopy_wrap_flags $$@
Jan Dabrosef1c9682020-03-28 00:15:03 +0100141
Jan Dabros7f00dba2020-05-22 09:57:17 +0200142$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100143 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
144
145endef
146
147$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200148 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
Patrick Georgice55ca22021-06-09 18:37:42 +0200149 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200150 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100151 $(patsubst %.c,%.o,$($(test)-srcs)))))
152$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200153 $(eval $(test)-bin := $(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100154$(foreach test, $(alltests), \
155 $(eval $(call TEST_CC_template,$(test))))
156
157$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200158 $(eval all-test-objs += $($(test)-objs)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100159$(foreach test, $(alltests), \
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200160 $(eval test-bins += $($(test)-bin)))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100161
162DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
163-include $(DEPENDENCIES)
164
Jan Dabros7f00dba2020-05-22 09:57:17 +0200165# Build cmocka
166$(CMOCKA_LIB):
167 echo "*** Building CMOCKA ***"
168 mkdir -p $(cmockaobj)
169 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
170 $(MAKE) -C $(cmockaobj)
171
Jan Dabrosef1c9682020-03-28 00:15:03 +0100172# Kconfig targets
173$(TEST_DOTCONFIG):
174 mkdir -p $(dir $@)
175 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
176
177# Don't override default Kconfig variables, since this will affect all
178# Kconfig targets. Change them only when calling sub-make instead.
Jakub Czapigaeaf5ff32021-08-09 14:20:28 +0200179$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100180 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
181 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
182 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
183 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
184 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
185 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
186
187$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
188 mkdir -p $(dir $@)
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100189 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
190 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
Jan Dabrosef1c9682020-03-28 00:15:03 +0100191
192$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
193 true
194
195.PHONY: $(alltests) $(addprefix clean-,$(alltests))
196.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
197
Patrick Georgia0a198f2020-05-27 10:59:09 +0200198ifeq ($(JUNIT_OUTPUT),y)
199$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga1add4832021-03-05 11:37:23 +0100200$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200201endif
202
Jan Dabrosef1c9682020-03-28 00:15:03 +0100203$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200204 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
205 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100206
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600207# Build a code coverage report by collecting all the gcov files into a single
208# report. If COV is not set, this might be a user error, and they're trying
209# to generate a coverage report without first having built and run the code
210# with code coverage. So instead of silently correcting it by adding COV=1,
211# let's flag it to the user so they can be sure they're doing the thing they
212# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600213
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600214.PHONY: coverage-report clean-coverage-report
215
216ifeq ($(COV),1)
217coverage-report:
218 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
219 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600220 -s $(testobj)/tests.info
221
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600222clean-coverage-report:
223 rm -Rf $(testobj)/$(coverage_dir)
224else
225coverage-report:
226 COV=1 V=$(V) $(MAKE) coverage-report
227
228clean-coverage-report:
229 COV=1 V=$(V) $(MAKE) clean-coverage-report
230endif
231
Jan Dabrosef1c9682020-03-28 00:15:03 +0100232unit-tests: build-unit-tests run-unit-tests
233
234build-unit-tests: $(test-bins)
235
236run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200237 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
238 echo "**********************"; \
239 echo " TESTS FAILED"; \
240 echo "**********************"; \
241 exit 1; \
242 else \
243 echo "**********************"; \
244 echo " ALL TESTS PASSED"; \
245 echo "**********************"; \
246 exit 0; \
247 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100248
249$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600250 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100251
252clean-unit-tests:
253 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200254
255list-unit-tests:
256 @echo "unit-tests:"
257 for t in $(sort $(alltests)); do \
258 echo " $$t"; \
259 done
260
261help-unit-tests help::
262 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600263 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600264 @echo ' unit-tests - Run all unit-tests from tests/'
265 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
266 @echo ' list-unit-tests - List all unit-tests'
267 @echo ' <unit-test> - Build and run single unit-test'
268 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600269 @echo ' coverage-report - Generate a code coverage report'
270 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200271 @echo