|
1 | | -fn main() { |
| 1 | +use std::{env, fs, path::{Path, PathBuf}}; |
| 2 | + |
| 3 | +/// Finds libxml2 and optionally return a list of header |
| 4 | +/// files from which the bindings can be generated. |
| 5 | +fn find_libxml2() -> Option<Vec<PathBuf>> { |
| 6 | + #![allow(unreachable_code)] // for platform-dependent dead code |
| 7 | + |
2 | 8 | if let Ok(ref s) = std::env::var("LIBXML2") { |
3 | 9 | // println!("{:?}", std::env::vars()); |
4 | 10 | // panic!("set libxml2."); |
@@ -27,32 +33,54 @@ fn main() { |
27 | 33 | .expect("no library path in LIBXML2 env") |
28 | 34 | .to_string_lossy() |
29 | 35 | ); |
30 | | - } else { |
| 36 | + None |
| 37 | + } else { |
31 | 38 | #[cfg(any(target_family = "unix", target_os = "macos"))] |
32 | 39 | { |
33 | | - if pkg_config_dep::find() { |
34 | | - return; |
35 | | - } |
| 40 | + let lib = pkg_config::Config::new() |
| 41 | + .probe("libxml-2.0") |
| 42 | + .expect("Couldn't find libxml2 via pkg-config"); |
| 43 | + return Some(lib.include_paths) |
36 | 44 | } |
37 | 45 |
|
38 | 46 | #[cfg(windows)] |
39 | 47 | { |
40 | 48 | if vcpkg_dep::find() { |
41 | | - return; |
| 49 | + return None |
42 | 50 | } |
43 | 51 | } |
44 | | - |
| 52 | + |
45 | 53 | panic!("Could not find libxml2.") |
46 | 54 | } |
47 | 55 | } |
48 | 56 |
|
49 | | -#[cfg(any(target_family = "unix", target_os = "macos"))] |
50 | | -mod pkg_config_dep { |
51 | | - pub fn find() -> bool { |
52 | | - if pkg_config::find_library("libxml-2.0").is_ok() { |
53 | | - return true; |
54 | | - } |
55 | | - false |
| 57 | +fn generate_bindings(header_dirs: Vec<PathBuf>, output_path: &Path) { |
| 58 | + let bindings = bindgen::Builder::default() |
| 59 | + .header("src/wrapper.h") |
| 60 | + // invalidate build as soon as the wrapper changes |
| 61 | + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
| 62 | + .layout_tests(true) |
| 63 | + .clang_args(&["-DPKG-CONFIG"]) |
| 64 | + .clang_args( |
| 65 | + header_dirs.iter() |
| 66 | + .map(|dir| format!("-I{}", dir.display())) |
| 67 | + ); |
| 68 | + bindings |
| 69 | + .generate() |
| 70 | + .expect("failed to generate bindings with bindgen") |
| 71 | + .write_to_file(output_path) |
| 72 | + .expect("Failed to write bindings.rs"); |
| 73 | +} |
| 74 | + |
| 75 | +fn main() { |
| 76 | + let bindings_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("bindings.rs"); |
| 77 | + if let Some(header_dirs) = find_libxml2() { |
| 78 | + // if we could find header files, generate fresh bindings from them |
| 79 | + generate_bindings(header_dirs, &bindings_path); |
| 80 | + } else { |
| 81 | + // otherwise, use the default bindings on platforms where pkg-config isn't available |
| 82 | + fs::copy(PathBuf::from("src/default_bindings.rs"), bindings_path) |
| 83 | + .expect("Failed to copy the default bindings to the build directory"); |
56 | 84 | } |
57 | 85 | } |
58 | 86 |
|
|
0 commit comments