Skip to content

Commit 7a442be

Browse files
authored
Merge pull request #82 from danielverkamp/sysroot
Allow overriding system root via environment
2 parents 070b4e9 + e94a533 commit 7a442be

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/lib.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use std::ffi::{OsStr, OsString};
7575
use std::fmt;
7676
use std::io;
7777
use std::ops::{Bound, RangeBounds};
78-
use std::path::{Path, PathBuf};
78+
use std::path::PathBuf;
7979
use std::process::{Command, Output};
8080
use std::str;
8181

@@ -145,7 +145,7 @@ impl fmt::Display for Error {
145145
Install a sysroot for the target platform and configure it via
146146
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
147147
cross-compiling wrapper for pkg-config and set it via
148-
PKG_CONFIG environment variable."
148+
PKG_CONFIG environment variable.",
149149
),
150150
Error::Command {
151151
ref command,
@@ -499,6 +499,25 @@ impl Library {
499499
}
500500
}
501501

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+
502521
let words = split_flags(output);
503522
let parts = words
504523
.iter()
@@ -530,7 +549,7 @@ impl Library {
530549
continue;
531550
}
532551

533-
if statik && is_static_available(val, &dirs) {
552+
if statik && is_static_available(val, &system_roots, &dirs) {
534553
let meta = format!("rustc-link-lib=static={}", val);
535554
config.print_metadata(&meta);
536555
} else {
@@ -583,13 +602,8 @@ fn envify(name: &str) -> String {
583602
}
584603

585604
/// 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 {
587606
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-
};
593607

594608
dirs.iter().any(|dir| {
595609
!system_roots.iter().any(|sys| dir.starts_with(sys)) && dir.join(&libname).exists()
@@ -654,18 +668,23 @@ fn split_flags(output: &[u8]) -> Vec<String> {
654668
#[test]
655669
#[cfg(target_os = "macos")]
656670
fn system_library_mac_test() {
671+
let system_roots = vec![PathBuf::from("/Library"), PathBuf::from("/System")];
672+
657673
assert!(!is_static_available(
658674
"PluginManager",
675+
system_roots,
659676
&[PathBuf::from("/Library/Frameworks")]
660677
));
661678
assert!(!is_static_available(
662679
"python2.7",
680+
system_roots,
663681
&[PathBuf::from(
664682
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config"
665683
)]
666684
));
667685
assert!(!is_static_available(
668686
"ffi_convenience",
687+
system_roots,
669688
&[PathBuf::from(
670689
"/Library/Ruby/Gems/2.0.0/gems/ffi-1.9.10/ext/ffi_c/libffi-x86_64/.libs"
671690
)]
@@ -675,6 +694,7 @@ fn system_library_mac_test() {
675694
if Path::new("/usr/local/lib/libpng16.a").exists() {
676695
assert!(is_static_available(
677696
"png16",
697+
system_roots,
678698
&[PathBuf::from("/usr/local/lib")]
679699
));
680700

@@ -691,7 +711,12 @@ fn system_library_mac_test() {
691711
fn system_library_linux_test() {
692712
assert!(!is_static_available(
693713
"util",
714+
&[PathBuf::from("/usr")],
694715
&[PathBuf::from("/usr/lib/x86_64-linux-gnu")]
695716
));
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+
));
697722
}

0 commit comments

Comments
 (0)