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 | |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 36 | from compat import itervalues |
| 37 | |
Florian Mayer | 246c142 | 2019-09-18 15:40:38 +0100 | [diff] [blame] | 38 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 39 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 40 | # Arguments for the GN output directory. |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 41 | gn_args = ' '.join([ |
| 42 | 'is_debug=false', |
Primiano Tucci | 7e05fc1 | 2019-08-27 17:29:47 +0200 | [diff] [blame] | 43 | 'is_perfetto_build_generator=true', |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 44 | 'perfetto_build_with_android=true', |
| 45 | 'target_cpu="arm"', |
| 46 | 'target_os="android"', |
| 47 | ]) |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 48 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 49 | # Default targets to translate to the blueprint file. |
| 50 | default_targets = [ |
| 51 | '//:libperfetto_client_experimental', |
| 52 | '//:libperfetto', |
| 53 | '//:perfetto_integrationtests', |
| 54 | '//:perfetto_unittests', |
| 55 | '//protos/perfetto/trace:perfetto_trace_protos', |
| 56 | '//src/android_internal:libperfetto_android_internal', |
| 57 | '//src/perfetto_cmd:perfetto', |
| 58 | '//src/perfetto_cmd:trigger_perfetto', |
| 59 | '//src/profiling/memory:heapprofd_client', |
| 60 | '//src/profiling/memory:heapprofd', |
Ryan Savitski | 462b5db | 2019-11-20 19:06:46 +0000 | [diff] [blame] | 61 | '//src/profiling/perf:traced_perf', |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 62 | '//src/traced/probes:traced_probes', |
| 63 | '//src/traced/service:traced', |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame] | 64 | '//test/cts:perfetto_cts_deps', |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 65 | '//test/cts:perfetto_cts_jni_deps', |
Primiano Tucci | cbbe480 | 2020-02-20 13:19:11 +0000 | [diff] [blame] | 66 | '//test:perfetto_gtest_logcat_printer', |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 67 | ] |
| 68 | |
| 69 | # Host targets |
| 70 | ipc_plugin = '//src/ipc/protoc_plugin:ipc_plugin(%s)' % gn_utils.HOST_TOOLCHAIN |
| 71 | protozero_plugin = '//src/protozero/protoc_plugin:protozero_plugin(%s)' % ( |
| 72 | gn_utils.HOST_TOOLCHAIN) |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 73 | cppgen_plugin = '//src/protozero/protoc_plugin:cppgen_plugin(%s)' % ( |
| 74 | gn_utils.HOST_TOOLCHAIN) |
| 75 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 76 | default_targets += [ |
| 77 | '//src/trace_processor:trace_processor_shell(%s)' % gn_utils.HOST_TOOLCHAIN, |
| 78 | '//tools/trace_to_text:trace_to_text(%s)' % gn_utils.HOST_TOOLCHAIN, |
| 79 | protozero_plugin, |
| 80 | ipc_plugin, |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 81 | ] |
| 82 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 83 | # Defines a custom init_rc argument to be applied to the corresponding output |
| 84 | # blueprint target. |
| 85 | target_initrc = { |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 86 | '//src/traced/service:traced': {'perfetto.rc'}, |
| 87 | '//src/profiling/memory:heapprofd': {'heapprofd.rc'}, |
Ryan Savitski | 29082bf | 2020-02-12 15:13:51 +0000 | [diff] [blame] | 88 | '//src/profiling/perf:traced_perf': {'traced_perf.rc'}, |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | target_host_supported = [ |
| 92 | '//protos/perfetto/trace:perfetto_trace_protos', |
Hector Dearman | 04cfac7 | 2019-09-24 22:05:55 +0100 | [diff] [blame] | 93 | '//:libperfetto', |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 94 | ] |
| 95 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 96 | # All module names are prefixed with this string to avoid collisions. |
| 97 | module_prefix = 'perfetto_' |
| 98 | |
| 99 | # Shared libraries which are directly translated to Android system equivalents. |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 100 | shared_library_whitelist = [ |
Hector Dearman | 64f2e05 | 2019-12-04 20:51:14 +0000 | [diff] [blame] | 101 | 'android', |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 102 | 'android.hardware.atrace@1.0', |
| 103 | 'android.hardware.health@2.0', |
| 104 | 'android.hardware.power.stats@1.0', |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 105 | 'base', |
Sami Kyostila | b5b7169 | 2018-01-12 12:16:44 +0000 | [diff] [blame] | 106 | 'binder', |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 107 | 'cutils', |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 108 | 'hidlbase', |
| 109 | 'hidltransport', |
| 110 | 'hwbinder', |
Ryan Savitski | 53ca60b | 2019-06-03 13:04:40 +0100 | [diff] [blame] | 111 | 'incident', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 112 | 'log', |
Sami Kyostila | b5b7169 | 2018-01-12 12:16:44 +0000 | [diff] [blame] | 113 | 'services', |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 114 | 'statssocket', |
Primiano Tucci | edf099c | 2018-01-08 18:27:56 +0000 | [diff] [blame] | 115 | 'utils', |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 116 | ] |
| 117 | |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 118 | # Static libraries which are directly translated to Android system equivalents. |
| 119 | static_library_whitelist = [ |
| 120 | 'statslog_perfetto', |
| 121 | ] |
| 122 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 123 | # Name of the module which settings such as compiler flags for all other |
| 124 | # modules. |
| 125 | defaults_module = module_prefix + 'defaults' |
| 126 | |
| 127 | # Location of the project in the Android source tree. |
| 128 | tree_path = 'external/perfetto' |
| 129 | |
Primiano Tucci | edf099c | 2018-01-08 18:27:56 +0000 | [diff] [blame] | 130 | # Compiler flags which are passed through to the blueprint. |
| 131 | cflag_whitelist = r'^-DPERFETTO.*$' |
| 132 | |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 133 | # Compiler defines which are passed through to the blueprint. |
Primiano Tucci | 8e62744 | 2019-08-28 07:58:38 +0200 | [diff] [blame] | 134 | define_whitelist = r'^(GOOGLE_PROTO.*)|(ZLIB_.*)|(USE_MMAP)|(HAVE_HIDDEN)$' |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 135 | |
Logan Chien | 9bfaaf9 | 2018-02-13 18:49:24 +0800 | [diff] [blame] | 136 | # Shared libraries which are not in PDK. |
| 137 | library_not_in_pdk = { |
| 138 | 'libandroid', |
| 139 | 'libservices', |
| 140 | } |
| 141 | |
Primiano Tucci | 8e62744 | 2019-08-28 07:58:38 +0200 | [diff] [blame] | 142 | # The directory where the generated perfetto_build_flags.h will be copied into. |
| 143 | buildflags_dir = 'include/perfetto/base/build_configs/android_tree' |
| 144 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 145 | |
| 146 | def enumerate_data_deps(): |
| 147 | with open(os.path.join(ROOT_DIR, 'tools', 'test_data.txt')) as f: |
| 148 | lines = f.readlines() |
| 149 | for line in (line.strip() for line in lines if not line.startswith('#')): |
Andrew Shulaev | 576054d | 2020-01-23 13:44:51 +0000 | [diff] [blame] | 150 | assert os.path.exists(line), 'file %s should exist' % line |
Primiano Tucci | 0269116 | 2020-01-21 13:30:13 +0000 | [diff] [blame] | 151 | if line.startswith('test/data/'): |
| 152 | # Skip test data files that require GCS. They are only for benchmarks. |
| 153 | # We don't run benchmarks in the android tree. |
| 154 | continue |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 155 | if line.endswith('/'): |
| 156 | yield line + '**/*' |
| 157 | else: |
| 158 | yield line |
| 159 | |
| 160 | |
Florian Mayer | b6a921f | 2018-10-18 18:55:23 +0100 | [diff] [blame] | 161 | # Additional arguments to apply to Android.bp rules. |
| 162 | additional_args = { |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 163 | 'heapprofd_client': [ |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 164 | ('include_dirs', {'bionic/libc'}), |
| 165 | ('static_libs', {'libasync_safe'}), |
| 166 | ('header_libs', {'bionic_libc_platform_headers'}), |
Primiano Tucci | 676f0cc | 2018-12-03 20:03:26 +0100 | [diff] [blame] | 167 | ], |
Ryan Savitski | 703bcab | 2019-12-18 14:38:14 +0000 | [diff] [blame] | 168 | 'perfetto_unittests': [ |
| 169 | ('data', set(enumerate_data_deps())), |
| 170 | ('include_dirs', {'bionic/libc/kernel'}), |
| 171 | ], |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 172 | 'traced_probes': [ |
Ryan Savitski | 29082bf | 2020-02-12 15:13:51 +0000 | [diff] [blame] | 173 | ('required', {'libperfetto_android_internal', |
| 174 | 'trigger_perfetto', |
| 175 | 'traced_perf'}), |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 176 | ], |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 177 | 'libperfetto_android_internal': [('static_libs', {'libhealthhalutils'}),], |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 178 | 'trace_processor_shell': [ |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 179 | ('stl', 'libc++_static'), |
| 180 | ], |
Jiyong Park | d5ea011 | 2020-04-28 18:22:00 +0900 | [diff] [blame^] | 181 | 'libperfetto_client_experimental': [ |
| 182 | ('apex_available', { |
| 183 | '//apex_available:platform', |
| 184 | 'com.android.art.debug', |
| 185 | 'com.android.art.release'}), |
| 186 | ], |
| 187 | 'perfetto_trace_protos': [ |
| 188 | ('apex_available', { |
| 189 | '//apex_available:platform', |
| 190 | 'com.android.art.debug', |
| 191 | 'com.android.art.release'}), |
| 192 | ], |
Florian Mayer | b6a921f | 2018-10-18 18:55:23 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 195 | |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame] | 196 | def enable_gtest_and_gmock(module): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 197 | module.static_libs.add('libgmock') |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame] | 198 | module.static_libs.add('libgtest') |
Primiano Tucci | cbbe480 | 2020-02-20 13:19:11 +0000 | [diff] [blame] | 199 | if module.name != 'perfetto_gtest_logcat_printer': |
| 200 | module.whole_static_libs.add('perfetto_gtest_logcat_printer') |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 201 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 202 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 203 | def enable_protobuf_full(module): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 204 | module.shared_libs.add('libprotobuf-cpp-full') |
| 205 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 206 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 207 | def enable_protobuf_lite(module): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 208 | module.shared_libs.add('libprotobuf-cpp-lite') |
| 209 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 210 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 211 | def enable_protoc_lib(module): |
Lalit Maganti | 3d415ec | 2019-10-23 17:53:17 +0100 | [diff] [blame] | 212 | if module.type == 'cc_binary_host': |
| 213 | module.static_libs.add('libprotoc') |
| 214 | else: |
| 215 | module.shared_libs.add('libprotoc') |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 216 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 217 | |
Florian Mayer | a2fae26 | 2018-08-31 12:10:01 -0700 | [diff] [blame] | 218 | def enable_libunwindstack(module): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 219 | module.shared_libs.add('libunwindstack') |
| 220 | module.shared_libs.add('libprocinfo') |
| 221 | module.shared_libs.add('libbase') |
| 222 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 223 | |
| 224 | def enable_libunwind(module): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 225 | # libunwind is disabled on Darwin so we cannot depend on it. |
| 226 | pass |
| 227 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 228 | |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 229 | def enable_sqlite(module): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 230 | if module.type == 'cc_binary_host': |
| 231 | module.static_libs.add('libsqlite') |
| 232 | else: |
| 233 | # Copy what the sqlite3 command line tool does. |
| 234 | module.android.shared_libs.add('libsqlite') |
| 235 | module.android.shared_libs.add('libandroidicu') |
| 236 | module.android.shared_libs.add('liblog') |
| 237 | module.android.shared_libs.add('libutils') |
| 238 | module.host.static_libs.add('libsqlite') |
| 239 | |
Lalit Maganti | 17aa273 | 2019-02-08 15:47:26 +0000 | [diff] [blame] | 240 | |
Hector Dearman | e0b993f | 2019-05-24 18:48:16 +0100 | [diff] [blame] | 241 | def enable_zlib(module): |
Lalit Maganti | 3d415ec | 2019-10-23 17:53:17 +0100 | [diff] [blame] | 242 | if module.type == 'cc_binary_host': |
| 243 | module.static_libs.add('libz') |
| 244 | else: |
| 245 | module.shared_libs.add('libz') |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 246 | |
Hector Dearman | e0b993f | 2019-05-24 18:48:16 +0100 | [diff] [blame] | 247 | |
Ryan Savitski | 56bc0c6 | 2020-01-27 13:50:02 +0000 | [diff] [blame] | 248 | def enable_uapi_headers(module): |
| 249 | module.include_dirs.add('bionic/libc/kernel') |
| 250 | |
| 251 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 252 | # Android equivalents for third-party libraries that the upstream project |
| 253 | # depends on. |
| 254 | builtin_deps = { |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 255 | '//gn:default_deps': lambda x: None, |
| 256 | '//gn:gtest_main': lambda x: None, |
| 257 | '//gn:protoc': lambda x: None, |
Primiano Tucci | fbf4a73 | 2019-12-11 00:32:15 +0000 | [diff] [blame] | 258 | '//gn:gtest_and_gmock': enable_gtest_and_gmock, |
Primiano Tucci | b7ebffd | 2019-09-09 07:42:35 -0700 | [diff] [blame] | 259 | '//gn:libunwind': enable_libunwind, |
| 260 | '//gn:protobuf_full': enable_protobuf_full, |
| 261 | '//gn:protobuf_lite': enable_protobuf_lite, |
| 262 | '//gn:protoc_lib': enable_protoc_lib, |
| 263 | '//gn:libunwindstack': enable_libunwindstack, |
| 264 | '//gn:sqlite': enable_sqlite, |
| 265 | '//gn:zlib': enable_zlib, |
Ryan Savitski | 56bc0c6 | 2020-01-27 13:50:02 +0000 | [diff] [blame] | 266 | '//gn:bionic_kernel_uapi_headers' : enable_uapi_headers, |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | # ---------------------------------------------------------------------------- |
| 270 | # End of configuration. |
| 271 | # ---------------------------------------------------------------------------- |
| 272 | |
| 273 | |
| 274 | class Error(Exception): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 275 | pass |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 276 | |
| 277 | |
| 278 | class ThrowingArgumentParser(argparse.ArgumentParser): |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 279 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 280 | def __init__(self, context): |
| 281 | super(ThrowingArgumentParser, self).__init__() |
| 282 | self.context = context |
| 283 | |
| 284 | def error(self, message): |
| 285 | raise Error('%s: %s' % (self.context, message)) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 286 | |
| 287 | |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 288 | def write_blueprint_key_value(output, name, value, sort=True): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 289 | """Writes a Blueprint key-value pair to the output""" |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 290 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 291 | if not value: |
| 292 | return |
| 293 | if isinstance(value, set): |
| 294 | value = sorted(value) |
| 295 | if isinstance(value, list): |
| 296 | output.append(' %s: [' % name) |
| 297 | for item in sorted(value) if sort else value: |
| 298 | output.append(' "%s",' % item) |
| 299 | output.append(' ],') |
| 300 | return |
| 301 | if isinstance(value, bool): |
| 302 | output.append(' %s: true,' % name) |
| 303 | return |
| 304 | if isinstance(value, Target): |
| 305 | value.to_string(output) |
| 306 | return |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 307 | if isinstance(value, dict): |
| 308 | kv_output = [] |
| 309 | for k, v in value.items(): |
| 310 | write_blueprint_key_value(kv_output, k, v) |
| 311 | |
| 312 | output.append(' %s: {' % name) |
| 313 | for line in kv_output: |
| 314 | output.append(' %s' % line) |
| 315 | output.append(' },') |
| 316 | return |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 317 | output.append(' %s: "%s",' % (name, value)) |
| 318 | |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 319 | |
| 320 | class Target(object): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 321 | """A target-scoped part of a module""" |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 322 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 323 | def __init__(self, name): |
| 324 | self.name = name |
| 325 | self.shared_libs = set() |
| 326 | self.static_libs = set() |
Primiano Tucci | cbbe480 | 2020-02-20 13:19:11 +0000 | [diff] [blame] | 327 | self.whole_static_libs = set() |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 328 | self.cflags = set() |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 329 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 330 | def to_string(self, output): |
| 331 | nested_out = [] |
| 332 | self._output_field(nested_out, 'shared_libs') |
| 333 | self._output_field(nested_out, 'static_libs') |
Primiano Tucci | cbbe480 | 2020-02-20 13:19:11 +0000 | [diff] [blame] | 334 | self._output_field(nested_out, 'whole_static_libs') |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 335 | self._output_field(nested_out, 'cflags') |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 336 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 337 | if nested_out: |
| 338 | output.append(' %s: {' % self.name) |
| 339 | for line in nested_out: |
| 340 | output.append(' %s' % line) |
| 341 | output.append(' },') |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 342 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 343 | def _output_field(self, output, name, sort=True): |
| 344 | value = getattr(self, name) |
| 345 | return write_blueprint_key_value(output, name, value, sort) |
| 346 | |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 347 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 348 | class Module(object): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 349 | """A single module (e.g., cc_binary, cc_test) in a blueprint.""" |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 350 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 351 | def __init__(self, mod_type, name, gn_target): |
| 352 | self.type = mod_type |
| 353 | self.gn_target = gn_target |
| 354 | self.name = name |
| 355 | self.srcs = set() |
| 356 | self.comment = 'GN: ' + gn_utils.label_without_toolchain(gn_target) |
| 357 | self.shared_libs = set() |
| 358 | self.static_libs = set() |
Primiano Tucci | cbbe480 | 2020-02-20 13:19:11 +0000 | [diff] [blame] | 359 | self.whole_static_libs = set() |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 360 | self.tools = set() |
| 361 | self.cmd = None |
| 362 | self.host_supported = False |
| 363 | self.init_rc = set() |
| 364 | self.out = set() |
| 365 | self.export_include_dirs = set() |
| 366 | self.generated_headers = set() |
| 367 | self.export_generated_headers = set() |
| 368 | self.defaults = set() |
| 369 | self.cflags = set() |
| 370 | self.include_dirs = set() |
| 371 | self.header_libs = set() |
| 372 | self.required = set() |
| 373 | self.user_debug_flag = False |
| 374 | self.tool_files = None |
| 375 | self.android = Target('android') |
| 376 | self.host = Target('host') |
| 377 | self.lto = None |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 378 | self.stl = None |
| 379 | self.dist = dict() |
Lalit Maganti | accd64b | 2020-03-16 19:54:10 +0000 | [diff] [blame] | 380 | self.strip = dict() |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 381 | self.data = set() |
Jiyong Park | d5ea011 | 2020-04-28 18:22:00 +0900 | [diff] [blame^] | 382 | self.apex_available = set() |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 383 | # The genrule_XXX below are properties that must to be propagated back |
| 384 | # on the module(s) that depend on the genrule. |
| 385 | self.genrule_headers = set() |
| 386 | self.genrule_srcs = set() |
| 387 | self.genrule_shared_libs = set() |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 388 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 389 | def to_string(self, output): |
| 390 | if self.comment: |
| 391 | output.append('// %s' % self.comment) |
| 392 | output.append('%s {' % self.type) |
| 393 | self._output_field(output, 'name') |
| 394 | self._output_field(output, 'srcs') |
| 395 | self._output_field(output, 'shared_libs') |
| 396 | self._output_field(output, 'static_libs') |
Primiano Tucci | cbbe480 | 2020-02-20 13:19:11 +0000 | [diff] [blame] | 397 | self._output_field(output, 'whole_static_libs') |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 398 | self._output_field(output, 'tools') |
| 399 | self._output_field(output, 'cmd', sort=False) |
| 400 | self._output_field(output, 'host_supported') |
| 401 | self._output_field(output, 'init_rc') |
| 402 | self._output_field(output, 'out') |
| 403 | self._output_field(output, 'export_include_dirs') |
| 404 | self._output_field(output, 'generated_headers') |
| 405 | self._output_field(output, 'export_generated_headers') |
| 406 | self._output_field(output, 'defaults') |
| 407 | self._output_field(output, 'cflags') |
| 408 | self._output_field(output, 'include_dirs') |
| 409 | self._output_field(output, 'header_libs') |
| 410 | self._output_field(output, 'required') |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 411 | self._output_field(output, 'dist') |
Lalit Maganti | accd64b | 2020-03-16 19:54:10 +0000 | [diff] [blame] | 412 | self._output_field(output, 'strip') |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 413 | self._output_field(output, 'tool_files') |
| 414 | self._output_field(output, 'data') |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 415 | self._output_field(output, 'stl') |
Jiyong Park | d5ea011 | 2020-04-28 18:22:00 +0900 | [diff] [blame^] | 416 | self._output_field(output, 'apex_available') |
Lalit Maganti | d8b1a1d | 2018-05-23 14:41:43 +0100 | [diff] [blame] | 417 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 418 | target_out = [] |
| 419 | self._output_field(target_out, 'android') |
| 420 | self._output_field(target_out, 'host') |
| 421 | if target_out: |
| 422 | output.append(' target: {') |
| 423 | for line in target_out: |
| 424 | output.append(' %s' % line) |
| 425 | output.append(' },') |
Lalit Maganti | edace41 | 2019-06-18 13:28:28 +0100 | [diff] [blame] | 426 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 427 | disable_pdk = any(name in library_not_in_pdk for name in self.shared_libs) |
| 428 | if self.user_debug_flag or disable_pdk: |
| 429 | output.append(' product_variables: {') |
| 430 | if disable_pdk: |
| 431 | output.append(' pdk: {') |
| 432 | output.append(' enabled: false,') |
| 433 | output.append(' },') |
| 434 | if self.user_debug_flag: |
| 435 | output.append(' debuggable: {') |
| 436 | output.append( |
| 437 | ' cflags: ["-DPERFETTO_BUILD_WITH_ANDROID_USERDEBUG"],') |
| 438 | output.append(' },') |
| 439 | output.append(' },') |
| 440 | if self.lto is not None: |
| 441 | output.append(' target: {') |
| 442 | output.append(' android: {') |
| 443 | output.append(' lto: {') |
| 444 | output.append(' thin: %s,' % 'true' if self.lto else 'false') |
| 445 | output.append(' },') |
| 446 | output.append(' },') |
| 447 | output.append(' },') |
| 448 | output.append('}') |
| 449 | output.append('') |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 450 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 451 | def _output_field(self, output, name, sort=True): |
| 452 | value = getattr(self, name) |
| 453 | return write_blueprint_key_value(output, name, value, sort) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 454 | |
| 455 | |
| 456 | class Blueprint(object): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 457 | """In-memory representation of an Android.bp file.""" |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 458 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 459 | def __init__(self): |
| 460 | self.modules = {} |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 461 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 462 | def add_module(self, module): |
| 463 | """Adds a new module to the blueprint, replacing any existing module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 464 | with the same name. |
| 465 | |
| 466 | Args: |
| 467 | module: Module instance. |
| 468 | """ |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 469 | self.modules[module.name] = module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 470 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 471 | def to_string(self, output): |
| 472 | for m in sorted(itervalues(self.modules), key=lambda m: m.name): |
| 473 | m.to_string(output) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 474 | |
| 475 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 476 | def label_to_module_name(label): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 477 | """Turn a GN label (e.g., //:perfetto_tests) into a module name.""" |
| 478 | # If the label is explicibly listed in the default target list, don't prefix |
| 479 | # its name and return just the target name. This is so tools like |
| 480 | # "trace_to_text" stay as such in the Android tree. |
| 481 | label_without_toolchain = gn_utils.label_without_toolchain(label) |
| 482 | if label in default_targets or label_without_toolchain in default_targets: |
| 483 | return label_without_toolchain.split(':')[-1] |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 484 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 485 | module = re.sub(r'^//:?', '', label_without_toolchain) |
| 486 | module = re.sub(r'[^a-zA-Z0-9_]', '_', module) |
| 487 | if not module.startswith(module_prefix): |
| 488 | return module_prefix + module |
| 489 | return module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 490 | |
| 491 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 492 | def is_supported_source_file(name): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 493 | """Returns True if |name| can appear in a 'srcs' list.""" |
| 494 | return os.path.splitext(name)[1] in ['.c', '.cc', '.proto'] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 495 | |
| 496 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 497 | def create_proto_modules(blueprint, gn, target): |
| 498 | """Generate genrules for a proto GN target. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 499 | |
| 500 | GN actions are used to dynamically generate files during the build. The |
| 501 | Soong equivalent is a genrule. This function turns a specific kind of |
| 502 | 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] | 503 | equivalent genrules. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 504 | |
| 505 | Args: |
| 506 | blueprint: Blueprint instance which is being generated. |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 507 | target: gn_utils.Target object. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 508 | |
| 509 | Returns: |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 510 | The source_genrule module. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 511 | """ |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 512 | assert (target.type == 'proto_library') |
| 513 | cpp_out_dir = '$(genDir)/%s/' % tree_path |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 514 | cmd = ['mkdir -p %s &&' % cpp_out_dir, '$(location aprotoc)'] |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 515 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 516 | # We create two genrules for each proto target: one for the headers and |
| 517 | # another for the sources. This is because the module that depends on the |
| 518 | # generated files needs to declare two different types of dependencies -- |
| 519 | # source files in 'srcs' and headers in 'generated_headers' -- and it's not |
| 520 | # valid to generate .h files from a source dependency and vice versa. |
| 521 | source_module_name = label_to_module_name(target.name) + '_gen' |
| 522 | source_module = Module('genrule', source_module_name, target.name) |
| 523 | blueprint.add_module(source_module) |
| 524 | source_module.srcs.update( |
| 525 | gn_utils.label_to_path(src) for src in target.sources) |
| 526 | tools = {'aprotoc'} |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 527 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 528 | header_module = Module('genrule', source_module_name + '_headers', |
| 529 | target.name) |
| 530 | blueprint.add_module(header_module) |
| 531 | header_module.srcs = set(source_module.srcs) |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 532 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 533 | # TODO(primiano): at some point we should remove this. This was introduced |
| 534 | # by aosp/1108421 when adding "protos/" to .proto include paths, in order to |
| 535 | # avoid doing multi-repo changes and allow old clients in the android tree |
| 536 | # to still do the old #include "perfetto/..." rather than |
| 537 | # #include "protos/perfetto/...". |
| 538 | header_module.export_include_dirs = {'.', 'protos'} |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 539 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 540 | source_module.genrule_srcs.add(':' + source_module.name) |
| 541 | source_module.genrule_headers.add(header_module.name) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 542 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 543 | # In GN builds the proto path is always relative to the output directory |
| 544 | # (out/tmp.xxx). |
| 545 | cmd += ['--proto_path=%s' % tree_path] |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 546 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 547 | if target.proto_plugin == 'proto': |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 548 | suffixes = ['pb'] |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 549 | source_module.genrule_shared_libs.add('libprotobuf-cpp-lite') |
Primiano Tucci | 7b6a788 | 2020-01-20 22:34:31 +0000 | [diff] [blame] | 550 | cmd += ['--cpp_out=lite=true:' + cpp_out_dir] |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 551 | elif target.proto_plugin == 'protozero': |
| 552 | suffixes = ['pbzero'] |
| 553 | plugin = create_modules_from_target(blueprint, gn, protozero_plugin) |
| 554 | tools.add(plugin.name) |
| 555 | cmd += ['--plugin=protoc-gen-plugin=$(location %s)' % plugin.name] |
| 556 | cmd += ['--plugin_out=wrapper_namespace=pbzero:' + cpp_out_dir] |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 557 | elif target.proto_plugin == 'cppgen': |
| 558 | suffixes = ['gen'] |
| 559 | plugin = create_modules_from_target(blueprint, gn, cppgen_plugin) |
| 560 | tools.add(plugin.name) |
| 561 | cmd += ['--plugin=protoc-gen-plugin=$(location %s)' % plugin.name] |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 562 | cmd += ['--plugin_out=wrapper_namespace=gen:' + cpp_out_dir] |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 563 | elif target.proto_plugin == 'ipc': |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 564 | suffixes = ['ipc'] |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 565 | plugin = create_modules_from_target(blueprint, gn, ipc_plugin) |
| 566 | tools.add(plugin.name) |
| 567 | cmd += ['--plugin=protoc-gen-plugin=$(location %s)' % plugin.name] |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 568 | cmd += ['--plugin_out=wrapper_namespace=gen:' + cpp_out_dir] |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 569 | else: |
| 570 | raise Error('Unsupported proto plugin: %s' % target.proto_plugin) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 571 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 572 | cmd += ['$(in)'] |
| 573 | source_module.cmd = ' '.join(cmd) |
| 574 | header_module.cmd = source_module.cmd |
| 575 | source_module.tools = tools |
| 576 | header_module.tools = tools |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 577 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 578 | for sfx in suffixes: |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 579 | source_module.out.update('%s/%s' % |
| 580 | (tree_path, src.replace('.proto', '.%s.cc' % sfx)) |
| 581 | for src in source_module.srcs) |
| 582 | header_module.out.update('%s/%s' % |
| 583 | (tree_path, src.replace('.proto', '.%s.h' % sfx)) |
| 584 | for src in header_module.srcs) |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 585 | return source_module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 586 | |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 587 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 588 | def create_merged_sql_metrics_module(blueprint, target): |
| 589 | module = Module('genrule', 'gen_merged_sql_metrics', |
| 590 | '//src/trace_processor/metrics:gen_merged_sql_metrics') |
| 591 | module.genrule_headers.add('gen_merged_sql_metrics') |
| 592 | module.tool_files = [ |
| 593 | 'tools/gen_merged_sql_metrics.py', |
| 594 | ] |
| 595 | module.cmd = ' '.join([ |
| 596 | '$(location tools/gen_merged_sql_metrics.py)', |
| 597 | '--cpp_out=$(out)', |
| 598 | '$(in)', |
| 599 | ]) |
| 600 | module.out.update(target.outputs) |
| 601 | module.srcs.update(gn_utils.label_to_path(src) for src in target.inputs) |
| 602 | blueprint.add_module(module) |
| 603 | return module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 604 | |
Sami Kyostila | 3c88a1d | 2019-05-22 18:29:42 +0100 | [diff] [blame] | 605 | |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 606 | def _get_cflags(target): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 607 | cflags = {flag for flag in target.cflags if re.match(cflag_whitelist, flag)} |
| 608 | cflags |= set("-D%s" % define |
| 609 | for define in target.defines |
| 610 | if re.match(define_whitelist, define)) |
| 611 | return cflags |
Florian Mayer | 3d5e7e6 | 2018-01-19 15:22:46 +0000 | [diff] [blame] | 612 | |
| 613 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 614 | def create_modules_from_target(blueprint, gn, gn_target_name): |
| 615 | """Generate module(s) for a given GN target. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 616 | |
| 617 | Given a GN target name, generate one or more corresponding modules into a |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 618 | blueprint. The only case when this generates >1 module is proto libraries. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 619 | |
| 620 | Args: |
| 621 | blueprint: Blueprint instance which is being generated. |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 622 | gn: gn_utils.GnParser object. |
| 623 | gn_target_name: GN target for module generation. |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 624 | """ |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 625 | bp_module_name = label_to_module_name(gn_target_name) |
| 626 | if bp_module_name in blueprint.modules: |
| 627 | return blueprint.modules[bp_module_name] |
| 628 | target = gn.get_target(gn_target_name) |
| 629 | export_include_dirs = {'include', buildflags_dir} |
| 630 | |
| 631 | if target.type == 'executable': |
| 632 | if target.toolchain == gn_utils.HOST_TOOLCHAIN: |
| 633 | module_type = 'cc_binary_host' |
| 634 | elif target.testonly: |
| 635 | module_type = 'cc_test' |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 636 | else: |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 637 | module_type = 'cc_binary' |
| 638 | module = Module(module_type, bp_module_name, gn_target_name) |
| 639 | elif target.type == 'static_library': |
| 640 | module = Module('cc_library_static', bp_module_name, gn_target_name) |
| 641 | module.export_include_dirs = export_include_dirs |
| 642 | elif target.type == 'shared_library': |
| 643 | module = Module('cc_library_shared', bp_module_name, gn_target_name) |
| 644 | module.export_include_dirs = export_include_dirs |
| 645 | elif target.type == 'source_set': |
| 646 | module = Module('filegroup', bp_module_name, gn_target_name) |
| 647 | elif target.type == 'group': |
| 648 | # "group" targets are resolved recursively by gn_utils.get_target(). |
| 649 | # There's nothing we need to do at this level for them. |
| 650 | return None |
| 651 | elif target.type == 'proto_library': |
| 652 | module = create_proto_modules(blueprint, gn, target) |
| 653 | elif target.type == 'action' and 'gen_merged_sql_metrics' in target.name: |
| 654 | module = create_merged_sql_metrics_module(blueprint, target) |
| 655 | else: |
| 656 | raise Error('Unknown target %s (%s)' % (target.name, target.type)) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 657 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 658 | blueprint.add_module(module) |
| 659 | module.host_supported = target.name in target_host_supported |
| 660 | module.init_rc = target_initrc.get(target.name, []) |
| 661 | module.srcs.update( |
| 662 | gn_utils.label_to_path(src) |
| 663 | for src in target.sources |
| 664 | if is_supported_source_file(src)) |
Primiano Tucci | 6067e73 | 2018-01-08 16:19:40 +0000 | [diff] [blame] | 665 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 666 | if target.type in gn_utils.LINKER_UNIT_TYPES: |
| 667 | module.cflags.update(_get_cflags(target)) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 668 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 669 | module_is_compiled = module.type not in ('genrule', 'filegroup') |
| 670 | if module_is_compiled: |
| 671 | # Don't try to inject library/source dependencies into genrules or |
| 672 | # filegroups because they are not compiled in the traditional sense. |
| 673 | module.defaults = [defaults_module] |
| 674 | for lib in target.libs: |
| 675 | # Generally library names should be mangled as 'libXXX', unless they |
| 676 | # are HAL libraries (e.g., android.hardware.health@2.0). |
| 677 | android_lib = lib if '@' in lib else 'lib' + lib |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 678 | if lib in shared_library_whitelist: |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 679 | module.shared_libs.add(android_lib) |
Hector Dearman | 92d7d11 | 2019-12-05 15:19:57 +0000 | [diff] [blame] | 680 | if lib in static_library_whitelist: |
| 681 | module.static_libs.add(android_lib) |
Lalit Maganti | c5bcd79 | 2018-01-12 18:38:11 +0000 | [diff] [blame] | 682 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 683 | # If the module is a static library, export all the generated headers. |
| 684 | if module.type == 'cc_library_static': |
| 685 | module.export_generated_headers = module.generated_headers |
Ryan Savitski | e65beca | 2019-01-29 18:29:13 +0000 | [diff] [blame] | 686 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 687 | # Merge in additional hardcoded arguments. |
| 688 | for key, add_val in additional_args.get(module.name, []): |
| 689 | curr = getattr(module, key) |
| 690 | if add_val and isinstance(add_val, set) and isinstance(curr, set): |
| 691 | curr.update(add_val) |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 692 | elif isinstance(add_val, str) and (not curr or isinstance(curr, str)): |
Lalit Maganti | 3d415ec | 2019-10-23 17:53:17 +0100 | [diff] [blame] | 693 | setattr(module, key, add_val) |
Lalit Maganti | cdda911 | 2019-11-27 14:19:49 +0000 | [diff] [blame] | 694 | elif isinstance(add_val, dict) and isinstance(curr, dict): |
| 695 | curr.update(add_val) |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 696 | else: |
| 697 | raise Error('Unimplemented type of additional_args: %r' % key) |
| 698 | |
| 699 | # dep_name is an unmangled GN target name (e.g. //foo:bar(toolchain)). |
| 700 | for dep_name in target.deps | target.source_set_deps | target.proto_deps: |
| 701 | # If the dependency refers to a library which we can replace with an |
| 702 | # Android equivalent, stop recursing and patch the dependency in. |
| 703 | # Don't recurse into //buildtools, builtin_deps are intercepted at |
| 704 | # the //gn:xxx level. |
| 705 | if dep_name.startswith('//buildtools'): |
| 706 | continue |
| 707 | |
| 708 | # Ignore the dependency on the gen_buildflags genrule. That is run |
| 709 | # separately in this generator and the generated file is copied over |
| 710 | # into the repo (see usage of |buildflags_dir| in this script). |
| 711 | if dep_name.startswith(gn_utils.BUILDFLAGS_TARGET): |
| 712 | continue |
| 713 | |
| 714 | dep_module = create_modules_from_target(blueprint, gn, dep_name) |
| 715 | |
| 716 | # For filegroups and genrule, recurse but don't apply the deps. |
| 717 | if not module_is_compiled: |
| 718 | continue |
| 719 | |
| 720 | # |builtin_deps| override GN deps with Android-specific ones. See the |
| 721 | # config in the top of this file. |
| 722 | if gn_utils.label_without_toolchain(dep_name) in builtin_deps: |
| 723 | builtin_deps[gn_utils.label_without_toolchain(dep_name)](module) |
| 724 | continue |
| 725 | |
| 726 | # Don't recurse in any other //gn dep if not handled by builtin_deps. |
| 727 | if dep_name.startswith('//gn:'): |
| 728 | continue |
| 729 | |
| 730 | if dep_module is None: |
| 731 | continue |
| 732 | if dep_module.type == 'cc_library_shared': |
| 733 | module.shared_libs.add(dep_module.name) |
| 734 | elif dep_module.type == 'cc_library_static': |
| 735 | module.static_libs.add(dep_module.name) |
| 736 | elif dep_module.type == 'filegroup': |
| 737 | module.srcs.add(':' + dep_module.name) |
| 738 | elif dep_module.type == 'genrule': |
| 739 | module.generated_headers.update(dep_module.genrule_headers) |
| 740 | module.srcs.update(dep_module.genrule_srcs) |
| 741 | module.shared_libs.update(dep_module.genrule_shared_libs) |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 742 | elif dep_module.type == 'cc_binary': |
| 743 | continue # Ignore executables deps (used by cmdline integration tests). |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 744 | else: |
| 745 | raise Error('Unknown dep %s (%s) for target %s' % |
| 746 | (dep_module.name, dep_module.type, module.name)) |
| 747 | |
| 748 | return module |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 749 | |
| 750 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 751 | def create_blueprint_for_targets(gn, desc, targets): |
| 752 | """Generate a blueprint for a list of GN targets.""" |
| 753 | blueprint = Blueprint() |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 754 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 755 | # Default settings used by all modules. |
| 756 | defaults = Module('cc_defaults', defaults_module, '//gn:default_deps') |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 757 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 758 | # We have to use include_dirs passing the path relative to the android tree. |
| 759 | # This is because: (i) perfetto_cc_defaults is used also by |
| 760 | # test/**/Android.bp; (ii) if we use local_include_dirs instead, paths |
| 761 | # become relative to the Android.bp that *uses* cc_defaults (not the one |
| 762 | # that defines it).s |
| 763 | defaults.include_dirs = { |
| 764 | tree_path, tree_path + '/include', tree_path + '/' + buildflags_dir |
| 765 | } |
| 766 | defaults.cflags = [ |
| 767 | '-Wno-error=return-type', |
| 768 | '-Wno-sign-compare', |
| 769 | '-Wno-sign-promo', |
| 770 | '-Wno-unused-parameter', |
| 771 | '-fvisibility=hidden', |
| 772 | '-O2', |
| 773 | ] |
| 774 | defaults.user_debug_flag = True |
| 775 | defaults.lto = True |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 776 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 777 | blueprint.add_module(defaults) |
| 778 | gn = gn_utils.GnParser(desc) |
| 779 | for target in targets: |
| 780 | create_modules_from_target(blueprint, gn, target) |
| 781 | return blueprint |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 782 | |
| 783 | |
| 784 | def main(): |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 785 | parser = argparse.ArgumentParser( |
| 786 | description='Generate Android.bp from a GN description.') |
| 787 | parser.add_argument( |
| 788 | '--check-only', |
| 789 | help='Don\'t keep the generated files', |
| 790 | action='store_true') |
| 791 | parser.add_argument( |
| 792 | '--desc', |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 793 | help='GN description (e.g., gn desc out --format=json --all-toolchains "//*"' |
| 794 | ) |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 795 | parser.add_argument( |
| 796 | '--extras', |
| 797 | help='Extra targets to include at the end of the Blueprint file', |
| 798 | default=os.path.join(gn_utils.repo_root(), 'Android.bp.extras'), |
| 799 | ) |
| 800 | parser.add_argument( |
| 801 | '--output', |
| 802 | help='Blueprint file to create', |
| 803 | default=os.path.join(gn_utils.repo_root(), 'Android.bp'), |
| 804 | ) |
| 805 | parser.add_argument( |
| 806 | 'targets', |
| 807 | nargs=argparse.REMAINDER, |
| 808 | help='Targets to include in the blueprint (e.g., "//:perfetto_tests")') |
| 809 | args = parser.parse_args() |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 810 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 811 | if args.desc: |
| 812 | with open(args.desc) as f: |
| 813 | desc = json.load(f) |
| 814 | else: |
| 815 | desc = gn_utils.create_build_description(gn_args) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 816 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 817 | gn = gn_utils.GnParser(desc) |
| 818 | blueprint = create_blueprint_for_targets(gn, desc, args.targets or |
| 819 | default_targets) |
| 820 | output = [ |
| 821 | """// Copyright (C) 2017 The Android Open Source Project |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 822 | // |
| 823 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 824 | // you may not use this file except in compliance with the License. |
| 825 | // You may obtain a copy of the License at |
| 826 | // |
| 827 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 828 | // |
| 829 | // Unless required by applicable law or agreed to in writing, software |
| 830 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 831 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 832 | // See the License for the specific language governing permissions and |
| 833 | // limitations under the License. |
| 834 | // |
| 835 | // This file is automatically generated by %s. Do not edit. |
| 836 | """ % (__file__) |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 837 | ] |
| 838 | blueprint.to_string(output) |
| 839 | with open(args.extras, 'r') as r: |
| 840 | for line in r: |
| 841 | output.append(line.rstrip("\n\r")) |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 842 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 843 | out_files = [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 844 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 845 | # Generate the Android.bp file. |
| 846 | out_files.append(args.output + '.swp') |
| 847 | with open(out_files[-1], 'w') as f: |
| 848 | f.write('\n'.join(output)) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 849 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 850 | # Generate the perfetto_build_flags.h file. |
| 851 | out_files.append(os.path.join(buildflags_dir, 'perfetto_build_flags.h.swp')) |
| 852 | gn_utils.gen_buildflags(gn_args, out_files[-1]) |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 853 | |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 854 | # Either check the contents or move the files to their final destination. |
| 855 | return gn_utils.check_or_commit_generated_files(out_files, args.check_only) |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 856 | |
| 857 | |
Sami Kyostila | 865d1d3 | 2017-12-12 18:37:04 +0000 | [diff] [blame] | 858 | if __name__ == '__main__': |
Primiano Tucci | f0d7ef8 | 2019-10-04 15:35:24 +0100 | [diff] [blame] | 859 | sys.exit(main()) |