]>
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) | |
8f426d7d | 16 | CMAKE_POLICY(SET CMP0015 NEW) |
583cf169 | 17 | |
7a6e0281 | 18 | # Add find modules to the path |
7f0de93a | 19 | SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/") |
7369b3c3 | 20 | |
5bda607c | 21 | INCLUDE(CheckLibraryExists) |
5c8d5eac | 22 | INCLUDE(AddCFlagIfSupported) |
6e1205ef | 23 | INCLUDE(FindPkgConfig) |
5bda607c | 24 | |
19a766a2 SC |
25 | # Build options |
26 | # | |
058956ce | 27 | OPTION( SONAME "Set the (SO)VERSION of the target" ON ) |
19a766a2 | 28 | OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON ) |
79b0ae4e | 29 | OPTION( THREADSAFE "Build libgit2 as threadsafe" ON ) |
19a766a2 SC |
30 | OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON ) |
31 | OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF ) | |
32 | OPTION( TAGS "Generate tags" OFF ) | |
33 | OPTION( PROFILE "Generate profiling information" OFF ) | |
b5ec5430 | 34 | OPTION( ENABLE_TRACE "Enables tracing support" OFF ) |
92dac975 | 35 | OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF ) |
c1cf1af4 | 36 | |
92dac975 RB |
37 | OPTION( ANDROID "Build for android NDK" OFF ) |
38 | ||
7641327c | 39 | OPTION( USE_OPENSSL "Link with and use openssl library" ON ) |
92dac975 | 40 | OPTION( USE_ICONV "Link with and use iconv library" OFF ) |
73291aff | 41 | OPTION( USE_SSH "Link with libssh to enable SSH support" ON ) |
23135afa | 42 | OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF ) |
7697e541 | 43 | OPTION( VALGRIND "Configure build for valgrind" OFF ) |
73291aff | 44 | |
726b75d1 | 45 | IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
92dac975 RB |
46 | SET( USE_ICONV ON ) |
47 | ENDIF() | |
82b2fc2c | 48 | |
19a766a2 | 49 | IF(MSVC) |
275d8d55 | 50 | # This option is only available when building with MSVC. By default, libgit2 |
e49dc687 BS |
51 | # is build using the cdecl calling convention, which is useful if you're |
52 | # writing C. However, the CLR and Win32 API both expect stdcall. | |
19a766a2 | 53 | # |
e49dc687 BS |
54 | # If you are writing a CLR program and want to link to libgit2, you'll want |
55 | # to turn this on by invoking CMake with the "-DSTDCALL=ON" argument. | |
56 | OPTION( STDCALL "Build libgit2 with the __stdcall convention" OFF ) | |
08f32085 Q |
57 | |
58 | # This option must match the settings used in your program, in particular if you | |
59 | # are linking statically | |
60 | OPTION( STATIC_CRT "Link the static CRT libraries" ON ) | |
1bfe7133 | 61 | |
3753110a CMN |
62 | # If you want to embed a copy of libssh2 into libgit2, pass a |
63 | # path to libssh2 | |
64 | OPTION( EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF ) | |
65 | ||
6874cafd | 66 | ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS) |
300f4412 M |
67 | ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE) |
68 | ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE) | |
19a766a2 SC |
69 | ENDIF() |
70 | ||
8f426d7d PK |
71 | IF(WIN32) |
72 | # By default, libgit2 is built with WinHTTP. To use the built-in | |
73 | # HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument. | |
74 | OPTION( WINHTTP "Use Win32 WinHTTP routines" ON ) | |
75 | ENDIF() | |
76 | ||
d06c589f JH |
77 | IF(MSVC) |
78 | # Enable MSVC CRTDBG memory leak reporting when in debug mode. | |
79 | OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF) | |
80 | ENDIF() | |
81 | ||
1ecda61e CMN |
82 | # This variable will contain the libraries we need to put into |
83 | # libgit2.pc's Requires.private. That is, what we're linking to or | |
84 | # what someone who's statically linking us needs to link to. | |
85 | SET(LIBGIT2_PC_REQUIRES "") | |
86 | # This will be set later if we use the system's http-parser library or | |
87 | # use iconv (OSX) and will be written to the Libs.private field in the | |
88 | # pc file. | |
89 | SET(LIBGIT2_PC_LIBS "") | |
90 | ||
19a766a2 SC |
91 | # Installation paths |
92 | # | |
93 | SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.") | |
94 | SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") | |
95 | SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.") | |
96 | ||
94243295 SC |
97 | FUNCTION(TARGET_OS_LIBRARIES target) |
98 | IF(WIN32) | |
99 | TARGET_LINK_LIBRARIES(${target} ws2_32) | |
100 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") | |
101 | TARGET_LINK_LIBRARIES(${target} socket nsl) | |
474c8cf8 | 102 | SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lsocket -lnsl" PARENT_SCOPE) |
5bda607c TM |
103 | ENDIF() |
104 | CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT) | |
105 | IF(NEED_LIBRT) | |
b176eded | 106 | TARGET_LINK_LIBRARIES(${target} rt) |
474c8cf8 | 107 | SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lrt" PARENT_SCOPE) |
92dac975 RB |
108 | ENDIF() |
109 | ||
94243295 SC |
110 | IF(THREADSAFE) |
111 | TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT}) | |
112 | ENDIF() | |
113 | ENDFUNCTION() | |
114 | ||
d335e73a ET |
115 | # For the MSVC IDE, this function splits up the source files like windows |
116 | # explorer does. This is esp. useful with the libgit2_clar project, were | |
117 | # usually 2 or more files share the same name. Sadly, this file grouping | |
118 | # is a per-directory option in cmake and not per-target, resulting in | |
83e1efbf | 119 | # empty virtual folders "tests" for the git2.dll |
523a3ae5 SC |
120 | FUNCTION(MSVC_SPLIT_SOURCES target) |
121 | IF(MSVC_IDE) | |
122 | GET_TARGET_PROPERTY(sources ${target} SOURCES) | |
123 | FOREACH(source ${sources}) | |
124 | IF(source MATCHES ".*/") | |
125 | STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" rel ${source}) | |
126 | IF(rel) | |
127 | STRING(REGEX REPLACE "/([^/]*)$" "" rel ${rel}) | |
128 | IF(rel) | |
129 | STRING(REPLACE "/" "\\\\" rel ${rel}) | |
130 | SOURCE_GROUP(${rel} FILES ${source}) | |
131 | ENDIF() | |
132 | ENDIF() | |
133 | ENDIF() | |
134 | ENDFOREACH() | |
135 | ENDIF() | |
136 | ENDFUNCTION() | |
137 | ||
96fab093 | 138 | FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$") |
9d1dcca2 VM |
139 | |
140 | STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}") | |
141 | STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}") | |
142 | STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}") | |
143 | SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}") | |
144 | ||
99807672 CMN |
145 | FILE(STRINGS "include/git2/version.h" GIT2_HEADER_SOVERSION REGEX "^#define LIBGIT2_SOVERSION [0-9]+$") |
146 | STRING(REGEX REPLACE "^.*LIBGIT2_SOVERSION ([0-9]+)$" "\\1" LIBGIT2_SOVERSION "${GIT2_HEADER_SOVERSION}") | |
147 | ||
7cbdaf7f | 148 | # Find required dependencies |
8d457891 | 149 | INCLUDE_DIRECTORIES(src include) |
20e83aa4 | 150 | |
3753110a CMN |
151 | IF (WIN32 AND EMBED_SSH_PATH) |
152 | FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c") | |
153 | INCLUDE_DIRECTORIES("${EMBED_SSH_PATH}/include") | |
154 | FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"") | |
155 | ADD_DEFINITIONS(-DGIT_SSH) | |
156 | ENDIF() | |
157 | ||
8f426d7d | 158 | IF (WIN32 AND WINHTTP) |
3ce22c74 | 159 | ADD_DEFINITIONS(-DGIT_WINHTTP) |
2d1feaa2 BS |
160 | INCLUDE_DIRECTORIES(deps/http-parser) |
161 | FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h) | |
8f426d7d PK |
162 | |
163 | # Since MinGW does not come with headers or an import library for winhttp, | |
164 | # we have to include a private header and generate our own import library | |
165 | IF (MINGW) | |
166 | FIND_PROGRAM(DLLTOOL dlltool CMAKE_FIND_ROOT_PATH_BOTH) | |
167 | IF (NOT DLLTOOL) | |
168 | MESSAGE(FATAL_ERROR "Could not find dlltool command") | |
169 | ENDIF () | |
170 | ||
d612e48e TK |
171 | SET(LIBWINHTTP_PATH "${CMAKE_CURRENT_BINARY_DIR}/deps/winhttp") |
172 | FILE(MAKE_DIRECTORY ${LIBWINHTTP_PATH}) | |
8f426d7d | 173 | |
547517d7 TK |
174 | IF ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") |
175 | set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp64.def") | |
176 | ELSE() | |
177 | set(WINHTTP_DEF "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp/winhttp.def") | |
178 | ENDIF() | |
179 | ||
8f426d7d PK |
180 | ADD_CUSTOM_COMMAND( |
181 | OUTPUT ${LIBWINHTTP_PATH}/libwinhttp.a | |
547517d7 TK |
182 | COMMAND ${DLLTOOL} -d ${WINHTTP_DEF} -k -D winhttp.dll -l libwinhttp.a |
183 | DEPENDS ${WINHTTP_DEF} | |
8f426d7d PK |
184 | WORKING_DIRECTORY ${LIBWINHTTP_PATH} |
185 | ) | |
186 | ||
187 | SET_SOURCE_FILES_PROPERTIES( | |
188 | ${CMAKE_CURRENT_SOURCE_DIR}/src/transports/winhttp.c | |
189 | PROPERTIES OBJECT_DEPENDS ${LIBWINHTTP_PATH}/libwinhttp.a | |
190 | ) | |
191 | ||
192 | INCLUDE_DIRECTORIES(deps/winhttp) | |
d612e48e | 193 | LINK_DIRECTORIES(${LIBWINHTTP_PATH}) |
8f426d7d PK |
194 | ENDIF () |
195 | ||
b631e0d9 | 196 | LINK_LIBRARIES(winhttp rpcrt4 crypt32) |
3ce22c74 | 197 | ELSE () |
7641327c | 198 | IF (NOT AMIGA AND USE_OPENSSL) |
c57c4af3 SB |
199 | FIND_PACKAGE(OpenSSL) |
200 | ENDIF () | |
7a6e0281 | 201 | |
04739e9f | 202 | FIND_PACKAGE(HTTP_Parser) |
7a6e0281 VP |
203 | IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2) |
204 | INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS}) | |
205 | LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES}) | |
1ecda61e | 206 | SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lhttp_parser") |
7a6e0281 | 207 | ELSE() |
86f92b74 | 208 | MESSAGE(STATUS "http-parser was not found or is too old; using bundled 3rd-party sources.") |
7a6e0281 | 209 | INCLUDE_DIRECTORIES(deps/http-parser) |
cdacd3d9 | 210 | FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h) |
7a6e0281 | 211 | ENDIF() |
3ce22c74 | 212 | ENDIF() |
20e83aa4 | 213 | |
c3f7a938 | 214 | # Specify sha1 implementation |
8f09f464 | 215 | IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin") |
ccf1a2ba | 216 | ADD_DEFINITIONS(-DWIN32_SHA1) |
217 | FILE(GLOB SRC_SHA1 src/hash/hash_win32.c) | |
2d2aa1ff LY |
218 | ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
219 | ADD_DEFINITIONS(-DGIT_COMMON_CRYPTO) | |
d6fb0924 ET |
220 | ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin") |
221 | ADD_DEFINITIONS(-DOPENSSL_SHA1) | |
096ac799 TWL |
222 | IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") |
223 | SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lssl") | |
224 | ELSE() | |
225 | SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} openssl") | |
226 | ENDIF () | |
d6fb0924 ET |
227 | ELSE() |
228 | FILE(GLOB SRC_SHA1 src/hash/hash_generic.c) | |
c3f7a938 CY |
229 | ENDIF() |
230 | ||
b5ec5430 ET |
231 | # Enable tracing |
232 | IF (ENABLE_TRACE STREQUAL "ON") | |
233 | ADD_DEFINITIONS(-DGIT_TRACE) | |
234 | ENDIF() | |
235 | ||
3d007f4f | 236 | # Include POSIX regex when it is required |
98b8fcff | 237 | IF(WIN32 OR AMIGA OR ANDROID OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
c17b1d00 CMN |
238 | INCLUDE_DIRECTORIES(deps/regex) |
239 | SET(SRC_REGEX deps/regex/regex.c) | |
1f4f4d17 | 240 | ENDIF() |
f443a879 | 241 | |
b53671ae | 242 | # Optional external dependency: zlib |
04739e9f | 243 | FIND_PACKAGE(ZLIB) |
1f4f4d17 TH |
244 | IF (ZLIB_FOUND) |
245 | INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) | |
246 | LINK_LIBRARIES(${ZLIB_LIBRARIES}) | |
096ac799 | 247 | IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD") |
474c8cf8 CMN |
248 | SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lz") |
249 | ELSE() | |
250 | SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} zlib") | |
251 | ENDIF() | |
c41281ad | 252 | ELSE() |
86f92b74 | 253 | MESSAGE(STATUS "zlib was not found; using bundled 3rd-party sources." ) |
1f4f4d17 TH |
254 | INCLUDE_DIRECTORIES(deps/zlib) |
255 | ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP) | |
cdacd3d9 | 256 | FILE(GLOB SRC_ZLIB deps/zlib/*.c deps/zlib/*.h) |
c9f79972 | 257 | ENDIF() |
f443a879 | 258 | |
726b75d1 | 259 | # Optional external dependency: libssh2 |
f5fc63bc | 260 | IF (USE_SSH) |
4f91bfa3 | 261 | PKG_CHECK_MODULES(LIBSSH2 libssh2) |
5be622fb | 262 | ENDIF() |
7369b3c3 BM |
263 | IF (LIBSSH2_FOUND) |
264 | ADD_DEFINITIONS(-DGIT_SSH) | |
461fd5a0 RKC |
265 | INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIRS}) |
266 | LINK_DIRECTORIES(${LIBSSH2_LIBRARY_DIRS}) | |
1ecda61e | 267 | SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} libssh2") |
7369b3c3 | 268 | SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES}) |
e15a0849 YK |
269 | ELSE() |
270 | MESSAGE(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.") | |
7369b3c3 BM |
271 | ENDIF() |
272 | ||
315cb38e ET |
273 | # Optional external dependency: libgssapi |
274 | IF (USE_GSSAPI) | |
275 | FIND_PACKAGE(GSSAPI) | |
276 | ENDIF() | |
277 | IF (GSSAPI_FOUND) | |
278 | ADD_DEFINITIONS(-DGIT_GSSAPI) | |
279 | ENDIF() | |
280 | ||
726b75d1 RB |
281 | # Optional external dependency: iconv |
282 | IF (USE_ICONV) | |
6bcb0987 | 283 | FIND_PACKAGE(Iconv) |
726b75d1 RB |
284 | ENDIF() |
285 | IF (ICONV_FOUND) | |
286 | ADD_DEFINITIONS(-DGIT_USE_ICONV) | |
6e0ff093 | 287 | INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR}) |
4bc94eb5 | 288 | SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} ${ICONV_LIBRARIES}") |
726b75d1 | 289 | ENDIF() |
d10de8bd | 290 | |
502dd2da | 291 | # Platform specific compilation flags |
dcd62cb2 | 292 | IF (MSVC) |
dcd62cb2 | 293 | |
ccf1a2ba | 294 | STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
94155e2f | 295 | |
19be3f9e PK |
296 | # /GF - String pooling |
297 | # /MP - Parallel build | |
298 | SET(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}") | |
299 | ||
1b5078f6 | 300 | IF (STDCALL) |
19be3f9e PK |
301 | # /Gz - stdcall calling convention |
302 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") | |
1b5078f6 | 303 | ENDIF () |
19be3f9e | 304 | |
08f32085 Q |
305 | IF (STATIC_CRT) |
306 | SET(CRT_FLAG_DEBUG "/MTd") | |
307 | SET(CRT_FLAG_RELEASE "/MT") | |
308 | ELSE() | |
ccf1a2ba | 309 | SET(CRT_FLAG_DEBUG "/MDd") |
08f32085 Q |
310 | SET(CRT_FLAG_RELEASE "/MD") |
311 | ENDIF() | |
312 | ||
d06c589f JH |
313 | IF (MSVC_CRTDBG) |
314 | SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG} /DGIT_MSVC_CRTDBG") | |
315 | ENDIF() | |
316 | ||
19be3f9e PK |
317 | # /Zi - Create debugging information |
318 | # /Od - Disable optimization | |
319 | # /D_DEBUG - #define _DEBUG | |
320 | # /MTd - Statically link the multithreaded debug version of the CRT | |
08f32085 | 321 | # /MDd - Dynamically link the multithreaded debug version of the CRT |
19be3f9e | 322 | # /RTC1 - Run time checks |
08f32085 | 323 | SET(CMAKE_C_FLAGS_DEBUG "/Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}") |
19be3f9e | 324 | |
69c28b75 | 325 | # /DNDEBUG - Disables asserts |
19be3f9e | 326 | # /MT - Statically link the multithreaded release version of the CRT |
08f32085 | 327 | # /MD - Dynamically link the multithreaded release version of the CRT |
19be3f9e PK |
328 | # /O2 - Optimize for speed |
329 | # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off) | |
330 | # /GL - Link time code generation (whole program optimization) | |
331 | # /Gy - Function-level linking | |
08f32085 | 332 | SET(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}") |
19be3f9e PK |
333 | |
334 | # /Oy- - Disable frame pointer omission (FPO) | |
08f32085 | 335 | SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}") |
19be3f9e PK |
336 | |
337 | # /O1 - Optimize for size | |
08f32085 | 338 | SET(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}") |
19be3f9e PK |
339 | |
340 | # /DYNAMICBASE - Address space load randomization (ASLR) | |
341 | # /NXCOMPAT - Data execution prevention (DEP) | |
342 | # /LARGEADDRESSAWARE - >2GB user address space on x86 | |
343 | # /VERSION - Embed version information in PE header | |
344 | SET(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}") | |
345 | ||
346 | # /DEBUG - Create a PDB | |
347 | # /LTCG - Link time code generation (whole program optimization) | |
348 | # /OPT:REF /OPT:ICF - Fold out duplicate code at link step | |
349 | # /INCREMENTAL:NO - Required to use /LTCG | |
350 | # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug) | |
351 | SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG") | |
352 | SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO") | |
353 | SET(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup") | |
354 | SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO") | |
355 | ||
356 | # Same linker settings for DLL as EXE | |
357 | SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") | |
358 | SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}") | |
359 | SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") | |
360 | SET(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}") | |
361 | SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}") | |
362 | ||
502dd2da | 363 | SET(WIN_RC "src/win32/git2.rc") |
9f75a9ce BS |
364 | |
365 | # Precompiled headers | |
94155e2f | 366 | |
e233fa6f | 367 | ELSE () |
c6cd3f8b | 368 | SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra ${CMAKE_C_FLAGS}") |
19be3f9e | 369 | |
98b8fcff JG |
370 | IF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
371 | SET(CMAKE_C_FLAGS "-std=c99 -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}") | |
372 | ENDIF() | |
373 | ||
19be3f9e PK |
374 | IF (WIN32 AND NOT CYGWIN) |
375 | SET(CMAKE_C_FLAGS_DEBUG "-D_DEBUG") | |
376 | ENDIF () | |
377 | ||
2eb18449 SG |
378 | IF (MINGW) # MinGW always does PIC and complains if we tell it to |
379 | STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}") | |
0887b580 CMN |
380 | # MinGW >= 3.14 uses the C99-style stdio functions |
381 | # automatically, but forks like mingw-w64 still want | |
382 | # us to define this in order to use them | |
383 | ADD_DEFINITIONS(-D__USE_MINGW_ANSI_STDIO=1) | |
384 | ||
b41e24a6 | 385 | ELSEIF (BUILD_SHARED_LIBS) |
5c8d5eac | 386 | ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden) |
c6cd3f8b JG |
387 | |
388 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") | |
39cdf272 | 389 | ENDIF () |
c6cd3f8b | 390 | |
5c8d5eac JG |
391 | ADD_C_FLAG_IF_SUPPORTED(-Wno-missing-field-initializers) |
392 | ADD_C_FLAG_IF_SUPPORTED(-Wstrict-aliasing=2) | |
393 | ADD_C_FLAG_IF_SUPPORTED(-Wstrict-prototypes) | |
394 | ADD_C_FLAG_IF_SUPPORTED(-Wdeclaration-after-statement) | |
395 | ADD_C_FLAG_IF_SUPPORTED(-Wno-unused-const-variable) | |
396 | ADD_C_FLAG_IF_SUPPORTED(-Wno-unused-function) | |
c6cd3f8b | 397 | |
364ef528 | 398 | IF (APPLE) # Apple deprecated OpenSSL |
5c8d5eac | 399 | ADD_C_FLAG_IF_SUPPORTED(-Wno-deprecated-declarations) |
c6cd3f8b | 400 | ENDIF() |
823c0e9c | 401 | |
706a9974 RB |
402 | IF (PROFILE) |
403 | SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") | |
404 | SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}") | |
405 | ENDIF () | |
90412507 | 406 | ENDIF() |
5b8bb8e7 | 407 | |
88149fae PT |
408 | IF( NOT CMAKE_CONFIGURATION_TYPES ) |
409 | # Build Debug by default | |
410 | IF (NOT CMAKE_BUILD_TYPE) | |
411 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) | |
412 | ENDIF () | |
413 | ELSE() | |
414 | # Using a multi-configuration generator eg MSVC or Xcode | |
415 | # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE | |
416 | ENDIF() | |
583cf169 | 417 | |
96ef3d84 CY |
418 | IF (OPENSSL_FOUND) |
419 | ADD_DEFINITIONS(-DGIT_SSL) | |
420 | INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) | |
421 | SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES}) | |
41cbbea8 | 422 | ENDIF() |
66024c7c | 423 | |
70236bab VM |
424 | IF (THREADSAFE) |
425 | IF (NOT WIN32) | |
48ce93e0 | 426 | FIND_PACKAGE(Threads REQUIRED) |
70236bab VM |
427 | ENDIF() |
428 | ||
429 | ADD_DEFINITIONS(-DGIT_THREADS) | |
430 | ENDIF() | |
431 | ||
678e9e04 VM |
432 | ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) |
433 | ||
583cf169 | 434 | # Collect sourcefiles |
cdacd3d9 | 435 | FILE(GLOB SRC_H include/git2.h include/git2/*.h include/git2/sys/*.h) |
583cf169 PD |
436 | |
437 | # On Windows use specific platform sources | |
5b8bb8e7 | 438 | IF (WIN32 AND NOT CYGWIN) |
19be3f9e | 439 | ADD_DEFINITIONS(-DWIN32 -D_WIN32_WINNT=0x0501) |
cdacd3d9 | 440 | FILE(GLOB SRC_OS src/win32/*.c src/win32/*.h) |
96ef3d84 | 441 | ELSEIF (AMIGA) |
7697e541 | 442 | ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R -DNO_MMAP) |
678e9e04 | 443 | ELSE() |
7697e541 RB |
444 | IF (VALGRIND) |
445 | ADD_DEFINITIONS(-DNO_MMAP) | |
446 | ENDIF() | |
cdacd3d9 | 447 | FILE(GLOB SRC_OS src/unix/*.c src/unix/*.h) |
521479b1 | 448 | ENDIF() |
cdacd3d9 | 449 | FILE(GLOB SRC_GIT2 src/*.c src/*.h src/transports/*.c src/transports/*.h src/xdiff/*.c src/xdiff/*.h) |
5b8bb8e7 | 450 | |
eb63fda2 ET |
451 | # Determine architecture of the machine |
452 | IF (CMAKE_SIZEOF_VOID_P EQUAL 8) | |
453 | ADD_DEFINITIONS(-DGIT_ARCH_64) | |
454 | ELSEIF (CMAKE_SIZEOF_VOID_P EQUAL 4) | |
455 | ADD_DEFINITIONS(-DGIT_ARCH_32) | |
456 | ELSE() | |
48ce93e0 | 457 | MESSAGE(FATAL_ERROR "Unsupported architecture") |
eb63fda2 ET |
458 | ENDIF() |
459 | ||
583cf169 | 460 | # Compile and link libgit2 |
3753110a | 461 | ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1} ${WIN_RC}) |
94243295 | 462 | TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES}) |
7369b3c3 | 463 | TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES}) |
315cb38e | 464 | TARGET_LINK_LIBRARIES(git2 ${GSSAPI_LIBRARIES}) |
726b75d1 | 465 | TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES}) |
94243295 | 466 | TARGET_OS_LIBRARIES(git2) |
39cdf272 | 467 | |
d66a7c06 Q |
468 | # Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240) |
469 | # Win64+MSVC+static libs = linker error | |
ac1d85cf | 470 | IF(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS) |
d66a7c06 Q |
471 | SET_TARGET_PROPERTIES(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64") |
472 | ENDIF() | |
473 | ||
523a3ae5 SC |
474 | MSVC_SPLIT_SOURCES(git2) |
475 | ||
e7da9acd NO |
476 | IF (SONAME) |
477 | SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) | |
99807672 | 478 | SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_SOVERSION}) |
c1cf1af4 | 479 | IF (LIBGIT2_FILENAME) |
480 | ADD_DEFINITIONS(-DLIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\") | |
481 | SET_TARGET_PROPERTIES(git2 PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME}) | |
3bbc87d6 | 482 | ENDIF() |
e7da9acd | 483 | ENDIF() |
4fd486e0 | 484 | CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) |
583cf169 | 485 | |
26d9e317 | 486 | IF (MSVC_IDE) |
73aaf674 BS |
487 | # Precompiled headers |
488 | SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
489 | SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") | |
490 | ENDIF () | |
491 | ||
583cf169 | 492 | # Install |
932d1baf | 493 | INSTALL(TARGETS git2 |
9795a40f | 494 | RUNTIME DESTINATION ${BIN_INSTALL_DIR} |
b3237ac3 ICQ |
495 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} |
496 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} | |
583cf169 | 497 | ) |
b3237ac3 | 498 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig ) |
9795a40f VP |
499 | INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} ) |
500 | INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} ) | |
583cf169 PD |
501 | |
502 | # Tests | |
3fd1520c | 503 | IF (BUILD_CLAR) |
86a459a8 CB |
504 | FIND_PACKAGE(PythonInterp REQUIRED) |
505 | ||
83e1efbf BS |
506 | SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/") |
507 | SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests") | |
508 | SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.") | |
3fd1520c | 509 | ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") |
e0799b6c | 510 | ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\") |
29eed1c7 | 511 | ADD_DEFINITIONS(-DCLAR_TMPDIR=\"libgit2_tests\") |
11385c3c | 512 | |
3fd1520c | 513 | INCLUDE_DIRECTORIES(${CLAR_PATH}) |
cdacd3d9 | 514 | FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/*/*.h) |
9a859ef5 | 515 | SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar_libgit2_trace.c" "${CLAR_PATH}/clar_libgit2_timer.c" "${CLAR_PATH}/clar.c") |
f1558d9b | 516 | |
86a459a8 | 517 | ADD_CUSTOM_COMMAND( |
156cfec0 | 518 | OUTPUT ${CLAR_PATH}/clar.suite |
e8242022 | 519 | COMMAND ${PYTHON_EXECUTABLE} generate.py -f -xonline -xstress . |
39444bea | 520 | DEPENDS ${SRC_TEST} |
3fd1520c | 521 | WORKING_DIRECTORY ${CLAR_PATH} |
86a459a8 | 522 | ) |
156cfec0 | 523 | |
5c2d3f6d | 524 | SET_SOURCE_FILES_PROPERTIES( |
2e6f06a8 | 525 | ${CLAR_PATH}/clar.c |
5c2d3f6d VM |
526 | PROPERTIES OBJECT_DEPENDS ${CLAR_PATH}/clar.suite) |
527 | ||
3753110a | 528 | ADD_EXECUTABLE(libgit2_clar ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SSH} ${SRC_SHA1}) |
5c2d3f6d | 529 | |
94243295 | 530 | TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES}) |
7369b3c3 | 531 | TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES}) |
315cb38e | 532 | TARGET_LINK_LIBRARIES(libgit2_clar ${GSSAPI_LIBRARIES}) |
726b75d1 | 533 | TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES}) |
94243295 | 534 | TARGET_OS_LIBRARIES(libgit2_clar) |
523a3ae5 | 535 | MSVC_SPLIT_SOURCES(libgit2_clar) |
73aaf674 | 536 | |
156cfec0 VM |
537 | IF (MSVC_IDE) |
538 | # Precompiled headers | |
539 | SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") | |
540 | ENDIF () | |
73aaf674 | 541 | |
f1558d9b | 542 | ENABLE_TESTING() |
06353282 TK |
543 | IF (WINHTTP OR OPENSSL_FOUND) |
544 | ADD_TEST(libgit2_clar libgit2_clar -ionline) | |
545 | ELSE () | |
546 | ADD_TEST(libgit2_clar libgit2_clar -v) | |
547 | ENDIF () | |
f1558d9b | 548 | ENDIF () |
e632f687 CB |
549 | |
550 | IF (TAGS) | |
551 | FIND_PROGRAM(CTAGS ctags) | |
552 | IF (NOT CTAGS) | |
48ce93e0 | 553 | MESSAGE(FATAL_ERROR "Could not find ctags command") |
e632f687 CB |
554 | ENDIF () |
555 | ||
556 | FILE(GLOB_RECURSE SRC_ALL *.[ch]) | |
557 | ||
558 | ADD_CUSTOM_COMMAND( | |
559 | OUTPUT tags | |
560 | COMMAND ${CTAGS} -a ${SRC_ALL} | |
561 | DEPENDS ${SRC_ALL} | |
562 | ) | |
563 | ADD_CUSTOM_TARGET( | |
564 | do_tags ALL | |
565 | DEPENDS tags | |
566 | ) | |
567 | ENDIF () | |
62986ff6 SG |
568 | |
569 | IF (BUILD_EXAMPLES) | |
60ee53df | 570 | ADD_SUBDIRECTORY(examples) |
583cf169 | 571 | ENDIF () |