blob: 25f712ce2d9e9602a6d819baae3fa2729b4226bf [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
Michael Mortensen4ccfb082020-01-22 16:24:03 -070010import os
Mike Frysingeref94e4c2020-02-10 23:59:54 -050011import sys
Michael Mortensen4ccfb082020-01-22 16:24:03 -070012
Alex Klein8cb365a2019-05-15 16:24:53 -060013from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060014from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060015from chromite.api import validate
Alex Kleina9d500b2019-04-22 15:37:51 -060016from chromite.api.controller import controller_util
Will Bradley7e5b8c12019-07-30 12:44:15 -060017from chromite.api.metrics import deserialize_metrics_log
Alex Kleinda35fcf2019-03-07 16:01:15 -070018from chromite.lib import build_target_util
19from chromite.lib import cros_build_lib
Alex Kleina9d64602019-05-17 14:55:37 -060020from chromite.lib import cros_logging as logging
Michael Mortensen798ee192020-01-17 13:04:43 -070021from chromite.lib import goma_lib
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070022from chromite.lib import osutils
Alex Kleina9d64602019-05-17 14:55:37 -060023from chromite.lib import portage_util
Alex Kleinda35fcf2019-03-07 16:01:15 -070024from chromite.lib import sysroot_lib
25from chromite.service import sysroot
Will Bradley7e5b8c12019-07-30 12:44:15 -060026from chromite.utils import metrics
Alex Kleinda35fcf2019-03-07 16:01:15 -070027
Mike Frysingeref94e4c2020-02-10 23:59:54 -050028
29assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
30
31
Alex Kleinda35fcf2019-03-07 16:01:15 -070032_ACCEPTED_LICENSES = '@CHROMEOS'
33
Alex Kleind4e1e422019-03-18 16:00:41 -060034
Alex Klein076841b2019-08-29 15:19:39 -060035@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060036@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060037@validate.validation_complete
38def Create(input_proto, output_proto, _config):
Alex Kleinda35fcf2019-03-07 16:01:15 -070039 """Create or replace a sysroot."""
40 update_chroot = not input_proto.flags.chroot_current
41 replace_sysroot = input_proto.flags.replace
42
43 build_target_name = input_proto.build_target.name
44 profile = input_proto.profile.name or None
45
Alex Kleinda35fcf2019-03-07 16:01:15 -070046 build_target = build_target_util.BuildTarget(name=build_target_name,
47 profile=profile)
Alex Kleind4e1e422019-03-18 16:00:41 -060048 run_configs = sysroot.SetupBoardRunConfig(
49 force=replace_sysroot, upgrade_chroot=update_chroot)
Alex Kleinda35fcf2019-03-07 16:01:15 -070050
51 try:
52 created = sysroot.Create(build_target, run_configs,
53 accept_licenses=_ACCEPTED_LICENSES)
54 except sysroot.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040055 cros_build_lib.Die(e)
Alex Kleinda35fcf2019-03-07 16:01:15 -070056
57 output_proto.sysroot.path = created.path
58 output_proto.sysroot.build_target.name = build_target_name
59
Alex Klein076841b2019-08-29 15:19:39 -060060 return controller.RETURN_CODE_SUCCESS
Alex Kleinda35fcf2019-03-07 16:01:15 -070061
Alex Klein076841b2019-08-29 15:19:39 -060062
Michael Mortensen98592f62019-09-27 13:34:18 -060063@faux.all_empty
64@validate.require('build_target.name')
65@validate.validation_complete
66def CreateSimpleChromeSysroot(input_proto, output_proto, _config):
67 """Create a sysroot for SimpleChrome to use."""
68 build_target_name = input_proto.build_target.name
69 use_flags = input_proto.use_flags
70
71 sysroot_tar_path = sysroot.CreateSimpleChromeSysroot(build_target_name,
72 use_flags)
73 # By assigning this Path variable to the tar path, the tar file will be
74 # copied out to the input_proto's ResultPath location.
75 output_proto.sysroot_archive.path = sysroot_tar_path
76
77 return controller.RETURN_CODE_SUCCESS
78
79
Michael Mortensen3f6b4bd2020-02-07 14:16:43 -070080@faux.all_empty
81@validate.require('build_target.name', 'packages')
82@validate.validation_complete
83def GenerateArchive(input_proto, output_proto, _config):
84 """Generate a sysroot. Typically used by informational builders."""
85 build_target_name = input_proto.build_target.name
86 pkg_list = []
87 for package in input_proto.packages:
88 pkg_list.append('%s/%s' % (package.category, package.package_name))
89
90 with osutils.TempDir(delete=False) as temp_output_dir:
91 sysroot_tar_path = sysroot.GenerateArchive(temp_output_dir,
92 build_target_name,
93 pkg_list)
94
95 # By assigning this Path variable to the tar path, the tar file will be
96 # copied out to the input_proto's ResultPath location.
97 output_proto.sysroot_archive.path = sysroot_tar_path
98
99
Alex Klein076841b2019-08-29 15:19:39 -0600100def _MockFailedPackagesResponse(_input_proto, output_proto, _config):
101 """Mock error response that populates failed packages."""
102 pkg = output_proto.failed_packages.add()
103 pkg.package_name = 'package'
104 pkg.category = 'category'
105 pkg.version = '1.0.0_rc-r1'
106
107 pkg2 = output_proto.failed_packages.add()
108 pkg2.package_name = 'bar'
109 pkg2.category = 'foo'
110 pkg2.version = '3.7-r99'
111
112
113@faux.empty_success
114@faux.error(_MockFailedPackagesResponse)
Alex Klein2b236722019-06-19 15:44:26 -0600115@validate.require('sysroot.path', 'sysroot.build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600116@validate.exists('sysroot.path')
117@validate.validation_complete
118def InstallToolchain(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600119 """Install the toolchain into a sysroot."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700120 compile_source = (
121 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700122
123 sysroot_path = input_proto.sysroot.path
124 build_target_name = input_proto.sysroot.build_target.name
125
Alex Kleinda35fcf2019-03-07 16:01:15 -0700126 build_target = build_target_util.BuildTarget(name=build_target_name)
127 target_sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein6682ae12019-06-25 13:50:32 -0600128 run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700129
Alex Kleina9d64602019-05-17 14:55:37 -0600130 _LogBinhost(build_target.name)
131
Alex Kleinda35fcf2019-03-07 16:01:15 -0700132 try:
133 sysroot.InstallToolchain(build_target, target_sysroot, run_configs)
134 except sysroot_lib.ToolchainInstallError as e:
135 # Error installing - populate the failed package info.
136 for package in e.failed_toolchain_info:
137 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600138 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700139
Alex Klein8cb365a2019-05-15 16:24:53 -0600140 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleind4e1e422019-03-18 16:00:41 -0600141
Alex Klein076841b2019-08-29 15:19:39 -0600142 return controller.RETURN_CODE_SUCCESS
Alex Kleind4e1e422019-03-18 16:00:41 -0600143
Alex Klein076841b2019-08-29 15:19:39 -0600144
145@faux.empty_success
146@faux.error(_MockFailedPackagesResponse)
Alex Klein566d80e2019-09-24 12:27:58 -0600147@validate.require('sysroot.build_target.name')
148@validate.exists('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600149@validate.validation_complete
Will Bradley7e5b8c12019-07-30 12:44:15 -0600150@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600151def InstallPackages(input_proto, output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600152 """Install packages into a sysroot, building as necessary and permitted."""
Chris McDonald68faa2a2020-01-13 12:23:05 -0700153 compile_source = (
154 input_proto.flags.compile_source or input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600155 event_file = input_proto.flags.event_file
Chris McDonald68faa2a2020-01-13 12:23:05 -0700156 # A new toolchain version will not yet have goma support, so goma must be
157 # disabled when we are testing toolchain changes.
158 use_goma = (
159 input_proto.flags.use_goma and not input_proto.flags.toolchain_changed)
Alex Kleind4e1e422019-03-18 16:00:41 -0600160
Alex Klein566d80e2019-09-24 12:27:58 -0600161 target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
162 build_target = controller_util.ParseBuildTarget(
163 input_proto.sysroot.build_target)
Mike Frysinger66ce4132019-07-17 22:52:52 -0400164 packages = [controller_util.PackageInfoToString(x)
165 for x in input_proto.packages]
Alex Kleind4e1e422019-03-18 16:00:41 -0600166
Alex Kleind4e1e422019-03-18 16:00:41 -0600167 if not target_sysroot.IsToolchainInstalled():
168 cros_build_lib.Die('Toolchain must first be installed.')
169
Alex Kleina9d64602019-05-17 14:55:37 -0600170 _LogBinhost(build_target.name)
171
Alex Kleinbe0bae42019-05-06 13:01:49 -0600172 use_flags = [u.flag for u in input_proto.use_flags]
Alex Kleind4e1e422019-03-18 16:00:41 -0600173 build_packages_config = sysroot.BuildPackagesRunConfig(
Alex Klein566d80e2019-09-24 12:27:58 -0600174 event_file=event_file,
175 usepkg=not compile_source,
176 install_debug_symbols=True,
177 packages=packages,
178 use_flags=use_flags,
Alex Klein5b940482019-12-26 10:38:39 -0700179 use_goma=use_goma,
180 incremental_build=False)
Alex Kleind4e1e422019-03-18 16:00:41 -0600181
182 try:
183 sysroot.BuildPackages(build_target, target_sysroot, build_packages_config)
184 except sysroot_lib.PackageInstallError as e:
Alex Klein2557b4f2019-07-11 14:34:00 -0600185 if not e.failed_packages:
186 # No packages to report, so just exit with an error code.
187 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
188
189 # We need to report the failed packages.
Alex Kleind4e1e422019-03-18 16:00:41 -0600190 for package in e.failed_packages:
191 package_info = output_proto.failed_packages.add()
Alex Kleina9d500b2019-04-22 15:37:51 -0600192 controller_util.CPVToPackageInfo(package, package_info)
Alex Kleind4e1e422019-03-18 16:00:41 -0600193
Alex Klein8cb365a2019-05-15 16:24:53 -0600194 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Kleina9d64602019-05-17 14:55:37 -0600195
Michael Mortensen798ee192020-01-17 13:04:43 -0700196 # Copy goma logs to specified directory if there is a goma_config and
197 # it contains a log_dir to store artifacts.
198 if input_proto.goma_config.log_dir.dir:
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700199 # Get the goma log directory based on the GLOG_log_dir env variable.
200 # TODO(crbug.com/1045001): Replace environment variable with query to
201 # goma object after goma refactoring allows this.
202 log_source_dir = os.getenv('GLOG_log_dir')
203 if not log_source_dir:
204 cros_build_lib.Die('GLOG_log_dir must be defined.')
Michael Mortensen798ee192020-01-17 13:04:43 -0700205 archiver = goma_lib.LogsArchiver(
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700206 log_source_dir,
Michael Mortensen798ee192020-01-17 13:04:43 -0700207 dest_dir=input_proto.goma_config.log_dir.dir,
208 stats_file=input_proto.goma_config.stats_file,
209 counterz_file=input_proto.goma_config.counterz_file)
210 archiver_tuple = archiver.Archive()
211 if archiver_tuple.stats_file:
212 output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file
213 if archiver_tuple.counterz_file:
Michael Mortensen1c7439c2020-01-24 14:43:19 -0700214 output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file
Michael Mortensen798ee192020-01-17 13:04:43 -0700215 output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files
216
Will Bradley7e5b8c12019-07-30 12:44:15 -0600217 # Read metric events log and pipe them into output_proto.events.
Alex Klein566d80e2019-09-24 12:27:58 -0600218 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Will Bradley7e5b8c12019-07-30 12:44:15 -0600219
Alex Kleina9d64602019-05-17 14:55:37 -0600220
221def _LogBinhost(board):
222 """Log the portage binhost for the given board."""
223 binhost = portage_util.PortageqEnvvar('PORTAGE_BINHOST', board=board,
224 allow_undefined=True)
225 if not binhost:
226 logging.warning('Portage Binhost not found.')
227 else:
228 logging.info('Portage Binhost: %s', binhost)