]> git.proxmox.com Git - libgit2.git/blame - CMakeLists.txt
New upstream version 0.27.7+dfsg.1
[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 14PROJECT(libgit2 C)
eae0bfdc 15CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
8f426d7d 16CMAKE_POLICY(SET CMP0015 NEW)
eae0bfdc
PP
17IF (POLICY CMP0051)
18 CMAKE_POLICY(SET CMP0051 NEW)
19ENDIF()
20IF (POLICY CMP0042)
21 CMAKE_POLICY(SET CMP0042 NEW)
22ENDIF()
583cf169 23
7a6e0281 24# Add find modules to the path
eae0bfdc 25SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake/Modules/")
7369b3c3 26
5bda607c 27INCLUDE(CheckLibraryExists)
8649dfd8 28INCLUDE(CheckFunctionExists)
ab96ca55 29INCLUDE(CheckSymbolExists)
e9e6df2c 30INCLUDE(CheckStructHasMember)
6c7cee42 31INCLUDE(CheckPrototypeDefinition) # Added in CMake 3.0
5c8d5eac 32INCLUDE(AddCFlagIfSupported)
4b3ec53c 33INCLUDE(FindPkgLibraries)
eae0bfdc
PP
34INCLUDE(FindThreads)
35INCLUDE(FindStatNsec)
36INCLUDE(IdeSplitSources)
37INCLUDE(FeatureSummary)
38INCLUDE(EnableWarnings)
5bda607c 39
19a766a2
SC
40# Build options
41#
058956ce 42OPTION( SONAME "Set the (SO)VERSION of the target" ON )
19a766a2 43OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON )
79b0ae4e 44OPTION( THREADSAFE "Build libgit2 as threadsafe" ON )
19a766a2
SC
45OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON )
46OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF )
47OPTION( TAGS "Generate tags" OFF )
48OPTION( PROFILE "Generate profiling information" OFF )
b5ec5430 49OPTION( ENABLE_TRACE "Enables tracing support" OFF )
92dac975 50OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
c1cf1af4 51
eae0bfdc 52SET(SHA1_BACKEND "CollisionDetection" CACHE STRING "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, CollisionDetection. ")
73291aff 53OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
eae0bfdc 54OPTION( USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON )
23135afa 55OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )
7697e541 56OPTION( VALGRIND "Configure build for valgrind" OFF )
13b0b7d5 57OPTION( CURL "Use curl for HTTP if available" ON)
eae0bfdc 58OPTION( USE_EXT_HTTP_PARSER "Use system HTTP_Parser if available" ON)
ed0571f8 59OPTION( DEBUG_POOL "Enable debug pool allocator" OFF )
eae0bfdc
PP
60OPTION( ENABLE_WERROR "Enable compilation with -Werror" OFF )
61OPTION( USE_BUNDLED_ZLIB "Use the bundled version of zlib" OFF )
ed0571f8 62
eae0bfdc
PP
63IF (UNIX AND NOT APPLE)
64 OPTION( ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF )
ed0571f8 65ENDIF()
73291aff 66
eae0bfdc
PP
67IF (APPLE)
68 OPTION( USE_ICONV "Link with and use iconv library" ON )
92dac975 69ENDIF()
82b2fc2c 70
19a766a2 71IF(MSVC)
275d8d55 72 # This option is only available when building with MSVC. By default, libgit2
e49dc687
BS
73 # is build using the cdecl calling convention, which is useful if you're
74 # writing C. However, the CLR and Win32 API both expect stdcall.
19a766a2 75 #
e49dc687
BS
76 # If you are writing a CLR program and want to link to libgit2, you'll want
77 # to turn this on by invoking CMake with the "-DSTDCALL=ON" argument.
78 OPTION( STDCALL "Build libgit2 with the __stdcall convention" OFF )
08f32085
Q
79
80 # This option must match the settings used in your program, in particular if you
81 # are linking statically
82 OPTION( STATIC_CRT "Link the static CRT libraries" ON )
1bfe7133 83
01fe8374
CMN
84 # If you want to embed a copy of libssh2 into libgit2, pass a
85 # path to libssh2
86 OPTION( EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF )
19a766a2
SC
87ENDIF()
88
6bb54cbf 89
8f426d7d
PK
90IF(WIN32)
91 # By default, libgit2 is built with WinHTTP. To use the built-in
92 # HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
93 OPTION( WINHTTP "Use Win32 WinHTTP routines" ON )
94ENDIF()
95
d06c589f
JH
96IF(MSVC)
97 # Enable MSVC CRTDBG memory leak reporting when in debug mode.
98 OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
99ENDIF()
100
eae0bfdc 101FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
9d1dcca2
VM
102
103STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
104STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
105STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
106SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
107
eae0bfdc 108FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER_SOVERSION REGEX "^#define LIBGIT2_SOVERSION [0-9]+$")
99807672
CMN
109STRING(REGEX REPLACE "^.*LIBGIT2_SOVERSION ([0-9]+)$" "\\1" LIBGIT2_SOVERSION "${GIT2_HEADER_SOVERSION}")
110
502dd2da 111# Platform specific compilation flags
dcd62cb2 112IF (MSVC)
eae0bfdc
PP
113 ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
114 ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
115 ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
dcd62cb2 116
ccf1a2ba 117 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
94155e2f 118
19be3f9e
PK
119 # /GF - String pooling
120 # /MP - Parallel build
121 SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
122
1b5078f6 123 IF (STDCALL)
19be3f9e
PK
124 # /Gz - stdcall calling convention
125 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
1b5078f6 126 ENDIF ()
19be3f9e 127
08f32085
Q
128 IF (STATIC_CRT)
129 SET(CRT_FLAG_DEBUG "/MTd")
130 SET(CRT_FLAG_RELEASE "/MT")
131 ELSE()
ccf1a2ba 132 SET(CRT_FLAG_DEBUG "/MDd")
08f32085
Q
133 SET(CRT_FLAG_RELEASE "/MD")
134 ENDIF()
135
d06c589f 136 IF (MSVC_CRTDBG)
eae0bfdc
PP
137 SET(GIT_MSVC_CRTDBG 1)
138 SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
139 SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
d06c589f
JH
140 ENDIF()
141
19be3f9e
PK
142 # /Zi - Create debugging information
143 # /Od - Disable optimization
144 # /D_DEBUG - #define _DEBUG
145 # /MTd - Statically link the multithreaded debug version of the CRT
08f32085 146 # /MDd - Dynamically link the multithreaded debug version of the CRT
19be3f9e 147 # /RTC1 - Run time checks
08f030ce 148 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
19be3f9e 149
69c28b75 150 # /DNDEBUG - Disables asserts
19be3f9e 151 # /MT - Statically link the multithreaded release version of the CRT
08f32085 152 # /MD - Dynamically link the multithreaded release version of the CRT
19be3f9e
PK
153 # /O2 - Optimize for speed
154 # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
155 # /GL - Link time code generation (whole program optimization)
156 # /Gy - Function-level linking
08f32085 157 SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
19be3f9e
PK
158
159 # /Oy- - Disable frame pointer omission (FPO)
08f32085 160 SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
19be3f9e
PK
161
162 # /O1 - Optimize for size
08f32085 163 SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
19be3f9e
PK
164
165 # /DYNAMICBASE - Address space load randomization (ASLR)
166 # /NXCOMPAT - Data execution prevention (DEP)
167 # /LARGEADDRESSAWARE - >2GB user address space on x86
168 # /VERSION - Embed version information in PE header
169 SET(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}")
170
171 # /DEBUG - Create a PDB
172 # /LTCG - Link time code generation (whole program optimization)
173 # /OPT:REF /OPT:ICF - Fold out duplicate code at link step
174 # /INCREMENTAL:NO - Required to use /LTCG
175 # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
176 SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
177 SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
178 SET(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
179 SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
180
181 # Same linker settings for DLL as EXE
182 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
183 SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
184 SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
185 SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
186 SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
e233fa6f 187ELSE ()
eae0bfdc
PP
188 IF (ENABLE_REPRODUCIBLE_BUILDS)
189 SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
190 SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
191 SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
192 ENDIF()
193
a02e8a38
PS
194 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
195
eae0bfdc
PP
196 ENABLE_WARNINGS(all)
197 ENABLE_WARNINGS(extra)
19be3f9e 198
98b8fcff
JG
199 IF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
200 SET(CMAKE_C_FLAGS "-std=c99 -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
201 ENDIF()
202
a64532e1 203 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
19be3f9e 204
b6a2fd0e 205 IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
2eb18449 206 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
b41e24a6 207 ELSEIF (BUILD_SHARED_LIBS)
5c8d5eac 208 ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden)
c6cd3f8b
JG
209
210 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
39cdf272 211 ENDIF ()
c6cd3f8b 212
b6a2fd0e
PS
213 IF (MINGW)
214 # MinGW >= 3.14 uses the C99-style stdio functions
215 # automatically, but forks like mingw-w64 still want
216 # us to define this in order to use them
217 ADD_DEFINITIONS(-D__USE_MINGW_ANSI_STDIO=1)
218 ENDIF ()
219
eae0bfdc
PP
220 ENABLE_WARNINGS(documentation)
221 DISABLE_WARNINGS(missing-field-initializers)
222 ENABLE_WARNINGS(strict-aliasing)
223 ENABLE_WARNINGS(strict-prototypes)
224 ENABLE_WARNINGS(declaration-after-statement)
225 ENABLE_WARNINGS(shift-count-overflow)
226 DISABLE_WARNINGS(unused-const-variable)
227 DISABLE_WARNINGS(unused-function)
6c7cee42
RD
228 ENABLE_WARNINGS(format)
229 ENABLE_WARNINGS(format-security)
230 ENABLE_WARNINGS(int-conversion)
c6cd3f8b 231
364ef528 232 IF (APPLE) # Apple deprecated OpenSSL
eae0bfdc 233 DISABLE_WARNINGS(deprecated-declarations)
c6cd3f8b 234 ENDIF()
823c0e9c 235
706a9974
RB
236 IF (PROFILE)
237 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
238 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
239 ENDIF ()
90412507 240ENDIF()
5b8bb8e7 241
88149fae
PT
242IF( NOT CMAKE_CONFIGURATION_TYPES )
243 # Build Debug by default
244 IF (NOT CMAKE_BUILD_TYPE)
245 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
246 ENDIF ()
247ELSE()
248 # Using a multi-configuration generator eg MSVC or Xcode
249 # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
250ENDIF()
583cf169 251
eae0bfdc 252ADD_SUBDIRECTORY(src)
678e9e04 253
eae0bfdc
PP
254# Tests
255IF (NOT MSVC)
256 IF (NOT BUILD_SHARED_LIBS)
257 SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
258 ENDIF()
73aaf674
BS
259ENDIF ()
260
3fd1520c 261IF (BUILD_CLAR)
f1558d9b 262 ENABLE_TESTING()
eae0bfdc 263 ADD_SUBDIRECTORY(tests)
f1558d9b 264ENDIF ()
e632f687
CB
265
266IF (TAGS)
267 FIND_PROGRAM(CTAGS ctags)
268 IF (NOT CTAGS)
48ce93e0 269 MESSAGE(FATAL_ERROR "Could not find ctags command")
e632f687
CB
270 ENDIF ()
271
272 FILE(GLOB_RECURSE SRC_ALL *.[ch])
273
274 ADD_CUSTOM_COMMAND(
275 OUTPUT tags
276 COMMAND ${CTAGS} -a ${SRC_ALL}
277 DEPENDS ${SRC_ALL}
278 )
279 ADD_CUSTOM_TARGET(
280 do_tags ALL
281 DEPENDS tags
282 )
283ENDIF ()
62986ff6
SG
284
285IF (BUILD_EXAMPLES)
60ee53df 286 ADD_SUBDIRECTORY(examples)
583cf169 287ENDIF ()
eae0bfdc
PP
288
289IF(CMAKE_VERSION VERSION_GREATER 3)
290 FEATURE_SUMMARY(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
291 FEATURE_SUMMARY(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")
292ELSE()
293 PRINT_ENABLED_FEATURES()
294 PRINT_DISABLED_FEATURES()
295ENDIF()