check_compiler_flag() with CMAKE_REQUIRED_LIBRARIES containing library defined by toolchain file causes configuration error
I'm using check_compiler_flag()
like this:
set(CMAKE_REQUIRED_LIBRARIES libatomic )
check_compiler_flag(C "-Wl,${FLAG}" C_${NAME})
Where libatomic
is an import target defined by my toolchain file.
add_library(libatomic SHARED IMPORTED GLOBAL)
set_target_properties(libatomic PROPERTIES
IMPORTED_LOCATION ${CMAKE_SYSROOT}/lib64/libatomic.so.1.2.0
IMPORTED_SONAME "libatomic.so.1.2.0"
)
check_compiler_flag()
is generating
# Generated by CMake
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.6)
message(FATAL_ERROR "CMake >= 2.6.0 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.6...3.21)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Create imported target libatomic
add_library(libatomic SHARED IMPORTED)
set_target_properties(libatomic PROPERTIES
IMPORTED_LOCATION "D:/builds/sysroot/lib64/libatomic.so.1.2.0"
IMPORTED_SONAME "libatomic.so.1.2.0"
POSITION_INDEPENDENT_CODE "True"
)
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
Which is conflicting with the toolchain provided libatomic.
This implementation detail is not mentioned anywhere in https://cmake.org/cmake/help/latest/command/try_compile.html or https://cmake.org/cmake/help/latest/module/CheckSourceCompiles.html
While I can (probably??) work around this by defining a new import target that has a different name than the one in my toolchain file, it would be much less noisy if cmake could add a prefix or suffix that's unique to the try_compile()
command to disambiguate the target provided by the toolchain from the target requested when I set CMAKE_REQUIRED_LIBRARIES