Skip to content

Commit adb6982

Browse files
committed
stub JSON information flow from cargo-build-time to run-time
1 parent 052dece commit adb6982

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

cargo-miri/bin.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use std::env;
22
use std::ffi::OsString;
33
use std::fs::{self, File};
4-
use std::io::{self, BufRead, Write};
4+
use std::io::{self, BufRead, BufReader, BufWriter, Write};
55
use std::ops::Not;
66
use std::path::{Path, PathBuf};
77
use std::process::Command;
8+
use std::collections::HashMap;
9+
10+
use serde::{Deserialize, Serialize};
811

912
use rustc_version::VersionMeta;
1013

@@ -41,6 +44,15 @@ enum MiriCommand {
4144
Setup,
4245
}
4346

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+
4456
fn show_help() {
4557
println!("{}", CARGO_MIRI_HELP);
4658
}
@@ -439,15 +451,24 @@ fn phase_cargo_rustc(mut args: env::Args) {
439451
// like we want them.
440452
// Instead of compiling, we write JSON into the output file with all the relevant command-line flags
441453
// 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+
"{}{}",
445459
get_arg_flag_value("--crate-name").unwrap(),
446460
// This is technically a `-C` flag but the prefix seems unique enough...
447461
// (and cargo passes this before the filename so it should be unique)
448462
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+
451472
return;
452473
}
453474

@@ -485,6 +506,13 @@ fn phase_cargo_rustc(mut args: env::Args) {
485506

486507
fn phase_cargo_runner(binary: &str, args: env::Args) {
487508
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.
488516
}
489517

490518
fn main() {

0 commit comments

Comments
 (0)