-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.rs
98 lines (86 loc) · 3.32 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 cc;
extern crate glob;
use std::env;
use std::path::PathBuf;
fn main() {
let manifest_dir =
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let vendor = manifest_dir.join("vendor");
if !vendor.join("src").exists() {
panic!(
"{} dir is missing files. Try running: `git submodule update --init --recursive`",
vendor.display()
);
}
let mut cc = cc::Build::new();
setup_build(&mut cc, &vendor);
for f in glob::glob("vendor/src/**/*.c")
.expect("glob vender/src failed")
.flatten()
{
cc.file(manifest_dir.join(f));
}
for f in glob::glob("vendor/sharpyuv/**/*.c").expect("glob vendor/src failed") {
let f = f.expect("glob iteration vendor/src failed");
cc.file(manifest_dir.join(f));
}
cc.compile("webpsys");
}
fn setup_build(build: &mut cc::Build, include_dir: &PathBuf) {
build.include(include_dir);
build.define("NDEBUG", Some("1"));
build.define("_THREAD_SAFE", Some("1"));
if !build.get_compiler().is_like_msvc() {
build.flag("-fvisibility=hidden").flag("-Wall");
} else {
build.flag("-D_CRT_SECURE_NO_WARNINGS");
}
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH");
let target_features = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
let has_feature = |f: &str| target_features.split(',').any(|feature| feature == f);
let target_cpu = env::var("TARGET_CPU").ok();
let target_cpu = target_cpu.as_deref().unwrap_or(&*target_arch);
build.flag_if_supported(&format!("-march={}", target_cpu));
match target_arch.as_str() {
"x86_64" | "i686" => {
build.define("WEBP_HAVE_SSE2", Some("1"));
if cfg!(feature = "sse41") || has_feature("sse4.1") {
build.define("WEBP_HAVE_SSE41", Some("1"));
build.flag_if_supported("-msse4.1");
}
if cfg!(feature = "avx2") || has_feature("avx2") {
build.define("WEBP_HAVE_AVX2", Some("1"));
build.flag_if_supported("-mavx2");
}
}
"aarch64" => {
if cfg!(feature = "neon") || has_feature("neon") {
build.define("WEBP_HAVE_NEON", Some("1"));
}
// If any optimizations are ennabled, we must remove -gdwarf flags
// Which we can only do by stopping debug mode entirely since we can't
// fix env flags.
let gccflags = build
.get_compiler()
.cflags_env()
.to_string_lossy()
.to_string();
if gccflags.contains("-O0")
|| gccflags.contains("-O1")
|| gccflags.contains("-O2")
|| gccflags.contains("-O3")
|| gccflags.contains("-Ofast")
|| gccflags.contains("-Os")
|| gccflags.contains("--opt-level=0")
|| gccflags.contains("--opt-level=1")
|| gccflags.contains("--opt-level=2")
|| gccflags.contains("--opt-level=3")
|| gccflags.contains("--opt-level=s")
|| gccflags.contains("--opt-level=z")
{
build.debug(false);
}
}
_ => {}
};
}