blob: ec89c3462e250c27d1c290c3de7917cb931dc1e7 [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
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070021from chromite.lib import osutils
Alex Kleina9d64602019-05-17 14:55:37 -060022from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070023from chromite.lib import sysroot_lib
24from chromite.service import sysroot
Will Bradley7e5b8c12019-07-30 12:44:15 -060025from chromite.utils import metrics
Alex Kleinda35fcf2019-03-07 16:01:15 -070026
Chris McDonald1672ddb2021-07-21 11:48:23 -060027
Alex Kleinda35fcf2019-03-07 16:01:15 -070028_ACCEPTED_LICENSES = '@CHROMEOS'
29
Alex Kleind4e1e422019-03-18 16:00:41 -060030
George Engelbrechtc9a8e812021-06-16 18:14:17 -060031def ExampleGetResponse():
32 """Give an example response to assemble upstream in caller artifacts."""
33 uabs = common_pb2.UploadedArtifactsByService
34 cabs = common_pb2.ArtifactsByService
35 return uabs.Sysroot(artifacts=[
George Engelbrecht35eeea42021-07-09 14:10:57 -060036 uabs.Sysroot.ArtifactPaths(
37 artifact_type=cabs.Sysroot.ArtifactType.SIMPLE_CHROME_SYSROOT,
38 paths=[
39 common_pb2.Path(
40 path='/tmp/sysroot_chromeos-base_chromeos-chrome.tar.xz',
41 location=common_pb2.Path.OUTSIDE)
42 ],
43 ),
George Engelbrechtc9a8e812021-06-16 18:14:17 -060044 uabs.Sysroot.ArtifactPaths(
45 artifact_type=cabs.Sysroot.ArtifactType.DEBUG_SYMBOLS,
46 paths=[
47 common_pb2.Path(
48 path='/tmp/debug.tgz', location=common_pb2.Path.OUTSIDE)
49 ],
50 ),
51 uabs.Sysroot.ArtifactPaths(
52 artifact_type=cabs.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS,
53 paths=[
54 common_pb2.Path(
55 path='/tmp/debug_breakpad.tar.xz',
56 location=common_pb2.Path.OUTSIDE)
57 ])
58 ])
59
60
61def GetArtifacts(in_proto: common_pb2.ArtifactsByService.Sysroot,
62 chroot: chroot_lib.Chroot, sysroot_class: sysroot_lib.Sysroot,
63 build_target: build_target_lib.BuildTarget, output_dir: str) -> list:
64 """Builds and copies sysroot artifacts to specified output_dir.
65
66 Copies sysroot artifacts to output_dir, returning a list of (output_dir: str)
67 paths to the desired files.
68
69 Args:
70 in_proto: Proto request defining reqs.
71 chroot: The chroot class used for these artifacts.
72 sysroot_class: The sysroot class used for these artifacts.
73 build_target: The build target used for these artifacts.
74 output_dir: The path to write artifacts to.
75
76 Returns:
77 A list of dictionary mappings of ArtifactType to list of paths.
78 """
79 generated = []
Jack Neus5e56fef2021-06-18 16:57:28 +000080 artifact_types = {
George Engelbrecht35eeea42021-07-09 14:10:57 -060081 in_proto.ArtifactType.SIMPLE_CHROME_SYSROOT:
82 sysroot.CreateSimpleChromeSysroot,
Jack Neus5fb6ca42021-07-14 18:05:44 +000083 in_proto.ArtifactType.CHROME_EBUILD_ENV: sysroot.CreateChromeEbuildEnv,
Jack Neus5e56fef2021-06-18 16:57:28 +000084 in_proto.ArtifactType.BREAKPAD_DEBUG_SYMBOLS: sysroot.BundleBreakpadSymbols,
George Engelbrecht35eeea42021-07-09 14:10:57 -060085 in_proto.ArtifactType.DEBUG_SYMBOLS: sysroot.BundleDebugSymbols,
Jack Neus5e56fef2021-06-18 16:57:28 +000086 }
87
George Engelbrechtc9a8e812021-06-16 18:14:17 -060088 for output_artifact in in_proto.output_artifacts:
Jack Neus5e56fef2021-06-18 16:57:28 +000089 for artifact_type, func in artifact_types.items():
90 if artifact_type in output_artifact.artifact_types:
91 result = func(chroot, sysroot_class, build_target, output_dir)
92 if result:
93 generated.append({
94 'paths': [result] if isinstance(result, str) else result,
95 'type': artifact_type,
96 })
97
George Engelbrechtc9a8e812021-06-16 18:14:17 -060098 return generated
99
100
Alex Klein076841b2019-08-29 15:19:39 -0600101@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -0600102@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600103@validate.validation_complete
104def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -0700105 """Create or replace a sysroot."""
106 update_chroot = not input_proto.flags.chroot_current
107 replace_sysroot = input_proto.flags.replace
108
Alex Klein26e472b2020-03-10 14:35:01 -0600109 build_target = controller_util.ParseBuildTarget(input_proto.build_target,
110 input_proto.profile)
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600111 package_indexes = [
112 binpkg.PackageIndexInfo.from_protobuf(x)
113 for x in input_proto.package_indexes
114 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600115 run_configs = sysroot.SetupBoardRunConfig(
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600116 force=replace_sysroot, upgrade_chroot=update_chroot,
117 package_indexes=package_indexes)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700118
119 try:
120 created = sysroot.Create(build_target, run_configs,
121 accept_licenses=_ACCEPTED_LICENSES)
122 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -0400123 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700124
125 output_proto.sysroot.path = created.path
Alex Klein26e472b2020-03-10 14:35:01 -0600126 output_proto.sysroot.build_target.name = build_target.name
Alex Kleinda35fcf2019-03-07 16:01:15 -0700127
Alex Klein076841b2019-08-29 15:19:39 -0600128 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -0700129
Alex Klein076841b2019-08-29 15:19:39 -0600130
Michael Mortensen98592f62019-09-27 13:34:18 -0600131@faux.all_empty
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700132@validate.require('build_target.name', 'packages')
Alex Klein45b73432020-09-23 13:51:20 -0600133@validate.require_each('packages', ['category', 'package_name'])
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700134@validate.validation_complete
135def GenerateArchive(input_proto, output_proto, _config):
136 """Generate a sysroot. Typically used by informational builders."""
137 build_target_name = input_proto.build_target.name
138 pkg_list = []
139 for package in input_proto.packages:
140 pkg_list.append('%s/%s' % (package.category, package.package_name))
141
142 with osutils.TempDir(delete=False) as temp_output_dir:
143 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
144 build_target_name,
145 pkg_list)
146
147 # By assigning this Path variable to the tar path, the tar file will be
148 # copied out to the input_proto's ResultPath location.
149 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -0700150 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700151
152
Alex Klein076841b2019-08-29 15:19:39 -0600153def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
154 """Mock error response that populates failed packages."""
155 pkg = output_proto.failed_packages.add()
156 pkg.package_name = 'package'
157 pkg.category = 'category'
158 pkg.version = '1.0.0_rc-r1'
159
160 pkg2 = output_proto.failed_packages.add()
161 pkg2.package_name = 'bar'
162 pkg2.category = 'foo'
163 pkg2.version = '3.7-r99'
164
165
166@faux.empty_success
167@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600168@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600169@validate.exists('sysroot.path')
170@validate.validation_complete
171def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600172 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700173 compile_source = (
174 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700175
176 sysroot_path = input_proto.sysroot.path
Alex Kleinda35fcf2019-03-07 16:01:15 -0700177
Alex Klein26e472b2020-03-10 14:35:01 -0600178 build_target = controller_util.ParseBuildTarget(
179 input_proto.sysroot.build_target)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700180 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600181 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700182
Alex Kleina9d64602019-05-17 14:55:37 -0600183 _LogBinhost(build_target.name)
184
Alex Kleinda35fcf2019-03-07 16:01:15 -0700185 try:
186 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
187 except sysroot_lib.ToolchainInstallError as e:
188 # Error installing - populate the failed package info.
Alex Kleinea0c89e2021-09-09 15:17:35 -0600189 for pkg_info in e.failed_toolchain_info:
190 package_info_msg = output_proto.failed_packages.add()
191 controller_util.serialize_package_info(pkg_info, package_info_msg)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700192
Alex Klein8cb365a2019-05-15 16:24:53 -0600193 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600194
Alex Klein076841b2019-08-29 15:19:39 -0600195 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600196
Alex Klein076841b2019-08-29 15:19:39 -0600197
198@faux.empty_success
199@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600200@validate.require('sysroot.build_target.name')
201@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -0600202@validate.require_each('packages', ['category', 'package_name'])
203@validate.require_each('use_flags', ['flag'])
Alex Klein231d2da2019-07-22 16:44:45 -0600204@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600205@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600206def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600207 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700208 compile_source = (
209 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
LaMont Jones6dc755b2020-07-17 12:06:29 -0600210 # Testing if Goma will support unknown compilers now.
211 use_goma = input_proto.flags.use_goma
Alex Kleind4e1e422019-03-18 16:00:41 -0600212
Alex Klein566d80e2019-09-24 12:27:58 -0600213 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
214 build_target = controller_util.ParseBuildTarget(
215 input_proto.sysroot.build_target)
Alex Kleinca572ee2020-09-03 10:47:14 -0600216
217 # Get the package atom for each specified package. The field is optional, so
218 # error only when we cannot parse an atom for each of the given packages.
219 packages = [controller_util.PackageInfoToCPV(x).cp
Mike Frysinger66ce4132019-07-17 22:52:52 -0400220 for x in input_proto.packages]
Alex Kleinca572ee2020-09-03 10:47:14 -0600221
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600222 package_indexes = [
223 binpkg.PackageIndexInfo.from_protobuf(x)
224 for x in input_proto.package_indexes
225 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600226
Navil Perez5766d1b2021-05-26 17:38:15 +0000227 # Calculate which packages would have been merged, but don't install anything.
228 dryrun = input_proto.flags.dryrun
229
Alex Kleind4e1e422019-03-18 16:00:41 -0600230 if not target_sysroot.IsToolchainInstalled():
231 cros_build_lib.Die('Toolchain must first be installed.')
232
Alex Kleina9d64602019-05-17 14:55:37 -0600233 _LogBinhost(build_target.name)
234
Alex Kleinbe0bae42019-05-06 13:01:49 -0600235 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600236 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600237 usepkg=not compile_source,
238 install_debug_symbols=True,
239 packages=packages,
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600240 package_indexes=package_indexes,
Alex Klein566d80e2019-09-24 12:27:58 -0600241 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700242 use_goma=use_goma,
Alex Kleineb76da72021-03-19 10:43:09 -0600243 incremental_build=False,
Navil Perez5766d1b2021-05-26 17:38:15 +0000244 setup_board=False,
245 dryrun=dryrun)
Alex Kleind4e1e422019-03-18 16:00:41 -0600246
247 try:
248 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
249 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600250 if not e.failed_packages:
251 # No packages to report, so just exit with an error code.
252 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
253
254 # We need to report the failed packages.
Alex Kleinea0c89e2021-09-09 15:17:35 -0600255 for pkg_info in e.failed_packages:
256 package_info_msg = output_proto.failed_packages.add()
257 controller_util.serialize_package_info(pkg_info, package_info_msg)
Alex Kleind4e1e422019-03-18 16:00:41 -0600258
Alex Klein8cb365a2019-05-15 16:24:53 -0600259 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600260
Navil Perez5766d1b2021-05-26 17:38:15 +0000261 # Return without populating the response if it is a dryrun.
262 if dryrun:
263 return controller.RETURN_CODE_SUCCESS
264
Michael Mortensen798ee192020-01-17 13:04:43 -0700265 # Copy goma logs to specified directory if there is a goma_config and
266 # it contains a log_dir to store artifacts.
267 if input_proto.goma_config.log_dir.dir:
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700268 # Get the goma log directory based on the GLOG_log_dir env variable.
269 # TODO(crbug.com/1045001): Replace environment variable with query to
270 # goma object after goma refactoring allows this.
271 log_source_dir = os.getenv('GLOG_log_dir')
272 if not log_source_dir:
273 cros_build_lib.Die('GLOG_log_dir must be defined.')
Michael Mortensen798ee192020-01-17 13:04:43 -0700274 archiver = goma_lib.LogsArchiver(
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700275 log_source_dir,
Michael Mortensen798ee192020-01-17 13:04:43 -0700276 dest_dir=input_proto.goma_config.log_dir.dir,
277 stats_file=input_proto.goma_config.stats_file,
278 counterz_file=input_proto.goma_config.counterz_file)
279 archiver_tuple = archiver.Archive()
280 if archiver_tuple.stats_file:
281 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
282 if archiver_tuple.counterz_file:
Michael Mortensen1c7439c2020-01-24 14:43:19 -0700283 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
Michael Mortensen798ee192020-01-17 13:04:43 -0700284 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
285
Will Bradley7e5b8c12019-07-30 12:44:15 -0600286 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600287 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600288
Alex Kleina9d64602019-05-17 14:55:37 -0600289
290def _LogBinhost(board):
291 """Log the portage binhost for the given board."""
292 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
293 allow_undefined=True)
294 if not binhost:
295 logging.warning('Portage Binhost not found.')
296 else:
297 logging.info('Portage Binhost: %s', binhost)