]> git.proxmox.com Git - libgit2.git/blame - CMakeLists.txt
zlib: Declare preprocessor directives at build time
[libgit2.git] / CMakeLists.txt
CommitLineData
583cf169 1# CMake build script for the libgit2 project
73c46d53 2#
bfe0658e 3# Building (out of source build):
73c46d53 4# > mkdir build && cd build
bfe0658e
PD
5# > cmake .. [-DSETTINGS=VALUE]
6# > cmake --build .
73c46d53
PD
7#
8# Testing:
9# > ctest -V
10#
11# Install:
bfe0658e 12# > cmake --build . --target install
73c46d53 13
583cf169
PD
14PROJECT(libgit2 C)
15CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
16
71d33382 17FILE(STRINGS "include/git2.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
9d1dcca2
VM
18
19STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
20STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
21STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
22SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
23
7cbdaf7f 24# Find required dependencies
ab6a3d3d 25INCLUDE_DIRECTORIES(deps/zlib src include)
7cbdaf7f 26
5b8bb8e7 27# Installation paths
73c46d53
PD
28SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
29SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
032db4d0 30SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
5b8bb8e7
PD
31
32# Build options
f0890fcc 33OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
583cf169 34OPTION (BUILD_TESTS "Build Tests" ON)
70236bab 35OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
5b8bb8e7 36
90412507 37# Platform specific compilation flags
38IF (MSVC)
8172dd43 39 SET(CMAKE_C_FLAGS "/W4 /WX /nologo /Zi")
e01f7f64
VM
40 # TODO: bring back /RTC1 /RTCc
41 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
90412507 42 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
e233fa6f
CMN
43ELSE ()
44 SET(CMAKE_C_FLAGS "-Wall -Wextra -fPIC")
45 SET(CMAKE_C_FLAGS_DEBUG "-g -O0")
46 SET(CMAKE_C_FLAGS_RELEASE "-O2")
90412507 47ENDIF()
48
3d96996d 49# Build Debug by default
583cf169 50IF (NOT CMAKE_BUILD_TYPE)
1a7b52dc 51 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
583cf169
PD
52ENDIF ()
53
70236bab
VM
54IF (THREADSAFE)
55 IF (NOT WIN32)
56 find_package(Threads REQUIRED)
57 ENDIF()
58
59 ADD_DEFINITIONS(-DGIT_THREADS)
60ENDIF()
61
583cf169 62# Collect sourcefiles
17d52304 63FILE(GLOB SRC src/*.c)
ab6a3d3d 64FILE(GLOB SRC_ZLIB deps/zlib/*.c)
71d33382 65FILE(GLOB SRC_H include/git2/*.h)
583cf169
PD
66
67# On Windows use specific platform sources
5b8bb8e7 68IF (WIN32 AND NOT CYGWIN)
ab6a3d3d 69 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_LIB)
17d52304 70 FILE(GLOB SRC src/*.c src/win32/*.c)
5b8bb8e7
PD
71ENDIF ()
72
ec626853 73ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -DNO_VIZ -DSTDC -DNO_GZIP)
b2cef77c 74
583cf169 75# Compile and link libgit2
17d52304 76ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB})
953e1f93 77TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT})
9d1dcca2
VM
78SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
79SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
583cf169
PD
80
81# Install
82INSTALL(TARGETS git2
73c46d53
PD
83 RUNTIME DESTINATION ${INSTALL_BIN}
84 LIBRARY DESTINATION ${INSTALL_LIB}
85 ARCHIVE DESTINATION ${INSTALL_LIB}
583cf169 86)
71d33382
VM
87INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
88INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
583cf169
PD
89
90# Tests
953e1f93
VM
91IF (BUILD_TESTS)
92 SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
93 ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")
94
95 INCLUDE_DIRECTORIES(tests)
96 FILE(GLOB SRC_TEST tests/t??-*.c)
97
17d52304 98 ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB})
953e1f93 99 TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
f64586fa
VM
100
101 ENABLE_TESTING()
102 ADD_TEST(libgit2_test libgit2_test)
953e1f93 103ENDIF ()