-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.rs
395 lines (368 loc) · 12.9 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#![allow(unused)]
use std::iter::FromIterator;
use std::collections::HashSet;
use std::convert::AsRef;
use std::path::{PathBuf, Path};
use std::string::ToString;
use tar::Archive;
use flate2::read::GzDecoder;
///////////////////////////////////////////////////////////////////////////////
// UTILS - ENVIROMENT
///////////////////////////////////////////////////////////////////////////////
fn out_dir() -> PathBuf {
PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR env var"))
}
fn is_release_mode() -> bool {
has_env_var_with_value("PROFILE", "release")
}
fn is_debug_mode() -> bool {
has_env_var_with_value("PROFILE", "debug")
}
fn opt_level_eq(x: u8) -> bool {
has_env_var_with_value("OPT_LEVEL", &format!("{}", x))
}
fn has_env_var_with_value(s: &str, v: &str) -> bool {
std::env::var(s)
.map(|x| x.to_lowercase())
.map(|x| x == v.to_lowercase())
.unwrap_or(false)
}
///////////////////////////////////////////////////////////////////////////////
// UTILS - BUILD
///////////////////////////////////////////////////////////////////////////////
pub fn extract_tar_file<P: AsRef<Path>, Q: AsRef<Path>>(tar_file: P, dest: Q) -> Result<(), String> {
let source = std::fs::read(tar_file).expect("read tar file");
let tar = GzDecoder::new(&source[..]);
let mut archive = Archive::new(tar);
// UNPACK ARCHIVE
let tmp_source_dir: Option<PathBuf> = {
archive
.unpack(&dest)
.map_err(|x| format!("[{:?}] failed to unpack tar file: {:?}", dest.as_ref(), x))?;
let xs = std::fs::read_dir(&dest)
.expect(&format!("unable to read dir {:?}", dest.as_ref()))
.filter_map(Result::ok)
.filter(|file| file.file_type().map(|x| x.is_dir()).unwrap_or(false))
.collect::<Vec<std::fs::DirEntry>>();
match &xs[..] {
[x] => Some(x.path()),
_ => None,
}
};
Ok(())
}
pub fn lookup_newest(paths: Vec<PathBuf>) -> Option<PathBuf> {
use std::time::{SystemTime, Duration};
let mut newest: Option<(PathBuf, Duration)> = None;
paths
.clone()
.into_iter()
.filter_map(|x: PathBuf| {
let timestamp = x
.metadata()
.ok()
.and_then(|y| y.created().ok())
.and_then(|x| x.duration_since(SystemTime::UNIX_EPOCH).ok());
match timestamp {
Some(y) => Some((x, y)),
_ => None
}
})
.for_each(|(x_path, x_created)| match &newest {
None => {
newest = Some((x_path, x_created));
}
Some((_, y_created)) => {
if &x_created > y_created {
newest = Some((x_path, x_created));
}
}
});
// DONE
newest.map(|(x, _)| x)
}
pub fn files_with_prefix(dir: &PathBuf, pattern: &str) -> Vec<PathBuf> {
std::fs::read_dir(dir)
.expect(&format!("get dir contents: {:?}", dir))
.filter_map(Result::ok)
.filter_map(|x| {
let file_name = x
.file_name()
.to_str()?
.to_owned();
if file_name.starts_with(pattern) {
Some(x.path())
} else {
None
}
})
.collect::<Vec<_>>()
}
fn run_make(source_path: &PathBuf, makefile: &str) {
let result = std::process::Command::new("make")
.arg("-C")
.arg(source_path)
.arg("-f")
.arg(makefile)
.output()
.expect(&format!("make -C {:?} failed", source_path));
assert!(result.status.success());
}
fn cpy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
std::fs::copy(&from, &to)
.expect(&format!(
"unable to cpy from {:?} to {:?}",
from.as_ref(),
to.as_ref(),
));
}
///////////////////////////////////////////////////////////////////////////////
// PATHS
///////////////////////////////////////////////////////////////////////////////
pub const STATIC_LIBS: &[(&str, &str)] = &[
(
"avcodec",
"libavcodec/libavcodec.a",
),
(
"avdevice",
"libavdevice/libavdevice.a",
),
(
"avfilter",
"libavfilter/libavfilter.a",
),
(
"avformat",
"libavformat/libavformat.a",
),
(
"avutil",
"libavutil/libavutil.a",
),
(
"swresample",
"libswresample/libswresample.a",
),
(
"swscale",
"libswscale/libswscale.a",
),
];
pub const SEARCH_PATHS: &[&str] = &[
"libavcodec",
"libavdevice",
"libavfilter",
"libavformat",
"libavresample",
"libavutil",
"libpostproc",
"libswresample",
"libswscale",
];
///////////////////////////////////////////////////////////////////////////////
// CODEGEN
///////////////////////////////////////////////////////////////////////////////
// See https://github.com/rust-lang/rust-bindgen/issues/687#issuecomment-450750547
#[derive(Debug, Clone)]
struct IgnoreMacros(HashSet<String>);
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}
///////////////////////////////////////////////////////////////////////////////
// BUILD PIPELINE
///////////////////////////////////////////////////////////////////////////////
fn build() {
let out_path = out_dir();
let source_path = out_path.join("FFmpeg-FFmpeg-2722fc2");
// SPEED UP DEV - UNLESS IN RELASE MODE
let already_built = {
STATIC_LIBS
.iter()
.map(|(_, x)| source_path.join(x))
.all(|x| x.exists())
};
let mut skip_build = already_built && !is_release_mode();
if has_env_var_with_value("FFDEV1", "1") {
skip_build = false;
}
// EXTRACT
if !source_path.exists() || !skip_build {
{
let result = std::process::Command::new("tar")
.arg("-xJf")
.arg("archive/FFmpeg-FFmpeg-2722fc2.tar.xz")
.arg("-C")
.arg(out_path.to_str().expect("PathBuf to str"))
.output()
.expect("tar decompression of ffmpeg source repo using xz (to fit the 10M crates limit)");
assert!(result.status.success());
}
assert!(source_path.exists());
}
// BUILD CODE PHASE
if skip_build == false {
// CONFIGURE
{
let mut configure_flags = vec![
"--disable-programs",
"--disable-doc",
"--disable-autodetect",
];
// TRY TO SPEED THIS UP FOR DEV BUILDS
if is_debug_mode() && opt_level_eq(0) {
configure_flags.push("--disable-optimizations");
configure_flags.push("--disable-debug");
configure_flags.push("--disable-stripping");
}
let eval_configure = |flags: Vec<&str>| {
let flags = flags.join(" ");
std::process::Command::new("sh")
.arg("-c")
.arg(&format!(
"cd {path} && ./configure {flags}",
path=source_path.to_str().expect("PathBuf to str"),
flags=flags,
))
.output()
.expect(&format!("ffmpeg configure script"))
};
let result = eval_configure(configure_flags.clone());
if !result.status.success() {
let stderr = String::from_utf8(result.stderr).expect("invalid str");
let stdout = String::from_utf8(result.stdout).expect("invalid str");
let nasm_yasm_issue = stderr
.lines()
.chain(stdout.lines())
.any(|x| x.contains("nasm/yasm not found or too old"));
// MAYBE RETRY (USE CRIPPLED BUILD)
if nasm_yasm_issue {
configure_flags.push("--disable-x86asm");
let result = eval_configure(configure_flags);
if !result.status.success() {
let stderr = String::from_utf8(result.stderr).expect("invalid str");
let stdout = String::from_utf8(result.stdout).expect("invalid str");
panic!("configure failed:\n{}", vec![stderr, stdout].join("\n"));
}
} else {
panic!("configure failed:\n{}", vec![stderr, stdout].join("\n"));
}
}
}
// BUILD
{
let mut cpu_number = num_cpus::get();
let result = std::process::Command::new("make")
.arg("-C")
.arg(&source_path)
.arg("-f")
.arg("Makefile")
.arg(&format!("-j{}", cpu_number))
.output()
.expect(&format!("make -C {:?} failed", source_path));
if !result.status.success() {
let stderr = format!(
"* stderr:\n{}",
String::from_utf8(result.stderr).expect("invalid utf8 str from make stderr")
);
let stdout = format!(
"* stdout:\n{}",
String::from_utf8(result.stdout).expect("invalid utf8 str from make stdout")
);
panic!("make failed:\n{}", vec![stderr, stdout].join("\n\n"));
}
}
}
// LINK
println!("cargo:rustc-link-search=native={}", source_path.to_str().expect("PathBuf to str"));
for path in SEARCH_PATHS {
println!("cargo:rustc-link-search=native={}", {
source_path.join(path).to_str().expect("PathBuf as str")
});
}
for (name, _) in STATIC_LIBS {
println!("cargo:rustc-link-lib=static={}", name);
}
// CODEGEN
{
// SETUP
println!("rerun-if-changed=headers");
let ffmpeg_headers = std::fs::read("headers").expect("unable to read headers file");
let ffmpeg_headers = String::from_utf8(ffmpeg_headers).expect("invalid utf8 file");
let ffmpeg_headers = ffmpeg_headers
.lines()
.collect::<Vec<&str>>();
assert!(
ffmpeg_headers
.iter()
.map(|x| x.trim())
.all(|x| !x.is_empty())
);
let gen_file_name = "bindings_ffmpeg.rs";
let ignored_macros = IgnoreMacros(HashSet::from_iter(vec![
String::from("FP_INFINITE"),
String::from("FP_NAN"),
String::from("FP_NORMAL"),
String::from("FP_SUBNORMAL"),
String::from("FP_ZERO"),
String::from("IPPORT_RESERVED"),
]));
let mut skip_codegen = out_path.join(gen_file_name).exists();
if has_env_var_with_value("FFDEV2", "2") {
skip_codegen = false;
}
// CONFIG
if !skip_codegen {
let codegen = bindgen::Builder::default();
let codegen = codegen.clang_arg(format!("-I{}", source_path.to_str().expect("PathBuf to str")));
let mut missing = Vec::new();
let codegen = ffmpeg_headers
.iter()
.fold(codegen, |codegen: bindgen::Builder, path: &&str| -> bindgen::Builder {
let path: &str = path.clone();
let path: PathBuf = source_path.join(path);
let path: &str = path.to_str().expect("PathBuf to str");
if !PathBuf::from(path).exists() {
missing.push(String::from(path));
codegen
} else {
codegen.header(path)
}
});
if !missing.is_empty() {
panic!("missing headers: {:#?}", missing);
}
// RUN
codegen
.parse_callbacks(Box::new(ignored_macros.clone()))
.layout_tests(false)
.rustfmt_bindings(true)
.detect_include_paths(true)
.generate_comments(true)
.generate()
.expect("Unable to generate bindings")
.write_to_file(out_path.join(gen_file_name))
.expect("Couldn't write bindings!");
}
}
// COMPILE CBITS
cc::Build::new()
.include({
source_path.to_str().expect("PathBuf to str")
})
.file("cbits/defs.c")
.file("cbits/img_utils.c")
.compile("cbits");
}
///////////////////////////////////////////////////////////////////////////////
// MAIN
///////////////////////////////////////////////////////////////////////////////
fn main() {
build();
}