blob: ead57ed4b0bab735dc5811191787e94be54884cb [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
Alex Kleinfee86da2023-01-20 18:40:06 -0700212 # output is included (e.g. messages from bashrc scripts that run upon
213 # entering the chroot.)
Alex Klein1699fab2022-09-08 08:46:06 -0600214 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 Kleinfee86da2023-01-20 18:40:06 -0700256 PUpr for Android does not test the uprev change in CQ; instead we run
257 separate jobs to test new Android versions, and we write the latest vetted
258 version to 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)
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900270 android_version = android.ReadLKGB(android_package_dir)["build_id"]
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
Alex Kleinfee86da2023-01-20 18:40:06 -0700283 # cros_mark_android_as_stable returns paths relative to
284 # |android.OVERLAY_DIR|.
Alex Klein1699fab2022-09-08 08:46:06 -0600285 result.add_result(
286 android_version,
287 [
288 os.path.join(android.OVERLAY_DIR, f)
289 for f in uprev_result.modified_files
290 ],
291 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900292 return result
293
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900294
295def define_uprev_android_lkgb_handlers():
Alex Klein1699fab2022-09-08 08:46:06 -0600296 """Dynamically define uprev handlers for each Android package"""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900297
Alex Klein1699fab2022-09-08 08:46:06 -0600298 def define_handler(android_package):
299 """Defines the uprev handler for an Android package."""
300 full_package_name = "chromeos-base/" + android_package
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900301
Alex Klein1699fab2022-09-08 08:46:06 -0600302 @uprevs_versioned_package(full_package_name)
303 def _handler(build_targets, _refs, chroot):
304 return uprev_android_lkgb(android_package, build_targets, chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900305
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900306 for android_package in android.GetAllAndroidPackages():
Alex Klein1699fab2022-09-08 08:46:06 -0600307 define_handler(android_package)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900308
309
310define_uprev_android_lkgb_handlers()
Alex Klein4de25e82019-08-05 15:58:39 -0600311
312
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700313def uprev_build_targets(
Alex Klein1699fab2022-09-08 08:46:06 -0600314 build_targets: Optional[List["build_target_lib.BuildTarget"]],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700315 overlay_type: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600316 chroot: "chroot_lib.Chroot" = None,
317 output_dir: Optional[str] = None,
318):
319 """Uprev the set provided build targets, or all if not specified.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600320
Alex Klein1699fab2022-09-08 08:46:06 -0600321 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600322 build_targets: The build targets whose overlays should be uprevved,
323 empty or None for all.
324 overlay_type: One of the valid overlay types except None (see
325 constants.VALID_OVERLAYS).
326 chroot: The chroot to clean, if desired.
327 output_dir: The path to optionally dump result files.
Alex Klein1699fab2022-09-08 08:46:06 -0600328 """
329 # Need a valid overlay, but exclude None.
330 assert overlay_type and overlay_type in constants.VALID_OVERLAYS
Alex Kleineb77ffa2019-05-28 14:47:44 -0600331
Alex Klein1699fab2022-09-08 08:46:06 -0600332 if build_targets:
333 overlays = portage_util.FindOverlaysForBoards(
334 overlay_type, boards=[t.name for t in build_targets]
335 )
336 else:
337 overlays = portage_util.FindOverlays(overlay_type)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600338
Alex Klein1699fab2022-09-08 08:46:06 -0600339 return uprev_overlays(
340 overlays,
341 build_targets=build_targets,
342 chroot=chroot,
343 output_dir=output_dir,
344 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600345
346
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700347def uprev_overlays(
348 overlays: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -0600349 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
350 chroot: Optional["chroot_lib.Chroot"] = None,
351 output_dir: Optional[str] = None,
352) -> List[str]:
353 """Uprev the given overlays.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600354
Alex Klein1699fab2022-09-08 08:46:06 -0600355 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600356 overlays: The list of overlay paths.
357 build_targets: The build targets to clean in |chroot|, if desired. No
358 effect unless |chroot| is provided.
359 chroot: The chroot to clean, if desired.
360 output_dir: The path to optionally dump result files.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600361
Alex Klein1699fab2022-09-08 08:46:06 -0600362 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600363 The paths to all the modified ebuild files. This includes the new files
364 that were added (i.e. the new versions) and all the removed files
Alex Klein1699fab2022-09-08 08:46:06 -0600365 (i.e. the old versions).
366 """
367 assert overlays
Alex Kleineb77ffa2019-05-28 14:47:44 -0600368
Alex Klein1699fab2022-09-08 08:46:06 -0600369 manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600370
Alex Klein1699fab2022-09-08 08:46:06 -0600371 uprev_manager = uprev_lib.UprevOverlayManager(
372 overlays,
373 manifest,
374 build_targets=build_targets,
375 chroot=chroot,
376 output_dir=output_dir,
377 )
378 uprev_manager.uprev()
Alex Kleineb77ffa2019-05-28 14:47:44 -0600379
Alex Klein1699fab2022-09-08 08:46:06 -0600380 return uprev_manager.modified_ebuilds, uprev_manager.revved_packages
Alex Kleineb77ffa2019-05-28 14:47:44 -0600381
382
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700383def uprev_versioned_package(
384 package: package_info.CPV,
Alex Klein1699fab2022-09-08 08:46:06 -0600385 build_targets: List["build_target_lib.BuildTarget"],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700386 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600387 chroot: "chroot_lib.Chroot",
388) -> "uprev_lib.UprevVersionedPackageResult":
389 """Call registered uprev handler function for the package.
Alex Klein87531182019-08-12 15:23:37 -0600390
Alex Klein1699fab2022-09-08 08:46:06 -0600391 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600392 package: The package being uprevved.
393 build_targets: The build targets to clean on a successful uprev.
394 refs:
395 chroot: The chroot to enter for cleaning.
Alex Klein87531182019-08-12 15:23:37 -0600396
Alex Klein1699fab2022-09-08 08:46:06 -0600397 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600398 The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600399 """
400 assert package
Alex Klein87531182019-08-12 15:23:37 -0600401
Alex Klein1699fab2022-09-08 08:46:06 -0600402 if package.cp not in _UPREV_FUNCS:
403 raise UnknownPackageError(
404 'Package "%s" does not have a registered handler.' % package.cp
405 )
Alex Klein87531182019-08-12 15:23:37 -0600406
Alex Klein1699fab2022-09-08 08:46:06 -0600407 return _UPREV_FUNCS[package.cp](build_targets, refs, chroot)
Alex Klein87531182019-08-12 15:23:37 -0600408
409
Alex Klein1699fab2022-09-08 08:46:06 -0600410@uprevs_versioned_package("media-libs/virglrenderer")
Navil Perezf57ba872020-06-04 22:38:37 +0000411def uprev_virglrenderer(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600412 """Updates virglrenderer ebuilds.
Navil Perezf57ba872020-06-04 22:38:37 +0000413
Alex Klein1699fab2022-09-08 08:46:06 -0600414 See: uprev_versioned_package.
Navil Perezf57ba872020-06-04 22:38:37 +0000415
Alex Klein1699fab2022-09-08 08:46:06 -0600416 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600417 UprevVersionedPackageResult: The result of updating virglrenderer
418 ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600419 """
420 overlay = os.path.join(
421 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
422 )
423 repo_path = os.path.join(
424 constants.SOURCE_ROOT, "src", "third_party", "virglrenderer"
425 )
426 manifest = git.ManifestCheckout.Cached(repo_path)
Navil Perezf57ba872020-06-04 22:38:37 +0000427
Alex Klein1699fab2022-09-08 08:46:06 -0600428 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
429 # TODO(crbug.com/1066242): Ebuilds for virglrenderer are currently
430 # denylisted. Do not force uprevs after builder is stable and ebuilds are no
431 # longer denylisted.
432 uprev_manager.uprev(package_list=["media-libs/virglrenderer"], force=True)
Navil Perezf57ba872020-06-04 22:38:37 +0000433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 updated_files = uprev_manager.modified_ebuilds
435 result = uprev_lib.UprevVersionedPackageResult()
436 result.add_result(refs[-1].revision, updated_files)
437 return result
Navil Perezf57ba872020-06-04 22:38:37 +0000438
Alex Klein1699fab2022-09-08 08:46:06 -0600439
Matthew Lam59ca37d2022-10-24 18:11:06 +0000440@uprevs_versioned_package("x11-apps/igt-gpu-tools")
441def uprev_igt_gpu_tools(_build_targets, refs, _chroot):
442 """Updates igt-gpu-tools ebuilds.
443
444 See: uprev_versioned_package.
445
446 Returns:
Alex Kleinfee86da2023-01-20 18:40:06 -0700447 UprevVersionedPackageResult: The result of updating igt-gpu-tools
448 ebuilds.
Matthew Lam59ca37d2022-10-24 18:11:06 +0000449 """
450 overlay = os.path.join(
451 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
452 )
453 repo_path = os.path.join(
454 constants.SOURCE_ROOT, "src", "third_party", "igt-gpu-tools"
455 )
456 manifest = git.ManifestCheckout.Cached(repo_path)
457
458 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
459 uprev_manager.uprev(package_list=["x11-apps/igt-gpu-tools"], force=True)
460
461 updated_files = uprev_manager.modified_ebuilds
462 result = uprev_lib.UprevVersionedPackageResult()
463 result.add_result(refs[-1].revision, updated_files)
464 return result
465
466
Alex Klein1699fab2022-09-08 08:46:06 -0600467@uprevs_versioned_package("chromeos-base/drivefs")
Jose Magana03b5a842020-08-19 12:52:59 +1000468def uprev_drivefs(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600469 """Updates drivefs ebuilds.
Jose Magana03b5a842020-08-19 12:52:59 +1000470
Alex Klein1699fab2022-09-08 08:46:06 -0600471 DriveFS versions follow the tag format of refs/tags/drivefs_1.2.3.
472 See: uprev_versioned_package.
Jose Magana03b5a842020-08-19 12:52:59 +1000473
Alex Klein1699fab2022-09-08 08:46:06 -0600474 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600475 UprevVersionedPackageResult: The result of updating drivefs ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600476 """
Jose Magana03b5a842020-08-19 12:52:59 +1000477
Alex Klein1699fab2022-09-08 08:46:06 -0600478 DRIVEFS_PATH_PREFIX = "src/private-overlays/chromeos-overlay/chromeos-base"
479 result = uprev_lib.UprevVersionedPackageResult()
480 all_changed_files = []
Jose Magana03b5a842020-08-19 12:52:59 +1000481
Alex Klein1699fab2022-09-08 08:46:06 -0600482 DRIVEFS_REFS_PREFIX = "refs/tags/drivefs_"
483 drivefs_version = _get_latest_version_from_refs(DRIVEFS_REFS_PREFIX, refs)
484 if not drivefs_version:
485 # No valid DriveFS version is identified.
486 return result
487
488 logging.debug("DriveFS version determined from refs: %s", drivefs_version)
489
490 # Attempt to uprev drivefs package.
491 pkg_path = os.path.join(DRIVEFS_PATH_PREFIX, "drivefs")
492 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
493 pkg_path, drivefs_version, chroot, allow_downrev=False
494 )
495
496 if not uprev_result:
497 return result
498 all_changed_files.extend(uprev_result.changed_files)
499 result.add_result(drivefs_version, all_changed_files)
500
Ben Reich4f3fa1b2020-12-19 08:21:26 +0000501 return result
Jose Magana03b5a842020-08-19 12:52:59 +1000502
Jose Magana03b5a842020-08-19 12:52:59 +1000503
Alex Klein1699fab2022-09-08 08:46:06 -0600504@uprevs_versioned_package("chromeos-base/perfetto")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800505def uprev_perfetto(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600506 """Updates Perfetto ebuilds.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800507
Alex Klein1699fab2022-09-08 08:46:06 -0600508 Perfetto versions follow the tag format of refs/tags/v1.2.
509 See: uprev_versioned_package.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800510
Alex Klein1699fab2022-09-08 08:46:06 -0600511 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600512 UprevVersionedPackageResult: The result of updating Perfetto ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600513 """
514 result = uprev_lib.UprevVersionedPackageResult()
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800515
Alex Klein1699fab2022-09-08 08:46:06 -0600516 PERFETTO_REFS_PREFIX = "refs/tags/v"
Chinglin Yuad12a512022-10-07 17:26:12 +0800517 PERFETTO_PATH = os.path.join(
518 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base/perfetto"
519 )
520
521 # Decide the version number to uprev to:
522 # * If |refs| contains refs/tags/v*, get the latest from them.
Alex Klein1699fab2022-09-08 08:46:06 -0600523 perfetto_version = _get_latest_version_from_refs(PERFETTO_REFS_PREFIX, refs)
Chinglin Yuad12a512022-10-07 17:26:12 +0800524 # * Or if |refs| contains only the latest trunk revisions, use the current
525 # stable ebuild version for a revision bump.
526 if refs and not perfetto_version:
527 perfetto_version = uprev_lib.get_stable_ebuild_version(PERFETTO_PATH)
528
Alex Klein1699fab2022-09-08 08:46:06 -0600529 if not perfetto_version:
530 # No valid Perfetto version is identified.
531 return result
532
Alex Klein1699fab2022-09-08 08:46:06 -0600533 # Attempt to uprev perfetto package.
Chinglin Yuad12a512022-10-07 17:26:12 +0800534 # |perfetto_version| is only used in determining the ebuild version. The
535 # package is always updated to the latest HEAD.
Alex Klein1699fab2022-09-08 08:46:06 -0600536 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
537 PERFETTO_PATH,
538 perfetto_version,
539 chroot,
540 allow_downrev=False,
Chinglin Yu84818732022-10-03 12:03:43 +0800541 # Use default ref="HEAD"
Alex Klein1699fab2022-09-08 08:46:06 -0600542 )
543
544 if not uprev_result:
545 return result
546
Chinglin Yu5de28a42022-11-11 19:52:21 +0800547 # Include short git sha hash in the uprev commit message.
548 # Use 9 digits to match the short hash length in `perfetto --version`.
549 short_revision = refs[-1].revision[0:9]
550 version_and_rev = f"{perfetto_version}-{short_revision}"
551 result.add_result(version_and_rev, uprev_result.changed_files)
Alex Klein1699fab2022-09-08 08:46:06 -0600552
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800553 return result
554
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800555
Denis Nikitin63613e32022-09-09 22:26:50 -0700556class AfdoMetadata(NamedTuple):
557 """Data class holding AFDO metadata."""
558
559 var_name: str
560 path: str
561
562
Alex Klein1699fab2022-09-08 08:46:06 -0600563@uprevs_versioned_package("afdo/kernel-profiles")
Yaakov Shaul395ae832019-09-09 14:45:32 -0600564def uprev_kernel_afdo(*_args, **_kwargs):
Alex Klein1699fab2022-09-08 08:46:06 -0600565 """Updates kernel ebuilds with versions from kernel_afdo.json.
Yaakov Shaul395ae832019-09-09 14:45:32 -0600566
Alex Klein1699fab2022-09-08 08:46:06 -0600567 See: uprev_versioned_package.
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600568
Alex Klein1699fab2022-09-08 08:46:06 -0600569 Raises:
Alex Klein348e7692022-10-13 17:03:37 -0600570 EbuildManifestError: When ebuild manifest does not complete
571 successfully.
572 JSONDecodeError: When json is malformed.
Alex Klein1699fab2022-09-08 08:46:06 -0600573 """
Denis Nikitin63613e32022-09-09 22:26:50 -0700574 metadata_dir = os.path.join(
Alex Klein1699fab2022-09-08 08:46:06 -0600575 constants.SOURCE_ROOT,
576 "src",
577 "third_party",
578 "toolchain-utils",
579 "afdo_metadata",
Denis Nikitin63613e32022-09-09 22:26:50 -0700580 )
581 metadata_files = (
582 AfdoMetadata(
583 var_name="AFDO_PROFILE_VERSION",
584 path=os.path.join(metadata_dir, "kernel_afdo.json"),
585 ),
586 AfdoMetadata(
587 var_name="ARM_AFDO_PROFILE_VERSION",
588 path=os.path.join(metadata_dir, "kernel_arm_afdo.json"),
589 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600590 )
Yaakov Shaul395ae832019-09-09 14:45:32 -0600591
Alex Klein1699fab2022-09-08 08:46:06 -0600592 result = uprev_lib.UprevVersionedPackageResult()
Denis Nikitin63613e32022-09-09 22:26:50 -0700593 for metadata in metadata_files:
594 with open(metadata.path, "r") as f:
595 versions = json.load(f)
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600596
Denis Nikitin63613e32022-09-09 22:26:50 -0700597 for kernel_pkg, version_info in versions.items():
598 path = os.path.join(
599 constants.CHROMIUMOS_OVERLAY_DIR, "sys-kernel", kernel_pkg
600 )
601 ebuild_path = os.path.join(
602 constants.SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
603 )
604 chroot_ebuild_path = os.path.join(
605 constants.CHROOT_SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
606 )
607 afdo_profile_version = version_info["name"]
608 patch_ebuild_vars(
609 ebuild_path, {metadata.var_name: afdo_profile_version}
Alex Klein1699fab2022-09-08 08:46:06 -0600610 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600611
Denis Nikitin63613e32022-09-09 22:26:50 -0700612 try:
613 cmd = ["ebuild", chroot_ebuild_path, "manifest", "--force"]
614 cros_build_lib.run(cmd, enter_chroot=True)
615 except cros_build_lib.RunCommandError as e:
616 raise uprev_lib.EbuildManifestError(
617 "Error encountered when regenerating the manifest for "
618 f"ebuild: {chroot_ebuild_path}\n{e}",
619 e,
620 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600621
Denis Nikitin63613e32022-09-09 22:26:50 -0700622 manifest_path = os.path.join(
623 constants.SOURCE_ROOT, path, "Manifest"
624 )
625 result.add_result(
626 afdo_profile_version, [ebuild_path, manifest_path]
627 )
Yaakov Shaul730814a2019-09-10 13:58:25 -0600628
Alex Klein1699fab2022-09-08 08:46:06 -0600629 return result
Yaakov Shaul395ae832019-09-09 14:45:32 -0600630
631
Alex Klein1699fab2022-09-08 08:46:06 -0600632@uprevs_versioned_package("chromeos-base/termina-dlc")
633@uprevs_versioned_package("chromeos-base/termina-tools-dlc")
Maciek Swiech6b12f662022-01-25 16:51:19 +0000634def uprev_termina_dlcs(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600635 """Updates shared termina-dlc and termina-tools-dlc ebuilds.
Maciek Swiech6b12f662022-01-25 16:51:19 +0000636
Alex Klein1699fab2022-09-08 08:46:06 -0600637 termina-dlc - chromeos-base/termina-dlc
638 termina-tools-dlc - chromeos-base/termina-tools-dlc
Trent Beginaf51f1b2020-03-09 17:35:31 -0600639
Alex Klein1699fab2022-09-08 08:46:06 -0600640 See: uprev_versioned_package.
641 """
642 termina_dlc_pkg = "termina-dlc"
643 termina_dlc_pkg_path = os.path.join(
644 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", termina_dlc_pkg
645 )
646 tools_dlc_pkg = "termina-tools-dlc"
647 tools_dlc_pkg_path = os.path.join(
648 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", tools_dlc_pkg
649 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000650
Alex Klein1699fab2022-09-08 08:46:06 -0600651 # termina-dlc and termina-tools-dlc are pinned to the same version.
652 version_pin_src_path = _get_version_pin_src_path(termina_dlc_pkg_path)
653 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Patrick Meiring5897add2020-09-16 16:30:17 +1000654
Alex Klein1699fab2022-09-08 08:46:06 -0600655 result = uprev_lib.uprev_ebuild_from_pin(
656 termina_dlc_pkg_path, version_no_rev, chroot
657 )
658 result += uprev_lib.uprev_ebuild_from_pin(
659 tools_dlc_pkg_path, version_no_rev, chroot
660 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000661
Alex Klein1699fab2022-09-08 08:46:06 -0600662 return result
Patrick Meiring5897add2020-09-16 16:30:17 +1000663
Alex Klein1699fab2022-09-08 08:46:06 -0600664
665@uprevs_versioned_package("chromeos-base/chromeos-lacros")
Julio Hurtadof1befec2021-05-05 21:34:26 +0000666def uprev_lacros(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600667 """Updates lacros ebuilds.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000668
Alex Klein1699fab2022-09-08 08:46:06 -0600669 Version to uprev to is gathered from the QA qualified version tracking file
Alex Kleinfee86da2023-01-20 18:40:06 -0700670 stored in chromium/src/chrome/LACROS_QA_QUALIFIED_VERSION. Uprev is
671 triggered on modification of this file across all chromium/src branches.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000672
Alex Klein1699fab2022-09-08 08:46:06 -0600673 See: uprev_versioned_package.
674 """
675 result = uprev_lib.UprevVersionedPackageResult()
676 path = os.path.join(
677 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
678 )
679 lacros_version = refs[0].revision
680 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
681 path, lacros_version, chroot, allow_downrev=False
682 )
Julio Hurtadoa994e002021-07-07 17:57:45 +0000683
Alex Klein1699fab2022-09-08 08:46:06 -0600684 if not uprev_result:
685 return result
686
687 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtadoa994e002021-07-07 17:57:45 +0000688 return result
689
Julio Hurtadof1befec2021-05-05 21:34:26 +0000690
Alex Klein1699fab2022-09-08 08:46:06 -0600691@uprevs_versioned_package("chromeos-base/chromeos-lacros-parallel")
Julio Hurtado870ed322021-12-03 18:22:40 +0000692def uprev_lacros_in_parallel(
Alex Klein1699fab2022-09-08 08:46:06 -0600693 _build_targets: Optional[List["build_target_lib.BuildTarget"]],
Julio Hurtado870ed322021-12-03 18:22:40 +0000694 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600695 chroot: "chroot_lib.Chroot",
696) -> "uprev_lib.UprevVersionedPackageResult":
697 """Updates lacros ebuilds in parallel with ash-chrome.
Julio Hurtado870ed322021-12-03 18:22:40 +0000698
Alex Kleinfee86da2023-01-20 18:40:06 -0700699 This handler is going to be used temporarily while lacros transitions to
700 being uprevved atomically with ash-chrome. Unlike a standalone lacros uprev,
701 this handler will not need to look at the QA qualified file. Rather, it will
Alex Klein1699fab2022-09-08 08:46:06 -0600702 function identical to ash-chrome using git tags.
Julio Hurtado870ed322021-12-03 18:22:40 +0000703
Alex Klein1699fab2022-09-08 08:46:06 -0600704 See: uprev_versioned_package.
Julio Hurtado870ed322021-12-03 18:22:40 +0000705
Alex Klein1699fab2022-09-08 08:46:06 -0600706 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600707 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600708 """
709 result = uprev_lib.UprevVersionedPackageResult()
710 path = os.path.join(
711 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
712 )
713 lacros_version = uprev_lib.get_version_from_refs(refs)
714 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
715 path, lacros_version, chroot, allow_downrev=False
716 )
Julio Hurtado870ed322021-12-03 18:22:40 +0000717
Alex Klein1699fab2022-09-08 08:46:06 -0600718 if not uprev_result:
719 return result
720
721 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtado870ed322021-12-03 18:22:40 +0000722 return result
723
Julio Hurtado870ed322021-12-03 18:22:40 +0000724
Alex Klein1699fab2022-09-08 08:46:06 -0600725@uprevs_versioned_package("app-emulation/parallels-desktop")
Patrick Meiring5897add2020-09-16 16:30:17 +1000726def uprev_parallels_desktop(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600727 """Updates Parallels Desktop ebuild - app-emulation/parallels-desktop.
Patrick Meiring5897add2020-09-16 16:30:17 +1000728
Alex Klein1699fab2022-09-08 08:46:06 -0600729 See: uprev_versioned_package
Patrick Meiring5897add2020-09-16 16:30:17 +1000730
Alex Klein1699fab2022-09-08 08:46:06 -0600731 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600732 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600733 """
734 package = "parallels-desktop"
735 package_path = os.path.join(
736 constants.CHROMEOS_PARTNER_OVERLAY_DIR, "app-emulation", package
737 )
738 version_pin_src_path = _get_version_pin_src_path(package_path)
Patrick Meiring5897add2020-09-16 16:30:17 +1000739
Alex Klein1699fab2022-09-08 08:46:06 -0600740 # Expect a JSON blob like the following:
741 # {
742 # "version": "1.2.3",
743 # "test_image": { "url": "...", "size": 12345678,
744 # "sha256sum": "<32 bytes of hexadecimal>" }
745 # }
746 with open(version_pin_src_path, "r") as f:
747 pinned = json.load(f)
Patrick Meiring5897add2020-09-16 16:30:17 +1000748
Alex Klein1699fab2022-09-08 08:46:06 -0600749 if "version" not in pinned or "test_image" not in pinned:
750 raise UprevError(
751 "VERSION-PIN for %s missing version and/or "
752 "test_image field" % package
753 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000754
Alex Klein1699fab2022-09-08 08:46:06 -0600755 version = pinned["version"]
756 if not isinstance(version, str):
757 raise UprevError("version in VERSION-PIN for %s not a string" % package)
Patrick Meiring5897add2020-09-16 16:30:17 +1000758
Alex Klein1699fab2022-09-08 08:46:06 -0600759 # Update the ebuild.
760 result = uprev_lib.uprev_ebuild_from_pin(package_path, version, chroot)
Patrick Meiring5897add2020-09-16 16:30:17 +1000761
Alex Klein1699fab2022-09-08 08:46:06 -0600762 # Update the VM image used for testing.
763 test_image_path = (
764 "src/platform/tast-tests-private/src/chromiumos/tast/"
765 "local/bundles/crosint/pita/data/"
766 "pluginvm_image.zip.external"
767 )
768 test_image_src_path = os.path.join(constants.SOURCE_ROOT, test_image_path)
769 with open(test_image_src_path, "w") as f:
770 json.dump(pinned["test_image"], f, indent=2)
771 result.add_result(version, [test_image_src_path])
Patrick Meiring5897add2020-09-16 16:30:17 +1000772
Alex Klein1699fab2022-09-08 08:46:06 -0600773 return result
Trent Beginaf51f1b2020-03-09 17:35:31 -0600774
775
Alex Klein1699fab2022-09-08 08:46:06 -0600776@uprevs_versioned_package("chromeos-base/chromeos-dtc-vm")
Trent Beginaf51f1b2020-03-09 17:35:31 -0600777def uprev_sludge(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600778 """Updates sludge VM - chromeos-base/chromeos-dtc-vm.
Trent Begin315d9d92019-12-03 21:55:53 -0700779
Alex Klein1699fab2022-09-08 08:46:06 -0600780 See: uprev_versioned_package.
781 """
782 package = "chromeos-dtc-vm"
783 package_path = os.path.join(
784 "src",
785 "private-overlays",
786 "project-wilco-private",
787 "chromeos-base",
788 package,
789 )
790 version_pin_src_path = _get_version_pin_src_path(package_path)
791 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Trent Begin315d9d92019-12-03 21:55:53 -0700792
Alex Klein1699fab2022-09-08 08:46:06 -0600793 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
Trent Begin315d9d92019-12-03 21:55:53 -0700794
795
Alex Klein1699fab2022-09-08 08:46:06 -0600796@uprevs_versioned_package("chromeos-base/borealis-dlc")
David Riley8513c1f2021-10-14 17:07:41 -0700797def uprev_borealis_dlc(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600798 """Updates shared borealis-dlc ebuild - chromeos-base/borealis-dlc.
David Riley8513c1f2021-10-14 17:07:41 -0700799
Alex Klein1699fab2022-09-08 08:46:06 -0600800 See: uprev_versioned_package.
801 """
802 package_path = os.path.join(
803 "src",
804 "private-overlays",
805 "chromeos-partner-overlay",
806 "chromeos-base",
807 "borealis-dlc",
808 )
David Riley8513c1f2021-10-14 17:07:41 -0700809
Alex Klein1699fab2022-09-08 08:46:06 -0600810 version_pin_src_path = _get_version_pin_src_path(package_path)
811 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
David Riley8513c1f2021-10-14 17:07:41 -0700812
Alex Klein1699fab2022-09-08 08:46:06 -0600813 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
David Riley8513c1f2021-10-14 17:07:41 -0700814
815
Patrick Meiring5897add2020-09-16 16:30:17 +1000816def _get_version_pin_src_path(package_path):
Alex Klein1699fab2022-09-08 08:46:06 -0600817 """Returns the path to the VERSION-PIN file for the given package."""
818 return os.path.join(constants.SOURCE_ROOT, package_path, "VERSION-PIN")
Patrick Meiring5897add2020-09-16 16:30:17 +1000819
820
Alex Klein87531182019-08-12 15:23:37 -0600821@uprevs_versioned_package(constants.CHROME_CP)
Alex Klein4e839252022-01-06 13:29:18 -0700822def uprev_chrome_from_ref(build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600823 """Uprev chrome and its related packages.
Alex Klein87531182019-08-12 15:23:37 -0600824
Alex Klein1699fab2022-09-08 08:46:06 -0600825 See: uprev_versioned_package.
826 """
Alex Kleinfee86da2023-01-20 18:40:06 -0700827 # Determine the version from the refs (tags), i.e. the chrome versions are
828 # the tag names.
Alex Klein1699fab2022-09-08 08:46:06 -0600829 chrome_version = uprev_lib.get_version_from_refs(refs)
830 logging.debug("Chrome version determined from refs: %s", chrome_version)
Alex Klein87531182019-08-12 15:23:37 -0600831
Alex Klein1699fab2022-09-08 08:46:06 -0600832 return uprev_chrome(chrome_version, build_targets, None)
Alex Kleinf69bd802021-06-22 15:43:49 -0600833
834
Alex Klein9ce3f682021-06-23 15:06:44 -0600835def revbump_chrome(
Alex Klein1699fab2022-09-08 08:46:06 -0600836 build_targets: List["build_target_lib.BuildTarget"] = None,
837 chroot: Optional["chroot_lib.Chroot"] = None,
Alex Klein9ce3f682021-06-23 15:06:44 -0600838) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600839 """Attempt to revbump chrome.
Alex Kleinf69bd802021-06-22 15:43:49 -0600840
Alex Klein1699fab2022-09-08 08:46:06 -0600841 Revbumps are done by executing an uprev using the current stable version.
842 E.g. if chrome is on 1.2.3.4 and has a 1.2.3.4_rc-r2.ebuild, performing an
843 uprev on version 1.2.3.4 when there are applicable changes (e.g. to the 9999
844 ebuild) will result in a revbump to 1.2.3.4_rc-r3.ebuild.
845 """
846 chrome_version = uprev_lib.get_stable_chrome_version()
847 return uprev_chrome(chrome_version, build_targets, chroot)
Alex Kleinf69bd802021-06-22 15:43:49 -0600848
849
Alex Klein9ce3f682021-06-23 15:06:44 -0600850def uprev_chrome(
Alex Klein16ea1b32021-10-01 15:48:50 -0600851 chrome_version: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600852 build_targets: Optional[List["build_target_lib.BuildTarget"]],
853 chroot: Optional["chroot_lib.Chroot"],
Alex Klein9ce3f682021-06-23 15:06:44 -0600854) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600855 """Attempt to uprev chrome and its related packages to the given version."""
856 uprev_manager = uprev_lib.UprevChromeManager(
857 chrome_version, build_targets=build_targets, chroot=chroot
858 )
859 result = uprev_lib.UprevVersionedPackageResult()
860 # TODO(crbug.com/1080429): Handle all possible outcomes of a Chrome uprev
861 # attempt. The expected behavior is documented in the following table:
862 #
863 # Outcome of Chrome uprev attempt:
864 # NEWER_VERSION_EXISTS:
865 # Do nothing.
866 # SAME_VERSION_EXISTS or REVISION_BUMP:
867 # Uprev followers
868 # Assert not VERSION_BUMP (any other outcome is fine)
869 # VERSION_BUMP or NEW_EBUILD_CREATED:
870 # Uprev followers
871 # Assert that Chrome & followers are at same package version
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600872
Alex Klein1699fab2022-09-08 08:46:06 -0600873 # Start with chrome itself so we can proceed accordingly.
874 chrome_result = uprev_manager.uprev(constants.CHROME_CP)
875 if chrome_result.newer_version_exists:
876 # Cannot use the given version (newer version already exists).
877 return result
878
879 # Also uprev related packages.
880 for package in constants.OTHER_CHROME_PACKAGES:
881 follower_result = uprev_manager.uprev(package)
882 if chrome_result.stable_version and follower_result.version_bump:
883 logging.warning(
884 "%s had a version bump, but no more than a revision bump "
885 "should have been possible.",
886 package,
887 )
888
889 if uprev_manager.modified_ebuilds:
890 # Record changes when we have them.
891 return result.add_result(chrome_version, uprev_manager.modified_ebuilds)
892
David Burger37f48672019-09-18 17:07:56 -0600893 return result
Alex Klein87531182019-08-12 15:23:37 -0600894
Alex Klein87531182019-08-12 15:23:37 -0600895
Alex Klein1699fab2022-09-08 08:46:06 -0600896def _get_latest_version_from_refs(
897 refs_prefix: str, refs: List[uprev_lib.GitRef]
898) -> str:
899 """Get the latest version from refs
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600900
Alex Klein1699fab2022-09-08 08:46:06 -0600901 Versions are compared using |distutils.version.LooseVersion| and
902 the latest version is returned.
Alex Klein87531182019-08-12 15:23:37 -0600903
Alex Klein1699fab2022-09-08 08:46:06 -0600904 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600905 refs_prefix: The refs prefix of the tag format.
906 refs: The tags to parse for the latest version.
Alex Klein87531182019-08-12 15:23:37 -0600907
Alex Klein1699fab2022-09-08 08:46:06 -0600908 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600909 The latest version to use as string.
Alex Klein1699fab2022-09-08 08:46:06 -0600910 """
911 valid_refs = []
912 for gitiles in refs:
913 if gitiles.ref.startswith(refs_prefix):
914 valid_refs.append(gitiles.ref)
Ben Reiche779cf42020-12-15 03:21:31 +0000915
Alex Klein1699fab2022-09-08 08:46:06 -0600916 if not valid_refs:
917 return None
Ben Reiche779cf42020-12-15 03:21:31 +0000918
Alex Klein1699fab2022-09-08 08:46:06 -0600919 # Sort by version and take the latest version.
920 target_version_ref = sorted(valid_refs, key=LooseVersion, reverse=True)[0]
921 return target_version_ref.replace(refs_prefix, "")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800922
923
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700924def _generate_platform_c_files(
925 replication_config: replication_config_pb2.ReplicationConfig,
Alex Klein1699fab2022-09-08 08:46:06 -0600926 chroot: "chroot_lib.Chroot",
927) -> List[str]:
928 """Generates platform C files from a platform JSON payload.
Andrew Lamb9563a152019-12-04 11:42:18 -0700929
Alex Klein1699fab2022-09-08 08:46:06 -0600930 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600931 replication_config: A ReplicationConfig that has already been run. If it
932 produced a build_config.json file, that file will be used to
933 generate platform C files. Otherwise, nothing will be generated.
934 chroot: The chroot to use to generate.
Andrew Lamb9563a152019-12-04 11:42:18 -0700935
Alex Klein1699fab2022-09-08 08:46:06 -0600936 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600937 A list of generated files.
Alex Klein1699fab2022-09-08 08:46:06 -0600938 """
939 # Generate the platform C files from the build config. Note that it would be
940 # more intuitive to generate the platform C files from the platform config;
Alex Kleinfee86da2023-01-20 18:40:06 -0700941 # however, cros_config_schema does not allow this, because the platform
942 # config payload is not always valid input. For example, if a property is
943 # both 'required' and 'build-only', it will fail schema validation. Thus,
944 # use the build config, and use '-f' to filter.
Alex Klein1699fab2022-09-08 08:46:06 -0600945 build_config_path = [
946 rule.destination_path
947 for rule in replication_config.file_replication_rules
948 if rule.destination_path.endswith("build_config.json")
949 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700950
Alex Klein1699fab2022-09-08 08:46:06 -0600951 if not build_config_path:
952 logging.info(
953 "No build_config.json found, will not generate platform C files. "
954 "Replication config: %s",
955 replication_config,
956 )
957 return []
Andrew Lamb9563a152019-12-04 11:42:18 -0700958
Alex Klein1699fab2022-09-08 08:46:06 -0600959 if len(build_config_path) > 1:
960 raise ValueError(
961 "Expected at most one build_config.json destination path. "
962 "Replication config: %s" % replication_config
963 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700964
Alex Klein1699fab2022-09-08 08:46:06 -0600965 build_config_path = build_config_path[0]
Andrew Lamb9563a152019-12-04 11:42:18 -0700966
Alex Klein1699fab2022-09-08 08:46:06 -0600967 # Paths to the build_config.json and dir to output C files to, in the
968 # chroot.
969 build_config_chroot_path = os.path.join(
970 constants.CHROOT_SOURCE_ROOT, build_config_path
971 )
972 generated_output_chroot_dir = os.path.join(
973 constants.CHROOT_SOURCE_ROOT, os.path.dirname(build_config_path)
974 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700975
Alex Klein1699fab2022-09-08 08:46:06 -0600976 command = [
977 "cros_config_schema",
978 "-m",
979 build_config_chroot_path,
980 "-g",
981 generated_output_chroot_dir,
982 "-f",
983 '"TRUE"',
984 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700985
Alex Klein1699fab2022-09-08 08:46:06 -0600986 cros_build_lib.run(
987 command, enter_chroot=True, chroot_args=chroot.get_enter_args()
988 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700989
Alex Klein1699fab2022-09-08 08:46:06 -0600990 # A relative (to the source root) path to the generated C files.
991 generated_output_dir = os.path.dirname(build_config_path)
992 generated_files = []
993 expected_c_files = ["config.c", "ec_config.c", "ec_config.h"]
994 for f in expected_c_files:
995 if os.path.exists(
996 os.path.join(constants.SOURCE_ROOT, generated_output_dir, f)
997 ):
998 generated_files.append(os.path.join(generated_output_dir, f))
Andrew Lamb9563a152019-12-04 11:42:18 -0700999
Alex Klein1699fab2022-09-08 08:46:06 -06001000 if len(expected_c_files) != len(generated_files):
1001 raise GeneratedCrosConfigFilesError(expected_c_files, generated_files)
Andrew Lamb9563a152019-12-04 11:42:18 -07001002
Alex Klein1699fab2022-09-08 08:46:06 -06001003 return generated_files
Andrew Lamb9563a152019-12-04 11:42:18 -07001004
1005
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001006def _get_private_overlay_package_root(ref: uprev_lib.GitRef, package: str):
Alex Klein1699fab2022-09-08 08:46:06 -06001007 """Returns the absolute path to the root of a given private overlay.
Andrew Lambe836f222019-12-09 12:27:38 -07001008
Alex Klein1699fab2022-09-08 08:46:06 -06001009 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001010 ref: GitRef for the private overlay.
1011 package: Path to the package in the overlay.
Alex Klein1699fab2022-09-08 08:46:06 -06001012 """
1013 # There might be a cleaner way to map from package -> path within the source
1014 # tree. For now, just use string patterns.
1015 private_overlay_ref_pattern = (
1016 r"/chromeos\/overlays\/overlay-([\w-]+)-private"
1017 )
1018 match = re.match(private_overlay_ref_pattern, ref.path)
1019 if not match:
1020 raise ValueError(
1021 "ref.path must match the pattern: %s. Actual ref: %s"
1022 % (private_overlay_ref_pattern, ref)
1023 )
Andrew Lambe836f222019-12-09 12:27:38 -07001024
Alex Klein1699fab2022-09-08 08:46:06 -06001025 overlay = match.group(1)
Andrew Lambe836f222019-12-09 12:27:38 -07001026
Alex Klein1699fab2022-09-08 08:46:06 -06001027 return os.path.join(
1028 constants.SOURCE_ROOT,
1029 "src/private-overlays/overlay-%s-private" % overlay,
1030 package,
1031 )
Andrew Lambe836f222019-12-09 12:27:38 -07001032
1033
Alex Klein1699fab2022-09-08 08:46:06 -06001034@uprevs_versioned_package("chromeos-base/chromeos-config-bsp")
Andrew Lambea9a8a22019-12-12 14:03:43 -07001035def replicate_private_config(_build_targets, refs, chroot):
Alex Kleinfee86da2023-01-20 18:40:06 -07001036 """Replicate private cros_config change to the corresponding public config.
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001037
Alex Klein1699fab2022-09-08 08:46:06 -06001038 See uprev_versioned_package for args
1039 """
1040 package = "chromeos-base/chromeos-config-bsp"
Andrew Lambea9a8a22019-12-12 14:03:43 -07001041
Alex Klein1699fab2022-09-08 08:46:06 -06001042 if len(refs) != 1:
1043 raise ValueError("Expected exactly one ref, actual %s" % refs)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001044
Alex Klein1699fab2022-09-08 08:46:06 -06001045 # Expect a replication_config.jsonpb in the package root.
1046 package_root = _get_private_overlay_package_root(refs[0], package)
1047 replication_config_path = os.path.join(
1048 package_root, "replication_config.jsonpb"
1049 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001050
Alex Klein1699fab2022-09-08 08:46:06 -06001051 try:
1052 replication_config = json_format.Parse(
1053 osutils.ReadFile(replication_config_path),
1054 replication_config_pb2.ReplicationConfig(),
1055 )
1056 except IOError:
1057 raise ValueError(
1058 "Expected ReplicationConfig missing at %s" % replication_config_path
1059 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001060
Alex Klein1699fab2022-09-08 08:46:06 -06001061 replication_lib.Replicate(replication_config)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001062
Alex Klein1699fab2022-09-08 08:46:06 -06001063 modified_files = [
1064 rule.destination_path
1065 for rule in replication_config.file_replication_rules
1066 ]
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001067
Alex Kleinfee86da2023-01-20 18:40:06 -07001068 # The generated platform C files are not easily filtered by replication
1069 # rules, i.e. JSON / proto filtering can be described by a FieldMask,
1070 # arbitrary C files cannot. Therefore, replicate and filter the JSON
1071 # payloads, and then generate filtered C files from the JSON payload.
Alex Klein1699fab2022-09-08 08:46:06 -06001072 modified_files.extend(
1073 _generate_platform_c_files(replication_config, chroot)
1074 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001075
Alex Klein1699fab2022-09-08 08:46:06 -06001076 # Use the private repo's commit hash as the new version.
1077 new_private_version = refs[0].revision
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001078
Alex Klein1699fab2022-09-08 08:46:06 -06001079 # modified_files should contain only relative paths at this point, but the
1080 # returned UprevVersionedPackageResult must contain only absolute paths.
1081 for i, modified_file in enumerate(modified_files):
1082 assert not os.path.isabs(modified_file)
1083 modified_files[i] = os.path.join(constants.SOURCE_ROOT, modified_file)
Andrew Lamb988f4da2019-12-10 10:16:43 -07001084
Alex Klein1699fab2022-09-08 08:46:06 -06001085 return uprev_lib.UprevVersionedPackageResult().add_result(
1086 new_private_version, modified_files
1087 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001088
1089
Alex Klein1699fab2022-09-08 08:46:06 -06001090@uprevs_versioned_package("chromeos-base/crosvm")
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001091def uprev_crosvm(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -06001092 """Updates crosvm ebuilds to latest revision
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001093
Alex Klein1699fab2022-09-08 08:46:06 -06001094 crosvm is not versioned. We are updating to the latest commit on the main
1095 branch.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001096
Alex Klein1699fab2022-09-08 08:46:06 -06001097 See: uprev_versioned_package.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001098
Alex Klein1699fab2022-09-08 08:46:06 -06001099 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001100 UprevVersionedPackageResult: The result of updating crosvm ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -06001101 """
1102 overlay = os.path.join(
1103 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1104 )
1105 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "crosvm")
1106 manifest = git.ManifestCheckout.Cached(repo_path)
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001107
Alex Klein1699fab2022-09-08 08:46:06 -06001108 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1109 uprev_manager.uprev(
1110 package_list=[
1111 "chromeos-base/crosvm",
1112 "dev-rust/assertions",
1113 "dev-rust/cros_async",
1114 "dev-rust/cros_fuzz",
1115 "dev-rust/data_model",
1116 "dev-rust/enumn",
1117 "dev-rust/io_uring",
1118 "dev-rust/p9",
1119 "dev-rust/sync",
1120 "dev-rust/sys_util",
1121 "dev-rust/tempfile",
1122 "media-sound/audio_streams",
1123 ],
1124 force=True,
1125 )
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001126
Alex Klein1699fab2022-09-08 08:46:06 -06001127 updated_files = uprev_manager.modified_ebuilds
1128 result = uprev_lib.UprevVersionedPackageResult()
1129 result.add_result(refs[0].revision, updated_files)
1130 return result
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001131
1132
Yi Choua4854ac2022-11-14 10:54:24 +08001133@uprevs_versioned_package("chromeos-base/ti50-emulator")
1134def uprev_ti50_emulator(_build_targets, refs, _chroot):
1135 """Updates ti50-emulator ebuilds to latest revision
1136
1137 ti50-emulator is not versioned. We are updating to the latest commit on the
1138 main branch.
1139
1140 See: uprev_versioned_package.
1141
1142 Returns:
1143 UprevVersionedPackageResult: The result of updating ti50-emulator
1144 ebuild.
1145 """
1146 overlay = os.path.join(
1147 constants.SOURCE_ROOT, constants.CHROMEOS_OVERLAY_DIR
1148 )
1149
1150 # The ti50-emulator will touch multiple repos.
1151 manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT)
1152
1153 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1154 uprev_manager.uprev(
1155 package_list=["chromeos-base/ti50-emulator"],
1156 force=True,
1157 )
1158
1159 updated_files = uprev_manager.modified_ebuilds
1160 result = uprev_lib.UprevVersionedPackageResult()
1161 result.add_result(refs[-1].revision, updated_files)
1162 return result
1163
1164
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001165@uprevs_versioned_package("chromeos-base/ec-devutils")
Jeremy Bettis0186d252023-01-19 14:47:46 -07001166def uprev_ecdevutils(_build_targets, refs, _chroot):
1167 """Updates ec-devutils ebuilds to latest revision
1168
Alex Kleinfee86da2023-01-20 18:40:06 -07001169 ec-devutils is not versioned. We are updating to the latest commit on the
1170 main branch.
Jeremy Bettis0186d252023-01-19 14:47:46 -07001171
1172 See: uprev_versioned_package.
1173
1174 Returns:
1175 UprevVersionedPackageResult: The result of updating ec-devutils ebuilds.
1176 """
1177 overlay = os.path.join(
1178 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1179 )
1180 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "platform", "ec")
1181 manifest = git.ManifestCheckout.Cached(repo_path)
1182
1183 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1184 uprev_manager.uprev(
1185 package_list=[
1186 "chromeos-base/ec-devutils",
1187 ],
1188 force=True,
1189 )
1190
1191 updated_files = uprev_manager.modified_ebuilds
1192 result = uprev_lib.UprevVersionedPackageResult()
1193 result.add_result(refs[0].revision, updated_files)
1194 return result
1195
1196
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001197@uprevs_versioned_package("chromeos-base/ec-utils")
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001198def uprev_ecutils(_build_targets, refs, _chroot):
1199 """Updates ec-utils ebuilds to latest revision
1200
1201 ec-utils is not versioned. We are updating to the latest commit on the main
1202 branch.
1203
1204 See: uprev_versioned_package.
1205
1206 Returns:
1207 UprevVersionedPackageResult: The result of updating ec-utils ebuilds.
1208 """
1209 overlay = os.path.join(
1210 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1211 )
1212 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "platform", "ec")
1213 manifest = git.ManifestCheckout.Cached(repo_path)
1214
1215 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1216 uprev_manager.uprev(
1217 package_list=[
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001218 "chromeos-base/ec-utils",
Jeremy Bettis0186d252023-01-19 14:47:46 -07001219 ],
1220 force=True,
1221 )
1222
1223 updated_files = uprev_manager.modified_ebuilds
1224 result = uprev_lib.UprevVersionedPackageResult()
1225 result.add_result(refs[0].revision, updated_files)
1226 return result
1227
1228
1229@uprevs_versioned_package("chromeos-base/ec-utils-test")
1230def uprev_ecutilstest(_build_targets, refs, _chroot):
1231 """Updates ec-utils-test ebuilds to latest revision
1232
Alex Kleinfee86da2023-01-20 18:40:06 -07001233 ec-utils-test is not versioned. We are updating to the latest commit on the
1234 main branch.
Jeremy Bettis0186d252023-01-19 14:47:46 -07001235
1236 See: uprev_versioned_package.
1237
1238 Returns:
Alex Kleinfee86da2023-01-20 18:40:06 -07001239 UprevVersionedPackageResult: The result of updating ec-utils-test
1240 ebuilds.
Jeremy Bettis0186d252023-01-19 14:47:46 -07001241 """
1242 overlay = os.path.join(
1243 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1244 )
1245 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "platform", "ec")
1246 manifest = git.ManifestCheckout.Cached(repo_path)
1247
1248 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1249 uprev_manager.uprev(
1250 package_list=[
Jeremy Bettisaf96afb2023-01-11 16:09:58 -07001251 "chromeos-base/ec-utils-test",
1252 ],
1253 force=True,
1254 )
1255
1256 updated_files = uprev_manager.modified_ebuilds
1257 result = uprev_lib.UprevVersionedPackageResult()
1258 result.add_result(refs[0].revision, updated_files)
1259 return result
1260
1261
Alex Klein5caab872021-09-10 11:44:37 -06001262def get_best_visible(
Alex Klein1699fab2022-09-08 08:46:06 -06001263 atom: str, build_target: Optional["build_target_lib.BuildTarget"] = None
Alex Klein5caab872021-09-10 11:44:37 -06001264) -> package_info.PackageInfo:
Alex Klein1699fab2022-09-08 08:46:06 -06001265 """Returns the best visible CPV for the given atom.
Alex Kleinbbef2b32019-08-27 10:38:50 -06001266
Alex Klein1699fab2022-09-08 08:46:06 -06001267 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001268 atom: The atom to look up.
1269 build_target: The build target whose sysroot should be searched, or the
1270 SDK if not provided.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001271
Alex Klein1699fab2022-09-08 08:46:06 -06001272 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001273 The best visible package, or None if none are visible.
Alex Klein1699fab2022-09-08 08:46:06 -06001274 """
1275 assert atom
Alex Kleinbbef2b32019-08-27 10:38:50 -06001276
Alex Klein1699fab2022-09-08 08:46:06 -06001277 return portage_util.PortageqBestVisible(
1278 atom,
1279 board=build_target.name if build_target else None,
1280 sysroot=build_target.root if build_target else None,
1281 )
Alex Kleinda39c6d2019-09-16 14:36:36 -06001282
1283
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001284def has_prebuilt(
1285 atom: str,
Alex Klein1699fab2022-09-08 08:46:06 -06001286 build_target: "build_target_lib.BuildTarget" = None,
1287 useflags: Union[Iterable[str], str] = None,
1288) -> bool:
1289 """Check if a prebuilt exists.
Alex Kleinda39c6d2019-09-16 14:36:36 -06001290
Alex Klein1699fab2022-09-08 08:46:06 -06001291 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001292 atom: The package whose prebuilt is being queried.
1293 build_target: The build target whose sysroot should be searched, or the
1294 SDK if not provided.
1295 useflags: Any additional USE flags that should be set. May be a string
1296 of properly formatted USE flags, or an iterable of individual flags.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001297
Alex Klein1699fab2022-09-08 08:46:06 -06001298 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001299 True if there is an available prebuilt, False otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -06001300 """
1301 assert atom
Alex Kleinda39c6d2019-09-16 14:36:36 -06001302
Alex Klein1699fab2022-09-08 08:46:06 -06001303 board = build_target.name if build_target else None
1304 extra_env = None
1305 if useflags:
1306 new_flags = useflags
1307 if not isinstance(useflags, str):
1308 new_flags = " ".join(useflags)
Alex Klein149fd3b2019-12-16 16:01:05 -07001309
Alex Klein1699fab2022-09-08 08:46:06 -06001310 existing = os.environ.get("USE", "")
1311 final_flags = "%s %s" % (existing, new_flags)
1312 extra_env = {"USE": final_flags.strip()}
1313 return portage_util.HasPrebuilt(atom, board=board, extra_env=extra_env)
Alex Klein36b117f2019-09-30 15:13:46 -06001314
1315
David Burger0f9dd4e2019-10-08 12:33:42 -06001316def builds(atom, build_target, packages=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001317 """Check if |build_target| builds |atom| (has it in its depgraph)."""
1318 cros_build_lib.AssertInsideChroot()
Alex Klein36b117f2019-09-30 15:13:46 -06001319
Alex Klein1699fab2022-09-08 08:46:06 -06001320 pkgs = tuple(packages) if packages else None
1321 # TODO(crbug/1081828): Receive and use sysroot.
1322 graph, _sdk_graph = dependency.GetBuildDependency(
1323 build_target.root, build_target.name, pkgs
1324 )
1325 return any(atom in package for package in graph["package_deps"])
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001326
1327
Alex Klein6becabc2020-09-11 14:03:05 -06001328def needs_chrome_source(
Alex Klein1699fab2022-09-08 08:46:06 -06001329 build_target: "build_target_lib.BuildTarget",
Alex Klein6becabc2020-09-11 14:03:05 -06001330 compile_source=False,
1331 packages: Optional[List[package_info.PackageInfo]] = None,
Alex Klein1699fab2022-09-08 08:46:06 -06001332 useflags=None,
1333):
1334 """Check if the chrome source is needed.
Alex Klein6becabc2020-09-11 14:03:05 -06001335
Alex Klein1699fab2022-09-08 08:46:06 -06001336 The chrome source is needed if the build target builds chrome or any of its
1337 follower packages, and can't use a prebuilt for them either because it's not
1338 available, or because we can't use prebuilts because it must build from
1339 source.
1340 """
1341 cros_build_lib.AssertInsideChroot()
Alex Klein6becabc2020-09-11 14:03:05 -06001342
Alex Klein1699fab2022-09-08 08:46:06 -06001343 # Check if it builds chrome and/or a follower package.
1344 graph = depgraph.get_sysroot_dependency_graph(build_target.root, packages)
1345 builds_chrome = constants.CHROME_CP in graph
1346 builds_follower = {
1347 pkg: pkg in graph for pkg in constants.OTHER_CHROME_PACKAGES
1348 }
Alex Klein6becabc2020-09-11 14:03:05 -06001349
Alex Klein1699fab2022-09-08 08:46:06 -06001350 local_uprev = builds_chrome and revbump_chrome([build_target])
Alex Klein9ce3f682021-06-23 15:06:44 -06001351
Alex Kleinfee86da2023-01-20 18:40:06 -07001352 # When we are compiling source set False since we do not use prebuilts. When
1353 # not compiling from source, start with True, i.e. we have every prebuilt
Alex Klein1699fab2022-09-08 08:46:06 -06001354 # we've checked for up to this point.
1355 has_chrome_prebuilt = not compile_source
1356 has_follower_prebuilts = not compile_source
1357 # Save packages that need prebuilts for reporting.
1358 pkgs_needing_prebuilts = []
1359 if compile_source:
1360 # Need everything.
Alex Klein6becabc2020-09-11 14:03:05 -06001361 pkgs_needing_prebuilts.append(constants.CHROME_CP)
Alex Klein1699fab2022-09-08 08:46:06 -06001362 pkgs_needing_prebuilts.extend(
1363 [pkg for pkg, builds_pkg in builds_follower.items() if builds_pkg]
1364 )
1365 else:
1366 # Check chrome itself.
1367 if builds_chrome:
1368 has_chrome_prebuilt = has_prebuilt(
1369 constants.CHROME_CP,
1370 build_target=build_target,
1371 useflags=useflags,
1372 )
1373 if not has_chrome_prebuilt:
1374 pkgs_needing_prebuilts.append(constants.CHROME_CP)
1375 # Check follower packages.
1376 for pkg, builds_pkg in builds_follower.items():
1377 if not builds_pkg:
1378 continue
1379 prebuilt = has_prebuilt(
1380 pkg, build_target=build_target, useflags=useflags
1381 )
1382 has_follower_prebuilts &= prebuilt
1383 if not prebuilt:
1384 pkgs_needing_prebuilts.append(pkg)
Alex Kleinfee86da2023-01-20 18:40:06 -07001385 # Postcondition: has_chrome_prebuilt and has_follower_prebuilts now
1386 # correctly reflect whether we actually have the corresponding prebuilts for
1387 # the build.
Alex Klein6becabc2020-09-11 14:03:05 -06001388
Alex Klein1699fab2022-09-08 08:46:06 -06001389 needs_chrome = builds_chrome and not has_chrome_prebuilt
1390 needs_follower = (
1391 any(builds_follower.values()) and not has_follower_prebuilts
1392 )
Alex Klein6becabc2020-09-11 14:03:05 -06001393
Alex Klein1699fab2022-09-08 08:46:06 -06001394 return NeedsChromeSourceResult(
1395 needs_chrome_source=needs_chrome or needs_follower,
1396 builds_chrome=builds_chrome,
1397 packages=[package_info.parse(p) for p in pkgs_needing_prebuilts],
1398 missing_chrome_prebuilt=not has_chrome_prebuilt,
1399 missing_follower_prebuilt=not has_follower_prebuilts,
1400 local_uprev=local_uprev,
1401 )
Alex Klein6becabc2020-09-11 14:03:05 -06001402
1403
Alex Klein68a28712021-11-08 11:08:30 -07001404class TargetVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001405 """Data class for the info that makes up the "target versions"."""
1406
1407 android_version: str
1408 android_branch: str
1409 android_target: str
1410 chrome_version: str
1411 platform_version: str
1412 milestone_version: str
1413 full_version: str
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001414 lacros_version: str
Alex Klein68a28712021-11-08 11:08:30 -07001415
1416
1417def get_target_versions(
Alex Klein1699fab2022-09-08 08:46:06 -06001418 build_target: "build_target_lib.BuildTarget",
1419 packages: List[package_info.PackageInfo] = None,
Alex Klein68a28712021-11-08 11:08:30 -07001420) -> TargetVersions:
Alex Klein1699fab2022-09-08 08:46:06 -06001421 """Aggregate version info for a few key packages and the OS as a whole."""
1422 # Android version.
1423 android_version = determine_android_version(build_target.name)
1424 logging.info("Found android version: %s", android_version)
1425 # Android branch version.
1426 android_branch = determine_android_branch(build_target.name)
1427 logging.info("Found android branch version: %s", android_branch)
1428 # Android target version.
1429 android_target = determine_android_target(build_target.name)
1430 logging.info("Found android target version: %s", android_target)
Alex Klein68a28712021-11-08 11:08:30 -07001431
Alex Klein1699fab2022-09-08 08:46:06 -06001432 # TODO(crbug/1019770): Investigate cases where builds_chrome is true but
1433 # chrome_version is None.
Alex Klein68a28712021-11-08 11:08:30 -07001434
Alex Klein1699fab2022-09-08 08:46:06 -06001435 builds_chrome = builds(constants.CHROME_CP, build_target, packages=packages)
1436 chrome_version = None
1437 if builds_chrome:
1438 # Chrome version fetch.
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001439 chrome_version = determine_package_version(
1440 constants.CHROME_CP, build_target
1441 )
Alex Klein1699fab2022-09-08 08:46:06 -06001442 logging.info("Found chrome version: %s", chrome_version)
Alex Klein68a28712021-11-08 11:08:30 -07001443
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001444 builds_lacros = builds(constants.LACROS_CP, build_target, packages=packages)
1445 lacros_version = None
1446 if builds_lacros:
1447 # LaCrOS version fetch.
1448 lacros_version = determine_package_version(
1449 constants.LACROS_CP, build_target
1450 )
1451 logging.info("Found LaCrOS version: %s", lacros_version)
1452
Alex Klein1699fab2022-09-08 08:46:06 -06001453 # The ChromeOS version info.
1454 platform_version = determine_platform_version()
1455 milestone_version = determine_milestone_version()
1456 full_version = determine_full_version()
Alex Klein68a28712021-11-08 11:08:30 -07001457
Alex Klein1699fab2022-09-08 08:46:06 -06001458 return TargetVersions(
1459 android_version,
1460 android_branch,
1461 android_target,
1462 chrome_version,
1463 platform_version,
1464 milestone_version,
1465 full_version,
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001466 lacros_version,
Alex Klein1699fab2022-09-08 08:46:06 -06001467 )
Alex Klein68a28712021-11-08 11:08:30 -07001468
1469
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001470def determine_package_version(
1471 cpv_name: str,
Alex Klein1699fab2022-09-08 08:46:06 -06001472 build_target: "build_target_lib.BuildTarget",
1473) -> Optional[str]:
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001474 """Returns the current package version for the board (or in buildroot).
Michael Mortensenc2615b72019-10-15 08:12:24 -06001475
Alex Klein1699fab2022-09-08 08:46:06 -06001476 Args:
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001477 cpv_name: the name of the ebuild CPV
Alex Klein348e7692022-10-13 17:03:37 -06001478 build_target: The board build target.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001479
Alex Klein1699fab2022-09-08 08:46:06 -06001480 Returns:
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001481 The version of the package, if available.
Alex Klein1699fab2022-09-08 08:46:06 -06001482 """
1483 # TODO(crbug/1019770): Long term we should not need the try/catch here once
1484 # the builds function above only returns True for chrome when
1485 # determine_chrome_version will succeed.
1486 try:
1487 pkg_info = portage_util.PortageqBestVisible(
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001488 cpv_name, build_target.name, cwd=constants.SOURCE_ROOT
Alex Klein1699fab2022-09-08 08:46:06 -06001489 )
1490 except cros_build_lib.RunCommandError as e:
1491 # Return None because portage failed when trying to determine the chrome
1492 # version.
1493 logging.warning("Caught exception in determine_chrome_package: %s", e)
1494 return None
1495 # Something like 78.0.3877.4_rc -> 78.0.3877.4
1496 return pkg_info.version.partition("_")[0]
Michael Mortensenc2615b72019-10-15 08:12:24 -06001497
1498
Alex Klein68a28712021-11-08 11:08:30 -07001499@functools.lru_cache()
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001500def determine_android_package(board: str) -> Optional[str]:
Alex Klein1699fab2022-09-08 08:46:06 -06001501 """Returns the active Android container package in use by the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001502
Alex Klein1699fab2022-09-08 08:46:06 -06001503 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001504 board: The board name this is specific to.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001505
Alex Klein1699fab2022-09-08 08:46:06 -06001506 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001507 The android package string if there is one.
Alex Klein1699fab2022-09-08 08:46:06 -06001508 """
1509 try:
1510 packages = portage_util.GetPackageDependencies(
1511 "virtual/target-os", board=board
1512 )
1513 except cros_build_lib.RunCommandError as e:
1514 # Return None because a command (likely portage) failed when trying to
1515 # determine the package.
1516 logging.warning("Caught exception in determine_android_package: %s", e)
1517 return None
1518
1519 # We assume there is only one Android package in the depgraph.
1520 for package in packages:
1521 if package.startswith(
1522 "chromeos-base/android-container-"
1523 ) or package.startswith("chromeos-base/android-vm-"):
1524 return package
Michael Mortensene0f4b542019-10-24 15:30:23 -06001525 return None
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001526
1527
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001528def determine_android_version(board: str, package: str = None):
Alex Klein1699fab2022-09-08 08:46:06 -06001529 """Determine the current Android version in buildroot now and return it.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001530
Alex Klein1699fab2022-09-08 08:46:06 -06001531 This uses the typical portage logic to determine which version of Android
1532 is active right now in the buildroot.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001533
Alex Klein1699fab2022-09-08 08:46:06 -06001534 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001535 board: The board name this is specific to.
1536 package: The Android package, if already computed.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001537
Alex Klein1699fab2022-09-08 08:46:06 -06001538 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001539 The Android build ID of the container for the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001540
Alex Klein1699fab2022-09-08 08:46:06 -06001541 Raises:
Alex Klein348e7692022-10-13 17:03:37 -06001542 NoAndroidVersionError: if no unique Android version can be determined.
Alex Klein1699fab2022-09-08 08:46:06 -06001543 """
1544 if not package:
1545 package = determine_android_package(board)
1546 if not package:
1547 return None
1548 cpv = package_info.SplitCPV(package)
1549 if not cpv:
1550 raise NoAndroidVersionError(
1551 "Android version could not be determined for %s" % board
1552 )
1553 return cpv.version_no_rev
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001554
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001555
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001556def determine_android_branch(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001557 """Returns the Android branch in use by the active container ebuild."""
1558 if not package:
1559 package = determine_android_package(board)
1560 if not package:
1561 return None
1562 ebuild_path = portage_util.FindEbuildForBoardPackage(package, board)
1563 # We assume all targets pull from the same branch and that we always
1564 # have at least one of the following targets.
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +09001565 # TODO(b/187795671): Do this in a less hacky way.
1566 targets = android.GetAllAndroidEbuildTargets()
Alex Klein1699fab2022-09-08 08:46:06 -06001567 ebuild_content = osutils.SourceEnvironment(ebuild_path, targets)
1568 for target in targets:
1569 if target in ebuild_content:
1570 branch = re.search(r"(.*?)-linux-", ebuild_content[target])
1571 if branch is not None:
1572 return branch.group(1)
1573 raise NoAndroidBranchError(
1574 "Android branch could not be determined for %s (ebuild empty?)" % board
1575 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001576
1577
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001578def determine_android_target(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001579 """Returns the Android target in use by the active container ebuild."""
1580 if not package:
1581 package = determine_android_package(board)
1582 if not package:
1583 return None
1584 if package.startswith("chromeos-base/android-vm-"):
1585 return "bertha"
1586 elif package.startswith("chromeos-base/android-container-"):
1587 return "cheets"
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001588
Alex Klein1699fab2022-09-08 08:46:06 -06001589 raise NoAndroidTargetError(
1590 "Android Target cannot be determined for the package: %s" % package
1591 )
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001592
1593
1594def determine_platform_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001595 """Returns the platform version from the source root."""
1596 # Platform version is something like '12575.0.0'.
1597 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1598 return version.VersionString()
Michael Mortensen009cb662019-10-21 11:38:43 -06001599
1600
1601def determine_milestone_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001602 """Returns the platform version from the source root."""
1603 # Milestone version is something like '79'.
1604 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1605 return version.chrome_branch
Michael Mortensen009cb662019-10-21 11:38:43 -06001606
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001607
Michael Mortensen009cb662019-10-21 11:38:43 -06001608def determine_full_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001609 """Returns the full version from the source root."""
1610 # Full version is something like 'R79-12575.0.0'.
1611 milestone_version = determine_milestone_version()
1612 platform_version = determine_platform_version()
1613 full_version = "R%s-%s" % (milestone_version, platform_version)
1614 return full_version
Michael Mortensen71ef5682020-05-07 14:29:24 -06001615
1616
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001617def find_fingerprints(
Alex Klein1699fab2022-09-08 08:46:06 -06001618 build_target: "build_target_lib.BuildTarget",
1619) -> List[str]:
1620 """Returns a list of fingerprints for this build.
Michael Mortensende716a12020-05-15 11:27:00 -06001621
Alex Klein1699fab2022-09-08 08:46:06 -06001622 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001623 build_target: The build target.
Michael Mortensende716a12020-05-15 11:27:00 -06001624
Alex Klein1699fab2022-09-08 08:46:06 -06001625 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001626 List of fingerprint strings.
Alex Klein1699fab2022-09-08 08:46:06 -06001627 """
1628 cros_build_lib.AssertInsideChroot()
1629 fp_file = "cheets-fingerprint.txt"
1630 fp_path = os.path.join(
1631 image_lib.GetLatestImageLink(build_target.name), fp_file
1632 )
1633 if not os.path.isfile(fp_path):
1634 logging.info("Fingerprint file not found: %s", fp_path)
1635 return []
1636 logging.info("Reading fingerprint file: %s", fp_path)
1637 fingerprints = osutils.ReadFile(fp_path).splitlines()
1638 return fingerprints
Michael Mortensende716a12020-05-15 11:27:00 -06001639
1640
Alex Klein1699fab2022-09-08 08:46:06 -06001641def get_all_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1642 """Extract firmware version for all models present.
Michael Mortensen59e30872020-05-18 14:12:49 -06001643
Alex Klein1699fab2022-09-08 08:46:06 -06001644 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001645 build_target: The build target.
Michael Mortensen59e30872020-05-18 14:12:49 -06001646
Alex Klein1699fab2022-09-08 08:46:06 -06001647 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001648 A dict of FirmwareVersions namedtuple instances by model.
1649 Each element will be populated based on whether it was present in the
1650 command output.
Alex Klein1699fab2022-09-08 08:46:06 -06001651 """
1652 cros_build_lib.AssertInsideChroot()
1653 result = {}
1654 # Note that example output for _get_firmware_version_cmd_result is available
1655 # in the packages_unittest.py for testing get_all_firmware_versions.
1656 cmd_result = _get_firmware_version_cmd_result(build_target)
Michael Mortensen59e30872020-05-18 14:12:49 -06001657
Alex Klein1699fab2022-09-08 08:46:06 -06001658 if cmd_result:
1659 # There is a blank line between the version info for each model.
1660 firmware_version_payloads = cmd_result.split("\n\n")
1661 for firmware_version_payload in firmware_version_payloads:
1662 if "BIOS" in firmware_version_payload:
1663 firmware_version = _find_firmware_versions(
1664 firmware_version_payload
1665 )
1666 result[firmware_version.model] = firmware_version
1667 return result
Michael Mortensen59e30872020-05-18 14:12:49 -06001668
1669
Benjamin Shai0858cd32022-01-10 20:23:49 +00001670class FirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001671 """Tuple to hold firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001672
Alex Klein1699fab2022-09-08 08:46:06 -06001673 model: Optional[str]
1674 main: Optional[str]
1675 main_rw: Optional[str]
1676 ec: Optional[str]
1677 ec_rw: Optional[str]
1678
1679 def __bool__(self):
1680 return bool(
1681 self.model or self.main or self.main_rw or self.ec or self.ec_rw
1682 )
Michael Mortensen71ef5682020-05-07 14:29:24 -06001683
1684
Alex Klein1699fab2022-09-08 08:46:06 -06001685def get_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1686 """Extract version information from the firmware updater, if one exists.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001687
Alex Klein1699fab2022-09-08 08:46:06 -06001688 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001689 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001690
Alex Klein1699fab2022-09-08 08:46:06 -06001691 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001692 A FirmwareVersions namedtuple instance.
1693 Each element will either be set to the string output by the firmware
1694 updater shellball, or None if there is no firmware updater.
Alex Klein1699fab2022-09-08 08:46:06 -06001695 """
1696 cros_build_lib.AssertInsideChroot()
1697 cmd_result = _get_firmware_version_cmd_result(build_target)
1698 if cmd_result:
1699 return _find_firmware_versions(cmd_result)
1700 else:
1701 return FirmwareVersions(None, None, None, None, None)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001702
1703
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001704def _get_firmware_version_cmd_result(
Alex Klein1699fab2022-09-08 08:46:06 -06001705 build_target: "build_target_lib.BuildTarget",
1706) -> Optional[str]:
1707 """Gets the raw result output of the firmware updater version command.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001708
Alex Klein1699fab2022-09-08 08:46:06 -06001709 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001710 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001711
Alex Klein1699fab2022-09-08 08:46:06 -06001712 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001713 Command execution result.
Alex Klein1699fab2022-09-08 08:46:06 -06001714 """
1715 updater = os.path.join(
1716 build_target.root, "usr/sbin/chromeos-firmwareupdate"
1717 )
1718 logging.info("Calling updater %s", updater)
1719 # Call the updater using the chroot-based path.
1720 try:
1721 return cros_build_lib.run(
1722 [updater, "-V"],
1723 capture_output=True,
1724 log_output=True,
1725 encoding="utf-8",
1726 ).stdout
1727 except cros_build_lib.RunCommandError:
1728 # Updater probably doesn't exist (e.g. betty).
1729 return None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001730
1731
1732def _find_firmware_versions(cmd_output):
Alex Klein1699fab2022-09-08 08:46:06 -06001733 """Finds firmware version output via regex matches against the cmd_output.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001734
Alex Klein1699fab2022-09-08 08:46:06 -06001735 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001736 cmd_output: The raw output to search against.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001737
Alex Klein1699fab2022-09-08 08:46:06 -06001738 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001739 FirmwareVersions namedtuple with results.
1740 Each element will either be set to the string output by the firmware
1741 updater shellball, or None if there is no match.
Alex Klein1699fab2022-09-08 08:46:06 -06001742 """
Michael Mortensen71ef5682020-05-07 14:29:24 -06001743
Alex Klein1699fab2022-09-08 08:46:06 -06001744 # Sometimes a firmware bundle includes a special combination of RO+RW
1745 # firmware. In this case, the RW firmware version is indicated with a "(RW)
1746 # version" field. In other cases, the "(RW) version" field is not present.
1747 # Therefore, search for the "(RW)" fields first and if they aren't present,
1748 # fallback to the other format. e.g. just "BIOS version:".
1749 # TODO(mmortensen): Use JSON once the firmware updater supports it.
1750 main = None
1751 main_rw = None
1752 ec = None
1753 ec_rw = None
1754 model = None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001755
Alex Klein1699fab2022-09-08 08:46:06 -06001756 match = re.search(r"BIOS version:\s*(?P<version>.*)", cmd_output)
1757 if match:
1758 main = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001759
Alex Klein1699fab2022-09-08 08:46:06 -06001760 match = re.search(r"BIOS \(RW\) version:\s*(?P<version>.*)", cmd_output)
1761 if match:
1762 main_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001763
Alex Klein1699fab2022-09-08 08:46:06 -06001764 match = re.search(r"EC version:\s*(?P<version>.*)", cmd_output)
1765 if match:
1766 ec = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001767
Alex Klein1699fab2022-09-08 08:46:06 -06001768 match = re.search(r"EC \(RW\) version:\s*(?P<version>.*)", cmd_output)
1769 if match:
1770 ec_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001771
Alex Klein1699fab2022-09-08 08:46:06 -06001772 match = re.search(r"Model:\s*(?P<model>.*)", cmd_output)
1773 if match:
1774 model = match.group("model")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001775
Alex Klein1699fab2022-09-08 08:46:06 -06001776 return FirmwareVersions(model, main, main_rw, ec, ec_rw)
Michael Mortensena4af79e2020-05-06 16:18:48 -06001777
1778
Benjamin Shai0858cd32022-01-10 20:23:49 +00001779class MainEcFirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001780 """Tuple to hold main and ec firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001781
Alex Klein1699fab2022-09-08 08:46:06 -06001782 main_fw_version: Optional[str]
1783 ec_fw_version: Optional[str]
1784
1785 def __bool__(self):
1786 return bool(self.main_fw_version or self.ec_fw_version)
Benjamin Shai0858cd32022-01-10 20:23:49 +00001787
Michael Mortensena4af79e2020-05-06 16:18:48 -06001788
Alex Klein1699fab2022-09-08 08:46:06 -06001789def determine_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1790 """Returns a namedtuple with main and ec firmware versions.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001791
Alex Klein1699fab2022-09-08 08:46:06 -06001792 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001793 build_target: The build target.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001794
Alex Klein1699fab2022-09-08 08:46:06 -06001795 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001796 MainEcFirmwareVersions namedtuple with results.
Alex Klein1699fab2022-09-08 08:46:06 -06001797 """
1798 fw_versions = get_firmware_versions(build_target)
1799 main_fw_version = fw_versions.main_rw or fw_versions.main
1800 ec_fw_version = fw_versions.ec_rw or fw_versions.ec
Michael Mortensena4af79e2020-05-06 16:18:48 -06001801
Alex Klein1699fab2022-09-08 08:46:06 -06001802 return MainEcFirmwareVersions(main_fw_version, ec_fw_version)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001803
Benjamin Shai0858cd32022-01-10 20:23:49 +00001804
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001805def determine_kernel_version(
Alex Klein1699fab2022-09-08 08:46:06 -06001806 build_target: "build_target_lib.BuildTarget",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001807) -> str:
Alex Klein1699fab2022-09-08 08:46:06 -06001808 """Returns a string containing the kernel version for this build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001809
Alex Klein1699fab2022-09-08 08:46:06 -06001810 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001811 build_target: The build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001812
Alex Klein1699fab2022-09-08 08:46:06 -06001813 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001814 The kernel versions, or empty string.
Alex Klein1699fab2022-09-08 08:46:06 -06001815 """
Lizzy Presland0b978e62022-09-09 16:55:29 +00001816 target_virtual_pkg = "virtual/linux-sources"
Alex Klein1699fab2022-09-08 08:46:06 -06001817 try:
Lizzy Presland0b978e62022-09-09 16:55:29 +00001818 candidate_packages = portage_util.GetFlattenedDepsForPackage(
1819 target_virtual_pkg,
1820 sysroot=build_target.root,
1821 board=build_target.name,
1822 depth=1,
1823 )
1824 installed_packages = portage_util.GetPackageDependencies(
1825 target_virtual_pkg, board=build_target.name
Alex Klein1699fab2022-09-08 08:46:06 -06001826 )
1827 except cros_build_lib.RunCommandError as e:
1828 logging.warning("Unable to get package list for metadata: %s", e)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001829 return ""
1830 if not candidate_packages:
1831 raise KernelVersionError("No package found in FlattenedDepsForPackage")
1832 if not installed_packages:
1833 raise KernelVersionError("No package found in GetPackageDependencies")
1834 packages = [
1835 p
1836 for p in installed_packages
1837 if p in candidate_packages and target_virtual_pkg not in p
1838 ]
1839 if len(packages) == 0:
1840 raise KernelVersionError(
1841 "No matches for installed packages were found in candidate "
1842 "packages. Did GetFlattenedDepsForPackage search all possible "
1843 "package versions?\tInstalled: %s\tCandidates: %s"
1844 % (" ".join(installed_packages), " ".join(candidate_packages))
1845 )
1846 if len(packages) > 1:
1847 raise KernelVersionError(
1848 "Too many packages found in intersection of installed packages and "
1849 "possible kernel versions (%s)" % "".join(packages)
1850 )
1851 kernel_version = package_info.SplitCPV(packages[0]).version
1852 logging.info("Found active kernel version: %s", kernel_version)
1853 return kernel_version
Michael Mortensen125bb012020-05-21 14:02:10 -06001854
1855
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001856def get_models(
Alex Klein1699fab2022-09-08 08:46:06 -06001857 build_target: "build_target_lib.BuildTarget", log_output: bool = True
1858) -> Optional[List[str]]:
1859 """Obtain a list of models supported by a unified board.
Michael Mortensen125bb012020-05-21 14:02:10 -06001860
Alex Klein1699fab2022-09-08 08:46:06 -06001861 This ignored whitelabel models since GoldenEye has no specific support for
1862 these at present.
Michael Mortensen125bb012020-05-21 14:02:10 -06001863
Alex Klein1699fab2022-09-08 08:46:06 -06001864 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001865 build_target: The build target.
1866 log_output: Whether to log the output of the cros_config_host
1867 invocation.
Michael Mortensen125bb012020-05-21 14:02:10 -06001868
Alex Klein1699fab2022-09-08 08:46:06 -06001869 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001870 A list of models supported by this board, if it is a unified build;
1871 None, if it is not a unified build.
Alex Klein1699fab2022-09-08 08:46:06 -06001872 """
1873 return _run_cros_config_host(
1874 build_target, ["list-models"], log_output=log_output
1875 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001876
1877
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001878def get_key_id(
Alex Klein1699fab2022-09-08 08:46:06 -06001879 build_target: "build_target_lib.BuildTarget", model: str
1880) -> Optional[str]:
1881 """Obtain the key_id for a model within the build_target.
Michael Mortensen359c1f32020-05-28 19:35:42 -06001882
Alex Klein1699fab2022-09-08 08:46:06 -06001883 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001884 build_target: The build target.
1885 model: The model name
Michael Mortensen359c1f32020-05-28 19:35:42 -06001886
Alex Klein1699fab2022-09-08 08:46:06 -06001887 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001888 A key_id or None.
Alex Klein1699fab2022-09-08 08:46:06 -06001889 """
1890 model_arg = "--model=" + model
1891 key_id_list = _run_cros_config_host(
1892 build_target, [model_arg, "get", "/firmware-signing", "key-id"]
1893 )
1894 key_id = None
1895 if len(key_id_list) == 1:
1896 key_id = key_id_list[0]
1897 return key_id
Michael Mortensen359c1f32020-05-28 19:35:42 -06001898
1899
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001900def _run_cros_config_host(
Alex Klein1699fab2022-09-08 08:46:06 -06001901 build_target: "build_target_lib.BuildTarget",
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001902 args: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -06001903 log_output: bool = True,
1904) -> Optional[List[str]]:
1905 """Run the cros_config_host tool.
Michael Mortensen125bb012020-05-21 14:02:10 -06001906
Alex Klein1699fab2022-09-08 08:46:06 -06001907 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001908 build_target: The build target.
1909 args: List of arguments to pass.
1910 log_output: Whether to log the output of the cros_config_host.
Michael Mortensen125bb012020-05-21 14:02:10 -06001911
Alex Klein1699fab2022-09-08 08:46:06 -06001912 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001913 Output of the tool
Alex Klein1699fab2022-09-08 08:46:06 -06001914 """
1915 cros_build_lib.AssertInsideChroot()
1916 tool = "/usr/bin/cros_config_host"
1917 if not os.path.isfile(tool):
1918 return None
Michael Mortensen125bb012020-05-21 14:02:10 -06001919
Alex Klein1699fab2022-09-08 08:46:06 -06001920 config_fname = build_target.full_path(
1921 "usr/share/chromeos-config/yaml/config.yaml"
1922 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001923
Alex Klein1699fab2022-09-08 08:46:06 -06001924 result = cros_build_lib.run(
1925 [tool, "-c", config_fname] + args,
1926 capture_output=True,
1927 encoding="utf-8",
1928 log_output=log_output,
1929 check=False,
1930 )
1931 if result.returncode:
1932 # Show the output for debugging purposes.
1933 if "No such file or directory" not in result.stderr:
1934 logging.error("cros_config_host failed: %s\n", result.stderr)
1935 return None
1936 return result.stdout.strip().splitlines()