]> git.proxmox.com Git - libgit2.git/blob - CMakeLists.txt
Don't use the PPC native SHA1 :( CMake is refusing to acknowledge the sha1ppc.S...
[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 SET(CMAKE_SYSTEM_NAME "Generic")
15
16 PROJECT(libgit2 C)
17 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
18
19 FILE(STRINGS "include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
20
21 STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"([0-9]+).*$" "\\1" LIBGIT2_VERSION_MAJOR "${GIT2_HEADER}")
22 STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_MINOR "${GIT2_HEADER}")
23 STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}")
24 SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}")
25
26 # Uncomment out the line below to use PowerPC SHA1
27 #SET(SHA1_TYPE "ppc")
28
29 # Find required dependencies
30 INCLUDE_DIRECTORIES(src include deps/http-parser)
31
32 FILE(GLOB SRC_HTTP deps/http-parser/*.c)
33
34 # Specify sha1 implementation
35 IF (SHA1_TYPE STREQUAL "ppc")
36 ADD_DEFINITIONS(-DPPC_SHA1)
37 FILE(GLOB SRC_SHA1 src/ppc/*.c src/ppc/*.S)
38 ELSE ()
39 SET (SRC_SHA1)
40 ENDIF()
41
42 IF (NOT WIN32)
43 FIND_PACKAGE(ZLIB)
44 ELSE()
45 # Windows doesn't understand POSIX regex on its own
46 INCLUDE_DIRECTORIES(deps/regex)
47 SET(SRC_REGEX deps/regex/regex.c)
48 ENDIF()
49
50 IF (ZLIB_FOUND)
51 INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
52 LINK_LIBRARIES(${ZLIB_LIBRARIES})
53 ELSE (ZLIB_FOUND)
54 INCLUDE_DIRECTORIES(deps/zlib)
55 ADD_DEFINITIONS(-DNO_VIZ -DSTDC -DNO_GZIP)
56 FILE(GLOB SRC_ZLIB deps/zlib/*.c)
57 ENDIF()
58
59 # Installation paths
60 SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
61 SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
62 SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
63
64 # Build options
65 OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
66 OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
67 OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
68 OPTION (BUILD_EXAMPLES "Build library usage example apps" OFF)
69 OPTION (TAGS "Generate tags" OFF)
70 OPTION (PROFILE "Generate profiling information" OFF)
71
72 # Platform specific compilation flags
73 IF (MSVC)
74 # Not using __stdcall with the CRT causes problems
75 OPTION (STDCALL "Buildl libgit2 with the __stdcall convention" ON)
76
77 SET(CMAKE_C_FLAGS "/W4 /MP /nologo /Zi ${CMAKE_C_FLAGS}")
78 IF (STDCALL)
79 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
80 ENDIF ()
81 SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
82 SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
83 SET(WIN_RC "src/win32/git2.rc")
84
85 # Precompiled headers
86 ELSE ()
87 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}")
88 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
89 IF (MINGW) # MinGW always does PIC and complains if we tell it to
90 STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
91 ELSE ()
92 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
93 ENDIF ()
94 IF (PROFILE)
95 SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
96 SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
97 ENDIF ()
98 ENDIF()
99
100 # Build Debug by default
101 IF (NOT CMAKE_BUILD_TYPE)
102 SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
103 ENDIF ()
104
105 #FIND_PACKAGE(OpenSSL)
106 #IF (OPENSSL_FOUND)
107 # ADD_DEFINITIONS(-DGIT_SSL)
108 # INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
109 # SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
110 #ENDIF()
111
112 IF (THREADSAFE)
113 IF (NOT WIN32)
114 find_package(Threads REQUIRED)
115 ENDIF()
116
117 ADD_DEFINITIONS(-DGIT_THREADS)
118 ENDIF()
119
120 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
121
122 # Collect sourcefiles
123 FILE(GLOB SRC_H include/git2/*.h)
124
125 # On Windows use specific platform sources
126 IF (WIN32 AND NOT CYGWIN)
127 ADD_DEFINITIONS(-DWIN32 -D_DEBUG -D_WIN32_WINNT=0x0501)
128 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c)
129 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS|Generic)")
130 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c)
131 ELSE()
132 FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c)
133 ENDIF ()
134
135 # Compile and link libgit2
136 ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
137
138 IF (WIN32)
139 TARGET_LINK_LIBRARIES(git2 ws2_32)
140 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
141 TARGET_LINK_LIBRARIES(git2 socket nsl)
142 ENDIF ()
143
144 TARGET_LINK_LIBRARIES(git2 ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
145 SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
146 SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR})
147 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY)
148
149 # Install
150 INSTALL(TARGETS git2
151 RUNTIME DESTINATION ${INSTALL_BIN}
152 LIBRARY DESTINATION ${INSTALL_LIB}
153 ARCHIVE DESTINATION ${INSTALL_LIB}
154 )
155 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig )
156 INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
157 INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
158
159 # Tests
160 IF (BUILD_CLAR)
161
162 FIND_PACKAGE(PythonInterp REQUIRED)
163
164 SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
165 SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
166 SET(CLAR_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources" CACHE PATH "Path to test resources.")
167 ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
168 ADD_DEFINITIONS(-DCLAR_RESOURCES=\"${TEST_RESOURCES}\")
169
170 INCLUDE_DIRECTORIES(${CLAR_PATH})
171 FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c ${CLAR_PATH}/clar_helpers.c ${CLAR_PATH}/testlib.c)
172
173 ADD_CUSTOM_COMMAND(
174 OUTPUT ${CLAR_PATH}/clar_main.c ${CLAR_PATH}/clar.h
175 COMMAND ${PYTHON_EXECUTABLE} clar -vtap .
176 DEPENDS ${CLAR_PATH}/clar ${SRC_TEST}
177 WORKING_DIRECTORY ${CLAR_PATH}
178 )
179 ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
180 TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES})
181 IF (WIN32)
182 TARGET_LINK_LIBRARIES(libgit2_clar ws2_32)
183 ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
184 TARGET_LINK_LIBRARIES(libgit2_clar socket nsl)
185 ENDIF ()
186
187 ENABLE_TESTING()
188 ADD_TEST(libgit2_clar libgit2_clar)
189 ENDIF ()
190
191 IF (TAGS)
192 FIND_PROGRAM(CTAGS ctags)
193 IF (NOT CTAGS)
194 message(FATAL_ERROR "Could not find ctags command")
195 ENDIF ()
196
197 FILE(GLOB_RECURSE SRC_ALL *.[ch])
198
199 ADD_CUSTOM_COMMAND(
200 OUTPUT tags
201 COMMAND ${CTAGS} -a ${SRC_ALL}
202 DEPENDS ${SRC_ALL}
203 )
204 ADD_CUSTOM_TARGET(
205 do_tags ALL
206 DEPENDS tags
207 )
208 ENDIF ()
209
210 IF (BUILD_EXAMPLES)
211 FILE(GLOB_RECURSE EXAMPLE_SRC examples/network/*.c)
212 ADD_EXECUTABLE(cgit2 ${EXAMPLE_SRC})
213 TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
214
215 ADD_EXECUTABLE(git-diff examples/diff.c)
216 TARGET_LINK_LIBRARIES(git-diff git2)
217
218 ADD_EXECUTABLE(git-general examples/general.c)
219 TARGET_LINK_LIBRARIES(git-general git2)
220
221 ADD_EXECUTABLE(git-showindex examples/showindex.c)
222 TARGET_LINK_LIBRARIES(git-showindex git2)
223 ENDIF ()