Skip to content

Commit 34b0922

Browse files
committed
fix running a Miri that was built in bootstrap
1 parent ac2f6cb commit 34b0922

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/bin/miri.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,26 @@ fn init_late_loggers() {
102102

103103
/// Returns the "default sysroot" that Miri will use if no `--sysroot` flag is set.
104104
/// Should be a compile-time constant.
105-
fn compile_time_sysroot() -> String {
105+
fn compile_time_sysroot() -> Option<String> {
106+
if option_env!("RUSTC_STAGE").is_some() {
107+
// This is being built as part of rustc, and gets shipped with rustup.
108+
// We can rely on the sysroot computation in librustc.
109+
return None;
110+
}
111+
// For builds outside rustc, we need to ensure that we got a sysroot
112+
// that gets used as a default. The sysroot computation in librustc would
113+
// end up somewhere in the build dir.
106114
// Taken from PR <https://github.com/Manishearth/rust-clippy/pull/911>.
107115
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
108116
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
109-
match (home, toolchain) {
117+
Some(match (home, toolchain) {
110118
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
111119
_ => {
112120
option_env!("RUST_SYSROOT")
113-
.expect(
114-
"could not find sysroot. Either set `MIRI_SYSROOT` at run-time, or at \
115-
build-time specify `RUST_SYSROOT` env var or use rustup or multirust",
116-
)
121+
.expect("To build Miri without rustup, set the `RUST_SYSROOT` env var at build time")
117122
.to_owned()
118123
}
119-
}
124+
})
120125
}
121126

122127
fn main() {
@@ -165,14 +170,17 @@ fn main() {
165170
}
166171
}
167172

168-
// Determine sysroot.
169-
let sysroot_flag = "--sysroot".to_string();
170-
if !rustc_args.contains(&sysroot_flag) {
171-
// We need to *always* set a --sysroot, as the "default" rustc uses is
172-
// somewhere in the directory miri was built in.
173-
// If no --sysroot is given, fall back to env vars that are read at *compile-time*.
174-
rustc_args.push(sysroot_flag);
175-
rustc_args.push(compile_time_sysroot());
173+
// Determine sysroot if needed. Make sure we always call `compile_time_sysroot`
174+
// as that also does some sanity-checks of the environment we were built in.
175+
// FIXME: Ideally we'd turn a bad build env into a compile-time error, but
176+
// CTFE does not seem powerful enough for that yet.
177+
if let Some(sysroot) = compile_time_sysroot() {
178+
let sysroot_flag = "--sysroot".to_string();
179+
if !rustc_args.contains(&sysroot_flag) {
180+
// We need to overwrite the default that librustc would compute.
181+
rustc_args.push(sysroot_flag);
182+
rustc_args.push(sysroot);
183+
}
176184
}
177185

178186
// Finally, add the default flags all the way in the beginning, but after the binary name.

0 commit comments

Comments
 (0)