CMakeLists.txt 19.7 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 15
PROJECT(libgit2 C)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
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(AddCFlagIfSupported)
23
INCLUDE(FindPkgConfig)
24

25 26
# Build options
#
Miha committed
27
OPTION( SONAME				"Set the (SO)VERSION of the target"		ON  )
28
OPTION( BUILD_SHARED_LIBS	"Build Shared Library (OFF for Static)"	ON  )
29
OPTION( THREADSAFE			"Build libgit2 as threadsafe"			ON )
30 31 32 33
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
34
OPTION( ENABLE_TRACE		"Enables tracing support"				OFF )
35
OPTION( LIBGIT2_FILENAME	"Name of the produced binary"			OFF )
36

37
OPTION( USE_ICONV			"Link with and use iconv library" 		OFF )
38
OPTION( USE_SSH				"Link with libssh to enable SSH support" ON )
39
OPTION( USE_GSSAPI			"Link with libgssapi for SPNEGO auth"   OFF )
40
OPTION( VALGRIND			"Configure build for valgrind"			OFF )
41

42
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
43
	SET( USE_ICONV ON )
44 45
	FIND_PACKAGE(Security)
	FIND_PACKAGE(CoreFoundation REQUIRED)
46
ENDIF()
47

48
IF(MSVC)
Ben Straub committed
49
	# This option is only available when building with MSVC. By default, libgit2
50 51
	# is build using the cdecl calling convention, which is useful if you're
	# writing C. However, the CLR and Win32 API both expect stdcall.
52
	#
53 54 55
	# 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  )
56 57 58 59

	# 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  )
60

61
	ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS)
62 63
	ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
	ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
64 65
ENDIF()

66

67 68 69 70 71 72
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()

73 74 75 76 77
IF(MSVC)
	# Enable MSVC CRTDBG memory leak reporting when in debug mode.
	OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
ENDIF()

78 79 80 81
IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
	OPTION( USE_OPENSSL                     "Link with and use openssl library"             ON )
ENDIF()

82 83 84 85 86 87 88 89 90
# 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 "")

91 92 93 94 95 96
# 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.")

97 98 99 100 101
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)
102
		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lsocket -lnsl" PARENT_SCOPE)
103 104 105
	ENDIF()
	CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT)
	IF(NEED_LIBRT)
106
		TARGET_LINK_LIBRARIES(${target} rt)
107
		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lrt" PARENT_SCOPE)
108 109
	ENDIF()

110 111 112 113 114
	IF(THREADSAFE)
		TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
	ENDIF()
ENDFUNCTION()

115 116 117 118
# For the MSVC IDE, this function splits up the source files like windows
# explorer does. This is esp. useful with the libgit2_clar project, were
# usually 2 or more files share the same name.  Sadly, this file grouping
# is a per-directory option in cmake and not per-target, resulting in
119
# empty virtual folders "tests" for the git2.dll
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
FUNCTION(MSVC_SPLIT_SOURCES target)
	IF(MSVC_IDE)
		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()

138
FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
139 140 141 142 143 144

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

145 146 147
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}")

148
# Find required dependencies
149
INCLUDE_DIRECTORIES(src include)
150

151 152
IF (SECURITY_FOUND)
  MESSAGE("-- Found Security ${SECURITY_DIRS}")
153
  SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -framework Security")
154 155 156 157
ENDIF()

IF (COREFOUNDATION_FOUND)
  MESSAGE("-- Found CoreFoundation ${COREFOUNDATION_DIRS}")
158
  SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -framework CoreFoundation")
159 160 161
ENDIF()


162
IF (WIN32 AND WINHTTP)
163
	ADD_DEFINITIONS(-DGIT_WINHTTP)
164 165
	INCLUDE_DIRECTORIES(deps/http-parser)
	FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h)
166 167 168 169 170 171 172 173 174

	# 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
175 176
		SET(LIBWINHTTP_PATH "${CMAKE_CURRENT_BINARY_DIR}/deps/winhttp")
		FILE(MAKE_DIRECTORY ${LIBWINHTTP_PATH})
