extern crate bindgen;
use autotools::Config;
use std::collections::HashSet;
use std::env;
use std::path::PathBuf;
use std::process::Command;
static WOLFSSL_VERSION: &str = "wolfssl-5.3.0-stable";
#[derive(Debug)]
struct IgnoreMacros(HashSet<String>);
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}
fn extract_wolfssl(dest: &str) -> std::io::Result<()> {
Command::new("tar")
.arg("-zxvf")
.arg(format!("vendor/{}.tar.gz", WOLFSSL_VERSION))
.arg("-C")
.arg(dest)
.status()
.unwrap();
Ok(())
}
fn build_wolfssl(dest: &str) -> PathBuf {
Config::new(format!("{}/{}", dest, WOLFSSL_VERSION))
.reconf("-ivf")
.enable_static()
.disable_shared()
.enable("tls13", None)
.disable("oldtls", None)
.enable("aesni", None)
.enable("singlethreaded", None)
.enable("dtls", None)
.enable("sp", None)
.enable("sp-asm", None)
.enable("dtls-mtu", None)
.disable("sha3", None)
.enable("intelasm", None)
.disable("dh", None)
.enable("curve25519", None)
.enable("secure-renegotiation", None)
.cflag("-g")
.cflag("-fPIC")
.cflag("-DWOLFSSL_DTLS_ALLOW_FUTURE")
.cflag("-DWOLFSSL_MIN_RSA_BITS=2048")
.cflag("-DWOLFSSL_MIN_ECC_BITS=256")
.build()
}
fn main() -> std::io::Result<()> {
let dst_string = env::var("OUT_DIR").unwrap();
extract_wolfssl(&dst_string)?;
let dst = build_wolfssl(&dst_string);
let ignored_macros = IgnoreMacros(vec!["IPPORT_RESERVED".into()].into_iter().collect());
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}/include/", dst_string))
.parse_callbacks(Box::new(ignored_macros))
.rustfmt_bindings(true)
.blocklist_file("/usr/include/stdlib.h")
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(dst.join("bindings.rs"))
.expect("Couldn't write bindings!");
println!("cargo:rustc-link-lib=static=wolfssl");
println!(
"cargo:rustc-link-search=native={}",
format!("{}/lib/", dst_string)
);
println!("cargo:include={}", dst_string);
println!("cargo:rerun-if-changed=wrapper.h");
Ok(())
}