forked from mtl1979/Bittorium
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
354 lines (320 loc) · 14.4 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
cmake_minimum_required(VERSION 3.7)
cmake_policy(SET CMP0048 NEW)
set(VERSION "0.1")
# $Format:Packaged from commit %H%nset(COMMIT %h)%nset(REFS "%d")$
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CONFIGURATION_TYPES "Debug;RelWithDebInfo;Release" CACHE TYPE STRING INTERNAL)
set(CMAKE_SKIP_INSTALL_RULES OFF)
set(CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY ON)
set(CMAKE_SUPPRESS_REGENERATION ON)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(DO_TESTS ON CACHE STRING "Spend time making tests? Defaults to ON")
set(ENABLE_AVX OFF CACHE STRING "Enable RocksDB AVX/AVX2? Defaults to OFF")
if(DO_TESTS)
message(STATUS "BUILD TESTS: enabled")
enable_testing()
else()
message(STATUS "BUILD TESTS: disabled")
endif()
# copy CTestCustom.cmake to build dir to disable long running tests in 'make test'
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_CURRENT_BINARY_DIR})
# parse project version from src/version.h.in
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in" VERSION_FILE)
string(REGEX REPLACE ".*APP_VER_MAJOR ([0-9]+).*" "" DUMMY ${VERSION_FILE})
set(VER_MAJOR ${CMAKE_MATCH_1})
string(REGEX REPLACE "APP_VER_MINOR ([0-9]+)" "" DUMMY ${VERSION_FILE})
set(VER_MINOR ${CMAKE_MATCH_1})
string(REGEX REPLACE "APP_VER_REV ([0-9]+)" "" DUMMY ${VERSION_FILE})
set(VER_PATCH ${CMAKE_MATCH_1})
string(REGEX REPLACE "APP_VER_BUILD ([0-9]+)" "" DUMMY ${VERSION_FILE})
set(VER_TWEAK ${CMAKE_MATCH_1})
unset(DUMMY)
message("Version: ${VER_MAJOR}.${VER_MINOR}.${VER_PATCH}.${VER_TWEAK}")
project(Talleo VERSION ${VER_MAJOR}.${VER_MINOR}.${VER_PATCH}.${VER_TWEAK} LANGUAGES C CXX ASM)
include_directories(include src external "${CMAKE_CURRENT_BINARY_DIR}/version")
if(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD.*|FreeBSD")
set(FREEBSD TRUE)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "DragonFly.*")
set(DRAGONFLY TRUE)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
set(OPENBSD TRUE)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "NetBSD")
set(NETBSD TRUE)
endif()
if(APPLE)
include_directories(SYSTEM /usr/include/malloc)
enable_language(ASM)
endif()
if(MSVC)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Windows)
elseif(APPLE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/OSX)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Posix)
elseif(ANDROID)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Android)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Posix)
elseif(FREEBSD OR NETBSD OR OPENBSD)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/FreeBSD)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Posix)
else()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Linux)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Platform/Posix)
endif()
set(STATIC ${MSVC} CACHE BOOL "Link libraries statically")
if(MSVC)
add_definitions("/bigobj" "/MP" "/W3" "/GS-" "/D_CRT_SECURE_NO_WARNINGS" "/wd4996" "/wd4345" "/D_WIN32_WINNT=0x0600" "/DWIN32_LEAN_AND_MEAN" "/DGTEST_HAS_TR1_TUPLE=0" "/D_VARIADIC_MAX=8" "/D__SSE4_1__")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10485760")
if(STATIC)
foreach(VAR CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE)
string(REPLACE "/MD" "/MT" ${VAR} "${${VAR}}")
endforeach()
endif()
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/msc)
else()
if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
# clang for aarch64 (Android or Linux) doesn't like variable-length arrays allocated on stack
add_definitions("-DFORCE_USE_HEAP")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "ppc.*")
# gcc for powerpc64 doesn't like variable-length arrays allocated on stack
add_definitions("-DFORCE_USE_HEAP")
endif()
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
# This option has no effect in glibc version less than 2.20.
# Since glibc 2.20 _BSD_SOURCE is deprecated, this macro is recomended instead
add_definitions("-D_DEFAULT_SOURCE" "-D_GNU_SOURCE")
endif()
set(ARCH native CACHE STRING "CPU to build for: -march/-mcpu value or default")
if("${ARCH}" STREQUAL "default")
set(ARCH_FLAG "")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc.*")
set(ARCH_FLAG "-mcpu=${ARCH}")
else()
set(ARCH_FLAG "-march=${ARCH}")
endif()
set(WARNINGS "-Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Wno-error=extra -Wno-error=unused-function -Wno-error=deprecated-declarations -Wno-error=sign-compare -Wno-error=strict-aliasing -Wno-error=type-limits -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized -Wno-error=unused-result")
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(WARNINGS "${WARNINGS} -Wno-error=mismatched-tags -Wno-error=null-conversion -Wno-overloaded-shift-op-parentheses -Wno-error=shift-count-overflow -Wno-error=tautological-constant-out-of-range-compare -Wno-error=unused-private-field -Wno-error=unneeded-internal-declaration -Wno-error=unused-function -Wno-error=missing-braces")
elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
set(WARNINGS "${WARNINGS} -Wno-error=maybe-uninitialized -Wno-error=clobbered -Wno-error=unused-but-set-variable -diag-disable=10441 -diag-disable 11074 -diag-disable 11076")
else()
set(WARNINGS "${WARNINGS} -Wlogical-op -Wno-error=maybe-uninitialized -Wno-error=clobbered -Wno-error=unused-but-set-variable")
endif()
if(MINGW)
set(WARNINGS "${WARNINGS} -Wno-error=unused-value")
set(MINGW_FLAG "-DWIN32_LEAN_AND_MEAN")
include_directories(SYSTEM src/platform/mingw)
else()
set(MINGW_FLAG "")
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 5.1))
set(WARNINGS "${WARNINGS} -Wno-error=odr")
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
set(WARNINGS "${WARNINGS} -Wno-deprecated-declarations")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
set(CXX_WARNINGS "${CXX_WARNINGS} -Wno-unused-private-field -Wno-unused-lambda-capture")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
set(CXX_WARNINGS "${CXX_WARNINGS} -Wno-class-memaccess")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 9.0)
set(CXX_WARNINGS "${CXX_WARNINGS} -Wno-redundant-move")
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Intel")
set(C_WARNINGS "-Wold-style-definition -Wstrict-prototypes")
else()
set(C_WARNINGS "-Waggregate-return -Wnested-externs -Wold-style-definition -Wstrict-prototypes")
endif()
set(CXX_WARNINGS "${CXX_WARNINGS} -Wno-reorder -Wno-missing-field-initializers")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 ${MINGW_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG}")
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes")
endif()
if(NOT APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG}")
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
endif()
if(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_HAS_TR1_TUPLE=0")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 18.0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBOOST_NO_CXX98_FUNCTION_BASE")
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8))
set(DEBUG_FLAGS "-g3 -Og -gdwarf-4 -fvar-tracking -fvar-tracking-assignments -fno-inline -fno-omit-frame-pointer")
else()
set(DEBUG_FLAGS "-g3 -O0 -fno-omit-frame-pointer")
endif()
set(RELEASE_FLAGS "-Ofast -DNDEBUG -Wno-unused-variable")
if(NOT APPLE)
# There is a clang bug that does not allow to compile code that uses AES-NI intrinsics if -flto is enabled
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux"
AND CMAKE_BUILD_TYPE STREQUAL "Release" AND ((CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9) OR (CMAKE_C_COMPILER_VERSION VERSION_EQUAL 4.9)))
# On linux, to build in lto mode, check that ld.gold linker is used: 'update-alternatives --install /usr/bin/ld ld /usr/bin/ld.gold HIGHEST_PRIORITY'
set(CMAKE_AR gcc-ar)
set(CMAKE_RANLIB gcc-ranlib)
endif()
#set(RELEASE_FLAGS "${RELEASE_FLAGS} -flto")
endif()
#if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT MINGW)
# set(RELEASE_FLAGS "${RELEASE_FLAGS} -fno-fat-lto-objects")
#endif()
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${DEBUG_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${DEBUG_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${RELEASE_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${RELEASE_FLAGS}")
if(STATIC)
# STATIC already configures most deps to be linked in statically,
# here we make more deps static if the platform permits it
if(MINGW)
# On Windows, this is as close to fully-static as we get:
# this leaves only deps on /c/Windows/system32/*.dll
set(STATIC_FLAGS "-static")
elseif(NOT (APPLE OR FREEBSD OR OPENBSD OR DRAGONFLY))
# On Linux, we don't support fully static build, but these can be static
set(STATIC_FLAGS "-static-libgcc -static-libstdc++")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STATIC_FLAGS} ")
endif()
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT ANDROID)
set(CMAKE_EXE_LINKER_FLAGS "-pthread")
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Intel")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -diag-disable=10441")
endif()
set(OLD_LIB_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(STATIC)
if(STATIC AND MINGW)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
set(Boost_USE_STATIC_LIBS ON)
if(MSVC OR ANDROID)
set(Boost_USE_STATIC_RUNTIME ON)
endif()
set(RocksDB_USE_STATIC_LIBS ON)
set(MiniUPnPc_USE_STATIC_LIBS ON)
set(GFlags_USE_STATIC_LIBS ON)
endif()
if(APPLE)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
endif()
find_package(Boost 1.58 REQUIRED COMPONENTS system filesystem thread date_time chrono regex serialization program_options)
add_definitions("-DBOOST_BIND_GLOBAL_PLACEHOLDERS")
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_LIB_SUFFIXES})
if(NOT Boost_FOUND)
die("Could not find Boost libraries, please make sure you have installed Boost or libboost-all-dev (1.65) or the equivalent")
endif()
if(MINGW)
set(Boost_LIBRARIES "${Boost_LIBRARIES};ws2_32;mswsock")
elseif(APPLE OR OPENBSD OR ANDROID)
set(Boost_LIBRARIES "${Boost_LIBRARIES}")
elseif(NOT MSVC)
set(Boost_LIBRARIES "${Boost_LIBRARIES};rt")
endif()
# Force find_package(OpenSSL) to search the static library
if(STATIC)
set(OPENSSL_USE_STATIC_LIBS ON)
endif()
if(MSVC)
set(OPENSSL_MSVC_STATIC_RT ON)
endif()
# We have to look for Homebrew OpenSSL a bit differently
# Borrowed from https://github.com/tarantool/tarantool/commit/6eab201af1843f53a833c8928dc58fceffa08147
if(APPLE)
find_program(HOMEBREW_EXECUTABLE brew)
execute_process(COMMAND ${HOMEBREW_EXECUTABLE} --prefix openssl
OUTPUT_VARIABLE HOMEBREW_OPENSSL
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(DEFINED HOMEBREW_OPENSSL)
if(NOT DEFINED OPENSSL_ROOT_DIR)
message(STATUS "Setting OpenSSL root to ${HOMEBREW_OPENSSL}")
set(OPENSSL_ROOT_DIR "${HOMEBREW_OPENSSL}")
endif()
endif()
endif()
find_package(OpenSSL)
if(NOT OPENSSL_FOUND)
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_search_module(OPENSSL openssl)
endif()
endif()
if(OPENSSL_FOUND)
## On non MSVC build systems except NetBSD, we need to link dl with the static OpenSSL library
if(CMAKE_DL_LIBS)
if(NOT CMAKE_DL_LIBS IN_LIST OPENSSL_LIBRARIES)
set(OPENSSL_LIBRARIES "${OPENSSL_LIBRARIES};${CMAKE_DL_LIBS}")
endif()
endif()
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
message(STATUS "OpenSSL Found: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "OpenSSL Libraries: ${OPENSSL_LIBRARIES}")
else()
message(STATUS "OpenSSL Found: No... Skipping...")
endif()
if(MINGW)
set(EXTRA_LIBRARIES mswsock;ws2_32;iphlpapi)
elseif(APPLE OR OPENBSD OR ANDROID)
set(EXTRA_LIBRARIES "")
elseif(FREEBSD)
set(EXTRA_LIBRARIES execinfo)
elseif(DRAGONFLY)
find_library(COMPAT compat)
set(EXTRA_LIBRARIES execinfo ${COMPAT})
elseif(NOT MSVC)
find_library(RT rt)
set(EXTRA_LIBRARIES ${RT})
endif()
list(APPEND EXTRA_LIBRARIES ${CMAKE_DL_LIBS})
set(COMMIT_ID_IN_VERSION ON CACHE BOOL "Include commit ID in version")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/version")
if(NOT COMMIT_ID_IN_VERSION)
set(VERSION "${VERSION}-unknown")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version/version.h")
add_custom_target(version ALL)
elseif(DEFINED COMMIT)
string(REPLACE "." "\\." VERSION_RE "${VERSION}")
if(NOT REFS MATCHES "(\\(|, )tag: v${VERSION_RE}(\\)|, )")
set(VERSION "${VERSION}-g${COMMIT}")
endif()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version/version.h")
add_custom_target(version ALL)
else()
find_package(Git QUIET)
if(Git_FOUND OR GIT_FOUND)
message(STATUS "Found Git: ${GIT_EXECUTABLE}")
add_custom_target(version ALL "${CMAKE_COMMAND}" "-D" "VERSION=${VERSION}" "-D" "GIT=${GIT_EXECUTABLE}" "-D" "TO=${CMAKE_CURRENT_BINARY_DIR}/version/version.h" "-P" "${CMAKE_CURRENT_SOURCE_DIR}/src/version.cmake" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
else()
message(STATUS "WARNING: Git was not found!")
set(VERSION "${VERSION}-unknown")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version/version.h")
add_custom_target(version ALL)
endif()
endif()
find_package(RocksDB)
if(ROCKSDB_FOUND)
include_directories(SYSTEM ${ROCKSDB_INCLUDE_DIRS})
endif()
find_package(MiniUPnPc)
add_subdirectory(external)
add_subdirectory(src)
if(DO_TESTS)
add_subdirectory(tests)
endif()
## We need to setup the RocksDB build environment to match our system
if(NOT ROCKSDB_FOUND AND NOT MSVC)
execute_process(
COMMAND cmake ${CMAKE_CURRENT_SOURCE_DIR}/external/rocksdb -DWITH_GFLAGS=0 -B${PROJECT_BINARY_DIR}/rocksdb
)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_BINARY_DIR}/rocksdb/librocksdb.a")
endif()