Skip to content

Commit 2d33d7b

Browse files
committed
Run tests for all specified targets
Currently cargo-miri uses the first target specified in the command line. However, when multiple targets are specified in a `cargo build` invocation, cargo will build for all of them. Miri should match this behaviour to reduce surprises. Fixes: #3460
1 parent b0d791d commit 2d33d7b

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/tools/miri/cargo-miri/src/phases.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,17 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
104104
miri_for_host()
105105
)
106106
});
107-
let host = &rustc_version.host;
108-
let target = get_arg_flag_value("--target");
109-
let target = target.as_ref().unwrap_or(host);
107+
let mut targets = get_arg_flag_values("--target").collect::<Vec<_>>();
108+
// If `targets` is empty, we need to add a `--target $HOST` flag ourselves, and also ensure
109+
// that the host target is indeed setup.
110+
let target_flag = if targets.is_empty() {
111+
let host = &rustc_version.host;
112+
targets.push(host.clone());
113+
Some(host)
114+
} else {
115+
// We don't need to add a `--target` flag, we just forward the user's flags.
116+
None
117+
};
110118

111119
// If cleaning the target directory & sysroot cache,
112120
// delete them then exit. There is no reason to setup a new
@@ -118,8 +126,11 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
118126
return;
119127
}
120128

121-
// We always setup.
122-
let miri_sysroot = setup(&subcommand, target, &rustc_version, verbose, quiet);
129+
for target in &targets {
130+
// We always setup.
131+
setup(&subcommand, target.as_str(), &rustc_version, verbose, quiet);
132+
}
133+
let miri_sysroot = get_sysroot_dir();
123134

124135
// Invoke actual cargo for the job, but with different flags.
125136
// We re-use `cargo test` and `cargo run`, which makes target and binary handling very easy but
@@ -155,10 +166,9 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
155166
// This is needed to make the `target.runner` settings do something,
156167
// and it later helps us detect which crates are proc-macro/build-script
157168
// (host crates) and which crates are needed for the program itself.
158-
if get_arg_flag_value("--target").is_none() {
159-
// No target given. Explicitly pick the host.
169+
if let Some(target_flag) = target_flag {
160170
cmd.arg("--target");
161-
cmd.arg(host);
171+
cmd.arg(target_flag);
162172
}
163173

164174
// Set ourselves as runner for al binaries invoked by cargo.

src/tools/miri/ci/ci.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ function run_tests {
4141
if [ -n "${TEST_TARGET-}" ]; then
4242
begingroup "Testing foreign architecture $TEST_TARGET"
4343
TARGET_FLAG="--target $TEST_TARGET"
44+
MULTI_TARGET_FLAG=""
4445
else
4546
begingroup "Testing host architecture"
4647
TARGET_FLAG=""
48+
MULTI_TARGET_FLAG="--multi-target"
4749
fi
4850

4951
## ui test suite
@@ -93,7 +95,7 @@ function run_tests {
9395
echo 'build.rustc-wrapper = "thisdoesnotexist"' > .cargo/config.toml
9496
fi
9597
# Run the actual test
96-
time ${PYTHON} test-cargo-miri/run-test.py $TARGET_FLAG
98+
time ${PYTHON} test-cargo-miri/run-test.py $TARGET_FLAG $MULTI_TARGET_FLAG
9799
# Clean up
98100
unset RUSTC MIRI
99101
rm -rf .cargo

src/tools/miri/test-cargo-miri/run-test.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ def fail(msg):
2222
print("\nTEST FAIL: {}".format(msg))
2323
sys.exit(1)
2424

25-
def cargo_miri(cmd, quiet = True):
25+
def cargo_miri(cmd, quiet = True, targets = None):
2626
args = ["cargo", "miri", cmd] + CARGO_EXTRA_FLAGS
2727
if quiet:
2828
args += ["-q"]
29-
if ARGS.target:
29+
30+
if targets is not None:
31+
for target in targets:
32+
args.extend(("--target", target))
33+
elif ARGS.target is not None:
3034
args += ["--target", ARGS.target]
35+
3136
return args
3237

3338
def normalize_stdout(str):
@@ -186,10 +191,21 @@ def test_cargo_miri_test():
186191
default_ref, "test.stderr-empty.ref",
187192
env={'MIRIFLAGS': "-Zmiri-permissive-provenance"},
188193
)
194+
if ARGS.multi_target:
195+
test_cargo_miri_multi_target()
196+
197+
198+
def test_cargo_miri_multi_target():
199+
test("`cargo miri test` (multiple targets)",
200+
cargo_miri("test", targets = ["aarch64-unknown-linux-gnu", "s390x-unknown-linux-gnu"]),
201+
"test.multiple_targets.stdout.ref", "test.stderr-empty.ref",
202+
env={'MIRIFLAGS': "-Zmiri-permissive-provenance"},
203+
)
189204

190205
args_parser = argparse.ArgumentParser(description='`cargo miri` testing')
191206
args_parser.add_argument('--target', help='the target to test')
192207
args_parser.add_argument('--bless', help='bless the reference files', action='store_true')
208+
args_parser.add_argument('--multi-target', help='run tests related to multiple targets', action='store_true')
193209
ARGS = args_parser.parse_args()
194210

195211
os.chdir(os.path.dirname(os.path.realpath(__file__)))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
running 2 tests
3+
..
4+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
5+
6+
7+
running 2 tests
8+
..
9+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
10+
11+
imported main
12+
imported main
13+
14+
running 6 tests
15+
...i..
16+
test result: ok. 5 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in $TIME
17+
18+
19+
running 6 tests
20+
...i..
21+
test result: ok. 5 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in $TIME
22+

0 commit comments

Comments
 (0)