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