cmake: skip binutils ld hack if zlib/openssl target is not IMPORTED

The binutils ld hack requires reading the targets' `LOCATION` property.
This property exists in `IMPORTED` targets. `ZLIB::ZLIB` and
`OpenSSL::Crypto` are normally `IMPORTED` targets defined by CMake's
built-in Find modules. However, in some cases (e.g. in "superbuilds"),
they may be regular targets, defined manually, without a `LOCATION`
property. To avoid a CMake warning in such case, verify if the target is
`IMPORTED` before reading this property.

This also mean that in such case the binutils/ld/gcc hack is not
enabled, and libcurl may fail linking in static mode.

https://cmake.org/cmake/help/v4.2/prop_tgt/IMPORTED.html
https://cmake.org/cmake/help/v4.2/prop_tgt/LOCATION.html

Reported-by: Tomáš Malý
Fixes #20419
Follow-up to 3e841630ec #20427
Follow-up to 16f073ef49 #16973

Closes #20486
This commit is contained in:
Viktor Szakats 2026-01-31 21:15:51 +01:00
parent 96fa42c7c0
commit 4f1646ef8a
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201

View File

@ -1817,16 +1817,22 @@ endif()
# Enable the workaround for all compilers, to make it available when using GCC
# to consume libcurl, regardless of the compiler used to build libcurl itself.
if(USE_OPENSSL AND TARGET OpenSSL::Crypto)
add_library(CURL::OpenSSL_Crypto INTERFACE IMPORTED)
get_target_property(_curl_libname OpenSSL::Crypto LOCATION)
set_target_properties(CURL::OpenSSL_Crypto PROPERTIES INTERFACE_LINK_LIBRARIES "${_curl_libname}")
list(APPEND CURL_LIBS CURL::OpenSSL_Crypto)
get_target_property(_curl_imported OpenSSL::Crypto IMPORTED)
if(_curl_imported)
add_library(CURL::OpenSSL_Crypto INTERFACE IMPORTED)
get_target_property(_curl_libname OpenSSL::Crypto LOCATION)
set_target_properties(CURL::OpenSSL_Crypto PROPERTIES INTERFACE_LINK_LIBRARIES "${_curl_libname}")
list(APPEND CURL_LIBS CURL::OpenSSL_Crypto)
endif()
endif()
if(HAVE_LIBZ AND TARGET ZLIB::ZLIB)
add_library(CURL::ZLIB INTERFACE IMPORTED)
get_target_property(_curl_libname ZLIB::ZLIB LOCATION)
set_target_properties(CURL::ZLIB PROPERTIES INTERFACE_LINK_LIBRARIES "${_curl_libname}")
list(APPEND CURL_LIBS CURL::ZLIB)
get_target_property(_curl_imported ZLIB::ZLIB IMPORTED)
if(_curl_imported)
add_library(CURL::ZLIB INTERFACE IMPORTED)
get_target_property(_curl_libname ZLIB::ZLIB LOCATION)
set_target_properties(CURL::ZLIB PROPERTIES INTERFACE_LINK_LIBRARIES "${_curl_libname}")
list(APPEND CURL_LIBS CURL::ZLIB)
endif()
endif()
if(WIN32)
add_library(CURL::win32_winsock INTERFACE IMPORTED)