ninja_reclient.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. # Copyright 2023 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. """This script is a wrapper around the ninja.py script that also
  6. handles the client lifecycle safely. It will automatically start
  7. reproxy before running ninja and stop reproxy when ninja stops
  8. for any reason eg. build completes, keyboard interupt etc."""
  9. import hashlib
  10. import os
  11. import subprocess
  12. import sys
  13. import ninja
  14. import gclient_paths
  15. def find_reclient_bin_dir():
  16. tools_path = gclient_paths.GetBuildtoolsPath()
  17. if not tools_path:
  18. return None
  19. reclient_bin_dir = os.path.join(tools_path, 'reclient')
  20. if os.path.isdir(reclient_bin_dir):
  21. return reclient_bin_dir
  22. return None
  23. def find_reclient_cfg():
  24. tools_path = gclient_paths.GetBuildtoolsPath()
  25. if not tools_path:
  26. return None
  27. reclient_cfg = os.path.join(tools_path, 'reclient_cfgs', 'reproxy.cfg')
  28. if os.path.isfile(reclient_cfg):
  29. return reclient_cfg
  30. return None
  31. def run(cmd_args):
  32. if os.environ.get('NINJA_SUMMARIZE_BUILD') == '1':
  33. print(' '.join(cmd_args))
  34. return subprocess.call(cmd_args)
  35. def start_reproxy(reclient_cfg, reclient_bin_dir):
  36. return run([
  37. os.path.join(reclient_bin_dir, 'bootstrap'),
  38. '--re_proxy=' + os.path.join(reclient_bin_dir, 'reproxy'),
  39. '--cfg=' + reclient_cfg
  40. ])
  41. def stop_reproxy(reclient_cfg, reclient_bin_dir):
  42. return run([
  43. os.path.join(reclient_bin_dir, 'bootstrap'), '--shutdown',
  44. '--cfg=' + reclient_cfg
  45. ])
  46. def find_rel_ninja_out_dir(args):
  47. # Ninja uses getopt_long, which allow to intermix non-option arguments.
  48. # To leave non supported parameters untouched, we do not use getopt.
  49. for index, arg in enumerate(args[1:]):
  50. if arg == '-C':
  51. # + 1 to get the next argument and +1 because we trimmed off args[0]
  52. return args[index + 2]
  53. if arg.startswith('-C'):
  54. # Support -Cout/Default
  55. return arg[2:]
  56. return '.'
  57. def set_reproxy_path_flags(out_dir):
  58. """Helper to setup the logs and cache directories for reclient
  59. Creates the following directory structure:
  60. out_dir/
  61. .reproxy_tmp/
  62. logs/
  63. cache/
  64. The following env vars are set if not already set:
  65. RBE_output_dir=out_dir/.reproxy_tmp/logs
  66. RBE_proxy_log_dir=out_dir/.reproxy_tmp/logs
  67. RBE_log_dir=out_dir/.reproxy_tmp/logs
  68. RBE_cache_dir=out_dir/.reproxy_tmp/cache
  69. *Nix Only:
  70. RBE_server_address=unix://out_dir/.reproxy_tmp/reproxy.sock
  71. Windows Only:
  72. RBE_server_address=pipe://md5(out_dir/.reproxy_tmp)/reproxy.pipe
  73. """
  74. tmp_dir = os.path.abspath(os.path.join(out_dir, '.reproxy_tmp'))
  75. os.makedirs(tmp_dir, exist_ok=True)
  76. log_dir = os.path.join(tmp_dir, 'logs')
  77. os.makedirs(log_dir, exist_ok=True)
  78. os.environ.setdefault("RBE_output_dir", log_dir)
  79. os.environ.setdefault("RBE_proxy_log_dir", log_dir)
  80. os.environ.setdefault("RBE_log_dir", log_dir)
  81. cache_dir = os.path.join(tmp_dir, 'cache')
  82. os.makedirs(cache_dir, exist_ok=True)
  83. os.environ.setdefault("RBE_cache_dir", cache_dir)
  84. if sys.platform.startswith('win'):
  85. pipe_dir = hashlib.md5(tmp_dir.encode()).hexdigest()
  86. os.environ.setdefault("RBE_server_address",
  87. "pipe://%s/reproxy.pipe" % pipe_dir)
  88. else:
  89. os.environ.setdefault("RBE_server_address",
  90. "unix://%s/reproxy.sock" % tmp_dir)
  91. def main(argv):
  92. # If use_remoteexec is set, but the reclient binaries or configs don't
  93. # exist, display an error message and stop. Otherwise, the build will
  94. # attempt to run with rewrapper wrapping actions, but will fail with
  95. # possible non-obvious problems.
  96. # As of January 2023, dev builds with reclient are not supported, so
  97. # indicate that use_goma should be swapped for use_remoteexec. This
  98. # message will be changed when dev builds are fully supported.
  99. reclient_bin_dir = find_reclient_bin_dir()
  100. reclient_cfg = find_reclient_cfg()
  101. if reclient_bin_dir is None or reclient_cfg is None:
  102. print(("Build is configured to use reclient but necessary binaries "
  103. "or config files can't be found. Developer builds with "
  104. "reclient are not yet supported. Try regenerating your "
  105. "build with use_goma in place of use_remoteexec for now."),
  106. file=sys.stderr)
  107. return 1
  108. try:
  109. set_reproxy_path_flags(find_rel_ninja_out_dir(argv))
  110. except OSError:
  111. print("Error creating reproxy_tmp in output dir", file=sys.stderr)
  112. return 1
  113. reproxy_ret_code = start_reproxy(reclient_cfg, reclient_bin_dir)
  114. if reproxy_ret_code != 0:
  115. return reproxy_ret_code
  116. try:
  117. return ninja.main(argv)
  118. except KeyboardInterrupt:
  119. print("Caught User Interrupt", file=sys.stderr)
  120. # Suppress python stack trace if ninja is interrupted
  121. return 1
  122. finally:
  123. print("Shutting down reproxy...", file=sys.stderr)
  124. stop_reproxy(reclient_cfg, reclient_bin_dir)
  125. if __name__ == '__main__':
  126. sys.exit(main(sys.argv))