]> git.proxmox.com Git - libgit2.git/blob - CMakeLists.txt
fdced6148cab927f49fcbc391fd4caf6be9d743c
[libgit2.git] / CMakeLists.txt
1 # CMake build script for the libgit2 project
2 #
3 # Building (out of source build):
4 # > mkdir build && cd build
5 # > cmake .. [-DSETTINGS=VALUE]
6 # > cmake --build .
7 #
8 # Testing:
9 # > ctest -V
10 #
11 # Install:
12 # > cmake --build . --target install
13
14 PROJECT(libgit2 C)
15 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
16
17 FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
18
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}")
23
24 IF (AMIGA)
25 # Default AmigaOS to use the PowerPC SHA1
26 SET(SHA1_TYPE "ppc")
27 ENDIF()
28
29 # Find required dependencies
30 INCLUDE_DIRECTORIES(src include deps/http-parser)
31
32 IF (WIN32 AND NOT MINGW)
33 ADD_DEFINITIONS(-DGIT_WINHTTP)
34 ELSE ()
35 FIND_PACKAGE(OpenSSL)
36 FILE(GLOB SRC_HTTP deps/http-parser/*.c)
37 ENDIF()
38
39 # Specify sha1 implementation
40 IF (SHA1_TYPE STREQUAL "ppc")
41 ADD_DEFINITIONS(-DPPC_SHA1)
42 FILE(GLOB SRC_SHA1 src/ppc/*.c src/ppc/*.S)
43 ELSEIF (OPENSSL_FOUND) # libcrypto's implementation is faster than ours
44 ADD_DEFINITIONS(-DOPENSSL_SHA)
45 ELSE ()
46 FILE(GLOB SRC_SHA1 src/sha1/*.c)
47 ENDIF()
48
49 IF (NOT WIN32)
50 FIND_PACKAGE(ZLIB)
51 IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
52 INCLUDE_DIRECTORIES(deps/regex)
53 SET(SRC_REGEX deps/regex/regex.c)
54 ENDIF()
55 ELSE()
56 # Windows doesn't understand POSIX regex on its own
57 INCLUDE_DIRECTORIES(deps/regex)
58 SET(SRC_REGEX deps/regex/regex.c)
59 ENDIF()
60
61 IF (ZLIB_FOUND)
62 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
63 LINK_LIBRARIES(${ZLIB_LIBRARIES})
64 ELSE (ZLIB_FOUND)
65 INCLUDE_DIRECTORIES(deps/zlib)
66 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
67 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
68 ENDIF()
69
70 # Installation paths
71 SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
72 SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.")
73 SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.")
74
75 # Build options
76 OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
77 OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
78 OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
79 OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
80 OPTION (TAGS "Generate tags" OFF)
81 OPTION (PROFILE "Generate profiling information" OFF)
82
83 # Platform specific compilation flags
84 IF (MSVC)
85 # Default to stdcall, as that's what the CLR expects and how the Windows API is built
86 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
87
88 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
89
90 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
91 IF (STDCALL)
92 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
93 ENDIF ()
94 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
95 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
96 SET(WIN_RC "src/win32/git2.rc")
97
98 # Precompiled headers
99
100 ELSE ()
101 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
102 IF (MINGW) # MinGW always does PIC and complains if we tell it to
103 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
104 ELSE ()
105 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
106 ENDIF ()
107 IF (PROFILE)
108 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
109 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
110 ENDIF ()
111 ENDIF()
112
113 # Build Debug by default
114 IF (NOT CMAKE_BUILD_TYPE)
115 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
116 ENDIF ()
117
118 IF (OPENSSL_FOUND)
119 ADD_DEFINITIONS(-DGIT_SSL)
120 INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
121 SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
122 ENDIF()
123
124 IF (THREADSAFE)
125 IF (NOT WIN32)
126 find_package(Threads REQUIRED)
127 ENDIF()
128
129 ADD_DEFINITIONS(-DGIT_THREADS)
130 ENDIF()
131
132 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
133
134 # Collect sourcefiles
135 FILE(GLOB SRC_H include/git2/*.h)
136
137 # On Windows use specific platform sources
138 IF (WIN32 AND NOT CYGWIN)
139 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501)
140 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c)
141 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
142 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c)
143 ELSEIF (AMIGA)
144 ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
145 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c)
146 ELSE()
147 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
148 ENDIF ()
149
150 # Compile and link libgit2
151 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
152
153 IF (WIN32)
154 TARGET_LINK_LIBRARIES(git2 ws2_32)
155 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
156 TARGET_LINK_LIBRARIES(git2 socket nsl)
157 ENDIF ()
158
159 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
160 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
161 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
162 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
163
164 IF (MSVC)
165 # Precompiled headers
166 SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
167 SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
168 ENDIF ()
169
170 # Install
171 INSTALL(TARGETS git2
172 RUNTIME DESTINATION ${BIN_INSTALL_DIR}
173 LIBRARY DESTINATION ${LIB_INSTALL_DIR}
174 ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
175 )
176 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
177 INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
178 INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
179
180 # Tests
181 IF (BUILD_CLAR)
182
183 FIND_PACKAGE(PythonInterp REQUIRED)
184
185 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
186 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
187 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
188 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
189 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
190
191 INCLUDE_DIRECTORIES(${CLAR_PATH})
192 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
193
194 ADD_CUSTOM_COMMAND(
195 OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
196 COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
197 DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
198 WORKING_DIRECTORY ${CLAR_PATH}
199 )
200 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
201 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
202
203 IF (MSVC)
204 # Precompiled headers
205 SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
206 ENDIF ()
207
208 IF (WIN32)
209 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
210 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
211 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
212 ENDIF ()
213
214 ENABLE_TESTING()
215 ADD_TEST(libgit2_clar libgit2_clar -iall)
216 ENDIF ()
217
218 IF (TAGS)
219 FIND_PROGRAM(CTAGS ctags)
220 IF (NOT CTAGS)
221 message(FATAL_ERROR "Could not find ctags command")
222 ENDIF ()
223
224 FILE(GLOB_RECURSE SRC_ALL *.[ch])
225
226 ADD_CUSTOM_COMMAND(
227 OUTPUT tags
228 COMMAND ${CTAGS} -a ${SRC_ALL}
229 DEPENDS ${SRC_ALL}
230 )
231 ADD_CUSTOM_TARGET(
232 do_tags ALL
233 DEPENDS tags
234 )
235 ENDIF ()
236
237 IF (BUILD_EXAMPLES)
238 FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
239 ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
240 TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
241
242 ADD_EXECUTABLE(git-diff examples/diff.c)
243 TARGET_LINK_LIBRARIES(git-diff git2)
244
245 ADD_EXECUTABLE(git-general examples/general.c)
246 TARGET_LINK_LIBRARIES(git-general git2)
247
248 ADD_EXECUTABLE(git-showindex examples/showindex.c)
249 TARGET_LINK_LIBRARIES(git-showindex git2)
250 ENDIF ()