From nobody Fri Apr 4 03:24:01 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=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1739800210; cv=none; d=zohomail.com; s=zohoarc; b=FD8WvdMHAUKk/9154bNzdGZTjB4Umo/EexqfNzrszZFPlfuJHeOApUr7HZa019Smy44+/uWiHgs0SZnD8Z1YgDOi3v1oBxd30L8GH/oIobNg/JYakGOM+xUcIF3DJGgvwyfKVPYTOInnNzUctw/Cw++XSziYzyDyl8CPthiMNUc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739800210; 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=IdzuMFWpd4kAOz1t2kfPk7KRkHnHHz02VGHoJeSAB4s=; b=mZ7Tjp4WqqTJTQH0SEOcGkzeozC4c8JIKCSZiJeoac6PEsEj43LX3QGnufeOrXZjE1Hg6JUyeHXGDReyv6zSjl66W6i2HqCu/uLRPg7pBTKbjn8vKy1nCYccGUpScL2WiVuaKquQkQik+YSxVFH/Wr3456gIcLuAi9fiwQSUg9Y= 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 173980021034586.33615853794288; Mon, 17 Feb 2025 05:50:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tk1VK-0007bs-Ls; Mon, 17 Feb 2025 08:49:46 -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 1tk1VD-0007VC-4j for qemu-devel@nongnu.org; Mon, 17 Feb 2025 08:49:42 -0500 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 1tk1V9-0004Cq-20 for qemu-devel@nongnu.org; Mon, 17 Feb 2025 08:49:38 -0500 Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-331-ks_D4DluPrGvnHxIm3uidg-1; Mon, 17 Feb 2025 08:49:31 -0500 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E5C21193578F; Mon, 17 Feb 2025 13:49:30 +0000 (UTC) Received: from toolbx.redhat.com (unknown [10.42.28.176]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0F88619560AB; Mon, 17 Feb 2025 13:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739800173; 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=IdzuMFWpd4kAOz1t2kfPk7KRkHnHHz02VGHoJeSAB4s=; b=TtMLnhgEDEh5ZwXSjXwS0oAIGQUp8rhyfSYi9kxEj0a8uUDNa205Wtss2XO47j1/+KWAPS pDHcZ5XHDuMXv4tuZ6ZxoPFoIDONaHbyKTwiI6EOAXKRPpYpAv1nIYlUDVc5jklalnefPE YXFUJ1HMHQ8s2Q1M1PkeBRP/y77tUH0= X-MC-Unique: ks_D4DluPrGvnHxIm3uidg-1 X-Mimecast-MFC-AGG-ID: ks_D4DluPrGvnHxIm3uidg_1739800171 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Victor Toso , Michael Roth , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH] qapi: pluggable backend code generators Date: Mon, 17 Feb 2025 13:49:27 +0000 Message-ID: <20250217134927.1288486-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 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=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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_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: 1739800212877019000 The 'qapi.backend.QAPIBackend' class defines an API contract for code generators. The current generator is put into a new class 'qapi.backend.QAPICBackend' and made to be the default impl. A custom generator can be requested using the '-k' arg which takes a fully qualified python class name qapi-gen.py -k the.python.module.QAPIMyBackend Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- This is an impl of the idea I mentioned at: https://lists.nongnu.org/archive/html/qemu-devel/2025-02/msg03475.html With this change, it is possible for the Go generator code to live outside of qemu.git, invoked using: $ PYTHONPATH=3D/path/to/qemu.git/scripts \ python /path/to/qemu.git/scripts/qapi-gen.py \ -o somedir \ -k qapi.golang.golang.QAPIGoBackend \ /path/to/qemu.git/qga/qapi-schema.json The external app could just expect qemu.git to be checkedout somewhere convenient, or could use a git submodule to reference it. scripts/qapi/backend.py | 96 +++++++++++++++++++++++++++++++++++++++++ scripts/qapi/main.py | 65 ++++++++-------------------- 2 files changed, 113 insertions(+), 48 deletions(-) create mode 100644 scripts/qapi/backend.py diff --git a/scripts/qapi/backend.py b/scripts/qapi/backend.py new file mode 100644 index 0000000000..b6873fd2e3 --- /dev/null +++ b/scripts/qapi/backend.py @@ -0,0 +1,96 @@ +# This work is licensed under the terms of the GNU GPL, version 2 or later. +# See the COPYING file in the top-level directory. + +from abc import ABC, abstractmethod +from typing import Optional + +from .commands import gen_commands +from .common import must_match +from .events import gen_events +from .features import gen_features +from .introspect import gen_introspect +from .schema import QAPISchema +from .types import gen_types +from .visit import gen_visit + + +def invalid_prefix_char(prefix: str) -> Optional[str]: + match =3D must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix) + if match.end() !=3D len(prefix): + return prefix[match.end()] + return None + + +class QAPIBackend(ABC): + + def run(self, + schema_file: str, + output_dir: str, + prefix: str, + unmask: bool =3D False, + builtins: bool =3D False, + gen_tracing: bool =3D False) -> None: + """ + Run the code generator for the given schema into the target direct= ory. + + :param schema_file: The primary QAPI schema file. + :param output_dir: The output directory to store generated code. + :param prefix: Optional C-code prefix for symbol names. + :param unmask: Expose non-ABI names through introspection? + :param builtins: Generate code for built-in types? + + :raise QAPIError: On failures. + """ + assert invalid_prefix_char(prefix) is None + + schema =3D QAPISchema(schema_file) + self.generate(schema, output_dir, prefix, unmask, builtins, gen_tr= acing) + + @abstractmethod + def generate(self, + schema: QAPISchema, + output_dir: str, + prefix: str, + unmask: bool, + builtins: bool, + gen_tracing: bool) -> None: + """ + Generate code for the given schema into the target directory. + + :param schema: The primary QAPI schema object. + :param output_dir: The output directory to store generated code. + :param prefix: Optional C-code prefix for symbol names. + :param unmask: Expose non-ABI names through introspection? + :param builtins: Generate code for built-in types? + + :raise QAPIError: On failures. + """ + pass + + +class QAPICBackend(QAPIBackend): + + def generate(self, + schema: QAPISchema, + output_dir: str, + prefix: str, + unmask: bool, + builtins: bool, + gen_tracing: bool) -> None: + """ + Generate C code for the given schema into the target directory. + + :param schema_file: The primary QAPI schema file. + :param output_dir: The output directory to store generated code. + :param prefix: Optional C-code prefix for symbol names. + :param unmask: Expose non-ABI names through introspection? + :param builtins: Generate code for built-in types? + + :raise QAPIError: On failures. + """ + gen_types(schema, output_dir, prefix, builtins) + gen_features(schema, output_dir, prefix) + gen_visit(schema, output_dir, prefix, builtins) + gen_commands(schema, output_dir, prefix, gen_tracing) + gen_events(schema, output_dir, prefix) + gen_introspect(schema, output_dir, prefix, unmask) diff --git a/scripts/qapi/main.py b/scripts/qapi/main.py index 324081b9fc..35552dffce 100644 --- a/scripts/qapi/main.py +++ b/scripts/qapi/main.py @@ -8,53 +8,18 @@ """ =20 import argparse +from importlib import import_module import sys -from typing import Optional =20 -from .commands import gen_commands -from .common import must_match from .error import QAPIError -from .events import gen_events -from .features import gen_features -from .introspect import gen_introspect -from .schema import QAPISchema -from .types import gen_types -from .visit import gen_visit +from .backend import invalid_prefix_char =20 =20 -def invalid_prefix_char(prefix: str) -> Optional[str]: - match =3D must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix) - if match.end() !=3D len(prefix): - return prefix[match.end()] - return None - - -def generate(schema_file: str, - output_dir: str, - prefix: str, - unmask: bool =3D False, - builtins: bool =3D False, - gen_tracing: bool =3D False) -> None: - """ - Generate C code for the given schema into the target directory. - - :param schema_file: The primary QAPI schema file. - :param output_dir: The output directory to store generated code. - :param prefix: Optional C-code prefix for symbol names. - :param unmask: Expose non-ABI names through introspection? - :param builtins: Generate code for built-in types? - - :raise QAPIError: On failures. - """ - assert invalid_prefix_char(prefix) is None - - schema =3D QAPISchema(schema_file) - gen_types(schema, output_dir, prefix, builtins) - gen_features(schema, output_dir, prefix) - gen_visit(schema, output_dir, prefix, builtins) - gen_commands(schema, output_dir, prefix, gen_tracing) - gen_events(schema, output_dir, prefix) - gen_introspect(schema, output_dir, prefix, unmask) +def import_class_from_string(path): + module_path, _, class_name =3D path.rpartition('.') + mod =3D import_module(module_path) + klass =3D getattr(mod, class_name) + return klass =20 =20 def main() -> int: @@ -77,6 +42,8 @@ def main() -> int: parser.add_argument('-u', '--unmask-non-abi-names', action=3D'store_tr= ue', dest=3D'unmask', help=3D"expose non-ABI names in introspection") + parser.add_argument('-k', '--backend', default=3D"qapi.backend.QAPICBa= ckend", + help=3D"Python module name for code generator") =20 # Option --suppress-tracing exists so we can avoid solving build system # problems. TODO Drop it when we no longer need it. @@ -92,13 +59,15 @@ def main() -> int: print(f"{sys.argv[0]}: {msg}", file=3Dsys.stderr) return 1 =20 + backendclass =3D import_class_from_string(args.backend) try: - generate(args.schema, - output_dir=3Dargs.output_dir, - prefix=3Dargs.prefix, - unmask=3Dargs.unmask, - builtins=3Dargs.builtins, - gen_tracing=3Dnot args.suppress_tracing) + backend =3D backendclass() + backend.run(args.schema, + output_dir=3Dargs.output_dir, + prefix=3Dargs.prefix, + unmask=3Dargs.unmask, + builtins=3Dargs.builtins, + gen_tracing=3Dnot args.suppress_tracing) except QAPIError as err: print(err, file=3Dsys.stderr) return 1 --=20 2.47.1