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 | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 11 | from chromite.api import validate |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 12 | from chromite.api.controller import controller_util |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 13 | from chromite.api.metrics import deserialize_metrics_log |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 14 | from chromite.lib import build_target_util |
| 15 | from chromite.lib import cros_build_lib |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 16 | from chromite.lib import cros_logging as logging |
| 17 | from chromite.lib import portage_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 18 | from chromite.lib import sysroot_lib |
| 19 | from chromite.service import sysroot |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 20 | from chromite.utils import metrics |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 21 | |
| 22 | _ACCEPTED_LICENSES = '@CHROMEOS' |
| 23 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 24 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 25 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 26 | @validate.validation_complete |
| 27 | def Create(input_proto, output_proto, _config): |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 28 | """Create or replace a sysroot.""" |
| 29 | update_chroot = not input_proto.flags.chroot_current |
| 30 | replace_sysroot = input_proto.flags.replace |
| 31 | |
| 32 | build_target_name = input_proto.build_target.name |
| 33 | profile = input_proto.profile.name or None |
| 34 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 35 | build_target = build_target_util.BuildTarget(name=build_target_name, |
| 36 | profile=profile) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 37 | run_configs = sysroot.SetupBoardRunConfig( |
| 38 | force=replace_sysroot, upgrade_chroot=update_chroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 39 | |
| 40 | try: |
| 41 | created = sysroot.Create(build_target, run_configs, |
| 42 | accept_licenses=_ACCEPTED_LICENSES) |
| 43 | except sysroot.Error as e: |
| 44 | cros_build_lib.Die(e.message) |
| 45 | |
| 46 | output_proto.sysroot.path = created.path |
| 47 | output_proto.sysroot.build_target.name = build_target_name |
| 48 | |
| 49 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 50 | @validate.require('sysroot.path', 'sysroot.build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 51 | @validate.exists('sysroot.path') |
| 52 | @validate.validation_complete |
| 53 | def InstallToolchain(input_proto, output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 54 | """Install the toolchain into a sysroot.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 55 | compile_source = input_proto.flags.compile_source |
| 56 | |
| 57 | sysroot_path = input_proto.sysroot.path |
| 58 | build_target_name = input_proto.sysroot.build_target.name |
| 59 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 60 | build_target = build_target_util.BuildTarget(name=build_target_name) |
| 61 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 6682ae1 | 2019-06-25 13:50:32 -0600 | [diff] [blame] | 62 | run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 63 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 64 | _LogBinhost(build_target.name) |
| 65 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 66 | try: |
| 67 | sysroot.InstallToolchain(build_target, target_sysroot, run_configs) |
| 68 | except sysroot_lib.ToolchainInstallError as e: |
| 69 | # Error installing - populate the failed package info. |
| 70 | for package in e.failed_toolchain_info: |
| 71 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 72 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 73 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 74 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 75 | |
| 76 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 77 | @validate.require('sysroot.path', 'sysroot.build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 78 | @validate.validation_complete |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 79 | @metrics.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 80 | def InstallPackages(input_proto, output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 81 | """Install packages into a sysroot, building as necessary and permitted.""" |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 82 | compile_source = input_proto.flags.compile_source |
| 83 | event_file = input_proto.flags.event_file |
| 84 | |
| 85 | sysroot_path = input_proto.sysroot.path |
| 86 | build_target_name = input_proto.sysroot.build_target.name |
Mike Frysinger | 66ce413 | 2019-07-17 22:52:52 -0400 | [diff] [blame] | 87 | packages = [controller_util.PackageInfoToString(x) |
| 88 | for x in input_proto.packages] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 89 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 90 | build_target = build_target_util.BuildTarget(build_target_name) |
| 91 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 92 | |
| 93 | if not target_sysroot.IsToolchainInstalled(): |
| 94 | cros_build_lib.Die('Toolchain must first be installed.') |
| 95 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 96 | _LogBinhost(build_target.name) |
| 97 | |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 98 | use_flags = [u.flag for u in input_proto.use_flags] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 99 | build_packages_config = sysroot.BuildPackagesRunConfig( |
| 100 | event_file=event_file, usepkg=not compile_source, |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 101 | install_debug_symbols=True, packages=packages, use_flags=use_flags) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 102 | |
| 103 | try: |
| 104 | sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) |
| 105 | except sysroot_lib.PackageInstallError as e: |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 106 | if not e.failed_packages: |
| 107 | # No packages to report, so just exit with an error code. |
| 108 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 109 | |
| 110 | # We need to report the failed packages. |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 111 | for package in e.failed_packages: |
| 112 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 113 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 114 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 115 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 116 | |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 117 | # Read metric events log and pipe them into output_proto.events. |
| 118 | deserialize_metrics_log(output_proto.events, prefix=build_target_name) |
| 119 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 120 | |
| 121 | def _LogBinhost(board): |
| 122 | """Log the portage binhost for the given board.""" |
| 123 | binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board, |
| 124 | allow_undefined=True) |
| 125 | if not binhost: |
| 126 | logging.warning('Portage Binhost not found.') |
| 127 | else: |
| 128 | logging.info('Portage Binhost: %s', binhost) |