1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::env::{self};
6 use std::path::{Path, PathBuf};
7 
8 mod bindgen_gen;
9 use bindgen_gen::vaapi_gen_builder;
10 
11 /// Environment variable that can be set to point to the directory containing the `va.h`, `va_drm.h` and `va_drmcommon.h`
12 /// files to use to generate the bindings.
13 const CROS_LIBVA_H_PATH_ENV: &str = "CROS_LIBVA_H_PATH";
14 const CROS_LIBVA_LIB_PATH_ENV: &str = "CROS_LIBVA_LIB_PATH";
15 
16 /// Wrapper file to use as input of bindgen.
17 const WRAPPER_PATH: &str = "libva-wrapper.h";
18 
main()19 fn main() {
20     // Do not require dependencies when generating docs.
21     if std::env::var("CARGO_DOC").is_ok() || std::env::var("DOCS_RS").is_ok() {
22         return;
23     }
24 
25     let va_h_path = env::var(CROS_LIBVA_H_PATH_ENV).unwrap_or_default();
26     let va_lib_path = env::var(CROS_LIBVA_LIB_PATH_ENV).unwrap_or_default();
27     // Check the path exists.
28     if !va_h_path.is_empty() {
29         assert!(
30             Path::new(&va_h_path).exists(),
31             "{} doesn't exist",
32             va_h_path
33         );
34     }
35 
36     if !va_lib_path.is_empty() {
37         assert!(
38             Path::new(&va_h_path).exists(),
39             "{} doesn't exist",
40             va_h_path
41         );
42         println!("cargo:rustc-link-arg=-Wl,-rpath={}", va_lib_path);
43     }
44 
45     // Tell cargo to link va and va-drm objects dynamically.
46     println!("cargo:rustc-link-lib=dylib=va");
47     println!("cargo:rustc-link-lib=dylib=va-drm"); // for the vaGetDisplayDRM entrypoint
48 
49     let mut bindings_builder = vaapi_gen_builder(bindgen::builder()).header(WRAPPER_PATH);
50     if !va_h_path.is_empty() {
51         bindings_builder = bindings_builder.clang_arg(format!("-I{}", va_h_path));
52     }
53     let bindings = bindings_builder
54         .generate()
55         .expect("unable to generate bindings");
56 
57     let out_path = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is not set"));
58 
59     bindings
60         .write_to_file(out_path.join("bindings.rs"))
61         .expect("Couldn't write bindings!");
62 }
63