summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
Diffstat (limited to 'rust')
-rw-r--r--rust/OWNERS1
-rw-r--r--rust/README.md18
-rw-r--r--rust/vboot_reference-sys/Cargo.toml17
-rw-r--r--rust/vboot_reference-sys/build.rs54
l---------rust/vboot_reference-sys/crossystem.h1
-rw-r--r--rust/vboot_reference-sys/lib.rs15
6 files changed, 106 insertions, 0 deletions
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"));
+}