forked from yugabyte/yugabyte-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
861 lines (733 loc) · 34.3 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# The following only applies to changes made to this file as part of YugaByte development.
#
# Portions Copyright (c) YugaByte, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations
# under the License.
#
cmake_minimum_required(VERSION 3.17.3)
project(YugabyteDB)
if("$CACHE{YB_PCH_ON}" STREQUAL "")
if("$ENV{YB_USE_PCH}" STREQUAL "1")
set(YB_USE_PCH ON)
else()
set(YB_USE_PCH OFF)
endif()
set(YB_PCH_ON ${YB_USE_PCH} CACHE BOOL "Whether to use precompiled headers" FORCE)
endif()
message("-- CMAKE_SYSTEM_INFO_FILE: ${CMAKE_SYSTEM_INFO_FILE}")
message("-- CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message("-- CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
message("-- CMAKE_SYSTEM: ${CMAKE_SYSTEM}")
message("-- CMAKE_VERSION: ${CMAKE_VERSION}")
message("-- PRECOMPILED HEADERS: ${YB_PCH_ON}")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_standard_modules")
include(YugabyteFunctions)
yb_initialize_constants()
include(YugabyteTesting)
ADD_CXX_FLAGS("-Werror")
ADD_CXX_FLAGS("-fno-strict-aliasing -Wall")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$")
# Certain platforms such as ARM do not use signed chars by default
# which causes issues with certain bounds checks.
ADD_CXX_FLAGS("-fsigned-char")
# Turn off fp-contract on aarch64 to avoid multiply-add operation result difference.
ADD_CXX_FLAGS("-ffp-contract=off")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
ADD_CXX_FLAGS("-msse4.2")
else()
message(FATAL_ERROR "Unsupported CPU architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
# Optimize for Graviton on Linux/aarch64 (not mac/arm64)
ADD_CXX_FLAGS("-march=armv8.2-a+fp16+rcpc+dotprod+crypto")
ADD_CXX_FLAGS("-mtune=neoverse-n1")
ADD_CXX_FLAGS("-mno-outline-atomics")
endif()
if (NOT "$ENV{YB_TARGET_ARCH}" STREQUAL "" AND
NOT "$ENV{YB_TARGET_ARCH}" STREQUAL "${CMAKE_SYSTEM_PROCESSOR}")
message(FATAL_ERROR "YB_TARGET_ARCH is set to $ENV{YB_TARGET_ARCH} but CMAKE_SYSTEM_PROCESSOR "
"is ${CMAKE_SYSTEM_PROCESSOR}.")
endif()
set(ENV{YB_TARGET_ARCH} "${CMAKE_SYSTEM_PROCESSOR}")
ADD_CXX_FLAGS("-Winvalid-pch")
ADD_CXX_FLAGS("-pthread -DBOOST_BIND_NO_PLACEHOLDERS")
ADD_LINKER_FLAGS("-pthread")
if (NOT APPLE)
ADD_CXX_FLAGS("-DBOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX")
endif()
ADD_CXX_FLAGS("-DROCKSDB_PLATFORM_POSIX")
ADD_CXX_FLAGS("-DBOOST_ERROR_CODE_HEADER_ONLY")
# CMake's INTERNAL type of cache variables implies FORCE, overwriting existing entries.
# https://cmake.org/cmake/help/latest/command/set.html
set(YB_NUM_TESTS "0" CACHE INTERNAL "Number of tests")
set(YB_NUM_INCLUDED_TESTS "0" CACHE INTERNAL "Number of included tests")
set(YB_NUM_EXECUTABLES "0" CACHE INTERNAL "Number of executables")
set(YB_NUM_INCLUDED_EXECUTABLES "0" CACHE INTERNAL "Number of included executables")
set(YB_ALL_DEPS "" CACHE INTERNAL "All dependencies")
# This is used to let the add_executable wrapper know if we're adding a test.
set(YB_ADDING_TEST_EXECUTABLE "FALSE" CACHE INTERNAL "")
if(NOT DEFINED YB_BUILD_TESTS)
message("Will build tests by default")
set(YB_BUILD_TESTS ON)
endif()
yb_put_vars_into_cache(YB_BUILD_TESTS)
message("YB_BUILD_TESTS=${YB_BUILD_TESTS}")
if(NOT DEFINED YB_BUILD_FUZZ_TARGETS)
message("Will not build fuzz targets by default")
set(YB_BUILD_FUZZ_TARGETS OFF)
endif()
yb_put_vars_into_cache(YB_BUILD_FUZZ_TARGETS)
message("YB_BUILD_FUZZ_TARGETS=${YB_BUILD_FUZZ_TARGETS}")
parse_build_root_basename()
if("${YB_BUILD_TYPE}" STREQUAL "")
message(FATAL_ERROR "YB_BUILD_TYPE still not set after parse_build_root_basename")
endif()
message("YB_BUILD_TYPE: ${YB_BUILD_TYPE}")
message("YB_LINKING_TYPE: ${YB_LINKING_TYPE}")
if("${YB_LINKING_TYPE}" STREQUAL "")
message(FATAL_ERROR "YB_LINKING_TYPE is not set")
endif()
message("CMAKE_MAKE_PROGRAM: ${CMAKE_MAKE_PROGRAM}")
# CMAKE_LINK_DEPENDS_NO_SHARED prevents prevent re-linking dependents of a shared library when it
# is re-linked. Enabling the optimized behavior by default, and allowing to customize it with the
# YB_CMAKE_LINK_DEPENDS_NO_SHARED environment variable.
if (NOT "$ENV{YB_CMAKE_LINK_DEPENDS_NO_SHARED}" STREQUAL "")
message(
"Setting CMAKE_LINK_DEPENDS_NO_SHARED to '$ENV{YB_CMAKE_LINK_DEPENDS_NO_SHARED}' "
"based on the YB_CMAKE_LINK_DEPENDS_NO_SHARED environment variable.")
set(CMAKE_LINK_DEPENDS_NO_SHARED $ENV{YB_CMAKE_LINK_DEPENDS_NO_SHARED})
else()
set(CMAKE_LINK_DEPENDS_NO_SHARED 1)
endif()
set(YB_FILTERING_TARGETS FALSE)
if (NOT "${YB_TEST_FILTER_RE}" STREQUAL "" OR NOT "${YB_EXECUTABLE_FILTER_RE}" STREQUAL "")
set(YB_FILTERING_TARGETS TRUE)
endif()
set(YB_SRC_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
message("YB_SRC_ROOT: ${YB_SRC_ROOT}")
set(YB_THIRDPARTY_DIR "$ENV{YB_THIRDPARTY_DIR}" CACHE INTERNAL
"Top-level directory of yugabyte-db-thirdparty checkout or pre-built location")
if("${YB_THIRDPARTY_DIR}" STREQUAL "")
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/thirdparty_path.txt")
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/thirdparty_path.txt" YB_THIRDPARTY_DIR)
else()
set(YB_THIRDPARTY_DIR "${YB_SRC_ROOT}/thirdparty")
endif()
set(ENV{YB_THIRDPARTY_DIR} "${YB_THIRDPARTY_DIR}")
endif()
message("YB_THIRDPARTY_DIR: ${YB_THIRDPARTY_DIR}")
set(YB_BUILD_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
set(ENV{YB_BUILD_ROOT} "${YB_BUILD_ROOT}")
get_filename_component(YB_BUILD_ROOT_PARENT "${YB_BUILD_ROOT}" DIRECTORY)
message("YB_BUILD_ROOT: ${YB_BUILD_ROOT}")
message("YB_TARGET_ARCH: $ENV{YB_TARGET_ARCH}")
DETECT_NUMBER_OF_PROCESSORS()
# Detect the shared library suffix on this platform
set(YB_STATIC_LIBRARY_SUFFIX ".a")
if(APPLE)
set(YB_SHARED_LIBRARY_SUFFIX ".dylib")
else()
set(YB_SHARED_LIBRARY_SUFFIX ".so")
endif()
message("Using shared library suffix '${YB_SHARED_LIBRARY_SUFFIX}'.")
set(YB_SHARED_LIBRARY_SUFFIX "${YB_SHARED_LIBRARY_SUFFIX}" CACHE INTERNAL
"Platform-specific shared library suffix, including the dot.")
message("CMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
CHECK_YB_COMPILER_PATH(${CMAKE_C_COMPILER})
message("CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
CHECK_YB_COMPILER_PATH(${CMAKE_CXX_COMPILER})
add_latest_symlink_target()
add_custom_target(dummy_target ALL
COMMAND cat /dev/null
COMMENT "Dummy target for dependency resolution testing")
include(CMakeParseArguments)
# Allow "make install" to not depend on all targets.
#
# Must be declared in the top-level CMakeLists.txt.
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
# -------------------------------------------------------------------------------------------------
# Decide whether we will be rebuilding third-party dependencies.
# -------------------------------------------------------------------------------------------------
set(REBUILD_THIRDPARTY TRUE)
# Also allow specifying -DNO_REBUILD_THIRDPARTY, because CLion does not always pass user-specified
# environment variables correctly.
if ((NOT "$ENV{NO_REBUILD_THIRDPARTY}" STREQUAL "") OR
("${YB_NO_REBUILD_THIRDPARTY}" STREQUAL "1") OR
(NOT "${YB_THIRDPARTY_DIR}" STREQUAL "${YB_SRC_ROOT}/thirdparty"))
message("Decided that we should not rebuild third-party dependencies. Criteria: "
"NO_REBUILD_THIRDPARTY env var: '$ENV{NO_REBUILD_THIRDPARTY}', "
"YB_NO_REBUILD_THIRDPARTY CMake var: '${YB_NO_REBUILD_THIRDPARTY}', "
"YB_THIRDPARTY_DIR: ${YB_THIRDPARTY_DIR}.")
set(REBUILD_THIRDPARTY FALSE)
endif()
# -------------------------------------------------------------------------------------------------
include(CompilerInfo)
# This helps find the right third-party build directory.
if ("${YB_BUILD_TYPE}" MATCHES "^(asan|tsan)$")
set(THIRDPARTY_INSTRUMENTATION_TYPE "${YB_BUILD_TYPE}")
elseif (IS_CLANG)
set(THIRDPARTY_INSTRUMENTATION_TYPE "uninstrumented")
elseif (IS_GCC)
set(THIRDPARTY_INSTRUMENTATION_TYPE "uninstrumented")
else()
message(FATAL_ERROR "Unknown compiler family: '${COMPILER_FAMILY}'.")
endif()
message("THIRDPARTY_INSTRUMENTATION_TYPE=${THIRDPARTY_INSTRUMENTATION_TYPE}")
# Make sure third-party dependency is up-to-date.
# TODO: do not invoke this step as part of the build. Always invoke it separately.
if (REBUILD_THIRDPARTY)
set(BUILD_THIRDPARTY_ARGS --build-type ${THIRDPARTY_INSTRUMENTATION_TYPE})
message("Invoking build_thirdparty.sh with these arguments: ${BUILD_THIRDPARTY_ARGS}")
execute_process(
COMMAND ${YB_SRC_ROOT}/build-support/invoke_thirdparty_build.sh ${BUILD_THIRDPARTY_ARGS}
RESULT_VARIABLE THIRDPARTY_SCRIPT_RESULT)
if (NOT (${THIRDPARTY_SCRIPT_RESULT} EQUAL 0))
message(FATAL_ERROR "Thirdparty was built unsuccessfully, terminating.")
endif()
else()
message("Skipping the third-party build (reasons logged earlier).")
endif()
if(NOT INCLUDE_COMPILER_INFO_EARLY)
message("Including the CompilerInfo module after we have considered building third-party "
"dependencies.")
include(CompilerInfo)
endif()
if(NOT DEFINED YB_LTO_ENABLED)
message(FATAL_ERROR "YB_LTO_ENABLED is not defined")
endif()
if(YB_LTO_ENABLED)
ADD_CXX_FLAGS("-DYB_LTO_ENABLED")
endif()
# Generate a compile_commands.json "compilation database" file.
# See http://clang.llvm.org/docs/JSONCompilationDatabase.html
if (CMAKE_EXPORT_COMPILE_COMMANDS)
message("CMAKE_EXPORT_COMPILE_COMMANDS is already enabled, will export compile_commands.json.")
elseif (YB_LTO_ENABLED)
message("This is an LTO-enabled build, will export compile_commands.json.")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
elseif ("$ENV{YB_EXPORT_COMPILE_COMMANDS}" STREQUAL "1")
message("The YB_EXPORT_COMPILE_COMMANDS environment variable is set to 1, will export "
"compile_commands.json.")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
elseif ("$ENV{YB_RUN_AFFECTED_TESTS_ONLY}" STREQUAL "1")
message("YB_RUN_AFFECTED_TESTS_ONLY is set to 1, will export compile_commands.json.")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
else()
message("CMake will not export the compilation database")
endif()
# -------------------------------------------------------------------------------------------------
# Build type (debug, release, fastdebug, etc.)
# -------------------------------------------------------------------------------------------------
# If no build type is specified, default to debug builds
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif(NOT CMAKE_BUILD_TYPE)
string (TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
# Alias RELEASE as RELWITHDEBINFO and MINSIZEREL. These are common CMake
# release type names and this provides compatibility with the CLion IDE.
if ("${CMAKE_BUILD_TYPE}" STREQUAL "RELWITHDEBINFO" OR "${CMAKE_BUILD_TYPE}" STREQUAL "MINSIZEREL")
set(CMAKE_BUILD_TYPE RELEASE)
endif()
VALIDATE_COMPILER_TYPE()
DETECT_BREW()
enable_lto_if_needed()
message("Using COMPILER_FAMILY=${COMPILER_FAMILY}")
if (NOT APPLE AND "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
# To enable 16-byte atomics support we should specify appropriate architecture.
ADD_CXX_FLAGS("-march=ivybridge")
ADD_CXX_FLAGS("-mcx16")
endif()
# Include compiler type and version in the compiler command line so that binaries built by different
# versions of the compiler will have different keys in ccache.
ADD_CXX_FLAGS("-DYB_COMPILER_TYPE=$ENV{YB_COMPILER_TYPE}")
ADD_CXX_FLAGS("-DYB_COMPILER_VERSION=${COMPILER_VERSION}")
ADD_CXX_FLAGS("-DSNAPPY")
ADD_CXX_FLAGS("-DLZ4")
ADD_CXX_FLAGS("-DZLIB")
############################################################
# Compiler flags
############################################################
if(NOT APPLE AND "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
# The following flags are required to not assume the presence of too many CPU features so that the
# code built can run on many platforms. For example, support building on c4.xlarge in AWS (dev
# servers) and running on c3.xlarge (flash-based cluster machines). There are a couple more flags
# (-mno-abm and -mno-movbe) that are not recognized by some clang versions we are using, so they
# are being added in the gcc-specific section below. We have also found that these flags don't
# work on Mac OS X, so we're not using them there.
ADD_CXX_FLAGS("-mno-avx -mno-bmi -mno-bmi2 -mno-fma")
endif()
# We want access to the PRI* print format macros.
ADD_CXX_FLAGS("-D__STDC_FORMAT_MACROS")
# Do not warn about uses of deprecated declarations. RocksDB has a few instances of those.
ADD_CXX_FLAGS("-Wno-deprecated-declarations")
ADD_CXX_FLAGS("-DGFLAGS=gflags")
# Don't allow virtual classes with non-virtual destructors.
ADD_CXX_FLAGS("-Wnon-virtual-dtor")
# Flags common to gcc and clang.
ADD_CXX_FLAGS("-Werror=enum-compare")
ADD_CXX_FLAGS("-Werror=reorder")
ADD_CXX_FLAGS("-Werror=switch")
ADD_CXX_FLAGS("-Werror=return-type")
ADD_CXX_FLAGS("-Werror=non-virtual-dtor")
if(IS_CLANG)
ADD_CXX_FLAGS("-Werror=string-plus-int")
ADD_CXX_FLAGS("-Werror=return-stack-address")
ADD_CXX_FLAGS("-Werror=implicit-fallthrough")
ADD_CXX_FLAGS("-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS")
ADD_CXX_FLAGS("-Wthread-safety-analysis")
if("$ENV{YB_ENABLE_STATIC_ANALYZER}" STREQUAL "1")
if(APPLE)
message("YB_ENABLE_STATIC_ANALYZER is set to 1, but we are not running the static analyzer"
" on macOS yet")
else()
message("YB_ENABLE_STATIC_ANALYZER is set to 1, enabling Clang static analyzer")
include(yb_clang_analyzer)
message("clang_analyzer_flags=${clang_analyzer_flags}")
ADD_CXX_FLAGS("${clang_analyzer_flags}")
endif()
else()
message("YB_ENABLE_STATIC_ANALYZER is not set to 1, not enabling Clang static analyzer")
endif()
ADD_CXX_FLAGS("-Wshorten-64-to-32")
endif()
if(USING_LINUXBREW AND IS_CLANG)
# We are only using Clang with Linuxbrew. We never use Linuxbrew GCC 5 anymore, it is too old.
ADD_LINKER_FLAGS("-Wl,--dynamic-linker=${LINUXBREW_LIB_DIR}/ld.so")
ADD_LINKER_FLAGS("--gcc-toolchain=${LINUXBREW_DIR}")
endif()
if(IS_GCC)
# For now code relies on fact that all libraries will be linked to binary (for using FLAGS_*)
# This flag is enabled implicitly on centos but not on ubuntu
# TODO: Subtitute it with '-as-needed' instead to avoid linking with unused library (issue #1495)
# for reducing pocess start time
ADD_LINKER_FLAGS("-Wl,-no-as-needed")
if ("${COMPILER_VERSION}" MATCHES "^11[.].*$")
# To silence these spurious warnings:
# https://gist.githubusercontent.com/mbautin/60c7fb897a92b998a111ff38429a1158/raw
# https://gist.githubusercontent.com/mbautin/56b95b7a6816ccdf77eaa60f53a7d3ef/raw
ADD_CXX_FLAGS("-Wno-array-bounds")
ADD_CXX_FLAGS("-Wno-stringop-overflow")
ADD_CXX_FLAGS("-Wno-stringop-overread")
endif()
endif()
if(USING_LINUXBREW)
# This is needed for finding correct versions of Flex, Bison, and other tools.
set(CMAKE_PREFIX_PATH "${LINUXBREW_DIR}" ${CMAKE_PREFIX_PATH})
endif()
# For both RocksDB and YugaByte code, we need to set the OS flag on Mac. Some code in RocksDB also
# distinguishes further into BSD vs everything else, but we probably do not care further.
if(APPLE)
ADD_CXX_FLAGS("-DOS_MACOSX")
endif()
# compiler flags for different build types (run 'cmake -DCMAKE_BUILD_TYPE=<type> .')
# For all builds:
# For CMAKE_BUILD_TYPE=Debug
# -ggdb: Enable gdb debugging
# For CMAKE_BUILD_TYPE=FastDebug
# Same as DEBUG, except with some optimizations on.
# For CMAKE_BUILD_TYPE=Release
# -O3: Enable all compiler optimizations
# -g: Enable symbols for profiler tools (TODO: remove for shipping)
# -DNDEBUG: Turn off dchecks/asserts/debug only code.
# -fno-omit-frame-pointer
# use frame pointers to allow simple stack frame walking for backtraces.
# This has a small perf hit but worth it for the ability to profile in production
# For profile guided optimization (PGO) builds, in addition to the flags for release builds:
# 1. Build first with CMAKE_BUILD_TYPE_PROFILE_GEN:
# -fprofile-generate: Indicates compiler should insert profile guided optimization events
# 2. Run the benchmarks (generates *.gcda profiling data).
# 3. Build again with CMAKE_BUILD_TYPE_PROFILE_BUILD
# -fprofile-use: Compiler will use the profile outputs for optimizations
set(CXX_FLAGS_DEBUG "-ggdb")
set(CXX_FLAGS_FASTDEBUG "-ggdb -O1 -fno-omit-frame-pointer -DFASTDEBUG")
set(CXX_FLAGS_RELEASE "-O3 -g -DNDEBUG -fno-omit-frame-pointer")
# Nullify CMake's predefined flags since we are handling different build types on our own.
# Without this change CMake will add flags from these variables and
# optimization level will be changed because in case of multiple -O flags gcc uses the last one.
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
set(CXX_FLAGS_PROFILE_GEN "${CXX_FLAGS_RELEASE} -fprofile-generate")
set(CXX_FLAGS_PROFILE_BUILD "${CXX_FLAGS_RELEASE} -fprofile-use")
set(CLANG_GCC_TOOLCHAIN "")
if(IS_CLANG AND NOT APPLE AND USING_LINUXBREW)
set(CLANG_GCC_TOOLCHAIN "${LINUXBREW_DIR}")
endif()
# Set compile flags based on the build type.
message("Configured for ${CMAKE_BUILD_TYPE} build "
"(set with cmake -DCMAKE_BUILD_TYPE={release,debug,...})")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
ADD_CXX_FLAGS("${CXX_FLAGS_DEBUG}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "FASTDEBUG")
ADD_CXX_FLAGS("${CXX_FLAGS_FASTDEBUG}")
# We specify RocksDB debug level that corresponds to the -O1 optimization level, the same as
# the rest of YB code in the "fastdebug" mode (used for ASAN/TSAN).
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
ADD_CXX_FLAGS("${CXX_FLAGS_RELEASE}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "PROFILE_GEN")
ADD_CXX_FLAGS("${CXX_FLAGS_PROFILE_GEN}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "PROFILE_BUILD")
ADD_CXX_FLAGS("${CXX_FLAGS_PROFILE_BUILD}")
else()
message(FATAL_ERROR "Unknown build type: ${CMAKE_BUILD_TYPE}")
endif ()
if (IS_CLANG)
# Using Clang with ccache causes a bunch of spurious warnings that are
# purportedly fixed in the next version of ccache. See the following for details:
#
# http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html
# http://petereisentraut.blogspot.com/2011/09/ccache-and-clang-part-2.html
# Clang generates ambiguous member template warnings when calling the ev++ api.
ADD_CXX_FLAGS("-Wno-ambiguous-member-template")
# Emit warnings on unannotated fallthrough in switch statements. This applies to both YB and
# RocksDB parts of the code.
ADD_CXX_FLAGS("-Wimplicit-fallthrough")
# Silence the "unused argument" warning.
ADD_CXX_FLAGS("-Qunused-arguments")
# Only hardcode -fcolor-diagnostics if stderr is opened on a terminal. Otherwise
# the color codes show up as noisy artifacts.
#
# This test is imperfect because 'cmake' and 'make' can be run independently
# (with different terminal options), and we're testing during the former.
execute_process(COMMAND test -t 2 RESULT_VARIABLE YB_IS_TTY)
if ((${YB_IS_TTY} EQUAL 0) AND (NOT ("$ENV{TERM}" STREQUAL "dumb")))
message("Running in a controlling terminal")
ADD_CXX_FLAGS("-fcolor-diagnostics")
else()
message("Running without a controlling terminal or in a dumb terminal")
endif()
if (APPLE)
ADD_CXX_FLAGS("-Wno-c++17-compat-mangling")
endif()
elseif(IS_GCC)
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
ADD_CXX_FLAGS("-mno-abm -mno-movbe")
endif()
else()
message(FATAL_ERROR "Unknown compiler family: ${COMPILER_FAMILY}")
endif()
include(YugabyteFindThirdParty)
yb_find_third_party_installed_dir()
set(YB_PREFIX_COMMON "${YB_THIRDPARTY_INSTALLED_DIR}/common")
yb_put_vars_into_cache(YB_PREFIX_COMMON)
if(IS_CLANG)
YB_SETUP_CLANG()
if ("${YB_BUILD_TYPE}" MATCHES "^(asan|tsan)$")
YB_SETUP_SANITIZER()
endif()
endif()
if (USING_LINUXBREW)
include_directories(SYSTEM "${LINUXBREW_DIR}/include")
endif()
if (NOT IS_CLANG)
ADD_LINKER_FLAGS("-latomic")
endif()
# Flag to enable clang undefined behavior sanitizer
# We explicitly don't enable all of the sanitizer flags:
# - disable 'vptr' because it currently crashes somewhere in boost::intrusive::list code
# - disable 'alignment' because unaligned access is really OK on Nehalem and we do it
# all over the place.
if ("${YB_BUILD_TYPE}" STREQUAL "asan")
set(CXX_NO_SANITIZE_FLAG "alignment")
if(NOT APPLE)
set(CXX_NO_SANITIZE_FLAG "${CXX_NO_SANITIZE_FLAG},vptr")
ADD_CXX_FLAGS("-fsanitize=undefined")
endif()
ADD_CXX_FLAGS("-fno-sanitize-recover=all -fno-sanitize=${CXX_NO_SANITIZE_FLAG}")
ADD_CXX_FLAGS("-fsanitize-recover=float-cast-overflow")
endif ()
if ("${YB_BUILD_TYPE}" MATCHES "^(asan|tsan)$")
if(IS_CLANG)
ADD_CXX_FLAGS("-fsanitize-blacklist=${BUILD_SUPPORT_DIR}/sanitize-blacklist.txt")
else()
message(WARNING "GCC does not support specifying a sanitizer blacklist. Known sanitizer "
"check failures will not be suppressed.")
endif()
endif()
set(BUILD_SHARED_LIBS ON)
if ("${YB_BUILD_TYPE}" STREQUAL "prof_gen")
ADD_CXX_FLAGS("-fprofile-instr-generate -DYB_PROFGEN")
endif ()
if ("${YB_BUILD_TYPE}" STREQUAL "prof_use")
if (NOT YB_PGO_DATA_PATH)
message (SEND_ERROR "Pgo data path is not set.")
endif()
ADD_CXX_FLAGS("-fprofile-instr-use=${YB_PGO_DATA_PATH}")
# Even with the fresh profile data we might get warnings like
# warning: Function control flow change detected (hash mismatch)
# [-Wbackend-plugin]
# Silencing it for now.
ADD_CXX_FLAGS("-Wno-backend-plugin")
ADD_CXX_FLAGS("-Wno-profile-instr-unprofiled")
ADD_CXX_FLAGS("-Wno-profile-instr-out-of-date")
endif ()
# Position independent code is only necessary when producing shared objects.
ADD_CXX_FLAGS(-fPIC)
# where to put generated archives (.a files)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
file(MAKE_DIRECTORY "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
# where to put generated libraries (.so files)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
file(MAKE_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
# where to put generated binaries
set(EXECUTABLE_OUTPUT_PATH "${YB_BUILD_ROOT}/bin")
file(MAKE_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}")
# Generated sources always have higher priority than even the "ent" directory include files.
include_directories(${CMAKE_CURRENT_BINARY_DIR}/src)
include_directories(ent/src)
include_directories(src)
enable_testing()
if (USING_LINUXBREW)
ADD_GLOBAL_RPATH_ENTRY_AND_LIB_DIR("${YB_BUILD_ROOT}/postgres/lib")
ADD_GLOBAL_RPATH_ENTRY_AND_LIB_DIR("${LINUXBREW_LIB_DIR}")
endif()
############################################################
# Dependencies
############################################################
function(ADD_THIRDPARTY_LIB LIB_NAME)
set(options)
set(one_value_args SHARED_LIB STATIC_LIB)
set(multi_value_args DEPS)
cmake_parse_arguments(ARG "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
if(ARG_UNPARSED_ARGUMENTS)
message(SEND_ERROR "Error: unrecognized arguments: ${ARG_UNPARSED_ARGUMENTS}")
endif()
if(NOT ARG_SHARED_LIB)
if(NOT ARG_STATIC_LIB)
message(FATAL_ERROR "No static or shared library provided for ${LIB_NAME}")
endif()
add_library(${LIB_NAME} STATIC IMPORTED)
set_target_properties(${LIB_NAME}
PROPERTIES IMPORTED_LOCATION "${ARG_STATIC_LIB}")
message("Added static library dependency ${LIB_NAME}: ${ARG_STATIC_LIB}")
else()
add_library(${LIB_NAME} SHARED IMPORTED)
set_target_properties(${LIB_NAME}
PROPERTIES IMPORTED_LOCATION "${ARG_SHARED_LIB}")
message("Added shared library dependency ${LIB_NAME}: ${ARG_SHARED_LIB}")
endif()
if(ARG_DEPS)
set_target_properties(${LIB_NAME}
PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES "${ARG_DEPS}")
endif()
# Set up an "exported variant" for this thirdparty library (see "Visibility"
# above). It's the same as the real target, just with an "_exported" suffix.
# We prefer the static archive if it exists (as it's akin to an "internal"
# library), but we'll settle for the shared object if we must.
#
# A shared object exported variant will force any "leaf" library that
# transitively depends on it to also depend on it at runtime; this is
# desirable for some libraries (e.g. cyrus_sasl).
set(LIB_NAME_EXPORTED ${LIB_NAME}_exported)
if(ARG_STATIC_LIB)
add_library(${LIB_NAME_EXPORTED} STATIC IMPORTED)
set_target_properties(${LIB_NAME_EXPORTED}
PROPERTIES IMPORTED_LOCATION "${ARG_STATIC_LIB}")
else()
add_library(${LIB_NAME_EXPORTED} SHARED IMPORTED)
set_target_properties(${LIB_NAME_EXPORTED}
PROPERTIES IMPORTED_LOCATION "${ARG_SHARED_LIB}")
endif()
if(ARG_DEPS)
set_target_properties(${LIB_NAME_EXPORTED}
PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES "${ARG_DEPS}")
endif()
endfunction()
# Look in thirdparty prefix paths before anywhere else for system dependencies.
set(CMAKE_PREFIX_PATH ${YB_PREFIX_COMMON} ${CMAKE_PREFIX_PATH})
ADD_LINKER_FLAGS("-L${YB_PREFIX_COMMON}/lib")
set(YB_THIRDPARTY_INSTALLED_DEPS_DIR
"${YB_THIRDPARTY_INSTALLED_DIR}/${THIRDPARTY_INSTRUMENTATION_TYPE}")
set(CMAKE_PREFIX_PATH ${YB_THIRDPARTY_INSTALLED_DEPS_DIR} ${CMAKE_PREFIX_PATH})
ADD_LINKER_FLAGS("-L${YB_THIRDPARTY_INSTALLED_DEPS_DIR}/lib")
set(YB_THIRDPARTY_INSTALLED_DIR ${YB_PREFIX_COMMON})
# See thirdparty/yb_build_thirdparty_main.py for an explanation of differences between installed and
# installed-deps.
ADD_GLOBAL_RPATH_ENTRY("${YB_THIRDPARTY_INSTALLED_DIR}/lib")
ADD_GLOBAL_RPATH_ENTRY("${YB_THIRDPARTY_INSTALLED_DEPS_DIR}/lib")
# If we are using a non-default gcc or clang compiler, we need to add its library directory to
# rpath.
if(IS_GCC AND NOT "$ENV{YB_GCC_PREFIX}" STREQUAL "")
# TODO: this works for the gcc 6.2.0 build on Linux, might be different on other platforms.
ADD_GLOBAL_RPATH_ENTRY("$ENV{YB_GCC_PREFIX}/lib64")
endif()
if(IS_CLANG AND NOT "$ENV{YB_CLANG_PREFIX}" STREQUAL "")
# TODO: this works for the Linux binary clang 3.9 package, might be different on Mac OS X.
ADD_GLOBAL_RPATH_ENTRY("$ENV{YB_CLANG_PREFIX}/lib")
endif()
configure_macos_sdk()
# -------------------------------------------------------------------------------------------------
yb_find_third_party_dependencies()
# Flags to link the tcmalloc static library. Used for each CMake target that is an executable,
# as well as for executables generated as part of Postgres build.
set(TCMALLOC_STATIC_LIB_LD_FLAGS "")
# Set flags based on whether we are using tcmalloc.
if ("${YB_TCMALLOC_ENABLED}" STREQUAL "1" OR
"${YB_BUILD_TYPE}" MATCHES "^(asan|tsan)$")
message("Allowing undefined symbols "
"(YB_TCMALLOC_ENABLED=${YB_TCMALLOC_ENABLED}, build type is ${YB_BUILD_TYPE})")
if(IS_GCC)
# Without this, yb_util cannot get linked with GCC 11 because it references some undefined
# symbols belonging to tcmalloc, e.g. HeapProfilerStart.
# https://gist.githubusercontent.com/mbautin/53e72fbc8c45b4b0d759673fba102ddf/raw
ADD_LINKER_FLAGS("-Wl,--unresolved-symbols=ignore-all")
endif()
if(APPLE)
# See https://bit.ly/3DrOibC on this Apple Clang specific flag.
ADD_LINKER_FLAGS("-undefined dynamic_lookup")
# To get rid of the following warning during linking on macOS:
#
# -undefined dynamic_lookup may not work with chained fixups
#
# See https://github.com/bazelbuild/bazel/issues/16413 for discussion.
ADD_LINKER_FLAGS("-Wl,-no_fixup_chains")
else()
ADD_LINKER_FLAGS("-Wl,--allow-shlib-undefined")
endif()
if(APPLE)
set(TCMALLOC_STATIC_LIB_LD_FLAGS "-force_load ${TCMALLOC_STATIC_LIB}")
else()
set(TCMALLOC_STATIC_LIB_LD_FLAGS
"-Wl,--whole-archive ${TCMALLOC_STATIC_LIB} -Wl,--no-whole-archive")
endif()
else()
message("Not allowing any undefined symbols "
"(tcmalloc is disabled and build type is ${YB_BUILD_TYPE})")
ADD_LINKER_FLAGS("-Wl,--no-undefined -Wl,--no-allow-shlib-undefined")
endif()
############################################################
# Linker setup
############################################################
set(YB_MIN_TEST_LIBS yb_test_main yb_test_util ${YB_BASE_LIBS})
set(YB_TEST_LINK_LIBS ${YB_MIN_TEST_LIBS})
############################################################
# Subdirectories
############################################################
# TODO(dmitry): Add YB_CMAKE_CXX_EXTRA_FLAGS to CMAKE_CXX_FLAGS when all source code will be
# compatible with with these flags #9279
set(YB_CMAKE_CXX_EXTRA_FLAGS "-Wextra -Wno-unused-parameter")
# For any C code, use the same flags as for C++.
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS}")
# C++-only flags.
ADD_CXX_FLAGS("-Werror=missing-field-initializers")
if ((IS_GCC) AND
("${CMAKE_CXX_COMPILER_VERSION}" VERSION_GREATER_EQUAL "7.0"))
# Used with GCC 7.
ADD_CXX_FLAGS("-faligned-new")
endif()
# -Wsign-compare is enabled for C++ only for GCC with just -Wall, and enabled for both C and C++
# for GCC and Clang with -Wextra. Remove when -Wextra is enabled for all source code (#9279).
if (IS_CLANG)
ADD_CXX_FLAGS("-Wsign-compare")
endif()
string (REPLACE "-Werror=non-virtual-dtor" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string (REPLACE "-Werror=reorder" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string (REPLACE "-Wnon-virtual-dtor" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string (REPLACE "-Woverloaded-virtual" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
yb_deduplicate_arguments(CMAKE_C_FLAGS)
set(CMAKE_CXX_STANDARD 20)
yb_deduplicate_arguments(CMAKE_CXX_FLAGS)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
yb_deduplicate_arguments(CMAKE_EXE_LINKER_FLAGS)
message("Linker flags for executables: ${CMAKE_EXE_LINKER_FLAGS}")
# Define full paths to libpq and libyb_pgbackend shared libraries. These libraries are built as part
# of building PostgreSQL. We need to declare these as by-products of running the PostgreSQL build
# command, as well as wrap them into CMake targets so that other libraries and executables can
# depend on them.
set(LIBPQ_SHARED_LIB
"${YB_BUILD_ROOT}/postgres/lib/libpq${YB_SHARED_LIBRARY_SUFFIX}")
set(YB_PGBACKEND_SHARED_LIB
"${YB_BUILD_ROOT}/postgres/lib/libyb_pgbackend${YB_SHARED_LIBRARY_SUFFIX}")
# -------------------------------------------------------------------------------------------------
# Subdirectories
# -------------------------------------------------------------------------------------------------
# PostgreSQL source code is not in src/yb so that we can grep non-PostgreSQL YB code easily.
add_subdirectory(src/postgres)
include_directories(${YB_BUILD_ROOT}/postgres/include)
add_postgres_shared_library(pq "${LIBPQ_SHARED_LIB}")
add_postgres_shared_library(yb_pgbackend "${YB_PGBACKEND_SHARED_LIB}")
add_subdirectory(src/yb/rocksdb)
add_subdirectory(src/yb/gutil)
add_subdirectory(src/yb/util)
add_subdirectory(src/yb/common)
add_subdirectory(src/yb/encryption)
add_subdirectory(src/yb/gen_yrpc)
add_subdirectory(src/yb/fs)
add_subdirectory(src/yb/server)
add_subdirectory(src/yb/tablet)
add_subdirectory(src/yb/bfcommon)
add_subdirectory(src/yb/bfql)
add_subdirectory(src/yb/bfpg)
add_subdirectory(src/yb/rpc)
add_subdirectory(src/yb/tserver)
add_subdirectory(src/yb/consensus)
add_subdirectory(src/yb/master)
add_subdirectory(src/yb/client)
add_subdirectory(src/yb/integration-tests)
add_subdirectory(src/yb/tools)
add_subdirectory(src/yb/rocksutil)
add_subdirectory(src/yb/docdb)
add_subdirectory(src/yb/yql)
add_subdirectory(src/yb/cdc)
include("${YB_SRC_ROOT}/ent/CMakeLists.txt")
# Note "yb-server", without the "t" -- this is not a typo. "yb-server" is the unified LTO executable
# that contains the functionality of both yb-master and yb-tserver, and yb-master/yb-tserver are
# symlinks to it. It decides to act as yb-master or yb-tserver based on its argv[0].
#
# In the command below, yb-master is the CMake target that we use as a base to determine the set of
# object files to be linked into the LTO executable, yb-server is the name of the LTO executable,
# and yb-master/yb-tserver are names of symlinks to create in the "bin" directory, pointing to the
# LTO executable.
yb_add_lto_target(yb-master yb-server "yb-master;yb-tserver")
# ------------------------------------------------------------------------------------------------
# Utilities for collecting the dependency graph
# ------------------------------------------------------------------------------------------------
# We could not accumulate multiple lines in YB_ALL_DEPS due to a CMake bug with cache variables,
# but we can write multiple lines in the output file.
string(REPLACE "\\n" "\n" YB_ALL_DEPS_FINAL "${YB_ALL_DEPS}")
# This file wlll contains dependencies in the following form, separated by newlines:
# target: dep1;dep2;..;depN
# The same target can appear on the left hand side of multiple such lines.
file(WRITE "${YB_BUILD_ROOT}/yb_cmake_deps.txt" "${YB_ALL_DEPS_FINAL}")
# ------------------------------------------------------------------------------------------------
# Final reporting
# ------------------------------------------------------------------------------------------------
math(EXPR YB_NUM_EXCLUDED_TESTS "${YB_NUM_TESTS} - ${YB_NUM_INCLUDED_TESTS}")
message("Total tests: ${YB_NUM_TESTS}, "
"included: ${YB_NUM_INCLUDED_TESTS}, "
"excluded: ${YB_NUM_EXCLUDED_TESTS}")
if(NOT YB_BUILD_TESTS AND NOT ${YB_NUM_INCLUDED_TESTS} STREQUAL "1")
message(
FATAL_ERROR
"BUILD_TESTS is not set, but we still added ${YB_NUM_INCLUDED_TESTS} tests"
" (one test would be OK because we always add create_initial_sys_catalog_snapshot).")
endif()
math(EXPR YB_NUM_EXCLUDED_EXECUTABLES "${YB_NUM_EXECUTABLES} - ${YB_NUM_INCLUDED_EXECUTABLES}")
message("Total non-test executables: ${YB_NUM_EXECUTABLES}, "
"included: ${YB_NUM_INCLUDED_EXECUTABLES}, "
"excluded: ${YB_NUM_EXCLUDED_EXECUTABLES}")