@@ -15,6 +15,7 @@ use lddtree::Library;
15
15
use std:: borrow:: Cow ;
16
16
use std:: collections:: HashMap ;
17
17
use std:: path:: { Path , PathBuf } ;
18
+ use std:: process:: { Command , Stdio } ;
18
19
19
20
/// The way the rust code is used in the wheel
20
21
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -272,7 +273,8 @@ impl BuildContext {
272
273
}
273
274
} ) ?;
274
275
let external_libs = if should_repair && !self . editable {
275
- get_external_libs ( & artifact, & policy) . with_context ( || {
276
+ let sysroot = get_sysroot_path ( & self . target ) ?;
277
+ get_external_libs ( & artifact, & policy, sysroot) . with_context ( || {
276
278
if let Some ( platform_tag) = platform_tag {
277
279
format ! ( "Error repairing wheel for {} compliance" , platform_tag)
278
280
} else {
@@ -685,6 +687,50 @@ fn relpath(to: &Path, from: &Path) -> PathBuf {
685
687
result
686
688
}
687
689
690
+ /// Get sysroot path from target C compiler
691
+ ///
692
+ /// Currently only gcc is supported, clang doesn't have a `--print-sysroot` option
693
+ /// TODO: allow specify sysroot from environment variable?
694
+ fn get_sysroot_path ( target : & Target ) -> Result < PathBuf > {
695
+ use crate :: target:: get_host_target;
696
+
697
+ let host_triple = get_host_target ( ) ?;
698
+ let target_triple = target. target_triple ( ) ;
699
+ if host_triple != target_triple {
700
+ let mut build = cc:: Build :: new ( ) ;
701
+ build
702
+ // Suppress cargo metadata for example env vars printing
703
+ . cargo_metadata ( false )
704
+ // opt_level, host and target are required
705
+ . opt_level ( 0 )
706
+ . host ( & host_triple)
707
+ . target ( target_triple) ;
708
+ let compiler = build
709
+ . try_get_compiler ( )
710
+ . with_context ( || format ! ( "Failed to get compiler for {}" , target_triple) ) ?;
711
+ let path = compiler. path ( ) ;
712
+ let out = Command :: new ( path)
713
+ . arg ( "--print-sysroot" )
714
+ . stdout ( Stdio :: piped ( ) )
715
+ . stderr ( Stdio :: null ( ) )
716
+ . output ( )
717
+ . with_context ( || format ! ( "Failed to run `{} --print-sysroot`" , path. display( ) ) ) ?;
718
+ if out. status . success ( ) {
719
+ let sysroot = String :: from_utf8 ( out. stdout )
720
+ . context ( "Failed to read the sysroot path" ) ?
721
+ . trim ( )
722
+ . to_owned ( ) ;
723
+ return Ok ( PathBuf :: from ( sysroot) ) ;
724
+ } else {
725
+ bail ! (
726
+ "Failed to get the sysroot path: {}" ,
727
+ String :: from_utf8( out. stderr) ?
728
+ ) ;
729
+ }
730
+ }
731
+ Ok ( PathBuf :: from ( "/" ) )
732
+ }
733
+
688
734
#[ cfg( test) ]
689
735
mod test {
690
736
use super :: relpath;
0 commit comments