xref: /qemu/scripts/qapi/main.py (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
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 sys
12from typing import Optional
13
14from .commands import gen_commands
15from .common import must_match
16from .error import QAPIError
17from .events import gen_events
18from .features import gen_features
19from .introspect import gen_introspect
20from .schema import QAPISchema
21from .types import gen_types
22from .visit import gen_visit
23
24
25def invalid_prefix_char(prefix: str) -> Optional[str]:
26    match = must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
27    if match.end() != len(prefix):
28        return prefix[match.end()]
29    return None
30
31
32def generate(schema_file: str,
33             output_dir: str,
34             prefix: str,
35             unmask: bool = False,
36             builtins: bool = False,
37             gen_tracing: 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_features(schema, output_dir, prefix)
54    gen_visit(schema, output_dir, prefix, builtins)
55    gen_commands(schema, output_dir, prefix, gen_tracing)
56    gen_events(schema, output_dir, prefix)
57    gen_introspect(schema, output_dir, prefix, unmask)
58
59
60def main() -> int:
61    """
62    gapi-gen executable entry point.
63    Expects arguments via sys.argv, see --help for details.
64
65    :return: int, 0 on success, 1 on failure.
66    """
67    parser = argparse.ArgumentParser(
68        description='Generate code from a QAPI schema')
69    parser.add_argument('-b', '--builtins', action='store_true',
70                        help="generate code for built-in types")
71    parser.add_argument('-o', '--output-dir', action='store',
72                        default='',
73                        help="write output to directory OUTPUT_DIR")
74    parser.add_argument('-p', '--prefix', action='store',
75                        default='',
76                        help="prefix for symbols")
77    parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
78                        dest='unmask',
79                        help="expose non-ABI names in introspection")
80
81    # Option --suppress-tracing exists so we can avoid solving build system
82    # problems.  TODO Drop it when we no longer need it.
83    parser.add_argument('--suppress-tracing', action='store_true',
84                        help="suppress adding trace events to qmp marshals")
85
86    parser.add_argument('schema', action='store')
87    args = parser.parse_args()
88
89    funny_char = invalid_prefix_char(args.prefix)
90    if funny_char:
91        msg = f"funny character '{funny_char}' in argument of --prefix"
92        print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
93        return 1
94
95    try:
96        generate(args.schema,
97                 output_dir=args.output_dir,
98                 prefix=args.prefix,
99                 unmask=args.unmask,
100                 builtins=args.builtins,
101                 gen_tracing=not args.suppress_tracing)
102    except QAPIError as err:
103        print(err, file=sys.stderr)
104        return 1
105    return 0
106