tests/tcg/tricore: Add clz test
[qemu.git] / scripts / qapi / main.py
blob703e7ed1ed546eca0dabca5de24781dac4e2358e
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.
4 """
5 QAPI Generator
7 This is the main entry point for generating C code from the QAPI schema.
8 """
10 import argparse
11 import re
12 import sys
13 from typing import Optional
15 from .commands import gen_commands
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
24 def 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
33 def 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.
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?
47 :raise QAPIError: On failures.
48 """
49 assert invalid_prefix_char(prefix) is None
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)
59 def main() -> int:
60 """
61 gapi-gen executable entry point.
62 Expects arguments via sys.argv, see --help for details.
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()
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
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