aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-10-02 19:02:53 +0100
committeruHOOCCOOHu <[email protected]>2019-10-02 19:28:03 +0100
commit1067a1c5f649cc206e35b427eaa8d6553280cc96 (patch)
treee55b3e9ad4b2ece335f5660712918b6b97390bd3 /crates/ra_project_model/src
parente0100e63ae2e873f119b905ac77c3355ffb351b0 (diff)
Read default cfgs from rustc
Diffstat (limited to 'crates/ra_project_model/src')
-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
3 files changed, 46 insertions, 6 deletions
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}