-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
197 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 0 additions & 99 deletions
99
src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
src/tools/rust-analyzer/crates/project-model/src/target_data_layout.rs
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/tools/rust-analyzer/crates/project-model/src/toolchain_info.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub mod rustc_cfg; | ||
pub mod target_data_layout; | ||
pub mod target_triple; | ||
|
||
use crate::{ManifestPath, Sysroot}; | ||
|
||
pub enum QueryConfig<'a> { | ||
/// Directly invoke `rustc` to query the desired information. | ||
Rustc(&'a Sysroot), | ||
/// Attempt to use cargo to query the desired information, honoring cargo configurations. | ||
/// If this fails, falls back to invoking `rustc` directly. | ||
Cargo(&'a Sysroot, &'a ManifestPath), | ||
} |
78 changes: 78 additions & 0 deletions
78
src/tools/rust-analyzer/crates/project-model/src/toolchain_info/rustc_cfg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! Get the built-in cfg flags for the to be compile platform. | ||
use anyhow::Context; | ||
use cfg::CfgAtom; | ||
use rustc_hash::FxHashMap; | ||
use toolchain::Tool; | ||
|
||
use crate::{toolchain_info::QueryConfig, utf8_stdout}; | ||
|
||
/// Uses `rustc --print cfg` to fetch the builtin cfgs. | ||
pub fn get( | ||
config: QueryConfig<'_>, | ||
target: Option<&str>, | ||
extra_env: &FxHashMap<String, String>, | ||
) -> Vec<CfgAtom> { | ||
let _p = tracing::info_span!("rustc_cfg::get").entered(); | ||
|
||
let rustc_cfgs = rustc_print_cfg(target, extra_env, config); | ||
let rustc_cfgs = match rustc_cfgs { | ||
Ok(cfgs) => cfgs, | ||
Err(e) => { | ||
tracing::error!(?e, "failed to get rustc cfgs"); | ||
return vec![]; | ||
} | ||
}; | ||
|
||
let rustc_cfgs = rustc_cfgs.lines().map(crate::parse_cfg).collect::<Result<Vec<_>, _>>(); | ||
match rustc_cfgs { | ||
Ok(rustc_cfgs) => { | ||
tracing::debug!(?rustc_cfgs, "rustc cfgs found"); | ||
rustc_cfgs | ||
} | ||
Err(e) => { | ||
tracing::error!(?e, "failed to parse rustc cfgs"); | ||
vec![] | ||
} | ||
} | ||
} | ||
|
||
fn rustc_print_cfg( | ||
target: Option<&str>, | ||
extra_env: &FxHashMap<String, String>, | ||
config: QueryConfig<'_>, | ||
) -> anyhow::Result<String> { | ||
const RUSTC_ARGS: [&str; 3] = ["--print", "cfg", "-O"]; | ||
let sysroot = match config { | ||
QueryConfig::Cargo(sysroot, cargo_toml) => { | ||
let mut cmd = sysroot.tool(Tool::Cargo); | ||
cmd.envs(extra_env); | ||
cmd.current_dir(cargo_toml.parent()).env("RUSTC_BOOTSTRAP", "1"); | ||
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS); | ||
if let Some(target) = target { | ||
cmd.args(["--target", target]); | ||
} | ||
|
||
match utf8_stdout(&mut cmd) { | ||
Ok(it) => return Ok(it), | ||
Err(e) => { | ||
tracing::warn!( | ||
%e, | ||
"failed to run `{cmd:?}`, falling back to invoking rustc directly" | ||
); | ||
sysroot | ||
} | ||
} | ||
} | ||
QueryConfig::Rustc(sysroot) => sysroot, | ||
}; | ||
|
||
let mut cmd = sysroot.tool(Tool::Rustc); | ||
cmd.envs(extra_env); | ||
cmd.args(RUSTC_ARGS); | ||
if let Some(target) = target { | ||
cmd.args(["--target", target]); | ||
} | ||
|
||
utf8_stdout(&mut cmd).with_context(|| format!("unable to fetch cfgs via `{cmd:?}`")) | ||
} |
Oops, something went wrong.