blob: 2a62888b188b6265e69c8577f0d87c64a5a738a3 [file] [log] [blame]
Ben Segalleb2866e2023-01-20 20:14:44 +00001#!/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
6handles the client lifecycle safely. It will automatically start
7reproxy before running ninja and stop reproxy when ninja stops
8for any reason eg. build completes, keyboard interupt etc."""
9
10import os
11import subprocess
12import sys
13
14import ninja
15import gclient_paths
16
17
18def find_reclient_bin_dir():
19 tools_path = gclient_paths.GetBuildtoolsPath()
20 if not tools_path:
21 return None
22
23 reclient_bin_dir = os.path.join(tools_path, 'reclient')
24 if os.path.isdir(reclient_bin_dir):
25 return reclient_bin_dir
26 return None
27
28
29def find_reclient_cfg():
30 tools_path = gclient_paths.GetBuildtoolsPath()
31 if not tools_path:
32 return None
33
34 reclient_cfg = os.path.join(tools_path, 'reclient_cfgs', 'reproxy.cfg')
35 if os.path.isfile(reclient_cfg):
36 return reclient_cfg
37 return None
38
39
40def run(cmd_args):
41 if os.environ.get('NINJA_SUMMARIZE_BUILD') == '1':
42 print(' '.join(cmd_args))
43 return subprocess.call(cmd_args)
44
45
46def start_reproxy(reclient_cfg, reclient_bin_dir):
47 return run([
48 os.path.join(reclient_bin_dir, 'bootstrap'),
49 '--re_proxy=' + os.path.join(reclient_bin_dir, 'reproxy'),
50 '--cfg=' + reclient_cfg
51 ])
52
53
54def stop_reproxy(reclient_cfg, reclient_bin_dir):
55 return run([
56 os.path.join(reclient_bin_dir, 'bootstrap'), '--shutdown',
57 '--cfg=' + reclient_cfg
58 ])
59
60
61def main(argv):
62 # If use_remoteexec is set, but the reclient binaries or configs don't
63 # exist, display an error message and stop. Otherwise, the build will
64 # attempt to run with rewrapper wrapping actions, but will fail with
65 # possible non-obvious problems.
66 # As of January 2023, dev builds with reclient are not supported, so
67 # indicate that use_goma should be swapped for use_remoteexec. This
68 # message will be changed when dev builds are fully supported.
69 reclient_bin_dir = find_reclient_bin_dir()
70 reclient_cfg = find_reclient_cfg()
71 if reclient_bin_dir is None or reclient_cfg is None:
72 print(("Build is configured to use reclient but necessary binaries "
73 "or config files can't be found. Developer builds with "
74 "reclient are not yet supported. Try regenerating your "
75 "build with use_goma in place of use_remoteexec for now."),
76 file=sys.stderr)
77 return 1
78 reproxy_ret_code = start_reproxy(reclient_cfg, reclient_bin_dir)
79 if reproxy_ret_code != 0:
80 return reproxy_ret_code
81 try:
82 return ninja.main(argv)
83 except KeyboardInterrupt:
84 # Suppress python stack trace if ninja is interrupted
85 return 1
86 finally:
87 stop_reproxy(reclient_cfg, reclient_bin_dir)
88
89
90if __name__ == '__main__':
91 sys.exit(main(sys.argv))