Skip to content

Commit de1f54a

Browse files
committed
Reorganize build script, more readable
1 parent fcd607f commit de1f54a

File tree

1 file changed

+113
-107
lines changed

1 file changed

+113
-107
lines changed

build.rs

Lines changed: 113 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ static HEADERS: Lazy<[&str; 64]> = Lazy::new(|| {
9191

9292
static PATH: Lazy<String> = Lazy::new(|| env::var("PATH").unwrap());
9393
static OUT_DIR: Lazy<String> = Lazy::new(|| env::var("OUT_DIR").unwrap());
94-
static FFMPEG_DIR: Lazy<String> = Lazy::new(|| format!("{}/ffmpeg", env::var("OUT_DIR").unwrap()));
95-
static BINDING_FILE_PATH: Lazy<String> =
96-
Lazy::new(|| format!("{}/binding.rs", env::var("OUT_DIR").unwrap()));
94+
static FFMPEG_DIR: Lazy<String> = Lazy::new(|| format!("{}/ffmpeg", *OUT_DIR));
95+
static BINDING_FILE_PATH: Lazy<String> = Lazy::new(|| format!("{}/binding.rs", *OUT_DIR));
9796
static NUM_CPUS: Lazy<usize> = Lazy::new(ncpus::get);
9897

9998
/// Filter out all symbols in the HashSet, and for others things it will act
@@ -121,7 +120,7 @@ fn probe_system_ffmpeg() -> Result<(), String> {
121120
.find(|libname| {
122121
pkgconfig::Config::new()
123122
.statik(true)
124-
// Remove side effect
123+
// Remove side effect by disable metadata emitting
125124
.cargo_metadata(false)
126125
.probe(&libname)
127126
.is_err()
@@ -131,6 +130,108 @@ fn probe_system_ffmpeg() -> Result<(), String> {
131130
}
132131
}
133132

133+
fn clone_and_build_ffmpeg() {
134+
// Check if FFmpeg is cloned.
135+
if !path::PathBuf::from(format!("{}/fftools", &*FFMPEG_DIR)).is_dir() {
136+
Command::new("git")
137+
.current_dir(&*OUT_DIR)
138+
.args(["clone", "https://github.com/ffmpeg/ffmpeg", "--depth", "1"].iter())
139+
.spawn()
140+
.expect("Failed to clone FFmpeg submodule.")
141+
.wait()
142+
.expect("Failed to clone FFmpeg submodule.");
143+
}
144+
145+
// All outputs are stored in ./ffmpeg/build/{bin, lib, share, include}
146+
// If no prebuilt FFmpeg libraries provided, we build a custom FFmpeg.
147+
148+
// Corresponding to the shell script below:
149+
// ./configure \
150+
// --prefix="$PWD/build" \
151+
// --extra-cflags="-I$PWD/build/include" \
152+
// --extra-ldflags="-L$PWD/build/lib" \
153+
// --bindir="$PWD/build/bin" \
154+
// --pkg-config-flags="--static" \
155+
// --extra-libs="-lpthread -lm" \
156+
// --enable-gpl \
157+
// --enable-libass \
158+
// --enable-libfdk-aac \
159+
// --enable-libfreetype \
160+
// --enable-libmp3lame \
161+
// --enable-libopus \
162+
// --enable-libvorbis \
163+
// --enable-libvpx \
164+
// --enable-libx264 \
165+
// --enable-libx265 \
166+
// --enable-nonfree
167+
Command::new(format!("{}/configure", *FFMPEG_DIR))
168+
.current_dir(&*FFMPEG_DIR)
169+
.env("PATH", format!("{}/build/bin:{}", *FFMPEG_DIR, *PATH))
170+
.env(
171+
"PKG_CONFIG_PATH",
172+
format!("{}/build/lib/pkgconfig", *FFMPEG_DIR),
173+
)
174+
.args(
175+
[
176+
format!(r#"--prefix={}/build"#, *FFMPEG_DIR),
177+
format!(r#"--extra-cflags=-I{}/build/include"#, *FFMPEG_DIR),
178+
format!(r#"--extra-ldflags=-L{}/build/lib"#, *FFMPEG_DIR),
179+
format!(r#"--bindir={}/build/bin"#, *FFMPEG_DIR),
180+
]
181+
.iter(),
182+
)
183+
.args(
184+
[
185+
"--pkg-config-flags=--static",
186+
"--extra-libs=-lpthread -lm",
187+
"--enable-gpl",
188+
"--enable-libass",
189+
"--enable-libfdk-aac",
190+
"--enable-libfreetype",
191+
"--enable-libmp3lame",
192+
"--enable-libopus",
193+
"--enable-libvorbis",
194+
"--enable-libvpx",
195+
"--enable-libx264",
196+
"--enable-libx265",
197+
"--enable-nonfree",
198+
]
199+
.iter(),
200+
)
201+
.spawn()
202+
.expect("FFmpeg build process: configure failed!")
203+
.wait()
204+
.expect("FFmpeg build process: configure failed!");
205+
206+
Command::new("make")
207+
.current_dir(&*FFMPEG_DIR)
208+
.env("PATH", format!("{}/build/bin:{}", *FFMPEG_DIR, *PATH))
209+
.arg(format!("-j{}", *NUM_CPUS))
210+
.spawn()
211+
.expect("FFmpeg build process: make compile failed!")
212+
.wait()
213+
.expect("FFmpeg build process: make compile failed!");
214+
215+
Command::new("make")
216+
.current_dir(&*FFMPEG_DIR)
217+
.arg(format!("-j{}", *NUM_CPUS))
218+
.arg("install")
219+
.spawn()
220+
.expect("FFmpeg build process: make install failed!")
221+
.wait()
222+
.expect("FFmpeg build process: make install failed!");
223+
224+
/* Commented because it's not needed, we are not using any specific shell.
225+
Command::new("hash")
226+
.current_dir(&*FFMPEG_DIR)
227+
.arg("-r")
228+
.spawn()
229+
.expect("FFmpeg build process: clear hash cache failed!")
230+
.wait()
231+
.expect("FFmpeg build process: clear hash cache failed!");
232+
*/
233+
}
234+
134235
fn main() {
135236
println!("cargo:rerun-if-changed=build.rs");
136237
println!("cargo:rerun-if-env-changed=DOCS_RS");
@@ -150,122 +251,27 @@ fn main() {
150251
}
151252

152253
if env::var("PKG_CONFIG_PATH").is_err() {
153-
// If no system FFmpeg found, download and build one
154254
if let Err(msg) = probe_system_ffmpeg() {
155-
eprintln!("{}! Try to git clone an FFmpeg and build.", msg);
156-
// All outputs are stored in ./ffmpeg/build/{bin, lib, share, include}
157-
// If no prebuilt FFmpeg libraries provided, we custom build a FFmpeg.
255+
// If no system FFmpeg found, download and build one
256+
eprintln!("{}! Start to git clone an FFmpeg and build.", msg);
257+
clone_and_build_ffmpeg();
158258
env::set_var(
159259
"PKG_CONFIG_PATH",
160260
format!("{}/build/lib/pkgconfig", *FFMPEG_DIR),
161261
);
162-
env::set_var("PATH", format!("{}/build/bin:{}", *FFMPEG_DIR, *PATH));
163-
164-
// Check if FFmpeg is not get cloned.
165-
if !path::PathBuf::from(format!("{}/fftools", &*FFMPEG_DIR)).is_dir() {
166-
Command::new("git")
167-
.current_dir(&*OUT_DIR)
168-
.args(["clone", "https://github.com/ffmpeg/ffmpeg", "--depth", "1"].iter())
169-
.spawn()
170-
.expect("FFmpeg submodule failed to clone.")
171-
.wait()
172-
.expect("FFmpeg submodule failed to clone.");
173-
}
174-
175-
// Corresponding to the shell script below:
176-
// ./configure \
177-
// --prefix="$PWD/build" \
178-
// --extra-cflags="-I$PWD/build/include" \
179-
// --extra-ldflags="-L$PWD/build/lib" \
180-
// --bindir="$PWD/build/bin" \
181-
// --pkg-config-flags="--static" \
182-
// --extra-libs="-lpthread -lm" \
183-
// --enable-gpl \
184-
// --enable-libass \
185-
// --enable-libfdk-aac \
186-
// --enable-libfreetype \
187-
// --enable-libmp3lame \
188-
// --enable-libopus \
189-
// --enable-libvorbis \
190-
// --enable-libvpx \
191-
// --enable-libx264 \
192-
// --enable-libx265 \
193-
// --enable-nonfree
194-
Command::new(format!("{}/configure", *FFMPEG_DIR))
195-
.current_dir(&*FFMPEG_DIR)
196-
.env(
197-
"PKG_CONFIG_PATH",
198-
format!("{}/build/lib/pkgconfig", *FFMPEG_DIR),
199-
)
200-
.args(
201-
[
202-
format!(r#"--prefix={}/build"#, *FFMPEG_DIR),
203-
format!(r#"--extra-cflags=-I{}/build/include"#, *FFMPEG_DIR),
204-
format!(r#"--extra-ldflags=-L{}/build/lib"#, *FFMPEG_DIR),
205-
format!(r#"--bindir={}/build/bin"#, *FFMPEG_DIR),
206-
]
207-
.iter(),
208-
)
209-
.args(
210-
[
211-
"--pkg-config-flags=--static",
212-
"--extra-libs=-lpthread -lm",
213-
"--enable-gpl",
214-
"--enable-libass",
215-
"--enable-libfdk-aac",
216-
"--enable-libfreetype",
217-
"--enable-libmp3lame",
218-
"--enable-libopus",
219-
"--enable-libvorbis",
220-
"--enable-libvpx",
221-
"--enable-libx264",
222-
"--enable-libx265",
223-
"--enable-nonfree",
224-
]
225-
.iter(),
226-
)
227-
.spawn()
228-
.expect("FFmpeg build process: configure failed!")
229-
.wait()
230-
.expect("FFmpeg build process: configure failed!");
231-
232-
Command::new("make")
233-
.current_dir(&*FFMPEG_DIR)
234-
.arg(format!("-j{}", *NUM_CPUS))
235-
.spawn()
236-
.expect("FFmpeg build process: make compile failed!")
237-
.wait()
238-
.expect("FFmpeg build process: make compile failed!");
239-
240-
Command::new("make")
241-
.current_dir(&*FFMPEG_DIR)
242-
.arg(format!("-j{}", *NUM_CPUS))
243-
.arg("install")
244-
.spawn()
245-
.expect("FFmpeg build process: make install failed!")
246-
.wait()
247-
.expect("FFmpeg build process: make install failed!");
248-
249-
/* Commented because it's not needed, we are not using any specific shell.
250-
Command::new("hash")
251-
.current_dir(&*FFMPEG_DIR)
252-
.arg("-r")
253-
.spawn()
254-
.expect("FFmpeg build process: clear hash cache failed!")
255-
.wait()
256-
.expect("FFmpeg build process: clear hash cache failed!");
257-
*/
258262
}
259263
}
264+
// Now we can ensure available FFmpeg libraries.
260265

261-
// We currently only support building with static libraries.
262-
// Probe libraries
263-
// TODO if not enabled, we should not probe it. Should modify probe_system_ffmpeg() too.
266+
// Probe libraries(enable emitting cargo metadata)
267+
// TODO: if specific library is not enabled, we should not probe it. If we
268+
// want to implement this, we Should modify probe_system_ffmpeg() too.
264269
let include_paths = (&*LIBS)
265270
.iter()
266271
.map(|name| "lib".to_owned() + name)
267272
.map(|libname| {
268273
pkgconfig::Config::new()
274+
// currently only support building with static libraries.
269275
.statik(true)
270276
.cargo_metadata(true)
271277
.probe(&libname)

0 commit comments

Comments
 (0)