summary refs log tree commit diff
path: root/tpm2-sys/build.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tpm2-sys/build.rs')
-rw-r--r--tpm2-sys/build.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/tpm2-sys/build.rs b/tpm2-sys/build.rs
new file mode 100644
index 0000000..f1abd4a
--- /dev/null
+++ b/tpm2-sys/build.rs
@@ -0,0 +1,48 @@
+// 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 std::env;
+use std::io;
+use std::path::Path;
+use std::process::{self, Command};
+
+fn main() -> io::Result<()> {
+    println!("cargo:rustc-link-lib=ssl");
+    println!("cargo:rustc-link-lib=crypto");
+
+    if pkg_config::probe_library("libtpm2").is_ok() {
+        // Use tpm2 package from the standard system location if available.
+        return Ok(());
+    }
+
+    // Build with `RUSTFLAGS='--cfg hermetic'` to disallow building our own
+    // libtpm2 in a production build context. Building from the libtpm2
+    // submodule is a convenience only intended for developer environments.
+    if cfg!(hermetic) {
+        eprintln!("libtpm2 not found; unable to perform hermetic build");
+        process::exit(1);
+    }
+
+    if !Path::new("libtpm2/.git").exists() {
+        Command::new("git")
+            .args(&["submodule", "update", "--init"])
+            .status()?;
+    }
+
+    if !Path::new("libtpm2/build/libtpm2.a").exists() {
+        let ncpu = num_cpus::get();
+        let status = Command::new("make")
+            .arg(format!("-j{}", ncpu))
+            .current_dir("libtpm2")
+            .status()?;
+        if !status.success() {
+            process::exit(status.code().unwrap_or(1));
+        }
+    }
+
+    let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
+    println!("cargo:rustc-link-search={}/libtpm2/build", dir);
+    println!("cargo:rustc-link-lib=static=tpm2");
+    Ok(())
+}