diff mbox series

[libgpiod,V9,1/8] bindings: rust: Add libgpiod-sys rust crate

Message ID 2a943ded955e634fc76022dd3bccdb0e8f8b4313.1667815011.git.viresh.kumar@linaro.org
State New
Headers show
Series libgpiod: Add Rust bindings | expand

Commit Message

Viresh Kumar Nov. 7, 2022, 9:57 a.m. UTC
This adds libgpiod-sys rust crate, which provides FFI (foreign function
interface) bindings for libgpiod APIs.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 .gitignore                            |  5 ++++
 bindings/rust/Cargo.toml              |  5 ++++
 bindings/rust/libgpiod-sys/Cargo.toml | 20 +++++++++++++
 bindings/rust/libgpiod-sys/build.rs   | 41 +++++++++++++++++++++++++++
 bindings/rust/libgpiod-sys/src/lib.rs | 13 +++++++++
 5 files changed, 84 insertions(+)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/libgpiod-sys/Cargo.toml
 create mode 100644 bindings/rust/libgpiod-sys/build.rs
 create mode 100644 bindings/rust/libgpiod-sys/src/lib.rs

Comments

Viresh Kumar Nov. 14, 2022, 10:04 a.m. UTC | #1
On 09-11-22, 10:07, Bartosz Golaszewski wrote:
> On Mon, Nov 7, 2022 at 10:57 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> >
> > This adds libgpiod-sys rust crate, which provides FFI (foreign function
> > interface) bindings for libgpiod APIs.
> >
> > Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> > ---
> >  .gitignore                            |  5 ++++
> >  bindings/rust/Cargo.toml              |  5 ++++
> >  bindings/rust/libgpiod-sys/Cargo.toml | 20 +++++++++++++
> >  bindings/rust/libgpiod-sys/build.rs   | 41 +++++++++++++++++++++++++++
> >  bindings/rust/libgpiod-sys/src/lib.rs | 13 +++++++++
> >  5 files changed, 84 insertions(+)
> >  create mode 100644 bindings/rust/Cargo.toml
> >  create mode 100644 bindings/rust/libgpiod-sys/Cargo.toml
> >  create mode 100644 bindings/rust/libgpiod-sys/build.rs
> >  create mode 100644 bindings/rust/libgpiod-sys/src/lib.rs
> >
> > diff --git a/.gitignore b/.gitignore
> > index 6c08415b390d..9f2fcf440c5d 100644
> > --- a/.gitignore
> > +++ b/.gitignore
> > @@ -35,3 +35,8 @@ stamp-h1
> >  # profiling
> >  *.gcda
> >  *.gcno
> > +
> > +# Added by cargo
> > +
> > +target
> > +Cargo.lock
> 
> This should be put into bindings/rust/.gitignore

Should I resend the series with this one change ? Or just an incremental fix is
enough ?
Bartosz Golaszewski Nov. 14, 2022, 10:30 a.m. UTC | #2
On Mon, Nov 14, 2022 at 11:04 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 09-11-22, 10:07, Bartosz Golaszewski wrote:
> > On Mon, Nov 7, 2022 at 10:57 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> > >
> > > This adds libgpiod-sys rust crate, which provides FFI (foreign function
> > > interface) bindings for libgpiod APIs.
> > >
> > > Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> > > ---
> > >  .gitignore                            |  5 ++++
> > >  bindings/rust/Cargo.toml              |  5 ++++
> > >  bindings/rust/libgpiod-sys/Cargo.toml | 20 +++++++++++++
> > >  bindings/rust/libgpiod-sys/build.rs   | 41 +++++++++++++++++++++++++++
> > >  bindings/rust/libgpiod-sys/src/lib.rs | 13 +++++++++
> > >  5 files changed, 84 insertions(+)
> > >  create mode 100644 bindings/rust/Cargo.toml
> > >  create mode 100644 bindings/rust/libgpiod-sys/Cargo.toml
> > >  create mode 100644 bindings/rust/libgpiod-sys/build.rs
> > >  create mode 100644 bindings/rust/libgpiod-sys/src/lib.rs
> > >
> > > diff --git a/.gitignore b/.gitignore
> > > index 6c08415b390d..9f2fcf440c5d 100644
> > > --- a/.gitignore
> > > +++ b/.gitignore
> > > @@ -35,3 +35,8 @@ stamp-h1
> > >  # profiling
> > >  *.gcda
> > >  *.gcno
> > > +
> > > +# Added by cargo
> > > +
> > > +target
> > > +Cargo.lock
> >
> > This should be put into bindings/rust/.gitignore
>
> Should I resend the series with this one change ? Or just an incremental fix is
> enough ?
>
> --
> viresh

