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