From 94979eff978a8e6b8c8e439787e9551671e949c7 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Mon, 16 Dec 2024 13:09:02 +0000 Subject: [PATCH] build: initialize GTest submodule in CMake Try to initialize the GTest Git submodule from the CMake code if it has not been. If that fails, turn off `BUILD_TESTING`. We do not want to require recursive clones or initializing submodules for users by default. Signed-off-by: Rafael Kitover --- CMakeLists.txt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0d5584a..2df6d218 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,8 +92,20 @@ endif() if(BUILD_TESTING) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) - add_subdirectory(./third_party/googletest) - include(GoogleTest) + + if(NOT EXISTS third_party/googletest/CMakeLists.txt) + execute_process( + COMMAND git submodule update --init --recursive -- third_party/googletest + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) + endif() + + if(EXISTS third_party/googletest/CMakeLists.txt) + add_subdirectory(./third_party/googletest) + include(GoogleTest) + else() + set(BUILD_TESTING OFF) + endif() endif() if(NOT CMAKE_PREFIX_PATH AND (NOT ("$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")))