Expand description
Build helper utilities for parsing pkg-config output with proper --whole-archive support.
§Problem
The standard pkg-config crate does not preserve
the ordering of -Wl,--whole-archive and -l flags, which breaks linking for libraries
that use constructor functions (like DPDK’s RTE_INIT macros). Additionally, it doesn’t
properly distinguish between static and dynamic libraries based on file availability.
§Solution
This crate parses pkg-config output directly and:
- Tracks
--whole-archiveregions from the pkg-config output - Auto-detects static library availability by checking if
lib<name>.aexists - Excludes system directories (default:
/usr) so system libs link dynamically - Emits correct cargo link directives with proper modifiers (
static:,+whole-archive,-bundle)
§How Static Detection Works
For each -l<name> flag, the parser checks if lib<name>.a exists in any of the
-L directories. If the .a file exists and is not under a system root directory,
the library is linked statically. Otherwise, it’s linked dynamically (letting the
system linker find the .so).
This mirrors the logic from the pkg-config crate’s
is_static_available() function.
§Link Kinds
Libraries are emitted with one of three link kinds:
| Condition | Link Kind | Cargo Directive |
|---|---|---|
No .a found (or in system dir) | Default | rustc-link-lib=name |
.a exists, outside whole-archive region | Static | rustc-link-lib=static:-bundle=name |
.a exists, inside whole-archive region | WholeArchive | rustc-link-lib=static:+whole-archive,-bundle=name |
§Example
use pkgconf::PkgConfigParser;
// Basic usage with default settings
let parser = PkgConfigParser::new();
parser.probe_and_emit(
["openssl", "libfoo"],
None,
).expect("pkg-config failed");§Customization
use pkgconf::PkgConfigParser;
let parser = PkgConfigParser::new()
// Add additional system roots (libs here link dynamically)
.system_roots(["/usr", "/usr/local"])
// Disable -bundle modifier (for non -sys crates)
.no_bundle(false);Structs§
- PkgConfig
Parser - Parser for pkg-config output that properly handles
--whole-archiveregions and auto-detects static library availability.
Enums§
- Link
Kind - Represents how a library should be linked.
- Linker
Flag - A parsed linker flag from pkg-config output.