bearssl-sys: break-up `build.rs`
The `build.rs` is destined to get more cluttered as the autodetection gets more optimial and Windows/non-amd64 arches are handled better.main
parent
70495f7fdc
commit
ebd9663752
|
@ -278,9 +278,9 @@ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.43"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
|
||||
checksum = "7bd7356a8122b6c4a24a82b278680c73357984ca2fc79a0f9fa6dea7dced7c58"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
|
|
@ -5,6 +5,7 @@ version = "0.0.5-pre.0"
|
|||
authors = ["Aydin Mercan <aydin@mercan.dev>"]
|
||||
license = "BSD-3-Clause"
|
||||
readme = "README.md"
|
||||
build = "build/main.rs"
|
||||
categories = ["cryptography", "no-std", "external-ffi-bindings"]
|
||||
keywords = ["bearssl", "crypto", "tls", "ssl"]
|
||||
edition = "2021"
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(not(feature = "bundled"))]
|
||||
static HEADER_PATH: &str = "wrapper.h";
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
static HEADER_PATH: &str = "bundled/inc/bearssl.h";
|
||||
|
||||
#[cfg(feature = "dont-assume-size_t-equals-uintptr_t")]
|
||||
static CTYPES_PREFIX: &str = "::libc";
|
||||
|
||||
#[cfg(not(feature = "dont-assume-size_t-equals-uintptr_t"))]
|
||||
static CTYPES_PREFIX: &str = "::core::ffi";
|
||||
|
||||
#[cfg(not(feature = "bundled"))]
|
||||
fn bearssl_handle_linkage() {
|
||||
#[cfg(not(unix))]
|
||||
panic!("linking dynamically is currently supported in only unix targets");
|
||||
|
||||
println!("cargo:rustc-link-search=/lib");
|
||||
println!("cargo:rustc-link-search=/usr/lib");
|
||||
println!("cargo:rustc-link-search=/usr/local/lib");
|
||||
println!("cargo:rustc-link-lib=dylib=bearssl");
|
||||
}
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
fn build_configure_arch_specific(build: &mut cc::Build) {
|
||||
let compiler = build.get_compiler();
|
||||
|
||||
build.define("BR_64", "1");
|
||||
|
||||
if compiler.is_like_gnu() || compiler.is_like_clang() {
|
||||
build.define("BR_INT128", "1");
|
||||
} else if compiler.is_like_msvc() {
|
||||
build.define("BR_UMUL128", "1");
|
||||
}
|
||||
|
||||
#[cfg(target_feature = "sse2")]
|
||||
build.define("BR_SSE2", "1");
|
||||
|
||||
#[cfg(target_feature = "aes")]
|
||||
build.define("BR_AES_X86NI", "1");
|
||||
}
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
#[cfg(not(target_arch = "x86_64"))]
|
||||
fn build_configure_arch_specific(build: &mut cc::Build) {
|
||||
#[cfg(target_pointer_width = 64)]
|
||||
build.define("BR_64", "1");
|
||||
}
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
#[cfg(unix)]
|
||||
fn build_configure_os_specific(build: &mut cc::Build) {
|
||||
build.define("BR_USE_UNIX_TIME", "1").define("BR_USE_URANDOM", "1");
|
||||
}
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
#[cfg(windows)]
|
||||
fn build_configure_os_specific(build: &mut cc::Build) {
|
||||
build.define("BR_USE_WIN32_RAND", "1").define("BR_USE_WIN32_TIME", "1");
|
||||
}
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
fn bearssl_handle_linkage() {
|
||||
let mut build = cc::Build::new();
|
||||
|
||||
build_configure_os_specific(&mut build);
|
||||
build_configure_arch_specific(&mut build);
|
||||
|
||||
build
|
||||
.include("bundled/inc")
|
||||
.files(
|
||||
std::fs::read_dir("bundled/src")
|
||||
.expect("failed to get bundled source path")
|
||||
.map(|e| e.expect("failed to read get file entry").path()),
|
||||
)
|
||||
.static_flag(true)
|
||||
.compile("bearssl");
|
||||
|
||||
println!("cargo:lib_dir={}", env::var("OUT_DIR").unwrap());
|
||||
println!("cargo:rustc-link-lib=static=bearssl");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
bearssl_handle_linkage();
|
||||
|
||||
let bindings = bindgen::builder()
|
||||
.use_core()
|
||||
.ctypes_prefix(CTYPES_PREFIX)
|
||||
.header(HEADER_PATH)
|
||||
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
|
||||
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
|
||||
.allowlist_type("(br|BR)_.*")
|
||||
.allowlist_var("(br|BR)_.*")
|
||||
.allowlist_function("(br|BR)_.*")
|
||||
.blocklist_type("__.*_t")
|
||||
.blocklist_type("size_t")
|
||||
.generate()
|
||||
.expect("Unable to generate bindings");
|
||||
|
||||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
|
||||
bindings
|
||||
.write_to_file(out_path.join("bindings.rs"))
|
||||
.expect("Couldn't write bindings!");
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#[cfg(not(feature = "bundled"))]
|
||||
mod dynamichost;
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
mod bundled;
|
||||
|
||||
#[cfg(feature = "bundled")]
|
||||
pub use bundled::{configure, HEADER_PATH};
|
||||
|
||||
#[cfg(not(feature = "bundled"))]
|
||||
pub use dynamichost::{configure, HEADER_PATH};
|
|
@ -0,0 +1,24 @@
|
|||
mod arch;
|
||||
mod os;
|
||||
|
||||
pub static HEADER_PATH: &str = "bundled/inc/bearssl.h";
|
||||
|
||||
pub fn configure() {
|
||||
let mut build = cc::Build::new();
|
||||
|
||||
os::configure(&mut build);
|
||||
arch::configure(&mut build);
|
||||
|
||||
build
|
||||
.include("bundled/inc")
|
||||
.files(
|
||||
std::fs::read_dir("bundled/src")
|
||||
.expect("failed to get bundled source path")
|
||||
.map(|e| e.expect("failed to read get file entry").path()),
|
||||
)
|
||||
.static_flag(true)
|
||||
.compile("bearssl");
|
||||
|
||||
println!("cargo:lib_dir={}", std::env::var("OUT_DIR").unwrap());
|
||||
println!("cargo:rustc-link-lib=static=bearssl");
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#[cfg(target_arch = "x86_64")]
|
||||
pub fn configure(build: &mut cc::Build) {
|
||||
let compiler = build.get_compiler();
|
||||
|
||||
build.define("BR_64", "1");
|
||||
|
||||
if compiler.is_like_gnu() || compiler.is_like_clang() {
|
||||
build.define("BR_INT128", "1");
|
||||
} else if compiler.is_like_msvc() {
|
||||
build.define("BR_UMUL128", "1");
|
||||
}
|
||||
|
||||
#[cfg(target_feature = "sse2")]
|
||||
build.define("BR_SSE2", "1");
|
||||
|
||||
#[cfg(target_feature = "aes")]
|
||||
build.define("BR_AES_X86NI", "1");
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "x86_64"))]
|
||||
pub fn configure(build: &mut cc::Build) {
|
||||
#[cfg(target_pointer_width = 64)]
|
||||
build.define("BR_64", "1");
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#[cfg(unix)]
|
||||
pub fn configure(build: &mut cc::Build) {
|
||||
build.define("BR_USE_UNIX_TIME", "1").define("BR_USE_URANDOM", "1");
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn configure(build: &mut cc::Build) {
|
||||
build.define("BR_USE_WIN32_RAND", "1").define("BR_USE_WIN32_TIME", "1");
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
pub static HEADER_PATH: &str = "wrapper.h";
|
||||
|
||||
pub fn configure() {
|
||||
#[cfg(not(unix))]
|
||||
panic!("linking dynamically is currently supported in only unix targets");
|
||||
|
||||
println!("cargo:rustc-link-search=/lib");
|
||||
println!("cargo:rustc-link-search=/usr/lib");
|
||||
println!("cargo:rustc-link-search=/usr/local/lib");
|
||||
println!("cargo:rustc-link-lib=dylib=bearssl");
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
mod linkage;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
linkage::configure();
|
||||
|
||||
let bindings = bindgen::builder()
|
||||
.use_core()
|
||||
.ctypes_prefix("::core::ffi")
|
||||
.header(linkage::HEADER_PATH)
|
||||
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
|
||||
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
|
||||
.allowlist_type("(br|BR)_.*")
|
||||
.allowlist_var("(br|BR)_.*")
|
||||
.allowlist_function("(br|BR)_.*")
|
||||
.blocklist_type("__.*_t")
|
||||
.blocklist_type("size_t")
|
||||
.generate_comments(false)
|
||||
.generate()
|
||||
.expect("Unable to generate bindings");
|
||||
|
||||
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
||||
|
||||
bindings
|
||||
.write_to_file(out_path.join("bindings.rs"))
|
||||
.expect("Couldn't write bindings!");
|
||||
}
|
Loading…
Reference in New Issue