Skip to content

Commit 4736692

Browse files
authored
Merge pull request #772 from RalfJung/sysroot
Fix sysroot handling
2 parents 965160d + 34b0922 commit 4736692

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/bin/cargo-miri.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ fn test_sysroot_consistency() {
133133
fn get_sysroot(mut cmd: Command) -> PathBuf {
134134
let out = cmd.arg("--print").arg("sysroot")
135135
.output().expect("Failed to run rustc to get sysroot info");
136-
assert!(out.status.success(), "Bad status code when getting sysroot info");
137-
let sysroot = out.stdout.lines().nth(0)
138-
.expect("didn't get at least one line for the sysroot").unwrap();
139-
PathBuf::from(sysroot).canonicalize()
140-
.expect("Failed to canonicalize sysroot")
136+
let stdout = String::from_utf8(out.stdout).expect("stdout is not valid UTF-8");
137+
let stderr = String::from_utf8(out.stderr).expect("stderr is not valid UTF-8");
138+
let stdout = stdout.trim();
139+
assert!(out.status.success(), "Bad status code when getting sysroot info.\nstdout:\n{}\nstderr:\n{}", stdout, stderr);
140+
PathBuf::from(stdout).canonicalize()
141+
.unwrap_or_else(|_| panic!("Failed to canonicalize sysroot: {}", stdout))
141142
}
142143

143144
let rustc_sysroot = get_sysroot(Command::new("rustc"));

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)