2020-03-28 00:15:03 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
testsrc := $(top)/tests
|
2021-05-11 17:56:48 +02:00
|
|
|
|
|
|
|
# Place the build output in one of two places depending on COV, so that code
|
|
|
|
# built with code coverage never mixes with code built without code coverage.
|
|
|
|
ifeq ($(COV),1)
|
2021-08-09 14:20:28 +02:00
|
|
|
testobj := $(obj)/coverage
|
2021-05-11 17:56:48 +02:00
|
|
|
else
|
2021-08-09 14:20:28 +02:00
|
|
|
testobj := $(obj)/tests
|
2021-05-11 17:56:48 +02:00
|
|
|
endif
|
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
cmockasrc := 3rdparty/cmocka
|
|
|
|
cmockaobj := $(objutil)/cmocka
|
|
|
|
coverage_dir := coverage_reports
|
2020-05-22 09:57:17 +02:00
|
|
|
|
|
|
|
CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
|
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
CMAKE := cmake
|
|
|
|
OBJCOPY ?= objcopy
|
|
|
|
OBJDUMP ?= objdump
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
TEST_DEFAULT_CONFIG := $(top)/configs/config.emulation_qemu_x86_i440fx
|
|
|
|
TEST_DOTCONFIG := $(testobj)/.config
|
2021-04-28 16:50:51 +02:00
|
|
|
TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
|
2020-03-28 00:15:03 +01:00
|
|
|
TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
|
|
|
|
TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
|
2021-07-15 12:54:27 +02:00
|
|
|
TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/
|
2020-03-28 00:15:03 +01:00
|
|
|
TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
|
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
TEST_CFLAGS := -include $(src)/include/kconfig.h \
|
2020-05-05 02:44:04 +02:00
|
|
|
-include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
|
|
|
|
-include $(src)/include/rules.h
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
# Include generic test mock headers, before original ones
|
2020-05-05 02:44:04 +02:00
|
|
|
TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-08-09 15:51:37 +02:00
|
|
|
TEST_CFLAGS += -I$(src) -I$(src)/include -I$(src)/commonlib/include \
|
|
|
|
-I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \
|
|
|
|
-I$(top)/3rdparty/vboot/firmware/include
|
2020-06-11 00:35:08 +02:00
|
|
|
|
|
|
|
# Note: This is intentionally just a subset of the warnings in the toplevel
|
|
|
|
# Makefile.inc. We don't need to be as strict with test code, and things like
|
|
|
|
# -Wmissing-prototypes just make working with the test framework cumbersome.
|
|
|
|
# Only put conservative warnings here that really detect code that's obviously
|
|
|
|
# unintentional.
|
2021-02-17 04:32:24 +01:00
|
|
|
TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
# Path for Kconfig autoheader
|
|
|
|
TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
|
|
|
|
|
2020-05-05 02:46:31 +02:00
|
|
|
TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-03-25 13:10:31 +01:00
|
|
|
TEST_CFLAGS += -D__TEST__
|
|
|
|
|
2020-05-22 09:57:17 +02:00
|
|
|
TEST_CFLAGS += -I$(cmockasrc)/include
|
|
|
|
|
2021-09-09 12:11:39 +02:00
|
|
|
ifneq ($(filter-out 0,$(TEST_PRINT)),)
|
|
|
|
TEST_CFLAGS += -DTEST_PRINT=1
|
|
|
|
endif
|
|
|
|
|
2020-03-28 00:15:03 +01:00
|
|
|
# Link against Cmocka
|
2021-08-09 14:20:28 +02:00
|
|
|
TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
|
2020-05-22 09:57:17 +02:00
|
|
|
TEST_LDFLAGS += -Wl,--gc-sections
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-02-13 02:37:27 +01:00
|
|
|
# Some memlayout symbols don't work with userspace relocation -- disable it.
|
|
|
|
TEST_CFLAGS += -fno-pie -fno-pic
|
|
|
|
TEST_LDFLAGS += -no-pie
|
|
|
|
|
2021-05-11 17:56:48 +02:00
|
|
|
# Enable code coverage if COV=1
|
|
|
|
ifeq ($(COV),1)
|
|
|
|
TEST_CFLAGS += --coverage
|
|
|
|
TEST_LDFLAGS += --coverage
|
|
|
|
endif
|
|
|
|
|
2020-03-28 00:15:03 +01:00
|
|
|
# Extra attributes for unit tests, declared per test
|
2021-08-09 14:20:28 +02:00
|
|
|
attributes := srcs cflags config mocks stage
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
stages := decompressor bootblock romstage smm verstage
|
|
|
|
stages += ramstage rmodule postcar libagesa
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-08-09 14:20:28 +02:00
|
|
|
alltests :=
|
|
|
|
subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
|
|
|
|
subdirs += tests/device tests/drivers tests/ec tests/lib tests/mainboard
|
|
|
|
subdirs += tests/northbridge tests/security tests/soc tests/southbridge
|
|
|
|
subdirs += tests/superio tests/vendorcode
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
define tests-handler
|
|
|
|
alltests += $(1)$(2)
|
|
|
|
$(foreach attribute,$(attributes),
|
|
|
|
$(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
|
|
|
|
$(foreach attribute,$(attributes),
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval $(2)-$(attribute) := ))
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
# Sanity check for stage attribute value
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
|
2020-03-28 00:15:03 +01:00
|
|
|
$(if $(findstring $($(1)$(2)-stage), $(stages)),,
|
|
|
|
$(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
|
|
|
|
Check your $(dir $(1)$(2))Makefile.inc))
|
|
|
|
endef
|
|
|
|
|
2021-08-09 15:00:43 +02:00
|
|
|
# Copy attributes of one test to another.
|
|
|
|
# $1 - input test name
|
|
|
|
# $2 - output test name
|
|
|
|
copy-test = $(foreach attr,$(attributes), \
|
|
|
|
$(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
|
|
|
|
|
2020-03-28 00:15:03 +01:00
|
|
|
$(call add-special-class, tests)
|
|
|
|
$(call evaluate_subdirs)
|
|
|
|
|
|
|
|
# Create actual targets for unit test binaries
|
|
|
|
# $1 - test name
|
|
|
|
define TEST_CC_template
|
2021-04-28 16:50:51 +02:00
|
|
|
|
2021-08-18 04:50:22 +02:00
|
|
|
# Generate custom config.h redefining given config symbols, and declaring mocked
|
|
|
|
# functions weak. It is important that the compiler already sees that they are
|
|
|
|
# weak (and they aren't just turned weak at a later stage) to prevent certain
|
|
|
|
# optimizations that would break if the function gets replaced. (For clang this
|
|
|
|
# file needs to be marked `system_header` to prevent it from warning about
|
|
|
|
# #pragma weak entries without a matching function declaration, since there's no
|
|
|
|
# -Wno-xxx command line option for that.)
|
2021-05-25 15:56:36 +02:00
|
|
|
$(1)-config-file := $(testobj)/$(1)/config.h
|
2021-04-28 16:50:51 +02:00
|
|
|
$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
|
|
|
|
mkdir -p $$(dir $$@)
|
|
|
|
printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
|
|
|
|
printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
|
|
|
|
for kv in $$($(1)-config); do \
|
|
|
|
key="`echo $$$$kv | cut -d '=' -f -1`"; \
|
|
|
|
value="`echo $$$$kv | cut -d '=' -f 2-`"; \
|
|
|
|
printf '#undef %s\n' "$$$$key" >> $$@; \
|
|
|
|
printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
|
|
|
|
done
|
2021-08-18 04:50:22 +02:00
|
|
|
printf '#ifdef __clang__\n' >> $$@;
|
|
|
|
printf '#pragma clang system_header\n' >> $$@;
|
|
|
|
printf '#endif\n' >> $$@;
|
|
|
|
printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
|
|
|
|
for m in $$($(1)-mocks); do \
|
|
|
|
printf '#pragma weak %s\n' "$$$$m" >> $$@; \
|
|
|
|
done
|
|
|
|
printf '#endif\n' >> $$@;
|
2021-04-28 16:50:51 +02:00
|
|
|
|
|
|
|
$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
|
2021-08-25 16:27:35 +02:00
|
|
|
-D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
|
2021-07-23 13:35:14 +02:00
|
|
|
-D__TEST_NAME__=\"$(subst /,_,$(1))\" \
|
|
|
|
-D__TEST_DATA_DIR__=\"$(testsrc)/data\"
|
2021-07-29 13:27:54 +02:00
|
|
|
|
2021-08-18 04:50:22 +02:00
|
|
|
# Give us a way to distinguish between coreboot source files and test files in code.
|
|
|
|
$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
|
2021-07-29 13:27:54 +02:00
|
|
|
|
|
|
|
# Compile sources and apply mocking/wrapping of selected symbols.
|
|
|
|
# For each listed mock add new symbol with prefix `__real_`,
|
|
|
|
# and pointing to the same section:address.
|
2021-05-11 17:56:48 +02:00
|
|
|
$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
|
2020-03-28 00:15:03 +01:00
|
|
|
mkdir -p $$(dir $$@)
|
2021-07-20 08:32:40 +02:00
|
|
|
$(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
|
|
|
|
-MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
|
2021-07-29 13:27:54 +02:00
|
|
|
objcopy_wrap_flags=''; \
|
|
|
|
for sym in $$($(1)-mocks); do \
|
2021-08-18 04:50:22 +02:00
|
|
|
sym_line="$$$$($(OBJDUMP) -t $$@.orig | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
|
2021-07-29 13:27:54 +02:00
|
|
|
if [ ! -z "$$$$sym_line" ] ; then \
|
|
|
|
addr="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$1 }')"; \
|
|
|
|
section="$$$$(echo \"$$$$sym_line\" | awk '{ print $$$$(NF - 2) }')"; \
|
|
|
|
objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
|
|
|
|
fi \
|
|
|
|
done ; \
|
2021-08-18 04:50:22 +02:00
|
|
|
$(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2020-05-22 09:57:17 +02:00
|
|
|
$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
|
2020-03-28 00:15:03 +01:00
|
|
|
$(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
|
|
|
|
|
|
|
|
endef
|
|
|
|
|
|
|
|
$(foreach test, $(alltests), \
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
|
2021-06-09 18:37:42 +02:00
|
|
|
$(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
|
2020-03-28 00:15:03 +01:00
|
|
|
$(patsubst %.c,%.o,$($(test)-srcs)))))
|
|
|
|
$(foreach test, $(alltests), \
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval $(test)-bin := $(testobj)/$(test)/run))
|
2020-03-28 00:15:03 +01:00
|
|
|
$(foreach test, $(alltests), \
|
|
|
|
$(eval $(call TEST_CC_template,$(test))))
|
|
|
|
|
|
|
|
$(foreach test, $(alltests), \
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval all-test-objs += $($(test)-objs)))
|
2020-03-28 00:15:03 +01:00
|
|
|
$(foreach test, $(alltests), \
|
2021-08-09 14:20:28 +02:00
|
|
|
$(eval test-bins += $($(test)-bin)))
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
|
|
|
|
-include $(DEPENDENCIES)
|
|
|
|
|
2020-05-22 09:57:17 +02:00
|
|
|
# Build cmocka
|
|
|
|
$(CMOCKA_LIB):
|
|
|
|
echo "*** Building CMOCKA ***"
|
|
|
|
mkdir -p $(cmockaobj)
|
|
|
|
cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
|
|
|
|
$(MAKE) -C $(cmockaobj)
|
|
|
|
|
2020-03-28 00:15:03 +01:00
|
|
|
# Kconfig targets
|
|
|
|
$(TEST_DOTCONFIG):
|
|
|
|
mkdir -p $(dir $@)
|
|
|
|
cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
|
|
|
|
|
|
|
|
# Don't override default Kconfig variables, since this will affect all
|
|
|
|
# Kconfig targets. Change them only when calling sub-make instead.
|
2021-08-09 14:20:28 +02:00
|
|
|
$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
|
2020-03-28 00:15:03 +01:00
|
|
|
KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
|
|
|
|
KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
|
|
|
|
KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
|
|
|
|
KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
|
|
|
|
KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
|
|
|
|
KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
|
|
|
|
|
|
|
|
$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
|
|
|
|
mkdir -p $(dir $@)
|
2019-11-22 16:55:58 +01:00
|
|
|
$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
|
|
|
|
$(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
|
|
|
|
true
|
|
|
|
|
|
|
|
.PHONY: $(alltests) $(addprefix clean-,$(alltests))
|
|
|
|
.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
|
|
|
|
|
2021-10-07 11:06:42 +02:00
|
|
|
# %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
|
|
|
|
# by macro cb_run_group_tests(), which should be used for running tests.
|
|
|
|
# __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
|
2020-05-27 10:59:09 +02:00
|
|
|
ifeq ($(JUNIT_OUTPUT),y)
|
|
|
|
$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
|
2021-10-07 11:06:42 +02:00
|
|
|
$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
|
2020-05-27 10:59:09 +02:00
|
|
|
endif
|
|
|
|
|
2020-03-28 00:15:03 +01:00
|
|
|
$(alltests): $$($$(@)-bin)
|
2021-10-07 11:06:42 +02:00
|
|
|
rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
|
|
|
|
rm -f $(testobj)/$(subst /,_,$^).failed
|
2021-07-24 00:43:18 +02:00
|
|
|
-$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
|
2020-03-28 00:15:03 +01:00
|
|
|
|
2021-05-11 17:56:48 +02:00
|
|
|
# Build a code coverage report by collecting all the gcov files into a single
|
|
|
|
# report. If COV is not set, this might be a user error, and they're trying
|
|
|
|
# to generate a coverage report without first having built and run the code
|
|
|
|
# with code coverage. So instead of silently correcting it by adding COV=1,
|
|
|
|
# let's flag it to the user so they can be sure they're doing the thing they
|
|
|
|
# want to do.
|
2021-04-16 19:36:25 +02:00
|
|
|
|
2021-05-11 17:56:48 +02:00
|
|
|
.PHONY: coverage-report clean-coverage-report
|
|
|
|
|
|
|
|
ifeq ($(COV),1)
|
|
|
|
coverage-report:
|
|
|
|
lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
|
|
|
|
genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
|
2021-04-16 19:36:25 +02:00
|
|
|
-s $(testobj)/tests.info
|
|
|
|
|
2021-05-11 17:56:48 +02:00
|
|
|
clean-coverage-report:
|
|
|
|
rm -Rf $(testobj)/$(coverage_dir)
|
|
|
|
else
|
|
|
|
coverage-report:
|
|
|
|
COV=1 V=$(V) $(MAKE) coverage-report
|
|
|
|
|
|
|
|
clean-coverage-report:
|
|
|
|
COV=1 V=$(V) $(MAKE) clean-coverage-report
|
|
|
|
endif
|
|
|
|
|
2020-03-28 00:15:03 +01:00
|
|
|
unit-tests: build-unit-tests run-unit-tests
|
|
|
|
|
|
|
|
build-unit-tests: $(test-bins)
|
|
|
|
|
|
|
|
run-unit-tests: $(alltests)
|
2020-05-27 11:39:32 +02:00
|
|
|
if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
|
|
|
|
echo "**********************"; \
|
|
|
|
echo " TESTS FAILED"; \
|
|
|
|
echo "**********************"; \
|
|
|
|
exit 1; \
|
|
|
|
else \
|
|
|
|
echo "**********************"; \
|
|
|
|
echo " ALL TESTS PASSED"; \
|
|
|
|
echo "**********************"; \
|
|
|
|
exit 0; \
|
|
|
|
fi
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
$(addprefix clean-,$(alltests)): clean-%:
|
2021-05-11 17:56:48 +02:00
|
|
|
rm -rf $(testobj)/$*
|
2020-03-28 00:15:03 +01:00
|
|
|
|
|
|
|
clean-unit-tests:
|
|
|
|
rm -rf $(testobj)
|
2021-04-13 16:07:05 +02:00
|
|
|
|
|
|
|
list-unit-tests:
|
|
|
|
@echo "unit-tests:"
|
|
|
|
for t in $(sort $(alltests)); do \
|
|
|
|
echo " $$t"; \
|
|
|
|
done
|
|
|
|
|
|
|
|
help-unit-tests help::
|
|
|
|
@echo '*** coreboot unit-tests targets ***'
|
2021-05-11 17:56:48 +02:00
|
|
|
@echo ' Use "COV=1 make [target]" to enable code coverage for unit tests'
|
2021-04-16 19:36:25 +02:00
|
|
|
@echo ' unit-tests - Run all unit-tests from tests/'
|
|
|
|
@echo ' clean-unit-tests - Remove unit-tests build artifacts'
|
|
|
|
@echo ' list-unit-tests - List all unit-tests'
|
|
|
|
@echo ' <unit-test> - Build and run single unit-test'
|
|
|
|
@echo ' clean-<unit-test> - Remove single unit-test build artifacts'
|
2021-05-11 17:56:48 +02:00
|
|
|
@echo ' coverage-report - Generate a code coverage report'
|
|
|
|
@echo ' clean-coverage-report - Remove the code coverage report'
|
2021-04-13 16:07:05 +02:00
|
|
|
@echo
|