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