Skip to content

Commit

Permalink
Make generic bindings
Browse files Browse the repository at this point in the history
The platform specific bindings are similar, except for:
- the `packed` attribute
- the layout_tests

By removing the tests and adding `cfg_attr` for `packed` you get a
binding that works cross platform.

Signed-off-by: Arjen Nienhuis <[email protected]>
  • Loading branch information
arjennienhuis committed Jun 20, 2023
1 parent a1d7d18 commit 6db620c
Show file tree
Hide file tree
Showing 4 changed files with 3,224 additions and 30 deletions.
54 changes: 24 additions & 30 deletions cryptoki-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@ fn main() {
{
generate::generate_bindings();
}

#[cfg(not(feature = "generate-bindings"))]
{
use std::str::FromStr;
use target_lexicon::{Architecture, OperatingSystem, Triple};

let target = Triple::from_str(&std::env::var("TARGET").unwrap())
.expect("Failed to parse target triple");
match (target.architecture, target.operating_system) {
(Architecture::Arm(_), OperatingSystem::Linux) => {}
(Architecture::Aarch64(_), OperatingSystem::Linux) => {}
(Architecture::X86_64, OperatingSystem::Linux) => {}
(Architecture::X86_32(_), OperatingSystem::Linux) => {}
(Architecture::Powerpc64, OperatingSystem::Linux) => {}
(Architecture::Powerpc64le, OperatingSystem::Linux) => {}
(Architecture::X86_64, OperatingSystem::Darwin) => {}
(Architecture::Aarch64(_), OperatingSystem::Darwin) => {}
(Architecture::X86_64, OperatingSystem::Windows) => {}
(Architecture::X86_64, OperatingSystem::Freebsd) => {}
(arch, os) => {
panic!("Compilation target (architecture, OS) tuple ({}, {}) is not part of the supported tuples. Please compile with the \"generate-bindings\" feature or add support for your platform :)", arch, os);
}
}
}
}

// Only on a specific feature
Expand Down Expand Up @@ -81,7 +57,19 @@ mod generate {
}

pub fn generate_bindings() {
let bindings = bindgen::Builder::default()
let make_generic: bool = std::env::var_os("MAKE_GENERIC_BINDINGS").is_some();
let mut builder = bindgen::Builder::default();
if make_generic {
// only WIN32 bindings are "packed". It's easier to "unpack" for other architectures
// __declspec is not needed and causes problems
const GENERIC_PRELUDE: &str = "#define _WIN32 1\n#define __declspec(x)\n";
builder = builder
// layout tests are not generic
.layout_tests(false)
.header_contents("generic-prelude.h", GENERIC_PRELUDE)
}

builder = builder
.header("pkcs11.h")
.dynamic_library_name("Pkcs11")
// The PKCS11 library works in a slightly different way to most shared libraries. We have
Expand All @@ -98,14 +86,20 @@ mod generate {
.derive_debug(true)
// Derive the `Default` trait for the generated structs where possible.
.derive_default(true)
.parse_callbacks(Box::new(CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
.parse_callbacks(Box::new(CargoCallbacks));

let bindings = builder.generate().expect("Unable to generate bindings");

let mut data = bindings.to_string();
if make_generic {
const PACK_ALWAYS: &str = "#[repr(C, packed)]";
const PACK_WINDOWS: &str = "#[repr(C)]\n#[cfg_attr(windows, repr(packed))]";
data = data.replace(PACK_ALWAYS, PACK_WINDOWS);
}

// Write the bindings to the $OUT_DIR/pkcs11_bindings.rs file.
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("pkcs11_bindings.rs"))
std::fs::write(out_path.join("pkcs11_bindings.rs"), data)
.expect("Couldn't write bindings!");
}
}
4 changes: 4 additions & 0 deletions cryptoki-sys/regenerate_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

set -xeuf -o pipefail

find ../target/ -name "pkcs11_bindings.rs" -delete
MAKE_GENERIC_BINDINGS=1 cargo build --features generate-bindings
find ../target/ -name "pkcs11_bindings.rs" | xargs -I '{}' cp '{}' src/bindings/generic.rs

targets="aarch64-unknown-linux-gnu arm-unknown-linux-gnueabi x86_64-pc-windows-msvc i686-unknown-linux-gnu powerpc64-unknown-linux-gnu x86_64-unknown-linux-gnu x86_64-apple-darwin aarch64-apple-darwin x86_64-unknown-freebsd"
TARGET_INSTALLED=

Expand Down
Loading

0 comments on commit 6db620c

Please sign in to comment.