Skip to content

Commit

Permalink
get cargo-miri to work
Browse files Browse the repository at this point in the history
  • Loading branch information
David Renshaw committed Jun 29, 2017
1 parent 8722ce8 commit e3fa4fb
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/bin/cargo-miri.rs
Expand Up @@ -44,8 +44,6 @@ fn main() {
return;
}

let dep_path = std::env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps");

if let Some("miri") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
// this arm is when `cargo miri` is called

Expand Down Expand Up @@ -84,13 +82,11 @@ fn main() {
let args = std::env::args().skip(skip);
let kind = target.kind.get(0).expect("badly formatted cargo metadata: target::kind is an empty array");
if test && kind == "test" {
if let Err(code) = process(vec!["--test".to_string(), target.name].into_iter().chain(args),
&dep_path) {
if let Err(code) = process(vec!["--test".to_string(), target.name].into_iter().chain(args)) {
std::process::exit(code);
}
} else if !test && kind == "bin" {
if let Err(code) = process(vec!["--bin".to_string(), target.name].into_iter().chain(args),
&dep_path) {
if let Err(code) = process(vec!["--bin".to_string(), target.name].into_iter().chain(args)) {
std::process::exit(code);
}
}
Expand All @@ -117,7 +113,7 @@ fn main() {
.expect("need to specify RUST_SYSROOT env var during miri compilation, or use rustup or multirust")
};

// this conditional check for the --sysroot flag is there so users can call `cargo-clippy` directly
// this conditional check for the --sysroot flag is there so users can call `cargo-miri` directly
// without having to pass --sysroot or anything
let mut args: Vec<String> = if std::env::args().any(|s| s == "--sysroot") {
std::env::args().skip(1).collect()
Expand All @@ -129,25 +125,29 @@ fn main() {
// interpreted but not built
let miri_enabled = std::env::args().any(|s| s == "-Zno-trans");

if miri_enabled {
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-miri""#.to_owned()]);
}
let mut command = if miri_enabled {
let mut path = std::env::current_exe().expect("current executable path invalid");
path.set_file_name("miri");
Command::new(path)
} else {
Command::new("rustc")
};

let mut path = std::env::current_exe().expect("current executable path invalid");
path.set_file_name("miri");
args.extend_from_slice(&["-Z".to_owned(), "always-encode-mir".to_owned()]);
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-miri""#.to_owned()]);

match Command::new(path).args(&args).status() {
match command.args(&args).status() {
Ok(exit) => if !exit.success() {
std::process::exit(exit.code().unwrap_or(42));
},
Err(e) => panic!("error during miri run: {:?}", e),
Err(ref e) if miri_enabled => panic!("error during miri run: {:?}", e),
Err(ref e) => panic!("error during rustc call: {:?}", e),
}
}
}

fn process<P, I>(old_args: I, dep_path: P) -> Result<(), i32>
where P: AsRef<Path>,
I: Iterator<Item = String>
fn process<I>(old_args: I) -> Result<(), i32>
where I: Iterator<Item = String>
{
let mut args = vec!["rustc".to_owned()];

Expand All @@ -159,8 +159,6 @@ fn process<P, I>(old_args: I, dep_path: P) -> Result<(), i32>
if !found_dashes {
args.push("--".to_owned());
}
args.push("-L".to_owned());
args.push(dep_path.as_ref().to_string_lossy().into_owned());
args.push("-Zno-trans".to_owned());
args.push("--cfg".to_owned());
args.push(r#"feature="cargo-miri""#.to_owned());
Expand Down

0 comments on commit e3fa4fb

Please sign in to comment.