forked from yugabyte/yugabyte-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-build-env.sh
2717 lines (2419 loc) · 88.9 KB
/
common-build-env.sh
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
#!/usr/bin/env bash
# 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.
#
# This is common between build and test scripts.
set -euo pipefail
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
echo "${BASH_SOURCE[0]} must be sourced, not executed" >&2
exit 1
fi
# Guard against multiple inclusions.
if [[ -n ${YB_COMMON_BUILD_ENV_SOURCED:-} ]]; then
# Return to the executing script.
return
fi
readonly YB_COMMON_BUILD_ENV_SOURCED=1
# -------------------------------------------------------------------------------------------------
# Functions used during initialization
# -------------------------------------------------------------------------------------------------
set_yb_src_root() {
export YB_SRC_ROOT=$1
YB_BUILD_SUPPORT_DIR=$YB_SRC_ROOT/build-support
if [[ ! -d $YB_SRC_ROOT ]]; then
fatal "YB_SRC_ROOT directory '$YB_SRC_ROOT' does not exist"
fi
YB_COMPILER_WRAPPER_CC=$YB_BUILD_SUPPORT_DIR/compiler-wrappers/cc
YB_COMPILER_WRAPPER_CXX=$YB_BUILD_SUPPORT_DIR/compiler-wrappers/c++
yb_java_project_dirs=( "$YB_SRC_ROOT/java" )
}
# Puts the current Git SHA1 in the current directory into the current_sha1 variable.
# Remember, this variable could also be local to the calling function.
get_current_sha1() {
current_sha1=$( git rev-parse HEAD )
if [[ ! $current_sha1 =~ ^[0-9a-f]{40}$ ]]; then
# We can't use the "fatal" function yet.
echo >&2 "Could not get current Git SHA1 in $PWD"
exit 1
fi
}
initialize_yugabyte_bash_common() {
local target_sha1
target_sha1=$(<"$YB_SRC_ROOT/build-support/yugabyte-bash-common-sha1.txt")
if [[ ! $target_sha1 =~ ^[0-9a-f]{40}$ ]]; then
echo >&2 "Invalid yugabyte-bash-common SHA1: $target_sha1"
exit 1
fi
# Put this submodule-like directory under "build".
YB_BASH_COMMON_DIR=$YB_SRC_ROOT/build/yugabyte-bash-common
if [[ ! -d $YB_BASH_COMMON_DIR ]]; then
mkdir -p "$YB_SRC_ROOT/build"
git clone https://github.com/yugabyte/yugabyte-bash-common.git "$YB_BASH_COMMON_DIR"
fi
pushd "$YB_BASH_COMMON_DIR" >/dev/null
local current_sha1
get_current_sha1
if [[ $current_sha1 != "$target_sha1" ]]; then
if ! ( set -x; git checkout "$target_sha1" ); then
(
set -x
git fetch
git checkout "$target_sha1"
)
fi
get_current_sha1
if [[ $current_sha1 != "$target_sha1" ]]; then
echo >&2 "Failed to check out target SHA1 $target_sha1 in directory $PWD." \
"Current SHA1: $current_sha1."
exit 1
fi
fi
popd >/dev/null
}
# This script is expected to be in build-support, a subdirectory of the repository root directory.
set_yb_src_root "$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"
if [[ $YB_SRC_ROOT == */ ]]; then
fatal "YB_SRC_ROOT ends with '/' (not allowed): '$YB_SRC_ROOT'"
fi
initialize_yugabyte_bash_common
# shellcheck source=build/yugabyte-bash-common/src/yugabyte-bash-common.sh
. "$YB_BASH_COMMON_DIR/src/yugabyte-bash-common.sh"
# -------------------------------------------------------------------------------------------------
# Constants
# -------------------------------------------------------------------------------------------------
declare -i MAX_JAVA_BUILD_ATTEMPTS=5
# Reuse the C errno value for this.
# shellcheck disable=SC2034
declare -r -i YB_EXIT_CODE_NO_SUCH_FILE_OR_DIRECTORY=2
readonly YB_JENKINS_NFS_HOME_DIR=/Volumes/n/jenkins
# This is the parent directory for all kinds of thirdparty and toolchain tarballs.
readonly OPT_YB_BUILD_DIR="/opt/yb-build"
readonly LOCAL_THIRDPARTY_DIR_PARENT="$OPT_YB_BUILD_DIR/thirdparty"
readonly YSQL_SNAPSHOTS_DIR_PARENT="$OPT_YB_BUILD_DIR/ysql-sys-catalog-snapshots"
# Parent directories for different compiler toolchains that we know how to download and install.
readonly TOOLCHAIN_PARENT_DIR_LINUXBREW="$OPT_YB_BUILD_DIR/brew"
readonly TOOLCHAIN_PARENT_DIR_LLVM="$OPT_YB_BUILD_DIR/llvm"
readonly LOCAL_DOWNLOAD_DIR="${LOCAL_DOWNLOAD_DIR:-$OPT_YB_BUILD_DIR/download_cache}"
# The assumed number of cores per build worker. This is used in the default make parallelism level
# calculation in yb_build.sh. This does not have to be the exact number of cores per worker, but
# will affect whether or not we force the auto-scaling group of workers to expand.
readonly YB_NUM_CORES_PER_BUILD_WORKER=8
# The "number of build workers" that we'll end up using to compute the parallelism (by multiplying
# it by YB_NUM_CORES_PER_BUILD_WORKER) will be first brought into this range.
readonly MIN_EFFECTIVE_NUM_BUILD_WORKERS=5
readonly MAX_EFFECTIVE_NUM_BUILD_WORKERS=10
if [[ -z ${is_run_test_script:-} ]]; then
is_run_test_script=false
fi
readonly is_run_test_script
# Setting this to "true" will prevent any changes to the virtualenv (creating it or installing
# modules into it) as part of activate_virtualenv.
yb_readonly_virtualenv=false
YB_NFS_DOWNLOAD_CACHE_DIR=${YB_NFS_DOWNLOAD_CACHE_DIR:-$YB_JENKINS_NFS_HOME_DIR/download_cache}
readonly -a VALID_BUILD_TYPES=(
asan
compilecmds
debug
fastdebug
release
tsan
tsan_slow
pvs
prof_gen
prof_use
)
make_regex_from_list VALID_BUILD_TYPES "${VALID_BUILD_TYPES[@]}"
# Valid values of CMAKE_BUILD_TYPE passed to the top-level CMake build. This is the same as the
# above with the exclusion of ASAN/TSAN.
readonly -a VALID_CMAKE_BUILD_TYPES=(
debug
fastdebug
release
)
make_regex_from_list VALID_CMAKE_BUILD_TYPES "${VALID_CMAKE_BUILD_TYPES[@]}"
readonly -a VALID_COMPILER_TYPES=(
gcc
gcc11
gcc12
clang
clang14
clang15
clang16
)
make_regex_from_list VALID_COMPILER_TYPES "${VALID_COMPILER_TYPES[@]}"
readonly -a VALID_LINKING_TYPES=(
dynamic
thin-lto
full-lto
)
make_regex_from_list VALID_LINKING_TYPES "${VALID_LINKING_TYPES[@]}"
readonly -a VALID_ARCHITECTURES=(
x86_64
aarch64
arm64
)
make_regex_from_list VALID_ARCHITECTURES "${VALID_ARCHITECTURES[@]}"
readonly BUILD_ROOT_BASENAME_RE=\
"^($VALID_BUILD_TYPES_RAW_RE)-\
($VALID_COMPILER_TYPES_RAW_RE)\
(-linuxbrew)?\
(-($VALID_LINKING_TYPES_RAW_RE))\
(-($VALID_ARCHITECTURES_RAW_RE))?\
(-ninja)?\
(-clion)?$"
declare -i -r DIRECTORY_EXISTENCE_WAIT_TIMEOUT_SEC=100
declare -i -r YB_DOWNLOAD_LOCK_TIMEOUT_SEC=120
readonly YB_DOWNLOAD_LOCKS_DIR=/tmp/yb_download_locks
readonly YB_NFS_PATH_RE="^/(n|z|u|net|Volumes/net|servers|nfusr)/"
if is_mac; then
if [[ -x /usr/local/bin/flock ]]; then
readonly FLOCK="/usr/local/bin/flock"
readonly FLOCK_MSG="File locked"
else
readonly FLOCK="/usr/bin/true"
readonly FLOCK_MSG="Skipped file lock on macOS"
fi
else
readonly FLOCK="/usr/bin/flock"
readonly FLOCK_MSG="File locked"
fi
readonly DELAY_ON_BUILD_WORKERS_LIST_HTTP_ERROR_SEC=0.5
declare -i -r MAX_ATTEMPTS_TO_GET_BUILD_WORKER=10
readonly YB_VIRTUALENV_BASENAME=venv
# -------------------------------------------------------------------------------------------------
# Maven related constants
# -------------------------------------------------------------------------------------------------
readonly YB_DEFAULT_MVN_LOCAL_REPO=$HOME/.m2/repository
readonly YB_SHARED_MVN_SETTINGS_PATH="$YB_JENKINS_NFS_HOME_DIR/m2_settings.xml"
readonly YB_DEFAULT_MVN_SETTINGS_PATH=$HOME/.m2/settings.xml
# What matches these expressions will be filtered out of Maven output.
MVN_OUTPUT_FILTER_REGEX='^\[INFO\] (Download(ing|ed)( from [-a-z0-9.]+)?): '
MVN_OUTPUT_FILTER_REGEX+='|^\[INFO\] [^ ]+ already added, skipping$'
MVN_OUTPUT_FILTER_REGEX+='|^\[INFO\] Copying .*[.]jar to .*[.]jar$'
MVN_OUTPUT_FILTER_REGEX+='|^\[INFO\] Resolved: .*$'
MVN_OUTPUT_FILTER_REGEX+='|^\[INFO\] Resolved plugin: .*$'
MVN_OUTPUT_FILTER_REGEX+='|^\[INFO\] Resolved dependency: .*$'
MVN_OUTPUT_FILTER_REGEX+='|^\[INFO\] Installing .* to .*$'
MVN_OUTPUT_FILTER_REGEX+='|^Generating .*[.]html[.][.][.]$'
readonly MVN_OUTPUT_FILTER_REGEX
# This is used in yb_build.sh and build-and-test.sh.
# shellcheck disable=SC2034
readonly -a MVN_OPTS_TO_DOWNLOAD_ALL_DEPS=(
dependency:go-offline
dependency:resolve
dependency:resolve-plugins
-DoutputFile=/dev/null
)
# -------------------------------------------------------------------------------------------------
# Global variables
# -------------------------------------------------------------------------------------------------
# This is needed so we can ignore thirdparty_path.txt and linuxbrew_path.txt
# in the build directory and not pick up old paths from those files in
# a clean build.
is_clean_build=false
# A human-readable description of how we set the respective variables.
yb_thirdparty_dir_origin=""
if [[ -n ${YB_THIRDPARTY_DIR:-} ]]; then
yb_thirdparty_dir_origin="from environment"
fi
yb_thirdparty_url_origin=""
if [[ -n ${YB_THIRDPARTY_URL:-} ]]; then
yb_thirdparty_url_origin="from environment"
fi
yb_linuxbrew_dir_origin=""
if [[ -n ${YB_LINUXBREW_DIR:-} ]]; then
yb_linuxbrew_dir_origin="from environment"
fi
yb_llvm_toolchain_url_origin=""
if [[ -n ${YB_LLVM_TOOLCHAIN_URL:-} ]]; then
yb_llvm_toolchain_url_origin="from environment"
fi
yb_llvm_toolchain_dir_origin=""
if [[ -n ${YB_LLVM_TOOLCHAIN_DIR:-} ]]; then
yb_llvm_toolchain_dir_origin="from environment"
fi
# To deduplicate Maven arguments
yb_mvn_parameters_already_set=false
is_apple_silicon="" # Will be set to true or false when necessary.
# -------------------------------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------------------------------
yb_activate_debug_mode() {
PS4='[${BASH_SOURCE[0]}:${LINENO} ${FUNCNAME[0]:-}] '
set -x
}
normalize_build_type() {
if [[ -z ${build_type:-} ]]; then
if [[ -n ${BUILD_TYPE:-} ]]; then
build_type=$BUILD_TYPE
else
fatal "Neither build_type or BUILD_TYPE are set"
fi
fi
validate_build_type "$build_type"
local lowercase_build_type
lowercase_build_type=$( echo "$build_type" | to_lowercase )
if [[ "$build_type" != "$lowercase_build_type" ]]; then
# Only assign if we actually need to, because the build_type variable may already be read-only.
build_type=$lowercase_build_type
fi
}
decide_whether_to_use_linuxbrew() {
expect_vars_to_be_set YB_COMPILER_TYPE build_type
if [[ -z ${YB_USE_LINUXBREW:-} ]]; then
if [[ -n ${predefined_build_root:-} ]]; then
if [[ ${predefined_build_root##*/} == *-linuxbrew-* ]]; then
YB_USE_LINUXBREW=1
fi
elif [[ -n ${YB_LINUXBREW_DIR:-} ||
( ${YB_COMPILER_TYPE} =~ ^clang[0-9]+$ &&
$build_type =~ ^(release|prof_(gen|use))$ &&
"$( uname -m )" == "x86_64" &&
${OSTYPE} =~ ^linux.*$ ) ]] && ! is_ubuntu; then
YB_USE_LINUXBREW=1
fi
export YB_USE_LINUXBREW=${YB_USE_LINUXBREW:-0}
fi
}
# Sets the build directory based on the given build type (the build_type variable) and the value of
# the YB_COMPILER_TYPE environment variable.
set_build_root() {
detect_architecture
if [[ ${1:-} == "--no-readonly" ]]; then
local -r make_build_root_readonly=false
shift
else
local -r make_build_root_readonly=true
fi
expect_num_args 0 "$@"
normalize_build_type
readonly build_type
if [[ -z ${YB_COMPILER_TYPE:-} ]]; then
fatal "YB_COMPILER_TYPE is not set"
fi
validate_compiler_type "$YB_COMPILER_TYPE"
BUILD_ROOT=$YB_BUILD_PARENT_DIR/$build_type-$YB_COMPILER_TYPE
decide_whether_to_use_linuxbrew
if using_linuxbrew; then
BUILD_ROOT+="-linuxbrew"
fi
BUILD_ROOT+="-${YB_LINKING_TYPE:-dynamic}"
if is_apple_silicon; then
# Append the target architecture (x86_64 or arm64).
BUILD_ROOT+=-${YB_TARGET_ARCH}
fi
if using_ninja; then
BUILD_ROOT+="-ninja"
fi
normalize_build_root
if [[ ${make_build_root_readonly} == "true" ]]; then
readonly BUILD_ROOT
fi
if [[ -n ${predefined_build_root:-} &&
$predefined_build_root != "$BUILD_ROOT" ]] &&
! "$YB_BUILD_SUPPORT_DIR/is_same_path.py" "$predefined_build_root" "$BUILD_ROOT"; then
fatal "An inconsistency between predefined BUILD_ROOT ('$predefined_build_root') and" \
"computed BUILD_ROOT ('$BUILD_ROOT')."
fi
export BUILD_ROOT
export YB_BUILD_ROOT=$BUILD_ROOT
decide_whether_to_use_ninja
}
# Resolve the BUILD_ROOT symlink and save the result to the real_build_root_path variable.
set_real_build_root_path() {
if [[ -h $BUILD_ROOT ]]; then
real_build_root_path=$( readlink "$BUILD_ROOT" )
else
real_build_root_path="$BUILD_ROOT"
fi
real_build_root_path=$( cd "$real_build_root_path" && pwd )
readonly real_build_root_path
}
ensure_build_root_is_set() {
if [[ -z ${BUILD_ROOT:-} ]]; then
fatal "The BUILD_ROOT environment variable is not set. This must point to the absolute path" \
"of the build root directory, e.g. '<yugabyte_src_dir>/build/debug'."
fi
}
ensure_build_root_exists() {
ensure_build_root_is_set
if [[ ! -d $BUILD_ROOT ]]; then
fatal "The directory BUILD_ROOT ('$BUILD_ROOT') does not exist"
fi
}
normalize_build_root() {
ensure_build_root_is_set
if [[ -d $BUILD_ROOT ]]; then
BUILD_ROOT=$( cd "$BUILD_ROOT" && pwd )
fi
}
validate_build_type() {
expect_num_args 1 "$@"
# Local variable named _build_type to avoid a collision with the global build_type variable.
local _build_type=$1
if ! is_valid_build_type "$_build_type"; then
fatal "Invalid build type: '$_build_type'. Valid build types are: ${VALID_BUILD_TYPES[*]}" \
"(case-insensitive)."
fi
}
is_valid_build_type() {
expect_num_args 1 "$@"
local -r _build_type=$( echo "$1" | to_lowercase )
[[ "$_build_type" =~ $VALID_BUILD_TYPES_RE ]]
}
set_build_type_based_on_jenkins_job_name() {
if [[ -n "${build_type:-}" ]]; then
if [[ -n "${JOB_NAME:-}" ]]; then
# This message only makes sense if JOB_NAME is set.
log "Build type is already set to '$build_type', not setting it based on Jenkins job name."
fi
normalize_build_type
readonly build_type
return
fi
build_type=debug
if [[ -z "${JOB_NAME:-}" ]]; then
log "Using build type '$build_type' by default because JOB_NAME is not set."
readonly build_type
return
fi
local _build_type # to avoid collision with the global build_type variable
local jenkins_job_name
jenkins_job_name=$( echo "$JOB_NAME" | to_lowercase )
for _build_type in "${VALID_BUILD_TYPES[@]}"; do
if [[ "-$jenkins_job_name-" =~ [-_]${_build_type}[-_] ]]; then
log "Using build type '$_build_type' based on Jenkins job name '$JOB_NAME'."
readonly build_type=$_build_type
return
fi
done
readonly build_type
log "Using build type '$build_type' by default: could not determine from Jenkins job name" \
"'$JOB_NAME'."
}
set_default_compiler_type() {
if [[ -z ${YB_COMPILER_TYPE:-} ]]; then
if is_mac; then
YB_COMPILER_TYPE=clang
adjust_compiler_type_on_mac
elif [[ $OSTYPE =~ ^linux ]]; then
detect_architecture
YB_COMPILER_TYPE=clang15
else
fatal "Cannot set default compiler type on OS $OSTYPE"
fi
export YB_COMPILER_TYPE
readonly YB_COMPILER_TYPE
fi
}
is_clang() {
if [[ $YB_COMPILER_TYPE == clang* ]]; then
return 0
else
return 1
fi
}
is_gcc() {
if [[ $YB_COMPILER_TYPE == "gcc" ]]; then
return 0
else
return 1
fi
}
is_ubuntu() {
[[ -f /etc/issue ]] && grep -q Ubuntu /etc/issue
}
set_compiler_type_based_on_jenkins_job_name() {
if [[ -n "${YB_COMPILER_TYPE:-}" ]]; then
if [[ -n "${JOB_NAME:-}" ]]; then
log "The YB_COMPILER_TYPE variable is already set to '${YB_COMPILER_TYPE}', not setting it" \
"based on the Jenkins job name."
fi
else
local compiler_type
local jenkins_job_name
jenkins_job_name=$( echo "$JOB_NAME" | to_lowercase )
YB_COMPILER_TYPE=""
for compiler_type in "${VALID_COMPILER_TYPES[@]}"; do
if [[ "-$jenkins_job_name-" =~ [-_]${compiler_type}[-_] ]]; then
log "Setting YB_COMPILER_TYPE='$compiler_type' based on Jenkins job name '$JOB_NAME'."
YB_COMPILER_TYPE=$compiler_type
break
fi
done
if [[ -z "$YB_COMPILER_TYPE" ]]; then
log "Could not determine compiler type from Jenkins job name '$JOB_NAME'," \
"will use the default."
return
fi
fi
adjust_compiler_type_on_mac
validate_compiler_type
readonly YB_COMPILER_TYPE
export YB_COMPILER_TYPE
}
validate_compiler_type() {
local compiler_type
if [[ $# -eq 0 ]]; then
if [[ -z ${YB_COMPILER_TYPE:-} ]]; then
fatal "${FUNCNAME[0]} is called with no arguments but YB_COMPILER_TYPE is not set or is empty"
fi
compiler_type=$YB_COMPILER_TYPE
elif [[ $# -eq 1 ]]; then
compiler_type=$1
else
fatal "${FUNCNAME[0]} can only be called with 0 or 1 argument, got $# arguments: $*"
fi
if [[ ! $compiler_type =~ $VALID_COMPILER_TYPES_RE ]]; then
fatal "Invalid compiler type: YB_COMPILER_TYPE='$compiler_type'" \
"(expected one of: ${VALID_COMPILER_TYPES[*]})."
fi
}
validate_cmake_build_type() {
expect_num_args 1 "$@"
local _cmake_build_type=$1
_cmake_build_type=$( echo "$_cmake_build_type" | to_lowercase )
if [[ ! "$_cmake_build_type" =~ $VALID_CMAKE_BUILD_TYPES_RE ]]; then
fatal "Invalid CMake build type (what we're about to pass to our CMake build as" \
"_cmake_build_type): '$_cmake_build_type'." \
"Valid CMake build types are: ${VALID_CMAKE_BUILD_TYPES[*]}."
fi
}
# This performs two configuration actions:
# - Sets cmake_build_type based on build_type. cmake_build_type is what's being passed to CMake
# using the CMAKE_BUILD_TYPE variable. CMAKE_BUILD_TYPE can't be "asan" or "tsan".
# - Ensure the YB_COMPILER_TYPE environment variable is set. It is used by our compiler-wrapper.sh
# script to invoke the appropriate C/C++ compiler.
set_cmake_build_type_and_compiler_type() {
if [[ -z "${cmake_opts:-}" ]]; then
cmake_opts=()
fi
if [[ -z ${build_type:-} ]]; then
if [[ ${YB_LINKING_TYPE:-} == *-lto ]]; then
log "Setting build type to 'release' by default (YB_LINKING_TYPE=${YB_LINKING_TYPE})"
build_type=release
else
log "Setting build type to 'debug' by default"
build_type=debug
fi
fi
normalize_build_type
# We're relying on build_type to set more variables, so make sure it does not change later.
readonly build_type
case "$build_type" in
asan)
cmake_build_type=fastdebug
;;
compilecmds)
cmake_build_type=debug
export YB_EXPORT_COMPILE_COMMANDS=1
;;
pvs)
cmake_build_type=debug
export YB_EXPORT_COMPILE_COMMANDS=1
export YB_DO_NOT_BUILD_TESTS=1
export YB_SKIP_INITIAL_SYS_CATALOG_SNAPSHOT=1
export YB_REMOTE_COMPILATION=0
;;
tsan)
cmake_build_type=fastdebug
;;
tsan_slow)
cmake_build_type=debug
;;
prof_gen|prof_use)
cmake_build_type=release
;;
*)
cmake_build_type=$build_type
esac
validate_cmake_build_type "$cmake_build_type"
readonly cmake_build_type
set_default_compiler_type
validate_compiler_type
readonly YB_COMPILER_TYPE
export YB_COMPILER_TYPE
if [[ $build_type =~ ^(asan|tsan|tsan_slow)$ && $YB_COMPILER_TYPE == gcc* ]]; then
fatal "Build type $build_type not supported with compiler type $YB_COMPILER_TYPE." \
"Sanitizers are only supported with Clang."
fi
if [[ $build_type =~ ^(prof_gen|prof_use)$ && $YB_COMPILER_TYPE == gcc* ]]; then
fatal "Build type $build_type not supported with compiler type $YB_COMPILER_TYPE." \
"PGO works only with Clang for now."
fi
# We need to set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER outside of CMake. We used to do that from
# CMakeLists.txt, and got into an infinite loop where CMake kept saying:
#
# You have changed variables that require your cache to be deleted.
# Configure will be re-run and you may have to reset some variables.
# The following variables have changed:
# CMAKE_CXX_COMPILER= /usr/bin/c++
#
# Not sure why it printed the old value there, since we tried to assign it the new value, the
# same as what's given below.
#
# So our new approach is to pass the correct command-line options to CMake, and still let CMake
# use the default compiler in CLion-triggered builds.
cmake_opts+=( "-DCMAKE_BUILD_TYPE=$cmake_build_type" )
cmake_opts+=( "${YB_DEFAULT_CMAKE_OPTS[@]}" )
}
find_make_or_ninja_and_update_cmake_opts() {
if using_ninja; then
cmake_opts+=( -G Ninja )
find_ninja_executable
make_program=$YB_NINJA_PATH
# make_file is used in yb_build.sh
# shellcheck disable=SC2034
make_file="build.ninja"
else
make_program="make"
# shellcheck disable=SC2034
make_file="Makefile"
fi
cmake_opts+=( "-DCMAKE_MAKE_PROGRAM=$make_program" )
}
create_mvn_repo_path_file() {
if [[ -n ${YB_MVN_LOCAL_REPO:-} ]]; then
local mvn_repo_file_path=$BUILD_ROOT/mvn_repo
log "Saving YB_MVN_LOCAL_REPO ($YB_MVN_LOCAL_REPO) to $mvn_repo_file_path"
echo "$YB_MVN_LOCAL_REPO" > "$mvn_repo_file_path"
fi
}
set_mvn_parameters() {
if [[ ${yb_mvn_parameters_already_set} == "true" ]]; then
return
fi
if is_jenkins; then
local m2_repository_in_build_root=$BUILD_ROOT/m2_repository
if "$is_run_test_script" && [[ -d $m2_repository_in_build_root ]]; then
YB_MVN_LOCAL_REPO=$m2_repository_in_build_root
# Do not use the "shared Maven settings" path even if it is available.
YB_MVN_SETTINGS_PATH=$YB_DEFAULT_MVN_SETTINGS_PATH
log "Will use Maven repository from build root ($YB_MVN_LOCAL_REPO) and the" \
"default Maven settings path ($YB_MVN_SETTINGS_PATH)"
elif [[ -z ${YB_MVN_SETTINGS_PATH:-} ]]; then
export YB_MVN_SETTINGS_PATH=$YB_SHARED_MVN_SETTINGS_PATH
log "Will use shared Maven settings file ($YB_MVN_SETTINGS_PATH)."
fi
fi
if [[ -z ${YB_MVN_LOCAL_REPO:-} ]]; then
YB_MVN_LOCAL_REPO=$YB_DEFAULT_MVN_LOCAL_REPO
fi
export YB_MVN_LOCAL_REPO
if [[ -z ${YB_MVN_SETTINGS_PATH:-} ]]; then
YB_MVN_SETTINGS_PATH=$YB_DEFAULT_MVN_SETTINGS_PATH
fi
export YB_MVN_SETTINGS_PATH
mvn_common_options=(
"--batch-mode"
"-DbinDir=$BUILD_ROOT/bin"
"-Dmaven.repo.local=$YB_MVN_LOCAL_REPO"
"-Dyb.thirdparty.dir=$YB_THIRDPARTY_DIR"
)
log "The result of set_mvn_parameters:" \
"YB_MVN_LOCAL_REPO=$YB_MVN_LOCAL_REPO," \
"YB_MVN_SETTINGS_PATH=$YB_MVN_SETTINGS_PATH"
create_mvn_repo_path_file
yb_mvn_parameters_already_set=true
}
# Put a retry loop here since it is possible that multiple concurrent builds will try to do this
# at the same time. However, this should converge quickly.
rsync_with_retries() {
declare -i attempt=1
declare -i -r max_attempts=5
while true; do
if ( set -x; rsync "$@" ); then
return
fi
if [[ $attempt -eq $max_attempts ]]; then
log "rsync failed after $max_attempts attempts, giving up"
return 1
fi
log "This was rsync attempt $attempt out of $max_attempts. Re-trying after a delay."
sleep 1
(( attempt+=1 ))
done
}
# Appends the settings path specified by $YB_MVN_SETTINGS_PATH (in case that path exists), as well
# as other common Maven options used across all invocations of Maven, to the mvn_opts array. The
# caller of this function usually declares mvn_opts as a local array.
append_common_mvn_opts() {
mvn_opts+=(
"${mvn_common_options[@]}"
)
if [[ -f $YB_MVN_SETTINGS_PATH ]]; then
mvn_opts+=(
--settings "$YB_MVN_SETTINGS_PATH"
)
elif [[ $YB_MVN_SETTINGS_PATH != $HOME/.m2/settings.xml ]]; then
log "Non-default maven user settings file specified by YB_MVN_SETTINGS_PATH does not exist:" \
"'$YB_MVN_SETTINGS_PATH'"
fi
}
# A utility function called by both 'build_yb_java_code' and 'build_yb_java_code_with_retries'.
build_yb_java_code_filter_save_output() {
set_mvn_parameters
log "Building Java code in $PWD"
# --batch-mode hides download progress.
# We are filtering out some patterns from Maven output, e.g.:
# [INFO] META-INF/NOTICE already added, skipping
# [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/2.4/plexus-classworlds-2.4.jar (46 KB at 148.2 KB/sec)
# [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/doxia/doxia-logging-api/1.1.2/doxia-logging-api-1.1.2.jar
local has_local_output=false # default is output path variable is set by calling function
if [[ -z ${java_build_output_path:-} ]]; then
local java_build_output_path
java_build_output_path=/tmp/yb-java-build-$( get_timestamp ).$$.tmp
has_local_output=true
fi
local -a mvn_opts=()
append_common_mvn_opts
if ! is_jenkins; then
mvn_opts+=( -Dmaven.javadoc.skip )
fi
set +e -x # +e: do not fail on grep failure, -x: print the command to stderr.
if mvn "${mvn_opts[@]}" "$@" 2>&1 | \
grep -Ev --line-buffered "$MVN_OUTPUT_FILTER_REGEX" | \
tee "$java_build_output_path"; then
set +x # stop printing commands
# We are testing for mvn build failure with grep, since we run mvn with '--fail-never' which
# always returns success. '--fail-at-end' could have been another possibility, but that mode
# skips dependent modules so most tests are often not run. Therefore, we resort to grep.
grep -Eq "BUILD SUCCESS" "$java_build_output_path"
local mvn_exit_code=$?
set -e
if [[ $has_local_output == "true" ]]; then
rm -f "$java_build_output_path" # cleaning up
fi
if [[ $mvn_exit_code -eq 0 ]]; then
log "Java build SUCCEEDED"
else
# Useful for searching in console output.
log "Java build FAILED: could not find 'BUILD SUCCESS' in Maven output"
fi
return $mvn_exit_code
fi
set -e +x
log "Java build or one of its output filters failed"
if [[ -f $java_build_output_path ]]; then
log "Java build output (from '$java_build_output_path'):"
cat "$java_build_output_path"
log "(End of Java build output)"
rm -f "$java_build_output_path"
else
log "Java build output path file not found at '$java_build_output_path'"
fi
return 1
}
build_yb_java_code() {
local java_build_output_path
java_build_output_path=/tmp/yb-java-build-$( get_timestamp ).$$.tmp
build_yb_java_code_filter_save_output "$@"
local mvn_exit_code=$?
rm -f "$java_build_output_path"
return $mvn_exit_code
}
build_yb_java_code_with_retries() {
local java_build_output_path
java_build_output_path=/tmp/yb-java-build-$( get_timestamp ).$$.tmp
declare -i attempt=1
while [[ $attempt -le $MAX_JAVA_BUILD_ATTEMPTS ]]; do
if build_yb_java_code_filter_save_output "$@"; then
rm -f "$java_build_output_path"
return 0
fi
if grep "Could not transfer artifact" "$java_build_output_path" >/dev/null; then
log "Java build attempt $attempt failed due to temporary connectivity issues, re-trying."
else
return 1
fi
rm -f "$java_build_output_path"
(( attempt+=1 ))
done
return 1
}
build_yb_java_code_in_all_dirs() {
local java_project_dir
for java_project_dir in "${yb_java_project_dirs[@]}"; do
pushd "$java_project_dir"
if ! time build_yb_java_code_with_retries "$@"; then
log "Failed to build Java code in directory '$java_project_dir'" \
"with these Maven arguments: $*"
return 1
fi
# shellcheck disable=SC2119
popd
done
}
# Skip the most part of the normal C++ build output. Still keep the "100%" lines so we can see
# if the build runs to completion. This only filters stdin, so it is expected that stderr is
# redirected to stdout when invoking the C++ build.
filter_boring_cpp_build_output() {
# For Ninja, keep every 10th successful C/C++ compilation message.
grep -Ev --line-buffered "\
^(\[ *[0-9]{1,2}%\] +)*(\
Building C(XX)? object |\
Running C[+][+] protocol buffer compiler (with YRPC plugin )?on |\
Linking CXX ((static|shared )?library|executable) |\
Built target \
)|\
Scanning dependencies of target |\
^ssh: connect to host .* port [0-9]+: Connection (timed out|refused)|\
Host .* seems to be down, retrying on a different host|\
Connection to .* closed by remote host.|\
ssh: Could not resolve hostname build-workers-.*: Name or service not known|\
^\[[0-9]+?[1-9]/[0-9]+\] (\
Building CXX object|\
Running C[+][+] protocol buffer compiler|\
Linking CXX shared library|\
Linking CXX executable)"
}
put_path_entry_first() {
expect_num_args 1 "$@"
local path_entry=$1
remove_path_entry "$path_entry"
export PATH=$path_entry:$PATH
}
add_path_entry_last() {
expect_num_args 1 "$@"
local path_entry=$1
if [[ $PATH != *:$path_entry && $PATH != $path_entry:* && $PATH != *:$path_entry:* ]]; then
export PATH+=:$path_entry
fi
}
# Removes the ccache wrapper directory from PATH so we can find the real path to a compiler, e.g.
# /usr/bin/gcc instead of /usr/lib64/ccache/gcc. This is expected to run in a subshell so that we
# don't make any unexpected changes to the script's PATH.
# TODO: how to do this properly on Mac OS X?
remove_ccache_dir_from_path() {
remove_path_entry /usr/lib64/ccache
}
log_diagnostics_about_local_thirdparty() {
if [[ $YB_THIRDPARTY_DIR == /opt/yb-build/* ]]; then
log "[Host $(hostname)]" \
"See diagnostic information below about subdirectories of /opt/yb-build:"
(
set -x +e
ls -l /opt/yb-build/brew >&2
ls -l /opt/yb-build/thirdparty >&2
)
fi
}
# Given a compiler type, e.g. gcc or clang, find the actual compiler executable (not a wrapper
# provided by ccache). Takes into account YB_GCC_PREFIX and YB_CLANG_PREFIX variables that allow to
# use custom gcc and clang installations. Sets cc_executable and cxx_executable variables. This is
# used in compiler-wrapper.sh.
find_compiler_by_type() {
expect_num_args 0 "$@"
expect_vars_to_be_set YB_COMPILER_TYPE
if [[ -n ${YB_RESOLVED_C_COMPILER:-} && -n ${YB_RESOLVED_CXX_COMPILER:-} ]]; then
cc_executable=$YB_RESOLVED_C_COMPILER
cxx_executable=$YB_RESOLVED_CXX_COMPILER
return
fi
validate_compiler_type "$YB_COMPILER_TYPE"
unset cc_executable
unset cxx_executable
case "$YB_COMPILER_TYPE" in
gcc|gcc5)
if [[ -n ${YB_GCC_PREFIX:-} ]]; then
if [[ ! -d $YB_GCC_PREFIX/bin ]]; then
fatal "Directory YB_GCC_PREFIX/bin ($YB_GCC_PREFIX/bin) does not exist"
fi
cc_executable=$YB_GCC_PREFIX/bin/gcc
cxx_executable=$YB_GCC_PREFIX/bin/g++
elif using_linuxbrew; then
cc_executable=$YB_LINUXBREW_DIR/bin/gcc
cxx_executable=$YB_LINUXBREW_DIR/bin/g++
else
cc_executable=gcc
cxx_executable=g++
fi
cc_executable+=${YB_GCC_SUFFIX:-}
cxx_executable+=${YB_GCC_SUFFIX:-}
;;
gcc*)
local gcc_major_version=${YB_COMPILER_TYPE#gcc}
if [[ ! $gcc_major_version =~ ^[0-9]+$ ]]; then
fatal "Invalid GCC major version: '$gcc_major_version'" \
"(from compiler type '$YB_COMPILER_TYPE')."
fi
if is_redhat_family; then
local gcc_bin_dir
if [[ -d /opt/rh/gcc-toolset-$gcc_major_version ]]; then
gcc_bin_dir=/opt/rh/gcc-toolset-$gcc_major_version/root/usr/bin
else
gcc_bin_dir=/opt/rh/devtoolset-$gcc_major_version/root/usr/bin
fi
cc_executable=$gcc_bin_dir/gcc
cxx_executable=$gcc_bin_dir/g++
# This is needed for other tools, such as "as" (the assembler).
put_path_entry_first "$gcc_bin_dir"
else
# shellcheck disable=SC2230
cc_executable=$(which "gcc-$gcc_major_version")
# shellcheck disable=SC2230
cxx_executable=$(which "g++-$gcc_major_version")
fi
;;
# This is the old Linuxbrew-based Clang 7 build type.
clang)
if [[ -n ${YB_CLANG_PREFIX:-} ]]; then
if [[ ! -d $YB_CLANG_PREFIX/bin ]]; then
fatal "Directory \$YB_CLANG_PREFIX/bin ($YB_CLANG_PREFIX/bin) does not exist"
fi