CMakeLists.txt 24.9 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
PROJECT(libgit2 C)
15
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
16
CMAKE_POLICY(SET CMP0015 NEW)
17

18
# Add find modules to the path
19
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
Brad Morgan committed
20

21
INCLUDE(CheckLibraryExists)
22
INCLUDE(CheckFunctionExists)
23
INCLUDE(CheckSymbolExists)
24
INCLUDE(CheckStructHasMember)
25
INCLUDE(AddCFlagIfSupported)
26
INCLUDE(FindPkgConfig)
27

28 29
# Build options
#
Miha committed
30
OPTION( SONAME				"Set the (SO)VERSION of the target"		ON  )
31
OPTION( BUILD_SHARED_LIBS	"Build Shared Library (OFF for Static)"	ON  )
32
OPTION( THREADSAFE			"Build libgit2 as threadsafe"			ON )
33 34 35 36
OPTION( BUILD_CLAR			"Build Tests using the Clar suite"		ON  )
OPTION( BUILD_EXAMPLES		"Build library usage example apps"		OFF )
OPTION( TAGS				"Generate tags"							OFF )
OPTION( PROFILE				"Generate profiling information"		OFF )
Edward Thomson committed
37
OPTION( ENABLE_TRACE		"Enables tracing support"				OFF )
38
OPTION( LIBGIT2_FILENAME	"Name of the produced binary"			OFF )
39

40
OPTION( USE_ICONV			"Link with and use iconv library" 		OFF )
41
OPTION( USE_SSH				"Link with libssh to enable SSH support" ON )
42
OPTION( USE_GSSAPI			"Link with libgssapi for SPNEGO auth"   OFF )
43
OPTION( VALGRIND			"Configure build for valgrind"			OFF )
Elan Ruusamäe committed
44
OPTION( CURL			"Use curl for HTTP if available" ON)
45 46 47 48 49
OPTION( DEBUG_POOL			"Enable debug pool allocator"			OFF )

IF(DEBUG_POOL)
	ADD_DEFINITIONS(-DGIT_DEBUG_POOL)
ENDIF()
50

51
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
52
	SET( USE_ICONV ON )
53 54
	FIND_PACKAGE(Security)
	FIND_PACKAGE(CoreFoundation REQUIRED)
55
ENDIF()
56

57
IF(MSVC)
Ben Straub committed
58
	# This option is only available when building with MSVC. By default, libgit2
59 60
	# is build using the cdecl calling convention, which is useful if you're
	# writing C. However, the CLR and Win32 API both expect stdcall.
61
	#
62 63 64
	# If you are writing a CLR program and want to link to libgit2, you'll want
	# to turn this on by invoking CMake with the "-DSTDCALL=ON" argument.
	OPTION( STDCALL			"Build libgit2 with the __stdcall convention"	OFF  )
65 66 67 68

	# This option must match the settings used in your program, in particular if you
	# are linking statically
	OPTION( STATIC_CRT		"Link the static CRT libraries"	ON  )
69

70 71 72 73
	# If you want to embed a copy of libssh2 into libgit2, pass a
	# path to libssh2
	OPTION( EMBED_SSH_PATH		"Path to libssh2 to embed (Windows)" OFF )

74
	ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
75 76
	ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
	ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
77 78
ENDIF()

79

80 81 82 83 84 85
IF(WIN32)
	# By default, libgit2 is built with WinHTTP.  To use the built-in
	# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
	OPTION(	WINHTTP			"Use Win32 WinHTTP routines"	ON	)
ENDIF()

86 87 88 89 90
IF(MSVC)
	# Enable MSVC CRTDBG memory leak reporting when in debug mode.
	OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
ENDIF()

91 92 93 94
IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
	OPTION( USE_OPENSSL                     "Link with and use openssl library"             ON )
ENDIF()

95 96 97 98 99 100 101 102
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtim "sys/types.h;sys/stat.h"
	HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtimespec "sys/types.h;sys/stat.h"
	HAVE_STRUCT_STAT_ST_MTIMESPEC LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtime_nsec sys/stat.h
	HAVE_STRUCT_STAT_MTIME_NSEC LANGUAGE C)

