diff mbox series

[V4,8/8] libgpiod: Integrate building of rust bindings with make

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

Commit Message

Viresh Kumar July 8, 2022, 11:35 a.m. UTC
Lets make build rust bindings as well.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 README                    |  8 +++++---
 TODO                      |  8 --------
 bindings/Makefile.am      |  6 ++++++
 bindings/rust/Makefile.am | 18 ++++++++++++++++++
 configure.ac              | 16 ++++++++++++++++
 5 files changed, 45 insertions(+), 11 deletions(-)
 create mode 100644 bindings/rust/Makefile.am

Comments

Kent Gibson July 27, 2022, 2:59 a.m. UTC | #1
On Fri, Jul 08, 2022 at 05:05:01PM +0530, Viresh Kumar wrote:
> Lets make build rust bindings as well.
> 
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  README                    |  8 +++++---
>  TODO                      |  8 --------
>  bindings/Makefile.am      |  6 ++++++
>  bindings/rust/Makefile.am | 18 ++++++++++++++++++
>  configure.ac              | 16 ++++++++++++++++
>  5 files changed, 45 insertions(+), 11 deletions(-)
>  create mode 100644 bindings/rust/Makefile.am
> 
> diff --git a/README b/README
> index 814a0f161fd2..68b5d69f9b66 100644
> --- a/README
> +++ b/README
> @@ -119,9 +119,9 @@ TOOLS
>  BINDINGS
>  --------
>  
> -High-level, object-oriented bindings for C++ and python3 are provided. They
> -can be enabled by passing --enable-bindings-cxx and --enable-bindings-python
> -arguments respectively to configure.
> +High-level, object-oriented bindings for C++, python3 and Rust are provided.
> +They can be enabled by passing --enable-bindings-cxx, --enable-bindings-python
> +and --enable-bindings-rust arguments respectively to configure.
>  
>  C++ bindings require C++11 support and autoconf-archive collection if building
>  from git.
> @@ -132,6 +132,8 @@ the PYTHON_CPPFLAGS and PYTHON_LIBS variables in order to point the build
>  system to the correct locations. During native builds, the configure script
>  can auto-detect the location of the development files.
>  
> +Rust bindings require cargo support.
> +
>  TESTING
>  -------
>  
> diff --git a/TODO b/TODO
> index 8bb4d8f3ad56..cf4fd7b4a962 100644
> --- a/TODO
> +++ b/TODO
> @@ -28,14 +28,6 @@ and is partially functional.
>  
>  ----------
>  
> -* implement rust bindings
> -
> -With Rust gaining popularity as a low-level system's language and the
> -possibility of it making its way into the linux kernel, it's probably time to
> -provide Rust bindings to libgpiod as part of the project.
> -
> -----------
> -
>  * implement a simple daemon for controlling GPIOs in C together with a client
>    program
>  
> diff --git a/bindings/Makefile.am b/bindings/Makefile.am
> index 8f8c762f254f..004ae23dbc58 100644
> --- a/bindings/Makefile.am
> +++ b/bindings/Makefile.am
> @@ -14,3 +14,9 @@ if WITH_BINDINGS_PYTHON
>  SUBDIRS += python
>  
>  endif
> +
> +if WITH_BINDINGS_RUST
> +
> +SUBDIRS += rust
> +
> +endif
> diff --git a/bindings/rust/Makefile.am b/bindings/rust/Makefile.am
> new file mode 100644
> index 000000000000..79a52bc691ae
> --- /dev/null
> +++ b/bindings/rust/Makefile.am
> @@ -0,0 +1,18 @@
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +# SPDX-FileCopyrightText: 2022 Viresh Kumar <viresh.kumar@linaro.org>
> +
> +command = cargo build --release --lib
> +
> +if WITH_TESTS
> +command += --tests
> +endif
> +
> +if WITH_EXAMPLES
> +command += --examples
> +endif
> +
> +all:
> +	$(command)
> +
> +clean:
> +	cargo clean
> diff --git a/configure.ac b/configure.ac
> index ab03673589e9..8458f734a606 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -211,6 +211,21 @@ then
>  		[AC_SUBST(PYTHON_LIBS, [`$PYTHON-config --libs`])])
>  fi
>  
> +AC_ARG_ENABLE([bindings-rust],
> +	[AS_HELP_STRING([--enable-bindings-rust],[enable rust bindings [default=no]])],
> +	[if test "x$enableval" = xyes; then with_bindings_rust=true; fi],
> +	[with_bindings_rust=false])
> +AM_CONDITIONAL([WITH_BINDINGS_RUST], [test "x$with_bindings_rust" = xtrue])
> +
> +if test "x$with_bindings_rust" = xtrue
> +then
> +	AC_CHECK_PROG([has_cargo], [cargo], [true], [false])
> +	if test "x$has_cargo" = xfalse
> +	then
> +		AC_MSG_ERROR([cargo not found - needed for rust bindings])
> +	fi
> +fi
> +
>  AC_CHECK_PROG([has_doxygen], [doxygen], [true], [false])
>  AM_CONDITIONAL([HAS_DOXYGEN], [test "x$has_doxygen" = xtrue])
>  if test "x$has_doxygen" = xfalse
> @@ -245,6 +260,7 @@ AC_CONFIG_FILES([Makefile
>  		 bindings/python/Makefile
>  		 bindings/python/examples/Makefile
>  		 bindings/python/tests/Makefile
> +		 bindings/rust/Makefile
>  		 man/Makefile])
>  
>  AC_OUTPUT
> -- 
> 2.31.1.272.g89b43f80a514
> 

