@@ -227,28 +227,29 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
227
227
) ) ?;
228
228
229
229
// 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 ( ) ) {
231
231
dir. parent ( ) // chop off `$target`
232
232
. 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`
244
234
. 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
+ } ) ?
249
238
} 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
+ ) ?
251
250
}
251
+
252
+ Ok ( sysroot_dir)
252
253
}
253
254
254
255
// Use env::args().next() to get the path of the executable without
0 commit comments