]> git.proxmox.com Git - libgit2.git/blame - CMakeLists.txt
Use lowercase names for Windows headers
[libgit2.git] / CMakeLists.txt
CommitLineData
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
14PROJECT(libgit2 C)
15CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
16
96fab093 17FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
9d1dcca2
VM
18
19STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
20STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
21STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
22SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
23
7cbdaf7f 24# Find required dependencies
b8a8191f
CMN
25INCLUDE_DIRECTORIES(src include deps/http-parser)
26
27FILE(GLOB SRC_HTTP deps/http-parser/*.c)
28
1f4f4d17
TH
29IF (NOT WIN32)
30 FIND_PACKAGE(ZLIB)
c17b1d00
CMN
31ELSE()
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
35ENDIF()
36
37IF (ZLIB_FOUND)
38 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
39 LINK_LIBRARIES(${ZLIB_LIBRARIES})
40ELSE (ZLIB_FOUND)
41 INCLUDE_DIRECTORIES(deps/zlib)
42 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
43 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
44ENDIF()
7cbdaf7f 45
5b8bb8e7 46# Installation paths
73c46d53
PD
47SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
48SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
032db4d0 49SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
5b8bb8e7
PD
50
51# Build options
f0890fcc 52OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
70236bab 53OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
fd29cd13 54OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
62986ff6 55OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
e632f687 56OPTION (TAGS "Generate tags" OFF)
706a9974 57OPTION (PROFILE "Generate profiling information" OFF)
5b8bb8e7 58
502dd2da 59# Platform specific compilation flags
dcd62cb2 60IF (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 73ELSE ()
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 83ENDIF()
84
3d96996d 85# Build Debug by default
583cf169 86IF (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
88ENDIF ()
89
70236bab
VM
90IF (THREADSAFE)
91 IF (NOT WIN32)
92 find_package(Threads REQUIRED)
93 ENDIF()
94
95 ADD_DEFINITIONS(-DGIT_THREADS)
96ENDIF()
97
678e9e04
VM
98ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
99
583cf169 100# Collect sourcefiles
71d33382 101FILE(GLOB SRC_H include/git2/*.h)
583cf169
PD
102
103# On Windows use specific platform sources
5b8bb8e7 104IF (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)
107ELSEIF (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 109ELSE()
3a5ad90a 110 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
5b8bb8e7
PD
111ENDIF ()
112
583cf169 113# Compile and link libgit2
c17b1d00 114ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC})
39cdf272
CMN
115
116IF (WIN32)
117 TARGET_LINK_LIBRARIES(git2 ws2_32)
e944cfa9
CMN
118ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
119 TARGET_LINK_LIBRARIES(git2 socket nsl)
39cdf272
CMN
120ENDIF ()
121
953e1f93 122TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT})
9d1dcca2
VM
123SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
124SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
4fd486e0 125CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
583cf169
PD
126
127# Install
932d1baf 128INSTALL(TARGETS git2
86a459a8
CB
129 RUNTIME DESTINATION ${INSTALL_BIN}
130 LIBRARY DESTINATION ${INSTALL_LIB}
131 ARCHIVE DESTINATION ${INSTALL_LIB}
583cf169 132)
4fd486e0 133INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
71d33382
VM
134INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
135INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
583cf169
PD
136
137# Tests
3fd1520c 138IF (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 167ENDIF ()
e632f687
CB
168
169IF (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 )
186ENDIF ()
62986ff6
SG
187
188IF (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)
201ENDIF ()