CMakeLists.txt 13.2 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 16
PROJECT(libgit2 C)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

17 18
# Build options
#
19
OPTION( SONAME				"Set the (SO)VERSION of the target"		ON  )
20 21 22 23 24 25
OPTION( BUILD_SHARED_LIBS	"Build Shared Library (OFF for Static)"	ON  )
OPTION( THREADSAFE			"Build libgit2 as threadsafe"			OFF )
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
26
OPTION( ENABLE_TRACE		"Enables tracing support"				OFF )
27
IF(MSVC)
28 29 30
	# This option is only availalbe when building with MSVC. By default,
	# libgit2 is build using the stdcall calling convention, as that's what
	# the CLR expects by default and how the Windows API is built.
31
	#
32 33
	# If you are writing a C or C++ program and want to link to libgit2, you
	# have to either:
34
	# - Add /Gz to the compiler options of _your_ program / library.
35
	# - Turn this off by invoking CMake with the "-DSTDCALL=Off" argument.
36 37
	#
	OPTION( STDCALL			"Build libgit2 with the __stdcall convention"	ON  )
38 39 40 41

	# 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  )
42 43 44 45 46 47 48 49
ENDIF()

# 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.")

50 51 52 53 54 55 56 57 58 59 60
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)
	ENDIF ()
	IF(THREADSAFE)
		TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
	ENDIF()
ENDFUNCTION()

61 62 63 64 65
# 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
# empty virtual folders "tests-clar" for the git2.dll
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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()

84
FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
85 86 87 88 89 90

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

91
# Find required dependencies
92
INCLUDE_DIRECTORIES(src include)
93

94 95 96
IF (WIN32 AND NOT MINGW)
	ADD_DEFINITIONS(-DGIT_WINHTTP)
ELSE ()
97 98 99
	IF (NOT AMIGA)
		FIND_PACKAGE(OpenSSL)
	ENDIF ()
