[RFC PATCH 00/11] rust: improved integration with cargo

Paolo Bonzini posted 11 patches 2 weeks ago
There is a newer version of this series
meson.build                                   |  56 +++---
.gitlab-ci.d/buildtest-template.yml           |  14 ++
.gitlab-ci.d/buildtest.yml                    |  14 ++
rust/Cargo.toml                               |  80 ++++++++
rust/hw/char/pl011/Cargo.toml                 |   3 +
rust/hw/char/pl011/src/device.rs              |   6 +-
rust/hw/char/pl011/src/lib.rs                 |  18 +-
rust/hw/char/pl011/src/memory_ops.rs          |   4 +-
rust/meson.build                              |  14 ++
rust/qemu-api-macros/Cargo.toml               |   3 +
rust/qemu-api/.gitignore                      |   2 +-
rust/qemu-api/Cargo.toml                      |   5 +-
rust/qemu-api/build.rs                        |  24 ++-
rust/qemu-api/meson.build                     |   5 +
rust/qemu-api/src/bindings.rs                 |  29 +++
rust/qemu-api/src/lib.rs                      |  22 ---
rust/qemu-api/src/zeroable.rs                 |   6 +-
rust/qemu-api/tests/tests.rs                  |   2 +-
scripts/rust/rustc_args.py                    | 178 ++++++++++++++++--
.../dockerfiles/fedora-rust-nightly.docker    |   4 +
tests/lcitool/refresh                         |   4 +
21 files changed, 391 insertions(+), 102 deletions(-)
create mode 100644 rust/qemu-api/src/bindings.rs
[RFC PATCH 00/11] rust: improved integration with cargo
Posted by Paolo Bonzini 2 weeks ago
While we're not sure where we'll be going in the future, for now
using cargo remains an important part of developing QEMU Rust code.
This is because cargo is the easiest way to run clippy, rustfmt,
rustdoc.  Cargo also allows working with doc tests, though there are
pretty much none yet, and provides tools such as "cargo expand".

This series aims at improving the integration with cargo and
cargo-based tooling.

First, while it is currently possible to run cargo on the rust/ directory,
but it has the issue that the bindings.rs must be placed by hand in
the build directory.  Therefore, this series starts by allowing
cargo to "just work" when run in a "meson devenv" environment:

    meson devenv -w ../rust cargo clippy --tests
    meson devenv -w ../rust cargo fmt

If you are going to use cargo repeatedly, invoking just 'meson devenv'
will put you in a shell where commands like 'cargo clippy' just work.
For simplicity, I am also adding two targets 'make clippy' and 'make
rustfmt'.

Secondly, one problem with mixing Cargo and meson is having to redo the
configuration of "lints" in both sides.  This series standardizes
on using Cargo.toml to configure the build, and bringing the flags
over to build.ninja with extensions to the existing rustc_args.py script.
I admit that these additions to the script are pretty large and therefore
I'm open to scrapping the idea.  I tried to organize the changes so that
the changes are split over multiple patches.

Finally, this series adds a CI job that runs rustfmt, clippy, and
rustdoc, including running doctests.

Please send comments!

Paolo

