]> git.proxmox.com Git - libgit2.git/blame - CMakeLists.txt
Merge pull request #764 from sleeper/issue_763
[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
96fab093 17FILE(STRINGS "include/git2/version.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)
c17b1d00
CMN
31ELSE()
32 # Windows doesn't understand POSIX regex on its own
33 INCLUDE_DIRECTORIES(deps/regex)
34 SET(SRC_REGEX deps/regex/regex.c)
1f4f4d17
TH
35ENDIF()
36
37IF (ZLIB_FOUND)
38 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
39 LINK_LIBRARIES(${ZLIB_LIBRARIES})
40ELSE (ZLIB_FOUND)
41 INCLUDE_DIRECTORIES(deps/zlib)
42 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
43 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
44ENDIF()
7cbdaf7f 45
5b8bb8e7 46# Installation paths
73c46d53
PD
47SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
48SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
032db4d0 49SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
5b8bb8e7
PD
50
51# Build options
f0890fcc 52OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
70236bab 53OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
fd29cd13 54OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
62986ff6 55OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
e632f687 56OPTION (TAGS "Generate tags" OFF)
706a9974 57OPTION (PROFILE "Generate profiling information" OFF)
5b8bb8e7 58
502dd2da 59# Platform specific compilation flags
dcd62cb2 60IF (MSVC)
502dd2da 61 # Not using __stdcall with the CRT causes problems
dcd62cb2 62 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
dcd62cb2 63
06c081e1 64 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
1b5078f6
CMN
65 IF (STDCALL)
66 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
67 ENDIF ()
9f75a9ce 68 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
90412507 69 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
502dd2da 70 SET(WIN_RC "src/win32/git2.rc")
9f75a9ce
BS
71
72 # Precompiled headers
e233fa6f 73ELSE ()
2eb18449 74 SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
4ce16ed8 75 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
2eb18449
SG
76 IF (MINGW) # MinGW always does PIC and complains if we tell it to
77 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
78 ELSE ()
79 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
39cdf272 80 ENDIF ()
706a9974
RB
81 IF (PROFILE)
82 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
83 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
84 ENDIF ()
90412507 85ENDIF()
86
3d96996d 87# Build Debug by default
583cf169 88IF (NOT CMAKE_BUILD_TYPE)
1a7b52dc 89 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
583cf169
PD
90ENDIF ()
91
a6f24a5b
CMN
92FIND_PACKAGE(OpenSSL)
93IF (OPENSSL_FOUND)
a6f24a5b
CMN
94 ADD_DEFINITIONS(-DGIT_SSL)
95 INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
96 SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
66024c7c
CMN
97ENDIF()
98
70236bab
VM
99IF (THREADSAFE)
100 IF (NOT WIN32)
101 find_package(Threads REQUIRED)
102 ENDIF()
103
104 ADD_DEFINITIONS(-DGIT_THREADS)
105ENDIF()
106
678e9e04
VM
107ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
108
583cf169 109# Collect sourcefiles
71d33382 110FILE(GLOB SRC_H include/git2/*.h)
583cf169
PD
111
112# On Windows use specific platform sources
5b8bb8e7 113IF (WIN32 AND NOT CYGWIN)
901fbdad 114 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501)
ec42eafd
SG
115 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c)
116ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
117 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c)
678e9e04 118ELSE()
3a5ad90a 119 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
5b8bb8e7
PD
120ENDIF ()
121
583cf169 122# Compile and link libgit2
c17b1d00 123ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC})
39cdf272
CMN
124
125IF (WIN32)
126 TARGET_LINK_LIBRARIES(git2 ws2_32)
e944cfa9
CMN
127ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
128 TARGET_LINK_LIBRARIES(git2 socket nsl)
39cdf272
CMN
129ENDIF ()
130
a6f24a5b 131TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
9d1dcca2
VM
132SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
133SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
4fd486e0 134CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
583cf169
PD
135
136# Install
932d1baf 137INSTALL(TARGETS git2
86a459a8
CB
138 RUNTIME DESTINATION ${INSTALL_BIN}
139 LIBRARY DESTINATION ${INSTALL_LIB}
140 ARCHIVE DESTINATION ${INSTALL_LIB}
583cf169 141)
4fd486e0 142INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
71d33382
VM
143INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
144INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
583cf169
PD
145
146# Tests
3fd1520c 147IF (BUILD_CLAR)
e0799b6c 148
86a459a8
CB
149 FIND_PACKAGE(PythonInterp REQUIRED)
150
fd29cd13 151 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
3fd1520c 152 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
e0799b6c 153 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
3fd1520c 154 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
e0799b6c 155 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
11385c3c 156
3fd1520c
VM
157 INCLUDE_DIRECTORIES(${CLAR_PATH})
158 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
f1558d9b 159
86a459a8 160 ADD_CUSTOM_COMMAND(
3fd1520c
VM
161 OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
162 COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
163 DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
164 WORKING_DIRECTORY ${CLAR_PATH}
86a459a8 165 )
c17b1d00 166 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
a6f24a5b 167 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
f1558d9b 168 IF (WIN32)
3fd1520c 169 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
f1558d9b 170 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
3fd1520c 171 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
f1558d9b
VM
172 ENDIF ()
173
174 ENABLE_TESTING()
3fd1520c 175 ADD_TEST(libgit2_clar libgit2_clar)
f1558d9b 176ENDIF ()
e632f687
CB
177
178IF (TAGS)
179 FIND_PROGRAM(CTAGS ctags)
180 IF (NOT CTAGS)
181 message(FATAL_ERROR "Could not find ctags command")
182 ENDIF ()
183
184 FILE(GLOB_RECURSE SRC_ALL *.[ch])
185
186 ADD_CUSTOM_COMMAND(
187 OUTPUT tags
188 COMMAND ${CTAGS} -a ${SRC_ALL}
189 DEPENDS ${SRC_ALL}
190 )
191 ADD_CUSTOM_TARGET(
192 do_tags ALL
193 DEPENDS tags
194 )
195ENDIF ()
62986ff6
SG
196
197IF (BUILD_EXAMPLES)
198 FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
199 ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
200 TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
201
202 ADD_EXECUTABLE(git-diff examples/diff.c)
203 TARGET_LINK_LIBRARIES(git-diff git2)
204
205 ADD_EXECUTABLE(git-general examples/general.c)
206 TARGET_LINK_LIBRARIES(git-general git2)
207
208 ADD_EXECUTABLE(git-showindex examples/showindex.c)
209 TARGET_LINK_LIBRARIES(git-showindex git2)
210ENDIF ()