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