Paolo Bonzini (11):
  rust: qemu_api: do not disable lints outside bindgen-generated code
  rust: build: move rustc_args.py invocation to individual crates
  rust: build: restrict --cfg generation to only required symbols
  rust: build: generate warning flags from Cargo.toml
  rust: cargo: store desired warning levels in workspace Cargo.toml
  rust: build: move strict lints handling to rustc_args.py
  rust: fix a couple style issues from clippy
  rust: build: establish a baseline of lints across all crates
  rust: build: add "make clippy", "make rustfmt"
  rust: fix doc test syntax
  rust: ci: add job that runs Rust tools

 meson.build                                   |  56 +++---
 .gitlab-ci.d/buildtest-template.yml           |  14 ++
 .gitlab-ci.d/buildtest.yml                    |  14 ++
 rust/Cargo.toml                               |  80 ++++++++
 rust/hw/char/pl011/Cargo.toml                 |   3 +
 rust/hw/char/pl011/src/device.rs              |   6 +-
 rust/hw/char/pl011/src/lib.rs                 |  18 +-
 rust/hw/char/pl011/src/memory_ops.rs          |   4 +-
 rust/meson.build                              |  14 ++
 rust/qemu-api-macros/Cargo.toml               |   3 +
 rust/qemu-api/.gitignore                      |   2 +-
 rust/qemu-api/Cargo.toml                      |   5 +-
 rust/qemu-api/build.rs                        |  24 ++-
 rust/qemu-api/meson.build                     |   5 +
 rust/qemu-api/src/bindings.rs                 |  29 +++
 rust/qemu-api/src/lib.rs                      |  22 ---
 rust/qemu-api/src/zeroable.rs                 |   6 +-
 rust/qemu-api/tests/tests.rs                  |   2 +-
 scripts/rust/rustc_args.py                    | 178 ++++++++++++++++--
 .../dockerfiles/fedora-rust-nightly.docker    |   4 +
 tests/lcitool/refresh                         |   4 +
 21 files changed, 391 insertions(+), 102 deletions(-)
 create mode 100644 rust/qemu-api/src/bindings.rs

-- 
2.47.0
Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Alex Bennée 1 week, 2 days ago
Paolo Bonzini <pbonzini@redhat.com> writes:

> While we're not sure where we'll be going in the future, for now
> using cargo remains an important part of developing QEMU Rust code.
> This is because cargo is the easiest way to run clippy, rustfmt,
> rustdoc.  Cargo also allows working with doc tests, though there are
> pretty much none yet, and provides tools such as "cargo expand".
>
> This series aims at improving the integration with cargo and
> cargo-based tooling.
>
> First, while it is currently possible to run cargo on the rust/ directory,
> but it has the issue that the bindings.rs must be placed by hand in
> the build directory.  Therefore, this series starts by allowing
> cargo to "just work" when run in a "meson devenv" environment:
>
>     meson devenv -w ../rust cargo clippy --tests
>     meson devenv -w ../rust cargo fmt

Is this meant to be the rust source root, or the root of the rust
builddir:

$ meson devenv ../../rust

ERROR: Build data file './meson-private/build.dat' references functions or classes that don't exist. This probably means that it was generated with an old version of meson. Try running from the source directory meson setup . --wipe
🕙13:05:22 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] [🔴 ERROR] 
$ meson devenv rust

ERROR: Build data file './meson-private/build.dat' references functions or classes that don't exist. This probably means that it was generated with an old version of meson. Try running from the source directory meson setup . --wipe
🕙13:05:53 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] [🔴 ERROR] 

