Skip to content

Commit 8c19969

Browse files
committed
cmake: fix static linking for bundled deps
Our bundled deps are being built as simple static libraries which are then linked into the libgit2 library via `TARGET_LINK_LIBRARIES`. While this works for a dynamically built libgit2 library, using this function to link two static libraries does not have the expected outcome of merging those static libraries into one big library. This leads to symbols of our bundled deps being undefined in the resulting libgit2 archive. As we have bumped our minimum CMake version to 2.8.11, we can now easily make use of object libraries for our bundled dependencies. So build instructions are still self-contained inside of the dependency directories and the resulting object libraries can just be added to the LIBGIT2_OBJECTS list, which will cause them to be linked into the final resulting static library. This fixes the issue of undefined symbols.
1 parent d8d2f21 commit 8c19969

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

deps/http-parser/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
FILE(GLOB SRC_HTTP "*.c" "*.h")
22

3-
ADD_LIBRARY(http-parser STATIC ${SRC_HTTP})
3+
ADD_LIBRARY(http-parser OBJECT ${SRC_HTTP})

deps/regex/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
INCLUDE_DIRECTORIES(".")
2-
ADD_LIBRARY(regex STATIC "regex.c" "regex.h")
2+
ADD_LIBRARY(regex OBJECT "regex.c" "regex.h")

deps/zlib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
22
FILE(GLOB SRC_ZLIB "*.c" "*.h")
33
INCLUDE_DIRECTORIES(".")
4-
ADD_LIBRARY(zlib STATIC ${SRC_ZLIB})
4+
ADD_LIBRARY(zlib OBJECT ${SRC_ZLIB})

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ ENDIF()
183183
IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
184184
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/regex" "${CMAKE_BINARY_DIR}/deps/regex")
185185
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/regex")
186-
LIST(APPEND LIBGIT2_LIBS regex)
186+
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
187187
ENDIF()
188188

189189
# Optional external dependency: http-parser
@@ -196,7 +196,7 @@ ELSE()
196196
MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
197197
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/http-parser" "${CMAKE_BINARY_DIR}/deps/http-parser")
198198
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/http-parser")
199-
LIST(APPEND LIBGIT2_LIBS http-parser)
199+
LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
200200
ENDIF()
201201

202202
# Optional external dependency: zlib
@@ -214,7 +214,7 @@ ELSE()
214214
MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." )
215215
ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/zlib" "${CMAKE_BINARY_DIR}/deps/zlib")
216216
LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/zlib")
217-
LIST(APPEND LIBGIT2_LIBS zlib)
217+
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
218218
ENDIF()
219219

220220
# Optional external dependency: libssh2

0 commit comments

Comments
 (0)