]>
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}") |
2eb18449 SG |
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") | |
39cdf272 | 106 | ENDIF () |
dbd6850d RB |
107 | IF (APPLE) # Apple deprecated OpenSSL |
108 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations") | |
109 | ENDIF () | |
706a9974 RB |
110 | IF (PROFILE) |
111 | SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") | |
112 | SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}") | |
113 | ENDIF () | |
90412507 | 114 | ENDIF() |
5b8bb8e7 | 115 | |
88149fae PT |
116 | IF( NOT CMAKE_CONFIGURATION_TYPES ) |
117 | # Build Debug by default | |
118 | IF (NOT CMAKE_BUILD_TYPE) | |
119 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) | |
120 | ENDIF () | |
121 | ELSE() | |
122 | # Using a multi-configuration generator eg MSVC or Xcode | |
123 | # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE | |
124 | ENDIF() | |
583cf169 | 125 | |
96ef3d84 CY |
126 | IF (OPENSSL_FOUND) |
127 | ADD_DEFINITIONS(-DGIT_SSL) | |
128 | INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) | |
129 | SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES}) | |
41cbbea8 | 130 | ENDIF() |
66024c7c | 131 | |
70236bab VM |
132 | IF (THREADSAFE) |
133 | IF (NOT WIN32) | |
134 | find_package(Threads REQUIRED) | |
135 | ENDIF() | |
136 | ||
137 | ADD_DEFINITIONS(-DGIT_THREADS) | |
138 | ENDIF() | |
139 | ||
678e9e04 VM |
140 | ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) |
141 | ||
583cf169 | 142 | # Collect sourcefiles |
71d33382 | 143 | FILE(GLOB SRC_H include/git2/*.h) |
583cf169 PD |
144 | |
145 | # On Windows use specific platform sources | |
5b8bb8e7 | 146 | IF (WIN32 AND NOT CYGWIN) |
901fbdad | 147 | ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) |
ec42eafd | 148 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c) |
96ef3d84 | 149 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
ec42eafd | 150 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c) |
96ef3d84 CY |
151 | ELSEIF (AMIGA) |
152 | ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) | |
153 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c) | |
678e9e04 | 154 | ELSE() |
3a5ad90a | 155 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) |
5b8bb8e7 PD |
156 | ENDIF () |
157 | ||
583cf169 | 158 | # Compile and link libgit2 |
c3f7a938 | 159 | ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) |
39cdf272 CMN |
160 | |
161 | IF (WIN32) | |
162 | TARGET_LINK_LIBRARIES(git2 ws2_32) | |
e944cfa9 CMN |
163 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
164 | TARGET_LINK_LIBRARIES(git2 socket nsl) | |
7cbdaf7f | 165 | ENDIF () |
583cf169 | 166 | |
a6f24a5b | 167 | TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) |
9d1dcca2 VM |
168 | SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) |
169 | SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) | |
4fd486e0 | 170 | CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) |
583cf169 | 171 | |
73aaf674 BS |
172 | IF (MSVC) |
173 | # Precompiled headers | |
174 | SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
175 | SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") | |
176 | ENDIF () | |
177 | ||
583cf169 | 178 | # Install |
932d1baf | 179 | INSTALL(TARGETS git2 |
9795a40f | 180 | RUNTIME DESTINATION ${BIN_INSTALL_DIR} |
b3237ac3 ICQ |
181 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} |
182 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} | |
583cf169 | 183 | ) |
b3237ac3 | 184 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig ) |
9795a40f VP |
185 | INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} ) |
186 | INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} ) | |
583cf169 PD |
187 | |
188 | # Tests | |
3fd1520c | 189 | IF (BUILD_CLAR) |
e0799b6c | 190 | |
86a459a8 CB |
191 | FIND_PACKAGE(PythonInterp REQUIRED) |
192 | ||
fd29cd13 | 193 | SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/") |
3fd1520c | 194 | SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar") |
e0799b6c | 195 | SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.") |
3fd1520c | 196 | ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") |
e0799b6c | 197 | ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\") |
11385c3c | 198 | |
3fd1520c VM |
199 | INCLUDE_DIRECTORIES(${CLAR_PATH}) |
200 | FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c) | |
f1558d9b | 201 | |
86a459a8 | 202 | ADD_CUSTOM_COMMAND( |
3fd1520c VM |
203 | OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h |
204 | COMMAND ${PYTHON_EXECUTABLE} clar -vtap . | |
205 | DEPENDS ${CLAR_PATH}/clar ${SRC_TEST} | |
206 | WORKING_DIRECTORY ${CLAR_PATH} | |
86a459a8 | 207 | ) |
7205a4d9 | 208 | ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) |
a6f24a5b | 209 | TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) |
73aaf674 BS |
210 | |
211 | IF (MSVC) | |
212 | # Precompiled headers | |
213 | SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
214 | ENDIF () | |
215 | ||
f1558d9b | 216 | IF (WIN32) |
3fd1520c | 217 | TARGET_LINK_LIBRARIES(libgit2_clar ws2_32) |
f1558d9b | 218 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
3fd1520c | 219 | TARGET_LINK_LIBRARIES(libgit2_clar socket nsl) |
f1558d9b VM |
220 | ENDIF () |
221 | ||
222 | ENABLE_TESTING() | |
a75770fe | 223 | ADD_TEST(libgit2_clar libgit2_clar -iall) |
f1558d9b | 224 | ENDIF () |
e632f687 CB |
225 | |
226 | IF (TAGS) | |
227 | FIND_PROGRAM(CTAGS ctags) | |
228 | IF (NOT CTAGS) | |
229 | message(FATAL_ERROR "Could not find ctags command") | |
230 | ENDIF () | |
231 | ||
232 | FILE(GLOB_RECURSE SRC_ALL *.[ch]) | |
233 | ||
234 | ADD_CUSTOM_COMMAND( | |
235 | OUTPUT tags | |
236 | COMMAND ${CTAGS} -a ${SRC_ALL} | |
237 | DEPENDS ${SRC_ALL} | |
238 | ) | |
239 | ADD_CUSTOM_TARGET( | |
240 | do_tags ALL | |
241 | DEPENDS tags | |
242 | ) | |
243 | ENDIF () | |
62986ff6 SG |
244 | |
245 | IF (BUILD_EXAMPLES) | |
246 | FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c) | |
247 | ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC}) | |
248 | TARGET_LINK_LIBRARIES(cgit2 git2 pthread) | |
73c46d53 | 249 | |
62986ff6 SG |
250 | ADD_EXECUTABLE(git-diff examples/diff.c) |
251 | TARGET_LINK_LIBRARIES(git-diff git2) | |
b02c371e | 252 | |
62986ff6 SG |
253 | ADD_EXECUTABLE(git-general examples/general.c) |
254 | TARGET_LINK_LIBRARIES(git-general git2) | |
73c46d53 | 255 | |
62986ff6 SG |
256 | ADD_EXECUTABLE(git-showindex examples/showindex.c) |
257 | TARGET_LINK_LIBRARIES(git-showindex git2) | |
583cf169 | 258 | ENDIF () |