Skip to content

Commit 5fa9de0

Browse files
zerocom38dginev
authored andcommitted
check if libxml2 includes are in a libxml2 subdirectory
1 parent ecfa38c commit 5fa9de0

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

build.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{env, fs, path::{Path, PathBuf}};
1+
use std::{
2+
env, fs,
3+
path::{Path, PathBuf},
4+
};
25

36
struct ProbedLib {
47
version: String,
@@ -40,20 +43,24 @@ fn find_libxml2() -> Option<ProbedLib> {
4043
);
4144
None
4245
} else {
43-
#[cfg(any(target_family = "unix", target_os = "macos", all(target_family="windows", target_env="gnu")))]
46+
#[cfg(any(
47+
target_family = "unix",
48+
target_os = "macos",
49+
all(target_family = "windows", target_env = "gnu")
50+
))]
4451
{
4552
let lib = pkg_config::Config::new()
4653
.probe("libxml-2.0")
4754
.expect("Couldn't find libxml2 via pkg-config");
4855
return Some(ProbedLib {
4956
include_paths: lib.include_paths,
5057
version: lib.version,
51-
})
58+
});
5259
}
5360

5461
#[cfg(all(target_family = "windows", target_env = "msvc"))]
5562
{
56-
if let Some(meta) = vcpkg_dep::vcpkg_find_libxml2() {
63+
if let Some(meta) = vcpkg_dep::vcpkg_find_libxml2() {
5764
return Some(meta);
5865
} else {
5966
eprintln!("vcpkg did not succeed in finding libxml2.");
@@ -71,11 +78,12 @@ fn generate_bindings(header_dirs: Vec<PathBuf>, output_path: &Path) {
7178
// invalidate build as soon as the wrapper changes
7279
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
7380
.layout_tests(true)
74-
.clang_args(&["-DPKG-CONFIG", "-DLIBXML_C14N_ENABLED", "-DLIBXML_OUTPUT_ENABLED"])
75-
.clang_args(
76-
header_dirs.iter()
77-
.map(|dir| format!("-I{}", dir.display()))
78-
);
81+
.clang_args(&[
82+
"-DPKG-CONFIG",
83+
"-DLIBXML_C14N_ENABLED",
84+
"-DLIBXML_OUTPUT_ENABLED",
85+
])
86+
.clang_args(header_dirs.iter().map(|dir| format!("-I{}", dir.display())));
7987
bindings
8088
.generate()
8189
.expect("failed to generate bindings with bindgen")
@@ -92,10 +100,13 @@ fn main() {
92100
// if we could find header files, generate fresh bindings from them
93101
generate_bindings(probed_lib.include_paths, &bindings_path);
94102
// and expose the libxml2 version to the code
95-
let version_parts: Vec<i32> = probed_lib.version.split('.')
96-
.map(|part| part.parse::<i32>().unwrap_or(-1)).collect();
97-
let older_than_2_12 = version_parts.len() > 1 && (version_parts[0] < 2 ||
98-
version_parts[0] == 2 && version_parts[1] < 12);
103+
let version_parts: Vec<i32> = probed_lib
104+
.version
105+
.split('.')
106+
.map(|part| part.parse::<i32>().unwrap_or(-1))
107+
.collect();
108+
let older_than_2_12 = version_parts.len() > 1
109+
&& (version_parts[0] < 2 || version_parts[0] == 2 && version_parts[1] < 12);
99110
println!("cargo::rustc-check-cfg=cfg(libxml_older_than_2_12)");
100111
if older_than_2_12 {
101112
println!("cargo::rustc-cfg=libxml_older_than_2_12");
@@ -113,12 +124,19 @@ fn main() {
113124
mod vcpkg_dep {
114125
use crate::ProbedLib;
115126
pub fn vcpkg_find_libxml2() -> Option<ProbedLib> {
116-
if let Ok(metadata) = vcpkg::Config::new()
117-
.find_package("libxml2") {
118-
Some(ProbedLib { version: vcpkg_version(), include_paths: metadata.include_paths })
119-
} else {
120-
None
127+
if let Ok(mut metadata) = vcpkg::Config::new().find_package("libxml2") {
128+
if let Some(mut include_path) = metadata.include_paths.pop() {
129+
if include_path.join("libxml2").exists() {
130+
// libxml2 >= 2.14.5 is in a 'libxml2' subdirectory
131+
include_path = include_path.join("libxml2");
132+
}
133+
return Some(ProbedLib {
134+
version: vcpkg_version(),
135+
include_paths: vec![include_path],
136+
});
137+
}
121138
}
139+
None
122140
}
123141

124142
fn vcpkg_version() -> String {
@@ -127,7 +145,7 @@ mod vcpkg_dep {
127145
let mut vcpkg_exe = vcpkg::find_vcpkg_root(&vcpkg::Config::new()).unwrap();
128146
vcpkg_exe.push("vcpkg.exe");
129147
let vcpkg_list_libxml2 = std::process::Command::new(vcpkg_exe)
130-
.args(["list","libxml2"])
148+
.args(["list", "libxml2"])
131149
.output()
132150
.expect("vcpkg.exe failed to execute in vcpkg_dep build step");
133151
if vcpkg_list_libxml2.status.success() {
@@ -137,9 +155,8 @@ mod vcpkg_dep {
137155
let mut version_piece = line.split("2.");
138156
version_piece.next();
139157
if let Some(version_tail) = version_piece.next() {
140-
if let Some(version) = version_tail.split(' ').next()
141-
.unwrap().split('#').next() {
142-
return format!("2.{version}");
158+
if let Some(version) = version_tail.split(' ').next().unwrap().split('#').next() {
159+
return format!("2.{version}");
143160
}
144161
}
145162
}

0 commit comments

Comments
 (0)