@@ -75,7 +75,7 @@ use std::ffi::{OsStr, OsString};
75
75
use std:: fmt;
76
76
use std:: io;
77
77
use std:: ops:: { Bound , RangeBounds } ;
78
- use std:: path:: { Path , PathBuf } ;
78
+ use std:: path:: PathBuf ;
79
79
use std:: process:: { Command , Output } ;
80
80
use std:: str;
81
81
@@ -145,7 +145,7 @@ impl fmt::Display for Error {
145
145
Install a sysroot for the target platform and configure it via
146
146
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
147
147
cross-compiling wrapper for pkg-config and set it via
148
- PKG_CONFIG environment variable."
148
+ PKG_CONFIG environment variable." ,
149
149
) ,
150
150
Error :: Command {
151
151
ref command,
@@ -499,6 +499,25 @@ impl Library {
499
499
}
500
500
}
501
501
502
+ let system_roots = if cfg ! ( target_os = "macos" ) {
503
+ vec ! [ PathBuf :: from( "/Library" ) , PathBuf :: from( "/System" ) ]
504
+ } else {
505
+ let sysroot = config
506
+ . env_var_os ( "PKG_CONFIG_SYSROOT_DIR" )
507
+ . or_else ( || config. env_var_os ( "SYSROOT" ) )
508
+ . map ( PathBuf :: from) ;
509
+
510
+ if cfg ! ( target_os = "windows" ) {
511
+ if let Some ( sysroot) = sysroot {
512
+ vec ! [ sysroot]
513
+ } else {
514
+ vec ! [ ]
515
+ }
516
+ } else {
517
+ vec ! [ sysroot. unwrap_or_else( || PathBuf :: from( "/usr" ) ) ]
518
+ }
519
+ } ;
520
+
502
521
let words = split_flags ( output) ;
503
522
let parts = words
504
523
. iter ( )
@@ -530,7 +549,7 @@ impl Library {
530
549
continue ;
531
550
}
532
551
533
- if statik && is_static_available ( val, & dirs) {
552
+ if statik && is_static_available ( val, & system_roots , & dirs) {
534
553
let meta = format ! ( "rustc-link-lib=static={}" , val) ;
535
554
config. print_metadata ( & meta) ;
536
555
} else {
@@ -583,13 +602,8 @@ fn envify(name: &str) -> String {
583
602
}
584
603
585
604
/// System libraries should only be linked dynamically
586
- fn is_static_available ( name : & str , dirs : & [ PathBuf ] ) -> bool {
605
+ fn is_static_available ( name : & str , system_roots : & [ PathBuf ] , dirs : & [ PathBuf ] ) -> bool {
587
606
let libname = format ! ( "lib{}.a" , name) ;
588
- let system_roots = if cfg ! ( target_os = "macos" ) {
589
- vec ! [ Path :: new( "/Library" ) , Path :: new( "/System" ) ]
590
- } else {
591
- vec ! [ Path :: new( "/usr" ) ]
592
- } ;
593
607
594
608
dirs. iter ( ) . any ( |dir| {
595
609
!system_roots. iter ( ) . any ( |sys| dir. starts_with ( sys) ) && dir. join ( & libname) . exists ( )
@@ -654,18 +668,23 @@ fn split_flags(output: &[u8]) -> Vec<String> {
654
668
#[ test]
655
669
#[ cfg( target_os = "macos" ) ]
656
670
fn system_library_mac_test ( ) {
671
+ let system_roots = vec ! [ PathBuf :: from( "/Library" ) , PathBuf :: from( "/System" ) ] ;
672
+
657
673
assert ! ( !is_static_available(
658
674
"PluginManager" ,
675
+ system_roots,
659
676
& [ PathBuf :: from( "/Library/Frameworks" ) ]
660
677
) ) ;
661
678
assert ! ( !is_static_available(
662
679
"python2.7" ,
680
+ system_roots,
663
681
& [ PathBuf :: from(
664
682
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config"
665
683
) ]
666
684
) ) ;
667
685
assert ! ( !is_static_available(
668
686
"ffi_convenience" ,
687
+ system_roots,
669
688
& [ PathBuf :: from(
670
689
"/Library/Ruby/Gems/2.0.0/gems/ffi-1.9.10/ext/ffi_c/libffi-x86_64/.libs"
671
690
) ]
@@ -675,6 +694,7 @@ fn system_library_mac_test() {
675
694
if Path :: new ( "/usr/local/lib/libpng16.a" ) . exists ( ) {
676
695
assert ! ( is_static_available(
677
696
"png16" ,
697
+ system_roots,
678
698
& [ PathBuf :: from( "/usr/local/lib" ) ]
679
699
) ) ;
680
700
@@ -691,7 +711,12 @@ fn system_library_mac_test() {
691
711
fn system_library_linux_test ( ) {
692
712
assert ! ( !is_static_available(
693
713
"util" ,
714
+ & [ PathBuf :: from( "/usr" ) ] ,
694
715
& [ PathBuf :: from( "/usr/lib/x86_64-linux-gnu" ) ]
695
716
) ) ;
696
- assert ! ( !is_static_available( "dialog" , & [ PathBuf :: from( "/usr/lib" ) ] ) ) ;
717
+ assert ! ( !is_static_available(
718
+ "dialog" ,
719
+ & [ PathBuf :: from( "/usr" ) ] ,
720
+ & [ PathBuf :: from( "/usr/lib" ) ]
721
+ ) ) ;
697
722
}
0 commit comments