]>
Commit | Line | Data |
---|---|---|
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 | |
22a2d3d5 UG |
14 | CMAKE_MINIMUM_REQUIRED(VERSION 3.5.1) |
15 | ||
c25aa7cd | 16 | project(libgit2 VERSION "1.3.0" LANGUAGES C) |
583cf169 | 17 | |
7a6e0281 | 18 | # Add find modules to the path |
22a2d3d5 | 19 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake/") |
7369b3c3 | 20 | |
5bda607c | 21 | INCLUDE(CheckLibraryExists) |
8649dfd8 | 22 | INCLUDE(CheckFunctionExists) |
ab96ca55 | 23 | INCLUDE(CheckSymbolExists) |
e9e6df2c | 24 | INCLUDE(CheckStructHasMember) |
6c7cee42 | 25 | INCLUDE(CheckPrototypeDefinition) # Added in CMake 3.0 |
5c8d5eac | 26 | INCLUDE(AddCFlagIfSupported) |
4b3ec53c | 27 | INCLUDE(FindPkgLibraries) |
eae0bfdc PP |
28 | INCLUDE(FindThreads) |
29 | INCLUDE(FindStatNsec) | |
c25aa7cd | 30 | INCLUDE(Findfutimens) |
22a2d3d5 | 31 | INCLUDE(GNUInstallDirs) |
eae0bfdc PP |
32 | INCLUDE(IdeSplitSources) |
33 | INCLUDE(FeatureSummary) | |
34 | INCLUDE(EnableWarnings) | |
5bda607c | 35 | |
19a766a2 SC |
36 | # Build options |
37 | # | |
ac3d33df JK |
38 | OPTION(SONAME "Set the (SO)VERSION of the target" ON) |
39 | OPTION(BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON) | |
40 | OPTION(THREADSAFE "Build libgit2 as threadsafe" ON) | |
41 | OPTION(BUILD_CLAR "Build Tests using the Clar suite" ON) | |
42 | OPTION(BUILD_EXAMPLES "Build library usage example apps" OFF) | |
43 | OPTION(BUILD_FUZZERS "Build the fuzz targets" OFF) | |
22a2d3d5 | 44 | OPTION(ENABLE_TRACE "Enables tracing support" ON) |
ac3d33df | 45 | OPTION(LIBGIT2_FILENAME "Name of the produced binary" OFF) |
ac3d33df JK |
46 | OPTION(USE_SSH "Link with libssh2 to enable SSH support" ON) |
47 | OPTION(USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON) | |
22a2d3d5 | 48 | OPTION(USE_SHA1 "Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS/Generic" ON) |
ac3d33df JK |
49 | OPTION(USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF) |
50 | OPTION(USE_STANDALONE_FUZZERS "Enable standalone fuzzers (compatible with gcc)" OFF) | |
22a2d3d5 | 51 | OPTION(USE_LEAK_CHECKER "Run tests with leak checker" OFF) |
ac3d33df | 52 | OPTION(DEBUG_POOL "Enable debug pool allocator" OFF) |
c25aa7cd PP |
53 | OPTION(DEBUG_STRICT_ALLOC "Enable strict allocator behavior" OFF) |
54 | OPTION(DEBUG_STRICT_OPEN "Enable path validation in open" OFF) | |
ac3d33df | 55 | OPTION(ENABLE_WERROR "Enable compilation with -Werror" OFF) |
c25aa7cd | 56 | OPTION(USE_BUNDLED_ZLIB "Use the bundled version of zlib. Can be set to one of Bundled(ON)/Chromium. The Chromium option requires a x86_64 processor with SSE4.2 and CLMUL" OFF) |
22a2d3d5 | 57 | SET(USE_HTTP_PARSER "" CACHE STRING "Specifies the HTTP Parser implementation; either system or builtin.") |
ac3d33df | 58 | OPTION(DEPRECATE_HARD "Do not include deprecated functions in the library" OFF) |
22a2d3d5 UG |
59 | SET(REGEX_BACKEND "" CACHE STRING "Regular expression implementation. One of regcomp_l, pcre2, pcre, regcomp, or builtin.") |
60 | ||
61 | IF (UNIX) | |
62 | IF (NOT USE_HTTPS) | |
63 | OPTION(USE_NTLMCLIENT "Enable NTLM support on Unix." OFF ) | |
64 | ELSE() | |
65 | OPTION(USE_NTLMCLIENT "Enable NTLM support on Unix." ON ) | |
66 | ENDIF() | |
67 | ENDIF() | |
ed0571f8 | 68 | |
eae0bfdc | 69 | IF (UNIX AND NOT APPLE) |
ac3d33df | 70 | OPTION(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF) |
ed0571f8 | 71 | ENDIF() |
73291aff | 72 | |
eae0bfdc | 73 | IF (APPLE) |
ac3d33df | 74 | OPTION(USE_ICONV "Link with and use iconv library" ON) |
92dac975 | 75 | ENDIF() |
82b2fc2c | 76 | |
19a766a2 | 77 | IF(MSVC) |
08f32085 Q |
78 | # This option must match the settings used in your program, in particular if you |
79 | # are linking statically | |
ac3d33df | 80 | OPTION(STATIC_CRT "Link the static CRT libraries" ON) |
1bfe7133 | 81 | |
01fe8374 CMN |
82 | # If you want to embed a copy of libssh2 into libgit2, pass a |
83 | # path to libssh2 | |
ac3d33df | 84 | OPTION(EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF) |
19a766a2 SC |
85 | ENDIF() |
86 | ||
6bb54cbf | 87 | |
8f426d7d PK |
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. | |
ac3d33df | 91 | OPTION(WINHTTP "Use Win32 WinHTTP routines" ON) |
8f426d7d PK |
92 | ENDIF() |
93 | ||
d06c589f | 94 | IF(MSVC) |
c25aa7cd PP |
95 | # Enable leak checking using the debugging C runtime. |
96 | OPTION(WIN32_LEAKCHECK "Enable leak reporting via crtdbg" OFF) | |
d06c589f JH |
97 | ENDIF() |
98 | ||
ac3d33df JK |
99 | IF (DEPRECATE_HARD) |
100 | ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD) | |
101 | ENDIF() | |
102 | ||
502dd2da | 103 | # Platform specific compilation flags |
dcd62cb2 | 104 | IF (MSVC) |
ac3d33df JK |
105 | IF (STDCALL) |
106 | 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.") | |
107 | ENDIF() | |
108 | ||
eae0bfdc PP |
109 | ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS) |
110 | ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE) | |
111 | ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE) | |
dcd62cb2 | 112 | |
ccf1a2ba | 113 | STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
94155e2f | 114 | |
19be3f9e PK |
115 | # /GF - String pooling |
116 | # /MP - Parallel build | |
117 | SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}") | |
118 | ||
ac3d33df JK |
119 | # /Gd - explicitly set cdecl calling convention |
120 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd") | |
19be3f9e | 121 | |
22a2d3d5 UG |
122 | IF (NOT (MSVC_VERSION LESS 1900)) |
123 | # /guard:cf - Enable Control Flow Guard | |
124 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf") | |
125 | ENDIF() | |
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 | ||
c25aa7cd PP |
135 | IF (WIN32_LEAKCHECK) |
136 | SET(GIT_WIN32_LEAKCHECK 1) | |
eae0bfdc PP |
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 | 163 | |
ac3d33df JK |
164 | # /IGNORE:4221 - Ignore empty compilation units |
165 | SET(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221") | |
166 | ||
19be3f9e PK |
167 | # /DYNAMICBASE - Address space load randomization (ASLR) |
168 | # /NXCOMPAT - Data execution prevention (DEP) | |
169 | # /LARGEADDRESSAWARE - >2GB user address space on x86 | |
170 | # /VERSION - Embed version information in PE header | |
22a2d3d5 UG |
171 | SET(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}") |
172 | ||
173 | IF (NOT (MSVC_VERSION LESS 1900)) | |
174 | # /GUARD:CF - Enable Control Flow Guard | |
175 | SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF") | |
176 | ENDIF() | |
19be3f9e PK |
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}") | |
e233fa6f | 194 | ELSE () |
eae0bfdc PP |
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 | ||
a02e8a38 PS |
201 | SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}") |
202 | ||
eae0bfdc PP |
203 | ENABLE_WARNINGS(all) |
204 | ENABLE_WARNINGS(extra) | |
19be3f9e | 205 | |
98b8fcff | 206 | IF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
ac3d33df | 207 | SET(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}") |
98b8fcff JG |
208 | ENDIF() |
209 | ||
a64532e1 | 210 | SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0") |
19be3f9e | 211 | |
b6a2fd0e | 212 | IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to |
2eb18449 | 213 | STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}") |
b41e24a6 | 214 | ELSEIF (BUILD_SHARED_LIBS) |
5c8d5eac | 215 | ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden) |
c6cd3f8b JG |
216 | |
217 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") | |
39cdf272 | 218 | ENDIF () |
c6cd3f8b | 219 | |
b6a2fd0e PS |
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 | ||
22a2d3d5 UG |
227 | enable_warnings(documentation) |
228 | disable_warnings(documentation-deprecated-sync) | |
229 | disable_warnings(missing-field-initializers) | |
230 | enable_warnings(strict-aliasing) | |
231 | enable_warnings(strict-prototypes) | |
232 | enable_warnings(declaration-after-statement) | |
233 | enable_warnings(shift-count-overflow) | |
234 | enable_warnings(unused-const-variable) | |
235 | enable_warnings(unused-function) | |
236 | enable_warnings(int-conversion) | |
c25aa7cd PP |
237 | enable_warnings(c11-extensions) |
238 | enable_warnings(c99-c11-compat) | |
22a2d3d5 UG |
239 | |
240 | # MinGW uses gcc, which expects POSIX formatting for printf, but | |
241 | # uses the Windows C library, which uses its own format specifiers. | |
242 | # Disable format specifier warnings. | |
243 | if(MINGW) | |
244 | disable_warnings(format) | |
245 | disable_warnings(format-security) | |
246 | else() | |
247 | enable_warnings(format) | |
248 | enable_warnings(format-security) | |
249 | endif() | |
250 | ENDIF() | |
251 | ||
252 | # Ensure that MinGW provides the correct header files. | |
253 | IF (WIN32 AND NOT CYGWIN) | |
254 | ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0600) | |
90412507 | 255 | ENDIF() |
5b8bb8e7 | 256 | |
88149fae PT |
257 | IF( NOT CMAKE_CONFIGURATION_TYPES ) |
258 | # Build Debug by default | |
259 | IF (NOT CMAKE_BUILD_TYPE) | |
260 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) | |
261 | ENDIF () | |
262 | ELSE() | |
263 | # Using a multi-configuration generator eg MSVC or Xcode | |
264 | # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE | |
265 | ENDIF() | |
583cf169 | 266 | |
ac3d33df JK |
267 | IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS) |
268 | # The actual sanitizer link target will be added when linking the fuzz | |
269 | # targets. | |
270 | SET(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link") | |
271 | ADD_C_FLAG(-fsanitize=fuzzer-no-link) | |
272 | UNSET(CMAKE_REQUIRED_FLAGS) | |
273 | ENDIF () | |
274 | ||
eae0bfdc | 275 | ADD_SUBDIRECTORY(src) |
678e9e04 | 276 | |
eae0bfdc PP |
277 | # Tests |
278 | IF (NOT MSVC) | |
279 | IF (NOT BUILD_SHARED_LIBS) | |
280 | SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") | |
281 | ENDIF() | |
73aaf674 BS |
282 | ENDIF () |
283 | ||
3fd1520c | 284 | IF (BUILD_CLAR) |
f1558d9b | 285 | ENABLE_TESTING() |
eae0bfdc | 286 | ADD_SUBDIRECTORY(tests) |
f1558d9b | 287 | ENDIF () |
e632f687 | 288 | |
62986ff6 | 289 | IF (BUILD_EXAMPLES) |
60ee53df | 290 | ADD_SUBDIRECTORY(examples) |
583cf169 | 291 | ENDIF () |
eae0bfdc | 292 | |
ac3d33df JK |
293 | IF(BUILD_FUZZERS) |
294 | IF(NOT USE_STANDALONE_FUZZERS) | |
295 | IF(BUILD_EXAMPLES) | |
296 | MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the examples together") | |
297 | ENDIF() | |
298 | IF(BUILD_CLAR) | |
299 | MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the tests together") | |
300 | ENDIF() | |
301 | ENDIF() | |
302 | ADD_SUBDIRECTORY(fuzzers) | |
303 | ENDIF() | |
304 | ||
22a2d3d5 UG |
305 | FEATURE_SUMMARY(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:") |
306 | FEATURE_SUMMARY(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:") |