blob: 7c7f96279e72124eac8592a5fc771c1c9ba911ad [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleineb77ffa2019-05-28 14:47:44 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Package utility functionality."""
6
Yaakov Shaul730814a2019-09-10 13:58:25 -06007import collections
Ben Reiche779cf42020-12-15 03:21:31 +00008from distutils.version import LooseVersion
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -06009import fileinput
Alex Klein87531182019-08-12 15:23:37 -060010import functools
Yaakov Shaul395ae832019-09-09 14:45:32 -060011import json
Chris McDonaldf7c03d42021-07-21 11:54:26 -060012import logging
Evan Hernandezb51f1522019-08-15 11:29:40 -060013import os
Michael Mortensenb70e8a82019-10-10 18:43:41 -060014import re
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -060015import sys
Alex Klein68a28712021-11-08 11:08:30 -070016from typing import Iterable, List, NamedTuple, Optional, TYPE_CHECKING, Union
Alex Klein87531182019-08-12 15:23:37 -060017
Mike Frysinger2c024062021-05-22 15:43:22 -040018from chromite.third_party.google.protobuf import json_format
Yaakov Shaul730814a2019-09-10 13:58:25 -060019
Andrew Lamb2bde9e42019-11-04 13:24:09 -070020from chromite.api.gen.config import replication_config_pb2
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000021from chromite.lib import chromeos_version
Alex Kleineb77ffa2019-05-28 14:47:44 -060022from chromite.lib import constants
Evan Hernandezb51f1522019-08-15 11:29:40 -060023from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060024from chromite.lib import git
Michael Mortensende716a12020-05-15 11:27:00 -060025from chromite.lib import image_lib
Michael Mortensenb70e8a82019-10-10 18:43:41 -060026from chromite.lib import osutils
Alex Kleineb77ffa2019-05-28 14:47:44 -060027from chromite.lib import portage_util
Andrew Lamb2bde9e42019-11-04 13:24:09 -070028from chromite.lib import replication_lib
Alex Kleind6195b62019-08-06 16:01:16 -060029from chromite.lib import uprev_lib
Alex Klein18a60af2020-06-11 12:08:47 -060030from chromite.lib.parser import package_info
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090031from chromite.service import android
Alex Kleineb77ffa2019-05-28 14:47:44 -060032
Mike Frysinger68796b52019-08-25 00:04:27 -040033
Alex Klein5caab872021-09-10 11:44:37 -060034if TYPE_CHECKING:
Alex Klein1699fab2022-09-08 08:46:06 -060035 from chromite.lib import build_target_lib
36 from chromite.lib import chroot_lib
Chris McDonaldf7c03d42021-07-21 11:54:26 -060037
Alex Klein36b117f2019-09-30 15:13:46 -060038if cros_build_lib.IsInsideChroot():
Alex Klein1699fab2022-09-08 08:46:06 -060039 from chromite.lib import depgraph
40 from chromite.service import dependency
Alex Klein36b117f2019-09-30 15:13:46 -060041
Alex Klein87531182019-08-12 15:23:37 -060042# Registered handlers for uprevving versioned packages.
43_UPREV_FUNCS = {}
44
Alex Kleineb77ffa2019-05-28 14:47:44 -060045
46class Error(Exception):
Alex Klein1699fab2022-09-08 08:46:06 -060047 """Module's base error class."""
Alex Kleineb77ffa2019-05-28 14:47:44 -060048
49
Alex Klein4de25e82019-08-05 15:58:39 -060050class UnknownPackageError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060051 """Uprev attempted for a package without a registered handler."""
Alex Klein4de25e82019-08-05 15:58:39 -060052
53
Alex Kleineb77ffa2019-05-28 14:47:44 -060054class UprevError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060055 """An error occurred while uprevving packages."""
Alex Kleineb77ffa2019-05-28 14:47:44 -060056
57
Michael Mortensenb70e8a82019-10-10 18:43:41 -060058class NoAndroidVersionError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060059 """An error occurred while trying to determine the android version."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060060
61
62class NoAndroidBranchError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060063 """An error occurred while trying to determine the android branch."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060064
65
66class NoAndroidTargetError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060067 """An error occurred while trying to determine the android target."""
Michael Mortensenb70e8a82019-10-10 18:43:41 -060068
69
Lizzy Presland0b978e62022-09-09 16:55:29 +000070class KernelVersionError(Error):
71 """An error occurred while trying to determine the kernel version."""
72
73
Alex Klein4de25e82019-08-05 15:58:39 -060074class AndroidIsPinnedUprevError(UprevError):
Alex Klein1699fab2022-09-08 08:46:06 -060075 """Raised when we try to uprev while Android is pinned."""
Alex Klein4de25e82019-08-05 15:58:39 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 def __init__(self, new_android_atom):
78 """Initialize a AndroidIsPinnedUprevError.
Alex Klein4de25e82019-08-05 15:58:39 -060079
Alex Klein1699fab2022-09-08 08:46:06 -060080 Args:
Alex Klein348e7692022-10-13 17:03:37 -060081 new_android_atom: The Android atom that we failed to uprev to, due
82 to Android being pinned.
Alex Klein1699fab2022-09-08 08:46:06 -060083 """
84 assert new_android_atom
85 msg = (
86 "Failed up uprev to Android version %s as Android was pinned."
87 % new_android_atom
88 )
89 super().__init__(msg)
90 self.new_android_atom = new_android_atom
Alex Klein87531182019-08-12 15:23:37 -060091
92
Andrew Lamb9563a152019-12-04 11:42:18 -070093class GeneratedCrosConfigFilesError(Error):
Alex Klein1699fab2022-09-08 08:46:06 -060094 """Error when cros_config_schema does not produce expected files"""
Andrew Lamb9563a152019-12-04 11:42:18 -070095
Alex Klein1699fab2022-09-08 08:46:06 -060096 def __init__(self, expected_files, found_files):
97 msg = "Expected to find generated C files: %s. Actually found: %s" % (
98 expected_files,
99 found_files,
100 )
101 super().__init__(msg)
Andrew Lamb9563a152019-12-04 11:42:18 -0700102
Alex Klein7a3a7dd2020-01-08 16:44:38 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104NeedsChromeSourceResult = collections.namedtuple(
105 "NeedsChromeSourceResult",
106 (
107 "needs_chrome_source",
108 "builds_chrome",
109 "packages",
110 "missing_chrome_prebuilt",
111 "missing_follower_prebuilt",
112 "local_uprev",
113 ),
114)
Alex Klein6becabc2020-09-11 14:03:05 -0600115
116
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600117def patch_ebuild_vars(ebuild_path, variables):
Alex Klein1699fab2022-09-08 08:46:06 -0600118 """Updates variables in ebuild.
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 Use this function rather than portage_util.EBuild.UpdateEBuild when you
121 want to preserve the variable position and quotes within the ebuild.
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600124 ebuild_path: The path of the ebuild.
125 variables: Dictionary of variables to update in ebuild.
Alex Klein1699fab2022-09-08 08:46:06 -0600126 """
127 try:
128 for line in fileinput.input(ebuild_path, inplace=1):
129 for var, value in variables.items():
130 line = re.sub(rf"\b{var}=\S+", f'{var}="{value}"', line)
131 sys.stdout.write(line)
132 finally:
133 fileinput.close()
Yaakov Shaulcb1cfc32019-09-16 13:51:19 -0600134
135
Alex Klein87531182019-08-12 15:23:37 -0600136def uprevs_versioned_package(package):
Alex Klein1699fab2022-09-08 08:46:06 -0600137 """Decorator to register package uprev handlers."""
138 assert package
Alex Klein87531182019-08-12 15:23:37 -0600139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 def register(func):
141 """Registers |func| as a handler for |package|."""
142 _UPREV_FUNCS[package] = func
Alex Klein87531182019-08-12 15:23:37 -0600143
Alex Klein1699fab2022-09-08 08:46:06 -0600144 @functools.wraps(func)
145 def pass_through(*args, **kwargs):
146 return func(*args, **kwargs)
Alex Klein87531182019-08-12 15:23:37 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 return pass_through
Alex Klein87531182019-08-12 15:23:37 -0600149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 return register
Alex Klein87531182019-08-12 15:23:37 -0600151
152
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900153class UprevAndroidResult(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -0600154 """Results of an Android uprev."""
155
156 revved: bool
157 android_atom: str = None
158 modified_files: List[str] = None
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900159
160
161def uprev_android(
162 android_package: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600163 chroot: "chroot_lib.Chroot",
164 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900165 android_build_branch: Optional[str] = None,
166 android_version: Optional[str] = None,
Alex Klein1699fab2022-09-08 08:46:06 -0600167 skip_commit: bool = False,
168) -> UprevAndroidResult:
169 """Performs an Android uprev by calling cros_mark_android_as_stable.
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600172 android_package: The Android package to uprev.
173 chroot: The chroot to enter.
174 build_targets: List of build targets to cleanup after uprev.
175 android_build_branch: Override the default Android branch corresponding
176 to the package.
177 android_version: Uprev to the particular version. By default the latest
178 available version is used.
179 skip_commit: Whether to skip committing the change after a successful
180 uprev.
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600183 The uprev result containing:
184 revved: Whether an uprev happened.
185 android_atom: If revved, the portage atom for the revved Android
186 ebuild.
187 modified_files: If revved, list of files being modified.
Alex Klein1699fab2022-09-08 08:46:06 -0600188 """
189 command = [
190 "cros_mark_android_as_stable",
191 f"--android_package={android_package}",
192 ]
193 if build_targets:
194 command.append(f'--boards={":".join(bt.name for bt in build_targets)}')
195 if android_build_branch:
196 command.append(f"--android_build_branch={android_build_branch}")
197 if android_version:
198 command.append(f"--force_version={android_version}")
199 if skip_commit:
200 command.append("--skip_commit")
Alex Klein4de25e82019-08-05 15:58:39 -0600201
Alex Klein1699fab2022-09-08 08:46:06 -0600202 result = cros_build_lib.run(
203 command,
204 stdout=True,
205 enter_chroot=True,
206 encoding="utf-8",
207 chroot_args=chroot.get_enter_args(),
208 )
Alex Klein4de25e82019-08-05 15:58:39 -0600209
Alex Klein1699fab2022-09-08 08:46:06 -0600210 # cros_mark_android_as_stable prints the uprev result to stdout as JSON in a
211 # single line. We only take the last line from stdout to make sure no junk
212 # output is included (e.g. messages from bashrc scripts that run upon entering
213 # the chroot.)
214 output = json.loads(result.stdout.strip().splitlines()[-1])
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900215
Alex Klein1699fab2022-09-08 08:46:06 -0600216 if not output["revved"]:
217 logging.info("Found nothing to rev.")
218 return UprevAndroidResult(revved=False)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900219
Alex Klein1699fab2022-09-08 08:46:06 -0600220 android_atom = output["android_atom"]
Alex Klein4de25e82019-08-05 15:58:39 -0600221
Alex Klein1699fab2022-09-08 08:46:06 -0600222 for target in build_targets or []:
223 # Sanity check: We should always be able to merge the version of
224 # Android we just unmasked.
225 command = [f"emerge-{target.name}", "-p", "--quiet", f"={android_atom}"]
226 try:
227 cros_build_lib.run(
228 command, enter_chroot=True, chroot_args=chroot.get_enter_args()
229 )
230 except cros_build_lib.RunCommandError:
231 logging.error(
232 "Cannot emerge-%s =%s\nIs Android pinned to an older "
233 "version?",
234 target,
235 android_atom,
236 )
237 raise AndroidIsPinnedUprevError(android_atom)
Alex Klein4de25e82019-08-05 15:58:39 -0600238
Alex Klein1699fab2022-09-08 08:46:06 -0600239 return UprevAndroidResult(
240 revved=True,
241 android_atom=android_atom,
242 modified_files=output["modified_files"],
243 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900244
245
Alex Klein1699fab2022-09-08 08:46:06 -0600246def uprev_android_lkgb(
247 android_package: str,
248 build_targets: List["build_target_lib.BuildTarget"],
249 chroot: "chroot_lib.Chroot",
250) -> uprev_lib.UprevVersionedPackageResult:
251 """Uprevs an Android package to the version specified in the LKGB file.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900252
Alex Klein1699fab2022-09-08 08:46:06 -0600253 This is the PUpr handler for Android packages, triggered whenever the
254 corresponding LKGB file is being updated.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 PUpr for Android does not test the uprev change in CQ; instead we run separate
257 jobs to test new Android versions, and we write the latest vetted version to
258 the LKGB file. Find the design at go/android-uprev-recipes.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900259
Alex Klein1699fab2022-09-08 08:46:06 -0600260 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600261 android_package: The Android package to uprev.
262 build_targets: List of build targets to cleanup after uprev.
263 chroot: The chroot to enter.
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900264
Alex Klein1699fab2022-09-08 08:46:06 -0600265 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600266 An uprev_lib.UprevVersionedPackageResult containing the new version and
267 a list of modified files.
Alex Klein1699fab2022-09-08 08:46:06 -0600268 """
269 android_package_dir = android.GetAndroidPackageDir(android_package)
270 android_version = android.ReadLKGB(android_package_dir)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900271
Alex Klein1699fab2022-09-08 08:46:06 -0600272 result = uprev_lib.UprevVersionedPackageResult()
273 uprev_result = uprev_android(
274 android_package,
275 chroot,
276 build_targets=build_targets,
277 android_version=android_version,
278 skip_commit=True,
279 )
280 if not uprev_result.revved:
281 return result
282
283 # cros_mark_android_as_stable returns paths relative to |android.OVERLAY_DIR|.
284 result.add_result(
285 android_version,
286 [
287 os.path.join(android.OVERLAY_DIR, f)
288 for f in uprev_result.modified_files
289 ],
290 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900291 return result
292
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900293
294def define_uprev_android_lkgb_handlers():
Alex Klein1699fab2022-09-08 08:46:06 -0600295 """Dynamically define uprev handlers for each Android package"""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900296
Alex Klein1699fab2022-09-08 08:46:06 -0600297 def define_handler(android_package):
298 """Defines the uprev handler for an Android package."""
299 full_package_name = "chromeos-base/" + android_package
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900300
Alex Klein1699fab2022-09-08 08:46:06 -0600301 @uprevs_versioned_package(full_package_name)
302 def _handler(build_targets, _refs, chroot):
303 return uprev_android_lkgb(android_package, build_targets, chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900304
Madeleine Hardt57722b72022-11-01 15:54:58 +0000305 for android_package in constants.ANDROID_ALL_PACKAGES:
Alex Klein1699fab2022-09-08 08:46:06 -0600306 define_handler(android_package)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900307
308
309define_uprev_android_lkgb_handlers()
Alex Klein4de25e82019-08-05 15:58:39 -0600310
311
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700312def uprev_build_targets(
Alex Klein1699fab2022-09-08 08:46:06 -0600313 build_targets: Optional[List["build_target_lib.BuildTarget"]],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700314 overlay_type: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600315 chroot: "chroot_lib.Chroot" = None,
316 output_dir: Optional[str] = None,
317):
318 """Uprev the set provided build targets, or all if not specified.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600319
Alex Klein1699fab2022-09-08 08:46:06 -0600320 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600321 build_targets: The build targets whose overlays should be uprevved,
322 empty or None for all.
323 overlay_type: One of the valid overlay types except None (see
324 constants.VALID_OVERLAYS).
325 chroot: The chroot to clean, if desired.
326 output_dir: The path to optionally dump result files.
Alex Klein1699fab2022-09-08 08:46:06 -0600327 """
328 # Need a valid overlay, but exclude None.
329 assert overlay_type and overlay_type in constants.VALID_OVERLAYS
Alex Kleineb77ffa2019-05-28 14:47:44 -0600330
Alex Klein1699fab2022-09-08 08:46:06 -0600331 if build_targets:
332 overlays = portage_util.FindOverlaysForBoards(
333 overlay_type, boards=[t.name for t in build_targets]
334 )
335 else:
336 overlays = portage_util.FindOverlays(overlay_type)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600337
Alex Klein1699fab2022-09-08 08:46:06 -0600338 return uprev_overlays(
339 overlays,
340 build_targets=build_targets,
341 chroot=chroot,
342 output_dir=output_dir,
343 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600344
345
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700346def uprev_overlays(
347 overlays: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -0600348 build_targets: Optional[List["build_target_lib.BuildTarget"]] = None,
349 chroot: Optional["chroot_lib.Chroot"] = None,
350 output_dir: Optional[str] = None,
351) -> List[str]:
352 """Uprev the given overlays.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600353
Alex Klein1699fab2022-09-08 08:46:06 -0600354 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600355 overlays: The list of overlay paths.
356 build_targets: The build targets to clean in |chroot|, if desired. No
357 effect unless |chroot| is provided.
358 chroot: The chroot to clean, if desired.
359 output_dir: The path to optionally dump result files.
Alex Kleineb77ffa2019-05-28 14:47:44 -0600360
Alex Klein1699fab2022-09-08 08:46:06 -0600361 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600362 The paths to all the modified ebuild files. This includes the new files
363 that were added (i.e. the new versions) and all the removed files
Alex Klein1699fab2022-09-08 08:46:06 -0600364 (i.e. the old versions).
365 """
366 assert overlays
Alex Kleineb77ffa2019-05-28 14:47:44 -0600367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT)
Alex Kleineb77ffa2019-05-28 14:47:44 -0600369
Alex Klein1699fab2022-09-08 08:46:06 -0600370 uprev_manager = uprev_lib.UprevOverlayManager(
371 overlays,
372 manifest,
373 build_targets=build_targets,
374 chroot=chroot,
375 output_dir=output_dir,
376 )
377 uprev_manager.uprev()
Alex Kleineb77ffa2019-05-28 14:47:44 -0600378
Alex Klein1699fab2022-09-08 08:46:06 -0600379 return uprev_manager.modified_ebuilds, uprev_manager.revved_packages
Alex Kleineb77ffa2019-05-28 14:47:44 -0600380
381
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700382def uprev_versioned_package(
383 package: package_info.CPV,
Alex Klein1699fab2022-09-08 08:46:06 -0600384 build_targets: List["build_target_lib.BuildTarget"],
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700385 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600386 chroot: "chroot_lib.Chroot",
387) -> "uprev_lib.UprevVersionedPackageResult":
388 """Call registered uprev handler function for the package.
Alex Klein87531182019-08-12 15:23:37 -0600389
Alex Klein1699fab2022-09-08 08:46:06 -0600390 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600391 package: The package being uprevved.
392 build_targets: The build targets to clean on a successful uprev.
393 refs:
394 chroot: The chroot to enter for cleaning.
Alex Klein87531182019-08-12 15:23:37 -0600395
Alex Klein1699fab2022-09-08 08:46:06 -0600396 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600397 The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600398 """
399 assert package
Alex Klein87531182019-08-12 15:23:37 -0600400
Alex Klein1699fab2022-09-08 08:46:06 -0600401 if package.cp not in _UPREV_FUNCS:
402 raise UnknownPackageError(
403 'Package "%s" does not have a registered handler.' % package.cp
404 )
Alex Klein87531182019-08-12 15:23:37 -0600405
Alex Klein1699fab2022-09-08 08:46:06 -0600406 return _UPREV_FUNCS[package.cp](build_targets, refs, chroot)
Alex Klein87531182019-08-12 15:23:37 -0600407
408
Alex Klein1699fab2022-09-08 08:46:06 -0600409@uprevs_versioned_package("media-libs/virglrenderer")
Navil Perezf57ba872020-06-04 22:38:37 +0000410def uprev_virglrenderer(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600411 """Updates virglrenderer ebuilds.
Navil Perezf57ba872020-06-04 22:38:37 +0000412
Alex Klein1699fab2022-09-08 08:46:06 -0600413 See: uprev_versioned_package.
Navil Perezf57ba872020-06-04 22:38:37 +0000414
Alex Klein1699fab2022-09-08 08:46:06 -0600415 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600416 UprevVersionedPackageResult: The result of updating virglrenderer
417 ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600418 """
419 overlay = os.path.join(
420 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
421 )
422 repo_path = os.path.join(
423 constants.SOURCE_ROOT, "src", "third_party", "virglrenderer"
424 )
425 manifest = git.ManifestCheckout.Cached(repo_path)
Navil Perezf57ba872020-06-04 22:38:37 +0000426
Alex Klein1699fab2022-09-08 08:46:06 -0600427 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
428 # TODO(crbug.com/1066242): Ebuilds for virglrenderer are currently
429 # denylisted. Do not force uprevs after builder is stable and ebuilds are no
430 # longer denylisted.
431 uprev_manager.uprev(package_list=["media-libs/virglrenderer"], force=True)
Navil Perezf57ba872020-06-04 22:38:37 +0000432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 updated_files = uprev_manager.modified_ebuilds
434 result = uprev_lib.UprevVersionedPackageResult()
435 result.add_result(refs[-1].revision, updated_files)
436 return result
Navil Perezf57ba872020-06-04 22:38:37 +0000437
Alex Klein1699fab2022-09-08 08:46:06 -0600438
439@uprevs_versioned_package("chromeos-base/drivefs")
Jose Magana03b5a842020-08-19 12:52:59 +1000440def uprev_drivefs(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600441 """Updates drivefs ebuilds.
Jose Magana03b5a842020-08-19 12:52:59 +1000442
Alex Klein1699fab2022-09-08 08:46:06 -0600443 DriveFS versions follow the tag format of refs/tags/drivefs_1.2.3.
444 See: uprev_versioned_package.
Jose Magana03b5a842020-08-19 12:52:59 +1000445
Alex Klein1699fab2022-09-08 08:46:06 -0600446 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600447 UprevVersionedPackageResult: The result of updating drivefs ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600448 """
Jose Magana03b5a842020-08-19 12:52:59 +1000449
Alex Klein1699fab2022-09-08 08:46:06 -0600450 DRIVEFS_PATH_PREFIX = "src/private-overlays/chromeos-overlay/chromeos-base"
451 result = uprev_lib.UprevVersionedPackageResult()
452 all_changed_files = []
Jose Magana03b5a842020-08-19 12:52:59 +1000453
Alex Klein1699fab2022-09-08 08:46:06 -0600454 DRIVEFS_REFS_PREFIX = "refs/tags/drivefs_"
455 drivefs_version = _get_latest_version_from_refs(DRIVEFS_REFS_PREFIX, refs)
456 if not drivefs_version:
457 # No valid DriveFS version is identified.
458 return result
459
460 logging.debug("DriveFS version determined from refs: %s", drivefs_version)
461
462 # Attempt to uprev drivefs package.
463 pkg_path = os.path.join(DRIVEFS_PATH_PREFIX, "drivefs")
464 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
465 pkg_path, drivefs_version, chroot, allow_downrev=False
466 )
467
468 if not uprev_result:
469 return result
470 all_changed_files.extend(uprev_result.changed_files)
471 result.add_result(drivefs_version, all_changed_files)
472
Ben Reich4f3fa1b2020-12-19 08:21:26 +0000473 return result
Jose Magana03b5a842020-08-19 12:52:59 +1000474
Jose Magana03b5a842020-08-19 12:52:59 +1000475
Alex Klein1699fab2022-09-08 08:46:06 -0600476@uprevs_versioned_package("chromeos-base/perfetto")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800477def uprev_perfetto(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600478 """Updates Perfetto ebuilds.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800479
Alex Klein1699fab2022-09-08 08:46:06 -0600480 Perfetto versions follow the tag format of refs/tags/v1.2.
481 See: uprev_versioned_package.
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800482
Alex Klein1699fab2022-09-08 08:46:06 -0600483 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600484 UprevVersionedPackageResult: The result of updating Perfetto ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -0600485 """
486 result = uprev_lib.UprevVersionedPackageResult()
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800487
Alex Klein1699fab2022-09-08 08:46:06 -0600488 PERFETTO_REFS_PREFIX = "refs/tags/v"
Chinglin Yuad12a512022-10-07 17:26:12 +0800489 PERFETTO_PATH = os.path.join(
490 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base/perfetto"
491 )
492
493 # Decide the version number to uprev to:
494 # * If |refs| contains refs/tags/v*, get the latest from them.
Alex Klein1699fab2022-09-08 08:46:06 -0600495 perfetto_version = _get_latest_version_from_refs(PERFETTO_REFS_PREFIX, refs)
Chinglin Yuad12a512022-10-07 17:26:12 +0800496 # * Or if |refs| contains only the latest trunk revisions, use the current
497 # stable ebuild version for a revision bump.
498 if refs and not perfetto_version:
499 perfetto_version = uprev_lib.get_stable_ebuild_version(PERFETTO_PATH)
500
Alex Klein1699fab2022-09-08 08:46:06 -0600501 if not perfetto_version:
502 # No valid Perfetto version is identified.
503 return result
504
Alex Klein1699fab2022-09-08 08:46:06 -0600505 # Attempt to uprev perfetto package.
Chinglin Yuad12a512022-10-07 17:26:12 +0800506 # |perfetto_version| is only used in determining the ebuild version. The
507 # package is always updated to the latest HEAD.
Alex Klein1699fab2022-09-08 08:46:06 -0600508 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
509 PERFETTO_PATH,
510 perfetto_version,
511 chroot,
512 allow_downrev=False,
Chinglin Yu84818732022-10-03 12:03:43 +0800513 # Use default ref="HEAD"
Alex Klein1699fab2022-09-08 08:46:06 -0600514 )
515
516 if not uprev_result:
517 return result
518
519 result.add_result(perfetto_version, uprev_result.changed_files)
520
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800521 return result
522
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800523
Denis Nikitin63613e32022-09-09 22:26:50 -0700524class AfdoMetadata(NamedTuple):
525 """Data class holding AFDO metadata."""
526
527 var_name: str
528 path: str
529
530
Alex Klein1699fab2022-09-08 08:46:06 -0600531@uprevs_versioned_package("afdo/kernel-profiles")
Yaakov Shaul395ae832019-09-09 14:45:32 -0600532def uprev_kernel_afdo(*_args, **_kwargs):
Alex Klein1699fab2022-09-08 08:46:06 -0600533 """Updates kernel ebuilds with versions from kernel_afdo.json.
Yaakov Shaul395ae832019-09-09 14:45:32 -0600534
Alex Klein1699fab2022-09-08 08:46:06 -0600535 See: uprev_versioned_package.
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600536
Alex Klein1699fab2022-09-08 08:46:06 -0600537 Raises:
Alex Klein348e7692022-10-13 17:03:37 -0600538 EbuildManifestError: When ebuild manifest does not complete
539 successfully.
540 JSONDecodeError: When json is malformed.
Alex Klein1699fab2022-09-08 08:46:06 -0600541 """
Denis Nikitin63613e32022-09-09 22:26:50 -0700542 metadata_dir = os.path.join(
Alex Klein1699fab2022-09-08 08:46:06 -0600543 constants.SOURCE_ROOT,
544 "src",
545 "third_party",
546 "toolchain-utils",
547 "afdo_metadata",
Denis Nikitin63613e32022-09-09 22:26:50 -0700548 )
549 metadata_files = (
550 AfdoMetadata(
551 var_name="AFDO_PROFILE_VERSION",
552 path=os.path.join(metadata_dir, "kernel_afdo.json"),
553 ),
554 AfdoMetadata(
555 var_name="ARM_AFDO_PROFILE_VERSION",
556 path=os.path.join(metadata_dir, "kernel_arm_afdo.json"),
557 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600558 )
Yaakov Shaul395ae832019-09-09 14:45:32 -0600559
Alex Klein1699fab2022-09-08 08:46:06 -0600560 result = uprev_lib.UprevVersionedPackageResult()
Denis Nikitin63613e32022-09-09 22:26:50 -0700561 for metadata in metadata_files:
562 with open(metadata.path, "r") as f:
563 versions = json.load(f)
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600564
Denis Nikitin63613e32022-09-09 22:26:50 -0700565 for kernel_pkg, version_info in versions.items():
566 path = os.path.join(
567 constants.CHROMIUMOS_OVERLAY_DIR, "sys-kernel", kernel_pkg
568 )
569 ebuild_path = os.path.join(
570 constants.SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
571 )
572 chroot_ebuild_path = os.path.join(
573 constants.CHROOT_SOURCE_ROOT, path, f"{kernel_pkg}-9999.ebuild"
574 )
575 afdo_profile_version = version_info["name"]
576 patch_ebuild_vars(
577 ebuild_path, {metadata.var_name: afdo_profile_version}
Alex Klein1699fab2022-09-08 08:46:06 -0600578 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600579
Denis Nikitin63613e32022-09-09 22:26:50 -0700580 try:
581 cmd = ["ebuild", chroot_ebuild_path, "manifest", "--force"]
582 cros_build_lib.run(cmd, enter_chroot=True)
583 except cros_build_lib.RunCommandError as e:
584 raise uprev_lib.EbuildManifestError(
585 "Error encountered when regenerating the manifest for "
586 f"ebuild: {chroot_ebuild_path}\n{e}",
587 e,
588 )
Yaakov Shaul1eafe832019-09-10 16:50:26 -0600589
Denis Nikitin63613e32022-09-09 22:26:50 -0700590 manifest_path = os.path.join(
591 constants.SOURCE_ROOT, path, "Manifest"
592 )
593 result.add_result(
594 afdo_profile_version, [ebuild_path, manifest_path]
595 )
Yaakov Shaul730814a2019-09-10 13:58:25 -0600596
Alex Klein1699fab2022-09-08 08:46:06 -0600597 return result
Yaakov Shaul395ae832019-09-09 14:45:32 -0600598
599
Alex Klein1699fab2022-09-08 08:46:06 -0600600@uprevs_versioned_package("chromeos-base/termina-dlc")
601@uprevs_versioned_package("chromeos-base/termina-tools-dlc")
Maciek Swiech6b12f662022-01-25 16:51:19 +0000602def uprev_termina_dlcs(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600603 """Updates shared termina-dlc and termina-tools-dlc ebuilds.
Maciek Swiech6b12f662022-01-25 16:51:19 +0000604
Alex Klein1699fab2022-09-08 08:46:06 -0600605 termina-dlc - chromeos-base/termina-dlc
606 termina-tools-dlc - chromeos-base/termina-tools-dlc
Trent Beginaf51f1b2020-03-09 17:35:31 -0600607
Alex Klein1699fab2022-09-08 08:46:06 -0600608 See: uprev_versioned_package.
609 """
610 termina_dlc_pkg = "termina-dlc"
611 termina_dlc_pkg_path = os.path.join(
612 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", termina_dlc_pkg
613 )
614 tools_dlc_pkg = "termina-tools-dlc"
615 tools_dlc_pkg_path = os.path.join(
616 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", tools_dlc_pkg
617 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000618
Alex Klein1699fab2022-09-08 08:46:06 -0600619 # termina-dlc and termina-tools-dlc are pinned to the same version.
620 version_pin_src_path = _get_version_pin_src_path(termina_dlc_pkg_path)
621 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Patrick Meiring5897add2020-09-16 16:30:17 +1000622
Alex Klein1699fab2022-09-08 08:46:06 -0600623 result = uprev_lib.uprev_ebuild_from_pin(
624 termina_dlc_pkg_path, version_no_rev, chroot
625 )
626 result += uprev_lib.uprev_ebuild_from_pin(
627 tools_dlc_pkg_path, version_no_rev, chroot
628 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000629
Alex Klein1699fab2022-09-08 08:46:06 -0600630 return result
Patrick Meiring5897add2020-09-16 16:30:17 +1000631
Alex Klein1699fab2022-09-08 08:46:06 -0600632
633@uprevs_versioned_package("chromeos-base/chromeos-lacros")
Julio Hurtadof1befec2021-05-05 21:34:26 +0000634def uprev_lacros(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600635 """Updates lacros ebuilds.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000636
Alex Klein1699fab2022-09-08 08:46:06 -0600637 Version to uprev to is gathered from the QA qualified version tracking file
638 stored in chromium/src/chrome/LACROS_QA_QUALIFIED_VERSION. Uprev is triggered
639 on modification of this file across all chromium/src branches.
Julio Hurtadof1befec2021-05-05 21:34:26 +0000640
Alex Klein1699fab2022-09-08 08:46:06 -0600641 See: uprev_versioned_package.
642 """
643 result = uprev_lib.UprevVersionedPackageResult()
644 path = os.path.join(
645 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
646 )
647 lacros_version = refs[0].revision
648 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
649 path, lacros_version, chroot, allow_downrev=False
650 )
Julio Hurtadoa994e002021-07-07 17:57:45 +0000651
Alex Klein1699fab2022-09-08 08:46:06 -0600652 if not uprev_result:
653 return result
654
655 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtadoa994e002021-07-07 17:57:45 +0000656 return result
657
Julio Hurtadof1befec2021-05-05 21:34:26 +0000658
Alex Klein1699fab2022-09-08 08:46:06 -0600659@uprevs_versioned_package("chromeos-base/chromeos-lacros-parallel")
Julio Hurtado870ed322021-12-03 18:22:40 +0000660def uprev_lacros_in_parallel(
Alex Klein1699fab2022-09-08 08:46:06 -0600661 _build_targets: Optional[List["build_target_lib.BuildTarget"]],
Julio Hurtado870ed322021-12-03 18:22:40 +0000662 refs: List[uprev_lib.GitRef],
Alex Klein1699fab2022-09-08 08:46:06 -0600663 chroot: "chroot_lib.Chroot",
664) -> "uprev_lib.UprevVersionedPackageResult":
665 """Updates lacros ebuilds in parallel with ash-chrome.
Julio Hurtado870ed322021-12-03 18:22:40 +0000666
Alex Klein1699fab2022-09-08 08:46:06 -0600667 This handler is going to be used temporarily while lacros transitions to being
668 uprevved atomically with ash-chrome. Unlike a standalone lacros uprev, this
669 handler will not need to look at the QA qualified file. Rather, it will
670 function identical to ash-chrome using git tags.
Julio Hurtado870ed322021-12-03 18:22:40 +0000671
Alex Klein1699fab2022-09-08 08:46:06 -0600672 See: uprev_versioned_package.
Julio Hurtado870ed322021-12-03 18:22:40 +0000673
Alex Klein1699fab2022-09-08 08:46:06 -0600674 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600675 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600676 """
677 result = uprev_lib.UprevVersionedPackageResult()
678 path = os.path.join(
679 constants.CHROMIUMOS_OVERLAY_DIR, "chromeos-base", "chromeos-lacros"
680 )
681 lacros_version = uprev_lib.get_version_from_refs(refs)
682 uprev_result = uprev_lib.uprev_workon_ebuild_to_version(
683 path, lacros_version, chroot, allow_downrev=False
684 )
Julio Hurtado870ed322021-12-03 18:22:40 +0000685
Alex Klein1699fab2022-09-08 08:46:06 -0600686 if not uprev_result:
687 return result
688
689 result.add_result(lacros_version, uprev_result.changed_files)
Julio Hurtado870ed322021-12-03 18:22:40 +0000690 return result
691
Julio Hurtado870ed322021-12-03 18:22:40 +0000692
Alex Klein1699fab2022-09-08 08:46:06 -0600693@uprevs_versioned_package("app-emulation/parallels-desktop")
Patrick Meiring5897add2020-09-16 16:30:17 +1000694def uprev_parallels_desktop(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600695 """Updates Parallels Desktop ebuild - app-emulation/parallels-desktop.
Patrick Meiring5897add2020-09-16 16:30:17 +1000696
Alex Klein1699fab2022-09-08 08:46:06 -0600697 See: uprev_versioned_package
Patrick Meiring5897add2020-09-16 16:30:17 +1000698
Alex Klein1699fab2022-09-08 08:46:06 -0600699 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600700 UprevVersionedPackageResult: The result.
Alex Klein1699fab2022-09-08 08:46:06 -0600701 """
702 package = "parallels-desktop"
703 package_path = os.path.join(
704 constants.CHROMEOS_PARTNER_OVERLAY_DIR, "app-emulation", package
705 )
706 version_pin_src_path = _get_version_pin_src_path(package_path)
Patrick Meiring5897add2020-09-16 16:30:17 +1000707
Alex Klein1699fab2022-09-08 08:46:06 -0600708 # Expect a JSON blob like the following:
709 # {
710 # "version": "1.2.3",
711 # "test_image": { "url": "...", "size": 12345678,
712 # "sha256sum": "<32 bytes of hexadecimal>" }
713 # }
714 with open(version_pin_src_path, "r") as f:
715 pinned = json.load(f)
Patrick Meiring5897add2020-09-16 16:30:17 +1000716
Alex Klein1699fab2022-09-08 08:46:06 -0600717 if "version" not in pinned or "test_image" not in pinned:
718 raise UprevError(
719 "VERSION-PIN for %s missing version and/or "
720 "test_image field" % package
721 )
Patrick Meiring5897add2020-09-16 16:30:17 +1000722
Alex Klein1699fab2022-09-08 08:46:06 -0600723 version = pinned["version"]
724 if not isinstance(version, str):
725 raise UprevError("version in VERSION-PIN for %s not a string" % package)
Patrick Meiring5897add2020-09-16 16:30:17 +1000726
Alex Klein1699fab2022-09-08 08:46:06 -0600727 # Update the ebuild.
728 result = uprev_lib.uprev_ebuild_from_pin(package_path, version, chroot)
Patrick Meiring5897add2020-09-16 16:30:17 +1000729
Alex Klein1699fab2022-09-08 08:46:06 -0600730 # Update the VM image used for testing.
731 test_image_path = (
732 "src/platform/tast-tests-private/src/chromiumos/tast/"
733 "local/bundles/crosint/pita/data/"
734 "pluginvm_image.zip.external"
735 )
736 test_image_src_path = os.path.join(constants.SOURCE_ROOT, test_image_path)
737 with open(test_image_src_path, "w") as f:
738 json.dump(pinned["test_image"], f, indent=2)
739 result.add_result(version, [test_image_src_path])
Patrick Meiring5897add2020-09-16 16:30:17 +1000740
Alex Klein1699fab2022-09-08 08:46:06 -0600741 return result
Trent Beginaf51f1b2020-03-09 17:35:31 -0600742
743
Alex Klein1699fab2022-09-08 08:46:06 -0600744@uprevs_versioned_package("chromeos-base/chromeos-dtc-vm")
Trent Beginaf51f1b2020-03-09 17:35:31 -0600745def uprev_sludge(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600746 """Updates sludge VM - chromeos-base/chromeos-dtc-vm.
Trent Begin315d9d92019-12-03 21:55:53 -0700747
Alex Klein1699fab2022-09-08 08:46:06 -0600748 See: uprev_versioned_package.
749 """
750 package = "chromeos-dtc-vm"
751 package_path = os.path.join(
752 "src",
753 "private-overlays",
754 "project-wilco-private",
755 "chromeos-base",
756 package,
757 )
758 version_pin_src_path = _get_version_pin_src_path(package_path)
759 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
Trent Begin315d9d92019-12-03 21:55:53 -0700760
Alex Klein1699fab2022-09-08 08:46:06 -0600761 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
Trent Begin315d9d92019-12-03 21:55:53 -0700762
763
Alex Klein1699fab2022-09-08 08:46:06 -0600764@uprevs_versioned_package("chromeos-base/borealis-dlc")
David Riley8513c1f2021-10-14 17:07:41 -0700765def uprev_borealis_dlc(_build_targets, _refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600766 """Updates shared borealis-dlc ebuild - chromeos-base/borealis-dlc.
David Riley8513c1f2021-10-14 17:07:41 -0700767
Alex Klein1699fab2022-09-08 08:46:06 -0600768 See: uprev_versioned_package.
769 """
770 package_path = os.path.join(
771 "src",
772 "private-overlays",
773 "chromeos-partner-overlay",
774 "chromeos-base",
775 "borealis-dlc",
776 )
David Riley8513c1f2021-10-14 17:07:41 -0700777
Alex Klein1699fab2022-09-08 08:46:06 -0600778 version_pin_src_path = _get_version_pin_src_path(package_path)
779 version_no_rev = osutils.ReadFile(version_pin_src_path).strip()
David Riley8513c1f2021-10-14 17:07:41 -0700780
Alex Klein1699fab2022-09-08 08:46:06 -0600781 return uprev_lib.uprev_ebuild_from_pin(package_path, version_no_rev, chroot)
David Riley8513c1f2021-10-14 17:07:41 -0700782
783
Patrick Meiring5897add2020-09-16 16:30:17 +1000784def _get_version_pin_src_path(package_path):
Alex Klein1699fab2022-09-08 08:46:06 -0600785 """Returns the path to the VERSION-PIN file for the given package."""
786 return os.path.join(constants.SOURCE_ROOT, package_path, "VERSION-PIN")
Patrick Meiring5897add2020-09-16 16:30:17 +1000787
788
Alex Klein87531182019-08-12 15:23:37 -0600789@uprevs_versioned_package(constants.CHROME_CP)
Alex Klein4e839252022-01-06 13:29:18 -0700790def uprev_chrome_from_ref(build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -0600791 """Uprev chrome and its related packages.
Alex Klein87531182019-08-12 15:23:37 -0600792
Alex Klein1699fab2022-09-08 08:46:06 -0600793 See: uprev_versioned_package.
794 """
795 # Determine the version from the refs (tags), i.e. the chrome versions are the
796 # tag names.
797 chrome_version = uprev_lib.get_version_from_refs(refs)
798 logging.debug("Chrome version determined from refs: %s", chrome_version)
Alex Klein87531182019-08-12 15:23:37 -0600799
Alex Klein1699fab2022-09-08 08:46:06 -0600800 return uprev_chrome(chrome_version, build_targets, None)
Alex Kleinf69bd802021-06-22 15:43:49 -0600801
802
Alex Klein9ce3f682021-06-23 15:06:44 -0600803def revbump_chrome(
Alex Klein1699fab2022-09-08 08:46:06 -0600804 build_targets: List["build_target_lib.BuildTarget"] = None,
805 chroot: Optional["chroot_lib.Chroot"] = None,
Alex Klein9ce3f682021-06-23 15:06:44 -0600806) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600807 """Attempt to revbump chrome.
Alex Kleinf69bd802021-06-22 15:43:49 -0600808
Alex Klein1699fab2022-09-08 08:46:06 -0600809 Revbumps are done by executing an uprev using the current stable version.
810 E.g. if chrome is on 1.2.3.4 and has a 1.2.3.4_rc-r2.ebuild, performing an
811 uprev on version 1.2.3.4 when there are applicable changes (e.g. to the 9999
812 ebuild) will result in a revbump to 1.2.3.4_rc-r3.ebuild.
813 """
814 chrome_version = uprev_lib.get_stable_chrome_version()
815 return uprev_chrome(chrome_version, build_targets, chroot)
Alex Kleinf69bd802021-06-22 15:43:49 -0600816
817
Alex Klein9ce3f682021-06-23 15:06:44 -0600818def uprev_chrome(
Alex Klein16ea1b32021-10-01 15:48:50 -0600819 chrome_version: str,
Alex Klein1699fab2022-09-08 08:46:06 -0600820 build_targets: Optional[List["build_target_lib.BuildTarget"]],
821 chroot: Optional["chroot_lib.Chroot"],
Alex Klein9ce3f682021-06-23 15:06:44 -0600822) -> uprev_lib.UprevVersionedPackageResult:
Alex Klein1699fab2022-09-08 08:46:06 -0600823 """Attempt to uprev chrome and its related packages to the given version."""
824 uprev_manager = uprev_lib.UprevChromeManager(
825 chrome_version, build_targets=build_targets, chroot=chroot
826 )
827 result = uprev_lib.UprevVersionedPackageResult()
828 # TODO(crbug.com/1080429): Handle all possible outcomes of a Chrome uprev
829 # attempt. The expected behavior is documented in the following table:
830 #
831 # Outcome of Chrome uprev attempt:
832 # NEWER_VERSION_EXISTS:
833 # Do nothing.
834 # SAME_VERSION_EXISTS or REVISION_BUMP:
835 # Uprev followers
836 # Assert not VERSION_BUMP (any other outcome is fine)
837 # VERSION_BUMP or NEW_EBUILD_CREATED:
838 # Uprev followers
839 # Assert that Chrome & followers are at same package version
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600840
Alex Klein1699fab2022-09-08 08:46:06 -0600841 # Start with chrome itself so we can proceed accordingly.
842 chrome_result = uprev_manager.uprev(constants.CHROME_CP)
843 if chrome_result.newer_version_exists:
844 # Cannot use the given version (newer version already exists).
845 return result
846
847 # Also uprev related packages.
848 for package in constants.OTHER_CHROME_PACKAGES:
849 follower_result = uprev_manager.uprev(package)
850 if chrome_result.stable_version and follower_result.version_bump:
851 logging.warning(
852 "%s had a version bump, but no more than a revision bump "
853 "should have been possible.",
854 package,
855 )
856
857 if uprev_manager.modified_ebuilds:
858 # Record changes when we have them.
859 return result.add_result(chrome_version, uprev_manager.modified_ebuilds)
860
David Burger37f48672019-09-18 17:07:56 -0600861 return result
Alex Klein87531182019-08-12 15:23:37 -0600862
Alex Klein87531182019-08-12 15:23:37 -0600863
Alex Klein1699fab2022-09-08 08:46:06 -0600864def _get_latest_version_from_refs(
865 refs_prefix: str, refs: List[uprev_lib.GitRef]
866) -> str:
867 """Get the latest version from refs
Alex Klein0b2ec2d2021-06-23 15:56:45 -0600868
Alex Klein1699fab2022-09-08 08:46:06 -0600869 Versions are compared using |distutils.version.LooseVersion| and
870 the latest version is returned.
Alex Klein87531182019-08-12 15:23:37 -0600871
Alex Klein1699fab2022-09-08 08:46:06 -0600872 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600873 refs_prefix: The refs prefix of the tag format.
874 refs: The tags to parse for the latest version.
Alex Klein87531182019-08-12 15:23:37 -0600875
Alex Klein1699fab2022-09-08 08:46:06 -0600876 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600877 The latest version to use as string.
Alex Klein1699fab2022-09-08 08:46:06 -0600878 """
879 valid_refs = []
880 for gitiles in refs:
881 if gitiles.ref.startswith(refs_prefix):
882 valid_refs.append(gitiles.ref)
Ben Reiche779cf42020-12-15 03:21:31 +0000883
Alex Klein1699fab2022-09-08 08:46:06 -0600884 if not valid_refs:
885 return None
Ben Reiche779cf42020-12-15 03:21:31 +0000886
Alex Klein1699fab2022-09-08 08:46:06 -0600887 # Sort by version and take the latest version.
888 target_version_ref = sorted(valid_refs, key=LooseVersion, reverse=True)[0]
889 return target_version_ref.replace(refs_prefix, "")
Harvey Yang9c61e9c2021-03-02 16:32:43 +0800890
891
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700892def _generate_platform_c_files(
893 replication_config: replication_config_pb2.ReplicationConfig,
Alex Klein1699fab2022-09-08 08:46:06 -0600894 chroot: "chroot_lib.Chroot",
895) -> List[str]:
896 """Generates platform C files from a platform JSON payload.
Andrew Lamb9563a152019-12-04 11:42:18 -0700897
Alex Klein1699fab2022-09-08 08:46:06 -0600898 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600899 replication_config: A ReplicationConfig that has already been run. If it
900 produced a build_config.json file, that file will be used to
901 generate platform C files. Otherwise, nothing will be generated.
902 chroot: The chroot to use to generate.
Andrew Lamb9563a152019-12-04 11:42:18 -0700903
Alex Klein1699fab2022-09-08 08:46:06 -0600904 Returns:
Alex Klein348e7692022-10-13 17:03:37 -0600905 A list of generated files.
Alex Klein1699fab2022-09-08 08:46:06 -0600906 """
907 # Generate the platform C files from the build config. Note that it would be
908 # more intuitive to generate the platform C files from the platform config;
909 # however, cros_config_schema does not allow this, because the platform config
910 # payload is not always valid input. For example, if a property is both
911 # 'required' and 'build-only', it will fail schema validation. Thus, use the
912 # build config, and use '-f' to filter.
913 build_config_path = [
914 rule.destination_path
915 for rule in replication_config.file_replication_rules
916 if rule.destination_path.endswith("build_config.json")
917 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700918
Alex Klein1699fab2022-09-08 08:46:06 -0600919 if not build_config_path:
920 logging.info(
921 "No build_config.json found, will not generate platform C files. "
922 "Replication config: %s",
923 replication_config,
924 )
925 return []
Andrew Lamb9563a152019-12-04 11:42:18 -0700926
Alex Klein1699fab2022-09-08 08:46:06 -0600927 if len(build_config_path) > 1:
928 raise ValueError(
929 "Expected at most one build_config.json destination path. "
930 "Replication config: %s" % replication_config
931 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700932
Alex Klein1699fab2022-09-08 08:46:06 -0600933 build_config_path = build_config_path[0]
Andrew Lamb9563a152019-12-04 11:42:18 -0700934
Alex Klein1699fab2022-09-08 08:46:06 -0600935 # Paths to the build_config.json and dir to output C files to, in the
936 # chroot.
937 build_config_chroot_path = os.path.join(
938 constants.CHROOT_SOURCE_ROOT, build_config_path
939 )
940 generated_output_chroot_dir = os.path.join(
941 constants.CHROOT_SOURCE_ROOT, os.path.dirname(build_config_path)
942 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700943
Alex Klein1699fab2022-09-08 08:46:06 -0600944 command = [
945 "cros_config_schema",
946 "-m",
947 build_config_chroot_path,
948 "-g",
949 generated_output_chroot_dir,
950 "-f",
951 '"TRUE"',
952 ]
Andrew Lamb9563a152019-12-04 11:42:18 -0700953
Alex Klein1699fab2022-09-08 08:46:06 -0600954 cros_build_lib.run(
955 command, enter_chroot=True, chroot_args=chroot.get_enter_args()
956 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700957
Alex Klein1699fab2022-09-08 08:46:06 -0600958 # A relative (to the source root) path to the generated C files.
959 generated_output_dir = os.path.dirname(build_config_path)
960 generated_files = []
961 expected_c_files = ["config.c", "ec_config.c", "ec_config.h"]
962 for f in expected_c_files:
963 if os.path.exists(
964 os.path.join(constants.SOURCE_ROOT, generated_output_dir, f)
965 ):
966 generated_files.append(os.path.join(generated_output_dir, f))
Andrew Lamb9563a152019-12-04 11:42:18 -0700967
Alex Klein1699fab2022-09-08 08:46:06 -0600968 if len(expected_c_files) != len(generated_files):
969 raise GeneratedCrosConfigFilesError(expected_c_files, generated_files)
Andrew Lamb9563a152019-12-04 11:42:18 -0700970
Alex Klein1699fab2022-09-08 08:46:06 -0600971 return generated_files
Andrew Lamb9563a152019-12-04 11:42:18 -0700972
973
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -0700974def _get_private_overlay_package_root(ref: uprev_lib.GitRef, package: str):
Alex Klein1699fab2022-09-08 08:46:06 -0600975 """Returns the absolute path to the root of a given private overlay.
Andrew Lambe836f222019-12-09 12:27:38 -0700976
Alex Klein1699fab2022-09-08 08:46:06 -0600977 Args:
Alex Klein348e7692022-10-13 17:03:37 -0600978 ref: GitRef for the private overlay.
979 package: Path to the package in the overlay.
Alex Klein1699fab2022-09-08 08:46:06 -0600980 """
981 # There might be a cleaner way to map from package -> path within the source
982 # tree. For now, just use string patterns.
983 private_overlay_ref_pattern = (
984 r"/chromeos\/overlays\/overlay-([\w-]+)-private"
985 )
986 match = re.match(private_overlay_ref_pattern, ref.path)
987 if not match:
988 raise ValueError(
989 "ref.path must match the pattern: %s. Actual ref: %s"
990 % (private_overlay_ref_pattern, ref)
991 )
Andrew Lambe836f222019-12-09 12:27:38 -0700992
Alex Klein1699fab2022-09-08 08:46:06 -0600993 overlay = match.group(1)
Andrew Lambe836f222019-12-09 12:27:38 -0700994
Alex Klein1699fab2022-09-08 08:46:06 -0600995 return os.path.join(
996 constants.SOURCE_ROOT,
997 "src/private-overlays/overlay-%s-private" % overlay,
998 package,
999 )
Andrew Lambe836f222019-12-09 12:27:38 -07001000
1001
Alex Klein1699fab2022-09-08 08:46:06 -06001002@uprevs_versioned_package("chromeos-base/chromeos-config-bsp")
Andrew Lambea9a8a22019-12-12 14:03:43 -07001003def replicate_private_config(_build_targets, refs, chroot):
Alex Klein1699fab2022-09-08 08:46:06 -06001004 """Replicate a private cros_config change to the corresponding public config.
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001005
Alex Klein1699fab2022-09-08 08:46:06 -06001006 See uprev_versioned_package for args
1007 """
1008 package = "chromeos-base/chromeos-config-bsp"
Andrew Lambea9a8a22019-12-12 14:03:43 -07001009
Alex Klein1699fab2022-09-08 08:46:06 -06001010 if len(refs) != 1:
1011 raise ValueError("Expected exactly one ref, actual %s" % refs)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001012
Alex Klein1699fab2022-09-08 08:46:06 -06001013 # Expect a replication_config.jsonpb in the package root.
1014 package_root = _get_private_overlay_package_root(refs[0], package)
1015 replication_config_path = os.path.join(
1016 package_root, "replication_config.jsonpb"
1017 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001018
Alex Klein1699fab2022-09-08 08:46:06 -06001019 try:
1020 replication_config = json_format.Parse(
1021 osutils.ReadFile(replication_config_path),
1022 replication_config_pb2.ReplicationConfig(),
1023 )
1024 except IOError:
1025 raise ValueError(
1026 "Expected ReplicationConfig missing at %s" % replication_config_path
1027 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001028
Alex Klein1699fab2022-09-08 08:46:06 -06001029 replication_lib.Replicate(replication_config)
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001030
Alex Klein1699fab2022-09-08 08:46:06 -06001031 modified_files = [
1032 rule.destination_path
1033 for rule in replication_config.file_replication_rules
1034 ]
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001035
Alex Klein1699fab2022-09-08 08:46:06 -06001036 # The generated platform C files are not easily filtered by replication rules,
1037 # i.e. JSON / proto filtering can be described by a FieldMask, arbitrary C
1038 # files cannot. Therefore, replicate and filter the JSON payloads, and then
1039 # generate filtered C files from the JSON payload.
1040 modified_files.extend(
1041 _generate_platform_c_files(replication_config, chroot)
1042 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001043
Alex Klein1699fab2022-09-08 08:46:06 -06001044 # Use the private repo's commit hash as the new version.
1045 new_private_version = refs[0].revision
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001046
Alex Klein1699fab2022-09-08 08:46:06 -06001047 # modified_files should contain only relative paths at this point, but the
1048 # returned UprevVersionedPackageResult must contain only absolute paths.
1049 for i, modified_file in enumerate(modified_files):
1050 assert not os.path.isabs(modified_file)
1051 modified_files[i] = os.path.join(constants.SOURCE_ROOT, modified_file)
Andrew Lamb988f4da2019-12-10 10:16:43 -07001052
Alex Klein1699fab2022-09-08 08:46:06 -06001053 return uprev_lib.UprevVersionedPackageResult().add_result(
1054 new_private_version, modified_files
1055 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -07001056
1057
Alex Klein1699fab2022-09-08 08:46:06 -06001058@uprevs_versioned_package("chromeos-base/crosvm")
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001059def uprev_crosvm(_build_targets, refs, _chroot):
Alex Klein1699fab2022-09-08 08:46:06 -06001060 """Updates crosvm ebuilds to latest revision
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001061
Alex Klein1699fab2022-09-08 08:46:06 -06001062 crosvm is not versioned. We are updating to the latest commit on the main
1063 branch.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001064
Alex Klein1699fab2022-09-08 08:46:06 -06001065 See: uprev_versioned_package.
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001066
Alex Klein1699fab2022-09-08 08:46:06 -06001067 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001068 UprevVersionedPackageResult: The result of updating crosvm ebuilds.
Alex Klein1699fab2022-09-08 08:46:06 -06001069 """
1070 overlay = os.path.join(
1071 constants.SOURCE_ROOT, constants.CHROMIUMOS_OVERLAY_DIR
1072 )
1073 repo_path = os.path.join(constants.SOURCE_ROOT, "src", "crosvm")
1074 manifest = git.ManifestCheckout.Cached(repo_path)
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001075
Alex Klein1699fab2022-09-08 08:46:06 -06001076 uprev_manager = uprev_lib.UprevOverlayManager([overlay], manifest)
1077 uprev_manager.uprev(
1078 package_list=[
1079 "chromeos-base/crosvm",
1080 "dev-rust/assertions",
1081 "dev-rust/cros_async",
1082 "dev-rust/cros_fuzz",
1083 "dev-rust/data_model",
1084 "dev-rust/enumn",
1085 "dev-rust/io_uring",
1086 "dev-rust/p9",
1087 "dev-rust/sync",
1088 "dev-rust/sys_util",
1089 "dev-rust/tempfile",
1090 "media-sound/audio_streams",
1091 ],
1092 force=True,
1093 )
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001094
Alex Klein1699fab2022-09-08 08:46:06 -06001095 updated_files = uprev_manager.modified_ebuilds
1096 result = uprev_lib.UprevVersionedPackageResult()
1097 result.add_result(refs[0].revision, updated_files)
1098 return result
Dennis Kempinef05f2b2021-09-08 16:36:49 -07001099
1100
Alex Klein5caab872021-09-10 11:44:37 -06001101def get_best_visible(
Alex Klein1699fab2022-09-08 08:46:06 -06001102 atom: str, build_target: Optional["build_target_lib.BuildTarget"] = None
Alex Klein5caab872021-09-10 11:44:37 -06001103) -> package_info.PackageInfo:
Alex Klein1699fab2022-09-08 08:46:06 -06001104 """Returns the best visible CPV for the given atom.
Alex Kleinbbef2b32019-08-27 10:38:50 -06001105
Alex Klein1699fab2022-09-08 08:46:06 -06001106 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001107 atom: The atom to look up.
1108 build_target: The build target whose sysroot should be searched, or the
1109 SDK if not provided.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001110
Alex Klein1699fab2022-09-08 08:46:06 -06001111 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001112 The best visible package, or None if none are visible.
Alex Klein1699fab2022-09-08 08:46:06 -06001113 """
1114 assert atom
Alex Kleinbbef2b32019-08-27 10:38:50 -06001115
Alex Klein1699fab2022-09-08 08:46:06 -06001116 return portage_util.PortageqBestVisible(
1117 atom,
1118 board=build_target.name if build_target else None,
1119 sysroot=build_target.root if build_target else None,
1120 )
Alex Kleinda39c6d2019-09-16 14:36:36 -06001121
1122
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001123def has_prebuilt(
1124 atom: str,
Alex Klein1699fab2022-09-08 08:46:06 -06001125 build_target: "build_target_lib.BuildTarget" = None,
1126 useflags: Union[Iterable[str], str] = None,
1127) -> bool:
1128 """Check if a prebuilt exists.
Alex Kleinda39c6d2019-09-16 14:36:36 -06001129
Alex Klein1699fab2022-09-08 08:46:06 -06001130 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001131 atom: The package whose prebuilt is being queried.
1132 build_target: The build target whose sysroot should be searched, or the
1133 SDK if not provided.
1134 useflags: Any additional USE flags that should be set. May be a string
1135 of properly formatted USE flags, or an iterable of individual flags.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001136
Alex Klein1699fab2022-09-08 08:46:06 -06001137 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001138 True if there is an available prebuilt, False otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -06001139 """
1140 assert atom
Alex Kleinda39c6d2019-09-16 14:36:36 -06001141
Alex Klein1699fab2022-09-08 08:46:06 -06001142 board = build_target.name if build_target else None
1143 extra_env = None
1144 if useflags:
1145 new_flags = useflags
1146 if not isinstance(useflags, str):
1147 new_flags = " ".join(useflags)
Alex Klein149fd3b2019-12-16 16:01:05 -07001148
Alex Klein1699fab2022-09-08 08:46:06 -06001149 existing = os.environ.get("USE", "")
1150 final_flags = "%s %s" % (existing, new_flags)
1151 extra_env = {"USE": final_flags.strip()}
1152 return portage_util.HasPrebuilt(atom, board=board, extra_env=extra_env)
Alex Klein36b117f2019-09-30 15:13:46 -06001153
1154
David Burger0f9dd4e2019-10-08 12:33:42 -06001155def builds(atom, build_target, packages=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001156 """Check if |build_target| builds |atom| (has it in its depgraph)."""
1157 cros_build_lib.AssertInsideChroot()
Alex Klein36b117f2019-09-30 15:13:46 -06001158
Alex Klein1699fab2022-09-08 08:46:06 -06001159 pkgs = tuple(packages) if packages else None
1160 # TODO(crbug/1081828): Receive and use sysroot.
1161 graph, _sdk_graph = dependency.GetBuildDependency(
1162 build_target.root, build_target.name, pkgs
1163 )
1164 return any(atom in package for package in graph["package_deps"])
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001165
1166
Alex Klein6becabc2020-09-11 14:03:05 -06001167def needs_chrome_source(
Alex Klein1699fab2022-09-08 08:46:06 -06001168 build_target: "build_target_lib.BuildTarget",
Alex Klein6becabc2020-09-11 14:03:05 -06001169 compile_source=False,
1170 packages: Optional[List[package_info.PackageInfo]] = None,
Alex Klein1699fab2022-09-08 08:46:06 -06001171 useflags=None,
1172):
1173 """Check if the chrome source is needed.
Alex Klein6becabc2020-09-11 14:03:05 -06001174
Alex Klein1699fab2022-09-08 08:46:06 -06001175 The chrome source is needed if the build target builds chrome or any of its
1176 follower packages, and can't use a prebuilt for them either because it's not
1177 available, or because we can't use prebuilts because it must build from
1178 source.
1179 """
1180 cros_build_lib.AssertInsideChroot()
Alex Klein6becabc2020-09-11 14:03:05 -06001181
Alex Klein1699fab2022-09-08 08:46:06 -06001182 # Check if it builds chrome and/or a follower package.
1183 graph = depgraph.get_sysroot_dependency_graph(build_target.root, packages)
1184 builds_chrome = constants.CHROME_CP in graph
1185 builds_follower = {
1186 pkg: pkg in graph for pkg in constants.OTHER_CHROME_PACKAGES
1187 }
Alex Klein6becabc2020-09-11 14:03:05 -06001188
Alex Klein1699fab2022-09-08 08:46:06 -06001189 local_uprev = builds_chrome and revbump_chrome([build_target])
Alex Klein9ce3f682021-06-23 15:06:44 -06001190
Alex Klein1699fab2022-09-08 08:46:06 -06001191 # When we are compiling source set False since we do not use prebuilts.
1192 # When not compiling from source, start with True, i.e. we have every prebuilt
1193 # we've checked for up to this point.
1194 has_chrome_prebuilt = not compile_source
1195 has_follower_prebuilts = not compile_source
1196 # Save packages that need prebuilts for reporting.
1197 pkgs_needing_prebuilts = []
1198 if compile_source:
1199 # Need everything.
Alex Klein6becabc2020-09-11 14:03:05 -06001200 pkgs_needing_prebuilts.append(constants.CHROME_CP)
Alex Klein1699fab2022-09-08 08:46:06 -06001201 pkgs_needing_prebuilts.extend(
1202 [pkg for pkg, builds_pkg in builds_follower.items() if builds_pkg]
1203 )
1204 else:
1205 # Check chrome itself.
1206 if builds_chrome:
1207 has_chrome_prebuilt = has_prebuilt(
1208 constants.CHROME_CP,
1209 build_target=build_target,
1210 useflags=useflags,
1211 )
1212 if not has_chrome_prebuilt:
1213 pkgs_needing_prebuilts.append(constants.CHROME_CP)
1214 # Check follower packages.
1215 for pkg, builds_pkg in builds_follower.items():
1216 if not builds_pkg:
1217 continue
1218 prebuilt = has_prebuilt(
1219 pkg, build_target=build_target, useflags=useflags
1220 )
1221 has_follower_prebuilts &= prebuilt
1222 if not prebuilt:
1223 pkgs_needing_prebuilts.append(pkg)
1224 # Postcondition: has_chrome_prebuilt and has_follower_prebuilts now correctly
1225 # reflect whether we actually have the corresponding prebuilts for the build.
Alex Klein6becabc2020-09-11 14:03:05 -06001226
Alex Klein1699fab2022-09-08 08:46:06 -06001227 needs_chrome = builds_chrome and not has_chrome_prebuilt
1228 needs_follower = (
1229 any(builds_follower.values()) and not has_follower_prebuilts
1230 )
Alex Klein6becabc2020-09-11 14:03:05 -06001231
Alex Klein1699fab2022-09-08 08:46:06 -06001232 return NeedsChromeSourceResult(
1233 needs_chrome_source=needs_chrome or needs_follower,
1234 builds_chrome=builds_chrome,
1235 packages=[package_info.parse(p) for p in pkgs_needing_prebuilts],
1236 missing_chrome_prebuilt=not has_chrome_prebuilt,
1237 missing_follower_prebuilt=not has_follower_prebuilts,
1238 local_uprev=local_uprev,
1239 )
Alex Klein6becabc2020-09-11 14:03:05 -06001240
1241
Alex Klein68a28712021-11-08 11:08:30 -07001242class TargetVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001243 """Data class for the info that makes up the "target versions"."""
1244
1245 android_version: str
1246 android_branch: str
1247 android_target: str
1248 chrome_version: str
1249 platform_version: str
1250 milestone_version: str
1251 full_version: str
Alex Klein68a28712021-11-08 11:08:30 -07001252
1253
1254def get_target_versions(
Alex Klein1699fab2022-09-08 08:46:06 -06001255 build_target: "build_target_lib.BuildTarget",
1256 packages: List[package_info.PackageInfo] = None,
Alex Klein68a28712021-11-08 11:08:30 -07001257) -> TargetVersions:
Alex Klein1699fab2022-09-08 08:46:06 -06001258 """Aggregate version info for a few key packages and the OS as a whole."""
1259 # Android version.
1260 android_version = determine_android_version(build_target.name)
1261 logging.info("Found android version: %s", android_version)
1262 # Android branch version.
1263 android_branch = determine_android_branch(build_target.name)
1264 logging.info("Found android branch version: %s", android_branch)
1265 # Android target version.
1266 android_target = determine_android_target(build_target.name)
1267 logging.info("Found android target version: %s", android_target)
Alex Klein68a28712021-11-08 11:08:30 -07001268
Alex Klein1699fab2022-09-08 08:46:06 -06001269 # TODO(crbug/1019770): Investigate cases where builds_chrome is true but
1270 # chrome_version is None.
Alex Klein68a28712021-11-08 11:08:30 -07001271
Alex Klein1699fab2022-09-08 08:46:06 -06001272 builds_chrome = builds(constants.CHROME_CP, build_target, packages=packages)
1273 chrome_version = None
1274 if builds_chrome:
1275 # Chrome version fetch.
1276 chrome_version = determine_chrome_version(build_target)
1277 logging.info("Found chrome version: %s", chrome_version)
Alex Klein68a28712021-11-08 11:08:30 -07001278
Alex Klein1699fab2022-09-08 08:46:06 -06001279 # The ChromeOS version info.
1280 platform_version = determine_platform_version()
1281 milestone_version = determine_milestone_version()
1282 full_version = determine_full_version()
Alex Klein68a28712021-11-08 11:08:30 -07001283
Alex Klein1699fab2022-09-08 08:46:06 -06001284 return TargetVersions(
1285 android_version,
1286 android_branch,
1287 android_target,
1288 chrome_version,
1289 platform_version,
1290 milestone_version,
1291 full_version,
1292 )
Alex Klein68a28712021-11-08 11:08:30 -07001293
1294
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001295def determine_chrome_version(
Alex Klein1699fab2022-09-08 08:46:06 -06001296 build_target: "build_target_lib.BuildTarget",
1297) -> Optional[str]:
1298 """Returns the current Chrome version for the board (or in buildroot).
Michael Mortensenc2615b72019-10-15 08:12:24 -06001299
Alex Klein1699fab2022-09-08 08:46:06 -06001300 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001301 build_target: The board build target.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001302
Alex Klein1699fab2022-09-08 08:46:06 -06001303 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001304 The chrome version if available.
Alex Klein1699fab2022-09-08 08:46:06 -06001305 """
1306 # TODO(crbug/1019770): Long term we should not need the try/catch here once
1307 # the builds function above only returns True for chrome when
1308 # determine_chrome_version will succeed.
1309 try:
1310 pkg_info = portage_util.PortageqBestVisible(
1311 constants.CHROME_CP, build_target.name, cwd=constants.SOURCE_ROOT
1312 )
1313 except cros_build_lib.RunCommandError as e:
1314 # Return None because portage failed when trying to determine the chrome
1315 # version.
1316 logging.warning("Caught exception in determine_chrome_package: %s", e)
1317 return None
1318 # Something like 78.0.3877.4_rc -> 78.0.3877.4
1319 return pkg_info.version.partition("_")[0]
Michael Mortensenc2615b72019-10-15 08:12:24 -06001320
1321
Alex Klein68a28712021-11-08 11:08:30 -07001322@functools.lru_cache()
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001323def determine_android_package(board: str) -> Optional[str]:
Alex Klein1699fab2022-09-08 08:46:06 -06001324 """Returns the active Android container package in use by the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001325
Alex Klein1699fab2022-09-08 08:46:06 -06001326 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001327 board: The board name this is specific to.
Alex Kleinad6b48a2020-01-08 16:57:41 -07001328
Alex Klein1699fab2022-09-08 08:46:06 -06001329 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001330 The android package string if there is one.
Alex Klein1699fab2022-09-08 08:46:06 -06001331 """
1332 try:
1333 packages = portage_util.GetPackageDependencies(
1334 "virtual/target-os", board=board
1335 )
1336 except cros_build_lib.RunCommandError as e:
1337 # Return None because a command (likely portage) failed when trying to
1338 # determine the package.
1339 logging.warning("Caught exception in determine_android_package: %s", e)
1340 return None
1341
1342 # We assume there is only one Android package in the depgraph.
1343 for package in packages:
1344 if package.startswith(
1345 "chromeos-base/android-container-"
1346 ) or package.startswith("chromeos-base/android-vm-"):
1347 return package
Michael Mortensene0f4b542019-10-24 15:30:23 -06001348 return None
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001349
1350
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001351def determine_android_version(board: str, package: str = None):
Alex Klein1699fab2022-09-08 08:46:06 -06001352 """Determine the current Android version in buildroot now and return it.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001353
Alex Klein1699fab2022-09-08 08:46:06 -06001354 This uses the typical portage logic to determine which version of Android
1355 is active right now in the buildroot.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001356
Alex Klein1699fab2022-09-08 08:46:06 -06001357 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001358 board: The board name this is specific to.
1359 package: The Android package, if already computed.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001360
Alex Klein1699fab2022-09-08 08:46:06 -06001361 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001362 The Android build ID of the container for the board.
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001363
Alex Klein1699fab2022-09-08 08:46:06 -06001364 Raises:
Alex Klein348e7692022-10-13 17:03:37 -06001365 NoAndroidVersionError: if no unique Android version can be determined.
Alex Klein1699fab2022-09-08 08:46:06 -06001366 """
1367 if not package:
1368 package = determine_android_package(board)
1369 if not package:
1370 return None
1371 cpv = package_info.SplitCPV(package)
1372 if not cpv:
1373 raise NoAndroidVersionError(
1374 "Android version could not be determined for %s" % board
1375 )
1376 return cpv.version_no_rev
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001377
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001378
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001379def determine_android_branch(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001380 """Returns the Android branch in use by the active container ebuild."""
1381 if not package:
1382 package = determine_android_package(board)
1383 if not package:
1384 return None
1385 ebuild_path = portage_util.FindEbuildForBoardPackage(package, board)
1386 # We assume all targets pull from the same branch and that we always
1387 # have at least one of the following targets.
Madeleine Hardt57722b72022-11-01 15:54:58 +00001388 targets = constants.ANDROID_ALL_BUILD_TARGETS
Alex Klein1699fab2022-09-08 08:46:06 -06001389 ebuild_content = osutils.SourceEnvironment(ebuild_path, targets)
1390 for target in targets:
1391 if target in ebuild_content:
1392 branch = re.search(r"(.*?)-linux-", ebuild_content[target])
1393 if branch is not None:
1394 return branch.group(1)
1395 raise NoAndroidBranchError(
1396 "Android branch could not be determined for %s (ebuild empty?)" % board
1397 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001398
1399
Mike Frysinger8e1c99a2021-03-05 00:58:11 -05001400def determine_android_target(board, package=None):
Alex Klein1699fab2022-09-08 08:46:06 -06001401 """Returns the Android target in use by the active container ebuild."""
1402 if not package:
1403 package = determine_android_package(board)
1404 if not package:
1405 return None
1406 if package.startswith("chromeos-base/android-vm-"):
1407 return "bertha"
1408 elif package.startswith("chromeos-base/android-container-"):
1409 return "cheets"
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001410
Alex Klein1699fab2022-09-08 08:46:06 -06001411 raise NoAndroidTargetError(
1412 "Android Target cannot be determined for the package: %s" % package
1413 )
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001414
1415
1416def determine_platform_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001417 """Returns the platform version from the source root."""
1418 # Platform version is something like '12575.0.0'.
1419 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1420 return version.VersionString()
Michael Mortensen009cb662019-10-21 11:38:43 -06001421
1422
1423def determine_milestone_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001424 """Returns the platform version from the source root."""
1425 # Milestone version is something like '79'.
1426 version = chromeos_version.VersionInfo.from_repo(constants.SOURCE_ROOT)
1427 return version.chrome_branch
Michael Mortensen009cb662019-10-21 11:38:43 -06001428
Alex Klein7a3a7dd2020-01-08 16:44:38 -07001429
Michael Mortensen009cb662019-10-21 11:38:43 -06001430def determine_full_version():
Alex Klein1699fab2022-09-08 08:46:06 -06001431 """Returns the full version from the source root."""
1432 # Full version is something like 'R79-12575.0.0'.
1433 milestone_version = determine_milestone_version()
1434 platform_version = determine_platform_version()
1435 full_version = "R%s-%s" % (milestone_version, platform_version)
1436 return full_version
Michael Mortensen71ef5682020-05-07 14:29:24 -06001437
1438
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001439def find_fingerprints(
Alex Klein1699fab2022-09-08 08:46:06 -06001440 build_target: "build_target_lib.BuildTarget",
1441) -> List[str]:
1442 """Returns a list of fingerprints for this build.
Michael Mortensende716a12020-05-15 11:27:00 -06001443
Alex Klein1699fab2022-09-08 08:46:06 -06001444 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001445 build_target: The build target.
Michael Mortensende716a12020-05-15 11:27:00 -06001446
Alex Klein1699fab2022-09-08 08:46:06 -06001447 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001448 List of fingerprint strings.
Alex Klein1699fab2022-09-08 08:46:06 -06001449 """
1450 cros_build_lib.AssertInsideChroot()
1451 fp_file = "cheets-fingerprint.txt"
1452 fp_path = os.path.join(
1453 image_lib.GetLatestImageLink(build_target.name), fp_file
1454 )
1455 if not os.path.isfile(fp_path):
1456 logging.info("Fingerprint file not found: %s", fp_path)
1457 return []
1458 logging.info("Reading fingerprint file: %s", fp_path)
1459 fingerprints = osutils.ReadFile(fp_path).splitlines()
1460 return fingerprints
Michael Mortensende716a12020-05-15 11:27:00 -06001461
1462
Alex Klein1699fab2022-09-08 08:46:06 -06001463def get_all_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1464 """Extract firmware version for all models present.
Michael Mortensen59e30872020-05-18 14:12:49 -06001465
Alex Klein1699fab2022-09-08 08:46:06 -06001466 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001467 build_target: The build target.
Michael Mortensen59e30872020-05-18 14:12:49 -06001468
Alex Klein1699fab2022-09-08 08:46:06 -06001469 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001470 A dict of FirmwareVersions namedtuple instances by model.
1471 Each element will be populated based on whether it was present in the
1472 command output.
Alex Klein1699fab2022-09-08 08:46:06 -06001473 """
1474 cros_build_lib.AssertInsideChroot()
1475 result = {}
1476 # Note that example output for _get_firmware_version_cmd_result is available
1477 # in the packages_unittest.py for testing get_all_firmware_versions.
1478 cmd_result = _get_firmware_version_cmd_result(build_target)
Michael Mortensen59e30872020-05-18 14:12:49 -06001479
Alex Klein1699fab2022-09-08 08:46:06 -06001480 if cmd_result:
1481 # There is a blank line between the version info for each model.
1482 firmware_version_payloads = cmd_result.split("\n\n")
1483 for firmware_version_payload in firmware_version_payloads:
1484 if "BIOS" in firmware_version_payload:
1485 firmware_version = _find_firmware_versions(
1486 firmware_version_payload
1487 )
1488 result[firmware_version.model] = firmware_version
1489 return result
Michael Mortensen59e30872020-05-18 14:12:49 -06001490
1491
Benjamin Shai0858cd32022-01-10 20:23:49 +00001492class FirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001493 """Tuple to hold firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001494
Alex Klein1699fab2022-09-08 08:46:06 -06001495 model: Optional[str]
1496 main: Optional[str]
1497 main_rw: Optional[str]
1498 ec: Optional[str]
1499 ec_rw: Optional[str]
1500
1501 def __bool__(self):
1502 return bool(
1503 self.model or self.main or self.main_rw or self.ec or self.ec_rw
1504 )
Michael Mortensen71ef5682020-05-07 14:29:24 -06001505
1506
Alex Klein1699fab2022-09-08 08:46:06 -06001507def get_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1508 """Extract version information from the firmware updater, if one exists.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001509
Alex Klein1699fab2022-09-08 08:46:06 -06001510 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001511 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001512
Alex Klein1699fab2022-09-08 08:46:06 -06001513 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001514 A FirmwareVersions namedtuple instance.
1515 Each element will either be set to the string output by the firmware
1516 updater shellball, or None if there is no firmware updater.
Alex Klein1699fab2022-09-08 08:46:06 -06001517 """
1518 cros_build_lib.AssertInsideChroot()
1519 cmd_result = _get_firmware_version_cmd_result(build_target)
1520 if cmd_result:
1521 return _find_firmware_versions(cmd_result)
1522 else:
1523 return FirmwareVersions(None, None, None, None, None)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001524
1525
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001526def _get_firmware_version_cmd_result(
Alex Klein1699fab2022-09-08 08:46:06 -06001527 build_target: "build_target_lib.BuildTarget",
1528) -> Optional[str]:
1529 """Gets the raw result output of the firmware updater version command.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001530
Alex Klein1699fab2022-09-08 08:46:06 -06001531 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001532 build_target: The build target.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001533
Alex Klein1699fab2022-09-08 08:46:06 -06001534 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001535 Command execution result.
Alex Klein1699fab2022-09-08 08:46:06 -06001536 """
1537 updater = os.path.join(
1538 build_target.root, "usr/sbin/chromeos-firmwareupdate"
1539 )
1540 logging.info("Calling updater %s", updater)
1541 # Call the updater using the chroot-based path.
1542 try:
1543 return cros_build_lib.run(
1544 [updater, "-V"],
1545 capture_output=True,
1546 log_output=True,
1547 encoding="utf-8",
1548 ).stdout
1549 except cros_build_lib.RunCommandError:
1550 # Updater probably doesn't exist (e.g. betty).
1551 return None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001552
1553
1554def _find_firmware_versions(cmd_output):
Alex Klein1699fab2022-09-08 08:46:06 -06001555 """Finds firmware version output via regex matches against the cmd_output.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001556
Alex Klein1699fab2022-09-08 08:46:06 -06001557 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001558 cmd_output: The raw output to search against.
Michael Mortensen71ef5682020-05-07 14:29:24 -06001559
Alex Klein1699fab2022-09-08 08:46:06 -06001560 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001561 FirmwareVersions namedtuple with results.
1562 Each element will either be set to the string output by the firmware
1563 updater shellball, or None if there is no match.
Alex Klein1699fab2022-09-08 08:46:06 -06001564 """
Michael Mortensen71ef5682020-05-07 14:29:24 -06001565
Alex Klein1699fab2022-09-08 08:46:06 -06001566 # Sometimes a firmware bundle includes a special combination of RO+RW
1567 # firmware. In this case, the RW firmware version is indicated with a "(RW)
1568 # version" field. In other cases, the "(RW) version" field is not present.
1569 # Therefore, search for the "(RW)" fields first and if they aren't present,
1570 # fallback to the other format. e.g. just "BIOS version:".
1571 # TODO(mmortensen): Use JSON once the firmware updater supports it.
1572 main = None
1573 main_rw = None
1574 ec = None
1575 ec_rw = None
1576 model = None
Michael Mortensen71ef5682020-05-07 14:29:24 -06001577
Alex Klein1699fab2022-09-08 08:46:06 -06001578 match = re.search(r"BIOS version:\s*(?P<version>.*)", cmd_output)
1579 if match:
1580 main = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001581
Alex Klein1699fab2022-09-08 08:46:06 -06001582 match = re.search(r"BIOS \(RW\) version:\s*(?P<version>.*)", cmd_output)
1583 if match:
1584 main_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001585
Alex Klein1699fab2022-09-08 08:46:06 -06001586 match = re.search(r"EC version:\s*(?P<version>.*)", cmd_output)
1587 if match:
1588 ec = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001589
Alex Klein1699fab2022-09-08 08:46:06 -06001590 match = re.search(r"EC \(RW\) version:\s*(?P<version>.*)", cmd_output)
1591 if match:
1592 ec_rw = match.group("version")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001593
Alex Klein1699fab2022-09-08 08:46:06 -06001594 match = re.search(r"Model:\s*(?P<model>.*)", cmd_output)
1595 if match:
1596 model = match.group("model")
Michael Mortensen71ef5682020-05-07 14:29:24 -06001597
Alex Klein1699fab2022-09-08 08:46:06 -06001598 return FirmwareVersions(model, main, main_rw, ec, ec_rw)
Michael Mortensena4af79e2020-05-06 16:18:48 -06001599
1600
Benjamin Shai0858cd32022-01-10 20:23:49 +00001601class MainEcFirmwareVersions(NamedTuple):
Alex Klein1699fab2022-09-08 08:46:06 -06001602 """Tuple to hold main and ec firmware versions, with truthiness."""
Benjamin Shai0858cd32022-01-10 20:23:49 +00001603
Alex Klein1699fab2022-09-08 08:46:06 -06001604 main_fw_version: Optional[str]
1605 ec_fw_version: Optional[str]
1606
1607 def __bool__(self):
1608 return bool(self.main_fw_version or self.ec_fw_version)
Benjamin Shai0858cd32022-01-10 20:23:49 +00001609
Michael Mortensena4af79e2020-05-06 16:18:48 -06001610
Alex Klein1699fab2022-09-08 08:46:06 -06001611def determine_firmware_versions(build_target: "build_target_lib.BuildTarget"):
1612 """Returns a namedtuple with main and ec firmware versions.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001613
Alex Klein1699fab2022-09-08 08:46:06 -06001614 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001615 build_target: The build target.
Michael Mortensena4af79e2020-05-06 16:18:48 -06001616
Alex Klein1699fab2022-09-08 08:46:06 -06001617 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001618 MainEcFirmwareVersions namedtuple with results.
Alex Klein1699fab2022-09-08 08:46:06 -06001619 """
1620 fw_versions = get_firmware_versions(build_target)
1621 main_fw_version = fw_versions.main_rw or fw_versions.main
1622 ec_fw_version = fw_versions.ec_rw or fw_versions.ec
Michael Mortensena4af79e2020-05-06 16:18:48 -06001623
Alex Klein1699fab2022-09-08 08:46:06 -06001624 return MainEcFirmwareVersions(main_fw_version, ec_fw_version)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001625
Benjamin Shai0858cd32022-01-10 20:23:49 +00001626
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001627def determine_kernel_version(
Alex Klein1699fab2022-09-08 08:46:06 -06001628 build_target: "build_target_lib.BuildTarget",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001629) -> str:
Alex Klein1699fab2022-09-08 08:46:06 -06001630 """Returns a string containing the kernel version for this build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001631
Alex Klein1699fab2022-09-08 08:46:06 -06001632 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001633 build_target: The build target.
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001634
Alex Klein1699fab2022-09-08 08:46:06 -06001635 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001636 The kernel versions, or empty string.
Alex Klein1699fab2022-09-08 08:46:06 -06001637 """
Lizzy Presland0b978e62022-09-09 16:55:29 +00001638 target_virtual_pkg = "virtual/linux-sources"
Alex Klein1699fab2022-09-08 08:46:06 -06001639 try:
Lizzy Presland0b978e62022-09-09 16:55:29 +00001640 candidate_packages = portage_util.GetFlattenedDepsForPackage(
1641 target_virtual_pkg,
1642 sysroot=build_target.root,
1643 board=build_target.name,
1644 depth=1,
1645 )
1646 installed_packages = portage_util.GetPackageDependencies(
1647 target_virtual_pkg, board=build_target.name
Alex Klein1699fab2022-09-08 08:46:06 -06001648 )
1649 except cros_build_lib.RunCommandError as e:
1650 logging.warning("Unable to get package list for metadata: %s", e)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001651 return ""
1652 if not candidate_packages:
1653 raise KernelVersionError("No package found in FlattenedDepsForPackage")
1654 if not installed_packages:
1655 raise KernelVersionError("No package found in GetPackageDependencies")
1656 packages = [
1657 p
1658 for p in installed_packages
1659 if p in candidate_packages and target_virtual_pkg not in p
1660 ]
1661 if len(packages) == 0:
1662 raise KernelVersionError(
1663 "No matches for installed packages were found in candidate "
1664 "packages. Did GetFlattenedDepsForPackage search all possible "
1665 "package versions?\tInstalled: %s\tCandidates: %s"
1666 % (" ".join(installed_packages), " ".join(candidate_packages))
1667 )
1668 if len(packages) > 1:
1669 raise KernelVersionError(
1670 "Too many packages found in intersection of installed packages and "
1671 "possible kernel versions (%s)" % "".join(packages)
1672 )
1673 kernel_version = package_info.SplitCPV(packages[0]).version
1674 logging.info("Found active kernel version: %s", kernel_version)
1675 return kernel_version
Michael Mortensen125bb012020-05-21 14:02:10 -06001676
1677
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001678def get_models(
Alex Klein1699fab2022-09-08 08:46:06 -06001679 build_target: "build_target_lib.BuildTarget", log_output: bool = True
1680) -> Optional[List[str]]:
1681 """Obtain a list of models supported by a unified board.
Michael Mortensen125bb012020-05-21 14:02:10 -06001682
Alex Klein1699fab2022-09-08 08:46:06 -06001683 This ignored whitelabel models since GoldenEye has no specific support for
1684 these at present.
Michael Mortensen125bb012020-05-21 14:02:10 -06001685
Alex Klein1699fab2022-09-08 08:46:06 -06001686 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001687 build_target: The build target.
1688 log_output: Whether to log the output of the cros_config_host
1689 invocation.
Michael Mortensen125bb012020-05-21 14:02:10 -06001690
Alex Klein1699fab2022-09-08 08:46:06 -06001691 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001692 A list of models supported by this board, if it is a unified build;
1693 None, if it is not a unified build.
Alex Klein1699fab2022-09-08 08:46:06 -06001694 """
1695 return _run_cros_config_host(
1696 build_target, ["list-models"], log_output=log_output
1697 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001698
1699
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001700def get_key_id(
Alex Klein1699fab2022-09-08 08:46:06 -06001701 build_target: "build_target_lib.BuildTarget", model: str
1702) -> Optional[str]:
1703 """Obtain the key_id for a model within the build_target.
Michael Mortensen359c1f32020-05-28 19:35:42 -06001704
Alex Klein1699fab2022-09-08 08:46:06 -06001705 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001706 build_target: The build target.
1707 model: The model name
Michael Mortensen359c1f32020-05-28 19:35:42 -06001708
Alex Klein1699fab2022-09-08 08:46:06 -06001709 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001710 A key_id or None.
Alex Klein1699fab2022-09-08 08:46:06 -06001711 """
1712 model_arg = "--model=" + model
1713 key_id_list = _run_cros_config_host(
1714 build_target, [model_arg, "get", "/firmware-signing", "key-id"]
1715 )
1716 key_id = None
1717 if len(key_id_list) == 1:
1718 key_id = key_id_list[0]
1719 return key_id
Michael Mortensen359c1f32020-05-28 19:35:42 -06001720
1721
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001722def _run_cros_config_host(
Alex Klein1699fab2022-09-08 08:46:06 -06001723 build_target: "build_target_lib.BuildTarget",
Matthias Kaehlckebf7d1772021-11-04 16:01:36 -07001724 args: List[str],
Alex Klein1699fab2022-09-08 08:46:06 -06001725 log_output: bool = True,
1726) -> Optional[List[str]]:
1727 """Run the cros_config_host tool.
Michael Mortensen125bb012020-05-21 14:02:10 -06001728
Alex Klein1699fab2022-09-08 08:46:06 -06001729 Args:
Alex Klein348e7692022-10-13 17:03:37 -06001730 build_target: The build target.
1731 args: List of arguments to pass.
1732 log_output: Whether to log the output of the cros_config_host.
Michael Mortensen125bb012020-05-21 14:02:10 -06001733
Alex Klein1699fab2022-09-08 08:46:06 -06001734 Returns:
Alex Klein348e7692022-10-13 17:03:37 -06001735 Output of the tool
Alex Klein1699fab2022-09-08 08:46:06 -06001736 """
1737 cros_build_lib.AssertInsideChroot()
1738 tool = "/usr/bin/cros_config_host"
1739 if not os.path.isfile(tool):
1740 return None
Michael Mortensen125bb012020-05-21 14:02:10 -06001741
Alex Klein1699fab2022-09-08 08:46:06 -06001742 config_fname = build_target.full_path(
1743 "usr/share/chromeos-config/yaml/config.yaml"
1744 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001745
Alex Klein1699fab2022-09-08 08:46:06 -06001746 result = cros_build_lib.run(
1747 [tool, "-c", config_fname] + args,
1748 capture_output=True,
1749 encoding="utf-8",
1750 log_output=log_output,
1751 check=False,
1752 )
1753 if result.returncode:
1754 # Show the output for debugging purposes.
1755 if "No such file or directory" not in result.stderr:
1756 logging.error("cros_config_host failed: %s\n", result.stderr)
1757 return None
1758 return result.stdout.strip().splitlines()