1 # CMake build script for the libgit2 project
3 # Building (out of source build):
4 # > mkdir build && cd build
5 # > cmake .. [-DSETTINGS=VALUE]
12 # > cmake --build . --target install
15 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
17 FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
19 STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
20 STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
21 STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
22 SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
25 # Default AmigaOS to use the PowerPC SHA1
29 # Find required dependencies
30 INCLUDE_DIRECTORIES(src include deps/http-parser)
32 IF (WIN32 AND NOT MINGW)
33 ADD_DEFINITIONS(-DGIT_WINHTTP)
36 FILE(GLOB SRC_HTTP deps/http-parser/*.c)
39 # Specify sha1 implementation
40 IF (SHA1_TYPE STREQUAL "ppc")
41 ADD_DEFINITIONS(-DPPC_SHA1)
42 FILE(GLOB SRC_SHA1 src/hash/hash_ppc.c src/hash/hash_ppc_core.S)
43 ELSEIF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
44 ADD_DEFINITIONS(-DWIN32_SHA1)
45 FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
46 ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
47 ADD_DEFINITIONS(-DOPENSSL_SHA1)
49 FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
54 IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
55 INCLUDE_DIRECTORIES(deps/regex)
56 SET(SRC_REGEX deps/regex/regex.c)
59 # Windows doesn't understand POSIX regex on its own
60 INCLUDE_DIRECTORIES(deps/regex)
61 SET(SRC_REGEX deps/regex/regex.c)
65 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
66 LINK_LIBRARIES(${ZLIB_LIBRARIES})
68 INCLUDE_DIRECTORIES(deps/zlib)
69 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
70 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
74 SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
75 SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.")
76 SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.")
79 OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
80 OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
81 OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
82 OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
83 OPTION (TAGS "Generate tags" OFF)
84 OPTION (PROFILE "Generate profiling information" OFF)
86 # Platform specific compilation flags
88 # Default to stdcall, as that's what the CLR expects and how the Windows API is built
89 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
91 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
93 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
95 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
97 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
98 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
99 SET(WIN_RC "src/win32/git2.rc")
101 # Precompiled headers
104 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
105 IF (MINGW) # MinGW always does PIC and complains if we tell it to
106 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
108 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
110 IF (APPLE) # Apple deprecated OpenSSL
111 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
114 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
115 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
119 IF( NOT CMAKE_CONFIGURATION_TYPES )
120 # Build Debug by default
121 IF (NOT CMAKE_BUILD_TYPE)
122 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
125 # Using a multi-configuration generator eg MSVC or Xcode
126 # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
130 ADD_DEFINITIONS(-DGIT_SSL)
131 INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
132 SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
137 find_package(Threads REQUIRED)
140 ADD_DEFINITIONS(-DGIT_THREADS)
143 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
145 # Collect sourcefiles
146 FILE(GLOB SRC_H include/git2/*.h)
148 # On Windows use specific platform sources
149 IF (WIN32 AND NOT CYGWIN)
150 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501)
151 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c)
152 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
153 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c)
155 ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
156 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c)
158 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
161 # Compile and link libgit2
162 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
165 TARGET_LINK_LIBRARIES(git2 ws2_32)
166 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
167 TARGET_LINK_LIBRARIES(git2 socket nsl)
170 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
171 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
172 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
173 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
176 # Precompiled headers
177 SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
178 SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
183 RUNTIME DESTINATION ${BIN_INSTALL_DIR}
184 LIBRARY DESTINATION ${LIB_INSTALL_DIR}
185 ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
187 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
188 INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
189 INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
194 FIND_PACKAGE(PythonInterp REQUIRED)
196 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
197 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
198 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
199 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
200 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
202 INCLUDE_DIRECTORIES(${CLAR_PATH})
203 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
206 OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
207 COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
208 DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
209 WORKING_DIRECTORY ${CLAR_PATH}
211 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
212 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
215 # Precompiled headers
216 SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
220 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
221 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
222 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
226 ADD_TEST(libgit2_clar libgit2_clar -iall)
230 FIND_PROGRAM(CTAGS ctags)
232 message(FATAL_ERROR "Could not find ctags command")
235 FILE(GLOB_RECURSE SRC_ALL *.[ch])
239 COMMAND ${CTAGS} -a ${SRC_ALL}
249 FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
250 ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
251 TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
253 ADD_EXECUTABLE(git-diff examples/diff.c)
254 TARGET_LINK_LIBRARIES(git-diff git2)
256 ADD_EXECUTABLE(git-general examples/general.c)
257 TARGET_LINK_LIBRARIES(git-general git2)
259 ADD_EXECUTABLE(git-showindex examples/showindex.c)
260 TARGET_LINK_LIBRARIES(git-showindex git2)