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