minijail-sys: Enable cross-compilation in build.rs
This adds the CROSS_COMPILE variable to the make call to pass the
correct prefix for the target platform.
This is used for cross-compiling and testing crosvm on aarch64.
(Note: To cross-compile on gLinux, we will need b/198310108)
BUG=b:198305518
TEST=cargo build --target=aarch64-unknown-linux-gnu
Change-Id: Iedc11531a3fc3c304fb11b2ba13f0a55f58396ba
diff --git a/rust/minijail-sys/build.rs b/rust/minijail-sys/build.rs
index f5c895e..ea0ed5c 100644
--- a/rust/minijail-sys/build.rs
+++ b/rust/minijail-sys/build.rs
@@ -12,6 +12,23 @@
use std::path::Path;
use std::process::Command;
+/// Returns the target triplet prefix for gcc commands. No prefix is required
+/// for native builds.
+fn get_cross_compile_prefix() -> String {
+ if let Ok(cross_compile) = env::var("CROSS_COMPILE") {
+ return cross_compile;
+ }
+
+ if env::var("HOST").unwrap() == env::var("TARGET").unwrap() {
+ return String::from("");
+ }
+
+ let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
+ let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
+ let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
+ return format!("{}-{}-{}-", arch, os, env);
+}
+
fn set_up_libminijail() -> io::Result<()> {
// Minijail requires libcap at runtime.
pkg_config::Config::new().probe("libcap").unwrap();
@@ -29,6 +46,7 @@
.current_dir(&out_dir)
.env("OUT", &out_dir)
.env("MODE", if profile == "release" { "opt" } else { "debug" })
+ .env("CROSS_COMPILE", get_cross_compile_prefix())
.arg("-C")
.arg(¤t_dir)
.arg("CC_STATIC_LIBRARY(libminijail.pic.a)")