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 | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 10 | from chromite.api.controller import controller_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 11 | from chromite.lib import build_target_util |
| 12 | from chromite.lib import cros_build_lib |
| 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 | 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 | 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 |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 83 | packages = map(controller_util.PackageInfoToString, input_proto.packages) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 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 | |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 96 | use_flags = [u.flag for u in input_proto.use_flags] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 97 | build_packages_config = sysroot.BuildPackagesRunConfig( |
| 98 | event_file=event_file, usepkg=not compile_source, |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 99 | install_debug_symbols=True, packages=packages, use_flags=use_flags) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 100 | |
| 101 | try: |
| 102 | sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) |
| 103 | except sysroot_lib.PackageInstallError as e: |
| 104 | for package in e.failed_packages: |
| 105 | package_info = output_proto.failed_packages.add() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 106 | controller_util.CPVToPackageInfo(package, package_info) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 107 | |
| 108 | return RC_ERROR |