Skip to content

Commit

Permalink
Auto merge of rust-lang#110281 - ozkanonur:multiarch-compatible-sysro…
Browse files Browse the repository at this point in the history
…ot-finding, r=jackh726

make sysroot finding compatible with multiarch systems

Tested on Debian 11 multiarch, worked just fine.

resolves rust-lang#109994
  • Loading branch information
bors committed Apr 23, 2023
2 parents 915aa06 + 2e98368 commit 7f94b31
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions compiler/rustc_session/src/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,29 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
))?;

// if `dir` points target's dir, move up to the sysroot
if dir.ends_with(crate::config::host_triple()) {
let mut sysroot_dir = if dir.ends_with(crate::config::host_triple()) {
dir.parent() // chop off `$target`
.and_then(|p| p.parent()) // chop off `rustlib`
.and_then(|p| {
// chop off `lib` (this could be also $arch dir if the host sysroot uses a
// multi-arch layout like Debian or Ubuntu)
match p.parent() {
Some(p) => match p.file_name() {
Some(f) if f == "lib" => p.parent(), // first chop went for $arch, so chop again for `lib`
_ => Some(p),
},
None => None,
}
})
.and_then(|p| p.parent()) // chop off `lib`
.map(|s| s.to_owned())
.ok_or(format!(
"Could not move 3 levels upper using `parent()` on {}",
dir.display()
))
.ok_or_else(|| {
format!("Could not move 3 levels upper using `parent()` on {}", dir.display())
})?
} else {
Ok(dir.to_owned())
dir.to_owned()
};

// On multiarch linux systems, there will be multiarch directory named
// with the architecture(e.g `x86_64-linux-gnu`) under the `lib` directory.
// Which cause us to mistakenly end up in the lib directory instead of the sysroot directory.
if sysroot_dir.ends_with("lib") {
sysroot_dir =
sysroot_dir.parent().map(|real_sysroot| real_sysroot.to_owned()).ok_or_else(
|| format!("Could not move to parent path of {}", sysroot_dir.display()),
)?
}

Ok(sysroot_dir)
}

// Use env::args().next() to get the path of the executable without
Expand Down

0 comments on commit 7f94b31

Please sign in to comment.