IF (HAVE_STRUCT_STAT_ST_MTIM)
103 104
	CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec sys/stat.h
		HAVE_STRUCT_STAT_NSEC LANGUAGE C)
105
ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
106 107
	CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec sys/stat.h
		HAVE_STRUCT_STAT_NSEC LANGUAGE C)
108 109
ELSE ()
	SET( HAVE_STRUCT_STAT_NSEC ON )
110 111 112
ENDIF()

IF (HAVE_STRUCT_STAT_NSEC OR WIN32)
113
	OPTION( USE_NSEC		"Care about sub-second file mtimes and ctimes"	ON  )
114
ENDIF()
115

116 117 118 119 120 121 122 123 124
# This variable will contain the libraries we need to put into
# libgit2.pc's Requires.private. That is, what we're linking to or
# what someone who's statically linking us needs to link to.
SET(LIBGIT2_PC_REQUIRES "")
# This will be set later if we use the system's http-parser library or
# use iconv (OSX) and will be written to the Libs.private field in the
# pc file.
SET(LIBGIT2_PC_LIBS "")

125 126 127 128 129 130
# Installation paths
#
SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.")
SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.")

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
# Set a couple variables to be substituted inside the .pc file.
# We can't just use LIB_INSTALL_DIR in the .pc file, as passing them as absolue
# or relative paths is both valid and supported by cmake.
SET (PKGCONFIG_PREFIX ${CMAKE_INSTALL_PREFIX})

IF(IS_ABSOLUTE ${LIB_INSTALL_DIR})
  SET (PKGCONFIG_LIBDIR ${LIB_INSTALL_DIR})
ELSE(IS_ABSOLUTE ${LIB_INSTALL_DIR})
  SET (PKGCONFIG_LIBDIR "\${prefix}/${LIB_INSTALL_DIR}")
ENDIF (IS_ABSOLUTE ${LIB_INSTALL_DIR})

IF(IS_ABSOLUTE ${INCLUDE_INSTALL_DIR})
  SET (PKGCONFIG_INCLUDEDIR ${INCLUDE_INSTALL_DIR})
ELSE(IS_ABSOLUTE ${INCLUDE_INSTALL_DIR})
  SET (PKGCONFIG_INCLUDEDIR "\${prefix}/${INCLUDE_INSTALL_DIR}")
ENDIF(IS_ABSOLUTE ${INCLUDE_INSTALL_DIR})

148 149 150 151 152
FUNCTION(TARGET_OS_LIBRARIES target)
	IF(WIN32)
		TARGET_LINK_LIBRARIES(${target} ws2_32)
	ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
		TARGET_LINK_LIBRARIES(${target} socket nsl)
153 154
		LIST(APPEND LIBGIT2_PC_LIBS "-lsocket" "-lnsl")
		SET(LIBGIT2_PC_LIBS ${LIBGIT2_PC_LIBS} PARENT_SCOPE)
155 156 157 158
	ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Haiku")
		TARGET_LINK_LIBRARIES(${target} network)
		LIST(APPEND LIBGIT2_PC_LIBS "-lnetwork")
		SET(LIBGIT2_PC_LIBS ${LIBGIT2_PC_LIBS} PARENT_SCOPE)
159 160 161
	ENDIF()
	CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT)
	IF(NEED_LIBRT)
162
		TARGET_LINK_LIBRARIES(${target} rt)
163 164
		LIST(APPEND LIBGIT2_PC_LIBS "-lrt")
		SET(LIBGIT2_PC_LIBS ${LIBGIT2_PC_LIBS} PARENT_SCOPE)
165 166
	ENDIF()

167 168
	IF(THREADSAFE)
		TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
169 170
		LIST(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT})
		SET(LIBGIT2_PC_LIBS ${LIBGIT2_PC_LIBS} PARENT_SCOPE)
171 172 173
	ENDIF()
ENDFUNCTION()

