crosvm/tpm2-sys/build.rs
Dennis Kempin 00bfcab3e8 Update tpm2-sys build.rs to support cross-compilation
Also removes the use of the hermetic flag in favor of not failing the
build if the submodule is not checked out.
This allows us to remove the tpm2 build from the build_environment
Makefile.

BUG=b:198293072
TEST=./test_all

Change-Id: Ide81e78efe0da3a1b64d4b8ef094a2e901f99ccf
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3133623
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2021-09-01 01:59:09 +00:00

72 lines
2.1 KiB
Rust

// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use anyhow::{bail, Result};
use std::env;
use std::path::Path;
use std::path::PathBuf;
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 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 build_libtpm2(out_dir: &Path) -> Result<()> {
let lib_path = out_dir.join("libtpm2.a");
if lib_path.exists() {
return Ok(());
}
if !Path::new("libtpm2/.git").exists() {
bail!(
"tpm2-sys/libtpm2 source does not exist, did you forget to \
`git submodule update --init`?"
);
}
let make_flags = env::var("CARGO_MAKEFLAGS").unwrap();
let prefix = get_cross_compile_prefix();
let status = Command::new("make")
.env("MAKEFLAGS", make_flags)
.arg(format!("AR={}ar", prefix))
.arg(format!("CC={}gcc", prefix))
.arg(format!("OBJCOPY={}objcopy", prefix))
.arg(format!("obj={}", out_dir.display()))
.current_dir("libtpm2")
.status()?;
if !status.success() {
bail!("make failed with status: {}", status);
}
Ok(())
}
fn main() -> Result<()> {
// Use tpm2 package from the standard system location if available.
if pkg_config::Config::new()
.statik(true)
.probe("libtpm2")
.is_ok()
{
return Ok(());
}
// Otherwise build from source
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
build_libtpm2(&out_dir)?;
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rustc-link-lib=static=tpm2");
println!("cargo:rustc-link-lib=ssl");
println!("cargo:rustc-link-lib=crypto");
Ok(())
}