Skip to content

Commit a1fabb9

Browse files
committed
make miri script work from other working directories
1 parent f9e0cf4 commit a1fabb9

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

miri

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ USAGE=$(cat <<"EOF"
55
66
./miri install <flags>:
77
Installs the miri driver and cargo-miri. <flags> are passed to `cargo
8-
install`. Sets up the rpath such that the installed binary should work in any
8+
install`. Sets up the rpath such that the installed binary should work in any
99
working directory.
1010
1111
./miri build <flags>:
12-
Just build miri. <flags> are passed to `cargo build`.
12+
Just build miri. <flags> are passed to `cargo build`.
1313
1414
./miri check <flags>:
15-
Just check miri. <flags> are passed to `cargo check`.
15+
Just check miri. <flags> are passed to `cargo check`.
1616
1717
./miri test <flags>:
1818
Build miri, set up a sysroot and then run the test suite. <flags> are passed
@@ -26,10 +26,10 @@ The commands above also exist in a "-debug" variant (e.g. "./miri run-debug
2626
times and slower execution times.
2727
2828
./miri fmt <flags>:
29-
Format all sources and tests. <flags> are passed to `rustfmt`.
29+
Format all sources and tests. <flags> are passed to `rustfmt`.
3030
3131
./miri clippy <flags>:
32-
Format all sources and tests. <flags> are passed to `cargo clippy`.
32+
Format all sources and tests. <flags> are passed to `cargo clippy`.
3333
3434
ENVIRONMENT VARIABLES
3535
@@ -42,17 +42,23 @@ EOF
4242
)
4343

4444
## Preparation
45-
TARGET=$(rustc --version --verbose | grep "^host:" | cut -d ' ' -f 2)
46-
SYSROOT=$(rustc --print sysroot)
47-
LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
4845
# macOS does not have a useful readlink/realpath so we have to use Python instead...
4946
MIRIDIR=$(dirname "$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$0")")
47+
# Determine toolchain *in the Miri dir* and use that.
48+
TOOLCHAIN=$(cd "$MIRIDIR"; rustup show active-toolchain | head -n 1 | cut -d ' ' -f 1)
49+
# Determine some toolchain properties
50+
TARGET=$(rustc +$TOOLCHAIN --version --verbose | grep "^host:" | cut -d ' ' -f 2)
51+
SYSROOT=$(rustc +$TOOLCHAIN --print sysroot)
52+
LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
53+
5054
if ! test -d "$LIBDIR"; then
5155
echo "Something went wrong determining the library dir."
5256
echo "I got $LIBDIR but that does not exist."
5357
echo "Please report a bug at https://github.com/rust-lang/miri/issues."
5458
exit 2
5559
fi
60+
61+
CARGO="cargo +$TOOLCHAIN"
5662
if [ -z "$CARGO_INCREMENTAL" ]; then
5763
# Default CARGO_INCREMENTAL to 1.
5864
export CARGO_INCREMENTAL=1
@@ -68,15 +74,15 @@ export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debugin
6874

6975
## Helper functions
7076

71-
# Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
77+
# Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
7278
build_sysroot() {
7379
# Build once, for the user to see.
74-
cargo run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
80+
$CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
7581
# Call again, to just set env var.
76-
export MIRI_SYSROOT="$(cargo run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
82+
export MIRI_SYSROOT="$($CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
7783
}
7884

79-
# Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
85+
# Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
8086
# locally built vs. distributed rustc.
8187
find_sysroot() {
8288
if [ -n "$MIRI_SYSROOT" ]; then
@@ -116,22 +122,22 @@ case "$COMMAND" in
116122
install|install-debug)
117123
# "--locked" to respect the Cargo.lock file if it exists,
118124
# "--offline" to avoid querying the registry (for yanked packages).
119-
cargo install $CARGO_INSTALL_FLAGS --path "$MIRIDIR" --force --locked --offline "$@"
120-
cargo install $CARGO_INSTALL_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked --offline "$@"
125+
$CARGO install $CARGO_INSTALL_FLAGS --path "$MIRIDIR" --force --locked --offline "$@"
126+
$CARGO install $CARGO_INSTALL_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked --offline "$@"
121127
;;
122128
check|check-debug)
123129
# Check, and let caller control flags.
124-
cargo check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
125-
cargo check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
130+
$CARGO check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
131+
$CARGO check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
126132
;;
127133
build|build-debug)
128134
# Build, and let caller control flags.
129-
cargo build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
130-
cargo build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
135+
$CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
136+
$CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
131137
;;
132138
test|test-debug|bless|bless-debug)
133139
# First build and get a sysroot.
134-
cargo build $CARGO_BUILD_FLAGS
140+
$CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
135141
find_sysroot
136142
case "$COMMAND" in
137143
bless|bless-debug)
@@ -140,8 +146,8 @@ test|test-debug|bless|bless-debug)
140146
esac
141147
# Then test, and let caller control flags.
142148
# Only in root project and ui_test as `cargo-miri` has no tests.
143-
cargo test $CARGO_BUILD_FLAGS "$@"
144-
cargo test $CARGO_BUILD_FLAGS --manifest-path ui_test/Cargo.toml "$@"
149+
$CARGO test $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
150+
$CARGO test $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml "$@"
145151
;;
146152
run|run-debug)
147153
# Scan for "--target" to set the "MIRI_TEST_TARGET" env var so
@@ -157,19 +163,19 @@ run|run-debug)
157163
done
158164
fi
159165
# First build and get a sysroot.
160-
cargo build $CARGO_BUILD_FLAGS
166+
$CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
161167
find_sysroot
162168
# Then run the actual command.
163-
exec cargo run $CARGO_BUILD_FLAGS -- --sysroot "$MIRI_SYSROOT" "$@"
169+
exec $CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --sysroot "$MIRI_SYSROOT" "$@"
164170
;;
165171
fmt)
166172
find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
167-
| xargs rustfmt --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
173+
| xargs rustfmt +$TOOLCHAIN --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
168174
;;
169175
clippy)
170-
cargo clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
171-
cargo clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml --all-targets "$@"
172-
cargo clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
176+
$CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
177+
$CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml --all-targets "$@"
178+
$CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
173179
;;
174180
*)
175181
if [ -n "$COMMAND" ]; then

0 commit comments

Comments
 (0)