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