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