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 | |
| 10 | from chromite.lib import build_target_util |
| 11 | from chromite.lib import cros_build_lib |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame^] | 12 | from chromite.lib import portage_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 13 | from chromite.lib import sysroot_lib |
| 14 | from chromite.service import sysroot |
| 15 | |
| 16 | _ACCEPTED_LICENSES = '@CHROMEOS' |
| 17 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame^] | 18 | # Return codes. |
| 19 | RC_ERROR = 1 |
| 20 | |
| 21 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 22 | def Create(input_proto, output_proto): |
| 23 | """Create or replace a sysroot.""" |
| 24 | update_chroot = not input_proto.flags.chroot_current |
| 25 | replace_sysroot = input_proto.flags.replace |
| 26 | |
| 27 | build_target_name = input_proto.build_target.name |
| 28 | profile = input_proto.profile.name or None |
| 29 | |
| 30 | if not build_target_name: |
| 31 | cros_build_lib.Die('The build target must be provided.') |
| 32 | |
| 33 | build_target = build_target_util.BuildTarget(name=build_target_name, |
| 34 | profile=profile) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame^] | 35 | run_configs = sysroot.SetupBoardRunConfig( |
| 36 | force=replace_sysroot, upgrade_chroot=update_chroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 37 | |
| 38 | try: |
| 39 | created = sysroot.Create(build_target, run_configs, |
| 40 | accept_licenses=_ACCEPTED_LICENSES) |
| 41 | except sysroot.Error as e: |
| 42 | cros_build_lib.Die(e.message) |
| 43 | |
| 44 | output_proto.sysroot.path = created.path |
| 45 | output_proto.sysroot.build_target.name = build_target_name |
| 46 | |
| 47 | |
| 48 | def InstallToolchain(input_proto, output_proto): |
| 49 | compile_source = input_proto.flags.compile_source |
| 50 | |
| 51 | sysroot_path = input_proto.sysroot.path |
| 52 | build_target_name = input_proto.sysroot.build_target.name |
| 53 | |
| 54 | if not sysroot_path: |
| 55 | cros_build_lib.Die('sysroot.path is required.') |
| 56 | if not build_target_name: |
| 57 | cros_build_lib.Die('sysroot.build_target.name is required.') |
| 58 | |
| 59 | build_target = build_target_util.BuildTarget(name=build_target_name) |
| 60 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 61 | run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source) |
| 62 | |
| 63 | if not target_sysroot.Exists(): |
| 64 | cros_build_lib.Die('Sysroot has not been created. Run Create first.') |
| 65 | |
| 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 | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame^] | 72 | _CPVToPackageInfo(package, package_info) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 73 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame^] | 74 | return RC_ERROR |
| 75 | |
| 76 | |
| 77 | def InstallPackages(input_proto, output_proto): |
| 78 | compile_source = input_proto.flags.compile_source |
| 79 | event_file = input_proto.flags.event_file |
| 80 | |
| 81 | sysroot_path = input_proto.sysroot.path |
| 82 | build_target_name = input_proto.sysroot.build_target.name |
| 83 | packages = map(_PackageInfoToCPV, input_proto.packages) |
| 84 | |
| 85 | if not build_target_name: |
| 86 | cros_build_lib.Die('Build target name is required.') |
| 87 | if not sysroot_path: |
| 88 | cros_build_lib.Die('Sysroot path is required') |
| 89 | |
| 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 | |
| 96 | build_packages_config = sysroot.BuildPackagesRunConfig( |
| 97 | event_file=event_file, usepkg=not compile_source, |
| 98 | install_debug_symbols=True, packages=packages) |
| 99 | |
| 100 | try: |
| 101 | sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) |
| 102 | except sysroot_lib.PackageInstallError as e: |
| 103 | for package in e.failed_packages: |
| 104 | package_info = output_proto.failed_packages.add() |
| 105 | _CPVToPackageInfo(package, package_info) |
| 106 | |
| 107 | return RC_ERROR |
| 108 | |
| 109 | |
| 110 | def _CPVToPackageInfo(cpv, package_info): |
| 111 | """Helper to translate CPVs into a PackageInfo message.""" |
| 112 | package_info.package_name = cpv.package |
| 113 | if cpv.category: |
| 114 | package_info.category = cpv.category |
| 115 | if cpv.version: |
| 116 | package_info.version = cpv.version |
| 117 | |
| 118 | |
| 119 | def _PackageInfoToCPV(package_info): |
| 120 | """Helper to translate a PackageInfo message into a CPV.""" |
| 121 | if not package_info or not package_info.package_name: |
| 122 | return None |
| 123 | |
| 124 | # Combine the components into the full CPV string that SplitCPV parses. |
| 125 | # TODO: Turn portage_util.CPV into a class that can handle building out an |
| 126 | # instance from components. |
| 127 | c = ('%s/' % package_info.category) if package_info.category else '' |
| 128 | p = package_info.package_name |
| 129 | v = ('-%s' % package_info.version) if package_info.version else '' |
| 130 | cpv = '%s%s%s' % (c, p, v) |
| 131 | |
| 132 | return portage_util.SplitCPV(cpv) |