]> git.lizzy.rs Git - rust.git/blob - miri
effbd023f1989f8d870670698b8349cb6407922a
[rust.git] / miri
1 #!/bin/sh
2 set -e
3 # I'd love to use `jq` for parsing the JSON properly, but macOS is totally underequipped for this kind of work.
4 TARGET=$(rustc --print target-spec-json -Z unstable-options | grep llvm-target | cut -d '"' -f 4)
5 SYSROOT=$(rustc --print sysroot)
6 # We set the rpath so that Miri finds the private rustc libraries it needs.
7 # We enable debug-assertions to get tracing.
8 # We enable line-only debuginfo for backtraces.
9 export RUSTFLAGS="-C link-args=-Wl,-rpath,$SYSROOT/lib/rustlib/$TARGET/lib -C debug-assertions -C debuginfo=1"
10
11 ## Helper functions
12
13 # Build a sysroot and set MIRI_SYSROOT to use it.  Arguments are passed to `cargo miri setup`.
14 build_sysroot() {
15     # Build once, for the user to see.
16     cargo run --release --bin cargo-miri -- miri setup "$@"
17     # Call again, to just set env var.
18     eval $(cargo run --release -q --bin cargo-miri -- miri setup --env "$@")
19     export MIRI_SYSROOT
20 }
21
22 # Prepare and set MIRI_SYSROOT.  Respects `MIRI_TEST_TARGET` and takes into account
23 # locally built vs. distributed rustc.
24 find_sysroot() {
25     # Get ourselves a sysroot
26     if [ -n "$MIRI_SYSROOT" ]; then
27         # Sysroot already set, use that.
28         true
29     elif echo "$SYSROOT" | egrep -q 'build/[^/]+/stage'; then
30         # A local rustc build.
31         if [ -n "$MIRI_TEST_TARGET" ]; then
32             # Foreign targets still need a build.  Use the rustc sources.
33             export XARGO_RUST_SRC="$SYSROOT/../../../src"
34             build_sysroot --target "$MIRI_TEST_TARGET"
35         else
36             # Assume we have a proper host libstd in $SYSROOT.
37             true
38         fi
39     else
40         # We have to build a sysroot either way.
41         if [ -n "$MIRI_TEST_TARGET" ]; then
42             build_sysroot --target "$MIRI_TEST_TARGET"
43         else
44             build_sysroot
45         fi
46     fi
47 }
48
49 ## Main
50
51 COMMAND="$1"
52 shift
53
54 case "$COMMAND" in
55 install)
56     # "--locked" to respect the Cargo.lock file if it exists,
57     # "--offline" to avoid querying the registry (for yanked packages).
58     exec cargo "$COMMAND" --path "$(dirname "$0")" --force --locked --offline "$@"
59     ;;
60 build)
61     # Build, and let caller control flags.
62     exec cargo "$COMMAND" --release "$@"
63     ;;
64 test|run)
65     # In "run" mode, scan for "--target" to set the "MIRI_TEST_TARGET" env var so
66     # that we set the MIRI_SYSROOT up the right way.
67     if [ "$COMMAND" = "run" ] && [ -z "$MIRI_TEST_TARGET" ]; then
68         for ARG in "$@"; do
69             if [ "$LAST_ARG" = "--target" ]; then
70                 # Found it!
71                 export MIRI_TEST_TARGET="$ARG"
72                 break
73             fi
74             LAST_ARG="$ARG"
75         done
76     fi
77     # First build and get a sysroot.
78     cargo build --release
79     find_sysroot
80     # Then run the actual command.
81     exec cargo "$COMMAND" --release "$@"
82     ;;
83 esac