aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model/src/cargo_workspace.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/project_model/src/cargo_workspace.rs')
-rw-r--r--crates/project_model/src/cargo_workspace.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index 894c5c952..bb3b6f2ef 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,34 @@ 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 = stdout.lines().find_map(|l| l.strip_prefix(field));
182 if let Some(target) = target {
183 Some(target.to_string())
184 } else {
185 // If we fail to resolve the host platform, it's not the end of the world.
186 log::info!("rustc -vV did not report host platform, got:\n{}", stdout);
187 None
188 }
189 }
190 Err(e) => {
191 log::warn!("Failed to discover host platform: {}", e);
192 None
193 }
194 }
195 };
196 if let Some(target) = target {
197 meta.other_options(vec![String::from("--filter-platform"), target]);
171 } 198 }
172 let mut meta = meta.exec().with_context(|| { 199 let mut meta = meta.exec().with_context(|| {
173 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()) 200 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())