Wouldn't build for me on a Debian bullseye VM.
Apparently bindgen requires clang to find the system headers [1][2], and
there is no dep check or warning about that.

Also not sure why the build wanted bindgen, as by default it uses the
pre-generated bindings?

Anyway, for reference this was the build error without clang installed:

...
   Compiling thiserror v1.0.31
   Compiling libgpiod-sys v0.1.0 (/home/dev/libgpiod/bindings/rust/libgpiod-sys)
error: failed to run custom build command for `libgpiod-sys v0.1.0 (/home/dev/libgpiod/bindings/rust/libgpiod-sys)`

Caused by:
  process didn't exit successfully: `/home/dev/libgpiod/bindings/rust/target/release/build/libgpiod-sys-0fb8ce8170c88d8f/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=wrapper.h
  cargo:rerun-if-changed=../../../lib/chip.c
  cargo:rerun-if-changed=../../../lib/chip-info.c
  cargo:rerun-if-changed=../../../lib/edge-event.c
  cargo:rerun-if-changed=../../../lib/info-event.c
  cargo:rerun-if-changed=../../../lib/internal.c
  cargo:rerun-if-changed=../../../lib/line-config.c
  cargo:rerun-if-changed=../../../lib/line-info.c
  cargo:rerun-if-changed=../../../lib/line-request.c
  cargo:rerun-if-changed=../../../lib/misc.c
  cargo:rerun-if-changed=../../../lib/request-config.c
  cargo:rerun-if-changed=../../../tests/gpiosim/gpiosim.c
  cargo:rerun-if-changed=gpiosim_wrapper.h
  cargo:rustc-link-lib=kmod
  cargo:rustc-link-lib=mount

  --- stderr
  /usr/include/string.h:33:10: fatal error: 'stddef.h' file not found
  /usr/include/string.h:33:10: fatal error: 'stddef.h' file not found, err: true
  thread 'main' panicked at 'Unable to generate bindings: ()', libgpiod-sys/build.rs:42:10

Cheers,
Kent.

[1] https://rust-lang.github.io/rust-bindgen/requirements.html
[2] https://github.com/rust-lang/rust-bindgen/issues/242
Viresh Kumar July 27, 2022, 6:18 a.m. UTC | #2
On 27-07-22, 10:59, Kent Gibson wrote:
> Wouldn't build for me on a Debian bullseye VM.
> Apparently bindgen requires clang to find the system headers [1][2],

Right.

> and
> there is no dep check or warning about that.

Ahh, I need to add that somewhere then.

> Also not sure why the build wanted bindgen, as by default it uses the
> pre-generated bindings?

Did you enable tests as well ? That enables "gpiosim", which enables "generate"
and bindgen will be required.

I thought it will be better to build bindings everytime for tests.

> Anyway, for reference this was the build error without clang installed:

Did it work for you after clang was installed ?

