8000 cmake: use a library target to avoid recompilation · libgit2/libgit2@9f9891d · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f9891d

Browse files
author
Edward Thomson
committed
cmake: use a library target to avoid recompilation
Instead of listing the sources separately for the git2 target and the libgit2_clar target, create an "object library" target (for more information, see at https://cmake.org/Wiki/CMake/Tutorials/Object_Library). The object library will then be used to link both the git2 library target and the libgit2_clar target instead of having to compile all the source twice.
1 parent 104a1b0 commit 9f9891d

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

CMakeLists.txt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ ENDFUNCTION()
173173

174174
# This function splits the sources files up into their appropriate
175175
# subdirectories. This is especially useful for IDEs like Xcode and
176-
# Visual Studio, so that you can navigate into the libgit2_clar project,
176+
# Visual Studio, so that you can navigate into the git2_tests project,
177177
# and see the folders within the tests folder (instead of just seeing all
178178
# source and tests in a single folder.)
179179
FUNCTION(IDE_SPLIT_SOURCES target)
@@ -600,45 +600,50 @@ ELSE()
600600
MESSAGE(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)")
601601
ENDIF()
602602

603-
# Compile and link libgit2
604-
ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1} ${WIN_RC})
605-
TARGET_LINK_LIBRARIES(git2 ${SECURITY_DIRS})
606-
TARGET_LINK_LIBRARIES(git2 ${COREFOUNDATION_DIRS})
607-
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
608-
TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES})
609-
TARGET_LINK_LIBRARIES(git2 ${GSSAPI_LIBRARIES})
610-
TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES})
611-
TARGET_OS_LIBRARIES(git2)
603+
# Compile and link a static version of libgit2; we will use this to link both
604+
# a shared library (unless disabled) and clar.
605+
ADD_LIBRARY(git2_src OBJECT ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
606+
607+
ADD_LIBRARY(git2_lib ${WIN_RC} $<TARGET_OBJECTS:git2_src>)
608+
TARGET_LINK_LIBRARIES(git2_lib ${SECURITY_DIRS})
609+
TARGET_LINK_LIBRARIES(git2_lib ${COREFOUNDATION_DIRS})
610+
TARGET_LINK_LIBRARIES(git2_lib ${SSL_LIBRARIES})
611+
TARGET_LINK_LIBRARIES(git2_lib ${SSH_LIBRARIES})
612+
TARGET_LINK_LIBRARIES(git2_lib ${GSSAPI_LIBRARIES})
613+
TARGET_LINK_LIBRARIES(git2_lib ${ICONV_LIBRARIES})
614+
TARGET_OS_LIBRARIES(git2_lib)
615+
SET_TARGET_PROPERTIES(git2_lib PROPERTIES OUTPUT_NAME git2)
612616

613617
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
614618
# Win64+MSVC+static libs = linker error
615619
IF(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
616-
SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
620+
SET_TARGET_PROPERTIES(git2_lib PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
617621
ENDIF()
618622

619-
IDE_SPLIT_SOURCES(git2)
623+
IDE_SPLIT_SOURCES(git2_src)
624+
IDE_SPLIT_SOURCES(git2_lib)
620625

621626
IF (SONAME)
622-
SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
623-
SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_SOVERSION})
627+
SET_TARGET_PROPERTIES(git2_lib PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
628+
SET_TARGET_PROPERTIES(git2_lib PROPERTIES SOVERSION ${LIBGIT2_SOVERSION})
624629
IF (LIBGIT2_FILENAME)
625630
ADD_DEFINITIONS(-DLIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\")
626-
SET_TARGET_PROPERTIES(git2 PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME})
631+
SET_TARGET_PROPERTIES(git2_lib PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME})
627632
ELSEIF (DEFINED LIBGIT2_PREFIX)
628-
SET_TARGET_PROPERTIES(git2 PROPERTIES PREFIX "${LIBGIT2_PREFIX}")
633+
SET_TARGET_PROPERTIES(git2_lib PROPERTIES PREFIX "${LIBGIT2_PREFIX}")
629634
ENDIF()
630635
ENDIF()
631636
STRING(REPLACE ";" " " LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS}")
632637
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
633638

634639
IF (MSVC_IDE)
635640
# Precompiled headers
636-
SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
641+
SET_TARGET_PROPERTIES(git2_src PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
637642
SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
638643
ENDIF ()
639644

640645
# Install
641-
INSTALL(TARGETS git2
646+
INSTALL(TARGETS git2_lib
642647
RUNTIME DESTINATION ${BIN_INSTALL_DIR}
643648
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
644649
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
@@ -678,21 +683,17 @@ IF (BUILD_CLAR)
678683
${CLAR_PATH}/clar.c
679684
PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite)
680685

681-
ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
682-
683-
TARGET_LINK_LIBRARIES(libgit2_clar ${COREFOUNDATION_DIRS})
684-
TARGET_LINK_LIBRARIES(libgit2_clar ${SECURITY_DIRS})
685-
TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
686-
TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES})
687-
TARGET_LINK_LIBRARIES(libgit2_clar ${GSSAPI_LIBRARIES})
688-
TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES})
689-
TARGET_OS_LIBRARIES(libgit2_clar)
690-
IDE_SPLIT_SOURCES(libgit2_clar)
691-
692-
IF (MSVC_IDE)
693-
# Precompiled headers
694-
SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
695-
ENDIF ()
686+
ADD_EXECUTABLE(git2_tests ${SRC_CLAR} ${SRC_TEST} $<TARGET_OBJECTS:git2_src>)
687+
688+
TARGET_LINK_LIBRARIES(git2_tests ${COREFOUNDATION_DIRS})
689+
TARGET_LINK_LIBRARIES(git2_tests ${SECURITY_DIRS})
690+
TARGET_LINK_LIBRARIES(git2_tests ${SSL_LIBRARIES})
691+
TARGET_LINK_LIBRARIES(git2_tests ${SSH_LIBRARIES})
692+
TARGET_LINK_LIBRARIES(git2_tests ${GSSAPI_LIBRARIES})
693+
TARGET_LINK_LIBRARIES(git2_tests ${ICONV_LIBRARIES})
694+
TARGET_OS_LIBRARIES(git2_tests)
695+
IDE_SPLIT_SOURCES(git2_tests)
696+
SET_TARGET_PROPERTIES(git2_tests PROPERTIES OUTPUT_NAME libgit2_clar)
696697

697698
ENABLE_TESTING()
698699
IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND)

0 commit comments

Comments
 (0)
0