blob: b6e925c4898f517bb25badbd850c34be2b21d440 [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
Alex Kleinda35fcf2019-03-07 16:01:15 -070016from chromite.lib import cros_build_lib
Alex Kleina9d64602019-05-17 14:55:37 -060017from chromite.lib import cros_logging as logging
Michael Mortensen798ee192020-01-17 13:04:43 -070018from chromite.lib import goma_lib
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070019from chromite.lib import osutils
Alex Kleina9d64602019-05-17 14:55:37 -060020from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070021from chromite.lib import sysroot_lib
22from chromite.service import sysroot
Will Bradley7e5b8c12019-07-30 12:44:15 -060023from chromite.utils import metrics
Alex Kleinda35fcf2019-03-07 16:01:15 -070024
Mike Frysingeref94e4c2020-02-10 23:59:54 -050025
Alex Kleinda35fcf2019-03-07 16:01:15 -070026_ACCEPTED_LICENSES = '@CHROMEOS'
27
Alex Kleind4e1e422019-03-18 16:00:41 -060028
Alex Klein076841b2019-08-29 15:19:39 -060029@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060030@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060031@validate.validation_complete
32def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070033 """Create or replace a sysroot."""
34 update_chroot = not input_proto.flags.chroot_current
35 replace_sysroot = input_proto.flags.replace
36
Alex Klein26e472b2020-03-10 14:35:01 -060037 build_target = controller_util.ParseBuildTarget(input_proto.build_target,
38 input_proto.profile)
LaMont Jonesfeffd1b2020-08-05 18:24:59 -060039 package_indexes = [
40 binpkg.PackageIndexInfo.from_protobuf(x)
41 for x in input_proto.package_indexes
42 ]
Alex Kleind4e1e422019-03-18 16:00:41 -060043 run_configs = sysroot.SetupBoardRunConfig(
LaMont Jonesfeffd1b2020-08-05 18:24:59 -060044 force=replace_sysroot, upgrade_chroot=update_chroot,
45 package_indexes=package_indexes)
Alex Kleinda35fcf2019-03-07 16:01:15 -070046
47 try:
48 created = sysroot.Create(build_target, run_configs,
49 accept_licenses=_ACCEPTED_LICENSES)
50 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040051 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -070052
53 output_proto.sysroot.path = created.path
Alex Klein26e472b2020-03-10 14:35:01 -060054 output_proto.sysroot.build_target.name = build_target.name
Alex Kleinda35fcf2019-03-07 16:01:15 -070055
Alex Klein076841b2019-08-29 15:19:39 -060056 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -070057
Alex Klein076841b2019-08-29 15:19:39 -060058
Michael Mortensen98592f62019-09-27 13:34:18 -060059@faux.all_empty
60@validate.require('build_target.name')
61@validate.validation_complete
62def CreateSimpleChromeSysroot(input_proto, output_proto, _config):
63 """Create a sysroot for SimpleChrome to use."""
64 build_target_name = input_proto.build_target.name
65 use_flags = input_proto.use_flags
66
67 sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name,
68 use_flags)
69 # By assigning this Path variable to the tar path, the tar file will be
70 # copied out to the input_proto's ResultPath location.
71 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -070072 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen98592f62019-09-27 13:34:18 -060073
74 return controller.RETURN_CODE_SUCCESS
75
76
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070077@faux.all_empty
78@validate.require('build_target.name', 'packages')
Alex Klein45b73432020-09-23 13:51:20 -060079@validate.require_each('packages', ['category', 'package_name'])
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070080@validate.validation_complete
81def GenerateArchive(input_proto, output_proto, _config):
82 """Generate a sysroot. Typically used by informational builders."""
83 build_target_name = input_proto.build_target.name
84 pkg_list = []
85 for package in input_proto.packages:
86 pkg_list.append('%s/%s' % (package.category, package.package_name))
87
88 with osutils.TempDir(delete=False) as temp_output_dir:
89 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
90 build_target_name,
91 pkg_list)
92
93 # By assigning this Path variable to the tar path, the tar file will be
94 # copied out to the input_proto's ResultPath location.
95 output_proto.sysroot_archive.path = sysroot_tar_path
Alex Klein6b499df2020-11-19 14:26:56 -070096 output_proto.sysroot_archive.location = common_pb2.Path.INSIDE
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070097
98
Alex Klein076841b2019-08-29 15:19:39 -060099def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
100 """Mock error response that populates failed packages."""
101 pkg = output_proto.failed_packages.add()
102 pkg.package_name = 'package'
103 pkg.category = 'category'
104 pkg.version = '1.0.0_rc-r1'
105
106 pkg2 = output_proto.failed_packages.add()
107 pkg2.package_name = 'bar'
108 pkg2.category = 'foo'
109 pkg2.version = '3.7-r99'
110
111
112@faux.empty_success
113@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600114@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600115@validate.exists('sysroot.path')
116@validate.validation_complete
117def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600118 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700119 compile_source = (
120 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700121
122 sysroot_path = input_proto.sysroot.path
Alex Kleinda35fcf2019-03-07 16:01:15 -0700123
Alex Klein26e472b2020-03-10 14:35:01 -0600124 build_target = controller_util.ParseBuildTarget(
125 input_proto.sysroot.build_target)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700126 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600127 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700128
Alex Kleina9d64602019-05-17 14:55:37 -0600129 _LogBinhost(build_target.name)
130
Alex Kleinda35fcf2019-03-07 16:01:15 -0700131 try:
132 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
133 except sysroot_lib.ToolchainInstallError as e:
134 # Error installing - populate the failed package info.
135 for package in e.failed_toolchain_info:
136 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600137 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700138
Alex Klein8cb365a2019-05-15 16:24:53 -0600139 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600140
Alex Klein076841b2019-08-29 15:19:39 -0600141 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600142
Alex Klein076841b2019-08-29 15:19:39 -0600143
144@faux.empty_success
145@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600146@validate.require('sysroot.build_target.name')
147@validate.exists('sysroot.path')
Alex Klein45b73432020-09-23 13:51:20 -0600148@validate.require_each('packages', ['category', 'package_name'])
149@validate.require_each('use_flags', ['flag'])
Alex Klein231d2da2019-07-22 16:44:45 -0600150@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600151@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600152def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600153 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700154 compile_source = (
155 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
LaMont Jones6dc755b2020-07-17 12:06:29 -0600156 # Testing if Goma will support unknown compilers now.
157 use_goma = input_proto.flags.use_goma
Alex Kleind4e1e422019-03-18 16:00:41 -0600158
Alex Klein566d80e2019-09-24 12:27:58 -0600159 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
160 build_target = controller_util.ParseBuildTarget(
161 input_proto.sysroot.build_target)
Alex Kleinca572ee2020-09-03 10:47:14 -0600162
163 # Get the package atom for each specified package. The field is optional, so
164 # error only when we cannot parse an atom for each of the given packages.
165 packages = [controller_util.PackageInfoToCPV(x).cp
Mike Frysinger66ce4132019-07-17 22:52:52 -0400166 for x in input_proto.packages]
Alex Kleinca572ee2020-09-03 10:47:14 -0600167
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600168 package_indexes = [
169 binpkg.PackageIndexInfo.from_protobuf(x)
170 for x in input_proto.package_indexes
171 ]
Alex Kleind4e1e422019-03-18 16:00:41 -0600172
Alex Kleind4e1e422019-03-18 16:00:41 -0600173 if not target_sysroot.IsToolchainInstalled():
174 cros_build_lib.Die('Toolchain must first be installed.')
175
Alex Kleina9d64602019-05-17 14:55:37 -0600176 _LogBinhost(build_target.name)
177
Alex Kleinbe0bae42019-05-06 13:01:49 -0600178 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600179 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600180 usepkg=not compile_source,
181 install_debug_symbols=True,
182 packages=packages,
LaMont Jonesfeffd1b2020-08-05 18:24:59 -0600183 package_indexes=package_indexes,
Alex Klein566d80e2019-09-24 12:27:58 -0600184 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700185 use_goma=use_goma,
Alex Kleineb76da72021-03-19 10:43:09 -0600186 incremental_build=False,
187 setup_board=False)
Alex Kleind4e1e422019-03-18 16:00:41 -0600188
189 try:
190 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
191 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600192 if not e.failed_packages:
193 # No packages to report, so just exit with an error code.
194 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
195
196 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600197 for package in e.failed_packages:
198 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600199 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600200
Alex Klein8cb365a2019-05-15 16:24:53 -0600201 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600202
Michael Mortensen798ee192020-01-17 13:04:43 -0700203 # Copy goma logs to specified directory if there is a goma_config and
204 # it contains a log_dir to store artifacts.
205 if input_proto.goma_config.log_dir.dir:
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700206 # Get the goma log directory based on the GLOG_log_dir env variable.
207 # TODO(crbug.com/1045001): Replace environment variable with query to
208 # goma object after goma refactoring allows this.
209 log_source_dir = os.getenv('GLOG_log_dir')
210 if not log_source_dir:
211 cros_build_lib.Die('GLOG_log_dir must be defined.')
Michael Mortensen798ee192020-01-17 13:04:43 -0700212 archiver = goma_lib.LogsArchiver(
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700213 log_source_dir,
Michael Mortensen798ee192020-01-17 13:04:43 -0700214 dest_dir=input_proto.goma_config.log_dir.dir,
215 stats_file=input_proto.goma_config.stats_file,
216 counterz_file=input_proto.goma_config.counterz_file)
217 archiver_tuple = archiver.Archive()
218 if archiver_tuple.stats_file:
219 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
220 if archiver_tuple.counterz_file:
Michael Mortensen1c7439c2020-01-24 14:43:19 -0700221 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
Michael Mortensen798ee192020-01-17 13:04:43 -0700222 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
223
Will Bradley7e5b8c12019-07-30 12:44:15 -0600224 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600225 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600226
Alex Kleina9d64602019-05-17 14:55:37 -0600227
228def _LogBinhost(board):
229 """Log the portage binhost for the given board."""
230 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
231 allow_undefined=True)
232 if not binhost:
233 logging.warning('Portage Binhost not found.')
234 else:
235 logging.info('Portage Binhost: %s', binhost)