main.py 3.1 KB

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