blob: a182eb0ec259ca4992e6d1941c0a3155e957076e [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
Lizzy Presland7e23a612021-11-09 21:49:42 +00007import glob
Chris McDonald1672ddb2021-07-21 11:48:23 -06008import logging
Michael Mortensen4ccfb082020-01-22 16:24:03 -07009import os
10
Alex Klein8cb365a2019-05-15 16:24:53 -060011from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060012from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060013from chromite.api import validate
Alex Kleina9d500b2019-04-22 15:37:51 -060014from chromite.api.controller import controller_util
Chris McDonald1672ddb2021-07-21 11:48:23 -060015from chromite.api.gen.chromiumos import common_pb2
Will Bradley7e5b8c12019-07-30 12:44:15 -060016from chromite.api.metrics import deserialize_metrics_log
LaMont Jonesfeffd1b2020-08-05 18:24:59 -060017from chromite.lib import binpkg
George Engelbrechtc9a8e812021-06-16 18:14:17 -060018from chromite.lib import build_target_lib
19from chromite.lib import chroot_lib
Alex Kleinda35fcf2019-03-07 16:01:15 -070020from chromite.lib import cros_build_lib
Michael Mortensen798ee192020-01-17 13:04:43 -070021from chromite.lib import goma_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
Will Bradley7e5b8c12019-07-30 12:44:15 -060026from chromite.utils import metrics
Alex Kleinda35fcf2019-03-07 16:01:15 -070027
Chris McDonald1672ddb2021-07-21 11:48:23 -060028
Alex Kleinda35fcf2019-03-07 16:01:15 -070029_ACCEPTED_LICENSES = '@CHROMEOS'
30
Alex Kleind4e1e422019-03-18 16:00:41 -060031
George Engelbrechtc9a8e812021-06-16 18:14:17 -060032def ExampleGetResponse():
33 """Give an example response to assemble upstream in caller artifacts."""
34 uabs = common_pb2.UploadedArtifactsByService
35 cabs = common_pb2.ArtifactsByService
36 return uabs.Sysroot(artifacts=[
Lizzy Preslandb2d60642021-11-12 01:41:58 +000037 uabs.Sysroot.ArtifactPaths(
George Engelbrecht35eeea42021-07-09 14:10:57 -060038 artifact_type=cabs.Sysroot.ArtifactType.SIMPLE_CHROME_SYSROOT,
39 paths=[
40 common_pb2.Path(
41 path='/tmp/sysroot_chromeos-base_chromeos-chrome.tar.xz',
42 location=common_pb2.Path.OUTSIDE)
43 ],
44 ),
George Engelbrechtc9a8e812021-06-16 18:14:17 -060045 uabs.Sysroot.ArtifactPaths(
46 artifact_type=cabs.Sysroot.ArtifactType.DEBUG_SYMBOLS,
47 paths=[
48 common_pb2.Path(
49 path='/tmp/debug.tgz', location=common_pb2.Path.OUTSIDE)
50 ],
51 ),
52 uabs.Sysroot.ArtifactPaths(
53 artifact_type=cabs.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS,
54 paths=[
55 common_pb2.Path(
56 path='/tmp/debug_breakpad.tar.xz',
57 location=common_pb2.Path.OUTSIDE)
58 ])
59 ])
60
61
62def GetArtifacts(in_proto: common_pb2.ArtifactsByService.Sysroot,
Lizzy Preslandb2d60642021-11-12 01:41:58 +000063 chroot: chroot_lib.Chroot, sysroot_class: sysroot_lib.Sysroot,
64 build_target: build_target_lib.BuildTarget, output_dir: str
65 ) -> list:
George Engelbrechtc9a8e812021-06-16 18:14:17 -060066 """Builds and copies sysroot artifacts to specified output_dir.
67
68 Copies sysroot artifacts to output_dir, returning a list of (output_dir: str)
69 paths to the desired files.
70
71 Args:
72 in_proto: Proto request defining reqs.
73 chroot: The chroot class used for these artifacts.
74 sysroot_class: The sysroot class used for these artifacts.
75 build_target: The build target used for these artifacts.
76 output_dir: The path to write artifacts to.
77
78 Returns:
79 A list of dictionary mappings of ArtifactType to list of paths.
80 """
81 generated = []
Jack Neus5e56fef2021-06-18 16:57:28 +000082 artifact_types = {
Lizzy Preslandb2d60642021-11-12 01:41:58 +000083 in_proto.ArtifactType.SIMPLE_CHROME_SYSROOT:
84 sysroot.CreateSimpleChromeSysroot,
85 in_proto.ArtifactType.CHROME_EBUILD_ENV: sysroot.CreateChromeEbuildEnv,
86 in_proto.ArtifactType.BREAKPAD_DEBUG_SYMBOLS:
87 sysroot.BundleBreakpadSymbols,
88 in_proto.ArtifactType.DEBUG_SYMBOLS: sysroot.BundleDebugSymbols,
Jack Neus5e56fef2021-06-18 16:57:28 +000089 }
90
George Engelbrechtc9a8e812021-06-16 18:14:17 -060091 for output_artifact in in_proto.output_artifacts:
Jack Neus5e56fef2021-06-18 16:57:28 +000092 for artifact_type, func in artifact_types.items():
93 if artifact_type in output_artifact.artifact_types:
94 result = func(chroot, sysroot_class, build_target, output_dir)
95 if result:
96 generated.append({
97 'paths': [result] if isinstance(result, str) else result,
98 'type': artifact_type,
99 })
100
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600101 return generated
102
103
Alex Klein076841b2019-08-29 15:19:39 -0600104@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -0600105@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600106@validate.validation_complete
107def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -0700108 """Create or replace a sysroot."""
109 update_chroot = not input_proto.flags.chroot_current
110 replace_sysroot = input_proto.flags.replace
111
Alex Klein26e472b2020-03-10 14:35:01 -0600112 build_target = controller_util.ParseBuildTarget(input_proto.build_target,
113 input_proto.profile)
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600114 package_indexes = [
115 binpkg.PackageIndexInfo.from_protobuf(x)
116 for x in input_proto.package_indexes
117 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600118 run_configs = sysroot.SetupBoardRunConfig(
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600119 force=replace_sysroot, upgrade_chroot=update_chroot,
120 package_indexes=package_indexes)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700121
122 try:
123 created = sysroot.Create(build_target, run_configs,
124 accept_licenses=_ACCEPTED_LICENSES)
125 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -0400126 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700127
128 output_proto.sysroot.path = created.path
Alex Klein26e472b2020-03-10 14:35:01 -0600129 output_proto.sysroot.build_target.name = build_target.name
Alex Kleinda35fcf2019-03-07 16:01:15 -0700130
Alex Klein076841b2019-08-29 15:19:39 -0600131 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -0700132
Alex Klein076841b2019-08-29 15:19:39 -0600133
Michael Mortensen98592f62019-09-27 13:34:18 -0600134@faux.all_empty
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700135@validate.require('build_target.name', 'packages')
Alex Klein45b73432020-09-23 13:51:20 -0600136@validate.require_each('packages', ['category', 'package_name'])
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700137@validate.validation_complete
138def GenerateArchive(input_proto, output_proto, _config):
139 """Generate a sysroot. Typically used by informational builders."""
140 build_target_name = input_proto.build_target.name
141 pkg_list = []
142 for package in input_proto.packages:
143 pkg_list.append('%s/%s' % (package.category, package.package_name))
144
145 with osutils.TempDir(delete=False) as temp_output_dir:
146 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
147 build_target_name,
148 pkg_list)
149
150 # By assigning this Path variable to the tar path, the tar file will be
151 # copied out to the input_proto's ResultPath location.
152 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -0700153 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -0700154
155
Alex Klein076841b2019-08-29 15:19:39 -0600156def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
157 """Mock error response that populates failed packages."""
158 pkg = output_proto.failed_packages.add()
159 pkg.package_name = 'package'
160 pkg.category = 'category'
161 pkg.version = '1.0.0_rc-r1'
162
163 pkg2 = output_proto.failed_packages.add()
164 pkg2.package_name = 'bar'
165 pkg2.category = 'foo'
166 pkg2.version = '3.7-r99'
167
Lizzy Presland7e23a612021-11-09 21:49:42 +0000168 fail = output_proto.failed_package_data.add()
169 fail.name.package_name = 'package'
170 fail.name.category = 'category'
171 fail.name.version = '1.0.0_rc-r1'
172 fail.log_path.path = '/path/to/package:category-1.0.0_rc-r1:20210609-1337.log'
173 fail.log_path.location = common_pb2.Path.INSIDE
174
175 fail2 = output_proto.failed_package_data.add()
176 fail2.name.package_name = 'bar'
177 fail2.name.category = 'foo'
178 fail2.name.version = '3.7-r99'
179 fail2.log_path.path = '/path/to/foo:bar-3.7-r99:20210609-1620.log'
180 fail2.log_path.location = common_pb2.Path.INSIDE
181
Alex Klein076841b2019-08-29 15:19:39 -0600182
183@faux.empty_success
184@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600185@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600186@validate.exists('sysroot.path')
187@validate.validation_complete
188def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600189 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700190 compile_source = (
191 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700192
193 sysroot_path = input_proto.sysroot.path
Alex Kleinda35fcf2019-03-07 16:01:15 -0700194
Alex Klein26e472b2020-03-10 14:35:01 -0600195 build_target = controller_util.ParseBuildTarget(
196 input_proto.sysroot.build_target)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700197 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600198 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700199
Alex Kleina9d64602019-05-17 14:55:37 -0600200 _LogBinhost(build_target.name)
201
Alex Kleinda35fcf2019-03-07 16:01:15 -0700202 try:
203 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
204 except sysroot_lib.ToolchainInstallError as e:
205 # Error installing - populate the failed package info.
Alex Kleinea0c89e2021-09-09 15:17:35 -0600206 for pkg_info in e.failed_toolchain_info:
Lizzy Presland7e23a612021-11-09 21:49:42 +0000207 # TODO(b/206514844): remove when field is deleted
Alex Kleinea0c89e2021-09-09 15:17:35 -0600208 package_info_msg = output_proto.failed_packages.add()
209 controller_util.serialize_package_info(pkg_info, package_info_msg)
Lizzy Presland7e23a612021-11-09 21:49:42 +0000210 # Grab the paths to the log files for each failed package from the
211 # sysroot.
212 # Logs currently exist within the sysroot in the form of:
213 # /build/${BOARD}/tmp/portage/logs/$CATEGORY:$PF:$TIMESTAMP.log
214 failed_pkg_data_msg = output_proto.failed_package_data.add()
215 controller_util.serialize_package_info(pkg_info, failed_pkg_data_msg.name)
Lizzy Presland4c279832021-11-19 20:27:43 +0000216 glob_path = os.path.join(target_sysroot.portage_logdir,
Lizzy Presland7e23a612021-11-09 21:49:42 +0000217 f'{pkg_info.category}:{pkg_info.package}:*.log')
218 log_files = glob.glob(glob_path)
219 log_files.sort(reverse=True)
220 # Omit path if files don't exist for some reason
221 if not log_files:
222 continue
223 failed_pkg_data_msg.log_path.path = log_files[0]
224 failed_pkg_data_msg.log_path.location = common_pb2.Path.INSIDE
Alex Kleinda35fcf2019-03-07 16:01:15 -0700225
Alex Klein8cb365a2019-05-15 16:24:53 -0600226 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600227
Alex Klein076841b2019-08-29 15:19:39 -0600228 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600229
Alex Klein076841b2019-08-29 15:19:39 -0600230
231@faux.empty_success
232@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600233@validate.require('sysroot.build_target.name')
234@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -0600235@validate.require_each('packages', ['category', 'package_name'])
236@validate.require_each('use_flags', ['flag'])
Alex Klein231d2da2019-07-22 16:44:45 -0600237@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600238@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600239def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600240 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700241 compile_source = (
242 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Joanna Wang1ec0c812021-11-17 17:41:27 -0800243
244 # TODO(crbug.com/1256966): Check if input_proto.remoteexec_config exists.
245 use_remoteexec = False
246
LaMont Jones6dc755b2020-07-17 12:06:29 -0600247 # Testing if Goma will support unknown compilers now.
Joanna Wang1ec0c812021-11-17 17:41:27 -0800248 use_goma = input_proto.flags.use_goma and not use_remoteexec
Alex Kleind4e1e422019-03-18 16:00:41 -0600249
Alex Klein566d80e2019-09-24 12:27:58 -0600250 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
251 build_target = controller_util.ParseBuildTarget(
252 input_proto.sysroot.build_target)
Alex Kleinca572ee2020-09-03 10:47:14 -0600253
254 # Get the package atom for each specified package. The field is optional, so
255 # error only when we cannot parse an atom for each of the given packages.
256 packages = [controller_util.PackageInfoToCPV(x).cp
Mike Frysinger66ce4132019-07-17 22:52:52 -0400257 for x in input_proto.packages]
Alex Kleinca572ee2020-09-03 10:47:14 -0600258
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600259 package_indexes = [
260 binpkg.PackageIndexInfo.from_protobuf(x)
261 for x in input_proto.package_indexes
262 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600263
Navil Perez5766d1b2021-05-26 17:38:15 +0000264 # Calculate which packages would have been merged, but don't install anything.
265 dryrun = input_proto.flags.dryrun
266
Alex Kleind4e1e422019-03-18 16:00:41 -0600267 if not target_sysroot.IsToolchainInstalled():
268 cros_build_lib.Die('Toolchain must first be installed.')
269
Alex Kleina9d64602019-05-17 14:55:37 -0600270 _LogBinhost(build_target.name)
271
Alex Kleinbe0bae42019-05-06 13:01:49 -0600272 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600273 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600274 usepkg=not compile_source,
275 install_debug_symbols=True,
276 packages=packages,
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600277 package_indexes=package_indexes,
Alex Klein566d80e2019-09-24 12:27:58 -0600278 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700279 use_goma=use_goma,
Joanna Wang1ec0c812021-11-17 17:41:27 -0800280 use_remoteexec=use_remoteexec,
Alex Kleineb76da72021-03-19 10:43:09 -0600281 incremental_build=False,
Navil Perez5766d1b2021-05-26 17:38:15 +0000282 setup_board=False,
283 dryrun=dryrun)
Alex Kleind4e1e422019-03-18 16:00:41 -0600284
285 try:
286 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
287 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600288 if not e.failed_packages:
289 # No packages to report, so just exit with an error code.
290 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
291
292 # We need to report the failed packages.
Alex Kleinea0c89e2021-09-09 15:17:35 -0600293 for pkg_info in e.failed_packages:
Lizzy Presland7e23a612021-11-09 21:49:42 +0000294 # TODO(b/206514844): remove when field is deleted
Alex Kleinea0c89e2021-09-09 15:17:35 -0600295 package_info_msg = output_proto.failed_packages.add()
296 controller_util.serialize_package_info(pkg_info, package_info_msg)
Lizzy Presland7e23a612021-11-09 21:49:42 +0000297 # Grab the paths to the log files for each failed package from the
298 # sysroot.
299 # Logs currently exist within the sysroot in the form of:
300 # /build/${BOARD}/tmp/portage/logs/$CATEGORY:$PF:$TIMESTAMP.log
301 failed_pkg_data_msg = output_proto.failed_package_data.add()
302 controller_util.serialize_package_info(pkg_info, failed_pkg_data_msg.name)
Lizzy Presland4c279832021-11-19 20:27:43 +0000303 glob_path = os.path.join(target_sysroot.portage_logdir,
Lizzy Presland7e23a612021-11-09 21:49:42 +0000304 f'{pkg_info.category}:{pkg_info.package}:*.log')
305 log_files = glob.glob(glob_path)
306 log_files.sort(reverse=True)
307 # Omit path if files don't exist for some reason
308 if not log_files:
309 continue
310 failed_pkg_data_msg.log_path.path = log_files[0]
311 failed_pkg_data_msg.log_path.location = common_pb2.Path.INSIDE
Alex Kleind4e1e422019-03-18 16:00:41 -0600312
Alex Klein8cb365a2019-05-15 16:24:53 -0600313 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600314
Navil Perez5766d1b2021-05-26 17:38:15 +0000315 # Return without populating the response if it is a dryrun.
316 if dryrun:
317 return controller.RETURN_CODE_SUCCESS
318
Michael Mortensen798ee192020-01-17 13:04:43 -0700319 # Copy goma logs to specified directory if there is a goma_config and
320 # it contains a log_dir to store artifacts.
321 if input_proto.goma_config.log_dir.dir:
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700322 # Get the goma log directory based on the GLOG_log_dir env variable.
323 # TODO(crbug.com/1045001): Replace environment variable with query to
324 # goma object after goma refactoring allows this.
325 log_source_dir = os.getenv('GLOG_log_dir')
326 if not log_source_dir:
327 cros_build_lib.Die('GLOG_log_dir must be defined.')
Michael Mortensen798ee192020-01-17 13:04:43 -0700328 archiver = goma_lib.LogsArchiver(
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700329 log_source_dir,
Michael Mortensen798ee192020-01-17 13:04:43 -0700330 dest_dir=input_proto.goma_config.log_dir.dir,
331 stats_file=input_proto.goma_config.stats_file,
332 counterz_file=input_proto.goma_config.counterz_file)
333 archiver_tuple = archiver.Archive()
334 if archiver_tuple.stats_file:
335 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
336 if archiver_tuple.counterz_file:
Michael Mortensen1c7439c2020-01-24 14:43:19 -0700337 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
Michael Mortensen798ee192020-01-17 13:04:43 -0700338 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
339
Will Bradley7e5b8c12019-07-30 12:44:15 -0600340 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600341 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600342
Alex Kleina9d64602019-05-17 14:55:37 -0600343
344def _LogBinhost(board):
345 """Log the portage binhost for the given board."""
346 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
347 allow_undefined=True)
348 if not binhost:
349 logging.warning('Portage Binhost not found.')
350 else:
351 logging.info('Portage Binhost: %s', binhost)