Skip to content

Commit 829969e

Browse files
authored
miri build script: support building miri in debug mode; avoid rebuilding Miri on CI (#747)
miri build script: support building miri in debug mode; avoid rebuilding Miri on CI
2 parents b4b71e3 + 2427fee commit 829969e

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

miri

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,60 @@
11
#!/bin/sh
22
set -e
3-
# I'd love to use `jq` for parsing the JSON properly, but macOS is totally underequipped for this kind of work.
4-
TARGET=$(rustc --print target-spec-json -Z unstable-options | grep llvm-target | cut -d '"' -f 4)
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)
537
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
645
# We set the rpath so that Miri finds the private rustc libraries it needs.
746
# We enable debug-assertions to get tracing.
847
# We enable line-only debuginfo for backtraces.
9-
export RUSTFLAGS="-C link-args=-Wl,-rpath,$SYSROOT/lib/rustlib/$TARGET/lib -C debug-assertions -C debuginfo=1"
48+
export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR -C debug-assertions -C debuginfo=1"
1049

1150
## Helper functions
1251

1352
# Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
1453
build_sysroot() {
1554
# Build once, for the user to see.
16-
cargo run --release --bin cargo-miri -- miri setup "$@"
55+
cargo run $CARGO_BUILD_FLAGS --bin cargo-miri -- miri setup "$@"
1756
# Call again, to just set env var.
18-
eval $(cargo run --release -q --bin cargo-miri -- miri setup --env "$@")
57+
eval $(cargo run $CARGO_BUILD_FLAGS -q --bin cargo-miri -- miri setup --env "$@")
1958
export MIRI_SYSROOT
2059
}
2160

@@ -48,23 +87,46 @@ find_sysroot() {
4887

4988
## Main
5089

90+
# Determine command.
5191
COMMAND="$1"
5292
shift
5393

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>.
5497
case "$COMMAND" in
55-
install)
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)
56111
# "--locked" to respect the Cargo.lock file if it exists,
57112
# "--offline" to avoid querying the registry (for yanked packages).
58-
exec cargo "$COMMAND" --path "$(dirname "$0")" --force --locked --offline "$@"
113+
exec cargo install $CARGO_INSTALL_FLAGS --path "$(dirname "$0")" --force --locked --offline "$@"
59114
;;
60-
build)
115+
build|build-debug)
61116
# Build, and let caller control flags.
62-
exec cargo "$COMMAND" --release "$@"
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 "$@"
63125
;;
64-
test|run)
65-
# In "run" mode, scan for "--target" to set the "MIRI_TEST_TARGET" env var so
126+
run|run-debug)
127+
# Scan for "--target" to set the "MIRI_TEST_TARGET" env var so
66128
# that we set the MIRI_SYSROOT up the right way.
67-
if [ "$COMMAND" = "run" ] && [ -z "$MIRI_TEST_TARGET" ]; then
129+
if [ -z "$MIRI_TEST_TARGET" ]; then
68130
for ARG in "$@"; do
69131
if [ "$LAST_ARG" = "--target" ]; then
70132
# Found it!
@@ -75,9 +137,14 @@ test|run)
75137
done
76138
fi
77139
# First build and get a sysroot.
78-
cargo build --release
140+
cargo build $CARGO_BUILD_FLAGS
79141
find_sysroot
80142
# Then run the actual command.
81-
exec cargo "$COMMAND" --release "$@"
143+
exec cargo run $CARGO_BUILD_FLAGS "$@"
82144
;;
145+
*)
146+
echo "Unknown command: $COMMAND"
147+
echo
148+
echo "$USAGE"
149+
exit 1
83150
esac

travis.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ if [ "$TRAVIS_OS_NAME" == osx ]; then
77
else
88
FOREIGN_TARGET=i686-unknown-linux-gnu
99
fi
10+
export CARGO_EXTRA_FLAGS="--all-features"
1011

1112
# Prepare
1213
echo "Build and install miri"
13-
./miri build --all-features --all-targets
14+
./miri build --all-targets
1415
./miri install
1516
echo
1617

0 commit comments

Comments
 (0)