Skip to content

Commit 81468be

Browse files
danielverkampsdroege
authored andcommitted
Allow overriding system root via environment
Use the PKG_CONFIG_SYSROOT_DIR (preferred) or SYSROOT (fallback) environment variables to override the default path used to determine whether a library is a system library or not (/usr). This enables cross-compilation scenarios where the system root is not a subdirectory of /usr. Specifically, when building Rust packages in the Chomium OS SDK, each board's sysroot is located in /build/$BOARD rather than a subdirectory of /usr. Additionally, cros_sdk only specifies SYSROOT (PKG_CONFIG_SYSROOT_DIR is set by the pkg-config-$BOARD cross compilation helpers, but that isn't available in pkg-config-rs). This has only been tested on Linux, as I don't have a macOS machine handy, but I believe the macOS path should work as before (it does not consider the new sysroot variables). Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
1 parent 070b4e9 commit 81468be

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/lib.rs

Lines changed: 27 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,17 @@ 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+
.unwrap_or_else(|| PathBuf::from("/usr"));
510+
vec![sysroot]
511+
};
512+
502513
let words = split_flags(output);
503514
let parts = words
504515
.iter()
@@ -530,7 +541,7 @@ impl Library {
530541
continue;
531542
}
532543

533-
if statik && is_static_available(val, &dirs) {
544+
if statik && is_static_available(val, &system_roots, &dirs) {
534545
let meta = format!("rustc-link-lib=static={}", val);
535546
config.print_metadata(&meta);
536547
} else {
@@ -583,13 +594,8 @@ fn envify(name: &str) -> String {
583594
}
584595

585596
/// System libraries should only be linked dynamically
586-
fn is_static_available(name: &str, dirs: &[PathBuf]) -> bool {
597+
fn is_static_available(name: &str, system_roots: &[PathBuf], dirs: &[PathBuf]) -> bool {
587598
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-
};
593599

594600
dirs.iter().any(|dir| {
595601
!system_roots.iter().any(|sys| dir.starts_with(sys)) && dir.join(&libname).exists()
@@ -654,18 +660,23 @@ fn split_flags(output: &[u8]) -> Vec<String> {
654660
#[test]
655661
#[cfg(target_os = "macos")]
656662
fn system_library_mac_test() {
663+
let system_roots = vec![PathBuf::from("/Library"), PathBuf::from("/System")];
664+
657665
assert!(!is_static_available(
658666
"PluginManager",
667+
system_roots,
659668
&[PathBuf::from("/Library/Frameworks")]
660669
));
661670
assert!(!is_static_available(
662671
"python2.7",
672+
system_roots,
663673
&[PathBuf::from(
664674
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config"
665675
)]
666676
));
667677
assert!(!is_static_available(
668678
"ffi_convenience",
679+
system_roots,
669680
&[PathBuf::from(
670681
"/Library/Ruby/Gems/2.0.0/gems/ffi-1.9.10/ext/ffi_c/libffi-x86_64/.libs"
671682
)]
@@ -675,6 +686,7 @@ fn system_library_mac_test() {
675686
if Path::new("/usr/local/lib/libpng16.a").exists() {
676687
assert!(is_static_available(
677688
"png16",
689+
system_roots,
678690
&[PathBuf::from("/usr/local/lib")]
679691
));
680692

@@ -691,7 +703,12 @@ fn system_library_mac_test() {
691703
fn system_library_linux_test() {
692704
assert!(!is_static_available(
693705
"util",
706+
&[PathBuf::from("/usr")],
694707
&[PathBuf::from("/usr/lib/x86_64-linux-gnu")]
695708
));
696-
assert!(!is_static_available("dialog", &[PathBuf::from("/usr/lib")]));
709+
assert!(!is_static_available(
710+
"dialog",
711+
&[PathBuf::from("/usr")],
712+
&[PathBuf::from("/usr/lib")]
713+
));
697714
}

0 commit comments

Comments
 (0)