]> git.lizzy.rs Git - rust.git/blob - miri
more tweaks
[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 Format all sources and tests. <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   ENVIRONMENT VARIABLES
41
42 MIRI_SYSROOT:
43 If already set, the "sysroot setup" step is skipped.
44
45 CARGO_EXTRA_FLAGS:
46 Pass extra flags to all cargo invocations.
47 EOF
48 )
49
50 # Determine command.
51 COMMAND="$1"
52 [ $# -gt 0 ] && shift
53
54 ## Handle some commands early, since they should *not* alter the environment.
55 case "$COMMAND" in
56 many-seeds)
57     for SEED in $({ echo obase=16; seq 0 255; } | bc); do
58         MIRIFLAGS="$MIRIFLAGS -Zmiri-seed=$SEED" $@ || { echo "Failing seed: $SEED"; break; }
59     done
60     exit 0
61     ;;
62 esac
63
64 ## Preparation
65 # macOS does not have a useful readlink/realpath so we have to use Python instead...
66 MIRIDIR=$(python3 -c 'import os, sys; print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0")
67 # Determine toolchain *in the Miri dir* and use that.
68 TOOLCHAIN=$(cd "$MIRIDIR"; rustup show active-toolchain | head -n 1 | cut -d ' ' -f 1)
69
70 # Determine some toolchain properties
71 TARGET=$(rustc +$TOOLCHAIN --version --verbose | grep "^host:" | cut -d ' ' -f 2)
72 SYSROOT=$(rustc +$TOOLCHAIN --print sysroot)
73 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
74 if ! test -d "$LIBDIR"; then
75     echo "Something went wrong determining the library dir."
76     echo "I got $LIBDIR but that does not exist."
77     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
78     exit 2
79 fi
80
81 # Prepare flags for cargo and rustc.
82 CARGO="cargo +$TOOLCHAIN"
83 if [ -z "$CARGO_INCREMENTAL" ]; then
84     # Default CARGO_INCREMENTAL to 1.
85     export CARGO_INCREMENTAL=1
86 fi
87 if [ -z "$CARGO_TARGET_DIR" ]; then
88     # Share target dir between `miri` and `cargo-miri`.
89     export CARGO_TARGET_DIR="$MIRIDIR/target"
90 fi
91 # We set the rpath so that Miri finds the private rustc libraries it needs.
92 # We enable debug-assertions to get tracing.
93 # We enable line-only debuginfo for backtraces.
94 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debuginfo=1 $RUSTFLAGS"
95 # Determine flags passed to all cargo invocations.
96 # This is a bit more annoying that one would hope due to
97 # <https://github.com/rust-lang/cargo/issues/6992>.
98 case "$COMMAND" in
99 *-debug)
100     CARGO_INSTALL_FLAGS="--target $TARGET --debug $CARGO_EXTRA_FLAGS"
101     CARGO_BUILD_FLAGS="--target $TARGET $CARGO_EXTRA_FLAGS"
102     ;;
103 *)
104     CARGO_INSTALL_FLAGS="--target $TARGET $CARGO_EXTRA_FLAGS"
105     CARGO_BUILD_FLAGS="--target $TARGET --release $CARGO_EXTRA_FLAGS"
106     ;;
107 esac
108
109 ## Helper functions
110
111 # Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
112 build_sysroot() {
113     # Build once, for the user to see.
114     $CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
115     # Call again, to just set env var.
116     export MIRI_SYSROOT="$($CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
117 }
118
119 # Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
120 # locally built vs. distributed rustc.
121 find_sysroot() {
122     if [ -n "$MIRI_SYSROOT" ]; then
123         # Sysroot already set, use that.
124         return 0
125     fi
126     # We need to build a sysroot.
127     if [ -n "$MIRI_TEST_TARGET" ]; then
128         build_sysroot --target "$MIRI_TEST_TARGET"
129     else
130         build_sysroot
131     fi
132 }
133
134 ## Main
135
136 # Run command.
137 case "$COMMAND" in
138 install|install-debug)
139     # "--locked" to respect the Cargo.lock file if it exists,
140     # "--offline" to avoid querying the registry (for yanked packages).
141     $CARGO install $CARGO_INSTALL_FLAGS --path "$MIRIDIR" --force --locked --offline "$@"
142     $CARGO install $CARGO_INSTALL_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked --offline "$@"
143     ;;
144 check|check-debug)
145     # Check, and let caller control flags.
146     $CARGO check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
147     $CARGO check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
148     ;;
149 build|build-debug)
150     # Build, and let caller control flags.
151     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
152     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
153     ;;
154 test|test-debug|bless|bless-debug)
155     # First build and get a sysroot.
156     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
157     find_sysroot
158     case "$COMMAND" in
159     bless|bless-debug)
160         export MIRI_BLESS="Gesundheit"
161         ;;
162     esac
163     # Then test, and let caller control flags.
164     # Only in root project and ui_test as `cargo-miri` has no tests.
165     $CARGO test $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
166     $CARGO test $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml "$@"
167     ;;
168 run|run-debug)
169     # Scan for "--target" to set the "MIRI_TEST_TARGET" env var so
170     # that we set the MIRI_SYSROOT up the right way.
171     if [ -z "$MIRI_TEST_TARGET" ]; then
172         for ARG in "$@"; do
173             if [ "$LAST_ARG" = "--target" ]; then
174                 # Found it!
175                 export MIRI_TEST_TARGET="$ARG"
176                 break
177             fi
178             LAST_ARG="$ARG"
179         done
180     fi
181     # First build and get a sysroot.
182     $CARGO build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
183     find_sysroot
184     # Then run the actual command.
185     exec $CARGO run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --sysroot "$MIRI_SYSROOT" $MIRIFLAGS "$@"
186     ;;
187 fmt)
188     find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
189         | xargs rustfmt +$TOOLCHAIN --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
190     ;;
191 clippy)
192     $CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
193     $CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/ui_test/Cargo.toml --all-targets "$@"
194     $CARGO clippy $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
195     ;;
196 *)
197     if [ -n "$COMMAND" ]; then
198       echo "Unknown command: $COMMAND"
199       echo
200     fi
201     echo "$USAGE"
202     exit 1
203 esac