Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2017 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 json |
| 30 | import os |
| 31 | import re |
| 32 | import sys |
| 33 | |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 34 | import gn_utils |
| 35 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 36 | # Default targets to translate to the blueprint file. |
Primiano Tucci | 4e49c02 | 2017-12-21 18:22:44 +0100 | [diff] [blame] | 37 | default_targets = [ |
Primiano Tucci | bdb2a59 | 2018-10-11 15:59:29 +0100 | [diff] [blame] | 38 | '//:libperfetto', |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 39 | '//:libperfetto_client_experimental', |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 40 | '//:libperfetto_android_internal', |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 41 | '//:perfetto_integrationtests', |
Primiano Tucci | 6aa7557 | 2018-03-21 05:33:14 -0700 | [diff] [blame] | 42 | '//:perfetto_trace_protos', |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 43 | '//:perfetto_unittests', |
Primiano Tucci | 3b72910 | 2018-01-08 18:16:36 +0000 | [diff] [blame] | 44 | '//:perfetto', |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 45 | '//:trace_processor_shell', |
Primiano Tucci | 4e49c02 | 2017-12-21 18:22:44 +0100 | [diff] [blame] | 46 | '//:traced', |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 47 | '//:traced_probes', |
Primiano Tucci | 21c19d8 | 2018-03-29 12:35:08 +0100 | [diff] [blame] | 48 | '//:trace_to_text', |
Florian Mayer | b6a921f | 2018-10-18 18:55:23 +0100 | [diff] [blame] | 49 | '//:heapprofd_client', |
| 50 | '//:heapprofd', |
Hector Dearman | 696ff77 | 2019-04-23 18:38:53 +0100 | [diff] [blame] | 51 | '//:trigger_perfetto', |
Primiano Tucci | 4e49c02 | 2017-12-21 18:22:44 +0100 | [diff] [blame] | 52 | ] |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 53 | |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 54 | # Targets which are testonly but should still be a cc_binary. |
| 55 | non_test_binaries = [ |
| 56 | '//:trace_processor_shell', |
| 57 | ] |
| 58 | |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 59 | # Defines a custom init_rc argument to be applied to the corresponding output |
| 60 | # blueprint target. |
Primiano Tucci | 5a30453 | 2018-01-09 14:15:43 +0000 | [diff] [blame] | 61 | target_initrc = { |
| 62 | '//:traced': 'perfetto.rc', |
Florian Mayer | 7a5d83b | 2018-10-18 18:57:02 +0100 | [diff] [blame] | 63 | '//:heapprofd': 'heapprofd.rc', |
Primiano Tucci | 5a30453 | 2018-01-09 14:15:43 +0000 | [diff] [blame] | 64 | } |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 65 | |
Primiano Tucci | 6aa7557 | 2018-03-21 05:33:14 -0700 | [diff] [blame] | 66 | target_host_supported = [ |
| 67 | '//:perfetto_trace_protos', |
| 68 | ] |
| 69 | |
Primiano Tucci | 21c19d8 | 2018-03-29 12:35:08 +0100 | [diff] [blame] | 70 | target_host_only = [ |
| 71 | '//:trace_to_text', |
| 72 | ] |
| 73 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 74 | # Arguments for the GN output directory. |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 75 | gn_args = 'target_os="android" target_cpu="arm" is_debug=false perfetto_build_with_android=true' |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 76 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 77 | # All module names are prefixed with this string to avoid collisions. |
| 78 | module_prefix = 'perfetto_' |
| 79 | |
| 80 | # Shared libraries which are directly translated to Android system equivalents. |
| 81 | library_whitelist = [ |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 82 | "android.hardware.atrace@1.0", |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 83 | 'android.hardware.health@2.0', |
Esteban Talavera | 1fecac9 | 2019-01-09 16:06:29 +0000 | [diff] [blame] | 84 | "android.hardware.power.stats@1.0", |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 85 | 'android', |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 86 | 'base', |
Sami Kyostila | b5b7169 | 2018-01-12 12:16:44 +0000 | [diff] [blame] | 87 | 'binder', |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 88 | 'hidlbase', |
| 89 | 'hidltransport', |
| 90 | 'hwbinder', |
Ryan Savitski | 53ca60b | 2019-06-03 13:04:40 +0100 | [diff] [blame] | 91 | 'incident', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 92 | 'log', |
Sami Kyostila | b5b7169 | 2018-01-12 12:16:44 +0000 | [diff] [blame] | 93 | 'services', |
Primiano Tucci | edf099c | 2018-01-08 18:27:56 +0000 | [diff] [blame] | 94 | 'utils', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 95 | ] |
| 96 | |
| 97 | # Name of the module which settings such as compiler flags for all other |
| 98 | # modules. |
| 99 | defaults_module = module_prefix + 'defaults' |
| 100 | |
| 101 | # Location of the project in the Android source tree. |
| 102 | tree_path = 'external/perfetto' |
| 103 | |
Primiano Tucci | edf099c | 2018-01-08 18:27:56 +0000 | [diff] [blame] | 104 | # Compiler flags which are passed through to the blueprint. |
| 105 | cflag_whitelist = r'^-DPERFETTO.*$' |
| 106 | |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 107 | # Compiler defines which are passed through to the blueprint. |
Hector Dearman | 554627f | 2019-06-04 17:58:22 +0100 | [diff] [blame] | 108 | define_whitelist = r'^(GOOGLE_PROTO.*)|(PERFETTO_BUILD_WITH_ANDROID)|(ZLIB_.*)|(USE_MMAP)|(HAVE_HIDDEN)$' |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 109 | |
Logan Chien | 9bfaaf9 | 2018-02-13 18:49:24 +0800 | [diff] [blame] | 110 | # Shared libraries which are not in PDK. |
| 111 | library_not_in_pdk = { |
| 112 | 'libandroid', |
| 113 | 'libservices', |
| 114 | } |
| 115 | |
Florian Mayer | b6a921f | 2018-10-18 18:55:23 +0100 | [diff] [blame] | 116 | # Additional arguments to apply to Android.bp rules. |
| 117 | additional_args = { |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 118 | 'heapprofd_client': [ |
| 119 | ('include_dirs', ['bionic/libc']), |
Ryan Savitski | e65beca | 2019-01-29 18:29:13 +0000 | [diff] [blame] | 120 | ('static_libs', ['libasync_safe']), |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 121 | ], |
| 122 | 'traced_probes': [ |
Hector Dearman | 696ff77 | 2019-04-23 18:38:53 +0100 | [diff] [blame] | 123 | ('required', ['libperfetto_android_internal', 'trigger_perfetto']), |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 124 | ], |
| 125 | 'libperfetto_android_internal': [ |
| 126 | ('static_libs', ['libhealthhalutils']), |
Florian Mayer | b6a921f | 2018-10-18 18:55:23 +0100 | [diff] [blame] | 127 | ], |
| 128 | } |
| 129 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 130 | |
| 131 | def enable_gmock(module): |
| 132 | module.static_libs.append('libgmock') |
| 133 | |
| 134 | |
Hector Dearman | 3e712a0 | 2017-12-19 16:39:59 +0000 | [diff] [blame] | 135 | def enable_gtest_prod(module): |
| 136 | module.static_libs.append('libgtest_prod') |
| 137 | |
| 138 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 139 | def enable_gtest(module): |
| 140 | assert module.type == 'cc_test' |
| 141 | |
| 142 | |
| 143 | def enable_protobuf_full(module): |
| 144 | module.shared_libs.append('libprotobuf-cpp-full') |
| 145 | |
| 146 | |
| 147 | def enable_protobuf_lite(module): |
| 148 | module.shared_libs.append('libprotobuf-cpp-lite') |
| 149 | |
| 150 | |
| 151 | def enable_protoc_lib(module): |
| 152 | module.shared_libs.append('libprotoc') |
| 153 | |
Florian Mayer | a2fae26 | 2018-08-31 12:10:01 -0700 | [diff] [blame] | 154 | def enable_libunwindstack(module): |
| 155 | module.shared_libs.append('libunwindstack') |
| 156 | module.shared_libs.append('libprocinfo') |
| 157 | module.shared_libs.append('libbase') |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 158 | |
| 159 | def enable_libunwind(module): |
Sami Kyostila | fc074d4 | 2017-12-15 10:33:42 +0000 | [diff] [blame] | 160 | # libunwind is disabled on Darwin so we cannot depend on it. |
| 161 | pass |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 162 | |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 163 | def enable_sqlite(module): |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 164 | if module.type == 'cc_binary_host': |
| 165 | module.static_libs.append('libsqlite') |
| 166 | else: |
| 167 | # Copy what the sqlite3 command line tool does. |
| 168 | module.android.shared_libs.append('libsqlite') |
| 169 | module.android.shared_libs.append('libandroidicu') |
| 170 | module.android.shared_libs.append('liblog') |
| 171 | module.android.shared_libs.append('libutils') |
| 172 | module.host.static_libs.append('libsqlite') |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 173 | |
Hector Dearman | e0b993f | 2019-05-24 18:48:16 +0100 | [diff] [blame] | 174 | def enable_zlib(module): |
| 175 | module.shared_libs.append('libz') |
| 176 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 177 | # Android equivalents for third-party libraries that the upstream project |
| 178 | # depends on. |
| 179 | builtin_deps = { |
| 180 | '//buildtools:gmock': enable_gmock, |
| 181 | '//buildtools:gtest': enable_gtest, |
| 182 | '//buildtools:gtest_main': enable_gtest, |
| 183 | '//buildtools:libunwind': enable_libunwind, |
| 184 | '//buildtools:protobuf_full': enable_protobuf_full, |
| 185 | '//buildtools:protobuf_lite': enable_protobuf_lite, |
| 186 | '//buildtools:protoc_lib': enable_protoc_lib, |
Florian Mayer | a2fae26 | 2018-08-31 12:10:01 -0700 | [diff] [blame] | 187 | '//buildtools:libunwindstack': enable_libunwindstack, |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 188 | '//buildtools:sqlite': enable_sqlite, |
Hector Dearman | e0b993f | 2019-05-24 18:48:16 +0100 | [diff] [blame] | 189 | '//buildtools:zlib': enable_zlib, |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | # ---------------------------------------------------------------------------- |
| 193 | # End of configuration. |
| 194 | # ---------------------------------------------------------------------------- |
| 195 | |
| 196 | |
| 197 | class Error(Exception): |
| 198 | pass |
| 199 | |
| 200 | |
| 201 | class ThrowingArgumentParser(argparse.ArgumentParser): |
| 202 | def __init__(self, context): |
| 203 | super(ThrowingArgumentParser, self).__init__() |
| 204 | self.context = context |
| 205 | |
| 206 | def error(self, message): |
| 207 | raise Error('%s: %s' % (self.context, message)) |
| 208 | |
| 209 | |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 210 | def write_blueprint_key_value(output, name, value, sort=True): |
| 211 | """Writes an Blueprint key-value pair to the output""" |
| 212 | |
| 213 | if not value: |
| 214 | return |
| 215 | if isinstance(value, set): |
| 216 | value = sorted(value) |
| 217 | if isinstance(value, list): |
| 218 | output.append(' %s: [' % name) |
| 219 | for item in sorted(value) if sort else value: |
| 220 | output.append(' "%s",' % item) |
| 221 | output.append(' ],') |
| 222 | return |
| 223 | if isinstance(value, bool): |
| 224 | output.append(' %s: true,' % name) |
| 225 | return |
| 226 | if isinstance(value, Target): |
| 227 | value.to_string(output) |
| 228 | return |
| 229 | output.append(' %s: "%s",' % (name, value)) |
| 230 | |
| 231 | class Target(object): |
| 232 | """A target-scoped part of a module""" |
| 233 | |
| 234 | def __init__(self, name): |
| 235 | self.name = name |
| 236 | self.shared_libs = [] |
| 237 | self.static_libs = [] |
| 238 | self.cflags = set() |
| 239 | |
| 240 | def to_string(self, output): |
| 241 | nested_out = [] |
| 242 | self._output_field(nested_out, 'shared_libs') |
| 243 | self._output_field(nested_out, 'static_libs') |
| 244 | self._output_field(nested_out, 'cflags') |
| 245 | |
| 246 | if nested_out: |
| 247 | output.append(' %s: {' % self.name) |
| 248 | for line in nested_out: |
| 249 | output.append(' %s' % line) |
| 250 | output.append(' },') |
| 251 | |
| 252 | def _output_field(self, output, name, sort=True): |
| 253 | value = getattr(self, name) |
| 254 | return write_blueprint_key_value(output, name, value, sort) |
| 255 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 256 | class Module(object): |
| 257 | """A single module (e.g., cc_binary, cc_test) in a blueprint.""" |
| 258 | |
| 259 | def __init__(self, mod_type, name): |
| 260 | self.type = mod_type |
| 261 | self.name = name |
| 262 | self.srcs = [] |
| 263 | self.comment = None |
| 264 | self.shared_libs = [] |
| 265 | self.static_libs = [] |
| 266 | self.tools = [] |
| 267 | self.cmd = None |
Primiano Tucci | 6aa7557 | 2018-03-21 05:33:14 -0700 | [diff] [blame] | 268 | self.host_supported = False |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 269 | self.init_rc = [] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 270 | self.out = [] |
| 271 | self.export_include_dirs = [] |
| 272 | self.generated_headers = [] |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 273 | self.export_generated_headers = [] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 274 | self.defaults = [] |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 275 | self.cflags = set() |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 276 | self.local_include_dirs = [] |
Ryan Savitski | e65beca | 2019-01-29 18:29:13 +0000 | [diff] [blame] | 277 | self.include_dirs = [] |
| 278 | self.required = [] |
Lalit Maganti | d8b1a1d | 2018-05-23 14:41:43 +0100 | [diff] [blame] | 279 | self.user_debug_flag = False |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 280 | self.tool_files = None |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 281 | self.android = Target('android') |
| 282 | self.host = Target('host') |
Florian Mayer | 19f734f | 2019-07-05 12:08:01 +0100 | [diff] [blame^] | 283 | self.lto = None |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 284 | |
| 285 | def to_string(self, output): |
| 286 | if self.comment: |
| 287 | output.append('// %s' % self.comment) |
| 288 | output.append('%s {' % self.type) |
| 289 | self._output_field(output, 'name') |
| 290 | self._output_field(output, 'srcs') |
| 291 | self._output_field(output, 'shared_libs') |
| 292 | self._output_field(output, 'static_libs') |
| 293 | self._output_field(output, 'tools') |
| 294 | self._output_field(output, 'cmd', sort=False) |
Primiano Tucci | 6aa7557 | 2018-03-21 05:33:14 -0700 | [diff] [blame] | 295 | self._output_field(output, 'host_supported') |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 296 | self._output_field(output, 'init_rc') |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 297 | self._output_field(output, 'out') |
| 298 | self._output_field(output, 'export_include_dirs') |
| 299 | self._output_field(output, 'generated_headers') |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 300 | self._output_field(output, 'export_generated_headers') |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 301 | self._output_field(output, 'defaults') |
| 302 | self._output_field(output, 'cflags') |
| 303 | self._output_field(output, 'local_include_dirs') |
Ryan Savitski | e65beca | 2019-01-29 18:29:13 +0000 | [diff] [blame] | 304 | self._output_field(output, 'include_dirs') |
| 305 | self._output_field(output, 'required') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 306 | self._output_field(output, 'tool_files') |
Lalit Maganti | d8b1a1d | 2018-05-23 14:41:43 +0100 | [diff] [blame] | 307 | |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 308 | target_out = [] |
| 309 | self._output_field(target_out, 'android') |
| 310 | self._output_field(target_out, 'host') |
| 311 | if target_out: |
| 312 | output.append(' target: {') |
| 313 | for line in target_out: |
| 314 | output.append(' %s' % line) |
| 315 | output.append(' },') |
| 316 | |
Lalit Maganti | d8b1a1d | 2018-05-23 14:41:43 +0100 | [diff] [blame] | 317 | disable_pdk = any(name in library_not_in_pdk for name in self.shared_libs) |
| 318 | if self.user_debug_flag or disable_pdk: |
Logan Chien | 9bfaaf9 | 2018-02-13 18:49:24 +0800 | [diff] [blame] | 319 | output.append(' product_variables: {') |
Lalit Maganti | d8b1a1d | 2018-05-23 14:41:43 +0100 | [diff] [blame] | 320 | if disable_pdk: |
| 321 | output.append(' pdk: {') |
| 322 | output.append(' enabled: false,') |
| 323 | output.append(' },') |
| 324 | if self.user_debug_flag: |
| 325 | output.append(' debuggable: {') |
| 326 | output.append(' cflags: ["-DPERFETTO_BUILD_WITH_ANDROID_USERDEBUG"],') |
| 327 | output.append(' },') |
Logan Chien | 9bfaaf9 | 2018-02-13 18:49:24 +0800 | [diff] [blame] | 328 | output.append(' },') |
Florian Mayer | 19f734f | 2019-07-05 12:08:01 +0100 | [diff] [blame^] | 329 | if self.lto is not None: |
| 330 | output.append(' target: {') |
| 331 | output.append(' android: {') |
| 332 | output.append(' lto: {') |
| 333 | output.append(' thin: %s,' % 'true' if self.lto else 'false') |
| 334 | output.append(' },') |
| 335 | output.append(' },') |
| 336 | output.append(' },') |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 337 | output.append('}') |
| 338 | output.append('') |
| 339 | |
| 340 | def _output_field(self, output, name, sort=True): |
| 341 | value = getattr(self, name) |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 342 | return write_blueprint_key_value(output, name, value, sort) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 343 | |
| 344 | |
| 345 | class Blueprint(object): |
| 346 | """In-memory representation of an Android.bp file.""" |
| 347 | |
| 348 | def __init__(self): |
| 349 | self.modules = {} |
| 350 | |
| 351 | def add_module(self, module): |
| 352 | """Adds a new module to the blueprint, replacing any existing module |
| 353 | with the same name. |
| 354 | |
| 355 | Args: |
| 356 | module: Module instance. |
| 357 | """ |
| 358 | self.modules[module.name] = module |
| 359 | |
| 360 | def to_string(self, output): |
Sami Kyostila | ebba0fe | 2017-12-19 14:01:52 +0000 | [diff] [blame] | 361 | for m in sorted(self.modules.itervalues(), key=lambda m: m.name): |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 362 | m.to_string(output) |
| 363 | |
| 364 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 365 | def label_to_module_name(label): |
| 366 | """Turn a GN label (e.g., //:perfetto_tests) into a module name.""" |
Primiano Tucci | 4e49c02 | 2017-12-21 18:22:44 +0100 | [diff] [blame] | 367 | module = re.sub(r'^//:?', '', label) |
| 368 | module = re.sub(r'[^a-zA-Z0-9_]', '_', module) |
| 369 | if not module.startswith(module_prefix) and label not in default_targets: |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 370 | return module_prefix + module |
| 371 | return module |
| 372 | |
| 373 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 374 | def is_supported_source_file(name): |
| 375 | """Returns True if |name| can appear in a 'srcs' list.""" |
| 376 | return os.path.splitext(name)[1] in ['.c', '.cc', '.proto'] |
| 377 | |
| 378 | |
| 379 | def is_generated_by_action(desc, label): |
| 380 | """Checks if a label is generated by an action. |
| 381 | |
| 382 | Returns True if a GN output label |label| is an output for any action, |
| 383 | i.e., the file is generated dynamically. |
| 384 | """ |
| 385 | for target in desc.itervalues(): |
| 386 | if target['type'] == 'action' and label in target['outputs']: |
| 387 | return True |
| 388 | return False |
| 389 | |
| 390 | |
| 391 | def apply_module_dependency(blueprint, desc, module, dep_name): |
| 392 | """Recursively collect dependencies for a given module. |
| 393 | |
| 394 | Walk the transitive dependencies for a GN target and apply them to a given |
| 395 | module. This effectively flattens the dependency tree so that |module| |
| 396 | directly contains all the sources, libraries, etc. in the corresponding GN |
| 397 | dependency tree. |
| 398 | |
| 399 | Args: |
| 400 | blueprint: Blueprint instance which is being generated. |
| 401 | desc: JSON GN description. |
| 402 | module: Module to which dependencies should be added. |
| 403 | dep_name: GN target of the dependency. |
| 404 | """ |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 405 | # If the dependency refers to a library which we can replace with an Android |
| 406 | # equivalent, stop recursing and patch the dependency in. |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 407 | if gn_utils.label_without_toolchain(dep_name) in builtin_deps: |
| 408 | builtin_deps[gn_utils.label_without_toolchain(dep_name)](module) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 409 | return |
| 410 | |
| 411 | # Similarly some shared libraries are directly mapped to Android |
| 412 | # equivalents. |
| 413 | target = desc[dep_name] |
| 414 | for lib in target.get('libs', []): |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 415 | # Generally library names sould be mangled as 'libXXX', unless they are |
| 416 | # HAL libraries (e.g., android.hardware.health@2.0). |
| 417 | android_lib = lib if '@' in lib else 'lib' + lib |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 418 | if lib in library_whitelist and not android_lib in module.shared_libs: |
| 419 | module.shared_libs.append(android_lib) |
| 420 | |
| 421 | type = target['type'] |
| 422 | if type == 'action': |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 423 | if "gen_merged_sql_metrics" in dep_name: |
| 424 | dep_mod = create_merged_sql_metrics_target(blueprint, desc, dep_name) |
| 425 | module.generated_headers.append(dep_mod.name) |
| 426 | else: |
| 427 | create_modules_from_target(blueprint, desc, dep_name) |
| 428 | # Depend both on the generated sources and headers -- see |
| 429 | # make_genrules_for_action. |
| 430 | module.srcs.append(':' + label_to_module_name(dep_name)) |
| 431 | module.generated_headers.append( |
| 432 | label_to_module_name(dep_name) + '_headers') |
Sami Kyostila | ebba0fe | 2017-12-19 14:01:52 +0000 | [diff] [blame] | 433 | elif type == 'static_library' and label_to_module_name( |
| 434 | dep_name) != module.name: |
| 435 | create_modules_from_target(blueprint, desc, dep_name) |
| 436 | module.static_libs.append(label_to_module_name(dep_name)) |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 437 | elif type == 'shared_library' and label_to_module_name( |
| 438 | dep_name) != module.name: |
| 439 | module.shared_libs.append(label_to_module_name(dep_name)) |
Sami Kyostila | ebba0fe | 2017-12-19 14:01:52 +0000 | [diff] [blame] | 440 | elif type in ['group', 'source_set', 'executable', 'static_library' |
| 441 | ] and 'sources' in target: |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 442 | # Ignore source files that are generated by actions since they will be |
| 443 | # implicitly added by the genrule dependencies. |
| 444 | module.srcs.extend( |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 445 | gn_utils.label_to_path(src) for src in target['sources'] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 446 | if is_supported_source_file(src) |
| 447 | and not is_generated_by_action(desc, src)) |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 448 | module.cflags |= _get_cflags(target) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 449 | |
| 450 | |
| 451 | def make_genrules_for_action(blueprint, desc, target_name): |
| 452 | """Generate genrules for a GN action. |
| 453 | |
| 454 | GN actions are used to dynamically generate files during the build. The |
| 455 | Soong equivalent is a genrule. This function turns a specific kind of |
| 456 | genrule which turns .proto files into source and header files into a pair |
Sami Kyostila | 71625d7 | 2017-12-18 10:29:49 +0000 | [diff] [blame] | 457 | equivalent genrules. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 458 | |
| 459 | Args: |
| 460 | blueprint: Blueprint instance which is being generated. |
| 461 | desc: JSON GN description. |
| 462 | target_name: GN target for genrule generation. |
| 463 | |
| 464 | Returns: |
| 465 | A (source_genrule, header_genrule) module tuple. |
| 466 | """ |
| 467 | target = desc[target_name] |
| 468 | |
| 469 | # We only support genrules which call protoc (with or without a plugin) to |
| 470 | # turn .proto files into header and source files. |
| 471 | args = target['args'] |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 472 | if '/protoc' not in args[0]: |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 473 | raise Error('Unsupported action in target %s: %s' % (target_name, |
| 474 | target['args'])) |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 475 | parser = ThrowingArgumentParser('Action in target %s (%s)' % |
| 476 | (target_name, ' '.join(target['args']))) |
| 477 | parser.add_argument('--proto_path') |
| 478 | parser.add_argument('--cpp_out') |
| 479 | parser.add_argument('--plugin') |
| 480 | parser.add_argument('--plugin_out') |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 481 | parser.add_argument('--descriptor_set_out') |
Lalit Maganti | dfe69ca | 2018-10-30 12:18:23 +0000 | [diff] [blame] | 482 | parser.add_argument('--include_imports', action='store_true') |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 483 | parser.add_argument('protos', nargs=argparse.REMAINDER) |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 484 | args = parser.parse_args(args[2:]) |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 485 | |
| 486 | # Depending on whether we are using the default protoc C++ generator or the |
| 487 | # protozero plugin, the output dir is passed as: |
| 488 | # --cpp_out=gen/xxx or |
| 489 | # --plugin_out=:gen/xxx or |
| 490 | # --plugin_out=wrapper_namespace=pbzero:gen/xxx |
| 491 | gen_dir = args.cpp_out if args.cpp_out else args.plugin_out.split(':')[1] |
| 492 | assert gen_dir.startswith('gen/') |
| 493 | gen_dir = gen_dir[4:] |
| 494 | cpp_out_dir = ('$(genDir)/%s/%s' % (tree_path, gen_dir)).rstrip('/') |
| 495 | |
| 496 | # TODO(skyostil): Is there a way to avoid hardcoding the tree path here? |
| 497 | # TODO(skyostil): Find a way to avoid creating the directory. |
| 498 | cmd = [ |
| 499 | 'mkdir -p %s &&' % cpp_out_dir, |
| 500 | '$(location aprotoc)', |
| 501 | '--cpp_out=%s' % cpp_out_dir |
| 502 | ] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 503 | |
| 504 | # We create two genrules for each action: one for the protobuf headers and |
| 505 | # another for the sources. This is because the module that depends on the |
| 506 | # generated files needs to declare two different types of dependencies -- |
| 507 | # source files in 'srcs' and headers in 'generated_headers' -- and it's not |
| 508 | # valid to generate .h files from a source dependency and vice versa. |
Sami Kyostila | 71625d7 | 2017-12-18 10:29:49 +0000 | [diff] [blame] | 509 | source_module = Module('genrule', label_to_module_name(target_name)) |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 510 | source_module.srcs.extend( |
| 511 | gn_utils.label_to_path(src) for src in target['sources']) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 512 | source_module.tools = ['aprotoc'] |
| 513 | |
Sami Kyostila | 71625d7 | 2017-12-18 10:29:49 +0000 | [diff] [blame] | 514 | header_module = Module('genrule', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 515 | label_to_module_name(target_name) + '_headers') |
| 516 | header_module.srcs = source_module.srcs[:] |
| 517 | header_module.tools = source_module.tools[:] |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 518 | header_module.export_include_dirs = [gen_dir or '.'] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 519 | |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 520 | # In GN builds the proto path is always relative to the output directory |
| 521 | # (out/tmp.xxx). |
| 522 | assert args.proto_path.startswith('../../') |
| 523 | cmd += [ '--proto_path=%s/%s' % (tree_path, args.proto_path[6:])] |
| 524 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 525 | namespaces = ['pb'] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 526 | if args.plugin: |
| 527 | _, plugin = os.path.split(args.plugin) |
| 528 | # TODO(skyostil): Can we detect this some other way? |
| 529 | if plugin == 'ipc_plugin': |
| 530 | namespaces.append('ipc') |
Primiano Tucci | 764c504 | 2019-06-22 18:28:45 +0100 | [diff] [blame] | 531 | elif plugin == 'protozero_plugin': |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 532 | namespaces = ['pbzero'] |
| 533 | for dep in target['deps']: |
| 534 | if desc[dep]['type'] != 'executable': |
| 535 | continue |
| 536 | _, executable = os.path.split(desc[dep]['outputs'][0]) |
| 537 | if executable == plugin: |
| 538 | cmd += [ |
| 539 | '--plugin=protoc-gen-plugin=$(location %s)' % |
| 540 | label_to_module_name(dep) |
| 541 | ] |
| 542 | source_module.tools.append(label_to_module_name(dep)) |
| 543 | # Also make sure the module for the tool is generated. |
| 544 | create_modules_from_target(blueprint, desc, dep) |
| 545 | break |
| 546 | else: |
| 547 | raise Error('Unrecognized protoc plugin in target %s: %s' % |
| 548 | (target_name, args[i])) |
| 549 | if args.plugin_out: |
| 550 | plugin_args = args.plugin_out.split(':')[0] |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 551 | cmd += ['--plugin_out=%s:%s' % (plugin_args, cpp_out_dir)] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 552 | |
| 553 | cmd += ['$(in)'] |
| 554 | source_module.cmd = ' '.join(cmd) |
| 555 | header_module.cmd = source_module.cmd |
| 556 | header_module.tools = source_module.tools[:] |
| 557 | |
| 558 | for ns in namespaces: |
| 559 | source_module.out += [ |
| 560 | '%s/%s' % (tree_path, src.replace('.proto', '.%s.cc' % ns)) |
| 561 | for src in source_module.srcs |
| 562 | ] |
| 563 | header_module.out += [ |
| 564 | '%s/%s' % (tree_path, src.replace('.proto', '.%s.h' % ns)) |
| 565 | for src in header_module.srcs |
| 566 | ] |
| 567 | return source_module, header_module |
| 568 | |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 569 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 570 | def create_merged_sql_metrics_target(blueprint, desc, gn_target_name): |
| 571 | target_desc = desc[gn_target_name] |
| 572 | module = Module( |
| 573 | 'genrule', |
| 574 | 'gen_merged_sql_metrics', |
| 575 | ) |
| 576 | module.tool_files = [ |
Lalit Maganti | 697cc48 | 2019-05-01 14:39:11 +0100 | [diff] [blame] | 577 | 'tools/gen_merged_sql_metrics.py', |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 578 | ] |
Lalit Maganti | 697cc48 | 2019-05-01 14:39:11 +0100 | [diff] [blame] | 579 | module.cmd = ' '.join([ |
| 580 | '$(location tools/gen_merged_sql_metrics.py)', |
| 581 | '--cpp_out=$(out)', |
| 582 | '$(in)', |
| 583 | ]) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 584 | module.out = set( |
| 585 | src[src.index('gen/') + len('gen/'):] |
| 586 | for src in target_desc.get('outputs', []) |
| 587 | ) |
| 588 | module.srcs.extend( |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 589 | gn_utils.label_to_path(src) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 590 | for src in target_desc.get('inputs', []) |
| 591 | ) |
| 592 | blueprint.add_module(module) |
| 593 | return module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 594 | |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 595 | |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 596 | def _get_cflags(target): |
| 597 | cflags = set(flag for flag in target.get('cflags', []) |
| 598 | if re.match(cflag_whitelist, flag)) |
| 599 | cflags |= set("-D%s" % define for define in target.get('defines', []) |
| 600 | if re.match(define_whitelist, define)) |
| 601 | return cflags |
| 602 | |
| 603 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 604 | def create_modules_from_target(blueprint, desc, target_name): |
| 605 | """Generate module(s) for a given GN target. |
| 606 | |
| 607 | Given a GN target name, generate one or more corresponding modules into a |
| 608 | blueprint. |
| 609 | |
| 610 | Args: |
| 611 | blueprint: Blueprint instance which is being generated. |
| 612 | desc: JSON GN description. |
| 613 | target_name: GN target for module generation. |
| 614 | """ |
| 615 | target = desc[target_name] |
| 616 | if target['type'] == 'executable': |
Primiano Tucci | 21c19d8 | 2018-03-29 12:35:08 +0100 | [diff] [blame] | 617 | if 'host' in target['toolchain'] or target_name in target_host_only: |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 618 | module_type = 'cc_binary_host' |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 619 | elif target.get('testonly') and target_name not in non_test_binaries: |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 620 | module_type = 'cc_test' |
| 621 | else: |
| 622 | module_type = 'cc_binary' |
| 623 | modules = [Module(module_type, label_to_module_name(target_name))] |
| 624 | elif target['type'] == 'action': |
| 625 | modules = make_genrules_for_action(blueprint, desc, target_name) |
Sami Kyostila | ebba0fe | 2017-12-19 14:01:52 +0000 | [diff] [blame] | 626 | elif target['type'] == 'static_library': |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 627 | module = Module('cc_library_static', label_to_module_name(target_name)) |
| 628 | module.export_include_dirs = ['include'] |
| 629 | modules = [module] |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 630 | elif target['type'] == 'shared_library': |
| 631 | modules = [ |
| 632 | Module('cc_library_shared', label_to_module_name(target_name)) |
| 633 | ] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 634 | else: |
| 635 | raise Error('Unknown target type: %s' % target['type']) |
| 636 | |
| 637 | for module in modules: |
| 638 | module.comment = 'GN target: %s' % target_name |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 639 | if target_name in target_initrc: |
| 640 | module.init_rc = [target_initrc[target_name]] |
Primiano Tucci | 6aa7557 | 2018-03-21 05:33:14 -0700 | [diff] [blame] | 641 | if target_name in target_host_supported: |
| 642 | module.host_supported = True |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 643 | |
Sami Kyostila | ebba0fe | 2017-12-19 14:01:52 +0000 | [diff] [blame] | 644 | # Don't try to inject library/source dependencies into genrules because |
| 645 | # they are not compiled in the traditional sense. |
Sami Kyostila | 71625d7 | 2017-12-18 10:29:49 +0000 | [diff] [blame] | 646 | if module.type != 'genrule': |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 647 | module.defaults = [defaults_module] |
Sami Kyostila | ebba0fe | 2017-12-19 14:01:52 +0000 | [diff] [blame] | 648 | apply_module_dependency(blueprint, desc, module, target_name) |
| 649 | for dep in resolve_dependencies(desc, target_name): |
| 650 | apply_module_dependency(blueprint, desc, module, dep) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 651 | |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 652 | # If the module is a static library, export all the generated headers. |
| 653 | if module.type == 'cc_library_static': |
| 654 | module.export_generated_headers = module.generated_headers |
| 655 | |
Ryan Savitski | e65beca | 2019-01-29 18:29:13 +0000 | [diff] [blame] | 656 | # Merge in additional hardcoded arguments. |
| 657 | for key, add_val in additional_args.get(module.name, []): |
| 658 | curr = getattr(module, key) |
| 659 | if add_val and isinstance(add_val, list) and isinstance(curr, list): |
| 660 | curr.extend(add_val) |
| 661 | else: |
| 662 | raise Error('Unimplemented type of additional_args') |
| 663 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 664 | blueprint.add_module(module) |
| 665 | |
| 666 | |
| 667 | def resolve_dependencies(desc, target_name): |
| 668 | """Return the transitive set of dependent-on targets for a GN target. |
| 669 | |
| 670 | Args: |
| 671 | blueprint: Blueprint instance which is being generated. |
| 672 | desc: JSON GN description. |
| 673 | |
| 674 | Returns: |
| 675 | A set of transitive dependencies in the form of GN targets. |
| 676 | """ |
| 677 | |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 678 | if gn_utils.label_without_toolchain(target_name) in builtin_deps: |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 679 | return set() |
| 680 | target = desc[target_name] |
| 681 | resolved_deps = set() |
| 682 | for dep in target.get('deps', []): |
| 683 | resolved_deps.add(dep) |
| 684 | # Ignore the transitive dependencies of actions because they are |
| 685 | # explicitly converted to genrules. |
| 686 | if desc[dep]['type'] == 'action': |
| 687 | continue |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 688 | # Dependencies on shared libraries shouldn't propagate any transitive |
| 689 | # dependencies but only depend on the shared library target |
| 690 | if desc[dep]['type'] == 'shared_library': |
| 691 | continue |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 692 | resolved_deps.update(resolve_dependencies(desc, dep)) |
| 693 | return resolved_deps |
| 694 | |
| 695 | |
Ryan Savitski | 160b582 | 2019-02-28 14:42:50 +0000 | [diff] [blame] | 696 | def create_blueprint_for_targets(desc, targets): |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 697 | """Generate a blueprint for a list of GN targets.""" |
| 698 | blueprint = Blueprint() |
| 699 | |
| 700 | # Default settings used by all modules. |
| 701 | defaults = Module('cc_defaults', defaults_module) |
| 702 | defaults.local_include_dirs = ['include'] |
| 703 | defaults.cflags = [ |
| 704 | '-Wno-error=return-type', |
| 705 | '-Wno-sign-compare', |
| 706 | '-Wno-sign-promo', |
| 707 | '-Wno-unused-parameter', |
Florian Mayer | cc424fd | 2018-01-15 11:19:01 +0000 | [diff] [blame] | 708 | '-fvisibility=hidden', |
Florian Mayer | c2a38ea | 2018-01-19 11:48:43 +0000 | [diff] [blame] | 709 | '-Oz', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 710 | ] |
Lalit Maganti | d8b1a1d | 2018-05-23 14:41:43 +0100 | [diff] [blame] | 711 | defaults.user_debug_flag = True |
Florian Mayer | 19f734f | 2019-07-05 12:08:01 +0100 | [diff] [blame^] | 712 | defaults.lto = True |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 713 | |
| 714 | blueprint.add_module(defaults) |
| 715 | for target in targets: |
| 716 | create_modules_from_target(blueprint, desc, target) |
| 717 | return blueprint |
| 718 | |
| 719 | |
| 720 | def main(): |
| 721 | parser = argparse.ArgumentParser( |
| 722 | description='Generate Android.bp from a GN description.') |
| 723 | parser.add_argument( |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 724 | '--desc', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 725 | help= |
| 726 | 'GN description (e.g., gn desc out --format=json --all-toolchains "//*"' |
| 727 | ) |
| 728 | parser.add_argument( |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 729 | '--extras', |
| 730 | help='Extra targets to include at the end of the Blueprint file', |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 731 | default=os.path.join(gn_utils.repo_root(), 'Android.bp.extras'), |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 732 | ) |
| 733 | parser.add_argument( |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 734 | '--output', |
| 735 | help='Blueprint file to create', |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 736 | default=os.path.join(gn_utils.repo_root(), 'Android.bp'), |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 737 | ) |
| 738 | parser.add_argument( |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 739 | 'targets', |
| 740 | nargs=argparse.REMAINDER, |
| 741 | help='Targets to include in the blueprint (e.g., "//:perfetto_tests")') |
| 742 | args = parser.parse_args() |
| 743 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 744 | if args.desc: |
| 745 | with open(args.desc) as f: |
| 746 | desc = json.load(f) |
| 747 | else: |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 748 | desc = gn_utils.create_build_description(gn_args) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 749 | |
Ryan Savitski | 160b582 | 2019-02-28 14:42:50 +0000 | [diff] [blame] | 750 | blueprint = create_blueprint_for_targets(desc, args.targets or |
| 751 | default_targets) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 752 | output = [ |
| 753 | """// Copyright (C) 2017 The Android Open Source Project |
| 754 | // |
| 755 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 756 | // you may not use this file except in compliance with the License. |
| 757 | // You may obtain a copy of the License at |
| 758 | // |
| 759 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 760 | // |
| 761 | // Unless required by applicable law or agreed to in writing, software |
| 762 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 763 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 764 | // See the License for the specific language governing permissions and |
| 765 | // limitations under the License. |
| 766 | // |
| 767 | // This file is automatically generated by %s. Do not edit. |
| 768 | """ % (__file__) |
| 769 | ] |
| 770 | blueprint.to_string(output) |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 771 | with open(args.extras, 'r') as r: |
| 772 | for line in r: |
| 773 | output.append(line.rstrip("\n\r")) |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 774 | with open(args.output, 'w') as f: |
| 775 | f.write('\n'.join(output)) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 776 | |
| 777 | |
| 778 | if __name__ == '__main__': |
| 779 | sys.exit(main()) |