blob: 604d936cfa58761d251489b68369a97eb0431ee8 [file] [log] [blame]
David Tolnayc49ef3e2019-01-04 11:50:58 -08001// 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
5use std::env;
6use std::io;
7use std::path::Path;
8use std::process::{self, Command};
9
10fn main() -> io::Result<()> {
David Tolnayde6b29a2018-12-20 11:49:46 -080011 if pkg_config::Config::new()
12 .statik(true)
13 .probe("libtpm2")
14 .is_ok()
15 {
David Tolnayc49ef3e2019-01-04 11:50:58 -080016 // 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");
David Tolnayde6b29a2018-12-20 11:49:46 -080048 println!("cargo:rustc-link-lib=ssl");
49 println!("cargo:rustc-link-lib=crypto");
David Tolnayc49ef3e2019-01-04 11:50:58 -080050 Ok(())
51}