xref: /openbmc/qemu/scripts/qapi/main.py (revision 2182e405)
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"""
5QAPI Generator
6
7This is the main entry point for generating C code from the QAPI schema.
8"""
9
10import argparse
11import re
12import sys
13from typing import Optional
14
15from .commands import gen_commands
16from .error import QAPIError
17from .events import gen_events
18from .introspect import gen_introspect
19from .schema import QAPISchema
20from .types import gen_types
21from .visit import gen_visit
22
23
24def invalid_prefix_char(prefix: str) -> Optional[str]:
25    match = re.match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
26    # match cannot be None, but mypy cannot infer that.
27    assert match is not None
28    if match.end() != len(prefix):
29        return prefix[match.end()]
30    return None
31
32
33def generate(schema_file: str,
34             output_dir: str,
35             prefix: str,
36             unmask: bool = False,
37             builtins: bool = False) -> None:
38    """
39    Generate C code for the given schema into the target directory.
40
41    :param schema_file: The primary QAPI schema file.
42    :param output_dir: The output directory to store generated code.
43    :param prefix: Optional C-code prefix for symbol names.
44    :param unmask: Expose non-ABI names through introspection?
45    :param builtins: Generate code for built-in types?
46
47    :raise QAPIError: On failures.
48    """
49    assert invalid_prefix_char(prefix) is None
50
51    schema = QAPISchema(schema_file)
52    gen_types(schema, output_dir, prefix, builtins)
53    gen_visit(schema, output_dir, prefix, builtins)
54    gen_commands(schema, output_dir, prefix)
55    gen_events(schema, output_dir, prefix)
56    gen_introspect(schema, output_dir, prefix, unmask)
57
58
59def main() -> int:
60    """
61    gapi-gen executable entry point.
62    Expects arguments via sys.argv, see --help for details.
63
64    :return: int, 0 on success, 1 on failure.
65    """
66    parser = argparse.ArgumentParser(
67        description='Generate code from a QAPI schema')
68    parser.add_argument('-b', '--builtins', action='store_true',
69                        help="generate code for built-in types")
70    parser.add_argument('-o', '--output-dir', action='store',
71                        default='',
72                        help="write output to directory OUTPUT_DIR")
73    parser.add_argument('-p', '--prefix', action='store',
74                        default='',
75                        help="prefix for symbols")
76    parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
77                        dest='unmask',
78                        help="expose non-ABI names in introspection")
79    parser.add_argument('schema', action='store')
80    args = parser.parse_args()
81
82    funny_char = invalid_prefix_char(args.prefix)
83    if funny_char:
84        msg = f"funny character '{funny_char}' in argument of --prefix"
85        print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
86        return 1
87
88    try:
89        generate(args.schema,
90                 output_dir=args.output_dir,
91                 prefix=args.prefix,
92                 unmask=args.unmask,
93                 builtins=args.builtins)
94    except QAPIError as err:
95        print(f"{sys.argv[0]}: {str(err)}", file=sys.stderr)
96        return 1
97    return 0
98