Skip to content
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
65 changes: 54 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

#![doc(html_root_url = "https://docs.rs/pkg-config/0.3")]

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::env;
use std::error;
use std::ffi::{OsStr, OsString};
Expand Down Expand Up @@ -122,6 +122,8 @@ pub struct Config {
pub struct Library {
/// Libraries specified by -l
pub libs: Vec<String>,
/// Libraries which are to be included with --whole-archive
pub whole_archive_libs: Vec<String>,
/// Library search paths specified by -L
pub link_paths: Vec<PathBuf>,
/// Library file paths specified without -l
Expand Down Expand Up @@ -790,6 +792,7 @@ impl Library {
fn new() -> Library {
Library {
libs: Vec::new(),
whole_archive_libs: Vec::new(),
link_paths: Vec::new(),
link_files: Vec::new(),
include_paths: Vec::new(),
Expand Down Expand Up @@ -894,6 +897,9 @@ impl Library {

let words = split_flags(output);

let mut whole_archive_depth = 0;
let mut added_libs = HashSet::new();

// Handle single-character arguments like `-I/usr/include`
let parts = words
.iter()
Expand Down Expand Up @@ -921,19 +927,46 @@ impl Library {
continue;
}

if val.starts_with(':') {
// static libraries with +whole-archive only
let whole_archive = statik && whole_archive_depth > 0;

let val = whole_archive
.then_some(val)
.and_then(|val| val.strip_prefix(":lib"))
.and_then(|val| val.strip_suffix(".a"))
.unwrap_or(val);

// adding a library multiple times with different modifiers is not allowed by
// rustc, but can be allowed by c compilers
if added_libs.contains(&val) {
Comment on lines +939 to +941

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This de-dup should probably go into a separate commit with explanation (can be in the same PR).

continue;
}

let mut push_to_whole_archive = false;
let meta = if val.starts_with(':') {
// Pass this flag to linker directly.
let meta = format!("rustc-link-arg={}{}", flag, val);
config.print_metadata(&meta);
} else if statik && is_static_available(val, &system_roots, &dirs) {
let meta = format!("rustc-link-lib=static={}", val);
config.print_metadata(&meta);
format!("rustc-link-arg={}{}", flag, val)
} else {
let meta = format!("rustc-link-lib={}", val);
config.print_metadata(&meta);
}
let static_available = is_static_available(val, &system_roots, &dirs);
added_libs.insert(val);

if whole_archive {
push_to_whole_archive = true;
format!("rustc-link-lib=static:+whole-archive={}", val)
} else if statik && static_available {
format!("rustc-link-lib=static={}", val)
} else {
format!("rustc-link-lib={}", val)
}
};

self.libs.push(val.to_string());
config.print_metadata(&meta);

if push_to_whole_archive {
self.whole_archive_libs.push(val.to_string());
} else {
self.libs.push(val.to_string());
}
}
"-D" => {
let mut iter = val.split('=');
Expand All @@ -946,6 +979,16 @@ impl Library {
let meta = format!("rustc-link-arg=-Wl,-u,{}", val);
config.print_metadata(&meta);
}
"-W" => {
if let Some(val) = val.strip_prefix("l,") {
if val == "--whole-archive" {
whole_archive_depth += 1;
} else if val == "--no-whole-archive" && whole_archive_depth > 0 {
whole_archive_depth -= 1;
}
}
}

_ => {}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/duplicate.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
prefix=/usr/local

Name: duplicate
Version: 4.2.0
Description: Duplicate Libraries Example
Libs: -L${prefix}/lib -Wl,-rpath,${prefix}/lib -lrpath -lrpath
Cflags: -I${prefix}/include
24 changes: 24 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,27 @@ fn rpath() {
.ld_args
.contains(&vec!["-rpath".to_string(), "/usr/local/lib".to_string(),]));
}

#[test]
fn duplicate() {
let _g = LOCK.lock();
reset();
let lib = find("duplicate").unwrap();

let lib_count = lib.libs.iter().filter(|&l| l == "rpath").count();

assert_eq!(lib_count, 1);
}

#[test]
fn whole_archive() {
let _g = LOCK.lock();
reset();

let lib = pkg_config::Config::new()
.statik(true)
.probe("wholearchive")
.unwrap();

assert_eq!(lib.whole_archive_libs, &["rte_eal"]);
}
4 changes: 4 additions & 0 deletions tests/wholearchive.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name: wholearchive
Description: Whole-archive test fixture
Version: 1.0
Libs.private: -Wl,--whole-archive -l:librte_eal.a -Wl,--no-whole-archive
Loading