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