pub struct PkgConfigParser { /* private fields */ }Expand description
Parser for pkg-config output that properly handles --whole-archive regions
and auto-detects static library availability.
§Usage
use pkgconf::PkgConfigParser;
PkgConfigParser::new()
.probe_and_emit(
["openssl", "libfoo"],
None,
)
.expect("pkg-config failed");Implementations§
Source§impl PkgConfigParser
impl PkgConfigParser
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new parser with default settings.
Defaults:
no_bundle:true(adds-bundlemodifier)system_roots:["/usr"]force_whole_archive:[](empty)
Sourcepub fn no_bundle(self, enabled: bool) -> Self
pub fn no_bundle(self, enabled: bool) -> Self
Sets whether to add the -bundle modifier to static libraries.
When true (default), static libraries are emitted with -bundle
(e.g., static:-bundle=foo), which prevents them from being
re-exported when this crate is used as an rlib dependency.
Set to false if you want downstream crates to also link against
these static libraries.
Sourcepub fn system_roots<I, P>(self, roots: I) -> Self
pub fn system_roots<I, P>(self, roots: I) -> Self
Sets the system root directories.
Libraries whose .a files are found under these directories will
be linked dynamically (using LinkKind::Default), even if the
static library exists. This is useful for system libraries like
lz4, numa, or openssl that should use the system’s shared
library rather than a static copy.
Default: ["/usr"]
§Example
use pkgconf::PkgConfigParser;
let parser = PkgConfigParser::new()
.system_roots(["/usr", "/usr/local", "/opt/homebrew"]);Sourcepub fn force_whole_archive<I, S>(self, libs: I) -> Self
pub fn force_whole_archive<I, S>(self, libs: I) -> Self
Sets libraries that should always use +whole-archive.
These libraries will be linked with LinkKind::WholeArchive even if
they don’t appear inside a --whole-archive region in the pkg-config
output. This is necessary for libraries that use constructor functions
(like __attribute__((constructor)) or DPDK’s RTE_INIT macros)
where the symbols would otherwise be discarded by the linker.
§Example
use pkgconf::PkgConfigParser;
// Force whole-archive for libraries with constructor functions
let parser = PkgConfigParser::new()
.force_whole_archive([
"mylib_with_constructors",
]);Sourcepub fn run_pkg_config<I, S>(
packages: I,
pkg_config_path: Option<&str>,
) -> Result<String, String>
pub fn run_pkg_config<I, S>( packages: I, pkg_config_path: Option<&str>, ) -> Result<String, String>
Runs pkg-config --static --libs and returns the raw output.
§Arguments
packages- Package names to query (e.g.,["spdk_env_dpdk", "libdpdk"])pkg_config_path- Optional path to set asPKG_CONFIG_PATHenvironment variable
§Errors
Returns an error if pkg-config is not found or if any package is not found.
Sourcepub fn parse(&self, pkg_config_output: &str) -> Vec<LinkerFlag>
pub fn parse(&self, pkg_config_output: &str) -> Vec<LinkerFlag>
Parse pkg-config output into structured linker flags.
This function:
- Tracks
--whole-archiveand--no-whole-archivemarkers - Checks if static libraries (.a) exist for each library
- Libraries with .a in non-system dirs → Static or WholeArchive
- Libraries without .a (or in system dirs) → Default (let linker find .so)
- If a library appears first outside, then inside a whole-archive region, it will be upgraded to WholeArchive.
Sourcepub fn emit_cargo_metadata(&self, flags: &[LinkerFlag])
pub fn emit_cargo_metadata(&self, flags: &[LinkerFlag])
Emits cargo metadata directives for the parsed flags.
Outputs cargo:rustc-link-search, cargo:rustc-link-lib, and
cargo:rustc-link-arg directives to stdout for cargo to consume.
Usually called via probe_and_emit, but can
be called separately if you need to inspect or modify the parsed flags.
Sourcepub fn probe_and_emit<I, S>(
&self,
packages: I,
pkg_config_path: Option<&str>,
) -> Result<(), String>
pub fn probe_and_emit<I, S>( &self, packages: I, pkg_config_path: Option<&str>, ) -> Result<(), String>
Runs pkg-config, parses the output, and emits cargo metadata.
This is the main entry point for most use cases. It combines
run_pkg_config, parse,
and emit_cargo_metadata.
§Arguments
packages- Package names to querypkg_config_path- OptionalPKG_CONFIG_PATHoverride
§Example
use pkgconf::PkgConfigParser;
PkgConfigParser::new()
.probe_and_emit(
["openssl", "libfoo"],
None,
)
.expect("pkg-config failed");Sourcepub fn add_extra_lib(&self, name: &str, kind: LinkKind)
pub fn add_extra_lib(&self, name: &str, kind: LinkKind)
Emits a cargo link directive for an additional library.
Use this to add libraries that aren’t in the pkg-config output but are still needed for linking.
§Example
use pkgconf::{PkgConfigParser, LinkKind};
let parser = PkgConfigParser::new();
parser.add_extra_lib("custom_lib", LinkKind::Static);Trait Implementations§
Source§impl Clone for PkgConfigParser
impl Clone for PkgConfigParser
Source§fn clone(&self) -> PkgConfigParser
fn clone(&self) -> PkgConfigParser
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more