|
1 | 1 | use std::env; |
2 | 2 | use std::ffi::OsString; |
3 | 3 | use std::fs::{self, File}; |
4 | | -use std::io::{self, BufRead, Write}; |
| 4 | +use std::io::{self, BufRead, BufReader, BufWriter, Write}; |
5 | 5 | use std::ops::Not; |
6 | 6 | use std::path::{Path, PathBuf}; |
7 | 7 | use std::process::Command; |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +use serde::{Deserialize, Serialize}; |
8 | 11 |
|
9 | 12 | use rustc_version::VersionMeta; |
10 | 13 |
|
@@ -41,6 +44,15 @@ enum MiriCommand { |
41 | 44 | Setup, |
42 | 45 | } |
43 | 46 |
|
| 47 | +/// The inforamtion Miri needs to run a crate. Stored as JSON when the crate is "compiled". |
| 48 | +#[derive(Serialize, Deserialize)] |
| 49 | +struct CrateRunInfo { |
| 50 | + /// The command-line arguments. |
| 51 | + args: Vec<OsString>, |
| 52 | + /// The environment. |
| 53 | + env: HashMap<OsString, OsString>, |
| 54 | +} |
| 55 | + |
44 | 56 | fn show_help() { |
45 | 57 | println!("{}", CARGO_MIRI_HELP); |
46 | 58 | } |
@@ -439,15 +451,24 @@ fn phase_cargo_rustc(mut args: env::Args) { |
439 | 451 | // like we want them. |
440 | 452 | // Instead of compiling, we write JSON into the output file with all the relevant command-line flags |
441 | 453 | // and environment variables; this is sued alter when cargo calls us again in the CARGO_TARGET_RUNNER phase. |
442 | | - let filename = format!( |
443 | | - "{}/{}{}", |
444 | | - get_arg_flag_value("--out-dir").unwrap(), |
| 454 | + let info = CrateRunInfo { args: Vec::new(), env: HashMap::new() }; |
| 455 | + |
| 456 | + let mut path = PathBuf::from(get_arg_flag_value("--out-dir").unwrap()); |
| 457 | + path.push(format!( |
| 458 | + "{}{}", |
445 | 459 | get_arg_flag_value("--crate-name").unwrap(), |
446 | 460 | // This is technically a `-C` flag but the prefix seems unique enough... |
447 | 461 | // (and cargo passes this before the filename so it should be unique) |
448 | 462 | get_arg_flag_value("extra-filename").unwrap_or(String::new()), |
449 | | - ); |
450 | | - eprintln!("Miri is supposed to run {}", filename); |
| 463 | + )); |
| 464 | + eprintln!("Miri is supposed to run {}", path.display()); |
| 465 | + |
| 466 | + let file = File::create(&path) |
| 467 | + .unwrap_or_else(|_| show_error(format!("Cannot create {}", path.display()))); |
| 468 | + let file = BufWriter::new(file); |
| 469 | + serde_json::ser::to_writer(file, &info) |
| 470 | + .unwrap_or_else(|_| show_error(format!("Cannot write to {}", path.display()))); |
| 471 | + |
451 | 472 | return; |
452 | 473 | } |
453 | 474 |
|
@@ -485,6 +506,13 @@ fn phase_cargo_rustc(mut args: env::Args) { |
485 | 506 |
|
486 | 507 | fn phase_cargo_runner(binary: &str, args: env::Args) { |
487 | 508 | eprintln!("Asked to execute {}, args: {:?}", binary, args.collect::<Vec<_>>()); |
| 509 | + |
| 510 | + let file = File::open(binary) |
| 511 | + .unwrap_or_else(|_| show_error(format!("File {:?} not found, or cargo-miri invoked incorrectly", binary))); |
| 512 | + let file = BufReader::new(file); |
| 513 | + let info: CrateRunInfo = serde_json::from_reader(file) |
| 514 | + .unwrap_or_else(|_| show_error(format!("File {:?} does not contain valid JSON", binary))); |
| 515 | + // FIXME: remove the file. |
488 | 516 | } |
489 | 517 |
|
490 | 518 | fn main() { |
|
0 commit comments