Add external project to install gtest (#6229)
authorGereon Kremer <gereon.kremer@cs.rwth-aachen.de>
Mon, 29 Mar 2021 22:24:46 +0000 (00:24 +0200)
committerGitHub <noreply@github.com>
Mon, 29 Mar 2021 22:24:46 +0000 (22:24 +0000)
This PR enables us to build gtest ourselves if it is not already installed.

cmake/FindGTest.cmake [new file with mode: 0644]
test/unit/CMakeLists.txt

diff --git a/cmake/FindGTest.cmake b/cmake/FindGTest.cmake
new file mode 100644 (file)
index 0000000..786a51b
--- /dev/null
@@ -0,0 +1,85 @@
+#####################
+## FindGTest.cmake
+## Top contributors (to current version):
+##   Gereon Kremer
+## This file is part of the CVC4 project.
+## Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
+## in the top-level source directory and their institutional affiliations.
+## All rights reserved.  See the file COPYING in the top-level source
+## directory for licensing information.
+##
+#
+# Find GTest
+
+include(deps-helper)
+
+find_path(GTest_INCLUDE_DIR NAMES gtest/gtest.h)
+find_library(GTest_LIBRARIES NAMES gtest)
+find_library(GTest_MAIN_LIBRARIES NAMES gtest_main)
+
+set(GTest_FOUND_SYSTEM FALSE)
+if(GTest_INCLUDE_DIR AND GTest_LIBRARIES AND GTest_MAIN_LIBRARIES)
+    set(GTest_FOUND_SYSTEM TRUE)
+endif()
+
+if(NOT GTest_FOUND_SYSTEM)
+    include(ExternalProject)
+
+    set(GTest_VERSION "1.10.0")
+
+    ExternalProject_Add(
+        GTest-EP
+        PREFIX ${DEPS_PREFIX}
+        URL https://github.com/google/googletest/archive/refs/tags/release-${GTest_VERSION}.tar.gz
+        URL_HASH SHA1=9c89be7df9c5e8cb0bc20b3c4b39bf7e82686770
+        DOWNLOAD_NAME gtest.tar.gz
+        CMAKE_ARGS
+          -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
+          -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
+        BUILD_COMMAND ${CMAKE_COMMAND} --build .
+            --config ${CMAKE_BUILD_TYPE} --target gtest
+        COMMAND ${CMAKE_COMMAND} --build .
+            --config ${CMAKE_BUILD_TYPE} --target gtest_main
+        BUILD_BYPRODUCTS
+            <INSTALL_DIR>/lib/libgtest.a
+            <INSTALL_DIR>/lib/libgtest_main.a
+    )
+
+    set(GTest_INCLUDE_DIR "${DEPS_BASE}/include/")
+    set(GTest_LIBRARIES "${DEPS_BASE}/lib/libgtest.a")
+    set(GTest_MAIN_LIBRARIES "${DEPS_BASE}/lib/libgtest_main.a")
+endif()
+
+set(GTest_FOUND TRUE)
+
+add_library(GTest::GTest STATIC IMPORTED GLOBAL)
+set_target_properties(GTest::GTest PROPERTIES
+    IMPORTED_LOCATION "${GTest_LIBRARIES}"
+    INTERFACE_INCLUDE_DIRECTORIES "${GTest_INCLUDE_DIR}"
+)
+
+add_library(GTest::Main STATIC IMPORTED GLOBAL)
+set_target_properties(GTest::Main PROPERTIES
+    IMPORTED_LOCATION "${GTest_MAIN_LIBRARIES}"
+    INTERFACE_INCLUDE_DIRECTORIES "${GTest_INCLUDE_DIR}"
+)
+
+find_package(Threads QUIET)
+if(TARGET Threads::Threads)
+    set_target_properties(GTest::GTest PROPERTIES
+        INTERFACE_LINK_LIBRARIES Threads::Threads)
+endif()
+
+mark_as_advanced(GTest_FOUND)
+mark_as_advanced(GTest_FOUND_SYSTEM)
+mark_as_advanced(GTest_INCLUDE_DIR)
+mark_as_advanced(GTest_LIBRARIES)
+mark_as_advanced(GTest_MAIN_LIBRARIES)
+
+if(GTest_FOUND_SYSTEM)
+    message(STATUS "Found GTest ${GTest_VERSION}: ${GTest_LIBRARIES}")
+else()
+    message(STATUS "Building GTest ${GTest_VERSION}: ${GTest_LIBRARIES}")
+    add_dependencies(GTest::GTest GTest-EP)
+    add_dependencies(GTest::Main GTest-EP)
+endif()
index f63f3f3157cdf680a7016f13009a47984bc32db4..7163b5a12e7a2454acf3fccedeb12e62149c348a 100644 (file)
@@ -38,8 +38,8 @@ macro(cvc4_add_unit_test is_white name output_dir)
   target_compile_definitions(${name} PRIVATE ${CVC4_UNIT_TEST_FLAGS_BLACK})
   gtest_add_tests(TARGET ${name})
   target_link_libraries(${name} main-test)
-  target_link_libraries(${name} GTest::GTest)
   target_link_libraries(${name} GTest::Main)
+  target_link_libraries(${name} GTest::GTest)
   if(USE_POLY)
     # We don't link against libpoly, because CVC4 is already linked against it.
     target_include_directories(${name} PRIVATE ${POLY_INCLUDE_DIR})