]> git.proxmox.com Git - libgit2.git/blob - CMakeLists.txt
Merge pull request #901 from carlosmn/winhttp
[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/ppc/*.c src/ppc/*.S)
43 ELSE ()
44 SET (SRC_SHA1)
45 ENDIF()
46
47 IF (NOT WIN32)
48 FIND_PACKAGE(ZLIB)
49 IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
50 INCLUDE_DIRECTORIES(deps/regex)
51 SET(SRC_REGEX deps/regex/regex.c)
52 ENDIF()
53 ELSE()
54 # Windows doesn't understand POSIX regex on its own
55 INCLUDE_DIRECTORIES(deps/regex)
56 SET(SRC_REGEX deps/regex/regex.c)
57 ENDIF()
58
59 IF (ZLIB_FOUND)
60 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
61 LINK_LIBRARIES(${ZLIB_LIBRARIES})
62 ELSE (ZLIB_FOUND)
63 INCLUDE_DIRECTORIES(deps/zlib)
64 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
65 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
66 ENDIF()
67
68 # Installation paths
69 SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
70 SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.")
71 SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
72
73 # Build options
74 OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
75 OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
76 OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
77 OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
78 OPTION (TAGS "Generate tags" OFF)
79 OPTION (PROFILE "Generate profiling information" OFF)
80
81 # Platform specific compilation flags
82 IF (MSVC)
83 # Default to stdcall, as that's what the CLR expects and how the Windows API is built
84 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
85
86 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
87 IF (STDCALL)
88 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
89 ENDIF ()
90 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
91 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
92 SET(WIN_RC "src/win32/git2.rc")
93
94 # Precompiled headers
95 ELSE ()
96 SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
97 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
98 IF (MINGW) # MinGW always does PIC and complains if we tell it to
99 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
100 ELSE ()
101 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
102 ENDIF ()
103 IF (PROFILE)
104 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
105 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
106 ENDIF ()
107 ENDIF()
108
109 # Build Debug by default
110 IF (NOT CMAKE_BUILD_TYPE)
111 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
112 ENDIF ()
113
114 IF (OPENSSL_FOUND)
115 ADD_DEFINITIONS(-DGIT_SSL)
116 INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
117 SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
118 ENDIF()
119
120 IF (THREADSAFE)
121 IF (NOT WIN32)
122 find_package(Threads REQUIRED)
123 ENDIF()
124
125 ADD_DEFINITIONS(-DGIT_THREADS)
126 ENDIF()
127
128 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
129
130 # Collect sourcefiles
131 FILE(GLOB SRC_H include/git2/*.h)
132
133 # On Windows use specific platform sources
134 IF (WIN32 AND NOT CYGWIN)
135 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501)
136 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c)
137 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
138 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c)
139 ELSEIF (AMIGA)
140 ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R)
141 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c)
142 ELSE()
143 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
144 ENDIF ()
145
146 # Compile and link libgit2
147 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
148
149 IF (WIN32)
150 TARGET_LINK_LIBRARIES(git2 ws2_32)
151 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
152 TARGET_LINK_LIBRARIES(git2 socket nsl)
153 ENDIF ()
154
155 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
156 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
157 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
158 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
159
160 IF (MSVC)
161 # Precompiled headers
162 SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
163 SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
164 ENDIF ()
165
166 # Backward compatibility with INSTALL_LIB variable
167 if (INSTALL_LIB)
168 set(LIB_INSTALL_DIR "${INSTALL_LIB}")
169 ENDIF()
170
171 # Install
172 INSTALL(TARGETS git2
173 RUNTIME DESTINATION ${INSTALL_BIN}
174 LIBRARY DESTINATION ${LIB_INSTALL_DIR}
175 ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
176 )
177 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
178 INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
179 INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
180
181 # Tests
182 IF (BUILD_CLAR)
183
184 FIND_PACKAGE(PythonInterp REQUIRED)
185
186 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
187 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
188 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
189 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
190 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
191
192 INCLUDE_DIRECTORIES(${CLAR_PATH})
193 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
194
195 ADD_CUSTOM_COMMAND(
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}
200 )
201 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
202 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
203
204 IF (MSVC)
205 # Precompiled headers
206 SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
207 ENDIF ()
208
209 IF (WIN32)
210 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
211 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
212 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
213 ENDIF ()
214
215 ENABLE_TESTING()
216 ADD_TEST(libgit2_clar libgit2_clar)
217 ENDIF ()
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 ()
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)
242
243 ADD_EXECUTABLE(git-diff examples/diff.c)
244 TARGET_LINK_LIBRARIES(git-diff git2)
245
246 ADD_EXECUTABLE(git-general examples/general.c)
247 TARGET_LINK_LIBRARIES(git-general git2)
248
249 ADD_EXECUTABLE(git-showindex examples/showindex.c)
250 TARGET_LINK_LIBRARIES(git-showindex git2)
251 ENDIF ()