Skip to content

Commit c35cfe5

Browse files
stephaneyfxtilpner
authored andcommitted
Link to pre-built libraries (meh#72)
Link to pre-built libraries in FFMPEG_DIR.
1 parent f10922d commit c35cfe5

File tree

1 file changed

+60
-55
lines changed

1 file changed

+60
-55
lines changed

sys/build.rs

+60-55
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,34 @@ use std::str;
1414
use regex::Regex;
1515
use bindgen::callbacks::{IntKind, ParseCallbacks};
1616

17+
#[derive(Debug)]
18+
struct Library {
19+
name: &'static str,
20+
is_feature: bool,
21+
}
22+
23+
impl Library {
24+
fn feature_name(&self) -> Option<String> {
25+
if self.is_feature {
26+
Some("CARGO_FEATURE_".to_string() + &self.name.to_uppercase())
27+
} else {
28+
None
29+
}
30+
}
31+
}
32+
33+
static LIBRARIES: &[Library] = &[
34+
Library {name: "avcodec", is_feature: true},
35+
Library {name: "avdevice", is_feature: true},
36+
Library {name: "avfilter", is_feature: true},
37+
Library {name: "avformat", is_feature: true},
38+
Library {name: "avresample", is_feature: true},
39+
Library {name: "avutil", is_feature: false},
40+
Library {name: "postproc", is_feature: true},
41+
Library {name: "swresample", is_feature: true},
42+
Library {name: "swscale", is_feature: true},
43+
];
44+
1745
#[derive(Debug)]
1846
struct IntCallbacks;
1947

@@ -93,6 +121,16 @@ fn fetch() -> io::Result<()> {
93121
}
94122
}
95123

