[PATCH] Update cmakelists to use system googletest if available.
authorGianfranco Costamagna <costamagnagianfranco@yahoo.it>
Sat, 14 Oct 2023 07:23:02 +0000 (09:23 +0200)
committerSimon Quigley <tsimonq2@debian.org>
Tue, 18 Feb 2025 21:10:35 +0000 (15:10 -0600)
There is no need to use the embedded gtest code copy in Linux systems, if they already provide the googletest framework system-wide.
Search for it, and fallback to the embedded one if the system one is not detected.

This patch has been also contributed by Simon Quigley <tsimonq2@debian.org>

Gbp-Pq: Name 1035.patch

CMakeLists.txt
test/CMakeLists.txt

index 46dc18059e5a3f73356555de6028d5d0c2762384..09b3a0bcb1a4d8efbea7cf666326aaf5f5d9b294 100644 (file)
@@ -25,6 +25,7 @@ option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
 option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIBS})
 option(YAML_CPP_INSTALL "Enable generation of yaml-cpp install targets" ${YAML_CPP_MAIN_PROJECT})
 option(YAML_CPP_FORMAT_SOURCE "Format source" ON)
+option(YAML_USE_SYSTEM_GTEST "Use system googletest if found" OFF)
 cmake_dependent_option(YAML_CPP_BUILD_TESTS
   "Enable yaml-cpp tests" OFF
   "BUILD_TESTING;YAML_CPP_MAIN_PROJECT" OFF)
index 351b03f81098b531e2cf5c9319007a8fd650030d..895a44fb675b842bfea1d3a1bf160d5c552904e9 100644 (file)
@@ -4,11 +4,17 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
 set(BUILD_MOCK ON CACHE BOOL "" FORCE)
 set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
 
-add_subdirectory(
-  "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0"
-  "${CMAKE_CURRENT_BINARY_DIR}/prefix")
-
-include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0/googletest/include")
+if(YAML_USE_SYSTEM_GTEST)
+  find_package(GTest)
+  if (NOT GTEST_FOUND)
+    message(FATAL_ERROR "system googletest was requested but not found")
+  endif()
+else()
+    add_subdirectory(
+      "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0"
+      "${CMAKE_CURRENT_BINARY_DIR}/prefix")
+    include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0/googletest/include")
+endif()
 
 set(test-new-api-pattern "new-api/*.cpp")
 set(test-source-pattern "*.cpp" "integration/*.cpp" "node/*.cpp")
@@ -38,6 +44,7 @@ target_link_libraries(yaml-cpp-tests
   PRIVATE
     Threads::Threads
     yaml-cpp
+    gtest
     gmock)
 
 set_property(TARGET yaml-cpp-tests PROPERTY CXX_STANDARD_REQUIRED ON)