From 64b99e3bacda1f50e2ba47ba881ba12b9e746293 Mon Sep 17 00:00:00 2001
From: Marc-Olivier Andrez <marc-olivier.andrez@desy.de>
Date: Tue, 7 May 2024 14:51:19 +0200
Subject: [PATCH] build: use `GTest` CMake module to handle dependencies on
 gtest

As a consequence, we can remove code in CMake files to handle these
dependencies on `gtest` and `gmock`, which fixes the following error
when running CMake:

```sh
$ cmake -G Ninja ../.. -DPython_EXECUTABLE=python3.7 -DBUILD_TESTS=ON -DENABLE_COVERAGE=ON -DBUILD_CPP_DOCS=ON -DBUILD_PYTHON_DOCS=ON
...
CMake Error at CMakeModules/testing_cpp.cmake:47 (IF):
  if given arguments:

    "WIN32" "AND" "STREQUAL" "Debug"

  Unknown arguments specified
Call Stack (most recent call first):
  common/cpp/src/common/CMakeLists.txt:11 (add_plain_unit_test)
```
---
 CMakeModules/testing_cpp.cmake | 22 ++--------------------
 README.md                      |  2 +-
 2 files changed, 3 insertions(+), 21 deletions(-)

diff --git a/CMakeModules/testing_cpp.cmake b/CMakeModules/testing_cpp.cmake
index 7f9a0448f..86c51d037 100644
--- a/CMakeModules/testing_cpp.cmake
+++ b/CMakeModules/testing_cpp.cmake
@@ -18,14 +18,12 @@ set (TOKENS "${TOKENS};BT_TEST_RUN_RW_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
 
 if (BUILD_TESTS)
     set(ASAPO_MINIMUM_COVERAGE 70)
+    find_package(GTest REQUIRED)
+    set(GTEST_LIBS GTest::gmock GTest::gmock_main)
     find_package(Threads)
     find_program(MEMORYCHECK_COMMAND valgrind)
     set(MEMORYCHECK_COMMAND_OPTIONS
             "--trace-children=yes --leak-check=full --error-exitcode=1 --num-callers=20 --suppressions=${CMAKE_SOURCE_DIR}/tests/valgrind.suppressions")
-    if (NOT "$ENV{gtest_SOURCE_DIR}" STREQUAL "")
-        set(gtest_SOURCE_DIR $ENV{gtest_SOURCE_DIR})
-    endif ()
-    message(STATUS "Will look for google test at ${gtest_SOURCE_DIR}")
     if (CMAKE_COMPILER_IS_GNUCXX)
         include(CodeCoverage)
         APPEND_COVERAGE_COMPILER_FLAGS()
@@ -35,20 +33,12 @@ endif ()
 #TODO: Call add_plain_unit_test in gtest
 function(add_plain_unit_test target test_source_files linktarget)
     if (BUILD_TESTS)
-        include_directories(SYSTEM ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
-        link_directories(${gtest_SOURCE_DIR}/lib)
-
         add_executable(test-${target} ${test_source_files})
 
         if (NOT ${libs} STREQUAL "")
             target_link_libraries(test-${target} ${libs})
         endif ()
 
-        IF (WIN32 AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
-            set(GTEST_LIBS gtestd gtest_maind gmockd)
-        ELSE ()
-            set(GTEST_LIBS gtest gmock gtest_main)
-        ENDIF (WIN32 AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
         target_link_libraries(test-${target} ${GTEST_LIBS} ${CMAKE_THREAD_LIBS_INIT})
 
         GET_PROPERTY(ASAPO_COMMON_IO_LIBRARIES GLOBAL PROPERTY ASAPO_COMMON_IO_LIBRARIES)
@@ -67,9 +57,6 @@ endfunction()
 
 function(gtest target test_source_files linktarget)
     if (BUILD_TESTS)
-        include_directories(SYSTEM ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
-        link_directories(${gtest_SOURCE_DIR}/lib)
-
         FOREACH (lib ${linktarget})
             if (NOT ${lib} STREQUAL "")
                 get_target_property(target_type ${lib} TYPE)
@@ -88,11 +75,6 @@ function(gtest target test_source_files linktarget)
             target_link_libraries(test-${target} ${libs})
         endif ()
 
-        IF (WIN32 AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
-            set(GTEST_LIBS gtestd gtest_maind gmockd)
-        ELSE ()
-            set(GTEST_LIBS gtest gmock gtest_main)
-        ENDIF (WIN32 AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
         target_link_libraries(test-${target} ${GTEST_LIBS} ${CMAKE_THREAD_LIBS_INIT})
 
         GET_PROPERTY(ASAPO_COMMON_IO_LIBRARIES GLOBAL PROPERTY ASAPO_COMMON_IO_LIBRARIES)
diff --git a/README.md b/README.md
index c61da8dc7..2c2e2e305 100644
--- a/README.md
+++ b/README.md
@@ -68,7 +68,7 @@ The software is MIT licensed (see LICENSE.txt) and uses third party libraries th
 (see LICENSE-3RD-PARTY.txt)
 
 ### with non-standard 3rd party libraries paths
-- cmake -Dlibmongoc-static-1.0_DIR=... -Dlibbson-static-1.0_DIR=...  -Dgtest_SOURCE_DIR=... -DLIBCURL_DIR=... ...
+- cmake -Dlibmongoc-static-1.0_DIR=... -Dlibbson-static-1.0_DIR=...  -DLIBCURL_DIR=... ...
 
 ### compile
  - make -j 4
-- 
GitLab