]> git.proxmox.com Git - libgit2.git/blob - CMakeLists.txt
Don't use precompiled headers for command-line based VC builds.
[libgit2.git] / CMakeLists.txt
1 # CMake build script for the libgit2 project
2 #
3 # Building (out of source build):
4 # > mkdir build && cd build
5 # > cmake .. [-DSETTINGS=VALUE]
6 # > cmake --build .
7 #
8 # Testing:
9 # > ctest -V
10 #
11 # Install:
12 # > cmake --build . --target install
13
14 PROJECT(libgit2 C)
15 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
16
17 FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
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
24 IF (AMIGA)
25 # Default AmigaOS to use the PowerPC SHA1
26 SET(SHA1_TYPE "ppc")
27 ENDIF()
28
29 # Find required dependencies
30 INCLUDE_DIRECTORIES(src include deps/http-parser)
31
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()
38
39 # Specify sha1 implementation
40 IF (SHA1_TYPE STREQUAL "ppc")
41 ADD_DEFINITIONS(-DPPC_SHA1)
42 FILE(GLOB SRC_SHA1 src/hash/hash_ppc.c src/hash/hash_ppc_core.S)
43 ELSEIF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
44 ADD_DEFINITIONS(-DWIN32_SHA1)
45 FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
46 ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
47 ADD_DEFINITIONS(-DOPENSSL_SHA1)
48 ELSE()
49 FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
50 ENDIF()
51
52 IF (NOT WIN32)
53 FIND_PACKAGE(ZLIB)
54 IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
55 INCLUDE_DIRECTORIES(deps/regex)
56 SET(SRC_REGEX deps/regex/regex.c)
57 ENDIF()
58 ELSE()
59 # Windows doesn't understand POSIX regex on its own
60 INCLUDE_DIRECTORIES(deps/regex)
61 SET(SRC_REGEX deps/regex/regex.c)
62 ENDIF()
63
64 IF (ZLIB_FOUND)
65 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
66 LINK_LIBRARIES(${ZLIB_LIBRARIES})
67 ELSE (ZLIB_FOUND)
68 INCLUDE_DIRECTORIES(deps/zlib)
69 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
70 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
71 ENDIF()
72
73 # Installation paths
74 SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
75 SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.")
76 SET(INCLUDE_INSTALL_DIR include CACHE PATH "Where to install headers to.")
77
78 # Build options
79 OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
80 OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
81 OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
82 OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
83 OPTION (TAGS "Generate tags" OFF)
84 OPTION (PROFILE "Generate profiling information" OFF)
85
86 # Platform specific compilation flags
87 IF (MSVC)
88 # Default to stdcall, as that's what the CLR expects and how the Windows API is built
89 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
90
91 STRING(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
92
93 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
94 IF (STDCALL)
95 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
96 ENDIF ()
97 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
98 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
99 SET(WIN_RC "src/win32/git2.rc")
100
101 # Precompiled headers
102
103 ELSE ()
104 SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
105 IF (MINGW) # MinGW always does PIC and complains if we tell it to
106 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
107 ELSE ()
108 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
109 ENDIF ()
110 IF (APPLE) # Apple deprecated OpenSSL
111 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
112 ENDIF ()
113 IF (PROFILE)
114 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
115 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
116 ENDIF ()
117 ENDIF()
118
119 IF( NOT CMAKE_CONFIGURATION_TYPES )
120 # Build Debug by default
121 IF (NOT CMAKE_BUILD_TYPE)
122 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
123 ENDIF ()
124 ELSE()
125 # Using a multi-configuration generator eg MSVC or Xcode
126 # that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
127 ENDIF()
128
129 IF (OPENSSL_FOUND)
130 ADD_DEFINITIONS(-DGIT_SSL)
131 INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
132 SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
133 ENDIF()
134
135 IF (THREADSAFE)
136 IF (NOT WIN32)
137 find_package(Threads REQUIRED)
138 ENDIF()
139
140 ADD_DEFINITIONS(-DGIT_THREADS)
141 ENDIF()
142
143 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
144
145 # Collect sourcefiles
146 FILE(GLOB SRC_H include/git2/*.h)
147
148 # On Windows use specific platform sources
149 IF (WIN32 AND NOT CYGWIN)
150 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501)
151 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c)
152 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
153 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c)
154 ELSEIF (AMIGA)
155 ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
156 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c)
157 ELSE()
158 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
159 ENDIF ()
160
161 # Compile and link libgit2
162 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
163
164 IF (WIN32)
165 TARGET_LINK_LIBRARIES(git2 ws2_32)
166 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
167 TARGET_LINK_LIBRARIES(git2 socket nsl)
168 ENDIF ()
169
170 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
171 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
172 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
173 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
174
175 IF (MSVC_IDE)
176 # Precompiled headers
177 SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
178 SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
179 ENDIF ()
180
181 # Install
182 INSTALL(TARGETS git2
183 RUNTIME DESTINATION ${BIN_INSTALL_DIR}
184 LIBRARY DESTINATION ${LIB_INSTALL_DIR}
185 ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
186 )
187 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
188 INSTALL(DIRECTORY include/git2 DESTINATION ${INCLUDE_INSTALL_DIR} )
189 INSTALL(FILES include/git2.h DESTINATION ${INCLUDE_INSTALL_DIR} )
190
191 # Tests
192 IF (BUILD_CLAR)
193
194 FIND_PACKAGE(PythonInterp REQUIRED)
195
196 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
197 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
198 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
199 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
200 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
201
202 INCLUDE_DIRECTORIES(${CLAR_PATH})
203 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
204
205 ADD_CUSTOM_COMMAND(
206 OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
207 COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
208 DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
209 WORKING_DIRECTORY ${CLAR_PATH}
210 )
211 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
212 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
213
214 IF (MSVC_IDE)
215 # Precompiled headers
216 SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
217 ENDIF ()
218
219 IF (WIN32)
220 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
221 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
222 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
223 ENDIF ()
224
225 ENABLE_TESTING()
226 ADD_TEST(libgit2_clar libgit2_clar -iall)
227 ENDIF ()
228
229 IF (TAGS)
230 FIND_PROGRAM(CTAGS ctags)
231 IF (NOT CTAGS)
232 message(FATAL_ERROR "Could not find ctags command")
233 ENDIF ()
234
235 FILE(GLOB_RECURSE SRC_ALL *.[ch])
236
237 ADD_CUSTOM_COMMAND(
238 OUTPUT tags
239 COMMAND ${CTAGS} -a ${SRC_ALL}
240 DEPENDS ${SRC_ALL}
241 )
242 ADD_CUSTOM_TARGET(
243 do_tags ALL
244 DEPENDS tags
245 )
246 ENDIF ()
247
248 IF (BUILD_EXAMPLES)
249 FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
250 ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
251 TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
252
253 ADD_EXECUTABLE(git-diff examples/diff.c)
254 TARGET_LINK_LIBRARIES(git-diff git2)
255
256 ADD_EXECUTABLE(git-general examples/general.c)
257 TARGET_LINK_LIBRARIES(git-general git2)
258
259 ADD_EXECUTABLE(git-showindex examples/showindex.c)
260 TARGET_LINK_LIBRARIES(git-showindex git2)
261 ENDIF ()