blob: 602037a1761d5e179c4cc687ed1890cf394a4f78 [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
Michael Mortensen4ccfb082020-01-22 16:24:03 -07007import os
8
Alex Klein6b499df2020-11-19 14:26:56 -07009from chromite.api.gen.chromiumos import common_pb2
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
Will Bradley7e5b8c12019-07-30 12:44:15 -060014from chromite.api.metrics import deserialize_metrics_log
LaMont Jonesfeffd1b2020-08-05 18:24:59 -060015from chromite.lib import binpkg
George Engelbrechtc9a8e812021-06-16 18:14:17 -060016from chromite.lib import build_target_lib
17from chromite.lib import chroot_lib
Alex Kleinda35fcf2019-03-07 16:01:15 -070018from chromite.lib import cros_build_lib
Alex Kleina9d64602019-05-17 14:55:37 -060019from chromite.lib import cros_logging as logging
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
27_ACCEPTED_LICENSES = '@CHROMEOS'
28
Alex Kleind4e1e422019-03-18 16:00:41 -060029
George Engelbrechtc9a8e812021-06-16 18:14:17 -060030def ExampleGetResponse():
31 """Give an example response to assemble upstream in caller artifacts."""
32 uabs = common_pb2.UploadedArtifactsByService
33 cabs = common_pb2.ArtifactsByService
34 return uabs.Sysroot(artifacts=[
35 uabs.Sysroot.ArtifactPaths(
36 artifact_type=cabs.Sysroot.ArtifactType.DEBUG_SYMBOLS,
37 paths=[
38 common_pb2.Path(
39 path='/tmp/debug.tgz', location=common_pb2.Path.OUTSIDE)
40 ],
41 ),
42 uabs.Sysroot.ArtifactPaths(
43 artifact_type=cabs.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS,
44 paths=[
45 common_pb2.Path(
46 path='/tmp/debug_breakpad.tar.xz',
47 location=common_pb2.Path.OUTSIDE)
48 ])
49 ])
50
51
52def GetArtifacts(in_proto: common_pb2.ArtifactsByService.Sysroot,
53 chroot: chroot_lib.Chroot, sysroot_class: sysroot_lib.Sysroot,
54 build_target: build_target_lib.BuildTarget, output_dir: str) -> list:
55 """Builds and copies sysroot artifacts to specified output_dir.
56
57 Copies sysroot artifacts to output_dir, returning a list of (output_dir: str)
58 paths to the desired files.
59
60 Args:
61 in_proto: Proto request defining reqs.
62 chroot: The chroot class used for these artifacts.
63 sysroot_class: The sysroot class used for these artifacts.
64 build_target: The build target used for these artifacts.
65 output_dir: The path to write artifacts to.
66
67 Returns:
68 A list of dictionary mappings of ArtifactType to list of paths.
69 """
70 generated = []
Jack Neus2ffee8a2021-06-18 16:57:28 +000071 artifact_types = {
72 in_proto.ArtifactType.BREAKPAD_DEBUG_SYMBOLS: sysroot.BundleBreakpadSymbols,
73 in_proto.ArtifactType.DEBUG_SYMBOLS: sysroot.BundleDebugSymbols
74 }
75
George Engelbrechtc9a8e812021-06-16 18:14:17 -060076 for output_artifact in in_proto.output_artifacts:
Jack Neus2ffee8a2021-06-18 16:57:28 +000077 for artifact_type, func in artifact_types.items():
78 if artifact_type in output_artifact.artifact_types:
79 result = func(chroot, sysroot_class, build_target, output_dir)
80 if result:
81 generated.append({
82 'paths': [result] if isinstance(result, str) else result,
83 'type': artifact_type,
84 })
85
George Engelbrechtc9a8e812021-06-16 18:14:17 -060086 return generated
87
88
89
Alex Klein076841b2019-08-29 15:19:39 -060090@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060091@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060092@validate.validation_complete
93def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070094 """Create or replace a sysroot."""
95 update_chroot = not input_proto.flags.chroot_current
96 replace_sysroot = input_proto.flags.replace
97
Alex Klein26e472b2020-03-10 14:35:01 -060098 build_target = controller_util.ParseBuildTarget(input_proto.build_target,
99 input_proto.profile)
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600100 package_indexes = [
101 binpkg.PackageIndexInfo.from_protobuf(x)
102 for x in input_proto.package_indexes
103 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600104 run_configs = sysroot.SetupBoardRunConfig(
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600105 force=replace_sysroot, upgrade_chroot=update_chroot,
106 package_indexes=package_indexes)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700107
108 try:
109 created = sysroot.Create(build_target, run_configs,
110 accept_licenses=_ACCEPTED_LICENSES)
111 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -0400112 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700113
114 output_proto.sysroot.path = created.path
Alex Klein26e472b2020-03-10 14:35:01 -0600115 output_proto.sysroot.build_target.name = build_target.name
Alex Kleinda35fcf2019-03-07 16:01:15 -0700116
Alex Klein076841b2019-08-29 15:19:39 -0600117 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -0700118
Alex Klein076841b2019-08-29 15:19:39 -0600119
Michael Mortensen98592f62019-09-27 13:34:18 -0600120@faux.all_empty
121@validate.require('build_target.name')
122@validate.validation_complete
123def CreateSimpleChromeSysroot(input_proto, output_proto, _config):
124 """Create a sysroot for SimpleChrome to use."""
125 build_target_name = input_proto.build_target.name
126 use_flags = input_proto.use_flags
127
128 sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name,
129 use_flags)
130 # By assigning this Path variable to the tar path, the tar file will be
131 # copied out to the input_proto's ResultPath location.
132 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -0700133 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen98592f62019-09-27 13:34:18 -0600134
135 return controller.RETURN_CODE_SUCCESS
136
137
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700138@faux.all_empty
139@validate.require('build_target.name', 'packages')
Alex Klein45b73432020-09-23 13:51:20 -0600140@validate.require_each('packages', ['category', 'package_name'])
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700141@validate.validation_complete
142def GenerateArchive(input_proto, output_proto, _config):
143 """Generate a sysroot. Typically used by informational builders."""
144 build_target_name = input_proto.build_target.name
145 pkg_list = []
146 for package in input_proto.packages:
147 pkg_list.append('%s/%s' % (package.category, package.package_name))
148
149 with osutils.TempDir(delete=False) as temp_output_dir:
150 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
151 build_target_name,
152 pkg_list)
153
154 # By assigning this Path variable to the tar path, the tar file will be
155 # copied out to the input_proto's ResultPath location.
156 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -0700157 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700158
159
Alex Klein076841b2019-08-29 15:19:39 -0600160def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
161 """Mock error response that populates failed packages."""
162 pkg = output_proto.failed_packages.add()
163 pkg.package_name = 'package'
164 pkg.category = 'category'
165 pkg.version = '1.0.0_rc-r1'
166
167 pkg2 = output_proto.failed_packages.add()
168 pkg2.package_name = 'bar'
169 pkg2.category = 'foo'
170 pkg2.version = '3.7-r99'
171
172
173@faux.empty_success
174@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600175@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600176@validate.exists('sysroot.path')
177@validate.validation_complete
178def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600179 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700180 compile_source = (
181 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700182
183 sysroot_path = input_proto.sysroot.path
Alex Kleinda35fcf2019-03-07 16:01:15 -0700184
Alex Klein26e472b2020-03-10 14:35:01 -0600185 build_target = controller_util.ParseBuildTarget(
186 input_proto.sysroot.build_target)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700187 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600188 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700189
Alex Kleina9d64602019-05-17 14:55:37 -0600190 _LogBinhost(build_target.name)
191
Alex Kleinda35fcf2019-03-07 16:01:15 -0700192 try:
193 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
194 except sysroot_lib.ToolchainInstallError as e:
195 # Error installing - populate the failed package info.
196 for package in e.failed_toolchain_info:
197 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600198 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700199
Alex Klein8cb365a2019-05-15 16:24:53 -0600200 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600201
Alex Klein076841b2019-08-29 15:19:39 -0600202 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600203
Alex Klein076841b2019-08-29 15:19:39 -0600204
205@faux.empty_success
206@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600207@validate.require('sysroot.build_target.name')
208@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -0600209@validate.require_each('packages', ['category', 'package_name'])
210@validate.require_each('use_flags', ['flag'])
Alex Klein231d2da2019-07-22 16:44:45 -0600211@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600212@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600213def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600214 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700215 compile_source = (
216 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
LaMont Jones6dc755b2020-07-17 12:06:29 -0600217 # Testing if Goma will support unknown compilers now.
218 use_goma = input_proto.flags.use_goma
Alex Kleind4e1e422019-03-18 16:00:41 -0600219
Alex Klein566d80e2019-09-24 12:27:58 -0600220 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
221 build_target = controller_util.ParseBuildTarget(
222 input_proto.sysroot.build_target)
Alex Kleinca572ee2020-09-03 10:47:14 -0600223
224 # Get the package atom for each specified package. The field is optional, so
225 # error only when we cannot parse an atom for each of the given packages.
226 packages = [controller_util.PackageInfoToCPV(x).cp
Mike Frysinger66ce4132019-07-17 22:52:52 -0400227 for x in input_proto.packages]
Alex Kleinca572ee2020-09-03 10:47:14 -0600228
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600229 package_indexes = [
230 binpkg.PackageIndexInfo.from_protobuf(x)
231 for x in input_proto.package_indexes
232 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600233
Navil Perez5766d1b2021-05-26 17:38:15 +0000234 # Calculate which packages would have been merged, but don't install anything.
235 dryrun = input_proto.flags.dryrun
236
Alex Kleind4e1e422019-03-18 16:00:41 -0600237 if not target_sysroot.IsToolchainInstalled():
238 cros_build_lib.Die('Toolchain must first be installed.')
239
Alex Kleina9d64602019-05-17 14:55:37 -0600240 _LogBinhost(build_target.name)
241
Alex Kleinbe0bae42019-05-06 13:01:49 -0600242 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600243 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600244 usepkg=not compile_source,
245 install_debug_symbols=True,
246 packages=packages,
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600247 package_indexes=package_indexes,
Alex Klein566d80e2019-09-24 12:27:58 -0600248 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700249 use_goma=use_goma,
Alex Kleineb76da72021-03-19 10:43:09 -0600250 incremental_build=False,
Navil Perez5766d1b2021-05-26 17:38:15 +0000251 setup_board=False,
252 dryrun=dryrun)
Alex Kleind4e1e422019-03-18 16:00:41 -0600253
254 try:
255 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
256 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600257 if not e.failed_packages:
258 # No packages to report, so just exit with an error code.
259 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
260
261 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600262 for package in e.failed_packages:
263 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600264 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600265
Alex Klein8cb365a2019-05-15 16:24:53 -0600266 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600267
Navil Perez5766d1b2021-05-26 17:38:15 +0000268 # Return without populating the response if it is a dryrun.
269 if dryrun:
270 return controller.RETURN_CODE_SUCCESS
271
Michael Mortensen798ee192020-01-17 13:04:43 -0700272 # Copy goma logs to specified directory if there is a goma_config and
273 # it contains a log_dir to store artifacts.
274 if input_proto.goma_config.log_dir.dir:
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700275 # Get the goma log directory based on the GLOG_log_dir env variable.
276 # TODO(crbug.com/1045001): Replace environment variable with query to
277 # goma object after goma refactoring allows this.
278 log_source_dir = os.getenv('GLOG_log_dir')
279 if not log_source_dir:
280 cros_build_lib.Die('GLOG_log_dir must be defined.')
Michael Mortensen798ee192020-01-17 13:04:43 -0700281 archiver = goma_lib.LogsArchiver(
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700282 log_source_dir,
Michael Mortensen798ee192020-01-17 13:04:43 -0700283 dest_dir=input_proto.goma_config.log_dir.dir,
284 stats_file=input_proto.goma_config.stats_file,
285 counterz_file=input_proto.goma_config.counterz_file)
286 archiver_tuple = archiver.Archive()
287 if archiver_tuple.stats_file:
288 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
289 if archiver_tuple.counterz_file:
Michael Mortensen1c7439c2020-01-24 14:43:19 -0700290 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
Michael Mortensen798ee192020-01-17 13:04:43 -0700291 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
292
Will Bradley7e5b8c12019-07-30 12:44:15 -0600293 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600294 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600295
Alex Kleina9d64602019-05-17 14:55:37 -0600296
297def _LogBinhost(board):
298 """Log the portage binhost for the given board."""
299 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
300 allow_undefined=True)
301 if not binhost:
302 logging.warning('Portage Binhost not found.')
303 else:
304 logging.info('Portage Binhost: %s', binhost)