Skip to content

Commit

Permalink
cargo: Fix feature resolution
Browse files Browse the repository at this point in the history
Introduce a global Cargo interpreter state that keeps track of enabled
features on each crate.

Before generating AST of a Cargo subproject, it downloads every
sub-subproject and resolves the set of features enabled on each of them
recursively. When it later generates AST for one its dependencies, its
set of features and dependencies is already determined.
  • Loading branch information
xclaesse committed Jun 17, 2024
1 parent c199faf commit c90696a
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 339 deletions.
21 changes: 0 additions & 21 deletions docs/markdown/Wrap-dependency-system-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,6 @@ method = cargo
dependency_names = foo-bar-0.1-rs
```

Cargo features are exposed as Meson boolean options, with the `feature-` prefix.
For example the `default` feature is named `feature-default` and can be set from
the command line with `-Dfoo-1-rs:feature-default=false`. When a cargo subproject
depends on another cargo subproject, it will automatically enable features it
needs using the `dependency('foo-1-rs', default_options: ...)` mechanism. However,
unlike Cargo, the set of enabled features is not managed globally. Let's assume
the main project depends on `foo-1-rs` and `bar-1-rs`, and they both depend on
`common-1-rs`. The main project will first look up `foo-1-rs` which itself will
configure `common-rs` with a set of features. Later, when `bar-1-rs` does a lookup
for `common-1-rs` it has already been configured and the set of features cannot be
changed. If `bar-1-rs` wants extra features from `common-1-rs`, Meson will error out.
It is currently the responsability of the main project to resolve those
issues by enabling extra features on each subproject:
```meson
project(...,
default_options: {
'common-1-rs:feature-something': true,
},
)
```

In addition, if the file `meson/meson.build` exists, Meson will call `subdir('meson')`
where the project can add manual logic that would usually be part of `build.rs`.
Some naming conventions need to be respected:
Expand Down
14 changes: 14 additions & 0 deletions docs/markdown/snippets/cargo_features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Cargo features are resolved globally

When configuring a Cargo dependency, Meson will now resolve its complete
dependency tree and feature set before generating the subproject AST.
This solves many cases of Cargo subprojects being configured with missing
features that the main project had to enable by hand using e.g.
`default_options: ['foo-rs:feature-default=true']`.

Note that there could still be issues in the case there are multiple Cargo
entry points. That happens if the main Meson project makes multiple `dependency()`
calls for different Cargo crates that have common dependencies.

Breaks: This change removes per feature Meson option That were previously
possible to set as show above or from command line `-Dfoo-rs:feature-foo=true`.
4 changes: 2 additions & 2 deletions mesonbuild/cargo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = [
'interpret',
'Interpreter',
'load_wraps',
]

from .interpreter import interpret, load_wraps
from .interpreter import Interpreter, load_wraps
Loading

0 comments on commit c90696a

Please sign in to comment.