blob: 03ae7c4aa0a68062930680c9a32bb7cd73e0edb5 [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
29TEST_CFLAGS := -include $(src)/include/kconfig.h \
30 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
31 -include $(src)/include/rules.h
32
33# Include generic test mock headers, before original ones
34TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
35
36TEST_CFLAGS += -I$(src) -I$(src)/include -I$(src)/commonlib/include \
37 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \
38 -I$(top)/3rdparty/vboot/firmware/include
39
40# Note: This is intentionally just a subset of the warnings in the toplevel
41# Makefile.inc. We don't need to be as strict with test code, and things like
42# -Wmissing-prototypes just make working with the test framework cumbersome.
43# Only put conservative warnings here that really detect code that's obviously
44# unintentional.
45TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
46TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type
47
48# Path for Kconfig autoheader
49TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
50
51TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
52
53TEST_CFLAGS += -D__TEST__
54
55TEST_CFLAGS += -I$(cmockasrc)/include
56
57ifneq ($(filter-out 0,$(TEST_PRINT)),)
58TEST_CFLAGS += -DTEST_PRINT=1
59endif
60
61# Link against Cmocka
62TEST_LDFLAGS := -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
63TEST_LDFLAGS += -Wl,--gc-sections
64
65# Some memlayout symbols don't work with userspace relocation -- disable it.
66TEST_CFLAGS += -fno-pie -fno-pic
67TEST_LDFLAGS += -no-pie
68
69# Extra attributes for unit tests, declared per test
70attributes := srcs cflags config mocks stage
71
72# Copy attributes of one test to another.
73# $1 - input test name
74# $2 - output test name
75copy-test = $(foreach attr,$(attributes), \
76 $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr))))
77
78# Create actual targets for unit test binaries
79# $1 - test name
80define TEST_CC_template
81
82# Generate custom config.h redefining given config symbols, and declaring mocked
83# functions weak. It is important that the compiler already sees that they are
84# weak (and they aren't just turned weak at a later stage) to prevent certain
85# optimizations that would break if the function gets replaced. (For clang this
86# file needs to be marked `system_header` to prevent it from warning about
87# #pragma weak entries without a matching function declaration, since there's no
88# -Wno-xxx command line option for that.)
89$(1)-config-file := $(testobj)/$(1)/config.h
90$$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
91 mkdir -p $$(dir $$@)
92 printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@
93 printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@
94 for kv in $$($(1)-config); do \
95 key="`echo $$$$kv | cut -d '=' -f -1`"; \
96 value="`echo $$$$kv | cut -d '=' -f 2-`"; \
97 printf '#undef %s\n' "$$$$key" >> $$@; \
98 printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \
99 done
100 printf '#ifdef __clang__\n' >> $$@;
101 printf '#pragma clang system_header\n' >> $$@;
102 printf '#endif\n' >> $$@;
103 printf '#ifdef __TEST_SRCOBJ__\n' >> $$@;
104 for m in $$($(1)-mocks); do \
105 printf '#pragma weak %s\n' "$$$$m" >> $$@; \
106 done
107 printf '#endif\n' >> $$@;
108
109$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
110 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \
111 -D__TEST_NAME__=\"$(subst /,_,$(1))\" \
112 -D__TEST_DATA_DIR__=\"$(testsrc)/data\"
113
114# Give us a way to distinguish between coreboot source files and test files in code.
115$($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
116
117# Compile sources and apply mocking/wrapping of selected symbols.
118# For each listed mock add new symbol with prefix `__real_`,
119# and pointing to the same section:address.
120$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
121 mkdir -p $$(dir $$@)
122 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
123 -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig
124 objcopy_wrap_flags=''; \
125 for sym in $$($(1)-mocks); do \
126 sym_line="$$$$($(OBJDUMP) -t $$@.orig \
127 | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \
128 if [ ! -z "$$$$sym_line" ] ; then \
129 addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \
130 section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \
131 objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \
132 fi \
133 done ; \
134 $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
135
136$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
137 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
138
139endef
140
141# Build cmocka
142$(CMOCKA_LIB):
143 echo "*** Building CMOCKA ***"
144 mkdir -p $(cmockaobj)
145 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
146 $(MAKE) -C $(cmockaobj)
147
148# Kconfig targets
149$(TEST_DOTCONFIG):
150 mkdir -p $(dir $@)
151 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
152
153# Don't override default Kconfig variables, since this will affect all
154# Kconfig targets. Change them only when calling sub-make instead.
155$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \
156 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
157 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
158 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
159 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
160 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
161 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
162
163$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
164 mkdir -p $(dir $@)
165 $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
166 $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig
167
168$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
169 true