124+
fn switch(configure: &mut Command, feature: &str, name: &str) {
125+
let arg = if env::var("CARGO_FEATURE_".to_string() + feature).is_ok() {
126+
"--enable-"
127+
}
128+
else {
129+
"--disable-"
130+
};
131+
configure.arg(arg.to_string() + name);
132+
}
133+
96134
fn build() -> io::Result<()> {
97135
let mut configure = Command::new("./configure");
98136
configure.current_dir(&source());
@@ -120,17 +158,6 @@ fn build() -> io::Result<()> {
120158
// do not build programs since we don't need them
121159
configure.arg("--disable-programs");
122160

123-
macro_rules! switch {
124-
($conf:expr, $feat:expr, $name:expr) => (
125-
if env::var(concat!("CARGO_FEATURE_", $feat)).is_ok() {
126-
$conf.arg(concat!("--enable-", $name));
127-
}
128-
else {
129-
$conf.arg(concat!("--disable-", $name));
130-
}
131-
)
132-
}
133-
134161
macro_rules! enable {
135162
($conf:expr, $feat:expr, $name:expr) => (
136163
if env::var(concat!("CARGO_FEATURE_", $feat)).is_ok() {
@@ -148,23 +175,18 @@ fn build() -> io::Result<()> {
148175
// }
149176

150177
// the binary using ffmpeg-sys must comply with GPL
151-
switch!(configure, "BUILD_LICENSE_GPL", "gpl");
178+
switch(&mut configure, "BUILD_LICENSE_GPL", "gpl");
152179

153180
// the binary using ffmpeg-sys must comply with (L)GPLv3
154-
switch!(configure, "BUILD_LICENSE_VERSION3", "version3");
181+
switch(&mut configure, "BUILD_LICENSE_VERSION3", "version3");
155182

156183
// the binary using ffmpeg-sys cannot be redistributed
157-
switch!(configure, "BUILD_LICENSE_NONFREE", "nonfree");
184+
switch(&mut configure, "BUILD_LICENSE_NONFREE", "nonfree");
158185

159186
// configure building libraries based on features
160-
switch!(configure, "AVCODEC", "avcodec");
161-
switch!(configure, "AVDEVICE", "avdevice");
162-
switch!(configure, "AVFILTER", "avfilter");
163-
switch!(configure, "AVFORMAT", "avformat");
164-
switch!(configure, "AVRESAMPLE", "avresample");
165-
switch!(configure, "POSTPROC", "postproc");
166-
switch!(configure, "SWRESAMPLE", "swresample");
167-
switch!(configure, "SWSCALE", "swscale");
187+
for lib in LIBRARIES.iter().filter(|lib| lib.is_feature) {
188+
switch(&mut configure, &lib.name.to_uppercase(), lib.name);
189+
}
168190

169191
// configure external SSL libraries
170192
enable!(configure, "BUILD_LIB_GNUTLS", "gnutls");
@@ -432,6 +454,20 @@ fn search_include(include_paths: &Vec<PathBuf>, header: &str) -> String {
432454
format!("/usr/include/{}", header)
433455
}
434456

457+
fn link_to_libraries(statik: bool) {
458+
let ffmpeg_ty = if statik { "static" } else { "dylib" };
459+
for lib in LIBRARIES {
460+
let feat_is_enabled =
461+
lib.feature_name().and_then(|f| env::var(&f).ok()).is_some();
462+
if !lib.is_feature || feat_is_enabled {
463+
println!("cargo:rustc-link-lib={}={}", ffmpeg_ty, lib.name);
464+
}
465+
}
466+
if env::var("CARGO_FEATURE_BUILD_ZLIB").is_ok() && cfg!(target_os = "linux") {
467+
println!("cargo:rustc-link-lib=z");
468+
}
469+
}
470+
435471
fn main() {
436472
let statik = env::var("CARGO_FEATURE_STATIC").is_ok();
437473

@@ -440,37 +476,7 @@ fn main() {
440476
"cargo:rustc-link-search=native={}",
441477
search().join("lib").to_string_lossy()
442478
);
443-
444-
let ffmpeg_ty = if statik { "static" } else { "dylib" };
445-
446-
// Make sure to link with the ffmpeg libs we built
447-
println!("cargo:rustc-link-lib={}=avutil", ffmpeg_ty);
448-
if env::var("CARGO_FEATURE_AVCODEC").is_ok() {
449-
println!("cargo:rustc-link-lib={}=avcodec", ffmpeg_ty);
450-
}
451-
if env::var("CARGO_FEATURE_AVFORMAT").is_ok() {
452-
println!("cargo:rustc-link-lib={}=avformat", ffmpeg_ty);
453-
}
454-
if env::var("CARGO_FEATURE_AVFILTER").is_ok() {
455-
println!("cargo:rustc-link-lib={}=avfilter", ffmpeg_ty);
456-
}
457-
if env::var("CARGO_FEATURE_AVDEVICE").is_ok() {
458-
println!("cargo:rustc-link-lib={}=avdevice", ffmpeg_ty);
459-
}
460-
if env::var("CARGO_FEATURE_AVRESAMPLE").is_ok() {
461-
println!("cargo:rustc-link-lib={}=avresample", ffmpeg_ty);
462-
}
463-
if env::var("CARGO_FEATURE_SWSCALE").is_ok() {
464-
println!("cargo:rustc-link-lib={}=swscale", ffmpeg_ty);
465-
}
466-
if env::var("CARGO_FEATURE_SWRESAMPLE").is_ok() {
467-
println!("cargo:rustc-link-lib={}=swresample", ffmpeg_ty);
468-
}
469-
470-
if env::var("CARGO_FEATURE_BUILD_ZLIB").is_ok() && cfg!(target_os = "linux") {
471-
println!("cargo:rustc-link-lib=z");
472-
}
473-
479+
link_to_libraries(statik);
474480
if fs::metadata(&search().join("lib").join("libavutil.a")).is_err() {
475481
fs::create_dir_all(&output())
476482
.ok()
@@ -505,12 +511,11 @@ fn main() {
505511
// Use prebuilt library
506512
else if let Ok(ffmpeg_dir) = env::var("FFMPEG_DIR") {
507513
let ffmpeg_dir = PathBuf::from(ffmpeg_dir);
508-
509514
println!(
510515
"cargo:rustc-link-search=native={}",
511516
ffmpeg_dir.join("lib").to_string_lossy()
512517
);
513-
518+
link_to_libraries(statik);
514519
vec![ffmpeg_dir.join("include")]
515520
}
516521
// Fallback to pkg-config

0 commit comments

Comments
 (0)