-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathrustup.rs
362 lines (329 loc) · 11.2 KB
/
rustup.rs
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
use std::path::PathBuf;
use std::process::Command;
use rustc_version::{Channel, Version};
use crate::errors::*;
pub use crate::extensions::{CommandExt, OutputExt};
use crate::rustc::QualifiedToolchain;
use crate::shell::{MessageInfo, Verbosity};
use crate::Target;
#[derive(Debug)]
pub struct AvailableTargets {
pub default: String,
pub installed: Vec<String>,
pub not_installed: Vec<String>,
}
impl AvailableTargets {
pub fn contains(&self, target: &Target) -> bool {
let triple = target.triple();
self.is_installed(target) || self.not_installed.iter().any(|x| x == triple)
}
pub fn is_installed(&self, target: &Target) -> bool {
let target = target.triple();
target == self.default || self.installed.iter().any(|x| x == target)
}
}
pub fn setup_rustup(
toolchain: &QualifiedToolchain,
msg_info: &mut MessageInfo,
) -> Result<AvailableTargets, color_eyre::Report> {
if !toolchain.is_custom
&& !installed_toolchains(msg_info)?
.into_iter()
.any(|t| t == toolchain.to_string())
{
install_toolchain(toolchain, msg_info)?;
}
let available_targets = if !toolchain.is_custom {
available_targets(&toolchain.full, msg_info).with_note(|| {
format!("cross would use the toolchain '{toolchain}' for mounting rust")
})?
} else {
AvailableTargets {
default: String::new(),
installed: vec![],
not_installed: vec![],
}
};
Ok(available_targets)
}
fn rustup_command(msg_info: &mut MessageInfo, no_flags: bool) -> Command {
let mut cmd = Command::new("rustup");
if no_flags {
return cmd;
}
match msg_info.verbosity {
Verbosity::Quiet => {
cmd.arg("--quiet");
}
Verbosity::Verbose(2..) => {
cmd.arg("--verbose");
}
_ => (),
}
cmd
}
pub fn active_toolchain(msg_info: &mut MessageInfo) -> Result<String> {
let out = rustup_command(msg_info, true)
.args(["show", "active-toolchain"])
.run_and_get_output(msg_info)?;
Ok(out
.stdout()?
.split_once(' ')
.ok_or_else(|| eyre::eyre!("rustup returned invalid data"))?
.0
.to_owned())
}
pub fn installed_toolchains(msg_info: &mut MessageInfo) -> Result<Vec<String>> {
let out = rustup_command(msg_info, true)
.args(["toolchain", "list"])
.run_and_get_stdout(msg_info)?;
Ok(out
.lines()
.map(|l| {
l.replace(" (default)", "")
.replace(" (override)", "")
.trim()
.to_owned()
})
.collect())
}
pub fn available_targets(
// this is explicitly a string and not `QualifiedToolchain`,
// this is because we use this as a way to ensure that
// the toolchain is an official toolchain, if this errors on
// `is a custom toolchain`, we tell the user to set CROSS_CUSTOM_TOOLCHAIN
// to handle the logic needed.
toolchain: &str,
msg_info: &mut MessageInfo,
) -> Result<AvailableTargets> {
let mut cmd = rustup_command(msg_info, true);
cmd.args(["target", "list", "--toolchain", toolchain]);
let output = cmd
.run_and_get_output(msg_info)
.suggestion("is rustup installed?")?;
if !output.status.success() {
let mut err = cmd
.status_result(msg_info, output.status, Some(&output))
.expect_err("we know the command failed")
.to_section_report();
if String::from_utf8_lossy(&output.stderr).contains("is a custom toolchain") {
err = err.wrap_err("'{toolchain}' is a custom toolchain.")
.suggestion(r#"To use this toolchain with cross, you'll need to set the environment variable `CROSS_CUSTOM_TOOLCHAIN=1`
cross will not attempt to configure the toolchain further so that it can run your binary."#);
} else if String::from_utf8_lossy(&output.stderr).contains("does not support components") {
err = err.suggestion(format!(
"try reinstalling the '{toolchain}' toolchain
$ rustup toolchain uninstall {toolchain}
$ rustup toolchain install {toolchain} --force-non-host"
));
}
return Err(err);
}
let out = output.stdout()?;
let mut default = String::new();
let mut installed = vec![];
let mut not_installed = vec![];
for line in out.lines() {
let target = line
.split(' ')
.next()
.expect("rustup output should be consistent")
.to_owned();
if line.contains("(default)") {
assert!(default.is_empty());
default = target;
} else if line.contains("(installed)") {
installed.push(target);
} else {
not_installed.push(target);
}
}
Ok(AvailableTargets {
default,
installed,
not_installed,
})
}
fn version(msg_info: &mut MessageInfo) -> Result<Version> {
let out = rustup_command(msg_info, false)
.arg("--version")
.run_and_get_stdout(msg_info)?;
match out
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
{
Some(version) => {
semver::Version::parse(version).wrap_err_with(|| "failed to parse rustup version")
}
None => eyre::bail!("failed to get rustup version"),
}
}
pub fn install_toolchain(toolchain: &QualifiedToolchain, msg_info: &mut MessageInfo) -> Result<()> {
let mut command = rustup_command(msg_info, false);
let toolchain = toolchain.to_string();
command.args(["toolchain", "add", &toolchain, "--profile", "minimal"]);
if version(msg_info)? >= semver::Version::new(1, 25, 0) {
command.arg("--force-non-host");
}
command
.run(msg_info, false)
.wrap_err_with(|| format!("couldn't install toolchain `{toolchain}`"))
}
pub fn install(
target: &Target,
toolchain: &QualifiedToolchain,
msg_info: &mut MessageInfo,
) -> Result<()> {
let target = target.triple();
let toolchain = toolchain.to_string();
rustup_command(msg_info, false)
.args(["target", "add", target, "--toolchain", &toolchain])
.run(msg_info, false)
.wrap_err_with(|| format!("couldn't install `std` for {target}"))
}
pub fn install_component(
component: &str,
toolchain: &QualifiedToolchain,
msg_info: &mut MessageInfo,
) -> Result<()> {
let toolchain = toolchain.to_string();
rustup_command(msg_info, false)
.args(["component", "add", component, "--toolchain", &toolchain])
.run(msg_info, false)
.wrap_err_with(|| format!("couldn't install the `{component}` component"))
}
#[derive(Debug)]
pub enum Component<'a> {
Installed(&'a str),
Available(&'a str),
NotAvailable(&'a str),
}
impl<'a> Component<'a> {
pub fn is_installed(&'a self) -> bool {
matches!(self, Component::Installed(_))
}
pub fn is_not_available(&'a self) -> bool {
matches!(self, Component::NotAvailable(_))
}
}
pub fn check_component<'a>(
component: &'a str,
toolchain: &QualifiedToolchain,
msg_info: &mut MessageInfo,
) -> Result<Component<'a>> {
Ok(Command::new("rustup")
.args(["component", "list", "--toolchain", &toolchain.to_string()])
.run_and_get_stdout(msg_info)?
.lines()
.find_map(|line| {
let available = line.starts_with(component);
let installed = line.contains("installed");
match available {
true => Some(installed),
false => None,
}
})
.map_or_else(
|| Component::NotAvailable(component),
|installed| match installed {
true => Component::Installed(component),
false => Component::Available(component),
},
))
}
pub fn component_is_installed(
component: &str,
toolchain: &QualifiedToolchain,
msg_info: &mut MessageInfo,
) -> Result<bool> {
Ok(check_component(component, toolchain, msg_info)?.is_installed())
}
#[allow(clippy::too_many_arguments)]
pub fn setup_components(
target: &Target,
uses_xargo: bool,
uses_build_std: bool,
toolchain: &QualifiedToolchain,
is_nightly: bool,
available_targets: AvailableTargets,
args: &crate::cli::Args,
msg_info: &mut MessageInfo,
) -> Result<(), color_eyre::Report> {
if !toolchain.is_custom {
// build-std overrides xargo, but only use it if it's a built-in
// tool but not an available target or doesn't have rust-std.
if !is_nightly && uses_build_std {
eyre::bail!(
"no rust-std component available for {}: must use nightly",
target.triple()
);
}
if !uses_xargo
&& !uses_build_std
&& !available_targets.is_installed(target)
&& available_targets.contains(target)
{
install(target, toolchain, msg_info)?;
} else if !component_is_installed("rust-src", toolchain, msg_info)? {
install_component("rust-src", toolchain, msg_info)?;
}
if args
.subcommand
.clone()
.map_or(false, |sc| sc == crate::Subcommand::Clippy)
&& !component_is_installed("clippy", toolchain, msg_info)?
{
install_component("clippy", toolchain, msg_info)?;
}
}
Ok(())
}
fn rustc_channel(version: &Version) -> Result<Channel> {
match version
.pre
.split('.')
.next()
.expect("rust prerelease version should contain `.`")
{
"" => Ok(Channel::Stable),
"dev" => Ok(Channel::Dev),
"beta" => Ok(Channel::Beta),
"nightly" => Ok(Channel::Nightly),
x => eyre::bail!("unknown prerelease tag {x}"),
}
}
impl QualifiedToolchain {
fn multirust_channel_manifest_path(&self) -> PathBuf {
self.get_sysroot()
.join("lib/rustlib/multirust-channel-manifest.toml")
}
pub fn rustc_version_string(&self) -> Result<Option<String>> {
let path = self.multirust_channel_manifest_path();
if path.exists() {
let contents =
std::fs::read(&path).wrap_err_with(|| format!("couldn't open file `{path:?}`"))?;
let manifest: toml::value::Table = toml::from_str(std::str::from_utf8(&contents)?)?;
return Ok(manifest
.get("pkg")
.and_then(|pkg| pkg.get("rust"))
.and_then(|rust| rust.get("version"))
.and_then(|version| version.as_str())
.map(|version| version.to_owned()));
}
Ok(None)
}
pub fn rustc_version(&self) -> Result<Option<(Version, Channel, String)>> {
let path = self.multirust_channel_manifest_path();
if let Some(rust_version) = self.rustc_version_string()? {
// Field is `"{version} ({commit} {date})"`
if let Some((version, meta)) = rust_version.split_once(' ') {
let version = Version::parse(version)
.wrap_err_with(|| format!("invalid rust version found in {path:?}"))?;
let channel = rustc_channel(&version)?;
return Ok(Some((version, channel, meta.to_owned())));
}
}
Ok(None)
}
}