blob: 518bac4a84e1e62a80affbd876441dc8090b8596 [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
14from chromite.lib import sysroot_lib
15from chromite.service import sysroot
16
17_ACCEPTED_LICENSES = '@CHROMEOS'
18
Alex Kleind4e1e422019-03-18 16:00:41 -060019
Alex Kleinda35fcf2019-03-07 16:01:15 -070020def Create(input_proto, output_proto):
21 """Create or replace a sysroot."""
22 update_chroot = not input_proto.flags.chroot_current
23 replace_sysroot = input_proto.flags.replace
24
25 build_target_name = input_proto.build_target.name
26 profile = input_proto.profile.name or None
27
28 if not build_target_name:
29 cros_build_lib.Die('The build target must be provided.')
30
31 build_target = build_target_util.BuildTarget(name=build_target_name,
32 profile=profile)
Alex Kleind4e1e422019-03-18 16:00:41 -060033 run_configs = sysroot.SetupBoardRunConfig(
34 force=replace_sysroot, upgrade_chroot=update_chroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -070035
36 try:
37 created = sysroot.Create(build_target, run_configs,
38 accept_licenses=_ACCEPTED_LICENSES)
39 except sysroot.Error as e:
40 cros_build_lib.Die(e.message)
41
42 output_proto.sysroot.path = created.path
43 output_proto.sysroot.build_target.name = build_target_name
44
45
46def InstallToolchain(input_proto, output_proto):
47 compile_source = input_proto.flags.compile_source
48
49 sysroot_path = input_proto.sysroot.path
50 build_target_name = input_proto.sysroot.build_target.name
51
52 if not sysroot_path:
53 cros_build_lib.Die('sysroot.path is required.')
54 if not build_target_name:
55 cros_build_lib.Die('sysroot.build_target.name is required.')
56
57 build_target = build_target_util.BuildTarget(name=build_target_name)
58 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
59 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
60
61 if not target_sysroot.Exists():
62 cros_build_lib.Die('Sysroot has not been created. Run Create first.')
63
64 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
75def InstallPackages(input_proto, output_proto):
76 compile_source = input_proto.flags.compile_source
77 event_file = input_proto.flags.event_file
78
79 sysroot_path = input_proto.sysroot.path
80 build_target_name = input_proto.sysroot.build_target.name
Alex Kleina9d500b2019-04-22 15:37:51 -060081 packages = map(controller_util.PackageInfoToString, input_proto.packages)
Alex Kleind4e1e422019-03-18 16:00:41 -060082
83 if not build_target_name:
84 cros_build_lib.Die('Build target name is required.')
85 if not sysroot_path:
86 cros_build_lib.Die('Sysroot path is required')
87
88 build_target = build_target_util.BuildTarget(build_target_name)
89 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
90
91 if not target_sysroot.IsToolchainInstalled():
92 cros_build_lib.Die('Toolchain must first be installed.')
93
Alex Kleinbe0bae42019-05-06 13:01:49 -060094 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -060095 build_packages_config = sysroot.BuildPackagesRunConfig(
96 event_file=event_file, usepkg=not compile_source,
Alex Kleinbe0bae42019-05-06 13:01:49 -060097 install_debug_symbols=True, packages=packages, use_flags=use_flags)
Alex Kleind4e1e422019-03-18 16:00:41 -060098
99 try:
100 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
101 except sysroot_lib.PackageInstallError as e:
102 for package in e.failed_packages:
103 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600104 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600105
Alex Klein8cb365a2019-05-15 16:24:53 -0600106 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE