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 | |
Alex Klein | dfad94c | 2022-05-23 16:59:47 -0600 | [diff] [blame] | 109 | def InstallProtoc(protoc_version: ProtocVersion) -> Path: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | """Install protoc from CIPD.""" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 111 | if protoc_version is ProtocVersion.SDK: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | cipd_root = None |
| 113 | else: |
| 114 | cipd_root = Path( |
| 115 | cipd.InstallPackage( |
| 116 | cipd.GetCIPDFromCache(), _CIPD_PACKAGE, _CIPD_PACKAGE_VERSION |
| 117 | ) |
| 118 | ) |
| 119 | return protoc_version.get_protoc_command(cipd_root) |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 120 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 121 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 122 | def _CleanTargetDirectory(directory: Path, protoc_version: ProtocVersion): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | """Remove any existing generated files in the directory. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | This clean only removes the generated files to avoid accidentally destroying |
| 126 | __init__.py customizations down the line. That will leave otherwise empty |
| 127 | directories in place if things get moved. Neither case is relevant at the |
| 128 | time of writing, but lingering empty directories seemed better than |
| 129 | diagnosing accidental __init__.py changes. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 132 | directory: Path to be cleaned up. |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 133 | protoc_version: The type of generated files to be cleaned. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | """ |
| 135 | logging.info("Cleaning old files from %s.", directory) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 136 | for current in directory.rglob(f"*_pb2.{protoc_version.get_suffix()}"): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | # Remove old generated files. |
| 138 | current.unlink() |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 139 | |
| 140 | # Note the generator does not currently make __init__.pyi files but, if it |
| 141 | # did, we'd want them to be cleaned up here. |
| 142 | for current in directory.rglob(f"__init__.{protoc_version.get_suffix()}"): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | # Remove empty init files to clean up otherwise empty directories. |
| 144 | if not current.stat().st_size: |
| 145 | current.unlink() |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 146 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | def _GenerateFiles( |
| 149 | source: Path, |
| 150 | output: Path, |
| 151 | protoc_version: ProtocVersion, |
| 152 | dir_subset: SubdirectorySet, |
| 153 | protoc_bin_path: Path, |
| 154 | ): |
| 155 | """Generate the proto files from the |source| tree into |output|. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 158 | source: Path to the proto source root directory. |
| 159 | output: Path to the output root directory. |
| 160 | protoc_version: Which protoc to use. |
| 161 | dir_subset: The subset of the proto to compile. |
| 162 | protoc_bin_path: The protoc command to use. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | """ |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 164 | logging.info("Generating %s files to %s.", protoc_version, output) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | osutils.SafeMakedirs(output) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 166 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | targets = [] |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 168 | |
Mike Frysinger | 903c1f7 | 2023-08-08 14:05:10 -0400 | [diff] [blame] | 169 | chromeos_config_path = constants.SOURCE_ROOT / "src" / "config" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | with tempfile.TemporaryDirectory() as tempdir: |
| 172 | if not chromeos_config_path.exists(): |
| 173 | chromeos_config_path = Path(tempdir) / "config" |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | logging.info("Creating shallow clone of chromiumos/config") |
| 176 | git.Clone( |
| 177 | chromeos_config_path, |
| 178 | "%s/chromiumos/config" % constants.EXTERNAL_GOB_URL, |
| 179 | depth=1, |
| 180 | ) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | for src_dir in dir_subset.get_source_dirs(source, chromeos_config_path): |
| 183 | targets.extend(list(src_dir.rglob("*.proto"))) |
Andrew Lamb | 59ed32e | 2021-07-26 15:14:37 -0600 | [diff] [blame] | 184 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 185 | output_type = ( |
| 186 | "pyi" if protoc_version is ProtocVersion.CHROMITE_PYI else "python" |
| 187 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | cmd = [ |
| 189 | protoc_bin_path, |
| 190 | "-I", |
| 191 | chromeos_config_path / "proto", |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 192 | f"--{output_type}_out", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | output, |
| 194 | "--proto_path", |
| 195 | source, |
| 196 | ] |
| 197 | cmd.extend(targets) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | result = cros_build_lib.dbg_run( |
| 200 | cmd, |
| 201 | cwd=source, |
| 202 | check=False, |
| 203 | enter_chroot=protoc_version is ProtocVersion.SDK, |
| 204 | ) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 205 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 206 | if result.returncode: |
| 207 | raise GenerationError( |
| 208 | "Error compiling the proto. See the output for a " "message." |
| 209 | ) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 210 | |
| 211 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 212 | def _InstallMissingInits(directory: Path, protoc_version: ProtocVersion): |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 213 | """Add missing __init__.py files in the generated protobuf folders.""" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 214 | if protoc_version is ProtocVersion.CHROMITE_PYI: |
| 215 | # For pyi, rely on module markers left behind by CHROMITE flows to avoid |
| 216 | # additional clutter. |
| 217 | return |
| 218 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 219 | logging.info("Adding missing __init__.py files in %s.", directory) |
| 220 | # glob ** returns only directories. |
| 221 | for current in directory.rglob("**"): |
| 222 | (current / "__init__.py").touch() |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 223 | |
| 224 | |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 225 | def _PostprocessFiles(directory: Path, protoc_version: ProtocVersion): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | """Do postprocessing on the generated files. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 229 | directory: The root directory containing the generated files that are |
| 230 | to be processed. |
| 231 | protoc_version: Which protoc is being used to generate the files. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 232 | """ |
| 233 | logging.info("Postprocessing: Fix imports in %s.", directory) |
| 234 | # We are using a negative address here (the /address/! portion of the sed |
| 235 | # command) to make sure we don't change any imports from protobuf itself. |
| 236 | address = "^from google.protobuf" |
| 237 | # Find: 'from x import y_pb2 as x_dot_y_pb2'. |
| 238 | # "\(^google.protobuf[^ ]*\)" matches the module we're importing from. |
| 239 | # - \( and \) are for groups in sed. |
| 240 | # - ^google.protobuf prevents changing the import for protobuf's files. |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 241 | # - [^ ] = Not a space. The [:space:] character set is too broad, but |
| 242 | # would technically work too. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | find = r"^from \([^ ]*\) import \([^ ]*\)_pb2 as \([^ ]*\)$" |
| 244 | # Substitute: 'from chromite.api.gen[_sdk].x import y_pb2 as x_dot_y_pb2'. |
| 245 | if protoc_version is ProtocVersion.SDK: |
| 246 | sub = "from chromite.api.gen_sdk.\\1 import \\2_pb2 as \\3" |
| 247 | else: |
| 248 | sub = "from chromite.api.gen.\\1 import \\2_pb2 as \\3" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 249 | |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 250 | seds = [f"/{address}/!s/{find}/{sub}/g"] |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 251 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | if protoc_version is ProtocVersion.CHROMITE: |
| 253 | # We also need to change the google.protobuf imports to point directly |
| 254 | # at the chromite.third_party version of the library. |
| 255 | # The SDK version of the proto is meant to be used with the protobuf |
| 256 | # libraries installed in the SDK, so leave those as google.protobuf. |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 257 | # For CHROMITE_PYI, types for the protobuf imports come from type stubs, |
| 258 | # which won't map if the imports are renamed to third_party. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | g_p_address = "^from google.protobuf" |
| 260 | g_p_find = r"from \([^ ]*\) import \(.*\)$" |
| 261 | g_p_sub = "from chromite.third_party.\\1 import \\2" |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 262 | 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] | 263 | |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 264 | if protoc_version is ProtocVersion.CHROMITE_PYI: |
| 265 | # Workaround https://github.com/protocolbuffers/protobuf/issues/11402 |
| 266 | # until cl/560557754 in in the protoc version used. |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 267 | untyped_empty_slot_list = "s/\\(^ *__slots__ = \\)\\[]/\\1()/" |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 268 | |
| 269 | # Suppress errors on GRPC options field numbers using ClassVar outside |
| 270 | # of a class body. See b/297782342. |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 271 | ignore_class_var_in_file_scope = ( |
| 272 | "s/^[A-Z_]*: _ClassVar\\[int]/& # type: ignore/" |
| 273 | ) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 274 | seds.extend((untyped_empty_slot_list, ignore_class_var_in_file_scope)) |
| 275 | |
| 276 | pb2 = list(directory.rglob(f"*_pb2.{protoc_version.get_suffix()}")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | if pb2: |
Trent Apted | c1618e7 | 2023-09-25 10:30:14 +1000 | [diff] [blame] | 278 | cros_build_lib.dbg_run(["sed", "-i", ";".join(seds)] + pb2) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 279 | |
| 280 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | def CompileProto( |
| 282 | protoc_version: ProtocVersion, |
| 283 | output: Optional[Path] = None, |
| 284 | dir_subset: SubdirectorySet = SubdirectorySet.DEFAULT, |
| 285 | postprocess: bool = True, |
| 286 | ): |
| 287 | """Compile the Build API protobuf files. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 288 | |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 289 | 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] | 290 | directory may be changed, but the imports will always be treated as if it is |
| 291 | in the default location. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 294 | output: The output directory. |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 295 | protoc_version: Which protoc to use for the compilation. |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 296 | dir_subset: What proto to compile. |
| 297 | postprocess: Whether to run the postprocess step. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 298 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 299 | source = protoc_version.get_proto_dir() / "src" |
| 300 | if not output: |
| 301 | output = protoc_version.get_gen_dir() |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 302 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 303 | protoc_bin_path = InstallProtoc(protoc_version) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 304 | _CleanTargetDirectory(output, protoc_version) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 305 | _GenerateFiles(source, output, protoc_version, dir_subset, protoc_bin_path) |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 306 | _InstallMissingInits(output, protoc_version) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 307 | if postprocess: |
| 308 | _PostprocessFiles(output, protoc_version) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 309 | |
| 310 | |
| 311 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 312 | """Build the argument parser.""" |
| 313 | parser = commandline.ArgumentParser(description=__doc__) |
| 314 | standard_group = parser.add_argument_group( |
| 315 | "Committed Bindings", |
| 316 | description="Options for generating the bindings in chromite/api/.", |
| 317 | ) |
| 318 | standard_group.add_argument( |
| 319 | "--chromite", |
| 320 | dest="protoc_version", |
| 321 | action="append_const", |
| 322 | const=ProtocVersion.CHROMITE, |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 323 | help="Generate only the chromite bindings. Generates all by default. " |
| 324 | "The chromite bindings are compatible with the version of protobuf " |
| 325 | "in chromite/third_party.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | ) |
| 327 | standard_group.add_argument( |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 328 | "--pyi", |
| 329 | dest="protoc_version", |
| 330 | action="append_const", |
| 331 | const=ProtocVersion.CHROMITE_PYI, |
| 332 | help="Generate only the pyi type annotations.", |
| 333 | ) |
| 334 | standard_group.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 335 | "--sdk", |
| 336 | dest="protoc_version", |
| 337 | action="append_const", |
| 338 | const=ProtocVersion.SDK, |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 339 | help="Generate only the SDK bindings. Generates all by default. The " |
| 340 | "SDK bindings are compiled by protoc in the SDK, and is compatible " |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 341 | "with the version of protobuf in the SDK (i.e. the one installed by " |
| 342 | "the ebuild).", |
| 343 | ) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 344 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 345 | dest_group = parser.add_argument_group( |
| 346 | "Out of Tree Bindings", |
| 347 | description="Options for generating bindings in a custom location.", |
| 348 | ) |
| 349 | dest_group.add_argument( |
| 350 | "--destination", |
| 351 | type="path", |
| 352 | help="A directory where a single version of the proto should be " |
| 353 | "generated. When not given, the proto generates in all default " |
| 354 | "locations instead.", |
| 355 | ) |
| 356 | dest_group.add_argument( |
| 357 | "--dest-sdk", |
| 358 | action="store_const", |
| 359 | dest="dest_protoc", |
| 360 | default=ProtocVersion.CHROMITE, |
| 361 | const=ProtocVersion.SDK, |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 362 | help="Generate the SDK version of the protos in --destination instead " |
| 363 | "of the chromite version.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 364 | ) |
| 365 | dest_group.add_argument( |
| 366 | "--all-proto", |
| 367 | action="store_const", |
| 368 | dest="dir_subset", |
| 369 | default=SubdirectorySet.DEFAULT, |
| 370 | const=SubdirectorySet.ALL, |
| 371 | help="Compile ALL proto instead of just the subset needed for the API. " |
| 372 | "Only considered when generating out of tree bindings.", |
| 373 | ) |
| 374 | dest_group.add_argument( |
| 375 | "--skip-postprocessing", |
| 376 | action="store_false", |
| 377 | dest="postprocess", |
| 378 | default=True, |
| 379 | help="Skip postprocessing files.", |
| 380 | ) |
| 381 | return parser |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 382 | |
| 383 | |
| 384 | def _ParseArguments(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 385 | """Parse and validate arguments.""" |
| 386 | parser = GetParser() |
| 387 | opts = parser.parse_args(argv) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 388 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 389 | if not opts.protoc_version: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 390 | opts.protoc_version = [ |
| 391 | ProtocVersion.CHROMITE, |
| 392 | ProtocVersion.SDK, |
| 393 | ProtocVersion.CHROMITE_PYI, |
| 394 | ] |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 395 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 396 | if opts.destination: |
| 397 | opts.destination = Path(opts.destination) |
Alex Klein | 177bb94 | 2022-05-24 13:32:27 -0600 | [diff] [blame] | 398 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 399 | opts.Freeze() |
| 400 | return opts |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 401 | |
| 402 | |
| 403 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 404 | opts = _ParseArguments(argv) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 405 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 406 | if opts.destination: |
| 407 | # Destination set, only compile a single version in the destination. |
| 408 | try: |
| 409 | CompileProto( |
| 410 | protoc_version=opts.dest_protoc, |
| 411 | output=opts.destination, |
| 412 | dir_subset=opts.dir_subset, |
| 413 | postprocess=opts.postprocess, |
| 414 | ) |
| 415 | except Error as e: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 416 | cros_build_lib.Die("Error compiling bindings to destination: %s", e) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 417 | else: |
| 418 | return 0 |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | if ProtocVersion.CHROMITE in opts.protoc_version: |
| 421 | # Compile the chromite bindings. |
| 422 | try: |
| 423 | CompileProto(protoc_version=ProtocVersion.CHROMITE) |
| 424 | except Error as e: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 425 | cros_build_lib.Die("Error compiling chromite bindings: %s", e) |
| 426 | |
| 427 | if ProtocVersion.CHROMITE_PYI in opts.protoc_version: |
| 428 | try: |
| 429 | CompileProto(protoc_version=ProtocVersion.CHROMITE_PYI) |
| 430 | except Error as e: |
| 431 | cros_build_lib.Die("Error compiling type annotations: %s", e) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 432 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 433 | if ProtocVersion.SDK in opts.protoc_version: |
| 434 | # Compile the SDK bindings. |
| 435 | if not cros_build_lib.IsInsideChroot(): |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 436 | # Rerun inside the SDK instead of trying to map all the paths. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 437 | cmd = [ |
| 438 | ( |
Mike Frysinger | 83e7ff2 | 2023-08-07 21:42:28 -0400 | [diff] [blame] | 439 | constants.CHROOT_SOURCE_ROOT |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | / "chromite" |
| 441 | / "api" |
| 442 | / "compile_build_api_proto" |
| 443 | ), |
| 444 | "--sdk", |
| 445 | ] |
| 446 | result = cros_build_lib.dbg_run(cmd, enter_chroot=True, check=False) |
| 447 | return result.returncode |
| 448 | else: |
| 449 | try: |
| 450 | CompileProto(protoc_version=ProtocVersion.SDK) |
| 451 | except Error as e: |
Trent Apted | 8f5782a | 2023-07-11 10:25:02 +1000 | [diff] [blame] | 452 | cros_build_lib.Die("Error compiling SDK bindings: %s", e) |