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 sys 12a76ab215SJohn Snowfrom typing import Optional 13a76ab215SJohn Snow 147137a960SJohn Snowfrom .commands import gen_commands 15e0e8a0acSJohn Snowfrom .common import must_match 167137a960SJohn Snowfrom .error import QAPIError 177137a960SJohn Snowfrom .events import gen_events 187137a960SJohn Snowfrom .introspect import gen_introspect 197137a960SJohn Snowfrom .schema import QAPISchema 207137a960SJohn Snowfrom .types import gen_types 217137a960SJohn Snowfrom .visit import gen_visit 22a76ab215SJohn Snow 23a76ab215SJohn Snow 24a76ab215SJohn Snowdef invalid_prefix_char(prefix: str) -> Optional[str]: 25e0e8a0acSJohn Snow match = must_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, 35bd2017bcSVladimir Sementsov-Ogievskiy builtins: bool = False, 36bd2017bcSVladimir Sementsov-Ogievskiy gen_tracing: bool = False) -> None: 37a76ab215SJohn Snow """ 38a76ab215SJohn Snow Generate C code for the given schema into the target directory. 39a76ab215SJohn Snow 40a76ab215SJohn Snow :param schema_file: The primary QAPI schema file. 41a76ab215SJohn Snow :param output_dir: The output directory to store generated code. 42a76ab215SJohn Snow :param prefix: Optional C-code prefix for symbol names. 43a76ab215SJohn Snow :param unmask: Expose non-ABI names through introspection? 44a76ab215SJohn Snow :param builtins: Generate code for built-in types? 45a76ab215SJohn Snow 46a76ab215SJohn Snow :raise QAPIError: On failures. 47a76ab215SJohn Snow """ 48a76ab215SJohn Snow assert invalid_prefix_char(prefix) is None 49a76ab215SJohn Snow 50a76ab215SJohn Snow schema = QAPISchema(schema_file) 51a76ab215SJohn Snow gen_types(schema, output_dir, prefix, builtins) 52a76ab215SJohn Snow gen_visit(schema, output_dir, prefix, builtins) 53bd2017bcSVladimir Sementsov-Ogievskiy gen_commands(schema, output_dir, prefix, gen_tracing) 54a76ab215SJohn Snow gen_events(schema, output_dir, prefix) 55a76ab215SJohn Snow gen_introspect(schema, output_dir, prefix, unmask) 56a76ab215SJohn Snow 57a76ab215SJohn Snow 58a76ab215SJohn Snowdef main() -> int: 59a76ab215SJohn Snow """ 60a76ab215SJohn Snow gapi-gen executable entry point. 61a76ab215SJohn Snow Expects arguments via sys.argv, see --help for details. 62a76ab215SJohn Snow 63a76ab215SJohn Snow :return: int, 0 on success, 1 on failure. 64a76ab215SJohn Snow """ 65a76ab215SJohn Snow parser = argparse.ArgumentParser( 66a76ab215SJohn Snow description='Generate code from a QAPI schema') 67a76ab215SJohn Snow parser.add_argument('-b', '--builtins', action='store_true', 68a76ab215SJohn Snow help="generate code for built-in types") 69a76ab215SJohn Snow parser.add_argument('-o', '--output-dir', action='store', 70a76ab215SJohn Snow default='', 71a76ab215SJohn Snow help="write output to directory OUTPUT_DIR") 72a76ab215SJohn Snow parser.add_argument('-p', '--prefix', action='store', 73a76ab215SJohn Snow default='', 74a76ab215SJohn Snow help="prefix for symbols") 75a76ab215SJohn Snow parser.add_argument('-u', '--unmask-non-abi-names', action='store_true', 76a76ab215SJohn Snow dest='unmask', 77a76ab215SJohn Snow help="expose non-ABI names in introspection") 78bd2017bcSVladimir Sementsov-Ogievskiy 79761a1a48SVladimir Sementsov-Ogievskiy # Option --suppress-tracing exists so we can avoid solving build system 80bd2017bcSVladimir Sementsov-Ogievskiy # problems. TODO Drop it when we no longer need it. 81761a1a48SVladimir Sementsov-Ogievskiy parser.add_argument('--suppress-tracing', action='store_true', 82761a1a48SVladimir Sementsov-Ogievskiy help="suppress adding trace events to qmp marshals") 83bd2017bcSVladimir Sementsov-Ogievskiy 84a76ab215SJohn Snow parser.add_argument('schema', action='store') 85a76ab215SJohn Snow args = parser.parse_args() 86a76ab215SJohn Snow 87a76ab215SJohn Snow funny_char = invalid_prefix_char(args.prefix) 88a76ab215SJohn Snow if funny_char: 89a76ab215SJohn Snow msg = f"funny character '{funny_char}' in argument of --prefix" 90a76ab215SJohn Snow print(f"{sys.argv[0]}: {msg}", file=sys.stderr) 91a76ab215SJohn Snow return 1 92a76ab215SJohn Snow 93a76ab215SJohn Snow try: 94a76ab215SJohn Snow generate(args.schema, 95a76ab215SJohn Snow output_dir=args.output_dir, 96a76ab215SJohn Snow prefix=args.prefix, 97a76ab215SJohn Snow unmask=args.unmask, 98bd2017bcSVladimir Sementsov-Ogievskiy builtins=args.builtins, 99761a1a48SVladimir Sementsov-Ogievskiy gen_tracing=not args.suppress_tracing) 100a76ab215SJohn Snow except QAPIError as err: 101*bc5d3031SMarkus Armbruster print(err, file=sys.stderr) 102a76ab215SJohn Snow return 1 103a76ab215SJohn Snow return 0 104