]> git.proxmox.com Git - libgit2.git/blob - CMakeLists.txt
Merge pull request #454 from brodie/parsing-fixes
[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
53 # Platform specific compilation flags
54 IF (MSVC)
55 # Not using __stdcall with the CRT causes problems
56 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
57
58 SET(CMAKE_C_FLAGS "/W4 /nologo /Zi ${CMAKE_C_FLAGS}")
59 IF (STDCALL)
60 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
61 ENDIF ()
62 # TODO: bring back /RTC1 /RTCc
63 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
64 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
65 SET(WIN_RC "src/win32/git2.rc")
66 ELSE ()
67 SET(CMAKE_C_FLAGS "-O2 -g -Wall -Wextra -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
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 ()
71 ENDIF()
72
73 # Build Debug by default
74 IF (NOT CMAKE_BUILD_TYPE)
75 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
76 ENDIF ()
77
78 IF (THREADSAFE)
79 IF (NOT WIN32)
80 find_package(Threads REQUIRED)
81 ENDIF()
82
83 ADD_DEFINITIONS(-DGIT_THREADS)
84 ENDIF()
85
86 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
87
88 # Collect sourcefiles
89 FILE(GLOB SRC_H include/git2/*.h)
90
91 # On Windows use specific platform sources
92 IF (WIN32 AND NOT CYGWIN)
93 ADD_DEFINITIONS(-DWIN32 -D_DEBUG)
94 FILE(GLOB SRC src/*.c src/transports/*.c src/win32/*.c)
95 ELSE()
96 FILE(GLOB SRC src/*.c src/transports/*.c src/unix/*.c)
97 ENDIF ()
98
99 # Compile and link libgit2
100 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${WIN_RC})
101
102 IF (WIN32)
103 TARGET_LINK_LIBRARIES(git2 ws2_32)
104 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
105 TARGET_LINK_LIBRARIES(git2 socket nsl)
106 ENDIF ()
107
108 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT})
109 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
110 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
111 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
112
113 # Install
114 INSTALL(TARGETS git2
115 RUNTIME DESTINATION ${INSTALL_BIN}
116 LIBRARY DESTINATION ${INSTALL_LIB}
117 ARCHIVE DESTINATION ${INSTALL_LIB}
118 )
119 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
120 INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
121 INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
122
123 # Tests
124 IF (BUILD_TESTS)
125 SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
126 ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")
127
128 INCLUDE_DIRECTORIES(tests)
129 FILE(GLOB SRC_TEST tests/t??-*.c)
130
131 ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP})
132 TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
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 ()
138
139 ENABLE_TESTING()
140 ADD_TEST(libgit2_test libgit2_test)
141 ENDIF ()
142
143 IF (BUILD_CLAY)
144 SET(CLAY_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
145 ADD_DEFINITIONS(-DCLAY_FIXTURE_PATH=\"${CLAY_FIXTURES}\")
146
147 INCLUDE_DIRECTORIES(tests-clay)
148 FILE(GLOB_RECURSE SRC_TEST tests-clay/*.c)
149
150 ADD_EXECUTABLE(libgit2_clay ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP})
151 TARGET_LINK_LIBRARIES(libgit2_clay ${CMAKE_THREAD_LIBS_INIT})
152 IF (WIN32)
153 TARGET_LINK_LIBRARIES(libgit2_clay ws2_32)
154 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
155 TARGET_LINK_LIBRARIES(libgit2_clay socket nsl)
156 ENDIF ()
157
158 ENABLE_TESTING()
159 ADD_TEST(libgit2_clay libgit2_clay)
160 ENDIF ()