blob: cd25e0f809989c4edeeb46db47c7e9cfbfdb7d8d [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
Jan Dabrosef1c9682020-03-28 00:15:03 +01004testobj = $(obj)/tests
Jan Dabros7f00dba2020-05-22 09:57:17 +02005cmockasrc = 3rdparty/cmocka
Patrick Georgia3402972020-05-27 10:53:01 +02006cmockaobj = $(objutil)/cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +02007
8CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
9
10CMAKE:= cmake
Jan Dabrosef1c9682020-03-28 00:15:03 +010011
12TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
13TEST_DOTCONFIG = $(testobj)/.config
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020014TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010015TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
16TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
17TEST_KCONFIG_SPLITCONFIG := $(testobj)/config
18TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
19
Julius Werner21744812020-05-04 17:44:04 -070020TEST_CFLAGS = -include $(src)/include/kconfig.h \
21 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
22 -include $(src)/include/rules.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010023
24# Include generic test mock headers, before original ones
Julius Werner21744812020-05-04 17:44:04 -070025TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
Jan Dabrosef1c9682020-03-28 00:15:03 +010026
27TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
Julius Werner1e14de82020-06-10 15:35:08 -070028 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include
29
30# Note: This is intentionally just a subset of the warnings in the toplevel
31# Makefile.inc. We don't need to be as strict with test code, and things like
32# -Wmissing-prototypes just make working with the test framework cumbersome.
33# Only put conservative warnings here that really detect code that's obviously
34# unintentional.
Julius Wernere54d7842021-02-16 19:32:24 -080035TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
Jan Dabrosef1c9682020-03-28 00:15:03 +010036
37# Path for Kconfig autoheader
38TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
39
Julius Werner25400372020-05-04 17:46:31 -070040TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010041
Jakub Czapiga084ad932021-03-25 13:10:31 +010042TEST_CFLAGS += -D__TEST__
43
Jan Dabros7f00dba2020-05-22 09:57:17 +020044TEST_CFLAGS += -I$(cmockasrc)/include
45
Jan Dabrosef1c9682020-03-28 00:15:03 +010046# Link against Cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +020047TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
48TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010049
Julius Werner84446e62021-02-12 17:37:27 -080050# Some memlayout symbols don't work with userspace relocation -- disable it.
51TEST_CFLAGS += -fno-pie -fno-pic
52TEST_LDFLAGS += -no-pie
53
Jan Dabrosef1c9682020-03-28 00:15:03 +010054# Extra attributes for unit tests, declared per test
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020055attributes:= srcs cflags config mocks stage
Jan Dabrosef1c9682020-03-28 00:15:03 +010056
57stages:= decompressor bootblock romstage smm verstage
58stages+= ramstage rmodule postcar libagesa
59
60alltests:=
Jakub Czapiga168b38b2021-02-11 12:59:51 +010061subdirs:= tests/arch tests/acpi tests/commonlib tests/console tests/cpu
62subdirs+= tests/device tests/drivers tests/ec tests/lib tests/mainboard
Jan Dabrosef1c9682020-03-28 00:15:03 +010063subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
64subdirs+= tests/superio tests/vendorcode
65
66define tests-handler
67alltests += $(1)$(2)
68$(foreach attribute,$(attributes),
69 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
70$(foreach attribute,$(attributes),
71 $(eval $(2)-$(attribute):=))
72
73# Sanity check for stage attribute value
74$(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
75$(if $(findstring $($(1)$(2)-stage), $(stages)),,
76 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
77 Check your $(dir $(1)$(2))Makefile.inc))
78endef
79
80$(call add-special-class, tests)
81$(call evaluate_subdirs)
82
83# Create actual targets for unit test binaries
84# $1 - test name
85define TEST_CC_template
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +020086
87# Generate custom config.h redefining given symbols
88$(1)-config-file := $(obj)/$(1)/config.h
89$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
90 mkdir -p $$(dir $$@)
91 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
92 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
93 for kv in $$($(1)-config); do \
94 key="`echo $$$$kv | cut -d '=' -f -1`"; \
95 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
96 printf '#undef %s\n' "$$$$key" >> $$@; \
97 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
98 done
99
100$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
Jan Dabrosef1c9682020-03-28 00:15:03 +0100101 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
Jakub Czapiga5c3b05e2021-04-28 16:50:51 +0200102$($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100103 mkdir -p $$(dir $$@)
104 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
105 -MT $$@ -c $$< -o $$@
106
107$($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
Jan Dabros7f00dba2020-05-22 09:57:17 +0200108$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +0100109 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
110
111endef
112
113$(foreach test, $(alltests), \
114 $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \
115 $(patsubst %.c,%.o,$($(test)-srcs)))))
116$(foreach test, $(alltests), \
117 $(eval $(test)-bin:=$(obj)/$(test)/run))
118$(foreach test, $(alltests), \
119 $(eval $(call TEST_CC_template,$(test))))
120
121$(foreach test, $(alltests), \
122 $(eval all-test-objs+=$($(test)-objs)))
123$(foreach test, $(alltests), \
124 $(eval test-bins+=$($(test)-bin)))
125
126DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
127-include $(DEPENDENCIES)
128
Jan Dabros7f00dba2020-05-22 09:57:17 +0200129# Build cmocka
130$(CMOCKA_LIB):
131 echo "*** Building CMOCKA ***"
132 mkdir -p $(cmockaobj)
133 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
134 $(MAKE) -C $(cmockaobj)
135
Jan Dabrosef1c9682020-03-28 00:15:03 +0100136# Kconfig targets
137$(TEST_DOTCONFIG):
138 mkdir -p $(dir $@)
139 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
140
141# Don't override default Kconfig variables, since this will affect all
142# Kconfig targets. Change them only when calling sub-make instead.
143$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
144 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
145 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
146 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
147 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
148 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
149 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
150
151$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
152 mkdir -p $(dir $@)
153 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
154 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
155
156$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
157 true
158
159.PHONY: $(alltests) $(addprefix clean-,$(alltests))
160.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
161
Patrick Georgia0a198f2020-05-27 10:59:09 +0200162ifeq ($(JUNIT_OUTPUT),y)
163$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga1add4832021-03-05 11:37:23 +0100164$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200165endif
166
Jan Dabrosef1c9682020-03-28 00:15:03 +0100167$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200168 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
169 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100170
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600171.PHONY: coverage-unit-tests
172
173coverage-unit-tests: TEST_CFLAGS += --coverage
174coverage-unit-tests: TEST_LDFLAGS += --coverage
175coverage-unit-tests: clean-unit-tests unit-tests
176 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '*/$(testobj)/*'
177 genhtml -q -o build/tests/coverage_rpt -t "coreboot unit tests" \
178 -s $(testobj)/tests.info
179
Jan Dabrosef1c9682020-03-28 00:15:03 +0100180unit-tests: build-unit-tests run-unit-tests
181
182build-unit-tests: $(test-bins)
183
184run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200185 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
186 echo "**********************"; \
187 echo " TESTS FAILED"; \
188 echo "**********************"; \
189 exit 1; \
190 else \
191 echo "**********************"; \
192 echo " ALL TESTS PASSED"; \
193 echo "**********************"; \
194 exit 0; \
195 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100196
197$(addprefix clean-,$(alltests)): clean-%:
198 rm -rf $(obj)/$*
199
200clean-unit-tests:
201 rm -rf $(testobj)
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200202
203list-unit-tests:
204 @echo "unit-tests:"
205 for t in $(sort $(alltests)); do \
206 echo " $$t"; \
207 done
208
209help-unit-tests help::
210 @echo '*** coreboot unit-tests targets ***'
Paul Fagerburg045fbf12021-04-16 11:36:25 -0600211 @echo ' unit-tests - Run all unit-tests from tests/'
212 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
213 @echo ' list-unit-tests - List all unit-tests'
214 @echo ' <unit-test> - Build and run single unit-test'
215 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
216 @echo ' coverage-unit-tests - Build unit tests for code coverage and'
217 @echo ' generate a code coverage report'
Jakub Czapigaa4819b32021-04-13 16:07:05 +0200218 @echo