1 use std::env::{self, VarError};
2 use std::path::PathBuf;
3 
4 include!("bindgen.rs");
5 
6 /// Environment variable that can be set to point to the directory containing the `videodev2.h`
7 /// file to use to generate the bindings.
8 const V4L2R_VIDEODEV_ENV: &str = "V4L2R_VIDEODEV2_H_PATH";
9 
10 /// Default header file to parse if the `V4L2R_VIDEODEV2_H_PATH` environment variable is not set.
11 const DEFAULT_VIDEODEV2_H_PATH: &str = "/usr/include/linux";
12 
13 /// Wrapper file to use as input of bindgen.
14 const WRAPPER_H: &str = "v4l2r_wrapper.h";
15 
main()16 fn main() {
17     let videodev2_h_path = env::var(V4L2R_VIDEODEV_ENV)
18         .or_else(|e| {
19             if let VarError::NotPresent = e {
20                 Ok(DEFAULT_VIDEODEV2_H_PATH.to_string())
21             } else {
22                 Err(e)
23             }
24         })
25         .expect("invalid `V4L2R_VIDEODEV2_H_PATH` environment variable");
26 
27     let videodev2_h = PathBuf::from(videodev2_h_path.clone()).join("videodev2.h");
28 
29     println!("cargo::rerun-if-env-changed={}", V4L2R_VIDEODEV_ENV);
30     println!("cargo::rerun-if-changed={}", videodev2_h.display());
31     println!("cargo::rerun-if-changed={}", WRAPPER_H);
32 
33     let bindings = v4l2r_bindgen_builder(bindgen::Builder::default())
34         .header(WRAPPER_H)
35         .clang_args([format!("-I{}", videodev2_h_path)])
36         .generate()
37         .expect("unable to generate bindings");
38 
39     let out_path = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is not set"));
40     bindings
41         .write_to_file(out_path.join("bindings.rs"))
42         .expect("Couldn't write bindings!");
43 }
44