@@ -91,9 +91,8 @@ static HEADERS: Lazy<[&str; 64]> = Lazy::new(|| {
91
91
92
92
static PATH : Lazy < String > = Lazy :: new ( || env:: var ( "PATH" ) . unwrap ( ) ) ;
93
93
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 ) ) ;
97
96
static NUM_CPUS : Lazy < usize > = Lazy :: new ( ncpus:: get) ;
98
97
99
98
/// Filter out all symbols in the HashSet, and for others things it will act
@@ -121,7 +120,7 @@ fn probe_system_ffmpeg() -> Result<(), String> {
121
120
. find ( |libname| {
122
121
pkgconfig:: Config :: new ( )
123
122
. statik ( true )
124
- // Remove side effect
123
+ // Remove side effect by disable metadata emitting
125
124
. cargo_metadata ( false )
126
125
. probe ( & libname)
127
126
. is_err ( )
@@ -131,6 +130,108 @@ fn probe_system_ffmpeg() -> Result<(), String> {
131
130
}
132
131
}
133
132
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
+
134
235
fn main ( ) {
135
236
println ! ( "cargo:rerun-if-changed=build.rs" ) ;
136
237
println ! ( "cargo:rerun-if-env-changed=DOCS_RS" ) ;
@@ -150,122 +251,27 @@ fn main() {
150
251
}
151
252
152
253
if env:: var ( "PKG_CONFIG_PATH" ) . is_err ( ) {
153
- // If no system FFmpeg found, download and build one
154
254
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 ( ) ;
158
258
env:: set_var (
159
259
"PKG_CONFIG_PATH" ,
160
260
format ! ( "{}/build/lib/pkgconfig" , * FFMPEG_DIR ) ,
161
261
) ;
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
- */
258
262
}
259
263
}
264
+ // Now we can ensure available FFmpeg libraries.
260
265
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.
264
269
let include_paths = ( & * LIBS )
265
270
. iter ( )
266
271
. map ( |name| "lib" . to_owned ( ) + name)
267
272
. map ( |libname| {
268
273
pkgconfig:: Config :: new ( )
274
+ // currently only support building with static libraries.
269
275
. statik ( true )
270
276
. cargo_metadata ( true )
271
277
. probe ( & libname)
0 commit comments