Skip to content

Conversation

@bonzini
Copy link
Collaborator

@bonzini bonzini commented Nov 8, 2025

For the spec see #14639.

This adds a Cargo package object that can provide rustc arguments and dependencies&mdashbut also nice shortcuts pkg.executable(), pkg.library(), pkg.proc_macro(), pkg.shared_module() that can basically reduce a Rust crate to just:

project('something', 'rust')
cargo = import('rust').workspace()
cargo.package().executable(install: true)

Based on #15158.
Fixes: #14639

@jpakkane
Copy link
Member

Merge branch 'master' into cargo-object-full

Hmmm?

@bonzini
Copy link
Collaborator Author

bonzini commented Nov 13, 2025

Hmmm?

It's a draft. :) it will go away when I rebase before making it non-draft. I often have merge commits locally when something builds on top of other PRs.

@bonzini bonzini force-pushed the cargo-object-full branch 3 times, most recently from 8d09d29 to 0056c4c Compare November 19, 2025 18:01
It should be added even if unexpected_cfgs is not part of Cargo.toml.

Signed-off-by: Paolo Bonzini <[email protected]>
Make the implicit `xyz = ["dep:xyz"]` declaration explicit in the Manifest.
This also makes it possible to implement correctly the part of the spec where
"If you specify the optional dependency with the dep: prefix anywhere in
the [features] table, that disables the implicit feature."

Unfortunately, this can only be done after target-specific configurations
are resolved, which makes it hard to write a unit test for this.  Rustix
requires it, though; without this patch, something like

        [package]
        name = "rustix"
        edition = "2021"
        rust-version = "1.63"
        version = "0.38.34"

        [dependencies]
        alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
        libc = { version = "0.2.0", optional = true, package = "libc" }
        libc_errno = { version = "0.3.8", optional = true, package = "errno" }

        [features]
        alloc = []
        default = ["std"]
        rustc-dep-of-std = ["dep:alloc"]
        std = ["alloc"]
        use-libc = ["libc_errno", "libc"]

would incorrectly request the rustc-std-workspace-alloc crate via the
chain default->std->alloc.  The patch blocks it because it finds the
"dep:alloc" dependency of feature rustc-dep-of-std.

Signed-off-by: Paolo Bonzini <[email protected]>
Some projects may want to override Meson's AST generation for Cargo
projects.  This was not really doable before without hard coding the
results of feature resolution; however, now it will be possible by
accessing the results of the global feature resolution from the Rust
module's workspace object.

At the same time, the subproject must keep using the Cargo method,
which is forced by the workspace object's subproject() method,
because otherwise the interpreter is not propagated.  So just
skip the interpretation phase if a Meson.build is present.

Signed-off-by: Paolo Bonzini <[email protected]>
rust.workspace() is the entry point for global feature resolution.
It loads a Cargo.toml file and ensures that all dependencies will be
built with the correct set of features.

Fixes: mesonbuild#13404
Add features property to cargo.Interpreter to make default features configurable;
customization of which features are enabled by default is triggered by
rust.workspace().

Fixes: mesonbuild#14290
This is up to 2x faster because it avoids checks for pkg-config and cmake.

Signed-off-by: Paolo Bonzini <[email protected]>
Note that, as shown in the testcase, package() works in the subproject
as well.  This means that in the future the Cargo code generator can be
changed to reduce the amount of generated code and instead rely on the
package object.

Signed-off-by: Paolo Bonzini <[email protected]>
…rgets

Add rustc_args(), env(), and rust_dependency_map() methods to the
RustPackage class.  They simply delegate to PackageState and
PackageConfiguration.
Move the logic for system dependencies outside Cargo.interpreter and
into the rust module, so that it can be reused by the workspace object.

Signed-off-by: Paolo Bonzini <[email protected]>
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.

Proposal: rust.workspace() Cargo integration

2 participants