]>
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 | ||
7cbdaf7f | 24 | # Find required dependencies |
b8a8191f CMN |
25 | INCLUDE_DIRECTORIES(src include deps/http-parser) |
26 | ||
27 | FILE(GLOB SRC_HTTP deps/http-parser/*.c) | |
28 | ||
1f4f4d17 TH |
29 | IF (NOT WIN32) |
30 | FIND_PACKAGE(ZLIB) | |
c17b1d00 CMN |
31 | ELSE() |
32 | # Windows doesn't understand POSIX regex on its own | |
33 | INCLUDE_DIRECTORIES(deps/regex) | |
34 | SET(SRC_REGEX deps/regex/regex.c) | |
1f4f4d17 TH |
35 | ENDIF() |
36 | ||
37 | IF (ZLIB_FOUND) | |
38 | INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) | |
39 | LINK_LIBRARIES(${ZLIB_LIBRARIES}) | |
40 | ELSE (ZLIB_FOUND) | |
41 | INCLUDE_DIRECTORIES(deps/zlib) | |
42 | ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP) | |
43 | FILE(GLOB SRC_ZLIB deps/zlib/*.c) | |
44 | ENDIF() | |
7cbdaf7f | 45 | |
5b8bb8e7 | 46 | # Installation paths |
73c46d53 PD |
47 | SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.") |
48 | SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.") | |
032db4d0 | 49 | SET(INSTALL_INC include CACHE PATH "Where to install headers to.") |
5b8bb8e7 PD |
50 | |
51 | # Build options | |
f0890fcc | 52 | OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON) |
70236bab | 53 | OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF) |
fd29cd13 | 54 | OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON) |
62986ff6 | 55 | OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF) |
e632f687 | 56 | OPTION (TAGS "Generate tags" OFF) |
706a9974 | 57 | OPTION (PROFILE "Generate profiling information" OFF) |
5b8bb8e7 | 58 | |
502dd2da | 59 | # Platform specific compilation flags |
dcd62cb2 | 60 | IF (MSVC) |
502dd2da | 61 | # Not using __stdcall with the CRT causes problems |
dcd62cb2 | 62 | OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON) |
dcd62cb2 | 63 | |
06c081e1 | 64 | SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}") |
1b5078f6 CMN |
65 | IF (STDCALL) |
66 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") | |
67 | ENDIF () | |
9f75a9ce | 68 | SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu") |
90412507 | 69 | SET(CMAKE_C_FLAGS_RELEASE "/MT /O2") |
502dd2da | 70 | SET(WIN_RC "src/win32/git2.rc") |
9f75a9ce BS |
71 | |
72 | # Precompiled headers | |
e233fa6f | 73 | ELSE () |
b15bef23 | 74 | SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -fvisibility=hidden -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}") |
4ce16ed8 | 75 | SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") |
39cdf272 CMN |
76 | IF (NOT MINGW) # MinGW always does PIC and complains if we tell it to |
77 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") | |
78 | ENDIF () | |
706a9974 RB |
79 | IF (PROFILE) |
80 | SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}") | |
81 | SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}") | |
82 | ENDIF () | |
90412507 | 83 | ENDIF() |
84 | ||
3d96996d | 85 | # Build Debug by default |
583cf169 | 86 | IF (NOT CMAKE_BUILD_TYPE) |
1a7b52dc | 87 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) |
583cf169 PD |
88 | ENDIF () |
89 | ||
70236bab VM |
90 | IF (THREADSAFE) |
91 | IF (NOT WIN32) | |
92 | find_package(Threads REQUIRED) | |
93 | ENDIF() | |
94 | ||
95 | ADD_DEFINITIONS(-DGIT_THREADS) | |
96 | ENDIF() | |
97 | ||
678e9e04 VM |
98 | ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) |
99 | ||
583cf169 | 100 | # Collect sourcefiles |
71d33382 | 101 | FILE(GLOB SRC_H include/git2/*.h) |
583cf169 PD |
102 | |
103 | # On Windows use specific platform sources | |
5b8bb8e7 | 104 | IF (WIN32 AND NOT CYGWIN) |
901fbdad | 105 | ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501) |
ec42eafd SG |
106 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c) |
107 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") | |
108 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c) | |
678e9e04 | 109 | ELSE() |
3a5ad90a | 110 | FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) |
5b8bb8e7 PD |
111 | ENDIF () |
112 | ||
583cf169 | 113 | # Compile and link libgit2 |
c17b1d00 | 114 | ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC}) |
39cdf272 CMN |
115 | |
116 | IF (WIN32) | |
117 | TARGET_LINK_LIBRARIES(git2 ws2_32) | |
e944cfa9 CMN |
118 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
119 | TARGET_LINK_LIBRARIES(git2 socket nsl) | |
39cdf272 CMN |
120 | ENDIF () |
121 | ||
953e1f93 | 122 | TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT}) |
9d1dcca2 VM |
123 | SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) |
124 | SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) | |
4fd486e0 | 125 | CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) |
583cf169 PD |
126 | |
127 | # Install | |
932d1baf | 128 | INSTALL(TARGETS git2 |
86a459a8 CB |
129 | RUNTIME DESTINATION ${INSTALL_BIN} |
130 | LIBRARY DESTINATION ${INSTALL_LIB} | |
131 | ARCHIVE DESTINATION ${INSTALL_LIB} | |
583cf169 | 132 | ) |
4fd486e0 | 133 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig ) |
71d33382 VM |
134 | INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} ) |
135 | INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} ) | |
583cf169 PD |
136 | |
137 | # Tests | |
3fd1520c | 138 | IF (BUILD_CLAR) |
e0799b6c | 139 | |
86a459a8 CB |
140 | FIND_PACKAGE(PythonInterp REQUIRED) |
141 | ||
fd29cd13 | 142 | SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/") |
3fd1520c | 143 | SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar") |
e0799b6c | 144 | SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.") |
3fd1520c | 145 | ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\") |
e0799b6c | 146 | ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\") |
11385c3c | 147 | |
3fd1520c VM |
148 | INCLUDE_DIRECTORIES(${CLAR_PATH}) |
149 | FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c) | |
f1558d9b | 150 | |
86a459a8 | 151 | ADD_CUSTOM_COMMAND( |
3fd1520c VM |
152 | OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h |
153 | COMMAND ${PYTHON_EXECUTABLE} clar -vtap . | |
154 | DEPENDS ${CLAR_PATH}/clar ${SRC_TEST} | |
155 | WORKING_DIRECTORY ${CLAR_PATH} | |
86a459a8 | 156 | ) |
c17b1d00 | 157 | ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX}) |
3fd1520c | 158 | TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT}) |
f1558d9b | 159 | IF (WIN32) |
3fd1520c | 160 | TARGET_LINK_LIBRARIES(libgit2_clar ws2_32) |
f1558d9b | 161 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
3fd1520c | 162 | TARGET_LINK_LIBRARIES(libgit2_clar socket nsl) |
f1558d9b VM |
163 | ENDIF () |
164 | ||
165 | ENABLE_TESTING() | |
3fd1520c | 166 | ADD_TEST(libgit2_clar libgit2_clar) |
f1558d9b | 167 | ENDIF () |
e632f687 CB |
168 | |
169 | IF (TAGS) | |
170 | FIND_PROGRAM(CTAGS ctags) | |
171 | IF (NOT CTAGS) | |
172 | message(FATAL_ERROR "Could not find ctags command") | |
173 | ENDIF () | |
174 | ||
175 | FILE(GLOB_RECURSE SRC_ALL *.[ch]) | |
176 | ||
177 | ADD_CUSTOM_COMMAND( | |
178 | OUTPUT tags | |
179 | COMMAND ${CTAGS} -a ${SRC_ALL} | |
180 | DEPENDS ${SRC_ALL} | |
181 | ) | |
182 | ADD_CUSTOM_TARGET( | |
183 | do_tags ALL | |
184 | DEPENDS tags | |
185 | ) | |
186 | ENDIF () | |
62986ff6 SG |
187 | |
188 | IF (BUILD_EXAMPLES) | |
189 | FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c) | |
190 | ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC}) | |
191 | TARGET_LINK_LIBRARIES(cgit2 git2 pthread) | |
192 | ||
193 | ADD_EXECUTABLE(git-diff examples/diff.c) | |
194 | TARGET_LINK_LIBRARIES(git-diff git2) | |
195 | ||
196 | ADD_EXECUTABLE(git-general examples/general.c) | |
197 | TARGET_LINK_LIBRARIES(git-general git2) | |
198 | ||
199 | ADD_EXECUTABLE(git-showindex examples/showindex.c) | |
200 | TARGET_LINK_LIBRARIES(git-showindex git2) | |
201 | ENDIF () |