diff mbox series

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

Message ID c8ef89c343a0c31e4132cc6757fdff8dec0d1669.1665744170.git.viresh.kumar@linaro.org
State Superseded
Headers show
Series libgpiod: Add Rust bindings | expand

Commit Message

Viresh Kumar Oct. 14, 2022, 10:47 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 | 15 ++++++++++
 bindings/rust/libgpiod-sys/build.rs   | 41 +++++++++++++++++++++++++++
 bindings/rust/libgpiod-sys/src/lib.rs | 13 +++++++++
 5 files changed, 79 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

Miguel Ojeda Oct. 18, 2022, 11:42 a.m. UTC | #1
On Tue, Oct 18, 2022 at 1:22 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> +description = "A rust library for libgpiod public header bindings"

I think "A rust library for" is redundant (I would write it in
GitHub's description, but not in Cargo's). But double-check with a few
other well-known libraries out there to see what they do for their
Cargo descriptions.

> +repository = "https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/"

No need for the trailing slash, I think.

More importantly, you may want to add a "documentation", "categories"
and "rust-version" keys.

Cheers,
Miguel
Viresh Kumar Oct. 19, 2022, 6:46 a.m. UTC | #2
On 18-10-22, 19:49, Kent Gibson wrote:
> You should add "categories" and "documentation" if you intend to publish
> to crates.io.

In order to add documentations, should I commit documentation (created
with cargo) as well in libgpiod, so I get a link to it ?

> LGPL is probably not appropriate here, as the binding code ends up
> being part of the binary - unless you plan to package it as a dynamic
> library?
> 
> The licenses of your dependencies are:
> $ cargo license
> (MIT OR Apache-2.0) AND Unicode-DFS-2016 (1): unicode-ident
> Apache-2.0 AND BSD-3-Clause (1): vmm-sys-util

This is the license used by the user crate for libgpiod, vhost-device,
I am inclined to use this then, unless someone has an objection to it.

> Apache-2.0 OR MIT (8): bitflags, cc, libc, proc-macro2, quote, syn, thiserror, thiserror-impl
> MIT (1): intmap
> N/A (3): gpiosim, libgpiod, libgpiod-sys
> 
> IANAL, but one or more of those would be more appropriate than either
> GPL or LGPL.
> You, Bart and linaro(?) will have to come to some agreement on which
> license or combination to go with - it depends.
Viresh Kumar Oct. 19, 2022, 11:22 a.m. UTC | #3
On 19-10-22, 15:21, Kent Gibson wrote:
> That doesn't really work - you get a link to the files in github, not
> as a web site.  You need somewhere that will host those generated files
> as a web site.  You could do that with github-pages if you don't have other
> options.

I don't have other options for now, so it is github.

> When I've done that I would commit the docs to a separate
> branch, just for the docs, and have github-pages host that branch.

Okay.

> If you eventually publish your crate to crates.io you get documentation on
> docs.rs for free - and you can skip the documentation key in that case too
> - it defaults to the appropriate page on docs.rs.

Right, that I knew.

> I assume that would be the case long term - you just need to find
> someway to host them in the meantime.

I wonder if we should be doing this at all right now, or if required
only Bartosz should do it instead of me, once this is merged ?

Maybe we need a libgpiod username for that on github ? To make it
independent of personal usernames ? From what I saw, the website name
will be username.github.io eventually.
Kent Gibson Oct. 19, 2022, 12:01 p.m. UTC | #4
On Wed, Oct 19, 2022 at 04:52:51PM +0530, Viresh Kumar wrote:
> On 19-10-22, 15:21, Kent Gibson wrote:
> > That doesn't really work - you get a link to the files in github, not
> > as a web site.  You need somewhere that will host those generated files
> > as a web site.  You could do that with github-pages if you don't have other
> > options.
> 
> I don't have other options for now, so it is github.
> 
> > When I've done that I would commit the docs to a separate
> > branch, just for the docs, and have github-pages host that branch.
> 
> Okay.
> 
> > If you eventually publish your crate to crates.io you get documentation on
> > docs.rs for free - and you can skip the documentation key in that case too
> > - it defaults to the appropriate page on docs.rs.
> 
> Right, that I knew.
> 
> > I assume that would be the case long term - you just need to find
> > someway to host them in the meantime.
> 
> I wonder if we should be doing this at all right now, or if required
> only Bartosz should do it instead of me, once this is merged ?
> 

I was assuming it was an interim solution, so it doesn't matter so much.

I'd be happy with docs.rs once libgpiod v2 is released and the bindings
published to crates.io, and I'm personally good with looking at the
generated docs locally in the meantime.

It would be easier for others to take a look if the docs were hosted,
but I don't have any feel as to whether that is worth the effort or not.

> Maybe we need a libgpiod username for that on github ? To make it
> independent of personal usernames ? From what I saw, the website name
> will be username.github.io eventually.
> 

A libgpiod group account might be useful.

And it would be nice to have a libgpiod space somewhere, not just
piggybacking on the gpio mailing list.  Not sure if github is the best
place - but I haven't given it much thought.

Bart, do you have any opinions?

Cheers,
Kent.
Viresh Kumar Oct. 20, 2022, 5:34 a.m. UTC | #5
On 19-10-22, 20:01, Kent Gibson wrote:
> I was assuming it was an interim solution, so it doesn't matter so much.
> 
> I'd be happy with docs.rs once libgpiod v2 is released and the bindings
> published to crates.io, and I'm personally good with looking at the
> generated docs locally in the meantime.

I do plan to publish them to crates.io.

> It would be easier for others to take a look if the docs were hosted,
> but I don't have any feel as to whether that is worth the effort or not.

Lets get the code merge without the docs published, we can always come
back and publish the documentation somewhere once we are sure where to
publish them, if it is really needed since we are going to migrate to
docs.rs anyway..
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..d0b3a3c88ff1
--- /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..77f82719d269
--- /dev/null
+++ b/bindings/rust/libgpiod-sys/Cargo.toml
@@ -0,0 +1,15 @@ 
+[package]
+name = "libgpiod-sys"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[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..1ca355b5f5ac
--- /dev/null
+++ b/bindings/rust/libgpiod-sys/src/lib.rs
@@ -0,0 +1,13 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#[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::*;