forked from ouster-lidar/ouster-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindGTest.cmake
50 lines (40 loc) · 1.84 KB
/
FindGTest.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Attempt to deal with gtest cmake differences across platforms
function(find_gtest)
# use system find module in this scope
set(CMAKE_MODULE_PATH "")
# using the cmake-provided find module succeeds, but the resulting GTest::GTest and
# GTest::Main targets cause link errors with vcpkg 2021.05.12 on macos. Try CONFIG-only first
find_package(GTest CONFIG QUIET)
if (GTest_CONFIG AND TARGET GTest::gtest AND TARGET GTest::gtest_main)
find_package_handle_standard_args(GTest DEFAULT_MSG GTest_CONFIG)
return()
endif()
# next, try using a find module. Requires temporarily turning off REQUIRED to avoid failing at the
# call to find_package()
set(GTest_FIND_REQUIRED_SAVED ${GTest_FIND_REQUIRED})
set(GTest_FIND_REQUIRED 0)
find_package(GTest MODULE QUIET)
set(GTest_FIND_REQUIRED ${GTest_FIND_REQUIRED_SAVED})
if (GTEST_FOUND)
find_package_handle_standard_args(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_MAIN_LIBRARY)
# aliases to cmake >= 3.20 targets. More portable than IMPORTED_GLOBAL
# https://cmake.org/cmake/help/v3.22/module/FindGTest.html#imported-targets
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()
return()
endif()
# finally, try src location for libgtest-dev for debian-based distros where
# the find module appears to be broken (xenial, bionic)
find_path(GTEST_ROOT CMakeLists.txt "/usr/src/gtest")
find_package_handle_standard_args(GTest DEFAULT_MSG GTEST_ROOT)
add_subdirectory(${GTEST_ROOT} gtest)
add_library(GTest::gtest ALIAS gtest)
add_library(GTest::gtest_main ALIAS gtest_main)
endfunction()
find_gtest()