From nobody Sat Nov 23 21:36:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731089042; cv=none; d=zohomail.com; s=zohoarc; b=PW9hL7IM3rtl3KhE4A7OfoPuZYgWmk0Odrn5OQWJFC6zni0eIZ+LpOxVTxnVX25XTO08tS/K98ODOb6vGLrcYwrESTpCS9FJ+ZPHTY8vCZ15rtYQClI55vsQWBqO+dLW2N2iWUYMbUaAHb1/kCp2kpeIYP4I4tUjZtZ/GLrJxL8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731089042; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NtlWVHUs02V1J7NuepxEhAY1b+bPSGqGN7MAC+hr2Y4=; b=COSTrbUfdpunw+MTgOqhyFTpzNBjZIU9cEFUmK5iDe64rSCbjlHjrLOw1Tufwlg4LGUQgdYs5G+FZy3CYlLV2yOFoYT6lS3zqYHeP8DsiocjppXs42NfDf8/UGmG1F+9hR3UJXwsQnh0V6NR5OYGivro04YlwcsAOLmvzc2Bj9E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731089042757945.018954030166; Fri, 8 Nov 2024 10:04:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t9TJ4-0001so-6c; Fri, 08 Nov 2024 13:02:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t9TIy-0001oj-Qm for qemu-devel@nongnu.org; Fri, 08 Nov 2024 13:01:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t9TIx-0001vh-85 for qemu-devel@nongnu.org; Fri, 08 Nov 2024 13:01:56 -0500 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-538-Lu1_ClCbPJyiNsdfw2aoRw-1; Fri, 08 Nov 2024 13:01:53 -0500 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-431673032e6so15596495e9.0 for ; Fri, 08 Nov 2024 10:01:53 -0800 (PST) Received: from [192.168.10.47] ([151.49.84.243]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432aa6b2a3bsm113584455e9.11.2024.11.08.10.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 10:01:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731088914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NtlWVHUs02V1J7NuepxEhAY1b+bPSGqGN7MAC+hr2Y4=; b=BemJddRb2NV/4RkOk3PJSrX4eDWa+WS8N9K4RRtwLFMMSW4TcW03qQq1XPmYIXGoH4GfO0 SAnq/VX4aaIciZx09M21CIaMHkutYTKmkSScCfe0KGOGIElrvJ7B7pEHqf7r8Xq1tX4lxB ztRMa87ppfbUXnDL6zorb1Zoc/vCM60= X-MC-Unique: Lu1_ClCbPJyiNsdfw2aoRw-1 X-Mimecast-MFC-AGG-ID: Lu1_ClCbPJyiNsdfw2aoRw X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731088911; x=1731693711; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NtlWVHUs02V1J7NuepxEhAY1b+bPSGqGN7MAC+hr2Y4=; b=JxYcMTZ4L4Uio6O3IG/KwnP4/Rmtd1iVWuJqJON0OfKzjXrOVGpy0mMWUEMpn103Qm +Yxit4eKalM2PfokBAwAtIURxvme4RdnB2Yem5aQhhtJjLuU+f1E8pWXV2Kg2ccFvo2z lcmI/uZFvdVRXjkqgK1LD7gnwgcVe0U/2pNYq6VWHhOHTYlYUmThceBHDeObt+Qu4qs9 e1EkkH42Jb9UNZuAiqc1xtXuf2Y/+EXdwgPWMesm6SQUtIp+9R27Kd0QXZRgzIC6Mw2v /oxUWKpQlJmra1/LRc2mHuRC9TMHBsXeJ3xcnYFeffEESKQtKys6jombHcLQNy3TYsCw Yi6Q== X-Gm-Message-State: AOJu0YwbNEzx4NSt77QHKfUYXL7pNn6KyANH2rHV7oOxmhl2huAqQMQB gP7tQA3H1PlxMikXrF2RRZWaM9rbfMqZsfJVGgX1GT2HC7xhijUqcWBXLXPjUOlcbG5KUaTs8LL vok3BSENZ6YalHOTgYBNoqW77TTIaSEHgD2b5k4UpMoIARuT/z8NlsxP8Vh8vcz1K9YBn8DnVl8 1Q+0Lt3r0JH4mlmD7dIhTpW1KZdK88rWSKaCh7D2g= X-Received: by 2002:a5d:5f93:0:b0:37c:ddab:a626 with SMTP id ffacd0b85a97d-381f18270b9mr3455486f8f.7.1731088911224; Fri, 08 Nov 2024 10:01:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXpELzcUPuq1aHn/ar1IEsvO024hFlT6xiueWWzvOMBU+egxqHhCsJJmsob2IeLdpsLN+guQ== X-Received: by 2002:a5d:5f93:0:b0:37c:ddab:a626 with SMTP id ffacd0b85a97d-381f18270b9mr3455431f8f.7.1731088910636; Fri, 08 Nov 2024 10:01:50 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: manos.pitsidianakis@linaro.org, kwolf@redhat.com, junjie.mao@hotmail.com, zhao1.liu@intel.com, qemu-rust@nondevel.org Subject: [RFC PATCH 03/11] rust: build: restrict --cfg generation to only required symbols Date: Fri, 8 Nov 2024 19:01:31 +0100 Message-ID: <20241108180139.117112-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241108180139.117112-1-pbonzini@redhat.com> References: <20241108180139.117112-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.34, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731089044832116600 Content-Type: text/plain; charset="utf-8" Parse the Cargo.toml file, looking for the unexpected_cfgs configuration. When generating --cfg options from the config-host.h file, only use those that are included in the configuration. Signed-off-by: Paolo Bonzini --- rust/qemu-api/meson.build | 2 +- scripts/rust/rustc_args.py | 61 ++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build index f84f85b88c6..bb2ed2844dc 100644 --- a/rust/qemu-api/meson.build +++ b/rust/qemu-api/meson.build @@ -5,7 +5,7 @@ if rustc.version().version_compare('>=3D1.77.0') endif =20 _qemu_api_cfg +=3D run_command(rustc_args, - '--config-headers', config_host_h, + '--config-headers', config_host_h, files('Cargo.toml'), capture: true, check: true).stdout().strip().split() =20 _qemu_api_rs =3D static_library( diff --git a/scripts/rust/rustc_args.py b/scripts/rust/rustc_args.py index e4cc9720e16..942dd2b2bab 100644 --- a/scripts/rust/rustc_args.py +++ b/scripts/rust/rustc_args.py @@ -26,30 +26,51 @@ =20 import argparse import logging +from pathlib import Path +from typing import Any, Iterable, Mapping, Optional, Set =20 -from typing import List +try: + import tomllib +except ImportError: + import tomli as tomllib =20 =20 -def generate_cfg_flags(header: str) -> List[str]: +class CargoTOML: + tomldata: Mapping[Any, Any] + check_cfg: Set[str] + + def __init__(self, path: str): + with open(path, 'rb') as f: + self.tomldata =3D tomllib.load(f) + + self.check_cfg =3D set(self.find_check_cfg()) + + def find_check_cfg(self) -> Iterable[str]: + toml_lints =3D self.lints + rust_lints =3D toml_lints.get("rust", {}) + cfg_lint =3D rust_lints.get("unexpected_cfgs", {}) + return cfg_lint.get("check-cfg", []) + + @property + def lints(self) -> Mapping[Any, Any]: + return self.get_table("lints") + + def get_table(self, key: str) -> Mapping[Any, Any]: + table =3D self.tomldata.get(key, {}) + + return table + + +def generate_cfg_flags(header: str, cargo_toml: CargoTOML) -> Iterable[str= ]: """Converts defines from config[..].h headers to rustc --cfg flags.""" =20 - def cfg_name(name: str) -> str: - """Filter function for C #defines""" - if ( - name.startswith("CONFIG_") - or name.startswith("TARGET_") - or name.startswith("HAVE_") - ): - return name - return "" - with open(header, encoding=3D"utf-8") as cfg: config =3D [l.split()[1:] for l in cfg if l.startswith("#define")] =20 cfg_list =3D [] for cfg in config: - name =3D cfg_name(cfg[0]) - if not name: + name =3D cfg[0] + if f'cfg({name})' not in cargo_toml.check_cfg: continue if len(cfg) >=3D 2 and cfg[1] !=3D "1": continue @@ -59,7 +80,6 @@ def cfg_name(name: str) -> str: =20 =20 def main() -> None: - # pylint: disable=3Dmissing-function-docstring parser =3D argparse.ArgumentParser() parser.add_argument("-v", "--verbose", action=3D"store_true") parser.add_argument( @@ -71,12 +91,21 @@ def main() -> None: required=3DFalse, default=3D[], ) + parser.add_argument( + metavar=3D"TOML_FILE", + action=3D"store", + dest=3D"cargo_toml", + help=3D"path to Cargo.toml file", + ) args =3D parser.parse_args() if args.verbose: logging.basicConfig(level=3Dlogging.DEBUG) logging.debug("args: %s", args) + + cargo_toml =3D CargoTOML(args.cargo_toml) + for header in args.config_headers: - for tok in generate_cfg_flags(header): + for tok in generate_cfg_flags(header, cargo_toml): print(tok) =20 =20 --=20 2.47.0