about summary refs log tree commit diff homepage
path: root/unittests/CMakeLists.txt
diff options
context:
space:
mode:
authorJulian Büning <julian.buening@rwth-aachen.de>2022-01-04 20:32:30 +0100
committerMartinNowack <2443641+MartinNowack@users.noreply.github.com>2022-01-05 20:22:12 +0000
commitacb90dc2d8cdf104740099a2d8773f5022194622 (patch)
tree221dced9ae763a3d2ece258b84b59eda006f1ad1 /unittests/CMakeLists.txt
parent9ead1dbc2a5f450a26ff1a2536c733758b2ec578 (diff)
downloadklee-acb90dc2d8cdf104740099a2d8773f5022194622.tar.gz
fix CMake: use own TestMain instead of gtest_main
Before, we would use gtest_main as provided by either LLVM or Google
Test itself; the TestMain.cpp was ignored (contrary to its source code
comment). In newer versions of Google Test (1.8.1+), gtest_main uses
`__FILE__` for its "Running main() from" line, but llvm-lit (which we
use to invoke unit tests) currently matches exactly "Running main()
from gtest_main.cc" for determining whether to skip this line. This
results in spurious "tests" that will be shown as unresolved.
Diffstat (limited to 'unittests/CMakeLists.txt')
-rw-r--r--unittests/CMakeLists.txt56
1 files changed, 30 insertions, 26 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index 5311b834..d21e6ad2 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -43,15 +43,14 @@ function(add_vanilla_googletest_subdirectory directory)
   add_subdirectory(${directory} "${CMAKE_CURRENT_BINARY_DIR}/gtest_build")
 endfunction()
 
