6912: Default to host platform for cargo metadata r=jonhoo a=jonhoo

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.

Co-authored-by: Jon Gjengset <jongje@amazon.com>
Co-authored-by: Jon Gjengset <jon@thesquareplanet.com>
This commit is contained in:
bors[bot] 2020-12-17 18:32:13 +00:00 committed by GitHub
commit b55cb897e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,7 @@
use rustc_hash::FxHashMap;
use crate::cfg_flag::CfgFlag;
use crate::utf8_stdout;
/// `CargoWorkspace` represents the logical structure of, well, a Cargo
/// workspace. It pretty closely mirrors `cargo metadata` output.
@ -166,8 +167,34 @@ pub fn from_cargo_metadata(
if let Some(parent) = cargo_toml.parent() {
meta.current_dir(parent.to_path_buf());
}
if let Some(target) = config.target.as_ref() {
meta.other_options(vec![String::from("--filter-platform"), target.clone()]);
let target = if let Some(target) = config.target.as_ref() {
Some(target.clone())
} else {
// cargo metadata defaults to giving information for _all_ targets.
// In the absence of a preference from the user, we use the host platform.
let mut rustc = Command::new(toolchain::rustc());
rustc.current_dir(cargo_toml.parent().unwrap()).arg("-vV");
log::debug!("Discovering host platform by {:?}", rustc);
match utf8_stdout(rustc) {
Ok(stdout) => {
let field = "host: ";
let target = stdout.lines().find_map(|l| l.strip_prefix(field));
if let Some(target) = target {
Some(target.to_string())
} else {
// If we fail to resolve the host platform, it's not the end of the world.
log::info!("rustc -vV did not report host platform, got:\n{}", stdout);
None
}
}
Err(e) => {
log::warn!("Failed to discover host platform: {}", e);
None
}
}
};
if let Some(target) = target {
meta.other_options(vec![String::from("--filter-platform"), target]);
}
let mut meta = meta.exec().with_context(|| {
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())