diff options
author | Benjamin Coenen <[email protected]> | 2020-05-10 11:31:55 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-10 11:31:55 +0100 |
commit | e80903a96564c2239489a8c630a4748bf21a3659 (patch) | |
tree | 12b31a1fd12deb2120065cea5a558425c8c1984f /crates/ra_project_model/src/cargo_workspace.rs | |
parent | 6203e9c4faee288f16d93dbb7dd0f1f8df487d83 (diff) | |
parent | 4578154b608fa075595103d0c933da60d55b25c8 (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into feat_4348
Diffstat (limited to 'crates/ra_project_model/src/cargo_workspace.rs')
-rw-r--r-- | crates/ra_project_model/src/cargo_workspace.rs | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 59f46a2a0..082af4f96 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::{ | 3 | use std::{ |
4 | env, | ||
5 | ffi::OsStr, | 4 | ffi::OsStr, |
6 | ops, | 5 | ops, |
7 | path::{Path, PathBuf}, | 6 | path::{Path, PathBuf}, |
@@ -87,6 +86,7 @@ pub struct PackageData { | |||
87 | pub dependencies: Vec<PackageDependency>, | 86 | pub dependencies: Vec<PackageDependency>, |
88 | pub edition: Edition, | 87 | pub edition: Edition, |
89 | pub features: Vec<String>, | 88 | pub features: Vec<String>, |
89 | pub cfgs: Vec<String>, | ||
90 | pub out_dir: Option<PathBuf>, | 90 | pub out_dir: Option<PathBuf>, |
91 | pub proc_macro_dylib_path: Option<PathBuf>, | 91 | pub proc_macro_dylib_path: Option<PathBuf>, |
92 | } | 92 | } |
@@ -145,12 +145,8 @@ impl CargoWorkspace { | |||
145 | cargo_toml: &Path, | 145 | cargo_toml: &Path, |
146 | cargo_features: &CargoConfig, | 146 | cargo_features: &CargoConfig, |
147 | ) -> Result<CargoWorkspace> { | 147 | ) -> Result<CargoWorkspace> { |
148 | let _ = Command::new(cargo_binary()) | ||
149 | .arg("--version") | ||
150 | .output() | ||
151 | .context("failed to run `cargo --version`, is `cargo` in PATH?")?; | ||
152 | |||
153 | let mut meta = MetadataCommand::new(); | 148 | let mut meta = MetadataCommand::new(); |
149 | meta.cargo_path(ra_toolchain::cargo()); | ||
154 | meta.manifest_path(cargo_toml); | 150 | meta.manifest_path(cargo_toml); |
155 | if cargo_features.all_features { | 151 | if cargo_features.all_features { |
156 | meta.features(CargoOpt::AllFeatures); | 152 | meta.features(CargoOpt::AllFeatures); |
@@ -172,10 +168,12 @@ impl CargoWorkspace { | |||
172 | })?; | 168 | })?; |
173 | 169 | ||
174 | let mut out_dir_by_id = FxHashMap::default(); | 170 | let mut out_dir_by_id = FxHashMap::default(); |
171 | let mut cfgs = FxHashMap::default(); | ||
175 | let mut proc_macro_dylib_paths = FxHashMap::default(); | 172 | let mut proc_macro_dylib_paths = FxHashMap::default(); |
176 | if cargo_features.load_out_dirs_from_check { | 173 | if cargo_features.load_out_dirs_from_check { |
177 | let resources = load_extern_resources(cargo_toml, cargo_features)?; | 174 | let resources = load_extern_resources(cargo_toml, cargo_features)?; |
178 | out_dir_by_id = resources.out_dirs; | 175 | out_dir_by_id = resources.out_dirs; |
176 | cfgs = resources.cfgs; | ||
179 | proc_macro_dylib_paths = resources.proc_dylib_paths; | 177 | proc_macro_dylib_paths = resources.proc_dylib_paths; |
180 | } | 178 | } |
181 | 179 | ||
@@ -201,6 +199,7 @@ impl CargoWorkspace { | |||
201 | edition, | 199 | edition, |
202 | dependencies: Vec::new(), | 200 | dependencies: Vec::new(), |
203 | features: Vec::new(), | 201 | features: Vec::new(), |
202 | cfgs: cfgs.get(&id).cloned().unwrap_or_default(), | ||
204 | out_dir: out_dir_by_id.get(&id).cloned(), | 203 | out_dir: out_dir_by_id.get(&id).cloned(), |
205 | proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(), | 204 | proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(), |
206 | }); | 205 | }); |
@@ -282,13 +281,14 @@ impl CargoWorkspace { | |||
282 | pub struct ExternResources { | 281 | pub struct ExternResources { |
283 | out_dirs: FxHashMap<PackageId, PathBuf>, | 282 | out_dirs: FxHashMap<PackageId, PathBuf>, |
284 | proc_dylib_paths: FxHashMap<PackageId, PathBuf>, | 283 | proc_dylib_paths: FxHashMap<PackageId, PathBuf>, |
284 | cfgs: FxHashMap<PackageId, Vec<String>>, | ||
285 | } | 285 | } |
286 | 286 | ||
287 | pub fn load_extern_resources( | 287 | pub fn load_extern_resources( |
288 | cargo_toml: &Path, | 288 | cargo_toml: &Path, |
289 | cargo_features: &CargoConfig, | 289 | cargo_features: &CargoConfig, |
290 | ) -> Result<ExternResources> { | 290 | ) -> Result<ExternResources> { |
291 | let mut cmd = Command::new(cargo_binary()); | 291 | let mut cmd = Command::new(ra_toolchain::cargo()); |
292 | cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); | 292 | cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); |
293 | if cargo_features.all_features { | 293 | if cargo_features.all_features { |
294 | cmd.arg("--all-features"); | 294 | cmd.arg("--all-features"); |
@@ -307,8 +307,14 @@ pub fn load_extern_resources( | |||
307 | for message in cargo_metadata::parse_messages(output.stdout.as_slice()) { | 307 | for message in cargo_metadata::parse_messages(output.stdout.as_slice()) { |
308 | if let Ok(message) = message { | 308 | if let Ok(message) = message { |
309 | match message { | 309 | match message { |
310 | Message::BuildScriptExecuted(BuildScript { package_id, out_dir, .. }) => { | 310 | Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => { |
311 | res.out_dirs.insert(package_id, out_dir); | 311 | res.out_dirs.insert(package_id.clone(), out_dir); |
312 | res.cfgs.insert( | ||
313 | package_id, | ||
314 | // FIXME: Current `cargo_metadata` uses `PathBuf` instead of `String`, | ||
315 | // change when https://github.com/oli-obk/cargo_metadata/pulls/112 reaches crates.io | ||
316 | cfgs.iter().filter_map(|c| c.to_str().map(|s| s.to_owned())).collect(), | ||
317 | ); | ||
312 | } | 318 | } |
313 | 319 | ||
314 | Message::CompilerArtifact(message) => { | 320 | Message::CompilerArtifact(message) => { |
@@ -336,7 +342,3 @@ fn is_dylib(path: &Path) -> bool { | |||
336 | Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), | 342 | Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), |
337 | } | 343 | } |
338 | } | 344 | } |
339 | |||
340 | fn cargo_binary() -> String { | ||
341 | env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()) | ||
342 | } | ||