> 
> ...
>    Compiling thiserror v1.0.31
>    Compiling libgpiod-sys v0.1.0 (/home/dev/libgpiod/bindings/rust/libgpiod-sys)
> error: failed to run custom build command for `libgpiod-sys v0.1.0 (/home/dev/libgpiod/bindings/rust/libgpiod-sys)`
> 
> Caused by:
>   process didn't exit successfully: `/home/dev/libgpiod/bindings/rust/target/release/build/libgpiod-sys-0fb8ce8170c88d8f/build-script-build` (exit status: 101)
>   --- stdout
>   cargo:rerun-if-changed=wrapper.h
>   cargo:rerun-if-changed=../../../lib/chip.c
>   cargo:rerun-if-changed=../../../lib/chip-info.c
>   cargo:rerun-if-changed=../../../lib/edge-event.c
>   cargo:rerun-if-changed=../../../lib/info-event.c
>   cargo:rerun-if-changed=../../../lib/internal.c
>   cargo:rerun-if-changed=../../../lib/line-config.c
>   cargo:rerun-if-changed=../../../lib/line-info.c
>   cargo:rerun-if-changed=../../../lib/line-request.c
>   cargo:rerun-if-changed=../../../lib/misc.c
>   cargo:rerun-if-changed=../../../lib/request-config.c
>   cargo:rerun-if-changed=../../../tests/gpiosim/gpiosim.c
>   cargo:rerun-if-changed=gpiosim_wrapper.h
>   cargo:rustc-link-lib=kmod
>   cargo:rustc-link-lib=mount
> 
>   --- stderr
>   /usr/include/string.h:33:10: fatal error: 'stddef.h' file not found
>   /usr/include/string.h:33:10: fatal error: 'stddef.h' file not found, err: true
>   thread 'main' panicked at 'Unable to generate bindings: ()', libgpiod-sys/build.rs:42:10
Kent Gibson July 27, 2022, 6:25 a.m. UTC | #3
On Wed, Jul 27, 2022 at 11:48:24AM +0530, Viresh Kumar wrote:
> On 27-07-22, 10:59, Kent Gibson wrote:
> > Wouldn't build for me on a Debian bullseye VM.
> > Apparently bindgen requires clang to find the system headers [1][2],
> 
> Right.
> 
> > and
> > there is no dep check or warning about that.
> 
> Ahh, I need to add that somewhere then.
> 
> > Also not sure why the build wanted bindgen, as by default it uses the
> > pre-generated bindings?
> 
> Did you enable tests as well ? That enables "gpiosim", which enables "generate"
> and bindgen will be required.

Ahh, yeah I do --enable-tests as well.

> 
> I thought it will be better to build bindings everytime for tests.
> 

So you can't run tests on those platforms where bindgen is problematic?

> > Anyway, for reference this was the build error without clang installed:
> 
> Did it work for you after clang was installed ?
> 

Yeah, all good once clang was installed.

Cheers,
Kent.

> > 
> > ...
> >    Compiling thiserror v1.0.31
> >    Compiling libgpiod-sys v0.1.0 (/home/dev/libgpiod/bindings/rust/libgpiod-sys)
> > error: failed to run custom build command for `libgpiod-sys v0.1.0 (/home/dev/libgpiod/bindings/rust/libgpiod-sys)`
> > 
> > Caused by:
> >   process didn't exit successfully: `/home/dev/libgpiod/bindings/rust/target/release/build/libgpiod-sys-0fb8ce8170c88d8f/build-script-build` (exit status: 101)
> >   --- stdout
> >   cargo:rerun-if-changed=wrapper.h
> >   cargo:rerun-if-changed=../../../lib/chip.c
> >   cargo:rerun-if-changed=../../../lib/chip-info.c
> >   cargo:rerun-if-changed=../../../lib/edge-event.c
> >   cargo:rerun-if-changed=../../../lib/info-event.c
> >   cargo:rerun-if-changed=../../../lib/internal.c
> >   cargo:rerun-if-changed=../../../lib/line-config.c
> >   cargo:rerun-if-changed=../../../lib/line-info.c
> >   cargo:rerun-if-changed=../../../lib/line-request.c
> >   cargo:rerun-if-changed=../../../lib/misc.c
> >   cargo:rerun-if-changed=../../../lib/request-config.c
> >   cargo:rerun-if-changed=../../../tests/gpiosim/gpiosim.c
> >   cargo:rerun-if-changed=gpiosim_wrapper.h
> >   cargo:rustc-link-lib=kmod
> >   cargo:rustc-link-lib=mount
> > 
> >   --- stderr
> >   /usr/include/string.h:33:10: fatal error: 'stddef.h' file not found
> >   /usr/include/string.h:33:10: fatal error: 'stddef.h' file not found, err: true
> >   thread 'main' panicked at 'Unable to generate bindings: ()', libgpiod-sys/build.rs:42:10
> 
> -- 
> viresh
Viresh Kumar July 27, 2022, 6:35 a.m. UTC | #4
On 27-07-22, 14:25, Kent Gibson wrote:
> So you can't run tests on those platforms where bindgen is problematic?

Specifically for my case, that environment (rust-vmm-container) never runs
libgpiod tests, but just vhost-device/gpio tests. So it works.
Kent Gibson July 27, 2022, 6:45 a.m. UTC | #5
On Wed, Jul 27, 2022 at 12:05:27PM +0530, Viresh Kumar wrote:
> On 27-07-22, 14:25, Kent Gibson wrote:
> > So you can't run tests on those platforms where bindgen is problematic?
> 
> Specifically for my case, that environment (rust-vmm-container) never runs
> libgpiod tests, but just vhost-device/gpio tests. So it works.
> 

That may work for you, but in general it would be better if the tests
can be run even if the bindings can't be regenerated, so the two should
be independent.

