John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 1 | # This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 2 | # See the COPYING file in the top-level directory. |
| 3 | |
| 4 | """ |
| 5 | QAPI Generator |
| 6 | |
| 7 | This is the main entry point for generating C code from the QAPI schema. |
| 8 | """ |
| 9 | |
| 10 | import argparse |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 11 | import sys |
| 12 | from typing import Optional |
| 13 | |
John Snow | 7137a96 | 2020-10-09 12:15:27 -0400 | [diff] [blame] | 14 | from .commands import gen_commands |
John Snow | e0e8a0a | 2021-05-19 14:39:45 -0400 | [diff] [blame] | 15 | from .common import must_match |
John Snow | 7137a96 | 2020-10-09 12:15:27 -0400 | [diff] [blame] | 16 | from .error import QAPIError |
| 17 | from .events import gen_events |
| 18 | from .introspect import gen_introspect |
| 19 | from .schema import QAPISchema |
| 20 | from .types import gen_types |
| 21 | from .visit import gen_visit |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def invalid_prefix_char(prefix: str) -> Optional[str]: |
John Snow | e0e8a0a | 2021-05-19 14:39:45 -0400 | [diff] [blame] | 25 | match = must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix) |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 26 | if match.end() != len(prefix): |
| 27 | return prefix[match.end()] |
| 28 | return None |
| 29 | |
| 30 | |
| 31 | def generate(schema_file: str, |
| 32 | output_dir: str, |
| 33 | prefix: str, |
| 34 | unmask: bool = False, |
Vladimir Sementsov-Ogievskiy | bd2017b | 2022-01-26 17:11:26 +0100 | [diff] [blame] | 35 | builtins: bool = False, |
| 36 | gen_tracing: bool = False) -> None: |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 37 | """ |
| 38 | Generate C code for the given schema into the target directory. |
| 39 | |
| 40 | :param schema_file: The primary QAPI schema file. |
| 41 | :param output_dir: The output directory to store generated code. |
| 42 | :param prefix: Optional C-code prefix for symbol names. |
| 43 | :param unmask: Expose non-ABI names through introspection? |
| 44 | :param builtins: Generate code for built-in types? |
| 45 | |
| 46 | :raise QAPIError: On failures. |
| 47 | """ |
| 48 | assert invalid_prefix_char(prefix) is None |
| 49 | |
| 50 | schema = QAPISchema(schema_file) |
| 51 | gen_types(schema, output_dir, prefix, builtins) |
| 52 | gen_visit(schema, output_dir, prefix, builtins) |
Vladimir Sementsov-Ogievskiy | bd2017b | 2022-01-26 17:11:26 +0100 | [diff] [blame] | 53 | gen_commands(schema, output_dir, prefix, gen_tracing) |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 54 | gen_events(schema, output_dir, prefix) |
| 55 | gen_introspect(schema, output_dir, prefix, unmask) |
| 56 | |
| 57 | |
| 58 | def main() -> int: |
| 59 | """ |
| 60 | gapi-gen executable entry point. |
| 61 | Expects arguments via sys.argv, see --help for details. |
| 62 | |
| 63 | :return: int, 0 on success, 1 on failure. |
| 64 | """ |
| 65 | parser = argparse.ArgumentParser( |
| 66 | description='Generate code from a QAPI schema') |
| 67 | parser.add_argument('-b', '--builtins', action='store_true', |
| 68 | help="generate code for built-in types") |
| 69 | parser.add_argument('-o', '--output-dir', action='store', |
| 70 | default='', |
| 71 | help="write output to directory OUTPUT_DIR") |
| 72 | parser.add_argument('-p', '--prefix', action='store', |
| 73 | default='', |
| 74 | help="prefix for symbols") |
| 75 | parser.add_argument('-u', '--unmask-non-abi-names', action='store_true', |
| 76 | dest='unmask', |
| 77 | help="expose non-ABI names in introspection") |
Vladimir Sementsov-Ogievskiy | bd2017b | 2022-01-26 17:11:26 +0100 | [diff] [blame] | 78 | |
Vladimir Sementsov-Ogievskiy | 761a1a4 | 2022-01-26 17:11:30 +0100 | [diff] [blame] | 79 | # Option --suppress-tracing exists so we can avoid solving build system |
Vladimir Sementsov-Ogievskiy | bd2017b | 2022-01-26 17:11:26 +0100 | [diff] [blame] | 80 | # problems. TODO Drop it when we no longer need it. |
Vladimir Sementsov-Ogievskiy | 761a1a4 | 2022-01-26 17:11:30 +0100 | [diff] [blame] | 81 | parser.add_argument('--suppress-tracing', action='store_true', |
| 82 | help="suppress adding trace events to qmp marshals") |
Vladimir Sementsov-Ogievskiy | bd2017b | 2022-01-26 17:11:26 +0100 | [diff] [blame] | 83 | |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 84 | parser.add_argument('schema', action='store') |
| 85 | args = parser.parse_args() |
| 86 | |
| 87 | funny_char = invalid_prefix_char(args.prefix) |
| 88 | if funny_char: |
| 89 | msg = f"funny character '{funny_char}' in argument of --prefix" |
| 90 | print(f"{sys.argv[0]}: {msg}", file=sys.stderr) |
| 91 | return 1 |
| 92 | |
| 93 | try: |
| 94 | generate(args.schema, |
| 95 | output_dir=args.output_dir, |
| 96 | prefix=args.prefix, |
| 97 | unmask=args.unmask, |
Vladimir Sementsov-Ogievskiy | bd2017b | 2022-01-26 17:11:26 +0100 | [diff] [blame] | 98 | builtins=args.builtins, |
Vladimir Sementsov-Ogievskiy | 761a1a4 | 2022-01-26 17:11:30 +0100 | [diff] [blame] | 99 | gen_tracing=not args.suppress_tracing) |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 100 | except QAPIError as err: |
Markus Armbruster | bc5d303 | 2023-03-16 08:13:12 +0100 | [diff] [blame] | 101 | print(err, file=sys.stderr) |
John Snow | a76ab21 | 2020-10-09 12:15:26 -0400 | [diff] [blame] | 102 | return 1 |
| 103 | return 0 |