]> git.lizzy.rs Git - rust.git/blob - miri
Explain `tests::init` function
[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.
10
11 ./miri build <flags>:
12 Just build miri. <flags> are passed to `cargo build`.
13
14 ./miri check <flags>:
15 Just check miri. <flags> are passed to `cargo check`.
16
17 ./miri test <flags>:
18 Build miri, set up a sysroot and then run the test suite. <flags> are passed
19 to the final `cargo test` invocation.
20
21 ./miri run <flags>:
22 Build miri, set up a sysroot and then run the driver with the given <flags>.
23 (Also respects MIRIFLAGS environment variable.)
24
25 The commands above also exist in a "-debug" variant (e.g. "./miri run-debug
26 <flags>") which uses debug builds instead of release builds, for faster build
27 times and slower execution times.
28
29 ./miri fmt <flags>:
30 Format all sources and tests. <flags> are passed to `rustfmt`.
31
32 ./miri clippy <flags>:
33 Runs clippy on all sources. <flags> are passed to `cargo clippy`.
34
35 ./miri many-seeds <command>:
36 Runs <command> over and over again with different seeds for Miri. The MIRIFLAGS
37 variable is set to its original value appended with ` -Zmiri-seed=$SEED` for
38 many different seeds.
39
40 ./miri bench <benches>:
41 Runs the benchmarks from bench-cargo-miri in hyperfine. hyperfine needs to be installed.
42 <benches> can explicitly list the benchmarks to run; by default, all of them are run.
43
44   ENVIRONMENT VARIABLES
45
46 MIRI_SYSROOT:
47 If already set, the "sysroot setup" step is skipped.
48
49 CARGO_EXTRA_FLAGS:
50 Pass extra flags to all cargo invocations.
51 EOF
52 )
53
54 ## Preparation
55 # macOS does not have a useful readlink/realpath so we have to use Python instead...
56 MIRIDIR=$(python3 -c 'import os, sys; print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0")
57 TOOLCHAIN=$(cd "$MIRIDIR"; rustup show active-toolchain | head -n 1 | cut -d ' ' -f 1)
58
59 # Determine command.
60 COMMAND="$1"
61 [ $# -gt 0 ] && shift
62
63 ## Handle some commands early, since they should *not* alter the environment.
64 case "$COMMAND" in
65 many-seeds)
66     for SEED in $({ echo obase=16; seq 0 255; } | bc); do
67         echo "Trying seed: $SEED"
68         MIRIFLAGS="$MIRIFLAGS -Zmiri-seed=$SEED" $@ || { echo "Failing seed: $SEED"; break; }
69     done
70     exit 0
71     ;;
72 bench)
73     # Make sure we have an up-to-date Miri installed
74     "$0" install
75     # Run the requested benchmarks
76     if [ -z "$@" ]; then
77         BENCHES=( $(ls "$MIRIDIR/bench-cargo-miri" ) )
78     else
79         BENCHES=("$@")
80     fi
81     for BENCH in "${BENCHES[@]}"; do
82         hyperfine -w 1 -m 5 --shell=none "cargo +$TOOLCHAIN miri run --manifest-path bench-cargo-miri/$BENCH/Cargo.toml"
83     done
84     exit 0
85     ;;
86 esac
87
88 ## Prepare the environment
89 # Determine some toolchain properties
90 TARGET=$(rustc +$TOOLCHAIN --version --verbose | grep "^host:" | cut -d ' ' -f 2)
91 SYSROOT=$(rustc +$TOOLCHAIN --print sysroot)
92 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
93 if ! test -d "$LIBDIR"; then
94     echo "Something went wrong determining the library dir."
95     echo "I got $LIBDIR but that does not exist."
96     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
97     exit 2
98 fi
99
100 # Prepare flags for cargo and rustc.
101 CARGO="cargo +$TOOLCHAIN"
102 if [ -z "$CARGO_INCREMENTAL" ]; then
103     # Default CARGO_INCREMENTAL to 1.
104     export CARGO_INCREMENTAL=1
105 fi
106 if [ -z "$CARGO_TARGET_DIR" ]; then
107     # Share target dir between `miri` and `cargo-miri`.
108     export CARGO_TARGET_DIR="$MIRIDIR/target"
109 fi
110 # We set the rpath so that Miri finds the private rustc libraries it needs.
111 # We enable debug-assertions to get tracing.
112 # We enable line-only debuginfo for backtraces.
113 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debuginfo=1 $RUSTFLAGS"
114 # Determine flags passed to all cargo invocations.
115 # This is a bit more annoying that one would hope due to
116 # <https://github.com/rust-lang/cargo/issues/6992>.
117 case "$COMMAND" in
118 *-debug)
119     CARGO_INSTALL_FLAGS="--target $TARGET --debug $CARGO_EXTRA_FLAGS"
120     CARGO_BUILD_FLAGS="--target $TARGET $CARGO_EXTRA_FLAGS"
121     ;;
122 *)
123     CARGO_INSTALL_FLAGS="--target $TARGET $CARGO_EXTRA_FLAGS"
124     CARGO_BUILD_FLAGS="--target $TARGET --release $CARGO_EXTRA_FLAGS"
125     ;;
126 esac
127
128 ## Helper functions
129
130 # Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
131 build_sysroot() {
132     # Build once, for the user to see.
133     $CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
134     # Call again, to just set env var.
135     export MIRI_SYSROOT="$($CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
136 }
137
138 # Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
139 # locally built vs. distributed rustc.
140 find_sysroot() {
141     if [ -n "$MIRI_SYSROOT" ]; then
142         # Sysroot already set, use that.
143         return 0
144     fi
145     # We need to build a sysroot.
146     if [ -n "$MIRI_TEST_TARGET" ]; then
147         build_sysroot --target "$MIRI_TEST_TARGET"
148     else
149         build_sysroot
150     fi
151 }
152
153 ## Main
154
155 # Run command.
156 case "$COMMAND" in
157 install|install-debug)
158     # "--locked" to respect the Cargo.lock file if it exists,
159     # "--offline" to avoid querying the registry (for yanked packages).
160     $CARGO install $CARGO_INSTALL_FLAGS --path "$MIRIDIR" --force --locked --offline "$@"
161     $CARGO install $CARGO_INSTALL_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked --offline "$@"
162     ;;
163 check|check-debug)
164     # Check, and let caller control flags.
165     $CARGO check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
166     $CARGO check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
167     ;;
168 build|build-debug)
169     # Build, and let caller control flags.
170     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
171     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
172     ;;
173 test|test-debug|bless|bless-debug)
174     # First build and get a sysroot.
175     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
176     find_sysroot
177     case "$COMMAND" in
178     bless|bless-debug)
179         export MIRI_BLESS="Gesundheit"
180         ;;
181     esac
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_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
185     $CARGO test $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml "$@"
186     ;;
187 run|run-debug)
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_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
206     find_sysroot
207     # Then run the actual command.
208     exec $CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --sysroot "$MIRI_SYSROOT" $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_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
216     $CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml --all-targets "$@"
217     $CARGO clippy $CARGO_BUILD_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