Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/transforms/run/toolchain.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 """
5 Support for running toolchain-building tasks via dedicated scripts
6 """
7
8 from voluptuous import ALLOW_EXTRA, Any, Optional, Required
9
10 import taskgraph
11 from taskgraph.transforms.run import configure_taskdesc_for_run, run_task_using
12 from taskgraph.transforms.run.common import (
13 docker_worker_add_artifacts,
14 generic_worker_add_artifacts,
15 get_vcsdir_name,
16 )
17 from taskgraph.util import path as mozpath
18 from taskgraph.util.hash import hash_paths
19 from taskgraph.util.schema import Schema
20 from taskgraph.util.shell import quote as shell_quote
21
22 CACHE_TYPE = "toolchains.v3"
23
24 toolchain_run_schema = Schema(
25 {
26 Required("using"): "toolchain-script",
27 # The script (in taskcluster/scripts/misc) to run.
28 Required("script"): str,
29 # Arguments to pass to the script.
30 Optional("arguments"): [str],
31 # Sparse profile to give to checkout using `run-task`. If given,
32 # a filename in `build/sparse-profiles`. Defaults to
33 # "toolchain-build", i.e., to
34 # `build/sparse-profiles/toolchain-build`. If `None`, instructs
35 # `run-task` to not use a sparse profile at all.
36 Required("sparse-profile"): Any(str, None),
37 # Paths/patterns pointing to files that influence the outcome of a
38 # toolchain build.
39 Optional("resources"): [str],
40 # Path to the artifact produced by the toolchain task
41 Required("toolchain-artifact"): str,
42 Optional(
43 "toolchain-alias",
44 description="An alias that can be used instead of the real toolchain task name in "
45 "fetch stanzas for tasks.",
46 ): Any(str, [str]),
47 Optional(
48 "toolchain-env",
49 description="Additional env variables to add to the worker when using this toolchain",
50 ): {str: object},
51 # Base work directory used to set up the task.
52 Required("workdir"): str,
53 },
54 extra=ALLOW_EXTRA,
55 )
56
57
58 def get_digest_data(config, run, taskdesc):
59 files = list(run.pop("resources", []))
60 # The script
61 script = mozpath.join("taskcluster/scripts/toolchain/", run["script"])
62 files.append(mozpath.normpath(script))
63
64 # Accumulate dependency hashes for index generation.
65 data = [hash_paths(config.graph_config.vcs_root, files)]
66
67 data.append(taskdesc["attributes"]["toolchain-artifact"])
68
69 # If the task uses an in-tree docker image, we want it to influence
70 # the index path as well. Ideally, the content of the docker image itself
71 # should have an influence, but at the moment, we can't get that
72 # information here. So use the docker image name as a proxy. Not a lot of
73 # changes to docker images actually have an impact on the resulting
74 # toolchain artifact, so we'll just rely on such important changes to be
75 # accompanied with a docker image name change.
76 image = taskdesc["worker"].get("docker-image", {}).get("in-tree")
77 if image:
78 data.append(image)
79
80 # Likewise script arguments should influence the index.
81 args = run.get("arguments")
82 if args:
83 data.extend(args)
84 return data
85
86
87 def common_toolchain(config, task, taskdesc, is_docker):
88 run = task["run"]
89
90 worker = taskdesc["worker"] = task["worker"]
91 worker["chain-of-trust"] = True
92
93 srcdir = get_vcsdir_name(worker["os"])
94
95 if is_docker:
96 # If the task doesn't have a docker-image, set a default
97 worker.setdefault("docker-image", {"in-tree": "toolchain-build"})
98
99 # Allow the task to specify where artifacts come from, but add
100 # public/build if it's not there already.
101 artifacts = worker.setdefault("artifacts", [])
102 if not any(artifact.get("name") == "public/build" for artifact in artifacts):
103 if is_docker:
104 docker_worker_add_artifacts(config, task, taskdesc)
105 else:
106 generic_worker_add_artifacts(config, task, taskdesc)
107
108 env = worker["env"]
109 env.update(
110 {
111 "MOZ_BUILD_DATE": config.params["moz_build_date"],
112 "MOZ_SCM_LEVEL": config.params["level"],
113 }
114 )
115
116 attributes = taskdesc.setdefault("attributes", {})
117 attributes["toolchain-artifact"] = run.pop("toolchain-artifact")
118 if "toolchain-alias" in run:
119 attributes["toolchain-alias"] = run.pop("toolchain-alias")
120 if "toolchain-env" in run:
121 attributes["toolchain-env"] = run.pop("toolchain-env")
122
123 if not taskgraph.fast:
124 name = taskdesc["label"].replace(f"{config.kind}-", "", 1)
125 taskdesc["cache"] = {
126 "type": CACHE_TYPE,
127 "name": name,
128 "digest-data": get_digest_data(config, run, taskdesc),
129 }
130
131 script = mozpath.join("taskcluster/scripts/toolchain/", run.pop("script"))
132 run["using"] = "run-task"
133 run["cwd"] = "{checkout}/.."
134
135 if script.endswith(".ps1"):
136 run["exec-with"] = "powershell"
137
138 command = [f"{srcdir}/{mozpath.normpath(script)}"] + run.pop("arguments", [])
139
140 if not is_docker:
141 # Don't quote the first item in the command because it purposely contains
142 # an environment variable that is not meant to be quoted.
143 if len(command) > 1:
144 command = command[0] + " " + shell_quote(*command[1:])
145 else:
146 command = command[0]
147
148 run["command"] = command
149
150 configure_taskdesc_for_run(config, task, taskdesc, worker["implementation"])
151
152
153 toolchain_defaults = {
154 "sparse-profile": "toolchain-build",
155 }
156
157
158 @run_task_using(
159 "docker-worker",
160 "toolchain-script",
161 schema=toolchain_run_schema,
162 defaults=toolchain_defaults,
163 )
164 def docker_worker_toolchain(config, task, taskdesc):
165 common_toolchain(config, task, taskdesc, is_docker=True)
166
167
168 @run_task_using(
169 "generic-worker",
170 "toolchain-script",
171 schema=toolchain_run_schema,
172 defaults=toolchain_defaults,
173 )
174 def generic_worker_toolchain(config, task, taskdesc):
175 common_toolchain(config, task, taskdesc, is_docker=False)