soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / tests / Makefile.mk
blobe271a7cea8951ed07640c67d31a74e7b30178f5b
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
12 coverage-dir := $(testobj)/coverage_reports
14 cmockasrc := $(coreboottop)/3rdparty/cmocka
15 cmockaobj := $(objutil)/cmocka
16 CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
18 CMAKE := cmake
20 TEST_DEFAULT_CONFIG := $(top)/configs/config.unit-tests
21 TEST_DOTCONFIG := $(testobj)/.config
22 TEST_KCONFIG_AUTOHEADER := $(testobj)/libpayload-config.src.h
23 TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
24 TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
25 TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/
26 TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
27 TEST_KCONFIG_NEGATIVES := 1
28 TEST_KBUILD_KCONFIG := $(top)/Kconfig
29 TEST_CONFIG_ := CONFIG_LP_
32 # Default includes
33 TEST_CFLAGS := -include include/kconfig.h
34 TEST_CFLAGS += -include $(coreboottop)/src/commonlib/bsd/include/commonlib/bsd/compiler.h
35 TEST_CFLAGS += -Iinclude -Iinclude/mock
36 TEST_CFLAGS += -I$(coreboottop)/src/commonlib/bsd/include
37 TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
38 TEST_CFLAGS += -I$(VBOOT_SOURCE)/firmware/include
40 # Test specific includes
41 TEST_CFLAGS += -I$(testsrc)/include
42 TEST_CFLAGS += -I$(cmockasrc)/include
44 # Minimal subset of warnings and errors. Tests can be less strict than actual build.
45 TEST_CFLAGS += -Wall -Wundef -Wstrict-prototypes -Wvla
46 TEST_CFLAGS += -Wwrite-strings -Wno-trigraphs -Wimplicit-fallthrough
47 TEST_CFLAGS += -Wstrict-aliasing -Wshadow -Werror
48 TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type
50 TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
52 # Make unit-tests detectable by the code
53 TEST_CFLAGS += -D__TEST__
55 # Link against CMocka
56 TEST_LDFLAGS := -L$(dir $(CMOCKA_LIB)) -lcmocka -Wl,-rpath=$(dir $(CMOCKA_LIB))
58 TEST_LDFLAGS += -Wl,--gc-sections
60 # Disable userspace relocations
61 TEST_CFLAGS += -fno-pie -fno-pic
62 TEST_LDFLAGS += -no-pie
64 ifeq ($(COV),1)
65 TEST_CFLAGS += --coverage
66 TEST_LDFLAGS += --coverage
67 endif
70 # Extra attributes for unit tests. Declated per each test. Only `srcs` is required.
71 attributes := cflags config mocks srcs
73 alltests :=
74 subdirs := tests/crypto tests/curses tests/drivers tests/gdb tests/libc tests/libcbfs
75 subdirs += tests/liblz4 tests/liblzma tests/libpci
77 define tests-handler
78 alltests += $(1)$(2)
79 $(foreach attribute,$(attributes), \
80 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
81 $(foreach attribute,$(attributes), \
82 $(eval $(2)-$(attribute) := ))
83 endef
85 # Copy attributes of one test to another
86 # $1 - input test name
87 # $2 - output test name
88 copy-test = $(foreach attribute,$(attributes), \
89 $(eval $(strip $(2))-$(attribute) := $($(strip $(1))-$(attribute))))
91 $(call add-special-class,tests)
92 $(call evaluate_subdirs)
94 # Create actual targets for unit test binaries
95 # $1 - test name
96 define TEST_CC_template
98 # Generate custom config.h redefining given config symbols, and declaring mocked
99 # functions weak. It is important that the compiler already sees that they are
100 # weak (and they aren't just turned weak at a later stage) to prevent certain
101 # optimizations that would break if the function gets replaced. (For clang this
102 # file needs to be marked `system_header` to prevent it from warning about
103 # `#pragma weak` entries without a matching function declaration, since there is
104 # no -Wno-xxx commandline for that.)
105 $(1)-config-file := $(testobj)/$(1)/libpayload-config.h
106 $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
107 mkdir -p $$(dir $$@);
108 printf '// File generated by tests/Makefile.mk\n// Do not change\n' > $$@;
109 printf '#ifndef TEST_LIBPAYLOAD_CONFIG_H_\n' >> $$@;
110 printf '#define TEST_LIBPAYLOAD_CONFIG_H_\n' >> $$@;
111 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@;
112 for kv in $$($(1)-config); do \
113 key="`echo $$$$kv | cut -d '=' -f -1`"; \
114 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
115 printf '#undef %s\n' "$$$$key" >> $$@; \
116 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
117 done
118 printf '#ifdef __clang__\n' >> $$@;
119 printf '#pragma clang system_header\n' >> $$@;
120 printf '#endif\n\n' >> $$@;
121 printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
122 for m in $$($(1)-mocks); do \
123 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
124 done
125 printf '#endif\n\n' >> $$@;
126 printf '#endif\n' >> $$@;
128 $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
129 -D__TEST_NAME__=\"$(subst /,_,$(1))\"
131 # Give us a way to distinguish between libpayload source files and test files in the code.
132 $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
134 # Compile sources and apply mocking/wrapping for selected symbols.
135 # For each listed mock add new symbol with prefix `__real_`,
136 # pointing to the same section:address. This will keep original
137 # function accessible if required.
138 $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
139 mkdir -p $$(dir $$@)
140 $(HOSTCC) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
141 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
142 objcopy_wrap_flags=''; \
143 for sym in $$($(1)-mocks); do \
144 sym_line="$$$$($(HOSTOBJDUMP) -t $$@.orig \
145 | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s+$$$$sym$$$$")"; \
146 if [ ! -z "$$$$sym_line" ] ; then \
147 addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \
148 section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \
149 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
150 fi \
151 done ; \
152 $(HOSTOBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
154 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
155 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
157 endef
159 $(foreach test,$(alltests), \
160 $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
161 $(patsubst %.c,%.o,$(filter-out tests/%,$($(test)-srcs))))) \
162 $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
163 $(patsubst %.c,%.o,$($(test)-srcs)))) \
164 $(eval $(test)-bin := $(testobj)/$(test)/run))
165 $(foreach test,$(alltests), \
166 $(eval $(call TEST_CC_template,$(test))))
167 $(foreach test,$(alltests), \
168 $(eval all-test-objs += $($(test)-objs)) \
169 $(eval test-bins += $($(test)-bin)))
171 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
172 -include $(DEPENDENCIES)
174 # Build CMocka
175 $(CMOCKA_LIB):
176 echo "*** Building CMOCKA ***"
177 mkdir -p $(cmockaobj)
178 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
179 $(MAKE) -C $(cmockaobj)
181 # Kconfig targets
182 $(TEST_DOTCONFIG):
183 mkdir -p $(dir $@)
184 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
186 $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
187 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
188 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
189 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
190 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
191 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
192 KCONFIG_NEGATIVES=$(TEST_KCONFIG_NEGATIVES) \
193 KBUILD_KCONFIG=$(TEST_KBUILD_KCONFIG) \
194 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG) \
195 CONFIG_=$(TEST_CONFIG_)
197 $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objk)/conf
198 mkdir -p $(dir $@)
199 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig V=$(V)
200 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig V=$(V)
202 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
203 true
205 .PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests))
206 .PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests))
207 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
208 .PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests
210 ifeq ($(JUNIT_OUTPUT),y)
211 $(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml
212 $(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-libpayload-%g.xml
213 endif
215 $(addprefix run-,$(alltests)): run-%: $$(%-bin)
216 rm -f $(testobj)/junit-libpayload-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
217 rm -f $(testobj)/$(subst /,_,$^).failed
218 -$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
220 $(addprefix build-,$(alltests)): build-%: $$(%-bin)
222 $(alltests): run-$$(@)
224 $(addprefix try-,$(alltests)): try-%: clean-% $(CMOCKA_LIB) $(TEST_KCONFIG_AUTOCONFIG)
225 mkdir -p $(testobj)/$*
226 echo "<testcase classname='libpayload_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \
227 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \
228 && type="system-out" || type="failure"; \
229 if [ $$type = "failure" ]; then \
230 echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \
231 else \
232 echo "<$$type>" >> $(testobj)/$*.tmp; \
233 fi; \
234 echo '<![CDATA[' >> $(testobj)/$*.tmp; \
235 cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \
236 echo "]]></$$type>" >> $(testobj)/$*.tmp; \
237 rm -f $(testobj)/$*.tmp.2; \
238 echo "</testcase>" >> $(testobj)/$*.tmp; \
239 if [ $$type != 'failure' ]; then \
240 $(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \
244 TESTS_BUILD_XML_FILE := $(testobj)/junit-libpayload-tests-build.xml
246 $(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests))
247 mkdir -p $(dir $@)
248 echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@
249 for tst in $(alltests); do \
250 cat $(testobj)/$$tst.tmp >> $@; \
251 done
252 echo "</testsuite>" >> $@
254 junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE)
256 clean-junit.xml-unit-tests:
257 rm -f $(TESTS_BUILD_XML_FILE)
260 # Build a code coverage report by collecting all the gcov files into a single
261 # report. If COV is not set, this might be a user error, and they're trying
262 # to generate a coverage report without first having built and run the code
263 # with code coverage. absence of COV=1 will be corrected.
265 .PHONY: coverage-report clean-coverage-report
267 ifeq ($(COV),1)
268 coverage-report:
269 lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
270 genhtml -q -o $(coverage-dir) -t "coreboot unit tests" -s $(testobj)/tests.info
272 clean-coverage-report:
273 rm -Rf $(coverage-dir)
274 else
275 coverage-report:
276 COV=1 V=$(V) $(MAKE) coverage-report
278 clean-coverage-report:
279 COV=1 V=$(V) $(MAKE) clean-coverage-report
280 endif
282 unit-tests: build-unit-tests run-unit-tests
284 build-unit-tests: $(test-bins)
286 run-unit-tests: $(alltests)
287 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
288 echo "**********************"; \
289 echo " TESTS FAILED"; \
290 echo "**********************"; \
291 exit 1; \
292 else \
293 echo "**********************"; \
294 echo " ALL TESTS PASSED"; \
295 echo "**********************"; \
296 exit 0; \
299 $(addprefix clean-,$(alltests)): clean-%:
300 rm -rf $(testobj)/$*
302 clean-unit-tests:
303 rm -rf $(testobj)
305 list-unit-tests:
306 @echo "unit-tests:"
307 for t in $(sort $(alltests)); do \
308 echo " $$t"; \
309 done
311 help-unit-tests help::
312 @echo '*** libpayload unit-tests targets ***'
313 @echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
314 @echo ' unit-tests - Run all unit-tests from tests/'
315 @echo ' clean-unit-tests - Remove unit-tests build artifacts'
316 @echo ' list-unit-tests - List all unit-tests'
317 @echo ' <unit-test> - Build and run single unit-test'
318 @echo ' clean-<unit-test> - Remove single unit-test build artifacts'
319 @echo ' coverage-report - Generate a code coverage report'
320 @echo ' clean-coverage-report - Remove the code coverage report'
321 @echo