summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-08-19 19:41:31 +0200
committerXavier Claessens <xclaesse@gmail.com>2025-10-23 16:20:22 +0100
commitb695f2d617fb965e364e8b05bd3f03490567782a (patch)
tree8954eaf7ee9ff2091f12e123a681c8cc7fb786a1
parent1e8ae5811d56662f701ef03013c2da0607e8f491 (diff)
downloadmeson-b695f2d617fb965e364e8b05bd3f03490567782a.tar.gz
cargo: convert proc_macro to crate_type in Library dataclass.
Do not look anymore at proc_macro after init, keeping crate_type as the sole source of truth about the desired crate types.
-rw-r--r--mesonbuild/cargo/interpreter.py2
-rw-r--r--mesonbuild/cargo/manifest.py5
-rw-r--r--unittests/cargotests.py25
3 files changed, 29 insertions, 3 deletions
diff --git a/mesonbuild/cargo/interpreter.py b/mesonbuild/cargo/interpreter.py
index 52c4816f8..fe0a2a591 100644
--- a/mesonbuild/cargo/interpreter.py
+++ b/mesonbuild/cargo/interpreter.py
@@ -597,7 +597,7 @@ class Interpreter:
depname = _dependency_name(pkg.manifest.package.name, pkg.manifest.package.api, depname_suffix)
lib: mparser.BaseNode
- if pkg.manifest.lib.proc_macro or crate_type == 'proc-macro':
+ if crate_type == 'proc-macro':
lib = build.method('proc_macro', build.identifier('rust'), posargs, kwargs)
else:
if crate_type in {'lib', 'rlib', 'staticlib'}:
diff --git a/mesonbuild/cargo/manifest.py b/mesonbuild/cargo/manifest.py
index 304ae7526..ad9c9943e 100644
--- a/mesonbuild/cargo/manifest.py
+++ b/mesonbuild/cargo/manifest.py
@@ -347,13 +347,14 @@ class Library(BuildTarget):
@classmethod
def from_raw(cls, raw: raw.LibTarget, pkg: Package) -> Self:
name = raw.get('name', fixup_meson_varname(pkg.name))
+ # If proc_macro is True, it takes precedence and sets crate_type to proc-macro
proc_macro = raw.get('proc-macro', False)
- crate_type = 'proc-macro' if proc_macro else 'lib'
return _raw_to_dataclass(raw, cls, f'Library entry {name}',
name=DefaultValue(name),
path=DefaultValue('src/lib.rs'),
edition=DefaultValue(pkg.edition),
- crate_type=DefaultValue([crate_type]))
+ crate_type=ConvertValue(lambda x: ['proc-macro'] if proc_macro else x,
+ ['lib']))
@dataclasses.dataclass
diff --git a/unittests/cargotests.py b/unittests/cargotests.py
index 22d77b35f..e9e779c1e 100644
--- a/unittests/cargotests.py
+++ b/unittests/cargotests.py
@@ -280,6 +280,18 @@ class CargoTomlTest(unittest.TestCase):
]
''')
+ CARGO_TOML_3 = textwrap.dedent('''\
+ [package]
+ name = "bits"
+ edition = "2021"
+ rust-version = "1.70"
+ version = "0.1.0"
+
+ [lib]
+ proc-macro = true
+ crate-type = ["lib"] # ignored
+ ''')
+
CARGO_TOML_WS = textwrap.dedent('''\
[workspace]
resolver = "2"
@@ -406,6 +418,19 @@ class CargoTomlTest(unittest.TestCase):
self.assertEqual(manifest.dev_dependencies['gir-format-check'].meson_version, ['>= 0.1', '< 0.2'])
self.assertEqual(manifest.dev_dependencies['gir-format-check'].api, '0.1')
+ def test_cargo_toml_proc_macro(self) -> None:
+ with tempfile.TemporaryDirectory() as tmpdir:
+ fname = os.path.join(tmpdir, 'Cargo.toml')
+ with open(fname, 'w', encoding='utf-8') as f:
+ f.write(self.CARGO_TOML_3)
+ manifest_toml = load_toml(fname)
+ manifest = Manifest.from_raw(manifest_toml, 'Cargo.toml')
+
+ self.assertEqual(manifest.lib.name, 'bits')
+ self.assertEqual(manifest.lib.crate_type, ['proc-macro'])
+ self.assertEqual(manifest.lib.path, 'src/lib.rs')
+ self.assertEqual(manifest.lib.proc_macro, True)
+
def test_cargo_toml_targets(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
fname = os.path.join(tmpdir, 'Cargo.toml')