8000 cmake: keep track of required libraries in a variable · libgit2/libgit2@f137136 · GitHub
[go: up one dir, main page]

Skip to content

Commit f137136

Browse files
committed
cmake: keep track of required libraries in a variable
During the process of detecting required libraries, we set several variables which we then need to use later on when linking the library and tests. We can ease this process by saving all required libraries inside of a single list, which we can then use as the sole parameter to `LIBRARY_LINK_TARGET`. This has the nice benefit of being easier to export later on when splitting out a separate CMakeLists.txt for our library code.
1 parent 6f4dfbd commit f137136

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

CMakeLists.txt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ SET(LIBGIT2_PC_REQUIRES "")
124124
# pc file.
125125
SET(LIBGIT2_PC_LIBS "")
126126

127+
SET(LIBGIT2_LIBS "")
128+
SET(LIBGIT2_LIBDIRS "")
129+
127130
# Installation paths
128131
#
129132
SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
@@ -191,18 +194,18 @@ IF (SECURITY_FOUND)
191194
IF (HAVE_NEWER_SECURITY)
192195
MESSAGE("-- Found Security ${SECURITY_DIRS}")
193196
LIST(APPEND LIBGIT2_PC_LIBS "-framework Security")
197+
LIST(APPEND LIBGIT2_LIBS ${SECURITY_DIRS})
194198
ELSE()
195199
MESSAGE("-- Security framework is too old, falling back to OpenSSL")
196200
SET(SECURITY_FOUND "NO")
197-
SET(SECURITY_DIRS "")
198-
SET(SECURITY_DIR "")
199201
SET(USE_OPENSSL "ON")
200202
ENDIF()
201203
ENDIF()
202204

203205
IF (COREFOUNDATION_FOUND)
204206
MESSAGE("-- Found CoreFoundation ${COREFOUNDATION_DIRS}")
205207
LIST(APPEND LIBGIT2_PC_LIBS "-framework CoreFoundation")
208+
LIST(APPEND LIBGIT2_LIBS ${COREFOUNDATION_DIRS})
206209
ENDIF()
207210

208211

@@ -247,10 +250,10 @@ IF (WIN32 AND WINHTTP)
247250
)
248251

249252
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/deps/winhttp")
250-
LINK_DIRECTORIES(${LIBWINHTTP_PATH})
253+
LIST(APPEND LIBGIT2_LIBDIRS ${LIBWINHTTP_PATH})
251254
ENDIF ()
252255

253-
LINK_LIBRARIES(winhttp rpcrt4 crypt32 ole32)
256+
LIST(APPEND LIBGIT2_LIBS "winhttp" "rpcrt4" "crypt32" "ole32")
254257
LIST(APPEND LIBGIT2_PC_LIBS "-lwinhttp" "-lrpcrt4" "-lcrypt32" "-lole32")
255258
ELSE ()
256259
IF (CURL)
@@ -264,8 +267,8 @@ ELSE ()
264267
IF (CURL_FOUND)
265268
SET(GIT_CURL 1)
266269
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
267-
LINK_DIRECTORIES(${CURL_LIBRARY_DIRS})
268-
LINK_LIBRARIES(${CURL_LIBRARIES})
270+
LIST(APPEND LIBGIT2_LIBDIRS ${CURL_LIBRARY_DIRS})
271+
LIST(APPEND LIBGIT2_LIBS ${CURL_LIBRARIES})
269272
LIST(APPEND LIBGIT2_PC_LIBS ${CURL_LDFLAGS})
270273
ENDIF()
271274
ENDIF()
@@ -308,7 +311,7 @@ ENDIF()
308311
FIND_PACKAGE(HTTP_Parser)
309312
IF (USE_EXT_HTTP_PARSER AND HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
310313
INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS})
311-
LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES})
314+
LIST(APPEND LIBGIT2_LIBS ${HTTP_PARSER_LIBRARIES})
312315
LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
313316
ELSE()
314317
MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
@@ -320,8 +323,9 @@ ENDIF()
320323
FIND_PACKAGE(ZLIB)
321324
IF (ZLIB_FOUND)
322325
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
323-
LINK_LIBRARIES(${ZLIB_LIBRARIES})
326+
LIST(APPEND LIBGIT2_LIBS ${ZLIB_LIBRARIES})
324327
IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
328+
LIST(APPEND LIBGIT2_LIBS "z")
325329
LIST(APPEND LIBGIT2_PC_LIBS "-lz")
326330
ELSE()
327331
SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} zlib")
@@ -340,10 +344,10 @@ ENDIF()
340344
IF (LIBSSH2_FOUND)
341345
SET(GIT_SSH 1)
342346
INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIRS})
343-
LINK_DIRECTORIES(${LIBSSH2_LIBRARY_DIRS})
347+
LIST(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
348+
LIST(APPEND LIBGIT2_LIBDIRS ${LIBSSH2_LIBRARY_DIRS})
344349
LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
345350
#SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} ${LIBSSH2_LDFLAGS}")
346-
SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
347351

348352
CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
349353
IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)
@@ -359,6 +363,7 @@ IF (USE_GSSAPI)
359363
ENDIF()
360364
IF (GSSAPI_FOUND)
361365
SET(GIT_GSSAPI 1)
366+
LIST(APPEND LIBGIT2_LIBS ${GSSAPI_LIBRARIES})
362367
ENDIF()
363368

