aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model/src/cargo_workspace.rs
diff options
context:
space:
mode:
authorJon Gjengset <[email protected]>2020-12-17 01:38:21 +0000
committerJon Gjengset <[email protected]>2020-12-17 01:38:37 +0000
commit9802c0ccd0cf408260961eb61581b61363c4b47b (patch)
tree81be12d7e34db589eba33024a25af854cf93aaa3 /crates/project_model/src/cargo_workspace.rs
parent554dd215c729c3598ceb014f61d9d295d554036a (diff)
Default to host platform for cargo metadata
This modifies the logic for calling cargo metadata so that it will use the host platform if no explicit target platform is given. This is needed since cargo metadata defaults to outputting information for _all_ targets. Fixes #6908.
Diffstat (limited to 'crates/project_model/src/cargo_workspace.rs')
-rw-r--r--crates/project_model/src/cargo_workspace.rs32
1 files changed, 30 insertions, 2 deletions
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index 894c5c952..91fb645da 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -16,6 +16,7 @@ use paths::{AbsPath, AbsPathBuf};
16use rustc_hash::FxHashMap; 16use rustc_hash::FxHashMap;
17 17
18use crate::cfg_flag::CfgFlag; 18use crate::cfg_flag::CfgFlag;
19use crate::utf8_stdout;
19 20
20/// `CargoWorkspace` represents the logical structure of, well, a Cargo 21/// `CargoWorkspace` represents the logical structure of, well, a Cargo
21/// workspace. It pretty closely mirrors `cargo metadata` output. 22/// workspace. It pretty closely mirrors `cargo metadata` output.
@@ -166,8 +167,35 @@ impl CargoWorkspace {
166 if let Some(parent) = cargo_toml.parent() { 167 if let Some(parent) = cargo_toml.parent() {
167 meta.current_dir(parent.to_path_buf()); 168 meta.current_dir(parent.to_path_buf());
168 } 169 }
169 if let Some(target) = config.target.as_ref() { 170 let target = if let Some(target) = config.target.as_ref() {
170 meta.other_options(vec![String::from("--filter-platform"), target.clone()]); 171 Some(target.clone())
172 } else {
173 // cargo metadata defaults to giving information for _all_ targets.
174 // In the absence of a preference from the user, we use the host platform.
175 let mut rustc = Command::new(toolchain::rustc());
176 rustc.current_dir(cargo_toml.parent().unwrap()).arg("-vV");
177 log::debug!("Discovering host platform by {:?}", rustc);
178 match utf8_stdout(rustc) {
179 Ok(stdout) => {
180 let field = "host: ";
181 let target =
182 stdout.lines().find(|l| l.starts_with(field)).map(|l| &l[field.len()..]);
183 if let Some(target) = target {
184 Some(target.to_string())
185 } else {
186 // If we fail to resolve the host platform, it's not the end of the world.
187 log::info!("rustc -vV did not report host platform, got:\n{}", stdout);
188 None
189 }
190 }
191 Err(e) => {
192 log::warn!("Failed to discover host platform: {}", e);
193 None
194 }
195 }
196 };
197 if let Some(target) = target {
198 meta.other_options(vec![String::from("--filter-platform"), target]);
171 } 199 }
172 let mut meta = meta.exec().with_context(|| { 200 let mut meta = meta.exec().with_context(|| {
173 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()) 201 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())