From 7b69999c37368ceaf41e79fa7d3b05ab4bd1e8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= Date: Mon, 6 Mar 2023 03:35:35 +0100 Subject: [PATCH] Reuse gtest on system (#1493) * Reuse gtest on the system Try to use gtest on the system if found. Avoid using recent CMake features. * Set CMP0135 only when using FetchContent * Add /bigobj option for MSVC * Support also cmake between 3.14 and 3.20 Older versions provided only GTest::Main target, not currently used gtest_main. Provide backward compatibility also to old cmake versions. * Remove redundant variable checking --------- Co-authored-by: Jiwoo Park --- test/CMakeLists.txt | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index eb97912..60e410c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,20 +1,31 @@ -cmake_policy(SET CMP0135 NEW) +find_package(GTest) -include(FetchContent) -include(GoogleTest) +if(GTest_FOUND) + if(NOT TARGET GTest::gtest_main AND TARGET GTest::Main) + # CMake <3.20 + add_library(GTest::gtest_main INTERFACE IMPORTED) + target_link_libraries(GTest::gtest_main INTERFACE GTest::Main) + endif() +else() + if(POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) + endif() -set(BUILD_GMOCK OFF) -set(INSTALL_GTEST OFF) -set(gtest_force_shared_crt ON) + include(FetchContent) -FetchContent_Declare( - gtest - URL https://github.com/google/googletest/archive/main.tar.gz -) -FetchContent_MakeAvailable(gtest) + set(BUILD_GMOCK OFF) + set(INSTALL_GTEST OFF) + set(gtest_force_shared_crt ON) + + FetchContent_Declare( + gtest + URL https://github.com/google/googletest/archive/main.tar.gz + ) + FetchContent_MakeAvailable(gtest) +endif() add_executable(httplib-test test.cc) -target_compile_options(httplib-test PRIVATE "$<$:/utf-8>") +target_compile_options(httplib-test PRIVATE "$<$:/utf-8;/bigobj>") target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main) gtest_discover_tests(httplib-test)