364369
# Optional external dependency: iconv
@@ -368,6 +373,7 @@ ENDIF()
368373
IF (ICONV_FOUND)
369374
SET(GIT_USE_ICONV 1)
370375
INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR})
376+
LIST(APPEND LIBGIT2_LIBS ${ICONV_LIBRARIES})
371377
LIST(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
372378
ENDIF()
373379

@@ -530,7 +536,7 @@ IF (OPENSSL_FOUND)
530536
SET(GIT_OPENSSL 1)
531537
SET(GIT_HTTPS 1)
532538
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
533-
SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
539+
LIST(APPEND LIBGIT2_LIBS ${OPENSSL_LIBRARIES})
534540
ENDIF()
535541

536542

@@ -605,12 +611,8 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
605611

606612
# Compile and link libgit2
607613
ADD_LIBRARY(git2 ${WIN_RC} ${GIT2INTERNAL_OBJECTS})
608-
TARGET_LINK_LIBRARIES(git2 ${SECURITY_DIRS})
609-
TARGET_LINK_LIBRARIES(git2 ${COREFOUNDATION_DIRS})
610-
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
611-
TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES})
612-
TARGET_LINK_LIBRARIES(git2 ${GSSAPI_LIBRARIES})
613-
TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES})
614+
LINK_DIRECTORIES(${LIBGIT2_LIBDIRS})
615+
TARGET_LINK_LIBRARIES(git2 ${LIBGIT2_LIBS})
614616
TARGET_OS_LIBRARIES(git2)
615617

616618
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)

tests/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ IF (${CMAKE_VERSION} VERSION_GREATER 2.8.11)
3232
TARGET_INCLUDE_DIRECTORIES(libgit2_clar PRIVATE ../src PUBLIC ../include)
3333
ENDIF()
3434

35-
TARGET_LINK_LIBRARIES(libgit2_clar ${COREFOUNDATION_DIRS})
36-
TARGET_LINK_LIBRARIES(libgit2_clar ${SECURITY_DIRS})
37-
TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
38-
TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES})
39-
TARGET_LINK_LIBRARIES(libgit2_clar ${GSSAPI_LIBRARIES})
40-
TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES})
41-
TARGET_OS_LIBRARIES(libgit2_clar)
35+
TARGET_LINK_LIBRARIES(libgit2_clar ${LIBGIT2_LIBS})
4236

4337
IF (MSVC_IDE)
4438
# Precompiled headers

0 commit comments

Comments
 (0)
0