Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to preserve libs flags from system libs (PKG_CONFIG_ALLOW_SYSTEM_LIBS) #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ type FnBuildInternal =
pub struct Config {
env: EnvVariables,
build_internals: HashMap<String, Box<FnBuildInternal>>,
print_system_libs: bool,
}

impl Default for Config {
Expand All @@ -299,6 +300,18 @@ impl Config {
Self {
env,
build_internals: HashMap::new(),
print_system_libs: false,
}
}

/// By default `pkg-config` strips libs flags pointing to standard system path, such as `-L/usr/lib64`.
/// Calling this function will ensure that those flags are preserved.
/// This is equivalent of setting the `PKG_CONFIG_ALLOW_SYSTEM_LIBS` environment variable when using `pkg-config` directly.
pub fn preserve_system_libs(self) -> Self {
Self {
env: self.env,
build_internals: self.build_internals,
print_system_libs: true,
}
}

Expand Down Expand Up @@ -334,6 +347,7 @@ impl Config {
Self {
env: self.env,
build_internals,
print_system_libs: self.print_system_libs,
}
}

Expand Down Expand Up @@ -492,7 +506,7 @@ impl Config {
} else {
match pkg_config::Config::new()
.atleast_version(&version)
.print_system_libs(false)
.print_system_libs(self.print_system_libs)
.cargo_metadata(false)
.probe(lib_name)
{
Expand Down
16 changes: 16 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn create_config(path: &str, env: Vec<(&'static str, &'static str)>) -> Config {
"PKG_CONFIG_PATH",
&env::current_dir().unwrap().join("src").join("tests"),
);
env::set_var("PKG_CONFIG_SYSTEM_LIBRARY_PATH", "/usr/lib64/");
}

let mut hash = HashMap::new();
Expand Down Expand Up @@ -756,3 +757,18 @@ fn build_internal_override_name() {
assert_eq!(called, true);
assert!(libraries.get("test_lib").is_some());
}

#[test]
fn system_libs() {
let config = create_config("toml-testlib64", vec![]);
let (libraries, _) = config.probe_full().unwrap();
let testlib = libraries.get("testlib64").unwrap();
// Link path is stripped as it's a system lib (PKG_CONFIG_SYSTEM_LIBRARY_PATH)
assert!(testlib.link_paths.is_empty());

// Request system libs to not be stripped
let config = create_config("toml-testlib64", vec![]).preserve_system_libs();
let (libraries, _) = config.probe_full().unwrap();
let testlib = libraries.get("testlib64").unwrap();
assert_eq!(testlib.link_paths[0], Path::new("/usr/lib64"));
}
2 changes: 1 addition & 1 deletion src/tests/testlib-2.0.pc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ includedir=${prefix}/include/testlib
Name: Test Library
Description: A fake library to test pkg-config.
Version: 2.0.0
Libs: L${libdir} -ltest
Libs: -L${libdir} -ltest
Cflags: -I${includedir}
2 changes: 1 addition & 1 deletion src/tests/testlib-3.0.pc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ includedir=${prefix}/include/testlib
Name: Test Library
Description: A fake library to test pkg-config.
Version: 3.0.0
Libs: L${libdir} -ltest
Libs: -L${libdir} -ltest
Cflags: -I${includedir}
10 changes: 10 additions & 0 deletions src/tests/testlib64.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib64/
includedir=${prefix}/include/testlib

Name: Test Library
Description: A fake library to test pkg-config.
Version: 1.2.3
Libs: -L${libdir} -ltest
Cflags: -I${includedir}
2 changes: 2 additions & 0 deletions src/tests/toml-testlib64/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package.metadata.system-deps]
testlib64 = "1"