Skip to content

Commit 531211c

Browse files
committed
handle binary suffices (for Windows); stop deleting fake binary
1 parent 15e1baf commit 531211c

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

cargo-miri/bin.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ impl CrateRunInfo {
5959
let env = env::vars_os().collect();
6060
CrateRunInfo { args, env }
6161
}
62+
63+
fn store(&self, filename: &Path) {
64+
let file = File::create(filename)
65+
.unwrap_or_else(|_| show_error(format!("Cannot create `{}`", filename.display())));
66+
let file = BufWriter::new(file);
67+
serde_json::ser::to_writer(file, self)
68+
.unwrap_or_else(|_| show_error(format!("Cannot write to `{}`", filename.display())));
69+
}
6270
}
6371

6472
fn show_help() {
@@ -479,18 +487,16 @@ fn phase_cargo_rustc(args: env::Args) {
479487
// Instead of compiling, we write JSON into the output file with all the relevant command-line flags
480488
// and environment variables; this is used when cargo calls us again in the CARGO_TARGET_RUNNER phase.
481489
let info = CrateRunInfo::collect(args);
482-
// FIXME: Windows might need a ".exe" suffix.
483490
let filename = out_filename("", "");
484-
485491
if verbose {
486492
eprintln!("[cargo-miri rustc] writing run info to `{}`", filename.display());
487493
}
488494

489-
let file = File::create(&filename)
490-
.unwrap_or_else(|_| show_error(format!("Cannot create `{}`", filename.display())));
491-
let file = BufWriter::new(file);
492-
serde_json::ser::to_writer(file, &info)
493-
.unwrap_or_else(|_| show_error(format!("Cannot write to `{}`", filename.display())));
495+
info.store(&filename);
496+
// For Windows, do the same thing again with `.exe` appended to the filename.
497+
// (Need to do this here as cargo moves that "binary" to a different place before running it.)
498+
info.store(&out_filename("", ".exe"));
499+
494500
return;
495501
}
496502

@@ -555,16 +561,11 @@ fn phase_cargo_rustc(args: env::Args) {
555561
fn phase_cargo_runner(binary: &str, binary_args: env::Args) {
556562
let verbose = std::env::var_os("MIRI_VERBOSE").is_some();
557563

558-
// Strip extension from binary name (Windows adds ".exe").
559-
let mut filename = PathBuf::from(binary);
560-
filename.set_extension("");
561-
let file = File::open(&filename)
564+
let file = File::open(&binary)
562565
.unwrap_or_else(|_| show_error(format!("File {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`", binary)));
563566
let file = BufReader::new(file);
564567
let info: CrateRunInfo = serde_json::from_reader(file)
565568
.unwrap_or_else(|_| show_error(format!("File {:?} does not contain valid JSON", binary)));
566-
fs::remove_file(&filename)
567-
.unwrap_or_else(|_| show_error(format!("Unable to remove file {:?}", binary)));
568569

569570
let mut cmd = miri();
570571
// Forward rustc arguments. We need to patch "--extern" filenames because
@@ -590,10 +591,10 @@ fn phase_cargo_runner(binary: &str, binary_args: env::Args) {
590591
if let Ok(a) = env::var("MIRIFLAGS") {
591592
// This code is taken from `RUSTFLAGS` handling in cargo.
592593
let args = a
593-
.split(' ')
594-
.map(str::trim)
595-
.filter(|s| !s.is_empty())
596-
.map(str::to_string);
594+
.split(' ')
595+
.map(str::trim)
596+
.filter(|s| !s.is_empty())
597+
.map(str::to_string);
597598
cmd.args(args);
598599
}
599600

0 commit comments

Comments
 (0)