Skip to content

Commit

Permalink
Allow GoogleTest to Test STATIC Functions
Browse files Browse the repository at this point in the history
Cmocka is able to test STATIC functions by including the C file
in the test file. However, in many scenarios, GoogleTest files
cannot do this as the C++ compiler has stricter rules than the
C compiler.

This patch updates the UnitTestFrameworkPkgHost.dsc.in to add
a new build flag GOOGLETEST_HOST_UNIT_TEST_BUILD for
HOST_APPLICATIONS only. Base.h is then updated to undef STATIC
if this flag is set. This allows for STATIC functions to be tested
in GoogleTest.

There is less danger of symbol collision here, because HOST_APPLICATIONS
are running with the least amount of dependencies possible. This still
allows for interfaces tests (where a Library Class is tested, not an
instance, so the library is linked in to the test instead of compiled)
as the library in this case will not be compiled as a HOST_APPLICATION
and so the STATIC functions will remain STATIC.
  • Loading branch information
os-d committed Apr 30, 2024
1 parent 7c8c4f0 commit 865b3dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions MdePkg/Include/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,13 @@ struct _LIST_ENTRY {

///
/// Datum is scoped to the current file or function.
/// To support GoogleTest compiling static functions, do away with static in that case
///
#ifndef GOOGLETEST_HOST_UNIT_TEST_BUILD
#define STATIC static
#else
#define STATIC
#endif

///
/// Undeclared type.
Expand Down
4 changes: 3 additions & 1 deletion UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#
# MSFT
#
MSFT:*_*_*_CC_FLAGS = /EHs
MSFT:*_*_*_CC_FLAGS = /EHs -D GOOGLETEST_HOST_UNIT_TEST_BUILD=1
MSFT:*_*_*_DLINK_FLAGS == /out:"$(BIN_DIR)\$(MODULE_NAME_GUID).exe" /pdb:"$(BIN_DIR)\$(MODULE_NAME_GUID).pdb" /IGNORE:4001 /NOLOGO /SUBSYSTEM:CONSOLE /DEBUG /STACK:0x40000,0x40000 /NODEFAULTLIB:libcmt.lib libcmtd.lib
MSFT:*_*_IA32_DLINK_FLAGS = /MACHINE:I386
MSFT:*_*_X64_DLINK_FLAGS = /MACHINE:AMD64
Expand All @@ -61,13 +61,15 @@
#
# GCC
#
GCC:*_*_*_CC_FLAGS = -D GOOGLETEST_HOST_UNIT_TEST_BUILD=1
GCC:*_*_IA32_DLINK_FLAGS == -o $(BIN_DIR)/$(MODULE_NAME_GUID) -m32 -no-pie
GCC:*_*_X64_DLINK_FLAGS == -o $(BIN_DIR)/$(MODULE_NAME_GUID) -m64 -no-pie
GCC:*_*_*_DLINK2_FLAGS == -lgcov -lpthread -lstdc++ -lm

#
# Need to do this link via gcc and not ld as the pathing to libraries changes from OS version to OS version
#
XCODE:*_*_*_CC_FLAGS = -D GOOGLETEST_HOST_UNIT_TEST_BUILD=1
XCODE:*_*_IA32_DLINK_PATH == gcc
XCODE:*_*_IA32_CC_FLAGS = -I$(WORKSPACE)/EmulatorPkg/Unix/Host/X11IncludeHack
XCODE:*_*_IA32_DLINK_FLAGS == -arch i386 -o $(BIN_DIR)/$(MODULE_NAME_GUID) -L/usr/X11R6/lib -lXext -lX11 -framework Carbon
Expand Down

0 comments on commit 865b3dd

Please sign in to comment.