blob: f051421d129ee94c84f274de108041c31cd470b6 [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'
32TARGET_TOOLCHAIN = '//gn/standalone/toolchain:gcc_like_host'
33HOST_TOOLCHAIN = '//gn/standalone/toolchain:gcc_like_host'
34LINKER_UNIT_TYPES = ('executable', 'shared_library', 'static_library')
35
36# TODO(primiano): investigate these, they require further componentization.
37ODR_VIOLATION_IGNORE_TARGETS = {
38 '//test/cts:perfetto_cts_deps',
39 '//:perfetto_integrationtests',
40}
41
42
Patrick Rohr92d74122022-10-21 15:50:52 -070043def repo_root():
44 """Returns an absolute path to the repository root."""
45 return os.path.join(
46 os.path.realpath(os.path.dirname(__file__)), os.path.pardir)
47
48
Patrick Rohr92d74122022-10-21 15:50:52 -070049def label_to_path(label):
50 """Turn a GN output label (e.g., //some_dir/file.cc) into a path."""
51 assert label.startswith('//')
Patrick Rohrc6331c82022-10-25 11:34:20 -070052 return label[2:] or "./"
Patrick Rohr92d74122022-10-21 15:50:52 -070053
54
55def label_without_toolchain(label):
56 """Strips the toolchain from a GN label.
57
58 Return a GN label (e.g //buildtools:protobuf(//gn/standalone/toolchain:
59 gcc_like_host) without the parenthesised toolchain part.
60 """
61 return label.split('(')[0]
62
63
64def label_to_target_name_with_path(label):
65 """
66 Turn a GN label into a target name involving the full path.
67 e.g., //src/perfetto:tests -> src_perfetto_tests
68 """
69 name = re.sub(r'^//:?', '', label)
70 name = re.sub(r'[^a-zA-Z0-9_]', '_', name)
71 return name
72
73
Patrick Rohr92d74122022-10-21 15:50:52 -070074class GnParser(object):
75 """A parser with some cleverness for GN json desc files
76
77 The main goals of this parser are:
78 1) Deal with the fact that other build systems don't have an equivalent
79 notion to GN's source_set. Conversely to Bazel's and Soong's filegroups,
80 GN source_sets expect that dependencies, cflags and other source_set
81 properties propagate up to the linker unit (static_library, executable or
82 shared_library). This parser simulates the same behavior: when a
83 source_set is encountered, some of its variables (cflags and such) are
84 copied up to the dependent targets. This is to allow gen_xxx to create
85 one filegroup for each source_set and then squash all the other flags
86 onto the linker unit.
87 2) Detect and special-case protobuf targets, figuring out the protoc-plugin
88 being used.
89 """
90
91 class Target(object):
92 """Reperesents A GN target.
93
94 Maked properties are propagated up the dependency chain when a
95 source_set dependency is encountered.
96 """
Patrick Rohr02ad51f2022-11-15 13:54:07 -080097 class Arch():
98 """Architecture-dependent properties
99 """
100 def __init__(self):
101 self.sources = set()
102
Patrick Rohr92d74122022-10-21 15:50:52 -0700103
104 def __init__(self, name, type):
105 self.name = name # e.g. //src/ipc:ipc
106
107 VALID_TYPES = ('static_library', 'shared_library', 'executable', 'group',
Patrick Rohrda778a02022-10-25 16:17:31 -0700108 'action', 'source_set', 'proto_library', 'copy', 'action_foreach')
Patrick Rohr92d74122022-10-21 15:50:52 -0700109 assert (type in VALID_TYPES)
110 self.type = type
111 self.testonly = False
112 self.toolchain = None
113
114 # These are valid only for type == proto_library.
115 # This is typically: 'proto', 'protozero', 'ipc'.
116 self.proto_plugin = None
117 self.proto_paths = set()
118 self.proto_exports = set()
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900119 self.proto_in_dir = ""
Patrick Rohr92d74122022-10-21 15:50:52 -0700120
121 self.sources = set()
122 # TODO(primiano): consider whether the public section should be part of
123 # bubbled-up sources.
124 self.public_headers = set() # 'public'
125
126 # These are valid only for type == 'action'
127 self.inputs = set()
128 self.outputs = set()
129 self.script = None
130 self.args = []
Patrick Rohr09716f52022-10-27 13:02:36 -0700131 self.response_file_contents = None
Patrick Rohr92d74122022-10-21 15:50:52 -0700132
133 # These variables are propagated up when encountering a dependency
134 # on a source_set target.
135 self.cflags = set()
136 self.defines = set()
137 self.deps = set()
138 self.libs = set()
139 self.include_dirs = set()
140 self.ldflags = set()
141 self.source_set_deps = set() # Transitive set of source_set deps.
142 self.proto_deps = set()
143 self.transitive_proto_deps = set()
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800144 self.transitive_static_libs_deps = set()
Patrick Rohr92d74122022-10-21 15:50:52 -0700145
146 # Deps on //gn:xxx have this flag set to True. These dependencies
147 # are special because they pull third_party code from buildtools/.
148 # We don't want to keep recursing into //buildtools in generators,
149 # this flag is used to stop the recursion and create an empty
150 # placeholder target once we hit //gn:protoc or similar.
151 self.is_third_party_dep_ = False
152
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
Patrick Rohr92d74122022-10-21 15:50:52 -0700158 def __lt__(self, other):
159 if isinstance(other, self.__class__):
160 return self.name < other.name
161 raise TypeError(
162 '\'<\' not supported between instances of \'%s\' and \'%s\'' %
163 (type(self).__name__, type(other).__name__))
164
165 def __repr__(self):
166 return json.dumps({
167 k: (list(sorted(v)) if isinstance(v, set) else v)
Patrick Rohr23f26192022-10-25 09:45:22 -0700168 for (k, v) in self.__dict__.items()
Patrick Rohr92d74122022-10-21 15:50:52 -0700169 },
170 indent=4,
171 sort_keys=True)
172
173 def update(self, other):
174 for key in ('cflags', 'defines', 'deps', 'include_dirs', 'ldflags',
175 'source_set_deps', 'proto_deps', 'transitive_proto_deps',
176 'libs', 'proto_paths'):
177 self.__dict__[key].update(other.__dict__.get(key, []))
178
Patrick Rohr70913562022-11-15 21:49:28 -0800179 def finalize(self):
180 """Move common properties out of arch-dependent subobjects to Target object.
181
182 TODO: find a better name for this function.
183 """
184 if self.is_finalized:
185 return
186 self.is_finalized = True
187
Patrick Rohr70913562022-11-15 21:49:28 -0800188 # Target contains the intersection of arch-dependent properties
189 self.sources = set.intersection(*[arch.sources for arch in self.arch.values()])
190
191 # Deduplicate arch-dependent properties
192 for arch in self.arch.keys():
193 self.arch[arch].sources -= self.sources
194
195
Patrick Rohr564d6be2022-11-15 12:57:57 -0800196 def __init__(self):
Patrick Rohr92d74122022-10-21 15:50:52 -0700197 self.all_targets = {}
198 self.linker_units = {} # Executables, shared or static libraries.
199 self.source_sets = {}
200 self.actions = {}
201 self.proto_libs = {}
Patrick Rohrb27587e2022-11-04 14:57:24 -0700202 self.java_sources = set()
Patrick Rohr92d74122022-10-21 15:50:52 -0700203
Patrick Rohr09716f52022-10-27 13:02:36 -0700204 def _get_response_file_contents(self, action_desc):
Patrick Rohrc20887d2022-10-28 12:59:20 -0700205 # response_file_contents are formatted as:
206 # ['--flags', '--flag=true && false'] and need to be formatted as:
207 # '--flags --flag=\"true && false\"'
208 flags = action_desc.get('response_file_contents', [])
209 formatted_flags = []
210 for flag in flags:
211 if '=' in flag:
212 key, val = flag.split('=')
213 formatted_flags.append('%s=\\"%s\\"' % (key, val))
214 else:
215 formatted_flags.append(flag)
216
217 return ' '.join(formatted_flags)
Patrick Rohr09716f52022-10-27 13:02:36 -0700218
Patrick Rohraf92fa62022-11-04 14:27:04 -0700219 def _is_java_target(self, target):
220 # Per https://chromium.googlesource.com/chromium/src/build/+/HEAD/android/docs/java_toolchain.md
221 # java target names must end in "_java".
222 # TODO: There are some other possible variations we might need to support.
Patrick Rohr67f53122022-11-09 10:57:40 -0800223 return target.type == 'group' and re.match('.*_java$', target.name)
Patrick Rohraf92fa62022-11-04 14:27:04 -0700224
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800225 def _get_arch(self, toolchain):
Patrick Rohrd938d532022-11-15 22:17:08 -0800226 if toolchain == '//build/toolchain/android:android_clang_x86':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800227 return 'android_x86'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800228 elif toolchain == '//build/toolchain/android:android_clang_x64':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800229 return 'android_x86_64'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800230 elif toolchain == '//build/toolchain/android:android_clang_arm':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800231 return 'android_arm'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800232 elif toolchain == '//build/toolchain/android:android_clang_arm64':
Patrick Rohr4eff2102022-11-15 22:21:52 -0800233 return 'android_arm64'
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800234 else:
235 return 'host'
236
Patrick Rohr92d74122022-10-21 15:50:52 -0700237 def get_target(self, gn_target_name):
238 """Returns a Target object from the fully qualified GN target name.
239
Patrick Rohrd0077b72022-11-15 12:43:26 -0800240 get_target() requires that parse_gn_desc() has already been called.
241 """
Patrick Rohr70913562022-11-15 21:49:28 -0800242 # Run this every time as parse_gn_desc can be called at any time.
243 for target in self.all_targets.values():
244 target.finalize()
245
Patrick Rohr7705bdb2022-11-15 13:26:30 -0800246 return self.all_targets[label_without_toolchain(gn_target_name)]
Patrick Rohrd0077b72022-11-15 12:43:26 -0800247
Patrick Rohr564d6be2022-11-15 12:57:57 -0800248 def parse_gn_desc(self, gn_desc, gn_target_name):
Patrick Rohrd0077b72022-11-15 12:43:26 -0800249 """Parses a gn desc tree and resolves all target dependencies.
250
Patrick Rohr92d74122022-10-21 15:50:52 -0700251 It bubbles up variables from source_set dependencies as described in the
252 class-level comments.
253 """
Patrick Rohr7705bdb2022-11-15 13:26:30 -0800254 # Use name without toolchain for targets to support targets built for
255 # multiple archs.
256 target_name = label_without_toolchain(gn_target_name)
257 target = self.all_targets.get(target_name)
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800258 desc = gn_desc[gn_target_name]
Patrick Rohrd938d532022-11-15 22:17:08 -0800259 arch = self._get_arch(desc['toolchain'])
Patrick Rohr02ad51f2022-11-15 13:54:07 -0800260 if target is None:
261 target = GnParser.Target(target_name, desc['type'])
262 self.all_targets[target_name] = target
263
264 if arch not in target.arch:
265 target.arch[arch] = GnParser.Target.Arch()
266 else:
Patrick Rohr92d74122022-10-21 15:50:52 -0700267 return target # Target already processed.
268
Patrick Rohr92d74122022-10-21 15:50:52 -0700269 target.testonly = desc.get('testonly', False)
Patrick Rohr81a4ac32022-11-15 14:38:21 -0800270 # TODO: remove toolchain from Target object
Patrick Rohr92d74122022-10-21 15:50:52 -0700271 target.toolchain = desc.get('toolchain', None)
Patrick Rohr92d74122022-10-21 15:50:52 -0700272
Patrick Rohr0d40da32022-11-15 13:08:12 -0800273 proto_target_type, proto_desc = self.get_proto_target_type(gn_desc, gn_target_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700274 if proto_target_type is not None:
275 self.proto_libs[target.name] = target
276 target.type = 'proto_library'
277 target.proto_plugin = proto_target_type
278 target.proto_paths.update(self.get_proto_paths(proto_desc))
279 target.proto_exports.update(self.get_proto_exports(proto_desc))
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900280 target.proto_in_dir = self.get_proto_in_dir(proto_desc)
Patrick Rohr53dcd102022-11-15 21:53:02 -0800281 target.arch[arch].sources.update(proto_desc.get('sources', []))
282 assert (all(x.endswith('.proto') for x in target.arch[arch].sources))
Patrick Rohr92d74122022-10-21 15:50:52 -0700283 elif target.type == 'source_set':
284 self.source_sets[gn_target_name] = target
Patrick Rohr53dcd102022-11-15 21:53:02 -0800285 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700286 elif target.type in LINKER_UNIT_TYPES:
287 self.linker_units[gn_target_name] = target
Patrick Rohr53dcd102022-11-15 21:53:02 -0800288 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohrda778a02022-10-25 16:17:31 -0700289 elif target.type in ['action', 'action_foreach']:
Patrick Rohr92d74122022-10-21 15:50:52 -0700290 self.actions[gn_target_name] = target
291 target.inputs.update(desc.get('inputs', []))
Patrick Rohr53dcd102022-11-15 21:53:02 -0800292 target.arch[arch].sources.update(desc.get('sources', []))
Patrick Rohr92d74122022-10-21 15:50:52 -0700293 outs = [re.sub('^//out/.+?/gen/', '', x) for x in desc['outputs']]
294 target.outputs.update(outs)
295 target.script = desc['script']
Patrick Rohr7aa98f92022-10-28 11:16:36 -0700296 target.args = desc['args']
Patrick Rohr09716f52022-10-27 13:02:36 -0700297 target.response_file_contents = self._get_response_file_contents(desc)
Patrick Rohrda778a02022-10-25 16:17:31 -0700298 elif target.type == 'copy':
299 # TODO: copy rules are not currently implemented.
300 self.actions[gn_target_name] = target
Patrick Rohr67f53122022-11-09 10:57:40 -0800301 elif self._is_java_target(target):
Patrick Rohraf92fa62022-11-04 14:27:04 -0700302 # java_group identifies the group target generated by the android_library
303 # or java_library template. A java_group must not be added as a dependency, but sources are collected
304 log.debug('Found java target %s', target.name)
305 target.type = 'java_group'
Patrick Rohr92d74122022-10-21 15:50:52 -0700306
307 # Default for 'public' is //* - all headers in 'sources' are public.
308 # TODO(primiano): if a 'public' section is specified (even if empty), then
309 # the rest of 'sources' is considered inaccessible by gn. Consider
310 # emulating that, so that generated build files don't end up with overly
311 # accessible headers.
312 public_headers = [x for x in desc.get('public', []) if x != '*']
313 target.public_headers.update(public_headers)
314
315 target.cflags.update(desc.get('cflags', []) + desc.get('cflags_cc', []))
316 target.libs.update(desc.get('libs', []))
317 target.ldflags.update(desc.get('ldflags', []))
318 target.defines.update(desc.get('defines', []))
319 target.include_dirs.update(desc.get('include_dirs', []))
320
321 # Recurse in dependencies.
Patrick Rohr7f4631e2022-11-15 14:35:03 -0800322 for gn_dep_name in desc.get('deps', []):
323 dep = self.parse_gn_desc(gn_desc, gn_dep_name)
324 dep_name = label_without_toolchain(gn_dep_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700325 if dep.is_third_party_dep_:
326 target.deps.add(dep_name)
327 elif dep.type == 'proto_library':
328 target.proto_deps.add(dep_name)
329 target.transitive_proto_deps.add(dep_name)
330 target.proto_paths.update(dep.proto_paths)
331 target.transitive_proto_deps.update(dep.transitive_proto_deps)
332 elif dep.type == 'source_set':
333 target.source_set_deps.add(dep_name)
334 target.update(dep) # Bubble up source set's cflags/ldflags etc.
335 elif dep.type == 'group':
336 target.update(dep) # Bubble up groups's cflags/ldflags etc.
Patrick Rohrda778a02022-10-25 16:17:31 -0700337 elif dep.type in ['action', 'action_foreach', 'copy']:
Patrick Rohr92d74122022-10-21 15:50:52 -0700338 if proto_target_type is None:
339 target.deps.add(dep_name)
340 elif dep.type in LINKER_UNIT_TYPES:
341 target.deps.add(dep_name)
Patrick Rohr3624f952022-11-04 14:30:18 -0700342 elif dep.type == 'java_group':
343 # Explicitly break dependency chain when a java_group is added.
344 # Java sources are collected and eventually compiled as one large
345 # java_library.
346 pass
Patrick Rohr92d74122022-10-21 15:50:52 -0700347
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800348 if dep.type == 'static_library':
349 # Bubble up static_libs. Necessary, since soong does not propagate
350 # static_libs up the build tree.
Patrick Rohra9c1dda2022-11-14 19:02:40 -0800351 # Protobuf dependencies are handled separately.
352 if '//third_party/protobuf' not in dep_name:
353 target.transitive_static_libs_deps.add(dep_name)
354
355 target.transitive_static_libs_deps.update(dep.transitive_static_libs_deps)
356 target.deps.update(target.transitive_static_libs_deps)
Patrick Rohr5de9f2e2022-11-11 15:33:20 -0800357
Patrick Rohrb27587e2022-11-04 14:57:24 -0700358 # Collect java sources. Java sources are kept inside the __compile_java target.
359 # This target can be used for both host and target compilation; only add
360 # the sources if they are destined for the target (i.e. they are a
361 # dependency of the __dex target)
362 # Note: this skips prebuilt java dependencies. These will have to be
363 # added manually when building the jar.
364 if re.match('.*__dex$', target.name):
365 if re.match('.*__compile_java$', dep.name):
366 log.debug('Adding java sources for %s', dep.name)
367 java_srcs = [src for src in dep.inputs if os.path.splitext(src)[1] == '.java']
368 self.java_sources.update(java_srcs)
369
Patrick Rohr92d74122022-10-21 15:50:52 -0700370 return target
371
372 def get_proto_exports(self, proto_desc):
373 # exports in metadata will be available for source_set targets.
374 metadata = proto_desc.get('metadata', {})
375 return metadata.get('exports', [])
376
377 def get_proto_paths(self, proto_desc):
378 # import_dirs in metadata will be available for source_set targets.
379 metadata = proto_desc.get('metadata', {})
380 return metadata.get('import_dirs', [])
381
Motomu Utsumid7e0e422022-11-08 17:49:52 +0900382
383 def get_proto_in_dir(self, proto_desc):
384 args = proto_desc.get('args')
385 return re.sub('^\.\./\.\./', '', args[args.index('--proto-in-dir') + 1])
386
Patrick Rohr0d40da32022-11-15 13:08:12 -0800387 def get_proto_target_type(self, gn_desc, gn_target_name):
Patrick Rohr92d74122022-10-21 15:50:52 -0700388 """ Checks if the target is a proto library and return the plugin.
389
390 Returns:
391 (None, None): if the target is not a proto library.
392 (plugin, proto_desc) where |plugin| is 'proto' in the default (lite)
393 case or 'protozero' or 'ipc' or 'descriptor'; |proto_desc| is the GN
394 json desc of the target with the .proto sources (_gen target for
395 non-descriptor types or the target itself for descriptor type).
396 """
Patrick Rohr0d40da32022-11-15 13:08:12 -0800397 parts = gn_target_name.split('(', 1)
Patrick Rohr92d74122022-10-21 15:50:52 -0700398 name = parts[0]
399 toolchain = '(' + parts[1] if len(parts) > 1 else ''
400
401 # Descriptor targets don't have a _gen target; instead we look for the
402 # characteristic flag in the args of the target itself.
Patrick Rohr0d40da32022-11-15 13:08:12 -0800403 desc = gn_desc.get(gn_target_name)
Patrick Rohr92d74122022-10-21 15:50:52 -0700404 if '--descriptor_set_out' in desc.get('args', []):
405 return 'descriptor', desc
406
407 # Source set proto targets have a non-empty proto_library_sources in the
408 # metadata of the description.
409 metadata = desc.get('metadata', {})
410 if 'proto_library_sources' in metadata:
411 return 'source_set', desc
412
413 # In all other cases, we want to look at the _gen target as that has the
414 # important information.
Patrick Rohr564d6be2022-11-15 12:57:57 -0800415 gen_desc = gn_desc.get('%s_gen%s' % (name, toolchain))
Patrick Rohr92d74122022-10-21 15:50:52 -0700416 if gen_desc is None or gen_desc['type'] != 'action':
417 return None, None
Patrick Rohrc5980782022-11-07 16:34:03 -0800418 if gen_desc['script'] != '//tools/protoc_wrapper/protoc_wrapper.py':
Patrick Rohr92d74122022-10-21 15:50:52 -0700419 return None, None
420 plugin = 'proto'
Patrick Rohrc5980782022-11-07 16:34:03 -0800421 args = gen_desc.get('args', [])
Patrick Rohr92d74122022-10-21 15:50:52 -0700422 for arg in (arg for arg in args if arg.startswith('--plugin=')):
423 # |arg| at this point looks like:
424 # --plugin=protoc-gen-plugin=gcc_like_host/protozero_plugin
425 # or
426 # --plugin=protoc-gen-plugin=protozero_plugin
427 plugin = arg.split('=')[-1].split('/')[-1].replace('_plugin', '')
428 return plugin, gen_desc