174 175 176 177 178 179 180
# This function splits the sources files up into their appropriate
# subdirectories.  This is especially useful for IDEs like Xcode and
# Visual Studio, so that you can navigate into the libgit2_clar project,
# and see the folders within the tests folder (instead of just seeing all
# source and tests in a single folder.)
FUNCTION(IDE_SPLIT_SOURCES target)
	IF(MSVC_IDE OR CMAKE_GENERATOR STREQUAL Xcode)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		GET_TARGET_PROPERTY(sources ${target} SOURCES)
		FOREACH(source ${sources})
			IF(source MATCHES ".*/")
				STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" rel ${source})
				IF(rel)
					STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel})
					IF(rel)
						STRING(REPLACE "/" "\\\\" rel ${rel})
						SOURCE_GROUP(${rel} FILES ${source})
					ENDIF()
				ENDIF()
			ENDIF()
		ENDFOREACH()
	ENDIF()
ENDFUNCTION()

197
FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
198 199 200 201 202 203

STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR  "${GIT2_HEADER}")
STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")

204 205 206
FILE(STRINGS "include/git2/version.h" GIT2_HEADER_SOVERSION REGEX "^#define LIBGIT2_SOVERSION [0-9]+$")
STRING(REGEX REPLACE "^.*LIBGIT2_SOVERSION ([0-9]+)$" "\\1" LIBGIT2_SOVERSION "${GIT2_HEADER_SOVERSION}")

207
# Find required dependencies
208
INCLUDE_DIRECTORIES(src include)
209

210
IF (SECURITY_FOUND)
211 212 213 214 215 216 217 218 219 220 221 222
  # OS X 10.7 and older do not have some functions we use, fall back to OpenSSL there
  CHECK_LIBRARY_EXISTS("${SECURITY_DIRS}" SSLCreateContext "Security/SecureTransport.h" HAVE_NEWER_SECURITY)
  IF (HAVE_NEWER_SECURITY)
    MESSAGE("-- Found Security ${SECURITY_DIRS}")
    LIST(APPEND LIBGIT2_PC_LIBS "-framework Security")
  ELSE()
    MESSAGE("-- Security framework is too old, falling back to OpenSSL")
    SET(SECURITY_FOUND "NO")
    SET(SECURITY_DIRS "")
    SET(SECURITY_DIR "")
    SET(USE_OPENSSL "ON")
  ENDIF()
223 224 225 226
ENDIF()

IF (COREFOUNDATION_FOUND)
  MESSAGE("-- Found CoreFoundation ${COREFOUNDATION_DIRS}")
227
  LIST(APPEND LIBGIT2_PC_LIBS "-framework CoreFoundation")
228 229 230
ENDIF()


231 232 233 234 235 236 237
IF (WIN32 AND EMBED_SSH_PATH)
	FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
	INCLUDE_DIRECTORIES("${EMBED_SSH_PATH}/include")
	FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
	ADD_DEFINITIONS(-DGIT_SSH)
ENDIF()

238
IF (WIN32 AND WINHTTP)
239
	ADD_DEFINITIONS(-DGIT_WINHTTP)
240 241
	INCLUDE_DIRECTORIES(deps/http-parser)
	FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h)
242 243 244 245 246 247 248 249 250

	# Since MinGW does not come with headers or an import library for winhttp,
	# we have to include a private header and generate our own import library
	IF (MINGW)
		FIND_PROGRAM(DLLTOOL dlltool CMAKE_FIND_ROOT_PATH_BOTH)
		IF (NOT DLLTOOL)
			MESSAGE(FATAL_ERROR "Could not find dlltool command")
		ENDIF ()

Tony Kelman committed
251 252
		SET(LIBWINHTTP_PATH "${CMAKE_CURRENT_BINARY_DIR}/deps/winhttp")
		FILE(MAKE_DIRECTORY ${LIBWINHTTP_PATH})
253

254
		IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
255 256 257 258 259
			set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp64.def")
		ELSE()
			set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp.def")
		ENDIF()

260 261
		ADD_CUSTOM_COMMAND(
			OUTPUT ${LIBWINHTTP_PATH}/libwinhttp.a
262 263
			COMMAND ${DLLTOOL} -d ${WINHTTP_DEF} -k -D winhttp.dll -l libwinhttp.a
			DEPENDS ${WINHTTP_DEF}
264 265 266 267 268 269 270 271 272
			WORKING_DIRECTORY ${LIBWINHTTP_PATH}
		)

		SET_SOURCE_FILES_PROPERTIES(
			${CMAKE_CURRENT_SOURCE_DIR}/src/transports/winhttp.c
			PROPERTIES OBJECT_DEPENDS ${LIBWINHTTP_PATH}/libwinhttp.a
		)

		INCLUDE_DIRECTORIES(deps/winhttp)
