main.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 sys
  9. from typing import Optional
  10. from .commands import gen_commands
  11. from .common import must_match
  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 = must_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,
  28. gen_tracing: bool = False) -> None:
  29. """
  30. Generate C code for the given schema into the target directory.
  31. :param schema_file: The primary QAPI schema file.
  32. :param output_dir: The output directory to store generated code.
  33. :param prefix: Optional C-code prefix for symbol names.
  34. :param unmask: Expose non-ABI names through introspection?
  35. :param builtins: Generate code for built-in types?
  36. :raise QAPIError: On failures.
  37. """
  38. assert invalid_prefix_char(prefix) is None
  39. schema = QAPISchema(schema_file)
  40. gen_types(schema, output_dir, prefix, builtins)
  41. gen_visit(schema, output_dir, prefix, builtins)
  42. gen_commands(schema, output_dir, prefix, gen_tracing)
  43. gen_events(schema, output_dir, prefix)
  44. gen_introspect(schema, output_dir, prefix, unmask)
  45. def main() -> int:
  46. """
  47. gapi-gen executable entry point.
  48. Expects arguments via sys.argv, see --help for details.
  49. :return: int, 0 on success, 1 on failure.
  50. """
  51. parser = argparse.ArgumentParser(
  52. description='Generate code from a QAPI schema')
  53. parser.add_argument('-b', '--builtins', action='store_true',
  54. help="generate code for built-in types")
  55. parser.add_argument('-o', '--output-dir', action='store',
  56. default='',
  57. help="write output to directory OUTPUT_DIR")
  58. parser.add_argument('-p', '--prefix', action='store',
  59. default='',
  60. help="prefix for symbols")
  61. parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
  62. dest='unmask',
  63. help="expose non-ABI names in introspection")
  64. # Option --gen-trace exists so we can avoid solving build system
  65. # problems. TODO Drop it when we no longer need it.
  66. parser.add_argument('--gen-trace', action='store_true',
  67. help="add trace events to qmp marshals")
  68. parser.add_argument('schema', action='store')
  69. args = parser.parse_args()
  70. funny_char = invalid_prefix_char(args.prefix)
  71. if funny_char:
  72. msg = f"funny character '{funny_char}' in argument of --prefix"
  73. print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
  74. return 1
  75. try:
  76. generate(args.schema,
  77. output_dir=args.output_dir,
  78. prefix=args.prefix,
  79. unmask=args.unmask,
  80. builtins=args.builtins,
  81. gen_tracing=args.gen_trace)
  82. except QAPIError as err:
  83. print(f"{sys.argv[0]}: {str(err)}", file=sys.stderr)
  84. return 1
  85. return 0