]> git.proxmox.com Git - libgit2.git/blame_incremental - CMakeLists.txt
Add Build-Depends-Package: libgit2-dev to libgit2-1.0.symbols
[libgit2.git] / CMakeLists.txt
... / ...
CommitLineData
1# CMake build script for the libgit2 project
2#
3# Building (out of source build):
4# > mkdir build && cd build
5# > cmake .. [-DSETTINGS=VALUE]
6# > cmake --build .
7#
8# Testing:
9# > ctest -V
10#
11# Install:
12# > cmake --build . --target install
13
14PROJECT(libgit2 C)
15CMAKE_MINIMUM_REQUIRED(VERSION 3.5.1)
16CMAKE_POLICY(SET CMP0015 NEW)
17IF(POLICY CMP0051)
18 CMAKE_POLICY(SET CMP0051 NEW)
19ENDIF()
20IF(POLICY CMP0042)
21 CMAKE_POLICY(SET CMP0042 NEW)
22ENDIF()
23IF(POLICY CMP0054)
24 CMAKE_POLICY(SET CMP0054 NEW)
25ENDIF()
26
27# Add find modules to the path
28SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake/Modules/")
29
30INCLUDE(CheckLibraryExists)
31INCLUDE(CheckFunctionExists)
32INCLUDE(CheckSymbolExists)
33INCLUDE(CheckStructHasMember)
34INCLUDE(CheckPrototypeDefinition) # Added in CMake 3.0
35INCLUDE(AddCFlagIfSupported)
36INCLUDE(FindPkgLibraries)
37INCLUDE(FindThreads)
38INCLUDE(FindStatNsec)
39INCLUDE(GNUInstallDirs)
40INCLUDE(IdeSplitSources)
41INCLUDE(FeatureSummary)
42INCLUDE(EnableWarnings)
43
44# Build options
45#
46OPTION(SONAME "Set the (SO)VERSION of the target" ON)
47OPTION(BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
48OPTION(THREADSAFE "Build libgit2 as threadsafe" ON)
49OPTION(BUILD_CLAR "Build Tests using the Clar suite" ON)
50OPTION(BUILD_EXAMPLES "Build library usage example apps" OFF)
51OPTION(BUILD_FUZZERS "Build the fuzz targets" OFF)
52OPTION(TAGS "Generate tags" OFF)
53OPTION(PROFILE "Generate profiling information" OFF)
54OPTION(ENABLE_TRACE "Enables tracing support" ON)
55OPTION(LIBGIT2_FILENAME "Name of the produced binary" OFF)
56OPTION(USE_SSH "Link with libssh2 to enable SSH support" ON)
57OPTION(USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON)
58OPTION(USE_SHA1 "Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS/Generic" ON)
59OPTION(USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF)
60OPTION(USE_STANDALONE_FUZZERS "Enable standalone fuzzers (compatible with gcc)" OFF)
61OPTION(USE_LEAK_CHECKER "Run tests with leak checker" OFF)
62OPTION(DEBUG_POOL "Enable debug pool allocator" OFF)
63OPTION(ENABLE_WERROR "Enable compilation with -Werror" OFF)
64OPTION(USE_BUNDLED_ZLIB "Use the bundled version of zlib" OFF)
65 SET(USE_HTTP_PARSER "" CACHE STRING "Specifies the HTTP Parser implementation; either system or builtin.")
66OPTION(DEPRECATE_HARD "Do not include deprecated functions in the library" OFF)
67 SET(REGEX_BACKEND "" CACHE STRING "Regular expression implementation. One of regcomp_l, pcre2, pcre, regcomp, or builtin.")
68
69IF (UNIX)
70 IF (NOT USE_HTTPS)
71 OPTION(USE_NTLMCLIENT "Enable NTLM support on Unix." OFF )
72 ELSE()
73 OPTION(USE_NTLMCLIENT "Enable NTLM support on Unix." ON )
74 ENDIF()
75ENDIF()
76
77IF (UNIX AND NOT APPLE)
78 OPTION(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF)
79ENDIF()
80
81IF (APPLE)
82 OPTION(USE_ICONV "Link with and use iconv library" ON)
83ENDIF()
84
85IF(MSVC)
86 # This option must match the settings used in your program, in particular if you
87 # are linking statically
88 OPTION(STATIC_CRT "Link the static CRT libraries" ON)
89
90 # If you want to embed a copy of libssh2 into libgit2, pass a
91 # path to libssh2
92 OPTION(EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF)
93ENDIF()
94
95
96IF(WIN32)
97 # By default, libgit2 is built with WinHTTP. To use the built-in
98 # HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
99 OPTION(WINHTTP "Use Win32 WinHTTP routines" ON)
100ENDIF()
101
102IF(MSVC)
103 # Enable MSVC CRTDBG memory leak reporting when in debug mode.
104 OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
105ENDIF()
106
107FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
108
109STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
110STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
111STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
112SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
113
114FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER_SOVERSION REGEX "^#define LIBGIT2_SOVERSION \"([0-9.]+)\"$")
115STRING(REGEX REPLACE "^.*LIBGIT2_SOVERSION \"([0-9.]+)\"$" "\\1" LIBGIT2_SOVERSION "${GIT2_HEADER_SOVERSION}")
116
117IF (DEPRECATE_HARD)
118 ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
119ENDIF()
120
121# Platform specific compilation flags
122IF (MSVC)
123 IF (STDCALL)
124 MESSAGE(FATAL_ERROR "The STDCALL option is no longer supported; libgit2 is now always built as a cdecl library. If you're using PInvoke, please add the CallingConventions.Cdecl attribute for support.")
125 ENDIF()
126
127 ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
128 ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
129 ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
130
131 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
132
133 # /GF - String pooling
134 # /MP - Parallel build
135 SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
136
137 # /Gd - explicitly set cdecl calling convention
138 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
139
140 IF (STATIC_CRT)
141 SET(CRT_FLAG_DEBUG "/MTd")
142 SET(CRT_FLAG_RELEASE "/MT")
143 ELSE()
144 SET(CRT_FLAG_DEBUG "/MDd")
145 SET(CRT_FLAG_RELEASE "/MD")
146 ENDIF()
147
148 IF (MSVC_CRTDBG)
149 SET(GIT_MSVC_CRTDBG 1)
150 SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
151 SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
152 ENDIF()
153
154 # /Zi - Create debugging information
155 # /Od - Disable optimization
156 # /D_DEBUG - #define _DEBUG
157 # /MTd - Statically link the multithreaded debug version of the CRT
158 # /MDd - Dynamically link the multithreaded debug version of the CRT
159 # /RTC1 - Run time checks
160 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
161
162 # /DNDEBUG - Disables asserts
163 # /MT - Statically link the multithreaded release version of the CRT
164 # /MD - Dynamically link the multithreaded release version of the CRT
165 # /O2 - Optimize for speed
166 # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
167 # /GL - Link time code generation (whole program optimization)
168 # /Gy - Function-level linking
169 SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
170
171 # /Oy- - Disable frame pointer omission (FPO)
172 SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
173
174 # /O1 - Optimize for size
175 SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
176
177 # /IGNORE:4221 - Ignore empty compilation units
178 SET(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
179
180 # /DYNAMICBASE - Address space load randomization (ASLR)
181 # /NXCOMPAT - Data execution prevention (DEP)
182 # /LARGEADDRESSAWARE - >2GB user address space on x86
183 # /VERSION - Embed version information in PE header
184 SET(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}")
185
186 # /DEBUG - Create a PDB
187 # /LTCG - Link time code generation (whole program optimization)
188 # /OPT:REF /OPT:ICF - Fold out duplicate code at link step
189 # /INCREMENTAL:NO - Required to use /LTCG
190 # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
191 SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
192 SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
193 SET(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
194 SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
195
196 # Same linker settings for DLL as EXE
197 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
198 SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
199 SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
200 SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
201 SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
202ELSE ()
203 IF (ENABLE_REPRODUCIBLE_BUILDS)
204 SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
205 SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
206 SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
207 ENDIF()
208
209 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
210
211 ENABLE_WARNINGS(all)
212 ENABLE_WARNINGS(extra)
213
214 IF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
215 SET(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
216 ENDIF()
217
218 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
219
220 IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
221 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
222 ELSEIF (BUILD_SHARED_LIBS)
223 ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden)
224
225 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
226 ENDIF ()
227
228 IF (MINGW)
229 # MinGW >= 3.14 uses the C99-style stdio functions
230 # automatically, but forks like mingw-w64 still want
231 # us to define this in order to use them
232 ADD_DEFINITIONS(-D__USE_MINGW_ANSI_STDIO=1)
233 ENDIF ()
234
235 ENABLE_WARNINGS(documentation)
236 DISABLE_WARNINGS(missing-field-initializers)
237 ENABLE_WARNINGS(strict-aliasing)
238 ENABLE_WARNINGS(strict-prototypes)
239 ENABLE_WARNINGS(declaration-after-statement)
240 ENABLE_WARNINGS(shift-count-overflow)
241 ENABLE_WARNINGS(unused-const-variable)
242 ENABLE_WARNINGS(unused-function)
243 ENABLE_WARNINGS(int-conversion)
244
245 # MinGW uses gcc, which expects POSIX formatting for printf, but
246 # uses the Windows C library, which uses its own format specifiers.
247 # Disable format specifier warnings.
248 IF(MINGW)
249 DISABLE_WARNINGS(format)
250 DISABLE_WARNINGS(format-security)
251 ELSE()
252 ENABLE_WARNINGS(format)
253 ENABLE_WARNINGS(format-security)
254 ENDIF()
255
256 IF("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
257 DISABLE_WARNINGS(documentation-deprecated-sync)
258 ENDIF()
259
260 IF (PROFILE)
261 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
262 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
263 ENDIF ()
264ENDIF()
265
266# Ensure that MinGW provides the correct header files.
267IF (WIN32 AND NOT CYGWIN)
268 ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0600)
269ENDIF()
270
271IF( NOT CMAKE_CONFIGURATION_TYPES )
272 # Build Debug by default
273 IF (NOT CMAKE_BUILD_TYPE)
274 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
275 ENDIF ()
276ELSE()
277 # Using a multi-configuration generator eg MSVC or Xcode
278 # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
279ENDIF()
280
281IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
282 # The actual sanitizer link target will be added when linking the fuzz
283 # targets.
284 SET(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
285 ADD_C_FLAG(-fsanitize=fuzzer-no-link)
286 UNSET(CMAKE_REQUIRED_FLAGS)
287ENDIF ()
288
289ADD_SUBDIRECTORY(src)
290
291# Tests
292IF (NOT MSVC)
293 IF (NOT BUILD_SHARED_LIBS)
294 SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
295 ENDIF()
296ENDIF ()
297
298IF (BUILD_CLAR)
299 ENABLE_TESTING()
300 ADD_SUBDIRECTORY(tests)
301ENDIF ()
302
303IF (TAGS)
304 FIND_PROGRAM(CTAGS ctags)
305 IF (NOT CTAGS)
306 MESSAGE(FATAL_ERROR "Could not find ctags command")
307 ENDIF ()
308
309 FILE(GLOB_RECURSE SRC_ALL *.[ch])
310
311 ADD_CUSTOM_COMMAND(
312 OUTPUT tags
313 COMMAND ${CTAGS} -a ${SRC_ALL}
314 DEPENDS ${SRC_ALL}
315 )
316 ADD_CUSTOM_TARGET(
317 do_tags ALL
318 DEPENDS tags
319 )
320ENDIF ()
321
322IF (BUILD_EXAMPLES)
323 ADD_SUBDIRECTORY(examples)
324ENDIF ()
325
326IF(BUILD_FUZZERS)
327 IF(NOT USE_STANDALONE_FUZZERS)
328 IF(BUILD_EXAMPLES)
329 MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the examples together")
330 ENDIF()
331 IF(BUILD_CLAR)
332 MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the tests together")
333 ENDIF()
334 ENDIF()
335 ADD_SUBDIRECTORY(fuzzers)
336ENDIF()
337
338FEATURE_SUMMARY(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
339FEATURE_SUMMARY(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")