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. |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 5 | """ |
| 6 | This script (intended to be invoked by autoninja or autoninja.bat) detects |
Simeon Anfinrud | 5dba9c9 | 2020-09-03 20:02:05 +0000 | [diff] [blame] | 7 | whether a build is accelerated using a service like goma. If so, it runs with a |
| 8 | large -j value, and otherwise it chooses a small one. This auto-adjustment |
| 9 | makes using remote build acceleration simpler and safer, and avoids errors that |
| 10 | can cause slow goma builds or swap-storms on unaccelerated builds. |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 11 | """ |
| 12 | |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 13 | import multiprocessing |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 14 | import os |
Takuto Ikuta | 6a1494e | 2022-05-06 01:22:16 +0000 | [diff] [blame] | 15 | import platform |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 16 | import re |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 17 | import subprocess |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 18 | import sys |
| 19 | |
Sylvain Defresne | 7b4ecc7 | 2023-07-27 16:24:54 +0000 | [diff] [blame] | 20 | if sys.platform == 'darwin': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 21 | import resource |
Sylvain Defresne | 7b4ecc7 | 2023-07-27 16:24:54 +0000 | [diff] [blame] | 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): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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 |
| 39 | # are 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 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +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 |
| 53 | # input_args[0] |
| 54 | output_dir = input_args[index + 2] |
| 55 | elif arg.startswith('-C'): |
| 56 | # Support -Cout/Default |
| 57 | output_dir = arg[2:] |
| 58 | elif arg in ('-o', '--offline'): |
| 59 | offline = True |
| 60 | elif arg == '-h': |
| 61 | print('autoninja: Use -o/--offline to temporary disable goma.', |
| 62 | file=sys.stderr) |
| 63 | print(file=sys.stderr) |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 64 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 65 | use_goma = False |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 66 | use_remoteexec = False |
| 67 | use_rbe = False |
| 68 | use_siso = False |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 69 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 70 | # Attempt to auto-detect remote build acceleration. We support gn-based |
| 71 | # builds, where we look for args.gn in the build tree, and cmake-based |
| 72 | # builds where we look for rules.ninja. |
| 73 | if os.path.exists(os.path.join(output_dir, 'args.gn')): |
| 74 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 75 | for line in file_handle: |
| 76 | # use_goma, use_remoteexec, or use_rbe will activate build |
| 77 | # acceleration. |
| 78 | # |
| 79 | # This test can match multi-argument lines. Examples of this |
| 80 | # are: is_debug=false use_goma=true is_official_build=false |
| 81 | # use_goma=false# use_goma=true This comment is ignored |
| 82 | # |
| 83 | # Anything after a comment is not consider a valid argument. |
| 84 | line_without_comment = line.split('#')[0] |
| 85 | if re.search(r'(^|\s)(use_goma)\s*=\s*true($|\s)', |
| 86 | line_without_comment): |
| 87 | use_goma = True |
| 88 | continue |
| 89 | if re.search(r'(^|\s)(use_remoteexec)\s*=\s*true($|\s)', |
| 90 | line_without_comment): |
| 91 | use_remoteexec = True |
| 92 | continue |
| 93 | if re.search(r'(^|\s)(use_rbe)\s*=\s*true($|\s)', |
| 94 | line_without_comment): |
| 95 | use_rbe = True |
| 96 | continue |
| 97 | if re.search(r'(^|\s)(use_siso)\s*=\s*true($|\s)', |
| 98 | line_without_comment): |
| 99 | use_siso = True |
| 100 | continue |
Bruce Dawson | b3b46a2 | 2019-09-06 15:57:52 +0000 | [diff] [blame] | 101 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 102 | siso_marker = os.path.join(output_dir, '.siso_deps') |
| 103 | if use_siso: |
| 104 | ninja_marker = os.path.join(output_dir, '.ninja_log') |
| 105 | # autosiso generates a .ninja_log file so the mere existence of a |
| 106 | # .ninja_log file doesn't imply that a ninja build was done. However |
| 107 | # if there is a .ninja_log but no .siso_deps then that implies a |
| 108 | # ninja build. |
| 109 | if os.path.exists(ninja_marker) and not os.path.exists(siso_marker): |
| 110 | return ( |
| 111 | 'echo Run gn clean before switching from ninja to siso in ' |
| 112 | '%s' % output_dir) |
| 113 | siso = ['autosiso'] if use_remoteexec else ['siso', 'ninja'] |
| 114 | if sys.platform.startswith('win'): |
| 115 | # An explicit 'call' is needed to make sure the invocation of |
| 116 | # autosiso returns to autoninja.bat, and the command prompt |
| 117 | # title gets reset. |
| 118 | siso = ['call'] + siso |
| 119 | return ' '.join(siso + input_args[1:]) |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 120 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 121 | if os.path.exists(siso_marker): |
| 122 | return ( |
| 123 | 'echo Run gn clean before switching from siso to ninja in %s' % |
| 124 | output_dir) |
Ben Segall | 467991e | 2023-08-09 19:02:09 +0000 | [diff] [blame] | 125 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 126 | else: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 127 | for relative_path in [ |
| 128 | '', # GN keeps them in the root of output_dir |
| 129 | 'CMakeFiles' |
| 130 | ]: |
| 131 | path = os.path.join(output_dir, relative_path, 'rules.ninja') |
| 132 | if os.path.exists(path): |
| 133 | with open(path) as file_handle: |
| 134 | for line in file_handle: |
| 135 | if re.match(r'^\s*command\s*=\s*\S+gomacc', line): |
| 136 | use_goma = True |
| 137 | break |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 138 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 139 | # Strip -o/--offline so ninja doesn't see them. |
| 140 | input_args = [arg for arg in input_args if arg not in ('-o', '--offline')] |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 141 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 142 | # If GOMA_DISABLED is set to "true", "t", "yes", "y", or "1" |
| 143 | # (case-insensitive) then gomacc will use the local compiler instead of |
| 144 | # doing a goma compile. This is convenient if you want to briefly disable |
| 145 | # goma. It avoids having to rebuild the world when transitioning between |
| 146 | # goma/non-goma builds. However, it is not as fast as doing a "normal" |
| 147 | # non-goma build because an extra process is created for each compile step. |
| 148 | # Checking this environment variable ensures that autoninja uses an |
| 149 | # appropriate -j value in this situation. |
| 150 | goma_disabled_env = os.environ.get('GOMA_DISABLED', '0').lower() |
| 151 | if offline or goma_disabled_env in ['true', 't', 'yes', 'y', '1']: |
| 152 | use_goma = False |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 153 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 154 | if use_goma: |
| 155 | gomacc_file = 'gomacc.exe' if sys.platform.startswith( |
| 156 | 'win') else 'gomacc' |
| 157 | goma_dir = os.environ.get('GOMA_DIR', |
| 158 | os.path.join(SCRIPT_DIR, '.cipd_bin')) |
| 159 | gomacc_path = os.path.join(goma_dir, gomacc_file) |
| 160 | # Don't invoke gomacc if it doesn't exist. |
| 161 | if os.path.exists(gomacc_path): |
| 162 | # Check to make sure that goma is running. If not, don't start the |
| 163 | # build. |
| 164 | status = subprocess.call([gomacc_path, 'port'], |
| 165 | stdout=subprocess.PIPE, |
| 166 | stderr=subprocess.PIPE, |
| 167 | shell=False) |
| 168 | if status == 1: |
| 169 | print( |
| 170 | 'Goma is not running. Use "goma_ctl ensure_start" to start ' |
| 171 | 'it.', |
| 172 | file=sys.stderr) |
| 173 | if sys.platform.startswith('win'): |
| 174 | # Set an exit code of 1 in the batch file. |
| 175 | print('cmd "/c exit 1"') |
| 176 | else: |
| 177 | # Set an exit code of 1 by executing 'false' in the bash |
| 178 | # script. |
| 179 | print('false') |
| 180 | sys.exit(1) |
| 181 | |
| 182 | # A large build (with or without goma) tends to hog all system resources. |
| 183 | # Launching the ninja process with 'nice' priorities improves this |
| 184 | # situation. |
| 185 | prefix_args = [] |
| 186 | if (sys.platform.startswith('linux') |
| 187 | and os.environ.get('NINJA_BUILD_IN_BACKGROUND', '0') == '1'): |
| 188 | # nice -10 is process priority 10 lower than default 0 |
| 189 | # ionice -c 3 is IO priority IDLE |
| 190 | prefix_args = ['nice'] + ['-10'] |
| 191 | |
| 192 | # Tell goma or reclient to do local compiles. On Windows these environment |
| 193 | # variables are set by the wrapper batch file. |
| 194 | offline_env = ['RBE_remote_disabled=1', 'GOMA_DISABLED=1' |
| 195 | ] if offline and not sys.platform.startswith('win') else [] |
| 196 | |
| 197 | # On macOS, the default limit of open file descriptors is too low (256). |
| 198 | # This causes a large j value to result in 'Too many open files' errors. |
| 199 | # Check whether the limit can be raised to a large enough value. If yes, |
| 200 | # use `ulimit -n .... &&` as a prefix to increase the limit when running |
| 201 | # ninja. |
| 202 | if sys.platform == 'darwin': |
| 203 | wanted_limit = 200000 # Large enough to avoid any risk of exhaustion. |
| 204 | fileno_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 205 | if fileno_limit <= wanted_limit: |
| 206 | try: |
| 207 | resource.setrlimit(resource.RLIMIT_NOFILE, |
| 208 | (wanted_limit, hard_limit)) |
| 209 | except Exception as _: |
| 210 | pass |
| 211 | fileno_limit, hard_limit = resource.getrlimit( |
| 212 | resource.RLIMIT_NOFILE) |
| 213 | if fileno_limit >= wanted_limit: |
| 214 | prefix_args = ['ulimit', '-n', f'{wanted_limit}', '&&' |
| 215 | ] + offline_env |
| 216 | offline_env = [] |
| 217 | |
| 218 | # Call ninja.py so that it can find ninja binary installed by DEPS or one in |
| 219 | # PATH. |
| 220 | ninja_path = os.path.join(SCRIPT_DIR, 'ninja.py') |
| 221 | # If using remoteexec, use ninja_reclient.py which wraps ninja.py with |
| 222 | # starting and stopping reproxy. |
| 223 | if use_remoteexec: |
| 224 | ninja_path = os.path.join(SCRIPT_DIR, 'ninja_reclient.py') |
| 225 | |
| 226 | args = offline_env + prefix_args + [sys.executable, ninja_path |
| 227 | ] + input_args[1:] |
| 228 | |
| 229 | num_cores = multiprocessing.cpu_count() |
| 230 | if not j_specified and not t_specified: |
| 231 | if not offline and (use_goma or use_remoteexec or use_rbe): |
| 232 | args.append('-j') |
| 233 | default_core_multiplier = 80 |
| 234 | if platform.machine() in ('x86_64', 'AMD64'): |
| 235 | # Assume simultaneous multithreading and therefore half as many |
| 236 | # cores as logical processors. |
| 237 | num_cores //= 2 |
| 238 | |
| 239 | core_multiplier = int( |
| 240 | os.environ.get('NINJA_CORE_MULTIPLIER', |
| 241 | default_core_multiplier)) |
| 242 | j_value = num_cores * core_multiplier |
| 243 | |
| 244 | core_limit = int(os.environ.get('NINJA_CORE_LIMIT', j_value)) |
| 245 | j_value = min(j_value, core_limit) |
| 246 | |
| 247 | if sys.platform.startswith('win'): |
| 248 | # On windows, j value higher than 1000 does not improve build |
| 249 | # performance. |
| 250 | j_value = min(j_value, 1000) |
| 251 | elif sys.platform == 'darwin': |
| 252 | # If the number of open file descriptors is large enough (or it |
| 253 | # can be raised to a large enough value), then set j value to |
| 254 | # 1000. This limit comes from ninja which is limited to at most |
| 255 | # FD_SETSIZE (1024) open file descriptors (using 1000 leave a |
| 256 | # bit of head room). |
| 257 | # |
| 258 | # If the number of open file descriptors cannot be raised, then |
| 259 | # use a j value of 200 which is the maximum value that reliably |
| 260 | # work with the default limit of 256. |
| 261 | if fileno_limit >= wanted_limit: |
| 262 | j_value = min(j_value, 1000) |
| 263 | else: |
| 264 | j_value = min(j_value, 200) |
| 265 | |
| 266 | args.append('%d' % j_value) |
| 267 | else: |
| 268 | j_value = num_cores |
| 269 | # Ninja defaults to |num_cores + 2| |
| 270 | j_value += int(os.environ.get('NINJA_CORE_ADDITION', '2')) |
| 271 | args.append('-j') |
| 272 | args.append('%d' % j_value) |
| 273 | |
| 274 | # On Windows, fully quote the path so that the command processor doesn't |
| 275 | # think the whole output is the command. On Linux and Mac, if people put |
| 276 | # depot_tools in directories with ' ', shell would misunderstand ' ' as a |
| 277 | # path separation. TODO(yyanagisawa): provide proper quoting for Windows. |
| 278 | # see https://cs.chromium.org/chromium/src/tools/mb/mb.py |
| 279 | for i in range(len(args)): |
| 280 | if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]: |
| 281 | args[i] = '"%s"' % args[i].replace('"', '\\"') |
| 282 | |
| 283 | if os.environ.get('NINJA_SUMMARIZE_BUILD', '0') == '1': |
| 284 | args += ['-d', 'stats'] |
| 285 | |
| 286 | return ' '.join(args) |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 287 | |
| 288 | |
| 289 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 290 | print(main(sys.argv)) |