Skip to content
/ miri Public
forked from rust-lang/miri

Commit

Permalink
Merge pull request rust-lang#772 from RalfJung/sysroot
Browse files Browse the repository at this point in the history
Fix sysroot handling
  • Loading branch information
RalfJung authored Jun 14, 2019
2 parents 965160d + 34b0922 commit 4736692
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/bin/cargo-miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ fn test_sysroot_consistency() {
fn get_sysroot(mut cmd: Command) -> PathBuf {
let out = cmd.arg("--print").arg("sysroot")
.output().expect("Failed to run rustc to get sysroot info");
assert!(out.status.success(), "Bad status code when getting sysroot info");
let sysroot = out.stdout.lines().nth(0)
.expect("didn't get at least one line for the sysroot").unwrap();
PathBuf::from(sysroot).canonicalize()
.expect("Failed to canonicalize sysroot")
let stdout = String::from_utf8(out.stdout).expect("stdout is not valid UTF-8");
let stderr = String::from_utf8(out.stderr).expect("stderr is not valid UTF-8");
let stdout = stdout.trim();
assert!(out.status.success(), "Bad status code when getting sysroot info.\nstdout:\n{}\nstderr:\n{}", stdout, stderr);
PathBuf::from(stdout).canonicalize()
.unwrap_or_else(|_| panic!("Failed to canonicalize sysroot: {}", stdout))
}

let rustc_sysroot = get_sysroot(Command::new("rustc"));
Expand Down
38 changes: 23 additions & 15 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,26 @@ fn init_late_loggers() {

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

fn main() {
Expand Down Expand Up @@ -165,14 +170,17 @@ fn main() {
}
}

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

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

0 comments on commit 4736692

Please sign in to comment.