177

178 179 180 181 182 183
		IF ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
			set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp64.def")
		ELSE()
			set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp.def")
		ENDIF()

184 185
		ADD_CUSTOM_COMMAND(
			OUTPUT ${LIBWINHTTP_PATH}/libwinhttp.a
186 187
			COMMAND ${DLLTOOL} -d ${WINHTTP_DEF} -k -D winhttp.dll -l libwinhttp.a
			DEPENDS ${WINHTTP_DEF}
188 189 190 191 192 193 194 195 196
			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
197
		LINK_DIRECTORIES(${LIBWINHTTP_PATH})
198 199
	ENDIF ()

200
	LINK_LIBRARIES(winhttp rpcrt4 crypt32)
201
ELSE ()
202
	IF (NOT AMIGA AND USE_OPENSSL)
203 204
		FIND_PACKAGE(OpenSSL)
	ENDIF ()
205

206
	FIND_PACKAGE(HTTP_Parser)
207 208 209
	IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
		INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS})
		LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES})
210
		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lhttp_parser")
211
	ELSE()
212
		MESSAGE(STATUS "http-parser was not found or is too old; using bundled 3rd-party sources.")
213
		INCLUDE_DIRECTORIES(deps/http-parser)
214
		FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h)
215
	ENDIF()
216
ENDIF()
217

218
# Specify sha1 implementation
Edward Thomson committed
219
IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
nulltoken committed
220 221
	ADD_DEFINITIONS(-DWIN32_SHA1)
	FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
222 223
ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
	ADD_DEFINITIONS(-DGIT_COMMON_CRYPTO)
224 225
ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
	ADD_DEFINITIONS(-DOPENSSL_SHA1)
226 227 228 229 230
	IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lssl")
	ELSE()
		SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} openssl")
	ENDIF ()
231 232
ELSE()
	FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
233 234
ENDIF()

Edward Thomson committed
235 236 237 238 239
# Enable tracing
IF (ENABLE_TRACE STREQUAL "ON")
	ADD_DEFINITIONS(-DGIT_TRACE)
ENDIF()

240
# Include POSIX regex when it is required
241
IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
242 243
	INCLUDE_DIRECTORIES(deps/regex)
	SET(SRC_REGEX deps/regex/regex.c)
244
ENDIF()
245

246
# Optional external dependency: zlib
247
FIND_PACKAGE(ZLIB)
248 249 250
IF (ZLIB_FOUND)
	INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
	LINK_LIBRARIES(${ZLIB_LIBRARIES})
251
	IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
252 253 254 255
		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lz")
	ELSE()
		SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} zlib")
	ENDIF()
256
ELSE()
257
	MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." )
258 259
	INCLUDE_DIRECTORIES(deps/zlib)
	ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
