Skip to content

build: add infrastructure for rust (v2) #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

dvdhrm
Copy link
Member

@dvdhrm dvdhrm commented May 9, 2025

Extend the Meson build system to compile an internal Rust library and link that into libbus.a, our internal utility library that is linked into all dbus-broker binaries.

The individual commits have extensive information on, and background for, the individual changes. To summarize, we now use the builtin Rust support of Meson to compile a static library that uses no external dependencies but standard Rust core and alloc, as well as the runtime-integration of std. This avoids the previous dance via meson-cargo, and it makes the build fully independent of Cargo.

This series merely extends the build system, but adds no meaningful Rust code. This will be added in other PRs.

(v2: rebased on the recent CI changes)

dvdhrm added 8 commits May 9, 2025 16:57
Extend the build system to compile Rust sources in our tree into an
internal, static library. This is then linked into `libbus.a`, which
itself is our static library that contains everything but the
entrypoints.

This does not add any Rust code, but merely extends the build-system to
support adding Rust code.

Additionally, we add infrastructure to call `rust-bindgen` on our own C
headers, so we get clean access to it from Rust.

Note that this significantly bumps the build-time requirements of the
project:

- Meson 1.7.0 (January 2025): Needed for support of Rust Edition 2024
- Rust 1.85 (February 2025): Needed for support of Rust Edition 2024
- rust-bindgen 0.71 (December 2024): Needed for `--rust-edition` and
  `--rust-target` configuration.

Signed-off-by: David Rheinsberg <[email protected]>
Instead of one static library with all Rust code, split it into two:

1) An rlib Rust library with all Rust code.
2) A staticlib Rust library that links the rlib and provides C stubs.

This split allows us to link the Rust library to other Rust code, like
doctests or unittests. It also keeps the C-API separate from the Rust
code and provides a clean path forward.

Signed-off-by: David Rheinsberg <[email protected]>
Export the generated symbols from our rlib, to ensure the C API can use
them as well. While at it, ensure we suppress common warnings with the C
naming scheme we use.

Signed-off-by: David Rheinsberg <[email protected]>
Ensure that we do not use assignments in the bindgen options. Meson
parses these options to avoid adding duplicates, so lets provide them
cleanly.

Additionally, add `--use-core` now that we have rlib as `no_std`.

Signed-off-by: David Rheinsberg <[email protected]>
Ensure that we generate doctest targets with the new Meson version. This
will test-run all Rust code in the documentation.

Signed-off-by: David Rheinsberg <[email protected]>
With everything settled, we can now reduce the build requirements and
turn some features into optional additions:

- Use Rust-2021 over Rust-2024, since we do not benefit much from the
  new edition.

- Reduce meson requirements to 1.3 and guard `mod_rust` additions by a
  version check.

- Reduce Rust MSV to 1.74.

- Reduce Bindgen MSV to 0.60 to at least get --allowlist-file.

Signed-off-by: David Rheinsberg <[email protected]>
Use plain version numbers in MSVs and require all call-sites to add
comparators, if necessary.

Signed-off-by: David Rheinsberg <[email protected]>
Ensure that we do not run doctests when cross-compiling. This can easily
fail and there is no suitable way for us to check whether `rustdoc` is
available:

    mesonbuild/meson#14583

Signed-off-by: David Rheinsberg <[email protected]>
@evverx
Copy link
Contributor

evverx commented May 10, 2025

Looking at https://github.com/bus1/dbus-broker/actions/runs/14931302421 it fails to build with the OSS-Fuzz toolchain as expected. It should be possible to switch to the rust builder image and install bindgen-cli there to get it to work for the time being but generally projects written in both C and Rust built with meson aren't natively supported there. I wonder if there are plans to keep fuzzing the C code and start fuzzing the fuzzable Rust code once it's introduced or should it be disabled on OSS-Fuzz until it's all settled?

evverx added a commit to evverx/oss-fuzz that referenced this pull request May 10, 2025
@bonzini
Copy link

bonzini commented May 12, 2025

The change in bc02e79 should not be needed in 1.8.1, hopefully, so maybe just change the version comparison once the release is out.

mod_rust.doctest(
'rbus-doctests',
rust_bus,
suite: 'unit',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using a separate suite here. I'm not sure how your CI works, but some projects do not copy over the individual .o files from the build stage to the test stage. That may prevent you from running the doctests in the test stage, and having a separate suite helps moving them to the build stage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants