Skip to content

Commit

Permalink
Auto merge of rust-lang#2450 - avrong:cargo-metadata, r=oli-obk
Browse files Browse the repository at this point in the history
Use cargo_metadata in cargo-miri

Closes rust-lang#2393

Added `cargo_metadata` to `cargo-miri` and changed metadata from manual parsing to `cargo_metadata` invocations. Thus, removed local `Metadata` struct too.

Happy to fix if anything isn't right :)
  • Loading branch information
bors committed Jul 29, 2022
2 parents a719c05 + 01a6109 commit 44dc49f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
75 changes: 55 additions & 20 deletions cargo-miri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-miri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ doctest = false # and no doc tests
directories = "3"
rustc_version = "0.4"
serde_json = "1.0.40"
cargo_metadata = "0.15.0"

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
Expand Down
37 changes: 13 additions & 24 deletions cargo-miri/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::ops::Not;
use std::path::{Path, PathBuf};
use std::process::{self, Command};

use cargo_metadata::{Metadata, MetadataCommand};
use rustc_version::VersionMeta;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -582,18 +583,13 @@ path = "lib.rs"
}
}

#[derive(Deserialize)]
struct Metadata {
target_directory: PathBuf,
workspace_members: Vec<String>,
}

fn get_cargo_metadata() -> Metadata {
let mut cmd = cargo();
// `-Zunstable-options` is required by `--config`.
cmd.args(["metadata", "--no-deps", "--format-version=1", "-Zunstable-options"]);
// The `build.target-dir` config can be passed by `--config` flags, so forward them to
// `cargo metadata`.
let mut additional_options = Vec::new();
// `-Zunstable-options` is required by `--config`.
additional_options.push("-Zunstable-options".to_string());

let config_flag = "--config";
for arg in ArgSplitFlagValue::new(
env::args().skip(3), // skip the program name, "miri" and "run" / "test"
Expand All @@ -602,21 +598,14 @@ fn get_cargo_metadata() -> Metadata {
// Only look at `Ok`
.flatten()
{
cmd.arg(config_flag).arg(arg);
}
let mut child = cmd
.stdin(process::Stdio::null())
.stdout(process::Stdio::piped())
.spawn()
.expect("failed ro run `cargo metadata`");
// Check this `Result` after `status.success()` is checked, so we don't print the error
// to stderr if `cargo metadata` is also printing to stderr.
let metadata: Result<Metadata, _> = serde_json::from_reader(child.stdout.take().unwrap());
let status = child.wait().expect("failed to wait for `cargo metadata` to exit");
if !status.success() {
std::process::exit(status.code().unwrap_or(-1));
additional_options.push(config_flag.to_string());
additional_options.push(arg);
}
metadata.unwrap_or_else(|e| show_error(format!("invalid `cargo metadata` output: {}", e)))

let metadata =
MetadataCommand::new().no_deps().other_options(additional_options).exec().unwrap();

metadata
}

/// Pulls all the crates in this workspace from the cargo metadata.
Expand All @@ -627,7 +616,7 @@ fn local_crates(metadata: &Metadata) -> String {
assert!(!metadata.workspace_members.is_empty());
let mut local_crates = String::new();
for member in &metadata.workspace_members {
let name = member.split(' ').next().unwrap();
let name = member.repr.split(' ').next().unwrap();
let name = name.replace('-', "_");
local_crates.push_str(&name);
local_crates.push(',');
Expand Down

0 comments on commit 44dc49f

Please sign in to comment.