1#!/usr/bin/env python3 2 3# This work is licensed under the terms of the GNU GPL, version 2 or later. 4# See the COPYING file in the top-level directory. 5 6""" 7QAPI Generator 8 9This is the main entry point for generating C code from the QAPI schema. 10""" 11 12import argparse 13import re 14import sys 15from typing import Optional 16 17from qapi.commands import gen_commands 18from qapi.error import QAPIError 19from qapi.events import gen_events 20from qapi.introspect import gen_introspect 21from qapi.schema import QAPISchema 22from qapi.types import gen_types 23from qapi.visit import gen_visit 24 25 26def invalid_prefix_char(prefix: str) -> Optional[str]: 27 match = re.match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix) 28 if match.end() != len(prefix): 29 return prefix[match.end()] 30 return None 31 32 33def 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. 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_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) 57 58 59def main() -> int: 60 """ 61 gapi-gen executable entry point. 62 Expects arguments via sys.argv, see --help for details. 63 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() 81 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 87 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 98 99 100if __name__ == '__main__': 101 sys.exit(main()) 102