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