Skip to content

Commit

Permalink
Merge pull request rust-lang#230 from dwrensha/cargo-miri
Browse files Browse the repository at this point in the history
get cargo-miri to work
  • Loading branch information
oli-obk authored Jun 29, 2017
2 parents 8722ce8 + 7a755ce commit 4fa71ad
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
10 changes: 10 additions & 0 deletions cargo-miri-test/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-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["Oliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>"]

[dependencies]
byteorder = "1.0"
8 changes: 7 additions & 1 deletion cargo-miri-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
extern crate byteorder;

use byteorder::{BigEndian, ByteOrder};

fn main() {
assert_eq!(5, 5);
let buf = &[1,2,3,4];
let n = <BigEndian as ByteOrder>::read_u32(buf);
assert_eq!(n, 0x01020304);
}
36 changes: 17 additions & 19 deletions src/bin/cargo-miri.rs
Original file line number Diff line number Diff line change
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 4fa71ad

Please sign in to comment.