Jakub Czapiga has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/70108 )
Change subject: tests: Support linking with system libc ......................................................................
tests: Support linking with system libc
This patch allows for linking selected files with system libc. This allows for creating libraries interacting with filesystem, standard I/O and other parts of system. Until now it was only possible using CMocka proxy functions or functions not masked by code under test.
Signed-off-by: Jakub Czapiga jacz@semihalf.com Change-Id: I652362ba61a25e974d706357fc36479ccee763e4 --- M tests/Makefile.common M tests/Makefile.inc 2 files changed, 48 insertions(+), 14 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/08/70108/1
diff --git a/tests/Makefile.common b/tests/Makefile.common index fe46aeb..8a4bc9a 100644 --- a/tests/Makefile.common +++ b/tests/Makefile.common @@ -27,17 +27,20 @@ TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
# Include order should be same as in real build -TEST_CFLAGS := -include $(src)/include/kconfig.h \ +TEST_INCLUDES := -include $(src)/include/kconfig.h \ -include $(src)/include/rules.h \ -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h
# Include generic test mock headers, before original ones -TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include +TEST_INCLUDES += -I$(testsrc)/include/mocks -I$(testsrc)/include
-TEST_CFLAGS += -I$(src) -I$(src)/include -I$(src)/commonlib/include \ +TEST_INCLUDES += -I$(src) -I$(src)/include -I$(src)/commonlib/include \ -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \ -I$(top)/3rdparty/vboot/firmware/include
+# Path for Kconfig autoheader +TEST_INCLUDES += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) + # Note: This is intentionally just a subset of the warnings in the toplevel # Makefile.inc. We don't need to be as strict with test code, and things like # -Wmissing-prototypes just make working with the test framework cumbersome. @@ -48,9 +51,6 @@ TEST_CFLAGS += -Wno-array-compare -Wno-packed-not-aligned -Wno-trigraphs TEST_CFLAGS += -Wno-unused-but-set-variables
-# Path for Kconfig autoheader -TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) - TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
TEST_CFLAGS += -D__TEST__ @@ -66,7 +66,14 @@ TEST_LDFLAGS += -no-pie
# Extra attributes for unit tests, declared per test -attributes := srcs cflags config mocks no_test_framework stage +# srcs - sources linked with coreboot libc symbols +# syssrcs - sources linked with system libc, no coreboot includes +# cflags - attribute with additional CFLAGS +# config - Kconfig and defines override (`gcc -D` style) +# mocks - list of symbols to mock +# no_test_framework - set to `1` not to link test framework +# stage - name of stage e.g. romstage, bootblock, etc. (default: ramstage) +attributes := srcs cflags config-file config mocks no_test_framework stage
# Copy attributes of one test to another. # $1 - input test name @@ -113,12 +120,15 @@ # Give us a way to distinguish between coreboot source files and test files in code. $($(1)-srcobjs): TEST_CFLAGS += -D__TEST_SRCOBJ__
+# Add coreboot, vboot, kconfig etc. includes only to non-system libc linked objects +$(filter-out $($(1)-syslibcobjs),$($(1)-objs)): TEST_CFLAGS += $(TEST_INCLUDES) + # Compile sources and apply mocking/wrapping of selected symbols. # For each listed mock add new symbol with prefix `__real_`, # and pointing to the same section:address. $($(1)-objs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) mkdir -p $$(dir $$@) - $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ + $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $$(TEST_INCLUDES) $($(1)-cflags) -MMD \ -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@.orig objcopy_wrap_flags=''; \ for sym in $$($(1)-mocks); do \ @@ -132,13 +142,20 @@ done ; \ $(OBJCOPY) $$@.orig $$$$objcopy_wrap_flags $$@
-# Link against Cmocka if not disabled -$(addsuffix $$(filter-out n 0,$($(1)-no_test_framework)),$($(1)-objs)): TEST_CFLAGS += -I$(cmockasrc)/include -$($(1)-bin)$$(filter-out n 0,$($(1)-no_test_framework)): TEST_LDFLAGS += -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src -$($(1)-bin)$$(filter-out n 0,$($(1)-no_test_framework)): TEST_CFLAGS += -I$(cmockasrc)/include -$($(1)-bin)$$(filter-out n 0,$($(1)-no_test_framework)): $(CMOCKA_LIB) +# Compile system-side sources linked with system libc, without mocking symbols +# or code-under-test includes. +$($(1)-syslibcobjs): $(testobj)/$(1)/%.o: $$$$*.c $$($(1)-config-file) + mkdir -p $$(dir $$@) + $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ + -MF $$(basename $$@).d -MT $$@ -c $$< -o $$@
-$($(1)-bin): $($(1)-objs) +# Link against Cmocka if not disabled +$(addsuffix $(filter-out n 0,$($(1)-no_test_framework)),$($(1)-objs)): TEST_CFLAGS += -I$(cmockasrc)/include +$($(1)-bin)$(filter-out n 0,$($(1)-no_test_framework)): TEST_LDFLAGS += -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src +$($(1)-bin)$(filter-out n 0,$($(1)-no_test_framework)): TEST_CFLAGS += -I$(cmockasrc)/include +$($(1)-bin)$(filter-out n 0,$($(1)-no_test_framework)): $(CMOCKA_LIB) + +$($(1)-bin): $($(1)-objs) $($(1)-syslibcobjs) $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
endef diff --git a/tests/Makefile.inc b/tests/Makefile.inc index 90f01d1..04792c1 100644 --- a/tests/Makefile.inc +++ b/tests/Makefile.inc @@ -51,6 +51,8 @@ $(foreach test, $(alltests), \ $(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \ $(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ + $(eval $(test)-syslibcobjs := $(addprefix $(testobj)/$(test)/, \ + $(patsubst %.c,%.o,$($(test)-syssrcs)))) \ $(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \ $(patsubst %.c,%.o,$($(test)-srcs))))) $(foreach test, $(alltests), \