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