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