]> git.lizzy.rs Git - rust.git/blob - miri
f1eae220e0ded30f62fb7618a3c278e17088e8da
[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 $RUSTC_EXTRA_FLAGS"
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     export MIRI_SYSROOT="$(cargo run $CARGO_BUILD_FLAGS -q --bin cargo-miri -- miri setup --print-sysroot "$@")"
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     if [ -n "$MIRI_SYSROOT" ]; then
64         # Sysroot already set, use that.
65         return 0
66     fi
67     # We need to build a sysroot.
68     if [ -n "$MIRI_TEST_TARGET" ]; then
69         build_sysroot --target "$MIRI_TEST_TARGET"
70     else
71         build_sysroot
72     fi
73 }
74
75 ## Main
76
77 # Determine command.
78 COMMAND="$1"
79 [ $# -gt 0 ] && shift
80
81 # Determine flags passed to all cargo invocations.
82 # This is a bit more annoying that one would hope due to
83 # <https://github.com/rust-lang/cargo/issues/6992>.
84 case "$COMMAND" in
85 *-debug)
86     CARGO_INSTALL_FLAGS="--debug $CARGO_EXTRA_FLAGS"
87     CARGO_BUILD_FLAGS="$CARGO_EXTRA_FLAGS"
88     ;;
89 *)
90     CARGO_INSTALL_FLAGS="$CARGO_EXTRA_FLAGS"
91     CARGO_BUILD_FLAGS="--release $CARGO_EXTRA_FLAGS"
92     ;;
93 esac
94
95 # Run command.
96 case "$COMMAND" in
97 install|install-debug)
98     # "--locked" to respect the Cargo.lock file if it exists,
99     # "--offline" to avoid querying the registry (for yanked packages).
100     exec cargo install $CARGO_INSTALL_FLAGS --path "$(dirname "$0")" --force --locked --offline "$@"
101     ;;
102 build|build-debug)
103     # Build, and let caller control flags.
104     exec cargo build $CARGO_BUILD_FLAGS "$@"
105     ;;
106 test|test-debug)
107     # First build and get a sysroot.
108     cargo build $CARGO_BUILD_FLAGS
109     find_sysroot
110     # Then test, and let caller control flags.
111     exec cargo test $CARGO_BUILD_FLAGS "$@"
112     ;;
113 run|run-debug)
114     # Scan for "--target" to set the "MIRI_TEST_TARGET" env var so
115     # that we set the MIRI_SYSROOT up the right way.
116     if [ -z "$MIRI_TEST_TARGET" ]; then
117         for ARG in "$@"; do
118             if [ "$LAST_ARG" = "--target" ]; then
119                 # Found it!
120                 export MIRI_TEST_TARGET="$ARG"
121                 break
122             fi
123             LAST_ARG="$ARG"
124         done
125     fi
126     # First build and get a sysroot.
127     cargo build $CARGO_BUILD_FLAGS
128     find_sysroot
129     # Then run the actual command.
130     exec cargo run $CARGO_BUILD_FLAGS -- --sysroot "$MIRI_SYSROOT" "$@"
131     ;;
132 *)
133     echo "Unknown command: $COMMAND"
134     echo
135     echo "$USAGE"
136     exit 1
137 esac