Skip to content

Use v3.0 OASIS header files #156

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[codespell]
skip = .git,target,Cargo.lock
skip = .git,target,Cargo.lock,vendor
ignore-words-list = crate,keypair
67 changes: 26 additions & 41 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,31 +57,40 @@ mod generate {
}

pub fn generate_bindings() {
let bindings = bindgen::Builder::default()
.header("pkcs11.h")
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
const GENERIC_PRELUDE: &str = "#define CRYPTOKI_FORCE_WIN32 1\n";
builder = builder
// layout tests are not generic
.layout_tests(false)
.header_contents("generic-prelude.h", GENERIC_PRELUDE)
}

builder = builder
.header("platform.h")
.dynamic_library_name("Pkcs11")
// The PKCS11 library works in a slightly different way to most shared libraries. We have
// to call `C_GetFunctionList`, which returns a list of pointers to the _actual_ library
// functions. This is the only function we need to create a binding for.
.allowlist_function("C_GetFunctionList")
// This is needed because no types will be generated if `allowlist_function` is used.
// Unsure if this is a bug.
.allowlist_type("*")
.allowlist_file("pkcs11.h")
// See this issue: https://github.com/parallaxsecond/rust-cryptoki/issues/12
.blocklist_type("max_align_t")
// ~1 is not converted properly
.blocklist_item("CK_UNAVAILABLE_INFORMATION")
// Derive the `Debug` trait for the generated structs where possible.
.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!");
}
}
Loading