Skip to content

Commit 0919c42

Browse files
committed
Use target rather than host toolchain.
Detect if using nightly by using the target rather than the host toolchain, by checking the channel in `rustup::rustc_version`.
1 parent fdcd3e9 commit 0919c42

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::path::PathBuf;
2121
use std::process::ExitStatus;
2222

2323
use config::Config;
24+
use rustc_version::Channel;
2425
use serde::Deserialize;
2526

2627
use self::cargo::{Root, Subcommand};
@@ -279,7 +280,6 @@ fn run() -> Result<ExitStatus> {
279280

280281
let host_version_meta =
281282
rustc_version::version_meta().wrap_err("couldn't fetch the `rustc` version")?;
282-
let is_nightly = rustc::is_nightly(&args.channel, &host_version_meta);
283283
if let Some(root) = cargo::root()? {
284284
let host = host_version_meta.host();
285285
let toml = toml(&root)?;
@@ -304,15 +304,17 @@ fn run() -> Result<ExitStatus> {
304304
.file_name()
305305
.and_then(|file_name| file_name.to_str())
306306
.ok_or_else(|| eyre::eyre!("couldn't get toolchain name"))?;
307-
let toolchain = if let Some(channel) = args.channel {
308-
[channel]
307+
let (toolchain, mut is_nightly) = if let Some(channel) = args.channel {
308+
let is_nightly = channel.contains("nightly");
309+
let toolchain = [channel]
309310
.iter()
310311
.map(|c| c.as_str())
311312
.chain(default_toolchain.splitn(2, '-').skip(1))
312313
.collect::<Vec<_>>()
313-
.join("-")
314+
.join("-");
315+
(toolchain, is_nightly)
314316
} else {
315-
default_toolchain.to_string()
317+
(default_toolchain.to_string(), false)
316318
};
317319
sysroot.set_file_name(&toolchain);
318320

@@ -322,13 +324,14 @@ fn run() -> Result<ExitStatus> {
322324
rustup::install_toolchain(&toolchain, verbose)?;
323325
}
324326
// TODO: Provide a way to pick/match the toolchain version as a consumer of `cross`.
325-
if let Some((rustc_version, rustc_commit)) = rustup::rustc_version(&sysroot)? {
327+
if let Some((rustc_version, channel, rustc_commit)) = rustup::rustc_version(&sysroot)? {
326328
warn_host_version_mismatch(
327329
&host_version_meta,
328330
&toolchain,
329331
&rustc_version,
330332
&rustc_commit,
331333
)?;
334+
is_nightly = channel == Channel::Nightly;
332335
}
333336

334337
let available_targets = rustup::available_targets(&toolchain, verbose)?;

src/rustc.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22
use std::process::Command;
33

4-
use rustc_version::{Channel, Version, VersionMeta};
4+
use rustc_version::{Version, VersionMeta};
55

66
use crate::errors::*;
77
use crate::extensions::CommandExt;
@@ -33,14 +33,6 @@ impl VersionMetaExt for VersionMeta {
3333
}
3434
}
3535

36-
pub fn is_nightly(channel: &Option<String>, host_version_meta: &VersionMeta) -> bool {
37-
if let Some(channel) = channel {
38-
channel.contains("nightly")
39-
} else {
40-
host_version_meta.channel == Channel::Nightly
41-
}
42-
}
43-
4436
pub fn target_list(verbose: bool) -> Result<TargetList> {
4537
Command::new("rustc")
4638
.args(&["--print", "target-list"])

src/rustup.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22
use std::process::Command;
33

4-
use rustc_version::Version;
4+
use rustc_version::{Channel, Version};
55

66
use crate::errors::*;
77
use crate::extensions::CommandExt;
@@ -101,7 +101,21 @@ pub fn component_is_installed(component: &str, toolchain: &str, verbose: bool) -
101101
.any(|l| l.starts_with(component) && l.contains("installed")))
102102
}
103103

104-
pub fn rustc_version(toolchain_path: &Path) -> Result<Option<(Version, String)>> {
104+
fn rustc_channel(version: &str) -> Channel {
105+
// the channel starts with `"{major}.{minor}.{patch}"`, and can
106+
// end also have -beta.{revision} or -nightly after version.
107+
if version.contains("dev") {
108+
Channel::Dev
109+
} else if version.contains("nightly") {
110+
Channel::Nightly
111+
} else if version.contains("beta") {
112+
Channel::Beta
113+
} else {
114+
Channel::Stable
115+
}
116+
}
117+
118+
pub fn rustc_version(toolchain_path: &Path) -> Result<Option<(Version, Channel, String)>> {
105119
let path = toolchain_path.join("lib/rustlib/multirust-channel-manifest.toml");
106120
if path.exists() {
107121
let contents = std::fs::read(&path)
@@ -119,6 +133,7 @@ pub fn rustc_version(toolchain_path: &Path) -> Result<Option<(Version, String)>>
119133
Version::parse(version).wrap_err_with(|| {
120134
format!("invalid rust version found in {}", path.display())
121135
})?,
136+
rustc_channel(version),
122137
meta.to_owned(),
123138
)));
124139
}

0 commit comments

Comments
 (0)