From 6063082a298605d9202cc2ed17793b0929a2c133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20=C5=81ukawski?= Date: Mon, 19 Feb 2018 16:01:03 +0100 Subject: [PATCH] Don't run compile tests unless package is found The cmake_cxx_source_compiles() command sets internal cache vars, so they won't get overwritten on a second run. This is relevant if GTest sources are not found on system, we run the CMake configure step, install libgtest-dev, and run CMake again. Even though the package is there, internal variables prevent find_package from updating the found/not_found status. Also, pass an adequate name of the _FOUND variable to find_package_handle_standard_args(), case is relevant. --- cmake/FindGTestSources.cmake | 83 +++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/cmake/FindGTestSources.cmake b/cmake/FindGTestSources.cmake index 4c4f77102..c2d1b15ce 100644 --- a/cmake/FindGTestSources.cmake +++ b/cmake/FindGTestSources.cmake @@ -41,53 +41,58 @@ if(NOT GTestSources_INCLUDE_DIR) NO_CMAKE_ENVIRONMENT_PATH) endif() -set(_cmake_include_dirs ${CMAKE_REQUIRED_INCLUDES}) +set(GTestSources_VERSION GTestSources_VERSION-NOT_FOUND) -include(CheckCXXSourceCompiles) -list(APPEND CMAKE_REQUIRED_INCLUDES ${GTestSources_INCLUDE_DIR}) +if(GTestSources_SOURCE_DIR AND GTestSources_INCLUDE_DIR) + set(_cmake_include_dirs ${CMAKE_REQUIRED_INCLUDES}) -check_cxx_source_compiles(" - #include - int main() { - typedef const char* (testing::TestInfo::*fun)() const; - fun f = &testing::TestInfo::type_param; - return 0; - }" - _gtest_compatible_1_6_0) + include(CheckCXXSourceCompiles) + list(APPEND CMAKE_REQUIRED_INCLUDES ${GTestSources_INCLUDE_DIR}) -check_cxx_source_compiles(" - #include - int main() { - typedef bool (testing::TestInfo::*fun)() const; - fun f = &testing::TestInfo::is_reportable; - return 0; - }" - _gtest_compatible_1_7_0) + check_cxx_source_compiles(" + #include + int main() { + typedef const char* (testing::TestInfo::*fun)() const; + fun f = &testing::TestInfo::type_param; + return 0; + }" + _gtest_compatible_1_6_0) -check_cxx_source_compiles(" - #include - int main() { - typedef const char* (testing::TestInfo::*fun)() const; - fun f = &testing::TestInfo::file; - return 0; - }" - _gtest_compatible_1_8_0) + check_cxx_source_compiles(" + #include + int main() { + typedef bool (testing::TestInfo::*fun)() const; + fun f = &testing::TestInfo::is_reportable; + return 0; + }" + _gtest_compatible_1_7_0) -if(_gtest_compatible_1_8_0) - set(GTestSources_VERSION 1.8.0) -elseif(_gtest_compatible_1_7_0) - set(GTestSources_VERSION 1.7.0) -elseif(_gtest_compatible_1_6_0) - set(GTestSources_VERSION 1.6.0) -else() - message(STATUS "FindGTestSources.cmake reports unhandled GTest version (<1.6.0)") - set(GTestSources_VERSION GTestSources_VERSION-NOT_FOUND) -endif() + check_cxx_source_compiles(" + #include + int main() { + typedef const char* (testing::TestInfo::*fun)() const; + fun f = &testing::TestInfo::file; + return 0; + }" + _gtest_compatible_1_8_0) + + if(_gtest_compatible_1_8_0) + set(GTestSources_VERSION 1.8.0) + elseif(_gtest_compatible_1_7_0) + set(GTestSources_VERSION 1.7.0) + elseif(_gtest_compatible_1_6_0) + set(GTestSources_VERSION 1.6.0) + else() + message(STATUS "FindGTestSources.cmake reports unhandled GTest version (<1.6.0)") + endif() -set(CMAKE_REQUIRED_INCLUDES "${_cmake_include_dirs}") + set(CMAKE_REQUIRED_INCLUDES "${_cmake_include_dirs}") + unset(_cmake_include_dirs) +endif() include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(GTestSources REQUIRED_VARS GTestSources_SOURCE_DIR +find_package_handle_standard_args(GTestSources FOUND_VAR GTestSources_FOUND + REQUIRED_VARS GTestSources_SOURCE_DIR GTestSources_INCLUDE_DIR VERSION_VAR GTestSources_VERSION)