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 |
| 12 | from chromite.lib import sysroot_lib |
| 13 | from chromite.service import sysroot |
| 14 | |
| 15 | _ACCEPTED_LICENSES = '@CHROMEOS' |
| 16 | |
| 17 | def Create(input_proto, output_proto): |
| 18 | """Create or replace a sysroot.""" |
| 19 | update_chroot = not input_proto.flags.chroot_current |
| 20 | replace_sysroot = input_proto.flags.replace |
| 21 | |
| 22 | build_target_name = input_proto.build_target.name |
| 23 | profile = input_proto.profile.name or None |
| 24 | |
| 25 | if not build_target_name: |
| 26 | cros_build_lib.Die('The build target must be provided.') |
| 27 | |
| 28 | build_target = build_target_util.BuildTarget(name=build_target_name, |
| 29 | profile=profile) |
| 30 | run_configs = sysroot.SetupBoardRunConfig(force=replace_sysroot, |
| 31 | upgrade_chroot=update_chroot) |
| 32 | |
| 33 | try: |
| 34 | created = sysroot.Create(build_target, run_configs, |
| 35 | accept_licenses=_ACCEPTED_LICENSES) |
| 36 | except sysroot.Error as e: |
| 37 | cros_build_lib.Die(e.message) |
| 38 | |
| 39 | output_proto.sysroot.path = created.path |
| 40 | output_proto.sysroot.build_target.name = build_target_name |
| 41 | |
| 42 | |
| 43 | def InstallToolchain(input_proto, output_proto): |
| 44 | compile_source = input_proto.flags.compile_source |
| 45 | |
| 46 | sysroot_path = input_proto.sysroot.path |
| 47 | build_target_name = input_proto.sysroot.build_target.name |
| 48 | |
| 49 | if not sysroot_path: |
| 50 | cros_build_lib.Die('sysroot.path is required.') |
| 51 | if not build_target_name: |
| 52 | cros_build_lib.Die('sysroot.build_target.name is required.') |
| 53 | |
| 54 | build_target = build_target_util.BuildTarget(name=build_target_name) |
| 55 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 56 | run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source) |
| 57 | |
| 58 | if not target_sysroot.Exists(): |
| 59 | cros_build_lib.Die('Sysroot has not been created. Run Create first.') |
| 60 | |
| 61 | try: |
| 62 | sysroot.InstallToolchain(build_target, target_sysroot, run_configs) |
| 63 | except sysroot_lib.ToolchainInstallError as e: |
| 64 | # Error installing - populate the failed package info. |
| 65 | for package in e.failed_toolchain_info: |
| 66 | package_info = output_proto.failed_packages.add() |
| 67 | package_info.category = package.category |
| 68 | package_info.package_name = package.package |
| 69 | if package.version: |
| 70 | package_info.version = package.version |
| 71 | |
| 72 | return 1 |