1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::env;
6 use std::io;
7 use std::path::Path;
8 use std::process::{self, Command};
9 
main() -> io::Result<()>10 fn main() -> io::Result<()> {
11     if pkg_config::Config::new()
12         .statik(true)
13         .probe("libtpm2")
14         .is_ok()
15     {
16         // Use tpm2 package from the standard system location if available.
17         return Ok(());
18     }
19 
20     // Build with `RUSTFLAGS='--cfg hermetic'` to disallow building our own
21     // libtpm2 in a production build context. Building from the libtpm2
22     // submodule is a convenience only intended for developer environments.
23     if cfg!(hermetic) {
24         eprintln!("libtpm2 not found; unable to perform hermetic build");
25         process::exit(1);
26     }
27 
28     if !Path::new("libtpm2/.git").exists() {
29         Command::new("git")
30             .args(&["submodule", "update", "--init"])
31             .status()?;
32     }
33 
34     if !Path::new("libtpm2/build/libtpm2.a").exists() {
35         let ncpu = num_cpus::get();
36         let status = Command::new("make")
37             .arg(format!("-j{}", ncpu))
38             .current_dir("libtpm2")
39             .status()?;
40         if !status.success() {
41             process::exit(status.code().unwrap_or(1));
42         }
43     }
44 
45     let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
46     println!("cargo:rustc-link-search={}/libtpm2/build", dir);
47     println!("cargo:rustc-link-lib=static=tpm2");
48     println!("cargo:rustc-link-lib=ssl");
49     println!("cargo:rustc-link-lib=crypto");
50     Ok(())
51 }
52