]> git.lizzy.rs Git - rust.git/blob - miri
Initial freebsd work
[rust.git] / miri
1 #!/bin/bash
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 The commands above 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 ./miri fmt <flags>:
29 Format all sources and tests.  <flags> are passed to `rustfmt`.
30
31   ENVIRONMENT VARIABLES
32
33 MIRI_SYSROOT:
34 If already set, the "sysroot setup" step is skipped.
35
36 CARGO_EXTRA_FLAGS:
37 Pass extra flags to all cargo invocations.
38 EOF
39 )
40
41 ## Preparation
42 TARGET=$(rustc --version --verbose | grep "^host:" | cut -d ' ' -f 2)
43 SYSROOT=$(rustc --print sysroot)
44 LIBDIR=$SYSROOT/lib/rustlib/$TARGET/lib
45 # macOS does not have a useful readlink/realpath so we have to use Python instead...
46 MIRIDIR=$(dirname "$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$0")")
47 if ! test -d "$LIBDIR"; then
48     echo "Something went wrong determining the library dir."
49     echo "I got $LIBDIR but that does not exist."
50     echo "Please report a bug at https://github.com/rust-lang/miri/issues."
51     exit 2
52 fi
53 if [ -z "$CARGO_INCREMENTAL" ]; then
54     # Default CARGO_INCREMENTAL to 1.
55     export CARGO_INCREMENTAL=1
56 fi
57 if [ -z "$CARGO_TARGET_DIR" ]; then
58     # Share target dir between `miri` and `cargo-miri`.
59     export CARGO_TARGET_DIR="$MIRIDIR/target"
60 fi
61 # We set the rpath so that Miri finds the private rustc libraries it needs.
62 # We enable debug-assertions to get tracing.
63 # We enable line-only debuginfo for backtraces.
64 export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debuginfo=1 $RUSTFLAGS"
65
66 ## Helper functions
67
68 # Build a sysroot and set MIRI_SYSROOT to use it.  Arguments are passed to `cargo miri setup`.
69 build_sysroot() {
70     # Build once, for the user to see.
71     cargo run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -- miri setup "$@"
72     # Call again, to just set env var.
73     export MIRI_SYSROOT="$(cargo run $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
74 }
75
76 # Prepare and set MIRI_SYSROOT.  Respects `MIRI_TEST_TARGET` and takes into account
77 # locally built vs. distributed rustc.
78 find_sysroot() {
79     if [ -n "$MIRI_SYSROOT" ]; then
80         # Sysroot already set, use that.
81         return 0
82     fi
83     # We need to build a sysroot.
84     if [ -n "$MIRI_TEST_TARGET" ]; then
85         build_sysroot --target "$MIRI_TEST_TARGET"
86     else
87         build_sysroot
88     fi
89 }
90
91 ## Main
92
93 # Determine command.
94 COMMAND="$1"
95 [ $# -gt 0 ] && shift
96
97 # Determine flags passed to all cargo invocations.
98 # This is a bit more annoying that one would hope due to
99 # <https://github.com/rust-lang/cargo/issues/6992>.
100 case "$COMMAND" in
101 *-debug)
102     CARGO_INSTALL_FLAGS="--target $TARGET --debug $CARGO_EXTRA_FLAGS"
103     CARGO_BUILD_FLAGS="--target $TARGET $CARGO_EXTRA_FLAGS"
104     ;;
105 *)
106     CARGO_INSTALL_FLAGS="--target $TARGET $CARGO_EXTRA_FLAGS"
107     CARGO_BUILD_FLAGS="--target $TARGET --release $CARGO_EXTRA_FLAGS"
108     ;;
109 esac
110
111 # Run command.
112 case "$COMMAND" in
113 install|install-debug)
114     # "--locked" to respect the Cargo.lock file if it exists,
115     # "--offline" to avoid querying the registry (for yanked packages).
116     cargo install $CARGO_INSTALL_FLAGS --path "$MIRIDIR" --force --locked --offline "$@"
117     cargo install $CARGO_INSTALL_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked --offline "$@"
118     ;;
119 check|check-debug)
120     # Check, and let caller control flags.
121     cargo check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml --all-targets "$@"
122     cargo check $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
123     ;;
124 build|build-debug)
125     # Build, and let caller control flags.
126     cargo build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
127     cargo build $CARGO_BUILD_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml "$@"
128     ;;
129 test|test-debug|bless|bless-debug)
130     # First build and get a sysroot.
131     cargo build $CARGO_BUILD_FLAGS
132     find_sysroot
133     case "$COMMAND" in
134     bless|bless-debug)
135         export MIRI_BLESS="Gesundheit"
136         ;;
137     esac
138     # Then test, and let caller control flags.
139     # Only in root project and ui_test as `cargo-miri` has no tests.
140     cargo test $CARGO_BUILD_FLAGS "$@"
141     cargo test $CARGO_BUILD_FLAGS --manifest-path ui_test/Cargo.toml "$@"
142     ;;
143 run|run-debug)
144     # Scan for "--target" to set the "MIRI_TEST_TARGET" env var so
145     # that we set the MIRI_SYSROOT up the right way.
146     if [ -z "$MIRI_TEST_TARGET" ]; then
147         for ARG in "$@"; do
148             if [ "$LAST_ARG" = "--target" ]; then
149                 # Found it!
150                 export MIRI_TEST_TARGET="$ARG"
151                 break
152             fi
153             LAST_ARG="$ARG"
154         done
155     fi
156     # First build and get a sysroot.
157     cargo build $CARGO_BUILD_FLAGS
158     find_sysroot
159     # Then run the actual command.
160     exec cargo run $CARGO_BUILD_FLAGS -- --sysroot "$MIRI_SYSROOT" "$@"
161     ;;
162 fmt)
163     find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
164         | xargs rustfmt --edition=2021 --config-path "$MIRIDIR/rustfmt.toml" "$@"
165     ;;
166 *)
167     if [ -n "$COMMAND" ]; then
168       echo "Unknown command: $COMMAND"
169       echo
170     fi
171     echo "$USAGE"
172     exit 1
173 esac