@@ -227,28 +227,29 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
227227 ) ) ?;
228228
229229 // if `dir` points target's dir, move up to the sysroot
230- if dir. ends_with ( crate :: config:: host_triple ( ) ) {
230+ let mut sysroot_dir = if dir. ends_with ( crate :: config:: host_triple ( ) ) {
231231 dir. parent ( ) // chop off `$target`
232232 . and_then ( |p| p. parent ( ) ) // chop off `rustlib`
233- . and_then ( |p| {
234- // chop off `lib` (this could be also $arch dir if the host sysroot uses a
235- // multi-arch layout like Debian or Ubuntu)
236- match p. parent ( ) {
237- Some ( p) => match p. file_name ( ) {
238- Some ( f) if f == "lib" => p. parent ( ) , // first chop went for $arch, so chop again for `lib`
239- _ => Some ( p) ,
240- } ,
241- None => None ,
242- }
243- } )
233+ . and_then ( |p| p. parent ( ) ) // chop off `lib`
244234 . map ( |s| s. to_owned ( ) )
245- . ok_or ( format ! (
246- "Could not move 3 levels upper using `parent()` on {}" ,
247- dir. display( )
248- ) )
235+ . ok_or_else ( || {
236+ format ! ( "Could not move 3 levels upper using `parent()` on {}" , dir. display( ) )
237+ } ) ?
249238 } else {
250- Ok ( dir. to_owned ( ) )
239+ dir. to_owned ( )
240+ } ;
241+
242+ // On multiarch linux systems, there will be multiarch directory named
243+ // with the architecture(e.g `x86_64-linux-gnu`) under the `lib` directory.
244+ // Which cause us to mistakenly end up in the lib directory instead of the sysroot directory.
245+ if sysroot_dir. ends_with ( "lib" ) {
246+ sysroot_dir =
247+ sysroot_dir. parent ( ) . map ( |real_sysroot| real_sysroot. to_owned ( ) ) . ok_or_else (
248+ || format ! ( "Could not move to parent path of {}" , sysroot_dir. display( ) ) ,
249+ ) ?
251250 }
251+
252+ Ok ( sysroot_dir)
252253 }
253254
254255 // Use env::args().next() to get the path of the executable without
0 commit comments