-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
118 lines (101 loc) · 2.58 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
use std::iter::FromIterator;
use std::collections::HashSet;
use std::path::PathBuf;
use std::env;
/*
* utils - environment
*/
#[allow(unused)]
fn out_dir() -> PathBuf {
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env var"))
}
#[allow(unused)]
fn is_release_mode() -> bool {
has_env_var_with_value("PROFILE", "release")
}
#[allow(unused)]
fn is_debug_mode() -> bool {
has_env_var_with_value("PROFILE", "debug")
}
#[allow(unused)]
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.as_str() == v)
.unwrap_or(false)
}
/*
* paths
*/
pub const LIBS: &[&str] = &[
"avcodec",
"avdevice",
"avfilter",
"avformat",
"avutil",
"swresample",
"swscale"
];
/*
* 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() {
/* Link */
for name in LIBS {
println!("cargo::rustc-link-lib=dylib={}", name);
}
/* codegen */
{
let gen_file_name = "src/sys.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"),
]));
if has_env_var_with_value("FF_DO_CODEGEN", "1") {
/*
* RUN
*/
bindgen::Builder::default()
.header("headers.h")
.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(gen_file_name)
.expect("Couldn't write bindings!");
}
}
/* complite cbits */
cc::Build::new()
.file("cbits/defs.c")
.compile("cbits");
}
/* main */
fn main() {
build();
}