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
154
155
156
157
158
159
160
161
162
163
// Copyright 2020-2023 the Tectonic Project
// Licensed under the MIT License.
//! Harfbuzz build script.
#[cfg(feature = "external-harfbuzz")]
mod inner {
use std::env;
use tectonic_cfg_support::target_cfg;
use tectonic_dep_support::{Configuration, Dependency, Spec};
struct HarfbuzzSpec;
impl Spec for HarfbuzzSpec {
// We require Harfbuzz >= 1.4, but that version is ancient, and
// specifying the version constraint in this string causes problems with
// the `pkgconf` implementation of pkg-config on Windows/MSYS2.
// (Specifically, its `--modversion` mode won't print anything out when
// given two arguments, causing an unhandled crash inside the pkg_config
// Rust library.) Likewise, for Harfbuzz < 2.5, the `harfbuzz-icu`
// pkg-config item is needed, but this may also cause problems for
// pkgconf. If you really need to compile against very old Harfbuzz,
// patch this file and don't use pkgconf.
fn get_pkgconfig_spec(&self) -> &str {
"harfbuzz"
}
// TODO: can we ensure that the graphite2 option is enabled?
fn get_vcpkg_spec(&self) -> &[&str] {
&["harfbuzz"]
}
}
pub fn build_harfbuzz() {
let cfg = Configuration::default();
let dep = Dependency::probe(HarfbuzzSpec, &cfg);
// This is the key. What we print here will be propagated into depending
// crates' build scripts as the envirnoment variable DEP_HARFBUZZ_INCLUDE_PATH,
// allowing them to find the headers internally.
let mut sep = "cargo:include-path=";
let is_macos_external =
target_cfg!(target_os = "macos") && env::var("CARGO_FEATURE_EXTERNAL_HARFBUZZ").is_ok();
dep.foreach_include_path(|p| {
// HACK: On macOS, the default brew harfbuzz for some reason points into the harfbuzz
// folder itself, so we need to go up one level to the includes folder.
if is_macos_external && p.ends_with("harfbuzz") {
print!("{}{}", sep, p.parent().unwrap().to_str().unwrap());
sep = ";";
}
print!("{}{}", sep, p.to_str().unwrap());
sep = ";";
print!("{}{}", sep, p.parent().unwrap().to_str().unwrap());
});
println!();
dep.emit();
}
}
#[cfg(not(feature = "external-harfbuzz"))]
mod inner {
use std::{env, ffi::OsStr, fs, path::PathBuf};
pub fn build_harfbuzz() {
let target = env::var("TARGET").unwrap();
// Check that the submodule has been checked out.
let src_dir = {
let mut p = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
p.push("harfbuzz");
p.push("src");
p
};
let test_file = src_dir.join("harfbuzz.cc");
if !test_file.exists() {
eprintln!("error: no such file {}", test_file.display());
eprintln!(
" if you have checked out from Git, you probably need to fetch submodules:"
);
eprintln!(" git submodule update --init");
eprintln!(
" This is needed because we are attempting to compile a local copy of Harfbuzz."
);
std::process::exit(1);
}
// Include paths exported by our internal dependencies:
let graphite2_include_path = env::var("DEP_GRAPHITE2_INCLUDE_PATH").unwrap();
let graphite2_static = !env::var("DEP_GRAPHITE2_DEFINE_STATIC").unwrap().is_empty();
let mut cfg = cc::Build::new();
cfg.cpp(true)
.flag("-std=c++11")
.warnings(false)
.define("HAVE_GRAPHITE2", "1")
.file("harfbuzz/src/harfbuzz.cc");
for item in graphite2_include_path.split(';') {
cfg.include(item);
}
if graphite2_static {
cfg.define("GRAPHITE2_STATIC", "1");
}
if !target.contains("windows") {
cfg.define("HAVE_PTHREAD", "1");
}
if target.contains("apple") {
cfg.define("HAVE_CORETEXT", "1");
// AssertMacros.h defines a verify() that conflicts with hb-shape.cc.
cfg.define("__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES", "0");
}
if target.contains("windows-gnu") {
cfg.flag("-Wa,-mbig-obj");
}
cfg.compile("harfbuzz");
// Copy the headers to have the same directory structure as a system install.
let include_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
print!(
"cargo:include-path={}",
include_dir.to_str().expect("non-string-friendly OUT_DIR")
);
for item in graphite2_include_path.split(';') {
print!(";{item}");
}
println!();
let dest_dir = include_dir.join("harfbuzz");
// Create the dest_dir if it does not exist
fs::create_dir_all(&dest_dir).expect("error creating dest_dir");
for entry in fs::read_dir(&src_dir).expect("failed to read dir") {
let entry = entry.expect("failed to get dir entry");
if entry.path().extension() == Some(OsStr::new("h")) {
let hdest = dest_dir.join(entry.path().file_name().unwrap());
fs::copy(entry.path(), hdest).expect("failed to copy header");
}
}
}
}
fn main() {
inner::build_harfbuzz();
}