Skip to content

Commit

Permalink
add helpers aggregating settings from all libraries
Browse files Browse the repository at this point in the history
Fix #28
  • Loading branch information
Guillaume Desmottes authored and gdesmott committed Feb 10, 2021
1 parent 5e2a6fb commit 422c1bc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ strum = "0.20"
strum_macros = "0.20"
thiserror = "1"
anyhow = "1.0"
itertools = "0.10"

[dev-dependencies]
lazy_static = "1"
Expand Down
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ extern crate lazy_static;
mod test;

use heck::{ShoutySnakeCase, SnakeCase};
use itertools::Itertools;
use std::collections::HashMap;
use std::env;
use std::fmt;
Expand Down Expand Up @@ -213,6 +214,67 @@ impl Libraries {
self.libs.iter().map(|(k, v)| (k.as_str(), v))
}

fn aggregate_str<F: Fn(&Library) -> &Vec<String>>(
&self,
getter: F,
) -> impl Iterator<Item = &str> {
self.libs
.values()
.map(|l| getter(l))
.flatten()
.map(|s| s.as_str())
.sorted()
.dedup()
}

fn aggregate_path_buf<F: Fn(&Library) -> &Vec<PathBuf>>(
&self,
getter: F,
) -> impl Iterator<Item = &PathBuf> {
self.libs
.values()
.map(|l| getter(l))
.flatten()
.sorted()
.dedup()
}

/// An iterator returning each [Library::libs] of each library, removing duplicates.
pub fn all_libs(&self) -> impl Iterator<Item = &str> {
self.aggregate_str(|l| &l.libs)
}

/// An iterator returning each [Library::link_paths] of each library, removing duplicates.
pub fn all_link_paths(&self) -> impl Iterator<Item = &PathBuf> {
self.aggregate_path_buf(|l| &l.link_paths)
}

/// An iterator returning each [Library::frameworks] of each library, removing duplicates.
pub fn all_frameworks(&self) -> impl Iterator<Item = &str> {
self.aggregate_str(|l| &l.frameworks)
}

/// An iterator returning each [Library::framework_paths] of each library, removing duplicates.
pub fn all_framework_paths(&self) -> impl Iterator<Item = &PathBuf> {
self.aggregate_path_buf(|l| &l.framework_paths)
}

/// An iterator returning each [Library::include_paths] of each library, removing duplicates.
pub fn all_include_paths(&self) -> impl Iterator<Item = &PathBuf> {
self.aggregate_path_buf(|l| &l.include_paths)
}

/// An iterator returning each [Library::defines] of each library, removing duplicates.
pub fn all_defines(&self) -> impl Iterator<Item = (&str, &Option<String>)> {
self.libs
.values()
.map(|l| l.defines.iter())
.flatten()
.map(|(k, v)| (k.as_str(), v))
.sorted()
.dedup()
}

fn add(&mut self, name: &str, lib: Library) {
self.libs.insert(name.to_string(), lib);
}
Expand Down
37 changes: 37 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,40 @@ fn optional() {
// testlib is no longer optional if enabling v5
toml_pkg_config_err_version("toml-optional", "5.0", vec![("CARGO_FEATURE_V5", "")]);
}

#[test]
fn aggregate() {
let (libraries, _) = toml("toml-two-libs", vec![]).unwrap();

assert_eq!(
libraries.all_libs().collect::<Vec<&str>>(),
vec!["test", "test2"]
);
assert_eq!(
libraries.all_link_paths().collect::<Vec<&PathBuf>>(),
vec![Path::new("/usr/lib"), Path::new("/usr/lib64")]
);
assert_eq!(
libraries.all_frameworks().collect::<Vec<&str>>(),
vec!["someframework", "someotherframework"]
);
assert_eq!(
libraries.all_framework_paths().collect::<Vec<&PathBuf>>(),
vec![Path::new("/usr/lib"), Path::new("/usr/lib64")]
);
assert_eq!(
libraries.all_include_paths().collect::<Vec<&PathBuf>>(),
vec![
Path::new("/usr/include/testanotherlib"),
Path::new("/usr/include/testlib")
]
);
assert_eq!(
libraries.all_defines().collect::<Vec<_>>(),
vec![
("AWESOME", &None),
("BADGER", &Some("yes".into())),
("GREAT", &None)
]
);
}
12 changes: 12 additions & 0 deletions src/tests/testanotherlib.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib/
libdir2=${exec_prefix}/lib64/
includedir=${prefix}/include/testlib
includedir2=${prefix}/include/testanotherlib

Name: Test Another Library
Description: Another fake library to test pkg-config.
Version: 1.2.3
Libs: -L${libdir} -ltest -L${libdir2} -ltest2 -F${libdir} -framework someframework -F${libdir2} -framework someotherframework
Cflags: -I${includedir} -I${includedir2} -DBADGER=yes -DAWESOME -DGREAT
3 changes: 3 additions & 0 deletions src/tests/toml-two-libs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package.metadata.system-deps]
testlib = { version = "1" }
testanotherlib = { version = "1" }

0 comments on commit 422c1bc

Please sign in to comment.