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 |
| 18 | import re |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 19 | import subprocess |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 20 | import sys |
| 21 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 22 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 23 | |
Yoshisato Yanagisawa | 0db62fc | 2018-11-01 01:09:53 +0000 | [diff] [blame] | 24 | # The -t tools are incompatible with -j |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 25 | t_specified = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 26 | j_specified = False |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 27 | offline = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 28 | output_dir = '.' |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 29 | input_args = sys.argv |
| 30 | # On Windows the autoninja.bat script passes along the arguments enclosed in |
| 31 | # double quotes. This prevents multiple levels of parsing of the special '^' |
| 32 | # characters needed when compiling a single file but means that this script gets |
| 33 | # called with a single argument containing all of the actual arguments, |
| 34 | # separated by spaces. When this case is detected we need to do argument |
| 35 | # splitting ourselves. This means that arguments containing actual spaces are |
| 36 | # not supported by autoninja, but that is not a real limitation. |
| 37 | if (sys.platform.startswith('win') and len(sys.argv) == 2 and |
| 38 | input_args[1].count(' ') > 0): |
| 39 | input_args = sys.argv[:1] + sys.argv[1].split() |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 40 | |
| 41 | # Ninja uses getopt_long, which allow to intermix non-option arguments. |
| 42 | # To leave non supported parameters untouched, we do not use getopt. |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 43 | for index, arg in enumerate(input_args[1:]): |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 44 | if arg.startswith('-j'): |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 45 | j_specified = True |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 46 | if arg.startswith('-t'): |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 47 | t_specified = True |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 48 | if arg == '-C': |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 49 | # + 1 to get the next argument and +1 because we trimmed off input_args[0] |
| 50 | output_dir = input_args[index + 2] |
Bruce Dawson | 6af3aa8 | 2018-10-03 16:39:42 +0000 | [diff] [blame] | 51 | elif arg.startswith('-C'): |
| 52 | # Support -Cout/Default |
| 53 | output_dir = arg[2:] |
Aravind Vasudevan | c5f0cbb | 2022-01-24 23:56:57 +0000 | [diff] [blame] | 54 | elif arg in ('-o', '--offline'): |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 55 | offline = True |
| 56 | elif arg == '-h': |
| 57 | print('autoninja: Use -o/--offline to temporary disable goma.', |
| 58 | file=sys.stderr) |
| 59 | print(file=sys.stderr) |
| 60 | |
| 61 | # Strip -o/--offline so ninja doesn't see them. |
Aravind Vasudevan | c5f0cbb | 2022-01-24 23:56:57 +0000 | [diff] [blame] | 62 | input_args = [ arg for arg in input_args if arg not in ('-o', '--offline')] |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 63 | |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 64 | use_goma = False |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 65 | use_remoteexec = False |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 66 | |
| 67 | # Currently get reclient binary and config dirs relative to output_dir. If |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 68 | # they exist and using remoteexec, then automatically call bootstrap to start |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 69 | # reproxy. This works under the current assumption that the output |
| 70 | # directory is two levels up from chromium/src. |
Ola Rozenfeld | ca63c05 | 2021-01-26 16:24:10 +0000 | [diff] [blame] | 71 | reclient_bin_dir = os.path.join( |
| 72 | output_dir, '..', '..', 'buildtools', 'reclient') |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 73 | reclient_cfg = os.path.join( |
Ola Rozenfeld | ca63c05 | 2021-01-26 16:24:10 +0000 | [diff] [blame] | 74 | output_dir, '..', '..', 'buildtools', 'reclient_cfgs', 'reproxy.cfg') |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 75 | |
Simeon Anfinrud | 5dba9c9 | 2020-09-03 20:02:05 +0000 | [diff] [blame] | 76 | # Attempt to auto-detect remote build acceleration. We support gn-based |
| 77 | # builds, where we look for args.gn in the build tree, and cmake-based builds |
| 78 | # where we look for rules.ninja. |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 79 | if os.path.exists(os.path.join(output_dir, 'args.gn')): |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 80 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 81 | for line in file_handle: |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 82 | # Either use_goma, use_remoteexec or use_rbe (in deprecation) |
| 83 | # activate build acceleration. |
Peter McNeeley | 958dc62 | 2020-10-18 17:05:04 +0000 | [diff] [blame] | 84 | # |
| 85 | # This test can match multi-argument lines. Examples of this are: |
| 86 | # is_debug=false use_goma=true is_official_build=false |
| 87 | # use_goma=false# use_goma=true This comment is ignored |
| 88 | # |
| 89 | # Anything after a comment is not consider a valid argument. |
| 90 | line_without_comment = line.split('#')[0] |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 91 | if re.search(r'(^|\s)(use_goma)\s*=\s*true($|\s)', |
Peter McNeeley | 958dc62 | 2020-10-18 17:05:04 +0000 | [diff] [blame] | 92 | line_without_comment): |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 93 | use_goma = True |
| 94 | continue |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 95 | if re.search(r'(^|\s)(use_rbe|use_remoteexec)\s*=\s*true($|\s)', |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 96 | line_without_comment): |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 97 | use_remoteexec = True |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 98 | continue |
Vitaly Buka | b2c18f2 | 2020-12-03 23:47:51 +0000 | [diff] [blame] | 99 | else: |
| 100 | for relative_path in [ |
| 101 | '', # GN keeps them in the root of output_dir |
| 102 | 'CMakeFiles' |
| 103 | ]: |
| 104 | path = os.path.join(output_dir, relative_path, 'rules.ninja') |
| 105 | if os.path.exists(path): |
| 106 | with open(path) as file_handle: |
| 107 | for line in file_handle: |
| 108 | if re.match(r'^\s*command\s*=\s*\S+gomacc', line): |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 109 | use_goma = True |
Vitaly Buka | b2c18f2 | 2020-12-03 23:47:51 +0000 | [diff] [blame] | 110 | break |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 111 | |
Bruce Dawson | 6a86032 | 2019-09-09 18:03:40 +0000 | [diff] [blame] | 112 | # If GOMA_DISABLED is set to "true", "t", "yes", "y", or "1" (case-insensitive) |
| 113 | # then gomacc will use the local compiler instead of doing a goma compile. This |
| 114 | # is convenient if you want to briefly disable goma. It avoids having to rebuild |
| 115 | # the world when transitioning between goma/non-goma builds. However, it is not |
| 116 | # as fast as doing a "normal" non-goma build because an extra process is created |
| 117 | # for each compile step. Checking this environment variable ensures that |
| 118 | # autoninja uses an appropriate -j value in this situation. |
| 119 | goma_disabled_env = os.environ.get('GOMA_DISABLED', '0').lower() |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 120 | if offline or goma_disabled_env in ['true', 't', 'yes', 'y', '1']: |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 121 | use_goma = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 122 | |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 123 | if use_goma: |
| 124 | gomacc_file = 'gomacc.exe' if sys.platform.startswith('win') else 'gomacc' |
Brandon DeRosier | 942a1ee | 2021-05-12 18:21:14 +0000 | [diff] [blame] | 125 | goma_dir = os.environ.get('GOMA_DIR', os.path.join(SCRIPT_DIR, '.cipd_bin')) |
| 126 | gomacc_path = os.path.join(goma_dir, gomacc_file) |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 127 | # Don't invoke gomacc if it doesn't exist. |
| 128 | if os.path.exists(gomacc_path): |
| 129 | # Check to make sure that goma is running. If not, don't start the build. |
| 130 | status = subprocess.call([gomacc_path, 'port'], stdout=subprocess.PIPE, |
| 131 | stderr=subprocess.PIPE, shell=False) |
| 132 | if status == 1: |
| 133 | print('Goma is not running. Use "goma_ctl ensure_start" to start it.', |
| 134 | file=sys.stderr) |
| 135 | if sys.platform.startswith('win'): |
| 136 | # Set an exit code of 1 in the batch file. |
Bruce Dawson | c3a48f9 | 2021-03-02 23:22:14 +0000 | [diff] [blame] | 137 | print('cmd "/c exit 1"') |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 138 | else: |
| 139 | # Set an exit code of 1 by executing 'false' in the bash script. |
| 140 | print('false') |
| 141 | sys.exit(1) |
| 142 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 143 | # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not |
| 144 | # be called back. |
| 145 | ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja' |
Allen Bauer | 75fa855 | 2018-11-07 22:43:39 +0000 | [diff] [blame] | 146 | ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe) |
| 147 | |
Peter McNeeley | 958dc62 | 2020-10-18 17:05:04 +0000 | [diff] [blame] | 148 | # A large build (with or without goma) tends to hog all system resources. |
| 149 | # Launching the ninja process with 'nice' priorities improves this situation. |
| 150 | prefix_args = [] |
| 151 | if (sys.platform.startswith('linux') |
| 152 | and os.environ.get('NINJA_BUILD_IN_BACKGROUND', '0') == '1'): |
| 153 | # nice -10 is process priority 10 lower than default 0 |
| 154 | # ionice -c 3 is IO priority IDLE |
| 155 | prefix_args = ['nice'] + ['-10'] |
| 156 | |
| 157 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 158 | # Use absolute path for ninja path, |
| 159 | # or fail to execute ninja if depot_tools is not in PATH. |
Peter McNeeley | 958dc62 | 2020-10-18 17:05:04 +0000 | [diff] [blame] | 160 | args = prefix_args + [ninja_exe_path] + input_args[1:] |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 161 | |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 162 | num_cores = multiprocessing.cpu_count() |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 163 | if not j_specified and not t_specified: |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 164 | if use_goma or use_remoteexec: |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 165 | args.append('-j') |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 166 | core_multiplier = int(os.environ.get('NINJA_CORE_MULTIPLIER', '40')) |
Takuto Ikuta | 1206a35 | 2019-02-07 22:18:08 +0000 | [diff] [blame] | 167 | j_value = num_cores * core_multiplier |
| 168 | |
| 169 | if sys.platform.startswith('win'): |
| 170 | # On windows, j value higher than 1000 does not improve build performance. |
| 171 | j_value = min(j_value, 1000) |
Takuto Ikuta | da4dbf8 | 2019-03-04 03:21:58 +0000 | [diff] [blame] | 172 | elif sys.platform == 'darwin': |
| 173 | # On Mac, j value higher than 500 causes 'Too many open files' error |
| 174 | # (crbug.com/936864). |
| 175 | j_value = min(j_value, 500) |
Takuto Ikuta | 1206a35 | 2019-02-07 22:18:08 +0000 | [diff] [blame] | 176 | |
| 177 | args.append('%d' % j_value) |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 178 | else: |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 179 | j_value = num_cores |
| 180 | # Ninja defaults to |num_cores + 2| |
| 181 | j_value += int(os.environ.get('NINJA_CORE_ADDITION', '2')) |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 182 | args.append('-j') |
| 183 | args.append('%d' % j_value) |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 184 | |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 185 | # On Windows, fully quote the path so that the command processor doesn't think |
| 186 | # the whole output is the command. |
| 187 | # On Linux and Mac, if people put depot_tools in directories with ' ', |
| 188 | # shell would misunderstand ' ' as a path separation. |
Quinten Yearsley | 925cedb | 2020-04-13 17:49:39 +0000 | [diff] [blame] | 189 | # TODO(yyanagisawa): provide proper quoting for Windows. |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 190 | # see https://cs.chromium.org/chromium/src/tools/mb/mb.py |
| 191 | for i in range(len(args)): |
| 192 | if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]: |
| 193 | args[i] = '"%s"' % args[i].replace('"', '\\"') |
| 194 | |
Bruce Dawson | b3b46a2 | 2019-09-06 15:57:52 +0000 | [diff] [blame] | 195 | if os.environ.get('NINJA_SUMMARIZE_BUILD', '0') == '1': |
| 196 | args += ['-d', 'stats'] |
| 197 | |
Bozydar Szadkowski | af9d5b7 | 2022-01-14 02:00:50 +0000 | [diff] [blame] | 198 | # If using remoteexec and the necessary environment variables are set, |
| 199 | # also start reproxy (via bootstrap) before running ninja. |
| 200 | if (not offline and use_remoteexec and os.path.exists(reclient_bin_dir) |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 201 | and os.path.exists(reclient_cfg)): |
Ola Rozenfeld | ca63c05 | 2021-01-26 16:24:10 +0000 | [diff] [blame] | 202 | bootstrap = os.path.join(reclient_bin_dir, 'bootstrap') |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 203 | setup_args = [ |
Ola Rozenfeld | ca63c05 | 2021-01-26 16:24:10 +0000 | [diff] [blame] | 204 | bootstrap, |
| 205 | '--cfg=' + reclient_cfg, |
| 206 | '--re_proxy=' + os.path.join(reclient_bin_dir, 'reproxy')] |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 207 | |
Ola Rozenfeld | ca63c05 | 2021-01-26 16:24:10 +0000 | [diff] [blame] | 208 | teardown_args = [bootstrap, '--cfg=' + reclient_cfg, '--shutdown'] |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 209 | |
Ola Rozenfeld | ca63c05 | 2021-01-26 16:24:10 +0000 | [diff] [blame] | 210 | cmd_sep = '\n' if sys.platform.startswith('win') else '&&' |
| 211 | args = setup_args + [cmd_sep] + args + [cmd_sep] + teardown_args |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 212 | |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 213 | if offline and not sys.platform.startswith('win'): |
Michael Savigny | 684460d | 2020-12-01 19:39:52 +0000 | [diff] [blame] | 214 | # Tell goma or reclient to do local compiles. On Windows these environment |
| 215 | # variables are set by the wrapper batch file. |
| 216 | print('RBE_remote_disabled=1 GOMA_DISABLED=1 ' + ' '.join(args)) |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 217 | else: |
| 218 | print(' '.join(args)) |