Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d5f7e78

Browse files
committed
Cleanup toolchain info fetching
1 parent 47f497d commit d5f7e78

File tree

13 files changed

+197
-230
lines changed

13 files changed

+197
-230
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/layout/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use chalk_ir::{AdtId, TyKind};
22
use either::Either;
33
use hir_def::db::DefDatabase;
4-
use project_model::{target_data_layout::RustcDataLayoutConfig, Sysroot};
4+
use project_model::{toolchain_info::QueryConfig, Sysroot};
55
use rustc_hash::FxHashMap;
66
use syntax::ToSmolStr;
77
use test_fixture::WithFixture;
@@ -17,8 +17,8 @@ use crate::{
1717
mod closure;
1818

1919
fn current_machine_data_layout() -> String {
20-
project_model::target_data_layout::get(
21-
RustcDataLayoutConfig::Rustc(&Sysroot::empty()),
20+
project_model::toolchain_info::target_data_layout::get(
21+
QueryConfig::Rustc(&Sysroot::empty()),
2222
None,
2323
&FxHashMap::default(),
2424
)

src/tools/rust-analyzer/crates/project-model/src/build_dependencies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ impl WorkspaceBuildScripts {
178178
.current_dir(current_dir)
179179
.args(["rustc", "-Z", "unstable-options", "--print", "target-libdir"])
180180
.env("RUSTC_BOOTSTRAP", "1");
181-
if let Ok(it) = utf8_stdout(cargo_config) {
181+
if let Ok(it) = utf8_stdout(&mut cargo_config) {
182182
return Ok(it);
183183
}
184184
let mut cmd = sysroot.tool(Tool::Rustc);
185185
cmd.envs(extra_env);
186186
cmd.args(["--print", "target-libdir"]);
187-
utf8_stdout(cmd)
187+
utf8_stdout(&mut cmd)
188188
})()?;
189189

190190
let target_libdir = AbsPathBuf::try_from(Utf8PathBuf::from(target_libdir))

src/tools/rust-analyzer/crates/project-model/src/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub(crate) fn cargo_config_env(
8585
}
8686
// if successful we receive `env.key.value = "value" per entry
8787
tracing::debug!("Discovering cargo config env by {:?}", cargo_config);
88-
utf8_stdout(cargo_config)
88+
utf8_stdout(&mut cargo_config)
8989
.map(parse_output_cargo_config_env)
9090
.inspect(|env| {
9191
tracing::debug!("Discovered cargo config env: {:?}", env);

src/tools/rust-analyzer/crates/project-model/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
//! procedural macros).
1616
//! * Lowering of concrete model to a [`base_db::CrateGraph`]
1717
18+
pub mod project_json;
19+
pub mod toolchain_info;
20+
1821
mod build_dependencies;
1922
mod cargo_workspace;
2023
mod env;
2124
mod manifest_path;
22-
pub mod project_json;
23-
mod rustc_cfg;
2425
mod sysroot;
25-
pub mod target_data_layout;
26-
mod target_triple;
2726
mod workspace;
2827

2928
#[cfg(test)]
@@ -182,7 +181,7 @@ impl fmt::Display for ProjectManifest {
182181
}
183182
}
184183

185-
fn utf8_stdout(mut cmd: Command) -> anyhow::Result<String> {
184+
fn utf8_stdout(cmd: &mut Command) -> anyhow::Result<String> {
186185
let output = cmd.output().with_context(|| format!("{cmd:?} failed"))?;
187186
if !output.status.success() {
188187
match String::from_utf8(output.stderr) {

src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/tools/rust-analyzer/crates/project-model/src/sysroot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ fn discover_sysroot_dir(
440440
rustc.envs(extra_env);
441441
rustc.current_dir(current_dir).args(["--print", "sysroot"]);
442442
tracing::debug!("Discovering sysroot by {:?}", rustc);
443-
let stdout = utf8_stdout(rustc)?;
443+
let stdout = utf8_stdout(&mut rustc)?;
444444
Ok(AbsPathBuf::assert(Utf8PathBuf::from(stdout)))
445445
}
446446

@@ -472,7 +472,7 @@ fn discover_sysroot_src_dir_or_add_component(
472472
rustup.envs(extra_env);
473473
rustup.current_dir(current_dir).args(["component", "add", "rust-src"]);
474474
tracing::info!("adding rust-src component by {:?}", rustup);
475-
utf8_stdout(rustup).ok()?;
475+
utf8_stdout(&mut rustup).ok()?;
476476
get_rust_src(sysroot_path)
477477
})
478478
.ok_or_else(|| {

src/tools/rust-analyzer/crates/project-model/src/target_data_layout.rs

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pub mod rustc_cfg;
2+
pub mod target_data_layout;
3+
pub mod target_triple;
4+
5+
use crate::{ManifestPath, Sysroot};
6+
7+
pub enum QueryConfig<'a> {
8+
/// Directly invoke `rustc` to query the desired information.
9+
Rustc(&'a Sysroot),
10+
/// Attempt to use cargo to query the desired information, honoring cargo configurations.
11+
/// If this fails, falls back to invoking `rustc` directly.
12+
Cargo(&'a Sysroot, &'a ManifestPath),
13+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! Get the built-in cfg flags for the to be compile platform.
2+
3+
use anyhow::Context;
4+
use cfg::CfgAtom;
5+
use rustc_hash::FxHashMap;
6+
use toolchain::Tool;
7+
8+
use crate::{toolchain_info::QueryConfig, utf8_stdout};
9+
10+
/// Uses `rustc --print cfg` to fetch the builtin cfgs.
11+
pub fn get(
12+
config: QueryConfig<'_>,
13+
target: Option<&str>,
14+
extra_env: &FxHashMap<String, String>,
15+
) -> Vec<CfgAtom> {
16+
let _p = tracing::info_span!("rustc_cfg::get").entered();
17+
18+
let rustc_cfgs = rustc_print_cfg(target, extra_env, config);
19+
let rustc_cfgs = match rustc_cfgs {
20+
Ok(cfgs) => cfgs,
21+
Err(e) => {
22+
tracing::error!(?e, "failed to get rustc cfgs");
23+
return vec![];
24+
}
25+
};
26+
27+
let rustc_cfgs = rustc_cfgs.lines().map(crate::parse_cfg).collect::<Result<Vec<_>, _>>();
28+
match rustc_cfgs {
29+
Ok(rustc_cfgs) => {
30+
tracing::debug!(?rustc_cfgs, "rustc cfgs found");
31+
rustc_cfgs
32+
}
33+
Err(e) => {
34+
tracing::error!(?e, "failed to parse rustc cfgs");
35+
vec![]
36+
}
37+
}
38+
}
39+
40+
fn rustc_print_cfg(
41+
target: Option<&str>,
42+
extra_env: &FxHashMap<String, String>,
43+
config: QueryConfig<'_>,
44+
) -> anyhow::Result<String> {
45+
const RUSTC_ARGS: [&str; 3] = ["--print", "cfg", "-O"];
46+
let sysroot = match config {
47+
QueryConfig::Cargo(sysroot, cargo_toml) => {
48+
let mut cmd = sysroot.tool(Tool::Cargo);
49+
cmd.envs(extra_env);
50+
cmd.current_dir(cargo_toml.parent()).env("RUSTC_BOOTSTRAP", "1");
51+
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS);
52+
if let Some(target) = target {
53+
cmd.args(["--target", target]);
54+
}
55+
56+
match utf8_stdout(&mut cmd) {
57+
Ok(it) => return Ok(it),
58+
Err(e) => {
59+
tracing::warn!(
60+
%e,
61+
"failed to run `{cmd:?}`, falling back to invoking rustc directly"
62+
);
63+
sysroot
64+
}
65+
}
66+
}
67+
QueryConfig::Rustc(sysroot) => sysroot,
68+
};
69+
70+
let mut cmd = sysroot.tool(Tool::Rustc);
71+
cmd.envs(extra_env);
72+
cmd.args(RUSTC_ARGS);
73+
if let Some(target) = target {
74+
cmd.args(["--target", target]);
75+
}
76+
77+
utf8_stdout(&mut cmd).with_context(|| format!("unable to fetch cfgs via `{cmd:?}`"))
78+
}

0 commit comments

Comments
 (0)