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