Commit 49551254 by Patrick Steinhardt

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.
parent 524c1d3c
...@@ -225,6 +225,10 @@ IF (MSVC) ...@@ -225,6 +225,10 @@ IF (MSVC)
SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}") SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}") SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
ELSE () ELSE ()
IF (NOT BUILD_SHARED_LIBS)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
ENDIF()
IF (ENABLE_REPRODUCIBLE_BUILDS) IF (ENABLE_REPRODUCIBLE_BUILDS)
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>") SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>") SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment