blob: eda110842a25810638ea82939e088d08111b89ff [file] [log] [blame]
Patrick Rohr92d74122022-10-21 15:50:52 -07001#!/usr/bin/env python3
2# Copyright (C) 2022 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This tool translates a collection of BUILD.gn files into a mostly equivalent
17# Android.bp file for the Android Soong build system. The input to the tool is a
18# JSON description of the GN build definition generated with the following
19# command:
20#
21# gn desc out --format=json --all-toolchains "//*" > desc.json
22#
23# The tool is then given a list of GN labels for which to generate Android.bp
24# build rules. The dependencies for the GN labels are squashed to the generated
25# Android.bp target, except for actions which get their own genrule. Some
26# libraries are also mapped to their Android equivalents -- see |builtin_deps|.
27
28import argparse
29import collections
30import json
31import os
32import re
33import sys
34
35import gn_utils
36
Patrick Rohr92d74122022-10-21 15:50:52 -070037ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
38
Patrick Rohr92d74122022-10-21 15:50:52 -070039# Defines a custom init_rc argument to be applied to the corresponding output
40# blueprint target.
41target_initrc = {
Patrick Rohrc36ef422022-10-25 10:38:05 -070042 # TODO: this can probably be removed.
Patrick Rohr92d74122022-10-21 15:50:52 -070043}
44
45target_host_supported = [
Patrick Rohrdc383942022-10-25 10:45:29 -070046 # TODO: remove if this is not useful for the cronet build.
Patrick Rohr92d74122022-10-21 15:50:52 -070047]
48
Patrick Rohr92d74122022-10-21 15:50:52 -070049# Proto target groups which will be made public.
50proto_groups = {
Patrick Rohr95212a22022-10-25 09:53:13 -070051 # TODO: remove if this is not used for the cronet build.
Patrick Rohr92d74122022-10-21 15:50:52 -070052}
53
54# All module names are prefixed with this string to avoid collisions.
55module_prefix = 'perfetto_'
56
57# Shared libraries which are directly translated to Android system equivalents.
58shared_library_allowlist = [
59 'android',
60 'android.hardware.atrace@1.0',
61 'android.hardware.health@2.0',
62 'android.hardware.health-V1-ndk',
63 'android.hardware.power.stats@1.0',
64 "android.hardware.power.stats-V1-cpp",
65 'base',
66 'binder',
67 'binder_ndk',
68 'cutils',
69 'hidlbase',
70 'hidltransport',
71 'hwbinder',
72 'incident',
73 'log',
74 'services',
75 'statssocket',
76 "tracingproxy",
77 'utils',
78]
79
80# Static libraries which are directly translated to Android system equivalents.
81static_library_allowlist = [
82 'statslog_perfetto',
83]
84
85# Name of the module which settings such as compiler flags for all other
86# modules.
87defaults_module = module_prefix + 'defaults'
88
89# Location of the project in the Android source tree.
90tree_path = 'external/perfetto'
91
92# Path for the protobuf sources in the standalone build.
93buildtools_protobuf_src = '//buildtools/protobuf/src'
94
95# Location of the protobuf src dir in the Android source tree.
96android_protobuf_src = 'external/protobuf/src'
97
98# Compiler flags which are passed through to the blueprint.
99cflag_allowlist = r'^-DPERFETTO.*$'
100
101# Compiler defines which are passed through to the blueprint.
102define_allowlist = r'^(GOOGLE_PROTO.*)|(ZLIB_.*)|(USE_MMAP)|(HAVE_HIDDEN)$'
103
104# The directory where the generated perfetto_build_flags.h will be copied into.
105buildflags_dir = 'include/perfetto/base/build_configs/android_tree'
106
107
108def enumerate_data_deps():
109 with open(os.path.join(ROOT_DIR, 'tools', 'test_data.txt')) as f:
110 lines = f.readlines()
111 for line in (line.strip() for line in lines if not line.startswith('#')):
112 assert os.path.exists(line), 'file %s should exist' % line
113 if line.startswith('test/data/'):
114 # Skip test data files that require GCS. They are only for benchmarks.
115 # We don't run benchmarks in the android tree.
116 continue
117 if line.endswith('/.'):
118 yield line[:-1] + '**/*'
119 else:
120 yield line
121
122
123# Additional arguments to apply to Android.bp rules.
124additional_args = {
125 'heapprofd_client_api': [
126 ('static_libs', {'libasync_safe'}),
127 # heapprofd_client_api MUST NOT have global constructors. Because it
128 # is loaded in an __attribute__((constructor)) of libc, we cannot
129 # guarantee that the global constructors get run before it is used.
130 ('cflags', {'-Wglobal-constructors', '-Werror=global-constructors'}),
131 ('version_script', 'src/profiling/memory/heapprofd_client_api.map.txt'),
132 ('stubs', {
133 'versions': ['S'],
134 'symbol_file': 'src/profiling/memory/heapprofd_client_api.map.txt',
135 }),
136 ('export_include_dirs', {'src/profiling/memory/include'}),
137 ],
138 'heapprofd_api_noop': [
139 ('version_script', 'src/profiling/memory/heapprofd_client_api.map.txt'),
140 ('stubs', {
141 'versions': ['S'],
142 'symbol_file': 'src/profiling/memory/heapprofd_client_api.map.txt',
143 }),
144 ('export_include_dirs', {'src/profiling/memory/include'}),
145 ],
146 'heapprofd_client': [
147 ('include_dirs', {'bionic/libc'}),
148 ('static_libs', {'libasync_safe'}),
149 ],
150 'heapprofd_standalone_client': [
151 ('static_libs', {'libasync_safe'}),
152 ('version_script', 'src/profiling/memory/heapprofd_client_api.map.txt'),
153 ('export_include_dirs', {'src/profiling/memory/include'}),
154 ('stl', 'libc++_static'),
155 ],
156# 'perfetto_unittests': [
157# ('data', set(enumerate_data_deps())),
158# ('include_dirs', {'bionic/libc/kernel'}),
159# ],
160 'perfetto_integrationtests': [
161 ('test_suites', {'general-tests'}),
162 ('test_config', 'PerfettoIntegrationTests.xml'),
163 ],
164 'traced_probes': [('required', {
165 'libperfetto_android_internal', 'trigger_perfetto', 'traced_perf',
166 'mm_events'
167 }),],
168 'libperfetto_android_internal': [('static_libs', {'libhealthhalutils'}),],
169 'trace_processor_shell': [
170 ('strip', {
171 'all': True
172 }),
173 ('host', {
174 'stl': 'libc++_static',
175 'dist': {
176 'targets': ['sdk_repo']
177 },
178 }),
179 ],
180 'libperfetto_client_experimental': [
181 ('apex_available', {
182 '//apex_available:platform', 'com.android.art',
183 'com.android.art.debug'
184 }),
185 ('min_sdk_version', 'S'),
186 ('shared_libs', {'liblog'}),
187 ('export_include_dirs', {'include', buildflags_dir}),
188 ],
189 'perfetto_trace_protos': [
190 ('apex_available', {
191 '//apex_available:platform', 'com.android.art',
192 'com.android.art.debug'
193 }),
194 ('min_sdk_version', 'S'),
195 ],
196 'libperfetto': [('export_include_dirs', {'include', buildflags_dir}),],
197}
198
199
200def enable_gtest_and_gmock(module):
201 module.static_libs.add('libgmock')
202 module.static_libs.add('libgtest')
203 if module.name != 'perfetto_gtest_logcat_printer':
204 module.whole_static_libs.add('perfetto_gtest_logcat_printer')
205
206
207def enable_protobuf_full(module):
208 if module.type == 'cc_binary_host':
209 module.static_libs.add('libprotobuf-cpp-full')
210 elif module.host_supported:
211 module.host.static_libs.add('libprotobuf-cpp-full')
212 module.android.shared_libs.add('libprotobuf-cpp-full')
213 else:
214 module.shared_libs.add('libprotobuf-cpp-full')
215
216
217def enable_protobuf_lite(module):
218 module.shared_libs.add('libprotobuf-cpp-lite')
219
220
221def enable_protoc_lib(module):
222 if module.type == 'cc_binary_host':
223 module.static_libs.add('libprotoc')
224 else:
225 module.shared_libs.add('libprotoc')
226
227
228def enable_libunwindstack(module):
229 if module.name != 'heapprofd_standalone_client':
230 module.shared_libs.add('libunwindstack')
231 module.shared_libs.add('libprocinfo')
232 module.shared_libs.add('libbase')
233 else:
234 module.static_libs.add('libunwindstack')
235 module.static_libs.add('libprocinfo')
236 module.static_libs.add('libbase')
237 module.static_libs.add('liblzma')
238 module.static_libs.add('libdexfile_support')
239 module.runtime_libs.add('libdexfile') # libdexfile_support dependency
240
241
242def enable_libunwind(module):
243 # libunwind is disabled on Darwin so we cannot depend on it.
244 pass
245
246
247def enable_sqlite(module):
248 if module.type == 'cc_binary_host':
249 module.static_libs.add('libsqlite')
250 module.static_libs.add('sqlite_ext_percentile')
251 elif module.host_supported:
252 # Copy what the sqlite3 command line tool does.
253 module.android.shared_libs.add('libsqlite')
254 module.android.shared_libs.add('libicu')
255 module.android.shared_libs.add('liblog')
256 module.android.shared_libs.add('libutils')
257 module.android.static_libs.add('sqlite_ext_percentile')
258 module.host.static_libs.add('libsqlite')
259 module.host.static_libs.add('sqlite_ext_percentile')
260 else:
261 module.shared_libs.add('libsqlite')
262 module.shared_libs.add('libicu')
263 module.shared_libs.add('liblog')
264 module.shared_libs.add('libutils')
265 module.static_libs.add('sqlite_ext_percentile')
266
267
268def enable_zlib(module):
269 if module.type == 'cc_binary_host':
270 module.static_libs.add('libz')
271 elif module.host_supported:
272 module.android.shared_libs.add('libz')
273 module.host.static_libs.add('libz')
274 else:
275 module.shared_libs.add('libz')
276
277
278def enable_uapi_headers(module):
279 module.include_dirs.add('bionic/libc/kernel')
280
281
282def enable_bionic_libc_platform_headers_on_android(module):
283 module.header_libs.add('bionic_libc_platform_headers')
284
285
286# Android equivalents for third-party libraries that the upstream project
287# depends on.
288builtin_deps = {
289 '//gn:default_deps':
290 lambda x: None,
291 '//gn:gtest_main':
292 lambda x: None,
293 '//gn:protoc':
294 lambda x: None,
295 '//gn:gtest_and_gmock':
296 enable_gtest_and_gmock,
297 '//gn:libunwind':
298 enable_libunwind,
299 '//gn:protobuf_full':
300 enable_protobuf_full,
301 '//gn:protobuf_lite':
302 enable_protobuf_lite,
303 '//gn:protoc_lib':
304 enable_protoc_lib,
305 '//gn:libunwindstack':
306 enable_libunwindstack,
307 '//gn:sqlite':
308 enable_sqlite,
309 '//gn:zlib':
310 enable_zlib,
311 '//gn:bionic_kernel_uapi_headers':
312 enable_uapi_headers,
313 '//src/profiling/memory:bionic_libc_platform_headers_on_android':
314 enable_bionic_libc_platform_headers_on_android,
315}
316
317# ----------------------------------------------------------------------------
318# End of configuration.
319# ----------------------------------------------------------------------------
320
321
322class Error(Exception):
323 pass
324
325
326class ThrowingArgumentParser(argparse.ArgumentParser):
327
328 def __init__(self, context):
329 super(ThrowingArgumentParser, self).__init__()
330 self.context = context
331
332 def error(self, message):
333 raise Error('%s: %s' % (self.context, message))
334
335
336def write_blueprint_key_value(output, name, value, sort=True):
337 """Writes a Blueprint key-value pair to the output"""
338
339 if isinstance(value, bool):
340 if value:
341 output.append(' %s: true,' % name)
342 else:
343 output.append(' %s: false,' % name)
344 return
345 if not value:
346 return
347 if isinstance(value, set):
348 value = sorted(value)
349 if isinstance(value, list):
350 output.append(' %s: [' % name)
351 for item in sorted(value) if sort else value:
352 output.append(' "%s",' % item)
353 output.append(' ],')
354 return
355 if isinstance(value, Target):
356 value.to_string(output)
357 return
358 if isinstance(value, dict):
359 kv_output = []
360 for k, v in value.items():
361 write_blueprint_key_value(kv_output, k, v)
362
363 output.append(' %s: {' % name)
364 for line in kv_output:
365 output.append(' %s' % line)
366 output.append(' },')
367 return
368 output.append(' %s: "%s",' % (name, value))
369
370
371class Target(object):
372 """A target-scoped part of a module"""
373
374 def __init__(self, name):
375 self.name = name
376 self.shared_libs = set()
377 self.static_libs = set()
378 self.whole_static_libs = set()
379 self.cflags = set()
380 self.dist = dict()
381 self.strip = dict()
382 self.stl = None
383
384 def to_string(self, output):
385 nested_out = []
386 self._output_field(nested_out, 'shared_libs')
387 self._output_field(nested_out, 'static_libs')
388 self._output_field(nested_out, 'whole_static_libs')
389 self._output_field(nested_out, 'cflags')
390 self._output_field(nested_out, 'stl')
391 self._output_field(nested_out, 'dist')
392 self._output_field(nested_out, 'strip')
393
394 if nested_out:
395 output.append(' %s: {' % self.name)
396 for line in nested_out:
397 output.append(' %s' % line)
398 output.append(' },')
399
400 def _output_field(self, output, name, sort=True):
401 value = getattr(self, name)
402 return write_blueprint_key_value(output, name, value, sort)
403
404
405class Module(object):
406 """A single module (e.g., cc_binary, cc_test) in a blueprint."""
407
408 def __init__(self, mod_type, name, gn_target):
409 self.type = mod_type
410 self.gn_target = gn_target
411 self.name = name
412 self.srcs = set()
413 self.comment = 'GN: ' + gn_utils.label_without_toolchain(gn_target)
414 self.shared_libs = set()
415 self.static_libs = set()
416 self.whole_static_libs = set()
417 self.runtime_libs = set()
418 self.tools = set()
419 self.cmd = None
420 self.host_supported = False
421 self.vendor_available = False
422 self.init_rc = set()
423 self.out = set()
424 self.export_include_dirs = set()
425 self.generated_headers = set()
426 self.export_generated_headers = set()
427 self.defaults = set()
428 self.cflags = set()
429 self.include_dirs = set()
430 self.header_libs = set()
431 self.required = set()
432 self.user_debug_flag = False
433 self.tool_files = None
434 self.android = Target('android')
435 self.host = Target('host')
436 self.lto = None
437 self.stl = None
438 self.dist = dict()
439 self.strip = dict()
440 self.data = set()
441 self.apex_available = set()
442 self.min_sdk_version = None
443 self.proto = dict()
444 # The genrule_XXX below are properties that must to be propagated back
445 # on the module(s) that depend on the genrule.
446 self.genrule_headers = set()
447 self.genrule_srcs = set()
448 self.genrule_shared_libs = set()
449 self.version_script = None
450 self.test_suites = set()
451 self.test_config = None
452 self.stubs = {}
453
454 def to_string(self, output):
455 if self.comment:
456 output.append('// %s' % self.comment)
457 output.append('%s {' % self.type)
458 self._output_field(output, 'name')
459 self._output_field(output, 'srcs')
460 self._output_field(output, 'shared_libs')
461 self._output_field(output, 'static_libs')
462 self._output_field(output, 'whole_static_libs')
463 self._output_field(output, 'runtime_libs')
464 self._output_field(output, 'tools')
465 self._output_field(output, 'cmd', sort=False)
466 if self.host_supported:
467 self._output_field(output, 'host_supported')
468 if self.vendor_available:
469 self._output_field(output, 'vendor_available')
470 self._output_field(output, 'init_rc')
471 self._output_field(output, 'out')
472 self._output_field(output, 'export_include_dirs')
473 self._output_field(output, 'generated_headers')
474 self._output_field(output, 'export_generated_headers')
475 self._output_field(output, 'defaults')
476 self._output_field(output, 'cflags')
477 self._output_field(output, 'include_dirs')
478 self._output_field(output, 'header_libs')
479 self._output_field(output, 'required')
480 self._output_field(output, 'dist')
481 self._output_field(output, 'strip')
482 self._output_field(output, 'tool_files')
483 self._output_field(output, 'data')
484 self._output_field(output, 'stl')
485 self._output_field(output, 'apex_available')
486 self._output_field(output, 'min_sdk_version')
487 self._output_field(output, 'version_script')
488 self._output_field(output, 'test_suites')
489 self._output_field(output, 'test_config')
490 self._output_field(output, 'stubs')
491 self._output_field(output, 'proto')
492
493 target_out = []
494 self._output_field(target_out, 'android')
495 self._output_field(target_out, 'host')
496 if target_out:
497 output.append(' target: {')
498 for line in target_out:
499 output.append(' %s' % line)
500 output.append(' },')
501
502 if self.user_debug_flag:
503 output.append(' product_variables: {')
504 output.append(' debuggable: {')
505 output.append(
506 ' cflags: ["-DPERFETTO_BUILD_WITH_ANDROID_USERDEBUG"],')
507 output.append(' },')
508 output.append(' },')
509 if self.lto is not None:
510 output.append(' target: {')
511 output.append(' android: {')
512 output.append(' lto: {')
513 output.append(' thin: %s,' %
514 'true' if self.lto else 'false')
515 output.append(' },')
516 output.append(' },')
517 output.append(' },')
518 output.append('}')
519 output.append('')
520
521 def add_android_static_lib(self, lib):
522 if self.type == 'cc_binary_host':
523 raise Exception('Adding Android static lib for host tool is unsupported')
524 elif self.host_supported:
525 self.android.static_libs.add(lib)
526 else:
527 self.static_libs.add(lib)
528
529 def add_android_shared_lib(self, lib):
530 if self.type == 'cc_binary_host':
531 raise Exception('Adding Android shared lib for host tool is unsupported')
532 elif self.host_supported:
533 self.android.shared_libs.add(lib)
534 else:
535 self.shared_libs.add(lib)
536
537 def _output_field(self, output, name, sort=True):
538 value = getattr(self, name)
539 return write_blueprint_key_value(output, name, value, sort)
540
541
542class Blueprint(object):
543 """In-memory representation of an Android.bp file."""
544
545 def __init__(self):
546 self.modules = {}
547
548 def add_module(self, module):
549 """Adds a new module to the blueprint, replacing any existing module
550 with the same name.
551
552 Args:
553 module: Module instance.
554 """
555 self.modules[module.name] = module
556
557 def to_string(self, output):
Patrick Rohr23f26192022-10-25 09:45:22 -0700558 for m in sorted(self.modules.values(), key=lambda m: m.name):
Patrick Rohr92d74122022-10-21 15:50:52 -0700559 m.to_string(output)
560
561
562def label_to_module_name(label):
563 """Turn a GN label (e.g., //:perfetto_tests) into a module name."""
564 # If the label is explicibly listed in the default target list, don't prefix
565 # its name and return just the target name. This is so tools like
566 # "traceconv" stay as such in the Android tree.
567 label_without_toolchain = gn_utils.label_without_toolchain(label)
Patrick Rohr92d74122022-10-21 15:50:52 -0700568 module = re.sub(r'^//:?', '', label_without_toolchain)
569 module = re.sub(r'[^a-zA-Z0-9_]', '_', module)
570 if not module.startswith(module_prefix):
571 return module_prefix + module
572 return module
573
574
575def is_supported_source_file(name):
576 """Returns True if |name| can appear in a 'srcs' list."""
577 return os.path.splitext(name)[1] in ['.c', '.cc', '.proto']
578
579
580def create_proto_modules(blueprint, gn, target):
581 """Generate genrules for a proto GN target.
582
583 GN actions are used to dynamically generate files during the build. The
584 Soong equivalent is a genrule. This function turns a specific kind of
585 genrule which turns .proto files into source and header files into a pair
586 equivalent genrules.
587
588 Args:
589 blueprint: Blueprint instance which is being generated.
590 target: gn_utils.Target object.
591
592 Returns:
593 The source_genrule module.
594 """
595 assert (target.type == 'proto_library')
596
597 tools = {'aprotoc'}
598 cpp_out_dir = '$(genDir)/%s/' % tree_path
599 target_module_name = label_to_module_name(target.name)
600
601 # In GN builds the proto path is always relative to the output directory
602 # (out/tmp.xxx).
603 cmd = ['mkdir -p %s &&' % cpp_out_dir, '$(location aprotoc)']
604 cmd += ['--proto_path=%s' % tree_path]
605
606 if buildtools_protobuf_src in target.proto_paths:
607 cmd += ['--proto_path=%s' % android_protobuf_src]
608
609 # We don't generate any targets for source_set proto modules because
610 # they will be inlined into other modules if required.
611 if target.proto_plugin == 'source_set':
612 return None
613
614 # Descriptor targets only generate a single target.
615 if target.proto_plugin == 'descriptor':
616 out = '{}.bin'.format(target_module_name)
617
618 cmd += ['--descriptor_set_out=$(out)']
619 cmd += ['$(in)']
620
621 descriptor_module = Module('genrule', target_module_name, target.name)
622 descriptor_module.cmd = ' '.join(cmd)
623 descriptor_module.out = [out]
624 descriptor_module.tools = tools
625 blueprint.add_module(descriptor_module)
626
627 # Recursively extract the .proto files of all the dependencies and
628 # add them to srcs.
629 descriptor_module.srcs.update(
630 gn_utils.label_to_path(src) for src in target.sources)
631 for dep in target.transitive_proto_deps:
632 current_target = gn.get_target(dep)
633 descriptor_module.srcs.update(
634 gn_utils.label_to_path(src) for src in current_target.sources)
635
636 return descriptor_module
637
638 # We create two genrules for each proto target: one for the headers and
639 # another for the sources. This is because the module that depends on the
640 # generated files needs to declare two different types of dependencies --
641 # source files in 'srcs' and headers in 'generated_headers' -- and it's not
642 # valid to generate .h files from a source dependency and vice versa.
643 source_module_name = target_module_name + '_gen'
644 source_module = Module('genrule', source_module_name, target.name)
645 blueprint.add_module(source_module)
646 source_module.srcs.update(
647 gn_utils.label_to_path(src) for src in target.sources)
648
649 header_module = Module('genrule', source_module_name + '_headers',
650 target.name)
651 blueprint.add_module(header_module)
652 header_module.srcs = set(source_module.srcs)
653
654 # TODO(primiano): at some point we should remove this. This was introduced
655 # by aosp/1108421 when adding "protos/" to .proto include paths, in order to
656 # avoid doing multi-repo changes and allow old clients in the android tree
657 # to still do the old #include "perfetto/..." rather than
658 # #include "protos/perfetto/...".
659 header_module.export_include_dirs = {'.', 'protos'}
660
661 source_module.genrule_srcs.add(':' + source_module.name)
662 source_module.genrule_headers.add(header_module.name)
663
664 if target.proto_plugin == 'proto':
665 suffixes = ['pb']
666 source_module.genrule_shared_libs.add('libprotobuf-cpp-lite')
667 cmd += ['--cpp_out=lite=true:' + cpp_out_dir]
668 elif target.proto_plugin == 'protozero':
669 suffixes = ['pbzero']
670 plugin = create_modules_from_target(blueprint, gn, protozero_plugin)
671 tools.add(plugin.name)
672 cmd += ['--plugin=protoc-gen-plugin=$(location %s)' % plugin.name]
673 cmd += ['--plugin_out=wrapper_namespace=pbzero:' + cpp_out_dir]
674 elif target.proto_plugin == 'cppgen':
675 suffixes = ['gen']
676 plugin = create_modules_from_target(blueprint, gn, cppgen_plugin)
677 tools.add(plugin.name)
678 cmd += ['--plugin=protoc-gen-plugin=$(location %s)' % plugin.name]
679 cmd += ['--plugin_out=wrapper_namespace=gen:' + cpp_out_dir]
680 elif target.proto_plugin == 'ipc':
681 suffixes = ['ipc']
682 plugin = create_modules_from_target(blueprint, gn, ipc_plugin)
683 tools.add(plugin.name)
684 cmd += ['--plugin=protoc-gen-plugin=$(location %s)' % plugin.name]
685 cmd += ['--plugin_out=wrapper_namespace=gen:' + cpp_out_dir]
686 else:
687 raise Error('Unsupported proto plugin: %s' % target.proto_plugin)
688
689 cmd += ['$(in)']
690 source_module.cmd = ' '.join(cmd)
691 header_module.cmd = source_module.cmd
692 source_module.tools = tools
693 header_module.tools = tools
694
695 for sfx in suffixes:
696 source_module.out.update('%s/%s' %
697 (tree_path, src.replace('.proto', '.%s.cc' % sfx))
698 for src in source_module.srcs)
699 header_module.out.update('%s/%s' %
700 (tree_path, src.replace('.proto', '.%s.h' % sfx))
701 for src in header_module.srcs)
702 return source_module
703
704
705def create_amalgamated_sql_metrics_module(blueprint, target):
706 bp_module_name = label_to_module_name(target.name)
707 module = Module('genrule', bp_module_name, target.name)
708 module.tool_files = [
709 'tools/gen_amalgamated_sql_metrics.py',
710 ]
711 module.cmd = ' '.join([
712 '$(location tools/gen_amalgamated_sql_metrics.py)',
713 '--cpp_out=$(out)',
714 '$(in)',
715 ])
716 module.genrule_headers.add(module.name)
717 module.out.update(target.outputs)
718 module.srcs.update(gn_utils.label_to_path(src) for src in target.inputs)
719 blueprint.add_module(module)
720 return module
721
722
723def create_cc_proto_descriptor_module(blueprint, target):
724 bp_module_name = label_to_module_name(target.name)
725 module = Module('genrule', bp_module_name, target.name)
726 module.tool_files = [
727 'tools/gen_cc_proto_descriptor.py',
728 ]
729 module.cmd = ' '.join([
730 '$(location tools/gen_cc_proto_descriptor.py)', '--gen_dir=$(genDir)',
731 '--cpp_out=$(out)', '$(in)'
732 ])
733 module.genrule_headers.add(module.name)
734 module.srcs.update(
735 ':' + label_to_module_name(dep) for dep in target.proto_deps)
736 module.srcs.update(
737 gn_utils.label_to_path(src)
738 for src in target.inputs
739 if "tmp.gn_utils" not in src)
740 module.out.update(target.outputs)
741 blueprint.add_module(module)
742 return module
743
744
745def create_gen_version_module(blueprint, target, bp_module_name):
746 module = Module('genrule', bp_module_name, gn_utils.GEN_VERSION_TARGET)
747 script_path = gn_utils.label_to_path(target.script)
748 module.genrule_headers.add(bp_module_name)
749 module.tool_files = [script_path]
750 module.out.update(target.outputs)
751 module.srcs.update(gn_utils.label_to_path(src) for src in target.inputs)
752 module.cmd = ' '.join([
753 'python3 $(location %s)' % script_path, '--no_git',
754 '--changelog=$(location CHANGELOG)', '--cpp_out=$(out)'
755 ])
756 blueprint.add_module(module)
757 return module
758
759
760def create_proto_group_modules(blueprint, gn, module_name, target_names):
761 # TODO(lalitm): today, we're only adding a Java lite module because that's
762 # the only one used in practice. In the future, if we need other target types
763 # (e.g. C++, Java full etc.) add them here.
764 bp_module_name = label_to_module_name(module_name) + '_java_protos'
765 module = Module('java_library', bp_module_name, bp_module_name)
766 module.comment = f'''GN: [{', '.join(target_names)}]'''
767 module.proto = {'type': 'lite', 'canonical_path_from_root': False}
768
769 for name in target_names:
770 target = gn.get_target(name)
771 module.srcs.update(gn_utils.label_to_path(src) for src in target.sources)
772 for dep_label in target.transitive_proto_deps:
773 dep = gn.get_target(dep_label)
774 module.srcs.update(gn_utils.label_to_path(src) for src in dep.sources)
775
776 blueprint.add_module(module)
777
778
779def _get_cflags(target):
780 cflags = {flag for flag in target.cflags if re.match(cflag_allowlist, flag)}
781 cflags |= set("-D%s" % define
782 for define in target.defines
783 if re.match(define_allowlist, define))
784 return cflags
785
786
787def create_modules_from_target(blueprint, gn, gn_target_name):
788 """Generate module(s) for a given GN target.
789
790 Given a GN target name, generate one or more corresponding modules into a
791 blueprint. The only case when this generates >1 module is proto libraries.
792
793 Args:
794 blueprint: Blueprint instance which is being generated.
795 gn: gn_utils.GnParser object.
796 gn_target_name: GN target for module generation.
797 """
798 bp_module_name = label_to_module_name(gn_target_name)
799 if bp_module_name in blueprint.modules:
800 return blueprint.modules[bp_module_name]
801 target = gn.get_target(gn_target_name)
802
803 name_without_toolchain = gn_utils.label_without_toolchain(target.name)
804 if target.type == 'executable':
805 if target.toolchain == gn_utils.HOST_TOOLCHAIN:
806 module_type = 'cc_binary_host'
807 elif target.testonly:
808 module_type = 'cc_test'
809 else:
810 module_type = 'cc_binary'
811 module = Module(module_type, bp_module_name, gn_target_name)
812 elif target.type == 'static_library':
813 module = Module('cc_library_static', bp_module_name, gn_target_name)
814 elif target.type == 'shared_library':
815 module = Module('cc_library_shared', bp_module_name, gn_target_name)
816 elif target.type == 'source_set':
817 module = Module('filegroup', bp_module_name, gn_target_name)
818 elif target.type == 'group':
819 # "group" targets are resolved recursively by gn_utils.get_target().
820 # There's nothing we need to do at this level for them.
821 return None
822 elif target.type == 'proto_library':
823 module = create_proto_modules(blueprint, gn, target)
824 if module is None:
825 return None
826 elif target.type == 'action':
827 if 'gen_amalgamated_sql_metrics' in target.name:
828 module = create_amalgamated_sql_metrics_module(blueprint, target)
829 elif re.match('.*gen_cc_.*_descriptor$', name_without_toolchain):
830 module = create_cc_proto_descriptor_module(blueprint, target)
831 elif target.type == 'action' and \
832 name_without_toolchain == gn_utils.GEN_VERSION_TARGET:
833 module = create_gen_version_module(blueprint, target, bp_module_name)
834 else:
835 raise Error('Unhandled action: {}'.format(target.name))
836 else:
837 raise Error('Unknown target %s (%s)' % (target.name, target.type))
838
839 blueprint.add_module(module)
840 module.host_supported = (name_without_toolchain in target_host_supported)
Patrick Rohr92d74122022-10-21 15:50:52 -0700841 module.init_rc = target_initrc.get(target.name, [])
842 module.srcs.update(
843 gn_utils.label_to_path(src)
844 for src in target.sources
845 if is_supported_source_file(src))
846
847 if target.type in gn_utils.LINKER_UNIT_TYPES:
848 module.cflags.update(_get_cflags(target))
849
850 module_is_compiled = module.type not in ('genrule', 'filegroup')
851 if module_is_compiled:
852 # Don't try to inject library/source dependencies into genrules or
853 # filegroups because they are not compiled in the traditional sense.
854 module.defaults = [defaults_module]
855 for lib in target.libs:
856 # Generally library names should be mangled as 'libXXX', unless they
857 # are HAL libraries (e.g., android.hardware.health@2.0) or AIDL c++ / NDK
858 # libraries (e.g. "android.hardware.power.stats-V1-cpp")
859 android_lib = lib if '@' in lib or "-cpp" in lib or "-ndk" in lib \
860 else 'lib' + lib
861 if lib in shared_library_allowlist:
862 module.add_android_shared_lib(android_lib)
863 if lib in static_library_allowlist:
864 module.add_android_static_lib(android_lib)
865
866 # If the module is a static library, export all the generated headers.
867 if module.type == 'cc_library_static':
868 module.export_generated_headers = module.generated_headers
869
870 # Merge in additional hardcoded arguments.
871 for key, add_val in additional_args.get(module.name, []):
872 curr = getattr(module, key)
873 if add_val and isinstance(add_val, set) and isinstance(curr, set):
874 curr.update(add_val)
875 elif isinstance(add_val, str) and (not curr or isinstance(curr, str)):
876 setattr(module, key, add_val)
877 elif isinstance(add_val, bool) and (not curr or isinstance(curr, bool)):
878 setattr(module, key, add_val)
879 elif isinstance(add_val, dict) and isinstance(curr, dict):
880 curr.update(add_val)
881 elif isinstance(add_val, dict) and isinstance(curr, Target):
882 curr.__dict__.update(add_val)
883 else:
884 raise Error('Unimplemented type %r of additional_args: %r' %
885 (type(add_val), key))
886
887 # dep_name is an unmangled GN target name (e.g. //foo:bar(toolchain)).
888 all_deps = target.deps | target.source_set_deps | target.transitive_proto_deps
889 for dep_name in all_deps:
890 # If the dependency refers to a library which we can replace with an
891 # Android equivalent, stop recursing and patch the dependency in.
892 # Don't recurse into //buildtools, builtin_deps are intercepted at
893 # the //gn:xxx level.
894 if dep_name.startswith('//buildtools'):
895 continue
896
897 # Ignore the dependency on the gen_buildflags genrule. That is run
898 # separately in this generator and the generated file is copied over
899 # into the repo (see usage of |buildflags_dir| in this script).
900 if dep_name.startswith(gn_utils.BUILDFLAGS_TARGET):
901 continue
902
903 dep_module = create_modules_from_target(blueprint, gn, dep_name)
904
905 # For filegroups and genrule, recurse but don't apply the deps.
906 if not module_is_compiled:
907 continue
908
909 # |builtin_deps| override GN deps with Android-specific ones. See the
910 # config in the top of this file.
911 if gn_utils.label_without_toolchain(dep_name) in builtin_deps:
912 builtin_deps[gn_utils.label_without_toolchain(dep_name)](module)
913 continue
914
915 # Don't recurse in any other //gn dep if not handled by builtin_deps.
916 if dep_name.startswith('//gn:'):
917 continue
918
919 if dep_module is None:
920 continue
921 if dep_module.type == 'cc_library_shared':
922 module.shared_libs.add(dep_module.name)
923 elif dep_module.type == 'cc_library_static':
924 module.static_libs.add(dep_module.name)
925 elif dep_module.type == 'filegroup':
926 module.srcs.add(':' + dep_module.name)
927 elif dep_module.type == 'genrule':
928 module.generated_headers.update(dep_module.genrule_headers)
929 module.srcs.update(dep_module.genrule_srcs)
930 module.shared_libs.update(dep_module.genrule_shared_libs)
931 elif dep_module.type == 'cc_binary':
932 continue # Ignore executables deps (used by cmdline integration tests).
933 else:
934 raise Error('Unknown dep %s (%s) for target %s' %
935 (dep_module.name, dep_module.type, module.name))
936
937 return module
938
939
940def create_blueprint_for_targets(gn, desc, targets):
941 """Generate a blueprint for a list of GN targets."""
942 blueprint = Blueprint()
943
944 # Default settings used by all modules.
945 defaults = Module('cc_defaults', defaults_module, '//gn:default_deps')
946
947 # We have to use include_dirs passing the path relative to the android tree.
948 # This is because: (i) perfetto_cc_defaults is used also by
949 # test/**/Android.bp; (ii) if we use local_include_dirs instead, paths
950 # become relative to the Android.bp that *uses* cc_defaults (not the one
951 # that defines it).s
952 defaults.include_dirs = {
953 tree_path, tree_path + '/include', tree_path + '/' + buildflags_dir,
954 tree_path + '/src/profiling/memory/include'
955 }
956 defaults.cflags = [
957 '-Wno-error=return-type',
958 '-Wno-sign-compare',
959 '-Wno-sign-promo',
960 '-Wno-unused-parameter',
961 '-fvisibility=hidden',
962 '-O2',
963 ]
964 defaults.user_debug_flag = True
965 defaults.lto = True
966
967 blueprint.add_module(defaults)
968 for target in targets:
969 create_modules_from_target(blueprint, gn, target)
970 return blueprint
971
972
973def main():
974 parser = argparse.ArgumentParser(
975 description='Generate Android.bp from a GN description.')
976 parser.add_argument(
Patrick Rohr92d74122022-10-21 15:50:52 -0700977 '--desc',
Patrick Rohr3db246a2022-10-25 10:25:17 -0700978 help='GN description (e.g., gn desc out --format=json --all-toolchains "//*"',
979 required=True
Patrick Rohr92d74122022-10-21 15:50:52 -0700980 )
981 parser.add_argument(
982 '--extras',
983 help='Extra targets to include at the end of the Blueprint file',
984 default=os.path.join(gn_utils.repo_root(), 'Android.bp.extras'),
985 )
986 parser.add_argument(
987 '--output',
988 help='Blueprint file to create',
989 default=os.path.join(gn_utils.repo_root(), 'Android.bp'),
990 )
991 parser.add_argument(
992 'targets',
993 nargs=argparse.REMAINDER,
Patrick Rohr1aa504a2022-10-25 10:30:42 -0700994 help='Targets to include in the blueprint (e.g., "//:perfetto_tests")'
995 )
Patrick Rohr92d74122022-10-21 15:50:52 -0700996 args = parser.parse_args()
997
Patrick Rohr3db246a2022-10-25 10:25:17 -0700998 with open(args.desc) as f:
999 desc = json.load(f)
Patrick Rohr92d74122022-10-21 15:50:52 -07001000
1001 gn = gn_utils.GnParser(desc)
Patrick Rohr1aa504a2022-10-25 10:30:42 -07001002 blueprint = create_blueprint_for_targets(gn, desc, args.targets)
Patrick Rohr92d74122022-10-21 15:50:52 -07001003 project_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
1004 tool_name = os.path.relpath(os.path.abspath(__file__), project_root)
1005
Patrick Rohr92d74122022-10-21 15:50:52 -07001006 # Add any proto groups to the blueprint.
1007 for l_name, t_names in proto_groups.items():
1008 create_proto_group_modules(blueprint, gn, l_name, t_names)
1009
1010 output = [
Patrick Rohr5478b392022-10-25 09:58:50 -07001011 """// Copyright (C) 2022 The Android Open Source Project
Patrick Rohr92d74122022-10-21 15:50:52 -07001012//
1013// Licensed under the Apache License, Version 2.0 (the "License");
1014// you may not use this file except in compliance with the License.
1015// You may obtain a copy of the License at
1016//
1017// http://www.apache.org/licenses/LICENSE-2.0
1018//
1019// Unless required by applicable law or agreed to in writing, software
1020// distributed under the License is distributed on an "AS IS" BASIS,
1021// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1022// See the License for the specific language governing permissions and
1023// limitations under the License.
1024//
1025// This file is automatically generated by %s. Do not edit.
1026""" % (tool_name)
1027 ]
1028 blueprint.to_string(output)
Patrick Rohrcb98e9b2022-10-25 09:57:02 -07001029 if os.path.exists(args.extras):
1030 with open(args.extras, 'r') as r:
1031 for line in r:
1032 output.append(line.rstrip("\n\r"))
Patrick Rohr92d74122022-10-21 15:50:52 -07001033
1034 out_files = []
1035
1036 # Generate the Android.bp file.
1037 out_files.append(args.output + '.swp')
1038 with open(out_files[-1], 'w') as f:
1039 f.write('\n'.join(output))
1040 # Text files should have a trailing EOL.
1041 f.write('\n')
1042
Patrick Rohr94693eb2022-10-25 10:09:16 -07001043 return 0
Patrick Rohr92d74122022-10-21 15:50:52 -07001044
1045
1046if __name__ == '__main__':
1047 sys.exit(main())