Commit 49551254c3146274dfb5764318f4661ce47a6f0a

Patrick Steinhardt 2017-09-22T09:34:37

cmake: use static dependencies when building static libgit2 CMake allows us to build a static library by simply setting the variable `BUILD_SHARED_LIBS` to `OFF`. While this causes us to create a static libgit2.a archive, it will not automatically cause CMake to only locate static archives when searching for dependencies. This does no harm in case of building our libgit2.a, as we do not want to include all required dependencies in the resulting archive anyway. Instead, we ask users of a static libgit2.a to link against the required set of static archives themselves, typically aided by the libgit2.pc file. Where it does cause harm, though, is when we build the libgit2_clar test suite. CMake has happily populated our LIBGIT2_LIBS variable with shared libraries, and so linking the final libgit2_clar test does not do the right thing. It will simply ignore those shared libraries, we end up with a test suite with undefined symbols. To fix the issue, we can instruct CMake to only locate libraries with a certain suffix. As static libraries are typically identifiable by their ".a" suffix on Unix-based systems, we can instruct CMake to only locate libraries with this suffix to restrict it from finding any shared libraries. This fixes building a static libgit2_clar test suite. Note that this ignores the problem on Windows. The problem here is that we cannot even distinguish static and dynamic libraries by only inspecting their suffix. So we just ignore the problem on Windows, for now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
diff --git a/CMakeLists.txt b/CMakeLists.txt
index af4c34e..6d0479f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -225,6 +225,10 @@ IF (MSVC)
 	SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
 	SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
 ELSE ()
+	IF (NOT BUILD_SHARED_LIBS)
+		SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+	ENDIF()
+
 	IF (ENABLE_REPRODUCIBLE_BUILDS)
 		SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
 		SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq  <TARGET> <LINK_FLAGS> <OBJECTS>")