From 9fd9e448d0e57fecb1fea2e27c2c89dabc2ad23d Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Thu, 14 Sep 2017 12:28:26 -0400 Subject: FindGTest: Avoid using find_dependency in a find module The `find_dependency` macro is not meant for use in find modules. Instead use plain `find_package` for the Threads package. Assume that if it is not found then it isn't needed on the current platform. Issue: #17257 --- Modules/FindGTest.cmake | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Modules/FindGTest.cmake b/Modules/FindGTest.cmake index cb71ef1..889a68d 100644 --- a/Modules/FindGTest.cmake +++ b/Modules/FindGTest.cmake @@ -151,13 +151,14 @@ if(GTEST_FOUND) _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY) set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) - include(CMakeFindDependencyMacro) - find_dependency(Threads) + find_package(Threads QUIET) if(NOT TARGET GTest::GTest) add_library(GTest::GTest UNKNOWN IMPORTED) - set_target_properties(GTest::GTest PROPERTIES - INTERFACE_LINK_LIBRARIES "Threads::Threads") + if(TARGET Threads::Threads) + set_target_properties(GTest::GTest PROPERTIES + INTERFACE_LINK_LIBRARIES Threads::Threads) + endif() if(GTEST_INCLUDE_DIRS) set_target_properties(GTest::GTest PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIRS}") -- cgit v0.12 From 350617914596a593015f78cf42c7ed894e6a7512 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Fri, 15 Sep 2017 10:10:16 -0400 Subject: FindGTest: Fix shared linking on Windows Add logic to FindGTest.cmake to attempt to determine if the libraries are shared or static. If the libraries are shared, add an interface compile definition that is needed in some cases for successful linking on Windows. See also https://github.com/google/googletest/issues/877. --- Modules/FindGTest.cmake | 112 +++++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 44 deletions(-) diff --git a/Modules/FindGTest.cmake b/Modules/FindGTest.cmake index 889a68d..4543857 100644 --- a/Modules/FindGTest.cmake +++ b/Modules/FindGTest.cmake @@ -95,6 +95,56 @@ function(_gtest_find_library _name) mark_as_advanced(${_name}) endfunction() +macro(_gtest_determine_windows_library_type _var) + if(EXISTS "${${_var}}") + file(TO_NATIVE_PATH "${${_var}}" _lib_path) + get_filename_component(_name "${${_var}}" NAME_WE) + file(STRINGS "${${_var}}" _match REGEX "${_name}\\.dll" LIMIT_COUNT 1) + if(NOT _match STREQUAL "") + set(${_var}_TYPE SHARED PARENT_SCOPE) + else() + set(${_var}_TYPE UNKNOWN PARENT_SCOPE) + endif() + return() + endif() +endmacro() + +function(_gtest_determine_library_type _var) + if(WIN32) + # For now, at least, only Windows really needs to know the library type + _gtest_determine_windows_library_type(${_var}) + _gtest_determine_windows_library_type(${_var}_RELEASE) + _gtest_determine_windows_library_type(${_var}_DEBUG) + endif() + # If we get here, no determination was made from the above checks + set(${_var}_TYPE UNKNOWN PARENT_SCOPE) +endfunction() + +function(_gtest_import_library _target _var _config) + if(_config) + set(_config_suffix "_${_config}") + else() + set(_config_suffix "") + endif() + + set(_lib "${${_var}${_config_suffix}}") + if(EXISTS "${_lib}") + if(_config) + set_property(TARGET ${_target} APPEND PROPERTY + IMPORTED_CONFIGURATIONS ${_config}) + endif() + set_target_properties(${_target} PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES${_config_suffix} "CXX") + if(WIN32 AND ${_var}_TYPE STREQUAL SHARED) + set_target_properties(${_target} PROPERTIES + IMPORTED_IMPLIB${_config_suffix} "${_lib}") + else() + set_target_properties(${_target} PROPERTIES + IMPORTED_LOCATION${_config_suffix} "${_lib}") + endif() + endif() +endfunction() + # if(NOT DEFINED GTEST_MSVC_SEARCH) @@ -154,57 +204,31 @@ if(GTEST_FOUND) find_package(Threads QUIET) if(NOT TARGET GTest::GTest) - add_library(GTest::GTest UNKNOWN IMPORTED) + _gtest_determine_library_type(GTEST_LIBRARY) + add_library(GTest::GTest ${GTEST_LIBRARY_TYPE} IMPORTED) if(TARGET Threads::Threads) set_target_properties(GTest::GTest PROPERTIES INTERFACE_LINK_LIBRARIES Threads::Threads) endif() - if(GTEST_INCLUDE_DIRS) - set_target_properties(GTest::GTest PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIRS}") - endif() - if(EXISTS "${GTEST_LIBRARY}") + if(GTEST_LIBRARY_TYPE STREQUAL "SHARED") set_target_properties(GTest::GTest PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" - IMPORTED_LOCATION "${GTEST_LIBRARY}") + INTERFACE_COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") endif() - if(EXISTS "${GTEST_LIBRARY_RELEASE}") - set_property(TARGET GTest::GTest APPEND PROPERTY - IMPORTED_CONFIGURATIONS RELEASE) - set_target_properties(GTest::GTest PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX" - IMPORTED_LOCATION_RELEASE "${GTEST_LIBRARY_RELEASE}") - endif() - if(EXISTS "${GTEST_LIBRARY_DEBUG}") - set_property(TARGET GTest::GTest APPEND PROPERTY - IMPORTED_CONFIGURATIONS DEBUG) + if(GTEST_INCLUDE_DIRS) set_target_properties(GTest::GTest PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX" - IMPORTED_LOCATION_DEBUG "${GTEST_LIBRARY_DEBUG}") + INTERFACE_INCLUDE_DIRECTORIES "${GTEST_INCLUDE_DIRS}") endif() - endif() - if(NOT TARGET GTest::Main) - add_library(GTest::Main UNKNOWN IMPORTED) - set_target_properties(GTest::Main PROPERTIES - INTERFACE_LINK_LIBRARIES "GTest::GTest") - if(EXISTS "${GTEST_MAIN_LIBRARY}") - set_target_properties(GTest::Main PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" - IMPORTED_LOCATION "${GTEST_MAIN_LIBRARY}") - endif() - if(EXISTS "${GTEST_MAIN_LIBRARY_RELEASE}") - set_property(TARGET GTest::Main APPEND PROPERTY - IMPORTED_CONFIGURATIONS RELEASE) - set_target_properties(GTest::Main PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX" - IMPORTED_LOCATION_RELEASE "${GTEST_MAIN_LIBRARY_RELEASE}") - endif() - if(EXISTS "${GTEST_MAIN_LIBRARY_DEBUG}") - set_property(TARGET GTest::Main APPEND PROPERTY - IMPORTED_CONFIGURATIONS DEBUG) - set_target_properties(GTest::Main PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX" - IMPORTED_LOCATION_DEBUG "${GTEST_MAIN_LIBRARY_DEBUG}") - endif() + _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) + _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") endif() endif() -- cgit v0.12 From 4636c64bfe71ceaad7991dfc3a397b721a578363 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Mon, 18 Sep 2017 09:58:12 -0400 Subject: FindGTest: Improve test to catch link error Add a reference to one of Google Test's command-line flags to the FindGTest test. This will ensure that we are using the correct compile definitions on Windows, as the test will otherwise fail to link. (IOW, this tests the changes made by the previous commit.) --- Tests/FindGTest/Test/main.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/FindGTest/Test/main.cxx b/Tests/FindGTest/Test/main.cxx index 0572a5d..19d2967 100644 --- a/Tests/FindGTest/Test/main.cxx +++ b/Tests/FindGTest/Test/main.cxx @@ -2,5 +2,7 @@ TEST(FindCMake, LinksAndRuns) { + using namespace testing; + EXPECT_FALSE(GTEST_FLAG(list_tests)); ASSERT_TRUE(true); } -- cgit v0.12 From e2cca9f8ee04e9c775aee2276dd7122dcabf5dc4 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Fri, 15 Sep 2017 12:53:16 -0400 Subject: FindGTest: Avoid macro name collision Use two _'s for private function/macro names rather than one. This avoids a potential collision if a function/macro with no leading _ that otherwise matches the name of a private function/macro also overrides a built-in function or is defined twice. --- Modules/FindGTest.cmake | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/Modules/FindGTest.cmake b/Modules/FindGTest.cmake index 4543857..b0579d9 100644 --- a/Modules/FindGTest.cmake +++ b/Modules/FindGTest.cmake @@ -75,7 +75,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/GoogleTest.cmake) -function(_gtest_append_debugs _endvar _library) +function(__gtest_append_debugs _endvar _library) if(${_library} AND ${_library}_DEBUG) set(_output optimized ${${_library}} debug ${${_library}_DEBUG}) else() @@ -84,7 +84,7 @@ function(_gtest_append_debugs _endvar _library) set(${_endvar} ${_output} PARENT_SCOPE) endfunction() -function(_gtest_find_library _name) +function(__gtest_find_library _name) find_library(${_name} NAMES ${ARGN} HINTS @@ -95,7 +95,7 @@ function(_gtest_find_library _name) mark_as_advanced(${_name}) endfunction() -macro(_gtest_determine_windows_library_type _var) +macro(__gtest_determine_windows_library_type _var) if(EXISTS "${${_var}}") file(TO_NATIVE_PATH "${${_var}}" _lib_path) get_filename_component(_name "${${_var}}" NAME_WE) @@ -109,18 +109,18 @@ macro(_gtest_determine_windows_library_type _var) endif() endmacro() -function(_gtest_determine_library_type _var) +function(__gtest_determine_library_type _var) if(WIN32) # For now, at least, only Windows really needs to know the library type - _gtest_determine_windows_library_type(${_var}) - _gtest_determine_windows_library_type(${_var}_RELEASE) - _gtest_determine_windows_library_type(${_var}_DEBUG) + __gtest_determine_windows_library_type(${_var}) + __gtest_determine_windows_library_type(${_var}_RELEASE) + __gtest_determine_windows_library_type(${_var}_DEBUG) endif() # If we get here, no determination was made from the above checks set(${_var}_TYPE UNKNOWN PARENT_SCOPE) endfunction() -function(_gtest_import_library _target _var _config) +function(__gtest_import_library _target _var _config) if(_config) set(_config_suffix "_${_config}") else() @@ -181,15 +181,15 @@ mark_as_advanced(GTEST_INCLUDE_DIR) if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD") # The provided /MD project files for Google Test add -md suffixes to the # library names. - _gtest_find_library(GTEST_LIBRARY gtest-md gtest) - _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd) - _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main) - _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind) + __gtest_find_library(GTEST_LIBRARY gtest-md gtest) + __gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd) + __gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main) + __gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind) else() - _gtest_find_library(GTEST_LIBRARY gtest) - _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd) - _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main) - _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind) + __gtest_find_library(GTEST_LIBRARY gtest) + __gtest_find_library(GTEST_LIBRARY_DEBUG gtestd) + __gtest_find_library(GTEST_MAIN_LIBRARY gtest_main) + __gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind) endif() include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) @@ -197,14 +197,14 @@ FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_ 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) + __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) - _gtest_determine_library_type(GTEST_LIBRARY) + __gtest_determine_library_type(GTEST_LIBRARY) add_library(GTest::GTest ${GTEST_LIBRARY_TYPE} IMPORTED) if(TARGET Threads::Threads) set_target_properties(GTest::GTest PROPERTIES @@ -218,17 +218,17 @@ if(GTEST_FOUND) 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) - _gtest_determine_library_type(GTEST_MAIN_LIBRARY) + __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") + __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") endif() endif() -- cgit v0.12