about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorLukáš Zaoral <lzaoral@redhat.com>2022-04-21 11:44:58 +0200
committerCristian Cadar <c.cadar@imperial.ac.uk>2022-05-05 14:26:40 -0700
commit0ceea620c91c53a5167af83deddc97571bb67808 (patch)
treea5483457fc2ca347ff57da8eadf9f18af07c791f
parent5536893ba57604dac1cc2a527e3137336bb96ae6 (diff)
downloadklee-0ceea620c91c53a5167af83deddc97571bb67808.tar.gz
cmake: try using system installation of GTest if it's present
This is a patch that I made few months ago as Fedora forbids bundling and
using pieces of software provided by other packages in its repositories but
forgot to upstream it at that time. [1]

It has been rebased and improved so that it also reflects changes made
in #1458.

This should also make the compilation of unittests easier for our users
as they don't need to clone googletest from GitHub anymore and just use
package manager in the distro of their choice, provided that the gtest
package includes a corresponding CMake module.

[1]: https://src.fedoraproject.org/rpms/klee/blob/4c81b78/f/use-system-gtest.patch
-rw-r--r--unittests/CMakeLists.txt39
1 files changed, 31 insertions, 8 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index 51151141..4ee90146 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -107,6 +107,12 @@ else()
       target_include_directories(gtest BEFORE PRIVATE ${LLVM_INCLUDE_DIRS})
       # we cannot disable gtest_main, but will not use it later
       target_include_directories(gtest_main BEFORE PRIVATE ${LLVM_INCLUDE_DIRS})
+    endif()
+
+    # try using system installation of GTest instead
+    find_package(GTest QUIET)
+    if (GTest_FOUND)
+      message(STATUS "Found GTest ${GTest_VERSION}")
     else()
       # try to find Google Test, as GTEST_SRC_DIR is not manually specified
       find_path(GTEST_SRC_DIR
@@ -124,7 +130,7 @@ else()
     endif()
   endif()
 
-  if (NOT TARGET gtest)
+  if (NOT TARGET gtest AND NOT GTest_FOUND)
     # building from GTEST_SRC_DIR, not from LLVM's utils directory
     find_path(GTEST_INCLUDE_DIR
       "gtest/gtest.h"
@@ -153,9 +159,11 @@ else()
     add_vanilla_googletest_subdirectory(${GTEST_SRC_DIR})
   endif()
 
-  # build Google Test with KLEE's defines and compile flags
-  target_compile_definitions(gtest PRIVATE ${KLEE_COMPONENT_CXX_DEFINES})
-  target_compile_options(gtest PRIVATE ${KLEE_COMPONENT_CXX_FLAGS})
+  if (NOT GTest_FOUND)
+    # build Google Test with KLEE's defines and compile flags
+    target_compile_definitions(gtest PRIVATE ${KLEE_COMPONENT_CXX_DEFINES})
+    target_compile_options(gtest PRIVATE ${KLEE_COMPONENT_CXX_FLAGS})
+  endif()
 endif()
 
 
@@ -168,18 +176,33 @@ define_property(GLOBAL
   FULL_DOCS "KLEE unit tests"
 )
 
-if (NOT IS_DIRECTORY "${GTEST_INCLUDE_DIR}")
+if (NOT GTest_FOUND)
+  # GTEST_INCLUDE_DIR is defined only when we build from sources.
+  if (NOT IS_DIRECTORY "${GTEST_INCLUDE_DIR}")
+    message(FATAL_ERROR
+      "Cannot find Google Test include directory \"${GTEST_INCLUDE_DIR}\"")
+  endif()
+  message(STATUS "GTEST_INCLUDE_DIR: ${GTEST_INCLUDE_DIR}")
+endif()
+
+if (TARGET gtest)
+  set(GTEST_TARGET_NAME gtest)
+elseif (TARGET GTest::gtest)
+  set(GTEST_TARGET_NAME GTest::gtest)
+elseif (TARGET GTest::GTest)
+  set(GTEST_TARGET_NAME GTest::GTest)
+else()
   message(FATAL_ERROR
-    "Cannot find Google Test include directory \"${GTEST_INCLUDE_DIR}\"")
+    "Cannot determine name of the Google Test CMake target (tried 'gtest', \
+    'GTest::gtest' and 'GTest::GTest')")
 endif()
-message(STATUS "GTEST_INCLUDE_DIR: ${GTEST_INCLUDE_DIR}")
 
 add_library(unittest_main)
 target_sources(unittest_main PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/TestMain.cpp")
 klee_get_llvm_libs(UNITTEST_MAIN_LIBS Support)
 target_link_libraries(unittest_main
   PUBLIC
-  gtest
+  ${GTEST_TARGET_NAME}
 
   PRIVATE
   ${UNITTEST_MAIN_LIBS}