Cheers,
Kent.
Viresh Kumar July 27, 2022, 6:51 a.m. UTC | #6
On 27-07-22, 14:45, Kent Gibson wrote:
> On Wed, Jul 27, 2022 at 12:05:27PM +0530, Viresh Kumar wrote:
> > On 27-07-22, 14:25, Kent Gibson wrote:
> > > So you can't run tests on those platforms where bindgen is problematic?
> > 
> > Specifically for my case, that environment (rust-vmm-container) never runs
> > libgpiod tests, but just vhost-device/gpio tests. So it works.
> > 
> 
> That may work for you, but in general it would be better if the tests
> can be run even if the bindings can't be regenerated, so the two should
> be independent.

Hmm, then we can take the same approach as for gpiod.h bindings. Use
pre-compiled ones unless "generate" feature is enabled.

Will do.
diff mbox series

Patch

diff --git a/README b/README
index 814a0f161fd2..68b5d69f9b66 100644
--- a/README
+++ b/README
@@ -119,9 +119,9 @@  TOOLS
 BINDINGS
 --------
 
-High-level, object-oriented bindings for C++ and python3 are provided. They
-can be enabled by passing --enable-bindings-cxx and --enable-bindings-python
-arguments respectively to configure.
+High-level, object-oriented bindings for C++, python3 and Rust are provided.
+They can be enabled by passing --enable-bindings-cxx, --enable-bindings-python
+and --enable-bindings-rust arguments respectively to configure.
 
 C++ bindings require C++11 support and autoconf-archive collection if building
 from git.
@@ -132,6 +132,8 @@  the PYTHON_CPPFLAGS and PYTHON_LIBS variables in order to point the build
 system to the correct locations. During native builds, the configure script
 can auto-detect the location of the development files.
 
+Rust bindings require cargo support.
+
 TESTING
 -------
 
diff --git a/TODO b/TODO
index 8bb4d8f3ad56..cf4fd7b4a962 100644
--- a/TODO
+++ b/TODO
@@ -28,14 +28,6 @@  and is partially functional.
 
 ----------
 
-* implement rust bindings
-
-With Rust gaining popularity as a low-level system's language and the
-possibility of it making its way into the linux kernel, it's probably time to
-provide Rust bindings to libgpiod as part of the project.
-
-----------
-
 * implement a simple daemon for controlling GPIOs in C together with a client
   program
 
diff --git a/bindings/Makefile.am b/bindings/Makefile.am
index 8f8c762f254f..004ae23dbc58 100644
--- a/bindings/Makefile.am
+++ b/bindings/Makefile.am
@@ -14,3 +14,9 @@  if WITH_BINDINGS_PYTHON
 SUBDIRS += python
 
 endif
+
+if WITH_BINDINGS_RUST
+
+SUBDIRS += rust
+
+endif
diff --git a/bindings/rust/Makefile.am b/bindings/rust/Makefile.am
new file mode 100644
index 000000000000..79a52bc691ae
--- /dev/null
+++ b/bindings/rust/Makefile.am
@@ -0,0 +1,18 @@ 
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2022 Viresh Kumar <viresh.kumar@linaro.org>
+
+command = cargo build --release --lib
+
+if WITH_TESTS
+command += --tests
+endif
+
+if WITH_EXAMPLES
+command += --examples
+endif
+
+all:
+	$(command)
+
+clean:
+	cargo clean
diff --git a/configure.ac b/configure.ac
index ab03673589e9..8458f734a606 100644
--- a/configure.ac
+++ b/configure.ac
@@ -211,6 +211,21 @@  then
 		[AC_SUBST(PYTHON_LIBS, [`$PYTHON-config --libs`])])
 fi
 
+AC_ARG_ENABLE([bindings-rust],
+	[AS_HELP_STRING([--enable-bindings-rust],[enable rust bindings [default=no]])],
+	[if test "x$enableval" = xyes; then with_bindings_rust=true; fi],
+	[with_bindings_rust=false])
+AM_CONDITIONAL([WITH_BINDINGS_RUST], [test "x$with_bindings_rust" = xtrue])
+
+if test "x$with_bindings_rust" = xtrue
+then
+	AC_CHECK_PROG([has_cargo], [cargo], [true], [false])
+	if test "x$has_cargo" = xfalse
+	then
+		AC_MSG_ERROR([cargo not found - needed for rust bindings])
+	fi
+fi
+
 AC_CHECK_PROG([has_doxygen], [doxygen], [true], [false])
 AM_CONDITIONAL([HAS_DOXYGEN], [test "x$has_doxygen" = xtrue])
 if test "x$has_doxygen" = xfalse
@@ -245,6 +260,7 @@  AC_CONFIG_FILES([Makefile
 		 bindings/python/Makefile
 		 bindings/python/examples/Makefile
 		 bindings/python/tests/Makefile
+		 bindings/rust/Makefile
 		 man/Makefile])
 
 AC_OUTPUT