forked from yugabyte/yugabyte-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
1560 lines (1371 loc) · 61.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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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.
#
# Portions Copyright (c) YugaByte, Inc.
# Require cmake that supports BYPRODUCTS in add_custom_command() [1].
#
# 1. https://cmake.org/Bug/view.php?id=14963
cmake_minimum_required(VERSION 3.2.0)
# =================================================================================================
# Functions
# =================================================================================================
function(CHECK_YB_COMPILER_PATH COMPILER_PATH)
if(NOT "${COMPILER_PATH}" MATCHES "/compiler-wrappers/(cc|c[+][+])$" AND
NOT "${CMAKE_COMMAND}" MATCHES "/[.]?(CLion|clion)")
message(
SEND_ERROR
"Invalid compiler path: '${COMPILER_PATH}'. Expected to end with one of: "
"/compiler-wrappers/{cc,c++}. The only exception is for builds invoked from CLion, but "
"CMAKE_COMMAND ('${CMAKE_COMMAND}') does not contain a substring '/[.]CLion' or '/[.]clion' "
"(feel free to tweak the pattern in the top-level CMakeLists.txt if it has to be updated "
"for the most recent version of CLion).")
endif()
endfunction()
# Determine the number of CPUs to be used so we can call make on existing Makefiles (e.g. RocksDB)
# with the right level of parallelism. Snippet taken from https://blog.kitware.com/how-many-ya-got/
function(DETECT_NUMBER_OF_PROCESSORS)
if(NOT DEFINED PROCESSOR_COUNT)
# Unknown:
set(PROCESSOR_COUNT 0)
# Linux:
set(cpuinfo_file "/proc/cpuinfo")
if(EXISTS "${cpuinfo_file}")
file(STRINGS "${cpuinfo_file}" procs REGEX "^processor.: [0-9]+$")
list(LENGTH procs PROCESSOR_COUNT)
endif()
# Mac:
if(APPLE)
execute_process(COMMAND /usr/sbin/sysctl -n hw.ncpu OUTPUT_VARIABLE PROCESSOR_COUNT)
# Strip trailing newline (otherwise it may get into the generated Makefile).
string(STRIP "${PROCESSOR_COUNT}" PROCESSOR_COUNT)
endif()
# Windows:
if(WIN32)
set(PROCESSOR_COUNT "$ENV{NUMBER_OF_PROCESSORS}")
endif()
endif()
if (NOT DEFINED PROCESSOR_COUNT OR "${PROCESSOR_COUNT}" STREQUAL "")
message(FATAL_ERROR "Could not determine the number of logical CPUs")
endif()
message("Detected the number of logical CPUs: ${PROCESSOR_COUNT}")
set(PROCESSOR_COUNT "${PROCESSOR_COUNT}" PARENT_SCOPE)
endfunction()
# Prevent builds from the top-level source directory. This ensures that build output is well
# isolated from the source tree.
function(ENFORCE_OUT_OF_SOURCE_BUILD)
if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
message(FATAL_ERROR
"YugaByte may not be built from the top-level source directory. Create a new "
"directory and run cmake from there, passing the path to the top-level "
"source directory as the last argument. "
"To override this, rerun CMake with -DYB_ALLOW_IN_SOURCE_BUILD=0. "
"Also, delete 'CMakeCache.txt' and 'CMakeFiles' from the top-level source "
"directory, otherwise future builds will not work.")
endif()
endfunction()
function(DETECT_LINUXBREW)
# Detect Linuxbrew. The logic needs to be consistent with the detect_linuxbrew function in
# common-build-env.sh.
set(USING_LINUXBREW FALSE)
if(NOT APPLE)
set(LINUXBREW_DIR "$ENV{YB_LINUXBREW_DIR}")
if("${LINUXBREW_DIR}" STREQUAL "")
set(LINUXBREW_DIR "$ENV{HOME}/.linuxbrew-yb-build")
endif()
if(EXISTS "${LINUXBREW_DIR}/bin" AND
EXISTS "${LINUXBREW_DIR}/lib")
message("Linuxbrew found at ${LINUXBREW_DIR}")
set(USING_LINUXBREW TRUE)
else()
message("Not using Linuxbrew: no valid Linuxbrew installation found at "
"${LINUXBREW_DIR}")
endif()
endif()
if(NOT USING_LINUXBREW)
set(LINUXBREW_DIR "/tmp/not-using-linuxbrew")
endif()
set(USING_LINUXBREW "${USING_LINUXBREW}" PARENT_SCOPE)
set(LINUXBREW_DIR "${LINUXBREW_DIR}" PARENT_SCOPE)
set(LINUXBREW_LIB_DIR "${LINUXBREW_DIR}/lib" PARENT_SCOPE)
endfunction()
# Ensures that the YB_COMPILER_TYPE environment variable matches the auto-detected compiler family.
# Also makes sure that we are using a supported compiler family. Finally, this sets the convienience
# variables IS_GCC and IS_CLANG.
function(VALIDATE_COMPILER_TYPE)
if ("${YB_USE_ASAN}" OR "${YB_USE_TSAN}" OR "${YB_USE_UBSAN}")
if (NOT "$ENV{YB_COMPILER_TYPE}" STREQUAL "" AND
NOT "$ENV{YB_COMPILER_TYPE}" STREQUAL "clang")
message(FATAL_ERROR
"YB_COMPILER_TYPE is set to '$ENV{YB_COMPILER_TYPE}', but it must be 'clang' for "
"ASAN/TSAN/UBSAN builds. "
"YB_USE_ASAN=${YB_USE_ASAN}, "
"YB_USE_TSAN=${YB_USE_TSAN}, "
"YB_USE_UBSAN=${YB_USE_UBSAN}")
endif()
set(ENV{YB_COMPILER_TYPE} "clang")
endif()
if ("$ENV{YB_COMPILER_TYPE}" STREQUAL "")
set(ENV{YB_COMPILER_TYPE} "${COMPILER_FAMILY}")
endif()
if (NOT "$ENV{YB_COMPILER_TYPE}" STREQUAL "${COMPILER_FAMILY}")
message(FATAL_ERROR
"YB_COMPILER_TYPE environment variable ($ENV{YB_COMPILER_TYPE}) does not match the "
"compiler family detected by CMake (${COMPILER_FAMILY}")
endif()
if (NOT "${COMPILER_FAMILY}" STREQUAL "gcc" AND
NOT "${COMPILER_FAMILY}" STREQUAL "clang")
message(FATAL_ERROR "Unknown compiler family: ${COMPILER_FAMILY} (expected 'gcc' or 'clang').")
endif()
set(IS_CLANG FALSE PARENT_SCOPE)
if ("${COMPILER_FAMILY}" STREQUAL "clang")
set(IS_CLANG TRUE PARENT_SCOPE)
endif()
set(IS_GCC FALSE PARENT_SCOPE)
if ("${COMPILER_FAMILY}" STREQUAL "gcc")
set(IS_GCC TRUE PARENT_SCOPE)
endif()
endfunction()
# Functions for adding compiler/linker flags ------------------------------------------------------
# Adding flags that apply to both compiler and linker.
set(COMMON_COMPILER_LINKER_FLAGS "")
set(CAN_ADD_COMMON_COMPILER_LINKER_FLAGS FALSE)
function(ADD_COMMON_COMPILER_LINKER_FLAGS FLAGS_TO_ADD)
if (NOT CAN_ADD_COMMON_LINKER_FLAGS)
message(FATAL_ERROR "Trying to add common compiler/linker flags after it has been disallowed.")
endif()
message("Adding common compiler/linker flags: ${FLAGS_TO_ADD}")
set(COMMON_COMPILER_LINKER_FLAGS
"${COMMON_COMPILER_LINKER_FLAGS} ${FLAGS_TO_ADD}"
PARENT_SCOPE)
endfunction()
# Linker flags applied to both executables and shared libraries. We append this both to
# CMAKE_EXE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS after we finish making changes to this.
# These flags apply to both YB and RocksDB parts of the codebase.
set(COMMON_LINKER_FLAGS "")
set(CAN_ADD_COMMON_LINKER_FLAGS TRUE)
function(ADD_COMMON_LINKER_FLAGS FLAGS)
if(NOT CAN_ADD_COMMON_LINKER_FLAGS)
message(FATAL_ERROR "Trying to add to COMMON_LINKER_FLAGS after it has been disallowed.")
endif()
message("Adding to linker flags used by both YB and RocksDB parts of the code: ${FLAGS}")
set(COMMON_LINKER_FLAGS "${COMMON_LINKER_FLAGS} ${FLAGS}" PARENT_SCOPE)
endfunction()
function(ADD_GLOBAL_RPATH_ENTRY RPATH_ENTRY)
if (NOT CAN_ADD_COMMON_LINKER_FLAGS)
message(FATAL_ERROR
"Trying to add an rpath entry to COMMON_LINKER_FLAGS after it has been disallowed.")
endif()
if (RPATH_ENTRY STREQUAL "")
message(FATAL_ERROR "Trying to add an empty rpath entry.")
endif()
set(NEW_RPATH_ENTRY_LINKER_FLAGS "-Wl,-rpath,${RPATH_ENTRY}")
message("Adding a global rpath entry: ${RPATH_ENTRY}")
# Note (mbautin): I don't think we can run ADD_COMMON_LINKER_FLAGS here, because it would need to
# modify the COMMON_LINKER_FLAGS variable at "parent parent scope", and I'm not sure if that even
# exists in CMake.
set(COMMON_LINKER_FLAGS "${COMMON_LINKER_FLAGS} ${NEW_RPATH_ENTRY_LINKER_FLAGS}"
PARENT_SCOPE)
endfunction()
# CXX_YB_COMMON_FLAGS are flags that are common across the 'src/yb' portion of the codebase (but do
# not apply to the 'src/rocksdb' part). "Common" in the name refers to the fact that they are common
# across different build types.
#
# Compiler flags that are common across debug/release builds:
# -msse4.2: Enable sse4.2 compiler intrinsics.
# -Wall: Enable all warnings.
# -Wno-sign-compare: suppress warnings for comparison between signed and unsigned integers
# -Wno-deprecated: some of the gutil code includes old things like ext/hash_set, ignore that
# -pthread: enable multithreaded malloc
# -fno-strict-aliasing
# Assume programs do not follow strict aliasing rules. GCC cannot always verify whether strict
# aliasing rules are indeed followed due to fundamental limitations in escape analysis, which
# can result in subtle bad code generation. This has a small perf hit but worth it to avoid
# hard to debug crashes.
set(CXX_YB_COMMON_FLAGS "")
set(CAN_ADD_CXX_YB_COMMON_FLAGS TRUE)
function(ADD_CXX_YB_COMMON_FLAGS FLAGS)
if(NOT CAN_ADD_CXX_YB_COMMON_FLAGS)
message(FATAL_ERROR
"Trying to add flags to CXX_YB_COMMON_FLAGS after it has been disallowed.")
endif()
message("Adding a C++ flags for the YB (not RocksDB) part of the codebase only: ${FLAGS}")
set(CXX_YB_COMMON_FLAGS "${CXX_YB_COMMON_FLAGS} ${FLAGS}" PARENT_SCOPE)
endfunction()
ADD_CXX_YB_COMMON_FLAGS("-Werror")
ADD_CXX_YB_COMMON_FLAGS("-fno-strict-aliasing -msse4.2 -Wall -Wno-sign-compare -Wno-deprecated")
ADD_CXX_YB_COMMON_FLAGS("-Winvalid-pch")
ADD_CXX_YB_COMMON_FLAGS("-pthread -Wno-parentheses -DBOOST_BIND_NO_PLACEHOLDERS")
ADD_CXX_YB_COMMON_FLAGS("-DROCKSDB_PLATFORM_POSIX")
if (NOT IS_CLANG)
# To enable 16-byte atomics support in GCC we should specify appropriate architecture.
ADD_CXX_YB_COMMON_FLAGS("-march=ivybridge")
endif()
# CXX_GLOBAL_COMMON_FLAGS are flags that are common to C++ code in both the YB and RocksDB parts of
# the codebase.
set(CXX_GLOBAL_COMMON_FLAGS "")
set(CAN_ADD_CXX_GLOBAL_COMMON_FLAGS TRUE)
function(ADD_CXX_GLOBAL_COMMON_FLAGS FLAGS)
if(NOT CAN_ADD_CXX_GLOBAL_COMMON_FLAGS)
message(FATAL_ERROR
"Trying to add flags to CXX_GLOBAL_COMMON_FLAGS after it has been disallowed.")
endif()
message("Adding C++ flags for both YB and RocksDB parts of the codebase: ${FLAGS}")
set(CXX_GLOBAL_COMMON_FLAGS "${CXX_GLOBAL_COMMON_FLAGS} ${FLAGS}"
PARENT_SCOPE)
endfunction()
# This duplicates part of the logic from common-build-env.sh so that we can run from CLion where
# cmake is invoked directly.
function(DETECT_EDITION)
set(YB_ENTERPRISE_ROOT "${YB_SRC_ROOT}/ent")
set(YB_ENTERPRISE_ROOT "${YB_ENTERPRISE_ROOT}" PARENT_SCOPE)
if ("$ENV{YB_EDITION}" STREQUAL "")
if (IS_DIRECTORY "${YB_ENTERPRISE_ROOT}")
set(ENV{YB_EDITION} "enterprise")
else()
set(ENV{YB_EDITION} "community")
endif()
endif()
set(YB_EDITION "$ENV{YB_EDITION}")
set(YB_EDITION "${YB_EDITION}" PARENT_SCOPE)
if ("${YB_EDITION}" STREQUAL "enterprise" AND NOT IS_DIRECTORY "${YB_ENTERPRISE_ROOT}")
message(FATAL_ERROR "YB_EDITION is '${YB_EDITION}' but the directory '${YB_ENTERPRISE_ROOT}'"
" does not exist.")
endif()
message("YB_EDITION: ${YB_EDITION}")
endfunction()
function(YB_INCLUDE_EXTENSIONS)
if ("${YB_EDITION}" STREQUAL "enterprise")
file(RELATIVE_PATH CUR_REL_LIST_FILE "${YB_SRC_ROOT}" "${CMAKE_CURRENT_LIST_FILE}")
get_filename_component(CUR_REL_LIST_NAME_NO_EXT "${CUR_REL_LIST_FILE}" NAME_WE)
get_filename_component(CUR_REL_LIST_DIR "${CUR_REL_LIST_FILE}" DIRECTORY)
set(INCLUDED_PATH
"${YB_ENTERPRISE_ROOT}/${CUR_REL_LIST_DIR}/${CUR_REL_LIST_NAME_NO_EXT}-include.txt")
message("Including '${INCLUDED_PATH}' into '${CMAKE_CURRENT_LIST_FILE}'")
include("${INCLUDED_PATH}")
endif()
endfunction()
# ------------------------------------------------------------------------------------------------
# Main build
set(YB_SRC_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
message("YB_SRC_ROOT: ${YB_SRC_ROOT}")
DETECT_EDITION()
if ("$YB_EDITION" STREQUAL "")
message(FATAL_ERROR "YB_EDITION not defined after calling DETECT_EDITION()")
endif()
set(YB_THIRDPARTY_DIR "$ENV{YB_THIRDPARTY_DIR}")
if("${YB_THIRDPARTY_DIR}" STREQUAL "")
set(YB_THIRDPARTY_DIR "${YB_SRC_ROOT}/thirdparty")
endif()
set(YB_BUILD_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
message("YB_BUILD_ROOT: ${YB_BUILD_ROOT}")
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}'.")
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})
# Provide a 'latest' symlink to this build directory if the "blessed" multi-build layout is
# detected:
#
# build/
# build/<first build directory>
# build/<second build directory>
# ...
if ("${CMAKE_CURRENT_BINARY_DIR}" MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}/build/[^/]+$")
if ("${CMAKE_CURRENT_BINARY_DIR}" MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}/build/latest")
message(FATAL_ERROR "Should not run cmake inside the build/latest symlink. "
"First change directories into the destination of the symlink.")
endif()
# This option is needed in addition to -sf, while linking, to force the link to
# occur on directories. Unfortunately, Linux & MacOS differ on the option name.
if (NOT APPLE)
set(MORE_ARGS "-T")
else()
set(MORE_ARGS "-h")
endif()
add_custom_target(latest_symlink ALL
ln ${MORE_ARGS} -sf ${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/build/latest
COMMENT "Recreating ../build/latest symlink")
# 'ALL' above doesn't actually add 'latest_symlink' as a dependency to all
# targets. So, we override add_executable to ensure that whenever any executable
# is built, the symlink is re-created.
function(add_executable name)
# Call through to the original add_executable function.
_add_executable(${name} ${ARGN})
add_dependencies(${name} latest_symlink)
endfunction()
endif()
# TODO: can we somehow pass this into the java build?
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/version.txt" YB_VERSION_NUMBER)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
include(CMakeParseArguments)
set(BUILD_SUPPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build-support)
# 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)
# Make sure thirdparty stuff is up-to-date.
if ("$ENV{NO_REBUILD_THIRDPARTY}" STREQUAL "")
set(BUILD_THIRDPARTY_ARGS "")
if (NOT YB_USE_TSAN)
set(BUILD_THIRDPARTY_ARGS ${BUILD_THIRDPARTY_ARGS} --skip-tsan)
endif()
if (NOT YB_USE_TSAN AND NOT YB_USE_ASAN)
set(BUILD_THIRDPARTY_ARGS ${BUILD_THIRDPARTY_ARGS} --skip-llvm)
endif()
message("Invoking build-thirdparty.sh with these arguments: ${BUILD_THIRDPARTY_ARGS}")
execute_process(
COMMAND ${YB_THIRDPARTY_DIR}/build-thirdparty.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("The NO_REBUILD_THIRDPARTY environment variable is non-empty, skipping the third-party"
"build")
endif()
# Generate a Clang compile_commands.json "compilation database" file for use
# with various development tools, such as Vim's YouCompleteMe plugin.
# See http://clang.llvm.org/docs/JSONCompilationDatabase.html
if ("$ENV{CMAKE_EXPORT_COMPILE_COMMANDS}" STREQUAL "1")
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
endif()
# -------------------------------------------------------------------------------------------------
# Build type (debug, release, fastdebug, etc.)
# -------------------------------------------------------------------------------------------------
# If no build 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()
############################################################
# Compiler type (gcc, clang, etc.)
############################################################
# Determine compiler version. This will set COMPILER_FAMILY.
include(CompilerInfo)
message("Using COMPILER_FAMILY=${COMPILER_FAMILY}")
VALIDATE_COMPILER_TYPE()
# 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_GLOBAL_COMMON_FLAGS("-DYB_COMPILER_TYPE=$ENV{YB_COMPILER_TYPE}")
ADD_CXX_GLOBAL_COMMON_FLAGS("-DYB_COMPILER_VERSION=${COMPILER_VERSION}")
############################################################
# Compiler flags
############################################################
if(NOT APPLE)
# 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_GLOBAL_COMMON_FLAGS("-mno-avx -mno-bmi -mno-bmi2 -mno-fma")
endif()
# We want access to the PRI* print format macros.
ADD_CXX_GLOBAL_COMMON_FLAGS("-D__STDC_FORMAT_MACROS")
# Do not warn about uses of deprecated declarations. RocksDB has a few instances of those.
ADD_CXX_GLOBAL_COMMON_FLAGS("-Wno-deprecated-declarations")
ADD_CXX_GLOBAL_COMMON_FLAGS("-DGFLAGS=gflags")
# Don't allow virtual classes with non-virtual destructors.
ADD_CXX_GLOBAL_COMMON_FLAGS("-Wnon-virtual-dtor")
# Flags common to gcc and clang.
ADD_CXX_YB_COMMON_FLAGS("-Werror=enum-compare")
ADD_CXX_YB_COMMON_FLAGS("-Werror=reorder")
ADD_CXX_YB_COMMON_FLAGS("-Werror=switch")
ADD_CXX_YB_COMMON_FLAGS("-Werror=return-type")
ADD_CXX_YB_COMMON_FLAGS("-Werror=non-virtual-dtor")
if(IS_CLANG)
ADD_CXX_YB_COMMON_FLAGS("-Werror=string-plus-int")
ADD_CXX_YB_COMMON_FLAGS("-Werror=return-stack-address")
ADD_CXX_YB_COMMON_FLAGS("-Werror=implicit-fallthrough")
endif()
if(IS_CLANG AND NOT APPLE)
if ("${COMPILER_VERSION}" VERSION_GREATER "3.8")
# This is a new warning in Clang 3.9 (or at least it is not there in Clang 3.7). We need to fix
# RocksDB code, and we are disabling this until then.
# This warning also does not appear on Mac OS X's version of Clang, at least on darwin15.5.0.
# http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20160118/147239.html
ADD_CXX_GLOBAL_COMMON_FLAGS("-Wno-expansion-to-defined")
endif()
endif()
DETECT_LINUXBREW()
if(USING_LINUXBREW AND IS_CLANG)
# We only need these when building using clang. If we use Linuxbrew's gcc, it should set the
# equivalent of these options automatically.
ADD_COMMON_LINKER_FLAGS("-Wl,--dynamic-linker=${LINUXBREW_LIB_DIR}/ld.so")
ADD_COMMON_COMPILER_LINKER_FLAGS("--gcc-toolchain=${LINUXBREW_DIR}")
ADD_COMMON_COMPILER_LINKER_FLAGS("-Wno-gnu-designator")
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_GLOBAL_COMMON_FLAGS("-DOS_MACOSX")
endif()
# Explicitly disable the new gcc5 ABI. Until clang supports abi tags [1], any
# generated code (which always uses clang) must be built against the old ABI.
# (Note: we are not using any LLVM codegen in YugaByte as of 08/2016).
# There's no recourse for using both ABIs in the same process; gcc's advice [2]
# is to build everything against the old ABI.
#
# 1. https://llvm.org/bugs/show_bug.cgi?id=23529
# 2. https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
# We want short macros from util/status.h.
add_definitions(-DYB_HEADERS_USE_SHORT_STATUS_MACROS=1)
# Slice includes many gutil dependencies that third-party users of the YugaByte
# client library don't have. Our build has them, though.
add_definitions(-DYB_HEADERS_USE_RICH_SLICE=1)
# We don't want to use any stubs; that's exclusively for builds using our
# exported client headers).
add_definitions(-DYB_HEADERS_NO_STUBS=1)
# 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")
set(CXX_FLAGS_RELEASE "-O3 -g -DNDEBUG -fno-omit-frame-pointer")
if (NOT "${YB_USE_LTO}" STREQUAL "")
set(CXX_FLAGS_RELEASE "${CXX_FLAGS_RELEASE} flto -fno-use-linker-plugin")
endif()
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")
set(CMAKE_CXX_FLAGS ${CXX_FLAGS_DEBUG})
if(NOT APPLE AND EXISTS "/usr/bin/ld.gold")
if ("$ENV{YB_NO_LD_GOLD}")
message("YB_NO_LD_GOLD is defined, not using ld.gold")
else()
message("Not using ld.gold to stay compatible with Linuxbrew-based gcc toolchain")
if(FALSE)
set(LD_GOLD_FLAGS "-fuse-ld=gold")
# TODO: should we also add this to CMAKE_SHARED_LINKER_FLAGS?
message("Adding flags in front of CMAKE_EXE_LINKER_FLAGS to use ld.gold: ${LD_GOLD_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${LD_GOLD_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
endif()
endif()
endif()
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "FASTDEBUG")
set(CMAKE_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")
set(CMAKE_CXX_FLAGS ${CXX_FLAGS_RELEASE})
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "PROFILE_GEN")
set(CMAKE_CXX_FLAGS ${CXX_FLAGS_PROFILE_GEN})
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "PROFILE_BUILD")
set(CMAKE_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_YB_COMMON_FLAGS("-Wno-ambiguous-member-template")
# Emit warnings (that we then treat as errors in compiler-wrapper.sh) on unannotated fallthrough
# in switch statements. This applies to both YB and RocksDB parts of the code.
ADD_CXX_GLOBAL_COMMON_FLAGS("-Wimplicit-fallthrough")
# Silence the "unused argument" warning.
ADD_CXX_GLOBAL_COMMON_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_YB_COMMON_FLAGS("-fcolor-diagnostics")
else()
message("Running without a controlling terminal or in a dumb terminal")
endif()
elseif("${COMPILER_FAMILY}" STREQUAL "gcc")
# Blacklist gcc versions known to generate broken optimized code.
#
# See KUDU-1030 for more details.
if ("${COMPILER_VERSION}" MATCHES "^4.[67]")
message(FATAL_ERROR "Building with gcc version ${COMPILER_VERSION} is "
"forbidden as it is known to produce broken code in release mode. "
"Upgrade gcc, or build with clang from the thirdparty directory.")
endif()
# These flags are not understood by some versions of clang we are using.
ADD_CXX_GLOBAL_COMMON_FLAGS("-mno-abm -mno-movbe")
else()
message(SEND_ERROR "Unknown compiler family: ${COMPILER_FAMILY}")
endif()
# Figure out the right directory layout under "thirdparty".
if(EXISTS "${YB_THIRDPARTY_DIR}/installed/common")
message("Using new directory layout with thirdparty/installed/{common,uninstrumented,tsan}")
# Using new-style directory layout under thirdparty/installed.
set(YB_PREFIX_COMMON "${YB_THIRDPARTY_DIR}/installed/common")
set(YB_PREFIX_DEPS "${YB_THIRDPARTY_DIR}/installed/uninstrumented")
set(YB_PREFIX_DEPS_TSAN "${YB_THIRDPARTY_DIR}/installed/tsan")
else()
message("Using old directory layout with "
"thirdparty/{installed,installed-deps,installed-deps-tsan}")
# We must be using a prebuilt third-party package with the old-style directory hierarchy. We need
# to get rid of this once we fully migrate to the new directory layout.
set(YB_PREFIX_COMMON "${YB_THIRDPARTY_DIR}/installed")
set(YB_PREFIX_DEPS "${YB_THIRDPARTY_DIR}/installed-deps")
set(YB_PREFIX_DEPS_TSAN "${YB_THIRDPARTY_DIR}/installed-deps-tsan")
endif()
# This helps find the right third-party build directory.
set(THIRDPARTY_INSTRUMENTATION_TYPE "uninstrumented")
# Flag to enable thread sanitizer (clang or gcc 4.8)
if (${YB_USE_TSAN})
if(NOT (("${COMPILER_FAMILY}" STREQUAL "clang") OR
("${COMPILER_FAMILY}" STREQUAL "gcc" AND "${COMPILER_VERSION}" VERSION_GREATER "4.8")))
message(SEND_ERROR "Cannot use TSAN without clang or gcc >= 4.8")
endif()
set(THIRDPARTY_INSTRUMENTATION_TYPE "tsan")
add_definitions("-fsanitize=thread")
# Enables dynamic_annotations.h to actually generate code
add_definitions("-DDYNAMIC_ANNOTATIONS_ENABLED")
# changes atomicops to use the tsan implementations
add_definitions("-DTHREAD_SANITIZER")
# Disables using the precompiled template specializations for std::string, shared_ptr, etc
# so that the annotations in the header actually take effect.
add_definitions("-D_GLIBCXX_EXTERN_TEMPLATE=0")
# Compile and link against the thirdparty TSAN instrumented libstdcxx.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND NOT EXISTS "/etc/redhat-release")
message("This looks like a non-CentOS Linux platform. Assuming this is Ubuntu, not using "
"TSAN-instrumented libstdc++. This can result in some false positive TSAN test "
"failures.")
else()
# TSAN build under RedHat/CentOS Linux.
message("Using TSAN-instrumented libstdc++")
set(TSAN_GCC_DIR "${YB_PREFIX_DEPS_TSAN}/gcc")
set(TSAN_GCC_INCLUDE_DIR "${TSAN_GCC_DIR}/include/c++/4.9.3")
ADD_GLOBAL_RPATH_ENTRY("${TSAN_GCC_DIR}/lib")
ADD_CXX_GLOBAL_COMMON_FLAGS("-nostdinc++")
ADD_COMMON_COMPILER_LINKER_FLAGS("-L${TSAN_GCC_DIR}/lib")
# This needs to appear before adding third-party dependencies that have their headers in the
# Linuxbrew include directory, because otherwise we'll pick up the standard library headers from
# the Linuxbrew include directory too.
include_directories(SYSTEM "${TSAN_GCC_INCLUDE_DIR}")
include_directories(SYSTEM "${TSAN_GCC_INCLUDE_DIR}/backward")
ADD_COMMON_COMPILER_LINKER_FLAGS("-isystem ${TSAN_GCC_INCLUDE_DIR}")
ADD_COMMON_COMPILER_LINKER_FLAGS("-isystem ${TSAN_GCC_INCLUDE_DIR}/backward")
endif()
# Strictly speaking, TSAN doesn't require dynamic linking. But it does
# require all code to be position independent, and the easiest way to
# guarantee that is via dynamic linking (not all 3rd party archives are
# compiled with -fPIC e.g. boost).
if("${YB_LINK}" STREQUAL "a")
message("Using dynamic linking for TSAN")
set(YB_LINK "d")
elseif("${YB_LINK}" STREQUAL "s")
message(SEND_ERROR "Cannot use TSAN with static linking")
endif()
elseif(USING_LINUXBREW AND IS_CLANG)
# Tell clang to use Linuxbrew's libstdc++ instead of the system one.
ADD_CXX_GLOBAL_COMMON_FLAGS("-nostdinc++")
# TODO: autodetect standard library version installed through Linuxbrew automatically.
set(LINUXBREW_CXX_INCLUDE_DIR "${LINUXBREW_DIR}/Cellar/gcc/5.3.0/include/c++/5.3.0")
ADD_COMMON_COMPILER_LINKER_FLAGS("-isystem ${LINUXBREW_CXX_INCLUDE_DIR}")
ADD_COMMON_COMPILER_LINKER_FLAGS("-isystem ${LINUXBREW_CXX_INCLUDE_DIR}/backward")
ADD_COMMON_COMPILER_LINKER_FLAGS("-isystem ${LINUXBREW_CXX_INCLUDE_DIR}/x86_64-unknown-linux-gnu")
endif()
if (USING_LINUXBREW AND IS_CLANG)
ADD_CXX_GLOBAL_COMMON_FLAGS("-isystem ${LINUXBREW_DIR}/include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -latomic")
endif()
# Sanity check -- to be removed.
set(MYVAR FALSE)
if(MYVAR)
message(FATAL_ERROR "MYVAR should be FALSE")
endif()
if(NOT MYVAR)
message("OK")
else()
message(FATAL_ERROR "NOT MYVAR should be TRUE")
endif()
set(MYVAR TRUE)
if(NOT MYVAR)
message(FATAL_ERROR "NOT MYVAR should be FALSE")
endif()
if(MYVAR)
message("OK")
else()
message(FATAL_ERROR "MYVAR should be TRUE")
endif()
ADD_CXX_GLOBAL_COMMON_FLAGS("${COMMON_COMPILER_LINKER_FLAGS}")
# Add "global" flags (those shared between YB and RocksDB parts of the code) to YB's flags.
# After this point, modifying CXX_GLOBAL_COMMON_FLAGS and CXX_YB_COMMON_FLAGS will have no effect!
# TODO: record the value of these variables and check that it has not changed at the end of the
# file.
ADD_CXX_YB_COMMON_FLAGS("${CXX_GLOBAL_COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_YB_COMMON_FLAGS}")
# Make sure functions for adding CXX_GLOBAL_COMMON_FLAGS and CXX_YB_COMMON_FLAGS fail with a fatal
# error frmo this point on.
set(CAN_ADD_CXX_GLOBAL_COMMON_FLAGS FALSE)
set(CAN_ADD_CXX_YB_COMMON_FLAGS FALSE)
if (NOT "$ENV{YB_LINK}" STREQUAL "" AND NOT "$ENV{YB_LINK}" STREQUAL "${YB_LINK}")
if (YB_LINK)
message(FATAL_ERROR "Conflicting values of YB_LINK CMake variable (\"${YB_LINK}\") and "
"environment variable (\"$ENV{YB_LINK}\").")
endif()
message("YB_LINK is not set as a CMake variable, using the YB_LINK environment variable: "
"\"$ENV{YB_LINK}\".")
set(YB_LINK "$ENV{YB_LINK}")
endif()
# Sanity check linking option.
if (NOT YB_LINK)
message("YB_LINK not specified, determining linking type automatically")
set(YB_LINK "a")
elseif(NOT ("${YB_LINK}" MATCHES "^[a-z]+$" AND (
"auto" MATCHES "^${YB_LINK}" OR
"dynamic" MATCHES "^${YB_LINK}" OR
"static" MATCHES "^${YB_LINK}"
)))
message(FATAL_ERROR "Unknown value \"${YB_LINK}\" for YB_LINK, must be auto|dynamic|static")
else()
message("YB_LINK explicitly specified as \"${YB_LINK}\".")
# Remove all but the first letter.
string(SUBSTRING "${YB_LINK}" 0 1 YB_LINK)
endif()
message("YB_USE_ASAN=${YB_USE_ASAN}")
# Clang does not support using ASAN and TSAN simultaneously.
if ("${YB_USE_ASAN}" AND "${YB_USE_TSAN}")
message(SEND_ERROR "Can only enable one of ASAN or TSAN at a time")
endif()
# Flag to enable clang address sanitizer
# This will only build if clang or a recent enough gcc is the chosen compiler
if (${YB_USE_ASAN})
message("Using ASAN (Address Sanitizer)")
if(NOT (("${COMPILER_FAMILY}" STREQUAL "clang") OR
("${COMPILER_FAMILY}" STREQUAL "gcc" AND "${COMPILER_VERSION}" VERSION_GREATER "4.8")))
message(SEND_ERROR "Cannot use ASAN without clang or gcc >= 4.8")
endif()
# If UBSAN is also enabled, and we're on clang < 3.5, ensure static linking is
# enabled. Otherwise, we run into https://llvm.org/bugs/show_bug.cgi?id=18211
if("${YB_USE_UBSAN}" AND
"${COMPILER_FAMILY}" STREQUAL "clang" AND
"${COMPILER_VERSION}" VERSION_LESS "3.5")
if("${YB_LINK}" STREQUAL "a")
message("Using static linking for ASAN+UBSAN build")
set(YB_LINK "s")
elseif("${YB_LINK}" STREQUAL "d")
message(SEND_ERROR "Cannot use dynamic linking when ASAN and UBSAN are both enabled")
endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -DADDRESS_SANITIZER")
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_USE_UBSAN})
if(NOT (("${COMPILER_FAMILY}" STREQUAL "clang") OR
("${COMPILER_FAMILY}" STREQUAL "gcc" AND "${COMPILER_VERSION}" VERSION_GREATER "4.9")))
message(SEND_ERROR "Cannot use UBSAN without clang or gcc >= 4.9")
endif()
set(CXX_NO_SANITIZE_FLAG "alignment")
if(NOT APPLE)
set(CXX_NO_SANITIZE_FLAG "${CXX_NO_SANITIZE_FLAG},vptr")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
endif()
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -fno-sanitize-recover -fno-sanitize=${CXX_NO_SANITIZE_FLAG}")
endif ()
if ("${YB_USE_UBSAN}" OR "${YB_USE_ASAN}" OR "${YB_USE_TSAN}")
# GCC 4.8 and 4.9 (latest as of this writing) don't allow you to specify a
# sanitizer blacklist.
if("${COMPILER_FAMILY}" STREQUAL "clang")
# Require clang 3.4 or newer; clang 3.3 has issues with TSAN and pthread
# symbol interception.
if("${COMPILER_VERSION}" VERSION_LESS "3.4")
message(SEND_ERROR "Must use clang 3.4 or newer to run a sanitizer build."
" Try using clang from thirdparty/")
endif()
add_definitions("-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()
# Code coverage
if ("${YB_GENERATE_COVERAGE}")
if("${CMAKE_CXX_COMPILER}" MATCHES ".*clang.*")
# There appears to be some bugs in clang 3.3 which cause code coverage
# to have link errors, not locating the llvm_gcda_* symbols.
# This should be fixed in llvm 3.4 with http://llvm.org/viewvc/llvm-project?view=revision&revision=184666
message(SEND_ERROR "Cannot currently generate coverage with clang")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -DCOVERAGE_BUILD")
# For coverage to work properly, we need to use static linkage. Otherwise,
# __gcov_flush() doesn't properly flush coverage from every module.
# See http://stackoverflow.com/questions/28164543/using-gcov-flush-within-a-library-doesnt-force-the-other-modules-to-yield-gc
if("${YB_LINK}" STREQUAL "a")
message("Using static linking for coverage build")
set(YB_LINK "s")
elseif("${YB_LINK}" STREQUAL "d")
message(SEND_ERROR "Cannot use coverage with static linking")
endif()
endif()
# If we still don't know what kind of linking to perform, choose based on
# build type (developers like fast builds).
if ("${YB_LINK}" STREQUAL "a")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG" OR
"${CMAKE_BUILD_TYPE}" STREQUAL "FASTDEBUG" OR
"${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
# We use dynamic builds by default even in the release mode.
message("Using dynamic linking for ${CMAKE_BUILD_TYPE} builds")
set(YB_LINK "d")
else()
message("Using static linking for ${CMAKE_BUILD_TYPE} builds")
set(YB_LINK "s")
endif()
endif()
# Are we using the gold linker? It doesn't work with dynamic linking as
# weak symbols aren't properly overridden, causing tcmalloc to be omitted.
# Let's flag this as an error in RELEASE builds (we shouldn't release a
# product like this).
#
# See https://sourceware.org/bugzilla/show_bug.cgi?id=16979 for details.
#
# The gold linker is only for ELF binaries, which OSX doesn't use. We can
# just skip.
if (NOT APPLE)
set(LINKER_DETECTION_COMMAND "${CMAKE_CXX_COMPILER} ${CMAKE_EXE_LINKER_FLAGS} -Wl,--version")
# TODO: how do we execute a command line stored in a variable without interpreting the entire
# string (including spaces) as the command name? Currently, just using "bash -c".
execute_process(COMMAND bash -c "${LINKER_DETECTION_COMMAND}"
OUTPUT_VARIABLE LINKER_OUTPUT
ERROR_VARIABLE LINKER_DETECTION_ERROR_OUTPUT
RESULT_VARIABLE LINKER_DETECTION_EXIT_CODE)
if(NOT ${LINKER_DETECTION_EXIT_CODE} EQUAL 0)
message(FATAL_ERROR
"Linker detection command '${LINKER_DETECTION_COMMAND}' failed with exit code\
'${LINKER_DETECTION_EXIT_CODE}'. Error output: ${LINKER_DETECTION_ERROR_OUTPUT}")
endif()
message("LINKER_DETECTION_COMMAND=${LINKER_DETECTION_COMMAND}")
message("LINKER_OUTPUT=${LINKER_OUTPUT}")
endif ()
if (LINKER_OUTPUT MATCHES "gold")
if ("${YB_LINK}" STREQUAL "d" AND
"${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
message(SEND_ERROR "Cannot use gold with dynamic linking in a RELEASE build "
"as it would cause tcmalloc symbols to get dropped")
else()
message("Using gold linker")
endif()
set(YB_USING_GOLD 1)
else()
message("Using ld linker")
endif()
message("Linking type: ${YB_LINK} (d=dynamic, s=static)")
# Having set YB_LINK due to build type and/or sanitizer, it's now safe to
# act on its value.
if ("${YB_LINK}" STREQUAL "d")
set(BUILD_SHARED_LIBS ON)
# Position independent code is only necessary when producing shared objects.
add_definitions(-fPIC)
endif()
# 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 enterprise code.
include_directories(${CMAKE_CURRENT_BINARY_DIR}/src)
if ("${YB_EDITION}" STREQUAL "enterprise")
# Enterprise sources have higher priority than community code.
include_directories(ent/src)
add_definitions(-DYB_EDITION_NS_PREFIX=enterprise::)
else()
add_definitions(-DYB_EDITION_NS_PREFIX=)
endif()
include_directories(src)
############################################################
# Visibility
############################################################
# For generate_export_header() and add_compiler_export_flags().
include(GenerateExportHeader)
# Compile TIme REducer
# See https://github.com/sakra/cotire
include(cotire)
# add_library() wrapper that adds a second variant of the library for use in the
# exported YB C++ client. This variant is suffixed with "_exported" and is
# compiled with special visibility flags to hide all symbols except those that
# are part of the public ABI.
#
# There are two different kinds of exported libraries: internal and leaf.
# Internal libraries are static archives while leaf libraries are shared
# objects built from internal libraries. In practice there is only one leaf
# library: the YB C++ client itself.
#
# Arguments:
#