Skip to content

Commit

Permalink
Use 'cargo check' to build the sysroot and target crate
Browse files Browse the repository at this point in the history
Fixes rust-lang#1057

I'm using my original approach from PR rust-lang#1048. Ideally, we would
distinguish between build-deps/dependencies/'final crate' via a
different approach (e.g. the target directory). However, I
haven't been able to get that to work just yet.

However, everything should be working with the approach I'm using. At a
minimum, we can use this PR to verify that everything works as expected
when we don't actually produce native build outputs.
  • Loading branch information
Aaron1011 committed Jan 1, 2020
1 parent 86d7db4 commit 29b2fad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
11 changes: 6 additions & 5 deletions 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.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ rustc-workspace-hack = "1.0.0"
# between "cargo build" and "cargo intall".
num-traits = "*"
serde = { version = "*", features = ["derive"] }
serde_json = "1.0.44"

[build-dependencies]
vergen = "3"
Expand Down
82 changes: 61 additions & 21 deletions src/bin/cargo-miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::Not;
use std::path::{Path, PathBuf};
use std::process::Command;

const XARGO_MIN_VERSION: (u32, u32, u32) = (0, 3, 17);
const XARGO_MIN_VERSION: (u32, u32, u32) = (0, 3, 19);

const CARGO_MIRI_HELP: &str = r#"Interprets bin crates and tests in Miri
Expand Down Expand Up @@ -84,7 +84,37 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
}
}

fn list_targets() -> impl Iterator<Item = cargo_metadata::Target> {
fn is_build_dep(mut args: impl Iterator<Item = String>) -> bool {
args.any(|arg| arg.starts_with("--emit=") && arg.contains("link"))
}

// Returns whether or not Cargo invoked the wrapper (this binary) to compile
// the final, target crate (either a test for 'cargo test', or a binary for 'cargo run')
// Right now, this is an awful hack that checks several different pieces of information
// to try to figure out if the crate being compiled is the right one.
// Ideally, Cargo would set en environment variable indicating whether or
// not the wrapper is being invoked on the target crate.
// For now, this is the best we can do
fn is_target_crate(is_build_script: bool) -> bool {
// Cargo sets this to the directory containing the manifest of the crate
// the wrapper is being invoekd to compile. This should be unique
// across the entire build (except for build scripts, which we handle below).
// We cannot check the crate name, since this may not be unique
// (e.g. if the build contains multiple versions of the same crate,
// or the same crate from multiple sources)
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").ok();

// The manifest directory for our target crate. This is set by `cargo-miri`
// (the original invoation by the user) by using `cargo_metadata` to locate
// the manifest.
let expected_dir = std::env::var("MIRI_MAGIC_DIR").expect("MIRI_MAGIC_DIR not set!");

manifest_dir == Some(expected_dir) && !is_build_script

}


fn read_cargo_metadata() -> (impl Iterator<Item = cargo_metadata::Target>, String) {
// We need to get the manifest, and then the metadata, to enumerate targets.
let manifest_path =
get_arg_flag_value("--manifest-path").map(|m| Path::new(&m).canonicalize().unwrap());
Expand Down Expand Up @@ -124,7 +154,7 @@ fn list_targets() -> impl Iterator<Item = cargo_metadata::Target> {
let package = metadata.packages.remove(package_index);

// Finally we got the list of targets to build
package.targets.into_iter()
(package.targets.into_iter(), metadata.workspace_root.to_string_lossy().to_string())
}

/// Returns the path to the `miri` binary
Expand Down Expand Up @@ -197,7 +227,7 @@ fn xargo() -> Command {
// Bootstrap tells us where to find xargo
Command::new(val)
} else {
Command::new("xargo")
Command::new("xargo-check")
}
}

Expand Down Expand Up @@ -458,8 +488,10 @@ fn in_cargo_miri() {
return;
}

let (targets, root_dir) = read_cargo_metadata();

// Now run the command.
for target in list_targets() {
for target in targets {
let mut args = std::env::args().skip(skip);
let kind = target
.kind
Expand All @@ -469,7 +501,8 @@ fn in_cargo_miri() {
// change to add additional arguments. `FLAGS` is set to identify
// this target. The user gets to control what gets actually passed to Miri.
let mut cmd = cargo();
cmd.arg("rustc");
cmd.arg("check");
cmd.arg("--target").arg("x86_64-unknown-linux-gnu");
match (subcommand, kind.as_str()) {
(MiriCommand::Run, "bin") => {
// FIXME: we just run all the binaries here.
Expand All @@ -496,10 +529,15 @@ fn in_cargo_miri() {
}
cmd.arg(arg);
}

let args_vec: Vec<String> = args.collect();
cmd.env("MIRI_MAGIC_DIR", root_dir.clone());
cmd.env("MIRI_MAGIC_ARGS", serde_json::to_string(&args_vec).expect("failed to serialize args"));

// Add `--` (to end the `cargo` flags), and then the user flags. We add markers around the
// user flags to be able to identify them later. "cargo rustc" adds more stuff after this,
// so we have to mark both the beginning and the end.
cmd.arg("--").arg("cargo-miri-marker-begin").args(args).arg("cargo-miri-marker-end");
//cmd.arg("--").arg("cargo-miri-marker-begin").args(args).arg("cargo-miri-marker-end");
let path = std::env::current_exe().expect("current executable path invalid");
cmd.env("RUSTC_WRAPPER", path);
if verbose {
Expand All @@ -519,25 +557,27 @@ fn inside_cargo_rustc() {
let sysroot = std::env::var("MIRI_SYSROOT").expect("The wrapper should have set MIRI_SYSROOT");

let rustc_args = std::env::args().skip(2); // skip `cargo rustc`
let mut args: Vec<String> =
rustc_args.chain(Some("--sysroot".to_owned())).chain(Some(sysroot)).collect();
args.splice(0..0, miri::miri_default_args().iter().map(ToString::to_string));

let in_build_script = is_build_dep(std::env::args().skip(2));

let mut args = if in_build_script {
rustc_args.collect()
} else {
let mut args: Vec<String> = rustc_args
.chain(Some("--sysroot".to_owned()))
.chain(Some(sysroot))
.collect();
args.splice(0..0, miri::miri_default_args().iter().map(ToString::to_string));
args
};

// See if we can find the `cargo-miri` markers. Those only get added to the binary we want to
// run. They also serve to mark the user-defined arguments, which we have to move all the way
// to the end (they get added somewhere in the middle).
let needs_miri =
if let Some(begin) = args.iter().position(|arg| arg == "cargo-miri-marker-begin") {
let end = args
.iter()
.position(|arg| arg == "cargo-miri-marker-end")
.expect("cannot find end marker");
// These mark the user arguments. We remove the first and last as they are the markers.
let mut user_args = args.drain(begin..=end);
assert_eq!(user_args.next().unwrap(), "cargo-miri-marker-begin");
assert_eq!(user_args.next_back().unwrap(), "cargo-miri-marker-end");
// Collect the rest and add it back at the end.
let mut user_args = user_args.collect::<Vec<String>>();
if is_target_crate(in_build_script) {
let magic = std::env::var("MIRI_MAGIC_ARGS").expect("missing MIRI_MAGIC_ARGS");
let mut user_args: Vec<String> = serde_json::from_str(&magic).expect("failed to deserialize args");
args.append(&mut user_args);
// Run this in Miri.
true
Expand Down

0 comments on commit 29b2fad

Please sign in to comment.