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