blob: dd37b3f2be6c2eba3d2463268d6127302e90233b [file] [log] [blame]
Alex Kleinda35fcf2019-03-07 16:01:15 -07001# -*- 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
8from __future__ import print_function
9
Alex Klein8cb365a2019-05-15 16:24:53 -060010from chromite.api import controller
Alex Klein2b236722019-06-19 15:44:26 -060011from chromite.api import validate
Alex Kleina9d500b2019-04-22 15:37:51 -060012from chromite.api.controller import controller_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070013from chromite.lib import build_target_util
14from chromite.lib import cros_build_lib
Alex Kleina9d64602019-05-17 14:55:37 -060015from chromite.lib import cros_logging as logging
16from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070017from chromite.lib import sysroot_lib
18from chromite.service import sysroot
19
20_ACCEPTED_LICENSES = '@CHROMEOS'
21
Alex Kleind4e1e422019-03-18 16:00:41 -060022
Alex Klein2b236722019-06-19 15:44:26 -060023@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060024@validate.validation_complete
25def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070026 """Create or replace a sysroot."""
27 update_chroot = not input_proto.flags.chroot_current
28 replace_sysroot = input_proto.flags.replace
29
30 build_target_name = input_proto.build_target.name
31 profile = input_proto.profile.name or None
32
Alex Kleinda35fcf2019-03-07 16:01:15 -070033 build_target = build_target_util.BuildTarget(name=build_target_name,
34 profile=profile)
Alex Kleind4e1e422019-03-18 16:00:41 -060035 run_configs = sysroot.SetupBoardRunConfig(
36 force=replace_sysroot, upgrade_chroot=update_chroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -070037
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
Alex Klein2b236722019-06-19 15:44:26 -060048@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060049@validate.exists('sysroot.path')
50@validate.validation_complete
51def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060052 """Install the toolchain into a sysroot."""
Alex Kleinda35fcf2019-03-07 16:01:15 -070053 compile_source = input_proto.flags.compile_source
54
55 sysroot_path = input_proto.sysroot.path
56 build_target_name = input_proto.sysroot.build_target.name
57
Alex Kleinda35fcf2019-03-07 16:01:15 -070058 build_target = build_target_util.BuildTarget(name=build_target_name)
59 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -060060 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -070061
Alex Kleina9d64602019-05-17 14:55:37 -060062 _LogBinhost(build_target.name)
63
Alex Kleinda35fcf2019-03-07 16:01:15 -070064 try:
65 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
66 except sysroot_lib.ToolchainInstallError as e:
67 # Error installing - populate the failed package info.
68 for package in e.failed_toolchain_info:
69 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -060070 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -070071
Alex Klein8cb365a2019-05-15 16:24:53 -060072 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -060073
74
Alex Klein2b236722019-06-19 15:44:26 -060075@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060076@validate.validation_complete
77def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060078 """Install packages into a sysroot, building as necessary and permitted."""
Alex Kleind4e1e422019-03-18 16:00:41 -060079 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
Mike Frysinger66ce4132019-07-17 22:52:52 -040084 packages = [controller_util.PackageInfoToString(x)
85 for x in input_proto.packages]
Alex Kleind4e1e422019-03-18 16:00:41 -060086
Alex Kleind4e1e422019-03-18 16:00:41 -060087 build_target = build_target_util.BuildTarget(build_target_name)
88 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
89
90 if not target_sysroot.IsToolchainInstalled():
91 cros_build_lib.Die('Toolchain must first be installed.')
92
Alex Kleina9d64602019-05-17 14:55:37 -060093 _LogBinhost(build_target.name)
94
Alex Kleinbe0bae42019-05-06 13:01:49 -060095 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -060096 build_packages_config = sysroot.BuildPackagesRunConfig(
97 event_file=event_file, usepkg=not compile_source,
Alex Kleinbe0bae42019-05-06 13:01:49 -060098 install_debug_symbols=True, packages=packages, use_flags=use_flags)
Alex Kleind4e1e422019-03-18 16:00:41 -060099
100 try:
101 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
102 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600103 if not e.failed_packages:
104 # No packages to report, so just exit with an error code.
105 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
106
107 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600108 for package in e.failed_packages:
109 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600110 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600111
Alex Klein8cb365a2019-05-15 16:24:53 -0600112 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600113
114
115def _LogBinhost(board):
116 """Log the portage binhost for the given board."""
117 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
118 allow_undefined=True)
119 if not binhost:
120 logging.warning('Portage Binhost not found.')
121 else:
122 logging.info('Portage Binhost: %s', binhost)