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