1 # CMake build script for the libgit2 project
3 # Building (out of source build):
4 # > mkdir build && cd build
5 # > cmake .. [-DSETTINGS=VALUE]
12 # > cmake --build . --target install
15 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
16 CMAKE_POLICY(SET CMP0015 NEW)
18 CMAKE_POLICY(SET CMP0051 NEW)
21 CMAKE_POLICY(SET CMP0042 NEW)
24 CMAKE_POLICY(SET CMP0054 NEW)
27 # Add find modules to the path
28 SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake/Modules/")
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)
39 INCLUDE(IdeSplitSources)
40 INCLUDE(FeatureSummary)
41 INCLUDE(EnableWarnings)
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)
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)
69 IF (UNIX AND NOT APPLE)
70 OPTION(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF)
74 OPTION(USE_ICONV "Link with and use iconv library" ON)
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)
82 # If you want to embed a copy of libssh2 into libgit2, pass a
84 OPTION(EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF)
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)
95 # Enable MSVC CRTDBG memory leak reporting when in debug mode.
96 OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
99 FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
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}")
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}")
110 ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
113 # Platform specific compilation flags
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.")
119 ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
120 ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
121 ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
123 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
125 # /GF - String pooling
126 # /MP - Parallel build
127 SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
129 # /Gd - explicitly set cdecl calling convention
130 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
133 SET(CRT_FLAG_DEBUG "/MTd")
134 SET(CRT_FLAG_RELEASE "/MT")
136 SET(CRT_FLAG_DEBUG "/MDd")
137 SET(CRT_FLAG_RELEASE "/MD")
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")
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}")
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}")
163 # /Oy- - Disable frame pointer omission (FPO)
164 SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
166 # /O1 - Optimize for size
167 SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
169 # /IGNORE:4221 - Ignore empty compilation units
170 SET(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
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}")
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")
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}")
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>")
201 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
204 ENABLE_WARNINGS(extra)
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}")
210 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
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)
217 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
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)
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)
241 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
242 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
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)
252 # Using a multi-configuration generator eg MSVC or Xcode
253 # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
256 IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
257 # The actual sanitizer link target will be added when linking the fuzz
259 SET(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
260 ADD_C_FLAG(-fsanitize=fuzzer-no-link)
261 UNSET(CMAKE_REQUIRED_FLAGS)
264 ADD_SUBDIRECTORY(src)
268 IF (NOT BUILD_SHARED_LIBS)
269 SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
275 ADD_SUBDIRECTORY(tests)
279 FIND_PROGRAM(CTAGS ctags)
281 MESSAGE(FATAL_ERROR "Could not find ctags command")
284 FILE(GLOB_RECURSE SRC_ALL *.[ch])
288 COMMAND ${CTAGS} -a ${SRC_ALL}
298 ADD_SUBDIRECTORY(examples)
302 IF(NOT USE_STANDALONE_FUZZERS)
304 MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the examples together")
307 MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the tests together")
310 ADD_SUBDIRECTORY(fuzzers)
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:")
317 PRINT_ENABLED_FEATURES()
318 PRINT_DISABLED_FEATURES()