Tony Kelman committed
273
		LINK_DIRECTORIES(${LIBWINHTTP_PATH})
274 275
	ENDIF ()

276 277
	LINK_LIBRARIES(winhttp rpcrt4 crypt32 ole32)
	LIST(APPEND LIBGIT2_PC_LIBS "-lwinhttp" "-lrpcrt4" "-lcrypt32" "-lole32")
278
ELSE ()
279
	IF (CURL)
280
		PKG_CHECK_MODULES(CURL libcurl)
281 282
	ENDIF ()

283
	IF (NOT AMIGA AND USE_OPENSSL)
284 285
		FIND_PACKAGE(OpenSSL)
	ENDIF ()
286

287 288 289
	IF (CURL_FOUND)
		ADD_DEFINITIONS(-DGIT_CURL)
		INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
290
		LINK_DIRECTORIES(${CURL_LIBRARY_DIRS})
291
		LINK_LIBRARIES(${CURL_LIBRARIES})
292
		LIST(APPEND LIBGIT2_PC_LIBS ${CURL_LDFLAGS})
293 294
	ENDIF()

295
	FIND_PACKAGE(HTTP_Parser)
296 297 298
	IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
		INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS})
		LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES})
299
		LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
300
	ELSE()
301
		MESSAGE(STATUS "http-parser was not found or is too old; using bundled 3rd-party sources.")
302
		INCLUDE_DIRECTORIES(deps/http-parser)
303
		FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h)
304
	ENDIF()
305
ENDIF()
306

307
# Specify sha1 implementation
Edward Thomson committed
308
IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
nulltoken committed
309 310
	ADD_DEFINITIONS(-DWIN32_SHA1)
	FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
311 312
ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
	ADD_DEFINITIONS(-DGIT_COMMON_CRYPTO)
313 314
ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
	ADD_DEFINITIONS(-DOPENSSL_SHA1)
315
	IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
316
		LIST(APPEND LIBGIT2_PC_LIBS "-lssl")
317 318 319
	ELSE()
		SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} openssl")
	ENDIF ()
320 321
ELSE()
	FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
322 323
ENDIF()

Edward Thomson committed
324 325 326 327 328
# Enable tracing
IF (ENABLE_TRACE STREQUAL "ON")
	ADD_DEFINITIONS(-DGIT_TRACE)
ENDIF()

329
# Include POSIX regex when it is required
330
IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
331 332
	INCLUDE_DIRECTORIES(deps/regex)
	SET(SRC_REGEX deps/regex/regex.c)
333
ENDIF()
334

335
# Optional external dependency: zlib
336
FIND_PACKAGE(ZLIB)
337 338 339
IF (ZLIB_FOUND)
	INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
	LINK_LIBRARIES(${ZLIB_LIBRARIES})
340
	IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
341
		LIST(APPEND LIBGIT2_PC_LIBS "-lz")
342 343 344
	ELSE()
		SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} zlib")
	ENDIF()
345
ELSE()
346
	MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." )
347 348
	INCLUDE_DIRECTORIES(deps/zlib)
	ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
