]> git.proxmox.com Git - libgit2.git/blame - CMakeLists.txt
libgit2 v0.15.0 "Das Wunderbar Release"
[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 .
932d1baf 7#
73c46d53
PD
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
b8a8191f
CMN
25INCLUDE_DIRECTORIES(src include deps/http-parser)
26
27FILE(GLOB SRC_HTTP deps/http-parser/*.c)
28
1f4f4d17
TH
29IF (NOT WIN32)
30 FIND_PACKAGE(ZLIB)
31ENDIF()
32
33IF (ZLIB_FOUND)
34 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
35 LINK_LIBRARIES(${ZLIB_LIBRARIES})
36ELSE (ZLIB_FOUND)
37 INCLUDE_DIRECTORIES(deps/zlib)
38 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
39 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
40ENDIF()
7cbdaf7f 41
5b8bb8e7 42# Installation paths
73c46d53
PD
43SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
44SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
032db4d0 45SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
5b8bb8e7
PD
46
47# Build options
f0890fcc 48OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
70236bab 49OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
1b5078f6 50OPTION (STDCALL "Buildl libgit2 with the __stdcall convention (Windows)" ON)
d3fb6a81
VM
51OPTION (BUILD_TESTS "Build Tests" ON)
52OPTION (BUILD_CLAY "Build Tests using the Clay suite" OFF)
5b8bb8e7 53
90412507 54# Platform specific compilation flags
55IF (MSVC)
5888860d 56 SET(CMAKE_C_FLAGS "/W4 /nologo /Zi")
1b5078f6
CMN
57 IF (STDCALL)
58 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
59 ENDIF ()
e01f7f64
VM
60 # TODO: bring back /RTC1 /RTCc
61 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
90412507 62 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
e233fa6f 63ELSE ()
d568d585 64 SET(CMAKE_C_FLAGS "-O2 -g -Wall -Wextra -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes")
39cdf272
CMN
65 IF (NOT MINGW) # MinGW always does PIC and complains if we tell it to
66 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
67 ENDIF ()
90412507 68ENDIF()
69
3d96996d 70# Build Debug by default
583cf169 71IF (NOT CMAKE_BUILD_TYPE)
1a7b52dc 72 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
583cf169
PD
73ENDIF ()
74
70236bab
VM
75IF (THREADSAFE)
76 IF (NOT WIN32)
77 find_package(Threads REQUIRED)
78 ENDIF()
79
80 ADD_DEFINITIONS(-DGIT_THREADS)
81ENDIF()
82
678e9e04
VM
83ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
84
583cf169 85# Collect sourcefiles
71d33382 86FILE(GLOB SRC_H include/git2/*.h)
583cf169
PD
87
88# On Windows use specific platform sources
5b8bb8e7 89IF (WIN32 AND NOT CYGWIN)
ab6a3d3d 90 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_LIB)
17d52304 91 FILE(GLOB SRC src/*.c src/win32/*.c)
678e9e04
VM
92ELSE()
93 FILE(GLOB SRC src/*.c src/unix/*.c)
5b8bb8e7
PD
94ENDIF ()
95
583cf169 96# Compile and link libgit2
b8a8191f 97ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP})
39cdf272
CMN
98
99IF (WIN32)
100 TARGET_LINK_LIBRARIES(git2 ws2_32)
e944cfa9
CMN
101ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
102 TARGET_LINK_LIBRARIES(git2 socket nsl)
39cdf272
CMN
103ENDIF ()
104
953e1f93 105TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT})
9d1dcca2
VM
106SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
107SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
4fd486e0 108CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
583cf169
PD
109
110# Install
932d1baf 111INSTALL(TARGETS git2
73c46d53
PD
112 RUNTIME DESTINATION ${INSTALL_BIN}
113 LIBRARY DESTINATION ${INSTALL_LIB}
114 ARCHIVE DESTINATION ${INSTALL_LIB}
583cf169 115)
4fd486e0 116INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
71d33382
VM
117INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
118INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
583cf169
PD
119
120# Tests
953e1f93 121IF (BUILD_TESTS)
11385c3c
VM
122 SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
123 ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")
124
953e1f93
VM
125 INCLUDE_DIRECTORIES(tests)
126 FILE(GLOB SRC_TEST tests/t??-*.c)
127
b8a8191f 128 ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP})
953e1f93 129 TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
e944cfa9
CMN
130 IF (WIN32)
131 TARGET_LINK_LIBRARIES(libgit2_test ws2_32)
132 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
133 TARGET_LINK_LIBRARIES(libgit2_test socket nsl)
134 ENDIF ()
f64586fa
VM
135
136 ENABLE_TESTING()
137 ADD_TEST(libgit2_test libgit2_test)
953e1f93 138ENDIF ()
f1558d9b
VM
139
140IF (BUILD_CLAY)
11385c3c
VM
141 SET(CLAY_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
142 ADD_DEFINITIONS(-DCLAY_FIXTURE_PATH=\"${CLAY_FIXTURES}\")
143
f1558d9b
VM
144 INCLUDE_DIRECTORIES(tests-clay)
145 FILE(GLOB_RECURSE SRC_TEST tests-clay/*.c)
146
87a26ad5 147 ADD_EXECUTABLE(libgit2_clay ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP})
48e97ed1 148 TARGET_LINK_LIBRARIES(libgit2_clay ${CMAKE_THREAD_LIBS_INIT})
f1558d9b 149 IF (WIN32)
48e97ed1 150 TARGET_LINK_LIBRARIES(libgit2_clay ws2_32)
f1558d9b 151 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
48e97ed1 152 TARGET_LINK_LIBRARIES(libgit2_clay socket nsl)
f1558d9b
VM
153 ENDIF ()
154
155 ENABLE_TESTING()
48e97ed1 156 ADD_TEST(libgit2_clay libgit2_clay)
f1558d9b 157ENDIF ()