Skip to content

Commit fe165fc

Browse files
dginevclaude
andcommitted
build: opt-in static libxml2 link via LIBXML2_STATIC (v0.3.13)
When LIBXML2_STATIC is set, build.rs probes via pkg-config with .statik(true), emitting static=xml2 (+ transitive -lm), for a SONAME-independent self-contained downstream binary. Default (env unset) is unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent da0f3ef commit fe165fc

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# Change Log
22

3-
## [0.3.13] (in development)
3+
## [0.3.13] (2026-06-11)
4+
5+
### Added
6+
7+
* Opt-in **static linking** of libxml2. Set the `LIBXML2_STATIC` environment
8+
variable and `build.rs` probes via pkg-config with `.statik(true)`,
9+
emitting `cargo:rustc-link-lib=static=xml2` (plus the transitive `-lm`).
10+
Point `PKG_CONFIG_PATH` at a non-system prefix holding a PIC `libxml2.a`
11+
to get a binary with no runtime `libxml2.so` dependency — independent of
12+
the host's libxml2 SONAME, which bumped `.so.2``.so.16` at libxml2 2.14.
13+
Unset (the default) is unchanged: a normal dynamic link against the host
14+
libxml2.
415

516
## [0.3.12] (2026-05-23)
617

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libxml"
3-
version = "0.3.12"
3+
version = "0.3.13"
44
edition = "2024"
55
authors = ["Andreas Franzén <andreas@devil.se>", "Deyan Ginev <deyan.ginev@gmail.com>","Jan Frederik Schaefer <j.schaefer@jacobs-university.de>"]
66
description = "A Rust wrapper for libxml2 - the XML C parser and toolkit developed for the Gnome project"

build.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,18 @@ fn find_libxml2() -> Option<ProbedLib> {
4949
all(target_family = "windows", target_env = "gnu")
5050
))]
5151
{
52-
let lib = pkg_config::Config::new()
52+
// Opt-in static link: when LIBXML2_STATIC is set and PKG_CONFIG_PATH points
53+
// at a non-system prefix carrying a PIC `libxml2.a`, `.statik(true)` makes
54+
// pkg-config emit `cargo:rustc-link-lib=static=xml2` plus the transitive
55+
// `-lm` (which stays dynamic). pkg-config's static guard refuses a `static=`
56+
// for a /usr/lib system path, so the non-system prefix is what enables this.
57+
// Unset (the default) is unchanged: a normal dynamic link against the host
58+
// libxml2. Used for the self-contained, SONAME-independent release binary.
59+
let mut cfg = pkg_config::Config::new();
60+
if std::env::var_os("LIBXML2_STATIC").is_some() {
61+
cfg.statik(true);
62+
}
63+
let lib = cfg
5364
.probe("libxml-2.0")
5465
.expect("Couldn't find libxml2 via pkg-config");
5566
return Some(ProbedLib {
@@ -92,6 +103,8 @@ fn generate_bindings(header_dirs: Vec<PathBuf>, output_path: &Path) {
92103
}
93104

94105
fn main() {
106+
// Re-run if the opt-in static toggle flips (see find_libxml2's static branch).
107+
println!("cargo:rerun-if-env-changed=LIBXML2_STATIC");
95108
let bindings_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("bindings.rs");
96109
// declare availability of config variable (without setting it)
97110
println!("cargo::rustc-check-cfg=cfg(libxml_older_than_2_12)");

0 commit comments

Comments
 (0)