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