xref: /openbmc/qemu/scripts/qapi-gen.py (revision 86044b24)
1#!/usr/bin/env python
2# QAPI generator
3#
4# This work is licensed under the terms of the GNU GPL, version 2 or later.
5# See the COPYING file in the top-level directory.
6
7from __future__ import print_function
8
9import argparse
10import re
11import sys
12
13from qapi.commands import gen_commands
14from qapi.doc import gen_doc
15from qapi.events import gen_events
16from qapi.introspect import gen_introspect
17from qapi.schema import QAPIError, QAPISchema
18from qapi.types import gen_types
19from qapi.visit import gen_visit
20
21
22def main(argv):
23    parser = argparse.ArgumentParser(
24        description='Generate code from a QAPI schema')
25    parser.add_argument('-b', '--builtins', action='store_true',
26                        help="generate code for built-in types")
27    parser.add_argument('-o', '--output-dir', action='store', default='',
28                        help="write output to directory OUTPUT_DIR")
29    parser.add_argument('-p', '--prefix', action='store', default='',
30                        help="prefix for symbols")
31    parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
32                        dest='unmask',
33                        help="expose non-ABI names in introspection")
34    parser.add_argument('schema', action='store')
35    args = parser.parse_args()
36
37    match = re.match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', args.prefix)
38    if match.end() != len(args.prefix):
39        print("%s: 'funny character '%s' in argument of --prefix"
40              % (sys.argv[0], args.prefix[match.end()]),
41              file=sys.stderr)
42        sys.exit(1)
43
44    try:
45        schema = QAPISchema(args.schema)
46    except QAPIError as err:
47        print(err, file=sys.stderr)
48        exit(1)
49
50    gen_types(schema, args.output_dir, args.prefix, args.builtins)
51    gen_visit(schema, args.output_dir, args.prefix, args.builtins)
52    gen_commands(schema, args.output_dir, args.prefix)
53    gen_events(schema, args.output_dir, args.prefix)
54    gen_introspect(schema, args.output_dir, args.prefix, args.unmask)
55    gen_doc(schema, args.output_dir, args.prefix)
56
57
58if __name__ == '__main__':
59    main(sys.argv)
60