-
Notifications
You must be signed in to change notification settings - Fork 59
/
build.rs
153 lines (129 loc) · 4.38 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#[cfg(feature = "bindgen")]
extern crate bindgen_;
#[cfg(feature = "build-native-harfbuzz")]
extern crate cc;
extern crate pkg_config;
use std::env;
use std::path::{Path, PathBuf};
#[cfg(feature = "bindgen")]
mod bindings {
use std::env;
use std::fs;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
static WRAPPER: &'static str = "#include \"hb.h\"
#include \"hb-ot.h\"
#include \"hb-aat.h\"
";
struct BindingsWriter {
file: fs::File,
ignore: regex::Regex,
replacer: regex::Regex,
}
impl BindingsWriter {
fn new(path: &Path) -> BindingsWriter {
BindingsWriter {
file: fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path)
.unwrap(),
ignore: regex::Regex::new("__u?int(8|16|32)_t ").unwrap(),
replacer: regex::Regex::new("(i|u)(8|16|32)_").unwrap(),
}
}
}
impl Write for BindingsWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let c = unsafe { std::str::from_utf8_unchecked(buf) };
for line in c.lines() {
if !self.ignore.is_match(&line) {
let rep = self.replacer.replacen(&line, 0, "${1}${2}");
self.file.write(rep.as_bytes())?;
self.file.write("\n".as_bytes())?;
}
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.file.flush()
}
}
pub(super) fn gen(include_dirs: &[PathBuf]) {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut builder = bindgen_::builder()
.no_convert_floats()
.prepend_enum_name(false)
.allowlist_type("hb_.*")
.allowlist_function("hb_.*")
.header_contents("wrapper.h", WRAPPER);
for include_dir in include_dirs {
builder = builder
.clang_arg("-I")
.clang_arg(include_dir.display().to_string());
}
let writer = BindingsWriter::new(&out_dir.join("bindings.rs"));
println!("cargo:bindings={}", out_dir.join("bindings.rs").display());
builder.generate().unwrap().write(Box::new(writer)).unwrap();
}
}
#[cfg(feature = "build-native-harfbuzz")]
fn main() {
let target = env::var("TARGET").unwrap();
let mut cfg = cc::Build::new();
cfg.cpp(true)
.flag_if_supported("-std=c++11") // for unix
.warnings(false)
.file("harfbuzz/src/harfbuzz.cc");
if !target.contains("windows") {
cfg.define("HAVE_PTHREAD", "1");
}
if target.contains("apple") {
cfg.define("HAVE_CORETEXT", "1");
}
if target.contains("windows") {
cfg.define("HAVE_DIRECTWRITE", "1");
}
if target.contains("windows-gnu") {
cfg.flag("-Wa,-mbig-obj");
}
cfg.compile("embedded_harfbuzz");
let out_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let include_dir = out_dir.join("harfbuzz").join("src");
println!("cargo:include={}", include_dir.display());
}
#[cfg(not(feature = "build-native-harfbuzz"))]
fn main() {
#[allow(unused_mut)]
let mut pkgcfg = pkg_config::Config::new();
// allow other version of harfbuzz when bindgen enabled.
#[cfg(not(feature = "bindgen"))]
pkgcfg.range_version("4.2".."5");
match pkgcfg.probe("harfbuzz") {
Ok(_lib) => {
#[cfg(feature = "bindgen")]
bindings::gen(&_lib.include_paths);
return;
}
Err(_) => {}
}
if let Ok(libdir) = env::var("HARFBUZZ_LIB_DIR") {
println!("cargo:rustc-link-search=native={}", &libdir);
if is_static_available("harfbuzz", &PathBuf::from(&libdir)) {
println!("cargo:rustc-link-lib=static={}", "harfbuzz");
} else {
println!("cargo:rustc-link-lib={}", "harfbuzz");
}
}
if let Ok(include_dir) = env::var("HARFBUZZ_INCLUDE_DIR") {
#[cfg(feature = "bindgen")]
bindings::gen(&[PathBuf::from(&include_dir)]);
}
}
#[allow(dead_code)]
/// System libraries should only be linked dynamically
fn is_static_available(name: &str, dir: &Path) -> bool {
let libname = format!("lib{}.a", name);
dir.join(&libname).exists()
}