Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 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 | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 12 | import os |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 13 | import tempfile |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 14 | from typing import Iterable, Union |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 15 | |
| 16 | from chromite.lib import commandline |
Alex Klein | c33c191 | 2019-02-15 10:29:13 -0700 | [diff] [blame] | 17 | from chromite.lib import constants |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 19 | from chromite.lib import git |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 20 | from chromite.lib import osutils |
| 21 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 22 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 23 | _CIPD_ROOT = os.path.join(constants.CHROMITE_DIR, '.cipd_bin') |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 24 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 25 | # Chromite's protobuf library version (third_party/google/protobuf). |
Mike Nichols | e366e7a | 2021-02-22 18:14:57 -0700 | [diff] [blame] | 26 | PROTOC_VERSION = '3.13.0' |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 27 | |
| 28 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 29 | class Error(Exception): |
| 30 | """Base error class for the module.""" |
| 31 | |
| 32 | |
| 33 | class GenerationError(Error): |
| 34 | """A failure we can't recover from.""" |
| 35 | |
| 36 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 37 | @enum.unique |
| 38 | class ProtocVersion(enum.Enum): |
| 39 | """Enum for possible protoc versions.""" |
| 40 | # The SDK version of the bindings use the protoc in the SDK, and so is |
| 41 | # compatible with the protobuf library in the SDK, i.e. the one installed |
| 42 | # via the ebuild. |
| 43 | SDK = enum.auto() |
| 44 | # The Chromite version of the bindings uses a protoc binary downloaded from |
| 45 | # CIPD that matches the version of the protobuf library in |
| 46 | # chromite/third_party/google/protobuf. |
| 47 | CHROMITE = enum.auto() |
| 48 | |
| 49 | |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 50 | @enum.unique |
| 51 | class SubdirectorySet(enum.Enum): |
| 52 | """Enum for the subsets of the proto to compile.""" |
| 53 | ALL = enum.auto() |
| 54 | DEFAULT = enum.auto() |
| 55 | |
| 56 | def get_source_dirs(self, source: Union[str, os.PathLike], |
| 57 | chromeos_config_path: Union[str, os.PathLike] |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 58 | ) -> Union[Iterable[str], Iterable[os.PathLike]]: |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 59 | """Get the directories for the given subdirectory set.""" |
| 60 | _join = lambda x, y: os.path.join(x, y) if isinstance(x, str) else x / y |
| 61 | if self is self.ALL: |
| 62 | return [ |
| 63 | source, |
| 64 | _join(chromeos_config_path, 'proto/chromiumos'), |
| 65 | ] |
| 66 | |
| 67 | subdirs = [ |
Alex Klein | 08c70e8 | 2022-05-19 10:10:27 -0600 | [diff] [blame] | 68 | _join(source, 'analysis_service'), |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 69 | _join(source, 'chromite'), |
| 70 | _join(source, 'chromiumos'), |
| 71 | _join(source, 'config'), |
| 72 | _join(source, 'test_platform'), |
| 73 | _join(source, 'device'), |
| 74 | _join(chromeos_config_path, 'proto/chromiumos'), |
| 75 | ] |
| 76 | return subdirs |
| 77 | |
| 78 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 79 | def _get_gen_dir(protoc_version: ProtocVersion): |
| 80 | """Get the chromite/api directory path.""" |
| 81 | if protoc_version is ProtocVersion.SDK: |
| 82 | return os.path.join(constants.CHROMITE_DIR, 'api', 'gen_sdk') |
| 83 | else: |
| 84 | return os.path.join(constants.CHROMITE_DIR, 'api', 'gen') |
| 85 | |
| 86 | |
| 87 | def _get_protoc_command(protoc_version: ProtocVersion): |
| 88 | """Get the protoc command for the target protoc.""" |
| 89 | if protoc_version is ProtocVersion.SDK: |
| 90 | return 'protoc' |
| 91 | else: |
| 92 | return os.path.join(_CIPD_ROOT, 'protoc') |
| 93 | |
| 94 | |
| 95 | def _get_proto_dir(_protoc_version): |
| 96 | """Get the proto directory for the target protoc.""" |
| 97 | return os.path.join(constants.CHROMITE_DIR, 'infra', 'proto') |
| 98 | |
| 99 | |
| 100 | def _InstallProtoc(protoc_version: ProtocVersion): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 101 | """Install protoc from CIPD.""" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 102 | if protoc_version is not ProtocVersion.CHROMITE: |
| 103 | return |
| 104 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 105 | logging.info('Installing protoc.') |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 106 | cmd = ['cipd', 'ensure'] |
| 107 | # Clean up the output. |
| 108 | cmd.extend(['-log-level', 'warning']) |
| 109 | # Set the install location. |
| 110 | cmd.extend(['-root', _CIPD_ROOT]) |
| 111 | |
| 112 | ensure_content = ('infra/tools/protoc/${platform} ' |
| 113 | 'protobuf_version:v%s' % PROTOC_VERSION) |
| 114 | with osutils.TempDir() as tempdir: |
| 115 | ensure_file = os.path.join(tempdir, 'cipd_ensure_file') |
| 116 | osutils.WriteFile(ensure_file, ensure_content) |
| 117 | |
| 118 | cmd.extend(['-ensure-file', ensure_file]) |
| 119 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 120 | cros_build_lib.run(cmd, cwd=constants.CHROMITE_DIR, print_cmd=False) |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 121 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 122 | |
Miriam Polzer | 713e654 | 2021-08-17 10:58:14 +0200 | [diff] [blame] | 123 | def _CleanTargetDirectory(directory: str): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 124 | """Remove any existing generated files in the directory. |
| 125 | |
| 126 | This clean only removes the generated files to avoid accidentally destroying |
| 127 | __init__.py customizations down the line. That will leave otherwise empty |
| 128 | directories in place if things get moved. Neither case is relevant at the |
| 129 | time of writing, but lingering empty directories seemed better than |
| 130 | diagnosing accidental __init__.py changes. |
| 131 | |
| 132 | Args: |
Miriam Polzer | 713e654 | 2021-08-17 10:58:14 +0200 | [diff] [blame] | 133 | directory: Path to be cleaned up. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 134 | """ |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 135 | logging.info('Cleaning old files from %s.', directory) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 136 | for dirpath, _dirnames, filenames in os.walk(directory): |
| 137 | old = [os.path.join(dirpath, f) for f in filenames if f.endswith('_pb2.py')] |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 138 | # Remove empty init files to clean up otherwise empty directories. |
| 139 | if '__init__.py' in filenames: |
| 140 | init = os.path.join(dirpath, '__init__.py') |
| 141 | if not osutils.ReadFile(init): |
| 142 | old.append(init) |
| 143 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 144 | for current in old: |
| 145 | osutils.SafeUnlink(current) |
| 146 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 147 | |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 148 | def _GenerateFiles(source: str, output: str, protoc_version: ProtocVersion, |
| 149 | dir_subset: SubdirectorySet): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 150 | """Generate the proto files from the |source| tree into |output|. |
| 151 | |
| 152 | Args: |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 153 | source: Path to the proto source root directory. |
| 154 | output: Path to the output root directory. |
| 155 | protoc_version: Which protoc to use. |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 156 | dir_subset: The subset of the proto to compile. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 157 | """ |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 158 | logging.info('Generating files to %s.', output) |
| 159 | osutils.SafeMakedirs(output) |
| 160 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 161 | targets = [] |
| 162 | |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 163 | chromeos_config_path = os.path.realpath( |
| 164 | os.path.join(constants.SOURCE_ROOT, 'src/config')) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 165 | |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 166 | with tempfile.TemporaryDirectory() as tempdir: |
| 167 | if not os.path.exists(chromeos_config_path): |
| 168 | chromeos_config_path = os.path.join(tempdir, 'config') |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 169 | |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 170 | logging.info('Creating shallow clone of chromiumos/config') |
Alex Klein | 4fd378d | 2022-03-29 16:00:49 -0600 | [diff] [blame] | 171 | git.Clone( |
| 172 | chromeos_config_path, |
| 173 | '%s/chromiumos/config' % constants.EXTERNAL_GOB_URL, |
| 174 | depth=1) |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 175 | |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 176 | for basedir in dir_subset.get_source_dirs(source, chromeos_config_path): |
Andrew Lamb | 59ed32e | 2021-07-26 15:14:37 -0600 | [diff] [blame] | 177 | for dirpath, _dirnames, filenames in os.walk(basedir): |
| 178 | for filename in filenames: |
| 179 | if filename.endswith('.proto'): |
| 180 | # We have a match, add the file. |
| 181 | targets.append(os.path.join(dirpath, filename)) |
| 182 | |
Sean McAllister | 6a5eaa0 | 2021-05-26 10:47:14 -0600 | [diff] [blame] | 183 | cmd = [ |
| 184 | _get_protoc_command(protoc_version), |
| 185 | '-I', |
| 186 | os.path.join(chromeos_config_path, 'proto'), |
| 187 | '--python_out', |
| 188 | output, |
| 189 | '--proto_path', |
| 190 | source, |
| 191 | ] |
| 192 | cmd.extend(targets) |
| 193 | |
| 194 | result = cros_build_lib.run( |
| 195 | cmd, |
| 196 | cwd=source, |
| 197 | print_cmd=False, |
| 198 | check=False, |
| 199 | enter_chroot=protoc_version is ProtocVersion.SDK) |
| 200 | |
| 201 | if result.returncode: |
| 202 | raise GenerationError('Error compiling the proto. See the output for a ' |
| 203 | 'message.') |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 204 | |
| 205 | |
| 206 | def _InstallMissingInits(directory): |
| 207 | """Add any __init__.py files not present in the generated protobuf folders.""" |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 208 | logging.info('Adding missing __init__.py files in %s.', directory) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 209 | for dirpath, _dirnames, filenames in os.walk(directory): |
| 210 | if '__init__.py' not in filenames: |
| 211 | osutils.Touch(os.path.join(dirpath, '__init__.py')) |
| 212 | |
| 213 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 214 | def _PostprocessFiles(directory: str, protoc_version: ProtocVersion): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 215 | """Do postprocessing on the generated files. |
| 216 | |
| 217 | Args: |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 218 | directory: The root directory containing the generated files that are |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 219 | to be processed. |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 220 | protoc_version: Which protoc is being used to generate the files. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 221 | """ |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 222 | logging.info('Postprocessing: Fix imports in %s.', directory) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 223 | # We are using a negative address here (the /address/! portion of the sed |
| 224 | # command) to make sure we don't change any imports from protobuf itself. |
Alex Klein | d20d816 | 2021-06-21 12:40:44 -0600 | [diff] [blame] | 225 | address = '^from google.protobuf' |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 226 | # Find: 'from x import y_pb2 as x_dot_y_pb2'. |
| 227 | # "\(^google.protobuf[^ ]*\)" matches the module we're importing from. |
| 228 | # - \( and \) are for groups in sed. |
| 229 | # - ^google.protobuf prevents changing the import for protobuf's files. |
| 230 | # - [^ ] = Not a space. The [:space:] character set is too broad, but would |
| 231 | # technically work too. |
| 232 | find = r'^from \([^ ]*\) import \([^ ]*\)_pb2 as \([^ ]*\)$' |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 233 | # Substitute: 'from chromite.api.gen[_sdk].x import y_pb2 as x_dot_y_pb2'. |
| 234 | if protoc_version is ProtocVersion.SDK: |
| 235 | sub = 'from chromite.api.gen_sdk.\\1 import \\2_pb2 as \\3' |
| 236 | else: |
| 237 | sub = 'from chromite.api.gen.\\1 import \\2_pb2 as \\3' |
| 238 | |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 239 | from_sed = [ |
| 240 | 'sed', '-i', |
| 241 | '/%(address)s/!s/%(find)s/%(sub)s/g' % { |
| 242 | 'address': address, |
| 243 | 'find': find, |
| 244 | 'sub': sub |
| 245 | } |
| 246 | ] |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 247 | |
Alex Klein | d20d816 | 2021-06-21 12:40:44 -0600 | [diff] [blame] | 248 | seds = [from_sed] |
| 249 | if protoc_version is ProtocVersion.CHROMITE: |
| 250 | # We also need to change the google.protobuf imports to point directly |
| 251 | # at the chromite.third_party version of the library. |
| 252 | # The SDK version of the proto is meant to be used with the protobuf |
| 253 | # libraries installed in the SDK, so leave those as google.protobuf. |
| 254 | g_p_address = '^from google.protobuf' |
| 255 | g_p_find = r'from \([^ ]*\) import \(.*\)$' |
| 256 | g_p_sub = 'from chromite.third_party.\\1 import \\2' |
| 257 | google_protobuf_sed = [ |
| 258 | 'sed', '-i', |
| 259 | '/%(address)s/s/%(find)s/%(sub)s/g' % { |
| 260 | 'address': g_p_address, |
| 261 | 'find': g_p_find, |
| 262 | 'sub': g_p_sub |
| 263 | } |
| 264 | ] |
| 265 | seds.append(google_protobuf_sed) |
| 266 | |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 267 | for dirpath, _dirnames, filenames in os.walk(directory): |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 268 | # Update the imports in the generated files. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 269 | pb2 = [os.path.join(dirpath, f) for f in filenames if f.endswith('_pb2.py')] |
| 270 | if pb2: |
Alex Klein | d20d816 | 2021-06-21 12:40:44 -0600 | [diff] [blame] | 271 | for sed in seds: |
| 272 | cmd = sed + pb2 |
| 273 | cros_build_lib.run(cmd, print_cmd=False) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 274 | |
| 275 | |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 276 | def CompileProto(output: str, |
| 277 | protoc_version: ProtocVersion, |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 278 | dir_subset: SubdirectorySet = SubdirectorySet.DEFAULT, |
| 279 | postprocess: bool = True): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 280 | """Compile the Build API protobuf files. |
| 281 | |
| 282 | By default this will compile from infra/proto/src to api/gen. The output |
| 283 | directory may be changed, but the imports will always be treated as if it is |
| 284 | in the default location. |
| 285 | |
| 286 | Args: |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 287 | output: The output directory. |
| 288 | protoc_version: Which protoc to use for the compile. |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 289 | dir_subset: What proto to compile. |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 290 | postprocess: Whether to run the postprocess step. |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 291 | """ |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 292 | source = os.path.join(_get_proto_dir(protoc_version), 'src') |
| 293 | protoc_version = protoc_version or ProtocVersion.CHROMITE |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 294 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 295 | _InstallProtoc(protoc_version) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 296 | _CleanTargetDirectory(output) |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 297 | _GenerateFiles(source, output, protoc_version, dir_subset) |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 298 | _InstallMissingInits(output) |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 299 | if postprocess: |
| 300 | _PostprocessFiles(output, protoc_version) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 301 | |
| 302 | |
| 303 | def GetParser(): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 304 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 305 | parser = commandline.ArgumentParser(description=__doc__) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 306 | standard_group = parser.add_argument_group( |
| 307 | 'Committed Bindings', |
| 308 | description='Options for generating the bindings in chromite/api/.') |
| 309 | standard_group.add_argument( |
| 310 | '--chromite', |
| 311 | dest='protoc_version', |
| 312 | action='append_const', |
| 313 | const=ProtocVersion.CHROMITE, |
| 314 | help='Generate only the chromite bindings. Generates all by default. The ' |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 315 | 'chromite bindings are compatible with the version of protobuf in ' |
| 316 | 'chromite/third_party.') |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 317 | standard_group.add_argument( |
| 318 | '--sdk', |
| 319 | dest='protoc_version', |
| 320 | action='append_const', |
| 321 | const=ProtocVersion.SDK, |
| 322 | help='Generate only the SDK bindings. Generates all by default. The SDK ' |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 323 | 'bindings are compiled by protoc in the SDK, and is compatible ' |
| 324 | 'with the version of protobuf in the SDK (i.e. the one installed by ' |
| 325 | 'the ebuild).') |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 326 | |
| 327 | dest_group = parser.add_argument_group( |
| 328 | 'Out of Tree Bindings', |
| 329 | description='Options for generating bindings in a custom location.') |
| 330 | dest_group.add_argument( |
Alex Klein | 5534f99 | 2019-09-16 16:31:23 -0600 | [diff] [blame] | 331 | '--destination', |
| 332 | type='path', |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 333 | help='A directory where a single version of the proto should be ' |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 334 | 'generated. When not given, the proto generates in all default ' |
| 335 | 'locations instead.') |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 336 | dest_group.add_argument( |
| 337 | '--dest-sdk', |
| 338 | action='store_const', |
| 339 | dest='dest_protoc', |
| 340 | default=ProtocVersion.CHROMITE, |
| 341 | const=ProtocVersion.SDK, |
| 342 | help='Generate the SDK version of the protos in --destination instead of ' |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 343 | 'the chromite version.') |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 344 | dest_group.add_argument( |
| 345 | '--all-proto', |
| 346 | action='store_const', |
| 347 | dest='dir_subset', |
| 348 | default=SubdirectorySet.DEFAULT, |
| 349 | const=SubdirectorySet.ALL, |
| 350 | help='Compile ALL proto instead of just the subset needed for the API. ' |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 351 | 'Only considered when generating out of tree bindings.') |
| 352 | dest_group.add_argument( |
| 353 | '--skip-postprocessing', |
| 354 | action='store_false', |
| 355 | dest='postprocess', |
| 356 | default=True, |
| 357 | help='Skip postprocessing files.' |
| 358 | ) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 359 | return parser |
| 360 | |
| 361 | |
| 362 | def _ParseArguments(argv): |
| 363 | """Parse and validate arguments.""" |
| 364 | parser = GetParser() |
| 365 | opts = parser.parse_args(argv) |
| 366 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 367 | if not opts.protoc_version: |
| 368 | opts.protoc_version = [ProtocVersion.CHROMITE, ProtocVersion.SDK] |
| 369 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 370 | opts.Freeze() |
| 371 | return opts |
| 372 | |
| 373 | |
| 374 | def main(argv): |
Alex Klein | f985997 | 2019-03-14 17:11:42 -0600 | [diff] [blame] | 375 | opts = _ParseArguments(argv) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 376 | |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 377 | if opts.destination: |
| 378 | # Destination set, only compile a single version in the destination. |
| 379 | try: |
Alex Klein | 851f4ee | 2022-03-29 16:03:45 -0600 | [diff] [blame] | 380 | CompileProto( |
| 381 | output=opts.destination, |
| 382 | protoc_version=opts.dest_protoc, |
Alex Klein | 5b1bca3 | 2022-04-05 10:56:35 -0600 | [diff] [blame] | 383 | dir_subset=opts.dir_subset, |
| 384 | postprocess=opts.postprocess |
| 385 | ) |
Alex Klein | 098f798 | 2021-03-01 13:15:29 -0700 | [diff] [blame] | 386 | except Error as e: |
| 387 | cros_build_lib.Die('Error compiling bindings to destination: %s', str(e)) |
| 388 | else: |
| 389 | return 0 |
| 390 | |
| 391 | if ProtocVersion.CHROMITE in opts.protoc_version: |
| 392 | # Compile the chromite bindings. |
| 393 | try: |
| 394 | CompileProto( |
| 395 | output=_get_gen_dir(ProtocVersion.CHROMITE), |
| 396 | protoc_version=ProtocVersion.CHROMITE) |
| 397 | except Error as e: |
| 398 | cros_build_lib.Die('Error compiling chromite bindings: %s', str(e)) |
| 399 | |
| 400 | if ProtocVersion.SDK in opts.protoc_version: |
| 401 | # Compile the SDK bindings. |
| 402 | if not cros_build_lib.IsInsideChroot(): |
| 403 | # Rerun inside of the SDK instead of trying to map all of the paths. |
| 404 | cmd = [ |
| 405 | os.path.join(constants.CHROOT_SOURCE_ROOT, 'chromite', 'api', |
| 406 | 'compile_build_api_proto'), |
| 407 | '--sdk', |
| 408 | ] |
| 409 | result = cros_build_lib.run( |
| 410 | cmd, print_cmd=False, enter_chroot=True, check=False) |
| 411 | return result.returncode |
| 412 | else: |
| 413 | try: |
| 414 | CompileProto( |
| 415 | output=_get_gen_dir(ProtocVersion.SDK), |
| 416 | protoc_version=ProtocVersion.SDK) |
| 417 | except Error as e: |
| 418 | cros_build_lib.Die('Error compiling SDK bindings: %s', str(e)) |