]> git.proxmox.com Git - libgit2.git/blobdiff - CMakeLists.txt
transports: smart: fix memory leak on OOM path
[libgit2.git] / CMakeLists.txt
index 994eed5e11eb0e4d314c88aa9601bc43054badfe..0aa58625a8820e8a439882eac868d6cb2cb4a5d8 100644 (file)
 # > cmake --build . --target install
 
 PROJECT(libgit2 C)
-CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
 CMAKE_POLICY(SET CMP0015 NEW)
 
 # Add find modules to the path
 SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
 
 INCLUDE(CheckLibraryExists)
+INCLUDE(CheckFunctionExists)
+INCLUDE(CheckStructHasMember)
 INCLUDE(AddCFlagIfSupported)
 INCLUDE(FindPkgConfig)
 
@@ -59,6 +61,10 @@ IF(MSVC)
        # are linking statically
        OPTION( STATIC_CRT              "Link the static CRT libraries" ON  )
 
+       # If you want to embed a copy of libssh2 into libgit2, pass a
+       # path to libssh2
+       OPTION( EMBED_SSH_PATH          "Path to libssh2 to embed (Windows)" OFF )
+
        ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
        ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
        ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
@@ -80,6 +86,23 @@ IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        OPTION( USE_OPENSSL                     "Link with and use openssl library"             ON )
 ENDIF()
 
+CHECK_STRUCT_HAS_MEMBER ("struct stat" st_atim "sys/types.h;sys/stat.h"
+       HAVE_STRUCT_STAT_ST_ATIM LANGUAGE C)
+CHECK_STRUCT_HAS_MEMBER ("struct stat" st_atimespec "sys/types.h;sys/stat.h"
+       HAVE_STRUCT_STAT_ST_ATIMESPEC LANGUAGE C)
+
+IF (HAVE_STRUCT_STAT_ST_ATIM)
+       CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec sys/stat.h
+               HAVE_STRUCT_STAT_NSEC LANGUAGE C)
+ELSEIF (HAVE_STRUCT_STAT_ST_ATIMESPEC)
+       CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec sys/stat.h
+               HAVE_STRUCT_STAT_NSEC LANGUAGE C)
+ENDIF()
+
+IF (HAVE_STRUCT_STAT_NSEC OR WIN32)
+       OPTION( USE_NSEC                "Care about sub-second file mtimes and ctimes"  OFF )
+ENDIF()
+
 # This variable will contain the libraries we need to put into
 # libgit2.pc's Requires.private. That is, what we're linking to or
 # what someone who's statically linking us needs to link to.
@@ -95,6 +118,23 @@ 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.")
 
+# Set a couple variables to be substituted inside the .pc file.
+# We can't just use LIB_INSTALL_DIR in the .pc file, as passing them as absolue
+# or relative paths is both valid and supported by cmake.
+SET (PKGCONFIG_PREFIX ${CMAKE_INSTALL_PREFIX})
+
+IF(IS_ABSOLUTE ${LIB_INSTALL_DIR})
+  SET (PKGCONFIG_LIBDIR ${LIB_INSTALL_DIR})
+ELSE(IS_ABSOLUTE ${LIB_INSTALL_DIR})
+  SET (PKGCONFIG_LIBDIR "\${prefix}/${LIB_INSTALL_DIR}")
+ENDIF (IS_ABSOLUTE ${LIB_INSTALL_DIR})
+
+IF(IS_ABSOLUTE ${INCLUDE_INSTALL_DIR})
+  SET (PKGCONFIG_INCLUDEDIR ${INCLUDE_INSTALL_DIR})
+ELSE(IS_ABSOLUTE ${INCLUDE_INSTALL_DIR})
+  SET (PKGCONFIG_INCLUDEDIR "\${prefix}/${INCLUDE_INSTALL_DIR}")
+ENDIF(IS_ABSOLUTE ${INCLUDE_INSTALL_DIR})
+
 FUNCTION(TARGET_OS_LIBRARIES target)
        IF(WIN32)
                TARGET_LINK_LIBRARIES(${target} ws2_32)
