blob: a03989c87ed40b1d6dcb5e0c7cbbbf9b75136b26 [file] [log] [blame]
Patrick Rohr92d74122022-10-21 15:50:52 -07001# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# A collection of utilities for extracting build rule information from GN
16# projects.
17
18from __future__ import print_function
19import collections
20import errno
21import filecmp
22import json
Patrick Rohraf92fa62022-11-04 14:27:04 -070023import logging as log
Patrick Rohr92d74122022-10-21 15:50:52 -070024import os
25import re
26import shutil
27import subprocess
28import sys
Patrick Rohr92d74122022-10-21 15:50:52 -070029
30BUILDFLAGS_TARGET = '//gn:gen_buildflags'
31GEN_VERSION_TARGET = '//src/base:version_gen_h'
Patrick Rohr92d74122022-10-21 15:50:52 -070032LINKER_UNIT_TYPES = ('executable', 'shared_library', 'static_library')
33
34# TODO(primiano): investigate these, they require further componentization.
35ODR_VIOLATION_IGNORE_TARGETS = {
36 '//test/cts:perfetto_cts_deps',
37 '//:perfetto_integrationtests',
38}
39
40
Patrick Rohr92d74122022-10-21 15:50:52 -070041def repo_root():
42 """Returns an absolute path to the repository root."""
43 return os.path.join(
44 os.path.realpath(os.path.dirname(__file__)), os.path.pardir)
45
46
Patrick Rohr92d74122022-10-21 15:50:52 -070047def label_to_path(label):
48 """Turn a GN output label (e.g., //some_dir/file.cc) into a path."""
49 assert label.startswith('//')
Patrick Rohrc6331c82022-10-25 11:34:20 -070050 return label[2:] or "./"
Patrick Rohr92d74122022-10-21 15:50:52 -070051
52
53def label_without_toolchain(label):
54 """Strips the toolchain from a GN label.
55
56 Return a GN label (e.g //buildtools:protobuf(//gn/standalone/toolchain:
57 gcc_like_host) without the parenthesised toolchain part.
58 """
59 return label.split('(')[0]
60
61
62def label_to_target_name_with_path(label):
63 """
64 Turn a GN label into a target name involving the full path.
65 e.g., //src/perfetto:tests -> src_perfetto_tests
66 """
67 name = re.sub(r'^//:?', '', label)
68 name = re.sub(r'[^a-zA-Z0-9_]', '_', name)
69 return name
70
71
Patrick Rohr92d74122022-10-21 15:50:52 -070072class GnParser(object):
73 """A parser with some cleverness for GN json desc files
74
75 The main goals of this parser are:
76 1) Deal with the fact that other build systems don't have an equivalent
77 notion to GN's source_set. Conversely to Bazel's and Soong's filegroups,
78 GN source_sets expect that dependencies, cflags and other source_set
79 properties propagate up to the linker unit (static_library, executable or
80 shared_library). This parser simulates the same behavior: when a
81 source_set is encountered, some of its variables (cflags and such) are
82 copied up to the dependent targets. This is to allow gen_xxx to create
83 one filegroup for each source_set and then squash all the other flags
84 onto the linker unit.
85 2) Detect and special-case protobuf targets, figuring out the protoc-plugin
86 being used.
87 """
88
89 class Target(object):
90 """Reperesents A GN target.
91
92 Maked properties are propagated up the dependency chain when a
93 source_set dependency is encountered.
94 """
Patrick Rohr02ad51f2022-11-15 13:54:07 -080095 class Arch():
96 """Architecture-dependent properties
97 """
98 def __init__(self):
99 self.sources = set()
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900100 self.cflags = set()
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800101
Patrick Rohr92d74122022-10-21 15:50:52 -0700102
103 def __init__(self, name, type):
104 self.name = name # e.g. //src/ipc:ipc
105
106 VALID_TYPES = ('static_library', 'shared_library', 'executable', 'group',
Patrick Rohrda778a02022-10-25 16:17:31 -0700107 'action', 'source_set', 'proto_library', 'copy', 'action_foreach')
Patrick Rohr92d74122022-10-21 15:50:52 -0700108 assert (type in VALID_TYPES)
109 self.type = type
110 self.testonly = False
111 self.toolchain = None
112
113 # These are valid only for type == proto_library.
114 # This is typically: 'proto', 'protozero', 'ipc'.
115 self.proto_plugin = None
116 self.proto_paths = set()
117 self.proto_exports = set()
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900118 self.proto_in_dir = ""
Patrick Rohr92d74122022-10-21 15:50:52 -0700119
120 self.sources = set()
121 # TODO(primiano): consider whether the public section should be part of
122 # bubbled-up sources.
123 self.public_headers = set() # 'public'
124
125 # These are valid only for type == 'action'
126 self.inputs = set()
127 self.outputs = set()
128 self.script = None
129 self.args = []
Patrick Rohr09716f52022-10-27 13:02:36 -0700130 self.response_file_contents = None
Patrick Rohr92d74122022-10-21 15:50:52 -0700131
132 # These variables are propagated up when encountering a dependency
133 # on a source_set target.
134 self.cflags = set()
135 self.defines = set()
136 self.deps = set()
137 self.libs = set()
138 self.include_dirs = set()
139 self.ldflags = set()
140 self.source_set_deps = set() # Transitive set of source_set deps.
141 self.proto_deps = set()
142 self.transitive_proto_deps = set()
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800143 self.transitive_static_libs_deps = set()
Patrick Rohr92d74122022-10-21 15:50:52 -0700144
145 # Deps on //gn:xxx have this flag set to True. These dependencies
146 # are special because they pull third_party code from buildtools/.
147 # We don't want to keep recursing into //buildtools in generators,
148 # this flag is used to stop the recursion and create an empty
149 # placeholder target once we hit //gn:protoc or similar.
150 self.is_third_party_dep_ = False
151
Patrick Rohr70913562022-11-15 21:49:28 -0800152 # TODO: come up with a better way to only run this once.
153 # is_finalized tracks whether finalize() was called on this target.
154 self.is_finalized = False
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800155 self.arch = dict()
156
Patrick Rohrc8f41cd2022-11-15 22:46:10 -0800157 def host_supported(self):
158 return 'host' in self.arch
159
160 def device_supported(self):
161 return any([name.startswith('android') for name in self.arch.keys()])
162
Patrick Rohr92d74122022-10-21 15:50:52 -0700163 def __lt__(self, other):
164 if isinstance(other, self.__class__):
165 return self.name < other.name
166 raise TypeError(
167 '\'<\' not supported between instances of \'%s\' and \'%s\'' %
168 (type(self).__name__, type(other).__name__))
169
170 def __repr__(self):
171 return json.dumps({
172 k: (list(sorted(v)) if isinstance(v, set) else v)
Patrick Rohr23f26192022-10-25 09:45:22 -0700173 for (k, v) in self.__dict__.items()
Patrick Rohr92d74122022-10-21 15:50:52 -0700174 },
175 indent=4,
176 sort_keys=True)
177
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900178 def update(self, other, arch):
Patrick Rohr92d74122022-10-21 15:50:52 -0700179 for key in ('cflags', 'defines', 'deps', 'include_dirs', 'ldflags',
180 'source_set_deps', 'proto_deps', 'transitive_proto_deps',
181 'libs', 'proto_paths'):
182 self.__dict__[key].update(other.__dict__.get(key, []))
183
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900184 for key_in_arch in ('cflags',):
185 self.arch[arch].__dict__[key_in_arch].update(
186 other.arch[arch].__dict__.get(key_in_arch, []))
187
Patrick Rohr70913562022-11-15 21:49:28 -0800188 def finalize(self):
189 """Move common properties out of arch-dependent subobjects to Target object.
190
191 TODO: find a better name for this function.
192 """
193 if self.is_finalized:
194 return
195 self.is_finalized = True
196
Patrick Rohr70913562022-11-15 21:49:28 -0800197 # Target contains the intersection of arch-dependent properties
198 self.sources = set.intersection(*[arch.sources for arch in self.arch.values()])
199
200 # Deduplicate arch-dependent properties
201 for arch in self.arch.keys():
202 self.arch[arch].sources -= self.sources
203
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900204 # TODO: Keep cflags per arch
205 for arch_value in self.arch.values():
206 self.cflags = self.cflags.union(arch_value.cflags)
207
Patrick Rohr70913562022-11-15 21:49:28 -0800208
Patrick Rohr564d6be2022-11-15 12:57:57 -0800209 def __init__(self):
Patrick Rohr92d74122022-10-21 15:50:52 -0700210 self.all_targets = {}
211 self.linker_units = {} # Executables, shared or static libraries.
212 self.source_sets = {}
213 self.actions = {}
214 self.proto_libs = {}
Patrick Rohrb27587e2022-11-04 14:57:24 -0700215 self.java_sources = set()
Patrick Rohr92d74122022-10-21 15:50:52 -0700216
Patrick Rohr09716f52022-10-27 13:02:36 -0700217 def _get_response_file_contents(self, action_desc):
Patrick Rohrc20887d2022-10-28 12:59:20 -0700218 # response_file_contents are formatted as:
219 # ['--flags', '--flag=true && false'] and need to be formatted as:
220 # '--flags --flag=\"true && false\"'
221 flags = action_desc.get('response_file_contents', [])
222 formatted_flags = []
223 for flag in flags:
224 if '=' in flag:
225 key, val = flag.split('=')
226 formatted_flags.append('%s=\\"%s\\"' % (key, val))
227 else:
228 formatted_flags.append(flag)
229
230 return ' '.join(formatted_flags)
Patrick Rohr09716f52022-10-27 13:02:36 -0700231
Patrick Rohraf92fa62022-11-04 14:27:04 -0700232 def _is_java_target(self, target):
233 # Per https://chromium.googlesource.com/chromium/src/build/+/HEAD/android/docs/java_toolchain.md
234 # java target names must end in "_java".
235 # TODO: There are some other possible variations we might need to support.
Patrick Rohr67f53122022-11-09 10:57:40 -0800236 return target.type == 'group' and re.match('.*_java$', target.name)
Patrick Rohraf92fa62022-11-04 14:27:04 -0700237
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800238 def _get_arch(self, toolchain):
Patrick Rohrd938d532022-11-15 22:17:08 -0800239 if toolchain == '//build/toolchain/android:android_clang_x86':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800240 return 'android_x86'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800241 elif toolchain == '//build/toolchain/android:android_clang_x64':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800242 return 'android_x86_64'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800243 elif toolchain == '//build/toolchain/android:android_clang_arm':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800244 return 'android_arm'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800245 elif toolchain == '//build/toolchain/android:android_clang_arm64':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800246 return 'android_arm64'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800247 else:
248 return 'host'
249
Patrick Rohr92d74122022-10-21 15:50:52 -0700250 def get_target(self, gn_target_name):
251 """Returns a Target object from the fully qualified GN target name.
252
Patrick Rohrd0077b72022-11-15 12:43:26 -0800253 get_target() requires that parse_gn_desc() has already been called.
254 """
Patrick Rohr70913562022-11-15 21:49:28 -0800255 # Run this every time as parse_gn_desc can be called at any time.
256 for target in self.all_targets.values():
257 target.finalize()
258
Patrick Rohr7705bdb2022-11-15 13:26:30 -0800259 return self.all_targets[label_without_toolchain(gn_target_name)]
Patrick Rohrd0077b72022-11-15 12:43:26 -0800260
Patrick Rohr564d6be2022-11-15 12:57:57 -0800261 def parse_gn_desc(self, gn_desc, gn_target_name):
Patrick Rohrd0077b72022-11-15 12:43:26 -0800262 """Parses a gn desc tree and resolves all target dependencies.
263
Patrick Rohr92d74122022-10-21 15:50:52 -0700264 It bubbles up variables from source_set dependencies as described in the
265 class-level comments.
266 """
Patrick Rohr7705bdb2022-11-15 13:26:30 -0800267 # Use name without toolchain for targets to support targets built for
268 # multiple archs.
269 target_name = label_without_toolchain(gn_target_name)
270 target = self.all_targets.get(target_name)
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800271 desc = gn_desc[gn_target_name]
Patrick Rohrd938d532022-11-15 22:17:08 -0800272 arch = self._get_arch(desc['toolchain'])
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800273 if target is None:
274 target = GnParser.Target(target_name, desc['type'])
275 self.all_targets[target_name] = target
276
277 if arch not in target.arch:
278 target.arch[arch] = GnParser.Target.Arch()
279 else:
Patrick Rohr92d74122022-10-21 15:50:52 -0700280 return target # Target already processed.
281
Patrick Rohr92d74122022-10-21 15:50:52 -0700282 target.testonly = desc.get('testonly', False)
Patrick Rohr92d74122022-10-21 15:50:52 -0700283
Patrick Rohr0d40da32022-11-15 13:08:12 -0800284 proto_target_type, proto_desc = self.get_proto_target_type(gn_desc, gn_target_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700285 if proto_target_type is not None:
286 self.proto_libs[target.name] = target
287 target.type = 'proto_library'
288 target.proto_plugin = proto_target_type
289 target.proto_paths.update(self.get_proto_paths(proto_desc))
290 target.proto_exports.update(self.get_proto_exports(proto_desc))
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900291 target.proto_in_dir = self.get_proto_in_dir(proto_desc)
Patrick Rohrad7a29c2022-11-16 21:48:09 -0800292 for gn_proto_deps_name in proto_desc.get('deps', []):
293 dep = self.parse_gn_desc(gn_desc, gn_proto_deps_name)
294 target.deps.add(dep.name)
Patrick Rohr53dcd102022-11-15 21:53:02 -0800295 target.arch[arch].sources.update(proto_desc.get('sources', []))
296 assert (all(x.endswith('.proto') for x in target.arch[arch].sources))
Patrick Rohr92d74122022-10-21 15:50:52 -0700297 elif target.type == 'source_set':
298 self.source_sets[gn_target_name] = target
Patrick Rohr53dcd102022-11-15 21:53:02 -0800299 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700300 elif target.type in LINKER_UNIT_TYPES:
301 self.linker_units[gn_target_name] = target
Patrick Rohr53dcd102022-11-15 21:53:02 -0800302 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohrda778a02022-10-25 16:17:31 -0700303 elif target.type in ['action', 'action_foreach']:
Patrick Rohr92d74122022-10-21 15:50:52 -0700304 self.actions[gn_target_name] = target
305 target.inputs.update(desc.get('inputs', []))
Patrick Rohr53dcd102022-11-15 21:53:02 -0800306 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700307 outs = [re.sub('^//out/.+?/gen/', '', x) for x in desc['outputs']]
308 target.outputs.update(outs)
309 target.script = desc['script']
Patrick Rohr7aa98f92022-10-28 11:16:36 -0700310 target.args = desc['args']
Patrick Rohr09716f52022-10-27 13:02:36 -0700311 target.response_file_contents = self._get_response_file_contents(desc)
Patrick Rohrda778a02022-10-25 16:17:31 -0700312 elif target.type == 'copy':
313 # TODO: copy rules are not currently implemented.
314 self.actions[gn_target_name] = target
Patrick Rohr67f53122022-11-09 10:57:40 -0800315 elif self._is_java_target(target):
Patrick Rohraf92fa62022-11-04 14:27:04 -0700316 # java_group identifies the group target generated by the android_library
317 # or java_library template. A java_group must not be added as a dependency, but sources are collected
318 log.debug('Found java target %s', target.name)
319 target.type = 'java_group'
Patrick Rohr92d74122022-10-21 15:50:52 -0700320
321 # Default for 'public' is //* - all headers in 'sources' are public.
322 # TODO(primiano): if a 'public' section is specified (even if empty), then
323 # the rest of 'sources' is considered inaccessible by gn. Consider
324 # emulating that, so that generated build files don't end up with overly
325 # accessible headers.
326 public_headers = [x for x in desc.get('public', []) if x != '*']
327 target.public_headers.update(public_headers)
328
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900329 target.arch[arch].cflags.update(desc.get('cflags', []) + desc.get('cflags_cc', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700330 target.libs.update(desc.get('libs', []))
331 target.ldflags.update(desc.get('ldflags', []))
332 target.defines.update(desc.get('defines', []))
333 target.include_dirs.update(desc.get('include_dirs', []))
334
335 # Recurse in dependencies.
Patrick Rohr7f4631e2022-11-15 14:35:03 -0800336 for gn_dep_name in desc.get('deps', []):
337 dep = self.parse_gn_desc(gn_desc, gn_dep_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700338 if dep.is_third_party_dep_:
Patrick Rohr9006b362022-11-16 21:49:53 -0800339 target.deps.add(dep.name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700340 elif dep.type == 'proto_library':
Patrick Rohr9006b362022-11-16 21:49:53 -0800341 target.proto_deps.add(dep.name)
342 target.transitive_proto_deps.add(dep.name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700343 target.proto_paths.update(dep.proto_paths)
344 target.transitive_proto_deps.update(dep.transitive_proto_deps)
345 elif dep.type == 'source_set':
Patrick Rohr9006b362022-11-16 21:49:53 -0800346 target.source_set_deps.add(dep.name)
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900347 target.update(dep, arch) # Bubble up source set's cflags/ldflags etc.
Patrick Rohr92d74122022-10-21 15:50:52 -0700348 elif dep.type == 'group':
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900349 target.update(dep, arch) # Bubble up groups's cflags/ldflags etc.
Patrick Rohrda778a02022-10-25 16:17:31 -0700350 elif dep.type in ['action', 'action_foreach', 'copy']:
Patrick Rohr92d74122022-10-21 15:50:52 -0700351 if proto_target_type is None:
Patrick Rohr9006b362022-11-16 21:49:53 -0800352 target.deps.add(dep.name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700353 elif dep.type in LINKER_UNIT_TYPES:
Patrick Rohr9006b362022-11-16 21:49:53 -0800354 target.deps.add(dep.name)
Patrick Rohr3624f952022-11-04 14:30:18 -0700355 elif dep.type == 'java_group':
356 # Explicitly break dependency chain when a java_group is added.
357 # Java sources are collected and eventually compiled as one large
358 # java_library.
359 pass
Patrick Rohr92d74122022-10-21 15:50:52 -0700360
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800361 if dep.type == 'static_library':
362 # Bubble up static_libs. Necessary, since soong does not propagate
363 # static_libs up the build tree.
Patrick Rohr9006b362022-11-16 21:49:53 -0800364 target.transitive_static_libs_deps.add(dep.name)
Patrick Rohra9c1dda2022-11-14 19:02:40 -0800365
366 target.transitive_static_libs_deps.update(dep.transitive_static_libs_deps)
367 target.deps.update(target.transitive_static_libs_deps)
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800368
Patrick Rohrb27587e2022-11-04 14:57:24 -0700369 # Collect java sources. Java sources are kept inside the __compile_java target.
370 # This target can be used for both host and target compilation; only add
371 # the sources if they are destined for the target (i.e. they are a
372 # dependency of the __dex target)
373 # Note: this skips prebuilt java dependencies. These will have to be
374 # added manually when building the jar.
375 if re.match('.*__dex$', target.name):
376 if re.match('.*__compile_java$', dep.name):
377 log.debug('Adding java sources for %s', dep.name)
378 java_srcs = [src for src in dep.inputs if os.path.splitext(src)[1] == '.java']
379 self.java_sources.update(java_srcs)
380
Patrick Rohr92d74122022-10-21 15:50:52 -0700381 return target
382
383 def get_proto_exports(self, proto_desc):
384 # exports in metadata will be available for source_set targets.
385 metadata = proto_desc.get('metadata', {})
386 return metadata.get('exports', [])
387
388 def get_proto_paths(self, proto_desc):
389 # import_dirs in metadata will be available for source_set targets.
390 metadata = proto_desc.get('metadata', {})
391 return metadata.get('import_dirs', [])
392
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900393
394 def get_proto_in_dir(self, proto_desc):
395 args = proto_desc.get('args')
396 return re.sub('^\.\./\.\./', '', args[args.index('--proto-in-dir') + 1])
397
Patrick Rohr0d40da32022-11-15 13:08:12 -0800398 def get_proto_target_type(self, gn_desc, gn_target_name):
Patrick Rohr92d74122022-10-21 15:50:52 -0700399 """ Checks if the target is a proto library and return the plugin.
400
401 Returns:
402 (None, None): if the target is not a proto library.
403 (plugin, proto_desc) where |plugin| is 'proto' in the default (lite)
404 case or 'protozero' or 'ipc' or 'descriptor'; |proto_desc| is the GN
405 json desc of the target with the .proto sources (_gen target for
406 non-descriptor types or the target itself for descriptor type).
407 """
Patrick Rohr0d40da32022-11-15 13:08:12 -0800408 parts = gn_target_name.split('(', 1)
Patrick Rohr92d74122022-10-21 15:50:52 -0700409 name = parts[0]
410 toolchain = '(' + parts[1] if len(parts) > 1 else ''
411
412 # Descriptor targets don't have a _gen target; instead we look for the
413 # characteristic flag in the args of the target itself.
Patrick Rohr0d40da32022-11-15 13:08:12 -0800414 desc = gn_desc.get(gn_target_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700415 if '--descriptor_set_out' in desc.get('args', []):
416 return 'descriptor', desc
417
418 # Source set proto targets have a non-empty proto_library_sources in the
419 # metadata of the description.
420 metadata = desc.get('metadata', {})
421 if 'proto_library_sources' in metadata:
422 return 'source_set', desc
423
424 # In all other cases, we want to look at the _gen target as that has the
425 # important information.
Patrick Rohr564d6be2022-11-15 12:57:57 -0800426 gen_desc = gn_desc.get('%s_gen%s' % (name, toolchain))
Patrick Rohr92d74122022-10-21 15:50:52 -0700427 if gen_desc is None or gen_desc['type'] != 'action':
428 return None, None
Patrick Rohrc5980782022-11-07 16:34:03 -0800429 if gen_desc['script'] != '//tools/protoc_wrapper/protoc_wrapper.py':
Patrick Rohr92d74122022-10-21 15:50:52 -0700430 return None, None
431 plugin = 'proto'
Patrick Rohrc5980782022-11-07 16:34:03 -0800432 args = gen_desc.get('args', [])
Patrick Rohr92d74122022-10-21 15:50:52 -0700433 for arg in (arg for arg in args if arg.startswith('--plugin=')):
434 # |arg| at this point looks like:
435 # --plugin=protoc-gen-plugin=gcc_like_host/protozero_plugin
436 # or
437 # --plugin=protoc-gen-plugin=protozero_plugin
438 plugin = arg.split('=')[-1].split('/')[-1].replace('_plugin', '')
439 return plugin, gen_desc