]> git.lizzy.rs Git - rust.git/blob - miri
Add a minimal reproducer of the ICE
[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 ## Preparation
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 TOOLCHAIN=$(cd "$MIRIDIR"; rustup show active-toolchain | head -n 1 | cut -d ' ' -f 1)
55
56 # Determine command.
57 COMMAND="$1"
58 [ $# -gt 0 ] && shift
59
60 ## Handle some commands early, since they should *not* alter the environment.
61 case "$COMMAND" in
62 many-seeds)
63     for SEED in $({ echo obase=16; seq 0 255; } | bc); do
64         echo "Trying seed: $SEED"
65         MIRIFLAGS="$MIRIFLAGS -Zmiri-seed=$SEED" $@ || { echo "Failing seed: $SEED"; break; }
66     done
67     exit 0
68     ;;
69 bench)
70     # Make sure we have an up-to-date Miri installed
71     "$0" install
72     # Run the requested benchmarks
73     if [ -z "$@" ]; then
74         BENCHES=( $(ls "$MIRIDIR/bench-cargo-miri" ) )
75     else
76         BENCHES=("$@")
77     fi
78     for BENCH in "${BENCHES[@]}"; do
79         hyperfine -w 1 -m 5 --shell=none "cargo +$TOOLCHAIN miri run --manifest-path bench-cargo-miri/$BENCH/Cargo.toml"
80     done
81     exit 0
82     ;;
83 esac
84
85 ## Prepare the environment
86 # Determine some toolchain properties
87 TARGET=$(rustc +$TOOLCHAIN --version --verbose | grep "^host:" | cut -d ' ' -f 2)
88 SYSROOT=$(rustc +$TOOLCHAIN --print sysroot)
89 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
90 if ! test -d "$LIBDIR"; then
91     echo "Something went wrong determining the library dir."
92     echo "I got $LIBDIR but that does not exist."
93     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
94     exit 2
95 fi
96
97 # Prepare flags for cargo and rustc.
98 CARGO="cargo +$TOOLCHAIN"
99 # Share target dir between `miri` and `cargo-miri`.
100 if [ -z "$CARGO_TARGET_DIR" ]; then
101     export CARGO_TARGET_DIR="$MIRIDIR/target"
102 fi
103 # We configure dev builds to not be unusably slow.
104 if [ -z "$CARGO_PROFILE_DEV_OPT_LEVEL" ]; then
105     export CARGO_PROFILE_DEV_OPT_LEVEL=2
106 fi
107 # We set the rpath so that Miri finds the private rustc libraries it needs.
108 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR $RUSTFLAGS"
109
110 ## Helper functions
111
112 # Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
113 build_sysroot() {
114     # Build once, for the user to see.
115     $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
116     # Call again, to just set env var.
117     export MIRI_SYSROOT="$($CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
118 }
119
120 # Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
121 # locally built vs. distributed rustc.
122 find_sysroot() {
123     if [ -n "$MIRI_SYSROOT" ]; then
124         # Sysroot already set, use that.
125         return 0
126     fi
127     # We need to build a sysroot.
128     if [ -n "$MIRI_TEST_TARGET" ]; then
129         build_sysroot --target "$MIRI_TEST_TARGET"
130     else
131         build_sysroot
132     fi
133 }
134
135 ## Main
136
137 # Run command.
138 case "$COMMAND" in
139 install)
140     # "--locked" to respect the Cargo.lock file if it exists.
141     $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR" --force --locked "$@"
142     $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked "$@"
143     ;;
144 check)
145     # Check, and let caller control flags.
146     $CARGO check $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
147     $CARGO check $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
148     ;;
149 build)
150     # Build, and let caller control flags.
151     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
152     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
153     ;;
154 test|bless)
155     # First build and get a sysroot.
156     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
157     find_sysroot
158     if [ "$COMMAND" = "bless" ]; then
159         export MIRI_BLESS="Gesundheit"
160     fi
161     # Then test, and let caller control flags.
162     # Only in root project and ui_test as `cargo-miri` has no tests.
163     $CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
164     $CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml "$@"
165     ;;
166 run)
167     # Scan for "--target" to overwrite the "MIRI_TEST_TARGET" env var so
168     # that we set the MIRI_SYSROOT up the right way.
169     FOUND_TARGET_OPT=0
170     for ARG in "$@"; do
171         if [ "$LAST_ARG" = "--target" ]; then
172             # Found it!
173             export MIRI_TEST_TARGET="$ARG"
174             FOUND_TARGET_OPT=1
175             break
176         fi
177         LAST_ARG="$ARG"
178     done
179     if [ "$FOUND_TARGET_OPT" = "0" ] && [ -n "$MIRI_TEST_TARGET" ]; then
180         # Make sure Miri actually uses this target.
181         MIRIFLAGS="$MIRIFLAGS --target $MIRI_TEST_TARGET"
182     fi
183     # First build and get a sysroot.
184     $CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
185     find_sysroot
186     # Then run the actual command.
187     exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --sysroot "$MIRI_SYSROOT" $MIRIFLAGS "$@"
188     ;;
189 fmt)
190     find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
191         | xargs rustfmt +$TOOLCHAIN --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
192     ;;
193 clippy)
194     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
195     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml --all-targets "$@"
196     $CARGO clippy $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
197     ;;
198 *)
199     if [ -n "$COMMAND" ]; then
200       echo "Unknown command: $COMMAND"
201       echo
202     fi
203     echo "$USAGE"
204     exit 1
205 esac