blob: 2a431831ac184d858c9a9c089c425c0e81a27f41 [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)
Alex Klein7b8008e2019-06-14 09:23:35 -060061 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source,
62 use_parallel_binhost=True)
Alex Kleinda35fcf2019-03-07 16:01:15 -070063
64 if not target_sysroot.Exists():
65 cros_build_lib.Die('Sysroot has not been created. Run Create first.')
66
Alex Kleina9d64602019-05-17 14:55:37 -060067 _LogBinhost(build_target.name)
68
Alex Kleinda35fcf2019-03-07 16:01:15 -070069 try:
70 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
71 except sysroot_lib.ToolchainInstallError as e:
72 # Error installing - populate the failed package info.
73 for package in e.failed_toolchain_info:
74 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -060075 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -070076
Alex Klein8cb365a2019-05-15 16:24:53 -060077 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -060078
79
80def InstallPackages(input_proto, output_proto):
81 compile_source = input_proto.flags.compile_source
82 event_file = input_proto.flags.event_file
83
84 sysroot_path = input_proto.sysroot.path
85 build_target_name = input_proto.sysroot.build_target.name
Alex Kleina9d500b2019-04-22 15:37:51 -060086 packages = map(controller_util.PackageInfoToString, input_proto.packages)
Alex Kleind4e1e422019-03-18 16:00:41 -060087
88 if not build_target_name:
89 cros_build_lib.Die('Build target name is required.')
90 if not sysroot_path:
91 cros_build_lib.Die('Sysroot path is required')
92
93 build_target = build_target_util.BuildTarget(build_target_name)
94 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
95
96 if not target_sysroot.IsToolchainInstalled():
97 cros_build_lib.Die('Toolchain must first be installed.')
98
Alex Kleina9d64602019-05-17 14:55:37 -060099 _LogBinhost(build_target.name)
100
Alex Kleinbe0bae42019-05-06 13:01:49 -0600101 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600102 build_packages_config = sysroot.BuildPackagesRunConfig(
103 event_file=event_file, usepkg=not compile_source,
Alex Kleinbe0bae42019-05-06 13:01:49 -0600104 install_debug_symbols=True, packages=packages, use_flags=use_flags)
Alex Kleind4e1e422019-03-18 16:00:41 -0600105
106 try:
107 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
108 except sysroot_lib.PackageInstallError as e:
109 for package in e.failed_packages:
110 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600111 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600112
Alex Klein8cb365a2019-05-15 16:24:53 -0600113 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600114
115
116def _LogBinhost(board):
117 """Log the portage binhost for the given board."""
118 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
119 allow_undefined=True)
120 if not binhost:
121 logging.warning('Portage Binhost not found.')
122 else:
123 logging.info('Portage Binhost: %s', binhost)