From 283db281e27e89adabf40fbd8dabd4f489f083f4 Mon Sep 17 00:00:00 2001 From: Allen Webb Date: Fri, 14 Jan 2022 16:53:54 -0600 Subject: Add vboot_reference-sys This adds Rust bindings to vboot_reference. BRANCH=none BUG=b:214081328 TEST=cargo build Change-Id: I4f9df36a0de93c0617ead2a75ef2ca5fcf8f5652 Signed-off-by: Allen Webb Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3390514 Reviewed-by: Mike Frysinger --- .gitignore | 3 ++ PRESUBMIT.cfg | 4 +++ README | 3 ++ rust/OWNERS | 1 + rust/README.md | 18 ++++++++++++ rust/vboot_reference-sys/Cargo.toml | 17 +++++++++++ rust/vboot_reference-sys/build.rs | 54 +++++++++++++++++++++++++++++++++++ rust/vboot_reference-sys/crossystem.h | 1 + rust/vboot_reference-sys/lib.rs | 15 ++++++++++ 9 files changed, 116 insertions(+) create mode 100644 rust/OWNERS create mode 100644 rust/README.md create mode 100644 rust/vboot_reference-sys/Cargo.toml create mode 100644 rust/vboot_reference-sys/build.rs create mode 120000 rust/vboot_reference-sys/crossystem.h create mode 100644 rust/vboot_reference-sys/lib.rs diff --git a/.gitignore b/.gitignore index 6ac51f7a..9edc2439 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /build /build-au /build-main +Cargo.lock ID scripts/newbitmaps/default_source/*.bmp scripts/newbitmaps/images/out_* @@ -8,3 +9,5 @@ scripts/newbitmaps/lib/*.pyc scripts/newbitmaps/strings/font scripts/newbitmaps/strings/*.png scripts/newbitmaps/strings/localized_text/*/*.png +target +.idea diff --git a/PRESUBMIT.cfg b/PRESUBMIT.cfg index 54d538d5..9f665453 100644 --- a/PRESUBMIT.cfg +++ b/PRESUBMIT.cfg @@ -1,5 +1,6 @@ [Hook Overrides] branch_check: true +cargo_clippy_check: true checkpatch_check: true long_line_check: false signoff_check: true @@ -10,3 +11,6 @@ tab_check: false [Hook Overrides Options] cros_license_check: --exclude_regex=^\.checkpatch\.conf$ + +cargo_clippy_check: + --project=rust/vboot_reference-sys diff --git a/README b/README index 04ab0c93..1787d4dd 100644 --- a/README +++ b/README @@ -48,6 +48,9 @@ scripts/ Tools and scripts used to generate and use new signing keypairs. These are typically used only on a secure machine. +rust/ + + Rust bindings for vboot_reference. See rust/README.md for more details. -------------------- Building and testing diff --git a/rust/OWNERS b/rust/OWNERS new file mode 100644 index 00000000..fe3921f7 --- /dev/null +++ b/rust/OWNERS @@ -0,0 +1 @@ +allenwebb@chromium.org diff --git a/rust/README.md b/rust/README.md new file mode 100644 index 00000000..3a899cef --- /dev/null +++ b/rust/README.md @@ -0,0 +1,18 @@ +# Rust bindings for vboot_reference + +This path contains the vboot_reference-sys crate which uses bindgen to generate +Rust bindings for the vboot_reference C library. + +Each header is included as its own submodule. To use these bindings: + * Add `vboot_reference-sys` to your `Cargo.toml` for example: +```toml +[dependencies] +vboot_reference-sys = { path = "../../vboot_reference/rust/vboot_reference-sys" } +``` + * Include the symbols you need for example: +```rust +use vboot_reference_sys::crossystem::*; +``` + +The `build.rs` in `vboot_reference-sys` takes care of adding the necessary +includes and linker flags for `vboot_host` through the `pkg-config` crate. diff --git a/rust/vboot_reference-sys/Cargo.toml b/rust/vboot_reference-sys/Cargo.toml new file mode 100644 index 00000000..59769ee9 --- /dev/null +++ b/rust/vboot_reference-sys/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "vboot_reference-sys" +version = "1.0.0" +description = "Provides raw (unsafe) bindings to the vboot_reference C library." +authors = ["The Chromium OS Authors"] +edition = "2018" +build = "build.rs" + +[lib] +path = "lib.rs" + +[dependencies] +libc = "0.2.44" + +[build-dependencies] +pkg-config = "0.3" +which = "4.0.0" diff --git a/rust/vboot_reference-sys/build.rs b/rust/vboot_reference-sys/build.rs new file mode 100644 index 00000000..523602f7 --- /dev/null +++ b/rust/vboot_reference-sys/build.rs @@ -0,0 +1,54 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// Minijail's build script invoked by cargo. +/// +/// This script prefers linking against a pkg-config provided libminijail, but will fall back to +/// building libminijail statically. +use std::env; +use std::fs::remove_file; +use std::io; +use std::path::Path; +use std::process::Command; + +fn bindings_generation() -> io::Result<()> { + let bindgen = which::which("bindgen").unwrap(); + + let out_dir = env::var("OUT_DIR").unwrap(); + let gen_file = Path::new(&out_dir).join("./crossystem.rs"); + if gen_file.exists() { + remove_file(&gen_file).expect("Failed to remove generated file."); + } + let header_dir = Path::new("."); + let header_path = header_dir.join("crossystem.h"); + println!("cargo:rerun-if-changed={}", header_path.display()); + let status = Command::new(&bindgen) + .args(&["--default-enum-style", "rust"]) + .args(&["--blacklist-type", "__rlim64_t"]) + .args(&["--raw-line", "pub type __rlim64_t = u64;"]) + .args(&["--blacklist-type", "__u\\d{1,2}"]) + .args(&["--raw-line", "pub type __u8 = u8;"]) + .args(&["--raw-line", "pub type __u16 = u16;"]) + .args(&["--raw-line", "pub type __u32 = u32;"]) + .args(&["--blacklist-type", "__uint64_t"]) + .arg("--no-layout-tests") + .arg("--disable-header-comment") + .args(&["--output", gen_file.to_str().unwrap()]) + .arg(header_path.to_str().unwrap()) + .args(&[ + "--", + "-DUSE_BINDGEN", + "-D_FILE_OFFSET_BITS=64", + "-D_LARGEFILE_SOURCE", + "-D_LARGEFILE64_SOURCE", + ]) + .status()?; + assert!(status.success()); + Ok(()) +} + +fn main() -> io::Result<()> { + pkg_config::Config::new().probe("vboot_host").unwrap(); + bindings_generation() +} diff --git a/rust/vboot_reference-sys/crossystem.h b/rust/vboot_reference-sys/crossystem.h new file mode 120000 index 00000000..96726399 --- /dev/null +++ b/rust/vboot_reference-sys/crossystem.h @@ -0,0 +1 @@ +../../host/include/crossystem.h \ No newline at end of file diff --git a/rust/vboot_reference-sys/lib.rs b/rust/vboot_reference-sys/lib.rs new file mode 100644 index 00000000..85b6f2a1 --- /dev/null +++ b/rust/vboot_reference-sys/lib.rs @@ -0,0 +1,15 @@ +// Copyright 2022 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// vboot_reference bindings for Rust. + +#[allow( + clippy::all, + non_camel_case_types, + non_snake_case, + non_upper_case_globals +)] +pub mod crossystem { + include!(concat!(env!("OUT_DIR"), "/crossystem.rs")); +} -- cgit v1.2.1