blob: 4c21ecacb7e91dc6a3dc896bf54f9e878b7a4c86 [file] [log] [blame]
Alex Kleinda35fcf2019-03-07 16:01:15 -07001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Sysroot controller."""
7
8from __future__ import print_function
9
Michael Mortensen4ccfb082020-01-22 16:24:03 -070010import os
11
Alex Klein8cb365a2019-05-15 16:24:53 -060012from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060013from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060014from chromite.api import validate
Alex Kleina9d500b2019-04-22 15:37:51 -060015from chromite.api.controller import controller_util
Will Bradley7e5b8c12019-07-30 12:44:15 -060016from chromite.api.metrics import deserialize_metrics_log
Alex Kleinda35fcf2019-03-07 16:01:15 -070017from chromite.lib import build_target_util
18from 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
Alex Klein076841b2019-08-29 15:19:39 -060030@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060031@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060032@validate.validation_complete
33def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070034 """Create or replace a sysroot."""
35 update_chroot = not input_proto.flags.chroot_current
36 replace_sysroot = input_proto.flags.replace
37
38 build_target_name = input_proto.build_target.name
39 profile = input_proto.profile.name or None
40
Alex Kleinda35fcf2019-03-07 16:01:15 -070041 build_target = build_target_util.BuildTarget(name=build_target_name,
42 profile=profile)
Alex Kleind4e1e422019-03-18 16:00:41 -060043 run_configs = sysroot.SetupBoardRunConfig(
44 force=replace_sysroot, upgrade_chroot=update_chroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -070045
46 try:
47 created = sysroot.Create(build_target, run_configs,
48 accept_licenses=_ACCEPTED_LICENSES)
49 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040050 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -070051
52 output_proto.sysroot.path = created.path
53 output_proto.sysroot.build_target.name = build_target_name
54
Alex Klein076841b2019-08-29 15:19:39 -060055 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -070056
Alex Klein076841b2019-08-29 15:19:39 -060057
Michael Mortensen98592f62019-09-27 13:34:18 -060058@faux.all_empty
59@validate.require('build_target.name')
60@validate.validation_complete
61def CreateSimpleChromeSysroot(input_proto, output_proto, _config):
62 """Create a sysroot for SimpleChrome to use."""
63 build_target_name = input_proto.build_target.name
64 use_flags = input_proto.use_flags
65
66 sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name,
67 use_flags)
68 # By assigning this Path variable to the tar path, the tar file will be
69 # copied out to the input_proto's ResultPath location.
70 output_proto.sysroot_archive.path = sysroot_tar_path
71
72 return controller.RETURN_CODE_SUCCESS
73
74
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070075@faux.all_empty
76@validate.require('build_target.name', 'packages')
77@validate.validation_complete
78def GenerateArchive(input_proto, output_proto, _config):
79 """Generate a sysroot. Typically used by informational builders."""
80 build_target_name = input_proto.build_target.name
81 pkg_list = []
82 for package in input_proto.packages:
83 pkg_list.append('%s/%s' % (package.category, package.package_name))
84
85 with osutils.TempDir(delete=False) as temp_output_dir:
86 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
87 build_target_name,
88 pkg_list)
89
90 # By assigning this Path variable to the tar path, the tar file will be
91 # copied out to the input_proto's ResultPath location.
92 output_proto.sysroot_archive.path = sysroot_tar_path
93
94
Alex Klein076841b2019-08-29 15:19:39 -060095def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
96 """Mock error response that populates failed packages."""
97 pkg = output_proto.failed_packages.add()
98 pkg.package_name = 'package'
99 pkg.category = 'category'
100 pkg.version = '1.0.0_rc-r1'
101
102 pkg2 = output_proto.failed_packages.add()
103 pkg2.package_name = 'bar'
104 pkg2.category = 'foo'
105 pkg2.version = '3.7-r99'
106
107
108@faux.empty_success
109@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600110@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600111@validate.exists('sysroot.path')
112@validate.validation_complete
113def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600114 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700115 compile_source = (
116 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700117
118 sysroot_path = input_proto.sysroot.path
119 build_target_name = input_proto.sysroot.build_target.name
120
Alex Kleinda35fcf2019-03-07 16:01:15 -0700121 build_target = build_target_util.BuildTarget(name=build_target_name)
122 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600123 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700124
Alex Kleina9d64602019-05-17 14:55:37 -0600125 _LogBinhost(build_target.name)
126
Alex Kleinda35fcf2019-03-07 16:01:15 -0700127 try:
128 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
129 except sysroot_lib.ToolchainInstallError as e:
130 # Error installing - populate the failed package info.
131 for package in e.failed_toolchain_info:
132 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600133 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700134
Alex Klein8cb365a2019-05-15 16:24:53 -0600135 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600136
Alex Klein076841b2019-08-29 15:19:39 -0600137 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600138
Alex Klein076841b2019-08-29 15:19:39 -0600139
140@faux.empty_success
141@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600142@validate.require('sysroot.build_target.name')
143@validate.exists('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600144@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600145@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600146def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600147 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700148 compile_source = (
149 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600150 event_file = input_proto.flags.event_file
Chris McDonald68faa2a2020-01-13 12:23:05 -0700151 # A new toolchain version will not yet have goma support, so goma must be
152 # disabled when we are testing toolchain changes.
153 use_goma = (
154 input_proto.flags.use_goma and not input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600155
Alex Klein566d80e2019-09-24 12:27:58 -0600156 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
157 build_target = controller_util.ParseBuildTarget(
158 input_proto.sysroot.build_target)
Mike Frysinger66ce4132019-07-17 22:52:52 -0400159 packages = [controller_util.PackageInfoToString(x)
160 for x in input_proto.packages]
Alex Kleind4e1e422019-03-18 16:00:41 -0600161
Alex Kleind4e1e422019-03-18 16:00:41 -0600162 if not target_sysroot.IsToolchainInstalled():
163 cros_build_lib.Die('Toolchain must first be installed.')
164
Alex Kleina9d64602019-05-17 14:55:37 -0600165 _LogBinhost(build_target.name)
166
Alex Kleinbe0bae42019-05-06 13:01:49 -0600167 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600168 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600169 event_file=event_file,
170 usepkg=not compile_source,
171 install_debug_symbols=True,
172 packages=packages,
173 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700174 use_goma=use_goma,
175 incremental_build=False)
Alex Kleind4e1e422019-03-18 16:00:41 -0600176
177 try:
178 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
179 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600180 if not e.failed_packages:
181 # No packages to report, so just exit with an error code.
182 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
183
184 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600185 for package in e.failed_packages:
186 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600187 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600188
Alex Klein8cb365a2019-05-15 16:24:53 -0600189 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600190
Michael Mortensen798ee192020-01-17 13:04:43 -0700191 # Copy goma logs to specified directory if there is a goma_config and
192 # it contains a log_dir to store artifacts.
193 if input_proto.goma_config.log_dir.dir:
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700194 # Get the goma log directory based on the GLOG_log_dir env variable.
195 # TODO(crbug.com/1045001): Replace environment variable with query to
196 # goma object after goma refactoring allows this.
197 log_source_dir = os.getenv('GLOG_log_dir')
198 if not log_source_dir:
199 cros_build_lib.Die('GLOG_log_dir must be defined.')
Michael Mortensen798ee192020-01-17 13:04:43 -0700200 archiver = goma_lib.LogsArchiver(
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700201 log_source_dir,
Michael Mortensen798ee192020-01-17 13:04:43 -0700202 dest_dir=input_proto.goma_config.log_dir.dir,
203 stats_file=input_proto.goma_config.stats_file,
204 counterz_file=input_proto.goma_config.counterz_file)
205 archiver_tuple = archiver.Archive()
206 if archiver_tuple.stats_file:
207 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
208 if archiver_tuple.counterz_file:
Michael Mortensen1c7439c2020-01-24 14:43:19 -0700209 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
Michael Mortensen798ee192020-01-17 13:04:43 -0700210 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
211
Will Bradley7e5b8c12019-07-30 12:44:15 -0600212 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600213 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600214
Alex Kleina9d64602019-05-17 14:55:37 -0600215
216def _LogBinhost(board):
217 """Log the portage binhost for the given board."""
218 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
219 allow_undefined=True)
220 if not binhost:
221 logging.warning('Portage Binhost not found.')
222 else:
223 logging.info('Portage Binhost: %s', binhost)