]>
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) | |
31 | ENDIF() | |
32 | ||
33 | IF (ZLIB_FOUND) | |
34 | INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) | |
35 | LINK_LIBRARIES(${ZLIB_LIBRARIES}) | |
36 | ELSE (ZLIB_FOUND) | |
37 | INCLUDE_DIRECTORIES(deps/zlib) | |
38 | ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP) | |
39 | FILE(GLOB SRC_ZLIB deps/zlib/*.c) | |
40 | ENDIF() | |
7cbdaf7f | 41 | |
5b8bb8e7 | 42 | # Installation paths |
73c46d53 PD |
43 | SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.") |
44 | SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.") | |
032db4d0 | 45 | SET(INSTALL_INC include CACHE PATH "Where to install headers to.") |
5b8bb8e7 PD |
46 | |
47 | # Build options | |
f0890fcc | 48 | OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON) |
70236bab | 49 | OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF) |
d3fb6a81 VM |
50 | OPTION (BUILD_TESTS "Build Tests" ON) |
51 | OPTION (BUILD_CLAY "Build Tests using the Clay suite" OFF) | |
5b8bb8e7 | 52 | |
502dd2da | 53 | # Platform specific compilation flags |
dcd62cb2 | 54 | IF (MSVC) |
502dd2da | 55 | # Not using __stdcall with the CRT causes problems |
dcd62cb2 | 56 | OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON) |
dcd62cb2 | 57 | |
5b216d1a | 58 | SET(CMAKE_C_FLAGS "/W4 /nologo /Zi ${CMAKE_C_FLAGS}") |
1b5078f6 CMN |
59 | IF (STDCALL) |
60 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") | |
61 | ENDIF () | |
e01f7f64 VM |
62 | # TODO: bring back /RTC1 /RTCc |
63 | SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd") | |
90412507 | 64 | SET(CMAKE_C_FLAGS_RELEASE "/MT /O2") |
502dd2da | 65 | SET(WIN_RC "src/win32/git2.rc") |
e233fa6f | 66 | ELSE () |
b762e576 | 67 | SET(CMAKE_C_FLAGS "-O2 -g -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}") |
4ce16ed8 | 68 | SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") |
39cdf272 CMN |
69 | IF (NOT MINGW) # MinGW always does PIC and complains if we tell it to |
70 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") | |
71 | ENDIF () | |
90412507 | 72 | ENDIF() |
73 | ||
3d96996d | 74 | # Build Debug by default |
583cf169 | 75 | IF (NOT CMAKE_BUILD_TYPE) |
1a7b52dc | 76 | SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) |
583cf169 PD |
77 | ENDIF () |
78 | ||
70236bab VM |
79 | IF (THREADSAFE) |
80 | IF (NOT WIN32) | |
81 | find_package(Threads REQUIRED) | |
82 | ENDIF() | |
83 | ||
84 | ADD_DEFINITIONS(-DGIT_THREADS) | |
85 | ENDIF() | |
86 | ||
678e9e04 VM |
87 | ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) |
88 | ||
583cf169 | 89 | # Collect sourcefiles |
71d33382 | 90 | FILE(GLOB SRC_H include/git2/*.h) |
583cf169 PD |
91 | |
92 | # On Windows use specific platform sources | |
5b8bb8e7 | 93 | IF (WIN32 AND NOT CYGWIN) |
da2281c7 | 94 | ADD_DEFINITIONS(-DWIN32 -D_DEBUG) |
dfafb03b | 95 | FILE(GLOB SRC src/*.c src/transports/*.c src/win32/*.c) |
678e9e04 | 96 | ELSE() |
dfafb03b | 97 | FILE(GLOB SRC src/*.c src/transports/*.c src/unix/*.c) |
5b8bb8e7 PD |
98 | ENDIF () |
99 | ||
583cf169 | 100 | # Compile and link libgit2 |
502dd2da | 101 | ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${WIN_RC}) |
39cdf272 CMN |
102 | |
103 | IF (WIN32) | |
104 | TARGET_LINK_LIBRARIES(git2 ws2_32) | |
e944cfa9 CMN |
105 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
106 | TARGET_LINK_LIBRARIES(git2 socket nsl) | |
39cdf272 CMN |
107 | ENDIF () |
108 | ||
953e1f93 | 109 | TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT}) |
9d1dcca2 VM |
110 | SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) |
111 | SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) | |
4fd486e0 | 112 | CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) |
583cf169 PD |
113 | |
114 | # Install | |
932d1baf | 115 | INSTALL(TARGETS git2 |
73c46d53 PD |
116 | RUNTIME DESTINATION ${INSTALL_BIN} |
117 | LIBRARY DESTINATION ${INSTALL_LIB} | |
118 | ARCHIVE DESTINATION ${INSTALL_LIB} | |
583cf169 | 119 | ) |
4fd486e0 | 120 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig ) |
71d33382 VM |
121 | INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} ) |
122 | INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} ) | |
583cf169 PD |
123 | |
124 | # Tests | |
953e1f93 | 125 | IF (BUILD_TESTS) |
11385c3c VM |
126 | SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.") |
127 | ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\") | |
128 | ||
953e1f93 VM |
129 | INCLUDE_DIRECTORIES(tests) |
130 | FILE(GLOB SRC_TEST tests/t??-*.c) | |
131 | ||
b8a8191f | 132 | ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP}) |
953e1f93 | 133 | TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT}) |
e944cfa9 CMN |
134 | IF (WIN32) |
135 | TARGET_LINK_LIBRARIES(libgit2_test ws2_32) | |
136 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") | |
137 | TARGET_LINK_LIBRARIES(libgit2_test socket nsl) | |
138 | ENDIF () | |
f64586fa VM |
139 | |
140 | ENABLE_TESTING() | |
141 | ADD_TEST(libgit2_test libgit2_test) | |
953e1f93 | 142 | ENDIF () |
f1558d9b VM |
143 | |
144 | IF (BUILD_CLAY) | |
11385c3c VM |
145 | SET(CLAY_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/") |
146 | ADD_DEFINITIONS(-DCLAY_FIXTURE_PATH=\"${CLAY_FIXTURES}\") | |
147 | ||
f1558d9b VM |
148 | INCLUDE_DIRECTORIES(tests-clay) |
149 | FILE(GLOB_RECURSE SRC_TEST tests-clay/*.c) | |
150 | ||
87a26ad5 | 151 | ADD_EXECUTABLE(libgit2_clay ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP}) |
48e97ed1 | 152 | TARGET_LINK_LIBRARIES(libgit2_clay ${CMAKE_THREAD_LIBS_INIT}) |
f1558d9b | 153 | IF (WIN32) |
48e97ed1 | 154 | TARGET_LINK_LIBRARIES(libgit2_clay ws2_32) |
f1558d9b | 155 | ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") |
48e97ed1 | 156 | TARGET_LINK_LIBRARIES(libgit2_clay socket nsl) |
f1558d9b VM |
157 | ENDIF () |
158 | ||
159 | ENABLE_TESTING() | |
48e97ed1 | 160 | ADD_TEST(libgit2_clay libgit2_clay) |
f1558d9b | 161 | ENDIF () |