-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
719 lines (652 loc) · 29.2 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# Copyright 2019-2024 Laurynas Biveinis
cmake_minimum_required(VERSION 3.12)
project(unodb VERSION 0.1
DESCRIPTION "unodb key-value store library"
HOMEPAGE_URL "https://github.com/laurynas-biveinis/unodb" LANGUAGES CXX)
# https://stackoverflow.com/a/62488003/80458
macro(set_bool var)
if(${ARGN})
set(${var} ON)
else()
set(${var} OFF)
endif()
endmacro()
# Condition helpers
set_bool(is_debug CMAKE_BUILD_TYPE STREQUAL "Debug")
set_bool(is_release CMAKE_BUILD_TYPE STREQUAL "Release")
set_bool(is_darwin CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set_bool(is_windows CMAKE_SYSTEM_NAME STREQUAL "Windows")
set_bool(is_arm "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64"
OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set_bool(is_apple_clang "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
set_bool(is_clang "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set_bool(is_gxx "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set_bool(is_any_clang ${is_apple_clang} OR ${is_clang})
option(STANDALONE
"Build UnoDB not for linking with outside code. Enables extra global checks")
option(MAINTAINER_MODE
"Maintainer mode: compiler warnings are fatal, IWYU required")
if(MAINTAINER_MODE)
message(STATUS "Warning diagnostics are fatal")
else()
message(STATUS "Warning diagnostics are not fatal")
endif()
set(CLANG_CXX_WARNING_FLAGS
# Warning groups
"-Wall" "-Wextra" "-Wconversion" "-Wdelete-non-virtual-dtor" "-Wdeprecated"
"-Wgnu" "-Wimplicit" "-Wloop-analysis" "-Wparentheses" "-Wpedantic"
"-Wpragmas"
"-Wself-assign" "-Wshadow-all"
# Individual warnings
"-Wabstract-vbase-init" "-Warray-bounds-pointer-arithmetic" "-Wassign-enum"
"-Watomic-implicit-seq-cst" "-Wbad-function-cast" "-Wc++2a-compat"
"-Wc++2a-extensions" "-Wcast-align" "-Wcast-qual" "-Wclass-varargs" "-Wcomma"
"-Wconditional-uninitialized" "-Wcovered-switch-default" "-Wdate-time"
"-Wdeprecated-implementations" "-Wdisabled-macro-expansion"
"-Wdouble-promotion" "-Wduplicate-decl-specifier" "-Wduplicate-enum"
"-Wduplicate-method-arg" "-Wduplicate-method-match" "-Wextra-semi-stmt"
"-Wfloat-equal" "-Wformat-pedantic" "-Wformat=2" "-Wheader-hygiene"
"-Widiomatic-parentheses" "-Wimplicit-fallthrough" "-Wmain"
"-Wmethod-signatures" "-Wmissing-noreturn" "-Wmissing-prototypes"
"-Wmissing-variable-declarations" "-Wnewline-eof" "-Wnon-virtual-dtor"
"-Wnonportable-system-include-path" "-Wold-style-cast" "-Wover-aligned"
"-Wpacked" "-Wpointer-arith" "-Wprofile-instr-missing" "-Wredundant-parens"
"-Wshift-sign-overflow" "-Wstatic-in-inline" "-Wstrict-prototypes"
"-Wsuper-class-method-mismatch" "-Wswitch-enum" "-Wtautological-compare"
"-Wtautological-constant-in-range-compare" "-Wundef"
"-Wundefined-func-template" "-Wundefined-reinterpret-cast"
"-Wunreachable-code-aggressive" "-Wunused-exception-parameter"
"-Wunused-macros" "-Wunused-member-function" "-Wunused-template"
"-Wused-but-marked-unused" "-Wvector-conversion" "-Wvla"
"-Wweak-template-vtables" "-Wweak-vtables" "-Wzero-as-null-pointer-constant")
set(CLANG_LT_13_CXX_WARNING_FLAGS "-Wreserved-id-macro")
set(CLANG_GE_13_CXX_WARNING_FLAGS "-Wreserved-identifier")
set(GCC_CXX_WARNING_FLAGS
# Warning groups
"-Wall" "-Wextra" "-Wpedantic" "-Wunused" "-Wparentheses" "-Wconversion"
# Individual warnings
"-Wabi-tag" "-Wcast-align=strict" "-Wcast-qual" "-Wcatch-value=3"
"-Wctor-dtor-privacy" "-Wdouble-promotion" "-Wduplicated-branches"
"-Wduplicated-cond" "-Wextra-semi" "-Wfloat-equal" "-Wformat-overflow=2"
"-Wformat-signedness" "-Wformat-truncation=2" "-Wformat=2"
"-Wimplicit-fallthrough=5" "-Winvalid-pch" "-Wlogical-op" "-Wmismatched-tags"
"-Wmissing-declarations" "-Wmissing-include-dirs" "-Wnoexcept"
"-Wnon-virtual-dtor" "-Wnull-dereference" "-Wold-style-cast"
"-Woverloaded-virtual" "-Wpacked" "-Wplacement-new=2" "-Wredundant-decls"
"-Wshadow=global" "-Wsign-conversion" "-Wsign-promo" "-Wstrict-null-sentinel"
"-Wstringop-truncation" "-Wsuggest-attribute=cold" "-Wsuggest-attribute=const"
"-Wsuggest-attribute=format" "-Wsuggest-attribute=malloc"
"-Wsuggest-attribute=noreturn" "-Wsuggest-attribute=pure"
"-Wsuggest-final-methods" "-Wsuggest-final-types" "-Wsuggest-override"
"-Wswitch-enum" "-Wtrampolines" "-Wundef" "-Wuninitialized"
"-Wunsafe-loop-optimizations" "-Wunused-const-variable=2" "-Wunused-macros"
"-Wuseless-cast" "-Wvector-operation-performance" "-Wvla"
"-Wzero-as-null-pointer-constant" "-Wattribute-alias=2" "-Warray-bounds=2"
"-Wredundant-tags")
set(GCC_GE_11_CXX_WARNING_FLAGS
"-Wctad-maybe-unsupported" "-Wdeprecated-enum-enum-conversion"
"-Wdeprecated-enum-float-conversion" "-Wvexing-parse")
set(GCC_GE_12_CXX_WARNING_FLAGS "-Winterference-size")
set(GCC_GE_14_CXX_WARNING_FLAGS "-Wnrvo" "-Welaborated-enum-base"
"-Wdangling-reference")
set(UNIX_CXX_FLAGS "-g")
# clang 14 produces DWARF-5 by default, which Valgrind does not support yet
set(CLANG_GE_14_CXX_FLAGS "-gdwarf-4")
option(AVX2 "Enable AVX2 instructions on x86_64" ON)
if(AVX2)
message(STATUS "Using AVX2 instructions on x86_64")
else()
message(STATUS "Using SSE4.1 instructions on x86_64")
endif()
if(MSVC)
# Remove it once CMake minimum is bumped to 3.15 or greater
string(REGEX REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
# Disable the following warnings for MSVC:
# - "C4324: '...': structure was padded due to alignment specifier"
# - "C5030: attribute '...' is not recognized"
# - "C5072, ASAN enabled without debug information emission" - triggered by
# msvc-release-asan preset
# - "C5246: '...': the initialization of a subobject should be wrapped in
# braces": billions of std::array false positives with 17.2.2.
# - The other disabled warnings only activate on /Wall
# - "C5264": "'const' variable is not used": billions of false positives with
# 17.5.
set(MSVC_CXX_WARNING_FLAGS "/Wall" "/wd4324" "/wd5030" "/wd5072"
"/wd4623" "/wd4625" "/wd4626" "/wd4582" "/wd4820" "/wd4710" "/wd4711"
"/wd4868" "/wd5026" "/wd5027" "/wd5045" "/wd5246" "/wd5264")
# Flags for both MSVC cl and clang-cl compilers
set(ANY_MSVC_CXX_FLAGS "/permissive-")
# clang-cl warning flags that cl does not understand
set(MSVC_CLANG_WARNING_FLAGS "-Wno-c++98-compat" "-Wno-c++98-compat-pedantic"
"-Wno-exit-time-destructors" "-Wno-ctad-maybe-unsupported"
"-Wno-global-constructors" "-Wno-unsafe-buffer-usage" "-Wno-switch-default")
# cl flags that clang-cl does not understand
set(MSVC_CXX_FLAGS "/external:anglebrackets" "/external:W0")
# Disable the following warnings for MSVC static analysis. I'd like to have
# them enabled, but their suggestions result in runtime overhead in release
# builds.
# - "C26429: Symbol 'inode' is never tested for nullness, it can be marked as
# not_null (f.23)."
# - "C26446: Prefer to use gsl::at() instead of unchecked subscript operator
# (bounds.4)."
# - "C26482: Only index into arrays using constant expressions (bounds.2)"
# This one seems like a false positive, of course I could be wrong
# - "C26415: Smart pointer parameter '...' is used only to access contained
# pointer. Use T* or T& instead (r.30)."
# Won't fix
# - "C26472: Don't use a static_cast for arithmetic conversions. Use brace
# initialization, gsl::narrow_cast or gsl::narrow (type.1)"
# - "C26494: Variable '...' is uninitialized. Always initialize an object
# (type.5)
set(MSVC_STATIC_ANALYSIS_FLAGS "/analyze" "/analyze:external-"
"/analyze:pluginEspXEngine.dll" "/analyze:rulesetdirectory$ENV{VSINSTALLDIR}"
"/analyze:rulesetNativeRecommendedRules.ruleset" "/wd26429" "/wd26446"
"/wd26482" "/wd26415" "/wd26472" "/wd26494" "/analyze:log:format:sarif"
"/analyze:logmsvc.sarif" "/analyze:log:compilerwarnings")
option(COVERAGE "Enable code coverage reporting")
if(COVERAGE)
if(MSVC)
message(FATAL_ERROR "MSVC is incompatible with code code coverage reporting")
endif()
option(GCOV_PATH "gcov tool location to be used by lcov")
if(GCOV_PATH)
set(LCOV_GCOV_ARG "--gcov-tool" "${GCOV_PATH}")
message(STATUS "Code coverage reporting enabled with gcov at ${GCOV_PATH}")
else()
set(LCOV_GCOV_ARG "")
message(STATUS "Code coverage reporting enabled with default gcov path")
endif()
else()
message(STATUS "Code coverage reporting not enabled")
endif()
set(SANITIZER_CXX_FLAGS "")
set(SANITIZER_LD_FLAGS "")
macro(ADD_TO_GNU_SANITIZER_FLAGS)
if(is_gxx)
list(APPEND SANITIZER_CXX_FLAGS ${ARGV})
list(APPEND SANITIZER_LD_FLAGS ${ARGV})
endif()
endmacro()
macro(SET_COMMON_SANITIZER_FLAGS)
if(NOT MSVC)
list(APPEND SANITIZER_CXX_FLAGS "-fno-omit-frame-pointer"
"-fno-optimize-sibling-calls")
endif()
endmacro()
option(SANITIZE_ADDRESS "Enable AddressSanitizer runtime checks")
if(SANITIZE_ADDRESS)
set_common_sanitizer_flags()
if(MSVC)
list(APPEND SANITIZER_CXX_FLAGS "/fsanitize=address")
# Documented to be incompatible with ASan
string(REGEX REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG}")
string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO"
CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO"
CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}")
string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO"
CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO"
CMAKE_STATIC_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS_DEBUG}")
else()
list(APPEND SANITIZER_CXX_FLAGS "-fsanitize=address")
list(APPEND SANITIZER_LD_FLAGS "-fsanitize=address")
add_to_gnu_sanitizer_flags("-fsanitize=leak"
"-fsanitize-address-use-after-scope" "-fsanitize=pointer-compare"
"-fsanitize=pointer-subtract")
endif()
string(CONCAT ASAN_ENV "ASAN_OPTIONS="
"check_initialization_order=true:detect_stack_use_after_return=true:"
"alloc_dealloc_mismatch=true:strict_string_checks=true")
if(NOT is_gxx)
# False positive on std::vector::capacity with GCC 12
string(APPEND ASAN_ENV ":detect_invalid_pointer_pairs=2")
endif()
if(is_clang OR is_gxx)
string(APPEND ASAN_ENV ":detect_leaks=1")
endif()
set(SANITIZER_ENV ${ASAN_ENV})
unset(ASAN_ENV)
else()
if(MSVC AND NOT is_clang)
string(REGEX REPLACE "/Zi" "/ZI" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
endif()
endif()
option(SANITIZE_THREAD "Enable ThreadSanitizer runtime checks")
if(SANITIZE_THREAD)
if(MSVC)
message(FATAL_ERROR "MSVC is incompatible with ThreadSanitizer")
endif()
set_common_sanitizer_flags()
list(APPEND SANITIZER_CXX_FLAGS "-fsanitize=thread")
list(APPEND SANITIZER_LD_FLAGS "-fsanitize=thread")
endif()
option(SANITIZE_UB "Enable UndefinedBehaviorSanitizer runtime checks")
if(SANITIZE_UB)
if(MSVC)
message(FATAL_ERROR "MSVC is incompatible with UndefinedBehaviorSanitizer")
endif()
set_common_sanitizer_flags()
list(APPEND SANITIZER_CXX_FLAGS "-fsanitize=undefined")
if(is_any_clang)
# It was interesting to try -fsanitize=implicit-conversion, but the expected
# conversions do not appear to be fully suppressable
list(APPEND SANITIZER_CXX_FLAGS "-fsanitize=local-bounds"
"-fsanitize=nullability")
if(is_clang AND CMAKE_CXX_COMPILE_VERSION VERSION_GREATER_EQUAL 19.0)
list(APPEND SANITIZER_CXX_FLAGS "-fsanitize=implicit-bitfield-conversion")
endif()
endif()
set(SANITIZER_LD_FLAGS "-fsanitize=undefined")
string(CONCAT UBSAN_ENV "UBSAN_OPTIONS="
"print_stacktrace=1:halt_on_error=1:abort_on_error=1")
set(SANITIZER_ENV ${UBSAN_ENV})
unset(UBSAN_ENV)
endif()
option(STATIC_ANALYSIS "Enable compiler static analysis")
if(MSVC AND is_clang)
message(STATUS "Not enabling IPO/LTO due to MSVC LLVM not supporting it")
elseif(is_clang AND SANITIZE_ADDRESS AND CMAKE_CXX_COMPILER_VERSION
VERSION_GREATER_EQUAL 17.0)
# /usr/bin/ld: error: Failed to link module
# ../libunodb_qsbr.a.llvm.3080.qsbr.cpp: Expected at most one ThinLTO module
# per bitcode file
# ...
message(STATUS
"Not enabling IPO/LTO due to LLVM clang 17 w/ AddressSanitizer bug")
else()
include(CheckIPOSupported)
check_ipo_supported(RESULT IPO_SUPPORTED OUTPUT IPO_SUPPORT_ERROR LANGUAGES CXX)
if(IPO_SUPPORTED)
message(STATUS "Enabling IPO/LTO for release config")
else()
message(STATUS "IPO/LTO is not supported: ${IPO_SUPPORT_ERROR}")
endif()
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(Boost REQUIRED)
# TODO(laurynas): once the minimum CMake version is at least 3.13, convert to a
# find_package(Boost) component check
find_file(HAS_BOOST_STACKTRACE_HPP boost/stacktrace.hpp
PATHS "${Boost_INCLUDE_DIRS}")
if(HAS_BOOST_STACKTRACE_HPP)
message(STATUS "boost/stacktrace.hpp found, using Boost.Stacktrace")
else()
message(STATUS "boost/stacktrace.hpp not found, not using Boost.Stacktrace")
endif()
string(REPLACE ";" " " CXX_FLAGS_FOR_SUBDIR_STR "${SANITIZER_CXX_FLAGS}")
if(MSVC)
string(REPLACE ";" " " MSVC_WARNING_FLAGS_FOR_SUBDIR_STR_TMP "${MSVC_CXX_WARNING_FLAGS}")
string(REPLACE "/Wall" "" MSVC_WARNING_FLAGS_FOR_SUBDIR_STR
"${MSVC_WARNING_FLAGS_FOR_SUBDIR_STR_TMP}")
endif()
string(REPLACE ";" " " LD_FLAGS_FOR_SUBDIR_STR "${SANITIZER_LD_FLAGS}")
macro(ADD_CXX_FLAGS_FOR_SUBDIR)
set(ORIG_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(ORIG_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
set(ORIG_CMAKE_MODULE_LINKER_FLAGS ${CMAKE_MODULE_LINKER_FLAGS})
set(ORIG_CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
string(APPEND CMAKE_CXX_FLAGS " " "${CXX_FLAGS_FOR_SUBDIR_STR}")
if(MSVC)
string(APPEND CMAKE_CXX_FLAGS " " "${MSVC_WARNING_FLAGS_FOR_SUBDIR_STR}")
endif()
# Change to CMAKE_CXX_COMPILER_FRONTEND_VARIANT on 3.14.
if(NOT MSVC AND is_clang
AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 14.0)
string(APPEND CMAKE_CXX_FLAGS " " "${CLANG_GE_14_CXX_FLAGS}")
endif()
string(APPEND CMAKE_EXE_LINKER_FLAGS "${LD_FLAGS_FOR_SUBDIR_STR}")
string(APPEND CMAKE_MODULE_LINKER_FLAGS "${LD_FLAGS_FOR_SUBDIR_STR}")
string(APPEND CMAKE_SHARED_LINKER_FLAGS "${LD_FLAGS_FOR_SUBDIR_STR}")
endmacro()
macro(RESTORE_CXX_FLAGS_FOR_SUBDIR)
set(CMAKE_CXX_FLAGS ${ORIG_CMAKE_CXX_FLAGS})
set(CMAKE_EXE_LINKER_FLAGS ${ORIG_CMAKE_EXE_LINKER_FLAGS})
set(CMAKE_MODULE_LINKER_FLAGS ${ORIG_CMAKE_MODULE_LINKER_FLAGS})
set(CMAKE_SHARED_LINKER_FLAGS ${ORIG_CMAKE_SHARED_LINKER_FLAGS})
endmacro()
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
ADD_CXX_FLAGS_FOR_SUBDIR()
add_subdirectory(3rd_party/googletest)
RESTORE_CXX_FLAGS_FOR_SUBDIR()
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Suppressing Google Benchmark tests"
FORCE)
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL
"Suppressing Google Benchmark installation" FORCE)
if(IPO_SUPPORTED)
if(is_debug)
# It seems that Google Benchmark does not support multi-configuration
# generators if LTO is enabled
message(STATUS "Disabling LTO for Google Benchmark due to debug build")
elseif(is_apple_clang)
message(STATUS
"Disabling LTO for Google Benchmark because Apple clang is not supported")
else()
set(BENCHMARK_ENABLE_LTO ON CACHE BOOL "Enabling LTO for Google Benchmark"
FORCE)
message(STATUS "Enabling LTO for Google Benchmark")
endif()
endif()
ADD_CXX_FLAGS_FOR_SUBDIR()
add_subdirectory(3rd_party/benchmark)
RESTORE_CXX_FLAGS_FOR_SUBDIR()
# Do not build DeepState:
# - under Windows as it's not supported
# - on ARM
# - with GCC under macOS due to https://github.com/trailofbits/deepstate/issues/374
# - under macOS with ASan or TSan enabled
if(NOT (is_darwin AND (is_gxx OR SANITIZE_ADDRESS OR SANITIZE_THREAD))
AND NOT is_windows AND NOT is_arm)
# ThreadSanitizer is not compatible with libfuzzer and LLVM 11 linker crashes
# on libfuzzer release build
if(is_clang AND NOT(is_release) AND NOT(SANITIZE_THREAD))
set(LIBFUZZER_AVAILABLE TRUE)
set(BUILD_DEEPSTATE_LIBFUZZER "-DDEEPSTATE_LIBFUZZER=ON")
else()
set(LIBFUZZER_AVAILABLE FALSE)
set(BUILD_DEEPSTATE_LIBFUZZER "-DDEEPSTATE_LIBFUZZER=OFF")
endif()
include(ExternalProject)
ExternalProject_Add(3rd_party_deepstate
SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rd_party/deepstate"
BINARY_DIR "${CMAKE_BINARY_DIR}/3rd_party/deepstate"
CMAKE_ARGS "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_C_FLAGS=-w -Wno-implicit-function-declaration"
"-DCMAKE_CXX_FLAGS=-w" "${BUILD_DEEPSTATE_LIBFUZZER}"
INSTALL_COMMAND "")
ExternalProject_Get_property(3rd_party_deepstate SOURCE_DIR)
ExternalProject_Get_property(3rd_party_deepstate BINARY_DIR)
add_library(deepstate STATIC IMPORTED)
add_dependencies(deepstate 3rd_party_deepstate)
target_include_directories(deepstate INTERFACE "${SOURCE_DIR}/src/include/")
set_target_properties(deepstate PROPERTIES IMPORTED_LOCATION
"${BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}deepstate${CMAKE_STATIC_LIBRARY_SUFFIX}")
if(LIBFUZZER_AVAILABLE)
add_library(deepstate_lf STATIC IMPORTED)
add_dependencies(deepstate_lf 3rd_party_deepstate)
target_include_directories(deepstate_lf INTERFACE
"${SOURCE_DIR}/src/include/")
set_target_properties(deepstate_lf PROPERTIES IMPORTED_LOCATION
"${BINARY_DIR}/libdeepstate_LF.a")
endif()
endif()
# Generator expression helpers
set(is_release_genex "$<CONFIG:Release>")
set(is_not_release_genex "$<NOT:${is_release_genex}>")
set(is_not_windows_x86_64 "$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},x86_64>")
set(is_windows_x86_64 "$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},AMD64>")
set(is_windows_genex "$<PLATFORM_ID:Windows>")
set(is_not_windows "$<NOT:${is_windows_genex}>")
set(is_linux "$<PLATFORM_ID:Linux>")
set(is_gxx_genex "$<CXX_COMPILER_ID:GNU>")
set(is_clang_genex "$<CXX_COMPILER_ID:Clang>")
set(is_any_clang_genex "$<CXX_COMPILER_ID:AppleClang,Clang>")
set(is_msvc "$<CXX_COMPILER_ID:MSVC>")
# This condition is incorrect due to clang-cl having clang compiler ID but
# taking MSVC flags. Change to CMAXE_CXX_COMPILER_FRONTEND_VARIANT on 3.14
set(is_clang_cl "$<AND:${is_windows_genex},${is_clang_genex}>")
set(is_any_msvc "$<OR:${is_msvc},${is_clang_cl}>")
set(is_x86_64_any_msvc "$<AND:${is_any_msvc},${is_windows_x86_64}>")
set(is_clang_not_windows "$<AND:${is_clang_genex},${is_not_windows}>")
set(cxx_ge_11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11.0>")
set(cxx_ge_12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12.0>")
set(cxx_lt_13 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,13.0>")
set(cxx_ge_13 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,13.0>")
set(cxx_ge_14 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,14.0>")
set(is_clang_lt_13_not_windows "$<AND:${is_clang_not_windows},${cxx_lt_13}>")
set(is_clang_ge_13_not_windows "$<AND:${is_clang_not_windows},${cxx_ge_13}>")
set(is_clang_ge_14_not_windows "$<AND:${is_clang_not_windows},${cxx_ge_14}>")
set(is_gxx_ge_11 "$<AND:${is_gxx_genex},${cxx_ge_11}>")
set(is_gxx_ge_12 "$<AND:${is_gxx_genex},${cxx_ge_12}>")
set(is_gxx_ge_14 "$<AND:${is_gxx_genex},${cxx_ge_14}>")
set(has_avx2 "$<BOOL:${AVX2}>")
set(has_boost_stacktrace "$<BOOL:${HAS_BOOST_STACKTRACE_HPP}>")
set(fatal_warnings_on "$<BOOL:${MAINTAINER_MODE}>")
set(coverage_on "$<BOOL:${COVERAGE}>")
set(is_standalone "$<BOOL:${STANDALONE}>")
set(is_gxx_not_release_standalone
$<AND:${is_gxx_genex},${is_not_release_genex},${is_standalone}>)
target_compile_definitions(benchmark PUBLIC
"$<${is_gxx_not_release_standalone}:_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC>")
# Add benchmark_include_dirs by target_include_directories(... SYSTEM ...)
# before target_link_libraries so that benchmark headers are included through
# -isystem not -I, resulting in build-breaking diagnostics.
get_target_property(benchmark_include_dirs benchmark::benchmark
INTERFACE_INCLUDE_DIRECTORIES)
if(NOT is_any_clang)
message(STATUS "Not using clang-tidy due to non-clang compiler being used")
set(DO_CLANG_TIDY "")
elseif(MSVC)
message(STATUS "Not using clang-tidy due to broken (?) MSVC support")
else()
find_program(CLANG_TIDY_EXE NAMES "clang-tidy"
DOC "Path to clang-tidy executable")
if(NOT CLANG_TIDY_EXE)
message(STATUS "clang-tidy not found")
set(DO_CLANG_TIDY "")
else()
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-p=${CMAKE_BINARY_DIR}")
endif()
endif()
set(CPPCHECK_ARGS
# False positive on Google Test TEST macro and compilers are much better
# for syntax checking anyway
"--suppress=syntaxError"
# Likewise
"--suppress=internalAstError"
# Leave it for the compilers to diagnose
"--suppress=unreadVariable"
# False positives on structured bindings with 2.5
"--suppress=unassignedVariable"
"--suppress=unusedVariable"
# Informational message that fails the build. Remove once cppcheck 2.11 is the
# minimum
"--suppress=normalCheckLevelMaxBranches")
option(CPPCHECK_AGGRESSIVE "Enable inconclusive cppcheck checks")
if(CPPCHECK_AGGRESSIVE)
list(APPEND CPPCHECK_ARGS "--inconclusive")
endif()
if(MAINTAINER_MODE)
list(APPEND CPPCHECK_ARGS "--error-exitcode=2")
else()
list(APPEND CPPCHECK_ARGS "--error-exitcode=0")
endif()
find_program(CPPCHECK_EXE NAMES "cppcheck" DOC "Path to cppcheck executable")
if(NOT CPPCHECK_EXE)
message(STATUS "cppcheck not found")
else()
execute_process(COMMAND "${CPPCHECK_EXE}" "--version" OUTPUT_VARIABLE
CPPCHECK_VERSION_OUTPUT)
message(STATUS
"cppcheck found: ${CPPCHECK_EXE}, --version: ${CPPCHECK_VERSION_OUTPUT}")
set(DO_CPPCHECK "${CPPCHECK_EXE}"
"--enable=warning,style,performance,portability" "-D__x86_64" "-D__GLIBCXX__"
"--inline-suppr")
list(APPEND DO_CPPCHECK "${CPPCHECK_ARGS}")
endif()
find_program(CPPLINT_EXE NAMES "cpplint" DOC "Path to cpplint executable")
if(NOT CPPLINT_EXE)
message(STATUS "cpplint not found")
else()
message(STATUS "cpplint found: ${CPPLINT_EXE}")
endif()
option(IWYU "Enable include-what-you-use checking")
if(IWYU OR MAINTAINER_MODE)
if(NOT is_any_clang OR is_windows)
message(STATUS
"Not using include-what-you-use due to non-clang compiler or Windows being used")
else()
if(MAINTAINER_MODE AND NOT IWYU)
message(STATUS
"Forcing include-what-you-use because maintainer mode is on")
endif()
find_program(IWYU_EXE NAMES "include-what-you-use"
DOC "Path to include-what-you-use executable")
if(NOT IWYU_EXE)
if(MAINTAINER_MODE)
message(FATAL_ERROR "include-what-you-use not found")
else()
message(STATUS "include-what-you-use not found")
endif()
else()
execute_process(COMMAND "${IWYU_EXE}" "--version" OUTPUT_VARIABLE
IWYU_VERSION_OUTPUT)
message(STATUS
"include-what-you-use found: ${IWYU_EXE}, --version: ${IWYU_VERSION_OUTPUT}")
set(DO_IWYU "${IWYU_EXE}")
endif()
endif()
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(GSL_INCLUDES "3rd_party/GSL/include")
function(COMMON_TARGET_PROPERTIES TARGET)
cmake_parse_arguments(PARSE_ARGV 1 CTP "SKIP_CHECKS" "" "")
target_compile_features(${TARGET} PUBLIC cxx_std_17)
set_target_properties(${TARGET} PROPERTIES CXX_EXTENSIONS OFF)
target_compile_definitions(${TARGET} PRIVATE
"$<${is_standalone}:UNODB_DETAIL_STANDALONE>"
"$<${has_boost_stacktrace}:UNODB_DETAIL_BOOST_STACKTRACE>")
target_compile_options(${TARGET} PRIVATE
"${CXX_FLAGS}" "${SANITIZER_CXX_FLAGS}"
"$<${is_msvc}:${MSVC_CXX_FLAGS}>"
"$<${is_any_msvc}:${ANY_MSVC_CXX_FLAGS}>"
"$<${is_not_windows}:${UNIX_CXX_FLAGS}>"
"$<${is_clang_ge_14_not_windows}:${CLANG_GE_14_CXX_FLAGS}>"
# Architecture
"$<${is_x86_64_any_msvc}:$<IF:${has_avx2},/arch:AVX2,/arch:AVX>>"
"$<${is_not_windows_x86_64}:$<IF:${has_avx2},-mavx2,-msse4.1>>"
# Warnings
"$<${fatal_warnings_on}:$<IF:${is_any_msvc},/WX,-Werror>>"
"$<${is_any_msvc}:${MSVC_CXX_WARNING_FLAGS}>"
"$<${is_clang_cl}:${MSVC_CLANG_WARNING_FLAGS}>"
"$<$<AND:${is_any_clang_genex},${is_not_windows}>:${CLANG_CXX_WARNING_FLAGS}>"
"$<${is_clang_lt_13_not_windows}:${CLANG_LT_13_CXX_WARNING_FLAGS}>"
"$<${is_clang_ge_13_not_windows}:${CLANG_GE_13_CXX_WARNING_FLAGS}>"
"$<${is_gxx_genex}:${GCC_CXX_WARNING_FLAGS}>"
"$<${is_gxx_ge_11}:${GCC_GE_11_CXX_WARNING_FLAGS}>"
"$<${is_gxx_ge_12}:${GCC_GE_12_CXX_WARNING_FLAGS}>"
"$<${is_gxx_ge_14}:${GCC_GE_14_CXX_WARNING_FLAGS}>"
# Optimization
"$<${is_not_release_genex}:$<IF:${is_windows_genex},/Od,-O0>>"
"$<$<AND:${is_release_genex},${is_not_windows}>:$<IF:${coverage_on},-O0,-O3>>"
# Misc
"$<${coverage_on}:--coverage>")
# Change to target_link_options on 3.13 minimum CMake version
target_link_libraries(${TARGET} INTERFACE "$<${coverage_on}:--coverage>")
target_link_libraries(${TARGET} PRIVATE
"${SANITIZER_LD_FLAGS}"
"$<${is_linux}:${CMAKE_DL_LIBS}>"
"$<$<AND:${is_linux},${is_gxx_genex},${has_boost_stacktrace}>:backtrace>")
if(IPO_SUPPORTED)
set_target_properties(${TARGET} PROPERTIES
INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
endif()
if(NOT CTP_SKIP_CHECKS)
if(CPPCHECK_EXE)
set_target_properties(${TARGET} PROPERTIES CXX_CPPCHECK "${DO_CPPCHECK}")
endif()
if(CPPLINT_EXE)
set_target_properties(${TARGET} PROPERTIES CXX_CPPLINT "${CPPLINT_EXE}")
endif()
if(IWYU_EXE)
set_target_properties(${TARGET} PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "${DO_IWYU}")
endif()
if(STATIC_ANALYSIS)
# The condition below is incorrect due to clang-cl having clang compiler
# ID but taking MSVC flags. Change to CMAKE_CXX_COMPILER_FRONTEND_VARIANT
# on 3.14.
target_compile_options(${TARGET} PRIVATE
"$<IF:${is_msvc},${MSVC_STATIC_ANALYSIS_FLAGS},-fanalyzer>"
# GCC 12 -fanalyzer not fully ready for C++ yet
"$<${is_gxx_ge_11}:-Wno-analyzer-null-dereference>"
"$<${is_gxx_ge_11}:-Wno-analyzer-possible-null-argument>"
"$<${is_gxx_ge_11}:-Wno-analyzer-possible-null-dereference>"
"$<${is_gxx_ge_11}:-Wno-analyzer-malloc-leak>"
"$<${is_gxx_ge_11}:-Wno-analyzer-null-argument>")
endif()
endif()
endfunction()
function(SET_CLANG_TIDY_OPTIONS TARGET COMMAND)
if(DO_CLANG_TIDY)
set_target_properties(${TARGET} PROPERTIES CXX_CLANG_TIDY "${COMMAND}")
endif()
endfunction()
function(ADD_UNODB_LIBRARY LIB)
add_library(${LIB} ${ARGN})
common_target_properties(${LIB})
target_include_directories(${LIB} SYSTEM PUBLIC "${GSL_INCLUDES}")
set_clang_tidy_options(${LIB} "${DO_CLANG_TIDY}")
if(LIBFUZZER_AVAILABLE)
set(LIB_LF "${LIB}_lf")
add_library(${LIB_LF} ${ARGN})
common_target_properties(${LIB_LF} SKIP_CHECKS)
target_include_directories(${LIB_LF} SYSTEM PUBLIC "${GSL_INCLUDES}")
target_compile_options(${LIB_LF} PRIVATE "-fsanitize=fuzzer-no-link")
endif()
endfunction()
add_library(unodb_util INTERFACE)
target_include_directories(unodb_util INTERFACE ".")
target_include_directories(unodb_util SYSTEM INTERFACE "${Boost_INCLUDE_DIRS}")
add_unodb_library(unodb_qsbr qsbr.cpp qsbr.hpp qsbr_ptr.cpp qsbr_ptr.hpp)
target_include_directories(unodb_qsbr SYSTEM PUBLIC "${Boost_INCLUDE_DIRS}")
target_link_libraries(unodb_qsbr PRIVATE "${Boost_LIBRARIES}")
target_link_libraries(unodb_qsbr PUBLIC unodb_util Threads::Threads)
if(LIBFUZZER_AVAILABLE)
target_include_directories(unodb_qsbr_lf SYSTEM PUBLIC "${Boost_INCLUDE_DIRS}")
target_link_libraries(unodb_qsbr_lf PRIVATE "${Boost_LIBRARIES}")
target_link_libraries(unodb_qsbr_lf PUBLIC unodb_util Threads::Threads)
endif()
add_unodb_library(unodb art.cpp art.hpp art_common.cpp art_common.hpp
mutex_art.hpp optimistic_lock.hpp art_internal_impl.hpp olc_art.hpp
olc_art.cpp art_internal.cpp art_internal.hpp node_type.hpp)
target_link_libraries(unodb PUBLIC unodb_util unodb_qsbr)
if(LIBFUZZER_AVAILABLE)
target_link_libraries(unodb_lf PUBLIC unodb_util unodb_qsbr_lf)
endif()
set(VALGRIND_COMMAND "valgrind" "--error-exitcode=1" "--leak-check=full"
"--trace-children=yes" "-v")
add_custom_target(valgrind DEPENDS valgrind_tests valgrind_benchmarks)
enable_testing()
add_unodb_library(unodb_test test_heap.cpp)
target_link_libraries(unodb_test PUBLIC unodb_util)
target_link_libraries(unodb_test PRIVATE "${Boost_LIBRARIES}")
function(add_sanitized_test)
cmake_parse_arguments(AST "" "NAME" "COMMAND" ${ARGN})
add_test(NAME "${AST_NAME}" COMMAND ${AST_COMMAND})
if(SANITIZE_ADDRESS OR SANITIZE_THREAD OR SANITIZE_UB)
set_property(TEST "${AST_NAME}" APPEND PROPERTY ENVIRONMENT
"${SANITIZER_ENV}")
endif()
endfunction()
add_subdirectory(benchmark)
if(TARGET deepstate)
add_subdirectory(fuzz_deepstate)
add_dependencies(valgrind valgrind_deepstate)
endif()
add_subdirectory(test)
set(BIN_DIR_CDB "${CMAKE_BINARY_DIR}/compile_commands.json")
set(SRC_DIR_CDB "${CMAKE_SOURCE_DIR}/compile_commands.json")
# cmake -E create_symlink has Windows implementation, but additional permissions
# are required.
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
${BIN_DIR_CDB} ${SRC_DIR_CDB})
endif()
message(STATUS "User-set CMake options:")
message(STATUS "STANDALONE: ${STANDALONE}")
message(STATUS "MAINTAINER_MODE: ${MAINTAINER_MODE}")
message(STATUS "AVX2: ${AVX2}")
message(STATUS "COVERAGE: ${COVERAGE}")
message(STATUS "GCOV_PATH: ${GCOV_PATH}")
message(STATUS "SANITIZE_ADDRESS: ${SANITIZE_ADDRESS}")
message(STATUS "SANITIZE_THREAD: ${SANITIZE_THREAD}")
message(STATUS "SANITIZE_UB: ${SANITIZE_UB}")
message(STATUS "STATIC_ANALYSIS: ${STATIC_ANALYSIS}")
message(STATUS "CPPCHECK_AGGRESSIVE: ${CPPCHECK_AGGRESSIVE}")
message(STATUS "IWYU: ${IWYU}")