Skip to content

Commit cf96396

Browse files
committed
make miri script smarter: auto-determine MIRI_SYSROOT, handle MIRI_TEST_TARGET
1 parent eb85ced commit cf96396

File tree

4 files changed

+76
-37
lines changed

4 files changed

+76
-37
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,16 @@ version of `rustc` that, instead of compiling your code, runs it. It accepts
141141
all the same flags as `rustc` (though the ones only affecting code generation
142142
and linking obviously will have no effect) [and more][miri-flags].
143143

144-
Running the Miri driver requires some fiddling with environment variables, so the `miri` script helps you do that.
145-
For example, you can run the driver on a particular file by doing
144+
Running the Miri driver requires some fiddling with environment variables, so
145+
the `miri` script helps you do that. For example, you can run the driver on a
146+
particular file by doing
146147

147148
```sh
148-
./miri run tests/run-pass/format.rs # or whatever test you like
149+
./miri run tests/run-pass/format.rs
150+
./miri run tests/run-pass/hello.rs --target i686-unknown-linux-gnu
149151
```
150152

151-
and you can run the test suite using
153+
and you can run the test suite using:
152154

153155
```
154156
./miri test

miri

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,76 @@ SYSROOT=$(rustc --print sysroot)
77
# We enable line-only debuginfo for backtraces.
88
export RUSTFLAGS="-C link-args=-Wl,-rpath,$SYSROOT/lib/rustlib/$TARGET/lib -C debug-assertions -C debuginfo=1"
99

10-
COMMAND="$1"
11-
shift
10+
## Helper functions
1211

13-
case "$COMMAND" in
14-
install)
15-
exec cargo install --path "$(dirname "$0")" --force --locked --offline
16-
;;
17-
build|test|run)
18-
# Basic build
19-
cargo build --release
20-
21-
# We we want to just build, we are done.
22-
if [ "$COMMAND" = "build" ]; then exit 0; fi
12+
# Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
13+
build_sysroot() {
14+
# Build once, for the user to see.
15+
cargo run --release --bin cargo-miri -- miri setup "$@"
16+
# Call again, to just set env var.
17+
eval $(cargo run --release -q --bin cargo-miri -- miri setup --env "$@")
18+
export MIRI_SYSROOT
19+
}
2320

21+
# Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
22+
# locally built vs. distributed rustc.
23+
find_sysroot() {
2424
# Get ourselves a sysroot
2525
if [ -n "$MIRI_SYSROOT" ]; then
26-
# sysroot already set
26+
# Sysroot already set, use that.
2727
true
2828
elif echo "$SYSROOT" | egrep -q 'build/[^/]+/stage'; then
29-
# a local rustc build, assume we have a proper libstd in $SYSROOT
30-
true
29+
# A local rustc build.
30+
if [ -n "$MIRI_TEST_TARGET" ]; then
31+
# Foreign targets still need a build. Use the rustc sources.
32+
export XARGO_RUST_SRC="$SYSROOT/../../../src"
33+
build_sysroot --target "$MIRI_TEST_TARGET"
34+
else
35+
# Assume we have a proper host libstd in $SYSROOT.
36+
true
37+
fi
3138
else
32-
# we have to build a sysroot
33-
cargo run --release --bin cargo-miri -- miri setup
34-
export MIRI_SYSROOT=$HOME/.cache/miri/HOST
39+
# We have to build a sysroot either way.
40+
if [ -n "$MIRI_TEST_TARGET" ]; then
41+
build_sysroot --target "$MIRI_TEST_TARGET"
42+
else
43+
build_sysroot
44+
fi
3545
fi
46+
}
47+
48+
## Main
49+
50+
COMMAND="$1"
51+
shift
3652

53+
case "$COMMAND" in
54+
install)
55+
# "--locked" to respect the Cargo.lock file if it exists,
56+
# "--offline" to avoid querying the registry (for yanked packages).
57+
exec cargo "$COMMAND" --path "$(dirname "$0")" --force --locked --offline "$@"
58+
;;
59+
build)
60+
# Build, and let caller control flags.
61+
exec cargo "$COMMAND" --release "$@"
62+
;;
63+
test|run)
64+
# In "run" mode, scan for "--target" to set the "MIRI_TEST_TARGET" env var so
65+
# that we set the MIRI_SYSROOT up the right way.
66+
if [ "$COMMAND" = "run" ] && [ -z "$MIRI_TEST_TARGET" ]; then
67+
for ARG in "$@"; do
68+
if [ "$LAST_ARG" = "--target" ]; then
69+
# Found it!
70+
export MIRI_TEST_TARGET="$ARG"
71+
break
72+
fi
73+
LAST_ARG="$ARG"
74+
done
75+
fi
76+
# First build and get a sysroot.
77+
cargo build --release
78+
find_sysroot
79+
# Then run the actual command.
3780
exec cargo "$COMMAND" --release "$@"
3881
;;
3982
esac

src/bin/cargo-miri.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ path = "lib.rs"
243243
File::create(dir.join("lib.rs")).unwrap();
244244
// Run xargo.
245245
let target = get_arg_flag_value("--target");
246+
let print_env = !ask_user && has_arg_flag("--env"); // whether we just print the necessary environment variable
246247
let mut command = Command::new("xargo");
247248
command.arg("build").arg("-q")
248249
.current_dir(&dir)
@@ -265,7 +266,9 @@ path = "lib.rs"
265266
};
266267
let sysroot = if is_host { dir.join("HOST") } else { PathBuf::from(dir) };
267268
std::env::set_var("MIRI_SYSROOT", &sysroot);
268-
if !ask_user {
269+
if print_env {
270+
println!("MIRI_SYSROOT={}", sysroot.display());
271+
} else if !ask_user {
269272
println!("A libstd for Miri is now available in `{}`", sysroot.display());
270273
}
271274
}

travis.sh

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,27 @@ set -euo pipefail
33

44
# Determine configuration
55
if [ "$TRAVIS_OS_NAME" == osx ]; then
6-
MIRI_SYSROOT_BASE=~/Library/Caches/miri.miri.miri/
76
FOREIGN_TARGET=i686-apple-darwin
87
else
9-
MIRI_SYSROOT_BASE=~/.cache/miri/
108
FOREIGN_TARGET=i686-unknown-linux-gnu
119
fi
1210

1311
# Prepare
1412
echo "Build and install miri"
15-
cargo build --release --all-features --all-targets
16-
cargo install --all-features --force --path .
17-
echo
18-
19-
echo "Get ourselves a MIR-full libstd for the host and a foreign architecture"
20-
cargo miri setup
21-
cargo miri setup --target "$FOREIGN_TARGET"
13+
./miri build --all-features --all-targets
14+
./miri install
2215
echo
2316

2417
# Test
2518
function run_tests {
26-
cargo test --release --all-features
27-
test-cargo-miri/run-test.py
19+
./miri test
20+
test-cargo-miri/run-test.py
2821
}
2922

3023
echo "Test host architecture"
31-
export MIRI_SYSROOT="$MIRI_SYSROOT_BASE"/HOST
3224
run_tests
3325
echo
3426

3527
echo "Test foreign architecture ($FOREIGN_TARGET)"
36-
export MIRI_SYSROOT="$MIRI_SYSROOT_BASE" MIRI_TEST_TARGET="$FOREIGN_TARGET"
37-
run_tests
28+
MIRI_TEST_TARGET="$FOREIGN_TARGET" run_tests
3829
echo

0 commit comments

Comments
 (0)