-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.rs
280 lines (267 loc) · 10.5 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
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
// Based on https://github.com/rust-lang/libz-sys/blob/1.0.25/build.rs
use std::env;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-env-changed=LIBWEBP_SYS_STATIC");
println!("cargo:rerun-if-changed=build.rs");
let host = env::var("HOST").unwrap();
let target = env::var("TARGET").unwrap();
let host_and_target_contain = |s| host.contains(s) && target.contains(s);
// Don't run pkg-config if we're linking statically (we'll build below) and
// also don't run pkg-config on macOS/FreeBSD/DragonFly. That'll end up printing
// `-L /usr/lib` which wreaks havoc with linking to an OpenSSL in /usr/local/lib
// (Homebrew, Ports, etc.)
let want_static =
cfg!(feature = "static") || env::var("LIBWEBP_SYS_STATIC").unwrap_or(String::new()) == "1";
if !want_static &&
!target.contains("msvc") && // pkg-config just never works here
!(host_and_target_contain("apple") ||
host_and_target_contain("freebsd") ||
host_and_target_contain("dragonfly"))
{
let mut config = pkg_config::Config::new();
config.cargo_metadata(true);
if config.probe("libwebp").is_ok() {
if cfg!(feature = "demux") {
config.probe("libwebpdemux").unwrap();
}
if cfg!(feature = "mux") {
config.probe("libwebpmux").unwrap();
}
return;
}
}
if target.contains("msvc") {
if try_vcpkg() {
return;
}
}
// Whitelist a bunch of situations where we build unconditionally.
//
// MSVC basically never has it preinstalled, MinGW picks up a bunch of weird
// paths we don't like, `want_static` may force us, cross compiling almost
// never has a prebuilt version, and musl is almost always static.
if target.contains("msvc")
|| target.contains("pc-windows-gnu")
|| want_static
|| target != host
|| target.contains("musl")
{
return build_libwebp();
}
// If we've gotten this far we're probably a pretty standard platform.
// Almost all platforms here ship libz by default, but some don't have
// pkg-config files that we would find above.
//
// In any case test if zlib is actually installed and if so we link to it,
// otherwise continue below to build things.
if libwebp_installed() {
println!("cargo:rustc-link-lib=webp");
if cfg!(feature = "demux") {
println!("cargo:rustc-link-lib=webpdemux");
}
if cfg!(feature = "mux") {
println!("cargo:rustc-link-lib=webpmux");
}
return;
}
build_libwebp()
}
fn build_libwebp() {
// For testing purpose
if let Ok(value) = std::env::var("__LIBWEBP_SYS_FORBID_BUILD") {
if value == "1" {
panic!("__LIBWEBP_SYS_FORBID_BUILD is set to 1");
}
}
cc::Build::new()
// libwebpdecode_la_SOURCES (src/dec)
.file("c_src/src/dec/alpha_dec.c")
.file("c_src/src/dec/buffer_dec.c")
.file("c_src/src/dec/frame_dec.c")
.file("c_src/src/dec/idec_dec.c")
.file("c_src/src/dec/io_dec.c")
.file("c_src/src/dec/quant_dec.c")
.file("c_src/src/dec/tree_dec.c")
.file("c_src/src/dec/vp8_dec.c")
.file("c_src/src/dec/vp8l_dec.c")
.file("c_src/src/dec/webp_dec.c")
// libwebpencode_la_SOURCES (src/enc)
.file("c_src/src/enc/alpha_enc.c")
.file("c_src/src/enc/analysis_enc.c")
.file("c_src/src/enc/backward_references_cost_enc.c")
.file("c_src/src/enc/backward_references_enc.c")
.file("c_src/src/enc/config_enc.c")
.file("c_src/src/enc/cost_enc.c")
.file("c_src/src/enc/filter_enc.c")
.file("c_src/src/enc/frame_enc.c")
.file("c_src/src/enc/histogram_enc.c")
.file("c_src/src/enc/iterator_enc.c")
.file("c_src/src/enc/near_lossless_enc.c")
.file("c_src/src/enc/picture_enc.c")
.file("c_src/src/enc/picture_csp_enc.c")
.file("c_src/src/enc/picture_psnr_enc.c")
.file("c_src/src/enc/picture_rescale_enc.c")
.file("c_src/src/enc/picture_tools_enc.c")
.file("c_src/src/enc/predictor_enc.c")
.file("c_src/src/enc/quant_enc.c")
.file("c_src/src/enc/syntax_enc.c")
.file("c_src/src/enc/token_enc.c")
.file("c_src/src/enc/tree_enc.c")
.file("c_src/src/enc/vp8l_enc.c")
.file("c_src/src/enc/webp_enc.c")
// COMMON_SOURCES (src/dsp)
.file("c_src/src/dsp/alpha_processing.c")
.file("c_src/src/dsp/cpu.c")
.file("c_src/src/dsp/dec.c")
.file("c_src/src/dsp/dec_clip_tables.c")
.file("c_src/src/dsp/filters.c")
.file("c_src/src/dsp/lossless.c")
.file("c_src/src/dsp/rescaler.c")
.file("c_src/src/dsp/upsampling.c")
.file("c_src/src/dsp/yuv.c")
// ENC_SOURCES (src/dsp)
.file("c_src/src/dsp/cost.c")
.file("c_src/src/dsp/enc.c")
.file("c_src/src/dsp/lossless_enc.c")
.file("c_src/src/dsp/ssim.c")
// libwebpdspdecode_sse41_la_SOURCES (src/dsp)
.file("c_src/src/dsp/alpha_processing_sse41.c")
.file("c_src/src/dsp/dec_sse41.c")
.file("c_src/src/dsp/lossless_sse41.c")
.file("c_src/src/dsp/upsampling_sse41.c")
.file("c_src/src/dsp/yuv_sse41.c")
// libwebpdspdecode_sse2_la_SOURCES (src/dsp)
.file("c_src/src/dsp/alpha_processing_sse2.c")
.file("c_src/src/dsp/dec_sse2.c")
.file("c_src/src/dsp/filters_sse2.c")
.file("c_src/src/dsp/lossless_sse2.c")
.file("c_src/src/dsp/rescaler_sse2.c")
.file("c_src/src/dsp/upsampling_sse2.c")
.file("c_src/src/dsp/yuv_sse2.c")
// libwebpdspdecode_neon_la_SOURCES (src/dsp)
.file("c_src/src/dsp/alpha_processing_neon.c")
.file("c_src/src/dsp/dec_neon.c")
.file("c_src/src/dsp/filters_neon.c")
.file("c_src/src/dsp/lossless_neon.c")
.file("c_src/src/dsp/rescaler_neon.c")
.file("c_src/src/dsp/upsampling_neon.c")
.file("c_src/src/dsp/yuv_neon.c")
// libwebpdspdecode_msa_la_SOURCES (src/dsp)
.file("c_src/src/dsp/dec_msa.c")
.file("c_src/src/dsp/filters_msa.c")
.file("c_src/src/dsp/lossless_msa.c")
.file("c_src/src/dsp/rescaler_msa.c")
.file("c_src/src/dsp/upsampling_msa.c")
// libwebpdspdecode_mips32_la_SOURCES (src/dsp)
.file("c_src/src/dsp/dec_mips32.c")
.file("c_src/src/dsp/rescaler_mips32.c")
.file("c_src/src/dsp/yuv_mips32.c")
// libwebpdspdecode_mips_dsp_r2_la_SOURCES (src/dsp)
.file("c_src/src/dsp/alpha_processing_mips_dsp_r2.c")
.file("c_src/src/dsp/dec_mips_dsp_r2.c")
.file("c_src/src/dsp/filters_mips_dsp_r2.c")
.file("c_src/src/dsp/lossless_mips_dsp_r2.c")
.file("c_src/src/dsp/rescaler_mips_dsp_r2.c")
.file("c_src/src/dsp/upsampling_mips_dsp_r2.c")
.file("c_src/src/dsp/yuv_mips_dsp_r2.c")
// libwebpdsp_sse2_la_SOURCES (src/dsp)
.file("c_src/src/dsp/cost_sse2.c")
.file("c_src/src/dsp/enc_sse2.c")
.file("c_src/src/dsp/lossless_enc_sse2.c")
.file("c_src/src/dsp/ssim_sse2.c")
// libwebpdsp_sse41_la_SOURCES (src/dsp)
.file("c_src/src/dsp/enc_sse41.c")
.file("c_src/src/dsp/lossless_enc_sse41.c")
// libwebpdsp_neon_la_SOURCES (src/dsp)
.file("c_src/src/dsp/cost_neon.c")
.file("c_src/src/dsp/enc_neon.c")
.file("c_src/src/dsp/lossless_enc_neon.c")
// libwebpdsp_msa_la_SOURCES (src/dsp)
.file("c_src/src/dsp/enc_msa.c")
.file("c_src/src/dsp/lossless_enc_msa.c")
// libwebpdsp_mips32_la_SOURCES (src/dsp)
.file("c_src/src/dsp/cost_mips32.c")
.file("c_src/src/dsp/enc_mips32.c")
.file("c_src/src/dsp/lossless_enc_mips32.c")
// libwebpdsp_mips_dsp_r2_la_SOURCES (src/dsp)
.file("c_src/src/dsp/cost_mips_dsp_r2.c")
.file("c_src/src/dsp/enc_mips_dsp_r2.c")
.file("c_src/src/dsp/lossless_enc_mips_dsp_r2.c")
// COMMON_SOURCES (sharpyuv)
.file("c_src/sharpyuv/sharpyuv.c")
.file("c_src/sharpyuv/sharpyuv_cpu.c")
.file("c_src/sharpyuv/sharpyuv_csp.c")
.file("c_src/sharpyuv/sharpyuv_dsp.c")
.file("c_src/sharpyuv/sharpyuv_gamma.c")
.file("c_src/sharpyuv/sharpyuv_neon.c")
.file("c_src/sharpyuv/sharpyuv_sse2.c")
// COMMON_SOURCES (src/utils)
.file("c_src/src/utils/bit_reader_utils.c")
.file("c_src/src/utils/color_cache_utils.c")
.file("c_src/src/utils/filters_utils.c")
.file("c_src/src/utils/huffman_utils.c")
.file("c_src/src/utils/quant_levels_dec_utils.c")
.file("c_src/src/utils/rescaler_utils.c")
.file("c_src/src/utils/random_utils.c")
.file("c_src/src/utils/thread_utils.c")
.file("c_src/src/utils/utils.c")
// ENC_SOURCES (src/utils)
.file("c_src/src/utils/bit_writer_utils.c")
.file("c_src/src/utils/huffman_encode_utils.c")
.file("c_src/src/utils/quant_levels_utils.c")
.include("c_src")
.compile("webp");
if cfg!(feature = "demux") {
cc::Build::new()
// libwebpdemux_la_SOURCES (src/demux)
.file("c_src/src/demux/anim_decode.c")
.file("c_src/src/demux/demux.c")
.include("c_src")
.compile("webpdemux");
}
if cfg!(feature = "mux") {
cc::Build::new()
// libwebpmux_la_SOURCES (src/mux)
.file("c_src/src/mux/anim_encode.c")
.file("c_src/src/mux/muxedit.c")
.file("c_src/src/mux/muxinternal.c")
.file("c_src/src/mux/muxread.c")
.include("c_src")
.compile("webpmux");
}
}
#[cfg(not(target_env = "msvc"))]
fn try_vcpkg() -> bool {
false
}
#[cfg(target_env = "msvc")]
fn try_vcpkg() -> bool {
// see if there is a vcpkg tree with libwebp installed
match vcpkg::Config::new()
.emit_includes(true)
.lib_name("libwebp")
.probe("libwebp")
{
Ok(_) => true,
Err(e) => {
println!("note, vcpkg did not find libwebp: {}", e);
false
}
}
}
fn libwebp_installed() -> bool {
let compiler = cc::Build::new().get_compiler();
let mut cmd = Command::new(compiler.path());
cmd.arg("src/smoke.c")
.arg("-o")
.arg("/dev/null")
.arg("-lwebp");
println!("running {:?}", cmd);
if let Ok(status) = cmd.status() {
if status.success() {
return true;
}
}
false
}