]> git.lizzy.rs Git - rust.git/blob - miri
Merge pull request #766 from RalfJung/sysroot
[rust.git] / miri
1 #!/bin/sh
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 test <flags>:
15 Build miri, set up a sysroot and then run the test suite. <flags> are passed
16 to the final `cargo test` invocation.
17
18 ./miri run <flags>:
19 Build miri, set up a sysroot and then run the driver with the given <flags>.
20
21 All commands also exist in a "-debug" variant (e.g. "./miri run-debug
22 <flags>") which uses debug builds instead of release builds, for faster build
23 times and slower execution times.
24
25   ENVIRONMENT VARIABLES
26
27 MIRI_SYSROOT:
28 If already set, the "sysroot setup" step is skipped.
29
30 CARGO_EXTRA_FLAGS:
31 Pass extra flags to all cargo invocations.
32 EOF
33 )
34
35 ## Preparation
36 TARGET=$(rustc --version --verbose | grep "^host:" | cut -d ' ' -f 2)
37 SYSROOT=$(rustc --print sysroot)
38 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
39 if ! test -d "$LIBDIR"; then
40     echo "Something went wrong determining the library dir."
41     echo "I got $LIBDIR but that does not exist."
42     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
43     exit 2
44 fi
45 # We set the rpath so that Miri finds the private rustc libraries it needs.
46 # We enable debug-assertions to get tracing.
47 # We enable line-only debuginfo for backtraces.
48 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debuginfo=1"
49
50 ## Helper functions
51
52 # Build a sysroot and set MIRI_SYSROOT to use it.  Arguments are passed to `cargo miri setup`.
53 build_sysroot() {
54     # Build once, for the user to see.
55     cargo run $CARGO_BUILD_FLAGS --bin cargo-miri -- miri setup "$@"
56     # Call again, to just set env var.
57     eval $(cargo run $CARGO_BUILD_FLAGS -q --bin cargo-miri -- miri setup --env "$@")
58 }
59
60 # Prepare and set MIRI_SYSROOT.  Respects `MIRI_TEST_TARGET` and takes into account
61 # locally built vs. distributed rustc.
62 find_sysroot() {
63     # Get ourselves a sysroot
64     if [ -n "$MIRI_SYSROOT" ]; then
65         # Sysroot already set, use that.
66         true
67     elif echo "$SYSROOT" | egrep -q 'build/[^/]+/stage'; then
68         # A local rustc build.
69         if [ -n "$MIRI_TEST_TARGET" ]; then
70             # Foreign targets still need a build.  Use the rustc sources.
71             export XARGO_RUST_SRC="$SYSROOT/../../../src"
72             build_sysroot --target "$MIRI_TEST_TARGET"
73         else
74             # Assume we have a proper host libstd in $SYSROOT.
75             MIRI_SYSROOT="$SYSROOT"
76         fi
77     else
78         # A normal toolchain.  We have to build a sysroot either way.
79         if [ -n "$MIRI_TEST_TARGET" ]; then
80             build_sysroot --target "$MIRI_TEST_TARGET"
81         else
82             build_sysroot
83         fi
84     fi
85     export MIRI_SYSROOT
86 }
87
88 ## Main
89
90 # Determine command.
91 COMMAND="$1"
92 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="--debug $CARGO_EXTRA_FLAGS"
100     CARGO_BUILD_FLAGS="$CARGO_EXTRA_FLAGS"
101     ;;
102 *)
103     CARGO_INSTALL_FLAGS="$CARGO_EXTRA_FLAGS"
104     CARGO_BUILD_FLAGS="--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     exec cargo install $CARGO_INSTALL_FLAGS --path "$(dirname "$0")" --force --locked --offline "$@"
114     ;;
115 build|build-debug)
116     # Build, and let caller control flags.
117     exec cargo build $CARGO_BUILD_FLAGS "$@"
118     ;;
119 test|test-debug)
120     # First build and get a sysroot.
121     cargo build $CARGO_BUILD_FLAGS
122     find_sysroot
123     # Then test, and let caller control flags.
124     exec cargo test $CARGO_BUILD_FLAGS "$@"
125     ;;
126 run|run-debug)
127     # Scan for "--target" to set the "MIRI_TEST_TARGET" env var so
128     # that we set the MIRI_SYSROOT up the right way.
129     if [ -z "$MIRI_TEST_TARGET" ]; then
130         for ARG in "$@"; do
131             if [ "$LAST_ARG" = "--target" ]; then
132                 # Found it!
133                 export MIRI_TEST_TARGET="$ARG"
134                 break
135             fi
136             LAST_ARG="$ARG"
137         done
138     fi
139     # First build and get a sysroot.
140     cargo build $CARGO_BUILD_FLAGS
141     find_sysroot
142     # Then run the actual command.
143     exec cargo run $CARGO_BUILD_FLAGS -- --sysroot "$MIRI_SYSROOT" "$@"
144     ;;
145 *)
146     echo "Unknown command: $COMMAND"
147     echo
148     echo "$USAGE"
149     exit 1
150 esac