>
> If you are going to use cargo repeatedly, invoking just 'meson devenv'
> will put you in a shell where commands like 'cargo clippy' just work.
> For simplicity, I am also adding two targets 'make clippy' and 'make
> rustfmt'.
>
> Secondly, one problem with mixing Cargo and meson is having to redo the
> configuration of "lints" in both sides.  This series standardizes
> on using Cargo.toml to configure the build, and bringing the flags
> over to build.ninja with extensions to the existing rustc_args.py script.
> I admit that these additions to the script are pretty large and therefore
> I'm open to scrapping the idea.  I tried to organize the changes so that
> the changes are split over multiple patches.
>
> Finally, this series adds a CI job that runs rustfmt, clippy, and
> rustdoc, including running doctests.
>
> Please send comments!
>
> Paolo
>
> Paolo Bonzini (11):
>   rust: qemu_api: do not disable lints outside bindgen-generated code
>   rust: build: move rustc_args.py invocation to individual crates
>   rust: build: restrict --cfg generation to only required symbols
>   rust: build: generate warning flags from Cargo.toml
>   rust: cargo: store desired warning levels in workspace Cargo.toml
>   rust: build: move strict lints handling to rustc_args.py
>   rust: fix a couple style issues from clippy
>   rust: build: establish a baseline of lints across all crates
>   rust: build: add "make clippy", "make rustfmt"
>   rust: fix doc test syntax
>   rust: ci: add job that runs Rust tools
>
>  meson.build                                   |  56 +++---
>  .gitlab-ci.d/buildtest-template.yml           |  14 ++
>  .gitlab-ci.d/buildtest.yml                    |  14 ++
>  rust/Cargo.toml                               |  80 ++++++++
>  rust/hw/char/pl011/Cargo.toml                 |   3 +
>  rust/hw/char/pl011/src/device.rs              |   6 +-
>  rust/hw/char/pl011/src/lib.rs                 |  18 +-
>  rust/hw/char/pl011/src/memory_ops.rs          |   4 +-
>  rust/meson.build                              |  14 ++
>  rust/qemu-api-macros/Cargo.toml               |   3 +
>  rust/qemu-api/.gitignore                      |   2 +-
>  rust/qemu-api/Cargo.toml                      |   5 +-
>  rust/qemu-api/build.rs                        |  24 ++-
>  rust/qemu-api/meson.build                     |   5 +
>  rust/qemu-api/src/bindings.rs                 |  29 +++
>  rust/qemu-api/src/lib.rs                      |  22 ---
>  rust/qemu-api/src/zeroable.rs                 |   6 +-
>  rust/qemu-api/tests/tests.rs                  |   2 +-
>  scripts/rust/rustc_args.py                    | 178 ++++++++++++++++--
>  .../dockerfiles/fedora-rust-nightly.docker    |   4 +
>  tests/lcitool/refresh                         |   4 +
>  21 files changed, 391 insertions(+), 102 deletions(-)
>  create mode 100644 rust/qemu-api/src/bindings.rs

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Paolo Bonzini 1 week, 2 days ago
On Thu, Nov 14, 2024 at 2:07 PM Alex Bennée <alex.bennee@linaro.org> wrote:
> > First, while it is currently possible to run cargo on the rust/ directory,
> > it has the issue that the bindings.rs must be placed by hand in
> > the build directory.  Therefore, this series starts by allowing
> > cargo to "just work" when run in a "meson devenv" environment:
> >
> >     meson devenv -w ../rust cargo clippy --tests
> >     meson devenv -w ../rust cargo fmt
>
> Is this meant to be the rust source root, or the root of the rust
> builddir:
>
> $ meson devenv ../../rust

rust/ in the source directory.  You also need to run "meson devenv"
from the root of the build directory.

In practice you can just use "make clippy" or similar.

> ERROR: Build data file './meson-private/build.dat' references functions or classes that don't exist. This probably means that it was generated with an old version of meson. Try running from the source directory meson setup . --wipe
> 🕙13:05:22 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] [🔴 ERROR]
> $ meson devenv rust

Your meson-private/ directory is stale.  Any "make" or "ninja" invocation will
fix it.

Paolo
Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Alex Bennée 1 week, 2 days ago
Paolo Bonzini <pbonzini@redhat.com> writes:

> On Thu, Nov 14, 2024 at 2:07 PM Alex Bennée <alex.bennee@linaro.org> wrote:
>> > First, while it is currently possible to run cargo on the rust/ directory,
>> > it has the issue that the bindings.rs must be placed by hand in
>> > the build directory.  Therefore, this series starts by allowing
>> > cargo to "just work" when run in a "meson devenv" environment:
>> >
>> >     meson devenv -w ../rust cargo clippy --tests
>> >     meson devenv -w ../rust cargo fmt
>>
>> Is this meant to be the rust source root, or the root of the rust
>> builddir:
>>
>> $ meson devenv ../../rust
>
> rust/ in the source directory.  You also need to run "meson devenv"
> from the root of the build directory.
>
> In practice you can just use "make clippy" or similar.

make clippy certainly works

>> ERROR: Build data file './meson-private/build.dat' references
>> functions or classes that don't exist. This probably means that it
>> was generated with an old version of meson. Try running from the
>> source directory meson setup . --wipe
>> 🕙13:05:22 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] [🔴 ERROR]
>> $ meson devenv rust
>
> Your meson-private/ directory is stale.  Any "make" or "ninja" invocation will
> fix it.

