blob: 6fe5bee6b959fa5843c71618602a1dbca87091b8 [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
18from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070019from chromite.lib import sysroot_lib
20from chromite.service import sysroot
Will Bradley7e5b8c12019-07-30 12:44:15 -060021from chromite.utils import metrics
Alex Kleinda35fcf2019-03-07 16:01:15 -070022
23_ACCEPTED_LICENSES = '@CHROMEOS'
24
Alex Kleind4e1e422019-03-18 16:00:41 -060025
Alex Klein076841b2019-08-29 15:19:39 -060026@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060027@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060028@validate.validation_complete
29def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070030 """Create or replace a sysroot."""
31 update_chroot = not input_proto.flags.chroot_current
32 replace_sysroot = input_proto.flags.replace
33
34 build_target_name = input_proto.build_target.name
35 profile = input_proto.profile.name or None
36
Alex Kleinda35fcf2019-03-07 16:01:15 -070037 build_target = build_target_util.BuildTarget(name=build_target_name,
38 profile=profile)
Alex Kleind4e1e422019-03-18 16:00:41 -060039 run_configs = sysroot.SetupBoardRunConfig(
40 force=replace_sysroot, upgrade_chroot=update_chroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -070041
42 try:
43 created = sysroot.Create(build_target, run_configs,
44 accept_licenses=_ACCEPTED_LICENSES)
45 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040046 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -070047
48 output_proto.sysroot.path = created.path
49 output_proto.sysroot.build_target.name = build_target_name
50
Alex Klein076841b2019-08-29 15:19:39 -060051 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -070052
Alex Klein076841b2019-08-29 15:19:39 -060053
Michael Mortensen98592f62019-09-27 13:34:18 -060054@faux.all_empty
55@validate.require('build_target.name')
56@validate.validation_complete
57def CreateSimpleChromeSysroot(input_proto, output_proto, _config):
58 """Create a sysroot for SimpleChrome to use."""
59 build_target_name = input_proto.build_target.name
60 use_flags = input_proto.use_flags
61
62 sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name,
63 use_flags)
64 # By assigning this Path variable to the tar path, the tar file will be
65 # copied out to the input_proto's ResultPath location.
66 output_proto.sysroot_archive.path = sysroot_tar_path
67
68 return controller.RETURN_CODE_SUCCESS
69
70
Alex Klein076841b2019-08-29 15:19:39 -060071def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
72 """Mock error response that populates failed packages."""
73 pkg = output_proto.failed_packages.add()
74 pkg.package_name = 'package'
75 pkg.category = 'category'
76 pkg.version = '1.0.0_rc-r1'
77
78 pkg2 = output_proto.failed_packages.add()
79 pkg2.package_name = 'bar'
80 pkg2.category = 'foo'
81 pkg2.version = '3.7-r99'
82
83
84@faux.empty_success
85@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -060086@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060087@validate.exists('sysroot.path')
88@validate.validation_complete
89def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060090 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -070091 compile_source = (
92 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -070093
94 sysroot_path = input_proto.sysroot.path
95 build_target_name = input_proto.sysroot.build_target.name
96
Alex Kleinda35fcf2019-03-07 16:01:15 -070097 build_target = build_target_util.BuildTarget(name=build_target_name)
98 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -060099 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700100
Alex Kleina9d64602019-05-17 14:55:37 -0600101 _LogBinhost(build_target.name)
102
Alex Kleinda35fcf2019-03-07 16:01:15 -0700103 try:
104 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
105 except sysroot_lib.ToolchainInstallError as e:
106 # Error installing - populate the failed package info.
107 for package in e.failed_toolchain_info:
108 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600109 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700110
Alex Klein8cb365a2019-05-15 16:24:53 -0600111 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600112
Alex Klein076841b2019-08-29 15:19:39 -0600113 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600114
Alex Klein076841b2019-08-29 15:19:39 -0600115
116@faux.empty_success
117@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600118@validate.require('sysroot.build_target.name')
119@validate.exists('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600120@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600121@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600122def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600123 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700124 compile_source = (
125 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600126 event_file = input_proto.flags.event_file
Chris McDonald68faa2a2020-01-13 12:23:05 -0700127 # A new toolchain version will not yet have goma support, so goma must be
128 # disabled when we are testing toolchain changes.
129 use_goma = (
130 input_proto.flags.use_goma and not input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600131
Alex Klein566d80e2019-09-24 12:27:58 -0600132 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
133 build_target = controller_util.ParseBuildTarget(
134 input_proto.sysroot.build_target)
Mike Frysinger66ce4132019-07-17 22:52:52 -0400135 packages = [controller_util.PackageInfoToString(x)
136 for x in input_proto.packages]
Alex Kleind4e1e422019-03-18 16:00:41 -0600137
Alex Kleind4e1e422019-03-18 16:00:41 -0600138 if not target_sysroot.IsToolchainInstalled():
139 cros_build_lib.Die('Toolchain must first be installed.')
140
Alex Kleina9d64602019-05-17 14:55:37 -0600141 _LogBinhost(build_target.name)
142
Alex Kleinbe0bae42019-05-06 13:01:49 -0600143 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600144 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600145 event_file=event_file,
146 usepkg=not compile_source,
147 install_debug_symbols=True,
148 packages=packages,
149 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700150 use_goma=use_goma,
151 incremental_build=False)
Alex Kleind4e1e422019-03-18 16:00:41 -0600152
153 try:
154 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
155 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600156 if not e.failed_packages:
157 # No packages to report, so just exit with an error code.
158 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
159
160 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600161 for package in e.failed_packages:
162 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600163 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600164
Alex Klein8cb365a2019-05-15 16:24:53 -0600165 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600166
Will Bradley7e5b8c12019-07-30 12:44:15 -0600167 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600168 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600169
Alex Kleina9d64602019-05-17 14:55:37 -0600170
171def _LogBinhost(board):
172 """Log the portage binhost for the given board."""
173 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
174 allow_undefined=True)
175 if not binhost:
176 logging.warning('Portage Binhost not found.')
177 else:
178 logging.info('Portage Binhost: %s', binhost)