Skip to content

Commit 17bb2c0

Browse files
committed
Auto merge of rust-lang#2428 - RalfJung:cargo-miri-runner, r=RalfJung
cargo-miri: use '--config target.runner' rather than the TARGET_RUNNER env vars That means we can properly escape spaces in our filename. This should fix rust-lang/miri#2417, hopefully for real this time. We can also specify a "leading command" which makes it much easier to disambiguate runner invocations from rustdoc invocations! Thanks to `@ehuss` for suggesting this.
2 parents 62efc62 + b93fcd9 commit 17bb2c0

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

cargo-miri/bin.rs

+62-64
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::ffi::{OsStr, OsString};
99
use std::fmt::Write as _;
1010
use std::fs::{self, File};
1111
use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
12-
use std::iter::TakeWhile;
12+
use std::iter::{self, TakeWhile};
1313
use std::ops::Not;
1414
use std::path::{Path, PathBuf};
1515
use std::process::{self, Command};
@@ -35,6 +35,11 @@ The cargo options are exactly the same as for `cargo run` and `cargo test`, resp
3535
Examples:
3636
cargo miri run
3737
cargo miri test -- test-suite-filter
38+
39+
cargo miri setup --print sysroot
40+
This will print the path to the generated sysroot (and nothing else) on stdout.
41+
stderr will still contain progress information about how the build is doing.
42+
3843
"#;
3944

4045
#[derive(Clone, Debug)]
@@ -206,6 +211,14 @@ fn forward_miri_sysroot(cmd: &mut Command) {
206211
cmd.arg(sysroot);
207212
}
208213

