Skip to content

Commit 42d9b68

Browse files
committed
Auto merge of rust-lang#3588 - RossSmyth:CliTarget, r=RalfJung
Allow test targets to be set via CLI args Fixes rust-lang#3584 I'm not a pro shell script reader as I am a Windows user, but we shall see if the CI script broke.
2 parents 2e1d417 + d43cb71 commit 42d9b68

File tree

5 files changed

+80
-30
lines changed

5 files changed

+80
-30
lines changed

src/tools/miri/CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ For example:
7272

7373
You can (cross-)run the entire test suite using:
7474

75-
```
75+
```sh
7676
./miri test
77-
MIRI_TEST_TARGET=i686-unknown-linux-gnu ./miri test
77+
./miri test --target i686-unknown-linux-gnu
7878
```
7979

8080
`./miri test FILTER` only runs those tests that contain `FILTER` in their filename (including the
81-
base directory, e.g. `./miri test fail` will run all compile-fail tests). These filters are passed
82-
to `cargo test`, so for multiple filters you need to use `./miri test -- FILTER1 FILTER2`.
81+
base directory, e.g. `./miri test fail` will run all compile-fail tests). Multiple filters
82+
are supported: `./miri test FILTER1 FILTER2`.
8383

8484
#### Fine grained logging
8585

src/tools/miri/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ by all intended entry points, i.e. `cargo miri` and `./miri {test,run}`):
464464
setup -- only set this if you do not want to use the automatically created sysroot. When invoking
465465
`cargo miri setup`, this indicates where the sysroot will be put.
466466
* `MIRI_TEST_TARGET` (recognized by `./miri {test,run}`) indicates which target
467-
architecture to test against. `miri` and `cargo miri` accept the `--target` flag for the same
467+
architecture to test against. The `--target` flag may be used for the same
468468
purpose.
469469
* `MIRI_TEST_THREADS` (recognized by `./miri test`): set the number of threads to use for running tests.
470470
By default, the number of cores is used.

src/tools/miri/ci/ci.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function run_tests {
5959
# them. Also error locations change so we don't run the failing tests.
6060
# We explicitly enable debug-assertions here, they are disabled by -O but we have tests
6161
# which exist to check that we panic on debug assertion failures.
62-
time MIRIFLAGS="${MIRIFLAGS-} -O -Zmir-opt-level=4 -Cdebug-assertions=yes" MIRI_SKIP_UI_CHECKS=1 ./miri test -- tests/{pass,panic}
62+
time MIRIFLAGS="${MIRIFLAGS-} -O -Zmir-opt-level=4 -Cdebug-assertions=yes" MIRI_SKIP_UI_CHECKS=1 ./miri test tests/{pass,panic}
6363
fi
6464
if [ -n "${MANY_SEEDS-}" ]; then
6565
# Also run some many-seeds tests.
@@ -107,7 +107,7 @@ function run_tests_minimal {
107107
exit 1
108108
fi
109109

110-
time ./miri test -- "$@"
110+
time ./miri test "$@"
111111

112112
# Ensure that a small smoke test of cargo-miri works.
113113
time cargo miri run --manifest-path test-cargo-miri/no-std-smoke/Cargo.toml --target ${MIRI_TEST_TARGET-$HOST_TARGET}

src/tools/miri/miri-script/src/commands.rs

+38-17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const JOSH_PORT: &str = "42042";
2323

2424
impl MiriEnv {
2525
/// Returns the location of the sysroot.
26-
fn build_miri_sysroot(&mut self, quiet: bool) -> Result<PathBuf> {
26+
///
27+
/// If the target is None the sysroot will be built for the host machine.
28+
fn build_miri_sysroot(&mut self, quiet: bool, target: Option<&str>) -> Result<PathBuf> {
2729
if let Some(miri_sysroot) = self.sh.var_os("MIRI_SYSROOT") {
2830
// Sysroot already set, use that.
2931
return Ok(miri_sysroot.into());
@@ -35,26 +37,27 @@ impl MiriEnv {
3537
self.build(path!(self.miri_dir / "Cargo.toml"), &[], quiet)?;
3638
self.build(&manifest_path, &[], quiet)?;
3739

38-
let target = &match self.sh.var("MIRI_TEST_TARGET") {
39-
Ok(target) => vec!["--target".into(), target],
40-
Err(_) => vec![],
41-
};
40+
let target_flag =
41+
&if let Some(target) = target { vec!["--target", target] } else { vec![] };
42+
4243
if !quiet {
43-
match self.sh.var("MIRI_TEST_TARGET") {
44-
Ok(target) => eprintln!("$ (building Miri sysroot for {target})"),
45-
Err(_) => eprintln!("$ (building Miri sysroot)"),
44+
if let Some(target) = target {
45+
eprintln!("$ (building Miri sysroot for {target})");
46+
} else {
47+
eprintln!("$ (building Miri sysroot)");
4648
}
4749
}
50+
4851
let output = cmd!(self.sh,
4952
"cargo +{toolchain} --quiet run {cargo_extra_flags...} --manifest-path {manifest_path} --
50-
miri setup --print-sysroot {target...}"
53+
miri setup --print-sysroot {target_flag...}"
5154
).read();
5255
let Ok(output) = output else {
5356
// Run it again (without `--print-sysroot` or `--quiet`) so the user can see the error.
5457
cmd!(
5558
self.sh,
5659
"cargo +{toolchain} run {cargo_extra_flags...} --manifest-path {manifest_path} --
57-
miri setup {target...}"
60+
miri setup {target_flag...}"
5861
)
5962
.run()
6063
.with_context(|| "`cargo miri setup` failed")?;
@@ -161,7 +164,7 @@ impl Command {
161164
Command::Install { flags } => Self::install(flags),
162165
Command::Build { flags } => Self::build(flags),
163166
Command::Check { flags } => Self::check(flags),
164-
Command::Test { bless, flags } => Self::test(bless, flags),
167+
Command::Test { bless, flags, target } => Self::test(bless, flags, target),
165168
Command::Run { dep, verbose, many_seeds, flags } =>
166169
Self::run(dep, verbose, many_seeds, flags),
167170
Command::Fmt { flags } => Self::fmt(flags),
@@ -446,16 +449,23 @@ impl Command {
446449
Ok(())
447450
}
448451

449-
fn test(bless: bool, flags: Vec<OsString>) -> Result<()> {
452+
fn test(bless: bool, flags: Vec<OsString>, target: Option<String>) -> Result<()> {
450453
let mut e = MiriEnv::new()?;
454+
455+
if let Some(target) = target.as_deref() {
456+
// Tell the sysroot which target to test.
457+
e.sh.set_var("MIRI_TEST_TARGET", target);
458+
}
459+
451460
// Prepare a sysroot.
452-
e.build_miri_sysroot(/* quiet */ false)?;
461+
e.build_miri_sysroot(/* quiet */ false, target.as_deref())?;
453462

454463
// Then test, and let caller control flags.
455464
// Only in root project as `cargo-miri` has no tests.
456465
if bless {
457466
e.sh.set_var("RUSTC_BLESS", "Gesundheit");
458467
}
468+
459469
e.test(path!(e.miri_dir / "Cargo.toml"), &flags)?;
460470
Ok(())
461471
}
@@ -476,14 +486,24 @@ impl Command {
476486
.take_while(|arg| *arg != "--")
477487
.tuple_windows()
478488
.find(|(first, _)| *first == "--target");
479-
if let Some((_, target)) = target {
489+
490+
let target_triple = if let Some((_, target)) = target {
480491
// Found it!
481492
e.sh.set_var("MIRI_TEST_TARGET", target);
493+
494+
let triple =
495+
target.clone().into_string().map_err(|_| anyhow!("target triple is not UTF-8"))?;
496+
Some(triple)
482497
} else if let Ok(target) = std::env::var("MIRI_TEST_TARGET") {
483498
// Convert `MIRI_TEST_TARGET` into `--target`.
484499
flags.push("--target".into());
485-
flags.push(target.into());
486-
}
500+
flags.push(target.clone().into());
501+
502+
Some(target)
503+
} else {
504+
None
505+
};
506+
487507
// Scan for "--edition", set one ourselves if that flag is not present.
488508
let have_edition =
489509
flags.iter().take_while(|arg| *arg != "--").any(|arg| *arg == "--edition");
@@ -492,7 +512,8 @@ impl Command {
492512
}
493513

494514
// Prepare a sysroot, and add it to the flags.
495-
let miri_sysroot = e.build_miri_sysroot(/* quiet */ !verbose)?;
515+
let miri_sysroot =
516+
e.build_miri_sysroot(/* quiet */ !verbose, target_triple.as_deref())?;
496517
flags.push("--sysroot".into());
497518
flags.push(miri_sysroot.into());
498519

src/tools/miri/miri-script/src/main.rs

+35-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub enum Command {
3333
bless: bool,
3434
/// Flags that are passed through to `cargo test`.
3535
flags: Vec<OsString>,
36+
/// The cross-interpretation target.
37+
/// If none then the host is the target.
38+
target: Option<String>,
3639
},
3740
/// Build miri, set up a sysroot and then run the driver with the given <flags>.
3841
/// (Also respects MIRIFLAGS environment variable.)
@@ -84,9 +87,9 @@ Just build miri. <flags> are passed to `cargo build`.
8487
./miri check <flags>:
8588
Just check miri. <flags> are passed to `cargo check`.
8689
87-
./miri test [--bless] <flags>:
90+
./miri test [--bless] [--target <target>] <flags>:
8891
Build miri, set up a sysroot and then run the test suite. <flags> are passed
89-
to the final `cargo test` invocation.
92+
to the test harness.
9093
9194
./miri run [--dep] [-v|--verbose] [--many-seeds|--many-seeds=..to|--many-seeds=from..to] <flags>:
9295
Build miri, set up a sysroot and then run the driver with the given <flags>.
@@ -147,12 +150,38 @@ fn main() -> Result<()> {
147150
Some("build") => Command::Build { flags: args.collect() },
148151
Some("check") => Command::Check { flags: args.collect() },
149152
Some("test") => {
150-
let bless = args.peek().is_some_and(|a| a.to_str() == Some("--bless"));
151-
if bless {
152-
// Consume the flag.
153+
let mut target = std::env::var("MIRI_TEST_TARGET").ok();
154+
let mut bless = false;
155+
156+
while let Some(arg) = args.peek().and_then(|s| s.to_str()) {
157+
match arg {
158+
"--bless" => bless = true,
159+
"--target" => {
160+
// Skip "--target"
161+
args.next().unwrap();
162+
163+
// Check that there is a target triple, and that it is unicode.
164+
target = if let Some(value) = args.peek() {
165+
let target_str = value
166+
.clone()
167+
.into_string()
168+
.map_err(|_| anyhow!("target triple is not UTF-8"))?;
169+
Some(target_str)
170+
} else {
171+
bail!("no target triple found")
172+
}
173+
}
174+
// Only parse the leading flags.
175+
_ => break,
176+
}
177+
178+
// Consume the flag, look at the next one.
153179
args.next().unwrap();
154180
}
155-
Command::Test { bless, flags: args.collect() }
181+
182+
// Prepend a "--" so that the rest of the arguments are passed to the test driver.
183+
let args = std::iter::once(OsString::from("--")).chain(args);
184+
Command::Test { bless, flags: args.collect(), target }
156185
}
157186
Some("run") => {
158187
let mut dep = false;

0 commit comments

Comments
 (0)