-if (TARGET gtest AND TARGET gtest_main)
-  # try to reuse LLVM's targets
-
-  message(WARNING "LLVM exports 'gtest' and 'gtest_main' targets (for Google "
-    "Test), so KLEE cannot create them. By default, KLEE will reuse "
-    "LLVM's 'gtest' and 'gtest_main' targets if they are available. This is, "
-    "however, only recommended if LLVM and KLEE were build with the same "
-    "compiler and linker flags to prevent any compatibility issues.\n"
-    "To prevent CMake from reusing the targets or to use a different version "
+if (TARGET gtest)
+  # try to reuse LLVM's 'gtest' target
+
+  message(WARNING "LLVM exports its 'gtest' CMake target (for Google Test), so"
+    "KLEE cannot create its own. Thus, KLEE will reuse the existing one. This"
+    "is, however, only recommended if LLVM and KLEE were built using the same"
+    "compiler and linker flags (to prevent compatibility issues).\n"
+    "To prevent CMake from reusing the target or to use a different version "
     "of Google Test, try either of the following:\n"
     "- Point LLVM_DIR to the directory containing the `LLVMConfig.cmake` file "
     "of an installed copy of LLVM instead of a build tree.\n"
@@ -60,17 +59,16 @@ if (TARGET gtest AND TARGET gtest_main)
     "target to the build tree.")
 
   if (GTEST_SRC_DIR)
-    message(FATAL_ERROR "Cannot use GTEST_SRC_DIR when targets 'gtest' and "
-      "'gtest_main' are already defined.\n"
+    message(FATAL_ERROR "Cannot use GTEST_SRC_DIR when target 'gtest' is"
+      "already defined.\n"
       "Either reuse LLVM's Google Test setup by not setting GTEST_SRC_DIR or "
-      "choose one of the options to prevent LLVM from exporting these targets.")
+      "choose one of the options to prevent LLVM from exporting this target.")
   endif()
 
-  # check if it's really LLVM that exports them
+  # check if it's really LLVM that exports the 'gtest' target
   list(FIND LLVM_EXPORTED_TARGETS "gtest" _GTEST_INDEX)
-  list(FIND LLVM_EXPORTED_TARGETS "test_main" _GTEST_MAIN_INDEX)
-  if (${_GTEST_INDEX} GREATER -1 AND ${_GTEST_MAIN_INDEX})
-    message(STATUS "Google Test: Reusing LLVM's 'gtest' and 'gtest_main' targets.")
+  if (${_GTEST_INDEX} GREATER -1)
+    message(STATUS "Google Test: Reusing LLVM's 'gtest' target.")
     # in this case, only include directory has to be set
     if (LLVM_BUILD_MAIN_SRC_DIR)
       set(GTEST_INCLUDE_DIR
@@ -81,11 +79,11 @@ if (TARGET gtest AND TARGET gtest_main)
       )
     endif()
   else()
-    message(FATAL_ERROR "Reusing Google Test targets from LLVM failed:"
-      "LLVM_EXPORTED_TARGETS does not contain 'gtest' or 'gtest_main'.")
+    message(FATAL_ERROR "Reusing Google Test (target) from LLVM failed:"
+      "LLVM_EXPORTED_TARGETS does not contain 'gtest'.")
   endif()
 else()
-  # LLVM's targets are not reused
+  # LLVM's 'gtest' target is not reused
 
   if (NOT GTEST_SRC_DIR)
     if (USE_CMAKE_FIND_PACKAGE_LLVM AND LLVM_BUILD_MAIN_SRC_DIR)
@@ -107,7 +105,6 @@ else()
 
       # add includes for LLVM's modifications
       target_include_directories(gtest BEFORE PRIVATE ${LLVM_INCLUDE_DIRS})
-      target_include_directories(gtest_main BEFORE PRIVATE ${LLVM_INCLUDE_DIRS})
     else()
       # try to find Google Test, as GTEST_SRC_DIR is not manually specified
       find_path(GTEST_SRC_DIR
@@ -125,7 +122,7 @@ else()
     endif()
   endif()
 
-  if (NOT (TARGET gtest AND TARGET gtest_main))
+  if (NOT TARGET gtest)
     # building from GTEST_SRC_DIR, not from LLVM's utils directory
     find_path(GTEST_INCLUDE_DIR
       "gtest/gtest.h"
@@ -156,9 +153,7 @@ else()
 
   # build Google Test with KLEE's defines and compile flags
   target_compile_definitions(gtest PRIVATE ${KLEE_COMPONENT_CXX_DEFINES})
-  target_compile_definitions(gtest_main PRIVATE ${KLEE_COMPONENT_CXX_DEFINES})
   target_compile_options(gtest PRIVATE ${KLEE_COMPONENT_CXX_FLAGS})
-  target_compile_options(gtest_main PRIVATE ${KLEE_COMPONENT_CXX_FLAGS})
 endif()
 
 
@@ -177,11 +172,20 @@ if (NOT IS_DIRECTORY "${GTEST_INCLUDE_DIR}")
 endif()
 message(STATUS "GTEST_INCLUDE_DIR: ${GTEST_INCLUDE_DIR}")
 
+add_library(unittest_main)
+target_sources(unittest_main PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/TestMain.cpp")
+target_link_libraries(unittest_main PUBLIC gtest)
+target_include_directories(unittest_main
+  PUBLIC
+  ${GTEST_INCLUDE_DIR}
+  ${KLEE_COMPONENT_EXTRA_INCLUDE_DIRS}
+)
+target_compile_definitions(unittest_main PUBLIC ${KLEE_COMPONENT_CXX_DEFINES})
+target_compile_options(unittest_main PUBLIC ${KLEE_COMPONENT_CXX_FLAGS})
+
 function(add_klee_unit_test target_name)
   add_executable(${target_name} ${ARGN})
-  target_link_libraries(${target_name} PRIVATE gtest_main)
-  target_include_directories(${target_name} BEFORE PRIVATE "${GTEST_INCLUDE_DIR}")
-  target_include_directories(${target_name} BEFORE PRIVATE ${KLEE_COMPONENT_EXTRA_INCLUDE_DIRS})
+  target_link_libraries(${target_name} PRIVATE unittest_main)
   set_target_properties(${target_name}
     PROPERTIES
     RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/unittests/"