]> git.proxmox.com Git - libgit2.git/blob - CMakeLists.txt
Add POSIX regex sources when needed
[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 # Find required dependencies
25 INCLUDE_DIRECTORIES(src include deps/http-parser)
26
27 FILE(GLOB SRC_HTTP deps/http-parser/*.c)
28
29 IF (NOT WIN32)
30 FIND_PACKAGE(ZLIB)
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)
35 ADD_DEFINITIONS(-DGAWK -DNO_MBSUPPORT)
36 ENDIF()
37
38 IF (ZLIB_FOUND)
39 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
40 LINK_LIBRARIES(${ZLIB_LIBRARIES})
41 ELSE (ZLIB_FOUND)
42 INCLUDE_DIRECTORIES(deps/zlib)
43 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
44 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
45 ENDIF()
46
47 # Installation paths
48 SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
49 SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
50 SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
51
52 # Build options
53 OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
54 OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
55 OPTION (BUILD_TESTS "Build Tests" ON)
56 OPTION (BUILD_CLAR "Build Tests using the Clar suite" OFF)
57 OPTION (TAGS "Generate tags" OFF)
58
59 # Platform specific compilation flags
60 IF (MSVC)
61 # Not using __stdcall with the CRT causes problems
62 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
63
64 SET(CMAKE_C_FLAGS "/W4 /nologo /Zi ${CMAKE_C_FLAGS}")
65 IF (STDCALL)
66 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
67 ENDIF ()
68 # TODO: bring back /RTC1 /RTCc
69 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
70 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
71 SET(WIN_RC "src/win32/git2.rc")
72 ELSE ()
73 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}")
74 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
75 IF (NOT MINGW) # MinGW always does PIC and complains if we tell it to
76 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
77 ENDIF ()
78 ENDIF()
79
80 # Build Debug by default
81 IF (NOT CMAKE_BUILD_TYPE)
82 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
83 ENDIF ()
84
85 IF (THREADSAFE)
86 IF (NOT WIN32)
87 find_package(Threads REQUIRED)
88 ENDIF()
89
90 ADD_DEFINITIONS(-DGIT_THREADS)
91 ENDIF()
92
93 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
94
95 # Collect sourcefiles
96 FILE(GLOB SRC_H include/git2/*.h)
97
98 # On Windows use specific platform sources
99 IF (WIN32 AND NOT CYGWIN)
100 ADD_DEFINITIONS(-DWIN32 -D_DEBUG)
101 FILE(GLOB SRC src/*.c src/transports/*.c src/win32/*.c)
102 ELSE()
103 FILE(GLOB SRC src/*.c src/transports/*.c src/unix/*.c)
104 ENDIF ()
105
106 # Compile and link libgit2
107 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC})
108
109 IF (WIN32)
110 TARGET_LINK_LIBRARIES(git2 ws2_32)
111 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
112 TARGET_LINK_LIBRARIES(git2 socket nsl)
113 ENDIF ()
114
115 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT})
116 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
117 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
118 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
119
120 # Install
121 INSTALL(TARGETS git2
122 RUNTIME DESTINATION ${INSTALL_BIN}
123 LIBRARY DESTINATION ${INSTALL_LIB}
124 ARCHIVE DESTINATION ${INSTALL_LIB}
125 )
126 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
127 INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
128 INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
129
130 # Tests
131 IF (BUILD_TESTS)
132 SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
133 ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")
134
135 INCLUDE_DIRECTORIES(tests)
136 FILE(GLOB SRC_TEST tests/t??-*.c)
137
138 ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
139 TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
140 IF (WIN32)
141 TARGET_LINK_LIBRARIES(libgit2_test ws2_32)
142 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
143 TARGET_LINK_LIBRARIES(libgit2_test socket nsl)
144 ENDIF ()
145
146 ENABLE_TESTING()
147 ADD_TEST(libgit2_test libgit2_test)
148 ENDIF ()
149
150 IF (BUILD_CLAR)
151 FIND_PACKAGE(PythonInterp REQUIRED)
152
153 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
154 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
155 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
156
157 INCLUDE_DIRECTORIES(${CLAR_PATH})
158 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
159
160 ADD_CUSTOM_COMMAND(
161 OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
162 COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
163 DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
164 WORKING_DIRECTORY ${CLAR_PATH}
165 )
166 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
167 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT})
168 IF (WIN32)
169 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
170 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
171 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
172 ENDIF ()
173
174 ENABLE_TESTING()
175 ADD_TEST(libgit2_clar libgit2_clar)
176 ENDIF ()
177
178 IF (TAGS)
179 FIND_PROGRAM(CTAGS ctags)
180 IF (NOT CTAGS)
181 message(FATAL_ERROR "Could not find ctags command")
182 ENDIF ()
183
184 FILE(GLOB_RECURSE SRC_ALL *.[ch])
185
186 ADD_CUSTOM_COMMAND(
187 OUTPUT tags
188 COMMAND ${CTAGS} -a ${SRC_ALL}
189 DEPENDS ${SRC_ALL}
190 )
191 ADD_CUSTOM_TARGET(
192 do_tags ALL
193 DEPENDS tags
194 )
195 ENDIF ()