Skip to content

Commit 0f62e4c

Browse files
committed
cmake: resolve libraries found by pkg-config
Libraries found by CMake modules are usually handled with their full path. This makes linking against those libraries a lot more robust when it comes to libraries in non-standard locations, as otherwise we might mix up libraries from different locations when link directories are given. One excemption are libraries found by PKG_CHECK_MODULES. Instead of returning libraries with their complete path, it will return the variable names as well as a set of link directories. In case where multiple sets of the same library are installed in different locations, this can lead the compiler to link against the wrong libraries in the end, when link directories of other dependencies are added. To fix this shortcoming, we need to manually resolve library paths returned by CMake against their respective library directories. This is an easy task to do with `FIND_LIBRARY`.
1 parent 86353a7 commit 0f62e4c

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ INCLUDE(CheckFunctionExists)
2929
INCLUDE(CheckSymbolExists)
3030
INCLUDE(CheckStructHasMember)
3131
INCLUDE(AddCFlagIfSupported)
32-
INCLUDE(FindPkgConfig)
32+
INCLUDE(FindPkgLibraries)
3333
INCLUDE(FindThreads)
3434
INCLUDE(FindStatNsec)
3535
INCLUDE(IdeSplitSources)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
INCLUDE(FindPkgConfig)
2+
3+
# This function will find and set up a pkg-config based module.
4+
# If a pc-file was found, it will resolve library paths to
5+
# absolute paths. Furthermore, the function will automatically
6+
# fall back to use static libraries in case no dynamic libraries
7+
# were found.
8+
FUNCTION(FIND_PKGLIBRARIES prefix package)
9+
PKG_CHECK_MODULES(${prefix} ${package})
10+
IF(NOT ${prefix}_FOUND)
11+
RETURN()
12+
ENDIF()
13+
14+
FOREACH(LIBRARY ${${prefix}_LIBRARIES})
15+
FIND_LIBRARY(${LIBRARY}_RESOLVED ${LIBRARY} PATHS ${${prefix}_LIBRARY_DIRS})
16+
IF(${${LIBRARY}_RESOLVED} STREQUAL "${LIBRARY}_RESOLVED-NOTFOUND")
17+
MESSAGE(FATAL_ERROR "could not resolve ${LIBRARY}")
18+
ENDIF()
19+
LIST(APPEND RESOLVED_LIBRARIES ${${LIBRARY}_RESOLVED})
20+
ENDFOREACH(LIBRARY)
21+
22+
SET(${prefix}_FOUND 1 PARENT_SCOPE)
23+
SET(${prefix}_LIBRARIES ${RESOLVED_LIBRARIES} PARENT_SCOPE)
24+
SET(${prefix}_INCLUDE_DIRS ${${prefix}_INCLUDE_DIRS} PARENT_SCOPE)
25+
SET(${prefix}_LDFLAGS ${${prefix}_LDFLAGS} PARENT_SCOPE)
26+
27+
MESSAGE(STATUS " Resolved libraries: ${RESOLVED_LIBRARIES}")
28+
ENDFUNCTION()

src/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ IF (WIN32 AND WINHTTP)
119119
LIST(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32")
120120
ELSE ()
121121
IF (CURL)
122-
PKG_CHECK_MODULES(CURL libcurl)
122+
FIND_PKGLIBRARIES(CURL libcurl)
123123
ENDIF ()
124-
125124
IF (CURL_FOUND)
126125
SET(GIT_CURL 1)
127126
LIST(APPEND LIBGIT2_INCLUDES ${CURL_INCLUDE_DIRS})
@@ -340,15 +339,14 @@ ENDIF()
340339

341340
# Optional external dependency: libssh2
342341
IF (USE_SSH)
343-
PKG_CHECK_MODULES(LIBSSH2 libssh2)
342+
FIND_PKGLIBRARIES(LIBSSH2 libssh2)
344343
ENDIF()
345344
IF (LIBSSH2_FOUND)
346345
SET(GIT_SSH 1)
347346
LIST(APPEND LIBGIT2_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
348347
LIST(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
349348
LIST(APPEND LIBGIT2_LIBDIRS ${LIBSSH2_LIBRARY_DIRS})
350349
LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
351-
#SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} ${LIBSSH2_LDFLAGS}")
352350

353351
CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
354352
IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)

0 commit comments

Comments
 (0)