-
Notifications
You must be signed in to change notification settings - Fork 410
ensure sysroot is correct #1231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,11 @@ use std::process::Command; | |
use rustc_version::{Version, VersionMeta}; | ||
use serde::Deserialize; | ||
|
||
use crate::docker::ImagePlatform; | ||
use crate::errors::*; | ||
use crate::extensions::{env_program, CommandExt}; | ||
use crate::shell::MessageInfo; | ||
use crate::TargetTriple; | ||
use crate::{docker::ImagePlatform, rustup::ToolchainMode}; | ||
|
||
#[derive(Debug)] | ||
pub struct TargetList { | ||
|
@@ -196,19 +196,30 @@ impl QualifiedToolchain { | |
} | ||
|
||
/// Grab the current default toolchain | ||
pub fn default(config: &crate::config::Config, msg_info: &mut MessageInfo) -> Result<Self> { | ||
let sysroot = sysroot(msg_info)?; | ||
|
||
let default_toolchain_name = sysroot | ||
.file_name() | ||
.ok_or_else(|| eyre::eyre!("couldn't get name of active toolchain"))? | ||
.to_str() | ||
.ok_or_else(|| eyre::eyre!("toolchain was not utf-8"))?; | ||
pub fn default( | ||
config: &crate::config::Config, | ||
installed_toolchains: &[(String, ToolchainMode, PathBuf)], | ||
msg_info: &mut MessageInfo, | ||
) -> Result<Self> { | ||
let (toolchain_name, sysroot) = if let Some((toolchain_name, _, sysroot)) = | ||
installed_toolchains | ||
.iter() | ||
.find(|(_, mode, _)| mode.is_overriden()) | ||
{ | ||
(toolchain_name, sysroot) | ||
} else if let Some((toolchain_name, _, sysroot)) = installed_toolchains | ||
.iter() | ||
.find(|(_, mode, _)| mode.is_defaulted()) | ||
{ | ||
(toolchain_name, sysroot) | ||
} else { | ||
eyre::bail!("no default toolchain found"); | ||
}; | ||
|
||
if !config.custom_toolchain() { | ||
QualifiedToolchain::parse(sysroot.clone(), default_toolchain_name, config, msg_info) | ||
QualifiedToolchain::parse(sysroot.clone(), toolchain_name, config, msg_info) | ||
} else { | ||
QualifiedToolchain::custom(default_toolchain_name, &sysroot, config, msg_info) | ||
QualifiedToolchain::custom(toolchain_name, sysroot, config, msg_info) | ||
} | ||
} | ||
|
||
|
@@ -232,6 +243,26 @@ impl QualifiedToolchain { | |
pub fn set_sysroot(&mut self, convert: impl Fn(&Path) -> PathBuf) { | ||
self.sysroot = convert(&self.sysroot); | ||
} | ||
|
||
pub fn ensure_sysroot( | ||
&mut self, | ||
installed_toolchains: Vec<(String, ToolchainMode, std::path::PathBuf)>, | ||
msg_info: &mut MessageInfo, | ||
) -> Result<()> { | ||
self.sysroot = if let Some((_, _, sysroot)) = installed_toolchains | ||
.iter() | ||
.find(|(name, _, _)| &self.full == name) | ||
{ | ||
sysroot.clone() | ||
} else { | ||
let (_, _, sysroot) = crate::rustup::installed_toolchains(msg_info)? | ||
.into_iter() | ||
.find(|(name, _, _)| &self.full == name) | ||
.ok_or_else(|| eyre::eyre!("toolchain not found"))?; | ||
sysroot | ||
}; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for QualifiedToolchain { | ||
|
@@ -364,15 +395,6 @@ pub fn target_list(msg_info: &mut MessageInfo) -> Result<TargetList> { | |
}) | ||
} | ||
|
||
pub fn sysroot(msg_info: &mut MessageInfo) -> Result<PathBuf> { | ||
let stdout = rustc_command() | ||
.args(["--print", "sysroot"]) | ||
.run_and_get_stdout(msg_info)? | ||
.trim() | ||
.to_owned(); | ||
Ok(PathBuf::from(stdout)) | ||
} | ||
|
||
Comment on lines
-367
to
-375
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not needed anymore, |
||
pub fn version_meta() -> Result<rustc_version::VersionMeta> { | ||
rustc_version::version_meta().wrap_err("couldn't fetch the `rustc` version") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,12 +30,13 @@ impl AvailableTargets { | |
|
||
pub fn setup_rustup( | ||
toolchain: &QualifiedToolchain, | ||
installed_toolchains: &[(String, ToolchainMode, PathBuf)], | ||
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()) | ||
&& !installed_toolchains | ||
.iter() | ||
.any(|(t, _, _)| t == &toolchain.to_string()) | ||
{ | ||
install_toolchain(toolchain, msg_info)?; | ||
} | ||
|
@@ -83,18 +84,82 @@ pub fn active_toolchain(msg_info: &mut MessageInfo) -> Result<String> { | |
.to_owned()) | ||
} | ||
|
||
pub fn installed_toolchains(msg_info: &mut MessageInfo) -> Result<Vec<String>> { | ||
#[derive(Debug)] | ||
pub enum ToolchainMode { | ||
Override, | ||
Default, | ||
DefaultOverride, | ||
None, | ||
Other, | ||
} | ||
|
||
impl ToolchainMode { | ||
/// Returns `true` if the toolchain mode is [`Override`] or [`DefaultOverride`]. | ||
/// | ||
/// [`Override`]: ToolchainMode::Override | ||
/// [`DefaultOverride`]: ToolchainMode::DefaultOverride | ||
#[must_use] | ||
pub fn is_overriden(&self) -> bool { | ||
matches!(self, Self::Override | Self::DefaultOverride) | ||
} | ||
|
||
/// Returns `true` if the toolchain mode is [`None`]. | ||
/// | ||
/// [`None`]: ToolchainMode::None | ||
#[must_use] | ||
pub fn is_none(&self) -> bool { | ||
matches!(self, Self::None) | ||
} | ||
|
||
/// Returns `true` if the toolchain mode is [`Default`] or [`DefaultOverride`]. | ||
/// | ||
/// [`Default`]: ToolchainMode::Default | ||
/// [`DefaultOverride`]: ToolchainMode::DefaultOverride | ||
#[must_use] | ||
pub fn is_defaulted(&self) -> bool { | ||
matches!(self, Self::Default) | ||
} | ||
} | ||
|
||
pub fn installed_toolchains( | ||
msg_info: &mut MessageInfo, | ||
) -> Result<Vec<(String, ToolchainMode, std::path::PathBuf)>> { | ||
Comment on lines
+124
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably return a vec of |
||
let out = rustup_command(msg_info, true) | ||
.args(["toolchain", "list"]) | ||
.args(["toolchain", "list", "-v"]) | ||
.run_and_get_stdout(msg_info)?; | ||
|
||
Ok(out | ||
.lines() | ||
.map(|l| { | ||
l.replace(" (default)", "") | ||
.replace(" (override)", "") | ||
.trim() | ||
.to_owned() | ||
let mut mode = ToolchainMode::None; | ||
let mut l = if l.contains(" (override)") { | ||
mode = ToolchainMode::Override; | ||
l.replace(" (override)", "") | ||
} else { | ||
l.to_owned() | ||
}; | ||
if l.contains(" (default)") { | ||
if mode.is_overriden() { | ||
mode = ToolchainMode::DefaultOverride; | ||
} else { | ||
mode = ToolchainMode::Default; | ||
} | ||
l = l.replace(" (default)", ""); | ||
} | ||
|
||
(l, mode) | ||
}) | ||
Comment on lines
-94
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super happy about this. |
||
.map(|(l, mode)| { | ||
let mut i = l.split_whitespace(); | ||
( | ||
i.next() | ||
.map(|s| s.to_owned()) | ||
.expect("rustup output should be consistent"), | ||
mode, | ||
i.next() | ||
.map(PathBuf::from) | ||
.expect("rustup output should be consistent"), | ||
) | ||
}) | ||
.collect()) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like there is a better way to write this, not sure how though