mb/google/corsola: Remove 'corsola' from Kconfig board names
[coreboot.git] / tests / Makefile.inc
blob847beaf199b7eb2e0b1f950528c1b3c045a810cb
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
48 TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type
50 # Path for Kconfig autoheader
51 TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
53 TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
55 TEST_CFLAGS += -D__TEST__
57 TEST_CFLAGS += -I$(cmockasrc)/include
59 ifneq ($(filter-out 0,$(TEST_PRINT)),)
60 TEST_CFLAGS += -DTEST_PRINT=1
61 endif
63 # Link against Cmocka
64 TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
65 TEST_LDFLAGS += -Wl,--gc-sections
67 # Some memlayout symbols don't work with userspace relocation -- disable it.
68 TEST_CFLAGS += -fno-pie -fno-pic
69 TEST_LDFLAGS += -no-pie
71 # Enable code coverage if COV=1
72 ifeq ($(COV),1)
73 TEST_CFLAGS += --coverage
74 TEST_LDFLAGS += --coverage
75 endif
77 # Extra attributes for unit tests, declared per test
78 attributes := srcs cflags config mocks stage
80 stages := decompressor bootblock romstage smm verstage
81 stages += ramstage rmodule postcar libagesa
83 alltests :=
84 subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
85 subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
86 subdirs += tests/northbridge tests/security tests/soc tests/southbridge
87 subdirs += tests/superio tests/vendorcode
89 define tests-handler
90 alltests += $(1)$(2)
91 $(foreach attribute,$(attributes),
92         $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
93 $(foreach attribute,$(attributes),
94         $(eval $(2)-$(attribute) := ))
96 # Sanity check for stage attribute value
97 $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
98 $(if $(findstring $($(1)$(2)-stage), $(stages)),,
99         $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
100                 Check your $(dir $(1)$(2))Makefile.inc))
101 endef
103 # Copy attributes of one test to another.
104 # $1 - input test name
105 # $2 - output test name
106 copy-test = $(foreach attr,$(attributes), \
107                 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
109 $(call add-special-class, tests)
110 $(call evaluate_subdirs)
112 # Create actual targets for unit test binaries
113 # $1 - test name
114 define TEST_CC_template
116 # Generate custom config.h redefining given config symbols, and declaring mocked
117 # functions weak. It is important that the compiler already sees that they are
118 # weak (and they aren't just turned weak at a later stage) to prevent certain
119 # optimizations that would break if the function gets replaced. (For clang this
120 # file needs to be marked `system_header` to prevent it from warning about
121 # #pragma weak entries without a matching function declaration, since there's no
122 # -Wno-xxx command line option for that.)
123 $(1)-config-file := $(testobj)/$(1)/config.h
124 $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
125         mkdir -p $$(dir $$@)
126         printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
127         printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
128         for kv in $$($(1)-config); do \
129                 key="`echo $$$$kv | cut -d '=' -f -1`"; \
130                 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
131                 printf '#undef %s\n' "$$$$key" >> $$@; \
132                 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
133         done
134         printf '#ifdef __clang__\n' >> $$@;
135         printf '#pragma clang system_header\n' >> $$@;
136         printf '#endif\n' >> $$@;
137         printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
138         for m in $$($(1)-mocks); do \
139                 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
140         done
141         printf '#endif\n' >> $$@;
143 $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
144         -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
145         -D__TEST_NAME__=\"$(subst /,_,$(1))\" \
146         -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
148 # Give us a way to distinguish between coreboot source files and test files in code.
149 $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
151 # Compile sources and apply mocking/wrapping of selected symbols.
152 # For each listed mock add new symbol with prefix `__real_`,
153 # and pointing to the same section:address.
154 $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
155         mkdir -p $$(dir $$@)
156         $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
157                 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
158         objcopy_wrap_flags=''; \
159         for sym in $$($(1)-mocks); do \
160                 sym_line="$$$$($(OBJDUMP) -t $$@.orig \
161                         | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
162                 if [ ! -z "$$$$sym_line" ] ; then \
163                         addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \
164                         section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \
165                         objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
166                 fi \
167         done ; \
168         $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
170 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
171         $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
173 endef
175 $(foreach test, $(alltests), \
176         $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
177                 $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
178         $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
179                 $(patsubst %.c,%.o,$($(test)-srcs)))))
180 $(foreach test, $(alltests), \
181         $(eval $(test)-bin := $(testobj)/$(test)/run))
182 $(foreach test, $(alltests), \
183         $(eval $(call TEST_CC_template,$(test))))
185 $(foreach test, $(alltests), \
186         $(eval all-test-objs += $($(test)-objs)))
187 $(foreach test, $(alltests), \
188         $(eval test-bins += $($(test)-bin)))
190 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
191 -include $(DEPENDENCIES)
193 # Build cmocka
194 $(CMOCKA_LIB):
195         echo "*** Building CMOCKA ***"
196         mkdir -p $(cmockaobj)
197         cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
198         $(MAKE) -C $(cmockaobj)
200 # Kconfig targets
201 $(TEST_DOTCONFIG):
202         mkdir -p $(dir $@)
203         cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
205 # Don't override default Kconfig variables, since this will affect all
206 # Kconfig targets. Change them only when calling sub-make instead.
207 $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
208         KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
209         KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
210         KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
211         KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
212         KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
213         KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
215 $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
216         mkdir -p $(dir $@)
217         $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
218         $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
220 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
221         true
223 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
224 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
226 # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
227 # by macro cb_run_group_tests(), which should be used for running tests.
228 # __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
229 ifeq ($(JUNIT_OUTPUT),y)
230 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
231 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
232 endif
234 $(alltests): $$($$(@)-bin)
235         rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
236         rm -f $(testobj)/$(subst /,_,$^).failed
237         -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
239 # Build a code coverage report by collecting all the gcov files into a single
240 # report. If COV is not set, this might be a user error, and they're trying
241 # to generate a coverage report without first having built and run the code
242 # with code coverage. So instead of silently correcting it by adding COV=1,
243 # let's flag it to the user so they can be sure they're doing the thing they
244 # want to do.
246 .PHONY: coverage-report clean-coverage-report
248 ifeq ($(COV),1)
249 coverage-report:
250         lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
251         genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
252         -s $(testobj)/tests.info
254 clean-coverage-report:
255         rm -Rf $(testobj)/$(coverage_dir)
256 else
257 coverage-report:
258         COV=1 V=$(V) $(MAKE) coverage-report
260 clean-coverage-report:
261         COV=1 V=$(V) $(MAKE) clean-coverage-report
262 endif
264 unit-tests: build-unit-tests run-unit-tests
266 build-unit-tests: $(test-bins)
268 run-unit-tests: $(alltests)
269         if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
270                 echo "**********************"; \
271                 echo "     TESTS FAILED"; \
272                 echo "**********************"; \
273                 exit 1; \
274         else \
275                 echo "**********************"; \
276                 echo "   ALL TESTS PASSED"; \
277                 echo "**********************"; \
278                 exit 0; \
279         fi
281 $(addprefix clean-,$(alltests)): clean-%:
282         rm -rf $(testobj)/$*
284 clean-unit-tests:
285         rm -rf $(testobj)
287 list-unit-tests:
288         @echo "unit-tests:"
289         for t in $(sort $(alltests)); do \
290                 echo "  $$t"; \
291         done
293 help-unit-tests help::
294         @echo  '*** coreboot unit-tests targets ***'
295         @echo  '  Use "COV=1 make [target]" to enable code coverage for unit tests'
296         @echo  '  unit-tests            - Run all unit-tests from tests/'
297         @echo  '  clean-unit-tests      - Remove unit-tests build artifacts'
298         @echo  '  list-unit-tests       - List all unit-tests'
299         @echo  '  <unit-test>           - Build and run single unit-test'
300         @echo  '  clean-<unit-test>     - Remove single unit-test build artifacts'
301         @echo  '  coverage-report       - Generate a code coverage report'
302         @echo  '  clean-coverage-report - Remove the code coverage report'
303         @echo