From 1067a1c5f649cc206e35b427eaa8d6553280cc96 Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Thu, 3 Oct 2019 02:02:53 +0800 Subject: Read default cfgs from rustc --- Cargo.lock | 1 + crates/ra_batch/src/lib.rs | 18 +++++++---- crates/ra_cfg/src/lib.rs | 10 ++++++ crates/ra_lsp_server/Cargo.toml | 1 + crates/ra_lsp_server/src/world.rs | 8 +++-- crates/ra_project_model/src/cargo_workspace.rs | 6 ++++ crates/ra_project_model/src/json_project.rs | 2 ++ crates/ra_project_model/src/lib.rs | 44 ++++++++++++++++++++++---- 8 files changed, 76 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b95f176fc..736f1994e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1032,6 +1032,7 @@ dependencies = [ "lsp-server 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lsp-types 0.61.0 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ra_cfg 0.1.0", "ra_db 0.1.0", "ra_ide_api 0.1.0", "ra_prof 0.1.0", diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index 939f72037..a5fc2a23e 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs @@ -7,7 +7,7 @@ use rustc_hash::FxHashMap; use crossbeam_channel::{unbounded, Receiver}; use ra_db::{CrateGraph, FileId, SourceRootId}; use ra_ide_api::{AnalysisChange, AnalysisHost, FeatureFlags}; -use ra_project_model::{PackageRoot, ProjectWorkspace}; +use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace}; use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; use ra_vfs_glob::RustPackageFilterBuilder; @@ -41,11 +41,17 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap {:?}", path, vfs_file); - vfs_file.map(vfs_file_to_id) - }); + + // FIXME: cfg options? + let default_cfg_options = + get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); + + let (crate_graph, _crate_names) = + ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { + let vfs_file = vfs.load(path); + log::debug!("vfs file {:?} -> {:?}", path, vfs_file); + vfs_file.map(vfs_file_to_id) + }); log::debug!("crate graph: {:?}", crate_graph); let source_roots = roots diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs index dd81a73f4..e1c92fbba 100644 --- a/crates/ra_cfg/src/lib.rs +++ b/crates/ra_cfg/src/lib.rs @@ -1,4 +1,6 @@ //! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator +use std::iter::IntoIterator; + use ra_syntax::SmolStr; use rustc_hash::FxHashSet; @@ -44,6 +46,14 @@ impl CfgOptions { self } + /// Shortcut to set features + pub fn features(mut self, iter: impl IntoIterator) -> CfgOptions { + for feat in iter { + self = self.key_value("feature".into(), feat); + } + self + } + pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { self.atoms.remove(name); self diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 677d81835..aedc55a95 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -19,6 +19,7 @@ jod-thread = "0.1.0" ra_vfs = "0.4.0" ra_syntax = { path = "../ra_syntax" } ra_db = { path = "../ra_db" } +ra_cfg = { path = "../ra_cfg" } ra_text_edit = { path = "../ra_text_edit" } ra_ide_api = { path = "../ra_ide_api" } lsp-server = "0.2.0" diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index b55046ec9..27da751ab 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -13,7 +13,7 @@ use ra_ide_api::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, FeatureFlags, FileId, LibraryData, SourceRootId, }; -use ra_project_model::ProjectWorkspace; +use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace}; use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; use ra_vfs_glob::{Glob, RustPackageFilterBuilder}; use relative_path::RelativePathBuf; @@ -97,6 +97,10 @@ impl WorldState { change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string()); } + // FIXME: Read default cfgs from config + let default_cfg_options = + get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); + // Create crate graph from all the workspaces let mut crate_graph = CrateGraph::default(); let mut load = |path: &std::path::Path| { @@ -104,7 +108,7 @@ impl WorldState { vfs_file.map(|f| FileId(f.0)) }; for ws in workspaces.iter() { - let (graph, crate_names) = ws.to_crate_graph(&mut load); + let (graph, crate_names) = ws.to_crate_graph(&default_cfg_options, &mut load); let shift = crate_graph.extend(graph); for (crate_id, name) in crate_names { change.set_debug_crate_name(crate_id.shift(shift), name) diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 805eaa178..28dadea9d 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -39,6 +39,7 @@ struct PackageData { is_member: bool, dependencies: Vec, edition: Edition, + features: Vec, } #[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 + '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 }) diff --git a/crates/ra_project_model/src/json_project.rs b/crates/ra_project_model/src/json_project.rs index 54ddca2cb..b0d339f38 100644 --- a/crates/ra_project_model/src/json_project.rs +++ b/crates/ra_project_model/src/json_project.rs @@ -19,6 +19,8 @@ pub struct Crate { pub(crate) root_module: PathBuf, pub(crate) edition: Edition, pub(crate) deps: Vec, + #[serde(default)] + pub(crate) features: Vec, } #[derive(Clone, Copy, Debug, Deserialize)] diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 5ff3971e0..05e49f5ce 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -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, ) -> (CrateGraph, FxHashMap) { 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 { } 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 +} -- cgit v1.2.3