214+
/// Escapes `s` in a way that is suitable for using it as a string literal in TOML syntax.
215+
fn escape_for_toml(s: &str) -> String {
216+
// We want to surround this string in quotes `"`. So we first escape all quotes,
217+
// and also all backslashes (that are used to escape quotes).
218+
let s = s.replace('\\', r#"\\"#).replace('"', r#"\""#);
219+
format!("\"{}\"", s)
220+
}
221+
209222
/// Returns the path to the `miri` binary
210223
fn find_miri() -> PathBuf {
211224
if let Some(path) = env::var_os("MIRI") {
@@ -353,17 +366,15 @@ fn write_to_file(filename: &Path, content: &str) {
353366
/// done all this already.
354367
fn setup(subcommand: &MiriCommand) {
355368
let only_setup = matches!(subcommand, MiriCommand::Setup);
369+
let ask_user = !only_setup;
370+
let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
356371
if std::env::var_os("MIRI_SYSROOT").is_some() {
357372
if only_setup {
358373
println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
359374
}
360375
return;
361376
}
362377

363-
// Subcommands other than `setup` will do a setup if necessary, but
364-
// interactively confirm first.
365-
let ask_user = !only_setup;
366-
367378
// First, we need xargo.
368379
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
369380
if std::env::var_os("XARGO_CHECK").is_some() {
@@ -499,8 +510,14 @@ path = "lib.rs"
499510
command.env("RUSTFLAGS", "-Cdebug-assertions=off -Coverflow-checks=on");
500511
// Manage the output the user sees.
501512
if only_setup {
513+
// We want to be explicit.
502514
eprintln!("Preparing a sysroot for Miri...");
515+
if print_sysroot {
516+
// Be extra sure there is no noise on stdout.
517+
command.stdout(process::Stdio::null());
518+
}
503519
} else {
520+
// We want to be quiet, but still let the user know that something is happening.
504521
eprint!("Preparing a sysroot for Miri... ");
505522
command.stdout(process::Stdio::null());
506523
command.stderr(process::Stdio::null());
@@ -515,22 +532,21 @@ path = "lib.rs"
515532
))
516533
}
517534
}
518-
if !only_setup {
519-
eprintln!("done");
520-
}
521535

522536
// That should be it! But we need to figure out where xargo built stuff.
523537
// Unfortunately, it puts things into a different directory when the
524538
// architecture matches the host.
525539
let sysroot = if target == &host { dir.join("HOST") } else { PathBuf::from(dir) };
526540
std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
527541
// Figure out what to print.
528-
let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
542+
if only_setup {
543+
eprintln!("A sysroot for Miri is now available in `{}`.", sysroot.display());
544+
} else {
545+
eprintln!("done");
546+
}
529547
if print_sysroot {
530548
// Print just the sysroot and nothing else to stdout; this way we do not need any escaping.
531549
println!("{}", sysroot.display());
532-
} else if only_setup {
533-
eprintln!("A sysroot for Miri is now available in `{}`.", sysroot.display());
534550
}
535551
}
536552

@@ -669,7 +685,11 @@ fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
669685
// <https://github.com/rust-lang/miri/pull/1540#issuecomment-693553191> describes an alternative
670686
// approach that uses `cargo check`, making that part easier but target and binary handling
671687
// harder.
672-
let cargo_miri_path = std::env::current_exe().expect("current executable path invalid");
688+
let cargo_miri_path = std::env::current_exe()
689+
.expect("current executable path invalid")
690+
.into_os_string()
691+
.into_string()
692+
.expect("current executable path is not valid UTF-8");
673693
let cargo_cmd = match subcommand {
674694
MiriCommand::Forward(s) => s,
675695
MiriCommand::Setup => return, // `cargo miri setup` stops here.
@@ -699,20 +719,21 @@ fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
699719
target_dir.push("miri");
700720
cmd.arg("--target-dir").arg(target_dir);
701721

702-
// Make sure we know the build target, and cargo does, too.
703-
// This is needed to make the `CARGO_TARGET_*_RUNNER` env var do something,
722+
// Make sure the build target is explicitly set.
723+
// This is needed to make the `target.runner` settings do something,
704724
// and it later helps us detect which crates are proc-macro/build-script
705725
// (host crates) and which crates are needed for the program itself.
706-
let host = version_info().host;
707-
let target = get_arg_flag_value("--target");
708-
let target = if let Some(ref target) = target {
709-
target
710-
} else {
711-
// No target given. Pick default and tell cargo about it.
726+
if get_arg_flag_value("--target").is_none() {
727+
// No target given. Explicitly pick the host.
712728
cmd.arg("--target");
713-
cmd.arg(&host);
714-
&host
715-
};
729+
cmd.arg(version_info().host);
730+
}
731+
732+
// Set ourselves as runner for al binaries invoked by cargo.
733+
// We use `all()` since `true` is not a thing in cfg-lang, but the empty conjunction is. :)
734+
let cargo_miri_path_for_toml = escape_for_toml(&cargo_miri_path);
735+
cmd.arg("--config")
736+
.arg(format!("target.'cfg(all())'.runner=[{cargo_miri_path_for_toml}, 'runner']"));
716737

717738
// Forward all further arguments after `--` to cargo.
718739
cmd.arg("--").args(args);
@@ -743,16 +764,6 @@ fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
743764
// bootstrap `rustc` thing in our way! Instead, we have MIRI_HOST_SYSROOT to use for host builds.
744765
cmd.env("RUSTC", &fs::canonicalize(find_miri()).unwrap());
745766

746-
let runner_env_name =
747-
|triple: &str| format!("CARGO_TARGET_{}_RUNNER", triple.to_uppercase().replace('-', "_"));
748-
let host_runner_env_name = runner_env_name(&host);
749-
let target_runner_env_name = runner_env_name(target);
750-
// Set the target runner to us, so we can interpret the binaries.
751-
cmd.env(&target_runner_env_name, &cargo_miri_path);
752-
// Unit tests of `proc-macro` crates are run on the host, so we set the host runner to
753-
// us in order to skip them.
754-
cmd.env(&host_runner_env_name, &cargo_miri_path);
755-
756767
// Set rustdoc to us as well, so we can run doctests.
757768
cmd.env("RUSTDOC", &cargo_miri_path);
758769

@@ -1194,38 +1205,25 @@ fn main() {
11941205
return;
11951206
}
11961207

1197-
let mut args = args.peekable();
1198-
if args.next_if(|a| a == "miri").is_some() {
1199-
phase_cargo_miri(args);
1200-
} else if let Some(arg) = args.peek().cloned() {
1201-
// Cargo calls us for everything it does. We could be invoked as rustc, rustdoc, or the runner.
1202-
1203-
// If the first arg is equal to the RUSTC variable (which should be set at this point),
1204-
// then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1205-
// having both RUSTC and RUSTC_WRAPPER set (see
1206-
// https://github.com/rust-lang/cargo/issues/10886).
1207-
if arg == env::var("RUSTC").unwrap() {
1208-
args.next().unwrap(); // consume wrapped RUSTC command.
1209-
return phase_rustc(args, RustcPhase::Build);
1210-
}
1211-
// We have to distinguish the "runner" and "rustdoc" cases.
1212-
// As runner, the first argument is the binary (a file that should exist, with an absolute path);
1213-
// as rustdoc, the first argument is a flag (`--something`).
1214-
let binary = Path::new(&arg);
1215-
if binary.exists() {
1216-
assert!(!arg.starts_with("--")); // not a flag
1217-
phase_runner(args, RunnerPhase::Cargo);
1218-
} else if arg.starts_with("--") {
1219-
phase_rustdoc(args);
1220-
} else {
1221-
show_error(format!(
1222-
"`cargo-miri` called with unexpected first argument `{}`; please only invoke this binary through `cargo miri`",
1223-
arg
1224-
));
1225-
}
1226-
} else {
1208+
let Some(first) = args.next() else {
12271209
show_error(format!(
12281210
"`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
1229-
));
1211+
))
1212+
};
1213+
match first.as_str() {
1214+
"miri" => phase_cargo_miri(args),
1215+
"runner" => phase_runner(args, RunnerPhase::Cargo),
1216+
arg if arg == env::var("RUSTC").unwrap() => {
1217+
// If the first arg is equal to the RUSTC env ariable (which should be set at this
1218+
// point), then we need to behave as rustc. This is the somewhat counter-intuitive
1219+
// behavior of having both RUSTC and RUSTC_WRAPPER set
1220+
// (see https://github.com/rust-lang/cargo/issues/10886).
1221+
phase_rustc(args, RustcPhase::Build)
1222+
}
1223+
_ => {
1224+
// Everything else must be rustdoc. But we need to get `first` "back onto the iterator",
1225+
// it is some part of the rustdoc invocation.
1226+
phase_rustdoc(iter::once(first).chain(args));
1227+
}
12301228
}
12311229
}

miri

-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR $RUSTFLAGS"
131131

132132
# Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
133133
build_sysroot() {
134-
# Build once, for the user to see.
135-
$CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
136-
# Call again, to just set env var.
137134
export MIRI_SYSROOT="$($CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
138135
}
139136

0 commit comments

Comments
 (0)