Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Package utility functionality.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 10 | import collections |
Yaakov Shaul | cb1cfc3 | 2019-09-16 13:51:19 -0600 | [diff] [blame] | 11 | import fileinput |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 12 | import functools |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 13 | import json |
Evan Hernandez | b51f152 | 2019-08-15 11:29:40 -0600 | [diff] [blame] | 14 | import os |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 15 | import re |
Yaakov Shaul | cb1cfc3 | 2019-09-16 13:51:19 -0600 | [diff] [blame] | 16 | import sys |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 17 | |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 18 | from google.protobuf import json_format |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 19 | |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 20 | from chromite.api.gen.config import replication_config_pb2 |
Michael Mortensen | 9fdb14b | 2019-10-17 11:17:30 -0600 | [diff] [blame] | 21 | from chromite.cbuildbot import manifest_version |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 22 | from chromite.lib import constants |
Evan Hernandez | b51f152 | 2019-08-15 11:29:40 -0600 | [diff] [blame] | 23 | from chromite.lib import cros_build_lib |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 24 | from chromite.lib import cros_logging as logging |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 25 | from chromite.lib import git |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 26 | from chromite.lib import osutils |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 27 | from chromite.lib import portage_util |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 28 | from chromite.lib import replication_lib |
Alex Klein | d6195b6 | 2019-08-06 16:01:16 -0600 | [diff] [blame] | 29 | from chromite.lib import uprev_lib |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 30 | |
Alex Klein | 36b117f | 2019-09-30 15:13:46 -0600 | [diff] [blame] | 31 | if cros_build_lib.IsInsideChroot(): |
| 32 | from chromite.service import dependency |
| 33 | |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 34 | # Registered handlers for uprevving versioned packages. |
| 35 | _UPREV_FUNCS = {} |
| 36 | |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 37 | |
| 38 | class Error(Exception): |
| 39 | """Module's base error class.""" |
| 40 | |
| 41 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 42 | class UnknownPackageError(Error): |
| 43 | """Uprev attempted for a package without a registered handler.""" |
| 44 | |
| 45 | |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 46 | class UprevError(Error): |
| 47 | """An error occurred while uprevving packages.""" |
| 48 | |
| 49 | |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 50 | class NoAndroidVersionError(Error): |
| 51 | """An error occurred while trying to determine the android version.""" |
| 52 | |
| 53 | |
| 54 | class NoAndroidBranchError(Error): |
| 55 | """An error occurred while trying to determine the android branch.""" |
| 56 | |
| 57 | |
| 58 | class NoAndroidTargetError(Error): |
| 59 | """An error occurred while trying to determine the android target.""" |
| 60 | |
| 61 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 62 | class AndroidIsPinnedUprevError(UprevError): |
| 63 | """Raised when we try to uprev while Android is pinned.""" |
| 64 | |
| 65 | def __init__(self, new_android_atom): |
| 66 | """Initialize a AndroidIsPinnedUprevError. |
| 67 | |
| 68 | Args: |
| 69 | new_android_atom: The Android atom that we failed to |
| 70 | uprev to, due to Android being pinned. |
| 71 | """ |
| 72 | assert new_android_atom |
| 73 | msg = ('Failed up uprev to Android version %s as Android was pinned.' % |
| 74 | new_android_atom) |
| 75 | super(AndroidIsPinnedUprevError, self).__init__(msg) |
| 76 | self.new_android_atom = new_android_atom |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 77 | |
| 78 | |
Yaakov Shaul | 1eafe83 | 2019-09-10 16:50:26 -0600 | [diff] [blame] | 79 | class EbuildManifestError(Error): |
| 80 | """Error when running ebuild manifest.""" |
| 81 | |
| 82 | |
Andrew Lamb | 9563a15 | 2019-12-04 11:42:18 -0700 | [diff] [blame] | 83 | class GeneratedCrosConfigFilesError(Error): |
| 84 | """Error when cros_config_schema does not produce expected files""" |
| 85 | |
| 86 | def __init__(self, expected_files, found_files): |
| 87 | msg = ('Expected to find generated C files: %s. Actually found: %s' % |
| 88 | (expected_files, found_files)) |
| 89 | super(GeneratedCrosConfigFilesError, self).__init__(msg) |
| 90 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 91 | UprevVersionedPackageModifications = collections.namedtuple( |
| 92 | 'UprevVersionedPackageModifications', ('new_version', 'files')) |
Alex Klein | 34afcbc | 2019-08-22 16:14:31 -0600 | [diff] [blame] | 93 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 94 | |
| 95 | class UprevVersionedPackageResult(object): |
| 96 | """Data object for uprev_versioned_package.""" |
| 97 | |
| 98 | def __init__(self): |
| 99 | self.modified = [] |
| 100 | |
| 101 | def add_result(self, new_version, modified_files): |
| 102 | """Adds version/ebuilds tuple to result. |
| 103 | |
| 104 | Args: |
| 105 | new_version: New version number of package. |
| 106 | modified_files: List of files modified for the given version. |
| 107 | """ |
| 108 | result = UprevVersionedPackageModifications(new_version, modified_files) |
| 109 | self.modified.append(result) |
| 110 | return self |
Alex Klein | 34afcbc | 2019-08-22 16:14:31 -0600 | [diff] [blame] | 111 | |
| 112 | @property |
| 113 | def uprevved(self): |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 114 | return bool(self.modified) |
Alex Klein | 34afcbc | 2019-08-22 16:14:31 -0600 | [diff] [blame] | 115 | |
| 116 | |
Yaakov Shaul | cb1cfc3 | 2019-09-16 13:51:19 -0600 | [diff] [blame] | 117 | def patch_ebuild_vars(ebuild_path, variables): |
| 118 | """Updates variables in ebuild. |
| 119 | |
| 120 | Use this function rather than portage_util.EBuild.UpdateEBuild when you |
| 121 | want to preserve the variable position and quotes within the ebuild. |
| 122 | |
| 123 | Args: |
| 124 | ebuild_path: The path of the ebuild. |
| 125 | variables: Dictionary of variables to update in ebuild. |
| 126 | """ |
| 127 | try: |
| 128 | for line in fileinput.input(ebuild_path, inplace=1): |
| 129 | varname, eq, _ = line.partition('=') |
| 130 | if eq == '=' and varname.strip() in variables: |
| 131 | value = variables[varname] |
| 132 | sys.stdout.write('%s="%s"\n' % (varname, value)) |
| 133 | else: |
| 134 | sys.stdout.write(line) |
| 135 | finally: |
| 136 | fileinput.close() |
| 137 | |
| 138 | |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 139 | def uprevs_versioned_package(package): |
| 140 | """Decorator to register package uprev handlers.""" |
| 141 | assert package |
| 142 | |
| 143 | def register(func): |
| 144 | """Registers |func| as a handler for |package|.""" |
| 145 | _UPREV_FUNCS[package] = func |
| 146 | |
| 147 | @functools.wraps(func) |
| 148 | def pass_through(*args, **kwargs): |
| 149 | return func(*args, **kwargs) |
| 150 | |
| 151 | return pass_through |
| 152 | |
| 153 | return register |
| 154 | |
| 155 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 156 | def uprev_android(tracking_branch, android_package, android_build_branch, |
| 157 | chroot, build_targets=None, android_version=None, |
| 158 | android_gts_build_branch=None): |
| 159 | """Returns the portage atom for the revved Android ebuild - see man emerge.""" |
| 160 | command = ['cros_mark_android_as_stable', |
| 161 | '--tracking_branch=%s' % tracking_branch, |
| 162 | '--android_package=%s' % android_package, |
| 163 | '--android_build_branch=%s' % android_build_branch] |
| 164 | if build_targets: |
| 165 | command.append('--boards=%s' % ':'.join(bt.name for bt in build_targets)) |
| 166 | if android_version: |
| 167 | command.append('--force_version=%s' % android_version) |
| 168 | if android_gts_build_branch: |
| 169 | command.append('--android_gts_build_branch=%s' % android_gts_build_branch) |
| 170 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 171 | result = cros_build_lib.run(command, redirect_stdout=True, |
| 172 | enter_chroot=True, |
| 173 | chroot_args=chroot.get_enter_args()) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 174 | |
| 175 | android_atom = _parse_android_atom(result) |
| 176 | if not android_atom: |
| 177 | logging.info('Found nothing to rev.') |
| 178 | return None |
| 179 | |
| 180 | for target in build_targets or []: |
| 181 | # Sanity check: We should always be able to merge the version of |
| 182 | # Android we just unmasked. |
| 183 | command = ['emerge-%s' % target.name, '-p', '--quiet', |
| 184 | '=%s' % android_atom] |
| 185 | try: |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 186 | cros_build_lib.run(command, enter_chroot=True, |
| 187 | chroot_args=chroot.get_enter_args()) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 188 | except cros_build_lib.RunCommandError: |
| 189 | logging.error( |
| 190 | 'Cannot emerge-%s =%s\nIs Android pinned to an older ' |
| 191 | 'version?', target, android_atom) |
| 192 | raise AndroidIsPinnedUprevError(android_atom) |
| 193 | |
| 194 | return android_atom |
| 195 | |
| 196 | |
| 197 | def _parse_android_atom(result): |
| 198 | """Helper to parse the atom from the cros_mark_android_as_stable output. |
| 199 | |
| 200 | This function is largely just intended to make testing easier. |
| 201 | |
| 202 | Args: |
| 203 | result (cros_build_lib.CommandResult): The cros_mark_android_as_stable |
| 204 | command result. |
| 205 | """ |
| 206 | portage_atom_string = result.output.strip() |
| 207 | |
| 208 | android_atom = None |
| 209 | if portage_atom_string: |
| 210 | android_atom = portage_atom_string.splitlines()[-1].partition('=')[-1] |
| 211 | |
| 212 | return android_atom |
| 213 | |
| 214 | |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 215 | def uprev_build_targets(build_targets, overlay_type, chroot=None, |
| 216 | output_dir=None): |
| 217 | """Uprev the set provided build targets, or all if not specified. |
| 218 | |
| 219 | Args: |
| 220 | build_targets (list[build_target_util.BuildTarget]|None): The build targets |
| 221 | whose overlays should be uprevved, empty or None for all. |
| 222 | overlay_type (str): One of the valid overlay types except None (see |
| 223 | constants.VALID_OVERLAYS). |
| 224 | chroot (chroot_lib.Chroot|None): The chroot to clean, if desired. |
| 225 | output_dir (str|None): The path to optionally dump result files. |
| 226 | """ |
| 227 | # Need a valid overlay, but exclude None. |
| 228 | assert overlay_type and overlay_type in constants.VALID_OVERLAYS |
| 229 | |
| 230 | if build_targets: |
| 231 | overlays = portage_util.FindOverlaysForBoards( |
| 232 | overlay_type, boards=[t.name for t in build_targets]) |
| 233 | else: |
| 234 | overlays = portage_util.FindOverlays(overlay_type) |
| 235 | |
| 236 | return uprev_overlays(overlays, build_targets=build_targets, chroot=chroot, |
| 237 | output_dir=output_dir) |
| 238 | |
| 239 | |
| 240 | def uprev_overlays(overlays, build_targets=None, chroot=None, output_dir=None): |
| 241 | """Uprev the given overlays. |
| 242 | |
| 243 | Args: |
| 244 | overlays (list[str]): The list of overlay paths. |
| 245 | build_targets (list[build_target_util.BuildTarget]|None): The build targets |
| 246 | to clean in |chroot|, if desired. No effect unless |chroot| is provided. |
| 247 | chroot (chroot_lib.Chroot|None): The chroot to clean, if desired. |
| 248 | output_dir (str|None): The path to optionally dump result files. |
| 249 | |
| 250 | Returns: |
| 251 | list[str] - The paths to all of the modified ebuild files. This includes the |
| 252 | new files that were added (i.e. the new versions) and all of the removed |
| 253 | files (i.e. the old versions). |
| 254 | """ |
| 255 | assert overlays |
| 256 | |
| 257 | manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT) |
| 258 | |
Alex Klein | d6195b6 | 2019-08-06 16:01:16 -0600 | [diff] [blame] | 259 | uprev_manager = uprev_lib.UprevOverlayManager(overlays, manifest, |
| 260 | build_targets=build_targets, |
| 261 | chroot=chroot, |
| 262 | output_dir=output_dir) |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 263 | uprev_manager.uprev() |
| 264 | |
| 265 | return uprev_manager.modified_ebuilds |
| 266 | |
| 267 | |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 268 | def uprev_versioned_package(package, build_targets, refs, chroot): |
| 269 | """Call registered uprev handler function for the package. |
| 270 | |
| 271 | Args: |
| 272 | package (portage_util.CPV): The package being uprevved. |
| 273 | build_targets (list[build_target_util.BuildTarget]): The build targets to |
| 274 | clean on a successful uprev. |
| 275 | refs (list[uprev_lib.GitRef]): |
| 276 | chroot (chroot_lib.Chroot): The chroot to enter for cleaning. |
| 277 | |
| 278 | Returns: |
Alex Klein | 34afcbc | 2019-08-22 16:14:31 -0600 | [diff] [blame] | 279 | UprevVersionedPackageResult: The result. |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 280 | """ |
| 281 | assert package |
| 282 | |
| 283 | if package.cp not in _UPREV_FUNCS: |
| 284 | raise UnknownPackageError( |
| 285 | 'Package "%s" does not have a registered handler.' % package.cp) |
| 286 | |
Andrew Lamb | 1501649 | 2019-12-12 09:35:50 -0700 | [diff] [blame] | 287 | return _UPREV_FUNCS[package.cp](build_targets, refs, chroot, package.cp) |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 288 | |
| 289 | |
Evan Hernandez | b51f152 | 2019-08-15 11:29:40 -0600 | [diff] [blame] | 290 | # TODO(evanhernandez): Remove this. Only a quick hack for testing. |
| 291 | @uprevs_versioned_package('sample/sample') |
| 292 | def uprev_sample(*_args, **_kwargs): |
| 293 | """Mimics an uprev by changing files in sandbox repos. |
| 294 | |
| 295 | See: uprev_versioned_package. |
| 296 | """ |
| 297 | paths = [ |
| 298 | os.path.join(constants.SOURCE_ROOT, 'infra/dummies', repo, 'sample.txt') |
| 299 | for repo in ('general-sandbox', 'merge-sandbox') |
| 300 | ] |
| 301 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 302 | return UprevVersionedPackageResult().add_result('1.2.3', paths) |
Evan Hernandez | b51f152 | 2019-08-15 11:29:40 -0600 | [diff] [blame] | 303 | |
| 304 | |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 305 | @uprevs_versioned_package('afdo/kernel-profiles') |
| 306 | def uprev_kernel_afdo(*_args, **_kwargs): |
David Burger | 9248534 | 2019-09-10 17:52:45 -0600 | [diff] [blame] | 307 | """Updates kernel ebuilds with versions from kernel_afdo.json. |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 308 | |
| 309 | See: uprev_versioned_package. |
Yaakov Shaul | 1eafe83 | 2019-09-10 16:50:26 -0600 | [diff] [blame] | 310 | |
| 311 | Raises: |
| 312 | EbuildManifestError: When ebuild manifest does not complete successfuly. |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 313 | """ |
| 314 | path = os.path.join(constants.SOURCE_ROOT, 'src', 'third_party', |
| 315 | 'toolchain-utils', 'afdo_metadata', 'kernel_afdo.json') |
| 316 | |
David Burger | 9248534 | 2019-09-10 17:52:45 -0600 | [diff] [blame] | 317 | with open(path, 'r') as f: |
| 318 | versions = json.load(f) |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 319 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 320 | result = UprevVersionedPackageResult() |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 321 | for version, version_info in versions.items(): |
Yaakov Shaul | dd8b411 | 2019-09-11 11:44:03 -0600 | [diff] [blame] | 322 | path = os.path.join('src', 'third_party', 'chromiumos-overlay', |
| 323 | 'sys-kernel', version) |
| 324 | ebuild_path = os.path.join(constants.SOURCE_ROOT, path, |
| 325 | '%s-9999.ebuild' % version) |
Yaakov Shaul | a187b15 | 2019-09-11 12:41:32 -0600 | [diff] [blame] | 326 | chroot_ebuild_path = os.path.join(constants.CHROOT_SOURCE_ROOT, path, |
| 327 | '%s-9999.ebuild' % version) |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 328 | afdo_profile_version = version_info['name'] |
Yaakov Shaul | cb1cfc3 | 2019-09-16 13:51:19 -0600 | [diff] [blame] | 329 | patch_ebuild_vars(ebuild_path, |
| 330 | dict(AFDO_PROFILE_VERSION=afdo_profile_version)) |
Yaakov Shaul | 1eafe83 | 2019-09-10 16:50:26 -0600 | [diff] [blame] | 331 | |
| 332 | try: |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 333 | cmd = ['ebuild', chroot_ebuild_path, 'manifest', '--force'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 334 | cros_build_lib.run(cmd, enter_chroot=True) |
Yaakov Shaul | 1eafe83 | 2019-09-10 16:50:26 -0600 | [diff] [blame] | 335 | except cros_build_lib.RunCommandError as e: |
| 336 | raise EbuildManifestError( |
| 337 | 'Error encountered when regenerating the manifest for ebuild: %s\n%s' |
Yaakov Shaul | a187b15 | 2019-09-11 12:41:32 -0600 | [diff] [blame] | 338 | % (chroot_ebuild_path, e), e) |
Yaakov Shaul | 1eafe83 | 2019-09-10 16:50:26 -0600 | [diff] [blame] | 339 | |
Yaakov Shaul | dd8b411 | 2019-09-11 11:44:03 -0600 | [diff] [blame] | 340 | manifest_path = os.path.join(constants.SOURCE_ROOT, path, 'Manifest') |
Yaakov Shaul | 1eafe83 | 2019-09-10 16:50:26 -0600 | [diff] [blame] | 341 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 342 | result.add_result(afdo_profile_version, [ebuild_path, manifest_path]) |
| 343 | |
| 344 | return result |
Yaakov Shaul | 395ae83 | 2019-09-09 14:45:32 -0600 | [diff] [blame] | 345 | |
| 346 | |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 347 | @uprevs_versioned_package(constants.CHROME_CP) |
Andrew Lamb | 1501649 | 2019-12-12 09:35:50 -0700 | [diff] [blame] | 348 | def uprev_chrome(build_targets, refs, chroot, *_args, **_kwargs): |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 349 | """Uprev chrome and its related packages. |
| 350 | |
| 351 | See: uprev_versioned_package. |
| 352 | """ |
| 353 | # Determine the version from the refs (tags), i.e. the chrome versions are the |
| 354 | # tag names. |
| 355 | chrome_version = uprev_lib.get_chrome_version_from_refs(refs) |
| 356 | |
| 357 | uprev_manager = uprev_lib.UprevChromeManager( |
| 358 | chrome_version, build_targets=build_targets, chroot=chroot) |
David Burger | 37f4867 | 2019-09-18 17:07:56 -0600 | [diff] [blame] | 359 | result = UprevVersionedPackageResult() |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 360 | # Start with chrome itself, as we can't do anything else unless chrome |
| 361 | # uprevs successfully. |
| 362 | if not uprev_manager.uprev(constants.CHROME_CP): |
David Burger | 37f4867 | 2019-09-18 17:07:56 -0600 | [diff] [blame] | 363 | return result |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 364 | |
| 365 | # With a successful chrome rev, also uprev related packages. |
| 366 | for package in constants.OTHER_CHROME_PACKAGES: |
| 367 | uprev_manager.uprev(package) |
| 368 | |
David Burger | 37f4867 | 2019-09-18 17:07:56 -0600 | [diff] [blame] | 369 | return result.add_result(chrome_version, uprev_manager.modified_ebuilds) |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 370 | |
| 371 | |
Andrew Lamb | 9563a15 | 2019-12-04 11:42:18 -0700 | [diff] [blame] | 372 | def _generate_platform_c_files(replication_config, chroot): |
| 373 | """Generates platform C files from a platform JSON payload. |
| 374 | |
| 375 | Args: |
| 376 | replication_config (replication_config_pb2.ReplicationConfig): A |
| 377 | ReplicationConfig that has already been run. If it produced a |
| 378 | build_config.json file, that file will be used to generate platform C |
| 379 | files. Otherwise, nothing will be generated. |
| 380 | chroot (chroot_lib.Chroot): The chroot to use to generate. |
| 381 | |
| 382 | Returns: |
| 383 | A list of generated files. |
| 384 | """ |
| 385 | # Generate the platform C files from the build config. Note that it would be |
| 386 | # more intuitive to generate the platform C files from the platform config; |
| 387 | # however, cros_config_schema does not allow this, because the platform config |
| 388 | # payload is not always valid input. For example, if a property is both |
| 389 | # 'required' and 'build-only', it will fail schema validation. Thus, use the |
| 390 | # build config, and use '-f' to filter. |
| 391 | build_config_path = [ |
| 392 | rule.destination_path |
| 393 | for rule in replication_config.file_replication_rules |
| 394 | if rule.destination_path.endswith('build_config.json') |
| 395 | ] |
| 396 | |
| 397 | if not build_config_path: |
| 398 | logging.info( |
| 399 | 'No build_config.json found, will not generate platform C files.' |
| 400 | ' Replication config: %s', replication_config) |
| 401 | return [] |
| 402 | |
| 403 | if len(build_config_path) > 1: |
| 404 | raise ValueError('Expected at most one build_config.json destination path.' |
| 405 | ' Replication config: %s' % replication_config) |
| 406 | |
| 407 | build_config_path = build_config_path[0] |
| 408 | |
| 409 | # Paths to the build_config.json and dir to output C files to, in the |
| 410 | # chroot. |
| 411 | build_config_chroot_path = os.path.join(constants.CHROOT_SOURCE_ROOT, |
| 412 | build_config_path) |
| 413 | generated_output_chroot_dir = os.path.join(constants.CHROOT_SOURCE_ROOT, |
| 414 | os.path.dirname(build_config_path)) |
| 415 | |
| 416 | command = [ |
| 417 | 'cros_config_schema', '-m', build_config_chroot_path, '-g', |
| 418 | generated_output_chroot_dir, '-f', '"TRUE"' |
| 419 | ] |
| 420 | |
| 421 | cros_build_lib.run( |
| 422 | command, enter_chroot=True, chroot_args=chroot.get_enter_args()) |
| 423 | |
| 424 | # A relative (to the source root) path to the generated C files. |
| 425 | generated_output_dir = os.path.dirname(build_config_path) |
| 426 | generated_files = [] |
| 427 | expected_c_files = ['config.c', 'ec_config.c', 'ec_config.h'] |
| 428 | for f in expected_c_files: |
| 429 | if os.path.exists( |
| 430 | os.path.join(constants.SOURCE_ROOT, generated_output_dir, f)): |
| 431 | generated_files.append(os.path.join(generated_output_dir, f)) |
| 432 | |
| 433 | if len(expected_c_files) != len(generated_files): |
| 434 | raise GeneratedCrosConfigFilesError(expected_c_files, generated_files) |
| 435 | |
| 436 | return generated_files |
| 437 | |
| 438 | |
Andrew Lamb | e836f22 | 2019-12-09 12:27:38 -0700 | [diff] [blame] | 439 | def _get_private_overlay_package_root(ref, package): |
| 440 | """Returns the absolute path to the root of a given private overlay. |
| 441 | |
| 442 | Args: |
| 443 | ref (uprev_lib.GitRef): GitRef for the private overlay. |
| 444 | package (str): Path to the package in the overlay. |
| 445 | """ |
| 446 | # There might be a cleaner way to map from package -> path within the source |
| 447 | # tree. For now, just use string patterns. |
| 448 | private_overlay_ref_pattern = r'chromeos\/overlays\/overlay-([\w-]+)-private' |
| 449 | match = re.match(private_overlay_ref_pattern, ref.path) |
| 450 | if not match: |
| 451 | raise ValueError('ref.path must match the pattern: %s. Actual ref: %s' % |
| 452 | (private_overlay_ref_pattern, ref)) |
| 453 | |
| 454 | overlay = match.group(1) |
| 455 | |
| 456 | return os.path.join(constants.SOURCE_ROOT, |
| 457 | 'src/private-overlays/overlay-%s-private' % overlay, |
| 458 | package) |
| 459 | |
| 460 | |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 461 | @uprevs_versioned_package('chromeos-base/chromeos-config-bsp-coral-private') |
Andrew Lamb | 1501649 | 2019-12-12 09:35:50 -0700 | [diff] [blame] | 462 | def replicate_private_config(_build_targets, refs, chroot, package): |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 463 | """Replicate a private cros_config change to the corresponding public config. |
| 464 | |
| 465 | See uprev_versioned_package for args |
| 466 | """ |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 467 | if len(refs) != 1: |
| 468 | raise ValueError('Expected exactly one ref, actual %s' % refs) |
| 469 | |
| 470 | # Expect a replication_config.jsonpb in the package root. |
Andrew Lamb | e836f22 | 2019-12-09 12:27:38 -0700 | [diff] [blame] | 471 | package_root = _get_private_overlay_package_root(refs[0], package) |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 472 | replication_config_path = os.path.join(package_root, |
| 473 | 'replication_config.jsonpb') |
| 474 | |
| 475 | try: |
| 476 | replication_config = json_format.Parse( |
| 477 | osutils.ReadFile(replication_config_path), |
| 478 | replication_config_pb2.ReplicationConfig()) |
| 479 | except IOError: |
| 480 | raise ValueError('Expected ReplicationConfig missing at %s' % |
| 481 | replication_config_path) |
| 482 | |
| 483 | replication_lib.Replicate(replication_config) |
| 484 | |
| 485 | modified_files = [ |
| 486 | rule.destination_path |
| 487 | for rule in replication_config.file_replication_rules |
| 488 | ] |
| 489 | |
Andrew Lamb | 9563a15 | 2019-12-04 11:42:18 -0700 | [diff] [blame] | 490 | # The generated platform C files are not easily filtered by replication rules, |
| 491 | # i.e. JSON / proto filtering can be described by a FieldMask, arbitrary C |
| 492 | # files cannot. Therefore, replicate and filter the JSON payloads, and then |
| 493 | # generate filtered C files from the JSON payload. |
| 494 | modified_files.extend(_generate_platform_c_files(replication_config, chroot)) |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 495 | |
| 496 | # Use the private repo's commit hash as the new version. |
| 497 | new_private_version = refs[0].revision |
| 498 | |
Andrew Lamb | 988f4da | 2019-12-10 10:16:43 -0700 | [diff] [blame] | 499 | # modified_files should contain only relative paths at this point, but the |
| 500 | # returned UprevVersionedPackageResult must contain only absolute paths. |
| 501 | for i, modified_file in enumerate(modified_files): |
| 502 | assert not os.path.isabs(modified_file) |
| 503 | modified_files[i] = os.path.join(constants.SOURCE_ROOT, modified_file) |
| 504 | |
Andrew Lamb | 2bde9e4 | 2019-11-04 13:24:09 -0700 | [diff] [blame] | 505 | return UprevVersionedPackageResult().add_result(new_private_version, |
| 506 | modified_files) |
| 507 | |
| 508 | |
Alex Klein | bbef2b3 | 2019-08-27 10:38:50 -0600 | [diff] [blame] | 509 | def get_best_visible(atom, build_target=None): |
| 510 | """Returns the best visible CPV for the given atom. |
| 511 | |
| 512 | Args: |
| 513 | atom (str): The atom to look up. |
| 514 | build_target (build_target_util.BuildTarget): The build target whose |
Alex Klein | da39c6d | 2019-09-16 14:36:36 -0600 | [diff] [blame] | 515 | sysroot should be searched, or the SDK if not provided. |
Alex Klein | bbef2b3 | 2019-08-27 10:38:50 -0600 | [diff] [blame] | 516 | """ |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 517 | assert atom |
Alex Klein | bbef2b3 | 2019-08-27 10:38:50 -0600 | [diff] [blame] | 518 | |
| 519 | board = build_target.name if build_target else None |
| 520 | return portage_util.PortageqBestVisible(atom, board=board) |
Alex Klein | da39c6d | 2019-09-16 14:36:36 -0600 | [diff] [blame] | 521 | |
| 522 | |
| 523 | def has_prebuilt(atom, build_target=None): |
| 524 | """Check if a prebuilt exists. |
| 525 | |
| 526 | Args: |
| 527 | atom (str): The package whose prebuilt is being queried. |
| 528 | build_target (build_target_util.BuildTarget): The build target whose |
| 529 | sysroot should be searched, or the SDK if not provided. |
| 530 | """ |
| 531 | assert atom |
| 532 | |
| 533 | board = build_target.name if build_target else None |
| 534 | return portage_util.HasPrebuilt(atom, board=board) |
Alex Klein | 36b117f | 2019-09-30 15:13:46 -0600 | [diff] [blame] | 535 | |
| 536 | |
David Burger | 0f9dd4e | 2019-10-08 12:33:42 -0600 | [diff] [blame] | 537 | def builds(atom, build_target, packages=None): |
Alex Klein | 36b117f | 2019-09-30 15:13:46 -0600 | [diff] [blame] | 538 | """Check if |build_target| builds |atom| (has it in its depgraph).""" |
| 539 | cros_build_lib.AssertInsideChroot() |
| 540 | |
Chris McDonald | a22b74f | 2019-11-22 13:55:06 -0700 | [diff] [blame] | 541 | graph, _sdk_graph = dependency.GetBuildDependency(build_target.name, packages) |
Alex Klein | 36b117f | 2019-09-30 15:13:46 -0600 | [diff] [blame] | 542 | return any(atom in package for package in graph['package_deps']) |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 543 | |
| 544 | |
Michael Mortensen | b51a1f0 | 2019-10-16 13:28:20 -0600 | [diff] [blame] | 545 | def determine_chrome_version(build_target): |
Michael Mortensen | c2615b7 | 2019-10-15 08:12:24 -0600 | [diff] [blame] | 546 | """Returns the current Chrome version for the board (or in buildroot). |
| 547 | |
| 548 | Args: |
Michael Mortensen | b51a1f0 | 2019-10-16 13:28:20 -0600 | [diff] [blame] | 549 | build_target (build_target_util.BuildTarget): The board build target. |
Michael Mortensen | c2615b7 | 2019-10-15 08:12:24 -0600 | [diff] [blame] | 550 | """ |
Michael Mortensen | 9fe740c | 2019-10-29 14:42:48 -0600 | [diff] [blame] | 551 | # TODO(crbug/1019770): Long term we should not need the try/catch here once |
| 552 | # the builds function above only returns True for chrome when |
| 553 | # determine_chrome_version will succeed. |
| 554 | try: |
| 555 | cpv = portage_util.PortageqBestVisible(constants.CHROME_CP, |
| 556 | build_target.name, |
| 557 | cwd=constants.SOURCE_ROOT) |
| 558 | except cros_build_lib.RunCommandError as e: |
| 559 | # Return None because portage failed when trying to determine the chrome |
| 560 | # version. |
| 561 | logging.warning('Caught exception in determine_chrome_package: %s', e) |
| 562 | return None |
Michael Mortensen | c2615b7 | 2019-10-15 08:12:24 -0600 | [diff] [blame] | 563 | # Something like 78.0.3877.4_rc -> 78.0.3877.4 |
| 564 | return cpv.version_no_rev.partition('_')[0] |
| 565 | |
| 566 | |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 567 | def determine_android_package(board): |
| 568 | """Returns the active Android container package in use by the board. |
| 569 | |
| 570 | Args: |
| 571 | board: The board name this is specific to. |
| 572 | """ |
Michael Mortensen | e0f4b54 | 2019-10-24 15:30:23 -0600 | [diff] [blame] | 573 | try: |
| 574 | packages = portage_util.GetPackageDependencies(board, 'virtual/target-os') |
| 575 | # We assume there is only one Android package in the depgraph. |
| 576 | for package in packages: |
| 577 | if package.startswith('chromeos-base/android-container-') or \ |
| 578 | package.startswith('chromeos-base/android-vm-'): |
| 579 | return package |
| 580 | return None |
| 581 | except cros_build_lib.RunCommandError as e: |
| 582 | # Return None because a command (likely portage) failed when trying to |
| 583 | # determine the package. |
| 584 | logging.warning('Caught exception in determine_android_package: %s', e) |
| 585 | return None |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 586 | |
| 587 | |
| 588 | def determine_android_version(boards=None): |
| 589 | """Determine the current Android version in buildroot now and return it. |
| 590 | |
| 591 | This uses the typical portage logic to determine which version of Android |
| 592 | is active right now in the buildroot. |
| 593 | |
| 594 | Args: |
| 595 | boards: List of boards to check version of. |
| 596 | |
| 597 | Returns: |
| 598 | The Android build ID of the container for the boards. |
| 599 | |
| 600 | Raises: |
| 601 | NoAndroidVersionError: if no unique Android version can be determined. |
| 602 | """ |
| 603 | if not boards: |
| 604 | return None |
| 605 | # Verify that all boards have the same version. |
| 606 | version = None |
| 607 | for board in boards: |
| 608 | package = determine_android_package(board) |
| 609 | if not package: |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 610 | return None |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 611 | cpv = portage_util.SplitCPV(package) |
| 612 | if not cpv: |
| 613 | raise NoAndroidVersionError( |
| 614 | 'Android version could not be determined for %s' % board) |
| 615 | if not version: |
| 616 | version = cpv.version_no_rev |
| 617 | elif version != cpv.version_no_rev: |
| 618 | raise NoAndroidVersionError( |
| 619 | 'Different Android versions (%s vs %s) for %s' % |
| 620 | (version, cpv.version_no_rev, boards)) |
| 621 | return version |
| 622 | |
| 623 | def determine_android_branch(board): |
| 624 | """Returns the Android branch in use by the active container ebuild.""" |
| 625 | try: |
| 626 | android_package = determine_android_package(board) |
| 627 | except cros_build_lib.RunCommandError: |
| 628 | raise NoAndroidBranchError( |
| 629 | 'Android branch could not be determined for %s' % board) |
| 630 | if not android_package: |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 631 | return None |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 632 | ebuild_path = portage_util.FindEbuildForBoardPackage(android_package, board) |
| 633 | # We assume all targets pull from the same branch and that we always |
| 634 | # have an ARM_TARGET, ARM_USERDEBUG_TARGET, or an X86_USERDEBUG_TARGET. |
| 635 | targets = ['ARM_TARGET', 'ARM_USERDEBUG_TARGET', 'X86_USERDEBUG_TARGET'] |
| 636 | ebuild_content = osutils.SourceEnvironment(ebuild_path, targets) |
| 637 | for target in targets: |
| 638 | if target in ebuild_content: |
| 639 | branch = re.search(r'(.*?)-linux-', ebuild_content[target]) |
| 640 | if branch is not None: |
| 641 | return branch.group(1) |
| 642 | raise NoAndroidBranchError( |
| 643 | 'Android branch could not be determined for %s (ebuild empty?)' % board) |
| 644 | |
| 645 | |
| 646 | def determine_android_target(board): |
Michael Mortensen | 14960d0 | 2019-10-18 07:53:59 -0600 | [diff] [blame] | 647 | """Returns the Android target in use by the active container ebuild.""" |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 648 | try: |
| 649 | android_package = determine_android_package(board) |
| 650 | except cros_build_lib.RunCommandError: |
| 651 | raise NoAndroidTargetError( |
| 652 | 'Android Target could not be determined for %s' % board) |
| 653 | if not android_package: |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 654 | return None |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 655 | if android_package.startswith('chromeos-base/android-vm-'): |
| 656 | return 'bertha' |
| 657 | elif android_package.startswith('chromeos-base/android-container-'): |
| 658 | return 'cheets' |
| 659 | |
| 660 | raise NoAndroidTargetError( |
| 661 | 'Android Target cannot be determined for the package: %s' % |
| 662 | android_package) |
Michael Mortensen | 9fdb14b | 2019-10-17 11:17:30 -0600 | [diff] [blame] | 663 | |
| 664 | |
| 665 | def determine_platform_version(): |
| 666 | """Returns the platform version from the source root.""" |
Michael Mortensen | 009cb66 | 2019-10-21 11:38:43 -0600 | [diff] [blame] | 667 | # Platform version is something like '12575.0.0'. |
Michael Mortensen | 9fdb14b | 2019-10-17 11:17:30 -0600 | [diff] [blame] | 668 | version = manifest_version.VersionInfo.from_repo(constants.SOURCE_ROOT) |
| 669 | return version.VersionString() |
Michael Mortensen | 009cb66 | 2019-10-21 11:38:43 -0600 | [diff] [blame] | 670 | |
| 671 | |
| 672 | def determine_milestone_version(): |
| 673 | """Returns the platform version from the source root.""" |
| 674 | # Milestone version is something like '79'. |
| 675 | version = manifest_version.VersionInfo.from_repo(constants.SOURCE_ROOT) |
| 676 | return version.chrome_branch |
| 677 | |
| 678 | def determine_full_version(): |
| 679 | """Returns the full version from the source root.""" |
| 680 | # Full version is something like 'R79-12575.0.0'. |
| 681 | milestone_version = determine_milestone_version() |
| 682 | platform_version = determine_platform_version() |
| 683 | full_version = ('R%s-%s' % (milestone_version, platform_version)) |
| 684 | return full_version |