Skip to main content

Crate pkgconf

Crate pkgconf 

Source
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-archive regions from the pkg-config output
  • Auto-detects static library availability by checking if lib<name>.a exists
  • 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.

Libraries are emitted with one of three link kinds:

ConditionLink KindCargo Directive
No .a found (or in system dir)Defaultrustc-link-lib=name
.a exists, outside whole-archive regionStaticrustc-link-lib=static:-bundle=name
.a exists, inside whole-archive regionWholeArchiverustc-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§

PkgConfigParser
Parser for pkg-config output that properly handles --whole-archive regions and auto-detects static library availability.

Enums§

LinkKind
Represents how a library should be linked.
LinkerFlag
A parsed linker flag from pkg-config output.