code-intelligence.com/cifuzz@v0.40.0/third-party/minijail/rust/minijail-sys/build.rs (about)

     1  // Copyright 2019 The Chromium OS Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  /// Minijail's build script invoked by cargo.
     6  ///
     7  /// This script prefers linking against a pkg-config provided libminijail, but will fall back to
     8  /// building libminijail statically.
     9  use std::env;
    10  use std::fs::remove_file;
    11  use std::io;
    12  use std::path::Path;
    13  use std::process::Command;
    14  
    15  fn set_up_libminijail() -> io::Result<()> {
    16      // Minijail requires libcap at runtime.
    17      pkg_config::Config::new().probe("libcap").unwrap();
    18  
    19      // Prefer a system-provided Minijail library.
    20      if pkg_config::Config::new().probe("libminijail").is_ok() {
    21          return Ok(());
    22      }
    23  
    24      let current_dir = env::var("CARGO_MANIFEST_DIR").unwrap() + "/../..";
    25      let out_dir = env::var("OUT_DIR").unwrap();
    26      let profile = env::var("PROFILE").unwrap();
    27  
    28      let status = Command::new("make")
    29          .current_dir(&out_dir)
    30          .env("OUT", &out_dir)
    31          .env("MODE", if profile == "release" { "opt" } else { "debug" })
    32          .arg("-C")
    33          .arg(&current_dir)
    34          .arg("CC_STATIC_LIBRARY(libminijail.pic.a)")
    35          .status()?;
    36      if !status.success() {
    37          std::process::exit(status.code().unwrap_or(1));
    38      }
    39      println!("cargo:rustc-link-search=native={}", &out_dir);
    40      println!("cargo:rustc-link-lib=static=minijail.pic");
    41      Ok(())
    42  }
    43  
    44  fn bindings_generation() -> io::Result<()> {
    45      let bindgen = match which::which("bindgen") {
    46          Ok(v) => v,
    47          // Use already generated copy if bindgen is not present.
    48          _ => return Ok(()),
    49      };
    50  
    51      // If CROS_RUST is set, skip generation.
    52      let gen_file = Path::new("./libminijail.rs");
    53      if gen_file.exists() {
    54          if env::var("CROS_RUST") == Ok(String::from("1")) {
    55              return Ok(());
    56          }
    57          remove_file(gen_file).expect("Failed to remove generated file.");
    58      }
    59      let header_dir = Path::new("../../");
    60      let header_path = header_dir.join("libminijail.h");
    61      println!("cargo:rerun-if-changed={}", header_path.display());
    62      let status = Command::new(&bindgen)
    63          .args(&["--default-enum-style", "rust"])
    64          .args(&["--blacklist-type", "__rlim64_t"])
    65          .args(&["--raw-line", "pub type __rlim64_t = u64;"])
    66          .args(&["--blacklist-type", "__u\\d{1,2}"])
    67          .args(&["--raw-line", "pub type __u8 = u8;"])
    68          .args(&["--raw-line", "pub type __u16 = u16;"])
    69          .args(&["--raw-line", "pub type __u32 = u32;"])
    70          .args(&["--blacklist-type", "__uint64_t"])
    71          .args(&["--whitelist-function", "^minijail_.*"])
    72          .args(&["--whitelist-var", "^MINIJAIL_.*"])
    73          .arg("--no-layout-tests")
    74          .args(&["--output", gen_file.to_str().unwrap()])
    75          .arg(header_path.to_str().unwrap())
    76          .args(&[
    77              "--",
    78              "-DUSE_BINDGEN",
    79              "-D_FILE_OFFSET_BITS=64",
    80              "-D_LARGEFILE_SOURCE",
    81              "-D_LARGEFILE64_SOURCE",
    82          ])
    83          .status()?;
    84      assert!(status.success());
    85      Ok(())
    86  }
    87  
    88  fn main() -> io::Result<()> {
    89      set_up_libminijail()?;
    90      bindings_generation()
    91  }