CMakeLists.txt 10.4 KB
Newer Older
1
# CMake build script for the libgit2 project
2
#
3
# Building (out of source build):
4
# > mkdir build && cd build
5 6
# > cmake .. [-DSETTINGS=VALUE]
# > cmake --build .
7
#
8 9 10 11
# Testing:
# > ctest -V
#
# Install:
12
# > cmake --build . --target install
13

14
CMAKE_MINIMUM_REQUIRED(VERSION 3.5.1)
15

Edward Thomson committed
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/")
Brad Morgan committed
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 36
# Build options
#
37 38 39 40 41 42
OPTION(SONAME				"Set the (SO)VERSION of the target"			 ON)
OPTION(BUILD_SHARED_LIBS		"Build Shared Library (OFF for Static)"			 ON)
OPTION(THREADSAFE			"Build libgit2 as threadsafe"				 ON)
OPTION(BUILD_CLAR			"Build Tests using the Clar suite"			 ON)
OPTION(BUILD_EXAMPLES			"Build library usage example apps"			OFF)
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 49
OPTION(USE_GSSAPI			"Link with libgssapi for SPNEGO auth"			OFF)
OPTION(USE_STANDALONE_FUZZERS		"Enable standalone fuzzers (compatible with gcc)"	OFF)
50
OPTION(USE_LEAK_CHECKER			"Run tests with leak checker"				OFF)
51 52 53
OPTION(DEBUG_POOL			"Enable debug pool allocator"				OFF)
OPTION(ENABLE_WERROR			"Enable compilation with -Werror"			OFF)
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 60 61 62 63
	IF (NOT USE_HTTPS)
	    OPTION(USE_NTLMCLIENT		"Enable NTLM support on Unix."				OFF )
	ELSE()
	    OPTION(USE_NTLMCLIENT		"Enable NTLM support on Unix."				ON )
	ENDIF()
64 65
ENDIF()

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 76
	# This option must match the settings used in your program, in particular if you
	# are linking statically
77
	OPTION(STATIC_CRT		"Link the static CRT libraries"		 		 ON)
78

79 80
	# If you want to embed a copy of libssh2 into libgit2, pass a
	# path to libssh2
81
	OPTION(EMBED_SSH_PATH		"Path to libssh2 to embed (Windows)"			OFF)
82 83
ENDIF()

84

85 86 87
IF(WIN32)
	# By default, libgit2 is built with WinHTTP.  To use the built-in
	# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
88
	OPTION(WINHTTP			"Use Win32 WinHTTP routines"				 ON)
89 90
ENDIF()

91 92
IF(MSVC)
	# Enable MSVC CRTDBG memory leak reporting when in debug mode.
93
	OPTION(MSVC_CRTDBG 		"Enable CRTDBG memory leak reporting"			OFF)
94 95
ENDIF()

96 97 98 99
IF (DEPRECATE_HARD)
	ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
ENDIF()

100 101
# Platform specific compilation flags
IF (MSVC)
102 103 104 105
	IF (STDCALL)
		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.")
	ENDIF()

106 107 108 109 110 111 112 113 114 115
	ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
	ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
	ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)

	STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

	# /GF - String pooling
	# /MP - Parallel build
	SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")

116 117
	# /Gd - explicitly set cdecl calling convention
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
118

119 120 121 122 123
	IF (NOT (MSVC_VERSION LESS 1900))
		# /guard:cf - Enable Control Flow Guard
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
	ENDIF()

124 125 126 127 128 129 130 131 132 133 134
	IF (STATIC_CRT)
		SET(CRT_FLAG_DEBUG "/MTd")
		SET(CRT_FLAG_RELEASE "/MT")
	ELSE()
		SET(CRT_FLAG_DEBUG "/MDd")
		SET(CRT_FLAG_RELEASE "/MD")
	ENDIF()

	IF (MSVC_CRTDBG)
		SET(GIT_MSVC_CRTDBG 1)
		SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
135
		SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	ENDIF()

	# /Zi - Create debugging information
	# /Od - Disable optimization
	# /D_DEBUG - #define _DEBUG
	# /MTd - Statically link the multithreaded debug version of the CRT
	# /MDd - Dynamically link the multithreaded debug version of the CRT
	# /RTC1 - Run time checks
	SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")

	# /DNDEBUG - Disables asserts
	# /MT - Statically link the multithreaded release version of the CRT
	# /MD - Dynamically link the multithreaded release version of the CRT
	# /O2 - Optimize for speed
	# /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
	# /GL - Link time code generation (whole program optimization)
	# /Gy - Function-level linking
	SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")

	# /Oy- - Disable frame pointer omission (FPO)
	SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")

	# /O1 - Optimize for size
	SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")

