about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJulian Büning <julian.buening@rwth-aachen.de>2022-01-03 21:42:36 +0100
committerMartinNowack <2443641+MartinNowack@users.noreply.github.com>2022-01-05 20:22:12 +0000
commit9ead1dbc2a5f450a26ff1a2536c733758b2ec578 (patch)
tree21604a3016b25b62ac4a6565dd741c58fd728fd3
parentb6889b7a31b6e5f05600b2b49e36b02e99fc5d5c (diff)
downloadklee-9ead1dbc2a5f450a26ff1a2536c733758b2ec578.tar.gz
CMake: set Google Test options properly
If the used CMake version is recent enough to support the necessary
policy CMP0077, set INSTALL_GTEST using normal variables instead of
cache variables. We never want to allow Google Test to add to the
install target, so we should not allow a user to interfere with this.

In addition, we set BUILD_GMOCK=OFF (and the necessary BUILD_GTEST=ON
for Google Test 1.8.0), as our current tests do not require Google Mock.
-rw-r--r--unittests/CMakeLists.txt43
1 files changed, 37 insertions, 6 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index da041e28..5311b834 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -7,6 +7,42 @@
 #
 #===------------------------------------------------------------------------===#
 
+function(add_vanilla_googletest_subdirectory directory)
+  if (POLICY CMP0077)
+    # Prevent Google Test from adding to our install target (Google Test 1.8.0+)
+    # However, this can only be disabled starting with 1.8.1 (a.k.a. 1.9.0)
+    set(INSTALL_GTEST OFF)
+
+    # Google Mock is currently not used by our tests
+    set(BUILD_GMOCK OFF)
+
+    # (only) Google Test 1.8.0 with BUILD_GMOCK=OFF needs BUILD_GTEST=ON
+    set(BUILD_GTEST ON)
+
+    # Make option() in subdirectory respect normal variables (as set above).
+    # NOTE: The enclosing function limits the scope of this policy setting.
+    # FIXME: Remove once all supported Google Test versions require CMake 3.13+
+    #        (or set policy CMP0077 to NEW by themselves)
+    set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
+  else()
+    # FIXME: Remove with CMake minimum version 3.13+
+    # Use cache variable workaround as option() ignores normal variables
+
+    # Prevent Google Test from adding to our install target (Google Test 1.8.0+)
+    # However, this can only be disabled starting with 1.8.1 (a.k.a. 1.9.0)
+    set(INSTALL_GTEST OFF CACHE BOOL "disable installing Google Test" FORCE)
+
+    # Google Mock is currently not used by our tests
+    set(BUILD_GMOCK OFF CACHE BOOL "disable building Google Mock" FORCE)
+
+    # (only) Google Test 1.8.0 with BUILD_GMOCK=OFF needs BUILD_GTEST=ON
+    set(BUILD_GTEST ON CACHE BOOL "enable building Google Test" FORCE)
+  endif()
+
+  # include Google Test's CMakeLists.txt
+  add_subdirectory(${directory} "${CMAKE_CURRENT_BINARY_DIR}/gtest_build")
+endfunction()
+
 if (TARGET gtest AND TARGET gtest_main)
   # try to reuse LLVM's targets
 
@@ -115,12 +151,7 @@ else()
     message(STATUS "Google Test: Building from source.")
     message(STATUS "GTEST_SRC_DIR: ${GTEST_SRC_DIR}")
 
-    # Prevent Google Test from adding to our install target.
-    # Required for >= 1.8.0, but can only be disabled starting with 1.8.1
-    set(INSTALL_GTEST OFF CACHE BOOL "disable installing Google Test" FORCE)
-
-    # Build Google Test as part of our project
-    add_subdirectory(${GTEST_SRC_DIR} "${CMAKE_CURRENT_BINARY_DIR}/gtest_build")
+    add_vanilla_googletest_subdirectory(${GTEST_SRC_DIR})
   endif()
 
   # build Google Test with KLEE's defines and compile flags