Takuto Ikuta | 0d0f28a | 2023-11-28 05:49:19 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env vpython3 |
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 | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 11 | |
| 12 | autoninja tries to detect relevant build settings such as use_remoteexec, and it |
| 13 | does handle import statements, but it can't handle conditional setting of build |
| 14 | settings. |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 15 | """ |
| 16 | |
Takuto Ikuta | 0d0f28a | 2023-11-28 05:49:19 +0000 | [diff] [blame^] | 17 | import json |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 18 | import multiprocessing |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 19 | import os |
Takuto Ikuta | 6a1494e | 2022-05-06 01:22:16 +0000 | [diff] [blame] | 20 | import platform |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 21 | import re |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 22 | import shlex |
Takuto Ikuta | 0d0f28a | 2023-11-28 05:49:19 +0000 | [diff] [blame^] | 23 | import shutil |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 24 | import subprocess |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 25 | import sys |
Takuto Ikuta | 0d0f28a | 2023-11-28 05:49:19 +0000 | [diff] [blame^] | 26 | import warnings |
| 27 | |
| 28 | import google.auth |
| 29 | from google.auth.transport.requests import AuthorizedSession |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 30 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 31 | import autosiso |
| 32 | import ninja |
| 33 | import ninja_reclient |
| 34 | import siso |
| 35 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 36 | if sys.platform in ["darwin", "linux"]: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 37 | import resource |
Sylvain Defresne | 7b4ecc7 | 2023-07-27 16:24:54 +0000 | [diff] [blame] | 38 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 39 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 40 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 41 | # See [1] and [2] for the painful details of this next section, which handles |
| 42 | # escaping command lines so that they can be copied and pasted into a cmd |
| 43 | # window. |
| 44 | # |
| 45 | # pylint: disable=line-too-long |
| 46 | # [1] https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way # noqa |
| 47 | # [2] https://web.archive.org/web/20150815000000*/https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx # noqa |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 48 | _UNSAFE_FOR_CMD = set("^<>&|()%") |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 49 | _ALL_META_CHARS = _UNSAFE_FOR_CMD.union(set('"')) |
| 50 | |
| 51 | |
Takuto Ikuta | 0d0f28a | 2023-11-28 05:49:19 +0000 | [diff] [blame^] | 52 | def _adc_account(): |
| 53 | """Returns account used to authenticate with GCP application default credentials.""" |
| 54 | |
| 55 | try: |
| 56 | # Suppress warnings from google.auth.default. |
| 57 | # https://github.com/googleapis/google-auth-library-python/issues/271 |
| 58 | warnings.filterwarnings( |
| 59 | "ignore", |
| 60 | "Your application has authenticated using end user credentials from" |
| 61 | " Google Cloud SDK without a quota project.", |
| 62 | ) |
| 63 | credentials, _ = google.auth.default( |
| 64 | scopes=["https://www.googleapis.com/auth/userinfo.email"]) |
| 65 | except google.auth.exceptions.DefaultCredentialsError: |
| 66 | # Application Default Crendetials is not configured. |
| 67 | return None |
| 68 | finally: |
| 69 | warnings.resetwarnings() |
| 70 | |
| 71 | with AuthorizedSession(credentials) as session: |
| 72 | response = session.get("https://www.googleapis.com/oauth2/v1/userinfo") |
| 73 | return response.json().get("email") |
| 74 | |
| 75 | |
| 76 | def _gcloud_auth_account(): |
| 77 | """Returns active account authenticated with `gcloud auth login`.""" |
| 78 | if shutil.which("gcloud") is None: |
| 79 | return None |
| 80 | |
| 81 | accounts = json.loads( |
| 82 | subprocess.check_output("gcloud auth list --format=json", |
| 83 | shell=True, |
| 84 | text=True)) |
| 85 | for account in accounts: |
| 86 | if account["status"] == "ACTIVE": |
| 87 | return account["account"] |
| 88 | return None |
| 89 | |
| 90 | |
| 91 | def _is_google_corp_machine(): |
| 92 | """This assumes that corp machine has gcert binary in known location.""" |
| 93 | return shutil.which("gcert") is not None |
| 94 | |
| 95 | |
| 96 | def _is_google_corp_machine_using_external_account(): |
| 97 | if not _is_google_corp_machine(): |
| 98 | return False |
| 99 | |
| 100 | account = _adc_account() |
| 101 | if account and not account.endswith("@google.com"): |
| 102 | return True |
| 103 | |
| 104 | account = _gcloud_auth_account() |
| 105 | return account and not account.endswith("@google.com") |
| 106 | |
| 107 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 108 | def _quote_for_cmd(arg): |
| 109 | # First, escape the arg so that CommandLineToArgvW will parse it properly. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 110 | if arg == "" or " " in arg or '"' in arg: |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 111 | quote_re = re.compile(r'(\\*)"') |
| 112 | arg = '"%s"' % (quote_re.sub(lambda mo: 2 * mo.group(1) + '\\"', arg)) |
| 113 | |
| 114 | # Then check to see if the arg contains any metacharacters other than |
| 115 | # double quotes; if it does, quote everything (including the double |
| 116 | # quotes) for safety. |
| 117 | if any(a in _UNSAFE_FOR_CMD for a in arg): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 118 | arg = "".join("^" + a if a in _ALL_META_CHARS else a for a in arg) |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 119 | return arg |
| 120 | |
| 121 | |
| 122 | def _print_cmd(cmd): |
| 123 | shell_quoter = shlex.quote |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 124 | if sys.platform.startswith("win"): |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 125 | shell_quoter = _quote_for_cmd |
| 126 | print(*[shell_quoter(arg) for arg in cmd], file=sys.stderr) |
| 127 | |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 128 | |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 129 | def _gn_lines(output_dir, path): |
| 130 | """ |
| 131 | Generator function that returns args.gn lines one at a time, following |
| 132 | import directives as needed. |
| 133 | """ |
| 134 | import_re = re.compile(r'\s*import\("(.*)"\)') |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 135 | with open(path, encoding="utf-8") as f: |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 136 | for line in f: |
| 137 | match = import_re.match(line) |
| 138 | if match: |
Samuel Attard | 8a25998 | 2023-09-18 17:12:55 +0000 | [diff] [blame] | 139 | raw_import_path = match.groups()[0] |
| 140 | if raw_import_path[:2] == "//": |
| 141 | import_path = os.path.normpath( |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 142 | os.path.join(output_dir, "..", "..", |
Samuel Attard | 8a25998 | 2023-09-18 17:12:55 +0000 | [diff] [blame] | 143 | raw_import_path[2:])) |
| 144 | else: |
| 145 | import_path = os.path.normpath( |
| 146 | os.path.join(os.path.dirname(path), raw_import_path)) |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 147 | for import_line in _gn_lines(output_dir, import_path): |
| 148 | yield import_line |
| 149 | else: |
| 150 | yield line |
| 151 | |
| 152 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 153 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 154 | # The -t tools are incompatible with -j |
| 155 | t_specified = False |
| 156 | j_specified = False |
| 157 | offline = False |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 158 | output_dir = "." |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 159 | input_args = args |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 160 | summarize_build = os.environ.get("NINJA_SUMMARIZE_BUILD") == "1" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 161 | # On Windows the autoninja.bat script passes along the arguments enclosed in |
| 162 | # double quotes. This prevents multiple levels of parsing of the special '^' |
| 163 | # characters needed when compiling a single file but means that this script |
| 164 | # gets called with a single argument containing all of the actual arguments, |
| 165 | # separated by spaces. When this case is detected we need to do argument |
| 166 | # splitting ourselves. This means that arguments containing actual spaces |
| 167 | # are not supported by autoninja, but that is not a real limitation. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 168 | if (sys.platform.startswith("win") and len(args) == 2 |
| 169 | and input_args[1].count(" ") > 0): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 170 | input_args = args[:1] + args[1].split() |
Bruce Dawson | 655afeb | 2020-11-02 23:30:37 +0000 | [diff] [blame] | 171 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 172 | # Ninja uses getopt_long, which allow to intermix non-option arguments. |
| 173 | # To leave non supported parameters untouched, we do not use getopt. |
| 174 | for index, arg in enumerate(input_args[1:]): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 175 | if arg.startswith("-j"): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 176 | j_specified = True |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 177 | if arg.startswith("-t"): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 178 | t_specified = True |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 179 | if arg == "-C": |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 180 | # + 1 to get the next argument and +1 because we trimmed off |
| 181 | # input_args[0] |
| 182 | output_dir = input_args[index + 2] |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 183 | elif arg.startswith("-C"): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 184 | # Support -Cout/Default |
| 185 | output_dir = arg[2:] |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 186 | elif arg in ("-o", "--offline"): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 187 | offline = True |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 188 | elif arg == "-h": |
| 189 | print( |
| 190 | "autoninja: Use -o/--offline to temporary disable goma.", |
| 191 | file=sys.stderr, |
| 192 | ) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 193 | print(file=sys.stderr) |
Bruce Dawson | e952fae | 2021-02-27 23:33:37 +0000 | [diff] [blame] | 194 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 195 | use_goma = False |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 196 | use_remoteexec = False |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 197 | use_siso = False |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 198 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 199 | # Attempt to auto-detect remote build acceleration. We support gn-based |
| 200 | # builds, where we look for args.gn in the build tree, and cmake-based |
| 201 | # builds where we look for rules.ninja. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 202 | if os.path.exists(os.path.join(output_dir, "args.gn")): |
| 203 | for line in _gn_lines(output_dir, os.path.join(output_dir, "args.gn")): |
Takuto Ikuta | 5cbc521 | 2023-11-16 02:38:31 +0000 | [diff] [blame] | 204 | # use_goma, or use_remoteexec will activate build |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 205 | # acceleration. |
| 206 | # |
| 207 | # This test can match multi-argument lines. Examples of this |
| 208 | # are: is_debug=false use_goma=true is_official_build=false |
| 209 | # use_goma=false# use_goma=true This comment is ignored |
| 210 | # |
| 211 | # Anything after a comment is not consider a valid argument. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 212 | line_without_comment = line.split("#")[0] |
| 213 | if re.search(r"(^|\s)(use_goma)\s*=\s*true($|\s)", |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 214 | line_without_comment): |
| 215 | use_goma = True |
| 216 | continue |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 217 | if re.search( |
| 218 | r"(^|\s)(use_remoteexec)\s*=\s*true($|\s)", |
| 219 | line_without_comment, |
| 220 | ): |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 221 | use_remoteexec = True |
| 222 | continue |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 223 | if re.search(r"(^|\s)(use_siso)\s*=\s*true($|\s)", |
Bruce Dawson | 30c1cba | 2023-09-15 18:20:32 +0000 | [diff] [blame] | 224 | line_without_comment): |
| 225 | use_siso = True |
| 226 | continue |
Bruce Dawson | b3b46a2 | 2019-09-06 15:57:52 +0000 | [diff] [blame] | 227 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 228 | siso_marker = os.path.join(output_dir, ".siso_deps") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 229 | if use_siso: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 230 | # autosiso generates a .ninja_log file so the mere existence of a |
| 231 | # .ninja_log file doesn't imply that a ninja build was done. However |
| 232 | # if there is a .ninja_log but no .siso_deps then that implies a |
| 233 | # ninja build. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 234 | ninja_marker = os.path.join(output_dir, ".ninja_log") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 235 | if os.path.exists(ninja_marker) and not os.path.exists(siso_marker): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 236 | print( |
| 237 | "Run gn clean before switching from ninja to siso in %s" % |
| 238 | output_dir, |
| 239 | file=sys.stderr, |
| 240 | ) |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 241 | return 1 |
Fumitoshi Ukai | 406be82 | 2023-10-18 04:18:24 +0000 | [diff] [blame] | 242 | if use_goma: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 243 | print("Siso does not support Goma.", file=sys.stderr) |
| 244 | print( |
| 245 | "Do not use use_siso=true and use_goma=true", |
| 246 | file=sys.stderr, |
| 247 | ) |
Fumitoshi Ukai | 406be82 | 2023-10-18 04:18:24 +0000 | [diff] [blame] | 248 | return 1 |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 249 | if use_remoteexec: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 250 | return autosiso.main(["autosiso"] + input_args[1:]) |
| 251 | return siso.main(["siso", "ninja", "--offline"] + input_args[1:]) |
Michael Savigny | 20eda95 | 2021-01-20 01:16:27 +0000 | [diff] [blame] | 252 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 253 | if os.path.exists(siso_marker): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 254 | print( |
| 255 | "Run gn clean before switching from siso to ninja in %s" % |
| 256 | output_dir, |
| 257 | file=sys.stderr, |
| 258 | ) |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 259 | return 1 |
Ben Segall | 467991e | 2023-08-09 19:02:09 +0000 | [diff] [blame] | 260 | |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 261 | else: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 262 | for relative_path in [ |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 263 | "", # GN keeps them in the root of output_dir |
| 264 | "CMakeFiles", |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 265 | ]: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 266 | path = os.path.join(output_dir, relative_path, "rules.ninja") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 267 | if os.path.exists(path): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 268 | with open(path, encoding="utf-8") as file_handle: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 269 | for line in file_handle: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 270 | if re.match(r"^\s*command\s*=\s*\S+gomacc", line): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 271 | use_goma = True |
| 272 | break |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 273 | |
Takuto Ikuta | 0d0f28a | 2023-11-28 05:49:19 +0000 | [diff] [blame^] | 274 | if use_remoteexec or use_siso: |
| 275 | if _is_google_corp_machine_using_external_account(): |
| 276 | print( |
| 277 | "You can't use a non-@google.com account (%s and/or %s) on a" |
| 278 | " corp machine.\n" |
| 279 | "Please login via `gcloud auth login --update-adc` with your" |
| 280 | " @google.com account instead.\n" % |
| 281 | (_adc_account(), _gcloud_auth_account()), |
| 282 | file=sys.stderr, |
| 283 | ) |
| 284 | return 1 |
| 285 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 286 | # Strip -o/--offline so ninja doesn't see them. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 287 | 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] | 288 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 289 | # If GOMA_DISABLED is set to "true", "t", "yes", "y", or "1" |
| 290 | # (case-insensitive) then gomacc will use the local compiler instead of |
| 291 | # doing a goma compile. This is convenient if you want to briefly disable |
| 292 | # goma. It avoids having to rebuild the world when transitioning between |
| 293 | # goma/non-goma builds. However, it is not as fast as doing a "normal" |
| 294 | # non-goma build because an extra process is created for each compile step. |
| 295 | # Checking this environment variable ensures that autoninja uses an |
| 296 | # appropriate -j value in this situation. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 297 | goma_disabled_env = os.environ.get("GOMA_DISABLED", "0").lower() |
| 298 | if offline or goma_disabled_env in ["true", "t", "yes", "y", "1"]: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 299 | use_goma = False |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 300 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 301 | if use_goma: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 302 | gomacc_file = ("gomacc.exe" |
| 303 | if sys.platform.startswith("win") else "gomacc") |
| 304 | goma_dir = os.environ.get("GOMA_DIR", |
| 305 | os.path.join(SCRIPT_DIR, ".cipd_bin")) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 306 | gomacc_path = os.path.join(goma_dir, gomacc_file) |
| 307 | # Don't invoke gomacc if it doesn't exist. |
| 308 | if os.path.exists(gomacc_path): |
| 309 | # Check to make sure that goma is running. If not, don't start the |
| 310 | # build. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 311 | status = subprocess.call( |
| 312 | [gomacc_path, "port"], |
| 313 | stdout=subprocess.PIPE, |
| 314 | stderr=subprocess.PIPE, |
| 315 | shell=False, |
| 316 | ) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 317 | if status == 1: |
| 318 | print( |
| 319 | 'Goma is not running. Use "goma_ctl ensure_start" to start ' |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 320 | "it.", |
| 321 | file=sys.stderr, |
| 322 | ) |
| 323 | if sys.platform.startswith("win"): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 324 | # Set an exit code of 1 in the batch file. |
| 325 | print('cmd "/c exit 1"') |
| 326 | else: |
| 327 | # Set an exit code of 1 by executing 'false' in the bash |
| 328 | # script. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 329 | print("false") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 330 | sys.exit(1) |
| 331 | |
| 332 | # A large build (with or without goma) tends to hog all system resources. |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 333 | # Depending on the operating system, we might have mechanisms available |
| 334 | # to run at a lower priority, which improves this situation. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 335 | if os.environ.get("NINJA_BUILD_IN_BACKGROUND") == "1": |
| 336 | if sys.platform in ["darwin", "linux"]: |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 337 | # nice-level 10 is usually considered a good default for background |
| 338 | # tasks. The niceness is inherited by child processes, so we can |
| 339 | # just set it here for us and it'll apply to the build tool we |
| 340 | # spawn later. |
| 341 | os.nice(10) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 342 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 343 | # Tell goma or reclient to do local compiles. |
| 344 | if offline: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 345 | os.environ["RBE_remote_disabled"] = "1" |
| 346 | os.environ["GOMA_DISABLED"] = "1" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 347 | |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 348 | # On macOS and most Linux distributions, the default limit of open file |
| 349 | # descriptors is too low (256 and 1024, respectively). |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 350 | # This causes a large j value to result in 'Too many open files' errors. |
| 351 | # Check whether the limit can be raised to a large enough value. If yes, |
| 352 | # use `ulimit -n .... &&` as a prefix to increase the limit when running |
| 353 | # ninja. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 354 | if sys.platform in ["darwin", "linux"]: |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 355 | # Increase the number of allowed open file descriptors to the maximum. |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 356 | fileno_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 357 | if fileno_limit < hard_limit: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 358 | try: |
| 359 | resource.setrlimit(resource.RLIMIT_NOFILE, |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 360 | (hard_limit, hard_limit)) |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 361 | except Exception: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 362 | pass |
| 363 | fileno_limit, hard_limit = resource.getrlimit( |
| 364 | resource.RLIMIT_NOFILE) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 365 | |
| 366 | # Call ninja.py so that it can find ninja binary installed by DEPS or one in |
| 367 | # PATH. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 368 | ninja_path = os.path.join(SCRIPT_DIR, "ninja.py") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 369 | # If using remoteexec, use ninja_reclient.py which wraps ninja.py with |
| 370 | # starting and stopping reproxy. |
| 371 | if use_remoteexec: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 372 | ninja_path = os.path.join(SCRIPT_DIR, "ninja_reclient.py") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 373 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 374 | args = [sys.executable, ninja_path] + input_args[1:] |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 375 | |
| 376 | num_cores = multiprocessing.cpu_count() |
| 377 | if not j_specified and not t_specified: |
Takuto Ikuta | 5cbc521 | 2023-11-16 02:38:31 +0000 | [diff] [blame] | 378 | if not offline and (use_goma or use_remoteexec): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 379 | args.append("-j") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 380 | default_core_multiplier = 80 |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 381 | if platform.machine() in ("x86_64", "AMD64"): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 382 | # Assume simultaneous multithreading and therefore half as many |
| 383 | # cores as logical processors. |
| 384 | num_cores //= 2 |
| 385 | |
| 386 | core_multiplier = int( |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 387 | os.environ.get("NINJA_CORE_MULTIPLIER", |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 388 | default_core_multiplier)) |
| 389 | j_value = num_cores * core_multiplier |
| 390 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 391 | core_limit = int(os.environ.get("NINJA_CORE_LIMIT", j_value)) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 392 | j_value = min(j_value, core_limit) |
| 393 | |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 394 | # On Windows, a -j higher than 1000 doesn't improve build times. |
Henrique Ferreiro | 7eb4e48 | 2023-09-20 20:25:20 +0000 | [diff] [blame] | 395 | # On macOS, ninja is limited to at most FD_SETSIZE (1024) open file |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 396 | # descriptors. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 397 | if sys.platform in ["darwin", "win32"]: |
Henrique Ferreiro | 7eb4e48 | 2023-09-20 20:25:20 +0000 | [diff] [blame] | 398 | j_value = min(j_value, 1000) |
| 399 | |
| 400 | # Use a j value that reliably works with the open file descriptors |
| 401 | # limit. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 402 | if sys.platform in ["darwin", "linux"]: |
Henrique Ferreiro | 8bde164 | 2023-09-14 11:41:19 +0000 | [diff] [blame] | 403 | j_value = min(j_value, int(fileno_limit * 0.8)) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 404 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 405 | args.append("%d" % j_value) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 406 | else: |
| 407 | j_value = num_cores |
| 408 | # Ninja defaults to |num_cores + 2| |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 409 | j_value += int(os.environ.get("NINJA_CORE_ADDITION", "2")) |
| 410 | args.append("-j") |
| 411 | args.append("%d" % j_value) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 412 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 413 | if summarize_build: |
| 414 | # Enable statistics collection in Ninja. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 415 | args += ["-d", "stats"] |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 416 | # Print the command-line to reassure the user that the right settings |
| 417 | # are being used. |
| 418 | _print_cmd(args) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 419 | |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 420 | if use_remoteexec: |
| 421 | return ninja_reclient.main(args[1:]) |
| 422 | return ninja.main(args[1:]) |
Takuto Ikuta | 381db68 | 2022-04-27 23:54:02 +0000 | [diff] [blame] | 423 | |
| 424 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 425 | if __name__ == "__main__": |
Philipp Wollermann | 0b94340 | 2023-10-12 07:13:30 +0000 | [diff] [blame] | 426 | try: |
| 427 | sys.exit(main(sys.argv)) |
| 428 | except KeyboardInterrupt: |
| 429 | sys.exit(1) |