]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: fix libcurl.pc and curl-config library specifications
authorRichard Levitte <richard@levitte.org>
Tue, 13 Feb 2024 05:45:57 +0000 (06:45 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 7 Mar 2024 08:19:11 +0000 (09:19 +0100)
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

CMakeLists.txt
docs/KNOWN_BUGS

index 40c0f776510131ce556a5e52198fde6c2d895c2c..2c69e5628c865c69ed0e579d87fc04f9452b2ba5 100644 (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()
index eab7e6389750de604f54e51ce183e6663189a285..beaa036231a8512d227a56e9c0dd9e7014d52738 100644 (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