use std::env;
use std::path::PathBuf;
fn main() {
let wolfssl_include = env::var("DEP_WOLFCRYPT_SYS_INCLUDE")
.expect("DEP_WOLFCRYPT_SYS_INCLUDE not set; is wolfcrypt-sys a direct dependency?");
let wolfssl_settings = env::var("DEP_WOLFCRYPT_SYS_SETTINGS_INCLUDE")
.unwrap_or_else(|_| wolfssl_include.clone());
let wolfssl_vendored = env::var("DEP_WOLFCRYPT_SYS_VENDORED")
.map(|v| v == "1")
.unwrap_or(false);
let wolfhsm_include = env::var("DEP_WOLFHSM_SRC_INCLUDE")
.expect("DEP_WOLFHSM_SRC_INCLUDE not set; is wolfhsm-src a build-dependency?");
let wolfhsm_lib = env::var("DEP_WOLFHSM_SRC_LIB")
.expect("DEP_WOLFHSM_SRC_LIB not set");
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
cc::Build::new()
.file(manifest_dir.join("src").join("shims.c"))
.include(&wolfhsm_lib)
.include(&wolfhsm_include)
.include(format!("{wolfhsm_include}/port/posix"))
.include(&wolfssl_include)
.include(&wolfssl_settings)
.define(
if wolfssl_vendored {
"WOLFSSL_USER_SETTINGS"
} else {
"WOLFSSL_USE_OPTIONS_H"
},
None,
)
.define("WOLFHSM_CFG", None)
.define("WOLF_CRYPTO_CB", None)
.define("WOLFHSM_CFG_NO_WOLFCRYPT", Some("0"))
.warnings(false)
.opt_level(2)
.compile("wolfhsm_shims");
println!("cargo:rustc-link-search=native={wolfhsm_lib}");
println!("cargo:rustc-link-lib=static=wolfhsm");
let wolfssl_vendored_str = env::var("DEP_WOLFCRYPT_SYS_VENDORED").unwrap_or_default();
if wolfssl_vendored_str == "1" {
let wolfssl_root = env::var("DEP_WOLFCRYPT_SYS_ROOT").unwrap_or_default();
if !wolfssl_root.is_empty() {
println!("cargo:rustc-link-search=native={wolfssl_root}");
}
println!("cargo:rustc-link-lib=static=wolfssl");
} else {
let lib_dirs = env::var("DEP_WOLFCRYPT_SYS_LIB_DIRS").unwrap_or_default();
for dir in lib_dirs.split(':').filter(|s| !s.is_empty()) {
println!("cargo:rustc-link-search=native={dir}");
}
let libcrypto =
env::var("DEP_WOLFCRYPT_SYS_LIBCRYPTO").unwrap_or_else(|_| "wolfssl".into());
println!("cargo:rustc-link-lib={libcrypto}");
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings = bindgen::Builder::default()
.header(manifest_dir.join("wrapper.h").to_str().unwrap())
.clang_arg(format!("-I{wolfhsm_lib}"))
.clang_arg(format!("-I{wolfhsm_include}"))
.clang_arg(format!("-I{}/port/posix", wolfhsm_include))
.clang_arg(format!("-I{wolfssl_include}"))
.clang_arg(format!("-I{wolfssl_settings}"))
.clang_arg(if wolfssl_vendored {
"-DWOLFSSL_USER_SETTINGS"
} else {
"-DWOLFSSL_USE_OPTIONS_H"
})
.clang_arg("-DWOLFHSM_CFG")
.clang_arg("-DWOLF_CRYPTO_CB")
.clang_arg("-DWOLFHSM_CFG_NO_WOLFCRYPT=0")
.allowlist_type("wh.*")
.allowlist_type("WH.*")
.allowlist_type("posix_transport_.*")
.allowlist_type("PosixTransport.*")
.allowlist_type("posixTransport.*")
.allowlist_type("ptt.*")
.allowlist_type("ptu.*")
.allowlist_type("ptshm.*")
.allowlist_type("PTSHM.*")
.allowlist_function("wh_Client_.*")
.allowlist_function("wh_Comm_.*")
.allowlist_function("posixTransportTcp_.*")
.allowlist_function("posixTransportUds_.*")
.allowlist_function("posixTransportShm_.*")
.allowlist_item("WH_.*")
.allowlist_item("WOLFHSM_.*")
.use_core()
.layout_tests(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("failed to generate wolfhsm bindings");
bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("failed to write bindings.rs");
}