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 | |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 10 | import os |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 11 | import sys |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 12 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 13 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 14 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 15 | from chromite.api import validate |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 16 | from chromite.api.controller import controller_util |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 17 | from chromite.api.metrics import deserialize_metrics_log |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 18 | from chromite.lib import binpkg |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 20 | from chromite.lib import cros_logging as logging |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 21 | from chromite.lib import goma_lib |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 22 | from chromite.lib import osutils |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 23 | from chromite.lib import portage_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 24 | from chromite.lib import sysroot_lib |
| 25 | from chromite.service import sysroot |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 26 | from chromite.utils import metrics |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 27 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 28 | |
| 29 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 30 | |
| 31 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 32 | _ACCEPTED_LICENSES = '@CHROMEOS' |
| 33 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 34 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 35 | @faux.all_empty |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 36 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 37 | @validate.validation_complete |
| 38 | def Create(input_proto, output_proto, _config): |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 39 | """Create or replace a sysroot.""" |
| 40 | update_chroot = not input_proto.flags.chroot_current |
| 41 | replace_sysroot = input_proto.flags.replace |
| 42 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 43 | build_target = controller_util.ParseBuildTarget(input_proto.build_target, |
| 44 | input_proto.profile) |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 45 | package_indexes = [ |
| 46 | binpkg.PackageIndexInfo.from_protobuf(x) |
| 47 | for x in input_proto.package_indexes |
| 48 | ] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 49 | run_configs = sysroot.SetupBoardRunConfig( |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 50 | force=replace_sysroot, upgrade_chroot=update_chroot, |
| 51 | package_indexes=package_indexes) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 52 | |
| 53 | try: |
| 54 | created = sysroot.Create(build_target, run_configs, |
| 55 | accept_licenses=_ACCEPTED_LICENSES) |
| 56 | except sysroot.Error as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 57 | cros_build_lib.Die(e) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 58 | |
| 59 | output_proto.sysroot.path = created.path |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 60 | output_proto.sysroot.build_target.name = build_target.name |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 61 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 62 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 63 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 64 | |
Michael Mortensen | 98592f6 | 2019-09-27 13:34:18 -0600 | [diff] [blame] | 65 | @faux.all_empty |
| 66 | @validate.require('build_target.name') |
| 67 | @validate.validation_complete |
| 68 | def CreateSimpleChromeSysroot(input_proto, output_proto, _config): |
| 69 | """Create a sysroot for SimpleChrome to use.""" |
| 70 | build_target_name = input_proto.build_target.name |
| 71 | use_flags = input_proto.use_flags |
| 72 | |
| 73 | sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name, |
| 74 | use_flags) |
| 75 | # By assigning this Path variable to the tar path, the tar file will be |
| 76 | # copied out to the input_proto's ResultPath location. |
| 77 | output_proto.sysroot_archive.path = sysroot_tar_path |
| 78 | |
| 79 | return controller.RETURN_CODE_SUCCESS |
| 80 | |
| 81 | |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 82 | @faux.all_empty |
| 83 | @validate.require('build_target.name', 'packages') |
| 84 | @validate.validation_complete |
| 85 | def GenerateArchive(input_proto, output_proto, _config): |
| 86 | """Generate a sysroot. Typically used by informational builders.""" |
| 87 | build_target_name = input_proto.build_target.name |
| 88 | pkg_list = [] |
| 89 | for package in input_proto.packages: |
| 90 | pkg_list.append('%s/%s' % (package.category, package.package_name)) |
| 91 | |
| 92 | with osutils.TempDir(delete=False) as temp_output_dir: |
| 93 | sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir, |
| 94 | build_target_name, |
| 95 | pkg_list) |
| 96 | |
| 97 | # By assigning this Path variable to the tar path, the tar file will be |
| 98 | # copied out to the input_proto's ResultPath location. |
| 99 | output_proto.sysroot_archive.path = sysroot_tar_path |
| 100 | |
| 101 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 102 | def _MockFailedPackagesResponse(_input_proto, output_proto, _config): |
| 103 | """Mock error response that populates failed packages.""" |
| 104 | pkg = output_proto.failed_packages.add() |
| 105 | pkg.package_name = 'package' |
| 106 | pkg.category = 'category' |
| 107 | pkg.version = '1.0.0_rc-r1' |
| 108 | |
| 109 | pkg2 = output_proto.failed_packages.add() |
| 110 | pkg2.package_name = 'bar' |
| 111 | pkg2.category = 'foo' |
| 112 | pkg2.version = '3.7-r99' |
| 113 | |
| 114 | |
| 115 | @faux.empty_success |
| 116 | @faux.error(_MockFailedPackagesResponse) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 117 | @validate.require('sysroot.path', 'sysroot.build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 118 | @validate.exists('sysroot.path') |
| 119 | @validate.validation_complete |
| 120 | def InstallToolchain(input_proto, output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 121 | """Install the toolchain into a sysroot.""" |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 122 | compile_source = ( |
| 123 | input_proto.flags.compile_source or input_proto.flags.toolchain_changed) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 124 | |
| 125 | sysroot_path = input_proto.sysroot.path |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 126 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 127 | build_target = controller_util.ParseBuildTarget( |
| 128 | input_proto.sysroot.build_target) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 129 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 6682ae1 | 2019-06-25 13:50:32 -0600 | [diff] [blame] | 130 | run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 131 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 132 | _LogBinhost(build_target.name) |
| 133 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 134 | try: |
| 135 | sysroot.InstallToolchain(build_target, target_sysroot, run_configs) |
| 136 | except sysroot_lib.ToolchainInstallError as e: |
| 137 | # Error installing - populate the failed package info. |
| 138 | for package in e.failed_toolchain_info: |
| 139 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 140 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 141 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 142 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 143 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 144 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 145 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 146 | |
| 147 | @faux.empty_success |
| 148 | @faux.error(_MockFailedPackagesResponse) |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 149 | @validate.require('sysroot.build_target.name') |
| 150 | @validate.exists('sysroot.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 151 | @validate.validation_complete |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 152 | @metrics.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 153 | def InstallPackages(input_proto, output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 154 | """Install packages into a sysroot, building as necessary and permitted.""" |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 155 | compile_source = ( |
| 156 | input_proto.flags.compile_source or input_proto.flags.toolchain_changed) |
LaMont Jones | 6dc755b | 2020-07-17 12:06:29 -0600 | [diff] [blame^] | 157 | # Testing if Goma will support unknown compilers now. |
| 158 | use_goma = input_proto.flags.use_goma |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 159 | |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 160 | target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) |
| 161 | build_target = controller_util.ParseBuildTarget( |
| 162 | input_proto.sysroot.build_target) |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 163 | |
| 164 | # Get the package atom for each specified package. The field is optional, so |
| 165 | # error only when we cannot parse an atom for each of the given packages. |
| 166 | packages = [controller_util.PackageInfoToCPV(x).cp |
Mike Frysinger | 66ce413 | 2019-07-17 22:52:52 -0400 | [diff] [blame] | 167 | for x in input_proto.packages] |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 168 | if input_proto.packages and not all(packages): |
| 169 | cros_build_lib.Die( |
| 170 | 'Invalid package(s) specified. Unable to parse atom from all packages.') |
| 171 | |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 172 | package_indexes = [ |
| 173 | binpkg.PackageIndexInfo.from_protobuf(x) |
| 174 | for x in input_proto.package_indexes |
| 175 | ] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 176 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 177 | if not target_sysroot.IsToolchainInstalled(): |
| 178 | cros_build_lib.Die('Toolchain must first be installed.') |
| 179 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 180 | _LogBinhost(build_target.name) |
| 181 | |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 182 | use_flags = [u.flag for u in input_proto.use_flags] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 183 | build_packages_config = sysroot.BuildPackagesRunConfig( |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 184 | usepkg=not compile_source, |
| 185 | install_debug_symbols=True, |
| 186 | packages=packages, |
LaMont Jones | feffd1b | 2020-08-05 18:24:59 -0600 | [diff] [blame] | 187 | package_indexes=package_indexes, |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 188 | use_flags=use_flags, |
Alex Klein | 5b94048 | 2019-12-26 10:38:39 -0700 | [diff] [blame] | 189 | use_goma=use_goma, |
| 190 | incremental_build=False) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 191 | |
| 192 | try: |
| 193 | sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) |
| 194 | except sysroot_lib.PackageInstallError as e: |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 195 | if not e.failed_packages: |
| 196 | # No packages to report, so just exit with an error code. |
| 197 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 198 | |
| 199 | # We need to report the failed packages. |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 200 | for package in e.failed_packages: |
| 201 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 202 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 203 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 204 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 205 | |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 206 | # Copy goma logs to specified directory if there is a goma_config and |
| 207 | # it contains a log_dir to store artifacts. |
| 208 | if input_proto.goma_config.log_dir.dir: |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 209 | # Get the goma log directory based on the GLOG_log_dir env variable. |
| 210 | # TODO(crbug.com/1045001): Replace environment variable with query to |
| 211 | # goma object after goma refactoring allows this. |
| 212 | log_source_dir = os.getenv('GLOG_log_dir') |
| 213 | if not log_source_dir: |
| 214 | cros_build_lib.Die('GLOG_log_dir must be defined.') |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 215 | archiver = goma_lib.LogsArchiver( |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 216 | log_source_dir, |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 217 | dest_dir=input_proto.goma_config.log_dir.dir, |
| 218 | stats_file=input_proto.goma_config.stats_file, |
| 219 | counterz_file=input_proto.goma_config.counterz_file) |
| 220 | archiver_tuple = archiver.Archive() |
| 221 | if archiver_tuple.stats_file: |
| 222 | output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file |
| 223 | if archiver_tuple.counterz_file: |
Michael Mortensen | 1c7439c | 2020-01-24 14:43:19 -0700 | [diff] [blame] | 224 | output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 225 | output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files |
| 226 | |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 227 | # Read metric events log and pipe them into output_proto.events. |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 228 | deserialize_metrics_log(output_proto.events, prefix=build_target.name) |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 229 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 230 | |
| 231 | def _LogBinhost(board): |
| 232 | """Log the portage binhost for the given board.""" |
| 233 | binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board, |
| 234 | allow_undefined=True) |
| 235 | if not binhost: |
| 236 | logging.warning('Portage Binhost not found.') |
| 237 | else: |
| 238 | logging.info('Portage Binhost: %s', binhost) |