]>
Commit | Line | Data |
---|---|---|
583cf169 | 1 | # CMake build script for the libgit2 project |
73c46d53 | 2 | # |
bfe0658e | 3 | # Building (out of source build): |
73c46d53 | 4 | # > mkdir build && cd build |
bfe0658e PD |
5 | # > cmake .. [-DSETTINGS=VALUE] |
6 | # > cmake --build . | |
932d1baf | 7 | # |
73c46d53 PD |
8 | # Testing: |
9 | # > ctest -V | |
10 | # | |
11 | # Install: | |
bfe0658e | 12 | # > cmake --build . --target install |
73c46d53 | 13 | |
583cf169 PD |
14 | PROJECT(libgit2 C) |
15 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) | |
16 | ||
19a766a2 SC |
17 | # Build options |
18 | # | |
19 | OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON ) | |
20 | OPTION( THREADSAFE "Build libgit2 as threadsafe" OFF ) | |
21 | OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON ) | |
22 | OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF ) | |
23 | OPTION( TAGS "Generate tags" OFF ) | |
24 | OPTION( PROFILE "Generate profiling information" OFF ) | |
25 | IF(MSVC) | |
26 | # This option is only availalbe when building with MSVC. By default, libgit2 is | |
27 | # build using the stdcall calling convention, as that's what the CLR expects by | |
28 | # default and how the Windows API is built. | |
29 | # | |
30 | # If you are writing a C or C++ program and want to link to libgit2, you have to | |
31 | # either: | |
32 | # - Add /Gz to the compiler options of _your_ program / library. | |
33 | # - Turn this option off by invoking CMake with the "-DSTDCALL=Off" argument. | |
34 | # | |
35 | OPTION( STDCALL "Build libgit2 with the __stdcall convention" ON ) | |
36 | ENDIF() | |
37 | ||
38 | # Installation paths | |
39 | # | |
40 | SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") | |
41 | SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") | |
42 | SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") | |
43 | ||
94243295 SC |
44 | FUNCTION(TARGET_OS_LIBRARIES target) |
45 | IF(WIN32) | |
46 | TARGET_LINK_LIBRARIES(${target} ws2_32) | |
47 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") | |
48 | TARGET_LINK_LIBRARIES(${target} socket nsl) | |
49 | ENDIF () | |
50 | IF(THREADSAFE) | |
51 | TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT}) | |
52 | ENDIF() | |
53 | ENDFUNCTION() | |
54 | ||
523a3ae5 SC |
55 | # For the MSVC IDE, this function splits up the source files like windows explorer does. |
56 | # This is esp. useful with the libgit2_clar project, were usually 2 or more files share | |
57 | # the same name. | |
58 | # Sadly, this file grouping is a per-directory option in cmake and not per-target, resulting | |
59 | # in empty virtual folders "tests-clar" for the git2.dll | |
60 | FUNCTION(MSVC_SPLIT_SOURCES target) | |
61 | IF(MSVC_IDE) | |
62 | GET_TARGET_PROPERTY(sources ${target} SOURCES) | |
63 | FOREACH(source ${sources}) | |
64 | IF(source MATCHES ".*/") | |
65 | STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" rel ${source}) | |
66 | IF(rel) | |
67 | STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel}) | |
68 | IF(rel) | |
69 | STRING(REPLACE "/" "\\\\" rel ${rel}) | |
70 | SOURCE_GROUP(${rel} FILES ${source}) | |
71 | ENDIF() | |
72 | ENDIF() | |
73 | ENDIF() | |
74 | ENDFOREACH() | |
75 | ENDIF() | |
76 | ENDFUNCTION() | |
77 | ||
96fab093 | 78 | FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") |
9d1dcca2 VM |
79 | |
80 | STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}") | |
81 | STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}") | |
82 | STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}") | |
83 | SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}") | |
84 | ||
96ef3d84 CY |
85 | IF (AMIGA) |
86 | # Default AmigaOS to use the PowerPC SHA1 | |
87 | SET(SHA1_TYPE "ppc") | |
88 | ENDIF() | |
c3f7a938 | 89 | |
7cbdaf7f | 90 | # Find required dependencies |
8d457891 | 91 | INCLUDE_DIRECTORIES(src include) |
20e83aa4 | 92 | |
3ce22c74 CMN |
93 | IF (WIN32 AND NOT MINGW) |
94 | ADD_DEFINITIONS(-DGIT_WINHTTP) | |
95 | ELSE () | |
96 | FIND_PACKAGE(OpenSSL) | |
97 | FILE(GLOB SRC_HTTP deps/http-parser/*.c) | |
8d457891 | 98 | INCLUDE_DIRECTORIES(deps/http-parser) |
3ce22c74 | 99 | ENDIF() |
20e83aa4 | 100 | |
c3f7a938 CY |
101 | # Specify sha1 implementation |
102 | IF (SHA1_TYPE STREQUAL "ppc") | |
96ef3d84 | 103 | ADD_DEFINITIONS(-DPPC_SHA1) |
d6fb0924 ET |
104 | FILE(GLOB SRC_SHA1 src/hash/hash_ppc.c src/hash/hash_ppc_core.S) |
105 | ELSEIF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin") | |
106 | ADD_DEFINITIONS(-DWIN32_SHA1) | |
107 | FILE(GLOB SRC_SHA1 src/hash/hash_win32.c) | |
108 | ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin") | |
109 | ADD_DEFINITIONS(-DOPENSSL_SHA1) | |
110 | ELSE() | |
111 | FILE(GLOB SRC_SHA1 src/hash/hash_generic.c) | |
c3f7a938 CY |
112 | ENDIF() |
113 | ||
3d007f4f SC |
114 | # Include POSIX regex when it is required |
115 | IF(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "AmigaOS") | |
c17b1d00 CMN |
116 | INCLUDE_DIRECTORIES(deps/regex) |
117 | SET(SRC_REGEX deps/regex/regex.c) | |
1f4f4d17 | 118 | ENDIF() |
f443a879 | 119 | |
b53671ae SC |
120 | # Optional external dependency: zlib |
121 | IF(NOT ZLIB_LIBRARY) | |
122 | # It's optional, but FIND_PACKAGE gives a warning that looks more like an error. | |
123 | FIND_PACKAGE(ZLIB QUIET) | |
124 | ENDIF() | |
1f4f4d17 TH |
125 | IF (ZLIB_FOUND) |
126 | INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) | |
127 | LINK_LIBRARIES(${ZLIB_LIBRARIES}) | |
b53671ae SC |
128 | ELSE() |
129 | MESSAGE( "zlib was not found; using bundled 3rd-party sources." ) | |
1f4f4d17 TH |
130 | INCLUDE_DIRECTORIES(deps/zlib) |
131 | ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP) | |
132 | FILE(GLOB SRC_ZLIB deps/zlib/*.c) | |
c9f79972 | 133 | ENDIF() |
f443a879 | 134 | |
502dd2da | 135 | # Platform specific compilation flags |
dcd62cb2 | 136 | IF (MSVC) |
dcd62cb2 | 137 | |
94155e2f ET |
138 | STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
139 | ||
4a03913c | 140 | SET(CMAKE_C_FLAGS "/MP /nologo /Zi ${CMAKE_C_FLAGS}") |
1b5078f6 CMN |
141 | IF (STDCALL) |
142 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") | |
143 | ENDIF () | |
9f75a9ce | 144 | SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu") |
90412507 | 145 | SET(CMAKE_C_FLAGS_RELEASE "/MT /O2") |
502dd2da | 146 | SET(WIN_RC "src/win32/git2.rc") |
9f75a9ce BS |
147 | |
148 | # Precompiled headers | |
94155e2f | 149 | |
e233fa6f | 150 | ELSE () |
4236164a | 151 | SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes ${CMAKE_C_FLAGS}") |
2eb18449 SG |
152 | IF (MINGW) # MinGW always does PIC and complains if we tell it to |
153 | STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}") | |
154 | ELSE () | |
155 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC") | |
39cdf272 | 156 | ENDIF () |
dbd6850d RB |
157 | IF (APPLE) # Apple deprecated OpenSSL |
158 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations") | |
159 | ENDIF () | |
706a9974 RB |
160 | IF (PROFILE) |
161 | SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") | |
162 | SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}") | |
163 | ENDIF () | |
90412507 | 164 | ENDIF() |
5b8bb8e7 | 165 | |
88149fae PT |
166 | IF( NOT CMAKE_CONFIGURATION_TYPES ) |
167 | # Build Debug by default | |
168 | IF (NOT CMAKE_BUILD_TYPE) | |
169 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) | |
170 | ENDIF () | |
171 | ELSE() | |
172 | # Using a multi-configuration generator eg MSVC or Xcode | |
173 | # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE | |
174 | ENDIF() | |
583cf169 | 175 | |
96ef3d84 CY |
176 | IF (OPENSSL_FOUND) |
177 | ADD_DEFINITIONS(-DGIT_SSL) | |
178 | INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) | |
179 | SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES}) | |
41cbbea8 | 180 | ENDIF() |
66024c7c | 181 | |
70236bab VM |
182 | IF (THREADSAFE) |
183 | IF (NOT WIN32) | |
184 | find_package(Threads REQUIRED) | |
185 | ENDIF() | |
186 | ||
187 | ADD_DEFINITIONS(-DGIT_THREADS) | |
188 | ENDIF() | |
189 | ||
678e9e04 VM |
190 | ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) |
191 | ||
583cf169 | 192 | # Collect sourcefiles |
71d33382 | 193 | FILE(GLOB SRC_H include/git2/*.h) |
583cf169 PD |
194 | |
195 | # On Windows use specific platform sources | |
5b8bb8e7 | 196 | IF (WIN32 AND NOT CYGWIN) |
901fbdad | 197 | ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) |
521479b1 | 198 | FILE(GLOB SRC_OS src/win32/*.c) |
96ef3d84 CY |
199 | ELSEIF (AMIGA) |
200 | ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) | |
521479b1 | 201 | FILE(GLOB SRC_OS src/amiga/*.c) |
678e9e04 | 202 | ELSE() |
521479b1 SC |
203 | FILE(GLOB SRC_OS src/unix/*.c) |
204 | ENDIF() | |
205 | FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c) | |
5b8bb8e7 | 206 | |
583cf169 | 207 | # Compile and link libgit2 |
521479b1 | 208 | ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) |
94243295 SC |
209 | TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES}) |
210 | TARGET_OS_LIBRARIES(git2) | |
39cdf272 | 211 | |
523a3ae5 SC |
212 | MSVC_SPLIT_SOURCES(git2) |
213 | ||
9d1dcca2 VM |
214 | SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) |
215 | SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) | |
4fd486e0 | 216 | CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) |
583cf169 | 217 | |
26d9e317 | 218 | IF (MSVC_IDE) |
73aaf674 BS |
219 | # Precompiled headers |
220 | SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
221 | SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") | |
222 | ENDIF () | |
223 | ||
583cf169 | 224 | # Install |
932d1baf | 225 | INSTALL(TARGETS git2 |
9795a40f | 226 | RUNTIME DESTINATION ${BIN_INSTALL_DIR} |
b3237ac3 ICQ |
227 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} |
228 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} | |
583cf169 | 229 | ) |
b3237ac3 | 230 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig ) |
9795a40f VP |
231 | INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} ) |
232 | INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} ) | |
583cf169 PD |
233 | |
234 | # Tests | |
3fd1520c | 235 | IF (BUILD_CLAR) |
86a459a8 CB |
236 | FIND_PACKAGE(PythonInterp REQUIRED) |
237 | ||
fd29cd13 | 238 | SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/") |
3fd1520c | 239 | SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar") |
e0799b6c | 240 | SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.") |
3fd1520c | 241 | ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") |
e0799b6c | 242 | ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\") |
11385c3c | 243 | |
3fd1520c | 244 | INCLUDE_DIRECTORIES(${CLAR_PATH}) |
5c2d3f6d | 245 | FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c) |
2e6f06a8 | 246 | SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar.c") |
f1558d9b | 247 | |
86a459a8 | 248 | ADD_CUSTOM_COMMAND( |
156cfec0 | 249 | OUTPUT ${CLAR_PATH}/clar.suite |
2e6f06a8 | 250 | COMMAND ${PYTHON_EXECUTABLE} generate.py -xonline . |
39444bea | 251 | DEPENDS ${SRC_TEST} |
3fd1520c | 252 | WORKING_DIRECTORY ${CLAR_PATH} |
86a459a8 | 253 | ) |
156cfec0 | 254 | |
5c2d3f6d | 255 | SET_SOURCE_FILES_PROPERTIES( |
2e6f06a8 | 256 | ${CLAR_PATH}/clar.c |
5c2d3f6d VM |
257 | PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite) |
258 | ||
259 | ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) | |
260 | ||
94243295 SC |
261 | TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES}) |
262 | TARGET_OS_LIBRARIES(libgit2_clar) | |
523a3ae5 | 263 | MSVC_SPLIT_SOURCES(libgit2_clar) |
73aaf674 | 264 | |
156cfec0 VM |
265 | IF (MSVC_IDE) |
266 | # Precompiled headers | |
267 | SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
268 | ENDIF () | |
73aaf674 | 269 | |
f1558d9b | 270 | ENABLE_TESTING() |
b0a45829 | 271 | ADD_TEST(libgit2_clar libgit2_clar) |
f1558d9b | 272 | ENDIF () |
e632f687 CB |
273 | |
274 | IF (TAGS) | |
275 | FIND_PROGRAM(CTAGS ctags) | |
276 | IF (NOT CTAGS) | |
277 | message(FATAL_ERROR "Could not find ctags command") | |
278 | ENDIF () | |
279 | ||
280 | FILE(GLOB_RECURSE SRC_ALL *.[ch]) | |
281 | ||
282 | ADD_CUSTOM_COMMAND( | |
283 | OUTPUT tags | |
284 | COMMAND ${CTAGS} -a ${SRC_ALL} | |
285 | DEPENDS ${SRC_ALL} | |
286 | ) | |
287 | ADD_CUSTOM_TARGET( | |
288 | do_tags ALL | |
289 | DEPENDS tags | |
290 | ) | |
291 | ENDIF () | |
62986ff6 SG |
292 | |
293 | IF (BUILD_EXAMPLES) | |
294 | FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c) | |
295 | ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC}) | |
296 | TARGET_LINK_LIBRARIES(cgit2 git2 pthread) | |
73c46d53 | 297 | |
62986ff6 SG |
298 | ADD_EXECUTABLE(git-diff examples/diff.c) |
299 | TARGET_LINK_LIBRARIES(git-diff git2) | |
b02c371e | 300 | |
62986ff6 SG |
301 | ADD_EXECUTABLE(git-general examples/general.c) |
302 | TARGET_LINK_LIBRARIES(git-general git2) | |
73c46d53 | 303 | |
62986ff6 SG |
304 | ADD_EXECUTABLE(git-showindex examples/showindex.c) |
305 | TARGET_LINK_LIBRARIES(git-showindex git2) | |
583cf169 | 306 | ENDIF () |