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