blob: 25606100be7a49c0869a233521beb76de45a90a5 [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 Kleina9d500b2019-04-22 15:37:51 -060011from chromite.api.controller import controller_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070012from chromite.lib import build_target_util
13from chromite.lib import cros_build_lib
Alex Kleina9d64602019-05-17 14:55:37 -060014from chromite.lib import cros_logging as logging
15from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070016from chromite.lib import sysroot_lib
17from chromite.service import sysroot
18
19_ACCEPTED_LICENSES = '@CHROMEOS'
20
Alex Kleind4e1e422019-03-18 16:00:41 -060021
Alex Kleinda35fcf2019-03-07 16:01:15 -070022def 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 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
48def 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
Alex Kleina9d64602019-05-17 14:55:37 -060066 _LogBinhost(build_target.name)
67
Alex Kleinda35fcf2019-03-07 16:01:15 -070068 try:
69 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
70 except sysroot_lib.ToolchainInstallError as e:
71 # Error installing - populate the failed package info.
72 for package in e.failed_toolchain_info:
73 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -060074 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -070075
Alex Klein8cb365a2019-05-15 16:24:53 -060076 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -060077
78
79def InstallPackages(input_proto, output_proto):
80 compile_source = input_proto.flags.compile_source
81 event_file = input_proto.flags.event_file
82
83 sysroot_path = input_proto.sysroot.path
84 build_target_name = input_proto.sysroot.build_target.name
Alex Kleina9d500b2019-04-22 15:37:51 -060085 packages = map(controller_util.PackageInfoToString, input_proto.packages)
Alex Kleind4e1e422019-03-18 16:00:41 -060086
87 if not build_target_name:
88 cros_build_lib.Die('Build target name is required.')
89 if not sysroot_path:
90 cros_build_lib.Die('Sysroot path is required')
91
92 build_target = build_target_util.BuildTarget(build_target_name)
93 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
94
95 if not target_sysroot.IsToolchainInstalled():
96 cros_build_lib.Die('Toolchain must first be installed.')
97
Alex Kleina9d64602019-05-17 14:55:37 -060098 _LogBinhost(build_target.name)
99
Alex Kleinbe0bae42019-05-06 13:01:49 -0600100 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600101 build_packages_config = sysroot.BuildPackagesRunConfig(
102 event_file=event_file, usepkg=not compile_source,
Alex Kleinbe0bae42019-05-06 13:01:49 -0600103 install_debug_symbols=True, packages=packages, use_flags=use_flags)
Alex Kleind4e1e422019-03-18 16:00:41 -0600104
105 try:
106 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
107 except sysroot_lib.PackageInstallError as e:
108 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)