Commit 8c19969a88c3b6e72564e812537b15cbc0fb4cc0

Patrick Steinhardt 2017-09-06T07:38:48

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.

diff --git a/deps/http-parser/CMakeLists.txt b/deps/http-parser/CMakeLists.txt
index 9309841..77d9de7 100644
--- a/deps/http-parser/CMakeLists.txt
+++ b/deps/http-parser/CMakeLists.txt
@@ -1,3 +1,3 @@
 FILE(GLOB SRC_HTTP "*.c" "*.h")
 
-ADD_LIBRARY(http-parser STATIC ${SRC_HTTP})
+ADD_LIBRARY(http-parser OBJECT ${SRC_HTTP})
diff --git a/deps/regex/CMakeLists.txt b/deps/regex/CMakeLists.txt
index 6ef8a27..141b54c 100644
--- a/deps/regex/CMakeLists.txt
+++ b/deps/regex/CMakeLists.txt
@@ -1,2 +1,2 @@
 INCLUDE_DIRECTORIES(".")
-ADD_LIBRARY(regex STATIC "regex.c" "regex.h")
+ADD_LIBRARY(regex OBJECT "regex.c" "regex.h")
diff --git a/deps/zlib/CMakeLists.txt b/deps/zlib/CMakeLists.txt
index ca50d80..b0cb7f7 100644
--- a/deps/zlib/CMakeLists.txt
+++ b/deps/zlib/CMakeLists.txt
@@ -1,4 +1,4 @@
 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
 FILE(GLOB SRC_ZLIB "*.c" "*.h")
 INCLUDE_DIRECTORIES(".")
-ADD_LIBRARY(zlib STATIC ${SRC_ZLIB})
+ADD_LIBRARY(zlib OBJECT ${SRC_ZLIB})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6b7e0f6..daecc5a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -183,7 +183,7 @@ ENDIF()
 IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
 	ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/regex" "${CMAKE_BINARY_DIR}/deps/regex")
 	LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/regex")
-	LIST(APPEND LIBGIT2_LIBS regex)
+	LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
 ENDIF()
 
 # Optional external dependency: http-parser
@@ -196,7 +196,7 @@ ELSE()
 	MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
 	ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/http-parser" "${CMAKE_BINARY_DIR}/deps/http-parser")
 	LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/http-parser")
-	LIST(APPEND LIBGIT2_LIBS http-parser)
+	LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
 ENDIF()
 
 # Optional external dependency: zlib
@@ -214,7 +214,7 @@ ELSE()
 	MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." )
 	ADD_SUBDIRECTORY("${CMAKE_SOURCE_DIR}/deps/zlib" "${CMAKE_BINARY_DIR}/deps/zlib")
 	LIST(APPEND LIBGIT2_INCLUDES "${CMAKE_SOURCE_DIR}/deps/zlib")
-	LIST(APPEND LIBGIT2_LIBS zlib)
+	LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
 ENDIF()
 
 # Optional external dependency: libssh2