]> git.lizzy.rs Git - rust.git/blob - miri
adjust for more backtrace pruning
[rust.git] / miri
1 #!/bin/bash
2 set -e
3 USAGE=$(cat <<"EOF"
4   COMMANDS
5
6 ./miri install <flags>:
7 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
9 working directory. However, the rustup toolchain when invoking `cargo miri`
10 needs to be the same one used for `./miri install`.
11
12 ./miri build <flags>:
13 Just build miri. <flags> are passed to `cargo build`.
14
15 ./miri check <flags>:
16 Just check miri. <flags> are passed to `cargo check`.
17
18 ./miri test <flags>:
19 Build miri, set up a sysroot and then run the test suite. <flags> are passed
20 to the final `cargo test` invocation.
21
22 ./miri run <flags>:
23 Build miri, set up a sysroot and then run the driver with the given <flags>.
24 (Also respects MIRIFLAGS environment variable.)
25
26 ./miri fmt <flags>:
27 Format all sources and tests. <flags> are passed to `rustfmt`.
28
29 ./miri clippy <flags>:
30 Runs clippy on all sources. <flags> are passed to `cargo clippy`.
31
32 ./miri many-seeds <command>:
33 Runs <command> over and over again with different seeds for Miri. The MIRIFLAGS
34 variable is set to its original value appended with ` -Zmiri-seed=$SEED` for
35 many different seeds.
36
37 ./miri bench <benches>:
38 Runs the benchmarks from bench-cargo-miri in hyperfine. hyperfine needs to be installed.
39 <benches> can explicitly list the benchmarks to run; by default, all of them are run.
40
41   ENVIRONMENT VARIABLES
42
43 MIRI_SYSROOT:
44 If already set, the "sysroot setup" step is skipped.
45
46 CARGO_EXTRA_FLAGS:
47 Pass extra flags to all cargo invocations.
48 EOF
49 )
50
51 ## We need to know where we are.
52 # macOS does not have a useful readlink/realpath so we have to use Python instead...
53 MIRIDIR=$(python3 -c 'import os, sys; print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0")
54
55 ## Run the auto-things.
56 if [ -z "$MIRI_AUTO_OPS" ]; then
57     export MIRI_AUTO_OPS=42
58
59     # Run this first, so that the toolchain doesn't change after
60     # other code has run.
61     if [ -f "$MIRIDIR/.auto-everything" ] || [ -f "$MIRIDIR/.auto-toolchain" ] ; then
62         (cd "$MIRIDIR" && ./rustup-toolchain)
63     fi
64
65     if [ -f "$MIRIDIR/.auto-everything" ] || [ -f "$MIRIDIR/.auto-fmt" ] ; then
66         $0 fmt
67     fi
68
69     if [ -f "$MIRIDIR/.auto-everything" ] || [ -f "$MIRIDIR/.auto-clippy" ] ; then
70         $0 clippy -- -D warnings
71     fi
72 fi
73
74 ## Determine command and toolchain.
75 COMMAND="$1"
76 [ $# -gt 0 ] && shift
77 # Doing this *after* auto-toolchain logic above, since that might change the toolchain.
78 TOOLCHAIN=$(cd "$MIRIDIR"; rustup show active-toolchain | head -n 1 | cut -d ' ' -f 1)
79
80 ## Handle some commands early, since they should *not* alter the environment.
81 case "$COMMAND" in
82 many-seeds)
83     for SEED in $({ echo obase=16; seq 0 255; } | bc); do
84         echo "Trying seed: $SEED"
85         MIRIFLAGS="$MIRIFLAGS -Zmiri-seed=$SEED" $@ || { echo "Failing seed: $SEED"; break; }
86     done
87     exit 0
88     ;;
89 bench)
90     # Make sure we have an up-to-date Miri installed
91     "$0" install
92     # Run the requested benchmarks
93     if [ -z "$@" ]; then
94         BENCHES=( $(ls "$MIRIDIR/bench-cargo-miri" ) )
95     else
96         BENCHES=("$@")
97     fi
98     for BENCH in "${BENCHES[@]}"; do
99         hyperfine -w 1 -m 5 --shell=none "cargo +$TOOLCHAIN miri run --manifest-path bench-cargo-miri/$BENCH/Cargo.toml"
100     done
101     exit 0
102     ;;
103 esac
104
105 ## Prepare the environment
106 # Determine some toolchain properties
107 TARGET=$(rustc +$TOOLCHAIN --version --verbose | grep "^host:" | cut -d ' ' -f 2)
108 SYSROOT=$(rustc +$TOOLCHAIN --print sysroot)
109 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
110 if ! test -d "$LIBDIR"; then
111     echo "Something went wrong determining the library dir."
112     echo "I got $LIBDIR but that does not exist."
113     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
114     exit 2
115 fi
116
117 # Prepare flags for cargo and rustc.
118 CARGO="cargo +$TOOLCHAIN"
119 # Share target dir between `miri` and `cargo-miri`.
120 if [ -z "$CARGO_TARGET_DIR" ]; then
121     export CARGO_TARGET_DIR="$MIRIDIR/target"
122 fi
123 # We configure dev builds to not be unusably slow.
124 if [ -z "$CARGO_PROFILE_DEV_OPT_LEVEL" ]; then
125     export CARGO_PROFILE_DEV_OPT_LEVEL=2
126 fi
127 # We set the rpath so that Miri finds the private rustc libraries it needs.
128 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR $RUSTFLAGS"
129
130 ## Helper functions
131
132 # Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
133 build_sysroot() {
134     if ! MIRI_SYSROOT="$($CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"; then
135         echo "'cargo miri setup' failed"
136         exit 1
137     fi
138     export MIRI_SYSROOT
139 }
140
141 # Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
142 # locally built vs. distributed rustc.
143 find_sysroot() {
144     if [ -n "$MIRI_SYSROOT" ]; then
145         # Sysroot already set, use that.
146         return 0
147     fi
148     # We need to build a sysroot.
149     if [ -n "$MIRI_TEST_TARGET" ]; then
150         build_sysroot --target "$MIRI_TEST_TARGET"
151     else
152         build_sysroot
153     fi
154 }
155
156 ## Main
157
158 # Run command.
159 case "$COMMAND" in
160 install)
161     # "--locked" to respect the Cargo.lock file if it exists.
162     $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR" --force --locked "$@"
163     $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked "$@"
164     ;;
165 check)
166     # Check, and let caller control flags.
167     $CARGO check $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
168     $CARGO check $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
169     ;;
170 build)
171     # Build, and let caller control flags.
172     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
173     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
174     ;;
175 test|bless)
176     # First build and get a sysroot.
177     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
178     find_sysroot
179     if [ "$COMMAND" = "bless" ]; then
180         export MIRI_BLESS="Gesundheit"
181     fi
182     # Then test, and let caller control flags.
183     # Only in root project and ui_test as `cargo-miri` has no tests.
184     $CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
185     $CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml "$@"
186     ;;
187 run)
188     # Scan for "--target" to overwrite the "MIRI_TEST_TARGET" env var so
189     # that we set the MIRI_SYSROOT up the right way.
190     FOUND_TARGET_OPT=0
191     for ARG in "$@"; do
192         if [ "$LAST_ARG" = "--target" ]; then
193             # Found it!
194             export MIRI_TEST_TARGET="$ARG"
195             FOUND_TARGET_OPT=1
196             break
197         fi
198         LAST_ARG="$ARG"
199     done
200     if [ "$FOUND_TARGET_OPT" = "0" ] && [ -n "$MIRI_TEST_TARGET" ]; then
201         # Make sure Miri actually uses this target.
202         MIRIFLAGS="$MIRIFLAGS --target $MIRI_TEST_TARGET"
203     fi
204     # First build and get a sysroot.
205     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
206     find_sysroot
207     # Then run the actual command.
208     exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
209     ;;
210 fmt)
211     find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
212         | xargs rustfmt +$TOOLCHAIN --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
213     ;;
214 clippy)
215     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
216     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml --all-targets "$@"
217     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
218     ;;
219 *)
220     if [ -n "$COMMAND" ]; then
221       echo "Unknown command: $COMMAND"
222       echo
223     fi
224     echo "$USAGE"
225     exit 1
226 esac