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