349
	FILE(GLOB SRC_ZLIB deps/zlib/*.c deps/zlib/*.h)
Chris Young committed
350
ENDIF()
351

352
# Optional external dependency: libssh2
353
IF (USE_SSH)
354
	PKG_CHECK_MODULES(LIBSSH2 libssh2)
355
ENDIF()
Brad Morgan committed
356 357
IF (LIBSSH2_FOUND)
	ADD_DEFINITIONS(-DGIT_SSH)
358 359
	INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIRS})
	LINK_DIRECTORIES(${LIBSSH2_LIBRARY_DIRS})
360 361
	LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
	#SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} ${LIBSSH2_LDFLAGS}")
Brad Morgan committed
362
	SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
363

364
	CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
365 366 367
	IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)
		ADD_DEFINITIONS(-DGIT_SSH_MEMORY_CREDENTIALS)
	ENDIF()
368 369
ELSE()
	MESSAGE(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
Brad Morgan committed
370 371
ENDIF()

372 373 374 375 376 377 378 379
# Optional external dependency: libgssapi
IF (USE_GSSAPI)
	FIND_PACKAGE(GSSAPI)
ENDIF()
IF (GSSAPI_FOUND)
	ADD_DEFINITIONS(-DGIT_GSSAPI)
ENDIF()

380 381
# Optional external dependency: iconv
IF (USE_ICONV)
382
	FIND_PACKAGE(Iconv)
383 384 385
ENDIF()
IF (ICONV_FOUND)
	ADD_DEFINITIONS(-DGIT_USE_ICONV)
386
	INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR})
387
	LIST(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
388
ENDIF()
389

390
# Platform specific compilation flags
391 392
IF (MSVC)

nulltoken committed
393
	STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
394

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

399
	IF (STDCALL)
400 401
		# /Gz - stdcall calling convention
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
402
	ENDIF ()
403

404 405 406 407
	IF (STATIC_CRT)
		SET(CRT_FLAG_DEBUG "/MTd")
		SET(CRT_FLAG_RELEASE "/MT")
	ELSE()
nulltoken committed
408
		SET(CRT_FLAG_DEBUG "/MDd")
409 410 411
		SET(CRT_FLAG_RELEASE "/MD")
	ENDIF()

412 413
	IF (MSVC_CRTDBG)
		SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG} /DGIT_MSVC_CRTDBG")
414
		SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES}" "Dbghelp.lib")
415 416
	ENDIF()

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

425
	# /DNDEBUG - Disables asserts
426
	# /MT - Statically link the multithreaded release version of the CRT
427
	# /MD - Dynamically link the multithreaded release version of the CRT
428 429 430 431
	# /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
432
	SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
433 434

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

	# /O1 - Optimize for size
438
	SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

	# /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
	SET(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}")

	# /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}")

463
	SET(WIN_RC "src/win32/git2.rc")
464 465

   # Precompiled headers
466

467
ELSE ()
468
	SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra ${CMAKE_C_FLAGS}")
469

470 471 472 473
	IF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
		SET(CMAKE_C_FLAGS "-std=c99 -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
	ENDIF()

474
	IF (WIN32 AND NOT CYGWIN)
475
		SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
476 477
	ENDIF ()

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

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

486 487 488 489 490 491 492
	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 ()

493
	ADD_C_FLAG_IF_SUPPORTED(-Wdocumentation)
494 495 496 497 498 499
	ADD_C_FLAG_IF_SUPPORTED(-Wno-missing-field-initializers)
	ADD_C_FLAG_IF_SUPPORTED(-Wstrict-aliasing=2)
	ADD_C_FLAG_IF_SUPPORTED(-Wstrict-prototypes)
	ADD_C_FLAG_IF_SUPPORTED(-Wdeclaration-after-statement)
	ADD_C_FLAG_IF_SUPPORTED(-Wno-unused-const-variable)
	ADD_C_FLAG_IF_SUPPORTED(-Wno-unused-function)
500

501
	IF (APPLE) # Apple deprecated OpenSSL
502
		ADD_C_FLAG_IF_SUPPORTED(-Wno-deprecated-declarations)
503
	ENDIF()
504

505 506 507 508
	IF (PROFILE)
		SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
		SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
	ENDIF ()
509
ENDIF()
510

511
CHECK_SYMBOL_EXISTS(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)
512 513 514 515
IF (HAVE_REGCOMP_L)
	ADD_DEFINITIONS(-DHAVE_REGCOMP_L)
ENDIF ()

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS)
IF (HAVE_FUTIMENS)
	ADD_DEFINITIONS(-DHAVE_FUTIMENS)
ENDIF ()

CHECK_FUNCTION_EXISTS(qsort_r HAVE_QSORT_R)
IF (HAVE_QSORT_R)
	ADD_DEFINITIONS(-DHAVE_QSORT_R)
ENDIF ()

CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S)
IF (HAVE_QSORT_S)
	ADD_DEFINITIONS(-DHAVE_QSORT_S)
ENDIF ()

531 532 533 534 535 536 537 538 539
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()
540

541 542 543 544 545
IF (SECURITY_FOUND)
  ADD_DEFINITIONS(-DGIT_SECURE_TRANSPORT)
  INCLUDE_DIRECTORIES(${SECURITY_INCLUDE_DIR})
ENDIF ()

546
IF (OPENSSL_FOUND)
547
  ADD_DEFINITIONS(-DGIT_OPENSSL)
548 549
  INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
  SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
550
ENDIF()
551

552 553


554 555
IF (THREADSAFE)
	IF (NOT WIN32)
556
		FIND_PACKAGE(Threads REQUIRED)
557 558 559 560 561
	ENDIF()

	ADD_DEFINITIONS(-DGIT_THREADS)
ENDIF()

562 563 564 565
IF (USE_NSEC)
	ADD_DEFINITIONS(-DGIT_USE_NSEC)
ENDIF()

566 567 568 569 570 571
IF (HAVE_STRUCT_STAT_ST_MTIM)
	ADD_DEFINITIONS(-DGIT_USE_STAT_MTIM)
ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
	ADD_DEFINITIONS(-DGIT_USE_STAT_MTIMESPEC)
ELSEIF (HAVE_STRUCT_STAT_ST_MTIME_NSEC)
	ADD_DEFINITIONS(-DGIT_USE_STAT_MTIME_NSEC)
572 573
ENDIF()

574 575
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)

576
# Collect sourcefiles
577
FILE(GLOB SRC_H include/git2.h include/git2/*.h include/git2/sys/*.h)
578 579

# On Windows use specific platform sources
580
IF (WIN32 AND NOT CYGWIN)
581
	ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0501)
582
	FILE(GLOB SRC_OS src/win32/*.c src/win32/*.h)
583
ELSEIF (AMIGA)
584
	ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
585
ELSE()
586 587 588
	IF (VALGRIND)
		ADD_DEFINITIONS(-DNO_MMAP)
	ENDIF()
589
	FILE(GLOB SRC_OS src/unix/*.c src/unix/*.h)
590
ENDIF()
591
FILE(GLOB SRC_GIT2 src/*.c src/*.h src/transports/*.c src/transports/*.h src/xdiff/*.c src/xdiff/*.h)
592

593 594 595 596 597
# Determine architecture of the machine
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
	ADD_DEFINITIONS(-DGIT_ARCH_64)
ELSEIF (CMAKE_SIZEOF_VOID_P EQUAL 4)
	ADD_DEFINITIONS(-DGIT_ARCH_32)
598
ELSEIF (CMAKE_SIZEOF_VOID_P)
599
	MESSAGE(FATAL_ERROR "Unsupported architecture (pointer size is ${CMAKE_SIZEOF_VOID_P} bytes)")
600 601
ELSE()
	MESSAGE(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)")
602 603
ENDIF()

604
# Compile and link libgit2
605
ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1} ${WIN_RC})
606 607
TARGET_LINK_LIBRARIES(git2 ${SECURITY_DIRS})
TARGET_LINK_LIBRARIES(git2 ${COREFOUNDATION_DIRS})
608
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
Brad Morgan committed
609
TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES})
610
TARGET_LINK_LIBRARIES(git2 ${GSSAPI_LIBRARIES})
611
TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES})
612
TARGET_OS_LIBRARIES(git2)
613

614 615
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
# Win64+MSVC+static libs = linker error
616
IF(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
617 618 619
  SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
ENDIF()

620
IDE_SPLIT_SOURCES(git2)
621

622 623
IF (SONAME)
	SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
624
	SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_SOVERSION})
625 626 627
	IF (LIBGIT2_FILENAME)
		ADD_DEFINITIONS(-DLIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\")
		SET_TARGET_PROPERTIES(git2 PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME})
628 629
	ELSEIF (DEFINED LIBGIT2_PREFIX)
		SET_TARGET_PROPERTIES(git2 PROPERTIES PREFIX "${LIBGIT2_PREFIX}")
630
	ENDIF()
631
ENDIF()
632
STRING(REPLACE ";" " " LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS}")
633
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
634

635
IF (MSVC_IDE)
636 637 638 639 640
   # Precompiled headers
   SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
   SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
ENDIF ()

641
# Install
642
INSTALL(TARGETS git2
643
	RUNTIME DESTINATION ${BIN_INSTALL_DIR}
644 645
	LIBRARY DESTINATION ${LIB_INSTALL_DIR}
	ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
646
)
647
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
648 649
INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
650 651

# Tests
652
IF (BUILD_CLAR)
653 654 655 656 657 658
	FIND_PACKAGE(PythonInterp)

	IF(NOT PYTHONINTERP_FOUND)
	  MESSAGE(FATAL_ERROR "Could not find a python interpeter, which is needed to build the tests. "
	    "Make sure python is available, or pass -DBUILD_CLAR=OFF to skip building the tests")
	ENDIF()
659

660 661 662
	SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
	SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests")
	SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
663
	ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
664
	ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
665
	ADD_DEFINITIONS(-DCLAR_TMPDIR=\"libgit2_tests\")
666

667
	INCLUDE_DIRECTORIES(${CLAR_PATH})
668
	FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/*/*.h)
669
	SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar_libgit2_trace.c" "${CLAR_PATH}/clar_libgit2_timer.c" "${CLAR_PATH}/clar.c")
Vicent Marti committed
670

Vicent Marti committed
671
	ADD_CUSTOM_COMMAND(
672
		OUTPUT ${CLAR_PATH}/clar.suite
673
		COMMAND ${PYTHON_EXECUTABLE} generate.py -f -xonline -xstress .
Vicent Marti committed
674
		DEPENDS ${SRC_TEST}
675
		WORKING_DIRECTORY ${CLAR_PATH}
676
	)
677

678
	SET_SOURCE_FILES_PROPERTIES(
Vicent Marti committed
679
		${CLAR_PATH}/clar.c
680 681
		PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite)

682
	ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1})
683

684 685
	TARGET_LINK_LIBRARIES(libgit2_clar ${COREFOUNDATION_DIRS})
	TARGET_LINK_LIBRARIES(libgit2_clar ${SECURITY_DIRS})
686
	TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
Brad Morgan committed
687
	TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES})
688
	TARGET_LINK_LIBRARIES(libgit2_clar ${GSSAPI_LIBRARIES})
689
	TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES})
690
	TARGET_OS_LIBRARIES(libgit2_clar)
691
	IDE_SPLIT_SOURCES(libgit2_clar)
692

693 694 695 696
	IF (MSVC_IDE)
		# Precompiled headers
		SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
	ENDIF ()
697

Vicent Marti committed
698
	ENABLE_TESTING()
699
	IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND)
700 701 702 703
		ADD_TEST(libgit2_clar libgit2_clar -ionline)
	ELSE ()
		ADD_TEST(libgit2_clar libgit2_clar -v)
	ENDIF ()
704 705 706 707

	# Add a test target which runs the cred callback tests, to be
	# called after setting the url and user
	ADD_TEST(libgit2_clar-cred_callback libgit2_clar -v -sonline::clone::cred_callback)
708 709
	ADD_TEST(libgit2_clar-proxy_credentials_in_url libgit2_clar -v -sonline::clone::proxy_credentials_in_url)
	ADD_TEST(libgit2_clar-proxy_credentials_request libgit2_clar -v	-sonline::clone::proxy_credentials_request)
Vicent Marti committed
710
ENDIF ()
Clemens Buchacher committed
711 712 713 714

IF (TAGS)
	FIND_PROGRAM(CTAGS ctags)
	IF (NOT CTAGS)
715
		MESSAGE(FATAL_ERROR "Could not find ctags command")
Clemens Buchacher committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729
	ENDIF ()

	FILE(GLOB_RECURSE SRC_ALL *.[ch])

	ADD_CUSTOM_COMMAND(
		OUTPUT tags
		COMMAND ${CTAGS} -a ${SRC_ALL}
		DEPENDS ${SRC_ALL}
	)
	ADD_CUSTOM_TARGET(
		do_tags ALL
		DEPENDS tags
	)
ENDIF ()
730 731

IF (BUILD_EXAMPLES)
732
	ADD_SUBDIRECTORY(examples)
733
ENDIF ()