blob: 3473ef74d0b81a8ce75bcff67f6773b670e722a7 [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
Jan Dabrosef1c9682020-03-28 00:15:03 +010020
21TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
22TEST_DOTCONFIG = $(testobj)/.config
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020023TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010024TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
25TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
26TEST_KCONFIG_SPLITCONFIG := $(testobj)/config
27TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
28
Julius Werner21744812020-05-04 17:44:04 -070029TEST_CFLAGS = -include $(src)/include/kconfig.h \
30 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
31 -include $(src)/include/rules.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010032
33# Include generic test mock headers, before original ones
Julius Werner21744812020-05-04 17:44:04 -070034TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
Jan Dabrosef1c9682020-03-28 00:15:03 +010035
36TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
Julius Werner1e14de82020-06-10 15:35:08 -070037 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include
38
39# Note: This is intentionally just a subset of the warnings in the toplevel
40# Makefile.inc. We don't need to be as strict with test code, and things like
41# -Wmissing-prototypes just make working with the test framework cumbersome.
42# Only put conservative warnings here that really detect code that's obviously
43# unintentional.
Julius Wernere54d7842021-02-16 19:32:24 -080044TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
Jan Dabrosef1c9682020-03-28 00:15:03 +010045
46# Path for Kconfig autoheader
47TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
48
Julius Werner25400372020-05-04 17:46:31 -070049TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010050
Jakub Czapiga084ad932021-03-25 13:10:31 +010051TEST_CFLAGS += -D__TEST__
52
Jan Dabros7f00dba2020-05-22 09:57:17 +020053TEST_CFLAGS += -I$(cmockasrc)/include
54
Jan Dabrosef1c9682020-03-28 00:15:03 +010055# Link against Cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +020056TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
57TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010058
Julius Werner84446e62021-02-12 17:37:27 -080059# Some memlayout symbols don't work with userspace relocation -- disable it.
60TEST_CFLAGS += -fno-pie -fno-pic
61TEST_LDFLAGS += -no-pie
62
Paul Fagerburgde6cbac2021-05-11 09:56:48 -060063# Enable code coverage if COV=1
64ifeq ($(COV),1)
65TEST_CFLAGS += --coverage
66TEST_LDFLAGS += --coverage
67endif
68
Jan Dabrosef1c9682020-03-28 00:15:03 +010069# Extra attributes for unit tests, declared per test
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020070attributes:= srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010071
72stages:= decompressor bootblock romstage smm verstage
73stages+= ramstage rmodule postcar libagesa
74
75alltests:=
Jakub Czapiga168b38b2021-02-11 12:59:51 +010076subdirs:= tests/arch tests/acpi tests/commonlib tests/console tests/cpu
77subdirs+= tests/device tests/drivers tests/ec tests/lib tests/mainboard
Jan Dabrosef1c9682020-03-28 00:15:03 +010078subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
79subdirs+= tests/superio tests/vendorcode
80
81define tests-handler
82alltests += $(1)$(2)
83$(foreach attribute,$(attributes),
84 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
85$(foreach attribute,$(attributes),
86 $(eval $(2)-$(attribute):=))
87
88# Sanity check for stage attribute value
89$(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
90$(if $(findstring $($(1)$(2)-stage), $(stages)),,
91 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
92 Check your $(dir $(1)$(2))Makefile.inc))
93endef
94
95$(call add-special-class, tests)
96$(call evaluate_subdirs)
97
98# Create actual targets for unit test binaries
99# $1 - test name
100define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200101
102# Generate custom config.h redefining given symbols
Jakub Czapiga203698a2021-05-25 15:56:36 +0200103$(1)-config-file := $(testobj)/$(1)/config.h
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200104$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
105 mkdir -p $$(dir $$@)
106 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
107 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
108 for kv in $$($(1)-config); do \
109 key="`echo $$$$kv | cut -d '=' -f -1`"; \
110 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
111 printf '#undef %s\n' "$$$$key" >> $$@; \
112 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
113 done
114
115$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100116 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600117$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100118 mkdir -p $$(dir $$@)
119 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
120 -MT $$@ -c $$< -o $$@
121
122$($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
Jan Dabros7f00dba2020-05-22 09:57:17 +0200123$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100124 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
125
126endef
127
128$(foreach test, $(alltests), \
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600129 $(eval $(test)-objs:=$(addprefix $(testobj)/$(test)/, \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100130 $(patsubst %.c,%.o,$($(test)-srcs)))))
131$(foreach test, $(alltests), \
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600132 $(eval $(test)-bin:=$(testobj)/$(test)/run))
Jan Dabrosef1c9682020-03-28 00:15:03 +0100133$(foreach test, $(alltests), \
134 $(eval $(call TEST_CC_template,$(test))))
135
136$(foreach test, $(alltests), \
137 $(eval all-test-objs+=$($(test)-objs)))
138$(foreach test, $(alltests), \
139 $(eval test-bins+=$($(test)-bin)))
140
141DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
142-include $(DEPENDENCIES)
143
Jan Dabros7f00dba2020-05-22 09:57:17 +0200144# Build cmocka
145$(CMOCKA_LIB):
146 echo "*** Building CMOCKA ***"
147 mkdir -p $(cmockaobj)
148 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
149 $(MAKE) -C $(cmockaobj)
150
Jan Dabrosef1c9682020-03-28 00:15:03 +0100151# Kconfig targets
152$(TEST_DOTCONFIG):
153 mkdir -p $(dir $@)
154 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
155
156# Don't override default Kconfig variables, since this will affect all
157# Kconfig targets. Change them only when calling sub-make instead.
158$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
159 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
160 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
161 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
162 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
163 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
164 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
165
166$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
167 mkdir -p $(dir $@)
168 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
169 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
170
171$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
172 true
173
174.PHONY: $(alltests) $(addprefix clean-,$(alltests))
175.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
176
Patrick Georgia0a198f2020-05-27 10:59:09 +0200177ifeq ($(JUNIT_OUTPUT),y)
178$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga1add4832021-03-05 11:37:23 +0100179$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200180endif
181
Jan Dabrosef1c9682020-03-28 00:15:03 +0100182$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200183 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
184 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100185
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600186# Build a code coverage report by collecting all the gcov files into a single
187# report. If COV is not set, this might be a user error, and they're trying
188# to generate a coverage report without first having built and run the code
189# with code coverage. So instead of silently correcting it by adding COV=1,
190# let's flag it to the user so they can be sure they're doing the thing they
191# want to do.
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600192
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600193.PHONY: coverage-report clean-coverage-report
194
195ifeq ($(COV),1)
196coverage-report:
197 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
198 genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600199 -s $(testobj)/tests.info
200
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600201clean-coverage-report:
202 rm -Rf $(testobj)/$(coverage_dir)
203else
204coverage-report:
205 COV=1 V=$(V) $(MAKE) coverage-report
206
207clean-coverage-report:
208 COV=1 V=$(V) $(MAKE) clean-coverage-report
209endif
210
Jan Dabrosef1c9682020-03-28 00:15:03 +0100211unit-tests: build-unit-tests run-unit-tests
212
213build-unit-tests: $(test-bins)
214
215run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200216 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
217 echo "**********************"; \
218 echo " TESTS FAILED"; \
219 echo "**********************"; \
220 exit 1; \
221 else \
222 echo "**********************"; \
223 echo " ALL TESTS PASSED"; \
224 echo "**********************"; \
225 exit 0; \
226 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100227
228$(addprefix clean-,$(alltests)): clean-%:
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600229 rm -rf $(testobj)/$*
Jan Dabrosef1c9682020-03-28 00:15:03 +0100230
231clean-unit-tests:
232 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200233
234list-unit-tests:
235 @echo "unit-tests:"
236 for t in $(sort $(alltests)); do \
237 echo " $$t"; \
238 done
239
240help-unit-tests help::
241 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600242 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600243 @echo ' unit-tests - Run all unit-tests from tests/'
244 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
245 @echo ' list-unit-tests - List all unit-tests'
246 @echo ' <unit-test> - Build and run single unit-test'
247 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
Paul Fagerburgde6cbac2021-05-11 09:56:48 -0600248 @echo ' coverage-report - Generate a code coverage report'
249 @echo ' clean-coverage-report - Remove the code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200250 @echo