diff --git a/CMakeLists.txt b/CMakeLists.txt index ff49ac1e..5bd3415a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -134,8 +134,14 @@ if(BUILD_TESTING AND ABSL_BUILD_TESTING) if (ABSL_USE_EXTERNAL_GOOGLETEST) if (ABSL_FIND_GOOGLETEST) find_package(GTest REQUIRED) - else() - if (NOT TARGET gtest AND NOT TARGET GTest::gtest) + elseif(NOT TARGET GTest::gtest) + if(TARGET gtest) + # When Google Test is included directly rather than through find_package, the aliases are missing. + add_library(GTest::gtest ALIAS gtest) + add_library(GTest::gtest_main ALIAS gtest_main) + add_library(GTest::gmock ALIAS gmock) + add_library(GTest::gmock_main ALIAS gmock_main) + else() message(FATAL_ERROR "ABSL_USE_EXTERNAL_GOOGLETEST is ON and ABSL_FIND_GOOGLETEST is OFF, which means that the top-level project must build the Google Test project. However, the target gtest was not found.") endif() endif() @@ -157,14 +163,6 @@ if(BUILD_TESTING AND ABSL_BUILD_TESTING) include(CMake/Googletest/DownloadGTest.cmake) endif() - if (NOT ABSL_FIND_GOOGLETEST) - # When Google Test is included directly rather than through find_package, the aliases are missing. - add_library(GTest::gtest ALIAS gtest) - add_library(GTest::gtest_main ALIAS gtest_main) - add_library(GTest::gmock ALIAS gmock) - add_library(GTest::gmock_main ALIAS gmock_main) - endif() - check_target(GTest::gtest) check_target(GTest::gtest_main) check_target(GTest::gmock)