1*8617a60dSAndroid Build Coastguard Worker // Copyright 2019 The ChromiumOS Authors
2*8617a60dSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*8617a60dSAndroid Build Coastguard Worker // found in the LICENSE file.
4*8617a60dSAndroid Build Coastguard Worker
5*8617a60dSAndroid Build Coastguard Worker /// Minijail's build script invoked by cargo.
6*8617a60dSAndroid Build Coastguard Worker ///
7*8617a60dSAndroid Build Coastguard Worker /// This script prefers linking against a pkg-config provided libminijail, but will fall back to
8*8617a60dSAndroid Build Coastguard Worker /// building libminijail statically.
9*8617a60dSAndroid Build Coastguard Worker use std::env;
10*8617a60dSAndroid Build Coastguard Worker use std::fs::remove_file;
11*8617a60dSAndroid Build Coastguard Worker use std::path::Path;
12*8617a60dSAndroid Build Coastguard Worker
13*8617a60dSAndroid Build Coastguard Worker use anyhow::{Context, Result};
14*8617a60dSAndroid Build Coastguard Worker use bindgen::{Builder, EnumVariation};
15*8617a60dSAndroid Build Coastguard Worker
16*8617a60dSAndroid Build Coastguard Worker static COMMON_CFLAGS: &[&str] = &[
17*8617a60dSAndroid Build Coastguard Worker "-DUSE_BINDGEN",
18*8617a60dSAndroid Build Coastguard Worker "-D_FILE_OFFSET_BITS=64",
19*8617a60dSAndroid Build Coastguard Worker "-D_LARGEFILE_SOURCE",
20*8617a60dSAndroid Build Coastguard Worker "-D_LARGEFILE64_SOURCE",
21*8617a60dSAndroid Build Coastguard Worker ];
22*8617a60dSAndroid Build Coastguard Worker
get_bindgen_builder() -> Builder23*8617a60dSAndroid Build Coastguard Worker fn get_bindgen_builder() -> Builder {
24*8617a60dSAndroid Build Coastguard Worker bindgen::builder()
25*8617a60dSAndroid Build Coastguard Worker .default_enum_style(EnumVariation::Rust {
26*8617a60dSAndroid Build Coastguard Worker non_exhaustive: false,
27*8617a60dSAndroid Build Coastguard Worker })
28*8617a60dSAndroid Build Coastguard Worker .layout_tests(false)
29*8617a60dSAndroid Build Coastguard Worker .disable_header_comment()
30*8617a60dSAndroid Build Coastguard Worker }
31*8617a60dSAndroid Build Coastguard Worker
generate_crossystem_bindings() -> Result<()>32*8617a60dSAndroid Build Coastguard Worker fn generate_crossystem_bindings() -> Result<()> {
33*8617a60dSAndroid Build Coastguard Worker let out_dir = env::var("OUT_DIR").unwrap();
34*8617a60dSAndroid Build Coastguard Worker let gen_file = Path::new(&out_dir).join("./crossystem.rs");
35*8617a60dSAndroid Build Coastguard Worker if gen_file.exists() {
36*8617a60dSAndroid Build Coastguard Worker remove_file(&gen_file).expect("Failed to remove generated file.");
37*8617a60dSAndroid Build Coastguard Worker }
38*8617a60dSAndroid Build Coastguard Worker let header_dir = Path::new(".");
39*8617a60dSAndroid Build Coastguard Worker let header_path = header_dir.join("crossystem.h");
40*8617a60dSAndroid Build Coastguard Worker println!("cargo:rerun-if-changed={}", header_path.display());
41*8617a60dSAndroid Build Coastguard Worker
42*8617a60dSAndroid Build Coastguard Worker let bindings = get_bindgen_builder()
43*8617a60dSAndroid Build Coastguard Worker .allowlist_function("Vb.*")
44*8617a60dSAndroid Build Coastguard Worker .clang_args(COMMON_CFLAGS)
45*8617a60dSAndroid Build Coastguard Worker .header(header_path.display().to_string())
46*8617a60dSAndroid Build Coastguard Worker .generate()
47*8617a60dSAndroid Build Coastguard Worker .context("unable to generate bindings for crossystem.h")?;
48*8617a60dSAndroid Build Coastguard Worker
49*8617a60dSAndroid Build Coastguard Worker bindings
50*8617a60dSAndroid Build Coastguard Worker .write_to_file(gen_file.display().to_string())
51*8617a60dSAndroid Build Coastguard Worker .context("unable to write bindings to file")?;
52*8617a60dSAndroid Build Coastguard Worker
53*8617a60dSAndroid Build Coastguard Worker Ok(())
54*8617a60dSAndroid Build Coastguard Worker }
55*8617a60dSAndroid Build Coastguard Worker
generate_vboot_host_binding() -> Result<()>56*8617a60dSAndroid Build Coastguard Worker fn generate_vboot_host_binding() -> Result<()> {
57*8617a60dSAndroid Build Coastguard Worker let out_dir = env::var("OUT_DIR").unwrap();
58*8617a60dSAndroid Build Coastguard Worker let gen_file = Path::new(&out_dir).join("./vboot_host.rs");
59*8617a60dSAndroid Build Coastguard Worker if gen_file.exists() {
60*8617a60dSAndroid Build Coastguard Worker remove_file(&gen_file).expect("Failed to remove generated file.");
61*8617a60dSAndroid Build Coastguard Worker }
62*8617a60dSAndroid Build Coastguard Worker let header_dir = Path::new(".");
63*8617a60dSAndroid Build Coastguard Worker let header_path = header_dir.join("vboot_host.h");
64*8617a60dSAndroid Build Coastguard Worker println!("cargo:rerun-if-changed={}", header_path.display());
65*8617a60dSAndroid Build Coastguard Worker for file in std::fs::read_dir("include")? {
66*8617a60dSAndroid Build Coastguard Worker println!("cargo:rerun-if-changed={}", file?.path().display());
67*8617a60dSAndroid Build Coastguard Worker }
68*8617a60dSAndroid Build Coastguard Worker
69*8617a60dSAndroid Build Coastguard Worker let bindings = get_bindgen_builder()
70*8617a60dSAndroid Build Coastguard Worker .allowlist_function("Cgpt.*")
71*8617a60dSAndroid Build Coastguard Worker .allowlist_function(".*Guid.*")
72*8617a60dSAndroid Build Coastguard Worker .allowlist_function("FindKernelConfig")
73*8617a60dSAndroid Build Coastguard Worker .allowlist_function("ExtractVmlinuz")
74*8617a60dSAndroid Build Coastguard Worker .allowlist_function("vb2_.*")
75*8617a60dSAndroid Build Coastguard Worker .size_t_is_usize(false)
76*8617a60dSAndroid Build Coastguard Worker .clang_args(COMMON_CFLAGS)
77*8617a60dSAndroid Build Coastguard Worker .clang_arg("-Iinclude")
78*8617a60dSAndroid Build Coastguard Worker .header(header_path.display().to_string())
79*8617a60dSAndroid Build Coastguard Worker .generate()
80*8617a60dSAndroid Build Coastguard Worker .context("unable to generate bindings for vboot_host.h")?;
81*8617a60dSAndroid Build Coastguard Worker
82*8617a60dSAndroid Build Coastguard Worker bindings
83*8617a60dSAndroid Build Coastguard Worker .write_to_file(gen_file.display().to_string())
84*8617a60dSAndroid Build Coastguard Worker .context("unable to write bindings to file")?;
85*8617a60dSAndroid Build Coastguard Worker
86*8617a60dSAndroid Build Coastguard Worker Ok(())
87*8617a60dSAndroid Build Coastguard Worker }
88*8617a60dSAndroid Build Coastguard Worker
main() -> Result<()>89*8617a60dSAndroid Build Coastguard Worker fn main() -> Result<()> {
90*8617a60dSAndroid Build Coastguard Worker if pkg_config::Config::new().probe("vboot_host").is_err() {
91*8617a60dSAndroid Build Coastguard Worker // Fallback to generate bindings even if the library is not installed.
92*8617a60dSAndroid Build Coastguard Worker println!("cargo:rustc-link-lib=dylib=vboot_host");
93*8617a60dSAndroid Build Coastguard Worker println!("cargo:rustc-link-lib=dylib=dl");
94*8617a60dSAndroid Build Coastguard Worker }
95*8617a60dSAndroid Build Coastguard Worker generate_crossystem_bindings()?;
96*8617a60dSAndroid Build Coastguard Worker generate_vboot_host_binding()
97*8617a60dSAndroid Build Coastguard Worker }
98