diff --git a/mbedtls-sys/build/bindgen.rs b/mbedtls-sys/build/bindgen.rs index 8e28d0a6..49379493 100644 --- a/mbedtls-sys/build/bindgen.rs +++ b/mbedtls-sys/build/bindgen.rs @@ -11,6 +11,7 @@ use bindgen; use std::fmt::Write as _; use std::fs::{self, File}; use std::io::Write; +use std::env; use crate::headers; @@ -88,6 +89,7 @@ impl super::BuildConfig { } let mut cc = cc::Build::new(); + cc.include(&self.mbedtls_include) .flag(&format!( "-DMBEDTLS_CONFIG_FILE=\"{}\"", @@ -116,6 +118,15 @@ impl super::BuildConfig { }; } + // Bindgen utilizes libclang and the current `TARGET` to parse the C files. + // When the `TARGET` is custom, we need to override it so that bindgen + // finds the right stdlib headers. + // See https://docs.rust-embedded.org/embedonomicon/custom-target.html + // for more details. + if let Some(target) = env::var_os("RUST_MBEDTLS_BINDGEN_TARGET") { + env::set_var("TARGET", target); + } + let bindings = bindgen::builder() .clang_args(cc.get_compiler().args().iter().map(|arg| arg.to_str().unwrap())) .header_contents("bindgen-input.h", &input) diff --git a/mbedtls/src/lib.rs b/mbedtls/src/lib.rs index a98f6f40..ab53b32a 100644 --- a/mbedtls/src/lib.rs +++ b/mbedtls/src/lib.rs @@ -21,7 +21,7 @@ extern crate bitflags; #[macro_use] extern crate serde_derive; // required explicitly to force inclusion at link time -#[cfg(target_env = "sgx")] +#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] extern crate rs_libc; #[macro_use]