summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-10-22 16:52:08 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2025-12-22 11:58:30 +0100
commit9752def1c96eb32dc37c32398a0fe8802392fbc9 (patch)
treec43f2d983c6b9273df80d0edf9560d3ab726927d
parentbfb5ea6fdbd85568c4ce20e28d4d727b78994938 (diff)
downloadmeson-9752def1c96eb32dc37c32398a0fe8802392fbc9.tar.gz
modules: rust: implement more package accessors
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--docs/markdown/Rust-module.md41
-rw-r--r--mesonbuild/modules/rust.py35
-rw-r--r--test cases/rust/31 rust.workspace package/Cargo.toml6
-rw-r--r--test cases/rust/31 rust.workspace package/meson.build11
-rw-r--r--test cases/rust/31 rust.workspace package/src/main.rs4
-rw-r--r--test cases/rust/31 rust.workspace package/subprojects/answer-2.1/meson.build2
-rw-r--r--test cases/rust/31 rust.workspace package/subprojects/hello-1.0/Cargo.toml5
-rw-r--r--test cases/rust/31 rust.workspace package/subprojects/hello-1.0/src/lib.rs6
-rw-r--r--test cases/rust/32 rust.workspace workspace/Cargo.toml6
-rw-r--r--test cases/rust/32 rust.workspace workspace/meson.build11
-rw-r--r--test cases/rust/32 rust.workspace workspace/src/main.rs4
-rw-r--r--test cases/rust/32 rust.workspace workspace/subprojects/answer-2.1/meson.build2
-rw-r--r--test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/Cargo.toml5
-rw-r--r--test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/src/lib.rs6
14 files changed, 134 insertions, 10 deletions
diff --git a/docs/markdown/Rust-module.md b/docs/markdown/Rust-module.md
index 99faeecfe..fbe65f1b6 100644
--- a/docs/markdown/Rust-module.md
+++ b/docs/markdown/Rust-module.md
@@ -258,6 +258,47 @@ Positional arguments:
The package object returned by `workspace.subproject()` provides methods
for working with individual packages in a Cargo workspace.
+### subproject.name()
+
+```meson
+name = pkg.name()
+```
+
+Returns the name of the subproject.
+
+### subproject.version()
+
+```meson
+version = pkg.version()
+```
+
+Returns the normalized version number of the subproject.
+
+### subproject.api()
+
+```meson
+api = pkg.api()
+```
+
+Returns the API version of the subproject, that is the version up to the first
+nonzero element.
+
+### subproject.features()
+
+```meson
+features = pkg.features()
+```
+
+Returns selected features for a specific subproject.
+
+### subproject.all_features()
+
+```meson
+all_features = pkg.all_features()
+```
+
+Returns all defined features for a specific subproject.
+
### subproject.dependency()
```meson
diff --git a/mesonbuild/modules/rust.py b/mesonbuild/modules/rust.py
index 4c61b7e31..29d1b1fbb 100644
--- a/mesonbuild/modules/rust.py
+++ b/mesonbuild/modules/rust.py
@@ -146,10 +146,45 @@ class RustSubproject(ModuleObject):
self.rust_ws = rust_ws
self.package = package
self.methods.update({
+ 'all_features': self.all_features_method,
+ 'api': self.api_method,
'dependency': self.dependency_method,
+ 'features': self.features_method,
+ 'name': self.name_method,
+ 'version': self.version_method,
})
@noPosargs
+ @noKwargs
+ def name_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> str:
+ """Returns the name of the package."""
+ return self.package.manifest.package.name
+
+ @noPosargs
+ @noKwargs
+ def api_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> str:
+ """Returns the API version of the package."""
+ return self.package.manifest.package.api
+
+ @noPosargs
+ @noKwargs
+ def version_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> str:
+ """Returns the version of the package."""
+ return self.package.manifest.package.version
+
+ @noPosargs
+ @noKwargs
+ def all_features_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> T.List[str]:
+ """Returns all features for specific package."""
+ return sorted(list(self.package.manifest.features.keys()))
+
+ @noPosargs
+ @noKwargs
+ def features_method(self, state: ModuleState, args: T.List, kwargs: TYPE_kwargs) -> T.List[str]:
+ """Returns chosen features for specific package."""
+ return sorted(list(self.package.cfg.features))
+
+ @noPosargs
@typed_kwargs('package.dependency',
KwargInfo('rust_abi', (str, NoneType), default=None, validator=in_set_validator({'rust', 'c', 'proc-macro'})))
def dependency_method(self, state: ModuleState, args: T.List, kwargs: FuncDependency) -> Dependency:
diff --git a/test cases/rust/31 rust.workspace package/Cargo.toml b/test cases/rust/31 rust.workspace package/Cargo.toml
index 53bc49528..00bb0878e 100644
--- a/test cases/rust/31 rust.workspace package/Cargo.toml
+++ b/test cases/rust/31 rust.workspace package/Cargo.toml
@@ -4,8 +4,10 @@ version = "0.1.0"
edition = "2021"
[features]
-default = ["dep:answer"]
+default = ["feature1", "hello?/goodbye"]
+feature1 = ["answer/large", "dep:hello"]
+feature2 = []
[dependencies]
-hello = { version = "1.0", path = "subprojects/hello-1.0" }
+hello = { version = "1.0", path = "subprojects/hello-1.0", optional = true }
answer = { version = "2.1", path = "subprojects/answer-2.1", optional = true }
diff --git a/test cases/rust/31 rust.workspace package/meson.build b/test cases/rust/31 rust.workspace package/meson.build
index 5885a4524..d6e9aa42d 100644
--- a/test cases/rust/31 rust.workspace package/meson.build
+++ b/test cases/rust/31 rust.workspace package/meson.build
@@ -7,7 +7,18 @@ cargo_ws = rust.workspace()
assert(cargo_ws.packages() == ['answer', 'hello', 'package_test'])
hello_rs = cargo_ws.subproject('hello')
+assert(hello_rs.name() == 'hello')
+assert(hello_rs.version() == '1.0.0')
+assert(hello_rs.api() == '1')
+assert(hello_rs.all_features() == ['default', 'goodbye'])
+assert(hello_rs.features() == ['default', 'goodbye'])
+
answer_rs = cargo_ws.subproject('answer', '2')
+assert(answer_rs.name() == 'answer')
+assert(answer_rs.version() == '2.1.0')
+assert(answer_rs.api() == '2')
+assert(answer_rs.all_features() == ['default', 'large'])
+assert(answer_rs.features() == ['default', 'large'])
e = executable('package-test', 'src/main.rs',
dependencies: [hello_rs.dependency(), answer_rs.dependency()],
diff --git a/test cases/rust/31 rust.workspace package/src/main.rs b/test cases/rust/31 rust.workspace package/src/main.rs
index 39b324801..13c02dd64 100644
--- a/test cases/rust/31 rust.workspace package/src/main.rs
+++ b/test cases/rust/31 rust.workspace package/src/main.rs
@@ -1,6 +1,8 @@
-use hello::greet;
+use hello::{farewell, greet};
fn main() {
println!("{}", greet());
+ println!("{}", farewell());
println!("{}", answer::answer());
+ println!("{}", answer::large_answer());
}
diff --git a/test cases/rust/31 rust.workspace package/subprojects/answer-2.1/meson.build b/test cases/rust/31 rust.workspace package/subprojects/answer-2.1/meson.build
index 7b4e10b23..e8d10117f 100644
--- a/test cases/rust/31 rust.workspace package/subprojects/answer-2.1/meson.build
+++ b/test cases/rust/31 rust.workspace package/subprojects/answer-2.1/meson.build
@@ -4,6 +4,6 @@ rust = import('rust')
cargo_ws = rust.workspace()
assert(cargo_ws.packages() == ['answer'])
-l = static_library('answer', 'src/lib.rs')
+l = static_library('answer', 'src/lib.rs', rust_args: ['--cfg', 'feature="large"'])
dep = declare_dependency(link_with: l)
meson.override_dependency('answer-2-rs', dep)
diff --git a/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/Cargo.toml b/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/Cargo.toml
index ad0ae458c..f6ab8eb91 100644
--- a/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/Cargo.toml
+++ b/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/Cargo.toml
@@ -4,4 +4,7 @@ version = "1.0.0"
edition = "2021"
[lib]
-crate-type = ["lib"] \ No newline at end of file
+crate-type = ["lib"]
+
+[features]
+goodbye = []
diff --git a/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/src/lib.rs b/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/src/lib.rs
index 0631292f3..47346350b 100644
--- a/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/src/lib.rs
+++ b/test cases/rust/31 rust.workspace package/subprojects/hello-1.0/src/lib.rs
@@ -2,3 +2,9 @@ pub fn greet() -> &'static str
{
"hello world"
}
+
+#[cfg(feature = "goodbye")]
+pub fn farewell() -> &'static str
+{
+ "goodbye"
+}
diff --git a/test cases/rust/32 rust.workspace workspace/Cargo.toml b/test cases/rust/32 rust.workspace workspace/Cargo.toml
index ac3a340bd..44e4a18ef 100644
--- a/test cases/rust/32 rust.workspace workspace/Cargo.toml
+++ b/test cases/rust/32 rust.workspace workspace/Cargo.toml
@@ -7,8 +7,10 @@ version = "0.1.0"
edition = "2021"
[features]
-default = ["dep:answer"]
+default = ["feature1", "hello?/goodbye"]
+feature1 = ["answer/large", "dep:hello"]
+feature2 = []
[dependencies]
-hello = { version = "1.0", path = "subprojects/hello-1.0" }
+hello = { version = "1.0", path = "subprojects/hello-1.0", optional = true }
answer = { version = "2.1", path = "subprojects/answer-2.1", optional = true }
diff --git a/test cases/rust/32 rust.workspace workspace/meson.build b/test cases/rust/32 rust.workspace workspace/meson.build
index 77d0458a9..2626d3d50 100644
--- a/test cases/rust/32 rust.workspace workspace/meson.build
+++ b/test cases/rust/32 rust.workspace workspace/meson.build
@@ -7,7 +7,18 @@ cargo_ws = rust.workspace()
assert(cargo_ws.packages() == ['answer', 'hello', 'workspace_test'])
hello_rs = cargo_ws.subproject('hello')
+assert(hello_rs.name() == 'hello')
+assert(hello_rs.version() == '1.0.0')
+assert(hello_rs.api() == '1')
+assert(hello_rs.all_features() == ['default', 'goodbye'])
+assert(hello_rs.features() == ['default', 'goodbye'])
+
answer_rs = cargo_ws.subproject('answer', '2')
+assert(answer_rs.name() == 'answer')
+assert(answer_rs.version() == '2.1.0')
+assert(answer_rs.api() == '2')
+assert(answer_rs.all_features() == ['default', 'large'])
+assert(answer_rs.features() == ['default', 'large'])
e = executable('workspace-test', 'src/main.rs',
dependencies: [hello_rs.dependency(), answer_rs.dependency()],
diff --git a/test cases/rust/32 rust.workspace workspace/src/main.rs b/test cases/rust/32 rust.workspace workspace/src/main.rs
index 39b324801..13c02dd64 100644
--- a/test cases/rust/32 rust.workspace workspace/src/main.rs
+++ b/test cases/rust/32 rust.workspace workspace/src/main.rs
@@ -1,6 +1,8 @@
-use hello::greet;
+use hello::{farewell, greet};
fn main() {
println!("{}", greet());
+ println!("{}", farewell());
println!("{}", answer::answer());
+ println!("{}", answer::large_answer());
}
diff --git a/test cases/rust/32 rust.workspace workspace/subprojects/answer-2.1/meson.build b/test cases/rust/32 rust.workspace workspace/subprojects/answer-2.1/meson.build
index 7b4e10b23..e8d10117f 100644
--- a/test cases/rust/32 rust.workspace workspace/subprojects/answer-2.1/meson.build
+++ b/test cases/rust/32 rust.workspace workspace/subprojects/answer-2.1/meson.build
@@ -4,6 +4,6 @@ rust = import('rust')
cargo_ws = rust.workspace()
assert(cargo_ws.packages() == ['answer'])
-l = static_library('answer', 'src/lib.rs')
+l = static_library('answer', 'src/lib.rs', rust_args: ['--cfg', 'feature="large"'])
dep = declare_dependency(link_with: l)
meson.override_dependency('answer-2-rs', dep)
diff --git a/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/Cargo.toml b/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/Cargo.toml
index ad0ae458c..f6ab8eb91 100644
--- a/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/Cargo.toml
+++ b/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/Cargo.toml
@@ -4,4 +4,7 @@ version = "1.0.0"
edition = "2021"
[lib]
-crate-type = ["lib"] \ No newline at end of file
+crate-type = ["lib"]
+
+[features]
+goodbye = []
diff --git a/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/src/lib.rs b/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/src/lib.rs
index 0631292f3..47346350b 100644
--- a/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/src/lib.rs
+++ b/test cases/rust/32 rust.workspace workspace/subprojects/hello-1.0/src/lib.rs
@@ -2,3 +2,9 @@ pub fn greet() -> &'static str
{
"hello world"
}
+
+#[cfg(feature = "goodbye")]
+pub fn farewell() -> &'static str
+{
+ "goodbye"
+}