summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mesonbuild/cargo/interpreter.py22
-rw-r--r--mesonbuild/cargo/manifest.py18
-rw-r--r--mesonbuild/compilers/rust.py1
-rw-r--r--unittests/cargotests.py17
4 files changed, 58 insertions, 0 deletions
diff --git a/mesonbuild/cargo/interpreter.py b/mesonbuild/cargo/interpreter.py
index 52da56a89..241009a08 100644
--- a/mesonbuild/cargo/interpreter.py
+++ b/mesonbuild/cargo/interpreter.py
@@ -394,6 +394,12 @@ class Interpreter:
else:
self._enable_feature(pkg, f)
+ def has_check_cfg(self, machine: MachineChoice) -> bool:
+ if not self.environment.is_cross_build():
+ machine = MachineChoice.HOST
+ rustc = T.cast('RustCompiler', self.environment.coredata.compilers[machine]['rust'])
+ return rustc.has_check_cfg
+
@functools.lru_cache(maxsize=None)
def _get_cfgs(self, machine: MachineChoice) -> T.Dict[str, str]:
if not self.environment.is_cross_build():
@@ -415,6 +421,18 @@ class Interpreter:
value = value[1:-1]
return pair[0], value
+ def _lints_to_args(self, pkg: PackageState) -> T.List[str]:
+ args = []
+ has_check_cfg = self.has_check_cfg(MachineChoice.HOST)
+ for lint in pkg.manifest.lints:
+ args.extend(lint.to_arguments(has_check_cfg))
+ if has_check_cfg:
+ for feature in pkg.manifest.features:
+ if feature != 'default':
+ args.append('--check-cfg')
+ args.append(f'cfg(feature,values("{feature}"))')
+ return args
+
def _create_project(self, name: str, pkg: T.Optional[PackageState], build: builder.Builder) -> T.List[mparser.BaseNode]:
"""Create the project() function call
@@ -443,6 +461,10 @@ class Interpreter:
}
if pkg.downloaded:
default_options['warning_level'] = build.string('0')
+ else:
+ lint_args = build.array([build.string(arg) for arg in self._lints_to_args(pkg)])
+ default_options['rust_args'] = lint_args
+ default_options['build.rust_args'] = lint_args
kwargs.update({
'version': build.string(pkg.manifest.package.version),
diff --git a/mesonbuild/cargo/manifest.py b/mesonbuild/cargo/manifest.py
index ec10d4611..5e453c9a1 100644
--- a/mesonbuild/cargo/manifest.py
+++ b/mesonbuild/cargo/manifest.py
@@ -453,6 +453,24 @@ class Lint:
lints_final.sort(key=lambda x: x.priority)
return lints_final
+ def to_arguments(self, check_cfg: bool) -> T.List[str]:
+ if self.level == "deny":
+ flag = "-D"
+ elif self.level == "allow":
+ flag = "-A"
+ elif self.level == "warn":
+ flag = "-W"
+ elif self.level == "forbid":
+ flag = "-F"
+ else:
+ raise MesonException(f"invalid level {self.level!r} for {self.name}")
+ args = [flag, self.name]
+ if check_cfg and self.check_cfg:
+ for arg in self.check_cfg:
+ args.append('--check-cfg')
+ args.append(arg)
+ return args
+
@dataclasses.dataclass
class Manifest:
diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py
index d7609f370..c0acf92ad 100644
--- a/mesonbuild/compilers/rust.py
+++ b/mesonbuild/compilers/rust.py
@@ -104,6 +104,7 @@ class RustCompiler(Compiler):
self.native_static_libs: T.List[str] = []
self.is_beta = '-beta' in full_version
self.is_nightly = '-nightly' in full_version
+ self.has_check_cfg = version_compare(version, '>=1.80.0')
def needs_static_linker(self) -> bool:
return False
diff --git a/unittests/cargotests.py b/unittests/cargotests.py
index 0c2b645f5..16bc4cfee 100644
--- a/unittests/cargotests.py
+++ b/unittests/cargotests.py
@@ -430,6 +430,23 @@ class CargoTomlTest(unittest.TestCase):
self.assertEqual(manifest.lints[2].priority, 0)
self.assertEqual(manifest.lints[2].check_cfg, ['cfg(test)', 'cfg(MESON)'])
+ def test_cargo_toml_lints_to_args(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_1)
+ manifest_toml = load_toml(fname)
+ manifest = Manifest.from_raw(manifest_toml, 'Cargo.toml')
+
+ self.assertEqual(manifest.lints[0].to_arguments(False), ['-W', 'clippy::pedantic'])
+ self.assertEqual(manifest.lints[0].to_arguments(True), ['-W', 'clippy::pedantic'])
+ self.assertEqual(manifest.lints[1].to_arguments(False), ['-A', 'unknown_lints'])
+ self.assertEqual(manifest.lints[1].to_arguments(True), ['-A', 'unknown_lints'])
+ self.assertEqual(manifest.lints[2].to_arguments(False), ['-D', 'unexpected_cfgs'])
+ self.assertEqual(manifest.lints[2].to_arguments(True),
+ ['-D', 'unexpected_cfgs', '--check-cfg', 'cfg(test)',
+ '--check-cfg', 'cfg(MESON)'])
+
def test_cargo_toml_dependencies(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
fname = os.path.join(tmpdir, 'Cargo.toml')