mb/google/brya/var/vell: Generate LP5 RAM ID
[coreboot.git] / tests / Makefile.inc
blob7a7ab1d03723f909276a4fe8b02e65ce07bac611
1 # SPDX-License-Identifier: GPL-2.0-only
3 testsrc := $(top)/tests
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.
7 ifeq ($(COV),1)
8 testobj := $(obj)/coverage
9 else
10 testobj := $(obj)/tests
11 endif
13 cmockasrc := 3rdparty/cmocka
14 cmockaobj := $(objutil)/cmocka
15 coverage_dir := coverage_reports
17 CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
19 CMAKE := cmake
20 OBJCOPY ?= objcopy
21 OBJDUMP ?= objdump
23 TEST_DEFAULT_CONFIG := $(top)/configs/config.emulation_qemu_x86_i440fx
24 TEST_DOTCONFIG := $(testobj)/.config
25 TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
26 TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
27 TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
28 TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/
29 TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
31 TEST_CFLAGS := -include $(src)/include/kconfig.h \
32         -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
33         -include $(src)/include/rules.h
35 # Include generic test mock headers, before original ones
36 TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
38 TEST_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
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.
47 TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
49 # Path for Kconfig autoheader
50 TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
52 TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
54 TEST_CFLAGS += -D__TEST__
56 TEST_CFLAGS += -I$(cmockasrc)/include
58 ifneq ($(filter-out 0,$(TEST_PRINT)),)
59 TEST_CFLAGS += -DTEST_PRINT=1
60 endif
62 # Link against Cmocka
63 TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
64 TEST_LDFLAGS += -Wl,--gc-sections
66 # Some memlayout symbols don't work with userspace relocation -- disable it.
67 TEST_CFLAGS += -fno-pie -fno-pic
68 TEST_LDFLAGS += -no-pie
70 # Enable code coverage if COV=1
71 ifeq ($(COV),1)
72 TEST_CFLAGS += --coverage
73 TEST_LDFLAGS += --coverage
74 endif
76 # Extra attributes for unit tests, declared per test
77 attributes := srcs cflags config mocks stage
79 stages := decompressor bootblock romstage smm verstage
80 stages += ramstage rmodule postcar libagesa
82 alltests :=
83 subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
84 subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
85 subdirs += tests/northbridge tests/security tests/soc tests/southbridge
86 subdirs += tests/superio tests/vendorcode
88 define tests-handler
89 alltests += $(1)$(2)
90 $(foreach attribute,$(attributes),
91         $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
92 $(foreach attribute,$(attributes),
93         $(eval $(2)-$(attribute) := ))
95 # Sanity check for stage attribute value
96 $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
97 $(if $(findstring $($(1)$(2)-stage), $(stages)),,
98         $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
99                 Check your $(dir $(1)$(2))Makefile.inc))
100 endef
102 # Copy attributes of one test to another.
103 # $1 - input test name
104 # $2 - output test name
105 copy-test = $(foreach attr,$(attributes), \
106                 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
108 $(call add-special-class, tests)
109 $(call evaluate_subdirs)
111 # Create actual targets for unit test binaries
112 # $1 - test name
113 define TEST_CC_template
115 # Generate custom config.h redefining given config symbols, and declaring mocked
116 # functions weak. It is important that the compiler already sees that they are
117 # weak (and they aren't just turned weak at a later stage) to prevent certain
118 # optimizations that would break if the function gets replaced. (For clang this
119 # file needs to be marked `system_header` to prevent it from warning about
120 # #pragma weak entries without a matching function declaration, since there's no
121 # -Wno-xxx command line option for that.)
122 $(1)-config-file := $(testobj)/$(1)/config.h
123 $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
124         mkdir -p $$(dir $$@)
125         printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
126         printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
127         for kv in $$($(1)-config); do \
128                 key="`echo $$$$kv | cut -d '=' -f -1`"; \
129                 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
130                 printf '#undef %s\n' "$$$$key" >> $$@; \
131                 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
132         done
133         printf '#ifdef __clang__\n' >> $$@;
134         printf '#pragma clang system_header\n' >> $$@;
135         printf '#endif\n' >> $$@;
136         printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
137         for m in $$($(1)-mocks); do \
138                 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
139         done
140         printf '#endif\n' >> $$@;
142 $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
143         -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
144         -D__TEST_NAME__=\"$(subst /,_,$(1))\" \
145         -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
147 # Give us a way to distinguish between coreboot source files and test files in code.
148 $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
150 # Compile sources and apply mocking/wrapping of selected symbols.
151 # For each listed mock add new symbol with prefix `__real_`,
152 # and pointing to the same section:address.
153 $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
154         mkdir -p $$(dir $$@)
155         $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
156                 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
157         objcopy_wrap_flags=''; \
158         for sym in $$($(1)-mocks); do \
159                 sym_line="$$$$($(OBJDUMP) -t $$@.orig | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
160                 if [ ! -z "$$$$sym_line" ] ; then \
161                         addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
162                         section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
163                         objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
164                 fi \
165         done ; \
166         $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
168 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
169         $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
171 endef
173 $(foreach test, $(alltests), \
174         $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
175                 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
176         $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
177                 $(patsubst %.c,%.o,$($(test)-srcs)))))
178 $(foreach test, $(alltests), \
179         $(eval $(test)-bin := $(testobj)/$(test)/run))
180 $(foreach test, $(alltests), \
181         $(eval $(call TEST_CC_template,$(test))))
183 $(foreach test, $(alltests), \
184         $(eval all-test-objs += $($(test)-objs)))
185 $(foreach test, $(alltests), \
186         $(eval test-bins += $($(test)-bin)))
188 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
189 -include $(DEPENDENCIES)
191 # Build cmocka
192 $(CMOCKA_LIB):
193         echo "*** Building CMOCKA ***"
194         mkdir -p $(cmockaobj)
195         cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
196         $(MAKE) -C $(cmockaobj)
198 # Kconfig targets
199 $(TEST_DOTCONFIG):
200         mkdir -p $(dir $@)
201         cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
203 # Don't override default Kconfig variables, since this will affect all
204 # Kconfig targets. Change them only when calling sub-make instead.
205 $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
206         KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
207         KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
208         KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
209         KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
210         KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
211         KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
213 $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
214         mkdir -p $(dir $@)
215         $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
216         $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
218 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
219         true
221 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
222 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
224 # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
225 # by macro cb_run_group_tests(), which should be used for running tests.
226 # __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
227 ifeq ($(JUNIT_OUTPUT),y)
228 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
229 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
230 endif
232 $(alltests): $$($$(@)-bin)
233         rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
234         rm -f $(testobj)/$(subst /,_,$^).failed
235         -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
237 # Build a code coverage report by collecting all the gcov files into a single
238 # report. If COV is not set, this might be a user error, and they're trying
239 # to generate a coverage report without first having built and run the code
240 # with code coverage. So instead of silently correcting it by adding COV=1,
241 # let's flag it to the user so they can be sure they're doing the thing they
242 # want to do.
244 .PHONY: coverage-report clean-coverage-report
246 ifeq ($(COV),1)
247 coverage-report:
248         lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
249         genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
250         -s $(testobj)/tests.info
252 clean-coverage-report:
253         rm -Rf $(testobj)/$(coverage_dir)
254 else
255 coverage-report:
256         COV=1 V=$(V) $(MAKE) coverage-report
258 clean-coverage-report:
259         COV=1 V=$(V) $(MAKE) clean-coverage-report
260 endif
262 unit-tests: build-unit-tests run-unit-tests
264 build-unit-tests: $(test-bins)
266 run-unit-tests: $(alltests)
267         if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
268                 echo "**********************"; \
269                 echo "     TESTS FAILED"; \
270                 echo "**********************"; \
271                 exit 1; \
272         else \
273                 echo "**********************"; \
274                 echo "   ALL TESTS PASSED"; \
275                 echo "**********************"; \
276                 exit 0; \
277         fi
279 $(addprefix clean-,$(alltests)): clean-%:
280         rm -rf $(testobj)/$*
282 clean-unit-tests:
283         rm -rf $(testobj)
285 list-unit-tests:
286         @echo "unit-tests:"
287         for t in $(sort $(alltests)); do \
288                 echo "  $$t"; \
289         done
291 help-unit-tests help::
292         @echo  '*** coreboot unit-tests targets ***'
293         @echo  '  Use "COV=1 make [target]" to enable code coverage for unit tests'
294         @echo  '  unit-tests            - Run all unit-tests from tests/'
295         @echo  '  clean-unit-tests      - Remove unit-tests build artifacts'
296         @echo  '  list-unit-tests       - List all unit-tests'
297         @echo  '  <unit-test>           - Build and run single unit-test'
298         @echo  '  clean-<unit-test>     - Remove single unit-test build artifacts'
299         @echo  '  coverage-report       - Generate a code coverage report'
300         @echo  '  clean-coverage-report - Remove the code coverage report'
301         @echo