-
Notifications
You must be signed in to change notification settings - Fork 779
/
ruby-build
executable file
·1561 lines (1336 loc) · 42.7 KB
/
ruby-build
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
#
# Usage: ruby-build [-kpv] <definition> <prefix> [-- <configure-args...>]
# ruby-build {--list|--definitions}
# ruby-build --version
#
# -l, --list List latest stable releases for each Ruby
# --definitions List all local definitions, including outdated ones
# --version Show version of ruby-build
#
# -v, --verbose Verbose mode: forward all build output to stdout/stderr
# -p, --patch Apply a patch from stdin before building
# -k, --keep Do not remove source tree after installation
# -4, --ipv4 Resolve names to IPv4 addresses only
# -6, --ipv6 Resolve names to IPv6 addresses only
#
RUBY_BUILD_VERSION="20231107"
OLDIFS="$IFS"
# Have shell functions inherit the ERR trap.
set -E
# Some functions need to be able to write to the original process stderr
# stream, since fd 2 would often have been redirected elsewhere. To enable
# this, ruby-build initializes two additional file descriptors:
#
# 3: the original stderr
# 4: the log file
exec 3<&2
lib() {
parse_options() {
OPTIONS=()
ARGUMENTS=()
EXTRA_ARGUMENTS=()
local arg option index
while [ $# -gt 0 ]; do
arg="$1"
if [ "$arg" == "--" ]; then
shift 1
break
elif [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
OPTIONS[${#OPTIONS[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
OPTIONS[${#OPTIONS[*]}]="$option"
index=$((index+1))
done
fi
shift 1
else
ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
shift 1
fi
done
EXTRA_ARGUMENTS=("$@")
}
if [ "$1" == "--${FUNCNAME[0]}" ]; then
declare -f "${FUNCNAME[0]}"
echo "${FUNCNAME[0]} \"\$1\";"
exit
fi
}
lib "$1"
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
abs_dirname() {
local path="$1"
local cwd
cwd="$(pwd || true)"
while [ -n "$path" ]; do
cd "${path%/*}" || return 1
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd" || return 1
}
capitalize() {
# shellcheck disable=SC2018,SC2019
printf "%s" "$1" | tr 'a-z' 'A-Z'
}
sanitize() {
printf "%s" "$1" | sed "s/[^A-Za-z0-9.-]/_/g; s/__*/_/g"
}
colorize() {
if [[ -n $CLICOLOR_FORCE || ( -z $NO_COLOR && -t 1 ) ]]; then
printf "\e[%sm%s\e[m" "$1" "$2"
else
printf "%s" "$2"
fi
}
print_command() {
local arg
for arg; do
[ "${#TMPDIR}" -le 1 ] || arg="${arg//$TMP\//\$TMPDIR/}"
[ "${#HOME}" -le 1 ] || arg="${arg//$HOME\//\$HOME/}"
case "$arg" in
*\'* | *\$* )
printf ' "%s"' "$arg" ;;
*' '* )
printf " '%s'" "$arg" ;;
* )
printf ' %s' "$arg" ;;
esac
done
printf '\n'
}
# Log the full invocation of an external command.
log_command() {
local msg
msg="->$(print_command "$@")"
colorize 36 "$msg"
echo
[ -n "$VERBOSE" ] || printf "%s\n" "$msg" >&4
local status=0
"$@" || status="$?"
if [ "$status" -ne 0 ]; then
echo "external command failed with status $status" >&4
fi
return "$status"
}
# Log the full invocation of an external command and capture its output.
capture_command() {
local msg
msg="->$(print_command "$@")"
colorize 36 "$msg"
echo
# In verbose mode, connect the subcommand to original stdout & stderr.
local cmd_stdout=1
local cmd_stderr=3
if [ -z "$VERBOSE" ]; then
printf "%s\n" "$msg" >&4
# In normal mode, redirect all subcommand output to LOG_PATH.
cmd_stdout=4
cmd_stderr=4
fi
local status=0
# shellcheck disable=SC2261
"$@" 2>&$cmd_stderr >&$cmd_stdout || status="$?"
if [ "$status" -ne 0 ]; then
echo "external command failed with status $status" >&4
fi
return "$status"
}
log_info() {
colorize 1 "==> $*"
echo
[ -n "$VERBOSE" ] || echo "==> $*" >&4
}
log_notice() {
echo "ruby-build: $*"
}
os_information() {
if type -p lsb_release >/dev/null; then
lsb_release -sir | xargs echo
elif type -p sw_vers >/dev/null; then
echo "$(sw_vers -productName) $(sw_vers -productVersion)"
elif [ -r /etc/os-release ]; then
# shellcheck disable=SC1091
source /etc/os-release
# shellcheck disable=SC2153
echo "$NAME $VERSION_ID"
else
local os
os="$(cat /etc/{centos,redhat,fedora,system}-release /etc/debian_version 2>/dev/null | head -1)"
echo "${os:-$(uname -sr)}"
fi
}
is_mac() {
[ "$(uname -s)" = "Darwin" ] || return 1
[ $# -eq 0 ] || [ "$(osx_version)" -ge "$1" ]
}
is_freebsd() {
[ "$(uname -s)" = "FreeBSD" ]
}
freebsd_package_prefix() {
local package="$1"
pkg info --prefix "$package" 2>/dev/null | cut -wf2
}
# 9.1 -> 901
# 10.9 -> 1009
# 10.10 -> 1010
osx_version() {
local ver
IFS=. read -d "" -r -a ver <<<"$(sw_vers -productVersion)" || true
IFS="$OLDIFS"
echo $(( ver[0]*100 + ver[1] ))
}
build_failed() {
{ echo
colorize '31;1' "BUILD FAILED"
echo " ($(os_information) on $(uname -m) using $(version))"
echo
if ! rmdir "${BUILD_PATH}" 2>/dev/null; then
echo "You can inspect the build directory at ${BUILD_PATH}"
if [ -n "$(head -1 "$LOG_PATH" 2>/dev/null)" ]; then
colorize 33 "See the full build log at ${LOG_PATH}"
printf "\n"
fi
fi
} >&3
exit 1
}
num_cpu_cores() {
local num
case "$(uname -s)" in
Darwin | *BSD )
num="$(sysctl -n hw.ncpu 2>/dev/null || true)"
;;
SunOS )
num="$(getconf NPROCESSORS_ONLN 2>/dev/null || true)"
;;
* )
num="$({ getconf _NPROCESSORS_ONLN ||
grep -c ^processor /proc/cpuinfo; } 2>/dev/null)"
num="${num#0}"
;;
esac
echo "${num:-2}"
}
install_package() {
install_package_using "tarball" 1 "$@"
}
install_git() {
install_package_using "git" 2 "$@"
}
install_package_using() {
local package_type="$1"
local package_type_nargs="$2"
local package_name="$3"
shift 3
local fetch_args=( "$package_name" "${@:1:$package_type_nargs}" )
local -a build_steps
local arg last_arg
for arg in "${@:$(( package_type_nargs + 1 ))}"; do
if [ "$last_arg" = "--if" ]; then
if [[ $arg == *:* ]]; then
# Support colon-separated sub-argument, e.g. `needs_openssl:1.1`
"${arg%:*}" "$package_name" "${arg#*:}" || return 0
else
"$arg" "$package_name" || return 0
fi
elif [ "$arg" != "--if" ]; then
build_steps["${#build_steps[@]}"]="$arg"
fi
last_arg="$arg"
done
# shellcheck disable=SC2164
pushd "$BUILD_PATH" >/dev/null
echo "cd $PWD" >&4
# fetch_tarball, fetch_git
"fetch_${package_type}" "${fetch_args[@]}"
# shellcheck disable=SC2164
cd "$package_name"
echo "cd $PWD" >&4
before_install_package "$package_name"
build_package "$package_name" "${build_steps[@]}"
after_install_package "$package_name"
# shellcheck disable=SC2164
popd >/dev/null
log_info "Installed ${package_name} to ${PREFIX_PATH}"
}
compute_sha2() {
local output
if type shasum &>/dev/null; then
output="$(shasum -a 256 -b)" || return 1
echo "${output% *}"
elif type openssl &>/dev/null; then
local openssl
openssl="$(command -v "$(brew --prefix openssl 2>/dev/null || true)"/bin/openssl openssl | head -1)"
output="$("$openssl" dgst -sha256 2>/dev/null)" || return 1
echo "${output##* }"
elif type sha256sum &>/dev/null; then
output="$(sha256sum -b)" || return 1
echo "${output%% *}"
else
return 1
fi
}
compute_md5() {
local output
if type md5 &>/dev/null; then
md5 -q
elif type openssl &>/dev/null; then
output="$(openssl md5)" || return 1
echo "${output##* }"
elif type md5sum &>/dev/null; then
output="$(md5sum -b)" || return 1
echo "${output%% *}"
else
return 1
fi
}
has_checksum_support() {
local checksum_command="$1"
local has_checksum_var="HAS_CHECKSUM_SUPPORT_${checksum_command}"
if [ -z "${!has_checksum_var+defined}" ]; then
if "$checksum_command" <<<"test" >/dev/null; then
printf -v "$has_checksum_var" 0 # success
else
printf -v "$has_checksum_var" 1 # error
fi
fi
return "${!has_checksum_var}"
}
verify_checksum() {
local checksum_command
local filename="$1"
local expected_checksum
expected_checksum="$(tr 'A-F' 'a-f' <<<"$2")"
# If the specified filename doesn't exist, return success
[ -e "$filename" ] || return 0
case "${#expected_checksum}" in
0) return 0 ;; # empty checksum; return success
32) checksum_command="compute_md5" ;;
64) checksum_command="compute_sha2" ;;
*)
{ echo
echo "unexpected checksum length: ${#expected_checksum} (${expected_checksum})"
echo "expected 0 (no checksum), 32 (MD5), or 64 (SHA2-256)"
echo
} >&2
return 1 ;;
esac
# If chosen provided checksum algorithm isn't supported, return success
has_checksum_support "$checksum_command" || return 0
# If the computed checksum is empty, return failure
local computed_checksum
computed_checksum="$("$checksum_command" < "$filename" | tr 'A-F' 'a-f')"
[ -n "$computed_checksum" ] || return 1
if [ "$expected_checksum" != "$computed_checksum" ]; then
{ echo
echo "checksum mismatch: ${filename} (file is corrupt)"
echo "expected $expected_checksum, got $computed_checksum"
echo
} >&2
return 1
fi
}
http() {
local method="$1"
[ -n "$2" ] || return 1
shift 1
RUBY_BUILD_HTTP_CLIENT="${RUBY_BUILD_HTTP_CLIENT:-$(detect_http_client)}"
[ -n "$RUBY_BUILD_HTTP_CLIENT" ] || return 1
# http_get_curl, http_get_wget, etc.
"http_${method}_${RUBY_BUILD_HTTP_CLIENT}" "$@"
}
detect_http_client() {
local client
for client in aria2c curl wget; do
if type "$client" &>/dev/null; then
echo "$client"
return
fi
done
echo "error: install \`curl\`, \`wget\`, or \`aria2c\` to download packages" >&2
return 1
}
http_head_aria2c() {
# shellcheck disable=SC2086
aria2c --dry-run --no-conf=true $ARIA2_OPTS "$1" >/dev/null
}
http_get_aria2c() {
# shellcheck disable=SC2086
log_command aria2c --allow-overwrite=true --no-conf=true --console-log-level=warn --stderr $ARIA2_OPTS -o "$2" "$1" 2>&3
}
http_head_curl() {
# shellcheck disable=SC2086
curl -qsILf $CURL_OPTS "$1" >/dev/null
}
http_get_curl() {
# shellcheck disable=SC2086
log_command curl -q -fL $CURL_OPTS -o "$2" "$1" 2>&3
}
http_head_wget() {
# shellcheck disable=SC2086
wget -q --spider $WGET_OPTS "$1" >/dev/null
}
http_get_wget() {
# shellcheck disable=SC2086
log_command wget $WGET_OPTS -O "$2" "$1" 2>&3
}
fetch_tarball() {
local package_name="$1"
local package_url="$2"
local mirror_url
local checksum
local extracted_dir
if is_ruby_package "$1" && [ -n "$RUBY_BUILD_TARBALL_OVERRIDE" ]; then
package_url="$RUBY_BUILD_TARBALL_OVERRIDE"
fi
if [ "$package_url" != "${package_url/\#}" ]; then
checksum="${package_url#*#}"
package_url="${package_url%%#*}"
if [ -n "$RUBY_BUILD_MIRROR_URL" ]; then
if [[ -z "$RUBY_BUILD_DEFAULT_MIRROR" || $package_url != */cache.ruby-lang.org/* ]]; then
mirror_url="${RUBY_BUILD_MIRROR_URL}/$checksum"
fi
elif [ -n "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
mirror_url="$RUBY_BUILD_MIRROR_PACKAGE_URL"
fi
fi
local tar_args="xzf"
local package_filename="${package_name}.tar.gz"
if [ "$package_url" != "${package_url%bz2}" ]; then
if ! type -p bzip2 >/dev/null; then
echo "warning: bzip2 not found; consider installing the \`bzip2\` package" >&2
fi
package_filename="${package_filename%.gz}.bz2"
tar_args="${tar_args/z/j}"
fi
if ! reuse_existing_tarball "$package_filename" "$checksum"; then
local tarball_filename
tarball_filename="$(basename "$package_url")"
log_info "Downloading ${tarball_filename}..."
# shellcheck disable=SC2015
http head "$mirror_url" &&
download_tarball "$mirror_url" "$package_filename" "$checksum" ||
download_tarball "$package_url" "$package_filename" "$checksum"
fi
log_command tar "$tar_args" "$package_filename" >/dev/null
if [ ! -d "$package_name" ]; then
extracted_dir="$(find_extracted_directory)"
mv "$extracted_dir" "$package_name"
fi
[ -n "$KEEP_BUILD_PATH" ] || rm -f "$package_filename"
}
find_extracted_directory() {
for f in *; do
if [ -d "$f" ]; then
echo "$f"
return
fi
done
echo "Extracted directory not found" >&2
return 1
}
reuse_existing_tarball() {
local package_filename="$1"
local checksum="$2"
# Reuse existing file in build location
if [ -e "$package_filename" ] && verify_checksum "$package_filename" "$checksum"; then
return 0
fi
# Reuse previously downloaded file in cache location
[ -n "$RUBY_BUILD_CACHE_PATH" ] || return 1
local cached_package_filename="${RUBY_BUILD_CACHE_PATH}/$package_filename"
[ -e "$cached_package_filename" ] || return 1
verify_checksum "$cached_package_filename" "$checksum" || return 1
ln -s "$cached_package_filename" "$package_filename" || return 1
}
download_tarball() {
local package_url="$1"
[ -n "$package_url" ] || return 1
local package_filename="$2"
local checksum="$3"
if http get "$package_url" "$package_filename"; then
verify_checksum "$package_filename" "$checksum" || return 1
else
echo "error: failed to download $package_filename" >&2
return 1
fi
if [ -n "$RUBY_BUILD_CACHE_PATH" ]; then
local cached_package_filename="${RUBY_BUILD_CACHE_PATH}/$package_filename"
mv "$package_filename" "$cached_package_filename"
ln -s "$cached_package_filename" "$package_filename"
fi
}
fetch_git() {
local package_name="$1"
local git_url="$2"
local git_ref="$3"
log_info "Cloning ${git_url}..."
if ! type git &>/dev/null; then
echo "error: please install \`git\` and try again" >&2
exit 1
fi
if [ -n "$RUBY_BUILD_CACHE_PATH" ]; then
local cache_dir
cache_dir="$RUBY_BUILD_CACHE_PATH/$(sanitize "$git_url")"
if [ -e "$cache_dir" ]; then
log_command git -C "$cache_dir" fetch --force "$git_url" "+${git_ref}:${git_ref}" 2>&3
else
log_command git clone --bare --branch "$git_ref" "$git_url" "$cache_dir" 2>&3
fi
git_url="$cache_dir"
fi
if [ -e "$package_name" ]; then
log_command git -C "$package_name" fetch --depth 1 origin "+${git_ref}" 2>&3
log_command git -C "$package_name" checkout -q -B "$git_ref" "origin/${git_ref}" 2>&3
else
log_command git clone --depth 1 --branch "$git_ref" "$git_url" "$package_name" 2>&3
fi
}
build_package() {
local package_name="$1"
shift
# Use "build_package_standard" as the default build step.
[ $# -gt 0 ] || set -- standard
log_info "Installing ${package_name}..."
[ -n "$HAS_PATCH" ] && apply_ruby_patch "$package_name"
local step
for step; do
# e.g. build_package_standard, build_package_truffleruby, etc.
"build_package_${step}" "$package_name"
done
}
package_option() {
local package_name="$1"
local command_name="$2"
local variable
# e.g. RUBY_CONFIGURE_OPTS_ARRAY, OPENSSL_MAKE_OPTS_ARRAY
variable="$(capitalize "${package_name}_${command_name}")_OPTS_ARRAY"
local array="${variable}[@]"
shift 2
# shellcheck disable=SC2034
local value=( "${!array}" "$@" )
eval "$variable=( \"\${value[@]}\" )"
}
build_package_warn_eol() {
local package_name="$1"
{ echo
echo "WARNING: $package_name is past its end of life and is now unsupported."
echo "It no longer receives bug fixes or critical security updates."
echo
} >&2
}
build_package_warn_unsupported() {
local package_name="$1"
{ echo
echo "WARNING: $package_name is nearing its end of life."
echo "It only receives critical security updates, no bug fixes."
echo
} >&2
}
build_package_standard_build() {
local package_name="$1"
if [ "${MAKEOPTS+defined}" ]; then
MAKE_OPTS="$MAKEOPTS"
elif [ -z "${MAKE_OPTS+defined}" ]; then
MAKE_OPTS="-j $(num_cpu_cores)"
fi
# Support YAML_CONFIGURE_OPTS, RUBY_CONFIGURE_OPTS, etc.
local package_var_name
package_var_name="$(capitalize "${package_name%%-*}")"
local PACKAGE_CONFIGURE="${package_var_name}_CONFIGURE"
local PACKAGE_PREFIX_PATH="${package_var_name}_PREFIX_PATH"
local PACKAGE_CONFIGURE_OPTS="${package_var_name}_CONFIGURE_OPTS"
local PACKAGE_CONFIGURE_OPTS_ARRAY="${package_var_name}_CONFIGURE_OPTS_ARRAY[@]"
local PACKAGE_MAKE_OPTS="${package_var_name}_MAKE_OPTS"
local PACKAGE_MAKE_OPTS_ARRAY="${package_var_name}_MAKE_OPTS_ARRAY[@]"
local PACKAGE_CFLAGS="${package_var_name}_CFLAGS"
if [ "$package_var_name" = "RUBY" ]; then
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-readline-dir=* ]]; then
use_homebrew_readline || use_freebsd_readline || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libffi-dir=* ]]; then
use_freebsd_libffi || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libyaml-dir=* ]]; then
use_homebrew_yaml || use_freebsd_yaml || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-gmp-dir=* ]]; then
use_homebrew_gmp || true
fi
if [[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-openssl-dir=* ]]; then
if is_freebsd && [ -f /usr/local/include/openssl/ssl.h ]; then
# use openssl installed from Ports Collection
package_option ruby configure --with-openssl-dir="/usr/local"
fi
fi
fi
( if [ "${CFLAGS+defined}" ] || [ "${!PACKAGE_CFLAGS+defined}" ]; then
export CFLAGS="$CFLAGS ${!PACKAGE_CFLAGS}"
fi
if [ -z "$CC" ] && is_mac 1010; then
export CC=clang
fi
# ./configure --prefix=/path/to/ruby
# shellcheck disable=SC2086,SC2153
capture_command ${!PACKAGE_CONFIGURE:-./configure} --prefix="${!PACKAGE_PREFIX_PATH:-$PREFIX_PATH}" \
"${!PACKAGE_CONFIGURE_OPTS_ARRAY}" $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS}
) || return $?
# make -j <num_cpu_cores>
# shellcheck disable=SC2086
capture_command "$MAKE" "${!PACKAGE_MAKE_OPTS_ARRAY}" $MAKE_OPTS ${!PACKAGE_MAKE_OPTS}
}
build_package_standard_install() {
local package_name="$1"
local package_var_name
package_var_name="$(capitalize "${package_name%%-*}")"
local PACKAGE_MAKE_INSTALL_OPTS="${package_var_name}_MAKE_INSTALL_OPTS"
local PACKAGE_MAKE_INSTALL_OPTS_ARRAY="${package_var_name}_MAKE_INSTALL_OPTS_ARRAY[@]"
# make install
# shellcheck disable=SC2086
capture_command "$MAKE" ${MAKE_INSTALL_TARGET:-install} "${!PACKAGE_MAKE_INSTALL_OPTS_ARRAY}" $MAKE_INSTALL_OPTS ${!PACKAGE_MAKE_INSTALL_OPTS}
}
build_package_standard_install_with_bundled_gems() {
capture_command "$MAKE" update-gems
capture_command "$MAKE" extract-gems
build_package_standard_install "$@"
}
# Backward Compatibility for standard function
build_package_standard() {
build_package_standard_build "$@"
build_package_standard_install "$@"
}
build_package_autoconf() {
capture_command autoreconf -i
}
build_package_ruby() {
capture_command "$RUBY_BIN" setup.rb
}
build_package_ree_installer() {
build_package_auto_tcltk
local options=()
is_mac && options+=(--no-tcmalloc)
local option
for option in "${RUBY_CONFIGURE_OPTS_ARRAY[@]}" $RUBY_CONFIGURE_OPTS; do
options+=(-c "$option")
done
# Work around install_useful_libraries crash with --dont-install-useful-gems
mkdir -p "$PREFIX_PATH/lib/ruby/gems/1.8/gems"
# shellcheck disable=SC2086
capture_command ./installer --auto "$PREFIX_PATH" --dont-install-useful-gems "${options[@]}" $CONFIGURE_OPTS
}
build_package_rbx() {
export PATH="${PWD}/.gem/bin:${PATH}"
if [ -e "Gemfile" ]; then
bundle --version &>/dev/null || GEM_HOME="${PWD}/.gem" capture_command gem install bundler -v '~> 1.3.5'
capture_command bundle --path=vendor/bundle
fi
if [ -n "$RUBY_BUILD_CACHE_PATH" ]; then
mkdir -p vendor
ln -s "$RUBY_BUILD_CACHE_PATH" vendor/prebuilt
fi
local opt
local -a configure_opts
for opt in "${RUBY_CONFIGURE_OPTS_ARRAY[@]}"; do
if [[ $opt == --with-openssl-dir=* ]]; then
local openssl_dir="${opt#*=}"
configure_opts[${#configure_opts[@]}]="--with-lib-dir=${openssl_dir}/lib"
configure_opts[${#configure_opts[@]}]="--with-include-dir=${openssl_dir}/include"
else
configure_opts[${#configure_opts[@]}]="$opt"
fi
done
# shellcheck disable=SC2086
RUBYOPT="-rrubygems $RUBYOPT" capture_command ./configure --prefix="$PREFIX_PATH" "${configure_opts[@]}" $RUBY_CONFIGURE_OPTS
if [ -e "Gemfile" ]; then
capture_command bundle exec rake install
else
rake --version &>/dev/null || GEM_HOME="${PWD}/.gem" capture_command gem install rake -v '~> 10.1.0'
capture_command rake install
fi
local gemdir="${PREFIX_PATH}/gems/bin"
local file binstub
# Symlink Rubinius' `gems/bin/` into `bin/`
if [ -d "$gemdir" ] && [ ! -L "$gemdir" ]; then
for file in "$gemdir"/*; do
binstub="${PREFIX_PATH}/bin/${file##*/}"
rm -f "$binstub"
{ echo "#!${PREFIX_PATH}/bin/ruby"
grep -v '^#!' "$file"
} > "$binstub"
chmod +x "$binstub"
done
rm -rf "$gemdir"
ln -s ../bin "$gemdir"
fi
"${PREFIX_PATH}/bin/irb" --version &>/dev/null ||
capture_command "${PREFIX_PATH}/bin/gem" install rubysl-tracer -v '~> 2.0' --no-rdoc --no-ri ||
true
}
build_package_mruby() {
capture_command ./minirake
mkdir -p "$PREFIX_PATH"
cp -fR build/host/* include "$PREFIX_PATH"
ln -fs mruby "$PREFIX_PATH/bin/ruby"
ln -fs mirb "$PREFIX_PATH/bin/irb"
}
build_package_picoruby() {
capture_command ./minirake
mkdir -p "$PREFIX_PATH"
cp -fR build/host/* include "$PREFIX_PATH"
ln -fs picoruby "$PREFIX_PATH/bin/ruby"
ln -fs picoirb "$PREFIX_PATH/bin/irb"
}
build_package_jruby() {
build_package_copy
# shellcheck disable=SC2164
cd "${PREFIX_PATH}/bin"
ln -fs jruby ruby
chmod +x ruby
install_jruby_launcher
remove_windows_files
fix_jruby_shebangs
}
install_jruby_launcher() {
# shellcheck disable=SC2164
cd "${PREFIX_PATH}/bin"
# workaround for https://github.com/jruby/jruby/issues/7799
local jruby_version
jruby_version="$(./ruby -e 'puts JRUBY_VERSION' 2>/dev/null)"
[[ $jruby_version != "9.2."* ]] ||
capture_command ./ruby gem update -q --silent --system 3.3.26 --no-document --no-post-install-message
capture_command ./ruby gem install jruby-launcher --no-document
}
fix_jruby_shebangs() {
for file in "${PREFIX_PATH}/bin"/*; do
if [ "$(head -c 20 "$file" | LC_CTYPE=C tr -d '\0')" = "#!/usr/bin/env jruby" ]; then
sed -i.bak "1 s:.*:#\!${PREFIX_PATH}\/bin\/jruby:" "$file"
rm "$file".bak
fi
done
}
build_package_truffleruby() {
clean_prefix_path_truffleruby || return $?
build_package_copy
# shellcheck disable=SC2164
cd "${PREFIX_PATH}"
capture_command ./lib/truffle/post_install_hook.sh
}
build_package_truffleruby_graalvm() {
clean_prefix_path_truffleruby || return $?
PREFIX_PATH="${PREFIX_PATH}/graalvm" build_package_copy
if is_mac; then
# shellcheck disable=SC2164
cd "${PREFIX_PATH}/graalvm/Contents/Home"
else
# shellcheck disable=SC2164
cd "${PREFIX_PATH}/graalvm"
fi
if [ -e bin/gu ]; then
capture_command bin/gu install ruby
fi
local ruby_home
ruby_home=$(bin/ruby -e 'print RbConfig::CONFIG["prefix"]')
# Make gu available in PATH (useful to install other languages)
ln -s "$PWD/bin/gu" "$ruby_home/bin/gu"
# shellcheck disable=SC2164
cd "${PREFIX_PATH}"
ln -s "${ruby_home#"$PREFIX_PATH/"}/bin" .
capture_command "$ruby_home/lib/truffle/post_install_hook.sh"
}
build_package_artichoke() {
build_package_copy
mkdir -p "$PREFIX_PATH/bin"
# shellcheck disable=SC2164
cd "${PREFIX_PATH}/bin"
ln -fs ../artichoke ruby
ln -fs ../airb irb
ln -fs ../artichoke artichoke
ln -fs ../airb airb
}
remove_windows_files() {
# shellcheck disable=SC2164
cd "$PREFIX_PATH"
rm -f bin/*.exe bin/*.dll bin/*.bat bin/jruby.sh
}
clean_prefix_path_truffleruby() {
if [ -d "$PREFIX_PATH" ] &&
[ ! -e "$PREFIX_PATH/bin/truffleruby" ] &&
[ -n "$(ls -A "$PREFIX_PATH")" ]; then
{ echo
echo "ERROR: cannot install TruffleRuby to $PREFIX_PATH, which does not look like a valid TruffleRuby prefix."
echo "TruffleRuby only supports being installed to a not existing directory, an empty directory, or replacing an existing TruffleRuby installation."
echo "See https://github.com/oracle/truffleruby/issues/1389 for details"
} >&2
return 1
fi
# Make sure there are no leftover files in $PREFIX_PATH
rm -rf "$PREFIX_PATH"
}
build_package_copy() {
mkdir -p "$PREFIX_PATH"
cp -fR . "$PREFIX_PATH"
}
before_install_package() {
local stub=1
}
after_install_package() {
local stub=1
}
require_java() {
local required="$1"
local java_version version_string
java_version="$(java -version 2>&1 || true)"
version_string="$(grep 'java version' <<<"$java_version" | head -1 | grep -o '[0-9.]\+' | head -1 || true)"
[ -n "$version_string" ] || version_string="$(grep 'openjdk version' <<<"$java_version" | head -1 | grep -o '[0-9.]\+' | head -1 || true)"
IFS="."
# shellcheck disable=SC2206
local nums=($version_string)
IFS="$OLDIFS"
local found_version="${nums[0]}"
[ "$found_version" -gt 1 ] 2>/dev/null || found_version="${nums[1]}"
[ "$found_version" -ge "$required" ] 2>/dev/null && return 0
{ colorize 1 "ERROR"
printf ": Java >= %s required, but your Java version was:\n%s\n" "$required" "$java_version"
} >&2
return 1
}
# Kept for backward compatibility with JRuby <= 9.1.17 definitions.
require_java7() {
require_java 7
}
# Kept for backward compatibility with 3rd-party Ruby definitions.
require_gcc() {
local stub=1
}
# Kept for backward compatibility with 3rd-party Rubinius definitions.
# shellcheck disable=SC2034
require_llvm() {
local stub=1
}
needs_yaml() {
[[ "$RUBY_CONFIGURE_OPTS ${RUBY_CONFIGURE_OPTS_ARRAY[*]}" != *--with-libyaml-dir=* ]] &&
! use_homebrew_yaml
}
use_homebrew_yaml() {
local libdir
libdir="$(brew --prefix libyaml 2>/dev/null || true)"
if [ -d "$libdir" ]; then
log_notice "using libyaml from homebrew"
package_option ruby configure --with-libyaml-dir="$libdir"
else
return 1
fi
}
use_freebsd_yaml() {
if is_freebsd; then
local libyaml_prefix
libyaml_prefix="$(freebsd_package_prefix libyaml)"
if [ -n "$libyaml_prefix" ]; then
package_option ruby configure --with-libyaml-dir="$libyaml_prefix"
fi
fi
}
use_homebrew_gmp() {
local libdir
libdir="$(brew --prefix gmp 2>/dev/null || true)"
if [ -d "$libdir" ]; then
log_notice "using gmp from homebrew"
package_option ruby configure --with-gmp-dir="$libdir"
else