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