@@ -115,13 +155,13 @@ 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" for the git2.dll
-FUNCTION(MSVC_SPLIT_SOURCES target)
-       IF(MSVC_IDE)
+# This function splits the sources files up into their appropriate
+# subdirectories.  This is especially useful for IDEs like Xcode and
+# Visual Studio, so that you can navigate into the libgit2_clar project,
+# and see the folders within the tests folder (instead of just seeing all
+# source and tests in a single folder.)
+FUNCTION(IDE_SPLIT_SOURCES target)
+       IF(MSVC_IDE OR CMAKE_GENERATOR STREQUAL Xcode)
                GET_TARGET_PROPERTY(sources ${target} SOURCES)
                FOREACH(source ${sources})
                        IF(source MATCHES ".*/")
@@ -152,8 +192,18 @@ STRING(REGEX REPLACE "^.*LIBGIT2_SOVERSION ([0-9]+)$" "\\1" LIBGIT2_SOVERSION "$
 INCLUDE_DIRECTORIES(src include)
 
 IF (SECURITY_FOUND)
-  MESSAGE("-- Found Security ${SECURITY_DIRS}")
-  LIST(APPEND LIBGIT2_PC_LIBS "-framework Security")
+  # OS X 10.7 and older do not have some functions we use, fall back to OpenSSL there
+  CHECK_LIBRARY_EXISTS("${SECURITY_DIRS}" SSLCreateContext "Security/SecureTransport.h" HAVE_NEWER_SECURITY)
+  IF (HAVE_NEWER_SECURITY)
+    MESSAGE("-- Found Security ${SECURITY_DIRS}")
+    LIST(APPEND LIBGIT2_PC_LIBS "-framework Security")
+  ELSE()
+    MESSAGE("-- Security framework is too old, falling back to OpenSSL")
+    SET(SECURITY_FOUND "NO")
+    SET(SECURITY_DIRS "")
+    SET(SECURITY_DIR "")
+    SET(USE_OPENSSL "ON")
+  ENDIF()
 ENDIF()
 
 IF (COREFOUNDATION_FOUND)
@@ -162,6 +212,13 @@ IF (COREFOUNDATION_FOUND)
 ENDIF()
 
 
+IF (WIN32 AND EMBED_SSH_PATH)
+       FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
+       INCLUDE_DIRECTORIES("${EMBED_SSH_PATH}/include")
+       FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
+       ADD_DEFINITIONS(-DGIT_SSH)
+ENDIF()
+
 IF (WIN32 AND WINHTTP)
        ADD_DEFINITIONS(-DGIT_WINHTTP)
        INCLUDE_DIRECTORIES(deps/http-parser)
@@ -178,7 +235,7 @@ IF (WIN32 AND WINHTTP)
                SET(LIBWINHTTP_PATH "${CMAKE_CURRENT_BINARY_DIR}/deps/winhttp")
                FILE(MAKE_DIRECTORY ${LIBWINHTTP_PATH})
 
-               IF ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
+               IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
                        set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp64.def")
                ELSE()
                        set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp.def")
@@ -200,7 +257,8 @@ IF (WIN32 AND WINHTTP)
                LINK_DIRECTORIES(${LIBWINHTTP_PATH})
        ENDIF ()
 
-       LINK_LIBRARIES(winhttp rpcrt4 crypt32)
+       LINK_LIBRARIES(winhttp rpcrt4 crypt32 ole32)
+       LIST(APPEND LIBGIT2_PC_LIBS "-lwinhttp" "-lrpcrt4" "-lcrypt32" "-lole32")
 ELSE ()
        IF (CURL)
                PKG_CHECK_MODULES(CURL libcurl)
@@ -286,7 +344,7 @@ IF (LIBSSH2_FOUND)
        #SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} ${LIBSSH2_LDFLAGS}")
        SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
 
-       CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
+       CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
        IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)
                ADD_DEFINITIONS(-DGIT_SSH_MEMORY_CREDENTIALS)
        ENDIF()
