main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. from importlib import import_module
  9. import sys
  10. from typing import Optional
  11. from .backend import QAPIBackend, QAPICBackend
  12. from .common import must_match
  13. from .error import QAPIError
  14. from .schema import QAPISchema
  15. def invalid_prefix_char(prefix: str) -> Optional[str]:
  16. match = must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
  17. if match.end() != len(prefix):
  18. return prefix[match.end()]
  19. return None
  20. def create_backend(path: str) -> QAPIBackend:
  21. if path is None:
  22. return QAPICBackend()
  23. module_path, dot, class_name = path.rpartition('.')
  24. if not dot:
  25. print("argument of -B must be of the form MODULE.CLASS",
  26. file=sys.stderr)
  27. sys.exit(1)
  28. try:
  29. mod = import_module(module_path)
  30. except Exception as ex:
  31. print(f"unable to import '{module_path}': {ex}", file=sys.stderr)
  32. sys.exit(1)
  33. try:
  34. klass = getattr(mod, class_name)
  35. except AttributeError:
  36. print(f"module '{module_path}' has no class '{class_name}'",
  37. file=sys.stderr)
  38. sys.exit(1)
  39. try:
  40. backend = klass()
  41. except Exception as ex:
  42. print(f"backend '{path}' cannot be instantiated: {ex}",
  43. file=sys.stderr)
  44. sys.exit(1)
  45. if not isinstance(backend, QAPIBackend):
  46. print(f"backend '{path}' must be an instance of QAPIBackend",
  47. file=sys.stderr)
  48. sys.exit(1)
  49. return backend
  50. def main() -> int:
  51. """
  52. gapi-gen executable entry point.
  53. Expects arguments via sys.argv, see --help for details.
  54. :return: int, 0 on success, 1 on failure.
  55. """
  56. parser = argparse.ArgumentParser(
  57. description='Generate code from a QAPI schema')
  58. parser.add_argument('-b', '--builtins', action='store_true',
  59. help="generate code for built-in types")
  60. parser.add_argument('-o', '--output-dir', action='store',
  61. default='',
  62. help="write output to directory OUTPUT_DIR")
  63. parser.add_argument('-p', '--prefix', action='store',
  64. default='',
  65. help="prefix for symbols")
  66. parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
  67. dest='unmask',
  68. help="expose non-ABI names in introspection")
  69. parser.add_argument('-B', '--backend', default=None,
  70. help="Python module name for code generator")
  71. # Option --suppress-tracing exists so we can avoid solving build system
  72. # problems. TODO Drop it when we no longer need it.
  73. parser.add_argument('--suppress-tracing', action='store_true',
  74. help="suppress adding trace events to qmp marshals")
  75. parser.add_argument('schema', action='store')
  76. args = parser.parse_args()
  77. funny_char = invalid_prefix_char(args.prefix)
  78. if funny_char:
  79. msg = f"funny character '{funny_char}' in argument of --prefix"
  80. print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
  81. return 1
  82. try:
  83. schema = QAPISchema(args.schema)
  84. backend = create_backend(args.backend)
  85. backend.generate(schema,
  86. output_dir=args.output_dir,
  87. prefix=args.prefix,
  88. unmask=args.unmask,
  89. builtins=args.builtins,
  90. gen_tracing=not args.suppress_tracing)
  91. except QAPIError as err:
  92. print(err, file=sys.stderr)
  93. return 1
  94. return 0