summaryrefslogtreecommitdiffstats
path: root/tests/Makefile.inc
diff options
context:
space:
mode:
authorPatrick Georgi <pgeorgi@google.com>2021-06-09 18:37:42 +0200
committerPatrick Georgi <pgeorgi@google.com>2021-06-10 19:26:46 +0000
commitce55ca2fcaab23010b2f7e310c921f65b037034d (patch)
treeab160de356ce394bded5e4406096eba7dc278d20 /tests/Makefile.inc
parent36c90179f0c7ef0d11ca78a109bd563b3ec0e0a9 (diff)
downloadcoreboot-ce55ca2fcaab23010b2f7e310c921f65b037034d.tar.gz
coreboot-ce55ca2fcaab23010b2f7e310c921f65b037034d.tar.bz2
coreboot-ce55ca2fcaab23010b2f7e310c921f65b037034d.zip
tests: Rework mocking facility
Using the linker's --wrap feature has the downside that it only covers references across object files: If foo.c defines a() and b(), with b calling a, --wrap=a does nothing to that call. Instead, use objcopy to mark a weak and global so it can be overridden by another implementation, but only for files originating in src/. That way mocks - implemented in tests/ - become the source of truth. TEST=Had such an issue with get_log_level() in a follow-up commit, and the mock now takes over. Also, all existing unit tests still pass. Change-Id: I99c6d6e44ecfc73366bf464d9c51c7da3f8db388 Signed-off-by: Patrick Georgi <pgeorgi@google.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/55360 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Jakub Czapiga <jacz@semihalf.com>
Diffstat (limited to 'tests/Makefile.inc')
-rw-r--r--tests/Makefile.inc8
1 files changed, 6 insertions, 2 deletions
diff --git a/tests/Makefile.inc b/tests/Makefile.inc
index 3473ef74d0b8..15ea670336bf 100644
--- a/tests/Makefile.inc
+++ b/tests/Makefile.inc
@@ -17,6 +17,7 @@ coverage_dir = coverage_reports
CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
CMAKE:= cmake
+OBJCOPY?= objcopy
TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
TEST_DOTCONFIG = $(testobj)/.config
@@ -114,18 +115,21 @@ $$($(1)-config-file): $(TEST_KCONFIG_AUTOHEADER)
$($(1)-objs): TEST_CFLAGS += -I$$(dir $$($(1)-config-file)) \
-D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
+$($(1)-srcobjs): OBJCOPY_FLAGS += $$(foreach mock,$$($(1)-mocks),--globalize-symbol=$$(mock) --weaken-symbol=$$(mock))
$($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file)
mkdir -p $$(dir $$@)
$(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
- -MT $$@ -c $$< -o $$@
+ -MT $$@ -c $$< -o $$@.orig
+ $(OBJCOPY) $$@.orig $$(OBJCOPY_FLAGS) $$@
-$($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
$(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
endef
$(foreach test, $(alltests), \
+ $(eval $(test)-srcobjs:=$(addprefix $(testobj)/$(test)/, \
+ $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
$(eval $(test)-objs:=$(addprefix $(testobj)/$(test)/, \
$(patsubst %.c,%.o,$($(test)-srcs)))))
$(foreach test, $(alltests), \