diff --git a/README.md b/README.md index 130072825e1c..090694128e3a 100644 --- a/README.md +++ b/README.md @@ -250,9 +250,12 @@ Several `-Z` flags are relevant for Miri: Moreover, Miri recognizes some environment variables: -* `MIRI_SYSROOT` (recognized by `miri`, `cargo miri` and the test suite) - indicates the sysroot to use. -* `MIRI_TARGET` (recognized by the test suite) indicates which target +* `MIRI_LOG`, `MIRI_BACKTRACE` control logging and backtrace printing during + Miri executions, also [see above][testing-miri]. +* `MIRI_SYSROOT` (recognized by `cargo miri` and the test suite) + indicates the sysroot to use. To do the same thing with `miri` + directly, use the `--sysroot` flag. +* `MIRI_TEST_TARGET` (recognized by the test suite) indicates which target architecture to test against. `miri` and `cargo miri` accept the `--target` flag for the same purpose. diff --git a/miri b/miri index 2181403b7bde..bb6441bd97f9 100755 --- a/miri +++ b/miri @@ -140,7 +140,7 @@ run|run-debug) cargo build $CARGO_BUILD_FLAGS find_sysroot # Then run the actual command. - exec cargo run $CARGO_BUILD_FLAGS "$@" + exec cargo run $CARGO_BUILD_FLAGS -- --sysroot "$MIRI_SYSROOT" "$@" ;; *) echo "Unknown command: $COMMAND" diff --git a/src/bin/cargo-miri.rs b/src/bin/cargo-miri.rs index 53b849a1a70c..db53ca6ce7c1 100644 --- a/src/bin/cargo-miri.rs +++ b/src/bin/cargo-miri.rs @@ -125,7 +125,6 @@ fn list_targets() -> impl Iterator { fn test_sysroot_consistency() { fn get_sysroot(mut cmd: Command) -> PathBuf { let out = cmd.arg("--print").arg("sysroot") - .env_remove("MIRI_SYSROOT") // We want to test their "native" sysroot, not the manually set one .output().expect("Failed to run rustc to get sysroot info"); assert!(out.status.success(), "Bad statuc code when getting sysroot info"); let sysroot = out.stdout.lines().nth(0) @@ -298,7 +297,7 @@ path = "lib.rs" Some(target) => target == rustc_version::version_meta().unwrap().host, }; let sysroot = if is_host { dir.join("HOST") } else { PathBuf::from(dir) }; - std::env::set_var("MIRI_SYSROOT", &sysroot); + std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags if print_env { println!("MIRI_SYSROOT={}", sysroot.display()); } else if !ask_user { @@ -425,9 +424,9 @@ fn inside_cargo_rustc() { let rustc_args = std::env::args().skip(2); // skip `cargo rustc` let mut args: Vec = rustc_args - .chain(Some("--sysroot".to_owned())) - .chain(Some(sysroot)) - .collect(); + .chain(Some("--sysroot".to_owned())) + .chain(Some(sysroot)) + .collect(); args.splice(0..0, miri::miri_default_args().iter().map(ToString::to_string)); // See if we can find the `cargo-miri` markers. Those only get added to the binary we want to @@ -458,7 +457,6 @@ fn inside_cargo_rustc() { } else { Command::new("rustc") }; - command.env_remove("MIRI_SYSROOT"); // we already set the --sysroot flag command.args(&args); if has_arg_flag("-v") { eprintln!("+ {:?}", command); diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 660f1bec2d62..60f4751db8d2 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -165,12 +165,7 @@ fn main() { // Determine sysroot. let sysroot_flag = "--sysroot".to_string(); - if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") { - // MIRI_SYSROOT takes priority. rustc will ensure for us that this errors if there - // already is a "--sysroot" flag (because now there would be two). - rustc_args.push(sysroot_flag); - rustc_args.push(sysroot); - } else if !rustc_args.contains(&sysroot_flag) { + if !rustc_args.contains(&sysroot_flag) { // We need to *always* set a --sysroot, as the "default" rustc uses is // somewhere in the directory miri was built in. // If neither MIRI_SYSROOT nor --sysroot are given, fall back to env diff --git a/tests/compiletest.rs b/tests/compiletest.rs index 7f2c8966472f..d59be08c8e00 100644 --- a/tests/compiletest.rs +++ b/tests/compiletest.rs @@ -25,7 +25,15 @@ fn rustc_lib_path() -> PathBuf { option_env!("RUSTC_LIB_PATH").unwrap().into() } -fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp { +fn run_tests(mode: &str, path: &str, target: &str, mut flags: Vec) { + // Some flags we always want. + flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs + flags.push("--edition 2018".to_owned()); + if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") { + flags.push(format!("--sysroot {}", sysroot)); + } + + // The rest of the configuration. let mut config = compiletest::Config::default().tempdir(); config.mode = mode.parse().expect("Invalid mode"); config.rustc_path = miri_path(); @@ -35,7 +43,10 @@ fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp { } config.filter = env::args().nth(1); config.host = get_host(); - config + config.src_base = PathBuf::from(path); + config.target = target.to_owned(); + config.target_rustcflags = Some(flags.join(" ")); + compiletest::run_tests(&config); } fn compile_fail(path: &str, target: &str, opt: bool) { @@ -48,8 +59,6 @@ fn compile_fail(path: &str, target: &str, opt: bool) { ).green().bold()); let mut flags = Vec::new(); - flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs - flags.push("--edition 2018".to_owned()); if opt { // Optimizing too aggressivley makes UB detection harder, but test at least // the default value. @@ -57,11 +66,7 @@ fn compile_fail(path: &str, target: &str, opt: bool) { flags.push("-Zmir-opt-level=1".to_owned()); } - let mut config = mk_config("compile-fail"); - config.src_base = PathBuf::from(path); - config.target = target.to_owned(); - config.target_rustcflags = Some(flags.join(" ")); - compiletest::run_tests(&config); + run_tests("compile-fail", path, target, flags); } fn miri_pass(path: &str, target: &str, opt: bool) { @@ -74,17 +79,11 @@ fn miri_pass(path: &str, target: &str, opt: bool) { ).green().bold()); let mut flags = Vec::new(); - flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs - flags.push("--edition 2018".to_owned()); if opt { flags.push("-Zmir-opt-level=3".to_owned()); } - let mut config = mk_config("ui"); - config.src_base = PathBuf::from(path); - config.target = target.to_owned(); - config.target_rustcflags = Some(flags.join(" ")); - compiletest::run_tests(&config); + run_tests("ui", path, target, flags); } fn get_host() -> String {