| 1 |
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- |
| 2 |
# vim: set filetype=python: |
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
option( |
| 8 |
env="MOZ_FETCHES_DIR", |
| 9 |
nargs=1, |
| 10 |
when=moz_automation, |
| 11 |
help="Directory containing fetched artifacts", |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
@depends("MOZ_FETCHES_DIR", when=moz_automation) |
| 16 |
def moz_fetches_dir(value): |
| 17 |
if value: |
| 18 |
return value[0] |
| 19 |
|
| 20 |
|
| 21 |
@depends(vcs_checkout_type, milestone.is_nightly, moz_automation) |
| 22 |
def bootstrap_default(vcs_checkout_type, is_nightly, automation): |
| 23 |
if automation: |
| 24 |
return False |
| 25 |
# We only enable if building off a VCS checkout of central. |
| 26 |
if is_nightly and vcs_checkout_type: |
| 27 |
return True |
| 28 |
|
| 29 |
|
| 30 |
option( |
| 31 |
"--enable-bootstrap", |
| 32 |
nargs="*", |
| 33 |
default=bootstrap_default, |
| 34 |
help="{Automatically bootstrap or update some toolchains|Disable bootstrap or update of toolchains}", |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
@depends_if("--enable-bootstrap") |
| 39 |
def want_bootstrap(bootstrap): |
| 40 |
include = set() |
| 41 |
exclude = set() |
| 42 |
for item in bootstrap: |
| 43 |
if item == "no-update": |
| 44 |
continue |
| 45 |
if item.startswith("-"): |
| 46 |
exclude.add(item.lstrip("-")) |
| 47 |
else: |
| 48 |
include.add(item) |
| 49 |
|
| 50 |
def match(name): |
| 51 |
if name in exclude: |
| 52 |
return False |
| 53 |
if include and name in include: |
| 54 |
return True |
| 55 |
return not bool(include) |
| 56 |
|
| 57 |
return match |
| 58 |
|
| 59 |
|
| 60 |
toolchains_base_dir = moz_fetches_dir | mozbuild_state_path |
| 61 |
|
| 62 |
|
| 63 |
@dependable |
| 64 |
@imports("os") |
| 65 |
@imports(_from="os", _import="environ") |
| 66 |
def original_path(): |
| 67 |
return environ["PATH"].split(os.pathsep) |
| 68 |
|
| 69 |
|
| 70 |
@depends(host, when="--enable-bootstrap") |
| 71 |
@imports("os") |
| 72 |
@imports("traceback") |
| 73 |
@imports(_from="mozbuild.toolchains", _import="toolchain_task_definitions") |
| 74 |
@imports(_from="__builtin__", _import="Exception") |
| 75 |
def bootstrap_toolchain_tasks(host): |
| 76 |
prefix = { |
| 77 |
("x86_64", "GNU", "Linux"): "linux64", |
| 78 |
("aarch64", "GNU", "Linux"): "linux64-aarch64", |
| 79 |
("x86_64", "OSX", "Darwin"): "macosx64", |
| 80 |
("aarch64", "OSX", "Darwin"): "macosx64-aarch64", |
| 81 |
("x86_64", "WINNT", "WINNT"): "win64", |
| 82 |
("aarch64", "WINNT", "WINNT"): "win64-aarch64", |
| 83 |
}.get((host.cpu, host.os, host.kernel)) |
| 84 |
try: |
| 85 |
tasks = toolchain_task_definitions() |
| 86 |
except Exception as e: |
| 87 |
message = traceback.format_exc() |
| 88 |
log.warning(str(e)) |
| 89 |
log.debug(message) |
| 90 |
return None |
| 91 |
|
| 92 |
def task_data(t): |
| 93 |
result = { |
| 94 |
"index": t.optimization["index-search"], |
| 95 |
"artifact": t.attributes["toolchain-artifact"], |
| 96 |
"extract": t.attributes.get("toolchain-extract", True), |
| 97 |
} |
| 98 |
command = t.attributes.get("toolchain-command") |
| 99 |
if command: |
| 100 |
result["command"] = command |
| 101 |
return result |
| 102 |
|
| 103 |
# We only want to use toolchains annotated with "local-toolchain". We also limit the |
| 104 |
# amount of data to what we use, so that trace logs can be more useful. |
| 105 |
tasks = { |
| 106 |
k: task_data(t) |
| 107 |
for k, t in tasks.items() |
| 108 |
if t.attributes.get("local-toolchain") and "index-search" in t.optimization |
| 109 |
} |
| 110 |
|
| 111 |
return namespace(prefix=prefix, tasks=tasks) |
| 112 |
|
| 113 |
|
| 114 |
@template |
| 115 |
def bootstrap_path(path, **kwargs): |
| 116 |
when = kwargs.pop("when", None) |
| 117 |
allow_failure = kwargs.pop("allow_failure", None) |
| 118 |
if kwargs: |
| 119 |
configure_error( |
| 120 |
"bootstrap_path only takes `when` and `allow_failure` as a keyword argument" |
| 121 |
) |
| 122 |
|
| 123 |
@depends( |
| 124 |
"--enable-bootstrap", |
| 125 |
want_bootstrap, |
| 126 |
toolchains_base_dir, |
| 127 |
moz_fetches_dir, |
| 128 |
bootstrap_toolchain_tasks, |
| 129 |
build_environment, |
| 130 |
dependable(path), |
| 131 |
dependable(allow_failure), |
| 132 |
when=when, |
| 133 |
) |
| 134 |
@imports("os") |
| 135 |
@imports("subprocess") |
| 136 |
@imports("sys") |
| 137 |
@imports(_from="mozbuild.dirutils", _import="ensureParentDir") |
| 138 |
@imports(_from="importlib", _import="import_module") |
| 139 |
@imports(_from="shutil", _import="rmtree") |
| 140 |
@imports(_from="__builtin__", _import="open") |
| 141 |
@imports(_from="__builtin__", _import="Exception") |
| 142 |
def bootstrap_path( |
| 143 |
enable_bootstrap, |
| 144 |
want_bootstrap, |
| 145 |
toolchains_base_dir, |
| 146 |
moz_fetches_dir, |
| 147 |
tasks, |
| 148 |
build_env, |
| 149 |
path, |
| 150 |
allow_failure, |
| 151 |
): |
| 152 |
if not path: |
| 153 |
return |
| 154 |
path_parts = path.split("/") |
| 155 |
path_prefix = "" |
| 156 |
# Small hack until clang-tidy stops being a separate toolchain in a |
| 157 |
# weird location. |
| 158 |
if path_parts[0] == "clang-tools": |
| 159 |
path_prefix = path_parts.pop(0) |
| 160 |
|
| 161 |
def try_bootstrap(exists): |
| 162 |
if not tasks: |
| 163 |
return False |
| 164 |
prefixes = [""] |
| 165 |
if tasks.prefix: |
| 166 |
prefixes.insert(0, "{}-".format(tasks.prefix)) |
| 167 |
for prefix in prefixes: |
| 168 |
label = "toolchain-{}{}".format(prefix, path_parts[0]) |
| 169 |
task = tasks.tasks.get(label) |
| 170 |
if task: |
| 171 |
break |
| 172 |
log.debug("Trying to bootstrap %s", label) |
| 173 |
if not task: |
| 174 |
return False |
| 175 |
task_index = task["index"] |
| 176 |
log.debug("Resolved %s to %s", label, task_index[0]) |
| 177 |
task_index = task_index[0].split(".")[-1] |
| 178 |
artifact = task["artifact"] |
| 179 |
# `mach artifact toolchain` doesn't support authentication for |
| 180 |
# private artifacts. Some toolchains may provide a command that can be |
| 181 |
# used for local production of the artifact. |
| 182 |
command = None |
| 183 |
if not artifact.startswith("public/"): |
| 184 |
command = task.get("command") |
| 185 |
if not command: |
| 186 |
log.debug("Cannot bootstrap %s: not a public artifact", label) |
| 187 |
return False |
| 188 |
index_file = os.path.join(toolchains_base_dir, "indices", path_parts[0]) |
| 189 |
try: |
| 190 |
with open(index_file) as fh: |
| 191 |
index = fh.read().strip() |
| 192 |
except Exception: |
| 193 |
# On automation, if there's an artifact in MOZ_FETCHES_DIR, we assume it's |
| 194 |
# up-to-date. |
| 195 |
index = task_index if moz_fetches_dir else None |
| 196 |
if index == task_index and exists: |
| 197 |
log.debug("%s is up-to-date", label) |
| 198 |
return True |
| 199 |
# Manually import with import_module so that we can gracefully disable bootstrap |
| 200 |
# when e.g. building from a js standalone tarball, that doesn't contain the |
| 201 |
# taskgraph code. In those cases, `mach artifact toolchain --from-build` would |
| 202 |
# also fail. |
| 203 |
task_id = None |
| 204 |
if not command: |
| 205 |
try: |
| 206 |
IndexSearch = import_module( |
| 207 |
"taskgraph.optimize.strategies" |
| 208 |
).IndexSearch |
| 209 |
except Exception: |
| 210 |
log.debug("Cannot bootstrap %s: missing taskgraph module", label) |
| 211 |
return False |
| 212 |
task_id = IndexSearch().should_replace_task( |
| 213 |
task, {}, None, task["index"] |
| 214 |
) |
| 215 |
if task_id: |
| 216 |
# If we found the task in the index, use the `mach artifact toolchain` |
| 217 |
# fast path. |
| 218 |
command = [ |
| 219 |
"artifact", |
| 220 |
"toolchain", |
| 221 |
"--from-task", |
| 222 |
f"{task_id}:{artifact}", |
| 223 |
] |
| 224 |
if not task["extract"]: |
| 225 |
command.append("--no-unpack") |
| 226 |
|
| 227 |
elif command: |
| 228 |
# For private local toolchains, run the associated command. |
| 229 |
command = ( |
| 230 |
[ |
| 231 |
"python", |
| 232 |
"--virtualenv", |
| 233 |
"build", |
| 234 |
os.path.join( |
| 235 |
build_env.topsrcdir, |
| 236 |
"taskcluster/scripts/misc", |
| 237 |
command["script"], |
| 238 |
), |
| 239 |
] |
| 240 |
+ command["arguments"] |
| 241 |
+ [path_parts[0]] |
| 242 |
) |
| 243 |
# Clean up anything that was bootstrapped previously before going |
| 244 |
# forward. In other cases, that's taken care of by mach artifact toolchain. |
| 245 |
rmtree( |
| 246 |
os.path.join(toolchains_base_dir, path_prefix, path_parts[0]), |
| 247 |
ignore_errors=True, |
| 248 |
) |
| 249 |
else: |
| 250 |
# Otherwise, use the slower path, which will print a better error than |
| 251 |
# we would be able to. |
| 252 |
command = ["artifact", "toolchain", "--from-build", label] |
| 253 |
|
| 254 |
log.info( |
| 255 |
"%s bootstrapped toolchain in %s", |
| 256 |
"Updating" if exists else "Installing", |
| 257 |
os.path.join(toolchains_base_dir, path_prefix, path_parts[0]), |
| 258 |
) |
| 259 |
os.makedirs(os.path.join(toolchains_base_dir, path_prefix), exist_ok=True) |
| 260 |
proc = subprocess.run( |
| 261 |
[ |
| 262 |
sys.executable, |
| 263 |
os.path.join(build_env.topsrcdir, "mach"), |
| 264 |
"--log-no-times", |
| 265 |
] |
| 266 |
+ command, |
| 267 |
cwd=os.path.join(toolchains_base_dir, path_prefix), |
| 268 |
check=not allow_failure, |
| 269 |
) |
| 270 |
if proc.returncode != 0 and allow_failure: |
| 271 |
return False |
| 272 |
ensureParentDir(index_file) |
| 273 |
with open(index_file, "w") as fh: |
| 274 |
fh.write(task_index) |
| 275 |
return True |
| 276 |
|
| 277 |
path = os.path.join(toolchains_base_dir, path_prefix, *path_parts) |
| 278 |
if enable_bootstrap and want_bootstrap(path_parts[0]): |
| 279 |
exists = os.path.exists(path) |
| 280 |
try: |
| 281 |
# With --enable-bootstrap=no-update, we don't `try_bootstrap`, except |
| 282 |
# when the toolchain can't be found. |
| 283 |
if ( |
| 284 |
"no-update" not in enable_bootstrap or not exists |
| 285 |
) and not try_bootstrap(exists): |
| 286 |
# If there aren't toolchain artifacts to use for this build, |
| 287 |
# don't return a path. |
| 288 |
return None |
| 289 |
except Exception as e: |
| 290 |
log.error("%s", e) |
| 291 |
die( |
| 292 |
"If you can't fix the above, retry with --enable-bootstrap=no-update." |
| 293 |
) |
| 294 |
if enable_bootstrap or enable_bootstrap.origin == "default": |
| 295 |
# We re-test whether the path exists because it may have been created by |
| 296 |
# try_bootstrap. Automation will not have gone through the bootstrap |
| 297 |
# process, but we want to return the path if it exists. |
| 298 |
if os.path.exists(path): |
| 299 |
return path |
| 300 |
|
| 301 |
return bootstrap_path |
| 302 |
|
| 303 |
|
| 304 |
@template |
| 305 |
def bootstrap_search_path(path, paths=original_path, **kwargs): |
| 306 |
@depends( |
| 307 |
bootstrap_path(path, **kwargs), |
| 308 |
paths, |
| 309 |
original_path, |
| 310 |
) |
| 311 |
def bootstrap_search_path(path, paths, original_path): |
| 312 |
if paths is None: |
| 313 |
paths = original_path |
| 314 |
if not path: |
| 315 |
return paths |
| 316 |
return [path] + paths |
| 317 |
|
| 318 |
return bootstrap_search_path |