-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbuild.rs
More file actions
351 lines (300 loc) · 12.1 KB
/
Copy pathbuild.rs
File metadata and controls
351 lines (300 loc) · 12.1 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#![allow(unused_imports, dead_code)]
use anyhow::{Result, Context as _, anyhow, bail};
use std::{env, fs};
use std::collections::HashMap;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::Command;
fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs");
let link_kind = get_link_kind()?;
let library = build_or_find_library(link_kind)?;
generate_or_copy_bindings(&library)?;
Ok(())
}
#[derive(Debug)]
enum LinkKind {
Static,
Dynamic,
Default,
}
fn get_link_kind() -> Result<LinkKind> {
let static_env = env_bool("TURBOJPEG_STATIC")?;
let dynamic_env = env_bool("TURBOJPEG_DYNAMIC")?.or(env_bool("TURBOJPEG_SHARED")?);
match (static_env, dynamic_env) {
(Some(true), Some(true)) =>
bail!("Both TURBOJPEG_STATIC and TURBOJPEG_DYNAMIC/TURBOJPEG_SHARED are set to 1"),
(Some(false), Some(false)) =>
bail!("Both TURBOJPEG_STATIC and TURBOJPEG_DYNAMIC/TURBOJPEG_SHARED are set to 0"),
(None, None) =>
Ok(LinkKind::Default),
(Some(true) | None, Some(false) | None) =>
Ok(LinkKind::Static),
(Some(false) | None, Some(true) | None) =>
Ok(LinkKind::Dynamic),
}
}
#[derive(Debug)]
struct Library {
include_paths: Vec<PathBuf>,
defines: HashMap<String, Option<String>>,
}
fn build_or_find_library(link_kind: LinkKind) -> Result<Library> {
match env("TURBOJPEG_SOURCE") {
Some(source) => {
if source.eq_ignore_ascii_case("vendor") {
build_vendor(link_kind)
} else if source.eq_ignore_ascii_case("pkg-config") ||
source.eq_ignore_ascii_case("pkgconfig") ||
source.eq_ignore_ascii_case("pkgconf")
{
find_pkg_config(link_kind)
} else if source.eq_ignore_ascii_case("explicit") {
find_explicit(link_kind)
} else {
bail!("Unknown value of TURBOJPEG_SOURCE, supported values are:\n\
- 'vendor' to build the library from source bundled with the turbojpeg-sys crate,\n\
- 'pkg-config' to find the library using pkg-config,\n\
- 'explicit' to use TURBOJPEG_LIB_DIR and TURBOJPEG_INCLUDE_DIR")
}
},
None => {
if cfg!(feature = "cmake") {
build_vendor(link_kind)
} else if cfg!(feature = "pkg-config") {
find_pkg_config(link_kind)
} else {
find_explicit(link_kind)
}
},
}
}
#[cfg(feature = "pkg-config")]
fn find_pkg_config(link_kind: LinkKind) -> Result<Library> {
println!("Using pkg-config to find libturbojpeg");
let mut cfg = pkg_config::Config::new();
cfg.atleast_version("3.0");
match link_kind {
LinkKind::Static => { cfg.statik(true); },
LinkKind::Dynamic => { cfg.statik(false); },
LinkKind::Default => {},
}
let lib = cfg.probe("libturbojpeg")
.context("could not find turbojpeg using pkg-config")?;
Ok(Library {
include_paths: lib.include_paths,
defines: lib.defines,
})
}
#[cfg(not(feature = "pkg-config"))]
fn find_pkg_config(_: LinkKind) -> Result<Library> {
bail!("Trying to find turbojpeg using pkg-config, but the `pkg-config` feature is disabled. \
You have two options:\n\
- enable `pkg-config` feature of `turbojpeg-sys` crate\n\
- use TURBOJPEG_SOURCE to select other source for the library")
}
fn find_explicit(link_kind: LinkKind) -> Result<Library> {
println!("Using TURBOJPEG_LIB_DIR and TURBOJPEG_INCLUDE_DIR to find turbojpeg");
let lib_dir = env_path("TURBOJPEG_LIB_DIR")
.or_else(|| env_path("TURBOJPEG_LIB_PATH"))
.context("TURBOJPEG_SOURCE is set to 'explicit', but TURBOJPEG_LIB_DIR is not set")?;
let include_dir = env_path("TURBOJPEG_INCLUDE_DIR")
.or_else(|| env_path("TURBOJPEG_INCLUDE_PATH"));
let lib_dir = fs::canonicalize(lib_dir)
.context("Cannot canonicalize TURBOJPEG_LIB_DIR")?;
let include_dir = include_dir.map(fs::canonicalize).transpose()
.context("Cannot canonicalize TURBOJPEG_INCLUDE_DIR")?;
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-lib={}=turbojpeg", match link_kind {
LinkKind::Static | LinkKind::Default => "static",
LinkKind::Dynamic => "dylib",
});
Ok(Library {
include_paths: include_dir.into_iter().collect(),
defines: HashMap::new(),
})
}
#[cfg(feature = "cmake")]
fn build_vendor(link_kind: LinkKind) -> Result<Library> {
println!("Building turbojpeg from source");
if !cfg!(feature = "require-simd") {
check_nasm();
}
let source_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("libjpeg-turbo");
let mut cmake = cmake::Config::new(source_path);
cmake.configure_arg(format!("-DENABLE_SHARED={}", matches!(link_kind, LinkKind::Dynamic) as u32));
cmake.configure_arg(format!("-DENABLE_STATIC={}", !matches!(link_kind, LinkKind::Dynamic) as u32));
// Ignore DESTDIR because it will install turbojpeg to the wrong location
cmake.env("DESTDIR", "");
// On some 64 bit targets, the default libdir would be set to lib64.
// Let's remain consistent across build targets and set the libdir ourselves,
// instead of trying to figure out where to find the libs based on the target
cmake.define("CMAKE_INSTALL_DEFAULT_LIBDIR", "lib");
if cfg!(feature = "require-simd") {
cmake.configure_arg("-DREQUIRE_SIMD=ON");
}
let is_msvc = env("CARGO_CFG_TARGET_ENV").unwrap() == "msvc";
#[cfg(target_os = "windows")] {
// add missing msvc-specific flags
if is_msvc {
let opt_level = env("OPT_LEVEL")
.map(|s| s
.to_string_lossy()
.chars()
.nth(0)
.unwrap()
)
.unwrap();
let compiler_flags: &[&str] = match opt_level {
's' => &["/O1", "/Ob1"], // min size + no opt + min inlining
'z' => &["/O1", "/Ob0"], // min size + no opt + no inlining
'1' | '2' => &["/O1"], // min opt
'3' => &["/O2", "/Ob3"], // max opt + max inlining
'0' => &["/Od"], // no opt
_ => panic!("unknown optimization level: '{opt_level}'")
};
for flag in compiler_flags {
cmake.cflag(flag);
}
}
}
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os == "android" {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let android_abi = match target_arch.as_str() {
"arm" => "armeabi-v7a",
"aarch64" => "arm64-v8a",
"x86" => "x86",
"x86_64" => "x86_64",
_ => bail!("Unsupported Android arch: {target_arch:?}"),
};
cmake.configure_arg(format!("-DANDROID_ABI={android_abi}"));
}
let dst_path = cmake.build();
let lib_path = dst_path.join("lib");
let include_path = dst_path.join("include");
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib={}=turbojpeg{}", match link_kind {
LinkKind::Static | LinkKind::Default => "static",
LinkKind::Dynamic => "dylib",
}, if is_msvc && matches!(link_kind, LinkKind::Static | LinkKind::Default) {
"-static"
} else {
""
});
Ok(Library {
include_paths: vec![include_path],
defines: HashMap::new(),
})
}
fn check_nasm() {
if !Command::new("nasm").arg("-v").status().map(|s| s.success()).unwrap_or(false) {
println!("cargo:warning=NASM does not seem to be installed, so turbojpeg will be compiled without \
SIMD extensions. Performance will suffer.");
}
}
#[cfg(not(feature = "cmake"))]
fn build_vendor(_link_kind: LinkKind) -> Result<Library> {
bail!("Trying to build turbojpeg from source, but the `cmake` feature is disabled.\
You have two options:\n\
- enable `cmake` feature of `turbojpeg-sys` crate\n\
- use TURBOJPEG_SOURCE to select other source for the library")
}
fn generate_or_copy_bindings(library: &Library) -> Result<()> {
match env("TURBOJPEG_BINDING") {
Some(binding) => {
if binding.eq_ignore_ascii_case("pregenerated") {
copy_pregenerated_bindings()
} else if binding.eq_ignore_ascii_case("bindgen") {
generate_bindings(library)
} else {
bail!("Unknown value of TURBOJPEG_BINDING, supported values are:\n\
- `pregenerated` to use our pregenerated Rust bindings,\n\
- `bindgen` to generate the bindings with bindgen")
}
},
None => {
if cfg!(feature = "bindgen") {
generate_bindings(library)
} else {
copy_pregenerated_bindings()
}
},
}
}
fn copy_pregenerated_bindings() -> Result<()> {
println!("Using pregenerated bindings");
let out_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let crate_path = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
fs::copy(crate_path.join("bindings.rs"), out_path.join("bindings.rs"))?;
println!("cargo:rerun-if-changed={}", crate_path.join("bindings.rs").to_str().unwrap());
Ok(())
}
#[cfg(feature = "bindgen")]
fn generate_bindings(library: &Library) -> Result<()> {
println!("Generating bindings using bindgen");
let target = env::var("TARGET").unwrap();
let mut builder = bindgen::Builder::default()
.header("wrapper.h")
.use_core()
.ctypes_prefix("libc")
.clang_args(&["-target", &target]);
for path in library.include_paths.iter() {
let path = path.to_str().unwrap();
builder = builder.clang_arg(format!("-I{}", path));
println!("cargo:rerun-if-changed={}", path);
}
for (name, value) in library.defines.iter() {
if let Some(value) = value {
builder = builder.clang_arg(format!("-D{}={}", name, value));
} else {
builder = builder.clang_arg(format!("-D{}", name));
}
}
let bindings = builder.generate()
.map_err(|_| anyhow!("could not generate bindings"))?;
let out_file = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("bindings.rs");
bindings.write_to_file(&out_file)
.context("could not write bindings to OUT_DIR")?;
println!("Generated bindings are stored in {}", out_file.display());
Ok(())
}
#[cfg(not(feature = "bindgen"))]
fn generate_bindings(_: &Library) -> Result<()> {
bail!("Trying to build bindings with bindgen, but the `bindgen` feature is disabled. \
You have two options:\n\
- enable `bindgen` feature of `turbojpeg-sys` crate\n\
- use TURBOJPEG_BINDING to select other method to obtain the bindings")
}
fn env(name: &str) -> Option<OsString> {
// adapted from `openssl-sys` crate
fn env_inner(name: &str) -> Option<OsString> {
let value = env::var_os(name);
println!("cargo:rerun-if-env-changed={}", name);
match value {
Some(ref v) => println!("{} = {}", name, v.to_string_lossy()),
None => println!("{} unset", name),
}
value
}
let prefix = env::var("TARGET").unwrap().to_uppercase().replace('-', "_");
let prefixed = format!("{}_{}", prefix, name);
env_inner(&prefixed).or_else(|| env_inner(name))
}
fn env_bool(name: &str) -> Result<Option<bool>> {
match env(name) {
Some(value) => {
for v in ["", "1", "yes", "true", "on"].into_iter() {
if value.eq_ignore_ascii_case(v) { return Ok(Some(true)) }
}
for v in ["0", "no", "false", "off"].into_iter() {
if value.eq_ignore_ascii_case(v) { return Ok(Some(false)) }
}
bail!("Env variable {} has value {:?}, expected empty or boolean", name, value)
},
None => Ok(None),
}
}
fn env_path(name: &str) -> Option<PathBuf> {
env(name).map(|v| v.into())
}