blob: 175587a9119c05a4d3566fe76fdb6070b12a1c12 [file] [log] [blame]
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +02001# SPDX-License-Identifier: GPL-2.0-only
2
3# This file contains common definitions, macros and targets for depthcharge
4# unit-tests and screenshot utility. It requires list of defined variables:
5# - src - source directory
6# - testobj - build directory of tests
7# - objutil - utility programs/libraries output directory
8
9testsrc := $(top)/tests
10
11cmockasrc := 3rdparty/cmocka
12cmockaobj := $(objutil)/cmocka
13coverage_dir := coverage_reports
14
15CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
16
17CMAKE := cmake
18OBJCOPY ?= objcopy
19OBJDUMP ?= objdump
20
21TEST_DEFAULT_CONFIG := $(top)/configs/config.emulation_qemu_x86_i440fx
22TEST_DOTCONFIG := $(testobj)/.config
23TEST_KCONFIG_AUTOHEADER := $(testobj)/config.src.h
24TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
25TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
26TEST_KCONFIG_SPLITCONFIG := $(testobj)/config/
27TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
28
Bill XIE348909a2022-07-09 10:16:20 +080029# Include order should be same as in real build
Jakub Czapigae744ba62022-11-14 11:22:59 +010030TEST_INCLUDES := -include $(src)/include/kconfig.h \
Bill XIE348909a2022-07-09 10:16:20 +080031 -include $(src)/include/rules.h \
32 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020033
34# Include generic test mock headers, before original ones
Jakub Czapigae744ba62022-11-14 11:22:59 +010035TEST_INCLUDES += -I$(testsrc)/include/mocks -I$(testsrc)/include
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020036
Jakub Czapigae744ba62022-11-14 11:22:59 +010037TEST_INCLUDES += -I$(src) -I$(src)/include -I$(src)/commonlib/include \
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020038 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \
39 -I$(top)/3rdparty/vboot/firmware/include
40
Jakub Czapigae744ba62022-11-14 11:22:59 +010041# Path for Kconfig autoheader
42TEST_INCLUDES += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
43
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020044# Note: This is intentionally just a subset of the warnings in the toplevel
45# Makefile.inc. We don't need to be as strict with test code, and things like
46# -Wmissing-prototypes just make working with the test framework cumbersome.
47# Only put conservative warnings here that really detect code that's obviously
48# unintentional.
49TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
50TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type
Jakub Czapiga90072902022-09-08 16:38:50 +020051TEST_CFLAGS += -Wno-array-compare -Wno-packed-not-aligned -Wno-trigraphs
52TEST_CFLAGS += -Wno-unused-but-set-variables
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020053
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020054TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
55
56TEST_CFLAGS += -D__TEST__
57
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020058ifneq ($(filter-out 0,$(TEST_PRINT)),)
59TEST_CFLAGS += -DTEST_PRINT=1
60endif
61
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020062TEST_LDFLAGS += -Wl,--gc-sections
63
64# Some memlayout symbols don't work with userspace relocation -- disable it.
65TEST_CFLAGS += -fno-pie -fno-pic
66TEST_LDFLAGS += -no-pie
67
68# Extra attributes for unit tests, declared per test
Jakub Czapigae744ba62022-11-14 11:22:59 +010069attributes := srcs syssrcs cflags config mocks no_test_framework stage
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +020070
71# Copy attributes of one test to another.
72# $1 - input test name
73# $2 - output test name
74copy-test = $(foreach attr,$(attributes), \
75 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
76
77# Create actual targets for unit test binaries
78# $1 - test name
79define TEST_CC_template
80
81# Generate custom config.h redefining given config symbols, and declaring mocked
82# functions weak. It is important that the compiler already sees that they are
83# weak (and they aren't just turned weak at a later stage) to prevent certain
84# optimizations that would break if the function gets replaced. (For clang this
85# file needs to be marked `system_header` to prevent it from warning about
86# #pragma weak entries without a matching function declaration, since there's no
87# -Wno-xxx command line option for that.)
88$(1)-config-file := $(testobj)/$(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 printf '#ifdef __clang__\n' >> $$@;
100 printf '#pragma clang system_header\n' >> $$@;
101 printf '#endif\n' >> $$@;
102 printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
103 for m in $$($(1)-mocks); do \
104 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
105 done
106 printf '#endif\n' >> $$@;
107
108$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
109 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
110 -D__TEST_NAME__=\"$(subst /,_,$(1))\" \
111 -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
112
113# Give us a way to distinguish between coreboot source files and test files in code.
114$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
115
Jakub Czapigae744ba62022-11-14 11:22:59 +0100116# Add coreboot, vboot, kconfig etc. includes only to non-system libc linked objects
117$(filter-out $($(1)-sysobjs),$($(1)-objs)): TEST_CFLAGS += $(TEST_INCLUDES)
118
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +0200119# Compile sources and apply mocking/wrapping of selected symbols.
120# For each listed mock add new symbol with prefix `__real_`,
121# and pointing to the same section:address.
122$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
123 mkdir -p $$(dir $$@)
Jakub Czapigae744ba62022-11-14 11:22:59 +0100124 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $$(TEST_INCLUDES) $($(1)-cflags) -MMD \
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +0200125 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
126 objcopy_wrap_flags=''; \
127 for sym in $$($(1)-mocks); do \
128 sym_line="$$$$($(OBJDUMP) -t $$@.orig \
129 | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
130 if [ ! -z "$$$$sym_line" ] ; then \
131 addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \
132 section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \
133 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
134 fi \
135 done ; \
136 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
137
Jakub Czapigae744ba62022-11-14 11:22:59 +0100138# Compile system-side sources linked with system libc, without mocking symbols
139# or code-under-test includes.
140$($(1)-sysobjs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
141 mkdir -p $$(dir $$@)
142 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
143 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@
144
Jakub Czapiga3c8a3d12022-11-09 10:38:11 +0100145# Link against Cmocka if not disabled
146ifeq ($(strip $(filter-out 0 n no,$($(1)-no_test_framework))),)
147$($(1)-objs): TEST_CFLAGS += -I$(cmockasrc)/include
148$($(1)-bin): TEST_LDFLAGS += -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
149$($(1)-bin): TEST_CFLAGS += -I$(cmockasrc)/include
150$($(1)-bin): $(CMOCKA_LIB)
151endif
152
Jakub Czapigae744ba62022-11-14 11:22:59 +0100153$($(1)-bin): $($(1)-objs) $($(1)-sysobjs)
Jakub Czapiga9ebc6c12022-04-14 13:56:02 +0200154 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
155
156endef
157
158# Build cmocka
159$(CMOCKA_LIB):
160 echo "*** Building CMOCKA ***"
161 mkdir -p $(cmockaobj)
162 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
163 $(MAKE) -C $(cmockaobj)
164
165# Kconfig targets
166$(TEST_DOTCONFIG):
167 mkdir -p $(dir $@)
168 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
169
170# Don't override default Kconfig variables, since this will affect all
171# Kconfig targets. Change them only when calling sub-make instead.
172$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
173 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
174 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
175 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
176 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
177 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
178 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
179
180$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
181 mkdir -p $(dir $@)
182 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
183 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
184
185$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
186 true
Jakub Czapigaa0e36d82022-09-06 10:42:16 +0200187
188TEST_COMMON_DEPENDENCIES := $(CMOCKA_LIB) $(TEST_KCONFIG_AUTOCONFIG)