✗  make -j30
[1/53] Generating tests/include/QAPI test (include) with a custom command
[2/21] Generating rust_arm_softmmu.rs with a custom command (wrapped by meson to capture output)
[3/21] Generating rust_aarch64_softmmu.rs with a custom command (wrapped by meson to capture output)
[4/21] Generating qemu-version.h with a custom command (wrapped by meson to capture output)
🕙15:18:58 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] 
➜  meson devenv ../../rust

ERROR: Build data file './meson-private/build.dat' references functions or classes that don't exist. This probably means that it was generated with an old version of meson. Try running from the source directory meson setup . --wipe

I also tried a wipe and re-configure but the same thing.

➜  ls -la meson-private/
total 24768
drwxr-xr-x  4 alex alex     4096 Nov 14 15:20 ./
drwxr-xr-x 77 alex alex     4096 Nov 14 15:21 ../
-rw-r--r--  1 alex alex     7569 Nov 14 15:20 aarch64-softmmu-config-devices.mak.d
-rw-r--r--  1 alex alex     7084 Nov 14 15:20 arm-softmmu-config-devices.mak.d
-rw-r--r--  1 alex alex  1877658 Nov 14 15:20 build.dat
-rw-r--r--  1 alex alex    27208 Nov 14 15:20 cleantrees.dat
drwxr-xr-x  3 alex alex     4096 Nov 14 15:20 __CMake_compiler_info__/
drwxr-xr-x  3 alex alex     4096 Nov 14 15:20 cmake_libcbor/
-rw-r--r--  1 alex alex      162 Nov 14 15:20 cmd_line.txt
-rw-r--r--  1 alex alex   333651 Nov 14 15:20 coredata.dat
-rw-r--r--  1 alex alex    24920 Nov 14 15:20 install.dat
-rw-r--r--  1 alex alex 19049522 Nov 14 15:20 libsanity.a
-rw-r--r--  1 alex alex     1748 Nov 14 15:20 meson_benchmark_setup.dat
-rw-r--r--  1 alex alex        0 Nov 14 15:20 meson.lock
-rw-r--r--  1 alex alex   140166 Nov 14 15:20 meson_test_setup.dat
-rwxr-xr-x  1 alex alex  3826912 Nov 14 15:20 rusttest*
-rw-r--r--  1 alex alex       46 Nov 14 15:20 sanitycheckc.c
-rwxr-xr-x  1 alex alex    15840 Nov 14 15:20 sanitycheckc.exe*
-rw-r--r--  1 alex alex       30 Nov 14 15:20 sanity.rs
🕙15:21:27 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] 
➜  meson devenv ../../rust

ERROR: Build data file './meson-private/build.dat' references functions or classes that don't exist. This probably means that it was generated with an old version of meson. Try running from the source directory meson setup . --wipe
🕙15:21:43 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] [🔴 ERROR] 
✗  

>
> Paolo

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Paolo Bonzini 1 week, 2 days ago
On 11/14/24 16:22, Alex Bennée wrote:
> ERROR: Build data file './meson-private/build.dat' references functions or classes that don't exist. This probably means that it was generated with an old version of meson. Try running from the source directory meson setup . --wipe
> 
> I also tried a wipe and re-configure but the same thing.

Ah, nevermind - you must have an older meson installation in /usr.  You 
have to do pyvenv/bin/meson to pick the right one.  I'll adjust the docs.

Paolo


Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Alex Bennée 1 week, 2 days ago
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 11/14/24 16:22, Alex Bennée wrote:
>> ERROR: Build data file './meson-private/build.dat' references
>> functions or classes that don't exist. This probably means that it
>> was generated with an old version of meson. Try running from the
>> source directory meson setup . --wipe
>> I also tried a wipe and re-configure but the same thing.
>
> Ah, nevermind - you must have an older meson installation in /usr.
> You have to do pyvenv/bin/meson to pick the right one.  I'll adjust
> the docs.

Hmm,

✗  ./pyvenv/bin/meson devenv ../../rust
Traceback (most recent call last):
  File "/home/alex/lsrc/qemu.git/builds/rust/pyvenv/lib/python3.11/site-packages/mesonbuild/mesonmain.py", line 188, in run
    return options.run_func(options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/lsrc/qemu.git/builds/rust/pyvenv/lib/python3.11/site-packages/mesonbuild/mdevenv.py", line 228, in run
    return subprocess.call(args, close_fds=False,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 389, in call
    with Popen(*popenargs, **kwargs) as p:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 1024, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.11/subprocess.py", line 1901, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '../../rust'

ERROR: Unhandled python OSError. This is probably not a Meson bug, but an issue with your build environment.
🕙17:26:59 alex@draig:qemu.git/builds/rust  on  review/rust-cargo-rfc [$!?] [🔴 13] 
✗  ls -l ../../rust/
total 40
-rw-r--r-- 1 alex alex 3237 Nov 12 21:01 Cargo.lock
-rw-r--r-- 1 alex alex 2426 Nov 14 12:15 Cargo.toml
drwxr-xr-x 3 alex alex 4096 Nov 11 23:19 hw/
-rw-r--r-- 1 alex alex   18 Nov 11 23:19 Kconfig
-rw-r--r-- 1 alex alex  437 Nov 14 12:15 meson.build
drwxr-xr-x 4 alex alex 4096 Nov 14 12:15 qemu-api/
drwxr-xr-x 3 alex alex 4096 Nov 14 12:15 qemu-api-macros/
-rw-r--r-- 1 alex alex  191 Nov 11 23:19 rustfmt.toml
drwxr-xr-x 4 alex alex 4096 Nov 14 15:18 target/
-rw-r--r-- 1 alex alex 2262 Nov 12 21:01 wrapper.h

>
> Paolo

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Paolo Bonzini 1 week, 1 day ago
On 11/14/24 18:27, Alex Bennée wrote:
> Paolo Bonzini <pbonzini@redhat.com> writes:
> 
>> On 11/14/24 16:22, Alex Bennée wrote:
>>> ERROR: Build data file './meson-private/build.dat' references
>>> functions or classes that don't exist. This probably means that it
>>> was generated with an old version of meson. Try running from the
>>> source directory meson setup . --wipe
>>> I also tried a wipe and re-configure but the same thing.
>>
>> Ah, nevermind - you must have an older meson installation in /usr.
>> You have to do pyvenv/bin/meson to pick the right one.  I'll adjust
>> the docs.
> 
> Hmm,
> 
> ✗  ./pyvenv/bin/meson devenv ../../rust
> PermissionError: [Errno 13] Permission denied: '../../rust'

You're confusing two things:

1) to start a shell

pyvenv/bin/meson devenv

2) to run clippy

pyvenv/bin/meson devenv -w ../../rust cargo clippy --tests

Note the -w.  Since the latter is typically covered by make, the common 
one will be the former.

Paolo


Re: [RFC PATCH 00/11] rust: improved integration with cargo
Posted by Alex Bennée 1 week, 1 day ago
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 11/14/24 18:27, Alex Bennée wrote:
>> Paolo Bonzini <pbonzini@redhat.com> writes:
>> 
>>> On 11/14/24 16:22, Alex Bennée wrote:
>>>> ERROR: Build data file './meson-private/build.dat' references
>>>> functions or classes that don't exist. This probably means that it
>>>> was generated with an old version of meson. Try running from the
>>>> source directory meson setup . --wipe
>>>> I also tried a wipe and re-configure but the same thing.
>>>
>>> Ah, nevermind - you must have an older meson installation in /usr.
>>> You have to do pyvenv/bin/meson to pick the right one.  I'll adjust
>>> the docs.
>> Hmm,
>> ✗  ./pyvenv/bin/meson devenv ../../rust
>> PermissionError: [Errno 13] Permission denied: '../../rust'
>
> You're confusing two things:
>
> 1) to start a shell
>
> pyvenv/bin/meson devenv
>
> 2) to run clippy
>
> pyvenv/bin/meson devenv -w ../../rust cargo clippy --tests
>
> Note the -w.  Since the latter is typically covered by make, the
> common one will be the former.


Ahh right - I misunderstood, got it now.

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro