From nobody Sat Nov 15 09:32:07 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1753173373; cv=none; d=zohomail.com; s=zohoarc; b=dbabum5Q6Ah6LXtZGoOiVdxN21+MXCiL1SCqL29Sm8nngDS+lm+Ioif+2VDsP/Y6VoMhS99x81EOgnHJyaNub/sv3TPaSCnUQr0G6MhdiKpqz2CDkeo1i61I+4LLTrqPZi6ggB1cOoBBQPkyQPEMljIhVVOrT5DeSxtQoPZCcKM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753173373; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=dxy4jVdACSTIADEOvJ/2CPOfcLfKLmzp5UnXyb7OctA=; b=j8Acwv2rvuVJhaJ+oXY5us1T4GP/tYc5ABtmg6Ac/0h/M/KlAGFVH1F5DLD0l3XrTVpm6OLRXRhgOHmFeFXwbg/TXxLJe/LBDy/WkTAw5oUwElqr+sTOmz6aUYkLwsq6atsREh4z7BTg78R6IK4jmIOuqjgJCLvk1qNLGGtqbws= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1753173373196783.0873315891134; Tue, 22 Jul 2025 01:36:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ue8T6-0001Yl-4W; Tue, 22 Jul 2025 04:35:24 -0400 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 1ue8T4-0001US-Ao for qemu-devel@nongnu.org; Tue, 22 Jul 2025 04:35:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ue8Sz-00075y-LP for qemu-devel@nongnu.org; Tue, 22 Jul 2025 04:35:22 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-265-9R7iAoiPPg-vGh5KIQRiJg-1; Tue, 22 Jul 2025 04:35:11 -0400 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3a4fabcafecso2375842f8f.0 for ; Tue, 22 Jul 2025 01:35:11 -0700 (PDT) Received: from [192.168.10.48] ([151.49.73.155]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-458639ffca5sm10834135e9.1.2025.07.22.01.35.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jul 2025 01:35:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753173316; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dxy4jVdACSTIADEOvJ/2CPOfcLfKLmzp5UnXyb7OctA=; b=PTI3OFPsk7nCHZcj9B5Mz3nah8LCjkjmOQPr1g3JU+MZl7o4z0Cqp1tgLfNpfdLD8kZBg6 RB5t2i2WMZiSARhU0R7+jDYqknfwOd3SewFmVNaym8wZnHW7+Dy5fzHOFpXKR021+bR4Kp vMrKCaM6i15OnE7SO8fTpG7TbprSdrU= X-MC-Unique: 9R7iAoiPPg-vGh5KIQRiJg-1 X-Mimecast-MFC-AGG-ID: 9R7iAoiPPg-vGh5KIQRiJg_1753173310 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753173310; x=1753778110; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dxy4jVdACSTIADEOvJ/2CPOfcLfKLmzp5UnXyb7OctA=; b=cc9noAbBfEmVPcXZ01Zm4zOI/AhZsdkVdvpjgU0pA4/1ClW3EzBsBJcl4ct+FZrILU cRvN+wDXpAZ2SK7km+9y950CDXL6VgbpkQCbSIyC4ubsZV127lGCY4JGnIZQpT3AukEt eVNS5UJ6+jG8bObC10cCVJQjv/e7y0xzer1FxH9UrX7Vg9xenLTDlQ8lJ9uqWvc5I24h isJtXyUuZBpry8ULXQ7q51pp6ZVSFBp+7MPGOYMKCyuNMJD6dGH8XYZkw5FqR1tcbeIm Nb0C4GnoeFbRllH6YZNeivzWTwC/GC9ccDaLygC556YY2Ys0MfuIMV3xj/zjDR9Zd9wg IEVA== X-Gm-Message-State: AOJu0YzXVpPOtDNUSpadXr82bm4iv4VsI4VS1kIWDwab/zZ2nmC+YVdg 0cFXH9Kco+TxhPpGXPWCwKwKkTnBTv212hfXV2Dvo/B1yO6Hz2Oc9uN8YQYpHrHBUrQpYh/GbuF KYW5zgKYlUG10C33nVChBLkU0al9loUs+oGda/900pu3HsjnQGVRB7e64EGMG2Bd770O4F/pZHZ FA+ztcmmemThwOvdbkxJltbAi/BC2o346aLIes68cC X-Gm-Gg: ASbGncsvwIUFC2gT78XwumQjbm45olUjyr9im+hRXuGyzD7n1jyvac/E7Mla66pDNVe bWfKUDUdwhgkQXtHORCWVSGLTh5JAoIti/VC9JU1Smx39FCcnKhUDRdzolQLLa/vU++s/BfEniR NvvbF9mS4dMi70Z9HFSjS9wONNqngL/4uQ6xbwHxtaZlTu2El68rYQlIQpWQHNSczKGeMGd9Xv5 kwZ+RTBkIPAPTv17dSt1BqIGDuQE+WbCvPped3jUK04wPYv3/H68758AMPQyfup4EXChFY/0rt6 6AyNYpZ0hjXuJntTSKz0ugFDrehudCnbvA5hzQswSyQ= X-Received: by 2002:a5d:59c7:0:b0:3b3:9c75:acc6 with SMTP id ffacd0b85a97d-3b60ddd2a27mr18028327f8f.59.1753173309533; Tue, 22 Jul 2025 01:35:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDd5JuL92DcysibUPAV8He1JtEik7qdJCLlfSm7JJOPLh3mGsxnIy/VEDIXdyF0YPCGiF6Xw== X-Received: by 2002:a5d:59c7:0:b0:3b3:9c75:acc6 with SMTP id ffacd0b85a97d-3b60ddd2a27mr18028281f8f.59.1753173308905; Tue, 22 Jul 2025 01:35:08 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, berrange@redhat.com, ngompa@fedoraproject.org Subject: [PATCH] scripts: add script to help distro use global Rust packages Date: Tue, 22 Jul 2025 10:35:05 +0200 Message-ID: <20250722083507.678542-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.926, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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: 1753173374896116600 Some distros prefer to avoid vendored crate sources, and instead use local sources from e.g. ``/usr/share/cargo/registry``. Add a script, inspired by the Mesa spec file(*), that automatically performs this task. The script is meant to be invoked after unpacking the QEMU tarball. (*) This is the hack that Mesa uses: export MESON_PACKAGE_CACHE_DIR=3D"%{cargo_registry}/" %define inst_crate_nameversion() %(basename %{cargo_registry}/%{1}-*) %define rewrite_wrap_file() sed -e "/source.*/d" -e "s/%{1}-.*/%{inst_c= rate_nameversion %{1}}/" -i subprojects/%{1}.wrap %rewrite_wrap_file proc-macro2 ... more %rewrite_wrap_file invocations follow ... Signed-off-by: Paolo Bonzini Reviewed-by: Neal Gompa --- docs/about/build-platforms.rst | 8 + scripts/get-wraps-from-cargo-registry.py | 191 +++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100755 scripts/get-wraps-from-cargo-registry.py diff --git a/docs/about/build-platforms.rst b/docs/about/build-platforms.rst index 8ecbd6b26f7..8671c3be9cd 100644 --- a/docs/about/build-platforms.rst +++ b/docs/about/build-platforms.rst @@ -127,6 +127,14 @@ Rust build dependencies (or newer) package. The path to ``rustc`` and ``rustdoc`` must be provided manually to the configure script. =20 + Some distros prefer to avoid vendored crate sources, and instead use + local sources from e.g. ``/usr/share/cargo/registry``. QEMU includes a + script, ``scripts/get-wraps-from-cargo-registry.py``, that automatically + performs this task. The script is meant to be invoked after unpacking + the QEMU tarball. QEMU also includes ``rust/Cargo.toml`` and + ``rust/Cargo.lock`` files that can be used to compute QEMU's build + dependencies, e.g. using ``cargo2rpm -p rust/Cargo.toml buildrequires``. + Optional build dependencies Build components whose absence does not affect the ability to build QEMU may not be available in distros, or may be too old for our requirements. diff --git a/scripts/get-wraps-from-cargo-registry.py b/scripts/get-wraps-f= rom-cargo-registry.py new file mode 100755 index 00000000000..6b76d00a6d9 --- /dev/null +++ b/scripts/get-wraps-from-cargo-registry.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python3 + +""" +get-wraps-from-cargo-registry.py - Update Meson subprojects from a global = registry +""" + +# Copyright (C) 2025 Red Hat, Inc. +# +# Author: Paolo Bonzini +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +import argparse +import configparser +import filecmp +import glob +import os +import subprocess +import sys + + +def get_name_and_semver(namever: str) -> tuple[str, str]: + """Split a subproject name into its name and semantic version parts""" + parts =3D namever.rsplit("-", 1) + if len(parts) !=3D 2: + return namever, "" + + return parts[0], parts[1] + + +class UpdateSubprojects: + cargo_registry: str + top_srcdir: str + dry_run: bool + changes: int =3D 0 + + def find_installed_crate(self, namever: str) -> str | None: + """Find installed crate matching name and semver prefix""" + name, semver =3D get_name_and_semver(namever) + + # exact version match + path =3D os.path.join(self.cargo_registry, f"{name}-{semver}") + if os.path.exists(path): + return f"{name}-{semver}" + + # semver match + matches =3D sorted(glob.glob(f"{path}.*")) + return os.path.basename(matches[0]) if matches else None + + def compare_build_rs(self, orig_dir: str, registry_namever: str) -> No= ne: + """Warn if the build.rs in the original directory differs from the= registry version.""" + orig_build_rs =3D os.path.join(orig_dir, "build.rs") + new_build_rs =3D os.path.join(self.cargo_registry, registry_nameve= r, "build.rs") + + msg =3D None + if os.path.isfile(orig_build_rs) !=3D os.path.isfile(new_build_rs): + if os.path.isfile(orig_build_rs): + msg =3D f"build.rs removed in {registry_namever}" + if os.path.isfile(new_build_rs): + msg =3D f"build.rs added in {registry_namever}" + + elif os.path.isfile(orig_build_rs) and not filecmp.cmp(orig_build_= rs, new_build_rs): + msg =3D f"build.rs changed from {orig_dir} to {registry_nameve= r}" + + if msg: + print(f"=E2=9A=A0=EF=B8=8F Warning: {msg}") + print(" This may affect the build process - please review th= e differences.") + + def update_subproject(self, wrap_file: str, registry_namever: str) -> = None: + """Modify [wrap-file] section to point to self.cargo_registry.""" + assert wrap_file.endswith("-rs.wrap") + wrap_name =3D wrap_file[:-5] + + env =3D os.environ.copy() + env["MESON_PACKAGE_CACHE_DIR"] =3D self.cargo_registry + + config =3D configparser.ConfigParser() + config.read(wrap_file) + if "wrap-file" not in config: + return + + # do not download the wrap, always use the local copy + orig_dir =3D config["wrap-file"]["directory"] + if os.path.exists(orig_dir) and orig_dir !=3D registry_namever: + self.compare_build_rs(orig_dir, registry_namever) + if self.dry_run: + if orig_dir =3D=3D registry_namever: + print(f"Will install {orig_dir} from registry.") + else: + print(f"Will replace {orig_dir} with {registry_namever}.") + self.changes +=3D 1 + return + + config["wrap-file"]["directory"] =3D registry_namever + for key in list(config["wrap-file"].keys()): + if key.startswith("source"): + del config["wrap-file"][key] + + # replace existing directory with installed version + if os.path.exists(orig_dir): + subprocess.run( + ["meson", "subprojects", "purge", "--confirm", wrap_name], + cwd=3Dself.top_srcdir, + env=3Denv, + check=3DTrue, + ) + + with open(wrap_file, "w") as f: + config.write(f) + + if orig_dir =3D=3D registry_namever: + print(f"Installing {orig_dir} from registry.") + else: + print(f"Replacing {orig_dir} with {registry_namever}.") + + if orig_dir !=3D registry_namever: + patch_dir =3D config["wrap-file"]["patch_directory"] + patch_dir =3D os.path.join("packagefiles", patch_dir) + _, ver =3D registry_namever.rsplit("-", 1) + subprocess.run( + ["meson", "rewrite", "kwargs", "set", "project", "/", "ver= sion", ver], + cwd=3Dpatch_dir, + env=3Denv, + check=3DTrue, + ) + + subprocess.run( + ["meson", "subprojects", "download", wrap_name], + cwd=3Dself.top_srcdir, + env=3Denv, + check=3DTrue, + ) + + @staticmethod + def parse_cmdline() -> argparse.Namespace: + parser =3D argparse.ArgumentParser( + description=3D"Replace Meson subprojects with packages in a Ca= rgo registry" + ) + parser.add_argument( + "--cargo-registry", + default=3Dos.environ.get("CARGO_REGISTRY"), + help=3D"Path to Cargo registry (default: CARGO_REGISTRY env va= r)", + ) + parser.add_argument( + "--dry-run", + action=3D"store_true", + default=3DFalse, + help=3D"Do not actually replace anything", + ) + + args =3D parser.parse_args() + if not args.cargo_registry: + print("error: CARGO_REGISTRY environment variable not set and = --cargo-registry not provided") + sys.exit(1) + + return args + + def __init__(self, args: argparse.Namespace): + self.cargo_registry =3D args.cargo_registry + self.dry_run =3D args.dry_run + self.top_srcdir =3D os.getcwd() + + def main(self) -> None: + if not os.path.exists("subprojects"): + print("'subprojects' directory not found, nothing to do.") + return + + os.chdir("subprojects") + for wrap_file in sorted(glob.glob("*-rs.wrap")): + namever =3D wrap_file[:-8] # Remove '-rs.wrap' + + registry_namever =3D self.find_installed_crate(namever) + if not registry_namever: + print(f"No installed crate found for {wrap_file}") + continue + + self.update_subproject(wrap_file, registry_namever) + + if self.changes: + if self.dry_run: + print("Rerun without --dry-run to apply changes.") + else: + print(f"=E2=9C=A8 {self.changes} subproject(s) updated!") + else: + print("No changes.") + + +if __name__ =3D=3D "__main__": + args =3D UpdateSubprojects.parse_cmdline() + UpdateSubprojects(args).main() --=20 2.50.1