Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Sysroot controller.""" |
| 6 | |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 7 | import logging |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 8 | import os |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 9 | import traceback |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 10 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 11 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 12 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 13 | from chromite.api import validate |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 14 | from chromite.api.controller import controller_util |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 15 | from chromite.api.gen.chromiumos import common_pb2 |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 16 | from chromite.api.metrics import deserialize_metrics_log |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 17 | from chromite.lib import build_target_lib |
| 18 | from chromite.lib import chroot_lib |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 20 | from chromite.lib import goma_lib |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 21 | from chromite.lib import metrics_lib |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 22 | from chromite.lib import osutils |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 23 | from chromite.lib import portage_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 24 | from chromite.lib import sysroot_lib |
| 25 | from chromite.service import sysroot |
| 26 | |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | _ACCEPTED_LICENSES = "@CHROMEOS" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 29 | |
Alex Klein | eaa4853 | 2022-07-28 08:51:32 -0600 | [diff] [blame] | 30 | DEFAULT_BACKTRACK = 30 |
| 31 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 32 | |
Yoshiki Iguchi | a43704b | 2021-12-16 13:31:48 +0900 | [diff] [blame] | 33 | def _GetGomaLogDirectory(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | """Get goma's log directory based on the env variables. |
Yoshiki Iguchi | a43704b | 2021-12-16 13:31:48 +0900 | [diff] [blame] | 35 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 37 | a string of a directory name where goma's log may exist, or None if no |
| 38 | potential directories exist. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | """ |
| 40 | # TODO(crbug.com/1045001): Replace environment variable with query to |
| 41 | # goma object after goma refactoring allows this. |
| 42 | candidates = [ |
| 43 | "GLOG_log_dir", |
| 44 | "GOOGLE_LOG_DIR", |
| 45 | "TEST_TMPDIR", |
| 46 | "TMPDIR", |
| 47 | "TMP", |
| 48 | ] |
| 49 | for candidate in candidates: |
| 50 | value = os.environ.get(candidate) |
| 51 | if value and os.path.isdir(value): |
| 52 | return value |
Yoshiki Iguchi | a43704b | 2021-12-16 13:31:48 +0900 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | # "/tmp" will always exist. |
| 55 | return "/tmp" |
Yoshiki Iguchi | a43704b | 2021-12-16 13:31:48 +0900 | [diff] [blame] | 56 | |
| 57 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 58 | def ExampleGetResponse(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | """Give an example response to assemble upstream in caller artifacts.""" |
| 60 | uabs = common_pb2.UploadedArtifactsByService |
| 61 | cabs = common_pb2.ArtifactsByService |
| 62 | return uabs.Sysroot( |
| 63 | artifacts=[ |
| 64 | uabs.Sysroot.ArtifactPaths( |
| 65 | artifact_type=cabs.Sysroot.ArtifactType.SIMPLE_CHROME_SYSROOT, |
| 66 | paths=[ |
| 67 | common_pb2.Path( |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 68 | path=( |
| 69 | "/tmp/sysroot_chromeos-base_chromeos-chrome.tar.xz" |
| 70 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | location=common_pb2.Path.OUTSIDE, |
| 72 | ) |
| 73 | ], |
| 74 | ), |
| 75 | uabs.Sysroot.ArtifactPaths( |
| 76 | artifact_type=cabs.Sysroot.ArtifactType.DEBUG_SYMBOLS, |
| 77 | paths=[ |
| 78 | common_pb2.Path( |
| 79 | path="/tmp/debug.tgz", location=common_pb2.Path.OUTSIDE |
| 80 | ) |
| 81 | ], |
| 82 | ), |
| 83 | uabs.Sysroot.ArtifactPaths( |
| 84 | artifact_type=cabs.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS, |
| 85 | paths=[ |
| 86 | common_pb2.Path( |
| 87 | path="/tmp/debug_breakpad.tar.xz", |
| 88 | location=common_pb2.Path.OUTSIDE, |
| 89 | ) |
| 90 | ], |
| 91 | ), |
| 92 | ] |
| 93 | ) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 94 | |
| 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | def GetArtifacts( |
| 97 | in_proto: common_pb2.ArtifactsByService.Sysroot, |
| 98 | chroot: chroot_lib.Chroot, |
| 99 | sysroot_class: sysroot_lib.Sysroot, |
| 100 | build_target: build_target_lib.BuildTarget, |
| 101 | output_dir: str, |
| 102 | ) -> list: |
| 103 | """Builds and copies sysroot artifacts to specified output_dir. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 104 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 105 | Copies sysroot artifacts to output_dir, returning a list of |
| 106 | (output_dir: str) paths to the desired files. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 109 | in_proto: Proto request defining reqs. |
| 110 | chroot: The chroot class used for these artifacts. |
| 111 | sysroot_class: The sysroot class used for these artifacts. |
| 112 | build_target: The build target used for these artifacts. |
| 113 | output_dir: The path to write artifacts to. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 116 | A list of dictionary mappings of ArtifactType to list of paths. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | """ |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 118 | |
| 119 | def _BundleBreakpadSymbols(chroot, sysroot_class, build_target, output_dir): |
| 120 | ignore_breakpad_symbol_generation_expected_files = [ |
| 121 | common_pb2.ArtifactsByService.Sysroot.BreakpadSymbolGenerationExpectedFile.Name( |
| 122 | x |
| 123 | ) |
| 124 | for x in in_proto.ignore_breakpad_symbol_generation_expected_files |
| 125 | if x |
| 126 | != common_pb2.ArtifactsByService.Sysroot.BreakpadSymbolGenerationExpectedFile.EXPECTED_FILE_UNSET |
| 127 | and x |
| 128 | in common_pb2.ArtifactsByService.Sysroot.BreakpadSymbolGenerationExpectedFile.values() |
| 129 | ] |
| 130 | |
| 131 | ignore_breakpad_symbol_generation_expected_files = [ |
| 132 | x[len("EXPECTED_FILE_") :] |
| 133 | for x in ignore_breakpad_symbol_generation_expected_files |
| 134 | ] |
| 135 | |
| 136 | return sysroot.BundleBreakpadSymbols( |
| 137 | chroot, |
| 138 | sysroot_class, |
| 139 | build_target, |
| 140 | output_dir, |
| 141 | in_proto.ignore_breakpad_symbol_generation_errors, |
| 142 | ignore_breakpad_symbol_generation_expected_files, |
| 143 | ) |
| 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | generated = [] |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 146 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | artifact_types = { |
| 148 | in_proto.ArtifactType.SIMPLE_CHROME_SYSROOT: sysroot.CreateSimpleChromeSysroot, |
| 149 | in_proto.ArtifactType.CHROME_EBUILD_ENV: sysroot.CreateChromeEbuildEnv, |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 150 | in_proto.ArtifactType.BREAKPAD_DEBUG_SYMBOLS: _BundleBreakpadSymbols, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | in_proto.ArtifactType.DEBUG_SYMBOLS: sysroot.BundleDebugSymbols, |
Jack Neus | 11b6ebd | 2022-10-21 17:54:36 +0000 | [diff] [blame] | 152 | in_proto.ArtifactType.FUZZER_SYSROOT: sysroot.CreateFuzzerSysroot, |
Ram Chandrasekar | 5ba36b2 | 2023-03-20 16:10:48 -0600 | [diff] [blame] | 153 | in_proto.ArtifactType.SYSROOT_ARCHIVE: sysroot.ArchiveSysroot, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | } |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 155 | # pylint: enable=line-too-long |
Jack Neus | 5e56fef | 2021-06-18 16:57:28 +0000 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | for output_artifact in in_proto.output_artifacts: |
| 158 | for artifact_type, func in artifact_types.items(): |
| 159 | if artifact_type in output_artifact.artifact_types: |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 160 | try: |
| 161 | result = func( |
| 162 | chroot, sysroot_class, build_target, output_dir |
| 163 | ) |
| 164 | except Exception as e: |
| 165 | generated.append( |
| 166 | { |
| 167 | "type": artifact_type, |
| 168 | "failed": True, |
| 169 | "failure_reason": str(e), |
| 170 | } |
| 171 | ) |
| 172 | artifact_name = ( |
| 173 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.Name( |
| 174 | artifact_type |
| 175 | ) |
| 176 | ) |
| 177 | logging.warning( |
| 178 | "%s artifact generation failed with exception %s", |
| 179 | artifact_name, |
| 180 | e, |
| 181 | ) |
| 182 | logging.warning("traceback:\n%s", traceback.format_exc()) |
| 183 | continue |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | if result: |
| 185 | generated.append( |
| 186 | { |
Ram Chandrasekar | 5ba36b2 | 2023-03-20 16:10:48 -0600 | [diff] [blame] | 187 | "paths": [str(result)] |
| 188 | if isinstance(result, (os.PathLike, str)) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 189 | else result, |
| 190 | "type": artifact_type, |
| 191 | } |
| 192 | ) |
Jack Neus | 5e56fef | 2021-06-18 16:57:28 +0000 | [diff] [blame] | 193 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 194 | return generated |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 195 | |
| 196 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 197 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | @validate.require("build_target.name") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 199 | @validate.validation_complete |
| 200 | def Create(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 201 | """Create or replace a sysroot.""" |
| 202 | update_chroot = not input_proto.flags.chroot_current |
| 203 | replace_sysroot = input_proto.flags.replace |
Yoshiki Iguchi | 2f7f022 | 2023-05-17 19:30:09 +0900 | [diff] [blame] | 204 | use_cq_prebuilts = input_proto.flags.use_cq_prebuilts |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 205 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 206 | build_target = controller_util.ParseBuildTarget( |
| 207 | input_proto.build_target, input_proto.profile |
| 208 | ) |
| 209 | package_indexes = [ |
Alex Klein | 6d718d6 | 2023-01-18 15:55:51 -0700 | [diff] [blame] | 210 | controller_util.deserialize_package_index_info(x) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | for x in input_proto.package_indexes |
| 212 | ] |
| 213 | run_configs = sysroot.SetupBoardRunConfig( |
| 214 | force=replace_sysroot, |
| 215 | upgrade_chroot=update_chroot, |
| 216 | package_indexes=package_indexes, |
Yoshiki Iguchi | 2f7f022 | 2023-05-17 19:30:09 +0900 | [diff] [blame] | 217 | use_cq_prebuilts=use_cq_prebuilts, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | backtrack=DEFAULT_BACKTRACK, |
| 219 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 220 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | try: |
| 222 | created = sysroot.Create( |
| 223 | build_target, run_configs, accept_licenses=_ACCEPTED_LICENSES |
| 224 | ) |
| 225 | except sysroot.Error as e: |
| 226 | cros_build_lib.Die(e) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | output_proto.sysroot.path = created.path |
| 229 | output_proto.sysroot.build_target.name = build_target.name |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 232 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 233 | |
Michael Mortensen | 98592f6 | 2019-09-27 13:34:18 -0600 | [diff] [blame] | 234 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | @validate.require("build_target.name", "packages") |
| 236 | @validate.require_each("packages", ["category", "package_name"]) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 237 | @validate.validation_complete |
| 238 | def GenerateArchive(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 239 | """Generate a sysroot. Typically used by informational builders.""" |
| 240 | build_target_name = input_proto.build_target.name |
| 241 | pkg_list = [] |
| 242 | for package in input_proto.packages: |
| 243 | pkg_list.append("%s/%s" % (package.category, package.package_name)) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 245 | with osutils.TempDir(delete=False) as temp_output_dir: |
| 246 | sysroot_tar_path = sysroot.GenerateArchive( |
| 247 | temp_output_dir, build_target_name, pkg_list |
| 248 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | # By assigning this Path variable to the tar path, the tar file will be |
| 251 | # copied out to the input_proto's ResultPath location. |
| 252 | output_proto.sysroot_archive.path = sysroot_tar_path |
| 253 | output_proto.sysroot_archive.location = common_pb2.Path.INSIDE |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 254 | |
| 255 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 256 | def _MockFailedPackagesResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | """Mock error response that populates failed packages.""" |
| 258 | fail = output_proto.failed_package_data.add() |
| 259 | fail.name.package_name = "package" |
| 260 | fail.name.category = "category" |
| 261 | fail.name.version = "1.0.0_rc-r1" |
| 262 | fail.log_path.path = ( |
| 263 | "/path/to/package:category-1.0.0_rc-r1:20210609-1337.log" |
| 264 | ) |
| 265 | fail.log_path.location = common_pb2.Path.INSIDE |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 266 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | fail2 = output_proto.failed_package_data.add() |
| 268 | fail2.name.package_name = "bar" |
| 269 | fail2.name.category = "foo" |
| 270 | fail2.name.version = "3.7-r99" |
| 271 | fail2.log_path.path = "/path/to/foo:bar-3.7-r99:20210609-1620.log" |
| 272 | fail2.log_path.location = common_pb2.Path.INSIDE |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 273 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 274 | |
| 275 | @faux.empty_success |
| 276 | @faux.error(_MockFailedPackagesResponse) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | @validate.require("sysroot.path", "sysroot.build_target.name") |
| 278 | @validate.exists("sysroot.path") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 279 | @validate.validation_complete |
| 280 | def InstallToolchain(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | """Install the toolchain into a sysroot.""" |
| 282 | compile_source = ( |
| 283 | input_proto.flags.compile_source or input_proto.flags.toolchain_changed |
| 284 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 285 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 286 | sysroot_path = input_proto.sysroot.path |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 287 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | build_target = controller_util.ParseBuildTarget( |
| 289 | input_proto.sysroot.build_target |
| 290 | ) |
| 291 | target_sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 292 | run_configs = sysroot.SetupBoardRunConfig(usepkg=not compile_source) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 293 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 294 | _LogBinhost(build_target.name) |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 295 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 296 | try: |
| 297 | sysroot.InstallToolchain(build_target, target_sysroot, run_configs) |
| 298 | except sysroot_lib.ToolchainInstallError as e: |
| 299 | controller_util.retrieve_package_log_paths( |
| 300 | e.failed_toolchain_info, output_proto, target_sysroot |
| 301 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 302 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 303 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 304 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 305 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 306 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 307 | |
| 308 | @faux.empty_success |
| 309 | @faux.error(_MockFailedPackagesResponse) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 310 | @validate.require("sysroot.build_target.name") |
| 311 | @validate.exists("sysroot.path") |
| 312 | @validate.require_each("packages", ["category", "package_name"]) |
| 313 | @validate.require_each("use_flags", ["flag"]) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 314 | @validate.validation_complete |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 315 | @metrics_lib.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 316 | def InstallPackages(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | """Install packages into a sysroot, building as necessary and permitted.""" |
| 318 | compile_source = ( |
| 319 | input_proto.flags.compile_source or input_proto.flags.toolchain_changed |
| 320 | ) |
Joanna Wang | 1ec0c81 | 2021-11-17 17:41:27 -0800 | [diff] [blame] | 321 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 322 | use_remoteexec = bool( |
| 323 | input_proto.remoteexec_config.reproxy_cfg_file |
| 324 | and input_proto.remoteexec_config.reclient_dir |
| 325 | ) |
Joanna Wang | 1ec0c81 | 2021-11-17 17:41:27 -0800 | [diff] [blame] | 326 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 327 | # Testing if Goma will support unknown compilers now. |
| 328 | use_goma = input_proto.flags.use_goma and not use_remoteexec |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 329 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 330 | target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) |
| 331 | build_target = controller_util.ParseBuildTarget( |
| 332 | input_proto.sysroot.build_target |
| 333 | ) |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 334 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 335 | # Get the package atom for each specified package. The field is optional, so |
| 336 | # error only when we cannot parse an atom for each of the given packages. |
| 337 | packages = [ |
Alex Klein | d3b8404 | 2023-05-19 14:43:59 -0600 | [diff] [blame] | 338 | controller_util.deserialize_package_info(x).atom |
| 339 | for x in input_proto.packages |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 340 | ] |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 341 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 342 | package_indexes = [ |
Alex Klein | 6d718d6 | 2023-01-18 15:55:51 -0700 | [diff] [blame] | 343 | controller_util.deserialize_package_index_info(x) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 344 | for x in input_proto.package_indexes |
| 345 | ] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 346 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 347 | # Calculate which packages would have been merged, but don't install |
| 348 | # anything. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 349 | dryrun = input_proto.flags.dryrun |
Navil Perez | 5766d1b | 2021-05-26 17:38:15 +0000 | [diff] [blame] | 350 | |
Alex Klein | 8393dc2 | 2023-03-30 10:54:27 -0600 | [diff] [blame] | 351 | # Allow cros workon packages to build from the unstable ebuilds. |
| 352 | workon = input_proto.flags.workon |
| 353 | |
Ryo Hashimoto | 0845ebf | 2023-06-06 19:21:13 +0900 | [diff] [blame] | 354 | # Use Bazel to build packages. |
| 355 | bazel = input_proto.flags.bazel |
| 356 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | if not target_sysroot.IsToolchainInstalled(): |
| 358 | cros_build_lib.Die("Toolchain must first be installed.") |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 359 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 360 | _LogBinhost(build_target.name) |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 361 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 362 | use_flags = [u.flag for u in input_proto.use_flags] |
| 363 | build_packages_config = sysroot.BuildPackagesRunConfig( |
| 364 | use_any_chrome=False, |
| 365 | usepkg=not compile_source, |
| 366 | install_debug_symbols=True, |
| 367 | packages=packages, |
| 368 | package_indexes=package_indexes, |
| 369 | use_flags=use_flags, |
| 370 | use_goma=use_goma, |
| 371 | use_remoteexec=use_remoteexec, |
| 372 | incremental_build=False, |
| 373 | dryrun=dryrun, |
| 374 | backtrack=DEFAULT_BACKTRACK, |
Alex Klein | 8393dc2 | 2023-03-30 10:54:27 -0600 | [diff] [blame] | 375 | workon=workon, |
Ryo Hashimoto | 0845ebf | 2023-06-06 19:21:13 +0900 | [diff] [blame] | 376 | bazel=bazel, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 377 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 378 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 379 | try: |
| 380 | sysroot.BuildPackages( |
| 381 | build_target, target_sysroot, build_packages_config |
| 382 | ) |
| 383 | except sysroot_lib.PackageInstallError as e: |
| 384 | if not e.failed_packages: |
| 385 | # No packages to report, so just exit with an error code. |
| 386 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 387 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | controller_util.retrieve_package_log_paths( |
| 389 | e.failed_packages, output_proto, target_sysroot |
| 390 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 391 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 392 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
| 393 | finally: |
| 394 | # Copy goma logs to specified directory if there is a goma_config and |
| 395 | # it contains a log_dir to store artifacts. |
| 396 | if input_proto.goma_config.log_dir.dir: |
| 397 | log_source_dir = _GetGomaLogDirectory() |
| 398 | archiver = goma_lib.LogsArchiver( |
| 399 | log_source_dir, |
| 400 | dest_dir=input_proto.goma_config.log_dir.dir, |
| 401 | stats_file=input_proto.goma_config.stats_file, |
| 402 | counterz_file=input_proto.goma_config.counterz_file, |
| 403 | ) |
| 404 | archiver_tuple = archiver.Archive() |
| 405 | if archiver_tuple.stats_file: |
| 406 | output_proto.goma_artifacts.stats_file = ( |
| 407 | archiver_tuple.stats_file |
| 408 | ) |
| 409 | if archiver_tuple.counterz_file: |
| 410 | output_proto.goma_artifacts.counterz_file = ( |
| 411 | archiver_tuple.counterz_file |
| 412 | ) |
| 413 | output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 414 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 415 | # Return without populating the response if it is a dryrun. |
| 416 | if dryrun: |
| 417 | return controller.RETURN_CODE_SUCCESS |
Navil Perez | 5766d1b | 2021-05-26 17:38:15 +0000 | [diff] [blame] | 418 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 419 | # Read metric events log and pipe them into output_proto.events. |
| 420 | deserialize_metrics_log(output_proto.events, prefix=build_target.name) |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 421 | |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 422 | |
| 423 | def _LogBinhost(board): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 424 | """Log the portage binhost for the given board.""" |
| 425 | binhost = portage_util.PortageqEnvvar( |
| 426 | "PORTAGE_BINHOST", board=board, allow_undefined=True |
| 427 | ) |
| 428 | if not binhost: |
| 429 | logging.warning("Portage Binhost not found.") |
| 430 | else: |
| 431 | logging.info("Portage Binhost: %s", binhost) |