-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
336 lines (306 loc) · 14.1 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
cmake_minimum_required(VERSION 3.21)
cmake_policy(VERSION 3.21)
project(distplusplus)
include(FindPkgConfig)
include(CheckIncludeFileCXX)
include(CTest)
include(ExternalProject)
# see https://gitlab.kitware.com/cmake/cmake/-/issues/18090
macro(obj_propagate_targets name)
target_sources(${name} INTERFACE $<TARGET_OBJECTS:${name}>)
endmacro()
macro(copy_shim_target target src dest dep)
add_custom_command(
OUTPUT ${dest}
COMMAND cp ${src} ${dest}
DEPENDS ${dep}
)
add_custom_target(${target}__ DEPENDS ${dest})
add_dependencies(${target} ${target}__)
set_target_properties(${target} PROPERTIES IMPORTED_LOCATION ${dest})
endmacro()
option(COVERAGE "build with coverage data" OFF)
option(USE_DEFAULT_FLAGS "use default compiler flags" ON)
option(SYSTEM_GSL "use system Microsoft GSL" OFF)
option(SYSTEM_LMDBXX "use system lmdb++" OFF)
option(SYSTEM_BOOST "use system boost" ON)
option(SYSTEM_GRPC "use system protobuf & grpc" ON)
set(ZSTD ON) # can't be bothered to write proper detection logic
pkg_search_module(zstd REQUIRED IMPORTED_TARGET libzstd)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# no GNU C
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
# We don't use aliasing or interposition'
string(APPEND CMAKE_CXX_FLAGS " -fno-semantic-interposition -fstrict-aliasing -Wall")
if(COVERAGE)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "coverage requires clang")
endif()
string(PREPEND CMAKE_CXX_FLAGS "-fprofile-instr-generate -fcoverage-mapping ")
endif()
if(USE_DEFAULT_FLAGS)
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -flto=thin -fwhole-program-vtables")
string(APPEND CMAKE_EXE_LINKER_FLAGS " -fuse-ld=lld")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -flto=auto")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3 -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g3 -fno-omit-frame-pointer")
endif()
if(SANITIZE)
string(APPEND CMAKE_CXX_FLAGS " -g3 -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined -fsanitize-ignorelist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizer.supp")
endif()
pkg_search_module(lmdb REQUIRED IMPORTED_TARGET GLOBAL lmdb)
if(SYSTEM_LMDBXX)
CHECK_INCLUDE_FILE_CXX(lmdb++.h HAS_LMDBXX_H)
if(NOT HAS_LMDBXX_H)
message(FATAL_ERROR "required header lmdb++.h not found")
endif()
else()
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/lmdbxx)
endif()
if(SYSTEM_GSL)
CHECK_INCLUDE_FILE_CXX(gsl/gsl HAS_GSL_H)
if(NOT HAS_GSL_H)
message(FATAL_ERROR "required header gsl/gsl not found")
endif()
else()
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/GSL/include)
endif()
add_custom_target(protoc_ready)
if(SYSTEM_GRPC)
pkg_search_module(grpc++ REQUIRED IMPORTED_TARGET GLOBAL grpc++)
pkg_search_module(protobuf REQUIRED IMPORTED_TARGET GLOBAL protobuf>=3.15)
find_program(protoc protoc REQUIRED)
find_program(grpc_cpp_plugin grpc_cpp_plugin REQUIRED)
else()
# can't be bothered to sort the static archives
string(APPEND CMAKE_EXE_LINKER_FLAGS " -fuse-ld=lld")
ExternalProject_Add(grpc-third_party
PREFIX
${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc
SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/third_party/grpc
INSTALL_DIR
${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_INSTALL_LIBDIR=lib
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
)
set(protoc ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install/bin/protoc)
set(grpc_cpp_plugin ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install/bin/grpc_cpp_plugin)
add_dependencies(protoc_ready grpc-third_party)
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install/include)
macro(register_grpc_lib name)
add_library(${name} STATIC IMPORTED)
copy_shim_target(${name} ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install/lib/lib${name}.a ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install-fixed/lib/lib${name}.a grpc-third_party)
list(APPEND grpc_libs ${name})
endmacro()
# it was easier to create this via find | echo than think about a better solution to do it at runtime ¯\_(ツ)_/¯
# for i in $(find . -type f); do file=${i#./}; if [[ $file == *.a ]]; then file2=${file%.a}; file3=${file2#lib}; echo "register_grpc_lib($file3)"; fi; done
register_grpc_lib(cares)
register_grpc_lib(re2)
register_grpc_lib(ssl)
register_grpc_lib(crypto)
register_grpc_lib(z)
register_grpc_lib(address_sorting)
register_grpc_lib(gpr)
register_grpc_lib(grpc)
register_grpc_lib(grpc_unsecure)
register_grpc_lib(grpc++)
register_grpc_lib(grpc++_alts)
register_grpc_lib(grpc++_error_details)
register_grpc_lib(grpc++_reflection)
register_grpc_lib(grpc++_unsecure)
register_grpc_lib(grpc_plugin_support)
register_grpc_lib(grpcpp_channelz)
register_grpc_lib(upb)
register_grpc_lib(absl_log_severity)
register_grpc_lib(absl_raw_logging_internal)
register_grpc_lib(absl_spinlock_wait)
register_grpc_lib(absl_malloc_internal)
register_grpc_lib(absl_base)
register_grpc_lib(absl_throw_delegate)
register_grpc_lib(absl_scoped_set_env)
register_grpc_lib(absl_strerror)
register_grpc_lib(absl_hashtablez_sampler)
register_grpc_lib(absl_raw_hash_set)
register_grpc_lib(absl_stacktrace)
register_grpc_lib(absl_symbolize)
register_grpc_lib(absl_examine_stack)
register_grpc_lib(absl_failure_signal_handler)
register_grpc_lib(absl_debugging_internal)
register_grpc_lib(absl_demangle_internal)
register_grpc_lib(absl_leak_check)
register_grpc_lib(absl_leak_check_disable)
register_grpc_lib(absl_flags_program_name)
register_grpc_lib(absl_flags_config)
register_grpc_lib(absl_flags_marshalling)
register_grpc_lib(absl_flags_commandlineflag_internal)
register_grpc_lib(absl_flags_commandlineflag)
register_grpc_lib(absl_flags_private_handle_accessor)
register_grpc_lib(absl_flags_reflection)
register_grpc_lib(absl_flags_internal)
register_grpc_lib(absl_flags)
register_grpc_lib(absl_flags_usage_internal)
register_grpc_lib(absl_flags_usage)
register_grpc_lib(absl_flags_parse)
register_grpc_lib(absl_hash)
register_grpc_lib(absl_city)
register_grpc_lib(absl_low_level_hash)
register_grpc_lib(absl_int128)
register_grpc_lib(absl_exponential_biased)
register_grpc_lib(absl_periodic_sampler)
register_grpc_lib(absl_random_distributions)
register_grpc_lib(absl_random_seed_gen_exception)
register_grpc_lib(absl_random_seed_sequences)
register_grpc_lib(absl_random_internal_seed_material)
register_grpc_lib(absl_random_internal_pool_urbg)
register_grpc_lib(absl_random_internal_platform)
register_grpc_lib(absl_random_internal_randen)
register_grpc_lib(absl_random_internal_randen_slow)
register_grpc_lib(absl_random_internal_randen_hwaes)
register_grpc_lib(absl_random_internal_randen_hwaes_impl)
register_grpc_lib(absl_random_internal_distribution_test_util)
register_grpc_lib(absl_status)
register_grpc_lib(absl_statusor)
register_grpc_lib(absl_strings)
register_grpc_lib(absl_strings_internal)
register_grpc_lib(absl_str_format_internal)
register_grpc_lib(absl_cord_internal)
register_grpc_lib(absl_cordz_functions)
register_grpc_lib(absl_cordz_handle)
register_grpc_lib(absl_cordz_info)
register_grpc_lib(absl_cordz_sample_token)
register_grpc_lib(absl_cord)
register_grpc_lib(absl_graphcycles_internal)
register_grpc_lib(absl_synchronization)
register_grpc_lib(absl_time)
register_grpc_lib(absl_civil_time)
register_grpc_lib(absl_time_zone)
register_grpc_lib(absl_bad_any_cast_impl)
register_grpc_lib(absl_bad_optional_access)
register_grpc_lib(absl_bad_variant_access)
target_link_libraries(grpc++ INTERFACE ${grpc_libs})
add_library(protobuf STATIC IMPORTED)
copy_shim_target(protobuf ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install/lib/libprotobuf.a ${CMAKE_CURRENT_BINARY_DIR}/third_party/grpc/install-fixed/lib/libprotobuf.a grpc-third_party)
add_library(PkgConfig::grpc++ ALIAS grpc++)
add_library(PkgConfig::protobuf ALIAS protobuf)
endif()
if(SYSTEM_BOOST)
add_compile_definitions("BOOST_ALL_DYN_LINK=1")
# at least 1.70 due to zstd, no clue about the rest
find_package(Boost 1.70 REQUIRED COMPONENTS filesystem log program_options iostreams)
add_custom_target(Boost)
else()
add_custom_target(Boost_Build
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost/tools/build/src/engine/build.sh
)
add_custom_target(Boost_Build_Cleanup
BYPRODUCTS
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost-build/b2
DEPENDS
Boost_Build
COMMAND
mv
${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost/tools/build/src/engine/b2 ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost-build/b2
COMMAND
rm ${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost/tools/build/src/engine/bjam
)
add_custom_target(Boost
DEPENDS
Boost_Build_Cleanup
BYPRODUCTS
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_log.a
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_log_setup.a
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_thread.a
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_filesystem.a
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_program_options.a
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_iostreams.a
COMMAND
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost-build/b2 --prefix=${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install --with-log --with-filesystem --with-program_options --with-iostreams --stagedir=${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/stage link=static threading=multi
COMMAND
${CMAKE_CURRENT_BINARY_DIR}/third_party/boost-build/b2 --prefix=${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install --with-log --with-filesystem --with-program_options --with-iostreams --stagedir=${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/stage link=static threading=multi install
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost
)
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/include)
add_library(boost_log STATIC IMPORTED)
add_library(boost_log_setup STATIC IMPORTED)
set_property(TARGET boost_log_setup PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_log_setup.a)
add_library(boost_thread STATIC IMPORTED)
set_property(TARGET boost_thread PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_thread.a)
target_link_libraries(boost_log INTERFACE boost_log_setup boost_thread -lpthread)
add_library(Boost::log ALIAS boost_log)
set_property(TARGET boost_log PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_log.a)
add_dependencies(boost_log Boost)
add_library(boost_filesystem STATIC IMPORTED)
add_library(Boost::filesystem ALIAS boost_filesystem)
set_property(TARGET boost_filesystem PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_filesystem.a)
add_dependencies(boost_filesystem Boost)
add_library(boost_program_options STATIC IMPORTED)
add_library(Boost::program_options ALIAS boost_program_options)
set_property(TARGET boost_program_options PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_program_options.a)
add_dependencies(boost_program_options Boost)
add_library(boost_iostreams STATIC IMPORTED)
add_library(Boost::iostreams ALIAS boost_iostreams)
set_property(TARGET boost_iostreams PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost/install/lib/libboost_iostreams.a)
target_link_libraries(boost_iostreams INTERFACE PkgConfig::zstd)
add_dependencies(boost_iostreams Boost)
endif()
CHECK_INCLUDE_FILE_CXX("toml++/toml.h" HAVE_TOMLPLUSPLUS)
if(NOT HAVE_TOMLPLUSPLUS)
message(STATUS "system tomlplusplus not found, using bundled version")
include_directories(SYSTEM "third_party/tomlplusplus/include")
endif()
execute_process(
COMMAND bash -c "LANG=C ${CMAKE_CXX_COMPILER} -v 2> >(grep Target | cut -d ' ' -f 2 | xargs echo)"
OUTPUT_VARIABLE DISTPLUSPLUS_BUILD_TARGET)
string(STRIP ${DISTPLUSPLUS_BUILD_TARGET} DISTPLUSPLUS_BUILD_TARGET)
if(NOT DISTPLUSPLUS_BUILD_TARGET)
message(FATAL_ERROR "could not determine build target from $CXX -v")
endif()
add_compile_definitions("DISTPLUSPLUS_BUILD_TARGET=${DISTPLUSPLUS_BUILD_TARGET}")
include_directories("src")
include_directories(SYSTEM ${CMAKE_BINARY_DIR}/protos)
add_subdirectory("protos")
add_subdirectory("src/common")
add_subdirectory("src/client")
add_subdirectory("src/server")
macro(get_all_targets_recursive targets dir)
get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
foreach(subdir ${subdirectories})
get_all_targets_recursive(${targets} ${subdir})
endforeach()
get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
list(APPEND ${targets} ${current_targets})
# I'm not exactly happy with this
list(FILTER ${targets} EXCLUDE REGEX test)
list(FILTER ${targets} EXCLUDE REGEX coverage)
list(FILTER ${targets} EXCLUDE REGEX install)
endmacro()
get_all_targets_recursive(src_targets src/common)
get_all_targets_recursive(src_targets src/client)
get_all_targets_recursive(src_targets src/server)
get_all_targets_recursive(src_targets protos)
if(BUILD_TESTING)
add_subdirectory("test")
get_all_targets_recursive(src_targets test)
endif()
foreach(target ${src_targets})
if(NOT SYSTEM_BOOST)
add_dependencies(${target} Boost)
endif()
if(NOT SYSTEM_GRPC)
add_dependencies(${target} grpc-third_party)
endif()
add_dependencies(${target} gen-protos)
endforeach()