Skip to main content

PkgConfigParser

Struct PkgConfigParser 

Source
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

Source

pub fn new() -> Self

Creates a new parser with default settings.

Defaults:

  • no_bundle: true (adds -bundle modifier)
  • system_roots: ["/usr"]
  • force_whole_archive: [] (empty)
Source

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.

Source

pub fn system_roots<I, P>(self, roots: I) -> Self
where I: IntoIterator<Item = P>, P: Into<PathBuf>,

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"]);
Source

pub fn force_whole_archive<I, S>(self, libs: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

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",
    ]);
Source

pub fn run_pkg_config<I, S>( packages: I, pkg_config_path: Option<&str>, ) -> Result<String, String>
where I: IntoIterator<Item = S>, S: AsRef<str>,

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 as PKG_CONFIG_PATH environment variable
§Errors

Returns an error if pkg-config is not found or if any package is not found.

Source

pub fn parse(&self, pkg_config_output: &str) -> Vec<LinkerFlag>

Parse pkg-config output into structured linker flags.

This function:

  • Tracks --whole-archive and --no-whole-archive markers
  • 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.
Source

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.

Source

pub fn probe_and_emit<I, S>( &self, packages: I, pkg_config_path: Option<&str>, ) -> Result<(), String>
where I: IntoIterator<Item = S>, S: AsRef<str>,

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 query
  • pkg_config_path - Optional PKG_CONFIG_PATH override
§Example
use pkgconf::PkgConfigParser;

PkgConfigParser::new()
    .probe_and_emit(
        ["openssl", "libfoo"],
        None,
    )
    .expect("pkg-config failed");
Source

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

Source§

fn clone(&self) -> PkgConfigParser

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PkgConfigParser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PkgConfigParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.