project-model: when using rust-project.json
, prefer the sysroot-defined rustc over an env-based one
This commit is contained in:
parent
f29867bd26
commit
5b5bce8aaf
@ -4,10 +4,11 @@ use std::process::Command;
|
||||
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath};
|
||||
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot};
|
||||
|
||||
pub(crate) fn get(
|
||||
cargo_toml: Option<&ManifestPath>,
|
||||
sysroot: Option<&Sysroot>,
|
||||
target: Option<&str>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> Vec<CfgFlag> {
|
||||
@ -25,7 +26,7 @@ pub(crate) fn get(
|
||||
// Add miri cfg, which is useful for mir eval in stdlib
|
||||
res.push(CfgFlag::Atom("miri".into()));
|
||||
|
||||
match get_rust_cfgs(cargo_toml, target, extra_env) {
|
||||
match get_rust_cfgs(cargo_toml, sysroot, target, extra_env) {
|
||||
Ok(rustc_cfgs) => {
|
||||
tracing::debug!(
|
||||
"rustc cfgs found: {:?}",
|
||||
@ -44,6 +45,7 @@ pub(crate) fn get(
|
||||
|
||||
fn get_rust_cfgs(
|
||||
cargo_toml: Option<&ManifestPath>,
|
||||
sysroot: Option<&Sysroot>,
|
||||
target: Option<&str>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> anyhow::Result<String> {
|
||||
@ -62,8 +64,22 @@ fn get_rust_cfgs(
|
||||
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
|
||||
}
|
||||
}
|
||||
|
||||
let rustc = match sysroot {
|
||||
Some(sysroot) => {
|
||||
let rustc = sysroot.discover_rustc()?.into();
|
||||
tracing::debug!(?rustc, "using rustc from sysroot");
|
||||
rustc
|
||||
}
|
||||
None => {
|
||||
let rustc = toolchain::rustc();
|
||||
tracing::debug!(?rustc, "using rustc from env");
|
||||
rustc
|
||||
}
|
||||
};
|
||||
|
||||
// using unstable cargo features failed, fall back to using plain rustc
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
let mut cmd = Command::new(rustc);
|
||||
cmd.envs(extra_env);
|
||||
cmd.args(["--print", "cfg", "-O"]);
|
||||
if let Some(target) = target {
|
||||
|
@ -115,10 +115,19 @@ impl Sysroot {
|
||||
Ok(Sysroot::load(sysroot_dir, src))
|
||||
}
|
||||
|
||||
pub fn discover_rustc(&self) -> Option<ManifestPath> {
|
||||
pub fn discover_rustc_src(&self) -> Option<ManifestPath> {
|
||||
get_rustc_src(&self.root)
|
||||
}
|
||||
|
||||
pub fn discover_rustc(&self) -> Result<AbsPathBuf, std::io::Error> {
|
||||
let rustc = self.root.join("bin/rustc");
|
||||
tracing::debug!(?rustc, "checking for rustc binary at location");
|
||||
match fs::metadata(&rustc) {
|
||||
Ok(_) => Ok(rustc),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_sysroot_dir(sysroot_dir: AbsPathBuf) -> Result<Sysroot> {
|
||||
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir).ok_or_else(|| {
|
||||
format_err!("can't load standard library from sysroot path {sysroot_dir}")
|
||||
|
@ -240,9 +240,9 @@ impl ProjectWorkspace {
|
||||
Some(RustLibSource::Path(path)) => ManifestPath::try_from(path.clone())
|
||||
.map_err(|p| Some(format!("rustc source path is not absolute: {p}"))),
|
||||
Some(RustLibSource::Discover) => {
|
||||
sysroot.as_ref().ok().and_then(Sysroot::discover_rustc).ok_or_else(|| {
|
||||
Some(format!("Failed to discover rustc source for sysroot."))
|
||||
})
|
||||
sysroot.as_ref().ok().and_then(Sysroot::discover_rustc_src).ok_or_else(
|
||||
|| Some(format!("Failed to discover rustc source for sysroot.")),
|
||||
)
|
||||
}
|
||||
None => Err(None),
|
||||
};
|
||||
@ -279,8 +279,12 @@ impl ProjectWorkspace {
|
||||
}
|
||||
});
|
||||
|
||||
let rustc_cfg =
|
||||
rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
|
||||
let rustc_cfg = rustc_cfg::get(
|
||||
Some(&cargo_toml),
|
||||
None,
|
||||
config.target.as_deref(),
|
||||
&config.extra_env,
|
||||
);
|
||||
|
||||
let cfg_overrides = config.cfg_overrides.clone();
|
||||
let data_layout = target_data_layout::get(
|
||||
@ -335,7 +339,7 @@ impl ProjectWorkspace {
|
||||
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
|
||||
}
|
||||
|
||||
let rustc_cfg = rustc_cfg::get(None, target, extra_env);
|
||||
let rustc_cfg = rustc_cfg::get(None, sysroot.as_ref().ok(), target, extra_env);
|
||||
ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg, toolchain }
|
||||
}
|
||||
|
||||
@ -360,7 +364,7 @@ impl ProjectWorkspace {
|
||||
if let Ok(sysroot) = &sysroot {
|
||||
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
|
||||
}
|
||||
let rustc_cfg = rustc_cfg::get(None, None, &Default::default());
|
||||
let rustc_cfg = rustc_cfg::get(None, sysroot.as_ref().ok(), None, &Default::default());
|
||||
Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
|
||||
}
|
||||
|
||||
@ -756,7 +760,7 @@ fn project_json_to_crate_graph(
|
||||
let target_cfgs = match target.as_deref() {
|
||||
Some(target) => cfg_cache
|
||||
.entry(target)
|
||||
.or_insert_with(|| rustc_cfg::get(None, Some(target), extra_env)),
|
||||
.or_insert_with(|| rustc_cfg::get(None, sysroot, Some(target), extra_env)),
|
||||
None => &rustc_cfg,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user