mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Read default cfgs from rustc
This commit is contained in:
parent
e0100e63ae
commit
1067a1c5f6
8 changed files with 76 additions and 14 deletions
|
@ -39,6 +39,7 @@ struct PackageData {
|
|||
is_member: bool,
|
||||
dependencies: Vec<PackageDependency>,
|
||||
edition: Edition,
|
||||
features: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -91,6 +92,9 @@ impl Package {
|
|||
pub fn edition(self, ws: &CargoWorkspace) -> Edition {
|
||||
ws.packages[self].edition
|
||||
}
|
||||
pub fn features(self, ws: &CargoWorkspace) -> &[String] {
|
||||
&ws.packages[self].features
|
||||
}
|
||||
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
|
||||
ws.packages[self].targets.iter().cloned()
|
||||
}
|
||||
|
@ -144,6 +148,7 @@ impl CargoWorkspace {
|
|||
is_member,
|
||||
edition: Edition::from_string(&meta_pkg.edition),
|
||||
dependencies: Vec::new(),
|
||||
features: Vec::new(),
|
||||
});
|
||||
let pkg_data = &mut packages[pkg];
|
||||
pkg_by_id.insert(meta_pkg.id.clone(), pkg);
|
||||
|
@ -164,6 +169,7 @@ impl CargoWorkspace {
|
|||
let dep = PackageDependency { name: dep_node.name, pkg: pkg_by_id[&dep_node.pkg] };
|
||||
packages[source].dependencies.push(dep);
|
||||
}
|
||||
packages[source].features.extend(node.features);
|
||||
}
|
||||
|
||||
Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root })
|
||||
|
|
|
@ -19,6 +19,8 @@ pub struct Crate {
|
|||
pub(crate) root_module: PathBuf,
|
||||
pub(crate) edition: Edition,
|
||||
pub(crate) deps: Vec<Dep>,
|
||||
#[serde(default)]
|
||||
pub(crate) features: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||
|
|
|
@ -9,6 +9,7 @@ use std::{
|
|||
fs::File,
|
||||
io::BufReader,
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use ra_cfg::CfgOptions;
|
||||
|
@ -118,6 +119,7 @@ impl ProjectWorkspace {
|
|||
|
||||
pub fn to_crate_graph(
|
||||
&self,
|
||||
default_cfg_options: &CfgOptions,
|
||||
load: &mut dyn FnMut(&Path) -> Option<FileId>,
|
||||
) -> (CrateGraph, FxHashMap<CrateId, String>) {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
|
@ -134,7 +136,9 @@ impl ProjectWorkspace {
|
|||
};
|
||||
// FIXME: cfg options
|
||||
// Default to enable test for workspace crates.
|
||||
let cfg_options = CfgOptions::default().atom("test".into());
|
||||
let cfg_options = default_cfg_options
|
||||
.clone()
|
||||
.features(krate.features.iter().map(Into::into));
|
||||
crates.insert(
|
||||
crate_id,
|
||||
crate_graph.add_crate_root(file_id, edition, cfg_options),
|
||||
|
@ -164,9 +168,8 @@ impl ProjectWorkspace {
|
|||
let mut sysroot_crates = FxHashMap::default();
|
||||
for krate in sysroot.crates() {
|
||||
if let Some(file_id) = load(krate.root(&sysroot)) {
|
||||
// FIXME: cfg options
|
||||
// Crates from sysroot have `cfg(test)` disabled
|
||||
let cfg_options = CfgOptions::default();
|
||||
let cfg_options = default_cfg_options.clone().remove_atom(&"test".into());
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
|
||||
sysroot_crates.insert(krate, crate_id);
|
||||
|
@ -197,9 +200,9 @@ impl ProjectWorkspace {
|
|||
let root = tgt.root(&cargo);
|
||||
if let Some(file_id) = load(root) {
|
||||
let edition = pkg.edition(&cargo);
|
||||
// FIXME: cfg options
|
||||
// Default to enable test for workspace crates.
|
||||
let cfg_options = CfgOptions::default().atom("test".into());
|
||||
let cfg_options = default_cfg_options
|
||||
.clone()
|
||||
.features(pkg.features(&cargo).iter().map(Into::into));
|
||||
let crate_id =
|
||||
crate_graph.add_crate_root(file_id, edition, cfg_options);
|
||||
names.insert(crate_id, pkg.name(&cargo).to_string());
|
||||
|
@ -301,3 +304,32 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
|
|||
}
|
||||
Err(format!("can't find Cargo.toml at {}", path.display()))?
|
||||
}
|
||||
|
||||
pub fn get_rustc_cfg_options() -> CfgOptions {
|
||||
let mut cfg_options = CfgOptions::default();
|
||||
|
||||
match (|| -> Result<_> {
|
||||
// `cfg(test)` ans `cfg(debug_assertion)` is handled outside, so we suppress them here.
|
||||
let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?;
|
||||
if !output.status.success() {
|
||||
Err("failed to get rustc cfgs")?;
|
||||
}
|
||||
Ok(String::from_utf8(output.stdout)?)
|
||||
})() {
|
||||
Ok(rustc_cfgs) => {
|
||||
for line in rustc_cfgs.lines() {
|
||||
match line.find('=') {
|
||||
None => cfg_options = cfg_options.atom(line.into()),
|
||||
Some(pos) => {
|
||||
let key = &line[..pos];
|
||||
let value = line[pos + 1..].trim_matches('"');
|
||||
cfg_options = cfg_options.key_value(key.into(), value.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => log::error!("failed to get rustc cfgs: {}", e),
|
||||
}
|
||||
|
||||
cfg_options
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue