crosvm/tpm2-sys/build.rs

92 lines
2.5 KiB
Rust
Raw Normal View History

// Copyright 2019 The ChromiumOS Authors
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
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;
use std::path::PathBuf;
use std::process::Command;
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
use anyhow::bail;
use anyhow::Result;
/// Returns the target triplet prefix for gcc commands. No prefix is required
/// for native builds.
fn get_cross_compile_prefix() -> String {
let target = env::var("TARGET").unwrap();
if env::var("HOST").unwrap() == target {
return String::from("");
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
}
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let env = if target.ends_with("-gnueabihf") {
String::from("gnueabihf")
} else {
env::var("CARGO_CFG_TARGET_ENV").unwrap()
};
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(());
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
}
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("CFLAGS=-Wno-error")
.arg(format!("obj={}", out_dir.display()))
.current_dir("libtpm2")
.status()?;
if !status.success() {
bail!("make failed with status: {}", status);
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
}
Ok(())
}
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
fn main() -> Result<()> {
Reland "github: Minimize dependencies for document generation" This is a reland of commit 2b85d4d1e521ef66f165bca21741b52607acdb22 Diff from the original CL: * Changed the return value of `main()` in power_monitor/build.rs * Ran cargo-check with all features enabled Original change's description: > github: Minimize dependencies for document generation > > Instead of install full dependencies with `install-deps` in GitHub > action, minimize dependencies because we use GitHub only for document > generation. > > - Passed `CARGO_DOC` environment when running cargo-doc command so we > can skip unnecessary build flow when we just want to generate API docs. > - Added a new script `install-docs-deps` to install only doc-related > dependencies. > > BUG=none > TEST=test on GitHub Action at my personal repository > > Change-Id: Ibe988ab43215e285d946812bdd6c1536ae87b50e > Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3578144 > Tested-by: kokoro <noreply+kokoro@google.com> > Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> > Reviewed-by: Anton Romanov <romanton@google.com> > Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org> Bug=none TEST=cargo check --all-features Change-Id: I77ee6543910e3fe7f69be48f8f965eda3433d4e6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3584063 Auto-Submit: Keiichi Watanabe <keiichiw@chromium.org> Reviewed-by: Dennis Kempin <denniskempin@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
2022-04-08 10:51:32 +00:00
// Skip installing dependencies when generating documents.
if std::env::var("CARGO_DOC").is_ok() {
return Ok(());
}
// libtpm2 is unix only
if std::env::var("CARGO_CFG_UNIX").is_err() {
return Ok(());
}
// Use tpm2 package from the standard system location if available.
if pkg_config::Config::new()
.statik(true)
.probe("libtpm2")
.is_ok()
{
return Ok(());
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +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());
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
println!("cargo:rustc-link-lib=static=tpm2");
println!("cargo:rustc-link-lib=ssl");
println!("cargo:rustc-link-lib=crypto");
tpm: Add tpm2-sys crate This CL adds a tpm2-sys crate that builds libtpm2 from source (from a git submodule) using the existing Makefile and then links the generated static library as -ltpm2. For production builds there is a flag `RUSTFLAGS='--cfg hermetic'` to disallow building our own libtpm2. Instead it will expect to find libtpm2 installed in the standard system location. Building from the libtpm2 submodule is a convenience only intended for developer environments. The functions exposed by tpm2-sys are the ones that will be necessary to initialize a TPM simulator in crosvm and execute TPM commands. Trunks uses the same functions for its simulator mode here: https://chromium.googlesource.com/chromiumos/platform2/+/e4cf13c05773f3446bd76a13c4e37f0b80728711/trunks/tpm_simulator_handle.cc Tested by running: fn main() { unsafe { tpm2_sys::TPM_Manufacture(1); } } inside cros_sdk. Libtpm2 cannot be built outside of cros_sdk because it requires openssl 1.0.2p, whereas dev machines come with openssl 1.1.0j. I have not yet added any dependency on tpm2-sys from crosvm, but when it does get added it will be behind a tpm feature flag so that crosvm can continue to build outside of cros_sdk just without tpm support. I published num_cpus version 1.9.0 to chromeos-localmirror. TEST=running the code snippet above as described BUG=chromium:911799 Change-Id: I097729bc447f9dc95e39959a426d1ac42f46b16d Reviewed-on: https://chromium-review.googlesource.com/1396280 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
2019-01-04 19:50:58 +00:00
Ok(())
}