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