From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-rust@nongnu.org, berrange@redhat.com,
junjie.mao@hotmail.com, manos.pitsidianakis@linaro.org,
kwolf@redhat.com
Subject: [PATCH 05/12] rust: build: generate lint flags from Cargo.toml
Date: Fri, 15 Nov 2024 17:40:18 +0100 [thread overview]
Message-ID: <20241115164025.1917618-5-pbonzini@redhat.com> (raw)
In-Reply-To: <20241115163944.1917393-1-pbonzini@redhat.com>
Cargo.toml makes it possible to describe the desired lint level settings
in a nice format. We can extend this to Meson-built crates, by teaching
rustc_args.py to fetch lint and --check-cfg arguments from Cargo.toml.
--check-cfg arguments come from the unexpected_cfgs lint as well as crate
features
Start with qemu-api, since it already has a [lints.rust] table and
an invocation of rustc_args.py.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
meson.build | 3 +-
rust/qemu-api/meson.build | 4 +-
scripts/rust/rustc_args.py | 83 +++++++++++++++++++++++++++++++++++++-
3 files changed, 86 insertions(+), 4 deletions(-)
diff --git a/meson.build b/meson.build
index b51add16d2f..231fccf3928 100644
--- a/meson.build
+++ b/meson.build
@@ -120,7 +120,8 @@ if have_rust
endif
if have_rust
- rustc_args = find_program('scripts/rust/rustc_args.py')
+ rustc_args = [find_program('scripts/rust/rustc_args.py'),
+ '--rustc-version', rustc.version()]
rustfmt = find_program('rustfmt', required: false)
# Prohibit code that is forbidden in Rust 2024
diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build
index 2ff6d2ce3d0..1ed79672cc9 100644
--- a/rust/qemu-api/meson.build
+++ b/rust/qemu-api/meson.build
@@ -1,6 +1,6 @@
_qemu_api_cfg = run_command(rustc_args,
- '--config-headers', config_host_h, files('Cargo.toml'),
- capture: true, check: true).stdout().strip().split()
+ '--config-headers', config_host_h, '--features', '--lints', files('Cargo.toml'),
+ capture: true, check: true).stdout().strip().splitlines()
# _qemu_api_cfg += ['--cfg', 'feature="allocator"']
if rustc.version().version_compare('>=1.77.0')
diff --git a/scripts/rust/rustc_args.py b/scripts/rust/rustc_args.py
index 942dd2b2bab..9b9778a1cac 100644
--- a/scripts/rust/rustc_args.py
+++ b/scripts/rust/rustc_args.py
@@ -25,9 +25,10 @@
"""
import argparse
+from dataclasses import dataclass
import logging
from pathlib import Path
-from typing import Any, Iterable, Mapping, Optional, Set
+from typing import Any, Iterable, List, Mapping, Optional, Set
try:
import tomllib
@@ -61,6 +62,45 @@ def get_table(self, key: str) -> Mapping[Any, Any]:
return table
+@dataclass
+class LintFlag:
+ flags: List[str]
+ priority: int
+
+
+def generate_lint_flags(cargo_toml: CargoTOML) -> Iterable[str]:
+ """Converts Cargo.toml lints to rustc -A/-D/-F/-W flags."""
+
+ toml_lints = cargo_toml.lints
+
+ lint_list = []
+ for k, v in toml_lints.items():
+ prefix = "" if k == "rust" else k + "::"
+ for lint, data in v.items():
+ level = data if isinstance(data, str) else data["level"]
+ priority = 0 if isinstance(data, str) else data.get("priority", 0)
+ if level == "deny":
+ flag = "-D"
+ elif level == "allow":
+ flag = "-A"
+ elif level == "warn":
+ flag = "-W"
+ elif level == "forbid":
+ flag = "-F"
+ else:
+ raise Exception(f"invalid level {level} for {prefix}{lint}")
+
+ # This may change if QEMU ever invokes clippy-driver or rustdoc by
+ # hand. For now, check the syntax but do not add non-rustc lints to
+ # the command line.
+ if k == "rust":
+ lint_list.append(LintFlag(flags=[flag, prefix + lint], priority=priority))
+
+ lint_list.sort(key=lambda x: x.priority)
+ for lint in lint_list:
+ yield from lint.flags
+
+
def generate_cfg_flags(header: str, cargo_toml: CargoTOML) -> Iterable[str]:
"""Converts defines from config[..].h headers to rustc --cfg flags."""
@@ -97,13 +137,54 @@ def main() -> None:
dest="cargo_toml",
help="path to Cargo.toml file",
)
+ parser.add_argument(
+ "--features",
+ action="store_true",
+ dest="features",
+ help="generate --check-cfg arguments for features",
+ required=False,
+ default=None,
+ )
+ parser.add_argument(
+ "--lints",
+ action="store_true",
+ dest="lints",
+ help="generate arguments from [lints] table",
+ required=False,
+ default=None,
+ )
+ parser.add_argument(
+ "--rustc-version",
+ metavar="VERSION",
+ dest="rustc_version",
+ action="store",
+ help="version of rustc",
+ required=False,
+ default="1.0.0",
+ )
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
logging.debug("args: %s", args)
+ rustc_version = tuple((int(x) for x in args.rustc_version.split('.')[0:2]))
cargo_toml = CargoTOML(args.cargo_toml)
+ if args.lints:
+ for tok in generate_lint_flags(cargo_toml):
+ print(tok)
+
+ if rustc_version >= (1, 80):
+ if args.lints:
+ for cfg in sorted(cargo_toml.check_cfg):
+ print("--check-cfg")
+ print(cfg)
+ if args.features:
+ for feature in cargo_toml.get_table("features"):
+ if feature != "default":
+ print("--check-cfg")
+ print(f'cfg(feature,values("{feature}"))')
+
for header in args.config_headers:
for tok in generate_cfg_flags(header, cargo_toml):
print(tok)
--
2.47.0
next prev parent reply other threads:[~2024-11-15 16:41 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20241115163944.1917393-1-pbonzini@redhat.com>
2024-11-15 16:40 ` [PATCH 01/12] rust: apply --cfg MESON to all crates Paolo Bonzini
2024-11-15 16:40 ` [PATCH 02/12] rust: allow using build-root bindings.rs from cargo Paolo Bonzini
2024-11-15 16:40 ` [PATCH 03/12] rust: build: move rustc_args.py invocation to qemu-api crate Paolo Bonzini
2024-11-15 16:40 ` [PATCH 04/12] rust: build: restrict --cfg generation to only required symbols Paolo Bonzini
2024-11-15 16:40 ` Paolo Bonzini [this message]
2024-11-15 16:40 ` [PATCH 06/12] rust: cargo: store desired warning levels in workspace Cargo.toml Paolo Bonzini
2024-11-15 16:40 ` [PATCH 07/12] rust: build: move strict lints handling to rustc_args.py Paolo Bonzini
2024-11-15 16:40 ` [PATCH 08/12] rust: fix a couple style issues from clippy Paolo Bonzini
2024-11-15 16:40 ` [PATCH 09/12] rust: build: establish a baseline of lints across all crates Paolo Bonzini
2024-11-15 16:40 ` [PATCH 10/12] rust: build: add "make clippy", "make rustfmt", "make rustdoc" Paolo Bonzini
2024-11-15 16:40 ` [PATCH 11/12] rust: ci: add job that runs Rust tools Paolo Bonzini
2024-11-15 16:40 ` [PATCH 12/12] rust: fix doc test syntax Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241115164025.1917618-5-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=berrange@redhat.com \
--cc=junjie.mao@hotmail.com \
--cc=kwolf@redhat.com \
--cc=manos.pitsidianakis@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).