Takuto Ikuta | 237cc46 | 2022-01-19 18:04:15 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 2 | # Copyright (c) 2017 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 | """ |
| 7 | This script (intended to be invoked by autoninja or autoninja.bat) detects |
Simeon Anfinrud | 5dba9c9 | 2020-09-03 20:02:05 +0000 | [diff] [blame] | 8 | whether a build is accelerated using a service like goma. If so, it runs with a |
| 9 | large -j value, and otherwise it chooses a small one. This auto-adjustment |
| 10 | makes using remote build acceleration simpler and safer, and avoids errors that |
| 11 | can cause slow goma builds or swap-storms on unaccelerated builds. |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 12 | """ |
| 13 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 14 | from __future__ import print_function |
| 15 | |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 16 | import multiprocessing |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 17 | import os |
Takuto Ikuta | 6a1494e | 2022-05-06 01:22:16 +0000 | [diff] [blame] | 18 | import platform |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 19 | import re |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 20 | import subprocess |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 21 | import sys |
| 22 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 23 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 24 | |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 25 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 26 | def main(args): |
| 27 | # The -t tools are incompatible with -j |
| 28 | t_specified = False |
| 29 | j_specified = False |
| 30 | offline = False |
| 31 | output_dir = '.' |
| 32 | input_args = args |
| 33 | # On Windows the autoninja.bat script passes along the arguments enclosed in |
| 34 | # double quotes. This prevents multiple levels of parsing of the special '^' |
| 35 | # characters needed when compiling a single file but means that this script |
| 36 | # gets called with a single argument containing all of the actual arguments, |
| 37 | # separated by spaces. When this case is detected we need to do argument |
| 38 | # splitting ourselves. This means that arguments containing actual spaces are |
| 39 | # not supported by autoninja, but that is not a real limitation. |
| 40 | if (sys.platform.startswith('win') and len(args) == 2 |
| 41 | and input_args[1].count(' ') > 0): |
| 42 | input_args = args[:1] + args[1].split() |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 43 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 44 | # Ninja uses getopt_long, which allow to intermix non-option arguments. |
| 45 | # To leave non supported parameters untouched, we do not use getopt. |
| 46 | for index, arg in enumerate(input_args[1:]): |
| 47 | if arg.startswith('-j'): |
| 48 | j_specified = True |
| 49 | if arg.startswith('-t'): |
| 50 | t_specified = True |
| 51 | if arg == '-C': |
| 52 | # + 1 to get the next argument and +1 because we trimmed off input_args[0] |
| 53 | output_dir = input_args[index + 2] |
| 54 | elif arg.startswith('-C'): |
| 55 | # Support -Cout/Default |
| 56 | output_dir = arg[2:] |
| 57 | elif arg in ('-o', '--offline'): |
| 58 | offline = True |
| 59 | elif arg == '-h': |
| 60 | print('autoninja: Use -o/--offline to temporary disable goma.', |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 61 | file=sys.stderr) |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 62 | print(file=sys.stderr) |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 63 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 64 | # Strip -o/--offline so ninja doesn't see them. |
| 65 | input_args = [arg for arg in input_args if arg not in ('-o', '--offline')] |
Allen Bauer | 75fa855 | 2018-11-07 22:43:39 +0000 | [diff] [blame] | 66 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 67 | use_goma = False |
| 68 | use_remoteexec = False |
Peter McNeeley | 958dc62 | 2020-10-18 17:05:04 +0000 | [diff] [blame] | 69 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 70 | # Currently get reclient binary and config dirs relative to output_dir. If |
| 71 | # they exist and using remoteexec, then automatically call bootstrap to start |
| 72 | # reproxy. This works under the current assumption that the output |
| 73 | # directory is two levels up from chromium/src. |
| 74 | reclient_bin_dir = os.path.join(output_dir, '..', '..', 'buildtools', |
| 75 | 'reclient') |
| 76 | reclient_cfg = os.path.join(output_dir, '..', '..', 'buildtools', |
| 77 | 'reclient_cfgs', 'reproxy.cfg') |
Peter McNeeley | 958dc62 | 2020-10-18 17:05:04 +0000 | [diff] [blame] | 78 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 79 | # Attempt to auto-detect remote build acceleration. We support gn-based |
| 80 | # builds, where we look for args.gn in the build tree, and cmake-based builds |
| 81 | # where we look for rules.ninja. |
| 82 | if os.path.exists(os.path.join(output_dir, 'args.gn')): |
| 83 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 84 | for line in file_handle: |
Richard Wang | bb07d9e | 2022-07-07 02:28:59 +0000 | [diff] [blame] | 85 | # Either use_goma or use_remoteexec will activate build acceleration. |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 86 | # |
| 87 | # This test can match multi-argument lines. Examples of this are: |
| 88 | # is_debug=false use_goma=true is_official_build=false |
| 89 | # use_goma=false# use_goma=true This comment is ignored |
| 90 | # |
| 91 | # Anything after a comment is not consider a valid argument. |
| 92 | line_without_comment = line.split('#')[0] |
| 93 | if re.search(r'(^|\s)(use_goma)\s*=\s*true($|\s)', |
| 94 | line_without_comment): |
| 95 | use_goma = True |
| 96 | continue |
Richard Wang | bb07d9e | 2022-07-07 02:28:59 +0000 | [diff] [blame] | 97 | if re.search(r'(^|\s)(use_remoteexec)\s*=\s*true($|\s)', |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 98 | line_without_comment): |
| 99 | use_remoteexec = True |
| 100 | continue |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 101 | else: |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 102 | for relative_path in [ |
| 103 | '', # GN keeps them in the root of output_dir |
| 104 | 'CMakeFiles' |
| 105 | ]: |
| 106 | path = os.path.join(output_dir, relative_path, 'rules.ninja') |
| 107 | if os.path.exists(path): |
| 108 | with open(path) as file_handle: |
| 109 | for line in file_handle: |
| 110 | if re.match(r'^\s*command\s*=\s*\S+gomacc', line): |
| 111 | use_goma = True |
| 112 | break |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 113 | |
Michael Savigny | 1cf1fb5 | 2022-08-18 19:57:21 +0000 | [diff] [blame] | 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 August 2022, 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 | if (not offline and use_remoteexec and ( |
| 122 | not os.path.exists(reclient_bin_dir) or not os.path.exists(reclient_cfg)) |
| 123 | ): |
Michael Spang | f467089 | 2022-10-26 17:35:08 +0000 | [diff] [blame] | 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 | if sys.platform.startswith('win'): |
| 130 | # Set an exit code of 1 in the batch file. |
| 131 | print('cmd "/c exit 1"') |
| 132 | else: |
| 133 | # Set an exit code of 1 by executing 'false' in the bash script. |
| 134 | print('false') |
| 135 | sys.exit(1) |
Michael Savigny | 1cf1fb5 | 2022-08-18 19:57:21 +0000 | [diff] [blame] | 136 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 137 | # If GOMA_DISABLED is set to "true", "t", "yes", "y", or "1" |
| 138 | # (case-insensitive) then gomacc will use the local compiler instead of doing |
| 139 | # a goma compile. This is convenient if you want to briefly disable goma. It |
| 140 | # avoids having to rebuild the world when transitioning between goma/non-goma |
| 141 | # builds. However, it is not as fast as doing a "normal" non-goma build |
| 142 | # because an extra process is created for each compile step. Checking this |
| 143 | # environment variable ensures that autoninja uses an appropriate -j value in |
| 144 | # this situation. |
| 145 | goma_disabled_env = os.environ.get('GOMA_DISABLED', '0').lower() |
| 146 | if offline or goma_disabled_env in ['true', 't', 'yes', 'y', '1']: |
| 147 | use_goma = False |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 148 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 149 | if use_goma: |
| 150 | gomacc_file = 'gomacc.exe' if sys.platform.startswith('win') else 'gomacc' |
| 151 | goma_dir = os.environ.get('GOMA_DIR', os.path.join(SCRIPT_DIR, '.cipd_bin')) |
| 152 | gomacc_path = os.path.join(goma_dir, gomacc_file) |
| 153 | # Don't invoke gomacc if it doesn't exist. |
| 154 | if os.path.exists(gomacc_path): |
| 155 | # Check to make sure that goma is running. If not, don't start the build. |
| 156 | status = subprocess.call([gomacc_path, 'port'], |
| 157 | stdout=subprocess.PIPE, |
| 158 | stderr=subprocess.PIPE, |
| 159 | shell=False) |
| 160 | if status == 1: |
Michael Spang | f467089 | 2022-10-26 17:35:08 +0000 | [diff] [blame] | 161 | print('Goma is not running. Use "goma_ctl ensure_start" to start it.', |
| 162 | file=sys.stderr) |
| 163 | if sys.platform.startswith('win'): |
| 164 | # Set an exit code of 1 in the batch file. |
| 165 | print('cmd "/c exit 1"') |
| 166 | else: |
| 167 | # Set an exit code of 1 by executing 'false' in the bash script. |
| 168 | print('false') |
| 169 | sys.exit(1) |
Bruce Dawson | b3b46a2 | 2019-09-06 15:57:52 +0000 | [diff] [blame] | 170 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 171 | # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not |
| 172 | # be called back. |
| 173 | ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja' |
| 174 | ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe) |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 175 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 176 | # A large build (with or without goma) tends to hog all system resources. |
| 177 | # Launching the ninja process with 'nice' priorities improves this situation. |
| 178 | prefix_args = [] |
| 179 | if (sys.platform.startswith('linux') |
| 180 | and os.environ.get('NINJA_BUILD_IN_BACKGROUND', '0') == '1'): |
| 181 | # nice -10 is process priority 10 lower than default 0 |
| 182 | # ionice -c 3 is IO priority IDLE |
| 183 | prefix_args = ['nice'] + ['-10'] |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 184 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 185 | # Use absolute path for ninja path, |
| 186 | # or fail to execute ninja if depot_tools is not in PATH. |
| 187 | args = prefix_args + [ninja_exe_path] + input_args[1:] |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 188 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 189 | num_cores = multiprocessing.cpu_count() |
| 190 | if not j_specified and not t_specified: |
| 191 | if use_goma or use_remoteexec: |
| 192 | args.append('-j') |
Takuto Ikuta | 6a1494e | 2022-05-06 01:22:16 +0000 | [diff] [blame] | 193 | default_core_multiplier = 80 |
| 194 | if platform.machine() in ('x86_64', 'AMD64'): |
| 195 | # Assume simultaneous multithreading and therefore half as many cores as |
| 196 | # logical processors. |
| 197 | num_cores //= 2 |
| 198 | |
| 199 | core_multiplier = int( |
| 200 | os.environ.get('NINJA_CORE_MULTIPLIER', default_core_multiplier)) |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 201 | j_value = num_cores * core_multiplier |
| 202 | |
Aleksey Khoroshilov | 1bc3cd2 | 2022-05-09 19:49:42 +0000 | [diff] [blame] | 203 | core_limit = int(os.environ.get('NINJA_CORE_LIMIT', j_value)) |
| 204 | j_value = min(j_value, core_limit) |
| 205 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 206 | if sys.platform.startswith('win'): |
| 207 | # On windows, j value higher than 1000 does not improve build |
| 208 | # performance. |
| 209 | j_value = min(j_value, 1000) |
Sylvain Defresne | cb2cef9 | 2022-05-10 08:57:20 +0000 | [diff] [blame] | 210 | elif sys.platform == 'darwin': |
| 211 | # On macOS, j value higher than 800 causes 'Too many open files' error |
| 212 | # (crbug.com/936864). |
| 213 | j_value = min(j_value, 800) |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 214 | |
| 215 | args.append('%d' % j_value) |
| 216 | else: |
| 217 | j_value = num_cores |
| 218 | # Ninja defaults to |num_cores + 2| |
| 219 | j_value += int(os.environ.get('NINJA_CORE_ADDITION', '2')) |
| 220 | args.append('-j') |
| 221 | args.append('%d' % j_value) |
| 222 | |
| 223 | # On Windows, fully quote the path so that the command processor doesn't think |
| 224 | # the whole output is the command. |
| 225 | # On Linux and Mac, if people put depot_tools in directories with ' ', |
| 226 | # shell would misunderstand ' ' as a path separation. |
| 227 | # TODO(yyanagisawa): provide proper quoting for Windows. |
| 228 | # see https://cs.chromium.org/chromium/src/tools/mb/mb.py |
| 229 | for i in range(len(args)): |
| 230 | if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]: |
| 231 | args[i] = '"%s"' % args[i].replace('"', '\\"') |
| 232 | |
| 233 | if os.environ.get('NINJA_SUMMARIZE_BUILD', '0') == '1': |
| 234 | args += ['-d', 'stats'] |
| 235 | |
| 236 | # If using remoteexec and the necessary environment variables are set, |
| 237 | # also start reproxy (via bootstrap) before running ninja. |
| 238 | if (not offline and use_remoteexec and os.path.exists(reclient_bin_dir) |
| 239 | and os.path.exists(reclient_cfg)): |
| 240 | bootstrap = os.path.join(reclient_bin_dir, 'bootstrap') |
| 241 | setup_args = [ |
| 242 | bootstrap, '--cfg=' + reclient_cfg, |
| 243 | '--re_proxy=' + os.path.join(reclient_bin_dir, 'reproxy') |
| 244 | ] |
| 245 | |
| 246 | teardown_args = [bootstrap, '--cfg=' + reclient_cfg, '--shutdown'] |
| 247 | |
| 248 | cmd_sep = '\n' if sys.platform.startswith('win') else '&&' |
| 249 | args = setup_args + [cmd_sep] + args + [cmd_sep] + teardown_args |
| 250 | |
| 251 | if offline and not sys.platform.startswith('win'): |
| 252 | # Tell goma or reclient to do local compiles. On Windows these environment |
| 253 | # variables are set by the wrapper batch file. |
| 254 | return 'RBE_remote_disabled=1 GOMA_DISABLED=1 ' + ' '.join(args) |
| 255 | |
| 256 | return ' '.join(args) |
| 257 | |
| 258 | |
| 259 | if __name__ == '__main__': |
| 260 | print(main(sys.argv)) |