CMakeLists.txt 6.04 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
FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
18 19 20 21 22 23

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

24
# Find required dependencies
25 26 27 28
INCLUDE_DIRECTORIES(src include deps/http-parser)

FILE(GLOB SRC_HTTP deps/http-parser/*.c)

29 30
IF (NOT WIN32)
	FIND_PACKAGE(ZLIB)
31 32 33 34
ELSE()
	# Windows doesn't understand POSIX regex on its own
	INCLUDE_DIRECTORIES(deps/regex)
	SET(SRC_REGEX deps/regex/regex.c)
35 36 37 38 39 40 41 42 43 44
ENDIF()

IF (ZLIB_FOUND)
	INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
	LINK_LIBRARIES(${ZLIB_LIBRARIES})
ELSE (ZLIB_FOUND)
	INCLUDE_DIRECTORIES(deps/zlib)
	ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
	FILE(GLOB SRC_ZLIB deps/zlib/*.c)
ENDIF()
45

46
# Installation paths
47 48
SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
49
SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
50 51

# Build options
52
OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
53
OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
54
OPTION (BUILD_TESTS "Build Tests" ON)
55
OPTION (BUILD_CLAR "Build Tests using the Clar suite" OFF)
Clemens Buchacher committed
56
OPTION (TAGS "Generate tags" OFF)
57

58
# Platform specific compilation flags
59
IF (MSVC)
60
	# Not using __stdcall with the CRT causes problems
61 62
	OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)

63
	SET(CMAKE_C_FLAGS "/W4 /nologo /Zi ${CMAKE_C_FLAGS}")
64 65 66
	IF (STDCALL)
	  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
	ENDIF ()
67 68
	# TODO: bring back /RTC1 /RTCc
	SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
69
	SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
70
	SET(WIN_RC "src/win32/git2.rc")
71
ELSE ()
72
	SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
73
	SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
74 75 76
	IF (NOT MINGW) # MinGW always does PIC and complains if we tell it to
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
	ENDIF ()
77 78
ENDIF()

79
# Build Debug by default
80
IF (NOT CMAKE_BUILD_TYPE)
Vicent Marti committed
81
	SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
82 83
ENDIF ()

84 85 86 87 88 89 90 91
IF (THREADSAFE)
	IF (NOT WIN32)
		find_package(Threads REQUIRED)
	ENDIF()

	ADD_DEFINITIONS(-DGIT_THREADS)
ENDIF()

92 93
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)

94
# Collect sourcefiles
95
FILE(GLOB SRC_H include/git2/*.h)
96 97

# On Windows use specific platform sources
98
IF (WIN32 AND NOT CYGWIN)
99
	ADD_DEFINITIONS(-DWIN32 -D_DEBUG)
100
	FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c)
101
ELSE()
102
	FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
103 104
ENDIF ()

105
# Compile and link libgit2
106
ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC})
107 108 109

IF (WIN32)
	TARGET_LINK_LIBRARIES(git2 ws2_32)
110 111
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
	TARGET_LINK_LIBRARIES(git2 socket nsl)
112 113
ENDIF ()

Vicent Marti committed
114
TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT})
115 116
SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
117
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
118 119

# Install
120
INSTALL(TARGETS git2
121 122 123
	RUNTIME DESTINATION ${INSTALL_BIN}
	LIBRARY DESTINATION ${INSTALL_LIB}
	ARCHIVE DESTINATION ${INSTALL_LIB}
124
)
125
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
126 127
INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
128 129

# Tests
Vicent Marti committed
130
IF (BUILD_TESTS)
131 132 133
	SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
	ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")

134
	INCLUDE_DIRECTORIES(tests)
Vicent Marti committed
135 136
	FILE(GLOB SRC_TEST tests/t??-*.c)

137
	ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
Vicent Marti committed
138
	TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
139 140 141 142 143
	IF (WIN32)
		TARGET_LINK_LIBRARIES(libgit2_test ws2_32)
	ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
		TARGET_LINK_LIBRARIES(libgit2_test socket nsl)
	ENDIF ()
Vicent Marti committed
144 145 146

	ENABLE_TESTING()
	ADD_TEST(libgit2_test libgit2_test)
Vicent Marti committed
147
ENDIF ()
Vicent Marti committed
148

149
IF (BUILD_CLAR)
150 151
	FIND_PACKAGE(PythonInterp REQUIRED)

152 153 154
	SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
	SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
	ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
155

156 157
	INCLUDE_DIRECTORIES(${CLAR_PATH})
	FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
Vicent Marti committed
158

159
	ADD_CUSTOM_COMMAND(
160 161 162 163
		OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
		COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
		DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
		WORKING_DIRECTORY ${CLAR_PATH}
164
	)
165
	ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
166
	TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT})
Vicent Marti committed
167
	IF (WIN32)
168
		TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
Vicent Marti committed
169
	ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
170
		TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
Vicent Marti committed
171 172 173
	ENDIF ()

	ENABLE_TESTING()
174
	ADD_TEST(libgit2_clar libgit2_clar)
Vicent Marti committed
175
ENDIF ()
Clemens Buchacher committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

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 ()