Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 5 | """Compile the Build API's proto. |
| 6 | |
| 7 | Install proto using CIPD to ensure a consistent protoc version. |
| 8 | """ |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 9 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 10 | import enum |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 11 | import logging |
Alex Klein | b382e4b | 2022-05-23 16:29:19 -0600 | [diff] [blame] | 12 | from pathlib import Path |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 13 | import tempfile |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 14 | from typing import Iterable, Optional |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 15 | |
Alex Klein | b382e4b | 2022-05-23 16:29:19 -0600 | [diff] [blame] | 16 | from chromite.lib import cipd |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 17 | from chromite.lib import commandline |
Alex Klein | c33c191 | 2019-02-15 10:29:13 -0700 | [diff] [blame] | 18 | from chromite.lib import constants |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 20 | from chromite.lib import git |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 21 | from chromite.lib import osutils |
| 22 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 23 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 24 | # Chromite's protobuf library version (third_party/google/protobuf). |
Trent Apted | 5a2038f | 2023-07-26 15:23:46 +1000 | [diff] [blame] | 25 | PROTOC_VERSION = "21.9" |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 26 | |
Trent Apted | 5a2038f | 2023-07-26 15:23:46 +1000 | [diff] [blame] | 27 | # Protobuf dropped the major version number after 3.20, jumping to 21.0. But |
| 28 | # some places (e.g., in protobuf/__init__.py) refer to this as 4.21.0. |
| 29 | PROTOC_MAJOR_VERSION = "4" |
| 30 | |
| 31 | _CIPD_PACKAGE = "infra/3pp/tools/protoc/linux-amd64" |
| 32 | _CIPD_PACKAGE_VERSION = f"version:2@{PROTOC_VERSION}" |
Alex Klein | b382e4b | 2022-05-23 16:29:19 -0600 | [diff] [blame] | 33 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 34 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 35 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | """Base error class for the module.""" |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 37 | |
| 38 | |
| 39 | class GenerationError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | """A failure we can't recover from.""" |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 41 | |
| 42 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 43 | @enum.unique |
| 44 | class ProtocVersion(enum.Enum): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | """Enum for possible protoc versions.""" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | # The SDK version of the bindings use the protoc in the SDK, and so is |
| 48 | # compatible with the protobuf library in the SDK, i.e. the one installed |
| 49 | # via the ebuild. |
| 50 | SDK = enum.auto() |
| 51 | # The Chromite version of the bindings uses a protoc binary downloaded from |
| 52 | # CIPD that matches the version of the protobuf library in |
| 53 | # chromite/third_party/google/protobuf. |
| 54 | CHROMITE = enum.auto() |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 55 | # Type annotations for the chromite bindings. |
| 56 | CHROMITE_PYI = enum.auto() |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | def get_gen_dir(self) -> Path: |
| 59 | """Get the chromite/api directory path.""" |
| 60 | if self is ProtocVersion.SDK: |
Mike Frysinger | a69df98 | 2023-03-21 16:52:27 -0400 | [diff] [blame] | 61 | return constants.CHROMITE_DIR / "api" / "gen_sdk" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 62 | return constants.CHROMITE_DIR / "api" / "gen" |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | def get_proto_dir(self) -> Path: |
| 65 | """Get the proto directory for the target protoc.""" |
Mike Frysinger | a69df98 | 2023-03-21 16:52:27 -0400 | [diff] [blame] | 66 | return constants.CHROMITE_DIR / "infra" / "proto" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | |
| 68 | def get_protoc_command(self, cipd_root: Optional[Path] = None) -> Path: |
| 69 | """Get protoc command path.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | if self is ProtocVersion.SDK: |
| 71 | return Path("protoc") |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 72 | assert cipd_root |
| 73 | return cipd_root / "bin" / "protoc" |
| 74 | |
| 75 | def get_suffix(self) -> str: |
| 76 | """Get the file suffix of generated output.""" |
| 77 | return "pyi" if self is ProtocVersion.CHROMITE_PYI else "py" |
Alex Klein | dfad94c | 2022-05-23 16:59:47 -0600 | [diff] [blame] | 78 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 79 | |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 80 | @enum.unique |
| 81 | class SubdirectorySet(enum.Enum): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | """Enum for the subsets of the proto to compile.""" |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | ALL = enum.auto() |
| 85 | DEFAULT = enum.auto() |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | def get_source_dirs( |
| 88 | self, source: Path, chromeos_config_path: Path |
| 89 | ) -> Iterable[Path]: |
| 90 | """Get the directories for the given subdirectory set.""" |
| 91 | if self is self.ALL: |
| 92 | return [ |
| 93 | source, |
| 94 | chromeos_config_path / "proto" / "chromiumos", |
| 95 | ] |
| 96 | |
| 97 | subdirs = [ |
| 98 | source / "analysis_service", |
| 99 | source / "chromite", |
| 100 | source / "chromiumos", |
| 101 | source / "config", |
| 102 | source / "test_platform", |
| 103 | source / "device", |
| 104 | chromeos_config_path / "proto" / "chromiumos", |
| 105 | ] |
| 106 | return subdirs |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 107 | |
| 108 | |
Trent Apted | db63e14 | 2023-09-26 15:46:09 +1000 | [diff] [blame^] | 109 | def check_upstream_changes(repo: Path) -> None: |
| 110 | """Ensures the checked-out branch at `repo` includes the upstream HEAD.""" |
| 111 | branch = git.GetCurrentBranch(repo) |
| 112 | if branch: |
| 113 | upstream = git.GetTrackingBranchViaGitConfig( |
| 114 | repo, branch, for_checkout=False |
| 115 | ) |
| 116 | if not upstream: |
| 117 | cros_build_lib.Die("Failed to get upstream for %s.", repo) |
| 118 | else: |
| 119 | # Detached head. |
| 120 | branch = "HEAD" |
| 121 | upstream = git.RemoteRef("cros", "refs/heads/main") |
| 122 | remote_head = git.RunGit( |
| 123 | repo, |
| 124 | ["ls-remote", upstream.remote, upstream.ref], |
| 125 | ).stdout.split(maxsplit=1)[0] |
| 126 | logging.notice( |
| 127 | "Ensuring proto dir contains %s from %s", |
| 128 | upstream.ref, |
| 129 | upstream.remote, |
| 130 | ) |
| 131 | branches = git.RunGit( |
| 132 | repo, |
| 133 | ["branch", "--contains", remote_head, branch], |
| 134 | print_cmd=True, |
| 135 | check=False, |
| 136 | ) |
| 137 | # Git emits the branch name if the commit is in the history. Otherwise, the |
| 138 | # commit is either not found (git exits with an error - 129), or missing |
| 139 | # from the branch. In either case, there is no output to stdout. |
| 140 | if not branches.stdout: |
| 141 | cros_build_lib.Die( |
| 142 | "The checked-out branch (%s) at %s is missing the remote head.\n" |
| 143 | "Please repo sync (and rebase), or skip this check by passing" |
| 144 | " --no-check-upstream-proto-changes-included.", |
| 145 | branch, |
| 146 | repo, |
| 147 | ) |
| 148 | |
| 149 | |
Alex Klein | dfad94c | 2022-05-23 16:59:47 -0600 | [diff] [blame] | 150 | def InstallProtoc(protoc_version: ProtocVersion) -> Path: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | """Install protoc from CIPD.""" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 152 | if protoc_version is ProtocVersion.SDK: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | cipd_root = None |
| 154 | else: |
| 155 | cipd_root = Path( |
| 156 | cipd.InstallPackage( |
| 157 | cipd.GetCIPDFromCache(), _CIPD_PACKAGE, _CIPD_PACKAGE_VERSION |
| 158 | ) |
| 159 | ) |
| 160 | return protoc_version.get_protoc_command(cipd_root) |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 161 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 162 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 163 | def _CleanTargetDirectory(directory: Path, protoc_version: ProtocVersion): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | """Remove any existing generated files in the directory. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 165 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | This clean only removes the generated files to avoid accidentally destroying |
| 167 | __init__.py customizations down the line. That will leave otherwise empty |
| 168 | directories in place if things get moved. Neither case is relevant at the |
| 169 | time of writing, but lingering empty directories seemed better than |
| 170 | diagnosing accidental __init__.py changes. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 173 | directory: Path to be cleaned up. |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 174 | protoc_version: The type of generated files to be cleaned. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | """ |
| 176 | logging.info("Cleaning old files from %s.", directory) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 177 | for current in directory.rglob(f"*_pb2.{protoc_version.get_suffix()}"): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | # Remove old generated files. |
| 179 | current.unlink() |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 180 | |
| 181 | # Note the generator does not currently make __init__.pyi files but, if it |
| 182 | # did, we'd want them to be cleaned up here. |
| 183 | for current in directory.rglob(f"__init__.{protoc_version.get_suffix()}"): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | # Remove empty init files to clean up otherwise empty directories. |
| 185 | if not current.stat().st_size: |
| 186 | current.unlink() |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 187 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 188 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 189 | def _GenerateFiles( |
| 190 | source: Path, |
| 191 | output: Path, |
| 192 | protoc_version: ProtocVersion, |
| 193 | dir_subset: SubdirectorySet, |
| 194 | protoc_bin_path: Path, |
| 195 | ): |
| 196 | """Generate the proto files from the |source| tree into |output|. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 197 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 199 | source: Path to the proto source root directory. |
| 200 | output: Path to the output root directory. |
| 201 | protoc_version: Which protoc to use. |
| 202 | dir_subset: The subset of the proto to compile. |
| 203 | protoc_bin_path: The protoc command to use. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | """ |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 205 | logging.info("Generating %s files to %s.", protoc_version, output) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 206 | osutils.SafeMakedirs(output) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | targets = [] |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 209 | |
Mike Frysinger | 903c1f7 | 2023-08-08 14:05:10 -0400 | [diff] [blame] | 210 | chromeos_config_path = constants.SOURCE_ROOT / "src" / "config" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | with tempfile.TemporaryDirectory() as tempdir: |
| 213 | if not chromeos_config_path.exists(): |
| 214 | chromeos_config_path = Path(tempdir) / "config" |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 215 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | logging.info("Creating shallow clone of chromiumos/config") |
| 217 | git.Clone( |
| 218 | chromeos_config_path, |
| 219 | "%s/chromiumos/config" % constants.EXTERNAL_GOB_URL, |
| 220 | depth=1, |
| 221 | ) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 222 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 223 | for src_dir in dir_subset.get_source_dirs(source, chromeos_config_path): |
| 224 | targets.extend(list(src_dir.rglob("*.proto"))) |
Andrew Lamb | 59ed32e | 2021-07-26 15:14:37 -0600 | [diff] [blame] | 225 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 226 | output_type = ( |
| 227 | "pyi" if protoc_version is ProtocVersion.CHROMITE_PYI else "python" |
| 228 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | cmd = [ |
| 230 | protoc_bin_path, |
| 231 | "-I", |
| 232 | chromeos_config_path / "proto", |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 233 | f"--{output_type}_out", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 234 | output, |
| 235 | "--proto_path", |
| 236 | source, |
| 237 | ] |
| 238 | cmd.extend(targets) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 239 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | result = cros_build_lib.dbg_run( |
| 241 | cmd, |
| 242 | cwd=source, |
| 243 | check=False, |
| 244 | enter_chroot=protoc_version is ProtocVersion.SDK, |
| 245 | ) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 246 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 247 | if result.returncode: |
| 248 | raise GenerationError( |
| 249 | "Error compiling the proto. See the output for a " "message." |
| 250 | ) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 251 | |
| 252 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 253 | def _InstallMissingInits(directory: Path, protoc_version: ProtocVersion): |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 254 | """Add missing __init__.py files in the generated protobuf folders.""" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 255 | if protoc_version is ProtocVersion.CHROMITE_PYI: |
| 256 | # For pyi, rely on module markers left behind by CHROMITE flows to avoid |
| 257 | # additional clutter. |
| 258 | return |
| 259 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | logging.info("Adding missing __init__.py files in %s.", directory) |
| 261 | # glob ** returns only directories. |
| 262 | for current in directory.rglob("**"): |
| 263 | (current / "__init__.py").touch() |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 264 | |
| 265 | |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 266 | def _PostprocessFiles(directory: Path, protoc_version: ProtocVersion): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | """Do postprocessing on the generated files. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 268 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 269 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 270 | directory: The root directory containing the generated files that are |
| 271 | to be processed. |
| 272 | protoc_version: Which protoc is being used to generate the files. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 273 | """ |
| 274 | logging.info("Postprocessing: Fix imports in %s.", directory) |
| 275 | # We are using a negative address here (the /address/! portion of the sed |
| 276 | # command) to make sure we don't change any imports from protobuf itself. |
| 277 | address = "^from google.protobuf" |
| 278 | # Find: 'from x import y_pb2 as x_dot_y_pb2'. |
| 279 | # "\(^google.protobuf[^ ]*\)" matches the module we're importing from. |
| 280 | # - \( and \) are for groups in sed. |
| 281 | # - ^google.protobuf prevents changing the import for protobuf's files. |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 282 | # - [^ ] = Not a space. The [:space:] character set is too broad, but |
| 283 | # would technically work too. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 284 | find = r"^from \([^ ]*\) import \([^ ]*\)_pb2 as \([^ ]*\)$" |
| 285 | # Substitute: 'from chromite.api.gen[_sdk].x import y_pb2 as x_dot_y_pb2'. |
| 286 | if protoc_version is ProtocVersion.SDK: |
| 287 | sub = "from chromite.api.gen_sdk.\\1 import \\2_pb2 as \\3" |
| 288 | else: |
| 289 | sub = "from chromite.api.gen.\\1 import \\2_pb2 as \\3" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 290 | |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 291 | seds = [f"/{address}/!s/{find}/{sub}/g"] |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | if protoc_version is ProtocVersion.CHROMITE: |
| 294 | # We also need to change the google.protobuf imports to point directly |
| 295 | # at the chromite.third_party version of the library. |
| 296 | # The SDK version of the proto is meant to be used with the protobuf |
| 297 | # libraries installed in the SDK, so leave those as google.protobuf. |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 298 | # For CHROMITE_PYI, types for the protobuf imports come from type stubs, |
| 299 | # which won't map if the imports are renamed to third_party. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 300 | g_p_address = "^from google.protobuf" |
| 301 | g_p_find = r"from \([^ ]*\) import \(.*\)$" |
| 302 | g_p_sub = "from chromite.third_party.\\1 import \\2" |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 303 | seds.append(f"/{g_p_address}/s/{g_p_find}/{g_p_sub}/") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 304 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 305 | if protoc_version is ProtocVersion.CHROMITE_PYI: |
| 306 | # Workaround https://github.com/protocolbuffers/protobuf/issues/11402 |
| 307 | # until cl/560557754 in in the protoc version used. |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 308 | untyped_empty_slot_list = "s/\\(^ *__slots__ = \\)\\[]/\\1()/" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 309 | |
| 310 | # Suppress errors on GRPC options field numbers using ClassVar outside |
| 311 | # of a class body. See b/297782342. |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 312 | ignore_class_var_in_file_scope = ( |
| 313 | "s/^[A-Z_]*: _ClassVar\\[int]/& # type: ignore/" |
| 314 | ) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 315 | seds.extend((untyped_empty_slot_list, ignore_class_var_in_file_scope)) |
| 316 | |
| 317 | pb2 = list(directory.rglob(f"*_pb2.{protoc_version.get_suffix()}")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 318 | if pb2: |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 319 | cros_build_lib.dbg_run(["sed", "-i", ";".join(seds)] + pb2) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 320 | |
| 321 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 322 | def CompileProto( |
| 323 | protoc_version: ProtocVersion, |
| 324 | output: Optional[Path] = None, |
| 325 | dir_subset: SubdirectorySet = SubdirectorySet.DEFAULT, |
| 326 | postprocess: bool = True, |
| 327 | ): |
| 328 | """Compile the Build API protobuf files. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 329 | |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 330 | By default, this will compile from infra/proto/src to api/gen. The output |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 331 | directory may be changed, but the imports will always be treated as if it is |
| 332 | in the default location. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 333 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 335 | output: The output directory. |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 336 | protoc_version: Which protoc to use for the compilation. |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 337 | dir_subset: What proto to compile. |
| 338 | postprocess: Whether to run the postprocess step. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 339 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 340 | source = protoc_version.get_proto_dir() / "src" |
| 341 | if not output: |
| 342 | output = protoc_version.get_gen_dir() |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 343 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 344 | protoc_bin_path = InstallProtoc(protoc_version) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 345 | _CleanTargetDirectory(output, protoc_version) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 346 | _GenerateFiles(source, output, protoc_version, dir_subset, protoc_bin_path) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 347 | _InstallMissingInits(output, protoc_version) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | if postprocess: |
| 349 | _PostprocessFiles(output, protoc_version) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 350 | |
| 351 | |
| 352 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 353 | """Build the argument parser.""" |
| 354 | parser = commandline.ArgumentParser(description=__doc__) |
Trent Apted | db63e14 | 2023-09-26 15:46:09 +1000 | [diff] [blame^] | 355 | parser.add_bool_argument( |
| 356 | "--check-upstream-proto-changes-included", |
| 357 | True, |
| 358 | "Check whether the checked-out proto branch includes changes" |
| 359 | " from refs/heads/main on the remote.", |
| 360 | "Skip the check that validates whether upstream proto changes" |
| 361 | " are included in the current branch.", |
| 362 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 363 | standard_group = parser.add_argument_group( |
| 364 | "Committed Bindings", |
| 365 | description="Options for generating the bindings in chromite/api/.", |
| 366 | ) |
| 367 | standard_group.add_argument( |
| 368 | "--chromite", |
| 369 | dest="protoc_version", |
| 370 | action="append_const", |
| 371 | const=ProtocVersion.CHROMITE, |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 372 | help="Generate only the chromite bindings. Generates all by default. " |
| 373 | "The chromite bindings are compatible with the version of protobuf " |
| 374 | "in chromite/third_party.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 375 | ) |
| 376 | standard_group.add_argument( |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 377 | "--pyi", |
| 378 | dest="protoc_version", |
| 379 | action="append_const", |
| 380 | const=ProtocVersion.CHROMITE_PYI, |
| 381 | help="Generate only the pyi type annotations.", |
| 382 | ) |
| 383 | standard_group.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | "--sdk", |
| 385 | dest="protoc_version", |
| 386 | action="append_const", |
| 387 | const=ProtocVersion.SDK, |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 388 | help="Generate only the SDK bindings. Generates all by default. The " |
| 389 | "SDK bindings are compiled by protoc in the SDK, and is compatible " |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 390 | "with the version of protobuf in the SDK (i.e. the one installed by " |
| 391 | "the ebuild).", |
| 392 | ) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 393 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | dest_group = parser.add_argument_group( |
| 395 | "Out of Tree Bindings", |
| 396 | description="Options for generating bindings in a custom location.", |
| 397 | ) |
| 398 | dest_group.add_argument( |
| 399 | "--destination", |
| 400 | type="path", |
| 401 | help="A directory where a single version of the proto should be " |
| 402 | "generated. When not given, the proto generates in all default " |
| 403 | "locations instead.", |
| 404 | ) |
| 405 | dest_group.add_argument( |
| 406 | "--dest-sdk", |
| 407 | action="store_const", |
| 408 | dest="dest_protoc", |
| 409 | default=ProtocVersion.CHROMITE, |
| 410 | const=ProtocVersion.SDK, |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 411 | help="Generate the SDK version of the protos in --destination instead " |
| 412 | "of the chromite version.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 413 | ) |
| 414 | dest_group.add_argument( |
| 415 | "--all-proto", |
| 416 | action="store_const", |
| 417 | dest="dir_subset", |
| 418 | default=SubdirectorySet.DEFAULT, |
| 419 | const=SubdirectorySet.ALL, |
| 420 | help="Compile ALL proto instead of just the subset needed for the API. " |
| 421 | "Only considered when generating out of tree bindings.", |
| 422 | ) |
| 423 | dest_group.add_argument( |
| 424 | "--skip-postprocessing", |
| 425 | action="store_false", |
| 426 | dest="postprocess", |
| 427 | default=True, |
| 428 | help="Skip postprocessing files.", |
| 429 | ) |
| 430 | return parser |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 431 | |
| 432 | |
| 433 | def _ParseArguments(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | """Parse and validate arguments.""" |
| 435 | parser = GetParser() |
| 436 | opts = parser.parse_args(argv) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | if not opts.protoc_version: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 439 | opts.protoc_version = [ |
| 440 | ProtocVersion.CHROMITE, |
| 441 | ProtocVersion.SDK, |
| 442 | ProtocVersion.CHROMITE_PYI, |
| 443 | ] |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 444 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | if opts.destination: |
| 446 | opts.destination = Path(opts.destination) |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 447 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 448 | opts.Freeze() |
| 449 | return opts |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 450 | |
| 451 | |
| 452 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | opts = _ParseArguments(argv) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 455 | if opts.destination: |
| 456 | # Destination set, only compile a single version in the destination. |
| 457 | try: |
| 458 | CompileProto( |
| 459 | protoc_version=opts.dest_protoc, |
| 460 | output=opts.destination, |
| 461 | dir_subset=opts.dir_subset, |
| 462 | postprocess=opts.postprocess, |
| 463 | ) |
| 464 | except Error as e: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 465 | cros_build_lib.Die("Error compiling bindings to destination: %s", e) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 466 | else: |
| 467 | return 0 |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 468 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 469 | if ProtocVersion.CHROMITE in opts.protoc_version: |
Trent Apted | db63e14 | 2023-09-26 15:46:09 +1000 | [diff] [blame^] | 470 | # Validate here to avoid checking again inside the chroot. |
| 471 | if opts.check_upstream_proto_changes_included: |
| 472 | check_upstream_changes(ProtocVersion.CHROMITE.get_proto_dir()) |
| 473 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 474 | # Compile the chromite bindings. |
| 475 | try: |
| 476 | CompileProto(protoc_version=ProtocVersion.CHROMITE) |
| 477 | except Error as e: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 478 | cros_build_lib.Die("Error compiling chromite bindings: %s", e) |
| 479 | |
| 480 | if ProtocVersion.CHROMITE_PYI in opts.protoc_version: |
| 481 | try: |
| 482 | CompileProto(protoc_version=ProtocVersion.CHROMITE_PYI) |
| 483 | except Error as e: |
| 484 | cros_build_lib.Die("Error compiling type annotations: %s", e) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 485 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | if ProtocVersion.SDK in opts.protoc_version: |
| 487 | # Compile the SDK bindings. |
| 488 | if not cros_build_lib.IsInsideChroot(): |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 489 | # Rerun inside the SDK instead of trying to map all the paths. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 490 | cmd = [ |
| 491 | ( |
Mike Frysinger | 83e7ff2 | 2023-08-07 21:42:28 -0400 | [diff] [blame] | 492 | constants.CHROOT_SOURCE_ROOT |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 493 | / "chromite" |
| 494 | / "api" |
| 495 | / "compile_build_api_proto" |
| 496 | ), |
| 497 | "--sdk", |
| 498 | ] |
| 499 | result = cros_build_lib.dbg_run(cmd, enter_chroot=True, check=False) |
| 500 | return result.returncode |
| 501 | else: |
| 502 | try: |
| 503 | CompileProto(protoc_version=ProtocVersion.SDK) |
| 504 | except Error as e: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 505 | cros_build_lib.Die("Error compiling SDK bindings: %s", e) |