Blob Blame History Raw
From 29884ae01fde27204d92f554da4e92227a2ed1e6 Mon Sep 17 00:00:00 2001
From: Mattias Ellert <mattias.ellert@physics.uu.se>
Date: Sat, 26 Mar 2022 10:22:07 +0100
Subject: [PATCH 1/2] Implement builtin_gtest option

By setting the option to OFF the system gtest and gmock are used.
This allows doing tests without network available, e.g. during a
package build for Fedora/EPEL.
---
 cmake/modules/FindGTest.cmake               | 81 +++++++++++++++++++++
 cmake/modules/RootBuildOptions.cmake        |  2 +
 cmake/modules/RootMacros.cmake              |  2 +-
 cmake/modules/SearchInstalledSoftware.cmake | 44 ++++++++---
 4 files changed, 118 insertions(+), 11 deletions(-)
 create mode 100644 cmake/modules/FindGTest.cmake

diff --git a/cmake/modules/FindGTest.cmake b/cmake/modules/FindGTest.cmake
new file mode 100644
index 0000000000..438ec501c2
--- /dev/null
+++ b/cmake/modules/FindGTest.cmake
@@ -0,0 +1,81 @@
+# Find the gtest and gmock includes and library.
+#
+# This module defines
+# GTEST_LIBRARIES
+# GTEST_MAIN_LIBRARIES
+# GTEST_INCLUDE_DIRS
+# GMOCK_LIBRARIES
+# GMOCK_MAIN_LIBRARIES
+# GMOCK_INCLUDE_DIRS
+#
+# GTEST_FOUND           true if all libraries present
+
+find_package(Threads QUIET)
+
+find_path(GTEST_INCLUDE_DIRS NAMES gtest/gtest.h)
+find_library(GTEST_LIBRARIES NAMES gtest)
+find_library(GTEST_MAIN_LIBRARIES NAMES gtest_main)
+
+find_path(GMOCK_INCLUDE_DIRS NAMES gmock/gmock.h)
+find_library(GMOCK_LIBRARIES NAMES gmock)
+find_library(GMOCK_MAIN_LIBRARIES NAMES gmock_main)
+
+# Special for EPEL 7's gmock
+if(NOT GMOCK_LIBRARIES)
+  find_path(GMOCK_SRC_DIR NAMES gmock-all.cc PATHS /usr/src/gmock)
+endif()
+
+if(NOT GMOCK_MAIN_LIBRARIES)
+  find_path(GMOCK_MAIN_SRC_DIR NAMES gmock_main.cc PATHS /usr/src/gmock)
+endif()
+
+if (GTEST_INCLUDE_DIRS AND
+    GTEST_LIBRARIES AND
+    GTEST_MAIN_LIBRARIES AND
+    GMOCK_INCLUDE_DIRS AND
+    (GMOCK_LIBRARIES OR GMOCK_SRC_DIR) AND
+    (GMOCK_MAIN_LIBRARIES OR GMOCK_MAIN_SRC_DIR))
+
+  add_library(gtest UNKNOWN IMPORTED)
+  set_target_properties(gtest PROPERTIES
+    IMPORTED_LOCATION ${GTEST_LIBRARIES}
+    INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS})
+  target_link_libraries(gtest INTERFACE Threads::Threads)
+
+  add_library(gtest_main UNKNOWN IMPORTED)
+  set_target_properties(gtest_main PROPERTIES
+    IMPORTED_LOCATION ${GTEST_MAIN_LIBRARIES})
+  target_link_libraries(gtest_main INTERFACE gtest Threads::Threads)
+
+  if(GMOCK_LIBRARIES)
+    add_library(gmock UNKNOWN IMPORTED)
+    set_target_properties(gmock PROPERTIES
+      IMPORTED_LOCATION ${GMOCK_LIBRARIES}
+      INTERFACE_INCLUDE_DIRECTORIES ${GMOCK_INCLUDE_DIRS})
+  else()
+    add_library(gmock STATIC ${GMOCK_SRC_DIR}/gmock-all.cc)
+    target_include_directories(gmock PUBLIC ${GMOCK_INCLUDE_DIRS})
+    set(GMOCK_LIBRARIES gmock)
+  endif()
+  target_link_libraries(gmock INTERFACE gtest Threads::Threads)
+
+  if(GMOCK_MAIN_LIBRARIES)
+    add_library(gmock_main UNKNOWN IMPORTED)
+    set_target_properties(gmock_main PROPERTIES
+      IMPORTED_LOCATION ${GMOCK_MAIN_LIBRARIES})
+  else()
+    add_library(gmock_main STATIC ${GMOCK_MAIN_SRC_DIR}/gmock_main.cc)
+    set(GMOCK_MAIN_LIBRARIES gmock_main)
+  endif()
+  target_link_libraries(gmock_main INTERFACE gmock Threads::Threads)
+
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(GTest DEFAULT_MSG
+  GTEST_LIBRARIES
+  GTEST_MAIN_LIBRARIES
+  GTEST_INCLUDE_DIRS
+  GMOCK_LIBRARIES
+  GMOCK_MAIN_LIBRARIES
+  GMOCK_INCLUDE_DIRS)
diff --git a/cmake/modules/RootBuildOptions.cmake b/cmake/modules/RootBuildOptions.cmake
index 7886c5f3e9..01108a18c9 100644
--- a/cmake/modules/RootBuildOptions.cmake
+++ b/cmake/modules/RootBuildOptions.cmake
@@ -94,6 +94,7 @@ ROOT_BUILD_OPTION(builtin_ftgl OFF "Build bundled copy of FTGL")
 ROOT_BUILD_OPTION(builtin_gl2ps OFF "Build bundled copy of gl2ps")
 ROOT_BUILD_OPTION(builtin_glew OFF "Build bundled copy of GLEW")
 ROOT_BUILD_OPTION(builtin_gsl OFF "Build GSL internally (requires network)")
