From e233073f0160c77fdb1bb4147f78e2c04a9df917 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Thu, 14 Nov 2024 22:25:15 +0100 Subject: [PATCH] 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 e244d50064a56723c2ba4f0df8c847d6b70de0cb #15550 Follow-up to 9f56bb608ecfbb8978c6cb72a04d9e8b23162d82 #14681 Follow-up to 8ed66f98a954cfce92f770adeb2320deb1ea700e Closes #15617 --- CMakeLists.txt | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 52aa1177c6..b0da32131a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") -- 2.47.3