I’ve been working on this some more, and I’ve decided on doing the following. If any of this is a bad idea, please let me know.
I’ve gone with option #2, and figured out how to temporarily enable static builds for dependencies using the following pattern:
# .... rest of file
add_executable(${PROJECT_NAME} ${TEST_SOURCE_FILES})
# Force google test to build statically
set(${PROJECT_NAME}_TEMP_GTEST_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS NO)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
FetchContent_MakeAvailable(googletest)
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest_main GTest::gtest GTest::gmock_main GTest::gmock)
set(BUILD_SHARED_LIBS ${PROJECT_NAME}_TEMP_GTEST_BUILD_SHARED_LIBS})
# Restore shared library configuration from temporary variable
# ... repeat for other dependencies if needed
I did this in both my test cmake file, for its own dependencies (just gtest), as well as in the CMakeLists.txt
of the project that is being tested by it (which calls add_subdirectory
on this Tests/
directory).
And then, at the bottom of my test project CMakeLists.txt
, I add the following, which is now limited to exactly one additional PATH environment variable addition since the rest of the deps are built statically now. Since my project under test is a shared library in this case, I wanted that to remain a DLL, so I add that output folder to the PATH for CTest and that’s all that is required.
# Restore shared library configuration from temporary variable
target_link_libraries(${PROJECT_NAME} PUBLIC my_library_under_test)
include(GoogleTest)
gtest_add_tests(TARGET ${PROJECT_NAME} TEST_LIST allTests)
if(WIN32)
# Ensure the required DLLs can be found at runtime
set_tests_properties(
${allTests}
PROPERTIES ENVIRONMENT "PATH=$<SHELL_PATH:$<TARGET_FILE_DIR:my_library_under_test>>$<SEMICOLON>$ENV{PATH}"
)
endif()