]> git.proxmox.com Git - libgit2.git/blob - src/CMakeLists.txt
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / CMakeLists.txt
1 add_library(git2internal OBJECT)
2 set_target_properties(git2internal PROPERTIES C_STANDARD 90)
3 set_target_properties(git2internal PROPERTIES C_EXTENSIONS OFF)
4
5
6 if(DEPRECATE_HARD)
7 add_definitions(-DGIT_DEPRECATE_HARD)
8 endif()
9
10 if(DEBUG_POOL)
11 set(GIT_DEBUG_POOL 1)
12 endif()
13 add_feature_info(debugpool GIT_DEBUG_POOL "debug pool allocator")
14
15 if(DEBUG_STRICT_ALLOC)
16 set(GIT_DEBUG_STRICT_ALLOC 1)
17 endif()
18 add_feature_info(debugalloc GIT_DEBUG_STRICT_ALLOC "debug strict allocators")
19
20 if(DEBUG_STRICT_OPEN)
21 set(GIT_DEBUG_STRICT_OPEN 1)
22 endif()
23 add_feature_info(debugopen GIT_DEBUG_STRICT_OPEN "path validation in open")
24
25
26 include(PkgBuildConfig)
27 include(SanitizeBool)
28
29 # This variable will contain the libraries we need to put into
30 # libgit2.pc's Requires.private. That is, what we're linking to or
31 # what someone who's statically linking us needs to link to.
32 set(LIBGIT2_PC_REQUIRES "")
33 # This will be set later if we use the system's http-parser library or
34 # use iconv (OSX) and will be written to the Libs.private field in the
35 # pc file.
36 set(LIBGIT2_PC_LIBS "")
37
38 set(LIBGIT2_INCLUDES
39 "${CMAKE_CURRENT_BINARY_DIR}"
40 "${PROJECT_SOURCE_DIR}/src"
41 "${PROJECT_SOURCE_DIR}/include")
42
43 if(HAVE_FUTIMENS)
44 set(GIT_USE_FUTIMENS 1)
45 endif ()
46 add_feature_info(futimens GIT_USE_FUTIMENS "futimens support")
47
48 check_prototype_definition(qsort_r
49 "void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *))"
50 "" "stdlib.h" GIT_QSORT_R_BSD)
51
52 check_prototype_definition(qsort_r
53 "void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg)"
54 "" "stdlib.h" GIT_QSORT_R_GNU)
55
56 check_function_exists(qsort_s GIT_QSORT_S)
57
58 check_function_exists(getentropy GIT_RAND_GETENTROPY)
59
60 # Find required dependencies
61
62 if(WIN32)
63 list(APPEND LIBGIT2_SYSTEM_LIBS ws2_32)
64 elseif(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
65 list(APPEND LIBGIT2_SYSTEM_LIBS socket nsl)
66 list(APPEND LIBGIT2_PC_LIBS "-lsocket" "-lnsl")
67 elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku")
68 list(APPEND LIBGIT2_SYSTEM_LIBS network)
69 list(APPEND LIBGIT2_PC_LIBS "-lnetwork")
70 endif()
71
72 check_library_exists(rt clock_gettime "time.h" NEED_LIBRT)
73 if(NEED_LIBRT)
74 list(APPEND LIBGIT2_SYSTEM_LIBS rt)
75 list(APPEND LIBGIT2_PC_LIBS "-lrt")
76 endif()
77
78 if(USE_THREADS)
79 list(APPEND LIBGIT2_SYSTEM_LIBS ${CMAKE_THREAD_LIBS_INIT})
80 list(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT})
81 endif()
82 add_feature_info(threadsafe USE_THREADS "threadsafe support")
83
84
85 if(WIN32 AND EMBED_SSH_PATH)
86 file(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
87 list(SORT SRC_SSH)
88 target_sources(git2internal PRIVATE ${SRC_SSH})
89
90 list(APPEND LIBGIT2_SYSTEM_INCLUDES "${EMBED_SSH_PATH}/include")
91 file(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
92 set(GIT_SSH 1)
93 endif()
94
95 include(SelectHTTPSBackend)
96 include(SelectHashes)
97 include(SelectHTTPParser)
98 include(SelectRegex)
99 include(SelectSSH)
100 include(SelectWinHTTP)
101 include(SelectZlib)
102
103
104 if(USE_SHA1 STREQUAL "CollisionDetection")
105 file(GLOB SRC_SHA1 hash/sha1/collisiondetect.* hash/sha1/sha1dc/*)
106 elseif(USE_SHA1 STREQUAL "OpenSSL")
107 file(GLOB SRC_SHA1 hash/sha1/openssl.*)
108 elseif(USE_SHA1 STREQUAL "CommonCrypto")
109 file(GLOB SRC_SHA1 hash/sha1/common_crypto.*)
110 elseif(USE_SHA1 STREQUAL "mbedTLS")
111 file(GLOB SRC_SHA1 hash/sha1/mbedtls.*)
112 elseif(USE_SHA1 STREQUAL "Win32")
113 file(GLOB SRC_SHA1 hash/sha1/win32.*)
114 elseif(USE_SHA1 STREQUAL "Generic")
115 file(GLOB SRC_SHA1 hash/sha1/generic.*)
116 endif()
117 list(APPEND SRC_SHA1 "hash/sha1.h")
118 target_sources(git2internal PRIVATE ${SRC_SHA1})
119
120 # Optional external dependency: ntlmclient
121 if(USE_NTLMCLIENT)
122 set(GIT_NTLM 1)
123 add_subdirectory("${PROJECT_SOURCE_DIR}/deps/ntlmclient" "${PROJECT_BINARY_DIR}/deps/ntlmclient")
124 list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${PROJECT_SOURCE_DIR}/deps/ntlmclient")
125 list(APPEND LIBGIT2_DEPENDENCY_OBJECTS "$<TARGET_OBJECTS:ntlmclient>")
126 endif()
127 add_feature_info(ntlmclient GIT_NTLM "NTLM authentication support for Unix")
128
129 # Optional external dependency: GSSAPI
130
131 include(SelectGSSAPI)
132
133 # Optional external dependency: iconv
134 if(USE_ICONV)
135 find_package(Iconv)
136 endif()
137 if(ICONV_FOUND)
138 set(GIT_USE_ICONV 1)
139 list(APPEND LIBGIT2_SYSTEM_INCLUDES ${ICONV_INCLUDE_DIR})
140 list(APPEND LIBGIT2_SYSTEM_LIBS ${ICONV_LIBRARIES})
141 list(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
142 endif()
143 add_feature_info(iconv GIT_USE_ICONV "iconv encoding conversion support")
144
145
146 if(USE_THREADS)
147 if(NOT WIN32)
148 find_package(Threads REQUIRED)
149 endif()
150
151 set(GIT_THREADS 1)
152 endif()
153
154 if(USE_NSEC)
155 set(GIT_USE_NSEC 1)
156 endif()
157
158 if(HAVE_STRUCT_STAT_ST_MTIM)
159 set(GIT_USE_STAT_MTIM 1)
160 elseif(HAVE_STRUCT_STAT_ST_MTIMESPEC)
161 set(GIT_USE_STAT_MTIMESPEC 1)
162 elseif(HAVE_STRUCT_STAT_ST_MTIME_NSEC)
163 set(GIT_USE_STAT_MTIME_NSEC 1)
164 endif()
165
166 target_compile_definitions(git2internal PRIVATE _FILE_OFFSET_BITS=64)
167
168 # Collect sourcefiles
169 file(GLOB SRC_H
170 "${PROJECT_SOURCE_DIR}/include/git2.h"
171 "${PROJECT_SOURCE_DIR}/include/git2/*.h"
172 "${PROJECT_SOURCE_DIR}/include/git2/sys/*.h")
173 list(SORT SRC_H)
174 target_sources(git2internal PRIVATE ${SRC_H})
175
176 # On Windows use specific platform sources
177 if(WIN32 AND NOT CYGWIN)
178 set(WIN_RC "win32/git2.rc")
179
180 file(GLOB SRC_OS win32/*.c win32/*.h)
181 list(SORT SRC_OS)
182 target_sources(git2internal PRIVATE ${SRC_OS})
183 elseif(AMIGA)
184 target_compile_definitions(git2internal PRIVATE NO_ADDRINFO NO_READDIR_R NO_MMAP)
185 else()
186 file(GLOB SRC_OS unix/*.c unix/*.h)
187 list(SORT SRC_OS)
188 target_sources(git2internal PRIVATE ${SRC_OS})
189 endif()
190
191 if(USE_LEAK_CHECKER STREQUAL "valgrind")
192 target_compile_definitions(git2internal PRIVATE VALGRIND)
193 endif()
194
195 file(GLOB SRC_GIT2 *.c *.h
196 allocators/*.c allocators/*.h
197 streams/*.c streams/*.h
198 transports/*.c transports/*.h
199 xdiff/*.c xdiff/*.h)
200 list(SORT SRC_GIT2)
201 target_sources(git2internal PRIVATE ${SRC_GIT2})
202
203 if(APPLE)
204 # The old Secure Transport API has been deprecated in macOS 10.15.
205 set_source_files_properties(streams/stransport.c PROPERTIES COMPILE_FLAGS -Wno-deprecated)
206 endif()
207
208 # the xdiff dependency is not (yet) warning-free, disable warnings as
209 # errors for the xdiff sources until we've sorted them out
210 if(MSVC)
211 set_source_files_properties(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS -WX-)
212 set_source_files_properties(xdiff/xemit.c PROPERTIES COMPILE_FLAGS -WX-)
213 set_source_files_properties(xdiff/xhistogram.c PROPERTIES COMPILE_FLAGS -WX-)
214 set_source_files_properties(xdiff/xmerge.c PROPERTIES COMPILE_FLAGS -WX-)
215 set_source_files_properties(xdiff/xutils.c PROPERTIES COMPILE_FLAGS -WX-)
216 set_source_files_properties(xdiff/xpatience.c PROPERTIES COMPILE_FLAGS -WX-)
217 else()
218 set_source_files_properties(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-unused-parameter")
219 set_source_files_properties(xdiff/xemit.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-unused-parameter")
220 set_source_files_properties(xdiff/xhistogram.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare")
221 set_source_files_properties(xdiff/xutils.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare")
222 set_source_files_properties(xdiff/xpatience.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare")
223 endif()
224
225 # Determine architecture of the machine
226 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
227 set(GIT_ARCH_64 1)
228 elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
229 set(GIT_ARCH_32 1)
230 elseif(CMAKE_SIZEOF_VOID_P)
231 message(FATAL_ERROR "Unsupported architecture (pointer size is ${CMAKE_SIZEOF_VOID_P} bytes)")
232 else()
233 message(FATAL_ERROR "Unsupported architecture (CMAKE_SIZEOF_VOID_P is unset)")
234 endif()
235
236 configure_file(features.h.in git2/sys/features.h)
237
238 ide_split_sources(git2internal)
239 list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:git2internal> ${LIBGIT2_DEPENDENCY_OBJECTS})
240
241 target_include_directories(git2internal PRIVATE ${LIBGIT2_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES} PUBLIC ${PROJECT_SOURCE_DIR}/include)
242 target_include_directories(git2internal SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES})
243
244 set(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE)
245 set(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE)
246 set(LIBGIT2_DEPENDENCY_INCLUDES ${LIBGIT2_DEPENDENCY_INCLUDES} PARENT_SCOPE)
247 set(LIBGIT2_DEPENDENCY_OBJECTS ${LIBGIT2_DEPENDENCY_OBJECTS} PARENT_SCOPE)
248 set(LIBGIT2_SYSTEM_INCLUDES ${LIBGIT2_SYSTEM_INCLUDES} PARENT_SCOPE)
249 set(LIBGIT2_SYSTEM_LIBS ${LIBGIT2_SYSTEM_LIBS} PARENT_SCOPE)
250
251 if(XCODE_VERSION)
252 # This is required for Xcode to actually link the libgit2 library
253 # when using only object libraries.
254 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.c "")
255 list(APPEND LIBGIT2_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/dummy.c)
256 endif()
257
258 # Compile and link libgit2
259 add_library(git2 ${WIN_RC} ${LIBGIT2_OBJECTS})
260 target_link_libraries(git2 ${LIBGIT2_SYSTEM_LIBS})
261
262 set_target_properties(git2 PROPERTIES C_STANDARD 90)
263 set_target_properties(git2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
264 set_target_properties(git2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
265 set_target_properties(git2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
266
267 # Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
268 # Win64+MSVC+static libs = linker error
269 if(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
270 set_target_properties(git2 PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
271 endif()
272
273 ide_split_sources(git2)
274
275 if(SONAME)
276 set_target_properties(git2 PROPERTIES VERSION ${libgit2_VERSION})
277 set_target_properties(git2 PROPERTIES SOVERSION "${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
278 if(LIBGIT2_FILENAME)
279 target_compile_definitions(git2 PRIVATE LIBGIT2_FILENAME=\"${LIBGIT2_FILENAME}\")
280 set_target_properties(git2 PROPERTIES OUTPUT_NAME ${LIBGIT2_FILENAME})
281 elseif(DEFINED LIBGIT2_PREFIX)
282 set_target_properties(git2 PROPERTIES PREFIX "${LIBGIT2_PREFIX}")
283 endif()
284 endif()
285
286 pkg_build_config(NAME libgit2
287 VERSION ${libgit2_VERSION}
288 DESCRIPTION "The git library, take 2"
289 LIBS_SELF git2
290 PRIVATE_LIBS ${LIBGIT2_PC_LIBS}
291 REQUIRES ${LIBGIT2_PC_REQUIRES}
292 )
293
294 if(MSVC_IDE)
295 # Precompiled headers
296 set_target_properties(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
297 set_source_files_properties(win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
298 endif()
299
300 # Install
301 install(TARGETS git2
302 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
303 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
304 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
305 )
306 install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/git2 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
307 install(FILES ${PROJECT_SOURCE_DIR}/include/git2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})