cmake: do not echo most inherited LDFLAGS to config files

Sync with autotools and filter out most linker flags inherited via
`CMAKE_SHARED_LINKER_FLAGS` (that includes `LDFLAGS` env) before
echoing them in `libcurl.pc` `Libs.private` and `curl-config`
`--static-libs`.

Keep inheriting `-l`, `-L`, `-F`, `--library-path=`, `-framework`
options.

Follow-up to e244d50064 #15550
Follow-up to 9f56bb608e #14681
Follow-up to 8ed66f98a9

Closes #15617
This commit is contained in:
Viktor Szakats 2024-11-14 22:25:15 +01:00
parent cb2ae6e8a8
commit e233073f01
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201

View File

@ -2071,6 +2071,27 @@ if(NOT CURL_DISABLE_INSTALL)
set(_ldflags "")
set(LIBCURL_PC_LIBS_PRIVATE "")
# Filter CMAKE_SHARED_LINKER_FLAGS for libs and libpaths
string(STRIP "${CMAKE_SHARED_LINKER_FLAGS}" _custom_ldflags)
string(REGEX REPLACE " +-([^ \\t;]*)" ";-\\1" _custom_ldflags "${_custom_ldflags}")
set(_custom_libs "")
set(_custom_libdirs "")
foreach(_flag IN LISTS _custom_ldflags)
if(_flag MATCHES "^-l")
string(REGEX REPLACE "^-l" "" _flag "${_flag}")
list(APPEND _custom_libs "${_flag}")
elseif(_flag MATCHES "^-framework|^-F")
list(APPEND _custom_libs "${_flag}")
elseif(_flag MATCHES "^-L")
string(REGEX REPLACE "^-L" "" _flag "${_flag}")
list(APPEND _custom_libdirs "${_flag}")
elseif(_flag MATCHES "^--library-path=")
string(REGEX REPLACE "^--library-path=" "" _flag "${_flag}")
list(APPEND _custom_libdirs "${_flag}")
endif()
endforeach()
# Avoid getting unnecessary -L options for known system directories.
unset(_sys_libdirs)
foreach(_libdir IN LISTS CMAKE_SYSTEM_PREFIX_PATH)
@ -2090,7 +2111,7 @@ if(NOT CURL_DISABLE_INSTALL)
endif()
endforeach()
foreach(_libdir IN LISTS CURL_LIBDIRS)
foreach(_libdir IN LISTS _custom_libdirs CURL_LIBDIRS)
list(FIND _sys_libdirs "${_libdir}" _libdir_index)
if(_libdir_index LESS 0)
list(APPEND _ldflags "-L${_libdir}")
@ -2102,7 +2123,7 @@ if(NOT CURL_DISABLE_INSTALL)
set(_implicit_libs ${CMAKE_C_IMPLICIT_LINK_LIBRARIES})
endif()
foreach(_lib IN LISTS _implicit_libs CURL_LIBS)
foreach(_lib IN LISTS _implicit_libs _custom_libs CURL_LIBS)
if(TARGET "${_lib}")
set(_libname "${_lib}")
get_target_property(_imported "${_libname}" IMPORTED)
@ -2145,12 +2166,13 @@ if(NOT CURL_DISABLE_INSTALL)
if(LIBCURL_PC_LIBS_PRIVATE)
string(REPLACE ";" " " LIBCURL_PC_LIBS_PRIVATE "${LIBCURL_PC_LIBS_PRIVATE}")
endif()
set(LIBCURL_PC_LDFLAGS_PRIVATE "${CMAKE_SHARED_LINKER_FLAGS}")
if(_ldflags)
list(REMOVE_DUPLICATES _ldflags)
string(REPLACE ";" " " _ldflags "${_ldflags}")
set(LIBCURL_PC_LDFLAGS_PRIVATE "${LIBCURL_PC_LDFLAGS_PRIVATE} ${_ldflags}")
set(LIBCURL_PC_LDFLAGS_PRIVATE "${_ldflags}")
string(STRIP "${LIBCURL_PC_LDFLAGS_PRIVATE}" LIBCURL_PC_LDFLAGS_PRIVATE)
else()
set(LIBCURL_PC_LDFLAGS_PRIVATE "")
endif()
set(LIBCURL_PC_CFLAGS_PRIVATE "-DCURL_STATICLIB")