-
Notifications
You must be signed in to change notification settings - Fork 234
/
kerl
executable file
·2493 lines (2289 loc) · 78.7 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-2018 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.
unset ERL_TOP
# Make sure CDPATH doesn't affect cd in case path is relative.
unset CDPATH
KERL_VERSION='2.4.0'
DOCSH_GITHUB_URL='https://github.com/erszcz/docsh.git'
ERLANG_DOWNLOAD_URL='https://erlang.org/download'
KERL_CONFIG_STORAGE_FILENAME='.kerl_config'
# Check if tput is available for colorize
tp=$(tput -V 1>/dev/null 2>&1 && echo -n "OK" || echo -n "")
if [ -z "$tp" ]; then
stderr "Colorization disabled as 'tput' (via 'ncurses') seems to be unavailable"
KERL_COLOR_AVAILABLE=0
KERL_COLORIZE=0 # Force disabling colorization
else
KERL_COLOR_AVAILABLE=1
fi
# 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)"
right="$(tput setaf ${KERL_COLOR_D:=9})"
echo "${left}$*${right}" 1>&2
unset -v l
fi
fi
}
colorize()
{
case "$1" in
"error"|"err"|"e")
ret="$(tput setaf ${KERL_COLOR_E:=1})"
;;
"warning"|"warn"|"w")
ret="$(tput setaf ${KERL_COLOR_W:=3})"
;;
"notice"|"note"|"n")
ret="$(tput setaf ${KERL_COLOR_N:=4})"
;;
"tip"|"t")
ret="$(tput setaf ${KERL_COLOR_T:=6})"
;;
"success"|"s")
ret="$(tput setaf ${KERL_COLOR_S:=2})"
;;
*)
ret="$(tput setaf ${KERL_COLOR_D:=9})"
esac
echo -n "$ret"
}
TMP_DIR=${TMP_DIR:-'/tmp'}
if [ -z "$HOME" ]; then
# shellcheck disable=SC2016
l=e stderr '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}
if [ -n "$OTP_GITHUB_URL" ]; then
_OGU="$OTP_GITHUB_URL"
fi
if [ -n "$KERL_CONFIGURE_OPTIONS" ]; then
_KCO="$KERL_CONFIGURE_OPTIONS"
fi
if [ -n "$KERL_CONFIGURE_APPLICATIONS" ]; then
_KCA="$KERL_CONFIGURE_APPLICATIONS"
fi
if [ -n "$KERL_CONFIGURE_DISABLE_APPLICATIONS" ]; then
_KCDA="$KERL_CONFIGURE_DISABLE_APPLICATIONS"
fi
if [ -n "$KERL_SASL_STARTUP" ]; then
_KSS="$KERL_SASL_STARTUP"
fi
if [ -n "$KERL_DEPLOY_SSH_OPTIONS" ]; then
_KDSSH="$KERL_DEPLOY_SSH_OPTIONS"
fi
if [ -n "$KERL_DEPLOY_RSYNC_OPTIONS" ]; then
_KDRSYNC="$KERL_DEPLOY_RSYNC_OPTIONS"
fi
if [ -n "$KERL_INSTALL_MANPAGES" ]; then
_KIM="$KERL_INSTALL_MANPAGES"
fi
if [ -n "$KERL_INSTALL_HTMLDOCS" ]; then
_KIHD="$KERL_INSTALL_HTMLDOCS"
fi
if [ -n "$KERL_BUILD_PLT" ]; then
_KBPLT="$KERL_BUILD_PLT"
fi
if [ -n "$KERL_BUILD_DOCS" ]; then
_KBD="$KERL_BUILD_DOCS"
fi
if [ -n "$KERL_DOC_TARGETS" ]; then
_KDT="$KERL_DOC_TARGETS"
fi
if [ -n "$KERL_BUILD_BACKEND" ]; then
_KBB="$KERL_BUILD_BACKEND"
fi
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=
# 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
if [ -n "$_OGU" ]; then
OTP_GITHUB_URL="$_OGU"
fi
if [ -n "$_KCO" ]; then
KERL_CONFIGURE_OPTIONS="$_KCO"
fi
if [ -n "$_KCA" ]; then
KERL_CONFIGURE_APPLICATIONS="$_KCA"
fi
if [ -n "$_KCDA" ]; then
KERL_CONFIGURE_DISABLE_APPLICATIONS="$_KCDA"
fi
if [ -n "$_KSS" ]; then
KERL_SASL_STARTUP="$_KSS"
fi
if [ -n "$_KDSSH" ]; then
KERL_DEPLOY_SSH_OPTIONS="$_KDSSH"
fi
if [ -n "$_KDRSYNC" ]; then
KERL_DEPLOY_RSYNC_OPTIONS="$_KDRSYNC"
fi
if [ -n "$_KIM" ]; then
KERL_INSTALL_MANPAGES="$_KIM"
fi
if [ -n "$_KIHD" ]; then
KERL_INSTALL_HTMLDOCS="$_KIHD"
fi
if [ -n "$_KBPLT" ]; then
KERL_BUILD_PLT="$_KBPLT"
fi
if [ -n "$_KBD" ]; then
KERL_BUILD_DOCS="$_KBD"
fi
if [ -n "$_KDT" ]; then
KERL_DOC_TARGETS="$_KDT"
fi
if [ -n "$_KBB" ]; then
KERL_BUILD_BACKEND="$_KBB"
fi
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
l=e stderr "Unhandled value KERL_BUILD_BACKEND='${KERL_BUILD_BACKEND}'"
l=t stderr "KERL_BUILD_BACKEND must be one of '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
CP_OPT=-a
;;
*)
MD5SUM=md5sum
MD5SUM_FIELD=1
SED_OPT=-r
CP_OPT=-pr
;;
esac
usage() {
stderr 'kerl: build and install Erlang/OTP'
stderr "usage: $0 <command> [options ...]"
stderr ''
stderr ' <command> Command to be executed'
stderr ''
stderr 'Valid commands are:'
stderr ' build Build specified release or git repository'
stderr ' install Install the specified release at the given location'
stderr ' deploy Deploy the specified installation to the given host and location'
stderr ' update Update the list of available releases from your source provider'
stderr ' list List releases, builds and installations'
stderr ' delete Delete builds and installations'
stderr ' install-docsh Install erl shell documentation access extension - docsh'
stderr ' path Print the path of a given installation'
stderr ' active Print the path of the active installation'
stderr ' plt Print Dialyzer PLT path for the active installation'
stderr ' status Print available builds and installations'
stderr ' prompt Print a string suitable for insertion in prompt'
stderr ' cleanup Remove compilation artifacts (use after installation)'
stderr " version Print current version (current: $KERL_VERSION)"
exit 1
}
if [ $# -eq 0 ]; then usage; fi
get_releases() {
if [ "$KERL_BUILD_BACKEND" = 'git' ]; then
get_git_releases
else
get_tarball_releases
fi
}
get_git_releases() {
tmp="$(mktemp "$TMP_DIR"/kerl.XXXXXX)"
git ls-remote --tags --refs "$OTP_GITHUB_URL" > "$tmp"
ret=$?
if [ $ret -eq 0 ]; then
cat "$tmp" \
| cut -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 didn`t get prefixed above.' \
-e '/^[^:]/d' \
\
-e '# Move the colon markers preceding each version prefix' \
-e '# as to preceed 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-
fi
rm -f "$tmp"
return $ret
}
get_tarball_releases() {
tmp="$(mktemp "$TMP_DIR"/kerl.XXXXXX)"
if [ 200 = "$(\curl -qsL --output "$tmp" --write-out '%{http_code}' $ERLANG_DOWNLOAD_URL/)" ]; then
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
rm "$tmp"
return 0
fi
rm "$tmp"
exit 1
}
update_checksum_file() {
if [ "$KERL_BUILD_BACKEND" = 'git' ]; then
return 0
else
l=n stderr 'Getting checksum file from erlang.org...'
curl -f -L -o "$KERL_DOWNLOAD_DIR"/MD5 "$ERLANG_DOWNLOAD_URL"/MD5 || exit 1
fi
}
ensure_checksum_file() {
if [ ! -s "$KERL_DOWNLOAD_DIR"/MD5 ]; then
update_checksum_file
fi
}
check_releases() {
if [ ! -f "$KERL_BASE_DIR"/otp_releases ]; then
get_releases >"$KERL_BASE_DIR"/otp_releases
fi
}
is_valid_release() {
check_releases
while read -r rel; do
if [ "$1" = "$rel" ]; then
return 0
fi
done <"$KERL_BASE_DIR"/otp_releases
return 1
}
assert_valid_release() {
if ! is_valid_release "$1"; then
l=e stderr "$1 is not a valid Erlang/OTP release"
exit 1
fi
return 0
}
get_release_from_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
}
get_newest_valid_release() {
check_releases
rel=$(tail -1 "$KERL_BASE_DIR"/otp_releases)
if [ -n "$rel" ]; then
echo "$rel"
return 0
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
}
assert_valid_installation() {
if ! is_valid_installation "$1"; then
l=e stderr "$1 is not a kerl-managed Erlang/OTP installation"
exit 1
fi
return 0
}
assert_build_name_unused() {
if [ -f "$KERL_BASE_DIR"/otp_builds ]; then
while read -r l; do
name=$(echo "$l" | cut -d, -f2)
if [ "$name" = "$1" ]; then
l=e stderr "There's already a build named $1"
exit 1
fi
done <"$KERL_BASE_DIR"/otp_builds
fi
}
_check_required_pkgs() {
has_dpkg=$(command -v dpkg)
has_rpm=$(command -v rpm)
if [ -n "$has_dpkg" ] || [ -n "$has_rpm" ]; then
# found either dpkg or rpm (or maybe even both!)
if [ -n "$has_dpkg" ] && [ -n "$has_rpm" ]; then
l=w stderr 'WARNING: You appear to have BOTH rpm and dpkg. This is very strange. No package checks done.'
elif [ -n "$has_dpkg" ]; then
_check_dpkg
elif [ -n "$has_rpm" ]; then
_check_rpm
fi
fi
}
_dpkg_is_installed() {
# gratefully stolen from
# https://superuser.com/questions/427318/test-if-a-package-is-installed-in-apt
# returns 0 (true) if found, 1 otherwise
dpkg-query -Wf'${db:Status-abbrev}' "$1" 2>/dev/null | \grep -q '^i'
}
_check_dpkg() {
required='
libssl-dev
make
automake
autoconf
libncurses5-dev
gcc
'
for pkg in $required; do
if ! _dpkg_is_installed "$pkg"; then
l=w stderr "WARNING: It appears that a required development package '$pkg' is not installed."
fi
done
}
_rpm_is_installed() {
rpm --quiet -q "$1" >/dev/null 2>&1
}
_check_rpm() {
required='
openssl-devel
make
automake
autoconf
ncurses-devel
gcc
'
for pkg in $required; do
if ! _rpm_is_installed "$pkg"; then
l=w stderr "WARNING: It appears a required development package '$pkg' is not installed."
fi
done
}
do_git_build() {
assert_build_name_unused "$3"
GIT=$(printf '%s' "$1" | $MD5SUM | cut -d ' ' -f $MD5SUM_FIELD)
mkdir -p "$KERL_GIT_DIR" || exit 1
cd "$KERL_GIT_DIR" || exit 1
l=n stderr "Checking out Erlang/OTP git repository from $1..."
if [ ! -d "$GIT" ]; then
if ! git clone -q --mirror "$1" "$GIT" >/dev/null 2>&1; then
l=e stderr 'Error mirroring remote git repository'
exit 1
fi
fi
cd "$GIT" || exit 1
if ! git remote update --prune >/dev/null 2>&1; then
l=e stderr 'Error updating remote git repository'
exit 1
fi
rm -Rf "${KERL_BUILD_DIR:?}/$3"
mkdir -p "$KERL_BUILD_DIR/$3" || exit 1
cd "$KERL_BUILD_DIR/$3" || exit 1
if ! git clone -l "$KERL_GIT_DIR/$GIT" otp_src_git >/dev/null 2>&1; then
l=e stderr 'Error cloning local git repository'
exit 1
fi
cd otp_src_git || exit 1
if ! git checkout "$2" >/dev/null 2>&1; then
if ! git checkout -b "$2" "$2" >/dev/null 2>&1; then
l=e stderr 'Could not checkout specified version'
rm -Rf "${KERL_BUILD_DIR:?}/$3"
exit 1
fi
fi
if [ ! -x otp_build ]; then
l=e stderr 'Not a valid Erlang/OTP repository'
rm -Rf "${KERL_BUILD_DIR:?}/$3"
exit 1
fi
l=n stderr "Building Erlang/OTP $3 from git, please wait..."
if [ -z "$KERL_BUILD_AUTOCONF" ]; then
KERL_USE_AUTOCONF=1
fi
_do_build 'git' "$3"
l=s stderr "Erlang/OTP $3 from git has been successfully built"
list_add builds git,"$3"
}
get_otp_version() {
echo "$1" | sed $SED_OPT -e 's/R?([0-9]{1,2}).+/\1/'
}
get_perl_version() {
if assert_perl; then
# This is really evil but it's portable and it works. Don't @ me bro
perl -e 'print int(($] - 5)*1000)'
else
l=e stderr 'FATAL: could not find perl which is required to compile Erlang.'
exit 1
fi
}
assert_perl() {
perl_loc=$(command -v perl)
if [ -z "$perl_loc" ]; then
return 1
else
# 0 to bash is "true" because of Unix exit code conventions
return 0
fi
}
get_javac_version() {
java_loc=$(command -v javac)
if [ -z "$java_loc" ]; then
# Java's not installed, so just return 0
0
else
javaout=$(javac -version 2>&1)
echo "$javaout" | cut -d' ' -f2 | cut -d. -f2
fi
}
show_configuration_warnings() {
# $1 is logfile
# $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() {
echo "$1"
tail "$2"
echo
echo "Please see $2 for full details."
}
maybe_patch() {
# $1 = OS platform e.g., Darwin, etc
# $2 = OTP release
release=$(get_otp_version "$2")
case "$1" in
Darwin)
maybe_patch_darwin "$release"
# Catalina and clang require a "no-weaks-import" flag during build
maybe_patch_catalina "$release" "$2"
maybe_patch_macos_version_check "$release"
;;
SunOS)
maybe_patch_sunos "$release"
;;
*)
;;
esac
maybe_patch_all "$release"
}
maybe_patch_all() {
perlver=$(get_perl_version)
if [ "$perlver" -ge 22 ]; then
case "$1" in
14)
apply_r14_beam_makeops_patch >>"$LOGFILE"
;;
15)
apply_r15_beam_makeops_patch >>"$LOGFILE"
;;
*)
;;
esac
fi
# Are we building docs?
if [ -n "$KERL_BUILD_DOCS" ]; then
if [ "$1" -le 16 ]; then
javaver=$(get_javac_version)
if [ "$javaver" -ge 8 ]; then
apply_javadoc_linting_patch >>"$LOGFILE"
fi
fi
fi
# Maybe apply zlib patch
if [ "$1" -ge 17 ] && [ "$1" -le 19 ]; then
apply_zlib_patch >> "$LOGFILE"
fi
}
maybe_patch_darwin() {
# Reminder: $1 = OTP release version
if [ "$1" -le 14 ]; then
CFLAGS='-DERTS_DO_INCL_GLB_INLINE_FUNC_DEF'
apply_darwin_compiler_patch >>"$LOGFILE"
elif [ "$1" -eq 16 ]; then
apply_r16_wx_ptr_patch >>"$LOGFILE"
elif [ "$1" -ge 17 ] && [ "$1" -le 19 ]; then
apply_wx_ptr_patch >>"$LOGFILE"
elif [ "$1" -ge 21 ] && [ "$1" -le 23 ]; then
apply_in6addr_test_patch >> "$LOGFILE"
KERL_USE_AUTOCONF=1
fi
}
maybe_patch_catalina() {
clang_version=$(/usr/bin/clang --version | head -1 | awk '{print $4}')
command_line_tools_version=$(xcode-select -v | awk '{print $3}' | sed 's/.$//')
release="$1"
otp_version="$2"
if is_osx_catalina && \
[[ "$release" -lt 23 ]] && \
compare_sem_version $otp_version "<" "22.3.1" && \
compare_sem_version $clang_version ">" "11.4" && \
[[ "$command_line_tools_version" -le 2373 ]]; then
apply_catalina_no_weak_imports_patch >>"$LOGFILE"
KERL_USE_AUTOCONF=1
fi
}
is_osx_catalina() {
[[ $(uname -r) == "19"* ]]
}
compare_sem_version() {
version=$1
operator=$2
baseline=$3
case $operator in
'<')
[[ $(printf "$version\n$baseline" | sort --version-sort | head -n 1) != "$baseline" ]]
;;
('>' | "==")
[[ $(printf "$version\n$baseline" | sort --version-sort | head -n 1) == "$baseline" ]]
;;
esac
}
apply_catalina_no_weak_imports_patch() {
patch -p1 <<'_END_PATCH'
diff --git a/erts/configure.in b/erts/configure.in
index 3ba8216a19..d7cebc5ebc 100644
--- a/erts/configure.in
+++ b/erts/configure.in
@@ -926,20 +926,16 @@ dnl for now that is the way we do it.
USER_LD=$LD
USER_LDFLAGS="$LDFLAGS"
LD='$(CC)'
+
case $host_os in
- darwin*)
- saved_LDFLAGS="$LDFLAGS"
- LDFLAGS="$LDFLAGS -Wl,-no_weak_imports"
- AC_TRY_LINK([],[],
- [
- LD_MAY_BE_WEAK=no
- ],
- [
- LD_MAY_BE_WEAK=yes
- LDFLAGS="$saved_LDFLAGS"
- ]);;
- *)
- LD_MAY_BE_WEAK=no;;
+ darwin19*)
+ # Disable stack checking to avoid crashing with a segment fault
+ # in macOS Catalina.
+ AC_MSG_NOTICE([Turning off stack check on macOS 10.15 (Catalina)])
+ CFLAGS="-Wno-error=implicit-function-declaration -fno-stack-check $CFLAGS"
+ ;;
+ *)
+ ;;
esac
AC_SUBST(LD)
_END_PATCH
}
# https://github.com/erlang/otp/commit/f1044ef9e35da26f276b8127640e177d67aade6a.diff
maybe_patch_macos_version_check() {
release="$1"
if is_osx_bigsur || is_osx_monterey && \
[[ "$release" -lt 24 ]]; then
apply_bypass_version_check >>"$LOGFILE"
KERL_USE_AUTOCONF=1
fi
}
is_osx_bigsur() {
[[ $(uname -r) == "20"* ]]
}
is_osx_monterey() {
[[ $(uname -r) == "21"* ]]
}
apply_bypass_version_check() {
if [ -f make/configure.in ]; then
l=n stderr "Bypassing version check in make/configure.in"
sed -i '' 's/\(#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > $int_macosx_version\)/\1 \&\& false/' make/configure.in
elif [ -f configure.in ]; then
l=n stderr "Bypassing version check in configure.in"
sed -i '' 's/\(#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > $int_macosx_version\)/\1 \&\& false/' configure.in
l=n stderr "Removing no_weak_imports from LDFLAGS in erts/configure.in"
sed -i '' 's/LDFLAGS="$LDFLAGS -Wl,-no_weak_imports"//' erts/configure.in
fi
}
maybe_patch_sunos() {
if [ "$1" -le 14 ]; then
apply_solaris_networking_patch >>"$LOGFILE"
fi
}
do_normal_build() {
assert_valid_release "$1"
assert_build_name_unused "$2"
FILENAME=""
download "$1"
mkdir -p "$KERL_BUILD_DIR/$2" || exit 1
if [ ! -d "$KERL_BUILD_DIR/$2/$FILENAME" ]; then
l=n stderr 'Extracting source code'
UNTARDIRNAME="$KERL_BUILD_DIR/$2/$FILENAME-kerluntar-$$"
rm -rf "$UNTARDIRNAME"
mkdir -p "$UNTARDIRNAME" || exit 1
# 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" && tar xzf "$KERL_DOWNLOAD_DIR/$FILENAME".tar.gz && cp -rfp ./* "$KERL_BUILD_DIR/$2/otp_src_$1")
rm -rf "$UNTARDIRNAME"
fi
l=n stderr "Building Erlang/OTP $1 ($2), please wait..."
_do_build "$1" "$2"
l=s stderr "Erlang/OTP $1 ($2) has been successfully built"
list_add builds "$1,$2"
}
_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
}
_do_build() {
case "$KERL_SYSTEM" in
Darwin)
# Ensure that the --enable-darwin-64bit flag is set on all macOS
# That way even on older Erlangs we get 64 bit Erlang builds
# macOS has been mandatory 64 bit for a while
if ! echo "$KERL_CONFIGURE_OPTIONS" | \grep 'darwin-64bit' >/dev/null 2>&1; then
KERL_CONFIGURE_OPTIONS="$KERL_CONFIGURE_OPTIONS "--enable-darwin-64bit
fi
# Attempt to use brew to discover if and where openssl has been
# installed unless the user has already explictly set it.
if ! echo "$KERL_CONFIGURE_OPTIONS" | \grep 'with-ssl' >/dev/null 2>&1; then
whichbrew=$(command -v brew)
if [ -n "$whichbrew" ] && [ -x "$whichbrew" ]; then
brew_prefix=$(brew --prefix openssl@1.1)
if [ -n "$brew_prefix" ] && [ -d "$brew_prefix" ]; then
KERL_CONFIGURE_OPTIONS="$KERL_CONFIGURE_OPTIONS "--with-ssl=$brew_prefix
fi
fi
fi
;;
Linux)
# we are going to check here to see if the Linux uses dpkg or rpms
#
# this is a "best effort" attempt to discover if a Linux has the
# packages needed to build Erlang. We will always assume the user
# knows better than us and are going to go ahead and try to build
# Erlang anyway. But at least it will be a clear warning to the
# user if a build fails.
_check_required_pkgs
;;
*)
;;
esac
if [ -n "$KERL_BUILD_DOCS" ]; then
KERL_CONFIGURE_OPTIONS="$KERL_CONFIGURE_OPTIONS --prefix=$KERL_BUILD_DIR/$2/release_$1"
fi
ERL_TOP="$KERL_BUILD_DIR/$2/otp_src_$1"
cd "$ERL_TOP" || exit 1
LOGFILE="$KERL_BUILD_DIR/$2/otp_build_$1.log"
# Set configuation flags given applications white/black lists
if [ -n "$KERL_CONFIGURE_APPLICATIONS" ]; then
for app in $KERL_CONFIGURE_APPLICATIONS; do
case "$KERL_CONFIGURE_OPTIONS" in
*"--with-$app"*)
l=t stderr "Option '--with-$app' in KERL_CONFIGURE_OPTIONS is superfluous" ;;
*)
KERL_CONFIGURE_OPTIONS="$KERL_CONFIGURE_OPTIONS --with-$app" ;;
esac
done
fi
if [ -n "$KERL_CONFIGURE_DISABLE_APPLICATIONS" ]; then
for app in $KERL_CONFIGURE_DISABLE_APPLICATIONS; do
case "$KERL_CONFIGURE_OPTIONS" in
*"--without-$app"*)
l=t stderr "Option '--without-$app' in KERL_CONFIGURE_OPTIONS is superfluous" ;;
*)
KERL_CONFIGURE_OPTIONS="$KERL_CONFIGURE_OPTIONS --without-$app" ;;
esac
done
fi
# Check to see if configuration options need to be stored or have changed
TMPOPT="${TMP_DIR}/kerloptions.$$"
echo "$CFLAGS" >"$TMPOPT"
echo "$KERL_CONFIGURE_OPTIONS" >>"$TMPOPT"
SUM=$($MD5SUM "$TMPOPT" | cut -d ' ' -f $MD5SUM_FIELD)
# Check for a .kerl_config.md5 file
if [ -e ./"$KERL_CONFIG_STORAGE_FILENAME".md5 ]; then
# Compare our current options to the saved ones
read -r OLD_SUM <./"$KERL_CONFIG_STORAGE_FILENAME".md5
if [ "$SUM" != "$OLD_SUM" ]; then
l=n stderr 'Configure options have changed. Reconfiguring...'
rm -f configure
mv "$TMPOPT" ./"$KERL_CONFIG_STORAGE_FILENAME"
echo "$SUM" >./"$KERL_CONFIG_STORAGE_FILENAME".md5
else
# configure options are the same
rm -f "$TMPOPT"
fi
else
# no file exists, so write one
mv "$TMPOPT" .kerl_config
echo "$SUM" >.kerl_config.md5
fi
# Don't apply patches to "custom" git builds. We have no idea if they will apply
# cleanly or not.
if [ "$1" != 'git' ]; then
maybe_patch "$KERL_SYSTEM" "$1"
fi
if [ -n "$KERL_USE_AUTOCONF" ]; then
# shellcheck disable=SC2086
./otp_build autoconf $KERL_CONFIGURE_OPTIONS >>"$LOGFILE" 2>&1 && \
_flags ./otp_build configure $KERL_CONFIGURE_OPTIONS >>"$LOGFILE" 2>&1
else
# shellcheck disable=SC2086
_flags ./otp_build configure $KERL_CONFIGURE_OPTIONS >>"$LOGFILE" 2>&1
fi
if echo "$KERL_CONFIGURE_OPTIONS" | \grep -- '--enable-native-libs' >/dev/null 2>&1; then
make clean >>"$LOGFILE" 2>&1
# shellcheck disable=SC2086
if ! _flags ./otp_build configure $KERL_CONFIGURE_OPTIONS >>"$LOGFILE" 2>&1; then
show_logfile 'Configure failed.' "$LOGFILE"
list_remove builds "$1 $2"
exit 1
fi
fi
for SECTION in 'APPLICATIONS DISABLED' \
'APPLICATIONS INFORMATION' \
'DOCUMENTATION INFORMATION'; do
show_configuration_warnings "$LOGFILE" "$SECTION"
done
if [ -n "$KERL_CONFIGURE_APPLICATIONS" ]; then