blob: 6d79fae351105a3a5a0cbb50d363e2d7ac4385ae [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
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
Alex Kleinda35fcf2019-03-07 16:01:15 -070015from chromite.lib import build_target_util
16from 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
Alex Kleina9d64602019-05-17 14:55:37 -060019from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070020from chromite.lib import sysroot_lib
21from chromite.service import sysroot
Will Bradley7e5b8c12019-07-30 12:44:15 -060022from chromite.utils import metrics
Alex Kleinda35fcf2019-03-07 16:01:15 -070023
24_ACCEPTED_LICENSES = '@CHROMEOS'
25
Alex Kleind4e1e422019-03-18 16:00:41 -060026
Alex Klein076841b2019-08-29 15:19:39 -060027@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060028@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060029@validate.validation_complete
30def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070031 """Create or replace a sysroot."""
32 update_chroot = not input_proto.flags.chroot_current
33 replace_sysroot = input_proto.flags.replace
34
35 build_target_name = input_proto.build_target.name
36 profile = input_proto.profile.name or None
37
Alex Kleinda35fcf2019-03-07 16:01:15 -070038 build_target = build_target_util.BuildTarget(name=build_target_name,
39 profile=profile)
Alex Kleind4e1e422019-03-18 16:00:41 -060040 run_configs = sysroot.SetupBoardRunConfig(
41 force=replace_sysroot, upgrade_chroot=update_chroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -070042
43 try:
44 created = sysroot.Create(build_target, run_configs,
45 accept_licenses=_ACCEPTED_LICENSES)
46 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040047 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -070048
49 output_proto.sysroot.path = created.path
50 output_proto.sysroot.build_target.name = build_target_name
51
Alex Klein076841b2019-08-29 15:19:39 -060052 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -070053
Alex Klein076841b2019-08-29 15:19:39 -060054
Michael Mortensen98592f62019-09-27 13:34:18 -060055@faux.all_empty
56@validate.require('build_target.name')
57@validate.validation_complete
58def CreateSimpleChromeSysroot(input_proto, output_proto, _config):
59 """Create a sysroot for SimpleChrome to use."""
60 build_target_name = input_proto.build_target.name
61 use_flags = input_proto.use_flags
62
63 sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name,
64 use_flags)
65 # By assigning this Path variable to the tar path, the tar file will be
66 # copied out to the input_proto's ResultPath location.
67 output_proto.sysroot_archive.path = sysroot_tar_path
68
69 return controller.RETURN_CODE_SUCCESS
70
71
Alex Klein076841b2019-08-29 15:19:39 -060072def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
73 """Mock error response that populates failed packages."""
74 pkg = output_proto.failed_packages.add()
75 pkg.package_name = 'package'
76 pkg.category = 'category'
77 pkg.version = '1.0.0_rc-r1'
78
79 pkg2 = output_proto.failed_packages.add()
80 pkg2.package_name = 'bar'
81 pkg2.category = 'foo'
82 pkg2.version = '3.7-r99'
83
84
85@faux.empty_success
86@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -060087@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060088@validate.exists('sysroot.path')
89@validate.validation_complete
90def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060091 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -070092 compile_source = (
93 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -070094
95 sysroot_path = input_proto.sysroot.path
96 build_target_name = input_proto.sysroot.build_target.name
97
Alex Kleinda35fcf2019-03-07 16:01:15 -070098 build_target = build_target_util.BuildTarget(name=build_target_name)
99 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600100 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700101
Alex Kleina9d64602019-05-17 14:55:37 -0600102 _LogBinhost(build_target.name)
103
Alex Kleinda35fcf2019-03-07 16:01:15 -0700104 try:
105 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
106 except sysroot_lib.ToolchainInstallError as e:
107 # Error installing - populate the failed package info.
108 for package in e.failed_toolchain_info:
109 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600110 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700111
Alex Klein8cb365a2019-05-15 16:24:53 -0600112 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600113
Alex Klein076841b2019-08-29 15:19:39 -0600114 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600115
Alex Klein076841b2019-08-29 15:19:39 -0600116
117@faux.empty_success
118@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600119@validate.require('sysroot.build_target.name')
120@validate.exists('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600121@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600122@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600123def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600124 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700125 compile_source = (
126 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600127 event_file = input_proto.flags.event_file
Chris McDonald68faa2a2020-01-13 12:23:05 -0700128 # A new toolchain version will not yet have goma support, so goma must be
129 # disabled when we are testing toolchain changes.
130 use_goma = (
131 input_proto.flags.use_goma and not input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600132
Alex Klein566d80e2019-09-24 12:27:58 -0600133 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
134 build_target = controller_util.ParseBuildTarget(
135 input_proto.sysroot.build_target)
Mike Frysinger66ce4132019-07-17 22:52:52 -0400136 packages = [controller_util.PackageInfoToString(x)
137 for x in input_proto.packages]
Alex Kleind4e1e422019-03-18 16:00:41 -0600138
Alex Kleind4e1e422019-03-18 16:00:41 -0600139 if not target_sysroot.IsToolchainInstalled():
140 cros_build_lib.Die('Toolchain must first be installed.')
141
Alex Kleina9d64602019-05-17 14:55:37 -0600142 _LogBinhost(build_target.name)
143
Alex Kleinbe0bae42019-05-06 13:01:49 -0600144 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600145 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600146 event_file=event_file,
147 usepkg=not compile_source,
148 install_debug_symbols=True,
149 packages=packages,
150 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700151 use_goma=use_goma,
152 incremental_build=False)
Alex Kleind4e1e422019-03-18 16:00:41 -0600153
154 try:
155 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
156 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600157 if not e.failed_packages:
158 # No packages to report, so just exit with an error code.
159 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
160
161 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600162 for package in e.failed_packages:
163 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600164 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600165
Alex Klein8cb365a2019-05-15 16:24:53 -0600166 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600167
Michael Mortensen798ee192020-01-17 13:04:43 -0700168 # Copy goma logs to specified directory if there is a goma_config and
169 # it contains a log_dir to store artifacts.
170 if input_proto.goma_config.log_dir.dir:
171 archiver = goma_lib.LogsArchiver(
172 input_proto.goma_config.goma_dir,
173 dest_dir=input_proto.goma_config.log_dir.dir,
174 stats_file=input_proto.goma_config.stats_file,
175 counterz_file=input_proto.goma_config.counterz_file)
176 archiver_tuple = archiver.Archive()
177 if archiver_tuple.stats_file:
178 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
179 if archiver_tuple.counterz_file:
180 output_proto.goma_artifacts.stats_file = archiver_tuple.counterz_file
181 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
182
Will Bradley7e5b8c12019-07-30 12:44:15 -0600183 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600184 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600185
Alex Kleina9d64602019-05-17 14:55:37 -0600186
187def _LogBinhost(board):
188 """Log the portage binhost for the given board."""
189 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
190 allow_undefined=True)
191 if not binhost:
192 logging.warning('Portage Binhost not found.')
193 else:
194 logging.info('Portage Binhost: %s', binhost)