diff options
Diffstat (limited to 'Modules/FindGTest.cmake')
-rw-r--r-- | Modules/FindGTest.cmake | 102 |
1 files changed, 71 insertions, 31 deletions
diff --git a/Modules/FindGTest.cmake b/Modules/FindGTest.cmake index 53cab1a..8e22f79 100644 --- a/Modules/FindGTest.cmake +++ b/Modules/FindGTest.cmake @@ -7,10 +7,24 @@ FindGTest Locate the Google C++ Testing Framework. +.. versionadded:: 3.20 + Upstream ``GTestConfig.cmake`` is used if possible. + Imported targets ^^^^^^^^^^^^^^^^ -This module defines the following :prop_tgt:`IMPORTED` targets: +.. versionadded:: 3.20 + This module defines the following :prop_tgt:`IMPORTED` targets: + +``GTest::gtest`` + The Google Test ``gtest`` library, if found; adds Thread::Thread + automatically +``GTest::gtest_main`` + The Google Test ``gtest_main`` library, if found + +.. deprecated:: 3.20 + For backwards compatibility, this module defines additionally the + following deprecated :prop_tgt:`IMPORTED` targets (available since 3.5): ``GTest::GTest`` The Google Test ``gtest`` library, if found; adds Thread::Thread @@ -24,7 +38,7 @@ Result variables This module will set the following variables in your project: -``GTEST_FOUND`` +``GTest_FOUND`` Found the Google Testing framework ``GTEST_INCLUDE_DIRS`` the directory containing the Google Test headers @@ -62,7 +76,7 @@ Example usage find_package(GTest REQUIRED) add_executable(foo foo.cc) - target_link_libraries(foo GTest::GTest GTest::Main) + target_link_libraries(foo GTest::gtest GTest::gtest_main) add_test(AllTestsInFoo foo) @@ -72,6 +86,10 @@ Deeper integration with CTest See :module:`GoogleTest` for information on the :command:`gtest_add_tests` and :command:`gtest_discover_tests` commands. + +.. versionchanged:: 3.9 + Previous CMake versions defined :command:`gtest_add_tests` macro in this + module. #]=======================================================================] include(${CMAKE_CURRENT_LIST_DIR}/GoogleTest.cmake) @@ -146,8 +164,41 @@ function(__gtest_import_library _target _var _config) endif() endfunction() +function(__gtest_define_backwards_compatible_library_targets) + set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} PARENT_SCOPE) + + # Add targets mapping the same library names as defined in + # older versions of CMake's FindGTest + if(NOT TARGET GTest::GTest) + add_library(GTest::GTest INTERFACE IMPORTED) + target_link_libraries(GTest::GTest INTERFACE GTest::gtest) + endif() + if(NOT TARGET GTest::Main) + add_library(GTest::Main INTERFACE IMPORTED) + target_link_libraries(GTest::Main INTERFACE GTest::gtest_main) + endif() +endfunction() + # +include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) + +# first specifically look for the CMake version of GTest +find_package(GTest QUIET NO_MODULE) + +# if we found the GTest cmake package then we are done, and +# can print what we found and return. +if(GTest_FOUND) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest HANDLE_COMPONENTS CONFIG_MODE) + + set(GTEST_LIBRARIES GTest::gtest) + set(GTEST_MAIN_LIBRARIES GTest::gtest_main) + + __gtest_define_backwards_compatible_library_targets() + + return() +endif() + if(NOT DEFINED GTEST_MSVC_SEARCH) set(GTEST_MSVC_SEARCH MD) endif() @@ -201,54 +252,43 @@ else() __gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind) endif() -include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY) -if(GTEST_FOUND) +if(GTest_FOUND) set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR}) __gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY) __gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY) - set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) find_package(Threads QUIET) - if(NOT TARGET GTest::GTest) + if(NOT TARGET GTest::gtest) __gtest_determine_library_type(GTEST_LIBRARY) - add_library(GTest::GTest ${GTEST_LIBRARY_TYPE} IMPORTED) + add_library(GTest::gtest ${GTEST_LIBRARY_TYPE} IMPORTED) if(TARGET Threads::Threads) - set_target_properties(GTest::GTest PROPERTIES + set_target_properties(GTest::gtest PROPERTIES INTERFACE_LINK_LIBRARIES Threads::Threads) endif() if(GTEST_LIBRARY_TYPE STREQUAL "SHARED") - set_target_properties(GTest::GTest PROPERTIES + set_target_properties(GTest::gtest PROPERTIES INTERFACE_COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") endif() if(GTEST_INCLUDE_DIRS) - set_target_properties(GTest::GTest PROPERTIES + set_target_properties(GTest::gtest PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIRS}") endif() - __gtest_import_library(GTest::GTest GTEST_LIBRARY "") - __gtest_import_library(GTest::GTest GTEST_LIBRARY "RELEASE") - __gtest_import_library(GTest::GTest GTEST_LIBRARY "DEBUG") + __gtest_import_library(GTest::gtest GTEST_LIBRARY "") + __gtest_import_library(GTest::gtest GTEST_LIBRARY "RELEASE") + __gtest_import_library(GTest::gtest GTEST_LIBRARY "DEBUG") endif() - if(NOT TARGET GTest::Main) + if(NOT TARGET GTest::gtest_main) __gtest_determine_library_type(GTEST_MAIN_LIBRARY) - add_library(GTest::Main ${GTEST_MAIN_LIBRARY_TYPE} IMPORTED) - set_target_properties(GTest::Main PROPERTIES - INTERFACE_LINK_LIBRARIES "GTest::GTest") - __gtest_import_library(GTest::Main GTEST_MAIN_LIBRARY "") - __gtest_import_library(GTest::Main GTEST_MAIN_LIBRARY "RELEASE") - __gtest_import_library(GTest::Main GTEST_MAIN_LIBRARY "DEBUG") + add_library(GTest::gtest_main ${GTEST_MAIN_LIBRARY_TYPE} IMPORTED) + set_target_properties(GTest::gtest_main PROPERTIES + INTERFACE_LINK_LIBRARIES "GTest::gtest") + __gtest_import_library(GTest::gtest_main GTEST_MAIN_LIBRARY "") + __gtest_import_library(GTest::gtest_main GTEST_MAIN_LIBRARY "RELEASE") + __gtest_import_library(GTest::gtest_main GTEST_MAIN_LIBRARY "DEBUG") endif() - # Add targets mapping the same library names as defined in - # GTest's CMake package config. - if(NOT TARGET GTest::gtest) - add_library(GTest::gtest INTERFACE IMPORTED) - target_link_libraries(GTest::gtest INTERFACE GTest::GTest) - endif() - if(NOT TARGET GTest::gtest_main) - add_library(GTest::gtest_main INTERFACE IMPORTED) - target_link_libraries(GTest::gtest_main INTERFACE GTest::Main) - endif() + __gtest_define_backwards_compatible_library_targets() endif() |