blob: e8697fe30921890c55d9c62e8da785d292922985 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleineb77ffa2019-05-28 14:47:44 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Package utility functionality."""
6
Yaakov Shaul730814a2019-09-10 13:58:25 -06007import collections
Ben Reiche779cf42020-12-15 03:21:31 +00008from distutils.version import LooseVersion
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -06009import fileinput
Alex Klein87531182019-08-12 15:23:37 -060010import functools
Yaakov Shaul395ae832019-09-09 14:45:32 -060011import json
Chris McDonaldf7c03d42021-07-21 11:54:26 -060012import logging
Evan Hernandezb51f1522019-08-15 11:29:40 -060013import os
Michael Mortensenb70e8a82019-10-10 18:43:41 -060014import re
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -060015import sys
Alex Klein68a28712021-11-08 11:08:30 -070016from typing import Iterable, List, NamedTuple, Optional, TYPE_CHECKING, Union
Alex Klein87531182019-08-12 15:23:37 -060017
Mike Frysinger2c024062021-05-22 15:43:22 -040018from chromite.third_party.google.protobuf import json_format
Yaakov Shaul730814a2019-09-10 13:58:25 -060019
Andrew Lamb2bde9e42019-11-04 13:24:09 -070020from chromite.api.gen.config import replication_config_pb2
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000021from chromite.lib import chromeos_version
Alex Kleineb77ffa2019-05-28 14:47:44 -060022from chromite.lib import constants
Evan Hernandezb51f1522019-08-15 11:29:40 -060023from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060024from chromite.lib import git
Michael Mortensende716a12020-05-15 11:27:00 -060025from chromite.lib import image_lib
Michael Mortensenb70e8a82019-10-10 18:43:41 -060026from chromite.lib import osutils
Alex Kleineb77ffa2019-05-28 14:47:44 -060027from chromite.lib import portage_util
Andrew Lamb2bde9e42019-11-04 13:24:09 -070028from chromite.lib import replication_lib
Alex Kleind6195b62019-08-06 16:01:16 -060029from chromite.lib import uprev_lib
Alex Klein18a60af2020-06-11 12:08:47 -060030from chromite.lib.parser import package_info
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090031from chromite.service import android
Alex Kleineb77ffa2019-05-28 14:47:44 -060032
Mike Frysinger68796b52019-08-25 00:04:27 -040033
Alex Klein5caab872021-09-10 11:44:37 -060034if TYPE_CHECKING:
Alex Klein1699fab2022-09-08 08:46:06 -060035 from chromite.lib import build_target_lib
36 from chromite.lib import chroot_lib
Chris McDonaldf7c03d42021-07-21 11:54:26 -060037
Alex Klein36b117f2019-09-30 15:13:46 -060038if cros_build_lib.IsInsideChroot():
Alex Klein1699fab2022-09-08 08:46:06 -060039 from chromite.lib import depgraph
40 from chromite.service import dependency
Alex Klein36b117f2019-09-30 15:13:46 -060041
Alex Klein87531182019-08-12 15:23:37 -060042# Registered handlers for uprevving versioned packages.
43_UPREV_FUNCS = {}
44
Alex Kleineb77ffa2019-05-28 14:47:44 -060045
46class Error(Exception):
Alex Klein1699fab2022-09-08 08:46:06 -060047 """Module's base error class."""
Alex Kleineb77ffa2019-05-28 14:47:44 -060048
49
Alex Klein4de25e82019-08-05 15:58:39 -060050class UnknownPackageError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060051 """Uprev attempted for a package without a registered handler."""
Alex Klein4de25e82019-08-05 15:58:39 -060052
53
Alex Kleineb77ffa2019-05-28 14:47:44 -060054class UprevError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060055 """An error occurred while uprevving packages."""
Alex Kleineb77ffa2019-05-28 14:47:44 -060056
57
Michael Mortensenb70e8a82019-10-10 18:43:41 -060058class NoAndroidVersionError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060059 """An error occurred while trying to determine the android version."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060060
61
62class NoAndroidBranchError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060063 """An error occurred while trying to determine the android branch."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060064
65
66class NoAndroidTargetError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060067 """An error occurred while trying to determine the android target."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060068
69
Lizzy Presland0b978e62022-09-09 16:55:29 +000070class KernelVersionError(Error):
71 """An error occurred while trying to determine the kernel version."""
72
73
Alex Klein4de25e82019-08-05 15:58:39 -060074class AndroidIsPinnedUprevError(UprevError):
Alex Klein1699fab2022-09-08 08:46:06 -060075 """Raised when we try to uprev while Android is pinned."""
Alex Klein4de25e82019-08-05 15:58:39 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 def __init__(self, new_android_atom):
78 """Initialize a AndroidIsPinnedUprevError.
Alex Klein4de25e82019-08-05 15:58:39 -060079
Alex Klein1699fab2022-09-08 08:46:06 -060080 Args:
Alex Klein348e7692022-10-13 17:03:37 -060081 new_android_atom: The Android atom that we failed to uprev to, due
82 to Android being pinned.
Alex Klein1699fab2022-09-08 08:46:06 -060083 """
84 assert new_android_atom
85 msg = (
86 "Failed up uprev to Android version %s as Android was pinned."
87 % new_android_atom
88 )
89 super().__init__(msg)
90 self.new_android_atom = new_android_atom
Alex Klein87531182019-08-12 15:23:37 -060091
92
Andrew Lamb9563a152019-12-04 11:42:18 -070093class GeneratedCrosConfigFilesError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060094 """Error when cros_config_schema does not produce expected files"""
Andrew Lamb9563a152019-12-04 11:42:18 -070095
Alex Klein1699fab2022-09-08 08:46:06 -060096 def __init__(self, expected_files, found_files):
97 msg = "Expected to find generated C files: %s. Actually found: %s" % (
98 expected_files,
99 found_files,
100 )
101 super().__init__(msg)
Andrew Lamb9563a152019-12-04 11:42:18 -0700102
Alex Klein7a3a7dd2020-01-08 16:44:38 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104NeedsChromeSourceResult = collections.namedtuple(
105 "NeedsChromeSourceResult",
106 (
107 "needs_chrome_source",
108 "builds_chrome",
109 "packages",
110 "missing_chrome_prebuilt",
111 "missing_follower_prebuilt",
112 "local_uprev",
113 ),
114)
Alex Klein6becabc2020-09-11 14:03:05 -0600115
116
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600117def patch_ebuild_vars(ebuild_path, variables):
Alex Klein1699fab2022-09-08 08:46:06 -0600118 """Updates variables in ebuild.
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 Use this function rather than portage_util.EBuild.UpdateEBuild when you
121 want to preserve the variable position and quotes within the ebuild.
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600124 ebuild_path: The path of the ebuild.
125 variables: Dictionary of variables to update in ebuild.
Alex Klein1699fab2022-09-08 08:46:06 -0600126 """
127 try:
128 for line in fileinput.input(ebuild_path, inplace=1):
129 for var, value in variables.items():
130 line = re.sub(rf"\b{var}=\S+", f'{var}="{value}"', line)
131 sys.stdout.write(line)
132 finally:
133 fileinput.close()
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600134
135
Alex Klein87531182019-08-12 15:23:37 -0600136def uprevs_versioned_package(package):
Alex Klein1699fab2022-09-08 08:46:06 -0600137 """Decorator to register package uprev handlers."""
138 assert package
Alex Klein87531182019-08-12 15:23:37 -0600139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 def register(func):
141 """Registers |func| as a handler for |package|."""
142 _UPREV_FUNCS[package] = func
Alex Klein87531182019-08-12 15:23:37 -0600143
Alex Klein1699fab2022-09-08 08:46:06 -0600144 @functools.wraps(func)
145 def pass_through(*args, **kwargs):
146 return func(*args, **kwargs)
Alex Klein87531182019-08-12 15:23:37 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 return pass_through
Alex Klein87531182019-08-12 15:23:37 -0600149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 return register
Alex Klein87531182019-08-12 15:23:37 -0600151
152
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900153class UprevAndroidResult(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -0600154 """Results of an Android uprev."""
155
156 revved: bool
157 android_atom: str = None
158 modified_files: List[str] = None
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900159
160
161def uprev_android(
162 android_package: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600163 chroot: "chroot_lib.Chroot",
164 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900165 android_build_branch: Optional[str] = None,
166 android_version: Optional[str] = None,
Alex Klein1699fab2022-09-08 08:46:06 -0600167 skip_commit: bool = False,
168) -> UprevAndroidResult:
169 """Performs an Android uprev by calling cros_mark_android_as_stable.
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600172 android_package: The Android package to uprev.
173 chroot: The chroot to enter.
174 build_targets: List of build targets to cleanup after uprev.
175 android_build_branch: Override the default Android branch corresponding
176 to the package.
177 android_version: Uprev to the particular version. By default the latest
178 available version is used.
179 skip_commit: Whether to skip committing the change after a successful
180 uprev.
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600183 The uprev result containing:
184 revved: Whether an uprev happened.
185 android_atom: If revved, the portage atom for the revved Android
186 ebuild.
187 modified_files: If revved, list of files being modified.
Alex Klein1699fab2022-09-08 08:46:06 -0600188 """
189 command = [
190 "cros_mark_android_as_stable",
191 f"--android_package={android_package}",
192 ]
193 if build_targets:
194 command.append(f'--boards={":".join(bt.name for bt in build_targets)}')
195 if android_build_branch:
196 command.append(f"--android_build_branch={android_build_branch}")
197 if android_version:
198 command.append(f"--force_version={android_version}")
199 if skip_commit:
200 command.append("--skip_commit")
Alex Klein4de25e82019-08-05 15:58:39 -0600201
Alex Klein1699fab2022-09-08 08:46:06 -0600202 result = cros_build_lib.run(
203 command,
204 stdout=True,
205 enter_chroot=True,
206 encoding="utf-8",
207 chroot_args=chroot.get_enter_args(),
208 )
Alex Klein4de25e82019-08-05 15:58:39 -0600209
Alex Klein1699fab2022-09-08 08:46:06 -0600210 # cros_mark_android_as_stable prints the uprev result to stdout as JSON in a
211 # single line. We only take the last line from stdout to make sure no junk
212 # output is included (e.g. messages from bashrc scripts that run upon entering
213 # the chroot.)
214 output = json.loads(result.stdout.strip().splitlines()[-1])
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900215
Alex Klein1699fab2022-09-08 08:46:06 -0600216 if not output["revved"]:
217 logging.info("Found nothing to rev.")
218 return UprevAndroidResult(revved=False)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900219
Alex Klein1699fab2022-09-08 08:46:06 -0600220 android_atom = output["android_atom"]
Alex Klein4de25e82019-08-05 15:58:39 -0600221
Alex Klein1699fab2022-09-08 08:46:06 -0600222 for target in build_targets or []:
223 # Sanity check: We should always be able to merge the version of
224 # Android we just unmasked.
225 command = [f"emerge-{target.name}", "-p", "--quiet", f"={android_atom}"]
226 try:
227 cros_build_lib.run(
228 command, enter_chroot=True, chroot_args=chroot.get_enter_args()
229 )
230 except cros_build_lib.RunCommandError:
231 logging.error(
232 "Cannot emerge-%s =%s\nIs Android pinned to an older "
233 "version?",
234 target,
235 android_atom,
236 )
237 raise AndroidIsPinnedUprevError(android_atom)
Alex Klein4de25e82019-08-05 15:58:39 -0600238
Alex Klein1699fab2022-09-08 08:46:06 -0600239 return UprevAndroidResult(
240 revved=True,
241 android_atom=android_atom,
242 modified_files=output["modified_files"],
243 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900244
245
Alex Klein1699fab2022-09-08 08:46:06 -0600246def uprev_android_lkgb(
247 android_package: str,
248 build_targets: List["build_target_lib.BuildTarget"],
249 chroot: "chroot_lib.Chroot",
250) -> uprev_lib.UprevVersionedPackageResult:
251 """Uprevs an Android package to the version specified in the LKGB file.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900252
Alex Klein1699fab2022-09-08 08:46:06 -0600253 This is the PUpr handler for Android packages, triggered whenever the
254 corresponding LKGB file is being updated.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 PUpr for Android does not test the uprev change in CQ; instead we run separate
257 jobs to test new Android versions, and we write the latest vetted version to
258 the LKGB file. Find the design at go/android-uprev-recipes.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900259
Alex Klein1699fab2022-09-08 08:46:06 -0600260 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600261 android_package: The Android package to uprev.
262 build_targets: List of build targets to cleanup after uprev.
263 chroot: The chroot to enter.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900264
Alex Klein1699fab2022-09-08 08:46:06 -0600265 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600266 An uprev_lib.UprevVersionedPackageResult containing the new version and
267 a list of modified files.
Alex Klein1699fab2022-09-08 08:46:06 -0600268 """
269 android_package_dir = android.GetAndroidPackageDir(android_package)
270 android_version = android.ReadLKGB(android_package_dir)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900271
Alex Klein1699fab2022-09-08 08:46:06 -0600272 result = uprev_lib.UprevVersionedPackageResult()
273 uprev_result = uprev_android(
274 android_package,
275 chroot,
276 build_targets=build_targets,
277 android_version=android_version,
278 skip_commit=True,
279 )
280 if not uprev_result.revved:
281 return result
282
283 # cros_mark_android_as_stable returns paths relative to |android.OVERLAY_DIR|.
284 result.add_result(
285 android_version,
286 [
287 os.path.join(android.OVERLAY_DIR, f)
288 for f in uprev_result.modified_files
289 ],
290 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900291 return result
292
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900293
294def define_uprev_android_lkgb_handlers():
Alex Klein1699fab2022-09-08 08:46:06 -0600295 """Dynamically define uprev handlers for each Android package"""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900296
Alex Klein1699fab2022-09-08 08:46:06 -0600297 def define_handler(android_package):
298 """Defines the uprev handler for an Android package."""
299 full_package_name = "chromeos-base/" + android_package
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900300
Alex Klein1699fab2022-09-08 08:46:06 -0600301 @uprevs_versioned_package(full_package_name)
302 def _handler(build_targets, _refs, chroot):
303 return uprev_android_lkgb(android_package, build_targets, chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900304
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900305 for android_package in android.GetAllAndroidPackages():
Alex Klein1699fab2022-09-08 08:46:06 -0600306 define_handler(android_package)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900307
308
309define_uprev_android_lkgb_handlers()
Alex Klein4de25e82019-08-05 15:58:39 -0600310
311
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700312def uprev_build_targets(
Alex Klein1699fab2022-09-08 08:46:06 -0600313 build_targets: Optional[List["build_target_lib.BuildTarget"]],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700314 overlay_type: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600315 chroot: "chroot_lib.Chroot" = None,
316 output_dir: Optional[str] = None,
317):
318 """Uprev the set provided build targets, or all if not specified.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600319
Alex Klein1699fab2022-09-08 08:46:06 -0600320 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600321 build_targets: The build targets whose overlays should be uprevved,
322 empty or None for all.
323 overlay_type: One of the valid overlay types except None (see
324 constants.VALID_OVERLAYS).
325 chroot: The chroot to clean, if desired.
326 output_dir: The path to optionally dump result files.
Alex Klein1699fab2022-09-08 08:46:06 -0600327 """
328 # Need a valid overlay, but exclude None.
329 assert overlay_type and overlay_type in constants.VALID_OVERLAYS
Alex Kleineb77ffa2019-05-28 14:47:44 -0600330
Alex Klein1699fab2022-09-08 08:46:06 -0600331 if build_targets:
332 overlays = portage_util.FindOverlaysForBoards(
333 overlay_type, boards=[t.name for t in build_targets]
334 )
335 else:
336 overlays = portage_util.FindOverlays(overlay_type)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600337
Alex Klein1699fab2022-09-08 08:46:06 -0600338 return uprev_overlays(
339 overlays,
340 build_targets=build_targets,
341 chroot=chroot,
342 output_dir=output_dir,
343 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600344
345
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700346def uprev_overlays(
347 overlays: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -0600348 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
349 chroot: Optional["chroot_lib.Chroot"] = None,
350 output_dir: Optional[str] = None,
351) -> List[str]:
352 """Uprev the given overlays.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600353
Alex Klein1699fab2022-09-08 08:46:06 -0600354 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600355 overlays: The list of overlay paths.
356 build_targets: The build targets to clean in |chroot|, if desired. No
357 effect unless |chroot| is provided.
358 chroot: The chroot to clean, if desired.
359 output_dir: The path to optionally dump result files.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600360
Alex Klein1699fab2022-09-08 08:46:06 -0600361 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600362 The paths to all the modified ebuild files. This includes the new files
363 that were added (i.e. the new versions) and all the removed files
Alex Klein1699fab2022-09-08 08:46:06 -0600364 (i.e. the old versions).
365 """
366 assert overlays
Alex Kleineb77ffa2019-05-28 14:47:44 -0600367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600369
Alex Klein1699fab2022-09-08 08:46:06 -0600370 uprev_manager = uprev_lib.UprevOverlayManager(
371 overlays,
372 manifest,
373 build_targets=build_targets,
374 chroot=chroot,
375 output_dir=output_dir,
376 )
377 uprev_manager.uprev()
Alex Kleineb77ffa2019-05-28 14:47:44 -0600378
Alex Klein1699fab2022-09-08 08:46:06 -0600379 return uprev_manager.modified_ebuilds, uprev_manager.revved_packages
Alex Kleineb77ffa2019-05-28 14:47:44 -0600380
381
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700382def uprev_versioned_package(
383 package: package_info.CPV,
Alex Klein1699fab2022-09-08 08:46:06 -0600384 build_targets: List["build_target_lib.BuildTarget"],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700385 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600386 chroot: "chroot_lib.Chroot",
387) -> "uprev_lib.UprevVersionedPackageResult":
388 """Call registered uprev handler function for the package.
Alex Klein87531182019-08-12 15:23:37 -0600389
Alex Klein1699fab2022-09-08 08:46:06 -0600390 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600391 package: The package being uprevved.
392 build_targets: The build targets to clean on a successful uprev.
393 refs:
394 chroot: The chroot to enter for cleaning.
Alex Klein87531182019-08-12 15:23:37 -0600395
Alex Klein1699fab2022-09-08 08:46:06 -0600396 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600397 The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600398 """
399 assert package
Alex Klein87531182019-08-12 15:23:37 -0600400
Alex Klein1699fab2022-09-08 08:46:06 -0600401 if package.cp not in _UPREV_FUNCS:
402 raise UnknownPackageError(
403 'Package "%s" does not have a registered handler.' % package.cp
404 )
Alex Klein87531182019-08-12 15:23:37 -0600405
Alex Klein1699fab2022-09-08 08:46:06 -0600406 return _UPREV_FUNCS[package.cp](build_targets, refs, chroot)
Alex Klein87531182019-08-12 15:23:37 -0600407
408
Alex Klein1699fab2022-09-08 08:46:06 -0600409@uprevs_versioned_package("media-libs/virglrenderer")
Navil Perezf57ba872020-06-04 22:38:37 +0000410def uprev_virglrenderer(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600411 """Updates virglrenderer ebuilds.
Navil Perezf57ba872020-06-04 22:38:37 +0000412
Alex Klein1699fab2022-09-08 08:46:06 -0600413 See: uprev_versioned_package.
Navil Perezf57ba872020-06-04 22:38:37 +0000414
Alex Klein1699fab2022-09-08 08:46:06 -0600415 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600416 UprevVersionedPackageResult: The result of updating virglrenderer
417 ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600418 """
419 overlay = os.path.join(
420 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
421 )
422 repo_path = os.path.join(
423 constants.SOURCE_ROOT, "src", "third_party", "virglrenderer"
424 )
425 manifest = git.ManifestCheckout.Cached(repo_path)
Navil Perezf57ba872020-06-04 22:38:37 +0000426
Alex Klein1699fab2022-09-08 08:46:06 -0600427 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
428 # TODO(crbug.com/1066242): Ebuilds for virglrenderer are currently
429 # denylisted. Do not force uprevs after builder is stable and ebuilds are no
430 # longer denylisted.
431 uprev_manager.uprev(package_list=["media-libs/virglrenderer"], force=True)
Navil Perezf57ba872020-06-04 22:38:37 +0000432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 updated_files = uprev_manager.modified_ebuilds
434 result = uprev_lib.UprevVersionedPackageResult()
435 result.add_result(refs[-1].revision, updated_files)
436 return result
Navil Perezf57ba872020-06-04 22:38:37 +0000437
Alex Klein1699fab2022-09-08 08:46:06 -0600438
Matthew Lam59ca37d2022-10-24 18:11:06 +0000439@uprevs_versioned_package("x11-apps/igt-gpu-tools")
440def uprev_igt_gpu_tools(_build_targets, refs, _chroot):
441 """Updates igt-gpu-tools ebuilds.
442
443 See: uprev_versioned_package.
444
445 Returns:
446 UprevVersionedPackageResult: The result of updating igt-gpu-tools ebuilds.
447 """
448 overlay = os.path.join(
449 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
450 )
451 repo_path = os.path.join(
452 constants.SOURCE_ROOT, "src", "third_party", "igt-gpu-tools"
453 )
454 manifest = git.ManifestCheckout.Cached(repo_path)
455
456 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
457 uprev_manager.uprev(package_list=["x11-apps/igt-gpu-tools"], force=True)
458
459 updated_files = uprev_manager.modified_ebuilds
460 result = uprev_lib.UprevVersionedPackageResult()
461 result.add_result(refs[-1].revision, updated_files)
462 return result
463
464
Alex Klein1699fab2022-09-08 08:46:06 -0600465@uprevs_versioned_package("chromeos-base/drivefs")
Jose Magana03b5a842020-08-19 12:52:59 +1000466def uprev_drivefs(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600467 """Updates drivefs ebuilds.
Jose Magana03b5a842020-08-19 12:52:59 +1000468
Alex Klein1699fab2022-09-08 08:46:06 -0600469 DriveFS versions follow the tag format of refs/tags/drivefs_1.2.3.
470 See: uprev_versioned_package.
Jose Magana03b5a842020-08-19 12:52:59 +1000471
Alex Klein1699fab2022-09-08 08:46:06 -0600472 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600473 UprevVersionedPackageResult: The result of updating drivefs ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600474 """
Jose Magana03b5a842020-08-19 12:52:59 +1000475
Alex Klein1699fab2022-09-08 08:46:06 -0600476 DRIVEFS_PATH_PREFIX = "src/private-overlays/chromeos-overlay/chromeos-base"
477 result = uprev_lib.UprevVersionedPackageResult()
478 all_changed_files = []
Jose Magana03b5a842020-08-19 12:52:59 +1000479
Alex Klein1699fab2022-09-08 08:46:06 -0600480 DRIVEFS_REFS_PREFIX = "refs/tags/drivefs_"
481 drivefs_version = _get_latest_version_from_refs(DRIVEFS_REFS_PREFIX, refs)
482 if not drivefs_version:
483 # No valid DriveFS version is identified.
484 return result
485
486 logging.debug("DriveFS version determined from refs: %s", drivefs_version)
487
488 # Attempt to uprev drivefs package.
489 pkg_path = os.path.join(DRIVEFS_PATH_PREFIX, "drivefs")
490 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
491 pkg_path, drivefs_version, chroot, allow_downrev=False
492 )
493
494 if not uprev_result:
495 return result
496 all_changed_files.extend(uprev_result.changed_files)
497 result.add_result(drivefs_version, all_changed_files)
498
Ben Reich4f3fa1b2020-12-19 08:21:26 +0000499 return result
Jose Magana03b5a842020-08-19 12:52:59 +1000500
Jose Magana03b5a842020-08-19 12:52:59 +1000501
Alex Klein1699fab2022-09-08 08:46:06 -0600502@uprevs_versioned_package("chromeos-base/perfetto")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800503def uprev_perfetto(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600504 """Updates Perfetto ebuilds.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800505
Alex Klein1699fab2022-09-08 08:46:06 -0600506 Perfetto versions follow the tag format of refs/tags/v1.2.
507 See: uprev_versioned_package.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800508
Alex Klein1699fab2022-09-08 08:46:06 -0600509 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600510 UprevVersionedPackageResult: The result of updating Perfetto ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600511 """
512 result = uprev_lib.UprevVersionedPackageResult()
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800513
Alex Klein1699fab2022-09-08 08:46:06 -0600514 PERFETTO_REFS_PREFIX = "refs/tags/v"
Chinglin Yuad12a512022-10-07 17:26:12 +0800515 PERFETTO_PATH = os.path.join(
516 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base/perfetto"
517 )
518
519 # Decide the version number to uprev to:
520 # * If |refs| contains refs/tags/v*, get the latest from them.
Alex Klein1699fab2022-09-08 08:46:06 -0600521 perfetto_version = _get_latest_version_from_refs(PERFETTO_REFS_PREFIX, refs)
Chinglin Yuad12a512022-10-07 17:26:12 +0800522 # * Or if |refs| contains only the latest trunk revisions, use the current
523 # stable ebuild version for a revision bump.
524 if refs and not perfetto_version:
525 perfetto_version = uprev_lib.get_stable_ebuild_version(PERFETTO_PATH)
526
Alex Klein1699fab2022-09-08 08:46:06 -0600527 if not perfetto_version:
528 # No valid Perfetto version is identified.
529 return result
530
Alex Klein1699fab2022-09-08 08:46:06 -0600531 # Attempt to uprev perfetto package.
Chinglin Yuad12a512022-10-07 17:26:12 +0800532 # |perfetto_version| is only used in determining the ebuild version. The
533 # package is always updated to the latest HEAD.
Alex Klein1699fab2022-09-08 08:46:06 -0600534 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
535 PERFETTO_PATH,
536 perfetto_version,
537 chroot,
538 allow_downrev=False,
Chinglin Yu84818732022-10-03 12:03:43 +0800539 # Use default ref="HEAD"
Alex Klein1699fab2022-09-08 08:46:06 -0600540 )
541
542 if not uprev_result:
543 return result
544
Chinglin Yu5de28a42022-11-11 19:52:21 +0800545 # Include short git sha hash in the uprev commit message.
546 # Use 9 digits to match the short hash length in `perfetto --version`.
547 short_revision = refs[-1].revision[0:9]
548 version_and_rev = f"{perfetto_version}-{short_revision}"
549 result.add_result(version_and_rev, uprev_result.changed_files)
Alex Klein1699fab2022-09-08 08:46:06 -0600550
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800551 return result
552
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800553
Denis Nikitin63613e32022-09-09 22:26:50 -0700554class AfdoMetadata(NamedTuple):
555 """Data class holding AFDO metadata."""
556
557 var_name: str
558 path: str
559
560
Alex Klein1699fab2022-09-08 08:46:06 -0600561@uprevs_versioned_package("afdo/kernel-profiles")
Yaakov Shaul395ae832019-09-09 14:45:32 -0600562def uprev_kernel_afdo(*_args, **_kwargs):
Alex Klein1699fab2022-09-08 08:46:06 -0600563 """Updates kernel ebuilds with versions from kernel_afdo.json.
Yaakov Shaul395ae832019-09-09 14:45:32 -0600564
Alex Klein1699fab2022-09-08 08:46:06 -0600565 See: uprev_versioned_package.
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600566
Alex Klein1699fab2022-09-08 08:46:06 -0600567 Raises:
Alex Klein348e7692022-10-13 17:03:37 -0600568 EbuildManifestError: When ebuild manifest does not complete
569 successfully.
570 JSONDecodeError: When json is malformed.
Alex Klein1699fab2022-09-08 08:46:06 -0600571 """
Denis Nikitin63613e32022-09-09 22:26:50 -0700572 metadata_dir = os.path.join(
Alex Klein1699fab2022-09-08 08:46:06 -0600573 constants.SOURCE_ROOT,
574 "src",
575 "third_party",
576 "toolchain-utils",
577 "afdo_metadata",
Denis Nikitin63613e32022-09-09 22:26:50 -0700578 )
579 metadata_files = (
580 AfdoMetadata(
581 var_name="AFDO_PROFILE_VERSION",
582 path=os.path.join(metadata_dir, "kernel_afdo.json"),
583 ),
584 AfdoMetadata(
585 var_name="ARM_AFDO_PROFILE_VERSION",
586 path=os.path.join(metadata_dir, "kernel_arm_afdo.json"),
587 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600588 )
Yaakov Shaul395ae832019-09-09 14:45:32 -0600589
Alex Klein1699fab2022-09-08 08:46:06 -0600590 result = uprev_lib.UprevVersionedPackageResult()
Denis Nikitin63613e32022-09-09 22:26:50 -0700591 for metadata in metadata_files:
592 with open(metadata.path, "r") as f:
593 versions = json.load(f)
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600594
Denis Nikitin63613e32022-09-09 22:26:50 -0700595 for kernel_pkg, version_info in versions.items():
596 path = os.path.join(
597 constants.CHROMIUMOS_OVERLAY_DIR, "sys-kernel", kernel_pkg
598 )
599 ebuild_path = os.path.join(
600 constants.SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
601 )
602 chroot_ebuild_path = os.path.join(
603 constants.CHROOT_SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
604 )
605 afdo_profile_version = version_info["name"]
606 patch_ebuild_vars(
607 ebuild_path, {metadata.var_name: afdo_profile_version}
Alex Klein1699fab2022-09-08 08:46:06 -0600608 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600609
Denis Nikitin63613e32022-09-09 22:26:50 -0700610 try:
611 cmd = ["ebuild", chroot_ebuild_path, "manifest", "--force"]
612 cros_build_lib.run(cmd, enter_chroot=True)
613 except cros_build_lib.RunCommandError as e:
614 raise uprev_lib.EbuildManifestError(
615 "Error encountered when regenerating the manifest for "
616 f"ebuild: {chroot_ebuild_path}\n{e}",
617 e,
618 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600619
Denis Nikitin63613e32022-09-09 22:26:50 -0700620 manifest_path = os.path.join(
621 constants.SOURCE_ROOT, path, "Manifest"
622 )
623 result.add_result(
624 afdo_profile_version, [ebuild_path, manifest_path]
625 )
Yaakov Shaul730814a2019-09-10 13:58:25 -0600626
Alex Klein1699fab2022-09-08 08:46:06 -0600627 return result
Yaakov Shaul395ae832019-09-09 14:45:32 -0600628
629
Alex Klein1699fab2022-09-08 08:46:06 -0600630@uprevs_versioned_package("chromeos-base/termina-dlc")
631@uprevs_versioned_package("chromeos-base/termina-tools-dlc")
Maciek Swiech6b12f662022-01-25 16:51:19 +0000632def uprev_termina_dlcs(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600633 """Updates shared termina-dlc and termina-tools-dlc ebuilds.
Maciek Swiech6b12f662022-01-25 16:51:19 +0000634
Alex Klein1699fab2022-09-08 08:46:06 -0600635 termina-dlc - chromeos-base/termina-dlc
636 termina-tools-dlc - chromeos-base/termina-tools-dlc
Trent Beginaf51f1b2020-03-09 17:35:31 -0600637
Alex Klein1699fab2022-09-08 08:46:06 -0600638 See: uprev_versioned_package.
639 """
640 termina_dlc_pkg = "termina-dlc"
641 termina_dlc_pkg_path = os.path.join(
642 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", termina_dlc_pkg
643 )
644 tools_dlc_pkg = "termina-tools-dlc"
645 tools_dlc_pkg_path = os.path.join(
646 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", tools_dlc_pkg
647 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000648
Alex Klein1699fab2022-09-08 08:46:06 -0600649 # termina-dlc and termina-tools-dlc are pinned to the same version.
650 version_pin_src_path = _get_version_pin_src_path(termina_dlc_pkg_path)
651 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Patrick Meiring5897add2020-09-16 16:30:17 +1000652
Alex Klein1699fab2022-09-08 08:46:06 -0600653 result = uprev_lib.uprev_ebuild_from_pin(
654 termina_dlc_pkg_path, version_no_rev, chroot
655 )
656 result += uprev_lib.uprev_ebuild_from_pin(
657 tools_dlc_pkg_path, version_no_rev, chroot
658 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000659
Alex Klein1699fab2022-09-08 08:46:06 -0600660 return result
Patrick Meiring5897add2020-09-16 16:30:17 +1000661
Alex Klein1699fab2022-09-08 08:46:06 -0600662
663@uprevs_versioned_package("chromeos-base/chromeos-lacros")
Julio Hurtadof1befec2021-05-05 21:34:26 +0000664def uprev_lacros(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600665 """Updates lacros ebuilds.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000666
Alex Klein1699fab2022-09-08 08:46:06 -0600667 Version to uprev to is gathered from the QA qualified version tracking file
668 stored in chromium/src/chrome/LACROS_QA_QUALIFIED_VERSION. Uprev is triggered
669 on modification of this file across all chromium/src branches.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000670
Alex Klein1699fab2022-09-08 08:46:06 -0600671 See: uprev_versioned_package.
672 """
673 result = uprev_lib.UprevVersionedPackageResult()
674 path = os.path.join(
675 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
676 )
677 lacros_version = refs[0].revision
678 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
679 path, lacros_version, chroot, allow_downrev=False
680 )
Julio Hurtadoa994e002021-07-07 17:57:45 +0000681
Alex Klein1699fab2022-09-08 08:46:06 -0600682 if not uprev_result:
683 return result
684
685 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtadoa994e002021-07-07 17:57:45 +0000686 return result
687
Julio Hurtadof1befec2021-05-05 21:34:26 +0000688
Alex Klein1699fab2022-09-08 08:46:06 -0600689@uprevs_versioned_package("chromeos-base/chromeos-lacros-parallel")
Julio Hurtado870ed322021-12-03 18:22:40 +0000690def uprev_lacros_in_parallel(
Alex Klein1699fab2022-09-08 08:46:06 -0600691 _build_targets: Optional[List["build_target_lib.BuildTarget"]],
Julio Hurtado870ed322021-12-03 18:22:40 +0000692 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600693 chroot: "chroot_lib.Chroot",
694) -> "uprev_lib.UprevVersionedPackageResult":
695 """Updates lacros ebuilds in parallel with ash-chrome.
Julio Hurtado870ed322021-12-03 18:22:40 +0000696
Alex Klein1699fab2022-09-08 08:46:06 -0600697 This handler is going to be used temporarily while lacros transitions to being
698 uprevved atomically with ash-chrome. Unlike a standalone lacros uprev, this
699 handler will not need to look at the QA qualified file. Rather, it will
700 function identical to ash-chrome using git tags.
Julio Hurtado870ed322021-12-03 18:22:40 +0000701
Alex Klein1699fab2022-09-08 08:46:06 -0600702 See: uprev_versioned_package.
Julio Hurtado870ed322021-12-03 18:22:40 +0000703
Alex Klein1699fab2022-09-08 08:46:06 -0600704 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600705 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600706 """
707 result = uprev_lib.UprevVersionedPackageResult()
708 path = os.path.join(
709 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
710 )
711 lacros_version = uprev_lib.get_version_from_refs(refs)
712 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
713 path, lacros_version, chroot, allow_downrev=False
714 )
Julio Hurtado870ed322021-12-03 18:22:40 +0000715
Alex Klein1699fab2022-09-08 08:46:06 -0600716 if not uprev_result:
717 return result
718
719 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtado870ed322021-12-03 18:22:40 +0000720 return result
721
Julio Hurtado870ed322021-12-03 18:22:40 +0000722
Alex Klein1699fab2022-09-08 08:46:06 -0600723@uprevs_versioned_package("app-emulation/parallels-desktop")
Patrick Meiring5897add2020-09-16 16:30:17 +1000724def uprev_parallels_desktop(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600725 """Updates Parallels Desktop ebuild - app-emulation/parallels-desktop.
Patrick Meiring5897add2020-09-16 16:30:17 +1000726
Alex Klein1699fab2022-09-08 08:46:06 -0600727 See: uprev_versioned_package
Patrick Meiring5897add2020-09-16 16:30:17 +1000728
Alex Klein1699fab2022-09-08 08:46:06 -0600729 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600730 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600731 """
732 package = "parallels-desktop"
733 package_path = os.path.join(
734 constants.CHROMEOS_PARTNER_OVERLAY_DIR, "app-emulation", package
735 )
736 version_pin_src_path = _get_version_pin_src_path(package_path)
Patrick Meiring5897add2020-09-16 16:30:17 +1000737
Alex Klein1699fab2022-09-08 08:46:06 -0600738 # Expect a JSON blob like the following:
739 # {
740 # "version": "1.2.3",
741 # "test_image": { "url": "...", "size": 12345678,
742 # "sha256sum": "<32 bytes of hexadecimal>" }
743 # }
744 with open(version_pin_src_path, "r") as f:
745 pinned = json.load(f)
Patrick Meiring5897add2020-09-16 16:30:17 +1000746
Alex Klein1699fab2022-09-08 08:46:06 -0600747 if "version" not in pinned or "test_image" not in pinned:
748 raise UprevError(
749 "VERSION-PIN for %s missing version and/or "
750 "test_image field" % package
751 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000752
Alex Klein1699fab2022-09-08 08:46:06 -0600753 version = pinned["version"]
754 if not isinstance(version, str):
755 raise UprevError("version in VERSION-PIN for %s not a string" % package)
Patrick Meiring5897add2020-09-16 16:30:17 +1000756
Alex Klein1699fab2022-09-08 08:46:06 -0600757 # Update the ebuild.
758 result = uprev_lib.uprev_ebuild_from_pin(package_path, version, chroot)
Patrick Meiring5897add2020-09-16 16:30:17 +1000759
Alex Klein1699fab2022-09-08 08:46:06 -0600760 # Update the VM image used for testing.
761 test_image_path = (
762 "src/platform/tast-tests-private/src/chromiumos/tast/"
763 "local/bundles/crosint/pita/data/"
764 "pluginvm_image.zip.external"
765 )
766 test_image_src_path = os.path.join(constants.SOURCE_ROOT, test_image_path)
767 with open(test_image_src_path, "w") as f:
768 json.dump(pinned["test_image"], f, indent=2)
769 result.add_result(version, [test_image_src_path])
Patrick Meiring5897add2020-09-16 16:30:17 +1000770
Alex Klein1699fab2022-09-08 08:46:06 -0600771 return result
Trent Beginaf51f1b2020-03-09 17:35:31 -0600772
773
Alex Klein1699fab2022-09-08 08:46:06 -0600774@uprevs_versioned_package("chromeos-base/chromeos-dtc-vm")
Trent Beginaf51f1b2020-03-09 17:35:31 -0600775def uprev_sludge(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600776 """Updates sludge VM - chromeos-base/chromeos-dtc-vm.
Trent Begin315d9d92019-12-03 21:55:53 -0700777
Alex Klein1699fab2022-09-08 08:46:06 -0600778 See: uprev_versioned_package.
779 """
780 package = "chromeos-dtc-vm"
781 package_path = os.path.join(
782 "src",
783 "private-overlays",
784 "project-wilco-private",
785 "chromeos-base",
786 package,
787 )
788 version_pin_src_path = _get_version_pin_src_path(package_path)
789 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Trent Begin315d9d92019-12-03 21:55:53 -0700790
Alex Klein1699fab2022-09-08 08:46:06 -0600791 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
Trent Begin315d9d92019-12-03 21:55:53 -0700792
793
Alex Klein1699fab2022-09-08 08:46:06 -0600794@uprevs_versioned_package("chromeos-base/borealis-dlc")
David Riley8513c1f2021-10-14 17:07:41 -0700795def uprev_borealis_dlc(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600796 """Updates shared borealis-dlc ebuild - chromeos-base/borealis-dlc.
David Riley8513c1f2021-10-14 17:07:41 -0700797
Alex Klein1699fab2022-09-08 08:46:06 -0600798 See: uprev_versioned_package.
799 """
800 package_path = os.path.join(
801 "src",
802 "private-overlays",
803 "chromeos-partner-overlay",
804 "chromeos-base",
805 "borealis-dlc",
806 )
David Riley8513c1f2021-10-14 17:07:41 -0700807
Alex Klein1699fab2022-09-08 08:46:06 -0600808 version_pin_src_path = _get_version_pin_src_path(package_path)
809 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
David Riley8513c1f2021-10-14 17:07:41 -0700810
Alex Klein1699fab2022-09-08 08:46:06 -0600811 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
David Riley8513c1f2021-10-14 17:07:41 -0700812
813
Patrick Meiring5897add2020-09-16 16:30:17 +1000814def _get_version_pin_src_path(package_path):
Alex Klein1699fab2022-09-08 08:46:06 -0600815 """Returns the path to the VERSION-PIN file for the given package."""
816 return os.path.join(constants.SOURCE_ROOT, package_path, "VERSION-PIN")
Patrick Meiring5897add2020-09-16 16:30:17 +1000817
818
Alex Klein87531182019-08-12 15:23:37 -0600819@uprevs_versioned_package(constants.CHROME_CP)
Alex Klein4e839252022-01-06 13:29:18 -0700820def uprev_chrome_from_ref(build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600821 """Uprev chrome and its related packages.
Alex Klein87531182019-08-12 15:23:37 -0600822
Alex Klein1699fab2022-09-08 08:46:06 -0600823 See: uprev_versioned_package.
824 """
825 # Determine the version from the refs (tags), i.e. the chrome versions are the
826 # tag names.
827 chrome_version = uprev_lib.get_version_from_refs(refs)
828 logging.debug("Chrome version determined from refs: %s", chrome_version)
Alex Klein87531182019-08-12 15:23:37 -0600829
Alex Klein1699fab2022-09-08 08:46:06 -0600830 return uprev_chrome(chrome_version, build_targets, None)
Alex Kleinf69bd802021-06-22 15:43:49 -0600831
832
Alex Klein9ce3f682021-06-23 15:06:44 -0600833def revbump_chrome(
Alex Klein1699fab2022-09-08 08:46:06 -0600834 build_targets: List["build_target_lib.BuildTarget"] = None,
835 chroot: Optional["chroot_lib.Chroot"] = None,
Alex Klein9ce3f682021-06-23 15:06:44 -0600836) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600837 """Attempt to revbump chrome.
Alex Kleinf69bd802021-06-22 15:43:49 -0600838
Alex Klein1699fab2022-09-08 08:46:06 -0600839 Revbumps are done by executing an uprev using the current stable version.
840 E.g. if chrome is on 1.2.3.4 and has a 1.2.3.4_rc-r2.ebuild, performing an
841 uprev on version 1.2.3.4 when there are applicable changes (e.g. to the 9999
842 ebuild) will result in a revbump to 1.2.3.4_rc-r3.ebuild.
843 """
844 chrome_version = uprev_lib.get_stable_chrome_version()
845 return uprev_chrome(chrome_version, build_targets, chroot)
Alex Kleinf69bd802021-06-22 15:43:49 -0600846
847
Alex Klein9ce3f682021-06-23 15:06:44 -0600848def uprev_chrome(
Alex Klein16ea1b32021-10-01 15:48:50 -0600849 chrome_version: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600850 build_targets: Optional[List["build_target_lib.BuildTarget"]],
851 chroot: Optional["chroot_lib.Chroot"],
Alex Klein9ce3f682021-06-23 15:06:44 -0600852) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600853 """Attempt to uprev chrome and its related packages to the given version."""
854 uprev_manager = uprev_lib.UprevChromeManager(
855 chrome_version, build_targets=build_targets, chroot=chroot
856 )
857 result = uprev_lib.UprevVersionedPackageResult()
858 # TODO(crbug.com/1080429): Handle all possible outcomes of a Chrome uprev
859 # attempt. The expected behavior is documented in the following table:
860 #
861 # Outcome of Chrome uprev attempt:
862 # NEWER_VERSION_EXISTS:
863 # Do nothing.
864 # SAME_VERSION_EXISTS or REVISION_BUMP:
865 # Uprev followers
866 # Assert not VERSION_BUMP (any other outcome is fine)
867 # VERSION_BUMP or NEW_EBUILD_CREATED:
868 # Uprev followers
869 # Assert that Chrome & followers are at same package version
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600870
Alex Klein1699fab2022-09-08 08:46:06 -0600871 # Start with chrome itself so we can proceed accordingly.
872 chrome_result = uprev_manager.uprev(constants.CHROME_CP)
873 if chrome_result.newer_version_exists:
874 # Cannot use the given version (newer version already exists).
875 return result
876
877 # Also uprev related packages.
878 for package in constants.OTHER_CHROME_PACKAGES:
879 follower_result = uprev_manager.uprev(package)
880 if chrome_result.stable_version and follower_result.version_bump:
881 logging.warning(
882 "%s had a version bump, but no more than a revision bump "
883 "should have been possible.",
884 package,
885 )
886
887 if uprev_manager.modified_ebuilds:
888 # Record changes when we have them.
889 return result.add_result(chrome_version, uprev_manager.modified_ebuilds)
890
David Burger37f48672019-09-18 17:07:56 -0600891 return result
Alex Klein87531182019-08-12 15:23:37 -0600892
Alex Klein87531182019-08-12 15:23:37 -0600893
Alex Klein1699fab2022-09-08 08:46:06 -0600894def _get_latest_version_from_refs(
895 refs_prefix: str, refs: List[uprev_lib.GitRef]
896) -> str:
897 """Get the latest version from refs
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600898
Alex Klein1699fab2022-09-08 08:46:06 -0600899 Versions are compared using |distutils.version.LooseVersion| and
900 the latest version is returned.
Alex Klein87531182019-08-12 15:23:37 -0600901
Alex Klein1699fab2022-09-08 08:46:06 -0600902 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600903 refs_prefix: The refs prefix of the tag format.
904 refs: The tags to parse for the latest version.
Alex Klein87531182019-08-12 15:23:37 -0600905
Alex Klein1699fab2022-09-08 08:46:06 -0600906 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600907 The latest version to use as string.
Alex Klein1699fab2022-09-08 08:46:06 -0600908 """
909 valid_refs = []
910 for gitiles in refs:
911 if gitiles.ref.startswith(refs_prefix):
912 valid_refs.append(gitiles.ref)
Ben Reiche779cf42020-12-15 03:21:31 +0000913
Alex Klein1699fab2022-09-08 08:46:06 -0600914 if not valid_refs:
915 return None
Ben Reiche779cf42020-12-15 03:21:31 +0000916
Alex Klein1699fab2022-09-08 08:46:06 -0600917 # Sort by version and take the latest version.
918 target_version_ref = sorted(valid_refs, key=LooseVersion, reverse=True)[0]
919 return target_version_ref.replace(refs_prefix, "")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800920
921
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700922def _generate_platform_c_files(
923 replication_config: replication_config_pb2.ReplicationConfig,
Alex Klein1699fab2022-09-08 08:46:06 -0600924 chroot: "chroot_lib.Chroot",
925) -> List[str]:
926 """Generates platform C files from a platform JSON payload.
Andrew Lamb9563a152019-12-04 11:42:18 -0700927
Alex Klein1699fab2022-09-08 08:46:06 -0600928 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600929 replication_config: A ReplicationConfig that has already been run. If it
930 produced a build_config.json file, that file will be used to
931 generate platform C files. Otherwise, nothing will be generated.
932 chroot: The chroot to use to generate.
Andrew Lamb9563a152019-12-04 11:42:18 -0700933
Alex Klein1699fab2022-09-08 08:46:06 -0600934 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600935 A list of generated files.
Alex Klein1699fab2022-09-08 08:46:06 -0600936 """
937 # Generate the platform C files from the build config. Note that it would be
938 # more intuitive to generate the platform C files from the platform config;
939 # however, cros_config_schema does not allow this, because the platform config
940 # payload is not always valid input. For example, if a property is both
941 # 'required' and 'build-only', it will fail schema validation. Thus, use the
942 # build config, and use '-f' to filter.
943 build_config_path = [
944 rule.destination_path
945 for rule in replication_config.file_replication_rules
946 if rule.destination_path.endswith("build_config.json")
947 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700948
Alex Klein1699fab2022-09-08 08:46:06 -0600949 if not build_config_path:
950 logging.info(
951 "No build_config.json found, will not generate platform C files. "
952 "Replication config: %s",
953 replication_config,
954 )
955 return []
Andrew Lamb9563a152019-12-04 11:42:18 -0700956
Alex Klein1699fab2022-09-08 08:46:06 -0600957 if len(build_config_path) > 1:
958 raise ValueError(
959 "Expected at most one build_config.json destination path. "
960 "Replication config: %s" % replication_config
961 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700962
Alex Klein1699fab2022-09-08 08:46:06 -0600963 build_config_path = build_config_path[0]
Andrew Lamb9563a152019-12-04 11:42:18 -0700964
Alex Klein1699fab2022-09-08 08:46:06 -0600965 # Paths to the build_config.json and dir to output C files to, in the
966 # chroot.
967 build_config_chroot_path = os.path.join(
968 constants.CHROOT_SOURCE_ROOT, build_config_path
969 )
970 generated_output_chroot_dir = os.path.join(
971 constants.CHROOT_SOURCE_ROOT, os.path.dirname(build_config_path)
972 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700973
Alex Klein1699fab2022-09-08 08:46:06 -0600974 command = [
975 "cros_config_schema",
976 "-m",
977 build_config_chroot_path,
978 "-g",
979 generated_output_chroot_dir,
980 "-f",
981 '"TRUE"',
982 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700983
Alex Klein1699fab2022-09-08 08:46:06 -0600984 cros_build_lib.run(
985 command, enter_chroot=True, chroot_args=chroot.get_enter_args()
986 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700987
Alex Klein1699fab2022-09-08 08:46:06 -0600988 # A relative (to the source root) path to the generated C files.
989 generated_output_dir = os.path.dirname(build_config_path)
990 generated_files = []
991 expected_c_files = ["config.c", "ec_config.c", "ec_config.h"]
992 for f in expected_c_files:
993 if os.path.exists(
994 os.path.join(constants.SOURCE_ROOT, generated_output_dir, f)
995 ):
996 generated_files.append(os.path.join(generated_output_dir, f))
Andrew Lamb9563a152019-12-04 11:42:18 -0700997
Alex Klein1699fab2022-09-08 08:46:06 -0600998 if len(expected_c_files) != len(generated_files):
999 raise GeneratedCrosConfigFilesError(expected_c_files, generated_files)
Andrew Lamb9563a152019-12-04 11:42:18 -07001000
Alex Klein1699fab2022-09-08 08:46:06 -06001001 return generated_files
Andrew Lamb9563a152019-12-04 11:42:18 -07001002
1003
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001004def _get_private_overlay_package_root(ref: uprev_lib.GitRef, package: str):
Alex Klein1699fab2022-09-08 08:46:06 -06001005 """Returns the absolute path to the root of a given private overlay.
Andrew Lambe836f222019-12-09 12:27:38 -07001006
Alex Klein1699fab2022-09-08 08:46:06 -06001007 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001008 ref: GitRef for the private overlay.
1009 package: Path to the package in the overlay.
Alex Klein1699fab2022-09-08 08:46:06 -06001010 """
1011 # There might be a cleaner way to map from package -> path within the source
1012 # tree. For now, just use string patterns.
1013 private_overlay_ref_pattern = (
1014 r"/chromeos\/overlays\/overlay-([\w-]+)-private"
1015 )
1016 match = re.match(private_overlay_ref_pattern, ref.path)
1017 if not match:
1018 raise ValueError(
1019 "ref.path must match the pattern: %s. Actual ref: %s"
1020 % (private_overlay_ref_pattern, ref)
1021 )
Andrew Lambe836f222019-12-09 12:27:38 -07001022
Alex Klein1699fab2022-09-08 08:46:06 -06001023 overlay = match.group(1)
Andrew Lambe836f222019-12-09 12:27:38 -07001024
Alex Klein1699fab2022-09-08 08:46:06 -06001025 return os.path.join(
1026 constants.SOURCE_ROOT,
1027 "src/private-overlays/overlay-%s-private" % overlay,
1028 package,
1029 )
Andrew Lambe836f222019-12-09 12:27:38 -07001030
1031
Alex Klein1699fab2022-09-08 08:46:06 -06001032@uprevs_versioned_package("chromeos-base/chromeos-config-bsp")
Andrew Lambea9a8a22019-12-12 14:03:43 -07001033def replicate_private_config(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -06001034 """Replicate a private cros_config change to the corresponding public config.
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001035
Alex Klein1699fab2022-09-08 08:46:06 -06001036 See uprev_versioned_package for args
1037 """
1038 package = "chromeos-base/chromeos-config-bsp"
Andrew Lambea9a8a22019-12-12 14:03:43 -07001039
Alex Klein1699fab2022-09-08 08:46:06 -06001040 if len(refs) != 1:
1041 raise ValueError("Expected exactly one ref, actual %s" % refs)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001042
Alex Klein1699fab2022-09-08 08:46:06 -06001043 # Expect a replication_config.jsonpb in the package root.
1044 package_root = _get_private_overlay_package_root(refs[0], package)
1045 replication_config_path = os.path.join(
1046 package_root, "replication_config.jsonpb"
1047 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001048
Alex Klein1699fab2022-09-08 08:46:06 -06001049 try:
1050 replication_config = json_format.Parse(
1051 osutils.ReadFile(replication_config_path),
1052 replication_config_pb2.ReplicationConfig(),
1053 )
1054 except IOError:
1055 raise ValueError(
1056 "Expected ReplicationConfig missing at %s" % replication_config_path
1057 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001058
Alex Klein1699fab2022-09-08 08:46:06 -06001059 replication_lib.Replicate(replication_config)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001060
Alex Klein1699fab2022-09-08 08:46:06 -06001061 modified_files = [
1062 rule.destination_path
1063 for rule in replication_config.file_replication_rules
1064 ]
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001065
Alex Klein1699fab2022-09-08 08:46:06 -06001066 # The generated platform C files are not easily filtered by replication rules,
1067 # i.e. JSON / proto filtering can be described by a FieldMask, arbitrary C
1068 # files cannot. Therefore, replicate and filter the JSON payloads, and then
1069 # generate filtered C files from the JSON payload.
1070 modified_files.extend(
1071 _generate_platform_c_files(replication_config, chroot)
1072 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001073
Alex Klein1699fab2022-09-08 08:46:06 -06001074 # Use the private repo's commit hash as the new version.
1075 new_private_version = refs[0].revision
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001076
Alex Klein1699fab2022-09-08 08:46:06 -06001077 # modified_files should contain only relative paths at this point, but the
1078 # returned UprevVersionedPackageResult must contain only absolute paths.
1079 for i, modified_file in enumerate(modified_files):
1080 assert not os.path.isabs(modified_file)
1081 modified_files[i] = os.path.join(constants.SOURCE_ROOT, modified_file)
Andrew Lamb988f4da2019-12-10 10:16:43 -07001082
Alex Klein1699fab2022-09-08 08:46:06 -06001083 return uprev_lib.UprevVersionedPackageResult().add_result(
1084 new_private_version, modified_files
1085 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001086
1087
Alex Klein1699fab2022-09-08 08:46:06 -06001088@uprevs_versioned_package("chromeos-base/crosvm")
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001089def uprev_crosvm(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -06001090 """Updates crosvm ebuilds to latest revision
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001091
Alex Klein1699fab2022-09-08 08:46:06 -06001092 crosvm is not versioned. We are updating to the latest commit on the main
1093 branch.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001094
Alex Klein1699fab2022-09-08 08:46:06 -06001095 See: uprev_versioned_package.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001096
Alex Klein1699fab2022-09-08 08:46:06 -06001097 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001098 UprevVersionedPackageResult: The result of updating crosvm ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -06001099 """
1100 overlay = os.path.join(
1101 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1102 )
1103 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "crosvm")
1104 manifest = git.ManifestCheckout.Cached(repo_path)
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001105
Alex Klein1699fab2022-09-08 08:46:06 -06001106 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1107 uprev_manager.uprev(
1108 package_list=[
1109 "chromeos-base/crosvm",
1110 "dev-rust/assertions",
1111 "dev-rust/cros_async",
1112 "dev-rust/cros_fuzz",
1113 "dev-rust/data_model",
1114 "dev-rust/enumn",
1115 "dev-rust/io_uring",
1116 "dev-rust/p9",
1117 "dev-rust/sync",
1118 "dev-rust/sys_util",
1119 "dev-rust/tempfile",
1120 "media-sound/audio_streams",
1121 ],
1122 force=True,
1123 )
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001124
Alex Klein1699fab2022-09-08 08:46:06 -06001125 updated_files = uprev_manager.modified_ebuilds
1126 result = uprev_lib.UprevVersionedPackageResult()
1127 result.add_result(refs[0].revision, updated_files)
1128 return result
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001129
1130
Alex Klein5caab872021-09-10 11:44:37 -06001131def get_best_visible(
Alex Klein1699fab2022-09-08 08:46:06 -06001132 atom: str, build_target: Optional["build_target_lib.BuildTarget"] = None
Alex Klein5caab872021-09-10 11:44:37 -06001133) -> package_info.PackageInfo:
Alex Klein1699fab2022-09-08 08:46:06 -06001134 """Returns the best visible CPV for the given atom.
Alex Kleinbbef2b32019-08-27 10:38:50 -06001135
Alex Klein1699fab2022-09-08 08:46:06 -06001136 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001137 atom: The atom to look up.
1138 build_target: The build target whose sysroot should be searched, or the
1139 SDK if not provided.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001140
Alex Klein1699fab2022-09-08 08:46:06 -06001141 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001142 The best visible package, or None if none are visible.
Alex Klein1699fab2022-09-08 08:46:06 -06001143 """
1144 assert atom
Alex Kleinbbef2b32019-08-27 10:38:50 -06001145
Alex Klein1699fab2022-09-08 08:46:06 -06001146 return portage_util.PortageqBestVisible(
1147 atom,
1148 board=build_target.name if build_target else None,
1149 sysroot=build_target.root if build_target else None,
1150 )
Alex Kleinda39c6d2019-09-16 14:36:36 -06001151
1152
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001153def has_prebuilt(
1154 atom: str,
Alex Klein1699fab2022-09-08 08:46:06 -06001155 build_target: "build_target_lib.BuildTarget" = None,
1156 useflags: Union[Iterable[str], str] = None,
1157) -> bool:
1158 """Check if a prebuilt exists.
Alex Kleinda39c6d2019-09-16 14:36:36 -06001159
Alex Klein1699fab2022-09-08 08:46:06 -06001160 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001161 atom: The package whose prebuilt is being queried.
1162 build_target: The build target whose sysroot should be searched, or the
1163 SDK if not provided.
1164 useflags: Any additional USE flags that should be set. May be a string
1165 of properly formatted USE flags, or an iterable of individual flags.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001166
Alex Klein1699fab2022-09-08 08:46:06 -06001167 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001168 True if there is an available prebuilt, False otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -06001169 """
1170 assert atom
Alex Kleinda39c6d2019-09-16 14:36:36 -06001171
Alex Klein1699fab2022-09-08 08:46:06 -06001172 board = build_target.name if build_target else None
1173 extra_env = None
1174 if useflags:
1175 new_flags = useflags
1176 if not isinstance(useflags, str):
1177 new_flags = " ".join(useflags)
Alex Klein149fd3b2019-12-16 16:01:05 -07001178
Alex Klein1699fab2022-09-08 08:46:06 -06001179 existing = os.environ.get("USE", "")
1180 final_flags = "%s %s" % (existing, new_flags)
1181 extra_env = {"USE": final_flags.strip()}
1182 return portage_util.HasPrebuilt(atom, board=board, extra_env=extra_env)
Alex Klein36b117f2019-09-30 15:13:46 -06001183
1184
David Burger0f9dd4e2019-10-08 12:33:42 -06001185def builds(atom, build_target, packages=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001186 """Check if |build_target| builds |atom| (has it in its depgraph)."""
1187 cros_build_lib.AssertInsideChroot()
Alex Klein36b117f2019-09-30 15:13:46 -06001188
Alex Klein1699fab2022-09-08 08:46:06 -06001189 pkgs = tuple(packages) if packages else None
1190 # TODO(crbug/1081828): Receive and use sysroot.
1191 graph, _sdk_graph = dependency.GetBuildDependency(
1192 build_target.root, build_target.name, pkgs
1193 )
1194 return any(atom in package for package in graph["package_deps"])
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001195
1196
Alex Klein6becabc2020-09-11 14:03:05 -06001197def needs_chrome_source(
Alex Klein1699fab2022-09-08 08:46:06 -06001198 build_target: "build_target_lib.BuildTarget",
Alex Klein6becabc2020-09-11 14:03:05 -06001199 compile_source=False,
1200 packages: Optional[List[package_info.PackageInfo]] = None,
Alex Klein1699fab2022-09-08 08:46:06 -06001201 useflags=None,
1202):
1203 """Check if the chrome source is needed.
Alex Klein6becabc2020-09-11 14:03:05 -06001204
Alex Klein1699fab2022-09-08 08:46:06 -06001205 The chrome source is needed if the build target builds chrome or any of its
1206 follower packages, and can't use a prebuilt for them either because it's not
1207 available, or because we can't use prebuilts because it must build from
1208 source.
1209 """
1210 cros_build_lib.AssertInsideChroot()
Alex Klein6becabc2020-09-11 14:03:05 -06001211
Alex Klein1699fab2022-09-08 08:46:06 -06001212 # Check if it builds chrome and/or a follower package.
1213 graph = depgraph.get_sysroot_dependency_graph(build_target.root, packages)
1214 builds_chrome = constants.CHROME_CP in graph
1215 builds_follower = {
1216 pkg: pkg in graph for pkg in constants.OTHER_CHROME_PACKAGES
1217 }
Alex Klein6becabc2020-09-11 14:03:05 -06001218
Alex Klein1699fab2022-09-08 08:46:06 -06001219 local_uprev = builds_chrome and revbump_chrome([build_target])
Alex Klein9ce3f682021-06-23 15:06:44 -06001220
Alex Klein1699fab2022-09-08 08:46:06 -06001221 # When we are compiling source set False since we do not use prebuilts.
1222 # When not compiling from source, start with True, i.e. we have every prebuilt
1223 # we've checked for up to this point.
1224 has_chrome_prebuilt = not compile_source
1225 has_follower_prebuilts = not compile_source
1226 # Save packages that need prebuilts for reporting.
1227 pkgs_needing_prebuilts = []
1228 if compile_source:
1229 # Need everything.
Alex Klein6becabc2020-09-11 14:03:05 -06001230 pkgs_needing_prebuilts.append(constants.CHROME_CP)
Alex Klein1699fab2022-09-08 08:46:06 -06001231 pkgs_needing_prebuilts.extend(
1232 [pkg for pkg, builds_pkg in builds_follower.items() if builds_pkg]
1233 )
1234 else:
1235 # Check chrome itself.
1236 if builds_chrome:
1237 has_chrome_prebuilt = has_prebuilt(
1238 constants.CHROME_CP,
1239 build_target=build_target,
1240 useflags=useflags,
1241 )
1242 if not has_chrome_prebuilt:
1243 pkgs_needing_prebuilts.append(constants.CHROME_CP)
1244 # Check follower packages.
1245 for pkg, builds_pkg in builds_follower.items():
1246 if not builds_pkg:
1247 continue
1248 prebuilt = has_prebuilt(
1249 pkg, build_target=build_target, useflags=useflags
1250 )
1251 has_follower_prebuilts &= prebuilt
1252 if not prebuilt:
1253 pkgs_needing_prebuilts.append(pkg)
1254 # Postcondition: has_chrome_prebuilt and has_follower_prebuilts now correctly
1255 # reflect whether we actually have the corresponding prebuilts for the build.
Alex Klein6becabc2020-09-11 14:03:05 -06001256
Alex Klein1699fab2022-09-08 08:46:06 -06001257 needs_chrome = builds_chrome and not has_chrome_prebuilt
1258 needs_follower = (
1259 any(builds_follower.values()) and not has_follower_prebuilts
1260 )
Alex Klein6becabc2020-09-11 14:03:05 -06001261
Alex Klein1699fab2022-09-08 08:46:06 -06001262 return NeedsChromeSourceResult(
1263 needs_chrome_source=needs_chrome or needs_follower,
1264 builds_chrome=builds_chrome,
1265 packages=[package_info.parse(p) for p in pkgs_needing_prebuilts],
1266 missing_chrome_prebuilt=not has_chrome_prebuilt,
1267 missing_follower_prebuilt=not has_follower_prebuilts,
1268 local_uprev=local_uprev,
1269 )
Alex Klein6becabc2020-09-11 14:03:05 -06001270
1271
Alex Klein68a28712021-11-08 11:08:30 -07001272class TargetVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001273 """Data class for the info that makes up the "target versions"."""
1274
1275 android_version: str
1276 android_branch: str
1277 android_target: str
1278 chrome_version: str
1279 platform_version: str
1280 milestone_version: str
1281 full_version: str
Alex Klein68a28712021-11-08 11:08:30 -07001282
1283
1284def get_target_versions(
Alex Klein1699fab2022-09-08 08:46:06 -06001285 build_target: "build_target_lib.BuildTarget",
1286 packages: List[package_info.PackageInfo] = None,
Alex Klein68a28712021-11-08 11:08:30 -07001287) -> TargetVersions:
Alex Klein1699fab2022-09-08 08:46:06 -06001288 """Aggregate version info for a few key packages and the OS as a whole."""
1289 # Android version.
1290 android_version = determine_android_version(build_target.name)
1291 logging.info("Found android version: %s", android_version)
1292 # Android branch version.
1293 android_branch = determine_android_branch(build_target.name)
1294 logging.info("Found android branch version: %s", android_branch)
1295 # Android target version.
1296 android_target = determine_android_target(build_target.name)
1297 logging.info("Found android target version: %s", android_target)
Alex Klein68a28712021-11-08 11:08:30 -07001298
Alex Klein1699fab2022-09-08 08:46:06 -06001299 # TODO(crbug/1019770): Investigate cases where builds_chrome is true but
1300 # chrome_version is None.
Alex Klein68a28712021-11-08 11:08:30 -07001301
Alex Klein1699fab2022-09-08 08:46:06 -06001302 builds_chrome = builds(constants.CHROME_CP, build_target, packages=packages)
1303 chrome_version = None
1304 if builds_chrome:
1305 # Chrome version fetch.
1306 chrome_version = determine_chrome_version(build_target)
1307 logging.info("Found chrome version: %s", chrome_version)
Alex Klein68a28712021-11-08 11:08:30 -07001308
Alex Klein1699fab2022-09-08 08:46:06 -06001309 # The ChromeOS version info.
1310 platform_version = determine_platform_version()
1311 milestone_version = determine_milestone_version()
1312 full_version = determine_full_version()
Alex Klein68a28712021-11-08 11:08:30 -07001313
Alex Klein1699fab2022-09-08 08:46:06 -06001314 return TargetVersions(
1315 android_version,
1316 android_branch,
1317 android_target,
1318 chrome_version,
1319 platform_version,
1320 milestone_version,
1321 full_version,
1322 )
Alex Klein68a28712021-11-08 11:08:30 -07001323
1324
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001325def determine_chrome_version(
Alex Klein1699fab2022-09-08 08:46:06 -06001326 build_target: "build_target_lib.BuildTarget",
1327) -> Optional[str]:
1328 """Returns the current Chrome version for the board (or in buildroot).
Michael Mortensenc2615b72019-10-15 08:12:24 -06001329
Alex Klein1699fab2022-09-08 08:46:06 -06001330 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001331 build_target: The board build target.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001332
Alex Klein1699fab2022-09-08 08:46:06 -06001333 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001334 The chrome version if available.
Alex Klein1699fab2022-09-08 08:46:06 -06001335 """
1336 # TODO(crbug/1019770): Long term we should not need the try/catch here once
1337 # the builds function above only returns True for chrome when
1338 # determine_chrome_version will succeed.
1339 try:
1340 pkg_info = portage_util.PortageqBestVisible(
1341 constants.CHROME_CP, build_target.name, cwd=constants.SOURCE_ROOT
1342 )
1343 except cros_build_lib.RunCommandError as e:
1344 # Return None because portage failed when trying to determine the chrome
1345 # version.
1346 logging.warning("Caught exception in determine_chrome_package: %s", e)
1347 return None
1348 # Something like 78.0.3877.4_rc -> 78.0.3877.4
1349 return pkg_info.version.partition("_")[0]
Michael Mortensenc2615b72019-10-15 08:12:24 -06001350
1351
Alex Klein68a28712021-11-08 11:08:30 -07001352@functools.lru_cache()
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001353def determine_android_package(board: str) -> Optional[str]:
Alex Klein1699fab2022-09-08 08:46:06 -06001354 """Returns the active Android container package in use by the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001355
Alex Klein1699fab2022-09-08 08:46:06 -06001356 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001357 board: The board name this is specific to.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001358
Alex Klein1699fab2022-09-08 08:46:06 -06001359 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001360 The android package string if there is one.
Alex Klein1699fab2022-09-08 08:46:06 -06001361 """
1362 try:
1363 packages = portage_util.GetPackageDependencies(
1364 "virtual/target-os", board=board
1365 )
1366 except cros_build_lib.RunCommandError as e:
1367 # Return None because a command (likely portage) failed when trying to
1368 # determine the package.
1369 logging.warning("Caught exception in determine_android_package: %s", e)
1370 return None
1371
1372 # We assume there is only one Android package in the depgraph.
1373 for package in packages:
1374 if package.startswith(
1375 "chromeos-base/android-container-"
1376 ) or package.startswith("chromeos-base/android-vm-"):
1377 return package
Michael Mortensene0f4b542019-10-24 15:30:23 -06001378 return None
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001379
1380
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001381def determine_android_version(board: str, package: str = None):
Alex Klein1699fab2022-09-08 08:46:06 -06001382 """Determine the current Android version in buildroot now and return it.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001383
Alex Klein1699fab2022-09-08 08:46:06 -06001384 This uses the typical portage logic to determine which version of Android
1385 is active right now in the buildroot.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001386
Alex Klein1699fab2022-09-08 08:46:06 -06001387 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001388 board: The board name this is specific to.
1389 package: The Android package, if already computed.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001390
Alex Klein1699fab2022-09-08 08:46:06 -06001391 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001392 The Android build ID of the container for the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001393
Alex Klein1699fab2022-09-08 08:46:06 -06001394 Raises:
Alex Klein348e7692022-10-13 17:03:37 -06001395 NoAndroidVersionError: if no unique Android version can be determined.
Alex Klein1699fab2022-09-08 08:46:06 -06001396 """
1397 if not package:
1398 package = determine_android_package(board)
1399 if not package:
1400 return None
1401 cpv = package_info.SplitCPV(package)
1402 if not cpv:
1403 raise NoAndroidVersionError(
1404 "Android version could not be determined for %s" % board
1405 )
1406 return cpv.version_no_rev
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001407
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001408
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001409def determine_android_branch(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001410 """Returns the Android branch in use by the active container ebuild."""
1411 if not package:
1412 package = determine_android_package(board)
1413 if not package:
1414 return None
1415 ebuild_path = portage_util.FindEbuildForBoardPackage(package, board)
1416 # We assume all targets pull from the same branch and that we always
1417 # have at least one of the following targets.
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +09001418 # TODO(b/187795671): Do this in a less hacky way.
1419 targets = android.GetAllAndroidEbuildTargets()
Alex Klein1699fab2022-09-08 08:46:06 -06001420 ebuild_content = osutils.SourceEnvironment(ebuild_path, targets)
1421 for target in targets:
1422 if target in ebuild_content:
1423 branch = re.search(r"(.*?)-linux-", ebuild_content[target])
1424 if branch is not None:
1425 return branch.group(1)
1426 raise NoAndroidBranchError(
1427 "Android branch could not be determined for %s (ebuild empty?)" % board
1428 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001429
1430
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001431def determine_android_target(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001432 """Returns the Android target in use by the active container ebuild."""
1433 if not package:
1434 package = determine_android_package(board)
1435 if not package:
1436 return None
1437 if package.startswith("chromeos-base/android-vm-"):
1438 return "bertha"
1439 elif package.startswith("chromeos-base/android-container-"):
1440 return "cheets"
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001441
Alex Klein1699fab2022-09-08 08:46:06 -06001442 raise NoAndroidTargetError(
1443 "Android Target cannot be determined for the package: %s" % package
1444 )
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001445
1446
1447def determine_platform_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001448 """Returns the platform version from the source root."""
1449 # Platform version is something like '12575.0.0'.
1450 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1451 return version.VersionString()
Michael Mortensen009cb662019-10-21 11:38:43 -06001452
1453
1454def determine_milestone_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001455 """Returns the platform version from the source root."""
1456 # Milestone version is something like '79'.
1457 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1458 return version.chrome_branch
Michael Mortensen009cb662019-10-21 11:38:43 -06001459
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001460
Michael Mortensen009cb662019-10-21 11:38:43 -06001461def determine_full_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001462 """Returns the full version from the source root."""
1463 # Full version is something like 'R79-12575.0.0'.
1464 milestone_version = determine_milestone_version()
1465 platform_version = determine_platform_version()
1466 full_version = "R%s-%s" % (milestone_version, platform_version)
1467 return full_version
Michael Mortensen71ef5682020-05-07 14:29:24 -06001468
1469
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001470def find_fingerprints(
Alex Klein1699fab2022-09-08 08:46:06 -06001471 build_target: "build_target_lib.BuildTarget",
1472) -> List[str]:
1473 """Returns a list of fingerprints for this build.
Michael Mortensende716a12020-05-15 11:27:00 -06001474
Alex Klein1699fab2022-09-08 08:46:06 -06001475 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001476 build_target: The build target.
Michael Mortensende716a12020-05-15 11:27:00 -06001477
Alex Klein1699fab2022-09-08 08:46:06 -06001478 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001479 List of fingerprint strings.
Alex Klein1699fab2022-09-08 08:46:06 -06001480 """
1481 cros_build_lib.AssertInsideChroot()
1482 fp_file = "cheets-fingerprint.txt"
1483 fp_path = os.path.join(
1484 image_lib.GetLatestImageLink(build_target.name), fp_file
1485 )
1486 if not os.path.isfile(fp_path):
1487 logging.info("Fingerprint file not found: %s", fp_path)
1488 return []
1489 logging.info("Reading fingerprint file: %s", fp_path)
1490 fingerprints = osutils.ReadFile(fp_path).splitlines()
1491 return fingerprints
Michael Mortensende716a12020-05-15 11:27:00 -06001492
1493
Alex Klein1699fab2022-09-08 08:46:06 -06001494def get_all_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1495 """Extract firmware version for all models present.
Michael Mortensen59e30872020-05-18 14:12:49 -06001496
Alex Klein1699fab2022-09-08 08:46:06 -06001497 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001498 build_target: The build target.
Michael Mortensen59e30872020-05-18 14:12:49 -06001499
Alex Klein1699fab2022-09-08 08:46:06 -06001500 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001501 A dict of FirmwareVersions namedtuple instances by model.
1502 Each element will be populated based on whether it was present in the
1503 command output.
Alex Klein1699fab2022-09-08 08:46:06 -06001504 """
1505 cros_build_lib.AssertInsideChroot()
1506 result = {}
1507 # Note that example output for _get_firmware_version_cmd_result is available
1508 # in the packages_unittest.py for testing get_all_firmware_versions.
1509 cmd_result = _get_firmware_version_cmd_result(build_target)
Michael Mortensen59e30872020-05-18 14:12:49 -06001510
Alex Klein1699fab2022-09-08 08:46:06 -06001511 if cmd_result:
1512 # There is a blank line between the version info for each model.
1513 firmware_version_payloads = cmd_result.split("\n\n")
1514 for firmware_version_payload in firmware_version_payloads:
1515 if "BIOS" in firmware_version_payload:
1516 firmware_version = _find_firmware_versions(
1517 firmware_version_payload
1518 )
1519 result[firmware_version.model] = firmware_version
1520 return result
Michael Mortensen59e30872020-05-18 14:12:49 -06001521
1522
Benjamin Shai0858cd32022-01-10 20:23:49 +00001523class FirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001524 """Tuple to hold firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001525
Alex Klein1699fab2022-09-08 08:46:06 -06001526 model: Optional[str]
1527 main: Optional[str]
1528 main_rw: Optional[str]
1529 ec: Optional[str]
1530 ec_rw: Optional[str]
1531
1532 def __bool__(self):
1533 return bool(
1534 self.model or self.main or self.main_rw or self.ec or self.ec_rw
1535 )
Michael Mortensen71ef5682020-05-07 14:29:24 -06001536
1537
Alex Klein1699fab2022-09-08 08:46:06 -06001538def get_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1539 """Extract version information from the firmware updater, if one exists.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001540
Alex Klein1699fab2022-09-08 08:46:06 -06001541 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001542 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001543
Alex Klein1699fab2022-09-08 08:46:06 -06001544 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001545 A FirmwareVersions namedtuple instance.
1546 Each element will either be set to the string output by the firmware
1547 updater shellball, or None if there is no firmware updater.
Alex Klein1699fab2022-09-08 08:46:06 -06001548 """
1549 cros_build_lib.AssertInsideChroot()
1550 cmd_result = _get_firmware_version_cmd_result(build_target)
1551 if cmd_result:
1552 return _find_firmware_versions(cmd_result)
1553 else:
1554 return FirmwareVersions(None, None, None, None, None)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001555
1556
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001557def _get_firmware_version_cmd_result(
Alex Klein1699fab2022-09-08 08:46:06 -06001558 build_target: "build_target_lib.BuildTarget",
1559) -> Optional[str]:
1560 """Gets the raw result output of the firmware updater version command.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001561
Alex Klein1699fab2022-09-08 08:46:06 -06001562 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001563 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001564
Alex Klein1699fab2022-09-08 08:46:06 -06001565 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001566 Command execution result.
Alex Klein1699fab2022-09-08 08:46:06 -06001567 """
1568 updater = os.path.join(
1569 build_target.root, "usr/sbin/chromeos-firmwareupdate"
1570 )
1571 logging.info("Calling updater %s", updater)
1572 # Call the updater using the chroot-based path.
1573 try:
1574 return cros_build_lib.run(
1575 [updater, "-V"],
1576 capture_output=True,
1577 log_output=True,
1578 encoding="utf-8",
1579 ).stdout
1580 except cros_build_lib.RunCommandError:
1581 # Updater probably doesn't exist (e.g. betty).
1582 return None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001583
1584
1585def _find_firmware_versions(cmd_output):
Alex Klein1699fab2022-09-08 08:46:06 -06001586 """Finds firmware version output via regex matches against the cmd_output.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001587
Alex Klein1699fab2022-09-08 08:46:06 -06001588 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001589 cmd_output: The raw output to search against.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001590
Alex Klein1699fab2022-09-08 08:46:06 -06001591 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001592 FirmwareVersions namedtuple with results.
1593 Each element will either be set to the string output by the firmware
1594 updater shellball, or None if there is no match.
Alex Klein1699fab2022-09-08 08:46:06 -06001595 """
Michael Mortensen71ef5682020-05-07 14:29:24 -06001596
Alex Klein1699fab2022-09-08 08:46:06 -06001597 # Sometimes a firmware bundle includes a special combination of RO+RW
1598 # firmware. In this case, the RW firmware version is indicated with a "(RW)
1599 # version" field. In other cases, the "(RW) version" field is not present.
1600 # Therefore, search for the "(RW)" fields first and if they aren't present,
1601 # fallback to the other format. e.g. just "BIOS version:".
1602 # TODO(mmortensen): Use JSON once the firmware updater supports it.
1603 main = None
1604 main_rw = None
1605 ec = None
1606 ec_rw = None
1607 model = None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001608
Alex Klein1699fab2022-09-08 08:46:06 -06001609 match = re.search(r"BIOS version:\s*(?P<version>.*)", cmd_output)
1610 if match:
1611 main = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001612
Alex Klein1699fab2022-09-08 08:46:06 -06001613 match = re.search(r"BIOS \(RW\) version:\s*(?P<version>.*)", cmd_output)
1614 if match:
1615 main_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001616
Alex Klein1699fab2022-09-08 08:46:06 -06001617 match = re.search(r"EC version:\s*(?P<version>.*)", cmd_output)
1618 if match:
1619 ec = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001620
Alex Klein1699fab2022-09-08 08:46:06 -06001621 match = re.search(r"EC \(RW\) version:\s*(?P<version>.*)", cmd_output)
1622 if match:
1623 ec_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001624
Alex Klein1699fab2022-09-08 08:46:06 -06001625 match = re.search(r"Model:\s*(?P<model>.*)", cmd_output)
1626 if match:
1627 model = match.group("model")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001628
Alex Klein1699fab2022-09-08 08:46:06 -06001629 return FirmwareVersions(model, main, main_rw, ec, ec_rw)
Michael Mortensena4af79e2020-05-06 16:18:48 -06001630
1631
Benjamin Shai0858cd32022-01-10 20:23:49 +00001632class MainEcFirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001633 """Tuple to hold main and ec firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001634
Alex Klein1699fab2022-09-08 08:46:06 -06001635 main_fw_version: Optional[str]
1636 ec_fw_version: Optional[str]
1637
1638 def __bool__(self):
1639 return bool(self.main_fw_version or self.ec_fw_version)
Benjamin Shai0858cd32022-01-10 20:23:49 +00001640
Michael Mortensena4af79e2020-05-06 16:18:48 -06001641
Alex Klein1699fab2022-09-08 08:46:06 -06001642def determine_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1643 """Returns a namedtuple with main and ec firmware versions.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001644
Alex Klein1699fab2022-09-08 08:46:06 -06001645 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001646 build_target: The build target.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001647
Alex Klein1699fab2022-09-08 08:46:06 -06001648 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001649 MainEcFirmwareVersions namedtuple with results.
Alex Klein1699fab2022-09-08 08:46:06 -06001650 """
1651 fw_versions = get_firmware_versions(build_target)
1652 main_fw_version = fw_versions.main_rw or fw_versions.main
1653 ec_fw_version = fw_versions.ec_rw or fw_versions.ec
Michael Mortensena4af79e2020-05-06 16:18:48 -06001654
Alex Klein1699fab2022-09-08 08:46:06 -06001655 return MainEcFirmwareVersions(main_fw_version, ec_fw_version)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001656
Benjamin Shai0858cd32022-01-10 20:23:49 +00001657
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001658def determine_kernel_version(
Alex Klein1699fab2022-09-08 08:46:06 -06001659 build_target: "build_target_lib.BuildTarget",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001660) -> str:
Alex Klein1699fab2022-09-08 08:46:06 -06001661 """Returns a string containing the kernel version for this build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001662
Alex Klein1699fab2022-09-08 08:46:06 -06001663 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001664 build_target: The build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001665
Alex Klein1699fab2022-09-08 08:46:06 -06001666 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001667 The kernel versions, or empty string.
Alex Klein1699fab2022-09-08 08:46:06 -06001668 """
Lizzy Presland0b978e62022-09-09 16:55:29 +00001669 target_virtual_pkg = "virtual/linux-sources"
Alex Klein1699fab2022-09-08 08:46:06 -06001670 try:
Lizzy Presland0b978e62022-09-09 16:55:29 +00001671 candidate_packages = portage_util.GetFlattenedDepsForPackage(
1672 target_virtual_pkg,
1673 sysroot=build_target.root,
1674 board=build_target.name,
1675 depth=1,
1676 )
1677 installed_packages = portage_util.GetPackageDependencies(
1678 target_virtual_pkg, board=build_target.name
Alex Klein1699fab2022-09-08 08:46:06 -06001679 )
1680 except cros_build_lib.RunCommandError as e:
1681 logging.warning("Unable to get package list for metadata: %s", e)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001682 return ""
1683 if not candidate_packages:
1684 raise KernelVersionError("No package found in FlattenedDepsForPackage")
1685 if not installed_packages:
1686 raise KernelVersionError("No package found in GetPackageDependencies")
1687 packages = [
1688 p
1689 for p in installed_packages
1690 if p in candidate_packages and target_virtual_pkg not in p
1691 ]
1692 if len(packages) == 0:
1693 raise KernelVersionError(
1694 "No matches for installed packages were found in candidate "
1695 "packages. Did GetFlattenedDepsForPackage search all possible "
1696 "package versions?\tInstalled: %s\tCandidates: %s"
1697 % (" ".join(installed_packages), " ".join(candidate_packages))
1698 )
1699 if len(packages) > 1:
1700 raise KernelVersionError(
1701 "Too many packages found in intersection of installed packages and "
1702 "possible kernel versions (%s)" % "".join(packages)
1703 )
1704 kernel_version = package_info.SplitCPV(packages[0]).version
1705 logging.info("Found active kernel version: %s", kernel_version)
1706 return kernel_version
Michael Mortensen125bb012020-05-21 14:02:10 -06001707
1708
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001709def get_models(
Alex Klein1699fab2022-09-08 08:46:06 -06001710 build_target: "build_target_lib.BuildTarget", log_output: bool = True
1711) -> Optional[List[str]]:
1712 """Obtain a list of models supported by a unified board.
Michael Mortensen125bb012020-05-21 14:02:10 -06001713
Alex Klein1699fab2022-09-08 08:46:06 -06001714 This ignored whitelabel models since GoldenEye has no specific support for
1715 these at present.
Michael Mortensen125bb012020-05-21 14:02:10 -06001716
Alex Klein1699fab2022-09-08 08:46:06 -06001717 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001718 build_target: The build target.
1719 log_output: Whether to log the output of the cros_config_host
1720 invocation.
Michael Mortensen125bb012020-05-21 14:02:10 -06001721
Alex Klein1699fab2022-09-08 08:46:06 -06001722 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001723 A list of models supported by this board, if it is a unified build;
1724 None, if it is not a unified build.
Alex Klein1699fab2022-09-08 08:46:06 -06001725 """
1726 return _run_cros_config_host(
1727 build_target, ["list-models"], log_output=log_output
1728 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001729
1730
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001731def get_key_id(
Alex Klein1699fab2022-09-08 08:46:06 -06001732 build_target: "build_target_lib.BuildTarget", model: str
1733) -> Optional[str]:
1734 """Obtain the key_id for a model within the build_target.
Michael Mortensen359c1f32020-05-28 19:35:42 -06001735
Alex Klein1699fab2022-09-08 08:46:06 -06001736 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001737 build_target: The build target.
1738 model: The model name
Michael Mortensen359c1f32020-05-28 19:35:42 -06001739
Alex Klein1699fab2022-09-08 08:46:06 -06001740 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001741 A key_id or None.
Alex Klein1699fab2022-09-08 08:46:06 -06001742 """
1743 model_arg = "--model=" + model
1744 key_id_list = _run_cros_config_host(
1745 build_target, [model_arg, "get", "/firmware-signing", "key-id"]
1746 )
1747 key_id = None
1748 if len(key_id_list) == 1:
1749 key_id = key_id_list[0]
1750 return key_id
Michael Mortensen359c1f32020-05-28 19:35:42 -06001751
1752
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001753def _run_cros_config_host(
Alex Klein1699fab2022-09-08 08:46:06 -06001754 build_target: "build_target_lib.BuildTarget",
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001755 args: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -06001756 log_output: bool = True,
1757) -> Optional[List[str]]:
1758 """Run the cros_config_host tool.
Michael Mortensen125bb012020-05-21 14:02:10 -06001759
Alex Klein1699fab2022-09-08 08:46:06 -06001760 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001761 build_target: The build target.
1762 args: List of arguments to pass.
1763 log_output: Whether to log the output of the cros_config_host.
Michael Mortensen125bb012020-05-21 14:02:10 -06001764
Alex Klein1699fab2022-09-08 08:46:06 -06001765 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001766 Output of the tool
Alex Klein1699fab2022-09-08 08:46:06 -06001767 """
1768 cros_build_lib.AssertInsideChroot()
1769 tool = "/usr/bin/cros_config_host"
1770 if not os.path.isfile(tool):
1771 return None
Michael Mortensen125bb012020-05-21 14:02:10 -06001772
Alex Klein1699fab2022-09-08 08:46:06 -06001773 config_fname = build_target.full_path(
1774 "usr/share/chromeos-config/yaml/config.yaml"
1775 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001776
Alex Klein1699fab2022-09-08 08:46:06 -06001777 result = cros_build_lib.run(
1778 [tool, "-c", config_fname] + args,
1779 capture_output=True,
1780 encoding="utf-8",
1781 log_output=log_output,
1782 check=False,
1783 )
1784 if result.returncode:
1785 # Show the output for debugging purposes.
1786 if "No such file or directory" not in result.stderr:
1787 logging.error("cros_config_host failed: %s\n", result.stderr)
1788 return None
1789 return result.stdout.strip().splitlines()