| 1 | extern crate bindgen; |
| 2 | |
| 3 | use std::env; |
| 4 | use std::path::PathBuf; |
| 5 | #[cfg(windows)] |
| 6 | use vcpkg; |
| 7 | |
| 8 | // const MINIMUM_LEPT_VERSION: &str = "1.80.0"; |
| 9 | |
| 10 | #[cfg(windows)] |
| 11 | fn find_leptonica_system_lib() -> Option<String> { |
| 12 | println!("cargo:rerun-if-env-changed=LEPTONICA_INCLUDE_PATH"); |
| 13 | println!("cargo:rerun-if-env-changed=LEPTONICA_LINK_PATHS"); |
| 14 | println!("cargo:rerun-if-env-changed=LEPTONICA_LINK_LIBS"); |
| 15 | |
| 16 | let vcpkg = || { |
| 17 | let lib = vcpkg::Config::new().find_package("leptonica").unwrap(); |
| 18 | |
| 19 | let include = lib |
| 20 | .include_paths |
| 21 | .iter() |
| 22 | .map(|x| x.to_string_lossy()) |
| 23 | .collect::<String>(); |
| 24 | Some(include) |
| 25 | }; |
| 26 | |
| 27 | let include_path = env::var("LEPTONICA_INCLUDE_PATH").ok(); |
| 28 | let link_paths = env::var("LEPTONICA_LINK_PATHS").ok(); |
| 29 | let link_paths = link_paths.as_deref().map(|x| x.split(',')); |
| 30 | let link_libs = env::var("LEPTONICA_LINK_LIBS").ok(); |
| 31 | let link_libs = link_libs.as_deref().map(|x| x.split(',')); |
| 32 | if let (Some(include_path), Some(link_paths), Some(link_libs)) = |
| 33 | (include_path, link_paths, link_libs) |
| 34 | { |
| 35 | for link_path in link_paths { |
| 36 | println!("cargo:rustc-link-search={}", link_path) |
| 37 | } |
| 38 | |
| 39 | for link_lib in link_libs { |
| 40 | println!("cargo:rustc-link-lib={}", link_lib) |
| 41 | } |
| 42 | |
| 43 | Some(include_path) |
| 44 | } else { |
| 45 | vcpkg() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // we sometimes need additional search paths, which we get using pkg-config |
| 50 | // we can use leptonica installed anywhere on Linux. |
| 51 | // if you change install path(--prefix) to `configure` script. |
| 52 | // set `export PKG_CONFIG_PATH=/path-to-lib/pkgconfig` before. |
| 53 | #[cfg(any(target_os = "macos", target_os = "linux", target_os = "freebsd"))] |
| 54 | fn find_leptonica_system_lib() -> Option<String> { |
| 55 | let pk = pkg_config::Config::new().probe("lept").unwrap(); |
| 56 | // Tell cargo to tell rustc to link the system proj shared library. |
| 57 | println!("cargo:rustc-link-search=native={:?}", pk.link_paths[0]); |
| 58 | println!("cargo:rustc-link-lib={}", pk.libs[0]); |
| 59 | |
| 60 | let mut include_path = pk.include_paths[0].clone(); |
| 61 | if include_path.ends_with("leptonica") { |
| 62 | include_path.pop(); |
| 63 | } |
| 64 | Some(include_path.to_str().unwrap().into()) |
| 65 | } |
| 66 | |
| 67 | #[cfg(all( |
| 68 | not(windows), |
| 69 | not(target_os = "macos"), |
| 70 | not(target_os = "linux"), |
| 71 | not(target_os = "freebsd") |
| 72 | ))] |
| 73 | fn find_leptonica_system_lib() -> Option<String> { |
| 74 | println!("cargo:rustc-link-lib=lept"); |
| 75 | None |
| 76 | } |
| 77 | |
| 78 | fn main() { |
| 79 | let clang_extra_include = find_leptonica_system_lib(); |
| 80 | |
| 81 | let mut bindings = bindgen::Builder::default().header("wrapper.h"); |
| 82 | |
| 83 | if let Some(include_path) = clang_extra_include { |
| 84 | bindings = bindings.clang_arg(format!("-I{}", include_path)); |
| 85 | } |
| 86 | |
| 87 | bindings = bindings.blocklist_type("max_align_t"); |
| 88 | |
| 89 | let bindings = bindings |
| 90 | .parse_callbacks(Box::new(bindgen::CargoCallbacks)) |
| 91 | .generate() |
| 92 | .expect("Unable to generate bindings"); |
| 93 | |
| 94 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 95 | bindings |
| 96 | .write_to_file(out_path.join("bindings.rs")) |
| 97 | .expect("Couldn't write bindings!"); |
| 98 | } |