-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust.sh
executable file
·151 lines (116 loc) · 2.3 KB
/
rust.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
#!/usr/bin/env zsh
set -euo pipefail
declare toolchains=( stable nightly )
declare components=( rust-src )
declare tools=(
${=tools:+cargo-expand cargo-nextest}
${=linters:+cargo-audit cargo-deny cargo-spellcheck}
${sccache:+sccache}
)
declare toolchain=${toolchain:-${toolchains[1]:-stable}}
declare opts=( ${${:---component}:^^components} )
function install {
declare opts=( ${@:-$opts} )
if ! whence -p cargo &>/dev/null
then
install-rust $opts
fi
if whence -p rustup &>/dev/null
then
install-toolchains $opts
fi
install-tools
setup-rust
}
function install-rust {
declare opts=( ${@:-$opts} )
if whence -p rustup &>/dev/null
then
rustup toolchain install $toolchain $opts
else
install-rustup $opts
fi
}
function install-rustup {
declare opts=( -y --no-modify-path --default-toolchain $toolchain ${@:-$opts} )
if whence -p rustup-init &>/dev/null
then
rustup-init $opts
else
curl -sSf https://sh.rustup.rs | sh -s -- $opts
fi
}
function install-toolchains {
declare opts=( ${@:-$opts} )
for toolchain in $toolchains
do
rustup toolchain install $toolchain $opts
done
}
function install-tools {
declare tools=( ${@:-$tools} )
for tool in $tools
do
if ! whence -p $tool &>/dev/null
then
cargo install $tool
fi
done
}
function setup-rust {
setup-sccache
setup-ld
}
function setup-sccache {
if whence -p sccache &>/dev/null
then
cargo-config <<-EOF
[build]
rustc-wrapper = "sccache"
EOF
fi
}
function setup-ld {
setup-mold || setup-lld || :
}
function setup-mold {
try-setup-ld mold
}
function setup-lld {
try-setup-ld lld
}
function try-setup-ld {
declare target="$(rustup show | grep 'Default host' | sed -E 's|Default host: ||')"
declare ld=$1
if [[ $target == '' ]]
then
return 0
fi
if ! whence -p $ld &>/dev/null
then
return 1
fi
cargo-config <<-EOF
[target.$target]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=$ld"]
EOF
}
function cargo-config {
declare cargo=~/.cargo/config.toml
declare config="$(cat)"
declare header="$(head -n 1 <<< $config)"
if ! grep -F $header $cargo &>/dev/null
then
printf '%s\n\n' $config >> $cargo
fi
}
function uninstall {
rm -rf ~/.cargo ~/.rustup ~/.cache/sccache ~/Library/Caches/Mozilla.sccache
}
if [[ $ZSH_EVAL_CONTEXT == toplevel ]] && (( $# == 0 ))
then
install
else
$@
fi