1 # CMake build script for the libgit2 project
3 # Building (out of source build):
4 # > mkdir build && cd build
5 # > cmake .. [-DSETTINGS=VALUE]
12 # > cmake --build . --target install
15 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
17 FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
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}")
24 # Find required dependencies
25 INCLUDE_DIRECTORIES(src include deps/http-parser)
27 FILE(GLOB SRC_HTTP deps/http-parser/*.c)
32 # Windows doesn't understand POSIX regex on its own
33 INCLUDE_DIRECTORIES(deps/regex)
34 SET(SRC_REGEX deps/regex/regex.c)
38 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
39 LINK_LIBRARIES(${ZLIB_LIBRARIES})
41 INCLUDE_DIRECTORIES(deps/zlib)
42 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
43 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
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.")
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)
57 # Platform specific compilation flags
59 # Not using __stdcall with the CRT causes problems
60 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
62 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
64 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
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")
72 SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -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")
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)
86 find_package(Threads REQUIRED)
89 ADD_DEFINITIONS(-DGIT_THREADS)
92 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
95 FILE(GLOB SRC_H include/git2/*.h)
97 # On Windows use specific platform sources
98 IF (WIN32 AND NOT CYGWIN)
99 ADD_DEFINITIONS(-DWIN32 -D_DEBUG)
100 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c)
102 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
105 # Compile and link libgit2
106 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC})
109 TARGET_LINK_LIBRARIES(git2 ws2_32)
110 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
111 TARGET_LINK_LIBRARIES(git2 socket nsl)
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)
121 RUNTIME DESTINATION ${INSTALL_BIN}
122 LIBRARY DESTINATION ${INSTALL_LIB}
123 ARCHIVE DESTINATION ${INSTALL_LIB}
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} )
132 FIND_PACKAGE(PythonInterp REQUIRED)
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}\")
140 INCLUDE_DIRECTORIES(${CLAR_PATH})
141 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
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}
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})
152 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
153 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
154 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
158 ADD_TEST(libgit2_clar libgit2_clar)
162 FIND_PROGRAM(CTAGS ctags)
164 message(FATAL_ERROR "Could not find ctags command")
167 FILE(GLOB_RECURSE SRC_ALL *.[ch])
171 COMMAND ${CTAGS} -a ${SRC_ALL}