Skip to content

Commit caab827

Browse files
committed
cmake: create object library target
Currently, we're compiling our library code twice, once as part of the libgit2 library and once for the libgit2_clar executable. Since CMake 2.8.8, there exists a new library type OBJECT, which represents an intermediate target which can then subsequently be used when linking several targets against the same set of objects. Use an OBJECT library to create an internal library for linking. This new target is only used on CMake v2.8.8 or newer. As CMake 3.0 changed the way how generator expressions are evaluated when accessing properties, we need to enable CMake policy 0051 to keep `IDE_SPLIT_SOURCES` functioning.
1 parent f33911e commit caab827

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

CMakeLists.txt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
PROJECT(libgit2 C)
1515
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
1616
CMAKE_POLICY(SET CMP0015 NEW)
17+
IF (CMAKE_VERSION VERSION_GREATER 3.0)
18+
CMAKE_POLICY(SET CMP0051 NEW)
19+
ENDIF()
1720

1821
# Add find modules to the path
1922
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
@@ -609,8 +612,24 @@ ELSE()
609612
MESSAGE(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)")
610613
ENDIF()
611614

615+
SET(GIT2INTERNAL_OBJECTS ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
616+
617+
IF (CMAKE_VERSION VERSION_GREATER 2.8.7)
618+
ADD_LIBRARY(git2internal OBJECT ${GIT2INTERNAL_OBJECTS})
619+
IDE_SPLIT_SOURCES(git2internal)
620+
SET(GIT2INTERNAL_OBJECTS $<TARGET_OBJECTS:git2internal>)
621+
622+
IF (${CMAKE_VERSION} VERSION_LESS 2.8.12)
623+
INCLUDE_DIRECTORIES(src include)
624+
ELSE()
625+
TARGET_INCLUDE_DIRECTORIES(git2internal PRIVATE src PUBLIC include)
626+
ENDIF()
627+
ELSE()
628+
INCLUDE_DIRECTORIES(src include)
629+
ENDIF()
630+
612631
# Compile and link libgit2
613-
ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1} ${WIN_RC})
632+
ADD_LIBRARY(git2 ${WIN_RC} ${GIT2INTERNAL_OBJECTS})
614633
TARGET_LINK_LIBRARIES(git2 ${SECURITY_DIRS})
615634
TARGET_LINK_LIBRARIES(git2 ${COREFOUNDATION_DIRS})
616635
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
@@ -619,12 +638,6 @@ TARGET_LINK_LIBRARIES(git2 ${GSSAPI_LIBRARIES})
619638
TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES})
620639
TARGET_OS_LIBRARIES(git2)
621640

622-
IF (${CMAKE_VERSION} VERSION_LESS 2.8.12)
623-
INCLUDE_DIRECTORIES(src include)
624-
ELSE()
625-
TARGET_INCLUDE_DIRECTORIES(git2 PRIVATE src PUBLIC include)
626-
ENDIF()
627-
628641
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
629642
# Win64+MSVC+static libs = linker error
630643
IF(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
@@ -692,7 +705,7 @@ IF (BUILD_CLAR)
692705
${CLAR_PATH}/clar.c
693706
PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clar.suite)
694707

695-
ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
708+
ADD_EXECUTABLE(libgit2_clar ${SRC_CLAR} ${SRC_TEST} $<TARGET_OBJECTS:git2internal>)
696709

697710
IF (${CMAKE_VERSION} VERSION_GREATER 2.8.11)
698711
TARGET_INCLUDE_DIRECTORIES(libgit2_clar PRIVATE src PUBLIC include)

0 commit comments

Comments
 (0)