-
-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for building boringssl with bindgen #1831
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
#[cfg(feature = "bindgen")] | ||
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks}; | ||
use bindgen::RustTarget; | ||
use std::env; | ||
#[cfg(feature = "bindgen")] | ||
use bindgen::{MacroTypeVariation, RustTarget}; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
#[cfg(not(feature = "bindgen"))] | ||
use std::process; | ||
use std::{env, fs}; | ||
|
||
const INCLUDES: &str = " | ||
#include <openssl/aes.h> | ||
#include <openssl/asn1.h> | ||
#include <openssl/bio.h> | ||
#include <openssl/comp.h> | ||
#include <openssl/conf.h> | ||
#include <openssl/crypto.h> | ||
#include <openssl/dh.h> | ||
|
@@ -17,7 +21,6 @@ const INCLUDES: &str = " | |
#include <openssl/evp.h> | ||
#include <openssl/hmac.h> | ||
#include <openssl/objects.h> | ||
#include <openssl/ocsp.h> | ||
#include <openssl/opensslv.h> | ||
#include <openssl/pem.h> | ||
#include <openssl/pkcs12.h> | ||
|
@@ -35,10 +38,15 @@ const INCLUDES: &str = " | |
// this must be included after ssl.h for libressl! | ||
#include <openssl/srtp.h> | ||
|
||
#if !defined(LIBRESSL_VERSION_NUMBER) | ||
#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) | ||
#include <openssl/cms.h> | ||
#endif | ||
|
||
#if !defined(OPENSSL_IS_BORINGSSL) | ||
#include <openssl/comp.h> | ||
#include <openssl/ocsp.h> | ||
#endif | ||
|
||
#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000 | ||
#include <openssl/kdf.h> | ||
#endif | ||
|
@@ -48,6 +56,7 @@ const INCLUDES: &str = " | |
#endif | ||
"; | ||
|
||
#[cfg(feature = "bindgen")] | ||
pub fn run(include_dirs: &[PathBuf]) { | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
|
||
|
@@ -94,9 +103,107 @@ pub fn run(include_dirs: &[PathBuf]) { | |
.unwrap(); | ||
} | ||
|
||
#[cfg(feature = "bindgen")] | ||
pub fn run_boringssl(include_dirs: &[PathBuf]) { | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
let mut builder = bindgen::builder() | ||
.rust_target(RustTarget::Stable_1_47) | ||
.ctypes_prefix("::libc") | ||
.derive_default(false) | ||
.enable_function_attribute_detection() | ||
.size_t_is_usize(true) | ||
.default_macro_constant_type(MacroTypeVariation::Signed) | ||
.rustified_enum("point_conversion_form_t") | ||
.allowlist_file(".*/openssl/[^/]+\\.h") | ||
.wrap_static_fns(true) | ||
.wrap_static_fns_path(out_dir.join("boring_static_wrapper").display().to_string()) | ||
.layout_tests(false) | ||
.header_contents("includes.h", INCLUDES); | ||
|
||
for include_dir in include_dirs { | ||
builder = builder | ||
.clang_arg("-I") | ||
.clang_arg(include_dir.display().to_string()); | ||
} | ||
|
||
builder | ||
.generate() | ||
.unwrap() | ||
.write_to_file(out_dir.join("bindgen.rs")) | ||
.unwrap(); | ||
|
||
fs::File::create(out_dir.join("boring_static_wrapper.h")) | ||
.expect("Failed to create boring_static_wrapper.h") | ||
.write_all(INCLUDES.as_bytes()) | ||
.expect("Failed to write contents to boring_static_wrapper.h"); | ||
|
||
cc::Build::new() | ||
.file(out_dir.join("boring_static_wrapper.c")) | ||
.includes(include_dirs) | ||
.flag("-include") | ||
.flag( | ||
&out_dir | ||
.join("boring_static_wrapper.h") | ||
.display() | ||
.to_string(), | ||
) | ||
.compile("boring_static_wrapper"); | ||
} | ||
|
||
#[cfg(not(feature = "bindgen"))] | ||
pub fn run_boringssl(include_dirs: &[PathBuf]) { | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
|
||
fs::File::create(out_dir.join("boring_static_wrapper.h")) | ||
.expect("Failed to create boring_static_wrapper.h") | ||
.write_all(INCLUDES.as_bytes()) | ||
.expect("Failed to write contents to boring_static_wrapper.h"); | ||
|
||
let mut bindgen_cmd = process::Command::new("bindgen"); | ||
bindgen_cmd | ||
.arg("-o") | ||
.arg(out_dir.join("bindgen.rs")) | ||
.arg("--rust-target=1.47") | ||
.arg("--ctypes-prefix=::libc") | ||
.arg("--no-derive-default") | ||
.arg("--enable-function-attribute-detection") | ||
.arg("--size_t-is-usize") | ||
.arg("--default-macro-constant-type=signed") | ||
.arg("--rustified-enum=point_conversion_form_t") | ||
.arg("--allowlist-file=.*/openssl/[^/]+\\.h") | ||
.arg("--experimental") | ||
.arg("--wrap-static-fns") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on, it doesn't look like this flag works correctly in bindgen yet, so from BoringSSL's perspective, this bindings strategy is unsupported. I'd love to see a version of it that works, but bindgen isn't there yet. |
||
.arg("--wrap-static-fns-path") | ||
.arg(out_dir.join("boring_static_wrapper").display().to_string()) | ||
.arg("--no-layout-tests") | ||
.arg(out_dir.join("boring_static_wrapper.h")) | ||
.arg("--") | ||
.arg(format!("--target={}", env::var("TARGET").unwrap())); | ||
|
||
for include_dir in include_dirs { | ||
bindgen_cmd.arg("-I").arg(include_dir.display().to_string()); | ||
} | ||
|
||
let result = bindgen_cmd.status().expect("bindgen failed to execute"); | ||
assert!(result.success()); | ||
|
||
cc::Build::new() | ||
.file(out_dir.join("boring_static_wrapper.c")) | ||
.includes(include_dirs) | ||
.flag("-include") | ||
.flag( | ||
&out_dir | ||
.join("boring_static_wrapper.h") | ||
.display() | ||
.to_string(), | ||
) | ||
.compile("boring_static_wrapper"); | ||
} | ||
|
||
#[derive(Debug)] | ||
struct OpensslCallbacks; | ||
|
||
#[cfg(feature = "bindgen")] | ||
impl ParseCallbacks for OpensslCallbacks { | ||
// for now we'll continue hand-writing constants | ||
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems a bit unfortunate to be duplicating all of the bindgen flags here and above. If we end up having to tweak them in the future we should probably refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, and yes, makes sense.