blob: 80c90f68587d8cb90e055bfff601e1757fd39675 [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'
Mohannad Farrag7f29d832022-11-23 19:52:41 +000032LINKER_UNIT_TYPES = ('executable', 'shared_library', 'static_library', 'source_set')
Patrick Rohr92d74122022-10-21 15:50:52 -070033
34# TODO(primiano): investigate these, they require further componentization.
35ODR_VIOLATION_IGNORE_TARGETS = {
36 '//test/cts:perfetto_cts_deps',
37 '//:perfetto_integrationtests',
38}
Mohannad Farrag4bea14a2022-11-22 15:38:30 +000039ARCH_REGEX = r'(android_x86_64|android_x86|android_arm|android_arm64|host)'
40DEX_REGEX = '.*__dex__%s$' % ARCH_REGEX
41COMPILE_JAVA_REGEX = '.*__compile_java__%s$' % ARCH_REGEX
Patrick Rohr92d74122022-10-21 15:50:52 -070042def repo_root():
43 """Returns an absolute path to the repository root."""
44 return os.path.join(
45 os.path.realpath(os.path.dirname(__file__)), os.path.pardir)
46
47
Patrick Rohr92d74122022-10-21 15:50:52 -070048def label_to_path(label):
49 """Turn a GN output label (e.g., //some_dir/file.cc) into a path."""
50 assert label.startswith('//')
Patrick Rohrc6331c82022-10-25 11:34:20 -070051 return label[2:] or "./"
Patrick Rohr92d74122022-10-21 15:50:52 -070052
53
54def label_without_toolchain(label):
55 """Strips the toolchain from a GN label.
56
57 Return a GN label (e.g //buildtools:protobuf(//gn/standalone/toolchain:
58 gcc_like_host) without the parenthesised toolchain part.
59 """
60 return label.split('(')[0]
61
62
63def label_to_target_name_with_path(label):
64 """
65 Turn a GN label into a target name involving the full path.
66 e.g., //src/perfetto:tests -> src_perfetto_tests
67 """
68 name = re.sub(r'^//:?', '', label)
69 name = re.sub(r'[^a-zA-Z0-9_]', '_', name)
70 return name
71
Mohannad Farraga0db68b2022-11-24 12:28:09 +000072def _is_java_source(src):
73 return os.path.splitext(src)[1] == '.java' and not src.startswith("//out/test/gen/")
Patrick Rohr92d74122022-10-21 15:50:52 -070074
Patrick Rohr92d74122022-10-21 15:50:52 -070075class GnParser(object):
76 """A parser with some cleverness for GN json desc files
77
78 The main goals of this parser are:
79 1) Deal with the fact that other build systems don't have an equivalent
80 notion to GN's source_set. Conversely to Bazel's and Soong's filegroups,
81 GN source_sets expect that dependencies, cflags and other source_set
82 properties propagate up to the linker unit (static_library, executable or
83 shared_library). This parser simulates the same behavior: when a
84 source_set is encountered, some of its variables (cflags and such) are
85 copied up to the dependent targets. This is to allow gen_xxx to create
86 one filegroup for each source_set and then squash all the other flags
87 onto the linker unit.
88 2) Detect and special-case protobuf targets, figuring out the protoc-plugin
89 being used.
90 """
91
92 class Target(object):
93 """Reperesents A GN target.
94
95 Maked properties are propagated up the dependency chain when a
96 source_set dependency is encountered.
97 """
Patrick Rohr02ad51f2022-11-15 13:54:07 -080098 class Arch():
99 """Architecture-dependent properties
100 """
101 def __init__(self):
102 self.sources = set()
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900103 self.cflags = set()
Motomu Utsumi80e04472022-11-17 21:54:06 +0900104 self.defines = set()
Motomu Utsumi778f8302022-11-17 22:12:17 +0900105 self.include_dirs = set()
Patrick Rohr297f9792022-11-16 23:40:40 -0800106 self.deps = set()
107 self.transitive_static_libs_deps = set()
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000108 self.source_set_deps = set()
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800109
Patrick Rohr92d74122022-10-21 15:50:52 -0700110
111 def __init__(self, name, type):
112 self.name = name # e.g. //src/ipc:ipc
113
114 VALID_TYPES = ('static_library', 'shared_library', 'executable', 'group',
Patrick Rohrda778a02022-10-25 16:17:31 -0700115 'action', 'source_set', 'proto_library', 'copy', 'action_foreach')
Patrick Rohr92d74122022-10-21 15:50:52 -0700116 assert (type in VALID_TYPES)
117 self.type = type
118 self.testonly = False
119 self.toolchain = None
120
121 # These are valid only for type == proto_library.
122 # This is typically: 'proto', 'protozero', 'ipc'.
123 self.proto_plugin = None
124 self.proto_paths = set()
125 self.proto_exports = set()
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900126 self.proto_in_dir = ""
Patrick Rohr92d74122022-10-21 15:50:52 -0700127
128 self.sources = set()
129 # TODO(primiano): consider whether the public section should be part of
130 # bubbled-up sources.
131 self.public_headers = set() # 'public'
132
133 # These are valid only for type == 'action'
134 self.inputs = set()
135 self.outputs = set()
136 self.script = None
137 self.args = []
Patrick Rohr09716f52022-10-27 13:02:36 -0700138 self.response_file_contents = None
Patrick Rohr92d74122022-10-21 15:50:52 -0700139
140 # These variables are propagated up when encountering a dependency
141 # on a source_set target.
142 self.cflags = set()
143 self.defines = set()
144 self.deps = set()
145 self.libs = set()
146 self.include_dirs = set()
147 self.ldflags = set()
148 self.source_set_deps = set() # Transitive set of source_set deps.
149 self.proto_deps = set()
150 self.transitive_proto_deps = set()
Mohannad Farragbaf0d572022-11-22 11:53:54 +0000151 self.rtti = False
Patrick Rohr92d74122022-10-21 15:50:52 -0700152
Patrick Rohr70913562022-11-15 21:49:28 -0800153 # TODO: come up with a better way to only run this once.
154 # is_finalized tracks whether finalize() was called on this target.
155 self.is_finalized = False
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800156 self.arch = dict()
157
Motomu Utsumiee47af62022-11-30 16:41:15 +0900158 # This is used to get the name/version of libcronet
159 self.output_name = None
160
Patrick Rohrc8f41cd2022-11-15 22:46:10 -0800161 def host_supported(self):
162 return 'host' in self.arch
163
164 def device_supported(self):
165 return any([name.startswith('android') for name in self.arch.keys()])
166
Patrick Rohr92d74122022-10-21 15:50:52 -0700167 def __lt__(self, other):
168 if isinstance(other, self.__class__):
169 return self.name < other.name
170 raise TypeError(
171 '\'<\' not supported between instances of \'%s\' and \'%s\'' %
172 (type(self).__name__, type(other).__name__))
173
174 def __repr__(self):
175 return json.dumps({
176 k: (list(sorted(v)) if isinstance(v, set) else v)
Patrick Rohr23f26192022-10-25 09:45:22 -0700177 for (k, v) in self.__dict__.items()
Patrick Rohr92d74122022-10-21 15:50:52 -0700178 },
179 indent=4,
180 sort_keys=True)
181
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900182 def update(self, other, arch):
Patrick Rohr92d74122022-10-21 15:50:52 -0700183 for key in ('cflags', 'defines', 'deps', 'include_dirs', 'ldflags',
184 'source_set_deps', 'proto_deps', 'transitive_proto_deps',
185 'libs', 'proto_paths'):
186 self.__dict__[key].update(other.__dict__.get(key, []))
187
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000188 for key_in_arch in ('cflags', 'defines', 'include_dirs', 'source_set_deps'):
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900189 self.arch[arch].__dict__[key_in_arch].update(
190 other.arch[arch].__dict__.get(key_in_arch, []))
191
Patrick Rohr70913562022-11-15 21:49:28 -0800192 def finalize(self):
193 """Move common properties out of arch-dependent subobjects to Target object.
194
195 TODO: find a better name for this function.
196 """
197 if self.is_finalized:
198 return
199 self.is_finalized = True
200
Patrick Rohr70913562022-11-15 21:49:28 -0800201 # Target contains the intersection of arch-dependent properties
202 self.sources = set.intersection(*[arch.sources for arch in self.arch.values()])
Motomu Utsumif0f47682022-11-17 22:34:39 +0900203 self.cflags = set.intersection(*[arch.cflags for arch in self.arch.values()])
204 self.defines = set.intersection(*[arch.defines for arch in self.arch.values()])
205 self.include_dirs = set.intersection(*[arch.include_dirs for arch in self.arch.values()])
Patrick Rohr297f9792022-11-16 23:40:40 -0800206 self.deps.update(set.intersection(*[arch.deps for arch in self.arch.values()]))
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000207 self.source_set_deps.update(set.intersection(*[arch.source_set_deps for arch in self.arch.values()]))
Patrick Rohr70913562022-11-15 21:49:28 -0800208
209 # Deduplicate arch-dependent properties
210 for arch in self.arch.keys():
211 self.arch[arch].sources -= self.sources
Motomu Utsumif0f47682022-11-17 22:34:39 +0900212 self.arch[arch].cflags -= self.cflags
213 self.arch[arch].defines -= self.defines
214 self.arch[arch].include_dirs -= self.include_dirs
Patrick Rohr297f9792022-11-16 23:40:40 -0800215 self.arch[arch].deps -= self.deps
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000216 self.arch[arch].source_set_deps -= self.source_set_deps
Patrick Rohr297f9792022-11-16 23:40:40 -0800217
Patrick Rohr70913562022-11-15 21:49:28 -0800218
Patrick Rohr564d6be2022-11-15 12:57:57 -0800219 def __init__(self):
Patrick Rohr92d74122022-10-21 15:50:52 -0700220 self.all_targets = {}
221 self.linker_units = {} # Executables, shared or static libraries.
222 self.source_sets = {}
223 self.actions = {}
224 self.proto_libs = {}
Patrick Rohrb27587e2022-11-04 14:57:24 -0700225 self.java_sources = set()
Patrick Rohr92d74122022-10-21 15:50:52 -0700226
Patrick Rohr09716f52022-10-27 13:02:36 -0700227 def _get_response_file_contents(self, action_desc):
Patrick Rohrc20887d2022-10-28 12:59:20 -0700228 # response_file_contents are formatted as:
229 # ['--flags', '--flag=true && false'] and need to be formatted as:
230 # '--flags --flag=\"true && false\"'
231 flags = action_desc.get('response_file_contents', [])
232 formatted_flags = []
233 for flag in flags:
234 if '=' in flag:
235 key, val = flag.split('=')
236 formatted_flags.append('%s=\\"%s\\"' % (key, val))
237 else:
238 formatted_flags.append(flag)
239
240 return ' '.join(formatted_flags)
Patrick Rohr09716f52022-10-27 13:02:36 -0700241
Patrick Rohraf92fa62022-11-04 14:27:04 -0700242 def _is_java_target(self, target):
243 # Per https://chromium.googlesource.com/chromium/src/build/+/HEAD/android/docs/java_toolchain.md
244 # java target names must end in "_java".
245 # TODO: There are some other possible variations we might need to support.
Patrick Rohr67f53122022-11-09 10:57:40 -0800246 return target.type == 'group' and re.match('.*_java$', target.name)
Patrick Rohraf92fa62022-11-04 14:27:04 -0700247
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800248 def _get_arch(self, toolchain):
Patrick Rohrd938d532022-11-15 22:17:08 -0800249 if toolchain == '//build/toolchain/android:android_clang_x86':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800250 return 'android_x86'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800251 elif toolchain == '//build/toolchain/android:android_clang_x64':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800252 return 'android_x86_64'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800253 elif toolchain == '//build/toolchain/android:android_clang_arm':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800254 return 'android_arm'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800255 elif toolchain == '//build/toolchain/android:android_clang_arm64':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800256 return 'android_arm64'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800257 else:
258 return 'host'
259
Patrick Rohr92d74122022-10-21 15:50:52 -0700260 def get_target(self, gn_target_name):
261 """Returns a Target object from the fully qualified GN target name.
262
Patrick Rohrd0077b72022-11-15 12:43:26 -0800263 get_target() requires that parse_gn_desc() has already been called.
264 """
Patrick Rohr70913562022-11-15 21:49:28 -0800265 # Run this every time as parse_gn_desc can be called at any time.
266 for target in self.all_targets.values():
267 target.finalize()
268
Patrick Rohr7705bdb2022-11-15 13:26:30 -0800269 return self.all_targets[label_without_toolchain(gn_target_name)]
Patrick Rohrd0077b72022-11-15 12:43:26 -0800270
Patrick Rohr564d6be2022-11-15 12:57:57 -0800271 def parse_gn_desc(self, gn_desc, gn_target_name):
Patrick Rohrd0077b72022-11-15 12:43:26 -0800272 """Parses a gn desc tree and resolves all target dependencies.
273
Patrick Rohr92d74122022-10-21 15:50:52 -0700274 It bubbles up variables from source_set dependencies as described in the
275 class-level comments.
276 """
Patrick Rohr7705bdb2022-11-15 13:26:30 -0800277 # Use name without toolchain for targets to support targets built for
278 # multiple archs.
279 target_name = label_without_toolchain(gn_target_name)
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800280 desc = gn_desc[gn_target_name]
Patrick Rohr98600682022-11-18 18:29:15 -0800281 type_ = desc['type']
Patrick Rohrd938d532022-11-15 22:17:08 -0800282 arch = self._get_arch(desc['toolchain'])
Patrick Rohr98600682022-11-18 18:29:15 -0800283
284 # Action modules can differ depending on the target architecture, yet
285 # genrule's do not allow to overload cmd per target OS / arch. Create a
286 # separate action for every architecture.
Mohannad Farragd7efd7b92022-11-21 16:15:16 +0000287 # Cover both action and action_foreach
288 if type_.startswith('action'):
Patrick Rohr98600682022-11-18 18:29:15 -0800289 target_name += '__' + arch
290
291 target = self.all_targets.get(target_name)
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800292 if target is None:
Patrick Rohr98600682022-11-18 18:29:15 -0800293 target = GnParser.Target(target_name, type_)
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800294 self.all_targets[target_name] = target
295
296 if arch not in target.arch:
297 target.arch[arch] = GnParser.Target.Arch()
298 else:
Patrick Rohr92d74122022-10-21 15:50:52 -0700299 return target # Target already processed.
300
Patrick Rohr92d74122022-10-21 15:50:52 -0700301 target.testonly = desc.get('testonly', False)
Patrick Rohr92d74122022-10-21 15:50:52 -0700302
Patrick Rohr0d40da32022-11-15 13:08:12 -0800303 proto_target_type, proto_desc = self.get_proto_target_type(gn_desc, gn_target_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700304 if proto_target_type is not None:
305 self.proto_libs[target.name] = target
306 target.type = 'proto_library'
307 target.proto_plugin = proto_target_type
308 target.proto_paths.update(self.get_proto_paths(proto_desc))
309 target.proto_exports.update(self.get_proto_exports(proto_desc))
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900310 target.proto_in_dir = self.get_proto_in_dir(proto_desc)
Patrick Rohrad7a29c2022-11-16 21:48:09 -0800311 for gn_proto_deps_name in proto_desc.get('deps', []):
312 dep = self.parse_gn_desc(gn_desc, gn_proto_deps_name)
313 target.deps.add(dep.name)
Patrick Rohr53dcd102022-11-15 21:53:02 -0800314 target.arch[arch].sources.update(proto_desc.get('sources', []))
315 assert (all(x.endswith('.proto') for x in target.arch[arch].sources))
Patrick Rohr92d74122022-10-21 15:50:52 -0700316 elif target.type == 'source_set':
317 self.source_sets[gn_target_name] = target
Patrick Rohr53dcd102022-11-15 21:53:02 -0800318 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700319 elif target.type in LINKER_UNIT_TYPES:
320 self.linker_units[gn_target_name] = target
Patrick Rohr53dcd102022-11-15 21:53:02 -0800321 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohrda778a02022-10-25 16:17:31 -0700322 elif target.type in ['action', 'action_foreach']:
Patrick Rohr92d74122022-10-21 15:50:52 -0700323 self.actions[gn_target_name] = target
324 target.inputs.update(desc.get('inputs', []))
Patrick Rohr53dcd102022-11-15 21:53:02 -0800325 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700326 outs = [re.sub('^//out/.+?/gen/', '', x) for x in desc['outputs']]
327 target.outputs.update(outs)
328 target.script = desc['script']
Patrick Rohr7aa98f92022-10-28 11:16:36 -0700329 target.args = desc['args']
Patrick Rohr09716f52022-10-27 13:02:36 -0700330 target.response_file_contents = self._get_response_file_contents(desc)
Patrick Rohrda778a02022-10-25 16:17:31 -0700331 elif target.type == 'copy':
332 # TODO: copy rules are not currently implemented.
333 self.actions[gn_target_name] = target
Patrick Rohr67f53122022-11-09 10:57:40 -0800334 elif self._is_java_target(target):
Patrick Rohraf92fa62022-11-04 14:27:04 -0700335 # java_group identifies the group target generated by the android_library
336 # or java_library template. A java_group must not be added as a dependency, but sources are collected
337 log.debug('Found java target %s', target.name)
338 target.type = 'java_group'
Patrick Rohr92d74122022-10-21 15:50:52 -0700339
340 # Default for 'public' is //* - all headers in 'sources' are public.
341 # TODO(primiano): if a 'public' section is specified (even if empty), then
342 # the rest of 'sources' is considered inaccessible by gn. Consider
343 # emulating that, so that generated build files don't end up with overly
344 # accessible headers.
345 public_headers = [x for x in desc.get('public', []) if x != '*']
346 target.public_headers.update(public_headers)
347
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900348 target.arch[arch].cflags.update(desc.get('cflags', []) + desc.get('cflags_cc', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700349 target.libs.update(desc.get('libs', []))
350 target.ldflags.update(desc.get('ldflags', []))
Motomu Utsumi80e04472022-11-17 21:54:06 +0900351 target.arch[arch].defines.update(desc.get('defines', []))
Motomu Utsumi778f8302022-11-17 22:12:17 +0900352 target.arch[arch].include_dirs.update(desc.get('include_dirs', []))
Motomu Utsumiee47af62022-11-30 16:41:15 +0900353 target.output_name = desc.get('output_name', None)
Mohannad Farragbaf0d572022-11-22 11:53:54 +0000354 if "-frtti" in target.arch[arch].cflags:
355 target.rtti = True
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000356
Patrick Rohr92d74122022-10-21 15:50:52 -0700357 # Recurse in dependencies.
Patrick Rohr7f4631e2022-11-15 14:35:03 -0800358 for gn_dep_name in desc.get('deps', []):
359 dep = self.parse_gn_desc(gn_desc, gn_dep_name)
Patrick Rohrf1004372022-11-16 23:04:05 -0800360 if dep.type == 'proto_library':
Patrick Rohr9006b362022-11-16 21:49:53 -0800361 target.proto_deps.add(dep.name)
362 target.transitive_proto_deps.add(dep.name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700363 target.proto_paths.update(dep.proto_paths)
364 target.transitive_proto_deps.update(dep.transitive_proto_deps)
365 elif dep.type == 'source_set':
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000366 target.arch[arch].source_set_deps.add(dep.name)
367 target.arch[arch].source_set_deps.update(dep.arch[arch].source_set_deps)
Patrick Rohr92d74122022-10-21 15:50:52 -0700368 elif dep.type == 'group':
Motomu Utsumi1c64e442022-11-17 21:51:37 +0900369 target.update(dep, arch) # Bubble up groups's cflags/ldflags etc.
Patrick Rohrda778a02022-10-25 16:17:31 -0700370 elif dep.type in ['action', 'action_foreach', 'copy']:
Patrick Rohr92d74122022-10-21 15:50:52 -0700371 if proto_target_type is None:
Mohannad Farrag1de6cb12022-11-28 12:27:26 +0000372 target.arch[arch].deps.add(dep.name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700373 elif dep.type in LINKER_UNIT_TYPES:
Patrick Rohr297f9792022-11-16 23:40:40 -0800374 target.arch[arch].deps.add(dep.name)
Patrick Rohr3624f952022-11-04 14:30:18 -0700375 elif dep.type == 'java_group':
376 # Explicitly break dependency chain when a java_group is added.
377 # Java sources are collected and eventually compiled as one large
378 # java_library.
379 pass
Patrick Rohr92d74122022-10-21 15:50:52 -0700380
Mohannad Farrag7f29d832022-11-23 19:52:41 +0000381 # Source set bubble up transitive source sets but can't be combined with this
382 # if they are combined then source sets will bubble up static libraries
383 # while we only want to have source sets bubble up only source sets.
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800384 if dep.type == 'static_library':
385 # Bubble up static_libs. Necessary, since soong does not propagate
386 # static_libs up the build tree.
Patrick Rohr297f9792022-11-16 23:40:40 -0800387 target.arch[arch].transitive_static_libs_deps.add(dep.name)
Patrick Rohra9c1dda2022-11-14 19:02:40 -0800388
Patrick Rohr297f9792022-11-16 23:40:40 -0800389 if arch in dep.arch:
390 target.arch[arch].transitive_static_libs_deps.update(
391 dep.arch[arch].transitive_static_libs_deps)
392 target.arch[arch].deps.update(target.arch[arch].transitive_static_libs_deps)
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800393
Patrick Rohrb27587e2022-11-04 14:57:24 -0700394 # Collect java sources. Java sources are kept inside the __compile_java target.
395 # This target can be used for both host and target compilation; only add
396 # the sources if they are destined for the target (i.e. they are a
397 # dependency of the __dex target)
398 # Note: this skips prebuilt java dependencies. These will have to be
399 # added manually when building the jar.
Mohannad Farrag4bea14a2022-11-22 15:38:30 +0000400 if re.match(DEX_REGEX, target.name):
401 if re.match(COMPILE_JAVA_REGEX, dep.name):
Patrick Rohrb27587e2022-11-04 14:57:24 -0700402 log.debug('Adding java sources for %s', dep.name)
Mohannad Farraga0db68b2022-11-24 12:28:09 +0000403 java_srcs = [src for src in dep.inputs if _is_java_source(src)]
Patrick Rohrb27587e2022-11-04 14:57:24 -0700404 self.java_sources.update(java_srcs)
Patrick Rohr92d74122022-10-21 15:50:52 -0700405 return target
406
407 def get_proto_exports(self, proto_desc):
408 # exports in metadata will be available for source_set targets.
409 metadata = proto_desc.get('metadata', {})
410 return metadata.get('exports', [])
411
412 def get_proto_paths(self, proto_desc):
413 # import_dirs in metadata will be available for source_set targets.
414 metadata = proto_desc.get('metadata', {})
415 return metadata.get('import_dirs', [])
416
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900417
418 def get_proto_in_dir(self, proto_desc):
419 args = proto_desc.get('args')
420 return re.sub('^\.\./\.\./', '', args[args.index('--proto-in-dir') + 1])
421
Patrick Rohr0d40da32022-11-15 13:08:12 -0800422 def get_proto_target_type(self, gn_desc, gn_target_name):
Patrick Rohr92d74122022-10-21 15:50:52 -0700423 """ Checks if the target is a proto library and return the plugin.
424
425 Returns:
426 (None, None): if the target is not a proto library.
427 (plugin, proto_desc) where |plugin| is 'proto' in the default (lite)
428 case or 'protozero' or 'ipc' or 'descriptor'; |proto_desc| is the GN
429 json desc of the target with the .proto sources (_gen target for
430 non-descriptor types or the target itself for descriptor type).
431 """
Patrick Rohr0d40da32022-11-15 13:08:12 -0800432 parts = gn_target_name.split('(', 1)
Patrick Rohr92d74122022-10-21 15:50:52 -0700433 name = parts[0]
434 toolchain = '(' + parts[1] if len(parts) > 1 else ''
435
436 # Descriptor targets don't have a _gen target; instead we look for the
437 # characteristic flag in the args of the target itself.
Patrick Rohr0d40da32022-11-15 13:08:12 -0800438 desc = gn_desc.get(gn_target_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700439 if '--descriptor_set_out' in desc.get('args', []):
440 return 'descriptor', desc
441
442 # Source set proto targets have a non-empty proto_library_sources in the
443 # metadata of the description.
444 metadata = desc.get('metadata', {})
445 if 'proto_library_sources' in metadata:
446 return 'source_set', desc
447
448 # In all other cases, we want to look at the _gen target as that has the
449 # important information.
Patrick Rohr564d6be2022-11-15 12:57:57 -0800450 gen_desc = gn_desc.get('%s_gen%s' % (name, toolchain))
Patrick Rohr92d74122022-10-21 15:50:52 -0700451 if gen_desc is None or gen_desc['type'] != 'action':
452 return None, None
Patrick Rohrc5980782022-11-07 16:34:03 -0800453 if gen_desc['script'] != '//tools/protoc_wrapper/protoc_wrapper.py':
Patrick Rohr92d74122022-10-21 15:50:52 -0700454 return None, None
455 plugin = 'proto'
Patrick Rohrc5980782022-11-07 16:34:03 -0800456 args = gen_desc.get('args', [])
Patrick Rohr92d74122022-10-21 15:50:52 -0700457 for arg in (arg for arg in args if arg.startswith('--plugin=')):
458 # |arg| at this point looks like:
459 # --plugin=protoc-gen-plugin=gcc_like_host/protozero_plugin
460 # or
461 # --plugin=protoc-gen-plugin=protozero_plugin
462 plugin = arg.split('=')[-1].split('/')[-1].replace('_plugin', '')
463 return plugin, gen_desc