Skip to content

Commit 41f55fe

Browse files
committed
modules: rust: implement more package accessors
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 8650355 commit 41f55fe

File tree

14 files changed

+134
-10
lines changed

14 files changed

+134
-10
lines changed

docs/markdown/Rust-module.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,47 @@ Positional arguments:
229229
The package object returned by `workspace.subproject()` provides methods
230230
for working with individual packages in a Cargo workspace.
231231

232+
### subproject.name()
233+
234+
```meson
235+
name = pkg.name()
236+
```
237+
238+
Returns the name of the subproject.
239+
240+
### subproject.version()
241+
242+
```meson
243+
version = pkg.version()
244+
```
245+
246+
Returns the normalized version number of the subproject.
247+
248+
### subproject.api()
249+
250+
```meson
251+
api = pkg.api()
252+
```
253+
254+
Returns the API version of the subproject, that is the version up to the first
255+
nonzero element.
256+
257+
### subproject.features()
258+
259+
```meson
260+
features = pkg.features()
261+
```
262+
263+
Returns selected features for a specific subproject.
264+
265+
### subproject.all_features()
266+
267+
```meson
268+
all_features = pkg.all_features()
269+
```
270+
271+
Returns all defined features for a specific subproject.
272+
232273
### subproject.dependency()
233274

234275
```meson

mesonbuild/modules/rust.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,44 @@ def __init__(self, rust_ws: RustWorkspace, package: cargo.PackageState) -> None:
146146
self.rust_ws = rust_ws
147147
self.package = package
148148
self.methods.update({
149+
'all_features': self.all_features_method,
150+
'api': self.api_method,
149151
'dependency': self.dependency_method,
152+
'features': self.features_method,
153+
'name': self.name_method,
154+
'version': self.version_method,
150155
})
151156

157+
@noPosargs
158+
@noKwargs
159+
def name_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> str:
160+
"""Returns the name of the package."""
161+
return self.package.manifest.package.name
162+
163+
@noPosargs
164+
@noKwargs
165+
def api_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> str:
166+
"""Returns the API version of the package."""
167+
return self.package.manifest.package.api
168+
169+
@noPosargs
170+
@noKwargs
171+
def version_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> str:
172+
"""Returns the version of the package."""
173+
return self.package.manifest.package.version
174+
175+
@noPosargs
176+
@noKwargs
177+
def all_features_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> T.List[str]:
178+
"""Returns all features for specific package."""
179+
return sorted(list(self.package.manifest.features.keys()))
180+
181+
@noPosargs
182+
@noKwargs
183+
def features_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> T.List[str]:
184+
"""Returns chosen features for specific package."""
185+
return sorted(list(self.package.cfg.features))
186+
152187
@noPosargs
153188
@typed_kwargs('package.dependency',
154189
KwargInfo('rust_abi', (str, NoneType), default=None, validator=in_set_validator({'rust', 'c', 'proc-macro'})))

test cases/rust/31 rust.workspace package/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ version = "0.1.0"
44
edition = "2021"
55

66
[features]
7-
default = ["dep:answer"]
7+
default = ["feature1", "hello?/goodbye"]
8+
feature1 = ["answer/large", "dep:hello"]
9+
feature2 = []
810

911
[dependencies]
10-
hello = { version = "1.0", path = "subprojects/hello-1.0" }
12+
hello = { version = "1.0", path = "subprojects/hello-1.0", optional = true }
1113
answer = { version = "2.1", path = "subprojects/answer-2.1", optional = true }

test cases/rust/31 rust.workspace package/meson.build

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ cargo = rust.workspace()
77
assert(cargo.packages() == ['answer', 'hello', 'package_test'])
88

99
hello_rs = cargo.subproject('hello')
10+
assert(hello_rs.name() == 'hello')
11+
assert(hello_rs.version() == '1.0.0')
12+
assert(hello_rs.api() == '1')
13+
assert(hello_rs.all_features() == ['default', 'goodbye'])
14+
assert(hello_rs.features() == ['default', 'goodbye'])
15+
1016
answer_rs = cargo.subproject('answer', '2')
17+
assert(answer_rs.name() == 'answer')
18+
assert(answer_rs.version() == '2.1.0')
19+
assert(answer_rs.api() == '2')
20+
assert(answer_rs.all_features() == ['default', 'large'])
21+
assert(answer_rs.features() == ['default', 'large'])
1122

1223
e = executable('package-test', 'src/main.rs',
1324
dependencies: [hello_rs.dependency(), answer_rs.dependency()],
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use hello::greet;
1+
use hello::{farewell, greet};
22

33
fn main() {
44
println!("{}", greet());
5+
println!("{}", farewell());
56
println!("{}", answer::answer());
7+
println!("{}", answer::large_answer());
68
}

test cases/rust/31 rust.workspace package/subprojects/answer-2.1/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ rust = import('rust')
44
cargo = rust.workspace()
55
assert(cargo.packages() == ['answer'])
66

7-
l = static_library('answer', 'src/lib.rs')
7+
l = static_library('answer', 'src/lib.rs', rust_args: ['--cfg', 'feature="large"'])
88
dep = declare_dependency(link_with: l)
99
meson.override_dependency('answer-2-rs', dep)

test cases/rust/31 rust.workspace package/subprojects/hello-1.0/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ version = "1.0.0"
44
edition = "2021"
55

66
[lib]
7-
crate-type = ["lib"]
7+
crate-type = ["lib"]
8+
9+
[features]
10+
goodbye = []

test cases/rust/31 rust.workspace package/subprojects/hello-1.0/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ pub fn greet() -> &'static str
22
{
33
"hello world"
44
}
5+
6+
#[cfg(feature = "goodbye")]
7+
pub fn farewell() -> &'static str
8+
{
9+
"goodbye"
10+
}

test cases/rust/32 rust.workspace workspace/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ version = "0.1.0"
77
edition = "2021"
88

99
[features]
10-
default = ["dep:answer"]
10+
default = ["feature1", "hello?/goodbye"]
11+
feature1 = ["answer/large", "dep:hello"]
12+
feature2 = []
1113

1214
[dependencies]
13-
hello = { version = "1.0", path = "subprojects/hello-1.0" }
15+
hello = { version = "1.0", path = "subprojects/hello-1.0", optional = true }
1416
answer = { version = "2.1", path = "subprojects/answer-2.1", optional = true }

test cases/rust/32 rust.workspace workspace/meson.build

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ cargo = rust.workspace()
77
assert(cargo.packages() == ['answer', 'hello', 'workspace_test'])
88

99
hello_rs = cargo.subproject('hello')
10+
assert(hello_rs.name() == 'hello')
11+
assert(hello_rs.version() == '1.0.0')
12+
assert(hello_rs.api() == '1')
13+
assert(hello_rs.all_features() == ['default', 'goodbye'])
14+
assert(hello_rs.features() == ['default', 'goodbye'])
15+
1016
answer_rs = cargo.subproject('answer', '2')
17+
assert(answer_rs.name() == 'answer')
18+
assert(answer_rs.version() == '2.1.0')
19+
assert(answer_rs.api() == '2')
20+
assert(answer_rs.all_features() == ['default', 'large'])
21+
assert(answer_rs.features() == ['default', 'large'])
1122

1223
e = executable('workspace-test', 'src/main.rs',
1324
dependencies: [hello_rs.dependency(), answer_rs.dependency()],

0 commit comments

Comments
 (0)