mbox series

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

Message ID 20241108180139.117112-1-pbonzini@redhat.com (mailing list archive)
Headers show
Series rust: improved integration with cargo | expand

Message

Paolo Bonzini Nov. 8, 2024, 6:01 p.m. UTC
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

Comments

Alex Bennée Nov. 14, 2024, 1:07 p.m. UTC | #1
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

Paolo Bonzini Nov. 14, 2024, 1:11 p.m. UTC | #2
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
> 
Alex Bennée Nov. 14, 2024, 3:22 p.m. UTC | #3
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
>> 
Paolo Bonzini Nov. 14, 2024, 3:38 p.m. UTC | #4
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
Alex Bennée Nov. 14, 2024, 5:27 p.m. UTC | #5
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.

Paolo Bonzini Nov. 14, 2024, 6:18 p.m. UTC | #6
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
Alex Bennée Nov. 14, 2024, 9:13 p.m. UTC | #7
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.