260
	FILE(GLOB SRC_ZLIB deps/zlib/*.c deps/zlib/*.h)
Chris Young committed
261
ENDIF()
262

263
# Optional external dependency: libssh2
264
IF (USE_SSH)
265
	PKG_CHECK_MODULES(LIBSSH2 libssh2)
266
ENDIF()
Brad Morgan committed
267 268
IF (LIBSSH2_FOUND)
	ADD_DEFINITIONS(-DGIT_SSH)
269 270
	INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIRS})
	LINK_DIRECTORIES(${LIBSSH2_LIBRARY_DIRS})
271
	SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} libssh2")
Brad Morgan committed
272
	SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
273 274 275 276 277

	CHECK_LIBRARY_EXISTS(${LIBSSH2_LIBRARIES} libssh2_userauth_publickey_frommemory "" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
	IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)
		ADD_DEFINITIONS(-DGIT_SSH_MEMORY_CREDENTIALS)
	ENDIF()
278 279
ELSE()
	MESSAGE(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
Brad Morgan committed
280 281
ENDIF()

282 283 284 285 286 287 288 289
# Optional external dependency: libgssapi
IF (USE_GSSAPI)
	FIND_PACKAGE(GSSAPI)
ENDIF()
IF (GSSAPI_FOUND)
	ADD_DEFINITIONS(-DGIT_GSSAPI)
ENDIF()

290 291
# Optional external dependency: iconv
IF (USE_ICONV)
292
	FIND_PACKAGE(Iconv)
293 294 295
ENDIF()
IF (ICONV_FOUND)
	ADD_DEFINITIONS(-DGIT_USE_ICONV)
296
	INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR})
297
	SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} ${ICONV_LIBRARIES}")
298
ENDIF()
299

300
# Platform specific compilation flags
301 302
IF (MSVC)

nulltoken committed
303
	STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
304

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

309
	IF (STDCALL)
310 311
		# /Gz - stdcall calling convention
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
312
	ENDIF ()
313

314 315 316 317
	IF (STATIC_CRT)
		SET(CRT_FLAG_DEBUG "/MTd")
		SET(CRT_FLAG_RELEASE "/MT")
	ELSE()
nulltoken committed
318
		SET(CRT_FLAG_DEBUG "/MDd")
319 320 321
		SET(CRT_FLAG_RELEASE "/MD")
	ENDIF()

322 323 324 325
	IF (MSVC_CRTDBG)
		SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG} /DGIT_MSVC_CRTDBG")
	ENDIF()

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

334
	# /DNDEBUG - Disables asserts
335
	# /MT - Statically link the multithreaded release version of the CRT
336
	# /MD - Dynamically link the multithreaded release version of the CRT
337 338 339 340
	# /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
341
	SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
342 343

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

	# /O1 - Optimize for size
347
	SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

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

372
	SET(WIN_RC "src/win32/git2.rc")
373 374

   # Precompiled headers
375

376
ELSE ()
377
	SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra ${CMAKE_C_FLAGS}")
378

379 380 381 382
	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()

383 384 385 386
	IF (WIN32 AND NOT CYGWIN)
		SET(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
	ENDIF ()

387 388
	IF (MINGW) # MinGW always does PIC and complains if we tell it to
		STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
389 390 391 392 393
		# 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)

394
	ELSEIF (BUILD_SHARED_LIBS)
395
		ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden)
396 397

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

400 401 402 403 404 405
	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)
406

407
	IF (APPLE) # Apple deprecated OpenSSL
408
		ADD_C_FLAG_IF_SUPPORTED(-Wno-deprecated-declarations)
409
	ENDIF()
410

411 412 413 414
	IF (PROFILE)
		SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
		SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
	ENDIF ()
415
ENDIF()
416

417 418 419 420 421 422 423 424 425
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()
426

427 428 429 430 431
IF (SECURITY_FOUND)
  ADD_DEFINITIONS(-DGIT_SECURE_TRANSPORT)
  INCLUDE_DIRECTORIES(${SECURITY_INCLUDE_DIR})
ENDIF ()

432
IF (OPENSSL_FOUND)
433
  ADD_DEFINITIONS(-DGIT_OPENSSL)
434 435
  INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
  SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
436
ENDIF()
437

438 439


440 441
IF (THREADSAFE)
	IF (NOT WIN32)
442
		FIND_PACKAGE(Threads REQUIRED)
443 444 445 446 447
	ENDIF()

	ADD_DEFINITIONS(-DGIT_THREADS)
ENDIF()

448 449
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)

450
# Collect sourcefiles
451
FILE(GLOB SRC_H include/git2.h include/git2/*.h include/git2/sys/*.h)
452 453

# On Windows use specific platform sources
454
IF (WIN32 AND NOT CYGWIN)
455
	ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0501)
456
	FILE(GLOB SRC_OS src/win32/*.c src/win32/*.h)
457
ELSEIF (AMIGA)
458
	ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP)
459
ELSE()
460 461 462
	IF (VALGRIND)
		ADD_DEFINITIONS(-DNO_MMAP)
	ENDIF()
463
	FILE(GLOB SRC_OS src/unix/*.c src/unix/*.h)
464
ENDIF()
465
FILE(GLOB SRC_GIT2 src/*.c src/*.h src/transports/*.c src/transports/*.h src/xdiff/*.c src/xdiff/*.h)
466

467 468 469 470 471 472
# 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)
ELSE()
473
	MESSAGE(FATAL_ERROR "Unsupported architecture")
474 475
ENDIF()

476
# Compile and link libgit2
477
ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
478 479
TARGET_LINK_LIBRARIES(git2 ${SECURITY_DIRS})
TARGET_LINK_LIBRARIES(git2 ${COREFOUNDATION_DIRS})
480
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
Brad Morgan committed
481
TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES})
482
TARGET_LINK_LIBRARIES(git2 ${GSSAPI_LIBRARIES})
483
TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES})
484
TARGET_OS_LIBRARIES(git2)
485

486 487
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
# Win64+MSVC+static libs = linker error
488
IF(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
489 490 491
  SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
ENDIF()

492 493
MSVC_SPLIT_SOURCES(git2)

494 495
IF (SONAME)
	SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
496
	SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_SOVERSION})
497 498 499
	IF (LIBGIT2_FILENAME)
		ADD_DEFINITIONS(-DLIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\")
		SET_TARGET_PROPERTIES(git2 PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME})
500
	ENDIF()
501
ENDIF()
502
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
503

504
IF (MSVC_IDE)
505 506 507 508 509
   # 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 ()

510
# Install
511
INSTALL(TARGETS git2
512
	RUNTIME DESTINATION ${BIN_INSTALL_DIR}
513 514
	LIBRARY DESTINATION ${LIB_INSTALL_DIR}
	ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
515
)
516
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
517 518
INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
519 520

# Tests
521
IF (BUILD_CLAR)
522 523
	FIND_PACKAGE(PythonInterp REQUIRED)

524 525 526
	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.")
527
	ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
528
	ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
529
	ADD_DEFINITIONS(-DCLAR_TMPDIR=\"libgit2_tests\")
530

531
	INCLUDE_DIRECTORIES(${CLAR_PATH})
532
	FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/*/*.h)
