xref: /aosp_15_r20/external/virtio-media/extras/ffmpeg-decoder/build.rs (revision 1b4853f54772485c5dd4001ae33a7a958bcc97a1)
1*1b4853f5SAndroid Build Coastguard Worker // Copyright 2022 The ChromiumOS Authors
2*1b4853f5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*1b4853f5SAndroid Build Coastguard Worker // found in the LICENSE file.
4*1b4853f5SAndroid Build Coastguard Worker 
5*1b4853f5SAndroid Build Coastguard Worker use std::path::PathBuf;
6*1b4853f5SAndroid Build Coastguard Worker 
7*1b4853f5SAndroid Build Coastguard Worker use pkg_config::Config;
8*1b4853f5SAndroid Build Coastguard Worker 
main()9*1b4853f5SAndroid Build Coastguard Worker fn main() {
10*1b4853f5SAndroid Build Coastguard Worker     // Skip building dependencies when generating documents.
11*1b4853f5SAndroid Build Coastguard Worker     if std::env::var("CARGO_DOC").is_ok() {
12*1b4853f5SAndroid Build Coastguard Worker         return;
13*1b4853f5SAndroid Build Coastguard Worker     }
14*1b4853f5SAndroid Build Coastguard Worker 
15*1b4853f5SAndroid Build Coastguard Worker     // ffmpeg is currently only supported on unix
16*1b4853f5SAndroid Build Coastguard Worker     if std::env::var("CARGO_CFG_UNIX").is_err() {
17*1b4853f5SAndroid Build Coastguard Worker         return;
18*1b4853f5SAndroid Build Coastguard Worker     }
19*1b4853f5SAndroid Build Coastguard Worker 
20*1b4853f5SAndroid Build Coastguard Worker     // ffmgeg is not supported by CI on 32-bit arm
21*1b4853f5SAndroid Build Coastguard Worker     if std::env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "arm" {
22*1b4853f5SAndroid Build Coastguard Worker         return;
23*1b4853f5SAndroid Build Coastguard Worker     }
24*1b4853f5SAndroid Build Coastguard Worker 
25*1b4853f5SAndroid Build Coastguard Worker     // Match all ffmpeg 6.0+ versions.
26*1b4853f5SAndroid Build Coastguard Worker     Config::new()
27*1b4853f5SAndroid Build Coastguard Worker         .atleast_version("60")
28*1b4853f5SAndroid Build Coastguard Worker         .probe("libavcodec")
29*1b4853f5SAndroid Build Coastguard Worker         .unwrap();
30*1b4853f5SAndroid Build Coastguard Worker     Config::new()
31*1b4853f5SAndroid Build Coastguard Worker         .atleast_version("58")
32*1b4853f5SAndroid Build Coastguard Worker         .probe("libavutil")
33*1b4853f5SAndroid Build Coastguard Worker         .unwrap();
34*1b4853f5SAndroid Build Coastguard Worker     Config::new()
35*1b4853f5SAndroid Build Coastguard Worker         .atleast_version("7")
36*1b4853f5SAndroid Build Coastguard Worker         .probe("libswscale")
37*1b4853f5SAndroid Build Coastguard Worker         .unwrap();
38*1b4853f5SAndroid Build Coastguard Worker 
39*1b4853f5SAndroid Build Coastguard Worker     let bindings = bindgen::Builder::default()
40*1b4853f5SAndroid Build Coastguard Worker         .header("src/bindings.h")
41*1b4853f5SAndroid Build Coastguard Worker         .allowlist_function("av_.*")
42*1b4853f5SAndroid Build Coastguard Worker         .allowlist_function("avcodec_.*")
43*1b4853f5SAndroid Build Coastguard Worker         .allowlist_function("sws_.*")
44*1b4853f5SAndroid Build Coastguard Worker         .allowlist_function("av_image_.*")
45*1b4853f5SAndroid Build Coastguard Worker         .allowlist_var("FF_PROFILE.*")
46*1b4853f5SAndroid Build Coastguard Worker         .allowlist_var("AV_.*")
47*1b4853f5SAndroid Build Coastguard Worker         .allowlist_var("AVERROR_.*")
48*1b4853f5SAndroid Build Coastguard Worker         // Skip va_list and functions that use it to avoid ABI problems on aarch64.
49*1b4853f5SAndroid Build Coastguard Worker         .blocklist_type(".*va_list.*")
50*1b4853f5SAndroid Build Coastguard Worker         .blocklist_function("av_log_.*")
51*1b4853f5SAndroid Build Coastguard Worker         .blocklist_function("av_vlog")
52*1b4853f5SAndroid Build Coastguard Worker         .generate()
53*1b4853f5SAndroid Build Coastguard Worker         .expect("failed to generate bindings");
54*1b4853f5SAndroid Build Coastguard Worker     let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
55*1b4853f5SAndroid Build Coastguard Worker     bindings
56*1b4853f5SAndroid Build Coastguard Worker         .write_to_file(out_path.join("bindings.rs"))
57*1b4853f5SAndroid Build Coastguard Worker         .expect("writing bindings to file failed");
58*1b4853f5SAndroid Build Coastguard Worker }
59