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