Skip to content

Commit 553152e

Browse files
committed
refactor rustc_cfg to be more inline with docs/dev/style.md
1 parent 5b5bce8 commit 553152e

File tree

2 files changed

+81
-52
lines changed

2 files changed

+81
-52
lines changed

crates/project-model/src/rustc_cfg.rs

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
33
use std::process::Command;
44

5+
use anyhow::Context;
56
use rustc_hash::FxHashMap;
67

78
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot};
89

10+
pub(crate) enum Config<'a> {
11+
Cargo(&'a ManifestPath),
12+
Explicit(&'a Sysroot),
13+
Discover,
14+
}
15+
916
pub(crate) fn get(
10-
cargo_toml: Option<&ManifestPath>,
11-
sysroot: Option<&Sysroot>,
1217
target: Option<&str>,
1318
extra_env: &FxHashMap<String, String>,
19+
config: Config<'_>,
1420
) -> Vec<CfgFlag> {
1521
let _p = profile::span("rustc_cfg::get");
1622
let mut res = Vec::with_capacity(6 * 2 + 1);
@@ -26,64 +32,68 @@ pub(crate) fn get(
2632
// Add miri cfg, which is useful for mir eval in stdlib
2733
res.push(CfgFlag::Atom("miri".into()));
2834

29-
match get_rust_cfgs(cargo_toml, sysroot, target, extra_env) {
35+
let rustc_cfgs = get_rust_cfgs(target, extra_env, config);
36+
37+
let rustc_cfgs = match rustc_cfgs {
38+
Ok(cfgs) => cfgs,
39+
Err(e) => {
40+
tracing::error!(?e, "failed to get rustc cfgs");
41+
return res;
42+
}
43+
};
44+
45+
let rustc_cfgs =
46+
rustc_cfgs.lines().map(|it| it.parse::<CfgFlag>()).collect::<Result<Vec<_>, _>>();
47+
48+
match rustc_cfgs {
3049
Ok(rustc_cfgs) => {
31-
tracing::debug!(
32-
"rustc cfgs found: {:?}",
33-
rustc_cfgs
34-
.lines()
35-
.map(|it| it.parse::<CfgFlag>().map(|it| it.to_string()))
36-
.collect::<Vec<_>>()
37-
);
38-
res.extend(rustc_cfgs.lines().filter_map(|it| it.parse().ok()));
50+
tracing::debug!(?rustc_cfgs, "rustc cfgs found");
51+
res.extend(rustc_cfgs);
52+
}
53+
Err(e) => {
54+
tracing::error!(?e, "failed to get rustc cfgs")
3955
}
40-
Err(e) => tracing::error!("failed to get rustc cfgs: {e:?}"),
4156
}
4257

4358
res
4459
}
4560

4661
fn get_rust_cfgs(
47-
cargo_toml: Option<&ManifestPath>,
48-
sysroot: Option<&Sysroot>,
4962
target: Option<&str>,
5063
extra_env: &FxHashMap<String, String>,
64+
config: Config<'_>,
5165
) -> anyhow::Result<String> {
52-
if let Some(cargo_toml) = cargo_toml {
53-
let mut cargo_config = Command::new(toolchain::cargo());
54-
cargo_config.envs(extra_env);
55-
cargo_config
56-
.current_dir(cargo_toml.parent())
57-
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
58-
.env("RUSTC_BOOTSTRAP", "1");
59-
if let Some(target) = target {
60-
cargo_config.args(["--target", target]);
61-
}
62-
match utf8_stdout(cargo_config) {
63-
Ok(it) => return Ok(it),
64-
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
65-
}
66-
}
66+
let mut cmd = match config {
67+
Config::Cargo(cargo_toml) => {
68+
let mut cmd = Command::new(toolchain::cargo());
69+
cmd.envs(extra_env);
70+
cmd.current_dir(cargo_toml.parent())
71+
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
72+
.env("RUSTC_BOOTSTRAP", "1");
73+
if let Some(target) = target {
74+
cmd.args(["--target", target]);
75+
}
6776

68-
let rustc = match sysroot {
69-
Some(sysroot) => {
70-
let rustc = sysroot.discover_rustc()?.into();
71-
tracing::debug!(?rustc, "using rustc from sysroot");
72-
rustc
77+
return utf8_stdout(cmd).context("Unable to run `cargo rustc`");
78+
}
79+
Config::Explicit(sysroot) => {
80+
let rustc: std::path::PathBuf = sysroot.discover_rustc()?.into();
81+
tracing::debug!(?rustc, "using explicit rustc from sysroot");
82+
Command::new(rustc)
7383
}
74-
None => {
84+
Config::Discover => {
7585
let rustc = toolchain::rustc();
7686
tracing::debug!(?rustc, "using rustc from env");
77-
rustc
87+
Command::new(rustc)
7888
}
7989
};
8090

81-
// using unstable cargo features failed, fall back to using plain rustc
82-
let mut cmd = Command::new(rustc);
8391
cmd.envs(extra_env);
8492
cmd.args(["--print", "cfg", "-O"]);
8593
if let Some(target) = target {
8694
cmd.args(["--target", target]);
8795
}
88-
utf8_stdout(cmd)
96+
97+
let out = utf8_stdout(cmd).context("Unable to run `rustc`")?;
98+
Ok(out)
8999
}

crates/project-model/src/workspace.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,9 @@ impl ProjectWorkspace {
280280
});
281281

282282
let rustc_cfg = rustc_cfg::get(
283-
Some(&cargo_toml),
284-
None,
285283
config.target.as_deref(),
286284
&config.extra_env,
285+
rustc_cfg::Config::Cargo(cargo_toml),
287286
);
288287

289288
let cfg_overrides = config.cfg_overrides.clone();
@@ -335,11 +334,18 @@ impl ProjectWorkspace {
335334
}
336335
(None, None) => Err(None),
337336
};
338-
if let Ok(sysroot) = &sysroot {
339-
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
340-
}
337+
let config = match &sysroot {
338+
Ok(sysroot) => {
339+
tracing::debug!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
340+
rustc_cfg::Config::Explicit(sysroot)
341+
}
342+
Err(_) => {
343+
tracing::debug!("discovering sysroot");
344+
rustc_cfg::Config::Discover
345+
}
346+
};
341347

342-
let rustc_cfg = rustc_cfg::get(None, sysroot.as_ref().ok(), target, extra_env);
348+
let rustc_cfg = rustc_cfg::get(target, extra_env, config);
343349
ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg, toolchain }
344350
}
345351

@@ -361,10 +367,18 @@ impl ProjectWorkspace {
361367
}
362368
None => Err(None),
363369
};
364-
if let Ok(sysroot) = &sysroot {
365-
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
366-
}
367-
let rustc_cfg = rustc_cfg::get(None, sysroot.as_ref().ok(), None, &Default::default());
370+
let rustc_config = match &sysroot {
371+
Ok(sysroot) => {
372+
tracing::info!(src_root = %sysroot.src_root(), root = %sysroot.root(), "Using sysroot");
373+
rustc_cfg::Config::Explicit(sysroot)
374+
}
375+
Err(_) => {
376+
tracing::info!("discovering sysroot");
377+
rustc_cfg::Config::Discover
378+
}
379+
};
380+
381+
let rustc_cfg = rustc_cfg::get(None, &FxHashMap::default(), rustc_config);
368382
Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
369383
}
370384

@@ -758,9 +772,14 @@ fn project_json_to_crate_graph(
758772
let env = env.clone().into_iter().collect();
759773

760774
let target_cfgs = match target.as_deref() {
761-
Some(target) => cfg_cache
762-
.entry(target)
763-
.or_insert_with(|| rustc_cfg::get(None, sysroot, Some(target), extra_env)),
775+
Some(target) => cfg_cache.entry(target).or_insert_with(|| {
776+
let rustc_cfg = match sysroot {
777+
Some(sysroot) => rustc_cfg::Config::Explicit(sysroot),
778+
None => rustc_cfg::Config::Discover,
779+
};
780+
781+
rustc_cfg::get(Some(target), extra_env, rustc_cfg)
782+
}),
764783
None => &rustc_cfg,
765784
};
766785

0 commit comments

Comments
 (0)