summaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-10-24 10:24:45 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2025-12-22 11:59:07 +0100
commit7a1aa179087a274d6f6b13c2c666043ac9760ea5 (patch)
treeb9bf5eed53e6a3ff41dee74b6429ef2916dae37a /mesonbuild
parent9752def1c96eb32dc37c32398a0fe8802392fbc9 (diff)
downloadmeson-7a1aa179087a274d6f6b13c2c666043ac9760ea5.tar.gz
modules: rust: implement workspace.package()
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 <pbonzini@redhat.com>
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/cargo/interpreter.py15
-rw-r--r--mesonbuild/modules/rust.py29
2 files changed, 41 insertions, 3 deletions
diff --git a/mesonbuild/cargo/interpreter.py b/mesonbuild/cargo/interpreter.py
index 30639f526..dc5cdef2a 100644
--- a/mesonbuild/cargo/interpreter.py
+++ b/mesonbuild/cargo/interpreter.py
@@ -298,6 +298,21 @@ class Interpreter:
for feature in self.features:
self._enable_feature(pkg, feature)
+ def load_package(self, ws: WorkspaceState, package_name: T.Optional[str]) -> PackageState:
+ if package_name is None:
+ if not ws.workspace.root_package:
+ raise MesonException('no root package in workspace')
+ path = '.'
+ else:
+ try:
+ path = ws.packages_to_member[package_name]
+ except KeyError:
+ raise MesonException(f'workspace member "{package_name}" not found')
+
+ if is_parent_path(self.subprojects_dir, path):
+ raise MesonException('argument to package() cannot be a subproject')
+ return ws.packages[path]
+
def interpret(self, subdir: str, project_root: T.Optional[str] = None) -> mparser.CodeBlockNode:
filename = os.path.join(self.environment.source_dir, subdir, 'Cargo.toml')
build = builder.Builder(filename)
diff --git a/mesonbuild/modules/rust.py b/mesonbuild/modules/rust.py
index 29d1b1fbb..186774413 100644
--- a/mesonbuild/modules/rust.py
+++ b/mesonbuild/modules/rust.py
@@ -101,6 +101,7 @@ class RustWorkspace(ModuleObject):
self.ws = ws
self.methods.update({
'packages': self.packages_method,
+ 'package': self.package_method,
'subproject': self.subproject_method,
})
@@ -111,6 +112,12 @@ class RustWorkspace(ModuleObject):
package_names = [pkg.manifest.package.name for pkg in self.ws.packages.values()]
return sorted(package_names)
+ @typed_pos_args('workspace.package', optargs=[str])
+ def package_method(self, state: 'ModuleState', args: T.List, kwargs: TYPE_kwargs) -> RustPackage:
+ """Returns a package object."""
+ package_name = args[0] if args else None
+ return RustPackage(self, self.interpreter.cargo.load_package(self.ws, package_name))
+
def _do_subproject(self, pkg: cargo.PackageState) -> None:
kw: _kwargs.DoSubproject = {
'required': True,
@@ -138,8 +145,8 @@ class RustWorkspace(ModuleObject):
return RustSubproject(self, pkg)
-class RustSubproject(ModuleObject):
- """Represents a Rust package within a workspace."""
+class RustCrate(ModuleObject):
+ """Abstract base class for Rust crate representations."""
def __init__(self, rust_ws: RustWorkspace, package: cargo.PackageState) -> None:
super().__init__()
@@ -148,7 +155,6 @@ class RustSubproject(ModuleObject):
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,
@@ -184,6 +190,23 @@ class RustSubproject(ModuleObject):
"""Returns chosen features for specific package."""
return sorted(list(self.package.cfg.features))
+
+class RustPackage(RustCrate):
+ """Represents a Rust package within a workspace."""
+
+ def __init__(self, rust_ws: RustWorkspace, package: cargo.PackageState) -> None:
+ super().__init__(rust_ws, package)
+
+
+class RustSubproject(RustCrate):
+ """Represents a Cargo subproject."""
+
+ def __init__(self, rust_ws: RustWorkspace, package: cargo.PackageState) -> None:
+ super().__init__(rust_ws, package)
+ self.methods.update({
+ 'dependency': self.dependency_method,
+ })
+
@noPosargs
@typed_kwargs('package.dependency',
KwargInfo('rust_abi', (str, NoneType), default=None, validator=in_set_validator({'rust', 'c', 'proc-macro'})))