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