-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.rs
221 lines (195 loc) · 6.41 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
#![allow(unused)]
use std::convert::AsRef;
use std::env;
use std::ffi::OsString;
use std::path::{PathBuf, Path};
use std::process::Command;
use std::string::ToString;
use tar::Archive;
use flate2::read::GzDecoder;
///////////////////////////////////////////////////////////////////////////////
// UTILS - ENVIROMENT
///////////////////////////////////////////////////////////////////////////////
fn is_release_mode() -> bool {
let value = std::env::var("PROFILE")
.expect("missing PROFILE")
.to_lowercase();
&value == "release"
}
fn is_debug_mode() -> bool {
let value = std::env::var("PROFILE")
.expect("missing PROFILE")
.to_lowercase();
&value == "debug"
}
fn out_dir() -> PathBuf {
PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR env var"))
}
fn source_path() -> PathBuf {
out_dir().join("x264-stable")
}
fn install_prefix() -> PathBuf {
out_dir().join("build")
}
///////////////////////////////////////////////////////////////////////////////
// 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 build_x264() {
let source_path = source_path();
let mut prefix_arg = OsString::from("--prefix=");
prefix_arg.push(&install_prefix());
let result = Command::new("bash")
.arg("configure")
.arg(prefix_arg)
.arg("--disable-cli")
.arg("--enable-static")
.current_dir(&source_path)
.status()
.unwrap();
if !result.success() {
panic!("failed to configure x264");
}
let result = Command::new("make")
.arg("-j")
.arg(num_cpus::get().to_string())
.arg("all")
.arg("install")
.current_dir(&source_path)
.status()
.unwrap();
if !result.success() {
panic!("Failed to build x264");
}
}
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)] = &[
("x264", "./libx264.a"),
];
pub const HEADERS: &[&str] = &[
"x264.h",
];
///////////////////////////////////////////////////////////////////////////////
// BUILD PIPELINE
///////////////////////////////////////////////////////////////////////////////
fn build() {
// SETUP
extract_tar_file("archive/x264-stable.tar.gz", &out_dir());
assert!(source_path().exists());
// BUILD
build_x264();
// LINK
println!("cargo:rustc-link-search=native={}", {
install_prefix().join("lib").to_str().unwrap()
});
for (name, _) in STATIC_LIBS {
println!("cargo:rustc-link-lib=static={}", name);
}
// CODEGEN
let codegen = |file_name: &str, headers: &[&str]| {
let codegen = bindgen::Builder::default();
let codegen = codegen.header("include/prelude.h");
let codegen = headers
.iter()
.fold(codegen, |codegen: bindgen::Builder, path: &&str| -> bindgen::Builder {
let path: &str = path.clone();
let path: PathBuf = install_prefix().join("include").join(path);
let path: &str = path.to_str().expect("PathBuf to str");
assert!(PathBuf::from(path).exists());
codegen.header(path)
});
codegen
.generate_comments(true)
.generate()
.expect("Unable to generate bindings")
.write_to_file(out_dir().join(file_name))
.expect("Couldn't write bindings!");
};
codegen("bindings_x264.rs", HEADERS);
// CARGO METADATA
println!("cargo:libs={}", install_prefix().join("lib").to_str().unwrap());
println!("cargo:pkgconfig={}", install_prefix().join("lib").join("pkgconfig").to_str().unwrap());
}
///////////////////////////////////////////////////////////////////////////////
// MAIN
///////////////////////////////////////////////////////////////////////////////
fn main() {
build();
}