cmake: fix libcurl.pc and curl-config library specifications

Letting CMake figure out where libraries are located gives you full
paths. When generating libcurl.pc and curl-config, getting libraries as
full paths is unusual when one expects to get a list of -l<libname>.

To meet expectations, an effort is made to convert the full paths into
-l<libname>, possibly with -L<libdir> before it.

Fixes #6169
Fixes #12748
Closes #12930
This commit is contained in:
Richard Levitte 2024-02-13 06:45:57 +01:00 committed by Daniel Stenberg
parent 6a13d4d75c
commit 296e855d36
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 41 additions and 10 deletions

View File

@ -1642,6 +1642,30 @@ if(NOT CURL_DISABLE_INSTALL)
set(LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
set(LIBCURL_LIBS "")
set(libdir "${CMAKE_INSTALL_PREFIX}/lib")
# For processing full path libraries into -L and -l ld options,
# the directories that go with the -L option are cached, so they
# only get added once per such directory.
set(_libcurl_libs_dirs)
# To avoid getting unnecessary -L options for known system directories,
# _libcurl_libs_dirs is seeded with them.
foreach(_libdir ${CMAKE_SYSTEM_PREFIX_PATH})
if(_libdir MATCHES "/$")
set(_libdir "${_libdir}lib")
else()
set(_libdir "${_libdir}/lib")
endif()
if(IS_DIRECTORY "${_libdir}")
list(APPEND _libcurl_libs_dirs "${_libdir}")
endif()
if(DEFINED CMAKE_LIBRARY_ARCHITECTURE)
set(_libdir "${_libdir}/${CMAKE_LIBRARY_ARCHITECTURE}")
if(IS_DIRECTORY "${_libdir}")
list(APPEND _libcurl_libs_dirs "${_libdir}")
endif()
endif()
endforeach()
foreach(_lib ${CMAKE_C_IMPLICIT_LINK_LIBRARIES} ${CURL_LIBS})
if(TARGET "${_lib}")
set(_libname "${_lib}")
@ -1657,8 +1681,24 @@ if(NOT CURL_DISABLE_INSTALL)
continue()
endif()
endif()
if(_lib MATCHES ".*/.*" OR _lib MATCHES "^-")
if(_lib MATCHES "^-")
set(LIBCURL_LIBS "${LIBCURL_LIBS} ${_lib}")
elseif(_lib MATCHES ".*/.*")
# This gets a bit more complex, because we want to specify the
# directory separately, and only once per directory
string(REGEX REPLACE "^(.*)/[^/]*$" "\\1" _libdir "${_lib}")
string(REGEX REPLACE "^.*/([^/.]*).*$" "\\1" _libname "${_lib}")
if(_libname MATCHES "^lib")
list(FIND _libcurl_libs_dirs "${_libdir}" _libdir_index)
if(_libdir_index LESS 0)
list(APPEND _libcurl_libs_dirs "${_libdir}")
set(LIBCURL_LIBS "${LIBCURL_LIBS} -L${_libdir}")
endif()
string(REGEX REPLACE "^lib" "" _libname "${_libname}")
set(LIBCURL_LIBS "${LIBCURL_LIBS} -l${_libname}")
else()
set(LIBCURL_LIBS "${LIBCURL_LIBS} ${_lib}")
endif()
else()
set(LIBCURL_LIBS "${LIBCURL_LIBS} -l${_lib}")
endif()

View File

@ -98,7 +98,6 @@ problems may have been fixed or changed somewhat since this was written.
15.3 unusable tool_hugehelp.c with MinGW
15.6 uses -lpthread instead of Threads::Threads
15.7 generated .pc file contains strange entries
15.8 libcurl.pc uses absolute library paths
15.11 ExternalProject_Add does not set CURL_CA_PATH
15.13 CMake build with MIT Kerberos does not work
@ -577,14 +576,6 @@ problems may have been fixed or changed somewhat since this was written.
See https://github.com/curl/curl/issues/6167
15.8 libcurl.pc uses absolute library paths
The libcurl.pc file generated by cmake contains things like Libs.private:
/usr/lib64/libssl.so /usr/lib64/libcrypto.so /usr/lib64/libz.so. The
autotools equivalent would say Libs.private: -lssl -lcrypto -lz
See https://github.com/curl/curl/issues/6169
15.11 ExternalProject_Add does not set CURL_CA_PATH
CURL_CA_BUNDLE and CURL_CA_PATH are not set properly when cmake's