Skip to content

Commit 8ac0906

Browse files
committed
modules: rust: implement more package accessors
Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c011611 commit 8ac0906

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
@@ -258,6 +258,47 @@ Positional arguments:
258258
The package object returned by `workspace.subproject()` provides methods
259259
for working with individual packages in a Cargo workspace.
260260

261+
### subproject.name()
262+
263+
```meson
264+
name = pkg.name()
265+
```
266+
267+
Returns the name of the subproject.
268+
269+
### subproject.version()
270+
271+
```meson
272+
version = pkg.version()
273+
```
274+
275+
Returns the normalized version number of the subproject.
276+
277+
### subproject.api()
278+
279+
```meson
280+
api = pkg.api()
281+
```
282+
283+
Returns the API version of the subproject, that is the version up to the first
284+
nonzero element.
285+
286+
### subproject.features()
287+
288+
```meson
289+
features = pkg.features()
290+
```
291+
292+
Returns selected features for a specific subproject.
293+
294+
### subproject.all_features()
295+
296+
```meson
297+
all_features = pkg.all_features()
298+
```
299+
300+
Returns all defined features for a specific subproject.
301+
261302
### subproject.dependency()
262303

263304
```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)