]>
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 | ||
96fab093 | 17 | FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") |
9d1dcca2 VM |
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 | ||
96ef3d84 CY |
24 | IF (AMIGA) |
25 | # Default AmigaOS to use the PowerPC SHA1 | |
26 | SET(SHA1_TYPE "ppc") | |
27 | ENDIF() | |
c3f7a938 | 28 | |
7cbdaf7f | 29 | # Find required dependencies |
b8a8191f | 30 | INCLUDE_DIRECTORIES(src include deps/http-parser) |
20e83aa4 | 31 | |
3ce22c74 CMN |
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() | |
20e83aa4 | 38 | |
c3f7a938 CY |
39 | # Specify sha1 implementation |
40 | IF (SHA1_TYPE STREQUAL "ppc") | |
96ef3d84 CY |
41 | ADD_DEFINITIONS(-DPPC_SHA1) |
42 | FILE(GLOB SRC_SHA1 src/ppc/*.c src/ppc/*.S) | |
7205a4d9 CMN |
43 | ELSEIF (OPENSSL_FOUND) # libcrypto's implementation is faster than ours |
44 | ADD_DEFINITIONS(-DOPENSSL_SHA) | |
c3f7a938 | 45 | ELSE () |
7205a4d9 | 46 | FILE(GLOB SRC_SHA1 src/sha1/*.c) |
c3f7a938 CY |
47 | ENDIF() |
48 | ||
1f4f4d17 TH |
49 | IF (NOT WIN32) |
50 | FIND_PACKAGE(ZLIB) | |
96ef3d84 | 51 | IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS") |
41cbbea8 CY |
52 | INCLUDE_DIRECTORIES(deps/regex) |
53 | SET(SRC_REGEX deps/regex/regex.c) | |
54 | ENDIF() | |
c17b1d00 CMN |
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) | |
1f4f4d17 | 59 | ENDIF() |
f443a879 | 60 | |
1f4f4d17 TH |
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) | |
c9f79972 | 68 | ENDIF() |
f443a879 | 69 | |
5b8bb8e7 | 70 | # Installation paths |
9795a40f | 71 | SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") |
b3237ac3 | 72 | SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") |
9795a40f | 73 | SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") |
5b8bb8e7 PD |
74 | |
75 | # Build options | |
abd6d52c | 76 | OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON) |
70236bab | 77 | OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF) |
abd6d52c | 78 | OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON) |
62986ff6 | 79 | OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF) |
e632f687 | 80 | OPTION (TAGS "Generate tags" OFF) |
706a9974 | 81 | OPTION (PROFILE "Generate profiling information" OFF) |
5b8bb8e7 | 82 | |
502dd2da | 83 | # Platform specific compilation flags |
dcd62cb2 | 84 | IF (MSVC) |
3ce22c74 | 85 | # Default to stdcall, as that's what the CLR expects and how the Windows API is built |
dcd62cb2 | 86 | OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON) |
dcd62cb2 | 87 | |
94155e2f ET |
88 | STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
89 | ||
06c081e1 | 90 | SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}") |
1b5078f6 CMN |
91 | IF (STDCALL) |
92 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") | |
93 | ENDIF () | |
9f75a9ce | 94 | SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu") |
90412507 | 95 | SET(CMAKE_C_FLAGS_RELEASE "/MT /O2") |
502dd2da | 96 | SET(WIN_RC "src/win32/git2.rc") |
9f75a9ce BS |
97 | |
98 | # Precompiled headers | |
94155e2f | 99 | |
e233fa6f | 100 | ELSE () |
54c56d3e | 101 | SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}") |
2df7c944 | 102 | SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") |
2eb18449 SG |
103 | IF (MINGW) # MinGW always does PIC and complains if we tell it to |
104 | STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}") | |
105 | ELSE () | |
106 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC") | |
39cdf272 | 107 | ENDIF () |
706a9974 RB |
108 | IF (PROFILE) |
109 | SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") | |
110 | SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}") | |
111 | ENDIF () | |
90412507 | 112 | ENDIF() |
5b8bb8e7 | 113 | |
3d96996d | 114 | # Build Debug by default |
583cf169 | 115 | IF (NOT CMAKE_BUILD_TYPE) |
1a7b52dc | 116 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) |
583cf169 PD |
117 | ENDIF () |
118 | ||
96ef3d84 CY |
119 | IF (OPENSSL_FOUND) |
120 | ADD_DEFINITIONS(-DGIT_SSL) | |
121 | INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) | |
122 | SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES}) | |
41cbbea8 | 123 | ENDIF() |
66024c7c | 124 | |
70236bab VM |
125 | IF (THREADSAFE) |
126 | IF (NOT WIN32) | |
127 | find_package(Threads REQUIRED) | |
128 | ENDIF() | |
129 | ||
130 | ADD_DEFINITIONS(-DGIT_THREADS) | |
131 | ENDIF() | |
132 | ||
678e9e04 VM |
133 | ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) |
134 | ||
583cf169 | 135 | # Collect sourcefiles |
71d33382 | 136 | FILE(GLOB SRC_H include/git2/*.h) |
583cf169 PD |
137 | |
138 | # On Windows use specific platform sources | |
5b8bb8e7 | 139 | IF (WIN32 AND NOT CYGWIN) |
901fbdad | 140 | ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) |
ec42eafd | 141 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c) |
96ef3d84 | 142 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
ec42eafd | 143 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c) |
96ef3d84 CY |
144 | ELSEIF (AMIGA) |
145 | ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) | |
146 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c) | |
678e9e04 | 147 | ELSE() |
3a5ad90a | 148 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) |
5b8bb8e7 PD |
149 | ENDIF () |
150 | ||
583cf169 | 151 | # Compile and link libgit2 |
c3f7a938 | 152 | ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) |
39cdf272 CMN |
153 | |
154 | IF (WIN32) | |
155 | TARGET_LINK_LIBRARIES(git2 ws2_32) | |
e944cfa9 CMN |
156 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
157 | TARGET_LINK_LIBRARIES(git2 socket nsl) | |
7cbdaf7f | 158 | ENDIF () |
583cf169 | 159 | |
a6f24a5b | 160 | TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) |
9d1dcca2 VM |
161 | SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) |
162 | SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) | |
4fd486e0 | 163 | CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) |
583cf169 | 164 | |
73aaf674 BS |
165 | IF (MSVC) |
166 | # Precompiled headers | |
167 | SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
168 | SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") | |
169 | ENDIF () | |
170 | ||
583cf169 | 171 | # Install |
932d1baf | 172 | INSTALL(TARGETS git2 |
9795a40f | 173 | RUNTIME DESTINATION ${BIN_INSTALL_DIR} |
b3237ac3 ICQ |
174 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} |
175 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} | |
583cf169 | 176 | ) |
b3237ac3 | 177 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig ) |
9795a40f VP |
178 | INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} ) |
179 | INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} ) | |
583cf169 PD |
180 | |
181 | # Tests | |
3fd1520c | 182 | IF (BUILD_CLAR) |
e0799b6c | 183 | |
86a459a8 CB |
184 | FIND_PACKAGE(PythonInterp REQUIRED) |
185 | ||
fd29cd13 | 186 | SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/") |
3fd1520c | 187 | SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar") |
e0799b6c | 188 | SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.") |
3fd1520c | 189 | ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") |
e0799b6c | 190 | ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\") |
11385c3c | 191 | |
3fd1520c VM |
192 | INCLUDE_DIRECTORIES(${CLAR_PATH}) |
193 | FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c) | |
f1558d9b | 194 | |
86a459a8 | 195 | ADD_CUSTOM_COMMAND( |
3fd1520c VM |
196 | OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h |
197 | COMMAND ${PYTHON_EXECUTABLE} clar -vtap . | |
198 | DEPENDS ${CLAR_PATH}/clar ${SRC_TEST} | |
199 | WORKING_DIRECTORY ${CLAR_PATH} | |
86a459a8 | 200 | ) |
7205a4d9 | 201 | ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) |
a6f24a5b | 202 | TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) |
73aaf674 BS |
203 | |
204 | IF (MSVC) | |
205 | # Precompiled headers | |
206 | SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
207 | ENDIF () | |
208 | ||
f1558d9b | 209 | IF (WIN32) |
3fd1520c | 210 | TARGET_LINK_LIBRARIES(libgit2_clar ws2_32) |
f1558d9b | 211 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
3fd1520c | 212 | TARGET_LINK_LIBRARIES(libgit2_clar socket nsl) |
f1558d9b VM |
213 | ENDIF () |
214 | ||
215 | ENABLE_TESTING() | |
a75770fe | 216 | ADD_TEST(libgit2_clar libgit2_clar -iall) |
f1558d9b | 217 | ENDIF () |
e632f687 CB |
218 | |
219 | IF (TAGS) | |
220 | FIND_PROGRAM(CTAGS ctags) | |
221 | IF (NOT CTAGS) | |
222 | message(FATAL_ERROR "Could not find ctags command") | |
223 | ENDIF () | |
224 | ||
225 | FILE(GLOB_RECURSE SRC_ALL *.[ch]) | |
226 | ||
227 | ADD_CUSTOM_COMMAND( | |
228 | OUTPUT tags | |
229 | COMMAND ${CTAGS} -a ${SRC_ALL} | |
230 | DEPENDS ${SRC_ALL} | |
231 | ) | |
232 | ADD_CUSTOM_TARGET( | |
233 | do_tags ALL | |
234 | DEPENDS tags | |
235 | ) | |
236 | ENDIF () | |
62986ff6 SG |
237 | |
238 | IF (BUILD_EXAMPLES) | |
239 | FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c) | |
240 | ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC}) | |
241 | TARGET_LINK_LIBRARIES(cgit2 git2 pthread) | |
73c46d53 | 242 | |
62986ff6 SG |
243 | ADD_EXECUTABLE(git-diff examples/diff.c) |
244 | TARGET_LINK_LIBRARIES(git-diff git2) | |
b02c371e | 245 | |
62986ff6 SG |
246 | ADD_EXECUTABLE(git-general examples/general.c) |
247 | TARGET_LINK_LIBRARIES(git-general git2) | |
73c46d53 | 248 | |
62986ff6 SG |
249 | ADD_EXECUTABLE(git-showindex examples/showindex.c) |
250 | TARGET_LINK_LIBRARIES(git-showindex git2) | |
583cf169 | 251 | ENDIF () |