Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 10 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 11 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 12 | from chromite.api import validate |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 13 | from chromite.api.controller import controller_util |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 14 | from chromite.api.metrics import deserialize_metrics_log |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 15 | from chromite.lib import build_target_util |
| 16 | from chromite.lib import cros_build_lib |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 17 | from chromite.lib import cros_logging as logging |
| 18 | from chromite.lib import portage_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 19 | from chromite.lib import sysroot_lib |
| 20 | from chromite.service import sysroot |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 21 | from chromite.utils import metrics |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 22 | |
| 23 | _ACCEPTED_LICENSES = '@CHROMEOS' |
| 24 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 25 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 26 | @faux.all_empty |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 27 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 28 | @validate.validation_complete |
| 29 | def Create(input_proto, output_proto, _config): |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 30 | """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 Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 37 | build_target = build_target_util.BuildTarget(name=build_target_name, |
| 38 | profile=profile) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 39 | run_configs = sysroot.SetupBoardRunConfig( |
| 40 | force=replace_sysroot, upgrade_chroot=update_chroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 41 | |
| 42 | try: |
| 43 | created = sysroot.Create(build_target, run_configs, |
| 44 | accept_licenses=_ACCEPTED_LICENSES) |
| 45 | except sysroot.Error as e: |
| 46 | cros_build_lib.Die(e.message) |
| 47 | |
| 48 | output_proto.sysroot.path = created.path |
| 49 | output_proto.sysroot.build_target.name = build_target_name |
| 50 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 51 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 52 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 53 | |
Michael Mortensen | 98592f6 | 2019-09-27 13:34:18 -0600 | [diff] [blame^] | 54 | @faux.all_empty |
| 55 | @validate.require('build_target.name') |
| 56 | @validate.validation_complete |
| 57 | def 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 Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 71 | def _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 Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 86 | @validate.require('sysroot.path', 'sysroot.build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 87 | @validate.exists('sysroot.path') |
| 88 | @validate.validation_complete |
| 89 | def InstallToolchain(input_proto, output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 90 | """Install the toolchain into a sysroot.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 91 | compile_source = input_proto.flags.compile_source |
| 92 | |
| 93 | sysroot_path = input_proto.sysroot.path |
| 94 | build_target_name = input_proto.sysroot.build_target.name |
| 95 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 96 | build_target = build_target_util.BuildTarget(name=build_target_name) |
| 97 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 6682ae1 | 2019-06-25 13:50:32 -0600 | [diff] [blame] | 98 | run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 99 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 100 | _LogBinhost(build_target.name) |
| 101 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 102 | try: |
| 103 | sysroot.InstallToolchain(build_target, target_sysroot, run_configs) |
| 104 | except sysroot_lib.ToolchainInstallError as e: |
| 105 | # Error installing - populate the failed package info. |
| 106 | for package in e.failed_toolchain_info: |
| 107 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 108 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 109 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 110 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 111 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 112 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 113 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 114 | |
| 115 | @faux.empty_success |
| 116 | @faux.error(_MockFailedPackagesResponse) |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 117 | @validate.require('sysroot.build_target.name') |
| 118 | @validate.exists('sysroot.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 119 | @validate.validation_complete |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 120 | @metrics.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 121 | def InstallPackages(input_proto, output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 122 | """Install packages into a sysroot, building as necessary and permitted.""" |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 123 | compile_source = input_proto.flags.compile_source |
| 124 | event_file = input_proto.flags.event_file |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 125 | use_goma = input_proto.flags.use_goma |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 126 | |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 127 | target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) |
| 128 | build_target = controller_util.ParseBuildTarget( |
| 129 | input_proto.sysroot.build_target) |
Mike Frysinger | 66ce413 | 2019-07-17 22:52:52 -0400 | [diff] [blame] | 130 | packages = [controller_util.PackageInfoToString(x) |
| 131 | for x in input_proto.packages] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 132 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 133 | if not target_sysroot.IsToolchainInstalled(): |
| 134 | cros_build_lib.Die('Toolchain must first be installed.') |
| 135 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 136 | _LogBinhost(build_target.name) |
| 137 | |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 138 | use_flags = [u.flag for u in input_proto.use_flags] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 139 | build_packages_config = sysroot.BuildPackagesRunConfig( |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 140 | event_file=event_file, |
| 141 | usepkg=not compile_source, |
| 142 | install_debug_symbols=True, |
| 143 | packages=packages, |
| 144 | use_flags=use_flags, |
| 145 | use_goma=use_goma) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 146 | |
| 147 | try: |
| 148 | sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) |
| 149 | except sysroot_lib.PackageInstallError as e: |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 150 | if not e.failed_packages: |
| 151 | # No packages to report, so just exit with an error code. |
| 152 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 153 | |
| 154 | # We need to report the failed packages. |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 155 | for package in e.failed_packages: |
| 156 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 157 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 158 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 159 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 160 | |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 161 | # Read metric events log and pipe them into output_proto.events. |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 162 | deserialize_metrics_log(output_proto.events, prefix=build_target.name) |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 163 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 164 | |
| 165 | def _LogBinhost(board): |
| 166 | """Log the portage binhost for the given board.""" |
| 167 | binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board, |
| 168 | allow_undefined=True) |
| 169 | if not binhost: |
| 170 | logging.warning('Portage Binhost not found.') |
| 171 | else: |
| 172 | logging.info('Portage Binhost: %s', binhost) |