@@ -7,33 +7,76 @@ SYSROOT=$(rustc --print sysroot)
7
7
# We enable line-only debuginfo for backtraces.
8
8
export RUSTFLAGS=" -C link-args=-Wl,-rpath,$SYSROOT /lib/rustlib/$TARGET /lib -C debug-assertions -C debuginfo=1"
9
9
10
- COMMAND=" $1 "
11
- shift
10
+ # # Helper functions
12
11
13
- case " $COMMAND " in
14
- install)
15
- exec cargo install --path " $( dirname " $0 " ) " --force --locked --offline
16
- ;;
17
- build|test|run)
18
- # Basic build
19
- cargo build --release
20
-
21
- # We we want to just build, we are done.
22
- if [ " $COMMAND " = " build" ]; then exit 0; fi
12
+ # Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
13
+ build_sysroot () {
14
+ # Build once, for the user to see.
15
+ cargo run --release --bin cargo-miri -- miri setup " $@ "
16
+ # Call again, to just set env var.
17
+ eval $( cargo run --release -q --bin cargo-miri -- miri setup --env " $@ " )
18
+ export MIRI_SYSROOT
19
+ }
23
20
21
+ # Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
22
+ # locally built vs. distributed rustc.
23
+ find_sysroot () {
24
24
# Get ourselves a sysroot
25
25
if [ -n " $MIRI_SYSROOT " ]; then
26
- # sysroot already set
26
+ # Sysroot already set, use that.
27
27
true
28
28
elif echo " $SYSROOT " | egrep -q ' build/[^/]+/stage' ; then
29
- # a local rustc build, assume we have a proper libstd in $SYSROOT
30
- true
29
+ # A local rustc build.
30
+ if [ -n " $MIRI_TEST_TARGET " ]; then
31
+ # Foreign targets still need a build. Use the rustc sources.
32
+ export XARGO_RUST_SRC=" $SYSROOT /../../../src"
33
+ build_sysroot --target " $MIRI_TEST_TARGET "
34
+ else
35
+ # Assume we have a proper host libstd in $SYSROOT.
36
+ true
37
+ fi
31
38
else
32
- # we have to build a sysroot
33
- cargo run --release --bin cargo-miri -- miri setup
34
- export MIRI_SYSROOT=$HOME /.cache/miri/HOST
39
+ # We have to build a sysroot either way.
40
+ if [ -n " $MIRI_TEST_TARGET " ]; then
41
+ build_sysroot --target " $MIRI_TEST_TARGET "
42
+ else
43
+ build_sysroot
44
+ fi
35
45
fi
46
+ }
47
+
48
+ # # Main
49
+
50
+ COMMAND=" $1 "
51
+ shift
36
52
53
+ case " $COMMAND " in
54
+ install)
55
+ # "--locked" to respect the Cargo.lock file if it exists,
56
+ # "--offline" to avoid querying the registry (for yanked packages).
57
+ exec cargo " $COMMAND " --path " $( dirname " $0 " ) " --force --locked --offline " $@ "
58
+ ;;
59
+ build)
60
+ # Build, and let caller control flags.
61
+ exec cargo " $COMMAND " --release " $@ "
62
+ ;;
63
+ test|run)
64
+ # In "run" mode, scan for "--target" to set the "MIRI_TEST_TARGET" env var so
65
+ # that we set the MIRI_SYSROOT up the right way.
66
+ if [ " $COMMAND " = " run" ] && [ -z " $MIRI_TEST_TARGET " ]; then
67
+ for ARG in " $@ " ; do
68
+ if [ " $LAST_ARG " = " --target" ]; then
69
+ # Found it!
70
+ export MIRI_TEST_TARGET=" $ARG "
71
+ break
72
+ fi
73
+ LAST_ARG=" $ARG "
74
+ done
75
+ fi
76
+ # First build and get a sysroot.
77
+ cargo build --release
78
+ find_sysroot
79
+ # Then run the actual command.
37
80
exec cargo " $COMMAND " --release " $@ "
38
81
;;
39
82
esac
0 commit comments