| 1 |
use std::{ |
| 2 |
env, fs, |
| 3 |
path::{Path, PathBuf}, |
| 4 |
}; |
| 5 |
|
| 6 |
struct ProbedLib { |
| 7 |
version: String, |
| 8 |
include_paths: Vec<PathBuf>, |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
fn find_libxml2() -> Option<ProbedLib> { |
| 14 |
#![allow(unreachable_code)] |
| 15 |
|
| 16 |
if let Ok(ref s) = std::env::var("LIBXML2") { |
| 17 |
|
| 18 |
|
| 19 |
let p = std::path::Path::new(s); |
| 20 |
let fname = std::path::Path::new( |
| 21 |
p.file_name() |
| 22 |
.unwrap_or_else(|| panic!("no file name in LIBXML2 env ({s})")), |
| 23 |
); |
| 24 |
assert!( |
| 25 |
p.is_file(), |
| 26 |
"{}", |
| 27 |
&format!("not a file in LIBXML2 env ({s})") |
| 28 |
); |
| 29 |
println!( |
| 30 |
"cargo:rustc-link-lib={}", |
| 31 |
fname |
| 32 |
.file_stem() |
| 33 |
.unwrap() |
| 34 |
.to_string_lossy() |
| 35 |
.strip_prefix("lib") |
| 36 |
.unwrap() |
| 37 |
); |
| 38 |
println!( |
| 39 |
"cargo:rustc-link-search={}", |
| 40 |
p.parent() |
| 41 |
.expect("no library path in LIBXML2 env") |
| 42 |
.to_string_lossy() |
| 43 |
); |
| 44 |
None |
| 45 |
} else { |
| 46 |
#[cfg(any( |
| 47 |
target_family = "unix", |
| 48 |
target_os = "macos", |
| 49 |
all(target_family = "windows", target_env = "gnu") |
| 50 |
))] |
| 51 |
{ |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
| 64 |
.probe("libxml-2.0") |
| 65 |
.expect("Couldn't find libxml2 via pkg-config"); |
| 66 |
return Some(ProbedLib { |
| 67 |
include_paths: lib.include_paths, |
| 68 |
version: lib.version, |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
#[cfg(all(target_family = "windows", target_env = "msvc"))] |
| 73 |
{ |
| 74 |
if let Some(meta) = vcpkg_dep::vcpkg_find_libxml2() { |
| 75 |
return Some(meta); |
| 76 |
} else { |
| 77 |
eprintln!("vcpkg did not succeed in finding libxml2."); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
panic!("Could not find libxml2.") |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
fn generate_bindings(header_dirs: Vec<PathBuf>, output_path: &Path) { |
| 86 |
let bindings = bindgen::Builder::default() |
| 87 |
.header("src/wrapper.h") |
| 88 |
.opaque_type("max_align_t") |
| 89 |
|
| 90 |
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
| 91 |
.layout_tests(true) |
| 92 |
.clang_args(&[ |
| 93 |
"-DPKG-CONFIG", |
| 94 |
"-DLIBXML_C14N_ENABLED", |
| 95 |
"-DLIBXML_OUTPUT_ENABLED", |
| 96 |
]) |
| 97 |
.clang_args(header_dirs.iter().map(|dir| format!("-I{}", dir.display()))); |
| 98 |
bindings |
| 99 |
.generate() |
| 100 |
.expect("failed to generate bindings with bindgen") |
| 101 |
.write_to_file(output_path) |
| 102 |
.expect("Failed to write bindings.rs"); |
| 103 |
} |
| 104 |
|
| 105 |
fn main() { |
| 106 |
|
| 107 |
println!("cargo:rerun-if-env-changed=LIBXML2_STATIC"); |
| 108 |
let bindings_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("bindings.rs"); |
| 109 |
|
| 110 |
println!("cargo::rustc-check-cfg=cfg(libxml_older_than_2_12)"); |
| 111 |
|
| 112 |
if let Some(probed_lib) = find_libxml2() { |
| 113 |
|
| 114 |
generate_bindings(probed_lib.include_paths, &bindings_path); |
| 115 |
|
| 116 |
let version_parts: Vec<i32> = probed_lib |
| 117 |
.version |
| 118 |
.split('.') |
| 119 |
.map(|part| part.parse::<i32>().unwrap_or(-1)) |
| 120 |
.collect(); |
| 121 |
let older_than_2_12 = version_parts.len() > 1 |
| 122 |
&& (version_parts[0] < 2 || version_parts[0] == 2 && version_parts[1] < 12); |
| 123 |
println!("cargo::rustc-check-cfg=cfg(libxml_older_than_2_12)"); |
| 124 |
if older_than_2_12 { |
| 125 |
println!("cargo::rustc-cfg=libxml_older_than_2_12"); |
| 126 |
} |
| 127 |
} else { |
| 128 |
|
| 129 |
fs::copy(PathBuf::from("src/default_bindings.rs"), bindings_path) |
| 130 |
.expect("Failed to copy the default bindings to the build directory"); |
| 131 |
|
| 132 |
println!("cargo::rustc-cfg=libxml_older_than_2_12"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[cfg(all(target_family = "windows", target_env = "msvc"))] |
| 137 |
mod vcpkg_dep { |
| 138 |
use crate::ProbedLib; |
| 139 |
pub fn vcpkg_find_libxml2() -> Option<ProbedLib> { |
| 140 |
if let Ok(metadata) = vcpkg::Config::new().find_package("libxml2") { |
| 141 |
let include_paths = metadata |
| 142 |
.include_paths |
| 143 |
.into_iter() |
| 144 |
.fold(Vec::new(), |mut acc, p| { |
| 145 |
acc.push(p.join("libxml2")); |
| 146 |
acc.push(p); |
| 147 |
acc |
| 148 |
}); |
| 149 |
return Some(ProbedLib { |
| 150 |
version: vcpkg_version(), |
| 151 |
include_paths, |
| 152 |
}); |
| 153 |
} |
| 154 |
None |
| 155 |
} |
| 156 |
|
| 157 |
fn vcpkg_version() -> String { |
| 158 |
|
| 159 |
|
| 160 |
let mut vcpkg_exe = vcpkg::find_vcpkg_root(&vcpkg::Config::new()).unwrap(); |
| 161 |
vcpkg_exe.push("vcpkg.exe"); |
| 162 |
let vcpkg_list_libxml2 = std::process::Command::new(vcpkg_exe) |
| 163 |
.args(["list", "libxml2"]) |
| 164 |
.output() |
| 165 |
.expect("vcpkg.exe failed to execute in vcpkg_dep build step"); |
| 166 |
if vcpkg_list_libxml2.status.success() { |
| 167 |
let libxml2_list_str = String::from_utf8_lossy(&vcpkg_list_libxml2.stdout); |
| 168 |
for line in libxml2_list_str.lines() { |
| 169 |
if line.starts_with("libxml2:") { |
| 170 |
let mut version_piece = line.split("2."); |
| 171 |
version_piece.next(); |
| 172 |
if let Some(version_tail) = version_piece.next() { |
| 173 |
if let Some(version) = version_tail.split(' ').next().unwrap().split('#').next() { |
| 174 |
return format!("2.{version}"); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
String::from("2.13.5") |
| 183 |
} |
| 184 |
} |