Skip to content

Commit

Permalink
Rollup merge of rust-lang#108376 - liushuyu:fix-sysroot-infer-103660,…
Browse files Browse the repository at this point in the history
… r=ozkanonur

compiler/rustc_session: fix sysroot detection logic

This pull request fixes the sysroot detection logic on systems where `/usr/lib` contains a multi-arch structure (e.g. installs `rustc_driver` into `/usr/lib/x86_64-linux-gnu` folder).

This fixes a regression for various Linux systems introduced in rust-lang#103660. On Debian and Ubuntu systems, the logic in the pull request, as mentioned earlier, will resolve the sysroot to `/usr/lib`, making `rustc --print target-libdir` to return `/usr/lib/lib/rustlib/x86_64-unknown-linux-gnu/lib` (notice the extra `lib` at the beginning).

The fix is not very "clean" according to the standard. If you have any suggestions on improving the logic, you are more than welcome to comment below!
  • Loading branch information
matthiaskrgr authored Mar 1, 2023
2 parents 31f858d + 2186358 commit 0dfbce1
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion compiler/rustc_session/src/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
if dir.ends_with(crate::config::host_triple()) {
dir.parent() // chop off `$target`
.and_then(|p| p.parent()) // chop off `rustlib`
.and_then(|p| p.parent()) // chop off `lib`
.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,
}
})
.map(|s| s.to_owned())
.ok_or(format!(
"Could not move 3 levels upper using `parent()` on {}",
Expand Down

0 comments on commit 0dfbce1

Please sign in to comment.