From 8d45789167794da92d5e493ae6629110c6ca278a Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 18:52:49 +0100 Subject: [PATCH 1/9] Only add deps/http-parser to include-dirs if required --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fc530773d..aac6f4b5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,13 +27,14 @@ IF (AMIGA) ENDIF() # Find required dependencies -INCLUDE_DIRECTORIES(src include deps/http-parser) +INCLUDE_DIRECTORIES(src include) IF (WIN32 AND NOT MINGW) ADD_DEFINITIONS(-DGIT_WINHTTP) ELSE () FIND_PACKAGE(OpenSSL) FILE(GLOB SRC_HTTP deps/http-parser/*.c) + INCLUDE_DIRECTORIES(deps/http-parser) ENDIF() # Specify sha1 implementation From 3d007f4f9c4de32ed418b21ef4c51e388bb63547 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 18:55:23 +0100 Subject: [PATCH 2/9] DRY: Scan for regex.c only in one place --- CMakeLists.txt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aac6f4b5a..39dfbdc05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,12 +52,10 @@ ENDIF() IF (NOT WIN32) FIND_PACKAGE(ZLIB) - IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS") - INCLUDE_DIRECTORIES(deps/regex) - SET(SRC_REGEX deps/regex/regex.c) - ENDIF() -ELSE() - # Windows doesn't understand POSIX regex on its own +ENDIF() + +# Include POSIX regex when it is required +IF(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "AmigaOS") INCLUDE_DIRECTORIES(deps/regex) SET(SRC_REGEX deps/regex/regex.c) ENDIF() From b53671ae85e40804eefbfe2afecee1ba21df8d1d Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 19:03:29 +0100 Subject: [PATCH 3/9] Search for zlib unconditional Up to now, on windows we don't even bother to look if the user has a zlib available somwhere. In almost all larger commercial projects i've participated in, it was not at all uncommon to have such a dependency somewhere in the source tree and use it whereever required. Searching for it, even if it's unlikely to be present, allows for such a scenario (i.e. by prefilling the CMake-Cache). --- CMakeLists.txt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 39dfbdc05..2a41aad6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,20 +50,22 @@ ELSE() FILE(GLOB SRC_SHA1 src/hash/hash_generic.c) ENDIF() -IF (NOT WIN32) - FIND_PACKAGE(ZLIB) -ENDIF() - # Include POSIX regex when it is required IF(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "AmigaOS") INCLUDE_DIRECTORIES(deps/regex) SET(SRC_REGEX deps/regex/regex.c) ENDIF() +# Optional external dependency: zlib +IF(NOT ZLIB_LIBRARY) + # It's optional, but FIND_PACKAGE gives a warning that looks more like an error. + FIND_PACKAGE(ZLIB QUIET) +ENDIF() IF (ZLIB_FOUND) INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) LINK_LIBRARIES(${ZLIB_LIBRARIES}) -ELSE (ZLIB_FOUND) +ELSE() + MESSAGE( "zlib was not found; using bundled 3rd-party sources." ) INCLUDE_DIRECTORIES(deps/zlib) ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP) FILE(GLOB SRC_ZLIB deps/zlib/*.c) From 49b630086e16545b0770a3425adcfdcc83863599 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 19:07:08 +0100 Subject: [PATCH 4/9] Remove src/compat/*.c from source globs This directory doesn't exist. --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a41aad6d..3bec692f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -149,12 +149,12 @@ FILE(GLOB SRC_H include/git2/*.h) # On Windows use specific platform sources IF (WIN32 AND NOT CYGWIN) ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c) + FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c) ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c) + FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) ELSEIF (AMIGA) ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c) + FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c) ELSE() FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) ENDIF () From c5309eb2da80f04e3ad445c340d18fbe5babbc6a Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 19:07:59 +0100 Subject: [PATCH 5/9] Remove special case source globs for Solaris With the src/compat/*.c glob removed, there is no longer a difference to the default globs we use for the IF( UNIX ) case. --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bec692f3..64e5da44e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,8 +150,6 @@ FILE(GLOB SRC_H include/git2/*.h) IF (WIN32 AND NOT CYGWIN) ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c) -ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) ELSEIF (AMIGA) ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c) From 521479b1708c1bad7f3c62546000ead2fcec8313 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 19:18:13 +0100 Subject: [PATCH 6/9] DRY: Don't repeat globs for libgit2's own source files --- CMakeLists.txt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64e5da44e..62878797d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -149,16 +149,17 @@ FILE(GLOB SRC_H include/git2/*.h) # On Windows use specific platform sources IF (WIN32 AND NOT CYGWIN) ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c) + FILE(GLOB SRC_OS src/win32/*.c) ELSEIF (AMIGA) ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c) + FILE(GLOB SRC_OS src/amiga/*.c) ELSE() - FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) -ENDIF () + FILE(GLOB SRC_OS src/unix/*.c) +ENDIF() +FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c) # Compile and link libgit2 -ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) +ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) IF (WIN32) TARGET_LINK_LIBRARIES(git2 ws2_32) @@ -207,7 +208,7 @@ IF (BUILD_CLAR) DEPENDS ${CLAR_PATH}/clar ${SRC_TEST} WORKING_DIRECTORY ${CLAR_PATH} ) - ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) + ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) IF (MSVC_IDE) From 19a766a201b9eb8bce7e5b097bf44e78385d0069 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 19:32:31 +0100 Subject: [PATCH 7/9] Collect configuration options at the top of the file - Also document the -DSTDCALL even better. --- CMakeLists.txt | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62878797d..f39a1d04b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,33 @@ PROJECT(libgit2 C) CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +# Build options +# +OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON ) +OPTION( THREADSAFE "Build libgit2 as threadsafe" OFF ) +OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON ) +OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF ) +OPTION( TAGS "Generate tags" OFF ) +OPTION( PROFILE "Generate profiling information" OFF ) +IF(MSVC) + # This option is only availalbe when building with MSVC. By default, libgit2 is + # build using the stdcall calling convention, as that's what the CLR expects by + # default and how the Windows API is built. + # + # If you are writing a C or C++ program and want to link to libgit2, you have to + # either: + # - Add /Gz to the compiler options of _your_ program / library. + # - Turn this option off by invoking CMake with the "-DSTDCALL=Off" argument. + # + OPTION( STDCALL "Build libgit2 with the __stdcall convention" ON ) +ENDIF() + +# Installation paths +# +SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") +SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") +SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") + FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}") @@ -71,23 +98,8 @@ ELSE() FILE(GLOB SRC_ZLIB deps/zlib/*.c) ENDIF() -# Installation paths -SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") -SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") -SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") - -# Build options -OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON) -OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF) -OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON) -OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF) -OPTION (TAGS "Generate tags" OFF) -OPTION (PROFILE "Generate profiling information" OFF) - # Platform specific compilation flags IF (MSVC) - # Default to stdcall, as that's what the CLR expects and how the Windows API is built - OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON) STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") From 94243295b203fef6dbd2f225b13810c4ee683c38 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 19:51:31 +0100 Subject: [PATCH 8/9] DRY: Add function that adds os-specific libraries to our targets --- CMakeLists.txt | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f39a1d04b..800c26c19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,17 @@ SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") +FUNCTION(TARGET_OS_LIBRARIES target) + IF(WIN32) + TARGET_LINK_LIBRARIES(${target} ws2_32) + ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") + TARGET_LINK_LIBRARIES(${target} socket nsl) + ENDIF () + IF(THREADSAFE) + TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT}) + ENDIF() +ENDFUNCTION() + FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}") @@ -172,14 +183,9 @@ FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c) # Compile and link libgit2 ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) +TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES}) +TARGET_OS_LIBRARIES(git2) -IF (WIN32) - TARGET_LINK_LIBRARIES(git2 ws2_32) -ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") - TARGET_LINK_LIBRARIES(git2 socket nsl) -ENDIF () - -TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) @@ -221,19 +227,14 @@ IF (BUILD_CLAR) WORKING_DIRECTORY ${CLAR_PATH} ) ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) - TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) + TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES}) + TARGET_OS_LIBRARIES(libgit2_clar) IF (MSVC_IDE) # Precompiled headers SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") ENDIF () - IF (WIN32) - TARGET_LINK_LIBRARIES(libgit2_clar ws2_32) - ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") - TARGET_LINK_LIBRARIES(libgit2_clar socket nsl) - ENDIF () - ENABLE_TESTING() ADD_TEST(libgit2_clar libgit2_clar -iall) ENDIF () From 523a3ae5a3e7a70f4479c59e975211ae2e8d3785 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Tue, 18 Dec 2012 20:40:57 +0100 Subject: [PATCH 9/9] MSVC: Don't list all source files in an endless list Instead tell MSVC to group the source files by directory. --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 800c26c19..696d6a088 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,29 @@ FUNCTION(TARGET_OS_LIBRARIES target) ENDIF() ENDFUNCTION() +# For the MSVC IDE, this function splits up the source files like windows explorer does. +# This is esp. useful with the libgit2_clar project, were usually 2 or more files share +# the same name. +# Sadly, this file grouping is a per-directory option in cmake and not per-target, resulting +# in empty virtual folders "tests-clar" for the git2.dll +FUNCTION(MSVC_SPLIT_SOURCES target) + IF(MSVC_IDE) + GET_TARGET_PROPERTY(sources ${target} SOURCES) + FOREACH(source ${sources}) + IF(source MATCHES ".*/") + STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" rel ${source}) + IF(rel) + STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel}) + IF(rel) + STRING(REPLACE "/" "\\\\" rel ${rel}) + SOURCE_GROUP(${rel} FILES ${source}) + ENDIF() + ENDIF() + ENDIF() + ENDFOREACH() + ENDIF() +ENDFUNCTION() + FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}") @@ -186,6 +209,8 @@ ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SR TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES}) TARGET_OS_LIBRARIES(git2) +MSVC_SPLIT_SOURCES(git2) + SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) @@ -229,6 +254,7 @@ IF (BUILD_CLAR) ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES}) TARGET_OS_LIBRARIES(libgit2_clar) + MSVC_SPLIT_SOURCES(libgit2_clar) IF (MSVC_IDE) # Precompiled headers