CMakeLists.txt 13.1 KB
Newer Older
1 2 3
add_library(git2internal OBJECT)
set_target_properties(git2internal PROPERTIES C_STANDARD 90)

4 5 6
IF(DEBUG_POOL)
	SET(GIT_DEBUG_POOL 1)
ENDIF()
7
ADD_FEATURE_INFO(debugpool GIT_DEBUG_POOL "debug pool allocator")
8

9 10
INCLUDE(PkgBuildConfig)

11 12 13 14 15 16 17 18 19
# 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 "")

20 21
SET(LIBGIT2_INCLUDES
	"${CMAKE_CURRENT_BINARY_DIR}"
22 23
	"${libgit2_SOURCE_DIR}/src"
	"${libgit2_SOURCE_DIR}/include")
24
SET(LIBGIT2_SYSTEM_INCLUDES "")
25 26
SET(LIBGIT2_LIBS "")

27 28
enable_warnings(missing-declarations)

29
# Enable tracing
30
IF(ENABLE_TRACE)
31 32
	SET(GIT_TRACE 1)
ENDIF()
33
ADD_FEATURE_INFO(tracing GIT_TRACE "tracing support")
34 35 36 37 38 39

CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS)
IF (HAVE_FUTIMENS)
	SET(GIT_USE_FUTIMENS 1)
ENDIF ()

40 41 42 43
CHECK_PROTOTYPE_DEFINITION(qsort_r
	"void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *))"
	"" "stdlib.h" HAVE_QSORT_R_BSD)
IF (HAVE_QSORT_R_BSD)
44
	target_compile_definitions(git2internal PRIVATE HAVE_QSORT_R_BSD)
45 46 47 48 49 50
ENDIF()

CHECK_PROTOTYPE_DEFINITION(qsort_r
	"void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg)"
	"" "stdlib.h" HAVE_QSORT_R_GNU)
IF (HAVE_QSORT_R_GNU)
51
	target_compile_definitions(git2internal PRIVATE HAVE_QSORT_R_GNU)
52
ENDIF()
53 54 55

CHECK_FUNCTION_EXISTS(qsort_s HAVE_QSORT_S)
IF (HAVE_QSORT_S)
56
	target_compile_definitions(git2internal PRIVATE HAVE_QSORT_S)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
ENDIF ()

# Find required dependencies

IF(WIN32)
	LIST(APPEND LIBGIT2_LIBS ws2_32)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
	LIST(APPEND LIBGIT2_LIBS socket nsl)
	LIST(APPEND LIBGIT2_PC_LIBS "-lsocket" "-lnsl")
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Haiku")
	LIST(APPEND LIBGIT2_LIBS network)
	LIST(APPEND LIBGIT2_PC_LIBS "-lnetwork")
ENDIF()

CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT)
IF(NEED_LIBRT)
	LIST(APPEND LIBGIT2_LIBS rt)
	LIST(APPEND LIBGIT2_PC_LIBS "-lrt")
ENDIF()

IF(THREADSAFE)
	LIST(APPEND LIBGIT2_LIBS ${CMAKE_THREAD_LIBS_INIT})
	LIST(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT})
ENDIF()
81
ADD_FEATURE_INFO(threadsafe THREADSAFE "threadsafe support")
82 83


