Fix sysroot detection which would result in /usr/lib/lib/rustlib
authorDebian Rust Maintainers <pkg-rust-maintainers@alioth-lists.debian.net>
Wed, 13 Sep 2023 11:57:58 +0000 (12:57 +0100)
committerFabian Grünbichler <debian@fabian.gruenbichler.email>
Wed, 13 Sep 2023 11:57:58 +0000 (12:57 +0100)
Bug: https://github.com/rust-lang/rust/issues/109994

This patch is a mixture of two upstream fix attempts. The first from
https://github.com/rust-lang/rust/pull/108376 didn't actually solve the
problem in Debian's case. After reporting that in $Bug, a second attempt is
made at https://github.com/rust-lang/rust/pull/110281, which worked.

Gbp-Pq: Name u-fix-sysroot-detection-logic.patch

compiler/rustc_session/src/filesearch.rs

index f1fbf38217d64b82d09f36cb57c51e392e6170e4..4a12b52b77f4aeeb824940459265e8b9b4753a14 100644 (file)
@@ -179,7 +179,7 @@ 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| {
@@ -194,13 +194,25 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
                     }
                 })
                 .map(|s| s.to_owned())
-                .ok_or(format!(
+                .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