]> git.lizzy.rs Git - rust.git/blob - miri
just respect existing RUSTFLAGS instead of providing another override
[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 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 ! test -d "$LIBDIR"; then
43     echo "Something went wrong determining the library dir."
44     echo "I got $LIBDIR but that does not exist."
45     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
46     exit 2
47 fi
48 if [ -z "$CARGO_INCREMENTAL" ]; then
49     # Default CARGO_INCREMENTAL to 1.
50     export CARGO_INCREMENTAL=1
51 fi
52 # We set the rpath so that Miri finds the private rustc libraries it needs.
53 # We enable debug-assertions to get tracing.
54 # We enable line-only debuginfo for backtraces.
55 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debuginfo=1 $RUSTFLAGS"
56
57 ## Helper functions
58
59 # Build a sysroot and set MIRI_SYSROOT to use it.  Arguments are passed to `cargo miri setup`.
60 build_sysroot() {
61     # Build once, for the user to see.
62     cargo run $CARGO_BUILD_FLAGS -p cargo-miri -- miri setup "$@"
63     # Call again, to just set env var.
64     export MIRI_SYSROOT="$(cargo run $CARGO_BUILD_FLAGS -q -p cargo-miri -- miri setup --print-sysroot "$@")"
65 }
66
67 # Prepare and set MIRI_SYSROOT.  Respects `MIRI_TEST_TARGET` and takes into account
68 # locally built vs. distributed rustc.
69 find_sysroot() {
70     if [ -n "$MIRI_SYSROOT" ]; then
71         # Sysroot already set, use that.
72         return 0
73     fi
74     # We need to build a sysroot.
75     if [ -n "$MIRI_TEST_TARGET" ]; then
76         build_sysroot --target "$MIRI_TEST_TARGET"
77     else
78         build_sysroot
79     fi
80 }
81
82 ## Main
83
84 # Determine command.
85 COMMAND="$1"
86 [ $# -gt 0 ] && shift
87
88 # Determine flags passed to all cargo invocations.
89 # This is a bit more annoying that one would hope due to
90 # <https://github.com/rust-lang/cargo/issues/6992>.
91 case "$COMMAND" in
92 *-debug)
93     CARGO_INSTALL_FLAGS="--debug $CARGO_EXTRA_FLAGS"
94     CARGO_BUILD_FLAGS="$CARGO_EXTRA_FLAGS"
95     ;;
96 *)
97     CARGO_INSTALL_FLAGS="$CARGO_EXTRA_FLAGS"
98     CARGO_BUILD_FLAGS="--release $CARGO_EXTRA_FLAGS"
99     ;;
100 esac
101
102 # Run command.
103 case "$COMMAND" in
104 install|install-debug)
105     # "--locked" to respect the Cargo.lock file if it exists,
106     # "--offline" to avoid querying the registry (for yanked packages).
107     cargo install $CARGO_INSTALL_FLAGS --path "$(dirname "$0")" --force --locked --offline "$@"
108     cargo install $CARGO_INSTALL_FLAGS --path "$(dirname "$0")"/cargo-miri --force --locked --offline "$@"
109     ;;
110 check|check-debug)
111     # Check, and let caller control flags.
112     exec cargo check $CARGO_BUILD_FLAGS --all "$@"
113     ;;
114 build|build-debug)
115     # Build, and let caller control flags.
116     exec cargo build $CARGO_BUILD_FLAGS --all "$@"
117     ;;
118 test|test-debug)
119     # First build and get a sysroot.
120     cargo build $CARGO_BUILD_FLAGS
121     find_sysroot
122     # Then test, and let caller control flags.
123     # No `--all` as `cargo-miri` has no tests.
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     if [ -n "$COMMAND" ]; then
147       echo "Unknown command: $COMMAND"
148       echo
149     fi
150     echo "$USAGE"
151     exit 1
152 esac