Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 1 | # 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 | |
| 9 | testsrc := $(top)/tests |
| 10 | |
| 11 | cmockasrc := 3rdparty/cmocka |
| 12 | cmockaobj := $(objutil)/cmocka |
| 13 | coverage_dir := coverage_reports |
| 14 | |
| 15 | CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so |
| 16 | |
| 17 | CMAKE := cmake |
| 18 | OBJCOPY ?= objcopy |
| 19 | OBJDUMP ?= objdump |
| 20 | |
Jakub Czapiga | ab06861 | 2022-11-25 15:02:44 +0100 | [diff] [blame] | 21 | TEST_DEFAULT_CONFIG ?= $(top)/configs/config.emulation_qemu_x86_i440fx |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 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 |
| 28 | |
Bill XIE | 348909a | 2022-07-09 10:16:20 +0800 | [diff] [blame] | 29 | # Include order should be same as in real build |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 30 | TEST_INCLUDES := -include $(src)/include/kconfig.h \ |
Bill XIE | 348909a | 2022-07-09 10:16:20 +0800 | [diff] [blame] | 31 | -include $(src)/include/rules.h \ |
| 32 | -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 33 | |
| 34 | # Include generic test mock headers, before original ones |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 35 | TEST_INCLUDES += -I$(testsrc)/include/mocks -I$(testsrc)/include |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 36 | |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 37 | TEST_INCLUDES += -I$(src) -I$(src)/include -I$(src)/commonlib/include \ |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 38 | -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \ |
| 39 | -I$(top)/3rdparty/vboot/firmware/include |
| 40 | |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 41 | # Path for Kconfig autoheader |
| 42 | TEST_INCLUDES += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) |
| 43 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 44 | # 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. |
| 49 | TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm |
| 50 | TEST_CFLAGS += -Wno-unknown-warning-option -Wno-source-mgr -Wno-main-return-type |
Jakub Czapiga | 9007290 | 2022-09-08 16:38:50 +0200 | [diff] [blame] | 51 | TEST_CFLAGS += -Wno-array-compare -Wno-packed-not-aligned -Wno-trigraphs |
| 52 | TEST_CFLAGS += -Wno-unused-but-set-variables |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 53 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 54 | TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin |
| 55 | |
| 56 | TEST_CFLAGS += -D__TEST__ |
| 57 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 58 | ifneq ($(filter-out 0,$(TEST_PRINT)),) |
| 59 | TEST_CFLAGS += -DTEST_PRINT=1 |
| 60 | endif |
| 61 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 62 | TEST_LDFLAGS += -Wl,--gc-sections |
| 63 | |
| 64 | # Some memlayout symbols don't work with userspace relocation -- disable it. |
| 65 | TEST_CFLAGS += -fno-pie -fno-pic |
| 66 | TEST_LDFLAGS += -no-pie |
| 67 | |
| 68 | # Extra attributes for unit tests, declared per test |
Jakub Czapiga | 3088e43 | 2022-12-08 11:50:47 +0100 | [diff] [blame] | 69 | # srcs - sources linked with coreboot libc symbols |
| 70 | # syssrcs - sources linked with system libc, no coreboot includes |
| 71 | # cflags - attribute with additional CFLAGS |
| 72 | # config - Kconfig and defines override (`gcc -D` style) |
| 73 | # mocks - list of symbols to mock |
| 74 | # no_test_framework - set to `1` not to link test framework |
| 75 | # stage - name of stage e.g. romstage, bootblock, etc. |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 76 | attributes := srcs syssrcs cflags config mocks no_test_framework stage |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 77 | |
| 78 | # Copy attributes of one test to another. |
| 79 | # $1 - input test name |
| 80 | # $2 - output test name |
| 81 | copy-test = $(foreach attr,$(attributes), \ |
| 82 | $(eval $(strip $(2))-$(attr) := $($(strip $(1))-$(attr)))) |
| 83 | |
| 84 | # Create actual targets for unit test binaries |
| 85 | # $1 - test name |
| 86 | define TEST_CC_template |
| 87 | |
| 88 | # Generate custom config.h redefining given config symbols, and declaring mocked |
| 89 | # functions weak. It is important that the compiler already sees that they are |
| 90 | # weak (and they aren't just turned weak at a later stage) to prevent certain |
| 91 | # optimizations that would break if the function gets replaced. (For clang this |
| 92 | # file needs to be marked `system_header` to prevent it from warning about |
| 93 | # #pragma weak entries without a matching function declaration, since there's no |
| 94 | # -Wno-xxx command line option for that.) |
| 95 | $(1)-config-file := $(testobj)/$(1)/config.h |
| 96 | $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER) |
| 97 | mkdir -p $$(dir $$@) |
| 98 | printf '// File generated by tests/Makefile.inc\n// Do not change\n' > $$@ |
| 99 | printf '#include <%s>\n\n' "$(notdir $(TEST_KCONFIG_AUTOHEADER))" >> $$@ |
| 100 | for kv in $$($(1)-config); do \ |
| 101 | key="`echo $$$$kv | cut -d '=' -f -1`"; \ |
| 102 | value="`echo $$$$kv | cut -d '=' -f 2-`"; \ |
| 103 | printf '#undef %s\n' "$$$$key" >> $$@; \ |
| 104 | printf '#define %s %s\n\n' "$$$$key" "$$$$value" >> $$@; \ |
| 105 | done |
| 106 | printf '#ifdef __clang__\n' >> $$@; |
| 107 | printf '#pragma clang system_header\n' >> $$@; |
| 108 | printf '#endif\n' >> $$@; |
| 109 | printf '#ifdef __TEST_SRCOBJ__\n' >> $$@; |
| 110 | for m in $$($(1)-mocks); do \ |
| 111 | printf '#pragma weak %s\n' "$$$$m" >> $$@; \ |
| 112 | done |
| 113 | printf '#endif\n' >> $$@; |
| 114 | |
| 115 | $($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \ |
| 116 | -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ \ |
| 117 | -D__TEST_NAME__=\"$(subst /,_,$(1))\" \ |
| 118 | -D__TEST_DATA_DIR__=\"$(testsrc)/data\" |
| 119 | |
| 120 | # Give us a way to distinguish between coreboot source files and test files in code. |
| 121 | $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__ |
| 122 | |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 123 | # Add coreboot, vboot, kconfig etc. includes only to non-system libc linked objects |
| 124 | $(filter-out $($(1)-sysobjs),$($(1)-objs)): TEST_CFLAGS += $(TEST_INCLUDES) |
| 125 | |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 126 | # Compile sources and apply mocking/wrapping of selected symbols. |
| 127 | # For each listed mock add new symbol with prefix `__real_`, |
| 128 | # and pointing to the same section:address. |
| 129 | $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) |
| 130 | mkdir -p $$(dir $$@) |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 131 | $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $$(TEST_INCLUDES) $($(1)-cflags) -MMD \ |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 132 | -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig |
| 133 | objcopy_wrap_flags=''; \ |
| 134 | for sym in $$($(1)-mocks); do \ |
| 135 | sym_line="$$$$($(OBJDUMP) -t $$@.orig \ |
| 136 | | grep -E "[0-9a-fA-F]+\\s+w\\s+F\\s+.*\\s$$$$sym$$$$")"; \ |
| 137 | if [ ! -z "$$$$sym_line" ] ; then \ |
| 138 | addr="$$$$(echo "$$$$sym_line" | awk '{ print $$$$1 }')"; \ |
| 139 | section="$$$$(echo "$$$$sym_line" | awk '{ print $$$$(NF - 2) }')"; \ |
| 140 | objcopy_wrap_flags="$$$$objcopy_wrap_flags --add-symbol __real_$$$${sym}=$$$${section}:0x$$$${addr},function,global"; \ |
| 141 | fi \ |
| 142 | done ; \ |
| 143 | $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@ |
| 144 | |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 145 | # Compile system-side sources linked with system libc, without mocking symbols |
| 146 | # or code-under-test includes. |
| 147 | $($(1)-sysobjs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) |
| 148 | mkdir -p $$(dir $$@) |
| 149 | $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ |
| 150 | -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@ |
| 151 | |
Jakub Czapiga | 3c8a3d1 | 2022-11-09 10:38:11 +0100 | [diff] [blame] | 152 | # Link against Cmocka if not disabled |
| 153 | ifeq ($(strip $(filter-out 0 n no,$($(1)-no_test_framework))),) |
| 154 | $($(1)-objs): TEST_CFLAGS += -I$(cmockasrc)/include |
| 155 | $($(1)-bin): TEST_LDFLAGS += -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src |
| 156 | $($(1)-bin): TEST_CFLAGS += -I$(cmockasrc)/include |
| 157 | $($(1)-bin): $(CMOCKA_LIB) |
| 158 | endif |
| 159 | |
Jakub Czapiga | e744ba6 | 2022-11-14 11:22:59 +0100 | [diff] [blame] | 160 | $($(1)-bin): $($(1)-objs) $($(1)-sysobjs) |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 161 | $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@ |
| 162 | |
| 163 | endef |
| 164 | |
| 165 | # Build cmocka |
| 166 | $(CMOCKA_LIB): |
| 167 | echo "*** Building CMOCKA ***" |
| 168 | mkdir -p $(cmockaobj) |
| 169 | cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc)) |
| 170 | $(MAKE) -C $(cmockaobj) |
| 171 | |
| 172 | # Kconfig targets |
| 173 | $(TEST_DOTCONFIG): |
| 174 | mkdir -p $(dir $@) |
| 175 | cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG) |
| 176 | |
| 177 | # Don't override default Kconfig variables, since this will affect all |
| 178 | # Kconfig targets. Change them only when calling sub-make instead. |
| 179 | $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS := DOTCONFIG=$(TEST_DOTCONFIG) \ |
| 180 | KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \ |
| 181 | KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \ |
| 182 | KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \ |
| 183 | KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \ |
| 184 | KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \ |
| 185 | KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG) |
| 186 | |
Jakub Czapiga | d190e0f | 2022-12-05 08:49:35 +0100 | [diff] [blame] | 187 | $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) |
Jakub Czapiga | 9ebc6c1 | 2022-04-14 13:56:02 +0200 | [diff] [blame] | 188 | mkdir -p $(dir $@) |
| 189 | $(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig |
| 190 | $(MAKE) $(TEST_KCONFIG_FLAGS) syncconfig |
| 191 | |
| 192 | $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER) |
| 193 | true |
Jakub Czapiga | a0e36d8 | 2022-09-06 10:42:16 +0200 | [diff] [blame] | 194 | |
| 195 | TEST_COMMON_DEPENDENCIES := $(CMOCKA_LIB) $(TEST_KCONFIG_AUTOCONFIG) |