533
	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
534

Vicent Marti committed
535
	ADD_CUSTOM_COMMAND(
536
		OUTPUT ${CLAR_PATH}/clar.suite
537
		COMMAND ${PYTHON_EXECUTABLE} generate.py -f -xonline -xstress .
Vicent Marti committed
538
		DEPENDS ${SRC_TEST}
539
		WORKING_DIRECTORY ${CLAR_PATH}
540
	)
541

542
	SET_SOURCE_FILES_PROPERTIES(
Vicent Marti committed
543
		${CLAR_PATH}/clar.c
544 545
		PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite)

546
	ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
547

548 549
	TARGET_LINK_LIBRARIES(libgit2_clar ${COREFOUNDATION_DIRS})
	TARGET_LINK_LIBRARIES(libgit2_clar ${SECURITY_DIRS})
550
	TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
Brad Morgan committed
551
	TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES})
552
	TARGET_LINK_LIBRARIES(libgit2_clar ${GSSAPI_LIBRARIES})
553
	TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES})
554
	TARGET_OS_LIBRARIES(libgit2_clar)
555
	MSVC_SPLIT_SOURCES(libgit2_clar)
556

557 558 559 560
	IF (MSVC_IDE)
		# Precompiled headers
		SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
	ENDIF ()
561

Vicent Marti committed
562
	ENABLE_TESTING()
563
	IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND)
564 565 566 567
		ADD_TEST(libgit2_clar libgit2_clar -ionline)
	ELSE ()
		ADD_TEST(libgit2_clar libgit2_clar -v)
	ENDIF ()
Vicent Marti committed
568
ENDIF ()
Clemens Buchacher committed
569 570 571 572

IF (TAGS)
	FIND_PROGRAM(CTAGS ctags)
	IF (NOT CTAGS)
573
		MESSAGE(FATAL_ERROR "Could not find ctags command")
Clemens Buchacher committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587
	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 ()
588 589

IF (BUILD_EXAMPLES)
590
	ADD_SUBDIRECTORY(examples)
591
ENDIF ()