Skip to content

Commit

Permalink
Don't run compile tests unless package is found
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PeterBowman committed Feb 19, 2018
1 parent 77d5da2 commit 6063082
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions cmake/FindGTestSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gtest/gtest.h>
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 <gtest/gtest.h>
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 <gtest/gtest.h>
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 <gtest/gtest.h>
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 <gtest/gtest.h>
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 <gtest/gtest.h>
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)

Expand Down

0 comments on commit 6063082

Please sign in to comment.