Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 1 | # Copyright (C) 2022 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # A collection of utilities for extracting build rule information from GN |
| 16 | # projects. |
| 17 | |
| 18 | from __future__ import print_function |
| 19 | import collections |
| 20 | import errno |
| 21 | import filecmp |
| 22 | import json |
Patrick Rohr | af92fa6 | 2022-11-04 14:27:04 -0700 | [diff] [blame] | 23 | import logging as log |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 24 | import os |
| 25 | import re |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 29 | |
| 30 | BUILDFLAGS_TARGET = '//gn:gen_buildflags' |
| 31 | GEN_VERSION_TARGET = '//src/base:version_gen_h' |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 32 | LINKER_UNIT_TYPES = ('executable', 'shared_library', 'static_library', 'source_set') |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 33 | JAVA_BANNED_SCRIPTS = [ |
| 34 | "//build/android/gyp/turbine.py", |
| 35 | "//build/android/gyp/compile_java.py", |
| 36 | "//build/android/gyp/filter_zip.py", |
| 37 | "//build/android/gyp/dex.py", |
| 38 | "//build/android/gyp/write_build_config.py", |
| 39 | "//build/android/gyp/create_r_java.py", |
| 40 | "//build/android/gyp/ijar.py", |
| 41 | "//build/android/gyp/create_r_java.py", |
| 42 | "//build/android/gyp/bytecode_processor.py", |
| 43 | "//build/android/gyp/prepare_resources.py", |
| 44 | "//build/android/gyp/aar.py", |
| 45 | "//build/android/gyp/zip.py", |
| 46 | ] |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 47 | # TODO(primiano): investigate these, they require further componentization. |
| 48 | ODR_VIOLATION_IGNORE_TARGETS = { |
| 49 | '//test/cts:perfetto_cts_deps', |
| 50 | '//:perfetto_integrationtests', |
| 51 | } |
Mohannad Farrag | 4bea14a | 2022-11-22 15:38:30 +0000 | [diff] [blame] | 52 | ARCH_REGEX = r'(android_x86_64|android_x86|android_arm|android_arm64|host)' |
Mohannad Farrag | a0e37c1 | 2022-12-02 14:46:08 +0000 | [diff] [blame] | 53 | RESPONSE_FILE = '{{response_file_name}}' |
| 54 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 55 | def repo_root(): |
| 56 | """Returns an absolute path to the repository root.""" |
| 57 | return os.path.join( |
| 58 | os.path.realpath(os.path.dirname(__file__)), os.path.pardir) |
| 59 | |
| 60 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 61 | def label_to_path(label): |
| 62 | """Turn a GN output label (e.g., //some_dir/file.cc) into a path.""" |
| 63 | assert label.startswith('//') |
Patrick Rohr | c6331c8 | 2022-10-25 11:34:20 -0700 | [diff] [blame] | 64 | return label[2:] or "./" |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def label_without_toolchain(label): |
| 68 | """Strips the toolchain from a GN label. |
| 69 | |
| 70 | Return a GN label (e.g //buildtools:protobuf(//gn/standalone/toolchain: |
| 71 | gcc_like_host) without the parenthesised toolchain part. |
| 72 | """ |
| 73 | return label.split('(')[0] |
| 74 | |
| 75 | |
| 76 | def label_to_target_name_with_path(label): |
| 77 | """ |
| 78 | Turn a GN label into a target name involving the full path. |
| 79 | e.g., //src/perfetto:tests -> src_perfetto_tests |
| 80 | """ |
| 81 | name = re.sub(r'^//:?', '', label) |
| 82 | name = re.sub(r'[^a-zA-Z0-9_]', '_', name) |
| 83 | return name |
| 84 | |
Mohannad Farrag | a0db68b | 2022-11-24 12:28:09 +0000 | [diff] [blame] | 85 | def _is_java_source(src): |
| 86 | return os.path.splitext(src)[1] == '.java' and not src.startswith("//out/test/gen/") |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 87 | |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 88 | def is_java_action(script, outputs): |
| 89 | return (script != "" and script not in JAVA_BANNED_SCRIPTS) and any( |
| 90 | [file.endswith(".srcjar") or file.endswith(".java") |
| 91 | for file in outputs]) |
| 92 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 93 | class GnParser(object): |
| 94 | """A parser with some cleverness for GN json desc files |
| 95 | |
| 96 | The main goals of this parser are: |
| 97 | 1) Deal with the fact that other build systems don't have an equivalent |
| 98 | notion to GN's source_set. Conversely to Bazel's and Soong's filegroups, |
| 99 | GN source_sets expect that dependencies, cflags and other source_set |
| 100 | properties propagate up to the linker unit (static_library, executable or |
| 101 | shared_library). This parser simulates the same behavior: when a |
| 102 | source_set is encountered, some of its variables (cflags and such) are |
| 103 | copied up to the dependent targets. This is to allow gen_xxx to create |
| 104 | one filegroup for each source_set and then squash all the other flags |
| 105 | onto the linker unit. |
| 106 | 2) Detect and special-case protobuf targets, figuring out the protoc-plugin |
| 107 | being used. |
| 108 | """ |
| 109 | |
| 110 | class Target(object): |
| 111 | """Reperesents A GN target. |
| 112 | |
| 113 | Maked properties are propagated up the dependency chain when a |
| 114 | source_set dependency is encountered. |
| 115 | """ |
Patrick Rohr | 02ad51f | 2022-11-15 13:54:07 -0800 | [diff] [blame] | 116 | class Arch(): |
| 117 | """Architecture-dependent properties |
| 118 | """ |
| 119 | def __init__(self): |
| 120 | self.sources = set() |
Motomu Utsumi | 1c64e44 | 2022-11-17 21:51:37 +0900 | [diff] [blame] | 121 | self.cflags = set() |
Motomu Utsumi | 80e0447 | 2022-11-17 21:54:06 +0900 | [diff] [blame] | 122 | self.defines = set() |
Motomu Utsumi | 778f830 | 2022-11-17 22:12:17 +0900 | [diff] [blame] | 123 | self.include_dirs = set() |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 124 | self.deps = set() |
| 125 | self.transitive_static_libs_deps = set() |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 126 | self.source_set_deps = set() |
Patrick Rohr | 02ad51f | 2022-11-15 13:54:07 -0800 | [diff] [blame] | 127 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 128 | |
| 129 | def __init__(self, name, type): |
| 130 | self.name = name # e.g. //src/ipc:ipc |
| 131 | |
| 132 | VALID_TYPES = ('static_library', 'shared_library', 'executable', 'group', |
Patrick Rohr | da778a0 | 2022-10-25 16:17:31 -0700 | [diff] [blame] | 133 | 'action', 'source_set', 'proto_library', 'copy', 'action_foreach') |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 134 | assert (type in VALID_TYPES) |
| 135 | self.type = type |
| 136 | self.testonly = False |
| 137 | self.toolchain = None |
| 138 | |
| 139 | # These are valid only for type == proto_library. |
| 140 | # This is typically: 'proto', 'protozero', 'ipc'. |
| 141 | self.proto_plugin = None |
| 142 | self.proto_paths = set() |
| 143 | self.proto_exports = set() |
Motomu Utsumi | d7e0e42 | 2022-11-08 17:49:52 +0900 | [diff] [blame] | 144 | self.proto_in_dir = "" |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 145 | |
| 146 | self.sources = set() |
| 147 | # TODO(primiano): consider whether the public section should be part of |
| 148 | # bubbled-up sources. |
| 149 | self.public_headers = set() # 'public' |
| 150 | |
| 151 | # These are valid only for type == 'action' |
| 152 | self.inputs = set() |
| 153 | self.outputs = set() |
| 154 | self.script = None |
| 155 | self.args = [] |
Patrick Rohr | 09716f5 | 2022-10-27 13:02:36 -0700 | [diff] [blame] | 156 | self.response_file_contents = None |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 157 | |
| 158 | # These variables are propagated up when encountering a dependency |
| 159 | # on a source_set target. |
| 160 | self.cflags = set() |
| 161 | self.defines = set() |
| 162 | self.deps = set() |
| 163 | self.libs = set() |
| 164 | self.include_dirs = set() |
| 165 | self.ldflags = set() |
| 166 | self.source_set_deps = set() # Transitive set of source_set deps. |
| 167 | self.proto_deps = set() |
| 168 | self.transitive_proto_deps = set() |
Mohannad Farrag | baf0d57 | 2022-11-22 11:53:54 +0000 | [diff] [blame] | 169 | self.rtti = False |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 170 | |
Patrick Rohr | 7091356 | 2022-11-15 21:49:28 -0800 | [diff] [blame] | 171 | # TODO: come up with a better way to only run this once. |
| 172 | # is_finalized tracks whether finalize() was called on this target. |
| 173 | self.is_finalized = False |
Patrick Rohr | 02ad51f | 2022-11-15 13:54:07 -0800 | [diff] [blame] | 174 | self.arch = dict() |
| 175 | |
Motomu Utsumi | ee47af6 | 2022-11-30 16:41:15 +0900 | [diff] [blame] | 176 | # This is used to get the name/version of libcronet |
| 177 | self.output_name = None |
| 178 | |
Patrick Rohr | c8f41cd | 2022-11-15 22:46:10 -0800 | [diff] [blame] | 179 | def host_supported(self): |
| 180 | return 'host' in self.arch |
| 181 | |
| 182 | def device_supported(self): |
| 183 | return any([name.startswith('android') for name in self.arch.keys()]) |
| 184 | |
Patrick Rohr | 94c2490 | 2022-12-14 19:44:44 -0800 | [diff] [blame] | 185 | def is_linker_unit_type(self): |
| 186 | return self.type in LINKER_UNIT_TYPES |
| 187 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 188 | def __lt__(self, other): |
| 189 | if isinstance(other, self.__class__): |
| 190 | return self.name < other.name |
| 191 | raise TypeError( |
| 192 | '\'<\' not supported between instances of \'%s\' and \'%s\'' % |
| 193 | (type(self).__name__, type(other).__name__)) |
| 194 | |
| 195 | def __repr__(self): |
| 196 | return json.dumps({ |
| 197 | k: (list(sorted(v)) if isinstance(v, set) else v) |
Patrick Rohr | 23f2619 | 2022-10-25 09:45:22 -0700 | [diff] [blame] | 198 | for (k, v) in self.__dict__.items() |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 199 | }, |
| 200 | indent=4, |
| 201 | sort_keys=True) |
| 202 | |
Motomu Utsumi | 1c64e44 | 2022-11-17 21:51:37 +0900 | [diff] [blame] | 203 | def update(self, other, arch): |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 204 | for key in ('cflags', 'defines', 'deps', 'include_dirs', 'ldflags', |
| 205 | 'source_set_deps', 'proto_deps', 'transitive_proto_deps', |
| 206 | 'libs', 'proto_paths'): |
| 207 | self.__dict__[key].update(other.__dict__.get(key, [])) |
| 208 | |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 209 | for key_in_arch in ('cflags', 'defines', 'include_dirs', 'source_set_deps'): |
Motomu Utsumi | 1c64e44 | 2022-11-17 21:51:37 +0900 | [diff] [blame] | 210 | self.arch[arch].__dict__[key_in_arch].update( |
| 211 | other.arch[arch].__dict__.get(key_in_arch, [])) |
| 212 | |
Patrick Rohr | 7091356 | 2022-11-15 21:49:28 -0800 | [diff] [blame] | 213 | def finalize(self): |
| 214 | """Move common properties out of arch-dependent subobjects to Target object. |
| 215 | |
| 216 | TODO: find a better name for this function. |
| 217 | """ |
| 218 | if self.is_finalized: |
| 219 | return |
| 220 | self.is_finalized = True |
| 221 | |
Patrick Rohr | 7091356 | 2022-11-15 21:49:28 -0800 | [diff] [blame] | 222 | # Target contains the intersection of arch-dependent properties |
| 223 | self.sources = set.intersection(*[arch.sources for arch in self.arch.values()]) |
Motomu Utsumi | f0f4768 | 2022-11-17 22:34:39 +0900 | [diff] [blame] | 224 | self.cflags = set.intersection(*[arch.cflags for arch in self.arch.values()]) |
| 225 | self.defines = set.intersection(*[arch.defines for arch in self.arch.values()]) |
| 226 | self.include_dirs = set.intersection(*[arch.include_dirs for arch in self.arch.values()]) |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 227 | self.deps.update(set.intersection(*[arch.deps for arch in self.arch.values()])) |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 228 | self.source_set_deps.update(set.intersection(*[arch.source_set_deps for arch in self.arch.values()])) |
Patrick Rohr | 7091356 | 2022-11-15 21:49:28 -0800 | [diff] [blame] | 229 | |
| 230 | # Deduplicate arch-dependent properties |
| 231 | for arch in self.arch.keys(): |
| 232 | self.arch[arch].sources -= self.sources |
Motomu Utsumi | f0f4768 | 2022-11-17 22:34:39 +0900 | [diff] [blame] | 233 | self.arch[arch].cflags -= self.cflags |
| 234 | self.arch[arch].defines -= self.defines |
| 235 | self.arch[arch].include_dirs -= self.include_dirs |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 236 | self.arch[arch].deps -= self.deps |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 237 | self.arch[arch].source_set_deps -= self.source_set_deps |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 238 | |
Patrick Rohr | 7091356 | 2022-11-15 21:49:28 -0800 | [diff] [blame] | 239 | |
Patrick Rohr | 0913f0b | 2022-12-13 09:13:20 -0800 | [diff] [blame] | 240 | def __init__(self, builtin_deps): |
| 241 | self.builtin_deps = builtin_deps |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 242 | self.all_targets = {} |
| 243 | self.linker_units = {} # Executables, shared or static libraries. |
| 244 | self.source_sets = {} |
| 245 | self.actions = {} |
| 246 | self.proto_libs = {} |
Patrick Rohr | b27587e | 2022-11-04 14:57:24 -0700 | [diff] [blame] | 247 | self.java_sources = set() |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 248 | self.java_actions = set() |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 249 | |
Patrick Rohr | 09716f5 | 2022-10-27 13:02:36 -0700 | [diff] [blame] | 250 | def _get_response_file_contents(self, action_desc): |
Patrick Rohr | c20887d | 2022-10-28 12:59:20 -0700 | [diff] [blame] | 251 | # response_file_contents are formatted as: |
| 252 | # ['--flags', '--flag=true && false'] and need to be formatted as: |
| 253 | # '--flags --flag=\"true && false\"' |
| 254 | flags = action_desc.get('response_file_contents', []) |
| 255 | formatted_flags = [] |
| 256 | for flag in flags: |
| 257 | if '=' in flag: |
| 258 | key, val = flag.split('=') |
| 259 | formatted_flags.append('%s=\\"%s\\"' % (key, val)) |
| 260 | else: |
| 261 | formatted_flags.append(flag) |
| 262 | |
| 263 | return ' '.join(formatted_flags) |
Patrick Rohr | 09716f5 | 2022-10-27 13:02:36 -0700 | [diff] [blame] | 264 | |
Patrick Rohr | df3c20c | 2022-12-12 20:19:26 -0800 | [diff] [blame] | 265 | def _is_java_group(self, type_, target_name): |
Patrick Rohr | af92fa6 | 2022-11-04 14:27:04 -0700 | [diff] [blame] | 266 | # Per https://chromium.googlesource.com/chromium/src/build/+/HEAD/android/docs/java_toolchain.md |
| 267 | # java target names must end in "_java". |
| 268 | # TODO: There are some other possible variations we might need to support. |
Patrick Rohr | 689a9c0 | 2022-12-13 14:55:27 -0800 | [diff] [blame] | 269 | return type_ == 'group' and target_name.endswith('_java') |
Patrick Rohr | af92fa6 | 2022-11-04 14:27:04 -0700 | [diff] [blame] | 270 | |
Patrick Rohr | 81a4ac3 | 2022-11-15 14:38:21 -0800 | [diff] [blame] | 271 | def _get_arch(self, toolchain): |
Patrick Rohr | d938d53 | 2022-11-15 22:17:08 -0800 | [diff] [blame] | 272 | if toolchain == '//build/toolchain/android:android_clang_x86': |
Patrick Rohr | 4eff210 | 2022-11-15 22:21:52 -0800 | [diff] [blame] | 273 | return 'android_x86' |
Patrick Rohr | 81a4ac3 | 2022-11-15 14:38:21 -0800 | [diff] [blame] | 274 | elif toolchain == '//build/toolchain/android:android_clang_x64': |
Patrick Rohr | 4eff210 | 2022-11-15 22:21:52 -0800 | [diff] [blame] | 275 | return 'android_x86_64' |
Patrick Rohr | 81a4ac3 | 2022-11-15 14:38:21 -0800 | [diff] [blame] | 276 | elif toolchain == '//build/toolchain/android:android_clang_arm': |
Patrick Rohr | 4eff210 | 2022-11-15 22:21:52 -0800 | [diff] [blame] | 277 | return 'android_arm' |
Patrick Rohr | 81a4ac3 | 2022-11-15 14:38:21 -0800 | [diff] [blame] | 278 | elif toolchain == '//build/toolchain/android:android_clang_arm64': |
Patrick Rohr | 4eff210 | 2022-11-15 22:21:52 -0800 | [diff] [blame] | 279 | return 'android_arm64' |
Patrick Rohr | 81a4ac3 | 2022-11-15 14:38:21 -0800 | [diff] [blame] | 280 | else: |
| 281 | return 'host' |
| 282 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 283 | def get_target(self, gn_target_name): |
| 284 | """Returns a Target object from the fully qualified GN target name. |
| 285 | |
Patrick Rohr | d0077b7 | 2022-11-15 12:43:26 -0800 | [diff] [blame] | 286 | get_target() requires that parse_gn_desc() has already been called. |
| 287 | """ |
Patrick Rohr | 7091356 | 2022-11-15 21:49:28 -0800 | [diff] [blame] | 288 | # Run this every time as parse_gn_desc can be called at any time. |
| 289 | for target in self.all_targets.values(): |
| 290 | target.finalize() |
| 291 | |
Patrick Rohr | 7705bdb | 2022-11-15 13:26:30 -0800 | [diff] [blame] | 292 | return self.all_targets[label_without_toolchain(gn_target_name)] |
Patrick Rohr | d0077b7 | 2022-11-15 12:43:26 -0800 | [diff] [blame] | 293 | |
Patrick Rohr | 0c7ef52 | 2022-12-12 20:29:19 -0800 | [diff] [blame] | 294 | def parse_gn_desc(self, gn_desc, gn_target_name, is_java_target = False): |
Patrick Rohr | d0077b7 | 2022-11-15 12:43:26 -0800 | [diff] [blame] | 295 | """Parses a gn desc tree and resolves all target dependencies. |
| 296 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 297 | It bubbles up variables from source_set dependencies as described in the |
| 298 | class-level comments. |
| 299 | """ |
Patrick Rohr | 7705bdb | 2022-11-15 13:26:30 -0800 | [diff] [blame] | 300 | # Use name without toolchain for targets to support targets built for |
| 301 | # multiple archs. |
| 302 | target_name = label_without_toolchain(gn_target_name) |
Patrick Rohr | 02ad51f | 2022-11-15 13:54:07 -0800 | [diff] [blame] | 303 | desc = gn_desc[gn_target_name] |
Patrick Rohr | 9860068 | 2022-11-18 18:29:15 -0800 | [diff] [blame] | 304 | type_ = desc['type'] |
Patrick Rohr | d938d53 | 2022-11-15 22:17:08 -0800 | [diff] [blame] | 305 | arch = self._get_arch(desc['toolchain']) |
Patrick Rohr | 9860068 | 2022-11-18 18:29:15 -0800 | [diff] [blame] | 306 | |
Patrick Rohr | 689a9c0 | 2022-12-13 14:55:27 -0800 | [diff] [blame] | 307 | is_java_target |= self._is_java_group(type_, target_name) |
Patrick Rohr | 0c7ef52 | 2022-12-12 20:29:19 -0800 | [diff] [blame] | 308 | |
Patrick Rohr | 9860068 | 2022-11-18 18:29:15 -0800 | [diff] [blame] | 309 | # Action modules can differ depending on the target architecture, yet |
| 310 | # genrule's do not allow to overload cmd per target OS / arch. Create a |
| 311 | # separate action for every architecture. |
Mohannad Farrag | d7efd7b9 | 2022-11-21 16:15:16 +0000 | [diff] [blame] | 312 | # Cover both action and action_foreach |
Patrick Rohr | 0c7ef52 | 2022-12-12 20:29:19 -0800 | [diff] [blame] | 313 | if type_.startswith('action') and not is_java_target: |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 314 | # Don't meddle with the java actions name |
Patrick Rohr | 9860068 | 2022-11-18 18:29:15 -0800 | [diff] [blame] | 315 | target_name += '__' + arch |
| 316 | |
| 317 | target = self.all_targets.get(target_name) |
Patrick Rohr | 02ad51f | 2022-11-15 13:54:07 -0800 | [diff] [blame] | 318 | if target is None: |
Patrick Rohr | 9860068 | 2022-11-18 18:29:15 -0800 | [diff] [blame] | 319 | target = GnParser.Target(target_name, type_) |
Patrick Rohr | 02ad51f | 2022-11-15 13:54:07 -0800 | [diff] [blame] | 320 | self.all_targets[target_name] = target |
| 321 | |
| 322 | if arch not in target.arch: |
| 323 | target.arch[arch] = GnParser.Target.Arch() |
| 324 | else: |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 325 | return target # Target already processed. |
| 326 | |
Patrick Rohr | 0913f0b | 2022-12-13 09:13:20 -0800 | [diff] [blame] | 327 | if target.name in self.builtin_deps: |
| 328 | # return early, no need to dive into the modules deps as the module is a |
| 329 | # builtin. |
| 330 | return None |
| 331 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 332 | target.testonly = desc.get('testonly', False) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 333 | |
Patrick Rohr | 0d40da3 | 2022-11-15 13:08:12 -0800 | [diff] [blame] | 334 | proto_target_type, proto_desc = self.get_proto_target_type(gn_desc, gn_target_name) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 335 | if proto_target_type is not None: |
| 336 | self.proto_libs[target.name] = target |
| 337 | target.type = 'proto_library' |
| 338 | target.proto_plugin = proto_target_type |
| 339 | target.proto_paths.update(self.get_proto_paths(proto_desc)) |
| 340 | target.proto_exports.update(self.get_proto_exports(proto_desc)) |
Motomu Utsumi | d7e0e42 | 2022-11-08 17:49:52 +0900 | [diff] [blame] | 341 | target.proto_in_dir = self.get_proto_in_dir(proto_desc) |
Patrick Rohr | ad7a29c | 2022-11-16 21:48:09 -0800 | [diff] [blame] | 342 | for gn_proto_deps_name in proto_desc.get('deps', []): |
| 343 | dep = self.parse_gn_desc(gn_desc, gn_proto_deps_name) |
| 344 | target.deps.add(dep.name) |
Patrick Rohr | 53dcd10 | 2022-11-15 21:53:02 -0800 | [diff] [blame] | 345 | target.arch[arch].sources.update(proto_desc.get('sources', [])) |
| 346 | assert (all(x.endswith('.proto') for x in target.arch[arch].sources)) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 347 | elif target.type == 'source_set': |
| 348 | self.source_sets[gn_target_name] = target |
Patrick Rohr | 53dcd10 | 2022-11-15 21:53:02 -0800 | [diff] [blame] | 349 | target.arch[arch].sources.update(desc.get('sources', [])) |
Patrick Rohr | 94c2490 | 2022-12-14 19:44:44 -0800 | [diff] [blame] | 350 | elif target.is_linker_unit_type(): |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 351 | self.linker_units[gn_target_name] = target |
Patrick Rohr | 53dcd10 | 2022-11-15 21:53:02 -0800 | [diff] [blame] | 352 | target.arch[arch].sources.update(desc.get('sources', [])) |
Patrick Rohr | df3c20c | 2022-12-12 20:19:26 -0800 | [diff] [blame] | 353 | elif (desc.get("script", "") in JAVA_BANNED_SCRIPTS |
| 354 | or self._is_java_group(target.type, target.name)): |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 355 | # java_group identifies the group target generated by the android_library |
Patrick Rohr | e71a061 | 2022-12-13 14:58:14 -0800 | [diff] [blame] | 356 | # or java_library template. A java_group must not be added as a |
| 357 | # dependency, but sources are collected. |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 358 | log.debug('Found java target %s', target.name) |
| 359 | if target.type == "action": |
| 360 | # Convert java actions into java_group and keep the inputs for collection. |
| 361 | target.inputs.update(desc.get('inputs', [])) |
| 362 | target.type = 'java_group' |
Patrick Rohr | da778a0 | 2022-10-25 16:17:31 -0700 | [diff] [blame] | 363 | elif target.type in ['action', 'action_foreach']: |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 364 | self.actions[gn_target_name] = target |
| 365 | target.inputs.update(desc.get('inputs', [])) |
Patrick Rohr | 53dcd10 | 2022-11-15 21:53:02 -0800 | [diff] [blame] | 366 | target.arch[arch].sources.update(desc.get('sources', [])) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 367 | outs = [re.sub('^//out/.+?/gen/', '', x) for x in desc['outputs']] |
| 368 | target.outputs.update(outs) |
| 369 | target.script = desc['script'] |
Patrick Rohr | 7aa98f9 | 2022-10-28 11:16:36 -0700 | [diff] [blame] | 370 | target.args = desc['args'] |
Patrick Rohr | 09716f5 | 2022-10-27 13:02:36 -0700 | [diff] [blame] | 371 | target.response_file_contents = self._get_response_file_contents(desc) |
Patrick Rohr | da778a0 | 2022-10-25 16:17:31 -0700 | [diff] [blame] | 372 | elif target.type == 'copy': |
| 373 | # TODO: copy rules are not currently implemented. |
| 374 | self.actions[gn_target_name] = target |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 375 | |
| 376 | # Default for 'public' is //* - all headers in 'sources' are public. |
| 377 | # TODO(primiano): if a 'public' section is specified (even if empty), then |
| 378 | # the rest of 'sources' is considered inaccessible by gn. Consider |
| 379 | # emulating that, so that generated build files don't end up with overly |
| 380 | # accessible headers. |
| 381 | public_headers = [x for x in desc.get('public', []) if x != '*'] |
| 382 | target.public_headers.update(public_headers) |
| 383 | |
Motomu Utsumi | 1c64e44 | 2022-11-17 21:51:37 +0900 | [diff] [blame] | 384 | target.arch[arch].cflags.update(desc.get('cflags', []) + desc.get('cflags_cc', [])) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 385 | target.libs.update(desc.get('libs', [])) |
| 386 | target.ldflags.update(desc.get('ldflags', [])) |
Motomu Utsumi | 80e0447 | 2022-11-17 21:54:06 +0900 | [diff] [blame] | 387 | target.arch[arch].defines.update(desc.get('defines', [])) |
Motomu Utsumi | 778f830 | 2022-11-17 22:12:17 +0900 | [diff] [blame] | 388 | target.arch[arch].include_dirs.update(desc.get('include_dirs', [])) |
Motomu Utsumi | ee47af6 | 2022-11-30 16:41:15 +0900 | [diff] [blame] | 389 | target.output_name = desc.get('output_name', None) |
Mohannad Farrag | baf0d57 | 2022-11-22 11:53:54 +0000 | [diff] [blame] | 390 | if "-frtti" in target.arch[arch].cflags: |
| 391 | target.rtti = True |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 392 | |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 393 | # Recurse in dependencies. |
Patrick Rohr | 7f4631e | 2022-11-15 14:35:03 -0800 | [diff] [blame] | 394 | for gn_dep_name in desc.get('deps', []): |
Patrick Rohr | 0c7ef52 | 2022-12-12 20:29:19 -0800 | [diff] [blame] | 395 | dep = self.parse_gn_desc(gn_desc, gn_dep_name, is_java_target) |
Patrick Rohr | 0913f0b | 2022-12-13 09:13:20 -0800 | [diff] [blame] | 396 | if dep is None: |
| 397 | continue |
| 398 | elif dep.type == 'proto_library': |
Patrick Rohr | 9006b36 | 2022-11-16 21:49:53 -0800 | [diff] [blame] | 399 | target.proto_deps.add(dep.name) |
| 400 | target.transitive_proto_deps.add(dep.name) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 401 | target.proto_paths.update(dep.proto_paths) |
| 402 | target.transitive_proto_deps.update(dep.transitive_proto_deps) |
| 403 | elif dep.type == 'source_set': |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 404 | target.arch[arch].source_set_deps.add(dep.name) |
| 405 | target.arch[arch].source_set_deps.update(dep.arch[arch].source_set_deps) |
Patrick Rohr | 7069556 | 2022-12-14 19:45:34 -0800 | [diff] [blame^] | 406 | # flatten source_set deps |
| 407 | if target.is_linker_unit_type(): |
| 408 | target.arch[arch].deps.update(target.arch[arch].source_set_deps) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 409 | elif dep.type == 'group': |
Motomu Utsumi | 1c64e44 | 2022-11-17 21:51:37 +0900 | [diff] [blame] | 410 | target.update(dep, arch) # Bubble up groups's cflags/ldflags etc. |
Patrick Rohr | da778a0 | 2022-10-25 16:17:31 -0700 | [diff] [blame] | 411 | elif dep.type in ['action', 'action_foreach', 'copy']: |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 412 | if proto_target_type is None: |
Mohannad Farrag | 1de6cb1 | 2022-11-28 12:27:26 +0000 | [diff] [blame] | 413 | target.arch[arch].deps.add(dep.name) |
Patrick Rohr | 94c2490 | 2022-12-14 19:44:44 -0800 | [diff] [blame] | 414 | elif dep.is_linker_unit_type(): |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 415 | target.arch[arch].deps.add(dep.name) |
Patrick Rohr | 3624f95 | 2022-11-04 14:30:18 -0700 | [diff] [blame] | 416 | elif dep.type == 'java_group': |
| 417 | # Explicitly break dependency chain when a java_group is added. |
| 418 | # Java sources are collected and eventually compiled as one large |
| 419 | # java_library. |
| 420 | pass |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 421 | |
Mohannad Farrag | 7f29d83 | 2022-11-23 19:52:41 +0000 | [diff] [blame] | 422 | # Source set bubble up transitive source sets but can't be combined with this |
| 423 | # if they are combined then source sets will bubble up static libraries |
| 424 | # while we only want to have source sets bubble up only source sets. |
Patrick Rohr | 5de9f2e | 2022-11-11 15:33:20 -0800 | [diff] [blame] | 425 | if dep.type == 'static_library': |
| 426 | # Bubble up static_libs. Necessary, since soong does not propagate |
| 427 | # static_libs up the build tree. |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 428 | target.arch[arch].transitive_static_libs_deps.add(dep.name) |
Patrick Rohr | a9c1dda | 2022-11-14 19:02:40 -0800 | [diff] [blame] | 429 | |
Patrick Rohr | 297f979 | 2022-11-16 23:40:40 -0800 | [diff] [blame] | 430 | if arch in dep.arch: |
| 431 | target.arch[arch].transitive_static_libs_deps.update( |
| 432 | dep.arch[arch].transitive_static_libs_deps) |
| 433 | target.arch[arch].deps.update(target.arch[arch].transitive_static_libs_deps) |
Patrick Rohr | 5de9f2e | 2022-11-11 15:33:20 -0800 | [diff] [blame] | 434 | |
Patrick Rohr | b27587e | 2022-11-04 14:57:24 -0700 | [diff] [blame] | 435 | # Collect java sources. Java sources are kept inside the __compile_java target. |
| 436 | # This target can be used for both host and target compilation; only add |
| 437 | # the sources if they are destined for the target (i.e. they are a |
| 438 | # dependency of the __dex target) |
| 439 | # Note: this skips prebuilt java dependencies. These will have to be |
| 440 | # added manually when building the jar. |
Patrick Rohr | 0c7ef52 | 2022-12-12 20:29:19 -0800 | [diff] [blame] | 441 | if target.name.endswith('__dex'): |
| 442 | if dep.name.endswith('__compile_java'): |
Patrick Rohr | b27587e | 2022-11-04 14:57:24 -0700 | [diff] [blame] | 443 | log.debug('Adding java sources for %s', dep.name) |
Mohannad Farrag | a0db68b | 2022-11-24 12:28:09 +0000 | [diff] [blame] | 444 | java_srcs = [src for src in dep.inputs if _is_java_source(src)] |
Patrick Rohr | b27587e | 2022-11-04 14:57:24 -0700 | [diff] [blame] | 445 | self.java_sources.update(java_srcs) |
Mohannad Farrag | 6a2d88a | 2022-11-28 19:33:48 +0000 | [diff] [blame] | 446 | if dep.type in ["action"] and target.type == "java_group": |
Motomu Utsumi | 6e51412 | 2022-12-05 17:51:40 +0900 | [diff] [blame] | 447 | # //base:base_java_aidl generates srcjar from .aidl files. But java_library in soong can |
| 448 | # directly have .aidl files in srcs. So adding .aidl files to the java_sources. |
| 449 | # TODO: Find a better way/place to do this. |
| 450 | if dep.name == '//base:base_java_aidl': |
| 451 | self.java_sources.update(dep.arch[arch].sources) |
| 452 | else: |
| 453 | self.java_actions.add(dep.name) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 454 | return target |
| 455 | |
| 456 | def get_proto_exports(self, proto_desc): |
| 457 | # exports in metadata will be available for source_set targets. |
| 458 | metadata = proto_desc.get('metadata', {}) |
| 459 | return metadata.get('exports', []) |
| 460 | |
| 461 | def get_proto_paths(self, proto_desc): |
| 462 | # import_dirs in metadata will be available for source_set targets. |
| 463 | metadata = proto_desc.get('metadata', {}) |
| 464 | return metadata.get('import_dirs', []) |
| 465 | |
Motomu Utsumi | d7e0e42 | 2022-11-08 17:49:52 +0900 | [diff] [blame] | 466 | |
| 467 | def get_proto_in_dir(self, proto_desc): |
| 468 | args = proto_desc.get('args') |
| 469 | return re.sub('^\.\./\.\./', '', args[args.index('--proto-in-dir') + 1]) |
| 470 | |
Patrick Rohr | 0d40da3 | 2022-11-15 13:08:12 -0800 | [diff] [blame] | 471 | def get_proto_target_type(self, gn_desc, gn_target_name): |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 472 | """ Checks if the target is a proto library and return the plugin. |
| 473 | |
| 474 | Returns: |
| 475 | (None, None): if the target is not a proto library. |
| 476 | (plugin, proto_desc) where |plugin| is 'proto' in the default (lite) |
| 477 | case or 'protozero' or 'ipc' or 'descriptor'; |proto_desc| is the GN |
| 478 | json desc of the target with the .proto sources (_gen target for |
| 479 | non-descriptor types or the target itself for descriptor type). |
| 480 | """ |
Patrick Rohr | 0d40da3 | 2022-11-15 13:08:12 -0800 | [diff] [blame] | 481 | parts = gn_target_name.split('(', 1) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 482 | name = parts[0] |
| 483 | toolchain = '(' + parts[1] if len(parts) > 1 else '' |
| 484 | |
| 485 | # Descriptor targets don't have a _gen target; instead we look for the |
| 486 | # characteristic flag in the args of the target itself. |
Patrick Rohr | 0d40da3 | 2022-11-15 13:08:12 -0800 | [diff] [blame] | 487 | desc = gn_desc.get(gn_target_name) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 488 | if '--descriptor_set_out' in desc.get('args', []): |
| 489 | return 'descriptor', desc |
| 490 | |
| 491 | # Source set proto targets have a non-empty proto_library_sources in the |
| 492 | # metadata of the description. |
| 493 | metadata = desc.get('metadata', {}) |
| 494 | if 'proto_library_sources' in metadata: |
| 495 | return 'source_set', desc |
| 496 | |
| 497 | # In all other cases, we want to look at the _gen target as that has the |
| 498 | # important information. |
Patrick Rohr | 564d6be | 2022-11-15 12:57:57 -0800 | [diff] [blame] | 499 | gen_desc = gn_desc.get('%s_gen%s' % (name, toolchain)) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 500 | if gen_desc is None or gen_desc['type'] != 'action': |
| 501 | return None, None |
Patrick Rohr | c598078 | 2022-11-07 16:34:03 -0800 | [diff] [blame] | 502 | if gen_desc['script'] != '//tools/protoc_wrapper/protoc_wrapper.py': |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 503 | return None, None |
| 504 | plugin = 'proto' |
Patrick Rohr | c598078 | 2022-11-07 16:34:03 -0800 | [diff] [blame] | 505 | args = gen_desc.get('args', []) |
Patrick Rohr | 92d7412 | 2022-10-21 15:50:52 -0700 | [diff] [blame] | 506 | for arg in (arg for arg in args if arg.startswith('--plugin=')): |
| 507 | # |arg| at this point looks like: |
| 508 | # --plugin=protoc-gen-plugin=gcc_like_host/protozero_plugin |
| 509 | # or |
| 510 | # --plugin=protoc-gen-plugin=protozero_plugin |
| 511 | plugin = arg.split('=')[-1].split('/')[-1].replace('_plugin', '') |
| 512 | return plugin, gen_desc |