xref: /aosp_15_r20/external/minijail/rust/minijail-sys/build.rs (revision 4b9c6d91573e8b3a96609339b46361b5476dd0f9)
1*4b9c6d91SCole Faust // Copyright 2019 The ChromiumOS Authors
2*4b9c6d91SCole Faust // Use of this source code is governed by a BSD-style license that can be
3*4b9c6d91SCole Faust // found in the LICENSE file.
4*4b9c6d91SCole Faust 
5*4b9c6d91SCole Faust /// Minijail's build script invoked by cargo.
6*4b9c6d91SCole Faust ///
7*4b9c6d91SCole Faust /// This script prefers linking against a pkg-config provided libminijail, but will fall back to
8*4b9c6d91SCole Faust /// building libminijail statically.
9*4b9c6d91SCole Faust use std::env;
10*4b9c6d91SCole Faust use std::fs::remove_file;
11*4b9c6d91SCole Faust use std::io;
12*4b9c6d91SCole Faust use std::path::Path;
13*4b9c6d91SCole Faust use std::process::Command;
14*4b9c6d91SCole Faust 
15*4b9c6d91SCole Faust /// Returns the target triplet prefix for gcc commands. No prefix is required
16*4b9c6d91SCole Faust /// for native builds.
get_cross_compile_prefix() -> String17*4b9c6d91SCole Faust fn get_cross_compile_prefix() -> String {
18*4b9c6d91SCole Faust     if let Ok(cross_compile) = env::var("CROSS_COMPILE") {
19*4b9c6d91SCole Faust         return cross_compile;
20*4b9c6d91SCole Faust     }
21*4b9c6d91SCole Faust 
22*4b9c6d91SCole Faust     let target = env::var("TARGET").unwrap();
23*4b9c6d91SCole Faust 
24*4b9c6d91SCole Faust     if env::var("HOST").unwrap() == target {
25*4b9c6d91SCole Faust         return String::from("");
26*4b9c6d91SCole Faust     }
27*4b9c6d91SCole Faust 
28*4b9c6d91SCole Faust     let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
29*4b9c6d91SCole Faust     let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
30*4b9c6d91SCole Faust     let env = if target.ends_with("-gnueabihf") {
31*4b9c6d91SCole Faust         String::from("gnueabihf")
32*4b9c6d91SCole Faust     } else {
33*4b9c6d91SCole Faust         env::var("CARGO_CFG_TARGET_ENV").unwrap()
34*4b9c6d91SCole Faust     };
35*4b9c6d91SCole Faust     return format!("{}-{}-{}-", arch, os, env);
36*4b9c6d91SCole Faust }
37*4b9c6d91SCole Faust 
set_up_libminijail() -> io::Result<()>38*4b9c6d91SCole Faust fn set_up_libminijail() -> io::Result<()> {
39*4b9c6d91SCole Faust     // Minijail requires libcap at runtime.
40*4b9c6d91SCole Faust     pkg_config::Config::new().probe("libcap").unwrap();
41*4b9c6d91SCole Faust 
42*4b9c6d91SCole Faust     // Prefer a system-provided Minijail library.
43*4b9c6d91SCole Faust     if pkg_config::Config::new().probe("libminijail").is_ok() {
44*4b9c6d91SCole Faust         return Ok(());
45*4b9c6d91SCole Faust     }
46*4b9c6d91SCole Faust 
47*4b9c6d91SCole Faust     let current_dir = env::var("CARGO_MANIFEST_DIR").unwrap() + "/../..";
48*4b9c6d91SCole Faust     let out_dir = env::var("OUT_DIR").unwrap();
49*4b9c6d91SCole Faust     let profile = env::var("PROFILE").unwrap();
50*4b9c6d91SCole Faust 
51*4b9c6d91SCole Faust     let status = Command::new("make")
52*4b9c6d91SCole Faust         .current_dir(&out_dir)
53*4b9c6d91SCole Faust         .env("OUT", &out_dir)
54*4b9c6d91SCole Faust         .env("MODE", if profile == "release" { "opt" } else { "debug" })
55*4b9c6d91SCole Faust         .env("CROSS_COMPILE", get_cross_compile_prefix())
56*4b9c6d91SCole Faust         .env("BUILD_STATIC_LIBS", "yes")
57*4b9c6d91SCole Faust         .arg("-C")
58*4b9c6d91SCole Faust         .arg(&current_dir)
59*4b9c6d91SCole Faust         .status()?;
60*4b9c6d91SCole Faust     if !status.success() {
61*4b9c6d91SCole Faust         std::process::exit(status.code().unwrap_or(1));
62*4b9c6d91SCole Faust     }
63*4b9c6d91SCole Faust     println!("cargo:rustc-link-search=native={}", &out_dir);
64*4b9c6d91SCole Faust     println!("cargo:rustc-link-lib=static=minijail.pic");
65*4b9c6d91SCole Faust     Ok(())
66*4b9c6d91SCole Faust }
67*4b9c6d91SCole Faust 
bindings_generation() -> io::Result<()>68*4b9c6d91SCole Faust fn bindings_generation() -> io::Result<()> {
69*4b9c6d91SCole Faust     let bindgen = match which::which("bindgen") {
70*4b9c6d91SCole Faust         Ok(v) => v,
71*4b9c6d91SCole Faust         // Use already generated copy if bindgen is not present.
72*4b9c6d91SCole Faust         _ => return Ok(()),
73*4b9c6d91SCole Faust     };
74*4b9c6d91SCole Faust 
75*4b9c6d91SCole Faust     // If CROS_RUST is set, skip generation.
76*4b9c6d91SCole Faust     let gen_file = Path::new("./libminijail.rs");
77*4b9c6d91SCole Faust     if gen_file.exists() {
78*4b9c6d91SCole Faust         if env::var("CROS_RUST") == Ok(String::from("1")) {
79*4b9c6d91SCole Faust             return Ok(());
80*4b9c6d91SCole Faust         }
81*4b9c6d91SCole Faust         remove_file(gen_file).expect("Failed to remove generated file.");
82*4b9c6d91SCole Faust     }
83*4b9c6d91SCole Faust     let header_dir = Path::new("../../");
84*4b9c6d91SCole Faust     let header_path = header_dir.join("libminijail.h");
85*4b9c6d91SCole Faust     println!("cargo:rerun-if-changed={}", header_path.display());
86*4b9c6d91SCole Faust     let status = Command::new(&bindgen)
87*4b9c6d91SCole Faust         .args(&["--default-enum-style", "rust"])
88*4b9c6d91SCole Faust         .args(&["--blocklist-type", "__rlim64_t"])
89*4b9c6d91SCole Faust         .args(&["--raw-line", "pub type __rlim64_t = u64;"])
90*4b9c6d91SCole Faust         .args(&["--blocklist-type", "__u\\d{1,2}"])
91*4b9c6d91SCole Faust         .args(&["--raw-line", "pub type __u8 = u8;"])
92*4b9c6d91SCole Faust         .args(&["--raw-line", "pub type __u16 = u16;"])
93*4b9c6d91SCole Faust         .args(&["--raw-line", "pub type __u32 = u32;"])
94*4b9c6d91SCole Faust         .args(&["--blocklist-type", "__uint64_t"])
95*4b9c6d91SCole Faust         .args(&["--allowlist-function", "^minijail_.*"])
96*4b9c6d91SCole Faust         .args(&["--allowlist-var", "^MINIJAIL_.*"])
97*4b9c6d91SCole Faust         .arg("--no-layout-tests")
98*4b9c6d91SCole Faust         .arg("--disable-header-comment")
99*4b9c6d91SCole Faust         .args(&["--output", gen_file.to_str().unwrap()])
100*4b9c6d91SCole Faust         .arg(header_path.to_str().unwrap())
101*4b9c6d91SCole Faust         .args(&[
102*4b9c6d91SCole Faust             "--",
103*4b9c6d91SCole Faust             "-DUSE_BINDGEN",
104*4b9c6d91SCole Faust             "-D_FILE_OFFSET_BITS=64",
105*4b9c6d91SCole Faust             "-D_LARGEFILE_SOURCE",
106*4b9c6d91SCole Faust             "-D_LARGEFILE64_SOURCE",
107*4b9c6d91SCole Faust         ])
108*4b9c6d91SCole Faust         .status()?;
109*4b9c6d91SCole Faust     assert!(status.success());
110*4b9c6d91SCole Faust     Ok(())
111*4b9c6d91SCole Faust }
112*4b9c6d91SCole Faust 
main() -> io::Result<()>113*4b9c6d91SCole Faust fn main() -> io::Result<()> {
114*4b9c6d91SCole Faust     set_up_libminijail()?;
115*4b9c6d91SCole Faust     bindings_generation()
116*4b9c6d91SCole Faust }
117