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