blob: 04aa31e814a3361ea868f063a94033fc24081026 [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 Kleina9d500b2019-04-22 15:37:51 -060010from chromite.api.controller import controller_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070011from chromite.lib import build_target_util
12from chromite.lib import cros_build_lib
13from chromite.lib import sysroot_lib
14from chromite.service import sysroot
15
16_ACCEPTED_LICENSES = '@CHROMEOS'
17
Alex Kleind4e1e422019-03-18 16:00:41 -060018# Return codes.
19RC_ERROR = 1
20
21
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
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 Kleina9d500b2019-04-22 15:37:51 -060072 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -070073
Alex Kleind4e1e422019-03-18 16:00:41 -060074 return RC_ERROR
75
76
77def 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 Kleina9d500b2019-04-22 15:37:51 -060083 packages = map(controller_util.PackageInfoToString, input_proto.packages)
Alex Kleind4e1e422019-03-18 16:00:41 -060084
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 Kleinbe0bae42019-05-06 13:01:49 -060096 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -060097 build_packages_config = sysroot.BuildPackagesRunConfig(
98 event_file=event_file, usepkg=not compile_source,
Alex Kleinbe0bae42019-05-06 13:01:49 -060099 install_debug_symbols=True, packages=packages, use_flags=use_flags)
Alex Kleind4e1e422019-03-18 16:00:41 -0600100
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 Kleina9d500b2019-04-22 15:37:51 -0600106 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600107
108 return RC_ERROR