CMake: Remove check_target calls which can be problematic in case of dependency cycle

Also renames `_target_compile_features_if_available` CMake function to
`_absl_target_compile_features_if_available` since CMake's function
namespace is global.

The dependency cycle can occur if absl is configured with test helpers
enabled, and googletest is configured to use absl.

In the case that both projects are being built from source via
FetchContent with OVERRIDE_FIND_PACKAGE, depending on the order in
which the two projects are added to the build, the check_target calls
may fail even though the build would have otherwise succeeded.

The existing `check_target` calls seem to have been originally added
to detect missing gtest targets when the `GTest::` prefix was not yet
in use.  For target names without "::", CMake does not warn if they
are undefined, and just assumes they refer to system library names.
However, CMake does fail during build generation if a target name with
"::" is missing; thus the check_taget calls are redundant.

PiperOrigin-RevId: 480140797
Change-Id: Ic51631e4a36dd8b6f569ad6424bea15a4af0b875
pull/1298/head
Abseil Team 2 years ago committed by Copybara-Service
parent 76466b85fb
commit 2ed6963f2b
  1. 16
      CMake/AbseilHelpers.cmake
  2. 6
      CMakeLists.txt

@ -32,7 +32,7 @@ else()
set(ABSL_INTERNAL_INCLUDE_WARNING_GUARD "") set(ABSL_INTERNAL_INCLUDE_WARNING_GUARD "")
endif() endif()
function(_target_compile_features_if_available TARGET TYPE FEATURE) function(_absl_target_compile_features_if_available TARGET TYPE FEATURE)
if(FEATURE IN_LIST CMAKE_CXX_COMPILE_FEATURES) if(FEATURE IN_LIST CMAKE_CXX_COMPILE_FEATURES)
target_compile_features(${TARGET} ${TYPE} ${FEATURE}) target_compile_features(${TARGET} ${TYPE} ${FEATURE})
else() else()
@ -300,7 +300,7 @@ Cflags: -I\${includedir}${PC_CFLAGS}\n")
# Abseil libraries require C++14 as the current minimum standard. # Abseil libraries require C++14 as the current minimum standard.
# Top-level application CMake projects should ensure a consistent C++ # Top-level application CMake projects should ensure a consistent C++
# standard for all compiled sources by setting CMAKE_CXX_STANDARD. # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE}) _absl_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE})
else() else()
# Note: This is legacy (before CMake 3.8) behavior. Setting the # Note: This is legacy (before CMake 3.8) behavior. Setting the
# target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
@ -348,7 +348,7 @@ Cflags: -I\${includedir}${PC_CFLAGS}\n")
# Abseil libraries require C++14 as the current minimum standard. # Abseil libraries require C++14 as the current minimum standard.
# Top-level application CMake projects should ensure a consistent C++ # Top-level application CMake projects should ensure a consistent C++
# standard for all compiled sources by setting CMAKE_CXX_STANDARD. # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
_target_compile_features_if_available(${_NAME} INTERFACE ${ABSL_INTERNAL_CXX_STD_FEATURE}) _absl_target_compile_features_if_available(${_NAME} INTERFACE ${ABSL_INTERNAL_CXX_STD_FEATURE})
# (INTERFACE libraries can't have the CXX_STANDARD property set, so there # (INTERFACE libraries can't have the CXX_STANDARD property set, so there
# is no legacy behavior else case). # is no legacy behavior else case).
@ -460,7 +460,7 @@ function(absl_cc_test)
# Abseil libraries require C++14 as the current minimum standard. # Abseil libraries require C++14 as the current minimum standard.
# Top-level application CMake projects should ensure a consistent C++ # Top-level application CMake projects should ensure a consistent C++
# standard for all compiled sources by setting CMAKE_CXX_STANDARD. # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE}) _absl_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE})
else() else()
# Note: This is legacy (before CMake 3.8) behavior. Setting the # Note: This is legacy (before CMake 3.8) behavior. Setting the
# target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
@ -476,11 +476,3 @@ function(absl_cc_test)
add_test(NAME ${_NAME} COMMAND ${_NAME}) add_test(NAME ${_NAME} COMMAND ${_NAME})
endfunction() endfunction()
function(check_target my_target)
if(NOT TARGET ${my_target})
message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project,
see CMake/README.md for more details")
endif(NOT TARGET ${my_target})
endfunction()

@ -140,7 +140,6 @@ set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
) )
if((BUILD_TESTING AND ABSL_BUILD_TESTING) OR ABSL_BUILD_TEST_HELPERS) if((BUILD_TESTING AND ABSL_BUILD_TESTING) OR ABSL_BUILD_TEST_HELPERS)
## check targets
if (ABSL_USE_EXTERNAL_GOOGLETEST) if (ABSL_USE_EXTERNAL_GOOGLETEST)
if (ABSL_FIND_GOOGLETEST) if (ABSL_FIND_GOOGLETEST)
find_package(GTest REQUIRED) find_package(GTest REQUIRED)
@ -172,11 +171,6 @@ if((BUILD_TESTING AND ABSL_BUILD_TESTING) OR ABSL_BUILD_TEST_HELPERS)
endif() endif()
include(CMake/Googletest/DownloadGTest.cmake) include(CMake/Googletest/DownloadGTest.cmake)
endif() endif()
check_target(GTest::gtest)
check_target(GTest::gtest_main)
check_target(GTest::gmock)
check_target(GTest::gmock_main)
endif() endif()
add_subdirectory(absl) add_subdirectory(absl)

Loading…
Cancel
Save