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