I'll fix it locally.

Bart
diff mbox series

Patch

diff --git a/.gitignore b/.gitignore
index 6c08415b390d..9f2fcf440c5d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,3 +35,8 @@  stamp-h1
 # profiling
 *.gcda
 *.gcno
+
+# Added by cargo
+
+target
+Cargo.lock
diff --git a/bindings/rust/Cargo.toml b/bindings/rust/Cargo.toml
new file mode 100644
index 000000000000..c7bbcc798920
--- /dev/null
+++ b/bindings/rust/Cargo.toml
@@ -0,0 +1,5 @@ 
+[workspace]
+
+members = [
+    "libgpiod-sys"
+]
diff --git a/bindings/rust/libgpiod-sys/Cargo.toml b/bindings/rust/libgpiod-sys/Cargo.toml
new file mode 100644
index 000000000000..47d0c2a7311c
--- /dev/null
+++ b/bindings/rust/libgpiod-sys/Cargo.toml
@@ -0,0 +1,20 @@ 
+[package]
+name = "libgpiod-sys"
+version = "0.1.0"
+authors = ["Viresh Kumar <viresh.kumar@linaro.org>"]
+description = "libgpiod public header bindings"
+repository = "https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git"
+categories = ["external-ffi-bindings", "os::linux-apis"]
+rust-version = "1.56"
+keywords = ["libgpiod", "gpio"]
+license = "Apache-2.0 OR BSD-3-Clause"
+edition = "2021"
+
+[dependencies]
+
+[features]
+generate = [ "bindgen" ]
+
+[build-dependencies]
+bindgen = { version = "0.59.1", optional = true }
+cc = "1.0.46"
diff --git a/bindings/rust/libgpiod-sys/build.rs b/bindings/rust/libgpiod-sys/build.rs
new file mode 100644
index 000000000000..98863686c7af
--- /dev/null
+++ b/bindings/rust/libgpiod-sys/build.rs
@@ -0,0 +1,41 @@ 
+#[cfg(feature = "generate")]
+extern crate bindgen;
+#[cfg(feature = "generate")]
+use std::env;
+#[cfg(feature = "generate")]
+use std::path::PathBuf;
+
+#[cfg(feature = "generate")]
+fn generate_bindings() {
+    // Tell cargo to invalidate the built crate whenever following files change
+    println!("cargo:rerun-if-changed=../../../include/gpiod.h");
+
+    // The bindgen::Builder is the main entry point
+    // to bindgen, and lets you build up options for
+    // the resulting bindings.
+    let bindings = bindgen::Builder::default()
+        // The input header we would like to generate
+        // bindings for.
+        .header("../../../include/gpiod.h")
+        // Tell cargo to invalidate the built crate whenever any of the
+        // included header files changed.
+        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
+        // Finish the builder and generate the bindings.
+        .generate()
+        // Unwrap the Result and panic on failure.
+        .expect("Unable to generate bindings");
+
+    // Write the bindings to the $OUT_DIR/bindings.rs file.
+    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
+    bindings
+        .write_to_file(out_path.join("bindings.rs"))
+        .expect("Couldn't write bindings!");
+}
+
+fn main() {
+    #[cfg(feature = "generate")]
+    generate_bindings();
+
+    println!("cargo:rustc-link-search=./../../lib/.libs/");
+    println!("cargo:rustc-link-lib=static=gpiod");
+}
diff --git a/bindings/rust/libgpiod-sys/src/lib.rs b/bindings/rust/libgpiod-sys/src/lib.rs
new file mode 100644
index 000000000000..a1d1db19afe3
--- /dev/null
+++ b/bindings/rust/libgpiod-sys/src/lib.rs
@@ -0,0 +1,13 @@ 
+// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
+
+#[allow(non_camel_case_types, non_upper_case_globals)]
+#[cfg_attr(test, allow(deref_nullptr, non_snake_case))]
+
+mod bindings_raw {
+    #[cfg(feature = "generate")]
+    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+
+    #[cfg(not(feature = "generate"))]
+    include!("bindings.rs");
+}
+pub use bindings_raw::*;