From 06d32e11473bbd4631cf2709c16d220d81280f2d Mon Sep 17 00:00:00 2001 From: Evan Benn Date: Wed, 1 Jun 2022 09:30:50 +1000 Subject: bindings/rust/libflashrom-sys: Create thin rust bindgen binding Use bindgen to create a rust library that calls into libflashrom using FFI. BUG=b:230545739 BRANCH=None TEST=cargo test Change-Id: I36ba70de2bca8f83d2fdd6fd4c9b7c28a11624ee Signed-off-by: Evan Benn Reviewed-on: https://review.coreboot.org/c/flashrom/+/65280 Reviewed-by: Edward O'Callaghan Reviewed-by: Peter Marheine Tested-by: build bot (Jenkins) --- .gitattributes | 2 ++ .gitignore | 2 ++ bindings/rust/libflashrom-sys/.cargo/config.toml | 2 ++ bindings/rust/libflashrom-sys/Cargo.toml | 10 ++++++++ bindings/rust/libflashrom-sys/build.rs | 32 ++++++++++++++++++++++++ bindings/rust/libflashrom-sys/src/lib.rs | 5 ++++ 6 files changed, 53 insertions(+) create mode 100644 bindings/rust/libflashrom-sys/.cargo/config.toml create mode 100644 bindings/rust/libflashrom-sys/Cargo.toml create mode 100644 bindings/rust/libflashrom-sys/build.rs create mode 100644 bindings/rust/libflashrom-sys/src/lib.rs diff --git a/.gitattributes b/.gitattributes index 10f89626..a663df71 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,7 @@ .gitattributes export-ignore .gitignore export-ignore +/bindings export-ignore +/bindings/** export-ignore /util/getrevision.sh export-ignore /util/git-hooks export-ignore /util/git-hooks/** export-ignore diff --git a/.gitignore b/.gitignore index d47cb9e5..8a32a701 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ /util/ich_descriptors_tool/ich_descriptors_tool.exe /util/ich_descriptors_tool/.dep /util/ich_descriptors_tool/.obj + +target/ diff --git a/bindings/rust/libflashrom-sys/.cargo/config.toml b/bindings/rust/libflashrom-sys/.cargo/config.toml new file mode 100644 index 00000000..8af59dd8 --- /dev/null +++ b/bindings/rust/libflashrom-sys/.cargo/config.toml @@ -0,0 +1,2 @@ +[env] +RUST_TEST_THREADS = "1" diff --git a/bindings/rust/libflashrom-sys/Cargo.toml b/bindings/rust/libflashrom-sys/Cargo.toml new file mode 100644 index 00000000..54e016d3 --- /dev/null +++ b/bindings/rust/libflashrom-sys/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "libflashrom-sys" +version = "0.1.0" +links = "flashrom" +build = "build.rs" +edition = "2021" + +[build-dependencies] +pkg-config = "0.3.19" +bindgen = "0.59.2" diff --git a/bindings/rust/libflashrom-sys/build.rs b/bindings/rust/libflashrom-sys/build.rs new file mode 100644 index 00000000..43e7cf6c --- /dev/null +++ b/bindings/rust/libflashrom-sys/build.rs @@ -0,0 +1,32 @@ +fn main() { + pkg_config::probe_library("flashrom").unwrap(); + let bindings = bindgen::Builder::default() + .header("../../../include/libflashrom.h") + // Tell cargo to invalidate the built crate whenever any of the + // included header files changed. + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + // only generate the flashrom functions and used types. + .allowlist_function("flashrom_.*") + // newtype enums provide type checking without the UB potential of rust enums. + .default_enum_style(bindgen::EnumVariation::NewType { is_bitfield: false }) + // We use constified enum for flashrom_log_level to allow '<' comparison. + .constified_enum("flashrom_log_level") + .prepend_enum_name(false) + .derive_copy(false) + .must_use_type("flashrom_wp_result") + // Avoid some va_list related functionality that is not easy to use in rust. + .blocklist_function("flashrom_set_log_callback") + .blocklist_type("flashrom_log_callback") + .blocklist_type("va_list") + .blocklist_type("__builtin_va_list") + .blocklist_type("__va_list_tag") + .size_t_is_usize(true) + .generate() + .expect("Unable to generate bindings"); + + // Write the bindings to the $OUT_DIR/bindings.rs file. + let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/bindings/rust/libflashrom-sys/src/lib.rs b/bindings/rust/libflashrom-sys/src/lib.rs new file mode 100644 index 00000000..4dc82827 --- /dev/null +++ b/bindings/rust/libflashrom-sys/src/lib.rs @@ -0,0 +1,5 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(deref_nullptr)] +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); -- cgit v1.2.3