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