aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_batch/src/lib.rs18
-rw-r--r--crates/ra_cfg/src/lib.rs10
-rw-r--r--crates/ra_lsp_server/Cargo.toml1
-rw-r--r--crates/ra_lsp_server/src/world.rs8
-rw-r--r--crates/ra_project_model/src/cargo_workspace.rs6
-rw-r--r--crates/ra_project_model/src/json_project.rs2
-rw-r--r--crates/ra_project_model/src/lib.rs44
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 = [
1032 "lsp-server 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 1032 "lsp-server 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
1033 "lsp-types 0.61.0 (registry+https://github.com/rust-lang/crates.io-index)", 1033 "lsp-types 0.61.0 (registry+https://github.com/rust-lang/crates.io-index)",
1034 "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", 1034 "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
1035 "ra_cfg 0.1.0",
1035 "ra_db 0.1.0", 1036 "ra_db 0.1.0",
1036 "ra_ide_api 0.1.0", 1037 "ra_ide_api 0.1.0",
1037 "ra_prof 0.1.0", 1038 "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;
7use crossbeam_channel::{unbounded, Receiver}; 7use crossbeam_channel::{unbounded, Receiver};
8use ra_db::{CrateGraph, FileId, SourceRootId}; 8use ra_db::{CrateGraph, FileId, SourceRootId};
9use ra_ide_api::{AnalysisChange, AnalysisHost, FeatureFlags}; 9use ra_ide_api::{AnalysisChange, AnalysisHost, FeatureFlags};
10use ra_project_model::{PackageRoot, ProjectWorkspace}; 10use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace};
11use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; 11use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
12use ra_vfs_glob::RustPackageFilterBuilder; 12use ra_vfs_glob::RustPackageFilterBuilder;
13 13
@@ -41,11 +41,17 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId,
41 sender, 41 sender,
42 Watch(false), 42 Watch(false),
43 ); 43 );
44 let (crate_graph, _crate_names) = ws.to_crate_graph(&mut |path: &Path| { 44
45 let vfs_file = vfs.load(path); 45 // FIXME: cfg options?
46 log::debug!("vfs file {:?} -> {:?}", path, vfs_file); 46 let default_cfg_options =
47 vfs_file.map(vfs_file_to_id) 47 get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into());
48 }); 48
49 let (crate_graph, _crate_names) =
50 ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| {
51 let vfs_file = vfs.load(path);
52 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
53 vfs_file.map(vfs_file_to_id)
54 });
49 log::debug!("crate graph: {:?}", crate_graph); 55 log::debug!("crate graph: {:?}", crate_graph);
50 56
51 let source_roots = roots 57 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 @@
1//! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator 1//! ra_cfg defines conditional compiling options, `cfg` attibute parser and evaluator
2use std::iter::IntoIterator;
3
2use ra_syntax::SmolStr; 4use ra_syntax::SmolStr;
3use rustc_hash::FxHashSet; 5use rustc_hash::FxHashSet;
4 6
@@ -44,6 +46,14 @@ impl CfgOptions {
44 self 46 self
45 } 47 }
46 48
49 /// Shortcut to set features
50 pub fn features(mut self, iter: impl IntoIterator<Item = SmolStr>) -> CfgOptions {
51 for feat in iter {
52 self = self.key_value("feature".into(), feat);
53 }
54 self
55 }
56
47 pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { 57 pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions {
48 self.atoms.remove(name); 58 self.atoms.remove(name);
49 self 59 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"
19ra_vfs = "0.4.0" 19ra_vfs = "0.4.0"
20ra_syntax = { path = "../ra_syntax" } 20ra_syntax = { path = "../ra_syntax" }
21ra_db = { path = "../ra_db" } 21ra_db = { path = "../ra_db" }
22ra_cfg = { path = "../ra_cfg" }
22ra_text_edit = { path = "../ra_text_edit" } 23ra_text_edit = { path = "../ra_text_edit" }
23ra_ide_api = { path = "../ra_ide_api" } 24ra_ide_api = { path = "../ra_ide_api" }
24lsp-server = "0.2.0" 25lsp-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::{
13 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FeatureFlags, FileId, LibraryData, 13 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FeatureFlags, FileId, LibraryData,
14 SourceRootId, 14 SourceRootId,
15}; 15};
16use ra_project_model::ProjectWorkspace; 16use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace};
17use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; 17use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
18use ra_vfs_glob::{Glob, RustPackageFilterBuilder}; 18use ra_vfs_glob::{Glob, RustPackageFilterBuilder};
19use relative_path::RelativePathBuf; 19use relative_path::RelativePathBuf;
@@ -97,6 +97,10 @@ impl WorldState {
97 change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string()); 97 change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string());
98 } 98 }
99 99
100 // FIXME: Read default cfgs from config
101 let default_cfg_options =
102 get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into());
103
100 // Create crate graph from all the workspaces 104 // Create crate graph from all the workspaces
101 let mut crate_graph = CrateGraph::default(); 105 let mut crate_graph = CrateGraph::default();
102 let mut load = |path: &std::path::Path| { 106 let mut load = |path: &std::path::Path| {
@@ -104,7 +108,7 @@ impl WorldState {
104 vfs_file.map(|f| FileId(f.0)) 108 vfs_file.map(|f| FileId(f.0))
105 }; 109 };
106 for ws in workspaces.iter() { 110 for ws in workspaces.iter() {
107 let (graph, crate_names) = ws.to_crate_graph(&mut load); 111 let (graph, crate_names) = ws.to_crate_graph(&default_cfg_options, &mut load);
108 let shift = crate_graph.extend(graph); 112 let shift = crate_graph.extend(graph);
109 for (crate_id, name) in crate_names { 113 for (crate_id, name) in crate_names {
110 change.set_debug_crate_name(crate_id.shift(shift), name) 114 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 {
39 is_member: bool, 39 is_member: bool,
40 dependencies: Vec<PackageDependency>, 40 dependencies: Vec<PackageDependency>,
41 edition: Edition, 41 edition: Edition,
42 features: Vec<String>,
42} 43}
43 44
44#[derive(Debug, Clone)] 45#[derive(Debug, Clone)]
@@ -91,6 +92,9 @@ impl Package {
91 pub fn edition(self, ws: &CargoWorkspace) -> Edition { 92 pub fn edition(self, ws: &CargoWorkspace) -> Edition {
92 ws.packages[self].edition 93 ws.packages[self].edition
93 } 94 }
95 pub fn features(self, ws: &CargoWorkspace) -> &[String] {
96 &ws.packages[self].features
97 }
94 pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a { 98 pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
95 ws.packages[self].targets.iter().cloned() 99 ws.packages[self].targets.iter().cloned()
96 } 100 }
@@ -144,6 +148,7 @@ impl CargoWorkspace {
144 is_member, 148 is_member,
145 edition: Edition::from_string(&meta_pkg.edition), 149 edition: Edition::from_string(&meta_pkg.edition),
146 dependencies: Vec::new(), 150 dependencies: Vec::new(),
151 features: Vec::new(),
147 }); 152 });
148 let pkg_data = &mut packages[pkg]; 153 let pkg_data = &mut packages[pkg];
149 pkg_by_id.insert(meta_pkg.id.clone(), pkg); 154 pkg_by_id.insert(meta_pkg.id.clone(), pkg);
@@ -164,6 +169,7 @@ impl CargoWorkspace {
164 let dep = PackageDependency { name: dep_node.name, pkg: pkg_by_id[&dep_node.pkg] }; 169 let dep = PackageDependency { name: dep_node.name, pkg: pkg_by_id[&dep_node.pkg] };
165 packages[source].dependencies.push(dep); 170 packages[source].dependencies.push(dep);
166 } 171 }
172 packages[source].features.extend(node.features);
167 } 173 }
168 174
169 Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root }) 175 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 {
19 pub(crate) root_module: PathBuf, 19 pub(crate) root_module: PathBuf,
20 pub(crate) edition: Edition, 20 pub(crate) edition: Edition,
21 pub(crate) deps: Vec<Dep>, 21 pub(crate) deps: Vec<Dep>,
22 #[serde(default)]
23 pub(crate) features: Vec<String>,
22} 24}
23 25
24#[derive(Clone, Copy, Debug, Deserialize)] 26#[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::{
9 fs::File, 9 fs::File,
10 io::BufReader, 10 io::BufReader,
11 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
12 process::Command,
12}; 13};
13 14
14use ra_cfg::CfgOptions; 15use ra_cfg::CfgOptions;
@@ -118,6 +119,7 @@ impl ProjectWorkspace {
118 119
119 pub fn to_crate_graph( 120 pub fn to_crate_graph(
120 &self, 121 &self,
122 default_cfg_options: &CfgOptions,
121 load: &mut dyn FnMut(&Path) -> Option<FileId>, 123 load: &mut dyn FnMut(&Path) -> Option<FileId>,
122 ) -> (CrateGraph, FxHashMap<CrateId, String>) { 124 ) -> (CrateGraph, FxHashMap<CrateId, String>) {
123 let mut crate_graph = CrateGraph::default(); 125 let mut crate_graph = CrateGraph::default();
@@ -134,7 +136,9 @@ impl ProjectWorkspace {
134 }; 136 };
135 // FIXME: cfg options 137 // FIXME: cfg options
136 // Default to enable test for workspace crates. 138 // Default to enable test for workspace crates.
137 let cfg_options = CfgOptions::default().atom("test".into()); 139 let cfg_options = default_cfg_options
140 .clone()
141 .features(krate.features.iter().map(Into::into));
138 crates.insert( 142 crates.insert(
139 crate_id, 143 crate_id,
140 crate_graph.add_crate_root(file_id, edition, cfg_options), 144 crate_graph.add_crate_root(file_id, edition, cfg_options),
@@ -164,9 +168,8 @@ impl ProjectWorkspace {
164 let mut sysroot_crates = FxHashMap::default(); 168 let mut sysroot_crates = FxHashMap::default();
165 for krate in sysroot.crates() { 169 for krate in sysroot.crates() {
166 if let Some(file_id) = load(krate.root(&sysroot)) { 170 if let Some(file_id) = load(krate.root(&sysroot)) {
167 // FIXME: cfg options
168 // Crates from sysroot have `cfg(test)` disabled 171 // Crates from sysroot have `cfg(test)` disabled
169 let cfg_options = CfgOptions::default(); 172 let cfg_options = default_cfg_options.clone().remove_atom(&"test".into());
170 let crate_id = 173 let crate_id =
171 crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); 174 crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
172 sysroot_crates.insert(krate, crate_id); 175 sysroot_crates.insert(krate, crate_id);
@@ -197,9 +200,9 @@ impl ProjectWorkspace {
197 let root = tgt.root(&cargo); 200 let root = tgt.root(&cargo);
198 if let Some(file_id) = load(root) { 201 if let Some(file_id) = load(root) {
199 let edition = pkg.edition(&cargo); 202 let edition = pkg.edition(&cargo);
200 // FIXME: cfg options 203 let cfg_options = default_cfg_options
201 // Default to enable test for workspace crates. 204 .clone()
202 let cfg_options = CfgOptions::default().atom("test".into()); 205 .features(pkg.features(&cargo).iter().map(Into::into));
203 let crate_id = 206 let crate_id =
204 crate_graph.add_crate_root(file_id, edition, cfg_options); 207 crate_graph.add_crate_root(file_id, edition, cfg_options);
205 names.insert(crate_id, pkg.name(&cargo).to_string()); 208 names.insert(crate_id, pkg.name(&cargo).to_string());
@@ -301,3 +304,32 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
301 } 304 }
302 Err(format!("can't find Cargo.toml at {}", path.display()))? 305 Err(format!("can't find Cargo.toml at {}", path.display()))?
303} 306}
307
308pub fn get_rustc_cfg_options() -> CfgOptions {
309 let mut cfg_options = CfgOptions::default();
310
311 match (|| -> Result<_> {
312 // `cfg(test)` ans `cfg(debug_assertion)` is handled outside, so we suppress them here.
313 let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?;
314 if !output.status.success() {
315 Err("failed to get rustc cfgs")?;
316 }
317 Ok(String::from_utf8(output.stdout)?)
318 })() {
319 Ok(rustc_cfgs) => {
320 for line in rustc_cfgs.lines() {
321 match line.find('=') {
322 None => cfg_options = cfg_options.atom(line.into()),
323 Some(pos) => {
324 let key = &line[..pos];
325 let value = line[pos + 1..].trim_matches('"');
326 cfg_options = cfg_options.key_value(key.into(), value.into());
327 }
328 }
329 }
330 }
331 Err(e) => log::error!("failed to get rustc cfgs: {}", e),
332 }
333
334 cfg_options
335}