84 85 86
if(WIN32 AND EMBED_SSH_PATH)
	file(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
	list(SORT SRC_SSH)
87 88
	target_sources(git2internal PRIVATE ${SRC_SSH})

89 90 91 92
	list(APPEND LIBGIT2_SYSTEM_INCLUDES "${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\"")
	set(GIT_SSH 1)
endif()
93 94 95 96 97 98 99

IF (WIN32 AND WINHTTP)
	SET(GIT_WINHTTP 1)

	# 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)
100
		ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/winhttp" "${libgit2_BINARY_DIR}/deps/winhttp")
101
		LIST(APPEND LIBGIT2_LIBS winhttp)
102
		LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
103 104 105 106 107 108 109 110 111
	ELSE()
		LIST(APPEND LIBGIT2_LIBS "winhttp")
		LIST(APPEND LIBGIT2_PC_LIBS "-lwinhttp")
	ENDIF ()

	LIST(APPEND LIBGIT2_LIBS "rpcrt4" "crypt32" "ole32")
	LIST(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32")
ENDIF()

112 113 114
include(SelectHTTPSBackend)
include(SelectHashes)
target_sources(git2internal PRIVATE ${SRC_SHA1})
115

116
# Specify regular expression implementation
117 118
FIND_PACKAGE(PCRE)

119
IF(REGEX_BACKEND STREQUAL "")
120 121 122
	CHECK_SYMBOL_EXISTS(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)

	IF(HAVE_REGCOMP_L)
123
		SET(REGEX_BACKEND "regcomp_l")
124
	ELSEIF(PCRE_FOUND)
125
		SET(REGEX_BACKEND "pcre")
126
	ELSE()
127
		SET(REGEX_BACKEND "builtin")
128 129 130
	ENDIF()
ENDIF()

131
IF(REGEX_BACKEND STREQUAL "regcomp_l")
132
	ADD_FEATURE_INFO(regex ON "using system regcomp_l")
133
	SET(GIT_REGEX_REGCOMP_L 1)
134
ELSEIF(REGEX_BACKEND STREQUAL "pcre2")
135 136 137 138 139 140 141 142 143 144 145
	FIND_PACKAGE(PCRE2)

	IF(NOT PCRE2_FOUND)
		MESSAGE(FATAL_ERROR "PCRE2 support was requested but not found")
	ENDIF()

	ADD_FEATURE_INFO(regex ON "using system PCRE2")
	SET(GIT_REGEX_PCRE2 1)

	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE2_INCLUDE_DIRS})
	LIST(APPEND LIBGIT2_LIBS ${PCRE2_LIBRARIES})
146
	LIST(APPEND LIBGIT2_PC_REQUIRES "libpcre2-8")
147
ELSEIF(REGEX_BACKEND STREQUAL "pcre")
148 149
	ADD_FEATURE_INFO(regex ON "using system PCRE")
	SET(GIT_REGEX_PCRE 1)
150 151 152

	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE_INCLUDE_DIRS})
	LIST(APPEND LIBGIT2_LIBS ${PCRE_LIBRARIES})
153
	LIST(APPEND LIBGIT2_PC_REQUIRES "libpcre")
154
ELSEIF(REGEX_BACKEND STREQUAL "regcomp")
155 156
	ADD_FEATURE_INFO(regex ON "using system regcomp")
	SET(GIT_REGEX_REGCOMP 1)
157
ELSEIF(REGEX_BACKEND STREQUAL "builtin")
158
	ADD_FEATURE_INFO(regex ON "using bundled PCRE")
159
	SET(GIT_REGEX_BUILTIN 1)
160

161 162 163
	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/pcre" "${libgit2_BINARY_DIR}/deps/pcre")
	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/pcre")
	LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:pcre>)
164
ELSE()
165
	MESSAGE(FATAL_ERROR "The REGEX_BACKEND option provided is not supported")
166 167
ENDIF()

168
# Optional external dependency: http-parser
169 170 171 172 173 174 175 176 177 178 179
IF(USE_HTTP_PARSER STREQUAL "system")
	FIND_PACKAGE(HTTP_Parser)

	IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
		LIST(APPEND LIBGIT2_LIBS ${HTTP_PARSER_LIBRARIES})
		LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
		ADD_FEATURE_INFO(http-parser ON "http-parser support (system)")
	ELSE()
		MESSAGE(FATAL_ERROR "http-parser support was requested but not found")
	ENDIF()
180 181
ELSE()
	MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
182
	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/http-parser" "${libgit2_BINARY_DIR}/deps/http-parser")
183
	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
184
	LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
185
	ADD_FEATURE_INFO(http-parser ON "http-parser support (bundled)")
186 187 188
ENDIF()

# Optional external dependency: zlib
189 190 191
IF(NOT USE_BUNDLED_ZLIB)
	FIND_PACKAGE(ZLIB)
	IF(ZLIB_FOUND)
192
		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${ZLIB_INCLUDE_DIRS})
193
		LIST(APPEND LIBGIT2_LIBS ${ZLIB_LIBRARIES})
194 195 196 197 198
		IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
			LIST(APPEND LIBGIT2_PC_LIBS "-lz")
		ELSE()
			LIST(APPEND LIBGIT2_PC_REQUIRES "zlib")
		ENDIF()
199
		ADD_FEATURE_INFO(zlib ON "using system zlib")
200
	ELSE()
201
		MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." )
202
	ENDIF()
203 204
ENDIF()
IF(USE_BUNDLED_ZLIB OR NOT ZLIB_FOUND)
205
	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/zlib" "${libgit2_BINARY_DIR}/deps/zlib")
206
	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
207
	LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
208
	ADD_FEATURE_INFO(zlib ON "using bundled zlib")
209 210 211 212
ENDIF()

# Optional external dependency: libssh2
IF (USE_SSH)
213
	FIND_PKGLIBRARIES(LIBSSH2 libssh2)
214 215 216
ENDIF()
IF (LIBSSH2_FOUND)
	SET(GIT_SSH 1)
217
	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
218 219 220 221 222 223 224 225 226 227
	LIST(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
	LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})

	CHECK_LIBRARY_EXISTS("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
	IF (HAVE_LIBSSH2_MEMORY_CREDENTIALS)
		SET(GIT_SSH_MEMORY_CREDENTIALS 1)
	ENDIF()
ELSE()
	MESSAGE(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
ENDIF()
228
ADD_FEATURE_INFO(SSH GIT_SSH "SSH transport support")
229

230 231 232 233 234 235 236 237 238
# Optional external dependency: ntlmclient
IF (USE_NTLMCLIENT)
	SET(GIT_NTLM 1)
	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/ntlmclient" "${libgit2_BINARY_DIR}/deps/ntlmclient")
	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/ntlmclient")
	LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:ntlmclient>")
ENDIF()
ADD_FEATURE_INFO(ntlmclient GIT_NTLM "NTLM authentication support for Unix")

239 240 241
# Optional external dependency: GSSAPI

INCLUDE(SelectGSSAPI)
242 243

# Optional external dependency: iconv
244
IF (USE_ICONV)
245 246 247 248
	FIND_PACKAGE(Iconv)
ENDIF()
IF (ICONV_FOUND)
	SET(GIT_USE_ICONV 1)
249
	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${ICONV_INCLUDE_DIR})
250 251 252
	LIST(APPEND LIBGIT2_LIBS ${ICONV_LIBRARIES})
	LIST(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
ENDIF()
253
ADD_FEATURE_INFO(iconv GIT_USE_ICONV "iconv encoding conversion support")
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275


IF (THREADSAFE)
	IF (NOT WIN32)
		FIND_PACKAGE(Threads REQUIRED)
	ENDIF()

	SET(GIT_THREADS 1)
ENDIF()

IF (USE_NSEC)
	SET(GIT_USE_NSEC 1)
ENDIF()

IF (HAVE_STRUCT_STAT_ST_MTIM)
	SET(GIT_USE_STAT_MTIM 1)
ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
	SET(GIT_USE_STAT_MTIMESPEC 1)
ELSEIF (HAVE_STRUCT_STAT_ST_MTIME_NSEC)
	SET(GIT_USE_STAT_MTIME_NSEC 1)
ENDIF()

276
target_compile_definitions(git2internal PRIVATE _FILE_OFFSET_BITS=64)
277 278

# Collect sourcefiles
279
file(GLOB SRC_H
280 281 282
	"${libgit2_SOURCE_DIR}/include/git2.h"
	"${libgit2_SOURCE_DIR}/include/git2/*.h"
	"${libgit2_SOURCE_DIR}/include/git2/sys/*.h")
283
list(SORT SRC_H)
284
target_sources(git2internal PRIVATE ${SRC_H})
285 286

# On Windows use specific platform sources
287
if(WIN32 AND NOT CYGWIN)
288 289
	SET(WIN_RC "win32/git2.rc")

290 291
	file(GLOB SRC_OS win32/*.c win32/*.h)
	list(SORT SRC_OS)
292
	target_sources(git2internal PRIVATE ${SRC_OS})
293
elseif(AMIGA)
294
	target_compile_definitions(git2internal PRIVATE NO_ADDRINFO NO_READDIR_R NO_MMAP)
295 296 297
else()
	file(GLOB SRC_OS unix/*.c unix/*.h)
	list(SORT SRC_OS)
298
	target_sources(git2internal PRIVATE ${SRC_OS})
299
endif()
300

301
IF (USE_LEAK_CHECKER STREQUAL "valgrind")
302
	target_compile_definitions(git2internal PRIVATE VALGRIND)
303 304
ENDIF()

305
file(GLOB SRC_GIT2 *.c *.h
306
	allocators/*.c allocators/*.h
307 308 309
	streams/*.c streams/*.h
	transports/*.c transports/*.h
	xdiff/*.c xdiff/*.h)
310
list(SORT SRC_GIT2)
311
target_sources(git2internal PRIVATE ${SRC_GIT2})
312

313 314 315 316
IF(APPLE)
	# The old Secure Transport API has been deprecated in macOS 10.15.
	SET_SOURCE_FILES_PROPERTIES(streams/stransport.c PROPERTIES COMPILE_FLAGS -Wno-deprecated)
ENDIF()
317

318 319 320 321 322 323 324
# the xdiff dependency is not (yet) warning-free, disable warnings as
# errors for the xdiff sources until we've sorted them out
IF(MSVC)
	SET_SOURCE_FILES_PROPERTIES(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS -WX-)
	SET_SOURCE_FILES_PROPERTIES(xdiff/xutils.c PROPERTIES COMPILE_FLAGS -WX-)
ENDIF()

325 326 327 328 329 330 331 332 333 334 335 336 337
# Determine architecture of the machine
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
	SET(GIT_ARCH_64 1)
ELSEIF (CMAKE_SIZEOF_VOID_P EQUAL 4)
	SET(GIT_ARCH_32 1)
ELSEIF (CMAKE_SIZEOF_VOID_P)
	MESSAGE(FATAL_ERROR "Unsupported architecture (pointer size is ${CMAKE_SIZEOF_VOID_P} bytes)")
ELSE()
	MESSAGE(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)")
ENDIF()

CONFIGURE_FILE(features.h.in git2/sys/features.h)

338 339
IDE_SPLIT_SOURCES(git2internal)
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:git2internal>)
340

341 342
TARGET_INCLUDE_DIRECTORIES(git2internal PRIVATE ${LIBGIT2_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include)
TARGET_INCLUDE_DIRECTORIES(git2internal SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES})
343

344
SET(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE)
345
SET(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE)
346
SET(LIBGIT2_SYSTEM_INCLUDES ${LIBGIT2_SYSTEM_INCLUDES} PARENT_SCOPE)
347 348
SET(LIBGIT2_LIBS ${LIBGIT2_LIBS} PARENT_SCOPE)

349 350 351 352 353 354 355
IF(XCODE_VERSION)
	# This is required for Xcode to actually link the libgit2 library
	# when using only object libraries.
	FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.c "")
	LIST(APPEND LIBGIT2_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/dummy.c)
ENDIF()

356
# Compile and link libgit2
357
ADD_LIBRARY(git2 ${WIN_RC} ${LIBGIT2_OBJECTS})
358 359
TARGET_LINK_LIBRARIES(git2 ${LIBGIT2_LIBS})

360
SET_TARGET_PROPERTIES(git2 PROPERTIES C_STANDARD 90)
361 362 363
SET_TARGET_PROPERTIES(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
SET_TARGET_PROPERTIES(git2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
SET_TARGET_PROPERTIES(git2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
364 365 366 367 368 369 370 371 372

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

IDE_SPLIT_SOURCES(git2)

373 374 375 376
if(SONAME)
	set_target_properties(git2 PROPERTIES VERSION ${libgit2_VERSION})
	set_target_properties(git2 PROPERTIES SOVERSION "${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
	if(LIBGIT2_FILENAME)
377
		target_compile_definitions(git2internal PRIVATE LIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\")
378 379 380 381 382
		set_target_properties(git2 PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME})
	elseif(DEFINED LIBGIT2_PREFIX)
		set_target_properties(git2 PROPERTIES PREFIX "${LIBGIT2_PREFIX}")
	endif()
endif()
383

384
PKG_BUILD_CONFIG(NAME libgit2
385
	VERSION ${libgit2_VERSION}
386 387 388 389 390
	DESCRIPTION "The git library, take 2"
	LIBS_SELF git2
	PRIVATE_LIBS ${LIBGIT2_PC_LIBS}
	REQUIRES ${LIBGIT2_PC_REQUIRES}
)
391 392 393 394 395 396 397 398 399

IF (MSVC_IDE)
   # Precompiled headers
   SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
   SET_SOURCE_FILES_PROPERTIES(win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
ENDIF ()

# Install
INSTALL(TARGETS git2
400 401 402
	RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
403
)
404 405
INSTALL(DIRECTORY ${libgit2_SOURCE_DIR}/include/git2 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
INSTALL(FILES ${libgit2_SOURCE_DIR}/include/git2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})