forked from rust-lang-deprecated/rustup.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rustup.sh
executable file
·1953 lines (1639 loc) · 58.9 KB
/
rustup.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
#!/bin/sh
# Copyright 2015 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
# # Coding conventions
#
# * globals are `like_this`.
# * locals are `_like_this`.
# * exported values are `LIKE_THIS`.
# * out-of-band return values are put into `RETVAL`.
#
# # Error handling
#
# Oh, my goodness, error handling. It's terrifying.
#
# This doesn't use -e because it makes it hard to control the
# presentation of and response to errors.
#
# `set -u` is on, which means undefined variables are errors.
# Generally when evaluating a variable that may not exist I'll
# write `${mystery_variable-}`, which results in "" if the name
# is undefined.
#
# Every command should be expected to return 0 on success, and
# non-zero on failure. In one case, for `download_and_check`, the
# error code needs to be interpreted more carefully because there are
# multiple successful return codes. Additional return values may be
# passed the `$RETVAL` global or further `RETVAL_FOO` globals as
# needed.
#
# Most commands are executed via wrappers that provide extra diagnostics
# and error handling: `run`, which prints the command on failure, and
# returns the error code, `ignore` which does the same, but is used
# to indicate the error code won't be handled, and `ensure`, which
# prints the command on failure, and also exits the process.
#
# Pass errors on: `run cmd arg1 arg2 || return 1`. `run` will run
# the command, printing it if it fails; the `|| return 1` passes the
# error on to the caller. `ensure cmd arg1 arg1`, runs the command,
# printing it if it fails, and terminating execution.
#
# Don't make typos. You just have to be better than that.
#
# This code is very careful never to create empty paths. Any time a
# new string that will be used as a path is produced, it is checked
# with `assert_nz`. Likewise, pretty much any time a string is
# constructed via command invocation it needs to be tested against
# the empty string.
#
# Temporary files must be carefully deleted on every error path.
set -u # Undefined variables are errors
main() {
assert_cmds
set_globals
handle_command_line_args "$@"
}
set_globals() {
# Environment sanity checks
assert_nz "$HOME" "\$HOME is undefined"
assert_nz "$0" "\$0 is undefined"
# Some constants
version=0.0.1
metadata_version=1
# Find the location of the distribution server
default_dist_server="https://static.rust-lang.org"
insecure_dist_server="http://static-rust-lang-org.s3-website-us-west-1.amazonaws.com"
dist_server="${RUSTUP_DIST_SERVER-$default_dist_server}"
gpg_available=false
# Check to see if GNUPG version 2 is installed, falling back to using version 1 by default
gpg_exe=gpg
if command -v gpg2 > /dev/null 2>&1; then
gpg_exe=gpg2
fi
if command -v "$gpg_exe" > /dev/null 2>&1; then
gpg_available=true
fi
# The directory on the server containing the dist artifacts
rust_dist_dir=dist
default_channel="stable"
# Set up the rustup data dir
rustup_dir="${RUSTUP_HOME-$HOME/.rustup}"
assert_nz "$rustup_dir" "rustup_dir"
# Install prefix can be set by the environment
default_prefix="${RUSTUP_PREFIX-/usr/local}"
default_save=false
if [ -n "${RUSTUP_SAVE-}" ]; then
default_save=true
fi
# Data locations
version_file="$rustup_dir/rustup-version"
temp_dir="$rustup_dir/tmp"
dl_dir="$rustup_dir/dl"
# Set up the GPG key
official_rust_gpg_key="
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQINBFJEwMkBEADlPACa2K7reD4x5zd8afKx75QYKmxqZwywRbgeICeD4bKiQoJZ
dUjmn1LgrGaXuBMKXJQhyA34e/1YZel/8et+HPE5XpljBfNYXWbVocE1UMUTnFU9
CKXa4AhJ33f7we2/QmNRMUifw5adPwGMg4D8cDKXk02NdnqQlmFByv0vSaArR5kn
gZKnLY6o0zZ9Buyy761Im/ShXqv4ATUgYiFc48z33G4j+BDmn0ryGr1aFdP58tHp
gjWtLZs0iWeFNRDYDje6ODyu/MjOyuAWb2pYDH47Xu7XedMZzenH2TLM9yt/hyOV
xReDPhvoGkaO8xqHioJMoPQi1gBjuBeewmFyTSPS4deASukhCFOcTsw/enzJagiS
ZAq6Imehduke+peAL1z4PuRmzDPO2LPhVS7CDXtuKAYqUV2YakTq8MZUempVhw5n
LqVaJ5/XiyOcv405PnkT25eIVVVghxAgyz6bOU/UMjG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pVR7fmrMZ0t+yENd+9qzyAZs0ksxORoc2ze90SCx2jwEX/3K+m4I0hP2H/w5W
gqdvuRLiqf+4BGW4zqWkLLlNIe/okt0r82SwHtDN0Ui1asmZTGj6sm8SXtwx+5cE
38MttWqjDiibQOSthRVcETByRYM8KcjYSUCi4PoBc3NpDONkFbZm6XofR/f5mTcl
2jDw6fIeVc4Hd1jBGajNzEqtneqqbdAkPQaLsuD2TMkQfTDJfE/IljwjrhDa9Mi+
odtnMWq8vlwOZZ24/8/BNK5qXuCYL67O7AJB4ZQ6BT+g4z96iRLbupzu/XJyXkQF
rOY/Ghegvn7fDrnt2KC9MpgeFBXzUp+k5rzUdF8jbCx5apVjA1sWXB9Kh3L+DUwF
Mve696B5tlHyc1KxjHR6w9GRsh4=
=5FXw
-----END PGP PUBLIC KEY BLOCK-----
"
if [ -n "${RUSTUP_GPG_KEY-}" ]; then
gpg_key=$(cat "$RUSTUP_GPG_KEY")
else
gpg_key="$official_rust_gpg_key"
fi
# This is just used by test.sh for testing sha256sum fallback to shasum
sha256sum_cmd="${__RUSTUP_MOCK_SHA256SUM-sha256sum}"
flag_verbose=false
flag_yes=false
if [ -n "${RUSTUP_VERBOSE-}" ]; then
flag_verbose=true
fi
}
# Ensuresthat ~/.rustup exists and uses the correct format
initialize_metadata() {
local _disable_sudo="$1"
verbose_say "checking metadata version"
if [ "$rustup_dir" = "$HOME" ]; then
err "RUSTUP_HOME is the same as HOME. this cannot be correct. aborting"
fi
# This tries to guard against dumb values of RUSTUP_HOME like ~/ since
# rustup will delete the entire directory.
if [ -e "$rustup_dir" -a ! -e "$version_file" ]; then
say "rustup home dir exists at $rustup_dir but version file $version_file does not."
say "this may be old rustup metadata, in which case it can be deleted."
err "this is very suspicous. aborting."
fi
# Oh, my. We used to encourage people running this script as root,
# and that resulted in users' ~/.rustup directories being owned by
# root (running `sudo sh` doesn't change $HOME apparently). Now
# that we're not running as root, we can't touch our ~/.rustup
# directory. Try to fix that.
if [ -e "$version_file" ]; then
local _can_write=true
local _probe_file="$rustup_dir/write-probe"
ignore touch "$_probe_file" 2> /dev/null
if [ $? != 0 ]; then
_can_write=false
else
ensure rm "$_probe_file"
fi
if [ "$_can_write" = false ]; then
say "$rustup_dir is unwritable. it was likely created by a previous rustup run under sudo"
if [ "$_disable_sudo" = false ]; then
say "deleting it with sudo"
run sudo rm -R "$rustup_dir"
if [ $? != 0 ]; then
err "unable to delete unwritable $rustup_dir"
fi
else
say_err "not deleting it because of --disable-sudo"
err "delete $rustup_dir to continue. aborting"
fi
fi
fi
ensure mkdir -p "$rustup_dir"
rustup_dir="$(abs_path "$rustup_dir")"
assert_nz "$rustup_dir" "rustup_dir"
if [ ! -e "$version_file" ]; then
verbose_say "writing metadata version $metadata_version"
echo "$metadata_version" > "$version_file"
need_ok "failed to write metadata version"
else
local _current_version="$(ensure cat "$version_file")"
assert_nz "$_current_version"
verbose_say "got metadata version $_current_version"
if [ "$_current_version" != "$metadata_version" ]; then
# Wipe the out of date metadata.
say "metadata is out of date. deleting."
ensure rm -R "$rustup_dir"
ensure mkdir -p "$rustup_dir"
echo "$metadata_version" > "$version_file"
need_ok "failed to write metadata version"
fi
fi
}
handle_command_line_args() {
local _save="$default_save"
local _date=""
local _prefix="$default_prefix"
local _uninstall=false
local _channel=""
local _help=false
local _revision=""
local _spec=""
local _update_hash_file=""
local _disable_ldconfig=false
local _disable_sudo=false
local _extra_targets=""
local _add_target=""
local _list_targets=false
local _arg
for _arg in "$@"; do
case "$_arg" in
--save )
_save=true
;;
--uninstall )
_uninstall=true
;;
--help )
_help=true
;;
--verbose)
# verbose is a global flag
flag_verbose=true
;;
--disable-ldconfig)
_disable_ldconfig=true
;;
--disable-sudo)
_disable_sudo=true
;;
-y | --yes)
# yes is a global flag
flag_yes=true
;;
--list-available-targets)
_list_targets=true
;;
--version)
echo "rustup.sh $version"
exit 0
;;
esac
if is_value_arg "$_arg" "prefix"; then
_prefix="$(get_value_arg "$_arg")"
elif is_value_arg "$_arg" "channel"; then
_channel="$(get_value_arg "$_arg")"
elif is_value_arg "$_arg" "date"; then
_date="$(get_value_arg "$_arg")"
elif is_value_arg "$_arg" "revision"; then
_revision="$(get_value_arg "$_arg")"
elif is_value_arg "$_arg" "spec"; then
_spec="$(get_value_arg "$_arg")"
elif is_value_arg "$_arg" "update-hash-file"; then
# This option is used by multirust to short-circuit reinstalls
# when the channel has not been updated by examining a content
# hash in the update-hash-file
_update_hash_file="$(get_value_arg "$_arg")"
elif is_value_arg "$_arg" "with-target"; then
local _next_extra_target="$(get_value_arg "$_arg")"
_extra_targets="$_extra_targets $_next_extra_target"
elif is_value_arg "$_arg" "add-target"; then
_add_target="$(get_value_arg "$_arg")"
fi
done
if [ "$_help" = true ]; then
print_help
exit 0
fi
# Make sure either rust256sum or shasum exists
need_shasum_cmd
# Check that the various toolchain-specifying flags don't conflict
if [ -n "$_revision" ]; then
if [ -n "$_channel" ]; then
err "the --revision flag may not be combined with --channel"
fi
if [ -n "$_date" ]; then
err "the --revision flag may not be combined with --date"
fi
fi
if [ -n "$_spec" ]; then
if [ -n "$_channel" ]; then
err "the --spec flag may not be combined with --channel"
fi
if [ -n "$_revision" ]; then
err "the --spec flag may not be combined with --revision"
fi
fi
if [ -n "$_add_target" ]; then
if [ -n "$_channel" ]; then
err "the --add-target flag may not be combined with --channel"
fi
if [ -n "$_date" ]; then
err "the --add-target flag may not be combined with --date"
fi
if [ -n "$_spec" ]; then
err "the --add-target flag may not be combined with --spec"
fi
if [ -n "$_revision" ]; then
err "the --add-target flag may not be combined with --revision"
fi
fi
if [ "$_list_targets" = true ]; then
if [ -n "$_channel" ]; then
err "the --list-available-targets flag may not be combined with --channel"
fi
if [ -n "$_date" ]; then
err "the --list-available-targets flag may not be combined with --date"
fi
if [ -n "$_spec" ]; then
err "the --list-available-targets flag may not be combined with --spec"
fi
if [ -n "$_revision" ]; then
err "the --list-available-targets flag may not be combined with --revision"
fi
fi
if [ -z "$_channel" -a -z "$_revision" -a -z "$_spec" ]; then
_channel="$default_channel"
fi
# Toolchain can be either a channel, channel + date, or an explicit version
local _toolchain=""
if [ -n "$_channel" ]; then
validate_channel "$_channel"
_toolchain="$_channel"
if [ -n "$_date" ]; then
validate_date "$_date"
_toolchain="$_toolchain-$_date"
fi
elif [ -n "$_revision" ]; then
_toolchain="$_revision"
elif [ -n "$_spec" ]; then
_toolchain="$_spec"
fi
assert_nz "$_toolchain" "toolchain"
# --add-target is non-interactive
if [ -n "$_add_target" ]; then
flag_yes=true
fi
# --list-targets is non-interactive
if [ -n "$_list_targets" ]; then
flag_yes=true
fi
if [ "$flag_yes" = false ]; then
# Running in interactive mode, check that a tty exists
check_tty
# Print the welcome / warning message and wait for confirmation
print_welcome_message "$_prefix" "$_uninstall" "$_disable_sudo"
get_tty_confirmation
fi
# All work is done in the ~/.rustup dir, which will be deleted
# afterward if the user doesn't pass --save. *If* ~/.rustup
# already exists and they *did not* pass --save, we'll pretend
# they did anyway to avoid deleting their data.
local _preserve_rustup_dir="$_save"
if [ "$_save" = false -a -e "$version_file" ]; then
verbose_say "rustup home exists but not asked to save. saving anyway"
_preserve_rustup_dir=true
fi
# Make sure our data directory exists and is the right format
initialize_metadata "$_disable_sudo"
# OK, time to do the things
local _succeeded=true
if [ "$_list_targets" = true ]; then
list_targets "$_prefix"
if [ $? != 0 ]; then
_succeeded=false
fi
elif [ -n "$_add_target" ]; then
add_target_to_install "$_prefix" "$_add_target" "$_save" "$_disable_sudo"
if [ $? != 0 ]; then
_succeeded=false
fi
elif [ "$_uninstall" = false ]; then
install_toolchain_from_dist "$_toolchain" "$_prefix" "$_save" "$_update_hash_file" \
"$_disable_ldconfig" "$_disable_sudo" "$_extra_targets"
if [ $? != 0 ]; then
_succeeded=false
fi
else
remove_toolchain "$_prefix" "$_disable_sudo"
if [ $? != 0 ]; then
_succeeded=false
fi
fi
# Remove the temporary directory.
# This will not happen if we hit certain hard errors earlier.
if [ "$_preserve_rustup_dir" = false ]; then
verbose_say "removing rustup home $rustup_dir"
ensure rm -R "$rustup_dir"
else
verbose_say "leaving rustup home $rustup_dir"
fi
if [ "$_succeeded" = false ]; then
exit 1
fi
}
is_value_arg() {
local _arg="$1"
local _name="$2"
echo "$_arg" | grep -q -- "--$_name="
return $?
}
get_value_arg() {
local _arg="$1"
echo "$_arg" | cut -f2 -d=
}
validate_channel() {
local _channel="$1"
case "$_channel" in
stable | beta | nightly )
;;
* )
err "channel must be either 'stable', 'beta', or 'nightly'"
;;
esac
}
validate_date() {
local _date="$1"
case "$_date" in
[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] )
;;
* )
err "date must be in YYYY-MM-DD format"
;;
esac
}
print_welcome_message() {
local _prefix="$1"
local _uninstall="$2"
local _disable_sudo="$3"
cat <<EOF
Welcome to Rust.
EOF
if [ "$_disable_sudo" = false ]; then
if [ "$(id -u)" = 0 ]; then
cat <<EOF
WARNING: This script appears to be running as root. While it will work
correctly, it is no longer necessary for rustup.sh to run as root.
EOF
fi
fi
if [ "$_uninstall" = false ]; then
cat <<EOF
This script will download the Rust compiler and its package manager, Cargo, and
install them to $_prefix. You may install elsewhere by running this script
with the --prefix=<path> option.
EOF
else
cat <<EOF
This script will uninstall the existing Rust installation at $_prefix.
EOF
fi
if [ "$_disable_sudo" = false ]; then
cat <<EOF
The installer will run under 'sudo' and may ask you for your password. If you do
not want the script to run 'sudo' then pass it the --disable-sudo flag.
EOF
fi
if [ "$_uninstall" = false ]; then
cat <<EOF
You may uninstall later by running $_prefix/lib/rustlib/uninstall.sh,
or by running this script again with the --uninstall flag.
EOF
fi
echo
}
# Updating toolchains
# Returns 0 on success, 1 on error
install_toolchain_from_dist() {
local _toolchain="$1"
local _prefix="$2"
local _save="$3"
local _update_hash_file="$4"
local _disable_ldconfig="$5"
local _disable_sudo="$6"
local _extra_targets="$7"
# FIXME: Right now installing rust over top of multirust will
# result in a broken multirust installation.
# This hack tries to avoid that by detecting if multirust is installed,
# but I'd rather fix this by having the installers understand negative
# dependencies.
local _potential_multirust_bin="$_prefix/bin/multirust"
if [ -e "$_potential_multirust_bin" ]; then
say_err "multirust appears to be installed at the destination, $_potential_multirust_bin"
say_err "installing rust over multirust will result in breakage."
local _potential_uninstaller="$_prefix/lib/rustlib/uninstall.sh"
if [ -e "$_potential_uninstaller" ]; then
say_err "consider uninstalling multirust first by running $_potential_uninstaller"
fi
err "aborting"
fi
if [ "$gpg_available" = true ]; then
# disabling https avoids rust#21293
say "gpg available. signatures will be verified"
else
say "gpg not available. signatures will not be verified"
fi
get_architecture || return 1
local _arch="$RETVAL"
assert_nz "$_arch" "arch"
# Inspect any existing installation for additional stds that must be upgraded
# and add them to the list
merge_existing_extra_targets "$_extra_targets" "$_arch" "$_prefix" || return 1
_extra_targets="$RETVAL"
# We're going to fill in this variables by interrogating the channel
# metadata. There are two revisions of the channel metadata, v1 was
# a very simple list of binaries; v2 has richer information about
# the available packages.
# The URL of the main installer
local _remote_rust_installer
# A space-separated list of other things to install
local _extra_remote_installers
local _manifest_to_stash=""
# First, try to download a v2 manifest, before falling back to v1 codepaths.
download_rust_manifest_v2 "$_toolchain"
if [ $? = 0 ]; then
local _manifest="$RETVAL"
assert_nz "$_manifest" "manifest"
# We'll save the manifest in the install folder for future modifications
_manifest_to_stash="$_manifest"
validate_manifest_v2 "$_manifest"
if [ $? != 0 ]; then
say_err "failed to validate channel manifest for '$_toolchain'"
return 1
fi
determine_remote_rust_installer_location_v2 "$_manifest"
if [ $? != 0 ]; then
say_err "unable to find installer url in manifest"
return 1
fi
_remote_rust_installer="$RETVAL"
if [ "$_extra_targets" != "" ]; then
# The user has asked for additional standard libraries.
# Figure out where they are.
determine_remote_std_locations_v2 "$_manifest" "$_extra_targets" || return 1
_extra_remote_installers="$RETVAL"
else
_extra_remote_installers=""
fi
else
verbose_say "unable to find v2 manifest. trying v1"
if [ "$_extra_targets" != "" ]; then
say_err "v1 manifests don't support --with-target"
return 1
else
_extra_remote_installers=""
fi
determine_remote_rust_installer_location "$_toolchain" || return 1
_remote_rust_installer="$RETVAL"
fi
assert_nz "$_remote_rust_installer" "remote rust installer"
verbose_say "remote rust installer location: $_remote_rust_installer"
say "downloading toolchain for '$_toolchain'"
# Download and install rust package
download_and_check "$_remote_rust_installer" false "$_update_hash_file"
# Hey! I need to check $? twice here, so it has to be
# assigned to a named variable, otherwise the second
# check against $? will not be what I expect.
local _retval=$?
if [ "$_retval" = 20 ]; then
say "'$_toolchain' is already up to date"
# Successful short-circuit using the update-hash
return 0
fi
if [ "$_retval" != 0 ]; then
return 1
fi
local _rust_installer_file="$RETVAL"
local _rust_installer_cache="$RETVAL_CACHE"
local _rust_update_hash="$RETVAL_UPDATE_HASH"
assert_nz "$_rust_installer_file" "rust_installer_file"
assert_nz "$_rust_installer_cache" "rust_installer_cache"
assert_nz "$_rust_update_hash" "rust_update_hash"
say "installing toolchain for '$_toolchain'"
install_toolchain "$_rust_installer_file" "$_prefix" \
"$_disable_ldconfig" "$_disable_sudo" "$_rust_installer_cache" "$_save"
if [ $? != 0 ]; then
say_err "failed to install toolchain"
return 1
fi
# Download and install extra packages
# NB: Splitting $_extra_remote_installers on space by not quoting
local _extra_remote_installer
for _extra_remote_installer in $_extra_remote_installers; do
install_extra_component "$_prefix" "$_extra_remote_installer" "$_disable_sudo" "$_save"
done
# Write the update hash of the rust toolchain to file so that,
# when invoked by multirust, the toolchain won't be reinstalled.
if [ -n "$_update_hash_file" ]; then
echo "$_rust_update_hash" > "$_update_hash_file"
if [ $? != 0 ]; then
say_err "failed to write update hash to file"
return 1
fi
fi
# Install the manifest for future updates
if [ "$_manifest_to_stash" != "" ]; then
# Fix for rust-lang/rust#32154. Somehow rustup.sh managed
# until today to exist without escaping ~ in prefix. Probably
# because it's only ultimately used by the install script,
# which is called via sh. This command here though will fail
# if prefix contains ~ so run it through `sh` to escape it.
local _prefix="$(sh -c "printf '%s' $_prefix")"
ensure printf "%s" "$_manifest_to_stash" > "$_prefix/lib/rustlib/channel-manifest.toml"
fi
}
merge_existing_extra_targets() {
local _extra_targets="$1"
local _primary_arch="$2"
local _prefix="$3"
local _components_file="$_prefix/lib/rustlib/components"
if [ ! -e "$_components_file" ]; then
RETVAL="$_extra_targets"
return 0
fi
local _component
while read _component in; do
case "$_component" in
rust-std-*)
# Extract the triple
local _arch="$(ensure printf "%s" "$_component" | ensure sed "s/rust-std-//")"
assert_nz "$_arch", "arch"
# See if we've already got it
ignore printf "%s" "$_extra_targets" | grep -q "$_arch"
if [ $? = 0 ]; then
verbose_say "already installing extra std component: $_arch"
else
# See if it's the primary target
ignore printf "%s" "$_primary_arch" | grep -q "$_arch"
if [ $? = 0 ]; then
verbose_say "already installing extra std component: $_arch"
else
verbose_say "found extra std component: $_arch"
_extra_targets="$_extra_targets $_arch"
fi
fi
;;
*)
;;
esac
done < "$_components_file"
RETVAL="$_extra_targets"
return 0
}
install_toolchain() {
local _installer_file="$1"
local _prefix="$2"
local _disable_ldconfig="$3"
local _disable_sudo="$4"
local _installer_cache="$5"
local _save="$6"
# Create a temp directory to put the downloaded toolchain
make_temp_dir
local _workdir="$RETVAL"
assert_nz "$_workdir" "workdir"
verbose_say "install work dir: $_workdir"
local _failing=false
install_toolchain_with_workdir "$_installer_file" "$_prefix" \
"$_disable_ldconfig" "$_disable_sudo" "$_workdir"
if [ $? != 0 ]; then
_failing=true
fi
local _retval=$?
run rm -R "$_workdir"
if [ $? != 0 ]; then
say_err "couldn't delete workdir"
_failing=true
fi
# Throw away the cache if not --save
if [ "$_save" = false ]; then
verbose_say "discarding cache '$_installer_cache'"
run rm -R "$_installer_cache"
if [ $? != 0 ]; then
say_err "couldn't delete cache dir"
_failing=true
fi
fi
if [ "$_failing" = true ]; then
return 1
fi
}
install_toolchain_with_workdir() {
local _installer="$1"
local _prefix="$2"
local _disable_ldconfig="$3"
local _disable_sudo="$4"
local _workdir="$5"
local _installer_dir="$_workdir/$(basename "$_installer" | sed s/.tar.gz$//)"
# Extract the toolchain
say "extracting installer"
run tar xzf "$_installer" -C "$_workdir"
if [ $? != 0 ]; then
verbose_say "failed to extract installer"
return 1
fi
# Install the toolchain
local _toolchain_dir="$_prefix"
verbose_say "installing toolchain to '$_toolchain_dir'"
if [ "$_disable_ldconfig" = false ]; then
maybe_sudo "$_disable_sudo" sh "$_installer_dir/install.sh" --prefix="$_toolchain_dir"
else
maybe_sudo "$_disable_sudo" sh "$_installer_dir/install.sh" --prefix="$_toolchain_dir" --disable-ldconfig
fi
if [ $? != 0 ]; then
verbose_say "failed to install toolchain"
return 1
fi
}
remove_toolchain() {
local _prefix="$1"
local _disable_sudo="$2"
local _uninstall_script="$_prefix/lib/rustlib/uninstall.sh"
if [ -e "$_uninstall_script" ]; then
verbose_say "uninstalling from '$_uninstall_script'"
maybe_sudo "$_disable_sudo" sh "$_uninstall_script"
if [ $? != 0 ]; then
say_err "failed to remove toolchain"
return 1;
fi
say "toolchain '$_toolchain' uninstalled"
else
say "no toolchain installed at '$_prefix'"
fi
}
add_target_to_install() {
local _prefix="$1"
local _target="$2"
local _save="$3"
local _disable_sudo="$4"
local _manifest_file="$_prefix/lib/rustlib/channel-manifest.toml"
if [ ! -e "$_manifest_file" ]; then
say_err "no channel manifest at '$_manifest_file'"
return 1
fi
local _manifest="$(cat "$_manifest_file")"
determine_remote_std_locations_v2 "$_manifest" "$_target" || return 1
local _url="$RETVAL"
# NB: No quotes around $url - it's a space-separated list with one element. Removing
# the quotes to get rid of an extra space
install_extra_component "$_prefix" $_url "$_disable_sudo" "$_save"
}
list_targets() {
local _prefix="$1"
local _manifest_file="$_prefix/lib/rustlib/channel-manifest.toml"
if [ ! -e "$_manifest_file" ]; then
say_err "no channel manifest at '$_manifest_file'"
return 1
fi
local _manifest="$(cat "$_manifest_file")"
toml_find_package_triples "$_manifest" rust-std
if [ $? != 0 ]; then
say_err "error searching manifest for targets"
return 1
fi
local _all_stds="$RETVAL"
# NB: Not quoting to split on space
local _std
for _std in $_all_stds; do
printf "%s\n" "$_std"
done
}
install_extra_component() {
local _prefix="$1"
local _url="$2"
local _disable_sudo="$3"
local _save="$4"
say "downloading extra component from $_url"
download_and_check "$_url" false ""
# Don't need to check for the second success value since
# we didn't pass an update hash file to download_and_check
if [ $? != 0 ]; then
return 1
fi
local _extra_installer_file="$RETVAL"
local _extra_installer_cache="$RETVAL_CACHE"
assert_nz "$_extra_installer_file" "extra_installer_file"
assert_nz "$_extra_installer_cache" "extra_installer_cache"
say "installing extra component from $_extra_installer_file"
install_toolchain "$_extra_installer_file" "$_prefix" \
"$_disable_ldconfig" "$_disable_sudo" "$_extra_installer_cache" "$_save"
if [ $? != 0 ]; then
say_err "failed to install component"
return 1
fi
}
# Manifest v2 interface
# Returns 0 on success.
# Returns the manifest as a string in RETVAL
download_rust_manifest_v2() {
local _toolchain="$1"
verbose_say "dist_server: $dist_server"
case "$_toolchain" in