2022-09-13 17:55:17 +00:00
|
|
|
// Copyright 2019 The ChromiumOS Authors
|
2019-01-04 19:50:58 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
2021-08-31 17:12:06 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
2019-01-04 19:50:58 +00:00
|
|
|
|
2022-07-27 18:11:32 +00:00
|
|
|
use anyhow::bail;
|
|
|
|
use anyhow::Result;
|
|
|
|
|
2021-08-31 17:12:06 +00:00
|
|
|
/// Returns the target triplet prefix for gcc commands. No prefix is required
|
|
|
|
/// for native builds.
|
|
|
|
fn get_cross_compile_prefix() -> String {
|
2021-12-09 22:16:59 +00:00
|
|
|
let target = env::var("TARGET").unwrap();
|
|
|
|
|
|
|
|
if env::var("HOST").unwrap() == target {
|
2021-08-31 17:12:06 +00:00
|
|
|
return String::from("");
|
2019-01-04 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 17:12:06 +00:00
|
|
|
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
|
|
|
|
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
|
2021-12-09 22:16:59 +00:00
|
|
|
let env = if target.ends_with("-gnueabihf") {
|
|
|
|
String::from("gnueabihf")
|
|
|
|
} else {
|
|
|
|
env::var("CARGO_CFG_TARGET_ENV").unwrap()
|
|
|
|
};
|
2022-11-19 01:01:21 +00:00
|
|
|
format!("{}-{}-{}-", arch, os, env)
|
2021-08-31 17:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_libtpm2(out_dir: &Path) -> Result<()> {
|
|
|
|
let lib_path = out_dir.join("libtpm2.a");
|
|
|
|
if lib_path.exists() {
|
|
|
|
return Ok(());
|
2019-01-04 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !Path::new("libtpm2/.git").exists() {
|
2021-08-31 17:12:06 +00:00
|
|
|
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))
|
2022-07-27 23:35:00 +00:00
|
|
|
.arg("CFLAGS=-Wno-error")
|
2021-08-31 17:12:06 +00:00
|
|
|
.arg(format!("obj={}", out_dir.display()))
|
|
|
|
.current_dir("libtpm2")
|
|
|
|
.status()?;
|
|
|
|
if !status.success() {
|
|
|
|
bail!("make failed with status: {}", status);
|
2019-01-04 19:50:58 +00:00
|
|
|
}
|
2021-08-31 17:12:06 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-04 19:50:58 +00:00
|
|
|
|
2021-08-31 17:12:06 +00:00
|
|
|
fn main() -> Result<()> {
|
2022-04-08 10:51:32 +00:00
|
|
|
// Skip installing dependencies when generating documents.
|
|
|
|
if std::env::var("CARGO_DOC").is_ok() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:24:29 +00:00
|
|
|
// libtpm2 is unix only
|
|
|
|
if std::env::var("CARGO_CFG_UNIX").is_err() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:12:06 +00:00
|
|
|
// Use tpm2 package from the standard system location if available.
|
|
|
|
if pkg_config::Config::new()
|
|
|
|
.statik(true)
|
|
|
|
.probe("libtpm2")
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
return Ok(());
|
2019-01-04 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 17:12:06 +00:00
|
|
|
// 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());
|
2019-01-04 19:50:58 +00:00
|
|
|
println!("cargo:rustc-link-lib=static=tpm2");
|
2018-12-20 19:49:46 +00:00
|
|
|
println!("cargo:rustc-link-lib=ssl");
|
|
|
|
println!("cargo:rustc-link-lib=crypto");
|
2019-01-04 19:50:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|