forked from yugabyte/yugabyte-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
1802 lines (1576 loc) · 70.9 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 includes FindICU module (https://cmake.org/cmake/help/v3.7/release/3.7.html)
cmake_minimum_required(VERSION 3.7.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_BREW)
# Detect Linuxbrew. The logic needs to be consistent with the detect_linuxbrew function in
# common-build-env.sh.
set(USING_LINUXBREW FALSE)
set(USING_CUSTOM_HOMEBREW FALSE)
if(APPLE)
set(CUSTOM_HOMEBREW_DIR "$ENV{YB_CUSTOM_HOMEBREW_DIR}")
if("${CUSTOM_HOMEBREW_DIR}" STREQUAL "")
set(CUSTOM_HOMEBREW_DIR "$ENV{HOME}/.linuxbrew-yb-build")
endif()
if(EXISTS "${CUSTOM_HOMEBREW_DIR}/bin" AND
EXISTS "${CUSTOM_HOMEBREW_DIR}/lib")
message("Custom Homebrew installation found at ${CUSTOM_HOMEBREW_DIR}")
set(USING_CUSTOM_HOMEBREW TRUE)
else()
message("Not using custom Homebrew: no valid Homebewr installation found at "
"${CUSTOM_HOMEBREW_DIR}")
endif()
else()
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()
if(NOT USING_CUSTOM_HOMEBREW)
set(CUSTOM_HOMEBREW_DIR "/tmp/not-using-custom-homebrew")
endif()
set(USING_LINUXBREW "${USING_LINUXBREW}" PARENT_SCOPE)
set(LINUXBREW_DIR "${LINUXBREW_DIR}" PARENT_SCOPE)
set(LINUXBREW_LIB_DIR "${LINUXBREW_DIR}/lib" PARENT_SCOPE)
set(USING_CUSTOM_HOMEBREW "${USING_CUSTOM_HOMEBREW}" PARENT_SCOPE)
set(CUSTOM_HOMEBREW_DIR "${CUSTOM_HOMEBREW_DIR}" PARENT_SCOPE)
set(CUSTOM_HOMEBREW_LIB_DIR "${CUSTOM_HOMEBREW_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)
get_filename_component(BUILD_ROOT_BASENAME "${CMAKE_CURRENT_BINARY_DIR}" NAME)
if ("$ENV{YB_COMPILER_TYPE}" STREQUAL "")
# TODO: deduplicate this. Also might want to detect edition based on dir name, etc.
string(REGEX MATCH "^.*-zapcc-.*$" RE_MATCH_RESULT "${BUILD_ROOT_BASENAME}")
if (NOT "${RE_MATCH_RESULT}" STREQUAL "")
set(ENV{YB_COMPILER_TYPE} "zapcc")
else()
string(REGEX MATCH "^.*-gcc-.*$" RE_MATCH_RESULT "${BUILD_ROOT_BASENAME}")
if (NOT "${RE_MATCH_RESULT}" STREQUAL "")
set(ENV{YB_COMPILER_TYPE} "gcc")
else()
string(REGEX MATCH "^.*-clang-.*$" RE_MATCH_RESULT "${BUILD_ROOT_BASENAME}")
if (NOT "${RE_MATCH_RESULT}" STREQUAL "")
set(ENV{YB_COMPILER_TYPE} "clang")
endif()
endif()
endif()
endif()
set(USING_SANITIZERS FALSE PARENT_SCOPE)
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")
set(USING_SANITIZERS TRUE PARENT_SCOPE)
endif()
if ("$ENV{YB_COMPILER_TYPE}" STREQUAL "")
set(ENV{YB_COMPILER_TYPE} "${COMPILER_FAMILY}")
endif()
if ("$ENV{YB_COMPILER_TYPE}" STREQUAL "zapcc")
if (NOT "${COMPILER_FAMILY}" STREQUAL "clang")
message(FATAL_ERROR
"Compiler type is zapcc but the compiler family is '${COMPILER_FAMILY}' "
"(expected to be clang)")
endif()
elseif (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()
message("YB_COMPILER_TYPE env var: $ENV{YB_COMPILER_TYPE}")
endfunction()
# Functions for adding compiler/linker flags ------------------------------------------------------
# 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.
function(ADD_LINKER_FLAGS FLAGS)
message("Adding to linker flags: ${FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAGS}" PARENT_SCOPE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FLAGS}" PARENT_SCOPE)
endfunction()
function(ADD_GLOBAL_RPATH_ENTRY RPATH_ENTRY)
if (RPATH_ENTRY STREQUAL "")
message(FATAL_ERROR "Trying to add an empty rpath entry.")
endif()
message("Adding a global rpath entry: ${RPATH_ENTRY}")
set(FLAGS "-Wl,-rpath,${RPATH_ENTRY}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAGS}" PARENT_SCOPE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${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.
function(ADD_CXX_FLAGS FLAGS)
message("Adding C++ flags: ${FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}" PARENT_SCOPE)
endfunction()
ADD_CXX_FLAGS("-Werror")
ADD_CXX_FLAGS("-fno-strict-aliasing -msse4.2 -Wall -Wno-sign-compare -Wno-deprecated")
ADD_CXX_FLAGS("-Winvalid-pch")
ADD_CXX_FLAGS("-pthread -DBOOST_BIND_NO_PLACEHOLDERS")
if (NOT APPLE)
ADD_CXX_FLAGS("-DBOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX")
endif()
ADD_CXX_FLAGS("-DROCKSDB_PLATFORM_POSIX -DROCKSDB_USING_THREAD_STATUS=0")
ADD_CXX_FLAGS("-DBOOST_ERROR_CODE_HEADER_ONLY")
# 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(YB_MATCHING_ENTERPRISE_DIR "${YB_ENTERPRISE_ROOT}/${CUR_REL_LIST_DIR}" PARENT_SCOPE)
set(YB_MATCHING_ENTERPRISE_DIR "${YB_ENTERPRISE_ROOT}/${CUR_REL_LIST_DIR}")
set(INCLUDED_PATH "${YB_MATCHING_ENTERPRISE_DIR}/${CUR_REL_LIST_NAME_NO_EXT}-include.txt")
message("Including '${INCLUDED_PATH}' into '${CMAKE_CURRENT_LIST_FILE}'")
include("${INCLUDED_PATH}")
endif()
endfunction()
set(YB_NUM_TESTS "0" CACHE INTERNAL "Number of tests" FORCE)
set(YB_NUM_INCLUDED_TESTS "0" CACHE INTERNAL "Number of included tests" FORCE)
set(YB_NUM_EXECUTABLES "0" CACHE INTERNAL "Number of executables" FORCE)
set(YB_NUM_INCLUDED_EXECUTABLES "0" CACHE INTERNAL "Number of included executables" FORCE)
set(YB_ALL_DEPS "" CACHE INTERNAL "All dependencies" FORCE)
# This is used to let the add_executable wrapper know if we're adding a test.
set(YB_ADDING_TEST_EXECUTABLE "FALSE" CACHE INTERNAL "" FORCE)
function(yb_remember_dependency target)
# We use \\n instead of a real newline as this is stored in the CMake cache, and some versions
# of CMake can't parse their own cache in case some values have newlines.
set(YB_ALL_DEPS "${YB_ALL_DEPS}\\n${target}: ${ARGN}" CACHE INTERNAL "All dependencies" FORCE)
endfunction()
# Wrap add_dependencies so that we can capture dependencies for external processing. We use this
# when determining what tests to run for a particular set of changes.
function(add_dependencies target)
if (TARGET "${target}" OR NOT ${YB_FILTERING_TARGETS})
yb_remember_dependency(${target} ${ARGN})
_add_dependencies(${target} ${ARGN})
endif()
endfunction()
function(target_link_libraries target)
if (TARGET "${target}" OR NOT ${YB_FILTERING_TARGETS})
yb_remember_dependency(${target} ${ARGN})
_target_link_libraries(${target} ${ARGN})
endif()
endfunction()
# We override add_executable to ensure that whenever any executable is built, the latest symlink is
# re-created, and also to filter the set of executables if -DYB_EXECUTABLE_FILTER_RE is specified.
# This filtering is useful to keep CLion responsive when only working on a subset of code including
# e.g. yb-master / yb-tserver and some tests.
function(add_executable name)
if (NOT ${YB_ADDING_TEST_EXECUTABLE})
# Count non-test executables.
math(EXPR NEW_NUM_EXECUTABLES "${YB_NUM_EXECUTABLES} + 1")
set(YB_NUM_EXECUTABLES "${NEW_NUM_EXECUTABLES}" CACHE INTERNAL "Number of executables" FORCE)
endif()
if (NOT "${YB_EXECUTABLE_FILTER_RE}" STREQUAL "" AND
NOT ${YB_ADDING_TEST_EXECUTABLE} AND
NOT "${name}" STREQUAL "bfql_codegen" AND
NOT "${name}" STREQUAL "bfpg_codegen" AND
NOT "${name}" STREQUAL "run-with-timeout" AND
NOT "${name}" STREQUAL "protoc-gen-insertions" AND
NOT "${name}" STREQUAL "protoc-gen-yrpc")
# Only do this filtering for non-test executables. Tests can be filtered separately using
# YB_TEST_FILTER_RE.
string(REGEX MATCH "${YB_EXECUTABLE_FILTER_RE}" EXECUTABLE_FILTER_MATCH_RESULT "${name}")
if ("${EXECUTABLE_FILTER_MATCH_RESULT}" STREQUAL "")
return()
endif()
message("Executable matched the filter: ${name}")
endif()
if (NOT ${YB_ADDING_TEST_EXECUTABLE})
math(EXPR NEW_NUM_INCLUDED_EXECUTABLES "${YB_NUM_INCLUDED_EXECUTABLES} + 1")
set(YB_NUM_INCLUDED_EXECUTABLES "${NEW_NUM_INCLUDED_EXECUTABLES}" CACHE INTERNAL
"Number of included executables" FORCE)
endif()
# Call through to the original add_executable function.
_add_executable("${name}" ${ARGN})
if (NOT "$ENV{YB_DISABLE_LATEST_SYMLINK}" STREQUAL "1")
add_dependencies(${name} latest_symlink)
endif()
endfunction()
macro(YB_SETUP_SANITIZER SANITIZER)
if(NOT (("${COMPILER_FAMILY}" STREQUAL "clang") OR
("${COMPILER_FAMILY}" STREQUAL "gcc" AND "${COMPILER_VERSION}" VERSION_GREATER "4.8")))
message(FATAL_ERROR "Cannot use ${SANITIZER} without clang or gcc >= 4.8")
endif()
string(TOLOWER "${SANITIZER}" LOWER_SANITIZER)
ADD_CXX_FLAGS("-stdlib=libc++")
# Disables using the precompiled template specializations for std::string, shared_ptr, etc
# so that the annotations in the header actually take effect.
ADD_CXX_FLAGS("-D_GLIBCXX_EXTERN_TEMPLATE=0")
message("Using ${SANITIZER}-instrumented libc++")
set(LIBCXX_DIR "${YB_THIRDPARTY_DIR}/installed/${LOWER_SANITIZER}/libcxx")
set(LIBCXX_INCLUDE_DIR "${LIBCXX_DIR}/include/c++/v1")
ADD_GLOBAL_RPATH_ENTRY("${LIBCXX_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 "${LIBCXX_INCLUDE_DIR}")
ADD_CXX_FLAGS("-nostdinc++")
ADD_LINKER_FLAGS("-L${LIBCXX_DIR}/lib")
# 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 ${SANITIZER}")
set(YB_LINK "d")
elseif("${YB_LINK}" STREQUAL "s")
message(FATAL_ERROR "Cannot use ${SANITIZER} with static linking")
endif()
endmacro()
function(SHOW_FOUND_BOOST_DETAILS BOOST_LIBRARY_TYPE)
message("Results of finding Boost ${BOOST_LIBRARY_TYPE} libraries:")
message(" Boost_FOUND: ${Boost_FOUND}")
message(" Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(" Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
message(" Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(" Boost_SYSTEM_FOUND: ${Boost_SYSTEM_FOUND}")
message(" Boost_SYSTEM_LIBRARY: ${Boost_SYSTEM_LIBRARY}")
message(" Boost_THREAD_FOUND: ${Boost_THREAD_FOUND}")
message(" Boost_THREAD_LIBRARY: ${Boost_THREAD_LIBRARY}")
message(" Boost_VERSION: ${Boost_VERSION}")
message(" Boost_LIB_VERSION: ${Boost_LIB_VERSION}")
message(" Boost_MAJOR_VERSION: ${Boost_MAJOR_VERSION}")
message(" Boost_MINOR_VERSION: ${Boost_MINOR_VERSION}")
message(" Boost_SUBMINOR_VERSION: ${Boost_SUBMINOR_VERSION}")
message(" Boost_LIB_DIAGNOSTIC_DEFINITIONS: ${Boost_LIB_DIAGNOSTIC_DEFINITIONS}")
endfunction()
# ------------------------------------------------------------------------------------------------
# Main build
# 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}")
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()
message("YB_THIRDPARTY_DIR: ${YB_THIRDPARTY_DIR}")
set(YB_BUILD_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
get_filename_component(YB_BUILD_ROOT_PARENT "${YB_BUILD_ROOT}" DIRECTORY)
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})
set(BUILD_SUPPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build-support)
# 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>
# ...
set(LATEST_BUILD_SYMLINK_PATH "${YB_BUILD_ROOT_PARENT}/latest")
if (NOT "$ENV{YB_DISABLE_LATEST_SYMLINK}" STREQUAL "1")
message("LATEST SYMLINK PATH: ${LATEST_BUILD_SYMLINK_PATH}")
if ("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${LATEST_BUILD_SYMLINK_PATH}")
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
"${BUILD_SUPPORT_DIR}/create_latest_symlink.sh"
"${CMAKE_CURRENT_BINARY_DIR}"
"${LATEST_BUILD_SYMLINK_PATH}"
COMMENT "Recreating the 'latest' symlink at '${LATEST_BUILD_SYMLINK_PATH}'")
endif()
add_custom_target(dummy_target ALL
cat /dev/null
COMMENT "Dummy target for dependency resolution testing")
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(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)
# Also allow specifying -DNO_REBUILD_THIRDPARTY, because CLion does not always pass user-specified
# environment variables correctly.
if ("${YB_NO_REBUILD_THIRDPARTY}" STREQUAL "1")
set(ENV{NO_REBUILD_THIRDPARTY} "1")
endif()
# This helps find the right third-party build directory.
if (YB_USE_TSAN)
set(THIRDPARTY_INSTRUMENTATION_TYPE "tsan")
elseif (YB_USE_ASAN)
set(THIRDPARTY_INSTRUMENTATION_TYPE "asan")
else()
set(THIRDPARTY_INSTRUMENTATION_TYPE "uninstrumented")
endif()
# Make sure thirdparty stuff is up-to-date.
if ("$ENV{NO_REBUILD_THIRDPARTY}" STREQUAL "")
set(BUILD_THIRDPARTY_ARGS --build-type ${THIRDPARTY_INSTRUMENTATION_TYPE})
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" OR
"$ENV{YB_RUN_AFFECTED_TESTS_ONLY}" 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}")
# We can only use IS_CLANG after calling VALIDATE_COMPILER_TYPE.
VALIDATE_COMPILER_TYPE()
if (NOT APPLE)
# 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}")
if ($ENV{YB_COMPILER_TYPE} STREQUAL "zapcc")
ADD_CXX_FLAGS("-DYB_ZAPCC")
endif()
############################################################
# 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_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")
# Remove this check when all Clang builds will use libc++.
if("${USING_SANITIZERS}" OR APPLE)
ADD_CXX_FLAGS("-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS")
ADD_CXX_FLAGS("-Wthread-safety-analysis")
endif()
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()
# The actual static analysis is turned on using the --analyze flag.
endif()
DETECT_BREW()
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_LINKER_FLAGS("-Wl,--dynamic-linker=${LINUXBREW_LIB_DIR}/ld.so")
ADD_LINKER_FLAGS("--gcc-toolchain=${LINUXBREW_DIR}")
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()
if(USING_CUSTOM_HOMEBREW)
set(CMAKE_PREFIX_PATH "${CUSTOM_HOMEBREW_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")
elseif(NOT "${USING_SANITIZERS}")
message("Specifying linker flags to not allow any undefined symbols")
ADD_LINKER_FLAGS("-Wl,--no-undefined -Wl,--no-allow-shlib-undefined")
else()
message("Not specifying linker flags to not allow any undefined symbols: this is ASAN/TSAN")
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_CXX_FLAGS(-D_GLIBCXX_USE_CXX11_ABI=0)
# We don't want to use any stubs; that's exclusively for builds using our
# exported client headers).
ADD_CXX_FLAGS(-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 "${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 "${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 "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_RELEASE}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "PROFILE_GEN")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_PROFILE_GEN}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "PROFILE_BUILD")
set(CMAKE_CXX_FLAGS "${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_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_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()
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_FLAGS("-mno-abm -mno-movbe")
else()
message(FATAL_ERROR "Unknown compiler family: ${COMPILER_FAMILY}")
endif()
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")
set(YB_PREFIX_DEPS_ASAN "${YB_THIRDPARTY_DIR}/installed/asan")
# Flag to enable thread sanitizer (clang or gcc 4.8)
if (${YB_USE_TSAN})
YB_SETUP_SANITIZER("TSAN")
ADD_CXX_FLAGS("-fsanitize=thread")
# Enables dynamic_annotations.h to actually generate code
ADD_CXX_FLAGS("-DDYNAMIC_ANNOTATIONS_ENABLED")
# changes atomicops to use the tsan implementations
ADD_CXX_FLAGS("-DTHREAD_SANITIZER")
# Compile and link against the thirdparty TSAN instrumented libstdcxx.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
elseif (${YB_USE_ASAN})
YB_SETUP_SANITIZER("ASAN")
ADD_CXX_FLAGS("-fsanitize=address")
ADD_CXX_FLAGS("-DADDRESS_SANITIZER")
# Compile and link against the thirdparty ASAN instrumented libstdcxx.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
elseif (USING_LINUXBREW AND IS_CLANG)
# Tell clang to use Linuxbrew's libstdc++ instead of the system one.
ADD_CXX_FLAGS("-nostdinc++")
ADD_LINKER_FLAGS("-L${LINUXBREW_LIB_DIR}")
execute_process(COMMAND "${LINUXBREW_DIR}/bin/brew" list --versions gcc
OUTPUT_VARIABLE LINUXBREW_GCC_VERSION_OUTPUT
ERROR_VARIABLE LINUXBREW_GCC_VERSION_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)
string(REGEX REPLACE "gcc[ ]+(.*)$" "\\1" LINUXBREW_GCC_VERSION "${LINUXBREW_GCC_VERSION_OUTPUT}")
string(REGEX REPLACE "^(.*)_[^_]+$" "\\1" LINUXBREW_GCC_MAJOR_VERSION "${LINUXBREW_GCC_VERSION}")
message("Brew GCC version: ${LINUXBREW_GCC_VERSION}")
set(LINUXBREW_CXX_INCLUDE_DIR
"${LINUXBREW_DIR}/Cellar/gcc/${LINUXBREW_GCC_VERSION}/include/c++/\
${LINUXBREW_GCC_MAJOR_VERSION}")
if (NOT IS_DIRECTORY ${LINUXBREW_CXX_INCLUDE_DIR})
message(FATAL_ERROR "Fetching gcc version stdout: \n${LINUXBREW_GCC_VERSION_OUTPUT}\n\
Fetching gcc version stderr: \n${LINUXBREW_GCC_VERSION_ERROR}\n\
GCC include dir tried: ${LINUXBREW_CXX_INCLUDE_DIR}")
endif()
include_directories(SYSTEM ${LINUXBREW_CXX_INCLUDE_DIR})
include_directories(SYSTEM ${LINUXBREW_CXX_INCLUDE_DIR}/backward)
include_directories(SYSTEM ${LINUXBREW_CXX_INCLUDE_DIR}/x86_64-unknown-linux-gnu)
endif()
if (USING_LINUXBREW AND IS_CLANG)
include_directories(SYSTEM ${LINUXBREW_DIR}/include)
ADD_LINKER_FLAGS("-latomic")
endif()
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()
# 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 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_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()
# 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 (YB_INSTRUMENT_FUNCTIONS)
# We parse this special-case preprocessor definition flag in compiler-wrapper and add
# -finstrument-functions if input files match this pattern. Another consequence of passing this
# to the compiler wrapper through the command line is that changing this causes all files to be
# recompiled, which is necessary in the common case to ensure proper instrumentation or lack
# thereof.
#
# Need to escape parentheses and pipe in the regex.
# add_cxx_flags("-DYB_INSTRUMENT_FUNCTIONS_REL_PATH_RE=src/\\(yb\\|postgres\\)/")
add_cxx_flags("-DYB_INSTRUMENT_FUNCTIONS_REL_PATH_RE=^postgres_build/")
# Some functions in PostgreSQL are detected as not returning anything in this mode.
add_cxx_flags("-Wno-error=return-type")
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")