]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/miri
Rollup merge of #104404 - GuillaumeGomez:fix-missing-minification, r=notriddle
[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 ./miri rustc-pull:
46 Pull and merge Miri changes from the rustc repo.
47
48 ./miri rustc-push <github user> <branch>:
49 Push Miri changes back to the rustc repo. This will update the 'master' branch
50 in the Rust fork of the given user to upstream. It will also pull a copy of the
51 rustc history into the Miri repo, unless you set the RUSTC_GIT env var to an
52 existing clone of the rustc repo.
53
54 ./miri toolchain <commit> <flags>:
55 Update and activate the rustup toolchain 'miri'. If no commit is given, updates
56 to the commit given in the `rust-version` file. If the commit is `HEAD`, updates
57 to the latest upstream rustc commit.
58 `rustup-toolchain-install-master` must be installed for this to work. Any extra
59 flags are passed to `rustup-toolchain-install-master`.
60
61   ENVIRONMENT VARIABLES
62
63 MIRI_SYSROOT:
64 If already set, the "sysroot setup" step is skipped.
65
66 CARGO_EXTRA_FLAGS:
67 Pass extra flags to all cargo invocations. (Ignored by `./miri cargo`.)
68 EOF
69 )
70
71 ## We need to know which command to run and some global constants.
72 COMMAND="$1"
73 if [ -z "$COMMAND" ]; then
74     echo "$USAGE"
75     exit 1
76 fi
77 shift
78 # macOS does not have a useful readlink/realpath so we have to use Python instead...
79 MIRIDIR=$(python3 -c 'import os, sys; print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0")
80 # Used for rustc syncs.
81 JOSH_FILTER=":at_commit=75dd959a3a40eb5b4574f8d2e23aa6efbeb33573[:prefix=src/tools/miri]:/src/tools/miri"
82 # Needed for `./miri bench`.
83 TOOLCHAIN=$(cd "$MIRIDIR"; rustup show active-toolchain | head -n 1 | cut -d ' ' -f 1)
84
85 ## Early commands, that don't do auto-things and don't want the environment-altering things happening below.
86 case "$COMMAND" in
87 toolchain)
88     cd "$MIRIDIR"
89     # Make sure rustup-toolchain-install-master is installed.
90     if ! which rustup-toolchain-install-master >/dev/null; then
91         echo "Please install rustup-toolchain-install-master by running 'cargo install rustup-toolchain-install-master'"
92         exit 1
93     fi
94     # Determine new commit.
95     if [[ "$1" == "" ]]; then
96         NEW_COMMIT=$(cat rust-version)
97     elif [[ "$1" == "HEAD" ]]; then
98         NEW_COMMIT=$(git ls-remote https://github.com/rust-lang/rust/ HEAD | cut -f 1)
99     else
100         NEW_COMMIT="$1"
101     fi
102     echo "$NEW_COMMIT" > rust-version
103     shift || true # don't fail if shifting fails because no commit was given
104     # Check if we already are at that commit.
105     CUR_COMMIT=$(rustc +miri --version -v 2>/dev/null | grep "^commit-hash: " | cut -d " " -f 2)
106     if [[ "$CUR_COMMIT" == "$NEW_COMMIT" ]]; then
107         echo "miri toolchain is already at commit $CUR_COMMIT."
108         rustup override set miri
109         exit 0
110     fi
111     # Install and setup new toolchain.
112     rustup toolchain uninstall miri
113     rustup-toolchain-install-master -n miri -c cargo -c rust-src -c rustc-dev -c llvm-tools -c rustfmt -c clippy "$@" -- "$NEW_COMMIT"
114     rustup override set miri
115     # Cleanup.
116     cargo clean
117     # Call 'cargo metadata' on the sources in case that changes the lockfile
118     # (which fails under some setups when it is done from inside vscode).
119     cargo metadata --format-version 1 --manifest-path "$(rustc --print sysroot)/lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml" >/dev/null
120     # Done!
121     exit 0
122     ;;
123 rustc-pull)
124     cd "$MIRIDIR"
125     git fetch http://localhost:8000/rust-lang/rust.git$JOSH_FILTER.git master
126     git merge FETCH_HEAD --no-ff -m "Merge from rustc"
127     exit 0
128     ;;
129 rustc-push)
130     USER="$1"
131     BRANCH="$2"
132     if [ -z "$USER" ] || [ -z "$BRANCH" ]; then
133         echo "Usage: $0 rustc-push <github user> <branch>"
134         exit 1
135     fi
136     if [ -n "$RUSTC_GIT" ]; then
137         # Use an existing fork for the branch updates.
138         cd "$RUSTC_GIT"
139     else
140         # Do this in the local Miri repo.
141         echo "This will pull a copy of the rust-lang/rust history into this Miri checkout, growing it by about 1GB."
142         read -r -p "To avoid that, abort now and set the RUSTC_GIT environment variable to an existing rustc checkout. Proceed? [y/N] "
143         if [[ ! $REPLY =~ ^[Yy]$ ]]; then
144             exit 1
145         fi
146         cd "$MIRIDIR"
147     fi
148     # Prepare the branches. For reliable pushing we need to push to a non-existent branch
149     # and set `-o base` to a branch that holds current rustc master.
150     echo "Preparing $USER/rust..."
151     if git fetch https://github.com/$USER/rust $BRANCH &>/dev/null; then
152         echo "The '$BRANCH' seems to already exist in $USER/rust. Please delete it and try again."
153         exit 1
154     fi
155     git fetch https://github.com/rust-lang/rust master
156     git push https://github.com/$USER/rust FETCH_HEAD:master
157     # Do the actual push.
158     cd "$MIRIDIR"
159     echo "Pushing Miri changes..."
160     git push http://localhost:8000/$USER/rust.git$JOSH_FILTER.git HEAD:$BRANCH -o base=master
161     exit 0
162     ;;
163 many-seeds)
164     for SEED in $({ echo obase=16; seq 0 255; } | bc); do
165         echo "Trying seed: $SEED"
166         MIRIFLAGS="$MIRIFLAGS -Zmiri-seed=$SEED" $@ || { echo "Failing seed: $SEED"; break; }
167     done
168     exit 0
169     ;;
170 bench)
171     # Make sure we have an up-to-date Miri installed
172     "$0" install
173     # Run the requested benchmarks
174     if [ -z "${1+exists}" ]; then
175         BENCHES=( $(ls "$MIRIDIR/bench-cargo-miri" ) )
176     else
177         BENCHES=("$@")
178     fi
179     for BENCH in "${BENCHES[@]}"; do
180         hyperfine -w 1 -m 5 --shell=none "cargo +$TOOLCHAIN miri run --manifest-path $MIRIDIR/bench-cargo-miri/$BENCH/Cargo.toml"
181     done
182     exit 0
183     ;;
184 esac
185
186 ## Run the auto-things.
187 if [ -z "$MIRI_AUTO_OPS" ]; then
188     export MIRI_AUTO_OPS=42
189
190     # Run this first, so that the toolchain doesn't change after
191     # other code has run.
192     if [ -f "$MIRIDIR/.auto-everything" ] || [ -f "$MIRIDIR/.auto-toolchain" ] ; then
193         $0 toolchain
194         # Let's make sure to actually use that toolchain, too.
195         TOOLCHAIN=miri
196     fi
197
198     if [ -f "$MIRIDIR/.auto-everything" ] || [ -f "$MIRIDIR/.auto-fmt" ] ; then
199         $0 fmt
200     fi
201
202     if [ -f "$MIRIDIR/.auto-everything" ] || [ -f "$MIRIDIR/.auto-clippy" ] ; then
203         $0 clippy -- -D warnings
204     fi
205 fi
206
207 ## Prepare the environment
208 # Determine some toolchain properties
209 TARGET=$(rustc +$TOOLCHAIN --version --verbose | grep "^host:" | cut -d ' ' -f 2)
210 SYSROOT=$(rustc +$TOOLCHAIN --print sysroot)
211 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
212 if ! test -d "$LIBDIR"; then
213     echo "Something went wrong determining the library dir."
214     echo "I got $LIBDIR but that does not exist."
215     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
216     exit 2
217 fi
218
219 # Prepare flags for cargo and rustc.
220 CARGO="cargo +$TOOLCHAIN"
221 # Share target dir between `miri` and `cargo-miri`.
222 if [ -z "$CARGO_TARGET_DIR" ]; then
223     export CARGO_TARGET_DIR="$MIRIDIR/target"
224 fi
225 # We configure dev builds to not be unusably slow.
226 if [ -z "$CARGO_PROFILE_DEV_OPT_LEVEL" ]; then
227     export CARGO_PROFILE_DEV_OPT_LEVEL=2
228 fi
229 # Enable rustc-specific lints (ignored without `-Zunstable-options`).
230 export RUSTFLAGS="-Zunstable-options -Wrustc::internal $RUSTFLAGS"
231 # We set the rpath so that Miri finds the private rustc libraries it needs.
232 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR $RUSTFLAGS"
233
234 ## Helper functions
235
236 # Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
237 build_sysroot() {
238     if ! MIRI_SYSROOT="$($CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup --print-sysroot "$@")"; then
239         echo "'cargo miri setup' failed"
240         exit 1
241     fi
242     export MIRI_SYSROOT
243 }
244
245 # Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
246 # locally built vs. distributed rustc.
247 find_sysroot() {
248     if [ -n "$MIRI_SYSROOT" ]; then
249         # Sysroot already set, use that.
250         return 0
251     fi
252     # We need to build a sysroot.
253     if [ -n "$MIRI_TEST_TARGET" ]; then
254         build_sysroot --target "$MIRI_TEST_TARGET"
255     else
256         build_sysroot
257     fi
258 }
259
260 ## Main
261
262 # Run command.
263 case "$COMMAND" in
264 install)
265     # "--locked" to respect the Cargo.lock file if it exists.
266     $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR" --force --locked "$@"
267     $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked "$@"
268     ;;
269 check)
270     # Check, and let caller control flags.
271     $CARGO check $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
272     $CARGO check $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
273     ;;
274 build)
275     # Build, and let caller control flags.
276     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
277     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
278     ;;
279 test|bless)
280     # First build and get a sysroot.
281     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
282     find_sysroot
283     if [ "$COMMAND" = "bless" ]; then
284         export MIRI_BLESS="Gesundheit"
285     fi
286     # Then test, and let caller control flags.
287     # Only in root project as `cargo-miri` has no tests.
288     $CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
289     ;;
290 run)
291     # Scan for "--target" to overwrite the "MIRI_TEST_TARGET" env var so
292     # that we set the MIRI_SYSROOT up the right way.
293     FOUND_TARGET_OPT=0
294     for ARG in "$@"; do
295         if [ "$LAST_ARG" = "--target" ]; then
296             # Found it!
297             export MIRI_TEST_TARGET="$ARG"
298             FOUND_TARGET_OPT=1
299             break
300         fi
301         LAST_ARG="$ARG"
302     done
303     if [ "$FOUND_TARGET_OPT" = "0" ] && [ -n "$MIRI_TEST_TARGET" ]; then
304         # Make sure Miri actually uses this target.
305         MIRIFLAGS="$MIRIFLAGS --target $MIRI_TEST_TARGET"
306     fi
307     # First build and get a sysroot.
308     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
309     find_sysroot
310     # Then run the actual command.
311     exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
312     ;;
313 fmt)
314     find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
315         | xargs rustfmt +$TOOLCHAIN --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
316     ;;
317 clippy)
318     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
319     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
320     ;;
321 cargo)
322     # We carefully kept the working dir intact, so this will run cargo *on the workspace in the
323     # current working dir*, not on the main Miri workspace. That is exactly what RA needs.
324     $CARGO "$@"
325     ;;
326 *)
327     echo "Unknown command: $COMMAND"
328     exit 1
329     ;;
330 esac