@@ -14,6 +14,34 @@ use std::str;
14
14
use regex:: Regex ;
15
15
use bindgen:: callbacks:: { IntKind , ParseCallbacks } ;
16
16
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
+
17
45
#[ derive( Debug ) ]
18
46
struct IntCallbacks ;
19
47
@@ -93,6 +121,16 @@ fn fetch() -> io::Result<()> {
93
121
}
94
122
}
95
123
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
+
96
134
fn build ( ) -> io:: Result < ( ) > {
97
135
let mut configure = Command :: new ( "./configure" ) ;
98
136
configure. current_dir ( & source ( ) ) ;
@@ -120,17 +158,6 @@ fn build() -> io::Result<()> {
120
158
// do not build programs since we don't need them
121
159
configure. arg ( "--disable-programs" ) ;
122
160
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
-
134
161
macro_rules! enable {
135
162
( $conf: expr, $feat: expr, $name: expr) => (
136
163
if env:: var( concat!( "CARGO_FEATURE_" , $feat) ) . is_ok( ) {
@@ -148,23 +175,18 @@ fn build() -> io::Result<()> {
148
175
// }
149
176
150
177
// the binary using ffmpeg-sys must comply with GPL
151
- switch ! ( configure, "BUILD_LICENSE_GPL" , "gpl" ) ;
178
+ switch ( & mut configure, "BUILD_LICENSE_GPL" , "gpl" ) ;
152
179
153
180
// 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" ) ;
155
182
156
183
// the binary using ffmpeg-sys cannot be redistributed
157
- switch ! ( configure, "BUILD_LICENSE_NONFREE" , "nonfree" ) ;
184
+ switch ( & mut configure, "BUILD_LICENSE_NONFREE" , "nonfree" ) ;
158
185
159
186
// 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
+ }
168
190
169
191
// configure external SSL libraries
170
192
enable ! ( configure, "BUILD_LIB_GNUTLS" , "gnutls" ) ;
@@ -432,6 +454,20 @@ fn search_include(include_paths: &Vec<PathBuf>, header: &str) -> String {
432
454
format ! ( "/usr/include/{}" , header)
433
455
}
434
456
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
+
435
471
fn main ( ) {
436
472
let statik = env:: var ( "CARGO_FEATURE_STATIC" ) . is_ok ( ) ;
437
473
@@ -440,37 +476,7 @@ fn main() {
440
476
"cargo:rustc-link-search=native={}" ,
441
477
search( ) . join( "lib" ) . to_string_lossy( )
442
478
) ;
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) ;
474
480
if fs:: metadata ( & search ( ) . join ( "lib" ) . join ( "libavutil.a" ) ) . is_err ( ) {
475
481
fs:: create_dir_all ( & output ( ) )
476
482
. ok ( )
@@ -505,12 +511,11 @@ fn main() {
505
511
// Use prebuilt library
506
512
else if let Ok ( ffmpeg_dir) = env:: var ( "FFMPEG_DIR" ) {
507
513
let ffmpeg_dir = PathBuf :: from ( ffmpeg_dir) ;
508
-
509
514
println ! (
510
515
"cargo:rustc-link-search=native={}" ,
511
516
ffmpeg_dir. join( "lib" ) . to_string_lossy( )
512
517
) ;
513
-
518
+ link_to_libraries ( statik ) ;
514
519
vec ! [ ffmpeg_dir. join( "include" ) ]
515
520
}
516
521
// Fallback to pkg-config
0 commit comments