161 162 163
	# /IGNORE:4221 - Ignore empty compilation units
	SET(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")

164 165 166 167
	# /DYNAMICBASE - Address space load randomization (ASLR)
	# /NXCOMPAT - Data execution prevention (DEP)
	# /LARGEADDRESSAWARE - >2GB user address space on x86
	# /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 171 172 173 174
	IF (NOT (MSVC_VERSION LESS 1900))
		# /GUARD:CF - Enable Control Flow Guard
		SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
	ENDIF()

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	# /DEBUG - Create a PDB
	# /LTCG - Link time code generation (whole program optimization)
	# /OPT:REF /OPT:ICF - Fold out duplicate code at link step
	# /INCREMENTAL:NO - Required to use /LTCG
	# /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
	SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
	SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
	SET(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
	SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")

	# Same linker settings for DLL as EXE
	SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
	SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
	SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
	SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
	SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
ELSE ()
192 193 194 195 196 197
	IF (ENABLE_REPRODUCIBLE_BUILDS)
		SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
		SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq  <TARGET> <LINK_FLAGS> <OBJECTS>")
		SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
	ENDIF()

198 199
	SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")

200 201
	ENABLE_WARNINGS(all)
	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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	ENDIF()

	SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")

	IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
		STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
	ELSEIF (BUILD_SHARED_LIBS)
		ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden)

		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
	ENDIF ()

	IF (MINGW)
		# MinGW >= 3.14 uses the C99-style stdio functions
		# automatically, but forks like mingw-w64 still want
		# us to define this in order to use them
		ADD_DEFINITIONS(-D__USE_MINGW_ANSI_STDIO=1)
	ENDIF ()

224 225 226 227 228 229 230 231 232 233
	enable_warnings(documentation)
	disable_warnings(documentation-deprecated-sync)
	disable_warnings(missing-field-initializers)
	enable_warnings(strict-aliasing)
	enable_warnings(strict-prototypes)
	enable_warnings(declaration-after-statement)
	enable_warnings(shift-count-overflow)
	enable_warnings(unused-const-variable)
	enable_warnings(unused-function)
	enable_warnings(int-conversion)
234 235 236 237

	# MinGW uses gcc, which expects POSIX formatting for printf, but
	# uses the Windows C library, which uses its own format specifiers.
	# Disable format specifier warnings.
238 239 240 241 242 243 244
	if(MINGW)
		disable_warnings(format)
		disable_warnings(format-security)
	else()
		enable_warnings(format)
		enable_warnings(format-security)
	endif()
245 246
ENDIF()

247 248 249 250 251
# Ensure that MinGW provides the correct header files.
IF (WIN32 AND NOT CYGWIN)
	ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0600)
ENDIF()

252 253 254 255 256 257 258 259 260 261
IF( NOT CMAKE_CONFIGURATION_TYPES )
	# Build Debug by default
	IF (NOT CMAKE_BUILD_TYPE)
		SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
	ENDIF ()
ELSE()
	# Using a multi-configuration generator eg MSVC or Xcode
	# that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
ENDIF()

262 263 264
IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
	# The actual sanitizer link target will be added when linking the fuzz
	# targets.
265 266 267
	SET(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
	ADD_C_FLAG(-fsanitize=fuzzer-no-link)
	UNSET(CMAKE_REQUIRED_FLAGS)
268 269
ENDIF ()

270
ADD_SUBDIRECTORY(src)
271 272

# Tests
273 274 275 276 277 278
IF (NOT MSVC)
   IF (NOT BUILD_SHARED_LIBS)
       SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
   ENDIF()
ENDIF ()

279
IF (BUILD_CLAR)
Vicent Marti committed
280
	ENABLE_TESTING()
281
	ADD_SUBDIRECTORY(tests)
Vicent Marti committed
282
ENDIF ()
Clemens Buchacher committed
283

284
IF (BUILD_EXAMPLES)
285
	ADD_SUBDIRECTORY(examples)
286
ENDIF ()
287

288 289 290 291 292 293 294 295 296
IF(BUILD_FUZZERS)
	IF(NOT USE_STANDALONE_FUZZERS)
		IF(BUILD_EXAMPLES)
			MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the examples together")
		ENDIF()
		IF(BUILD_CLAR)
			MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the tests together")
		ENDIF()
	ENDIF()
297
	ADD_SUBDIRECTORY(fuzzers)
298 299
ENDIF()

300 301
FEATURE_SUMMARY(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
FEATURE_SUMMARY(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")