blob: 15ea670336bfc9ff56513809b5576df7968e8201 [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
Julius Werner21744812020-05-04 17:44:04 -07003testsrc = $(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)
8testobj = $(obj)/coverage
9else
Jan Dabrosef1c9682020-03-28 00:15:03 +010010testobj = $(obj)/tests
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060011endif
12
Jan Dabros7f00dba2020-05-22 09:57:17 +020013cmockasrc = 3rdparty/cmocka
Patrick Georgia3402972020-05-27 10:53:01 +020014cmockaobj = $(objutil)/cmocka
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060015coverage_dir = coverage_reports
Jan Dabros7f00dba2020-05-22 09:57:17 +020016
17CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
18
19CMAKE:= cmake
Patrick Georgice55ca22021-06-09 18:37:42 +020020OBJCOPY?= objcopy
Jan Dabrosef1c9682020-03-28 00:15:03 +010021
22TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
23TEST_DOTCONFIG = $(testobj)/.config
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020024TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010025TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
26TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
27TEST_KCONFIG_SPLITCONFIG := $(testobj)/config
28TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
29
Julius Werner21744812020-05-04 17:44:04 -070030TEST_CFLAGS = -include $(src)/include/kconfig.h \
31 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
32 -include $(src)/include/rules.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010033
34# Include generic test mock headers, before original ones
Julius Werner21744812020-05-04 17:44:04 -070035TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
Jan Dabrosef1c9682020-03-28 00:15:03 +010036
37TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
Julius Werner1e14de82020-06-10 15:35:08 -070038 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include
39
40# Note: This is intentionally just a subset of the warnings in the toplevel
41# Makefile.inc. We don't need to be as strict with test code, and things like
42# -Wmissing-prototypes just make working with the test framework cumbersome.
43# Only put conservative warnings here that really detect code that's obviously
44# unintentional.
Julius Wernere54d7842021-02-16 19:32:24 -080045TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
Jan Dabrosef1c9682020-03-28 00:15:03 +010046
47# Path for Kconfig autoheader
48TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
49
Julius Werner25400372020-05-04 17:46:31 -070050TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010051
Jakub Czapiga084ad932021-03-25 13:10:31 +010052TEST_CFLAGS += -D__TEST__
53
Jan Dabros7f00dba2020-05-22 09:57:17 +020054TEST_CFLAGS += -I$(cmockasrc)/include
55
Jan Dabrosef1c9682020-03-28 00:15:03 +010056# Link against Cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +020057TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
58TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010059
Julius Werner84446e62021-02-12 17:37:27 -080060# Some memlayout symbols don't work with userspace relocation -- disable it.
61TEST_CFLAGS += -fno-pie -fno-pic
62TEST_LDFLAGS += -no-pie
63
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060064# Enable code coverage if COV=1
65ifeq ($(COV),1)
66TEST_CFLAGS += --coverage
67TEST_LDFLAGS += --coverage
68endif
69
Jan Dabrosef1c9682020-03-28 00:15:03 +010070# Extra attributes for unit tests, declared per test
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020071attributes:= srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010072
73stages:= decompressor bootblock romstage smm verstage
74stages+= ramstage rmodule postcar libagesa
75
76alltests:=
Jakub Czapiga168b38b2021-02-11 12:59:51 +010077subdirs:= tests/arch tests/acpi tests/commonlib tests/console tests/cpu
78subdirs+= tests/device tests/drivers tests/ec tests/lib tests/mainboard
Jan Dabrosef1c9682020-03-28 00:15:03 +010079subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
80subdirs+= tests/superio tests/vendorcode
81
82define tests-handler
83alltests += $(1)$(2)
84$(foreach attribute,$(attributes),
85 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
86$(foreach attribute,$(attributes),
87 $(eval $(2)-$(attribute):=))
88
89# Sanity check for stage attribute value
90$(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
91$(if $(findstring $($(1)$(2)-stage), $(stages)),,
92 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
93 Check your $(dir $(1)$(2))Makefile.inc))
94endef
95
96$(call add-special-class, tests)
97$(call evaluate_subdirs)
98
99# Create actual targets for unit test binaries
100# $1 - test name
101define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200102
103# Generate custom config.h redefining given symbols
Jakub Czapiga203698a2021-05-25 15:56:36 +0200104$(1)-config-file := $(testobj)/$(1)/config.h
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200105$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
106 mkdir -p $$(dir $$@)
107 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
108 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
109 for kv in $$($(1)-config); do \
110 key="`echo $$$$kv | cut -d '=' -f -1`"; \
111 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
112 printf '#undef %s\n' "$$$$key" >> $$@; \
113 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
114 done
115
116$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100117 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
Patrick Georgice55ca22021-06-09 18:37:42 +0200118$($(1)-srcobjs): OBJCOPY_FLAGS += $$(foreach mock,$$($(1)-mocks),--globalize-symbol=$$(mock) --weaken-symbol=$$(mock))
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600119$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100120 mkdir -p $$(dir $$@)
121 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
Patrick Georgice55ca22021-06-09 18:37:42 +0200122 -MT $$@ -c $$< -o $$@.orig
123 $(OBJCOPY) $$@.orig $$(OBJCOPY_FLAGS) $$@
Jan Dabrosef1c9682020-03-28 00:15:03 +0100124
Jan Dabros7f00dba2020-05-22 09:57:17 +0200125$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100126 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
127
128endef
129
130$(foreach test, $(alltests), \
Patrick Georgice55ca22021-06-09 18:37:42 +0200131 $(eval $(test)-srcobjs:=$(addprefix $(testobj)/$(test)/, \
132 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600133 $(eval $(test)-objs:=$(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100134 $(patsubst %.c,%.o,$($(test)-srcs)))))
135$(foreach test, $(alltests), \
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600136 $(eval $(test)-bin:=$(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100137$(foreach test, $(alltests), \
138 $(eval $(call TEST_CC_template,$(test))))
139
140$(foreach test, $(alltests), \
141 $(eval all-test-objs+=$($(test)-objs)))
142$(foreach test, $(alltests), \
143 $(eval test-bins+=$($(test)-bin)))
144
145DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
146-include $(DEPENDENCIES)
147
Jan Dabros7f00dba2020-05-22 09:57:17 +0200148# Build cmocka
149$(CMOCKA_LIB):
150 echo "*** Building CMOCKA ***"
151 mkdir -p $(cmockaobj)
152 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
153 $(MAKE) -C $(cmockaobj)
154
Jan Dabrosef1c9682020-03-28 00:15:03 +0100155# Kconfig targets
156$(TEST_DOTCONFIG):
157 mkdir -p $(dir $@)
158 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
159
160# Don't override default Kconfig variables, since this will affect all
161# Kconfig targets. Change them only when calling sub-make instead.
162$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
163 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
164 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
165 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
166 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
167 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
168 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
169
170$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
171 mkdir -p $(dir $@)
172 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
173 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
174
175$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
176 true
177
178.PHONY: $(alltests) $(addprefix clean-,$(alltests))
179.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
180
Patrick Georgia0a198f2020-05-27 10:59:09 +0200181ifeq ($(JUNIT_OUTPUT),y)
182$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga1add4832021-03-05 11:37:23 +0100183$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200184endif
185
Jan Dabrosef1c9682020-03-28 00:15:03 +0100186$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200187 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
188 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100189
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600190# Build a code coverage report by collecting all the gcov files into a single
191# report. If COV is not set, this might be a user error, and they're trying
192# to generate a coverage report without first having built and run the code
193# with code coverage. So instead of silently correcting it by adding COV=1,
194# let's flag it to the user so they can be sure they're doing the thing they
195# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600196
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600197.PHONY: coverage-report clean-coverage-report
198
199ifeq ($(COV),1)
200coverage-report:
201 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
202 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600203 -s $(testobj)/tests.info
204
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600205clean-coverage-report:
206 rm -Rf $(testobj)/$(coverage_dir)
207else
208coverage-report:
209 COV=1 V=$(V) $(MAKE) coverage-report
210
211clean-coverage-report:
212 COV=1 V=$(V) $(MAKE) clean-coverage-report
213endif
214
Jan Dabrosef1c9682020-03-28 00:15:03 +0100215unit-tests: build-unit-tests run-unit-tests
216
217build-unit-tests: $(test-bins)
218
219run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200220 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
221 echo "**********************"; \
222 echo " TESTS FAILED"; \
223 echo "**********************"; \
224 exit 1; \
225 else \
226 echo "**********************"; \
227 echo " ALL TESTS PASSED"; \
228 echo "**********************"; \
229 exit 0; \
230 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100231
232$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600233 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100234
235clean-unit-tests:
236 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200237
238list-unit-tests:
239 @echo "unit-tests:"
240 for t in $(sort $(alltests)); do \
241 echo " $$t"; \
242 done
243
244help-unit-tests help::
245 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600246 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600247 @echo ' unit-tests - Run all unit-tests from tests/'
248 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
249 @echo ' list-unit-tests - List all unit-tests'
250 @echo ' <unit-test> - Build and run single unit-test'
251 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600252 @echo ' coverage-report - Generate a code coverage report'
253 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200254 @echo