wayland_scanner_wrapper.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # Copyright 2018 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. Script to run wayland-scaner for wayland_protocol.gni.
  7. """
  8. from __future__ import print_function
  9. import argparse
  10. import os.path
  11. import subprocess
  12. import sys
  13. def generate_code(wayland_scanner_cmd, code_type, path_in, path_out):
  14. ret = subprocess.call([wayland_scanner_cmd, code_type, path_in, path_out])
  15. if ret != 0:
  16. raise RuntimeError("wayland-scanner returned an error: %d" % ret)
  17. def main(argv):
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument("--cmd", help="wayland-scanner command to execute")
  20. parser.add_argument("--src-root", help="Root source directory")
  21. parser.add_argument("--root-gen-dir", help="Directory for generated files")
  22. parser.add_argument("protocols", nargs="+",
  23. help="Input protocol file paths relative to src root.")
  24. options = parser.parse_args()
  25. cmd = os.path.realpath(options.cmd)
  26. src_root = options.src_root
  27. root_gen_dir = options.root_gen_dir
  28. protocols = options.protocols
  29. version = subprocess.check_output([cmd, '--version'],
  30. stderr=subprocess.STDOUT).decode('utf-8')
  31. # The version is of the form "wayland-scanner 1.18.0\n"
  32. version = tuple([int(x) for x in version.strip().split(' ')[1].split('.')])
  33. for protocol in protocols:
  34. protocol_path = os.path.join(src_root, protocol)
  35. protocol_without_extension = protocol.rsplit(".", 1)[0]
  36. out_base_name = os.path.join(root_gen_dir, protocol_without_extension)
  37. code_cmd = 'private-code' if version > (1, 14, 90) else 'code'
  38. generate_code(cmd, code_cmd, protocol_path,
  39. out_base_name + "-protocol.c")
  40. generate_code(cmd, "client-header", protocol_path,
  41. out_base_name + "-client-protocol.h")
  42. generate_code(cmd, "server-header", protocol_path,
  43. out_base_name + "-server-protocol.h")
  44. if __name__ == "__main__":
  45. try:
  46. main(sys.argv)
  47. except RuntimeError as e:
  48. print(e, file=sys.stderr)
  49. sys.exit(1)