blob: 0cd8b0bebe56b0afb6c0dcb7cd41d2da5cfa8ba6 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleinda35fcf2019-03-07 16:01:15 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Sysroot controller."""
6
Chris McDonald1672ddb2021-07-21 11:48:23 -06007import logging
Michael Mortensen4ccfb082020-01-22 16:24:03 -07008import os
9
Alex Klein8cb365a2019-05-15 16:24:53 -060010from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060011from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060012from chromite.api import validate
Alex Kleina9d500b2019-04-22 15:37:51 -060013from chromite.api.controller import controller_util
Chris McDonald1672ddb2021-07-21 11:48:23 -060014from chromite.api.gen.chromiumos import common_pb2
Will Bradley7e5b8c12019-07-30 12:44:15 -060015from chromite.api.metrics import deserialize_metrics_log
LaMont Jonesfeffd1b2020-08-05 18:24:59 -060016from chromite.lib import binpkg
George Engelbrechtc9a8e812021-06-16 18:14:17 -060017from chromite.lib import build_target_lib
18from chromite.lib import chroot_lib
Alex Kleinda35fcf2019-03-07 16:01:15 -070019from chromite.lib import cros_build_lib
Michael Mortensen798ee192020-01-17 13:04:43 -070020from chromite.lib import goma_lib
Alex Kleinaef41942022-04-19 14:13:17 -060021from chromite.lib import metrics_lib
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070022from chromite.lib import osutils
Alex Kleina9d64602019-05-17 14:55:37 -060023from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070024from chromite.lib import sysroot_lib
25from chromite.service import sysroot
26
Chris McDonald1672ddb2021-07-21 11:48:23 -060027
Alex Klein1699fab2022-09-08 08:46:06 -060028_ACCEPTED_LICENSES = "@CHROMEOS"
Alex Kleinda35fcf2019-03-07 16:01:15 -070029
Alex Kleineaa48532022-07-28 08:51:32 -060030DEFAULT_BACKTRACK = 30
31
Alex Kleind4e1e422019-03-18 16:00:41 -060032
Yoshiki Iguchia43704b2021-12-16 13:31:48 +090033def _GetGomaLogDirectory():
Alex Klein1699fab2022-09-08 08:46:06 -060034 """Get goma's log directory based on the env variables.
Yoshiki Iguchia43704b2021-12-16 13:31:48 +090035
Alex Klein1699fab2022-09-08 08:46:06 -060036 Returns:
37 a string of a directory name where goma's log may exist, or None if no
38 potential directories exist.
39 """
40 # TODO(crbug.com/1045001): Replace environment variable with query to
41 # goma object after goma refactoring allows this.
42 candidates = [
43 "GLOG_log_dir",
44 "GOOGLE_LOG_DIR",
45 "TEST_TMPDIR",
46 "TMPDIR",
47 "TMP",
48 ]
49 for candidate in candidates:
50 value = os.environ.get(candidate)
51 if value and os.path.isdir(value):
52 return value
Yoshiki Iguchia43704b2021-12-16 13:31:48 +090053
Alex Klein1699fab2022-09-08 08:46:06 -060054 # "/tmp" will always exist.
55 return "/tmp"
Yoshiki Iguchia43704b2021-12-16 13:31:48 +090056
57
George Engelbrechtc9a8e812021-06-16 18:14:17 -060058def ExampleGetResponse():
Alex Klein1699fab2022-09-08 08:46:06 -060059 """Give an example response to assemble upstream in caller artifacts."""
60 uabs = common_pb2.UploadedArtifactsByService
61 cabs = common_pb2.ArtifactsByService
62 return uabs.Sysroot(
63 artifacts=[
64 uabs.Sysroot.ArtifactPaths(
65 artifact_type=cabs.Sysroot.ArtifactType.SIMPLE_CHROME_SYSROOT,
66 paths=[
67 common_pb2.Path(
68 path="/tmp/sysroot_chromeos-base_chromeos-chrome.tar.xz",
69 location=common_pb2.Path.OUTSIDE,
70 )
71 ],
72 ),
73 uabs.Sysroot.ArtifactPaths(
74 artifact_type=cabs.Sysroot.ArtifactType.DEBUG_SYMBOLS,
75 paths=[
76 common_pb2.Path(
77 path="/tmp/debug.tgz", location=common_pb2.Path.OUTSIDE
78 )
79 ],
80 ),
81 uabs.Sysroot.ArtifactPaths(
82 artifact_type=cabs.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS,
83 paths=[
84 common_pb2.Path(
85 path="/tmp/debug_breakpad.tar.xz",
86 location=common_pb2.Path.OUTSIDE,
87 )
88 ],
89 ),
90 ]
91 )
George Engelbrechtc9a8e812021-06-16 18:14:17 -060092
93
Alex Klein1699fab2022-09-08 08:46:06 -060094def GetArtifacts(
95 in_proto: common_pb2.ArtifactsByService.Sysroot,
96 chroot: chroot_lib.Chroot,
97 sysroot_class: sysroot_lib.Sysroot,
98 build_target: build_target_lib.BuildTarget,
99 output_dir: str,
100) -> list:
101 """Builds and copies sysroot artifacts to specified output_dir.
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 Copies sysroot artifacts to output_dir, returning a list of (output_dir: str)
104 paths to the desired files.
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 Args:
107 in_proto: Proto request defining reqs.
108 chroot: The chroot class used for these artifacts.
109 sysroot_class: The sysroot class used for these artifacts.
110 build_target: The build target used for these artifacts.
111 output_dir: The path to write artifacts to.
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 Returns:
114 A list of dictionary mappings of ArtifactType to list of paths.
115 """
116 generated = []
117 artifact_types = {
118 in_proto.ArtifactType.SIMPLE_CHROME_SYSROOT: sysroot.CreateSimpleChromeSysroot,
119 in_proto.ArtifactType.CHROME_EBUILD_ENV: sysroot.CreateChromeEbuildEnv,
120 in_proto.ArtifactType.BREAKPAD_DEBUG_SYMBOLS: sysroot.BundleBreakpadSymbols,
121 in_proto.ArtifactType.DEBUG_SYMBOLS: sysroot.BundleDebugSymbols,
122 }
Jack Neus5e56fef2021-06-18 16:57:28 +0000123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 for output_artifact in in_proto.output_artifacts:
125 for artifact_type, func in artifact_types.items():
126 if artifact_type in output_artifact.artifact_types:
127 result = func(chroot, sysroot_class, build_target, output_dir)
128 if result:
129 generated.append(
130 {
131 "paths": [result]
132 if isinstance(result, str)
133 else result,
134 "type": artifact_type,
135 }
136 )
Jack Neus5e56fef2021-06-18 16:57:28 +0000137
Alex Klein1699fab2022-09-08 08:46:06 -0600138 return generated
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600139
140
Alex Klein076841b2019-08-29 15:19:39 -0600141@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600142@validate.require("build_target.name")
Alex Klein231d2da2019-07-22 16:44:45 -0600143@validate.validation_complete
144def Create(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600145 """Create or replace a sysroot."""
146 update_chroot = not input_proto.flags.chroot_current
147 replace_sysroot = input_proto.flags.replace
Alex Kleinda35fcf2019-03-07 16:01:15 -0700148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 build_target = controller_util.ParseBuildTarget(
150 input_proto.build_target, input_proto.profile
151 )
152 package_indexes = [
153 binpkg.PackageIndexInfo.from_protobuf(x)
154 for x in input_proto.package_indexes
155 ]
156 run_configs = sysroot.SetupBoardRunConfig(
157 force=replace_sysroot,
158 upgrade_chroot=update_chroot,
159 package_indexes=package_indexes,
160 backtrack=DEFAULT_BACKTRACK,
161 )
Alex Kleinda35fcf2019-03-07 16:01:15 -0700162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 try:
164 created = sysroot.Create(
165 build_target, run_configs, accept_licenses=_ACCEPTED_LICENSES
166 )
167 except sysroot.Error as e:
168 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 output_proto.sysroot.path = created.path
171 output_proto.sysroot.build_target.name = build_target.name
Alex Kleinda35fcf2019-03-07 16:01:15 -0700172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -0700174
Alex Klein076841b2019-08-29 15:19:39 -0600175
Michael Mortensen98592f62019-09-27 13:34:18 -0600176@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600177@validate.require("build_target.name", "packages")
178@validate.require_each("packages", ["category", "package_name"])
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700179@validate.validation_complete
180def GenerateArchive(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600181 """Generate a sysroot. Typically used by informational builders."""
182 build_target_name = input_proto.build_target.name
183 pkg_list = []
184 for package in input_proto.packages:
185 pkg_list.append("%s/%s" % (package.category, package.package_name))
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700186
Alex Klein1699fab2022-09-08 08:46:06 -0600187 with osutils.TempDir(delete=False) as temp_output_dir:
188 sysroot_tar_path = sysroot.GenerateArchive(
189 temp_output_dir, build_target_name, pkg_list
190 )
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700191
Alex Klein1699fab2022-09-08 08:46:06 -0600192 # By assigning this Path variable to the tar path, the tar file will be
193 # copied out to the input_proto's ResultPath location.
194 output_proto.sysroot_archive.path = sysroot_tar_path
195 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700196
197
Alex Klein076841b2019-08-29 15:19:39 -0600198def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600199 """Mock error response that populates failed packages."""
200 fail = output_proto.failed_package_data.add()
201 fail.name.package_name = "package"
202 fail.name.category = "category"
203 fail.name.version = "1.0.0_rc-r1"
204 fail.log_path.path = (
205 "/path/to/package:category-1.0.0_rc-r1:20210609-1337.log"
206 )
207 fail.log_path.location = common_pb2.Path.INSIDE
Lizzy Presland7e23a612021-11-09 21:49:42 +0000208
Alex Klein1699fab2022-09-08 08:46:06 -0600209 fail2 = output_proto.failed_package_data.add()
210 fail2.name.package_name = "bar"
211 fail2.name.category = "foo"
212 fail2.name.version = "3.7-r99"
213 fail2.log_path.path = "/path/to/foo:bar-3.7-r99:20210609-1620.log"
214 fail2.log_path.location = common_pb2.Path.INSIDE
Lizzy Presland7e23a612021-11-09 21:49:42 +0000215
Alex Klein076841b2019-08-29 15:19:39 -0600216
217@faux.empty_success
218@faux.error(_MockFailedPackagesResponse)
Alex Klein1699fab2022-09-08 08:46:06 -0600219@validate.require("sysroot.path", "sysroot.build_target.name")
220@validate.exists("sysroot.path")
Alex Klein231d2da2019-07-22 16:44:45 -0600221@validate.validation_complete
222def InstallToolchain(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600223 """Install the toolchain into a sysroot."""
224 compile_source = (
225 input_proto.flags.compile_source or input_proto.flags.toolchain_changed
226 )
Alex Kleinda35fcf2019-03-07 16:01:15 -0700227
Alex Klein1699fab2022-09-08 08:46:06 -0600228 sysroot_path = input_proto.sysroot.path
Alex Kleinda35fcf2019-03-07 16:01:15 -0700229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 build_target = controller_util.ParseBuildTarget(
231 input_proto.sysroot.build_target
232 )
233 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
234 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700235
Alex Klein1699fab2022-09-08 08:46:06 -0600236 _LogBinhost(build_target.name)
Alex Kleina9d64602019-05-17 14:55:37 -0600237
Alex Klein1699fab2022-09-08 08:46:06 -0600238 try:
239 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
240 except sysroot_lib.ToolchainInstallError as e:
241 controller_util.retrieve_package_log_paths(
242 e.failed_toolchain_info, output_proto, target_sysroot
243 )
Alex Kleinda35fcf2019-03-07 16:01:15 -0700244
Alex Klein1699fab2022-09-08 08:46:06 -0600245 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600246
Alex Klein1699fab2022-09-08 08:46:06 -0600247 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600248
Alex Klein076841b2019-08-29 15:19:39 -0600249
250@faux.empty_success
251@faux.error(_MockFailedPackagesResponse)
Alex Klein1699fab2022-09-08 08:46:06 -0600252@validate.require("sysroot.build_target.name")
253@validate.exists("sysroot.path")
254@validate.require_each("packages", ["category", "package_name"])
255@validate.require_each("use_flags", ["flag"])
Alex Klein231d2da2019-07-22 16:44:45 -0600256@validate.validation_complete
Alex Kleinaef41942022-04-19 14:13:17 -0600257@metrics_lib.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600258def InstallPackages(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600259 """Install packages into a sysroot, building as necessary and permitted."""
260 compile_source = (
261 input_proto.flags.compile_source or input_proto.flags.toolchain_changed
262 )
Joanna Wang1ec0c812021-11-17 17:41:27 -0800263
Alex Klein1699fab2022-09-08 08:46:06 -0600264 use_remoteexec = bool(
265 input_proto.remoteexec_config.reproxy_cfg_file
266 and input_proto.remoteexec_config.reclient_dir
267 )
Joanna Wang1ec0c812021-11-17 17:41:27 -0800268
Alex Klein1699fab2022-09-08 08:46:06 -0600269 # Testing if Goma will support unknown compilers now.
270 use_goma = input_proto.flags.use_goma and not use_remoteexec
Alex Kleind4e1e422019-03-18 16:00:41 -0600271
Alex Klein1699fab2022-09-08 08:46:06 -0600272 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
273 build_target = controller_util.ParseBuildTarget(
274 input_proto.sysroot.build_target
275 )
Alex Kleinca572ee2020-09-03 10:47:14 -0600276
Alex Klein1699fab2022-09-08 08:46:06 -0600277 # Get the package atom for each specified package. The field is optional, so
278 # error only when we cannot parse an atom for each of the given packages.
279 packages = [
280 controller_util.PackageInfoToCPV(x).cp for x in input_proto.packages
281 ]
Alex Kleinca572ee2020-09-03 10:47:14 -0600282
Alex Klein1699fab2022-09-08 08:46:06 -0600283 package_indexes = [
284 binpkg.PackageIndexInfo.from_protobuf(x)
285 for x in input_proto.package_indexes
286 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600287
Alex Klein1699fab2022-09-08 08:46:06 -0600288 # Calculate which packages would have been merged, but don't install anything.
289 dryrun = input_proto.flags.dryrun
Navil Perez5766d1b2021-05-26 17:38:15 +0000290
Alex Klein1699fab2022-09-08 08:46:06 -0600291 if not target_sysroot.IsToolchainInstalled():
292 cros_build_lib.Die("Toolchain must first be installed.")
Alex Kleind4e1e422019-03-18 16:00:41 -0600293
Alex Klein1699fab2022-09-08 08:46:06 -0600294 _LogBinhost(build_target.name)
Alex Kleina9d64602019-05-17 14:55:37 -0600295
Alex Klein1699fab2022-09-08 08:46:06 -0600296 use_flags = [u.flag for u in input_proto.use_flags]
297 build_packages_config = sysroot.BuildPackagesRunConfig(
298 use_any_chrome=False,
299 usepkg=not compile_source,
300 install_debug_symbols=True,
301 packages=packages,
302 package_indexes=package_indexes,
303 use_flags=use_flags,
304 use_goma=use_goma,
305 use_remoteexec=use_remoteexec,
306 incremental_build=False,
307 dryrun=dryrun,
308 backtrack=DEFAULT_BACKTRACK,
309 )
Alex Kleind4e1e422019-03-18 16:00:41 -0600310
Alex Klein1699fab2022-09-08 08:46:06 -0600311 try:
312 sysroot.BuildPackages(
313 build_target, target_sysroot, build_packages_config
314 )
315 except sysroot_lib.PackageInstallError as e:
316 if not e.failed_packages:
317 # No packages to report, so just exit with an error code.
318 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Klein2557b4f2019-07-11 14:34:00 -0600319
Alex Klein1699fab2022-09-08 08:46:06 -0600320 controller_util.retrieve_package_log_paths(
321 e.failed_packages, output_proto, target_sysroot
322 )
Alex Kleind4e1e422019-03-18 16:00:41 -0600323
Alex Klein1699fab2022-09-08 08:46:06 -0600324 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
325 finally:
326 # Copy goma logs to specified directory if there is a goma_config and
327 # it contains a log_dir to store artifacts.
328 if input_proto.goma_config.log_dir.dir:
329 log_source_dir = _GetGomaLogDirectory()
330 archiver = goma_lib.LogsArchiver(
331 log_source_dir,
332 dest_dir=input_proto.goma_config.log_dir.dir,
333 stats_file=input_proto.goma_config.stats_file,
334 counterz_file=input_proto.goma_config.counterz_file,
335 )
336 archiver_tuple = archiver.Archive()
337 if archiver_tuple.stats_file:
338 output_proto.goma_artifacts.stats_file = (
339 archiver_tuple.stats_file
340 )
341 if archiver_tuple.counterz_file:
342 output_proto.goma_artifacts.counterz_file = (
343 archiver_tuple.counterz_file
344 )
345 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
Alex Kleina9d64602019-05-17 14:55:37 -0600346
Alex Klein1699fab2022-09-08 08:46:06 -0600347 # Return without populating the response if it is a dryrun.
348 if dryrun:
349 return controller.RETURN_CODE_SUCCESS
Navil Perez5766d1b2021-05-26 17:38:15 +0000350
Alex Klein1699fab2022-09-08 08:46:06 -0600351 # Read metric events log and pipe them into output_proto.events.
352 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600353
Alex Kleina9d64602019-05-17 14:55:37 -0600354
355def _LogBinhost(board):
Alex Klein1699fab2022-09-08 08:46:06 -0600356 """Log the portage binhost for the given board."""
357 binhost = portage_util.PortageqEnvvar(
358 "PORTAGE_BINHOST", board=board, allow_undefined=True
359 )
360 if not binhost:
361 logging.warning("Portage Binhost not found.")
362 else:
363 logging.info("Portage Binhost: %s", binhost)