]> git.proxmox.com Git - libgit2.git/blame - CMakeLists.txt
Merge pull request #1392 from ethomson/push_test_fix
[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
19a766a2
SC
17# Build options
18#
19OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON )
20OPTION( THREADSAFE "Build libgit2 as threadsafe" OFF )
21OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON )
22OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF )
23OPTION( TAGS "Generate tags" OFF )
24OPTION( PROFILE "Generate profiling information" OFF )
25IF(MSVC)
d335e73a
ET
26 # This option is only availalbe when building with MSVC. By default,
27 # libgit2 is build using the stdcall calling convention, as that's what
28 # the CLR expects by default and how the Windows API is built.
19a766a2 29 #
d335e73a
ET
30 # If you are writing a C or C++ program and want to link to libgit2, you
31 # have to either:
19a766a2 32 # - Add /Gz to the compiler options of _your_ program / library.
d335e73a 33 # - Turn this off by invoking CMake with the "-DSTDCALL=Off" argument.
19a766a2
SC
34 #
35 OPTION( STDCALL "Build libgit2 with the __stdcall convention" ON )
36ENDIF()
37
38# Installation paths
39#
40SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
41SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.")
42SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.")
43
94243295
SC
44FUNCTION(TARGET_OS_LIBRARIES target)
45 IF(WIN32)
46 TARGET_LINK_LIBRARIES(${target} ws2_32)
47 ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
48 TARGET_LINK_LIBRARIES(${target} socket nsl)
49 ENDIF ()
50 IF(THREADSAFE)
51 TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
52 ENDIF()
53ENDFUNCTION()
54
d335e73a
ET
55# For the MSVC IDE, this function splits up the source files like windows
56# explorer does. This is esp. useful with the libgit2_clar project, were
57# usually 2 or more files share the same name. Sadly, this file grouping
58# is a per-directory option in cmake and not per-target, resulting in
59# empty virtual folders "tests-clar" for the git2.dll
523a3ae5
SC
60FUNCTION(MSVC_SPLIT_SOURCES target)
61 IF(MSVC_IDE)
62 GET_TARGET_PROPERTY(sources ${target} SOURCES)
63 FOREACH(source ${sources})
64 IF(source MATCHES ".*/")
65 STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" rel ${source})
66 IF(rel)
67 STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel})
68 IF(rel)
69 STRING(REPLACE "/" "\\\\" rel ${rel})
70 SOURCE_GROUP(${rel} FILES ${source})
71 ENDIF()
72 ENDIF()
73 ENDIF()
74 ENDFOREACH()
75 ENDIF()
76ENDFUNCTION()
77
96fab093 78FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
9d1dcca2
VM
79
80STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
81STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
82STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
83SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
84
7cbdaf7f 85# Find required dependencies
8d457891 86INCLUDE_DIRECTORIES(src include)
20e83aa4 87
3ce22c74
CMN
88IF (WIN32 AND NOT MINGW)
89 ADD_DEFINITIONS(-DGIT_WINHTTP)
90ELSE ()
c57c4af3
SB
91 IF (NOT AMIGA)
92 FIND_PACKAGE(OpenSSL)
93 ENDIF ()
3ce22c74 94 FILE(GLOB SRC_HTTP deps/http-parser/*.c)
8d457891 95 INCLUDE_DIRECTORIES(deps/http-parser)
3ce22c74 96ENDIF()
20e83aa4 97
c3f7a938 98# Specify sha1 implementation
8f09f464 99IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
d6fb0924
ET
100 ADD_DEFINITIONS(-DWIN32_SHA1)
101 FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
102ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
103 ADD_DEFINITIONS(-DOPENSSL_SHA1)
104ELSE()
105 FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
c3f7a938
CY
106ENDIF()
107
3d007f4f 108# Include POSIX regex when it is required
707ede86 109IF(WIN32 OR AMIGA)
c17b1d00
CMN
110 INCLUDE_DIRECTORIES(deps/regex)
111 SET(SRC_REGEX deps/regex/regex.c)
1f4f4d17 112ENDIF()
f443a879 113
b53671ae
SC
114# Optional external dependency: zlib
115IF(NOT ZLIB_LIBRARY)
d335e73a
ET
116 # It's optional, but FIND_PACKAGE gives a warning that looks more like an
117 # error.
b53671ae
SC
118 FIND_PACKAGE(ZLIB QUIET)
119ENDIF()
1f4f4d17
TH
120IF (ZLIB_FOUND)
121 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
122 LINK_LIBRARIES(${ZLIB_LIBRARIES})
b53671ae
SC
123ELSE()
124 MESSAGE( "zlib was not found; using bundled 3rd-party sources." )
1f4f4d17
TH
125 INCLUDE_DIRECTORIES(deps/zlib)
126 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
127 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
c9f79972 128ENDIF()
f443a879 129
502dd2da 130# Platform specific compilation flags
dcd62cb2 131IF (MSVC)
dcd62cb2 132
94155e2f
ET
133 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
134
19be3f9e
PK
135 # /GF - String pooling
136 # /MP - Parallel build
137 SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
138
1b5078f6 139 IF (STDCALL)
19be3f9e
PK
140 # /Gz - stdcall calling convention
141 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
1b5078f6 142 ENDIF ()
19be3f9e
PK
143
144 # /Zi - Create debugging information
145 # /Od - Disable optimization
146 # /D_DEBUG - #define _DEBUG
147 # /MTd - Statically link the multithreaded debug version of the CRT
148 # /RTC1 - Run time checks
149 SET(CMAKE_C_FLAGS_DEBUG "/Zi /Od /D_DEBUG /MTd /RTC1")
150
151 # /MT - Statically link the multithreaded release version of the CRT
152 # /O2 - Optimize for speed
153 # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
154 # /GL - Link time code generation (whole program optimization)
155 # /Gy - Function-level linking
156 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2 /Oy /GL /Gy")
157
158 # /Oy- - Disable frame pointer omission (FPO)
159 SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/Zi /MT /O2 /Oy- /GL /Gy")
160
161 # /O1 - Optimize for size
162 SET(CMAKE_C_FLAGS_MINSIZEREL "/MT /O1 /Oy /GL /Gy")
163
164 # /DYNAMICBASE - Address space load randomization (ASLR)
165 # /NXCOMPAT - Data execution prevention (DEP)
166 # /LARGEADDRESSAWARE - >2GB user address space on x86
167 # /VERSION - Embed version information in PE header
168 SET(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}")
169
170 # /DEBUG - Create a PDB
171 # /LTCG - Link time code generation (whole program optimization)
172 # /OPT:REF /OPT:ICF - Fold out duplicate code at link step
173 # /INCREMENTAL:NO - Required to use /LTCG
174 # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
175 SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
176 SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
177 SET(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
178 SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
179
180 # Same linker settings for DLL as EXE
181 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
182 SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
183 SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
184 SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
185 SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
186
502dd2da 187 SET(WIN_RC "src/win32/git2.rc")
9f75a9ce
BS
188
189 # Precompiled headers
94155e2f 190
e233fa6f 191ELSE ()
4236164a 192 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes ${CMAKE_C_FLAGS}")
19be3f9e
PK
193
194 IF (WIN32 AND NOT CYGWIN)
195 SET(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
196 ENDIF ()
197
2eb18449
SG
198 IF (MINGW) # MinGW always does PIC and complains if we tell it to
199 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
b41e24a6 200 ELSEIF (BUILD_SHARED_LIBS)
2eb18449 201 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
39cdf272 202 ENDIF ()
dbd6850d
RB
203 IF (APPLE) # Apple deprecated OpenSSL
204 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
205 ENDIF ()
706a9974
RB
206 IF (PROFILE)
207 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
208 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
209 ENDIF ()
90412507 210ENDIF()
5b8bb8e7 211
88149fae
PT
212IF( NOT CMAKE_CONFIGURATION_TYPES )
213 # Build Debug by default
214 IF (NOT CMAKE_BUILD_TYPE)
215 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
216 ENDIF ()
217ELSE()
218 # Using a multi-configuration generator eg MSVC or Xcode
219 # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
220ENDIF()
583cf169 221
96ef3d84
CY
222IF (OPENSSL_FOUND)
223 ADD_DEFINITIONS(-DGIT_SSL)
224 INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
225 SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
41cbbea8 226ENDIF()
66024c7c 227
70236bab
VM
228IF (THREADSAFE)
229 IF (NOT WIN32)
230 find_package(Threads REQUIRED)
231 ENDIF()
232
233 ADD_DEFINITIONS(-DGIT_THREADS)
234ENDIF()
235
678e9e04
VM
236ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
237
583cf169 238# Collect sourcefiles
71d33382 239FILE(GLOB SRC_H include/git2/*.h)
583cf169
PD
240
241# On Windows use specific platform sources
5b8bb8e7 242IF (WIN32 AND NOT CYGWIN)
19be3f9e 243 ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0501)
521479b1 244 FILE(GLOB SRC_OS src/win32/*.c)
96ef3d84
CY
245ELSEIF (AMIGA)
246 ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
521479b1 247 FILE(GLOB SRC_OS src/amiga/*.c)
678e9e04 248ELSE()
521479b1
SC
249 FILE(GLOB SRC_OS src/unix/*.c)
250ENDIF()
251FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c)
5b8bb8e7 252
583cf169 253# Compile and link libgit2
521479b1 254ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
94243295
SC
255TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
256TARGET_OS_LIBRARIES(git2)
39cdf272 257
523a3ae5
SC
258MSVC_SPLIT_SOURCES(git2)
259
9d1dcca2
VM
260SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
261SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
4fd486e0 262CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
583cf169 263
26d9e317 264IF (MSVC_IDE)
73aaf674
BS
265 # Precompiled headers
266 SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
267 SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
268ENDIF ()
269
583cf169 270# Install
932d1baf 271INSTALL(TARGETS git2
9795a40f 272 RUNTIME DESTINATION ${BIN_INSTALL_DIR}
b3237ac3
ICQ
273 LIBRARY DESTINATION ${LIB_INSTALL_DIR}
274 ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
583cf169 275)
b3237ac3 276INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
9795a40f
VP
277INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
278INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
583cf169
PD
279
280# Tests
3fd1520c 281IF (BUILD_CLAR)
86a459a8
CB
282 FIND_PACKAGE(PythonInterp REQUIRED)
283
fd29cd13 284 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
3fd1520c 285 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
e0799b6c 286 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
3fd1520c 287 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
e0799b6c 288 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
11385c3c 289
3fd1520c 290 INCLUDE_DIRECTORIES(${CLAR_PATH})
5c2d3f6d 291 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c)
2e6f06a8 292 SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar.c")
f1558d9b 293
86a459a8 294 ADD_CUSTOM_COMMAND(
156cfec0 295 OUTPUT ${CLAR_PATH}/clar.suite
2e6f06a8 296 COMMAND ${PYTHON_EXECUTABLE} generate.py -xonline .
39444bea 297 DEPENDS ${SRC_TEST}
3fd1520c 298 WORKING_DIRECTORY ${CLAR_PATH}
86a459a8 299 )
156cfec0 300
5c2d3f6d 301 SET_SOURCE_FILES_PROPERTIES(
2e6f06a8 302 ${CLAR_PATH}/clar.c
5c2d3f6d
VM
303 PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite)
304
305 ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
306
94243295
SC
307 TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
308 TARGET_OS_LIBRARIES(libgit2_clar)
523a3ae5 309 MSVC_SPLIT_SOURCES(libgit2_clar)
73aaf674 310
156cfec0
VM
311 IF (MSVC_IDE)
312 # Precompiled headers
313 SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
314 ENDIF ()
73aaf674 315
f1558d9b 316 ENABLE_TESTING()
09556895 317 ADD_TEST(libgit2_clar libgit2_clar -ionline)
f1558d9b 318ENDIF ()
e632f687
CB
319
320IF (TAGS)
321 FIND_PROGRAM(CTAGS ctags)
322 IF (NOT CTAGS)
323 message(FATAL_ERROR "Could not find ctags command")
324 ENDIF ()
325
326 FILE(GLOB_RECURSE SRC_ALL *.[ch])
327
328 ADD_CUSTOM_COMMAND(
329 OUTPUT tags
330 COMMAND ${CTAGS} -a ${SRC_ALL}
331 DEPENDS ${SRC_ALL}
332 )
333 ADD_CUSTOM_TARGET(
334 do_tags ALL
335 DEPENDS tags
336 )
337ENDIF ()
62986ff6
SG
338
339IF (BUILD_EXAMPLES)
340 FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
341 ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
c27e2112
ET
342 IF(WIN32)
343 TARGET_LINK_LIBRARIES(cgit2 git2)
344 ELSE()
345 TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
346 ENDIF()
73c46d53 347
62986ff6
SG
348 ADD_EXECUTABLE(git-diff examples/diff.c)
349 TARGET_LINK_LIBRARIES(git-diff git2)
b02c371e 350
62986ff6
SG
351 ADD_EXECUTABLE(git-general examples/general.c)
352 TARGET_LINK_LIBRARIES(git-general git2)
73c46d53 353
62986ff6
SG
354 ADD_EXECUTABLE(git-showindex examples/showindex.c)
355 TARGET_LINK_LIBRARIES(git-showindex git2)
583cf169 356ENDIF ()