+ROOT_BUILD_OPTION(builtin_gtest OFF "Build googletest internally (requires network)")
 ROOT_BUILD_OPTION(builtin_llvm ON "Build bundled copy of LLVM")
 ROOT_BUILD_OPTION(builtin_lz4 OFF "Build bundled copy of lz4")
 ROOT_BUILD_OPTION(builtin_lzma OFF "Build bundled copy of lzma")
@@ -293,6 +294,7 @@ if(builtin_all)
   set(builtin_gl2ps_defvalue ON)
   set(builtin_glew_defvalue ON)
   set(builtin_gsl_defvalue ON)
+  set(builtin_gtest_defvalue ON)
   set(builtin_llvm_defvalue ON)
   set(builtin_lz4_defvalue ON)
   set(builtin_lzma_defvalue ON)
diff --git a/cmake/modules/RootMacros.cmake b/cmake/modules/RootMacros.cmake
index cb75c0b1da..38fd5d6baf 100644
--- a/cmake/modules/RootMacros.cmake
+++ b/cmake/modules/RootMacros.cmake
@@ -1671,7 +1671,7 @@ function(ROOT_ADD_TEST test)
 
   set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT ROOT_HIST=0)
 
-  #- Handle TIMOUT and DEPENDS arguments
+  #- Handle TIMEOUT and DEPENDS arguments
   if(ARG_TIMEOUT)
     set_property(TEST ${test} PROPERTY TIMEOUT ${ARG_TIMEOUT})
   endif()
diff --git a/cmake/modules/SearchInstalledSoftware.cmake b/cmake/modules/SearchInstalledSoftware.cmake
index fe40898d12..b6a3cc4c76 100644
--- a/cmake/modules/SearchInstalledSoftware.cmake
+++ b/cmake/modules/SearchInstalledSoftware.cmake
@@ -1860,15 +1860,6 @@ if (mpi)
   endif()
 endif()
 
-if(testing AND NO_CONNECTION)
-  if(fail-on-missing)
-    message(FATAL_ERROR "No internet connection. Please check your connection, or either disable the 'testing' option or the 'fail-on-missing' to automatically disable options requiring internet access")
-  else()
-    message(STATUS "No internet connection, disabling 'testing' option")
-    set(testing OFF CACHE BOOL "Disabled because there is no internet connection" FORCE)
-  endif()
-endif()
-
 #---Check for ZeroMQ when building RooFit::MultiProcess--------------------------------------------
 
 if (roofit_multiprocess)
@@ -1942,8 +1933,41 @@ if (roofit_multiprocess)
   target_compile_definitions(cppzmq INTERFACE ZMQ_NO_EXPORT)
 endif (roofit_multiprocess)
 
-#---Download googletest--------------------------------------------------------------
+#---Check for googletest---------------------------------------------------------------
 if (testing)
+  if (NOT builtin_gtest)
+    if(fail-on-missing)
+      find_package(GTest REQUIRED)
+    else()
+      find_package(GTest)
+      if(NOT GTEST_FOUND)
+        if(NO_CONNECTION)
+          if(fail-on-missing)
+            message(FATAL_ERROR "No internet connection and GTest was not found. Please check your connection, or either disable the 'testing' option or the 'fail-on-missing' to automatically disable options requiring internet access")
+          else()
+            message(STATUS "GTest not found, and no internet connection. Disabing the 'testing' option.")
+            set(testing OFF CACHE BOOL "Disabled because testing requested and GTest not found (${builtin_gtest_description}) and there is no internet connection" FORCE)
+          endif()
+        else()
+          message(STATUS "GTest not found, switching ON 'builtin_gtest' option.")
+          set(builtin_gtest ON CACHE BOOL "Enabled because testing requested and GTest not found (${builtin_gtest_description})" FORCE)
+        endif()
+      endif()
+    endif()
+  else()
+    if(NO_CONNECTION)
+      if(fail-on-missing)
+        message(FATAL_ERROR "No internet connection. Please check your connection, or either disable the 'testing' option or the 'builtin_gtest' option or the 'fail-on-missing' option to automatically disable options requiring internet access")
+      else()
+        message(STATUS "No internet connection, disabling the 'testing' and 'builtin_gtest' options")
+        set(testing OFF CACHE BOOL "Disabled because there is no internet connection" FORCE)
+        set(builtin_gtest OFF CACHE BOOL "Disabled because there is no internet connection" FORCE)
+      endif()
+    endif()
+  endif()
+endif()
+
+if (builtin_gtest)
   # FIXME: Remove our version of gtest in roottest. We can reuse this one.
   # Add googletest
   # http://stackoverflow.com/questions/9689183/cmake-googletest
-- 
2.35.1