@@ -413,6 +471,7 @@ ELSE ()
                SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
        ENDIF ()
 
+       ADD_C_FLAG_IF_SUPPORTED(-Wdocumentation)
        ADD_C_FLAG_IF_SUPPORTED(-Wno-missing-field-initializers)
        ADD_C_FLAG_IF_SUPPORTED(-Wstrict-aliasing=2)
        ADD_C_FLAG_IF_SUPPORTED(-Wstrict-prototypes)
@@ -430,6 +489,21 @@ ELSE ()
        ENDIF ()
 ENDIF()
 
+CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS)
+IF (HAVE_FUTIMENS)
+       ADD_DEFINITIONS(-DHAVE_FUTIMENS)
+ENDIF ()
+
+CHECK_FUNCTION_EXISTS(qsort_r HAVE_QSORT_R)
+IF (HAVE_QSORT_R)
+       ADD_DEFINITIONS(-DHAVE_QSORT_R)
+ENDIF ()
+
+CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S)
+IF (HAVE_QSORT_S)
+       ADD_DEFINITIONS(-DHAVE_QSORT_S)
+ENDIF ()
+
 IF( NOT CMAKE_CONFIGURATION_TYPES )
        # Build Debug by default
        IF (NOT CMAKE_BUILD_TYPE)
@@ -461,6 +535,14 @@ IF (THREADSAFE)
        ADD_DEFINITIONS(-DGIT_THREADS)
 ENDIF()
 
+IF (USE_NSEC)
+       ADD_DEFINITIONS(-DGIT_USE_NSEC)
+ENDIF()
+
+IF (HAVE_STRUCT_STAT_ST_ATIMESPEC)
+       ADD_DEFINITIONS(-DGIT_USE_STAT_ATIMESPEC)
+ENDIF()
+
 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
 
 # Collect sourcefiles
@@ -490,7 +572,7 @@ ELSE()
 ENDIF()
 
 # Compile and link libgit2
-ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
+ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1} ${WIN_RC})
 TARGET_LINK_LIBRARIES(git2 ${SECURITY_DIRS})
 TARGET_LINK_LIBRARIES(git2 ${COREFOUNDATION_DIRS})
 TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
@@ -505,7 +587,7 @@ IF(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
   SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
 ENDIF()
 
-MSVC_SPLIT_SOURCES(git2)
+IDE_SPLIT_SOURCES(git2)
 
 IF (SONAME)
        SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
@@ -536,7 +618,12 @@ INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
 
 # Tests
 IF (BUILD_CLAR)
-       FIND_PACKAGE(PythonInterp REQUIRED)
+       FIND_PACKAGE(PythonInterp)
+
+       IF(NOT PYTHONINTERP_FOUND)
+         MESSAGE(FATAL_ERROR "Could not find a python interpeter, which is needed to build the tests. "
+           "Make sure python is available, or pass -DBUILD_CLAR=OFF to skip building the tests")
+       ENDIF()
 
        SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
        SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests")
@@ -560,7 +647,7 @@ IF (BUILD_CLAR)
                ${CLAR_PATH}/clar.c
                PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite)
 
-       ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
+       ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
 
        TARGET_LINK_LIBRARIES(libgit2_clar ${COREFOUNDATION_DIRS})
        TARGET_LINK_LIBRARIES(libgit2_clar ${SECURITY_DIRS})
@@ -569,7 +656,7 @@ IF (BUILD_CLAR)
        TARGET_LINK_LIBRARIES(libgit2_clar ${GSSAPI_LIBRARIES})
        TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES})
        TARGET_OS_LIBRARIES(libgit2_clar)
-       MSVC_SPLIT_SOURCES(libgit2_clar)
+       IDE_SPLIT_SOURCES(libgit2_clar)
 
        IF (MSVC_IDE)
                # Precompiled headers