blob: 332b7a37584ce9834dbcb50b9c907c07e6a0a19c [file] [log] [blame]
Alex Kleinda35fcf2019-03-07 16:01:15 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# 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 Kleinda35fcf2019-03-07 16:01:15 -070028_ACCEPTED_LICENSES = '@CHROMEOS'
29
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():
34 """Get goma's log directory based on the env variables.
35
36 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 = [
Alex Kleind066b0f2022-07-28 08:47:35 -060043 'GLOG_log_dir',
44 'GOOGLE_LOG_DIR',
45 'TEST_TMPDIR',
46 'TMPDIR',
47 'TMP',
48 ]
Yoshiki Iguchia43704b2021-12-16 13:31:48 +090049 for candidate in candidates:
50 value = os.environ.get(candidate)
51 if value and os.path.isdir(value):
52 return value
53
54 # "/tmp" will always exist.
55 return '/tmp'
56
57
George Engelbrechtc9a8e812021-06-16 18:14:17 -060058def ExampleGetResponse():
59 """Give an example response to assemble upstream in caller artifacts."""
60 uabs = common_pb2.UploadedArtifactsByService
61 cabs = common_pb2.ArtifactsByService
62 return uabs.Sysroot(artifacts=[
Lizzy Preslandb2d60642021-11-12 01:41:58 +000063 uabs.Sysroot.ArtifactPaths(
George Engelbrecht35eeea42021-07-09 14:10:57 -060064 artifact_type=cabs.Sysroot.ArtifactType.SIMPLE_CHROME_SYSROOT,
65 paths=[
66 common_pb2.Path(
67 path='/tmp/sysroot_chromeos-base_chromeos-chrome.tar.xz',
68 location=common_pb2.Path.OUTSIDE)
69 ],
70 ),
George Engelbrechtc9a8e812021-06-16 18:14:17 -060071 uabs.Sysroot.ArtifactPaths(
72 artifact_type=cabs.Sysroot.ArtifactType.DEBUG_SYMBOLS,
73 paths=[
74 common_pb2.Path(
75 path='/tmp/debug.tgz', location=common_pb2.Path.OUTSIDE)
76 ],
77 ),
78 uabs.Sysroot.ArtifactPaths(
79 artifact_type=cabs.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS,
80 paths=[
81 common_pb2.Path(
82 path='/tmp/debug_breakpad.tar.xz',
83 location=common_pb2.Path.OUTSIDE)
84 ])
85 ])
86
87
88def GetArtifacts(in_proto: common_pb2.ArtifactsByService.Sysroot,
Lizzy Preslandb2d60642021-11-12 01:41:58 +000089 chroot: chroot_lib.Chroot, sysroot_class: sysroot_lib.Sysroot,
Alex Kleind066b0f2022-07-28 08:47:35 -060090 build_target: build_target_lib.BuildTarget,
91 output_dir: str) -> list:
George Engelbrechtc9a8e812021-06-16 18:14:17 -060092 """Builds and copies sysroot artifacts to specified output_dir.
93
94 Copies sysroot artifacts to output_dir, returning a list of (output_dir: str)
95 paths to the desired files.
96
97 Args:
98 in_proto: Proto request defining reqs.
99 chroot: The chroot class used for these artifacts.
100 sysroot_class: The sysroot class used for these artifacts.
101 build_target: The build target used for these artifacts.
102 output_dir: The path to write artifacts to.
103
104 Returns:
105 A list of dictionary mappings of ArtifactType to list of paths.
106 """
107 generated = []
Jack Neus5e56fef2021-06-18 16:57:28 +0000108 artifact_types = {
Lizzy Preslandb2d60642021-11-12 01:41:58 +0000109 in_proto.ArtifactType.SIMPLE_CHROME_SYSROOT:
110 sysroot.CreateSimpleChromeSysroot,
Alex Kleind066b0f2022-07-28 08:47:35 -0600111 in_proto.ArtifactType.CHROME_EBUILD_ENV:
112 sysroot.CreateChromeEbuildEnv,
Lizzy Preslandb2d60642021-11-12 01:41:58 +0000113 in_proto.ArtifactType.BREAKPAD_DEBUG_SYMBOLS:
114 sysroot.BundleBreakpadSymbols,
Alex Kleind066b0f2022-07-28 08:47:35 -0600115 in_proto.ArtifactType.DEBUG_SYMBOLS:
116 sysroot.BundleDebugSymbols,
Jack Neus5e56fef2021-06-18 16:57:28 +0000117 }
118
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600119 for output_artifact in in_proto.output_artifacts:
Jack Neus5e56fef2021-06-18 16:57:28 +0000120 for artifact_type, func in artifact_types.items():
121 if artifact_type in output_artifact.artifact_types:
122 result = func(chroot, sysroot_class, build_target, output_dir)
123 if result:
124 generated.append({
125 'paths': [result] if isinstance(result, str) else result,
126 'type': artifact_type,
127 })
128
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600129 return generated
130
131
Alex Klein076841b2019-08-29 15:19:39 -0600132@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -0600133@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600134@validate.validation_complete
135def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -0700136 """Create or replace a sysroot."""
137 update_chroot = not input_proto.flags.chroot_current
138 replace_sysroot = input_proto.flags.replace
139
Alex Klein26e472b2020-03-10 14:35:01 -0600140 build_target = controller_util.ParseBuildTarget(input_proto.build_target,
141 input_proto.profile)
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600142 package_indexes = [
143 binpkg.PackageIndexInfo.from_protobuf(x)
144 for x in input_proto.package_indexes
145 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600146 run_configs = sysroot.SetupBoardRunConfig(
Alex Kleind066b0f2022-07-28 08:47:35 -0600147 force=replace_sysroot,
148 upgrade_chroot=update_chroot,
149 package_indexes=package_indexes,
Alex Kleineaa48532022-07-28 08:51:32 -0600150 backtrack=DEFAULT_BACKTRACK,
Alex Kleind066b0f2022-07-28 08:47:35 -0600151 )
Alex Kleinda35fcf2019-03-07 16:01:15 -0700152
153 try:
Alex Kleind066b0f2022-07-28 08:47:35 -0600154 created = sysroot.Create(
155 build_target, run_configs, accept_licenses=_ACCEPTED_LICENSES)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700156 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -0400157 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700158
159 output_proto.sysroot.path = created.path
Alex Klein26e472b2020-03-10 14:35:01 -0600160 output_proto.sysroot.build_target.name = build_target.name
Alex Kleinda35fcf2019-03-07 16:01:15 -0700161
Alex Klein076841b2019-08-29 15:19:39 -0600162 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -0700163
Alex Klein076841b2019-08-29 15:19:39 -0600164
Michael Mortensen98592f62019-09-27 13:34:18 -0600165@faux.all_empty
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700166@validate.require('build_target.name', 'packages')
Alex Klein45b73432020-09-23 13:51:20 -0600167@validate.require_each('packages', ['category', 'package_name'])
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700168@validate.validation_complete
169def GenerateArchive(input_proto, output_proto, _config):
170 """Generate a sysroot. Typically used by informational builders."""
171 build_target_name = input_proto.build_target.name
172 pkg_list = []
173 for package in input_proto.packages:
174 pkg_list.append('%s/%s' % (package.category, package.package_name))
175
176 with osutils.TempDir(delete=False) as temp_output_dir:
177 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
Alex Kleind066b0f2022-07-28 08:47:35 -0600178 build_target_name, pkg_list)
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700179
180 # By assigning this Path variable to the tar path, the tar file will be
181 # copied out to the input_proto's ResultPath location.
182 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -0700183 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700184
185
Alex Klein076841b2019-08-29 15:19:39 -0600186def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
187 """Mock error response that populates failed packages."""
Lizzy Presland7e23a612021-11-09 21:49:42 +0000188 fail = output_proto.failed_package_data.add()
189 fail.name.package_name = 'package'
190 fail.name.category = 'category'
191 fail.name.version = '1.0.0_rc-r1'
192 fail.log_path.path = '/path/to/package:category-1.0.0_rc-r1:20210609-1337.log'
193 fail.log_path.location = common_pb2.Path.INSIDE
194
195 fail2 = output_proto.failed_package_data.add()
196 fail2.name.package_name = 'bar'
197 fail2.name.category = 'foo'
198 fail2.name.version = '3.7-r99'
199 fail2.log_path.path = '/path/to/foo:bar-3.7-r99:20210609-1620.log'
200 fail2.log_path.location = common_pb2.Path.INSIDE
201
Alex Klein076841b2019-08-29 15:19:39 -0600202
203@faux.empty_success
204@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600205@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600206@validate.exists('sysroot.path')
207@validate.validation_complete
208def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600209 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700210 compile_source = (
211 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700212
213 sysroot_path = input_proto.sysroot.path
Alex Kleinda35fcf2019-03-07 16:01:15 -0700214
Alex Klein26e472b2020-03-10 14:35:01 -0600215 build_target = controller_util.ParseBuildTarget(
216 input_proto.sysroot.build_target)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700217 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600218 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700219
Alex Kleina9d64602019-05-17 14:55:37 -0600220 _LogBinhost(build_target.name)
221
Alex Kleinda35fcf2019-03-07 16:01:15 -0700222 try:
223 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
224 except sysroot_lib.ToolchainInstallError as e:
Lizzy Presland084c20f2022-03-30 19:13:50 +0000225 controller_util.retrieve_package_log_paths(e.failed_toolchain_info,
226 output_proto, target_sysroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700227
Alex Klein8cb365a2019-05-15 16:24:53 -0600228 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600229
Alex Klein076841b2019-08-29 15:19:39 -0600230 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600231
Alex Klein076841b2019-08-29 15:19:39 -0600232
233@faux.empty_success
234@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600235@validate.require('sysroot.build_target.name')
236@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -0600237@validate.require_each('packages', ['category', 'package_name'])
238@validate.require_each('use_flags', ['flag'])
Alex Klein231d2da2019-07-22 16:44:45 -0600239@validate.validation_complete
Alex Kleinaef41942022-04-19 14:13:17 -0600240@metrics_lib.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600241def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600242 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700243 compile_source = (
244 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Joanna Wang1ec0c812021-11-17 17:41:27 -0800245
Joanna Wangf773c1b2021-12-09 11:46:44 -0800246 use_remoteexec = bool(input_proto.remoteexec_config.reproxy_cfg_file and
247 input_proto.remoteexec_config.reclient_dir)
Joanna Wang1ec0c812021-11-17 17:41:27 -0800248
LaMont Jones6dc755b2020-07-17 12:06:29 -0600249 # Testing if Goma will support unknown compilers now.
Joanna Wang1ec0c812021-11-17 17:41:27 -0800250 use_goma = input_proto.flags.use_goma and not use_remoteexec
Alex Kleind4e1e422019-03-18 16:00:41 -0600251
Alex Klein566d80e2019-09-24 12:27:58 -0600252 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
253 build_target = controller_util.ParseBuildTarget(
254 input_proto.sysroot.build_target)
Alex Kleinca572ee2020-09-03 10:47:14 -0600255
256 # Get the package atom for each specified package. The field is optional, so
257 # error only when we cannot parse an atom for each of the given packages.
Alex Kleind066b0f2022-07-28 08:47:35 -0600258 packages = [
259 controller_util.PackageInfoToCPV(x).cp for x in input_proto.packages
260 ]
Alex Kleinca572ee2020-09-03 10:47:14 -0600261
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600262 package_indexes = [
263 binpkg.PackageIndexInfo.from_protobuf(x)
264 for x in input_proto.package_indexes
265 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600266
Navil Perez5766d1b2021-05-26 17:38:15 +0000267 # Calculate which packages would have been merged, but don't install anything.
268 dryrun = input_proto.flags.dryrun
269
Alex Kleind4e1e422019-03-18 16:00:41 -0600270 if not target_sysroot.IsToolchainInstalled():
271 cros_build_lib.Die('Toolchain must first be installed.')
272
Alex Kleina9d64602019-05-17 14:55:37 -0600273 _LogBinhost(build_target.name)
274
Alex Kleinbe0bae42019-05-06 13:01:49 -0600275 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600276 build_packages_config = sysroot.BuildPackagesRunConfig(
Cindy Lin7487daa2022-02-23 04:14:10 +0000277 use_any_chrome=False,
Alex Klein566d80e2019-09-24 12:27:58 -0600278 usepkg=not compile_source,
279 install_debug_symbols=True,
280 packages=packages,
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600281 package_indexes=package_indexes,
Alex Klein566d80e2019-09-24 12:27:58 -0600282 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700283 use_goma=use_goma,
Joanna Wang1ec0c812021-11-17 17:41:27 -0800284 use_remoteexec=use_remoteexec,
Alex Kleineb76da72021-03-19 10:43:09 -0600285 incremental_build=False,
Alex Kleineaa48532022-07-28 08:51:32 -0600286 dryrun=dryrun,
287 backtrack=DEFAULT_BACKTRACK,
288 )
Alex Kleind4e1e422019-03-18 16:00:41 -0600289
290 try:
291 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
292 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600293 if not e.failed_packages:
294 # No packages to report, so just exit with an error code.
295 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
296
Lizzy Presland084c20f2022-03-30 19:13:50 +0000297 controller_util.retrieve_package_log_paths(e.failed_packages, output_proto,
298 target_sysroot)
Alex Kleind4e1e422019-03-18 16:00:41 -0600299
Alex Klein8cb365a2019-05-15 16:24:53 -0600300 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Yoshiki Iguchi6d2e3052021-12-13 11:06:04 +0900301 finally:
302 # Copy goma logs to specified directory if there is a goma_config and
303 # it contains a log_dir to store artifacts.
304 if input_proto.goma_config.log_dir.dir:
Yoshiki Iguchia43704b2021-12-16 13:31:48 +0900305 log_source_dir = _GetGomaLogDirectory()
Yoshiki Iguchi6d2e3052021-12-13 11:06:04 +0900306 archiver = goma_lib.LogsArchiver(
307 log_source_dir,
308 dest_dir=input_proto.goma_config.log_dir.dir,
309 stats_file=input_proto.goma_config.stats_file,
310 counterz_file=input_proto.goma_config.counterz_file)
311 archiver_tuple = archiver.Archive()
312 if archiver_tuple.stats_file:
313 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
314 if archiver_tuple.counterz_file:
315 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
316 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
Alex Kleina9d64602019-05-17 14:55:37 -0600317
Navil Perez5766d1b2021-05-26 17:38:15 +0000318 # Return without populating the response if it is a dryrun.
319 if dryrun:
320 return controller.RETURN_CODE_SUCCESS
321
Will Bradley7e5b8c12019-07-30 12:44:15 -0600322 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600323 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600324
Alex Kleina9d64602019-05-17 14:55:37 -0600325
326def _LogBinhost(board):
327 """Log the portage binhost for the given board."""
Alex Kleind066b0f2022-07-28 08:47:35 -0600328 binhost = portage_util.PortageqEnvvar(
329 'PORTAGE_BINHOST', board=board, allow_undefined=True)
Alex Kleina9d64602019-05-17 14:55:37 -0600330 if not binhost:
331 logging.warning('Portage Binhost not found.')
332 else:
333 logging.info('Portage Binhost: %s', binhost)