100
	FILE(GLOB SRC_HTTP deps/http-parser/*.c)
101
	INCLUDE_DIRECTORIES(deps/http-parser)
102
ENDIF()
103

104
# Specify sha1 implementation
Edward Thomson committed
105
IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
106 107 108 109 110 111
    ADD_DEFINITIONS(-DWIN32_SHA1)
    FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
	ADD_DEFINITIONS(-DOPENSSL_SHA1)
ELSE()
	FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
112 113
ENDIF()

Edward Thomson committed
114 115 116 117 118
# Enable tracing
IF (ENABLE_TRACE STREQUAL "ON")
	ADD_DEFINITIONS(-DGIT_TRACE)
ENDIF()

119
# Include POSIX regex when it is required
120
IF(WIN32 OR AMIGA)
121 122
	INCLUDE_DIRECTORIES(deps/regex)
	SET(SRC_REGEX deps/regex/regex.c)
123
ENDIF()
124

125 126
# Optional external dependency: zlib
IF(NOT ZLIB_LIBRARY)
127 128
	# It's optional, but FIND_PACKAGE gives a warning that looks more like an
	# error.
129 130
	FIND_PACKAGE(ZLIB QUIET)
ENDIF()
131 132 133
IF (ZLIB_FOUND)
	INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
	LINK_LIBRARIES(${ZLIB_LIBRARIES})
134 135
ELSE()
	MESSAGE( "zlib was not found; using bundled 3rd-party sources." )
136 137 138
	INCLUDE_DIRECTORIES(deps/zlib)
	ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
	FILE(GLOB SRC_ZLIB deps/zlib/*.c)
Chris Young committed
139
ENDIF()
140

141
# Platform specific compilation flags
142 143
IF (MSVC)

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

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

150
	IF (STDCALL)
151 152
		# /Gz - stdcall calling convention
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
153
	ENDIF ()
154

155 156 157 158 159 160 161 162
	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()

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

171
	# /DNDEBUG - Disables asserts
172
	# /MT - Statically link the multithreaded release version of the CRT
173
	# /MD - Dynamically link the multithreaded release version of the CRT
174 175 176 177
	# /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
178
	SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
179 180

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

	# /O1 - Optimize for size
184
	SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

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

209
	SET(WIN_RC "src/win32/git2.rc")
210 211

   # Precompiled headers
212

213
ELSE ()
214
	SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes ${CMAKE_C_FLAGS}")
215 216 217 218 219

	IF (WIN32 AND NOT CYGWIN)
		SET(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
	ENDIF ()

220 221
	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}")
222 223 224 225 226
		# 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)

227
	ELSEIF (BUILD_SHARED_LIBS)
228
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
229
	ENDIF ()
230 231 232
	IF (APPLE) # Apple deprecated OpenSSL
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
	ENDIF ()
233 234 235 236
	IF (PROFILE)
		SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
		SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
	ENDIF ()
237
ENDIF()
238

239 240 241 242 243 244 245 246 247
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()
248

249 250 251 252
IF (OPENSSL_FOUND)
  ADD_DEFINITIONS(-DGIT_SSL)
  INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
  SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
253
ENDIF()
254

255 256 257 258 259 260 261 262
IF (THREADSAFE)
	IF (NOT WIN32)
		find_package(Threads REQUIRED)
	ENDIF()

	ADD_DEFINITIONS(-DGIT_THREADS)
ENDIF()

263 264
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)

265
# Collect sourcefiles
266
FILE(GLOB SRC_H include/git2/*.h)
267 268

# On Windows use specific platform sources
269
IF (WIN32 AND NOT CYGWIN)
270
	ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0501)
271
	FILE(GLOB SRC_OS src/win32/*.c)
272 273
ELSEIF (AMIGA)
	ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
274
	FILE(GLOB SRC_OS src/amiga/*.c)
275
ELSE()
276 277 278
	FILE(GLOB SRC_OS src/unix/*.c)
ENDIF()
FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c)
279

280 281 282 283 284 285 286 287 288
# 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()
	message(FATAL_ERROR "Unsupported architecture")
ENDIF()

289
# Compile and link libgit2
290
ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
291 292
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
TARGET_OS_LIBRARIES(git2)
293

294 295 296 297 298 299
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
# Win64+MSVC+static libs = linker error
IF(MSVC AND NOT BUILD_SHARED_LIBS AND (${CMAKE_SIZEOF_VOID_P} MATCHES "8") )
  SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
ENDIF()

300 301
MSVC_SPLIT_SOURCES(git2)

302 303 304 305
IF (SONAME)
	SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
	SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
ENDIF()
306
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
307

308
IF (MSVC_IDE)
309 310 311 312 313
   # 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 ()

314
# Install
315
INSTALL(TARGETS git2
316
	RUNTIME DESTINATION ${BIN_INSTALL_DIR}
317 318
	LIBRARY DESTINATION ${LIB_INSTALL_DIR}
	ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
319
)
320
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
321 322
INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
323 324

# Tests
325
IF (BUILD_CLAR)
326 327
	FIND_PACKAGE(PythonInterp REQUIRED)

328
	SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
329
	SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
330
	SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
331
	ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
332
	ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
333

334
	INCLUDE_DIRECTORIES(${CLAR_PATH})
335
	FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c)
Vicent Marti committed
336
	SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar.c")
Vicent Marti committed
337

Vicent Marti committed
338
	ADD_CUSTOM_COMMAND(
339
		OUTPUT ${CLAR_PATH}/clar.suite
Vicent Marti committed
340
		COMMAND ${PYTHON_EXECUTABLE} generate.py -xonline .
Vicent Marti committed
341
		DEPENDS ${SRC_TEST}
342
		WORKING_DIRECTORY ${CLAR_PATH}
343
	)
344

345
	SET_SOURCE_FILES_PROPERTIES(
Vicent Marti committed
346
		${CLAR_PATH}/clar.c
347 348 349 350
		PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite)

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

351 352
	TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
	TARGET_OS_LIBRARIES(libgit2_clar)
353
	MSVC_SPLIT_SOURCES(libgit2_clar)
354

355 356 357 358
	IF (MSVC_IDE)
		# Precompiled headers
		SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
	ENDIF ()
359

Vicent Marti committed
360
	ENABLE_TESTING()
361
	ADD_TEST(libgit2_clar libgit2_clar -ionline)
Vicent Marti committed
362
ENDIF ()
Clemens Buchacher committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

IF (TAGS)
	FIND_PROGRAM(CTAGS ctags)
	IF (NOT CTAGS)
		message(FATAL_ERROR "Could not find ctags command")
	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 ()
382 383 384 385

IF (BUILD_EXAMPLES)
	FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
	ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
386 387 388 389 390
	IF(WIN32)
		TARGET_LINK_LIBRARIES(cgit2 git2)
	ELSE()
		TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
	ENDIF()
391

392 393
	ADD_EXECUTABLE(git-diff examples/diff.c)
	TARGET_LINK_LIBRARIES(git-diff git2)
394

395 396
	ADD_EXECUTABLE(git-general examples/general.c)
	TARGET_LINK_LIBRARIES(git-general git2)
397

398 399
	ADD_EXECUTABLE(git-showindex examples/showindex.c)
	TARGET_LINK_LIBRARIES(git-showindex git2)
400 401 402

	ADD_EXECUTABLE(git-rev-list examples/rev-list.c)
	TARGET_LINK_LIBRARIES(git-rev-list git2)
403
ENDIF ()