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.
7 This is the main entry point for generating C code from the QAPI schema.
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()]
33 def generate(schema_file
: str,
37 builtins
: bool = False) -> None:
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.
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
)
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.
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',
72 help="write output to directory OUTPUT_DIR")
73 parser
.add_argument('-p', '--prefix', action
='store',
75 help="prefix for symbols")
76 parser
.add_argument('-u', '--unmask-non-abi-names', action
='store_true',
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
)
84 msg
= f
"funny character '{funny_char}' in argument of --prefix"
85 print(f
"{sys.argv[0]}: {msg}", file=sys
.stderr
)
90 output_dir
=args
.output_dir
,
93 builtins
=args
.builtins
)
94 except QAPIError
as err
:
95 print(f
"{sys.argv[0]}: {str(err)}", file=sys
.stderr
)