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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![deny(missing_docs)]
use std::{
env,
path::{Path, PathBuf},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Backend {
PkgConfig,
Vcpkg,
}
impl Default for Backend {
fn default() -> Self {
Backend::PkgConfig
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Configuration {
pub backend: Backend,
semi_static_mode: bool,
}
impl Default for Configuration {
fn default() -> Self {
println!("cargo:rerun-if-env-changed=TECTONIC_DEP_BACKEND");
println!("cargo:rerun-if-env-changed=TECTONIC_PKGCONFIG_FORCE_SEMI_STATIC");
let backend = if let Ok(dep_backend_str) = env::var("TECTONIC_DEP_BACKEND") {
match dep_backend_str.as_ref() {
"pkg-config" => Backend::PkgConfig,
"vcpkg" => Backend::Vcpkg,
"default" => Backend::default(),
other => panic!("unrecognized TECTONIC_DEP_BACKEND setting {:?}", other),
}
} else {
Backend::default()
};
let semi_static_mode = env::var("TECTONIC_PKGCONFIG_FORCE_SEMI_STATIC").is_ok();
Configuration {
backend,
semi_static_mode,
}
}
}
pub trait Spec {
fn get_pkgconfig_spec(&self) -> &str;
fn get_vcpkg_spec(&self) -> &[&str];
}
#[derive(Debug)]
struct PkgConfigState {
libs: pkg_config::Library,
}
#[derive(Clone, Debug)]
struct VcPkgState {
include_paths: Vec<PathBuf>,
}
#[derive(Debug)]
enum DepState {
PkgConfig(PkgConfigState),
VcPkg(VcPkgState),
}
impl DepState {
fn new<T: Spec>(spec: &T, config: &Configuration) -> Self {
match config.backend {
Backend::PkgConfig => DepState::new_from_pkg_config(spec, config),
Backend::Vcpkg => DepState::new_from_vcpkg(spec, config),
}
}
fn new_from_pkg_config<T: Spec>(spec: &T, config: &Configuration) -> Self {
let libs = pkg_config::Config::new()
.cargo_metadata(false)
.statik(config.semi_static_mode)
.probe(spec.get_pkgconfig_spec())
.unwrap();
DepState::PkgConfig(PkgConfigState { libs })
}
fn new_from_vcpkg<T: Spec>(spec: &T, _config: &Configuration) -> Self {
let mut include_paths = vec![];
for dep in spec.get_vcpkg_spec() {
let library = vcpkg::Config::new()
.cargo_metadata(false)
.find_package(dep)
.unwrap_or_else(|e| panic!("failed to load package {} from vcpkg: {}", dep, e));
include_paths.extend(library.include_paths.iter().cloned());
}
DepState::VcPkg(VcPkgState { include_paths })
}
}
pub struct Dependency<'a, T: Spec> {
config: &'a Configuration,
spec: T,
state: DepState,
}
impl<'a, T: Spec> Dependency<'a, T> {
pub fn probe(spec: T, config: &'a Configuration) -> Self {
let state = DepState::new(&spec, config);
Dependency {
config,
spec,
state,
}
}
pub fn foreach_include_path<F>(&self, mut f: F)
where
F: FnMut(&Path),
{
match self.state {
DepState::PkgConfig(ref s) => {
for p in &s.libs.include_paths {
f(p);
}
}
DepState::VcPkg(ref s) => {
for p in &s.include_paths {
f(p);
}
}
}
}
pub fn emit(&self) {
match self.state {
DepState::PkgConfig(ref state) => {
if self.config.semi_static_mode {
for link_path in &state.libs.link_paths {
println!("cargo:rustc-link-search=native={}", link_path.display());
}
for fw_path in &state.libs.framework_paths {
println!("cargo:rustc-link-search=framework={}", fw_path.display());
}
for libbase in &state.libs.libs {
let do_static = match libbase.as_ref() {
"c" | "m" | "dl" | "pthread" => false,
_ => {
let libname = format!("lib{}.a", libbase);
state
.libs
.link_paths
.iter()
.any(|d| d.join(&libname).exists())
}
};
let mode = if do_static { "static=" } else { "" };
println!("cargo:rustc-link-lib={}{}", mode, libbase);
}
for fw in &state.libs.frameworks {
println!("cargo:rustc-link-lib=framework={}", fw);
}
} else {
pkg_config::Config::new()
.cargo_metadata(true)
.probe(self.spec.get_pkgconfig_spec())
.unwrap();
}
}
DepState::VcPkg(_) => {
for dep in self.spec.get_vcpkg_spec() {
vcpkg::find_package(dep).unwrap_or_else(|e| {
panic!("failed to load package {} from vcpkg: {}", dep, e)
});
}
}
}
}
}