|
2 | 2 |
|
3 | 3 | include_guard(GLOBAL) |
4 | 4 |
|
5 | | -include(FetchContent) |
6 | | -fetchcontent_declare( |
7 | | - GTest |
8 | | - GIT_REPOSITORY https://github.com/google/googletest.git |
9 | | - GIT_TAG v1.14.0 |
10 | | - OVERRIDE_FIND_PACKAGE) |
| 5 | +option(SUBSTRAIT_CPP_USE_SYSTEM_GTEST "Use system GTest via find_package" ON) |
| 6 | +option(SUBSTRAIT_CPP_FIND_GTEST_CONFIG |
| 7 | + "Use CONFIG mode to find system GTest via find_package" ON) |
| 8 | +option(SUBSTRAIT_CPP_FETCH_GTEST "Download GTest via FetchContent if not found" |
| 9 | + ON) |
| 10 | +set(SUBSTRAIT_CPP_GTEST_FETCH_TAG |
| 11 | + ${SUBSTRAIT_CPP_GTEST_DEFAULT_FETCH_TAG} |
| 12 | + CACHE STRING "Git tag or commit to use for GTest FetchContent") |
11 | 13 |
|
12 | | -# Disable warnings for dependency targets. |
| 14 | +# First use `find_package`. This allows downstream projects to inject their |
| 15 | +# version with `FetchContent_Declare(... OVERRIDE_FIND_PACKAGE`. |
| 16 | +if(SUBSTRAIT_CPP_USE_SYSTEM_GTEST) |
| 17 | + if(SUBSTRAIT_CPP_FIND_GTEST_CONFIG) |
| 18 | + find_package(GTest CONFIG) |
| 19 | + else() |
| 20 | + find_package(GTest) |
| 21 | + endif() |
| 22 | +endif() |
| 23 | + |
| 24 | +# Now fall back to using `FetchContent`. |
| 25 | +if(NOT GTest_FOUND AND SUBSTRAIT_CPP_FETCH_GTEST) |
| 26 | + message(STATUS "Fetching googletest version ${SUBSTRAIT_CPP_GTEST_FETCH_TAG}") |
| 27 | + include(FetchContent) |
| 28 | + fetchcontent_declare( |
| 29 | + googletest |
| 30 | + GIT_REPOSITORY https://github.com/google/googletest.git |
| 31 | + GIT_TAG ${SUBSTRAIT_CPP_GTEST_FETCH_TAG} |
| 32 | + SYSTEM OVERRIDE_FIND_PACKAGE CMAKE_ARGS |
| 33 | + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} |
| 34 | + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}) |
| 35 | + if(MSVC) |
| 36 | + set(gtest_force_shared_crt |
| 37 | + ON |
| 38 | + CACHE BOOL "" FORCE) |
| 39 | + add_compile_options("/W0") |
| 40 | + else() |
| 41 | + add_compile_options("-w") |
| 42 | + endif() |
| 43 | + fetchcontent_makeavailable(googletest) |
| 44 | + fetchcontent_getproperties(googletest SOURCE_DIR GTest_SOURCE_DIR) |
| 45 | + if(TARGET gmock AND NOT TARGET GTest::gmock) |
| 46 | + add_library(GTest::gmock ALIAS gmock) |
| 47 | + endif() |
| 48 | + if(TARGET gmock_main AND NOT TARGET GTest::gmock_main) |
| 49 | + add_library(GTest::gmock_main ALIAS gmock_main) |
| 50 | + endif() |
| 51 | +endif() |
| 52 | + |
| 53 | +if(NOT TARGET GTest::gmock) |
| 54 | + message(FATAL_ERROR "GTest is required but was not found or fetched.") |
| 55 | +endif() |
| 56 | + |
| 57 | +# XXX: Verify if this is still necessary. |
13 | 58 | if(MSVC) |
14 | | - set(gtest_force_shared_crt ON) |
15 | | - add_compile_options("/W0") |
16 | | -else() |
17 | | - add_compile_options("-w") |
| 59 | + # ------------------------------------------------------------------------------ |
| 60 | + # gtest MSVC fix |
| 61 | + # ------------------------------------------------------------------------------ |
| 62 | + # For some reason, googletest has include path issues when built with MSVC. |
| 63 | + # Specifically, this seems like some incorrect assumptions about include paths |
| 64 | + # inside the gmock project. |
| 65 | + # We can fix this by injecting the include paths here. |
| 66 | + function(fix_gtest_include TARGET) |
| 67 | + target_include_directories( |
| 68 | + ${TARGET} |
| 69 | + PUBLIC $<BUILD_INTERFACE:${gtest_SOURCE_DIR}> |
| 70 | + $<BUILD_INTERFACE:${gtest_SOURCE_DIR}/include> |
| 71 | + $<BUILD_INTERFACE:${gtest_SOURCE_DIR}/googletest> |
| 72 | + $<BUILD_INTERFACE:${gtest_SOURCE_DIR}/googletest/include> |
| 73 | + $<INSTALL_INTERFACE:include/${TARGET}>) |
| 74 | + endfunction() |
| 75 | + set(gtest_erroneous_targets gmock gmock_main) |
| 76 | + foreach(target ${gtest_erroneous_targets}) |
| 77 | + fix_gtest_include(${target}) |
| 78 | + endforeach() |
18 | 79 | endif() |
19 | | -fetchcontent_makeavailable(GTest) |
|
0 commit comments