/
kerl
executable file
·2684 lines (2355 loc) · 80.5 KB
/
kerl
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
#!/bin/sh
# Copyright (c) 2016- Jade Allen
# Copyright (c) 2011, 2012 Spawngrid, Inc
# Copyright (c) 2011 Evax Software <contact(at)evax(dot)org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# shellcheck disable=SC2250 # Prefer putting braces around variable references even when not strictly required
# shellcheck disable=SC2292 # Prefer `[[ ]]` over `[ ]` for tests in Bash/Ksh
# shellcheck disable=SC2312 # Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore)
unset ERL_TOP
# Make sure CDPATH doesn't affect cd in case path is relative.
unset CDPATH
if [ -n "$KERL_DEBUG" ]; then
set -x
## Line numbers do not work in dash (aka Debian/Ubuntu sh)
## so you may want to change sh to bash to get better debug info
if [ -n "${LINENO}" ]; then
PS4='+ ${LINENO}: '
fi
fi
KERL_VERSION='4.1.0'
OLDEST_OTP_LISTED='17'
OLDEST_OTP_SUPPORTED='24'
ERLANG_DOWNLOAD_URL='https://erlang.org/download'
KERL_CONFIG_STORAGE_FILENAME='.kerl_config'
_KERL_SCRIPT=$0
# Redirect to stderr only if it is a terminal, otherwise mute
stderr() {
if [ -t 1 ]; then
if [ -z "$l" ] || [ "$KERL_COLORIZE" -eq 0 ]; then
echo "$*" 1>&2
else
left="$(colorize "$l")"
# shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting
right="$(tput $KERL_COLOR_RESET)"
echo "${left}$*${right}" 1>&2
unset -v l
fi
elif [ -n "$KERL_DEBUG" ] && [ -n "$CI" ]; then
echo "$*"
fi
}
nocolor() {
# $1: log entry
stderr "$1"
}
usage_exit() {
# $1: usage tip
l=error stderr "usage: ${_KERL_SCRIPT} $1"
exit 1
}
error() {
# $1: log entry
l=error stderr "ERROR: $1"
}
warn() {
# $1: log entry
# $2: use 'noprefix' if you want to omit WARNING:
if [ "$2" != "noprefix" ]; then
l=warn stderr "WARNING: $1"
else
l=warn stderr "$1"
fi
}
notice() {
# $1: log entry
l=notice stderr "$1"
}
success() {
# $1: log entry
l=success stderr "$1"
}
tip() {
# $1: log entry
l=tip stderr "$1"
}
init_build_logfile() {
# $1: release
# $2: build name
BUILD_LOGFILE="$KERL_BUILD_DIR/$2/otp_build_$1.log"
rm -f "$BUILD_LOGFILE"
touch "$BUILD_LOGFILE"
tip "Initializing (build) log file at $BUILD_LOGFILE".
}
log_build_entry() {
# $1: log entry
echo "$1" >>"$BUILD_LOGFILE" 2>&1
}
log_build_cmd() {
# $*: command
build_cmd="$*"
eval "$build_cmd" >>"$BUILD_LOGFILE" 2>&1
}
init_install_logfile() {
# $1: release
# $2: build name
INSTALL_LOGFILE="$KERL_BUILD_DIR/$2/otp_install_$1.log"
rm -f "$INSTALL_LOGFILE"
touch "$INSTALL_LOGFILE"
tip "Initializing (install) log file at $INSTALL_LOGFILE".
}
log_install_cmd() {
# $*: command
install_cmd="$*"
eval "$install_cmd" >>"$INSTALL_LOGFILE" 2>&1
}
# Check if tput is available for colorize
tp=$(tput sgr0 1>/dev/null 2>&1 && printf "OK" || printf "")
if [ -z "$tp" ]; then
if [ -z "$CI" ]; then
nocolor "Colorization disabled as 'tput' (via 'ncurses') seems to be unavailable."
fi
KERL_COLOR_AVAILABLE=0
KERL_COLORIZE=0 # Force disabling colorization
else
KERL_COLOR_AVAILABLE=1
if [ -n "$KERL_COLOR_D" ]; then
KERL_COLOR_RESET="setaf $KERL_COLOR_D"
else
KERL_COLOR_RESET="sgr0"
fi
fi
colorize() {
case "$1" in
"error")
ret="$(tput setaf "${KERL_COLOR_E:=1}")"
;;
"warn")
ret="$(tput setaf "${KERL_COLOR_W:=3}")"
;;
"notice")
ret="$(tput setaf "${KERL_COLOR_N:=4}")"
;;
"tip")
ret="$(tput setaf "${KERL_COLOR_T:=6}")"
;;
"success")
ret="$(tput setaf "${KERL_COLOR_S:=2}")"
;;
*)
ret="$(tput setaf "${KERL_COLOR_D:=9}")"
;;
esac
printf "%b" "$ret"
}
autoclean() {
if [ "${KERL_AUTOCLEAN:=1}" -eq 1 ]; then
notice "Auto cleaning all artifacts except the log file..."
tip "(use KERL_AUTOCLEAN=0 to keep build on failure, if desired)"
# Cleaning current build
cd - 1>/dev/null 2>&1 || return 0
test -n "$1" && ${_KERL_SCRIPT} cleanup "$1"
else
warn "auto cleaning (on failure) disabled!"
fi
}
TMP_DIR=${TMP_DIR:-'/tmp'}
if [ -z "$HOME" ]; then
error "\$HOME is empty or not set."
exit 1
fi
# Default values
KERL_BASE_DIR=${KERL_BASE_DIR:="$HOME"/.kerl}
KERL_CONFIG=${KERL_CONFIG:="$HOME"/.kerlrc}
KERL_DOWNLOAD_DIR=${KERL_DOWNLOAD_DIR:="${KERL_BASE_DIR:?}"/archives}
KERL_BUILD_DIR=${KERL_BUILD_DIR:="${KERL_BASE_DIR:?}"/builds}
KERL_GIT_DIR=${KERL_GIT_DIR:="${KERL_BASE_DIR:?}"/gits}
KERL_GIT_BASE=https://raw.githubusercontent.com/kerl/kerl/master
KERL_COLORIZE=${KERL_COLORIZE:=$KERL_COLOR_AVAILABLE}
KERL_INCLUDE_RELEASE_CANDIDATES=${KERL_INCLUDE_RELEASE_CANDIDATES:=no}
KERL_CHECK_BUILD_PACKAGES=${KERL_CHECK_BUILD_PACKAGES:="yes"}
act_on_kerl_cfgs() {
# $1: kerl config. flags (we're only interested in the keys)
# $2: action - cache | restore (default)
for _KERL_CFG in $1; do
k=$(echo "$_KERL_CFG" | \sed 's|\(.*\)=.*|\1|')
if [ "$2" = cache ]; then
eval "_KERL_CFG_VAR=\$${k}"
eval "_KERL_TARGET_VAR=__${k}"
elif [ "$2" = restore ]; then
eval "_KERL_CFG_VAR=\$__${k}"
eval "_KERL_TARGET_VAR=${k}"
fi
if [ -n "${_KERL_CFG_VAR}" ]; then
# shellcheck disable=SC2154 # _KERL_TARGET_VAR is referenced but not assigned
eval "$_KERL_TARGET_VAR=\"$_KERL_CFG_VAR\""
fi
done
}
# List is [<config_key>=<def_value>, ...]
_KERL_CFGS="
OTP_GITHUB_URL=https://github.com/erlang/otp
KERL_CONFIGURE_OPTIONS=
KERL_CONFIGURE_APPLICATIONS=
KERL_CONFIGURE_DISABLE_APPLICATIONS=
KERL_SASL_STARTUP=
KERL_DEPLOY_SSH_OPTIONS=
KERL_DEPLOY_RSYNC_OPTIONS=
KERL_INSTALL_MANPAGES=
KERL_INSTALL_HTMLDOCS=
KERL_BUILD_PLT=
KERL_BUILD_DOCS=
KERL_DOC_TARGETS=chunks
KERL_BUILD_BACKEND=
KERL_RELEASE_TARGET=
"
act_on_kerl_cfgs "$_KERL_CFGS" "cache"
eval "$_KERL_CFGS"
# ensure the base dir exists
mkdir -p "$KERL_BASE_DIR" || exit 1
# source the config file if available
if [ -f "$KERL_CONFIG" ]; then
# shellcheck source=/dev/null
. "$KERL_CONFIG"
fi
act_on_kerl_cfgs "$_KERL_CFGS" "restore"
if [ -z "$KERL_SASL_STARTUP" ]; then
INSTALL_OPT='-minimal'
else
INSTALL_OPT='-sasl'
fi
if [ -z "$KERL_BUILD_BACKEND" ]; then
KERL_BUILD_BACKEND='git'
fi
if [ "$KERL_BUILD_BACKEND" = 'git' ]; then
KERL_USE_AUTOCONF=1
elif [ "$KERL_BUILD_BACKEND" != 'tarball' ]; then
error "unhandled value KERL_BUILD_BACKEND=${KERL_BUILD_BACKEND}."
tip "KERL_BUILD_BACKEND must be either 'git' (default), or 'tarball'."
exit 1
fi
KERL_SYSTEM=$(uname -s)
case "$KERL_SYSTEM" in
Darwin | FreeBSD | OpenBSD)
MD5SUM='openssl md5'
MD5SUM_FIELD=2
SED_OPT=-E
;;
*)
MD5SUM=md5sum
MD5SUM_FIELD=1
SED_OPT=-r
;;
esac
k_usage() {
nocolor "kerl: build and install Erlang/OTP"
nocolor "usage: ${_KERL_SCRIPT} <command> [options ...]"
nocolor ""
nocolor " <command> Command to be executed"
nocolor ""
nocolor "Valid commands are:"
nocolor " build Build specified release or git repository"
nocolor " install Install the specified release at the given location"
nocolor " build-install Builds and installs the specified release or git repository at the given location"
nocolor " deploy Deploy the specified installation to the given host and location"
nocolor " update Update the list of available releases from your source provider"
nocolor " list List releases, builds and installations"
nocolor " delete Delete builds and installations"
nocolor " path Print the path of a given installation"
nocolor " active Print the path of the active installation"
nocolor " plt Print Dialyzer PLT path for the active installation"
nocolor " status Print available builds and installations"
nocolor " prompt Print a string suitable for insertion in prompt"
nocolor " cleanup Remove compilation artifacts (use after installation)"
nocolor " emit-activate Print the activate script"
nocolor " upgrade Fetch and install the most recent kerl release"
nocolor " version Print current version (current: $KERL_VERSION)"
exit 1
}
if [ $# -eq 0 ]; then k_usage; fi
get_releases() {
# $1: path to file otp_releases
if [ "$KERL_BUILD_BACKEND" = 'git' ]; then
if ! get_git_releases "$1"; then
return 1
fi
else
if ! get_tarball_releases "$1"; then
return 1
fi
fi
}
get_git_releases() {
tmp="$(mktemp "$TMP_DIR"/kerl.XXXXXX)"
# shellcheck disable=SC2154 # OTP_GITHUB_URL is referenced but not assigned
git ls-remote --tags --refs "$OTP_GITHUB_URL" >"$tmp"
ret=$?
notice "Getting releases from GitHub..."
if [ "$ret" -eq 0 ]; then
if ! cut <"$tmp" -f2 |
cut -d'/' -f3- |
sed "$SED_OPT" \
-e '# Delete all tags starting with ":" as to not mix' \
-e '# them with the prefixed lines we`re generating next.' \
-e '/^:/d' \
\
-e '# Prefix "OTP*" release lines with the crux of their versions.' \
-e '# - "OTP_R16B01_RC1" => ":16B01_RC1 OTP_R16B01_RC1"' \
-e '# - "OTP_R16B03" => ":16B03 OTP_R16B03"' \
-e '# - "OTP-17.0" => ":17.0 OTP-17.0"' \
-e '# - "OTP-17.3.1" => ":17.3.1 OTP-17.3.1"' \
-e '# - "OTP-19.0-rc1" => ":19.0-rc1 OTP-19.0-rc1"' \
-e '# - "OTP-19.0-rc2" => ":19.0-rc2 OTP-19.0-rc2"' \
-e 's/^(OTP[-_](R?([0-9][^ :]*).*))/:\3 \2/' \
\
-e '# Delete all lines that did not get prefixed above.' \
-e '/^[^:]/d' \
\
-e '# Move the colon markers preceding each version prefix' \
-e '# as to precede the tag suffix instead, which will make' \
-e '# throwing the version prefixes easier later on.' \
-e '# - ":16B01_RC1 OTP_R16B03" => "16B01_RC1 :OTP_R16B01_RC1"' \
-e '# - ":16B03 OTP_R16B03" => "16B03 :OTP_R16B03"' \
-e '# - ":17.0 OTP_R16B03" => "17.0 :OTP-17.0"' \
-e '# - ":17.3.1 OTP_R16B03" => "17.3.1 :OTP-17.3.1"' \
-e '# - ":19.0-rc1 OTP_R16B03" => "19.0-rc1 :OTP-19.0-rc1"' \
-e '# - ":19.0-rc2 OTP_R16B03" => "19.0-rc2 :OTP-19.0-rc2"' \
-e 's/^:([^ ]+) /\1 :/' \
\
-e '# Repeatedly replace sequences of one or more dots, dashes' \
-e '# or underscores, within each version prefix, with single' \
-e '# space characters.' \
-e '# - "16B01_RC1 :OTP_R16B01_RC1" => "16B01 RC1 :OTP_R16B01_RC1"' \
-e '# - "16B03 :OTP_R16B03" => "16B03 :OTP_R16B03"' \
-e '# - "17.0 :OTP-17.0" => "17 0 :OTP-17.0"' \
-e '# - "17.3.1 :OTP-17.3.1" => "17 3 1 :OTP-17.3.1"' \
-e '# - "19.0-rc1 :OTP-19.0-rc1" => "19 0 rc1 :OTP-19.0-rc1"' \
-e '# - "19.0-rc2 :OTP-19.0-rc2" => "19 0 rc2 :OTP-19.0-rc2"' \
-e ':loop' \
-e 's/^([^:]*)[.-]+([^:]*) :/\1 \2 :/' \
-e 't loop' \
\
-e '# Repeatedly replace "A", "B", or "C" separators, within each' \
-e '# version prefix, with " 0 ", " 1 " and " 2 ", respectively.' \
-e '# - "16B01 RC1 :OTP_R16B01_RC1" => "16 1 01 RC1 :OTP_R16B01_RC1"' \
-e '# - "16B03 :OTP_R16B03" => "16 1 03 :OTP_R16B03"' \
-e ':loop2' \
-e 's/^(.*[0-9]+)A([^:]*) :/\1 0 \2 :/' \
-e 's/^(.*[0-9]+)B([^:]*) :/\1 1 \2 :/' \
-e 's/^(.*[0-9]+)C([^:]*) :/\1 2 \2 :/' \
-e 't loop2' \
\
-e '# Repeatedly replace space-release candidate infixes, within' \
-e '# each version prefix, with a leading zero followed by' \
-e '# the candidate number.' \
-e '# - "16 1 01 RC1 :OTP_R16B01_RC1" => "16 1 01 0 1 :OTP_R16B01_RC1"' \
-e '# - "19 0 rc1 :OTP-19.0-rc1" => "19 0 0 1 :OTP-19.0-rc1"' \
-e '# - "19 0 rc2 :OTP-19.0-rc2" => "19 0 0 2 :OTP-19.0-rc2"' \
-e ':loop3' \
-e 's/^([^:]* )(rc|RC)([0-9]+)(( [^:]*)?) :/\10 \3\4 :/' \
-e 't loop3' \
\
-e '# Repeatedly prefix single digits, within each version prefix,' \
-e '# with leading zeroes.' \
-e '# - "16 1 01 0 1 :OTP_R16B01_RC1" => "16 01 01 00 01 :OTP_R16B01_RC1"' \
-e '# - "16 1 03 :OTP_R16B03" => "16 01 03 :OTP_R16B03"' \
-e '# - "17 0 :OTP-17.0" => "17 00 :OTP-17.0"' \
-e '# - "17 3 1 :OTP-17.3.1" => "17 03 01 :OTP-17.3.1"' \
-e '# - "19 0 0 1 :OTP-19.0-rc1" => "19 00 00 01 :OTP-19.0-rc.1"' \
-e '# - "19 0 0 2 :OTP-19.0-rc2" => "19 00 00 02 :OTP-19.0-rc.2"' \
-e ':loop4' \
-e 's/^([^:]*[^0-9:])([0-9])(([^0-9][^:]*)?) :/\10\2\3 :/' \
-e 't loop4' \
\
-e '# Suffix each version prefix with 00 as to not compare ':' with a number.' \
-e '# - "16 01 01 00 01 :OTP_R16B01_RC1" => "16 01 01 00 01 00 :OTP_R16B01_RC1"' \
-e '# - "16 01 03 :OTP_R16B03" => "16 01 03 00 :OTP_R16B03"' \
-e '# - "17 00 :OTP-17.0"" => "17 00 00 :OTP-17.0"' \
-e '# - "17 03 01 :OTP-17.3.1" => "17 03 01 00 :OTP-17.3.1"' \
-e '# - "19 00 00 01 :OTP-19.0-rc.1" => "19 00 00 01 00 :OTP-19.0-rc.1"' \
-e '# - "19 00 00 02 :OTP-19.0-rc.2" => "19 00 00 02 00 :OTP-19.0-rc.2"' \
-e 's/^([^:]+) :/\1 00 :/' |
LC_ALL=C sort -n |
cut -d':' -f2- |
tee "$1" >/dev/null 2>&1; then
error "file $1 does not appear to be writable."
return 1
fi
rm -f "$tmp"
return 0
else
rm -f "$tmp"
error "git ls-remote --tags --refs $OTP_GITHUB_URL returned $ret!"
return 1
fi
}
_curl() {
# $1: <file> to write output to, instead of stdout (if body_only function only returns body)
# $2: URL to consider
# $3: extra options
if [ "$1" != body_only ]; then
_curl_extra="--output $1 --write-out %{http_code}"
fi
# shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting
\curl -q --silent --location --fail $2 $3 $_curl_extra
}
unpack() {
# $1: <file> to extract to current directory
tar -x --no-same-owner --no-same-permissions -zf "$1"
}
get_tarball_releases() {
tmp="$(mktemp "$TMP_DIR"/kerl.XXXXXX)"
notice "Getting releases from erlang.org..."
http_code=$(_curl "$tmp" "$ERLANG_DOWNLOAD_URL")
if [ 200 = "$http_code" ]; then
if ! sed "$SED_OPT" \
-e 's/^.*<[aA] [hH][rR][eE][fF]=\"otp_src_([-0-9A-Za-z_.]+)\.tar\.gz\">.*$/\1/' \
-e '/^R1|^[0-9]/!d' "$tmp" |
sed -e 's/^R\(.*\)/\1:R\1/' |
sed -e 's/^\([^\:]*\)$/\1-z:\1/' |
sort |
cut -d: -f2 |
tee "$1" >/dev/null 2>&1; then
error "file $1 does not appear to be writable."
return 1
fi
rm -f "$tmp"
return 0
else
rm -f "$tmp"
error "$ERLANG_DOWNLOAD_URL returned $http_code!"
return 1
fi
}
update_checksum_file() {
if [ "$KERL_BUILD_BACKEND" != 'git' ]; then
notice "Getting checksum file from erlang.org..."
http_code=$(_curl "$KERL_DOWNLOAD_DIR"/MD5 "$ERLANG_DOWNLOAD_URL"/MD5)
if [ 200 != "$http_code" ]; then
error "$ERLANG_DOWNLOAD_URL/MD5 returned $http_code!"
return 1
fi
fi
}
ensure_checksum_file() {
if [ ! -s "$KERL_DOWNLOAD_DIR"/MD5 ] && ! update_checksum_file; then
return 1
fi
}
check_releases() {
# $1: force?: if force, the list is always recreated
if [ "$1" = force ]; then
if ! rm -f "${KERL_BASE_DIR:?}"/otp_releases 2>/dev/null; then
error "${KERL_BASE_DIR:?}/otp_releases cannot be removed."
return 1
fi
fi
if [ ! -f "$KERL_BASE_DIR"/otp_releases ] &&
! get_releases "$KERL_BASE_DIR"/otp_releases; then
return 1
fi
}
is_valid_release() {
if ! check_releases; then
return 1
fi
while read -r rel; do
if [ "$1" = "$rel" ]; then
return 0
fi
done <"$KERL_BASE_DIR"/otp_releases
error "'$1' is not a valid Erlang/OTP release."
return 1
}
get_release_from_name() {
# $1: build_name
if [ -f "$KERL_BASE_DIR"/otp_builds ]; then
while read -r l; do
rel=$(echo "$l" | cut -d, -f1)
name=$(echo "$l" | cut -d, -f2)
if [ "$name" = "$1" ]; then
echo "$rel"
return 0
fi
done <"$KERL_BASE_DIR"/otp_builds
fi
return 1
}
is_valid_installation() {
if [ -f "$KERL_BASE_DIR"/otp_installations ]; then
while read -r l; do
name=$(echo "$l" | cut -d' ' -f1)
path=$(echo "$l" | cut -d' ' -f2)
if [ "$name" = "$1" ] || [ "$path" = "$1" ]; then
if [ -f "$path"/activate ]; then
return 0
fi
fi
done <"$KERL_BASE_DIR"/otp_installations
fi
return 1
}
is_build_name_used() {
if [ -f "$KERL_BASE_DIR"/otp_builds ]; then
while read -r l; do
name=$(echo "$l" | cut -d, -f2)
if [ "$name" = "$1" ]; then
return 0
fi
done <"$KERL_BASE_DIR"/otp_builds
fi
return 1
}
is_older_than_x_days() {
# $1: file to check
# $2: age in days
old_file=$(find "$1" -type f -mtime "$2")
if [ -n "$old_file" ]; then
return 0
else
return 1
fi
}
lock() {
# $1: build | install
# $2: folder to act on
if [ -f "$2/$1.lock" ]; then
if is_older_than_x_days "$2/$1.lock" "+14"; then
unlock "$1" "$2"
else
error "trying to $1 in $2, but lock file ($2/$1.lock) exists!"
exit 1
fi
else
mkdir -p "$2"
touch "$2/$1.lock"
fi
}
unlock() {
# $1: build | install
# $2: folder to act on
rm -f "$2/$1.lock"
}
lock_build() {
# $_KERL_BUILD_DIR is global
lock "build" "$_KERL_BUILD_DIR"
}
unlock_build() {
# $_KERL_BUILD_DIR is global
unlock "build" "$_KERL_BUILD_DIR"
}
lock_install() {
# $_KERL_INSTALL_DIR is global
lock "install" "$_KERL_INSTALL_DIR"
}
unlock_install() {
# $_KERL_INSTALL_DIR is global
unlock "install" "$_KERL_INSTALL_DIR"
}
exit_build() {
# $1: error message
# $2: build name
if [ -n "$1" ]; then
error "$1"
fi
tmp=$(save_logfile)
if [ -n "$2" ]; then
if [ "${KERL_AUTOCLEAN:=1}" -eq 1 ]; then
rm -Rf "${KERL_BUILD_DIR:?}/$2"
fi
fi
unlock_build
restore_logfile "$tmp"
exit 1
}
exit_install() {
# $1: error message
if [ -n "$1" ]; then
error "$1"
fi
unlock_install
exit 1
}
do_git_build() {
git_url=$1
git_version=$2
build_name=$3
if is_build_name_used "$build_name"; then
error "there's already a build named '$build_name'."
exit_build
fi
GIT=$(printf '%s' "$git_url" | $MD5SUM | cut -d ' ' -f "$MD5SUM_FIELD")
_KERL_BUILD_DIR="$KERL_GIT_DIR/$GIT"
mkdir -p "$KERL_GIT_DIR" || exit_build
cd "$KERL_GIT_DIR" || exit_build
notice "Checking out Erlang/OTP git repository from $git_url..."
if [ ! -d "$GIT" ] &&
! git clone -q --mirror "$git_url" "$GIT" >/dev/null 2>&1; then
exit_build "mirroring remote git repository ($_KERL_BUILD_DIR exists?)."
fi
lock_build
cd "$_KERL_BUILD_DIR" || exit_build
if ! git remote update --prune >/dev/null 2>&1; then
exit_build "updating remote git repository."
fi
rm -Rf "${KERL_BUILD_DIR:?}/$build_name"
mkdir -p "$KERL_BUILD_DIR/$build_name" || exit_build
cd "$KERL_BUILD_DIR/$build_name" || exit_build "" "$build_name"
if ! git clone -l "$_KERL_BUILD_DIR" otp_src_git >/dev/null 2>&1; then
exit_build "cloning local git repository." "$build_name"
fi
cd "$KERL_BUILD_DIR/$build_name/otp_src_git" || exit_build "" "$build_name"
if ! git checkout "$git_version" >/dev/null 2>&1 &&
! git checkout -b "$git_version" "$git_version" >/dev/null 2>&1; then
exit_build "could not checkout specified version." "$build_name"
fi
if [ ! -x otp_build ]; then
exit_build "not a valid Erlang/OTP repository." "$build_name"
fi
notice "Building (git) Erlang/OTP $git_version; please wait..."
if [ -z "$KERL_BUILD_AUTOCONF" ]; then
KERL_USE_AUTOCONF=1
fi
if ! _do_build 'git' "$build_name"; then
exit_build "" "$build_name"
fi
success "Erlang/OTP '$build_name' (from git) has been successfully built."
if ! list_add builds git,"$build_name"; then
exit_build "" "$build_name"
fi
unlock_build
}
get_otp_version() {
echo "$1" | sed "$SED_OPT" -e 's/R?([0-9]{1,2}).+/\1/'
}
show_configuration_warnings() {
# $1 is log file
# $2 is section header (E.g. "APPLICATIONS DISABLED")
# Find the row number for the section we are looking for
INDEX=$(\grep -n -m1 "$2" "$1" | cut -d: -f1)
# If there are no warnings, the section won't appear in the log
if [ -n "$INDEX" ]; then
# Skip the section header, find the end line and skip it
# then print the results indented
tail -n +$((INDEX + 3)) "$1" |
sed -n '1,/\*/p' |
awk -F: -v logfile="$1" -v section="$2" \
'BEGIN { printf "%s (See: %s)\n", section, logfile }
/^[^\*]/ { print " *", $0 }
END { print "" } '
fi
}
show_logfile() {
# $1 reason
# $2 log file
error "$1"
tail "$2" 1>&2
nocolor ""
tip "Please see $2 for full details."
}
show_build_logfile() {
# $1 reason
show_logfile "$1" "$BUILD_LOGFILE"
}
show_install_logfile() {
# $1 reason
show_logfile "$1" "$INSTALL_LOGFILE"
}
maybe_patch() {
# $1 = OS platform e.g., Darwin, etc
# $2 = OTP release
release=$(get_otp_version "$2")
case "$1" in
Darwin) ;;
SunOS) ;;
*) ;;
esac
}
do_normal_build() {
# $1: release
# $2: build name
if ! is_valid_release "$1"; then
exit_build
fi
if is_build_name_used "$2"; then
error "there's already a build named '$2'."
exit_build
fi
_KERL_BUILD_DIR="$KERL_BUILD_DIR/$2"
lock_build
FILENAME=""
if ! download "$1"; then
exit_build
fi
if [ ! -d "$_KERL_BUILD_DIR/$FILENAME" ]; then
notice "Extracting source code for normal build..."
UNTARDIRNAME="$_KERL_BUILD_DIR/$FILENAME-kerluntar-$$"
rm -rf "$UNTARDIRNAME"
mkdir -p "$UNTARDIRNAME" || exit_build
# github tarballs have a directory in the form of "otp[_-]TAGNAME"
# Ericsson tarballs have the classic otp_src_RELEASE pattern
# Standardize on Ericsson format because that's what the rest of the script expects
(cd "$UNTARDIRNAME" && unpack "$KERL_DOWNLOAD_DIR/$FILENAME".tar.gz &&
cp -rfp ./* "$_KERL_BUILD_DIR/otp_src_$1")
rm -rf "$UNTARDIRNAME"
fi
notice "Building (normal) Erlang/OTP $1 ($2); please wait..."
if ! _do_build "$1" "$2"; then
exit_build
fi
success "Erlang/OTP $1 ($2) has been successfully built."
if ! list_add builds "$1,$2"; then
exit_build
fi
unlock_build
}
_flags() {
# We used to munge the LD and DED flags for clang 9/10 shipped with
# High Sierra (macOS 10.13), Mojave (macOS 10.14) and Catalina
# (macOS 10.15)
#
# As of OTP 20.1 that is (apparently) no longer necessary and
# in OTP 24 breaks stuff. See thread and comment here:
# https://github.com/erlang/otp/issues/4821#issuecomment-845914942
case "$KERL_SYSTEM" in
Darwin)
# Make sure we don't overwrite stuff that someone who
# knows better than us set.
if [ -z "$CC" ]; then
CC='clang'
fi
CFLAGS="${CFLAGS:-}" CC="$CC" "$@"
;;
*)
CFLAGS="${CFLAGS:-}" "$@"
;;
esac
}
_dpkg() {
# gratefully stolen from
# https://superuser.com/questions/427318/test-if-a-package-is-installed-in-apt
# returns 0 (true) if found, 1 otherwise
echo "dpkg-query -Wf'\${db:Status-abbrev}' \"$1\" 2>/dev/null | \grep -q '^i'"
}
_rpm() {
echo "rpm -q \"$1\""
}
_apk() {
echo "apk -e info \"$1\""
}
common_ALL_pkgs="gcc make"
common_ALL_BUILD_BACKEND_git_pkgs="automake autoconf"
common_debian_pkgs="${common_ALL_pkgs} libssl-dev libncurses5-dev g++"
# To add package guessing magic for your Linux distro/package,
# create a variable named _KPP_<distro>_pkgs where the content
# is an array with packages, and then create a unique probe
# command, in variable _KPP_<distro>_probe to check from the package manager.
# It should return 0 if package installation is Ok, non-0 otherwise.
_KPP_alpine_pkgs="${common_ALL_pkgs} openssl-dev ncurses-dev g++"
_KPP_alpine_probe="_apk"
_KPP_debian_pkgs=${common_debian_pkgs}
_KPP_debian_probe="_dpkg"
_KPP_fedora_pkgs="${common_ALL_pkgs} openssl-devel ncurses-devel g++"
_KPP_fedora_probe="_rpm"
_KPP_linuxmint_pkgs=${common_debian_pkgs}
_KPP_linuxmint_probe="_dpkg"
_KPP_pop_pkgs=${common_debian_pkgs}
_KPP_pop_probe="_dpkg"
_KPP_rhel_pkgs="${common_ALL_pkgs} openssl-devel ncurses-devel gcc-c++"
_KPP_rhel_probe="_rpm"
_KPP_ubuntu_pkgs=${common_debian_pkgs}
_KPP_ubuntu_probe="_dpkg"
parse_os_release() {
# $1: path to os-release
# returns:
# - 2 if release ID is not found in os-release file
os_release_id0=$(\grep "^ID=" "$1")
os_release_id=$(echo "${os_release_id0}" | \sed 's|.*=||' | \sed 's|"||g')
os_release_pretty_name0=$(\grep "^PRETTY_NAME=" "$1")
os_release_pretty_name=$(echo "${os_release_pretty_name0}" | \sed 's|.*=||' | \sed 's|"||g')
log_build_entry "[packages] Found ${os_release_pretty_name} with ID ${os_release_id}"
if [ -z "${os_release_id}" ]; then
log_build_entry "[packages] Release ID not found in $1"
echo "2"
else
log_build_entry "[packages] Release ID found in $1: ${os_release_id}"
echo "${os_release_id}"
fi
}
get_id_from_os_release_files() {
# returns:
# 1 - if no release files exist
files="/etc/os-release /usr/lib/os-release"
for file in ${files}; do
if [ -f "${file}" ]; then
parsed=$(parse_os_release "${file}")
if [ "${parsed}" != "2" ]; then
break
fi
fi
done
if [ -z "${parsed}" ]; then
log_build_entry "[packages] Found no file in: ${files}. Bailing out..."
echo "1"
fi
echo "${parsed}"
}
probe_pkgs() {
os_release_id=$(get_id_from_os_release_files)
if [ "${os_release_id}" = "1" ]; then
msg="[packages] Unable to determine Linux distro (no release files); not checking build packages."
log_build_entry "${msg}"
warn "${msg}"
return 0
elif [ "${os_release_id}" = "2" ]; then
msg="[packages] Unable to determine Linux distro (no ID); not checking build packages."
log_build_entry "${msg}"
warn "${msg}"
return 0
fi
kpp=$(eval echo \$_KPP_"${os_release_id}"_pkgs)
if [ "$KERL_BUILD_BACKEND" = 'git' ]; then
kpp="${common_ALL_BUILD_BACKEND_git_pkgs} ${kpp}"
fi
if [ -n "${kpp}" ]; then
log_build_entry "[packages] Found package declarations for your Linux distro: ${os_release_id}"
for pkg in ${kpp}; do
cmd=$(eval echo "\$_KPP_${os_release_id}_probe ${pkg}")
probe=$(${cmd})
eval "${probe}" >/dev/null 2>&1
probe_res=$?
if [ "${probe_res}" != 0 ]; then
msg="[packages] Probe failed for ${pkg} (distro: ${os_release_id}): probe \"${probe}\" returned ${probe_res}"
log_build_entry "${msg}"
warn "${msg}"
else
log_build_entry "[packages] Probe success for ${pkg} (distro: ${os_release_id})!"
fi
done
else
msg="[packages] Unknown Linux distro ${os_release_id}; not checking build packages."
log_build_entry "${msg}"
warn "${msg}"
fi
}
save_logfile() {
tmp="$(mktemp "$TMP_DIR"/kerl.XXXXXX)"
test -f "$BUILD_LOGFILE" && cp -f "$BUILD_LOGFILE" "$tmp"
echo "$tmp"
}