• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright (C) 2022 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This tool translates a collection of BUILD.gn files into a mostly equivalent
17# Android.bp file for the Android Soong build system. The input to the tool is a
18# JSON description of the GN build definition generated with the following
19# command:
20#
21#   gn desc out --format=json --all-toolchains "//*" > desc.json
22#
23# The tool is then given a list of GN labels for which to generate Android.bp
24# build rules. The dependencies for the GN labels are squashed to the generated
25# Android.bp target, except for actions which get their own genrule. Some
26# libraries are also mapped to their Android equivalents -- see |builtin_deps|.
27
28import argparse
29import json
30import logging as log
31import operator
32import os
33import re
34import sys
35import copy
36from pathlib import Path
37
38import gn_utils
39
40ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
41
42CRONET_LICENSE_NAME = "external_cronet_license"
43
44# Default targets to translate to the blueprint file.
45DEFAULT_TARGETS = [
46    "//components/cronet/android:cronet_api_java",
47    '//components/cronet/android:cronet',
48    '//components/cronet/android:cronet_impl_native_base_java',
49    '//components/cronet/android:cronet_jni_registration_java',
50]
51
52DEFAULT_TESTS = [
53  '//components/cronet/android:cronet_unittests_android__library',
54  '//net:net_unittests__library',
55  '//components/cronet/android:cronet_tests',
56  '//components/cronet/android:cronet',
57  '//components/cronet/android:cronet_javatests',
58  '//components/cronet/android:cronet_jni_registration_java',
59  '//components/cronet/android:cronet_tests_jni_registration_java',
60  '//testing/android/native_test:native_test_java',
61  '//net/android:net_test_support_provider_java',
62  '//net/android:net_tests_java',
63  '//third_party/netty-tcnative:netty-tcnative-so',
64  '//third_party/netty4:netty_all_java',
65]
66
67EXTRAS_ANDROID_BP_FILE = "Android.extras.bp"
68
69CRONET_API_MODULE_NAME = "cronet_aml_api_java"
70
71# All module names are prefixed with this string to avoid collisions.
72module_prefix = 'cronet_aml_'
73
74REMOVE_GEN_JNI_JARJAR_RULES_FILE = "android/tools/remove-gen-jni-jarjar-rules.txt"
75# Shared libraries which are directly translated to Android system equivalents.
76shared_library_allowlist = [
77    'android',
78    'log',
79]
80
81# Include directories that will be removed from all targets.
82local_include_dirs_denylist = [
83    'third_party/zlib/',
84]
85
86# Name of the module which settings such as compiler flags for all other
87# modules.
88defaults_module = module_prefix + 'defaults'
89
90# Location of the project in the Android source tree.
91tree_path = 'external/cronet'
92
93# Path for the protobuf sources in the standalone build.
94buildtools_protobuf_src = '//buildtools/protobuf/src'
95
96# Location of the protobuf src dir in the Android source tree.
97android_protobuf_src = 'external/protobuf/src'
98
99# put all args on a new line for better diffs.
100NEWLINE = ' " +\n         "'
101
102# Compiler flags which are passed through to the blueprint.
103cflag_allowlist = [
104  # needed for zlib:zlib
105  "-mpclmul",
106  # needed for zlib:zlib
107  "-mssse3",
108  # needed for zlib:zlib
109  "-msse3",
110  # needed for zlib:zlib
111  "-msse4.2",
112  # flags to reduce binary size
113  "-O1",
114  "-O2",
115  "-O3",
116  "-Oz",
117  "-g1",
118  "-g2",
119  "-fdata-sections",
120  "-ffunction-sections",
121  "-fvisibility=hidden",
122  "-fvisibility-inlines-hidden",
123  "-fstack-protector",
124  "-mno-outline",
125  "-mno-outline-atomics",
126  "-fno-asynchronous-unwind-tables",
127  "-fno-unwind-tables",
128]
129
130# Linker flags which are passed through to the blueprint.
131ldflag_allowlist = [
132  # flags to reduce binary size
133  "-Wl,--as-needed",
134  "-Wl,--gc-sections",
135  "-Wl,--icf=all",
136]
137
138def get_linker_script_ldflag(script_path):
139  return f'-Wl,--script,{tree_path}/{script_path}'
140
141# Additional arguments to apply to Android.bp rules.
142additional_args = {
143    'cronet_aml_net_third_party_quiche_net_quic_test_tools_proto_gen_headers': [
144      ('export_include_dirs', {
145        "net/third_party/quiche/src",
146      })
147    ],
148    'cronet_aml_net_third_party_quiche_net_quic_test_tools_proto_gen__testing_headers': [
149        ('export_include_dirs', {
150        "net/third_party/quiche/src",
151        })
152     ],
153    'cronet_aml_third_party_quic_trace_quic_trace_proto_gen__testing_headers': [
154        ('export_include_dirs', {
155          "third_party/quic_trace/src",
156        })
157    ],
158    # TODO: fix upstream. Both //base:base and
159    # //base/allocator/partition_allocator:partition_alloc do not create a
160    # dependency on gtest despite using gtest_prod.h.
161    'cronet_aml_base_base': [
162        ('header_libs', {
163            'libgtest_prod_headers',
164        }),
165        ('export_header_lib_headers', {
166            'libgtest_prod_headers',
167        }),
168    ],
169    'cronet_aml_base_allocator_partition_allocator_partition_alloc': [
170        ('header_libs', {
171            'libgtest_prod_headers',
172        }),
173    ],
174    # TODO(b/309920629): Remove once upstreamed.
175    'cronet_aml_components_cronet_android_cronet_api_java': [
176        ('srcs', {
177            'components/cronet/android/api/src/org/chromium/net/UploadDataProviders.java',
178            'components/cronet/android/api/src/org/chromium/net/apihelpers/UploadDataProviders.java',
179        }),
180    ],
181    'cronet_aml_components_cronet_android_cronet_api_java__testing': [
182        ('srcs', {
183            'components/cronet/android/api/src/org/chromium/net/UploadDataProviders.java',
184            'components/cronet/android/api/src/org/chromium/net/apihelpers/UploadDataProviders.java',
185        }),
186    ],
187    'cronet_aml_components_cronet_android_cronet_javatests__testing': [
188        # Needed to @SkipPresubmit annotations
189        ('static_libs', {
190            'net-tests-utils',
191        }),
192        # This is necessary because net-tests-utils compiles against private SDK.
193        ('sdk_version', ""),
194    ],
195    'cronet_aml_components_cronet_android_cronet__testing': [
196        ('target',  ('android_riscv64', {'stem': "libmainlinecronet_riscv64"})),
197        ('comment', """TODO: remove stem for riscv64
198// This is essential as there can't be two different modules
199// with the same output. We usually got away with that because
200// the non-testing Cronet is part of the Tethering APEX and the
201// testing Cronet is not part of the Tethering APEX which made them
202// look like two different outputs from the build system perspective.
203// However, Cronet does not ship to Tethering APEX for RISCV64 which
204// raises the conflict. Once we start shipping Cronet for RISCV64,
205// this can be removed."""),
206      ],
207    'cronet_aml_third_party_netty_tcnative_netty_tcnative_so__testing': [
208        ('cflags', {
209            "-Wno-error=pointer-bool-conversion"
210        })
211    ],
212    'cronet_aml_third_party_apache_portable_runtime_apr__testing': [
213        ('cflags', {
214            "-Wno-incompatible-pointer-types-discards-qualifiers",
215        })
216    ],
217    # TODO(b/324872305): Remove when gn desc expands public_configs and update code to propagate the
218    # include_dir from the public_configs
219    # We had to add the export_include_dirs for each target because soong generates each header
220    # file in a specific directory named after the target.
221    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_chromecast_buildflags': [
222      ('export_include_dirs', {
223        "base/allocator/partition_allocator/src/",
224      })
225    ],
226    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_chromecast_buildflags__testing': [
227      ('export_include_dirs', {
228        "base/allocator/partition_allocator/src/",
229      })
230    ],
231    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_chromeos_buildflags': [
232      ('export_include_dirs', {
233        "base/allocator/partition_allocator/src/",
234      })
235    ],
236    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_chromeos_buildflags__testing': [
237      ('export_include_dirs', {
238        "base/allocator/partition_allocator/src/",
239      })
240    ],
241    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_debugging_buildflags': [
242      ('export_include_dirs', {
243        "base/allocator/partition_allocator/src/",
244      })
245    ],
246    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_debugging_buildflags__testing': [
247      ('export_include_dirs', {
248        "base/allocator/partition_allocator/src/",
249      })
250    ],
251    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_partition_alloc_buildflags': [
252      ('export_include_dirs', {
253        ".",
254        "base/allocator/partition_allocator/src/",
255      })
256    ],
257    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_partition_alloc_buildflags__testing': [
258      ('export_include_dirs', {
259        ".",
260        "base/allocator/partition_allocator/src/",
261      })
262    ],
263    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_raw_ptr_buildflags': [
264      ('export_include_dirs', {
265        "base/allocator/partition_allocator/src/",
266      })
267    ],
268    'cronet_aml_base_allocator_partition_allocator_src_partition_alloc_raw_ptr_buildflags__testing': [
269      ('export_include_dirs', {
270        "base/allocator/partition_allocator/src/",
271      })
272    ],
273    # end export_include_dir.
274}
275
276def always_disable(module, arch):
277  return None
278
279def enable_zlib(module, arch):
280  # Requires crrev/c/4109079
281  if arch == 'common':
282    module.shared_libs.add('libz')
283  else:
284    module.target[arch].shared_libs.add('libz')
285
286def enable_boringssl(module, arch):
287  # Do not add boringssl targets to cc_genrules. This happens, because protobuf targets are
288  # originally static_libraries, but later get converted to a cc_genrule.
289  if module.is_genrule(): return
290  # Lets keep statically linking BoringSSL for testing target for now. This should be fixed.
291  if module.name.endswith(gn_utils.TESTING_SUFFIX): return
292  if arch == 'common':
293    shared_libs = module.shared_libs
294  else:
295    shared_libs = module.target[arch].shared_libs
296  shared_libs.add('//external/cronet/third_party/boringssl:libcrypto')
297  shared_libs.add('//external/cronet/third_party/boringssl:libssl')
298  shared_libs.add('//external/cronet/third_party/boringssl:libpki')
299
300def add_androidx_experimental_java_deps(module, arch):
301  module.libs.add("androidx.annotation_annotation-experimental")
302
303def add_androidx_annotation_java_deps(module, arch):
304  module.libs.add("androidx.annotation_annotation")
305
306def add_protobuf_lite_runtime_java_deps(module, arch):
307  module.static_libs.add("libprotobuf-java-lite")
308
309def add_androidx_core_java_deps(module, arch):
310  module.libs.add("androidx.core_core")
311
312def add_jsr305_java_deps(module, arch):
313  module.libs.add("jsr305")
314
315def add_errorprone_annotation_java_deps(module, arch):
316  module.libs.add("error_prone_annotations")
317
318def add_androidx_collection_java_deps(module, arch):
319  module.libs.add("androidx.collection_collection")
320
321def add_junit_java_deps(module, arch):
322  module.static_libs.add("junit")
323
324def add_truth_java_deps(module, arch):
325  module.static_libs.add("truth")
326
327def add_hamcrest_java_deps(module, arch):
328  module.static_libs.add("hamcrest-library")
329  module.static_libs.add("hamcrest")
330
331def add_mockito_java_deps(module, arch):
332  module.static_libs.add("mockito")
333
334def add_guava_java_deps(module, arch):
335  module.static_libs.add("guava")
336
337def add_androidx_junit_java_deps(module, arch):
338  module.static_libs.add("androidx.test.ext.junit")
339
340def add_androidx_test_runner_java_deps(module, arch):
341  module.static_libs.add("androidx.test.runner")
342
343def add_android_test_base_java_deps(module, arch):
344  module.libs.add("android.test.base")
345
346def add_accessibility_test_framework_java_deps(module, arch):
347  module.static_libs.add("accessibility-test-framework")
348
349def add_espresso_java_deps(module, arch):
350  module.static_libs.add("androidx.test.espresso.contrib")
351
352def add_android_test_mock_java_deps(module, arch):
353  module.libs.add("android.test.mock")
354
355def add_androidx_multidex_java_deps(module, arch):
356  # Androidx-multidex is disabled on unbundled branches.
357  pass
358
359def add_androidx_test_monitor_java_deps(module, arch):
360  module.libs.add("androidx.test.monitor")
361
362def add_androidx_ui_automator_java_deps(module, arch):
363  module.static_libs.add("androidx.test.uiautomator_uiautomator")
364
365def add_androidx_test_annotation_java_deps(module, arch):
366  module.static_libs.add("androidx.test.rules")
367
368def add_androidx_test_core_java_deps(module, arch):
369  module.static_libs.add("androidx.test.core")
370
371def add_androidx_activity_activity(module, arch):
372  module.static_libs.add("androidx.activity_activity")
373
374def add_androidx_fragment_fragment(module, arch):
375  module.static_libs.add("androidx.fragment_fragment")
376
377# Android equivalents for third-party libraries that the upstream project
378# depends on. This will be applied to normal and testing targets.
379_builtin_deps = {
380    '//buildtools/third_party/libunwind:libunwind':
381        always_disable,
382    '//net/data/ssl/chrome_root_store:gen_root_store_inc':
383        always_disable,
384    '//net/tools/root_store_tool:root_store_tool':
385        always_disable,
386    '//third_party/zlib:zlib':
387        enable_zlib,
388    '//third_party/androidx:androidx_annotation_annotation_java':
389        add_androidx_annotation_java_deps,
390    '//third_party/android_deps:protobuf_lite_runtime_java':
391        add_protobuf_lite_runtime_java_deps,
392    '//third_party/androidx:androidx_annotation_annotation_experimental_java':
393        add_androidx_experimental_java_deps,
394    '//third_party/androidx:androidx_core_core_java':
395        add_androidx_core_java_deps,
396    '//third_party/android_deps:com_google_code_findbugs_jsr305_java':
397        add_jsr305_java_deps,
398    '//third_party/android_deps:com_google_errorprone_error_prone_annotations_java':
399        add_errorprone_annotation_java_deps,
400    '//third_party/androidx:androidx_collection_collection_java':
401        add_androidx_collection_java_deps,
402    '//third_party/junit:junit':
403        add_junit_java_deps,
404    '//third_party/google-truth:google_truth_java':
405        add_truth_java_deps,
406    '//third_party/hamcrest:hamcrest_core_java':
407        add_hamcrest_java_deps,
408    '//third_party/mockito:mockito_java':
409        add_mockito_java_deps,
410    '//third_party/android_deps:guava_android_java':
411        add_guava_java_deps,
412    '//third_party/androidx:androidx_test_ext_junit_java':
413        add_androidx_junit_java_deps,
414    '//third_party/androidx:androidx_test_runner_java':
415        add_androidx_test_runner_java_deps,
416    '//third_party/android_sdk:android_test_base_java':
417        add_android_test_base_java_deps,
418    '//third_party/accessibility_test_framework:accessibility_test_framework_java':
419        add_accessibility_test_framework_java_deps,
420    '//third_party/android_deps:espresso_java':
421        add_espresso_java_deps,
422    '//third_party/android_sdk:android_test_mock_java':
423        add_android_test_mock_java_deps,
424    '//third_party/androidx:androidx_multidex_multidex_java':
425        add_androidx_multidex_java_deps,
426    '//third_party/androidx:androidx_test_monitor_java':
427        add_androidx_test_monitor_java_deps,
428    '//third_party/androidx:androidx_test_annotation_java':
429        add_androidx_test_annotation_java_deps,
430    '//third_party/androidx:androidx_test_core_java':
431        add_androidx_test_core_java_deps,
432    '//third_party/androidx:androidx_test_uiautomator_uiautomator_java':
433        add_androidx_ui_automator_java_deps,
434    '//third_party/hamcrest:hamcrest_java':
435        add_hamcrest_java_deps,
436    '//third_party/androidx:androidx_activity_activity_java':
437        add_androidx_activity_activity,
438    '//third_party/androidx:androidx_fragment_fragment_java':
439        add_androidx_fragment_fragment,
440}
441builtin_deps = {"{}{}".format(key, suffix): value for key, value in _builtin_deps.items() for suffix in ["", gn_utils.TESTING_SUFFIX] }
442
443# Same as _builtin_deps but will only apply what is explicitly specified.
444builtin_deps.update({
445  '//third_party/boringssl:boringssl':
446    enable_boringssl,
447  '//third_party/boringssl:boringssl_asm':
448  # Due to FIPS requirements, downstream BoringSSL has a different "shape" than upstream's.
449  # We're guaranteed that if X depends on :boringssl it will also depend on :boringssl_asm.
450  # Hence, always drop :boringssl_asm and handle the translation entirely in :boringssl.
451    always_disable,
452})
453
454
455# Name of tethering apex module
456tethering_apex = "com.android.tethering"
457
458# Name of cronet api target
459java_api_target_name = "//components/cronet/android:cronet_api_java"
460
461# Visibility set for package default
462package_default_visibility = ":__subpackages__"
463
464# Visibility set for modules used from Connectivity and within external/cronet
465root_modules_visibility = {"//packages/modules/Connectivity:__subpackages__",
466                           "//external/cronet:__subpackages__"}
467
468# ----------------------------------------------------------------------------
469# End of configuration.
470# ----------------------------------------------------------------------------
471
472def write_blueprint_key_value(output, name, value, sort=True):
473  """Writes a Blueprint key-value pair to the output"""
474
475  if isinstance(value, bool):
476    if value:
477      output.append('    %s: true,' % name)
478    else:
479      output.append('    %s: false,' % name)
480    return
481  if not value:
482    return
483  if isinstance(value, set):
484    value = sorted(value)
485  if isinstance(value, list):
486    output.append('    %s: [' % name)
487    for item in sorted(value) if sort else value:
488      output.append('        "%s",' % item)
489    output.append('    ],')
490    return
491  if isinstance(value, Module.Target):
492    value.to_string(output)
493    return
494  if isinstance(value, dict):
495    kv_output = []
496    for k, v in value.items():
497      write_blueprint_key_value(kv_output, k, v)
498
499    output.append('    %s: {' % name)
500    for line in kv_output:
501      output.append('    %s' % line)
502    output.append('    },')
503    return
504  output.append('    %s: "%s",' % (name, value))
505
506
507
508class Module(object):
509  """A single module (e.g., cc_binary, cc_test) in a blueprint."""
510
511  class Target(object):
512    """A target-scoped part of a module"""
513
514    def __init__(self, name):
515      self.name = name
516      self.srcs = set()
517      self.shared_libs = set()
518      self.static_libs = set()
519      self.whole_static_libs = set()
520      self.header_libs = set()
521      self.cflags = set()
522      self.stl = None
523      self.cppflags = set()
524      self.local_include_dirs = set()
525      self.generated_headers = set()
526      self.export_generated_headers = set()
527      self.ldflags = set()
528      self.compile_multilib = None
529      self.stem = ""
530      if name == 'host':
531        self.compile_multilib = '64'
532
533    def to_string(self, output):
534      nested_out = []
535      self._output_field(nested_out, 'srcs')
536      self._output_field(nested_out, 'shared_libs')
537      self._output_field(nested_out, 'static_libs')
538      self._output_field(nested_out, 'whole_static_libs')
539      self._output_field(nested_out, 'header_libs')
540      self._output_field(nested_out, 'cflags')
541      self._output_field(nested_out, 'stl')
542      self._output_field(nested_out, 'cppflags')
543      self._output_field(nested_out, 'local_include_dirs')
544      self._output_field(nested_out, 'generated_headers')
545      self._output_field(nested_out, 'export_generated_headers')
546      self._output_field(nested_out, 'ldflags')
547      self._output_field(nested_out, 'stem')
548
549      if nested_out:
550        # This is added here to make sure it doesn't add a `host` arch-specific module just for
551        # `compile_multilib` flag.
552        self._output_field(nested_out, 'compile_multilib')
553        output.append('    %s: {' % self.name)
554        for line in nested_out:
555          output.append('    %s' % line)
556        output.append('    },')
557
558    def _output_field(self, output, name, sort=True):
559      value = getattr(self, name)
560      return write_blueprint_key_value(output, name, value, sort)
561
562
563  def __init__(self, mod_type, name, gn_target):
564    self.type = mod_type
565    self.gn_target = gn_target
566    self.name = name
567    self.srcs = set()
568    self.comment = 'GN: ' + gn_target
569    self.shared_libs = set()
570    self.static_libs = set()
571    self.whole_static_libs = set()
572    self.tools = set()
573    self.cmd = None
574    self.host_supported = False
575    self.device_supported = True
576    self.init_rc = set()
577    self.out = set()
578    self.export_include_dirs = set()
579    self.generated_headers = set()
580    self.export_generated_headers = set()
581    self.export_static_lib_headers = set()
582    self.export_header_lib_headers = set()
583    self.defaults = set()
584    self.cflags = set()
585    self.include_dirs = set()
586    self.local_include_dirs = set()
587    self.header_libs = set()
588    self.tool_files = set()
589    # target contains a dict of Targets indexed by os_arch.
590    # example: { 'android_x86': Target('android_x86')
591    self.target = dict()
592    self.target['android'] = self.Target('android')
593    self.target['android_x86'] = self.Target('android_x86')
594    self.target['android_x86_64'] = self.Target('android_x86_64')
595    self.target['android_arm'] = self.Target('android_arm')
596    self.target['android_arm64'] = self.Target('android_arm64')
597    self.target['android_riscv64'] = self.Target('android_riscv64')
598    self.target['host'] = self.Target('host')
599    self.target['glibc'] = self.Target('glibc')
600    self.stl = None
601    self.cpp_std = None
602    self.strip = dict()
603    self.data = set()
604    self.apex_available = set()
605    self.min_sdk_version = None
606    self.proto = dict()
607    self.linker_scripts = set()
608    self.ldflags = set()
609    # The genrule_XXX below are properties that must to be propagated back
610    # on the module(s) that depend on the genrule.
611    self.genrule_headers = set()
612    self.genrule_srcs = set()
613    self.genrule_shared_libs = set()
614    self.genrule_header_libs = set()
615    self.version_script = None
616    self.test_suites = set()
617    self.test_config = None
618    self.cppflags = set()
619    self.rtti = False
620    # Name of the output. Used for setting .so file name for libcronet
621    self.libs = set()
622    self.stem = None
623    self.compile_multilib = None
624    self.aidl = dict()
625    self.plugins = set()
626    self.processor_class = None
627    self.sdk_version = None
628    self.javacflags = set()
629    self.c_std = None
630    self.default_applicable_licenses = set()
631    self.default_visibility = []
632    self.visibility = set()
633    self.gn_type = None
634    self.jarjar_rules = ""
635    self.jars = set()
636
637  def to_string(self, output):
638    if self.comment:
639      output.append('// %s' % self.comment)
640    output.append('%s {' % self.type)
641    self._output_field(output, 'name')
642    self._output_field(output, 'srcs')
643    self._output_field(output, 'shared_libs')
644    self._output_field(output, 'static_libs')
645    self._output_field(output, 'whole_static_libs')
646    self._output_field(output, 'tools')
647    self._output_field(output, 'cmd', sort=False)
648    if self.host_supported:
649      self._output_field(output, 'host_supported')
650    if not self.device_supported:
651      self._output_field(output, 'device_supported')
652    self._output_field(output, 'init_rc')
653    self._output_field(output, 'out')
654    self._output_field(output, 'export_include_dirs')
655    self._output_field(output, 'generated_headers')
656    self._output_field(output, 'export_generated_headers')
657    self._output_field(output, 'export_static_lib_headers')
658    self._output_field(output, 'export_header_lib_headers')
659    self._output_field(output, 'defaults')
660    self._output_field(output, 'cflags')
661    self._output_field(output, 'include_dirs')
662    self._output_field(output, 'local_include_dirs')
663    self._output_field(output, 'header_libs')
664    self._output_field(output, 'strip')
665    self._output_field(output, 'tool_files')
666    self._output_field(output, 'data')
667    self._output_field(output, 'stl')
668    self._output_field(output, 'cpp_std')
669    self._output_field(output, 'apex_available')
670    self._output_field(output, 'min_sdk_version')
671    self._output_field(output, 'version_script')
672    self._output_field(output, 'test_suites')
673    self._output_field(output, 'test_config')
674    self._output_field(output, 'proto')
675    self._output_field(output, 'linker_scripts')
676    self._output_field(output, 'ldflags')
677    self._output_field(output, 'cppflags')
678    self._output_field(output, 'libs')
679    self._output_field(output, 'stem')
680    self._output_field(output, 'compile_multilib')
681    self._output_field(output, 'aidl')
682    self._output_field(output, 'plugins')
683    self._output_field(output, 'processor_class')
684    self._output_field(output, 'sdk_version')
685    self._output_field(output, 'javacflags')
686    self._output_field(output, 'c_std')
687    self._output_field(output, 'default_applicable_licenses')
688    self._output_field(output, 'default_visibility')
689    self._output_field(output, 'visibility')
690    self._output_field(output, 'jarjar_rules')
691    self._output_field(output, 'jars')
692    if self.rtti:
693      self._output_field(output, 'rtti')
694
695    target_out = []
696    for arch, target in sorted(self.target.items()):
697      # _output_field calls getattr(self, arch).
698      setattr(self, arch, target)
699      self._output_field(target_out, arch)
700
701    if target_out:
702      output.append('    target: {')
703      for line in target_out:
704        output.append('    %s' % line)
705      output.append('    },')
706
707    output.append('}')
708    output.append('')
709
710  def add_android_shared_lib(self, lib):
711    if self.type.startswith('java'):
712      raise Exception('Adding Android shared lib for java_* targets is unsupported')
713    elif self.type == 'cc_binary_host':
714      raise Exception('Adding Android shared lib for host tool is unsupported')
715    elif self.host_supported:
716      self.target['android'].shared_libs.add(lib)
717    else:
718      self.shared_libs.add(lib)
719
720  def is_test(self):
721    if gn_utils.TESTING_SUFFIX in self.name:
722      name_without_prefix = self.name[:self.name.find(gn_utils.TESTING_SUFFIX)]
723      return any([name_without_prefix == label_to_module_name(target) for target in DEFAULT_TESTS])
724    return False
725
726  def _output_field(self, output, name, sort=True):
727    value = getattr(self, name)
728    return write_blueprint_key_value(output, name, value, sort)
729
730  def is_compiled(self):
731    return self.type not in ('cc_genrule', 'filegroup', 'java_genrule')
732
733  def is_genrule(self):
734    return self.type == "cc_genrule"
735
736  def has_input_files(self):
737    if self.type in ["java_library", "java_import"]:
738        return True
739    if len(self.srcs) > 0:
740      return True
741    if any([len(target.srcs) > 0 for target in self.target.values()]):
742      return True
743    # Allow cc_static_library with export_generated_headers as those are crucial for
744    # the depending modules
745    return len(self.export_generated_headers) > 0
746
747
748class Blueprint(object):
749  """In-memory representation of an Android.bp file."""
750
751  def __init__(self):
752    self.modules = {}
753
754  def add_module(self, module):
755    """Adds a new module to the blueprint, replacing any existing module
756        with the same name.
757
758        Args:
759            module: Module instance.
760        """
761    self.modules[module.name] = module
762
763  def to_string(self, output):
764    for m in sorted(self.modules.values(), key=lambda m: m.name):
765      if m.type != "cc_library_static" or m.has_input_files():
766        # Don't print cc_library_static with empty srcs. These attributes are already
767        # propagated up the tree. Printing them messes the presubmits because
768        # every module is compiled while those targets are not reachable in
769        # a normal compilation path.
770        m.to_string(output)
771
772
773def label_to_module_name(label):
774  """Turn a GN label (e.g., //:perfetto_tests) into a module name."""
775  module = re.sub(r'^//:?', '', label)
776  module = re.sub(r'[^a-zA-Z0-9_]', '_', module)
777
778  if not module.startswith(module_prefix):
779    return module_prefix + module
780  return module
781
782
783def is_supported_source_file(name):
784  """Returns True if |name| can appear in a 'srcs' list."""
785  return os.path.splitext(name)[1] in ['.c', '.cc', '.cpp', '.java', '.proto', '.S', '.aidl']
786
787
788def get_protoc_module_name(gn):
789  protoc_gn_target_name = gn.get_target('//third_party/protobuf:protoc').name
790  return label_to_module_name(protoc_gn_target_name)
791
792
793def create_proto_modules(blueprint, gn, target):
794  """Generate genrules for a proto GN target.
795
796    GN actions are used to dynamically generate files during the build. The
797    Soong equivalent is a genrule. This function turns a specific kind of
798    genrule which turns .proto files into source and header files into a pair
799    equivalent genrules.
800
801    Args:
802        blueprint: Blueprint instance which is being generated.
803        target: gn_utils.Target object.
804
805    Returns:
806        The source_genrule module.
807    """
808  assert (target.type == 'proto_library')
809
810  protoc_module_name = get_protoc_module_name(gn)
811  tools = {protoc_module_name}
812  cpp_out_dir = '$(genDir)/%s/%s/' % (tree_path, target.proto_in_dir)
813  target_module_name = label_to_module_name(target.name)
814
815  # In GN builds the proto path is always relative to the output directory
816  # (out/tmp.xxx).
817  cmd = ['$(location %s)' % protoc_module_name]
818  cmd += ['--proto_path=%s/%s' % (tree_path, target.proto_in_dir)]
819
820  for proto_path in target.proto_paths:
821    cmd += [f'--proto_path={tree_path}/{proto_path}']
822  if buildtools_protobuf_src in target.proto_paths:
823    cmd += ['--proto_path=%s' % android_protobuf_src]
824
825  # We don't generate any targets for source_set proto modules because
826  # they will be inlined into other modules if required.
827  if target.proto_plugin == 'source_set':
828    return None
829
830  # Descriptor targets only generate a single target.
831  if target.proto_plugin == 'descriptor':
832    out = '{}.bin'.format(target_module_name)
833
834    cmd += ['--descriptor_set_out=$(out)']
835    cmd += ['$(in)']
836
837    descriptor_module = Module('cc_genrule', target_module_name, target.name)
838    descriptor_module.cmd = ' '.join(cmd)
839    descriptor_module.out = [out]
840    descriptor_module.tools = tools
841    blueprint.add_module(descriptor_module)
842
843    # Recursively extract the .proto files of all the dependencies and
844    # add them to srcs.
845    descriptor_module.srcs.update(
846        gn_utils.label_to_path(src) for src in target.sources)
847    for dep in target.proto_deps:
848      current_target = gn.get_target(dep)
849      descriptor_module.srcs.update(
850          gn_utils.label_to_path(src) for src in current_target.sources)
851
852    return descriptor_module
853
854  # We create two genrules for each proto target: one for the headers and
855  # another for the sources. This is because the module that depends on the
856  # generated files needs to declare two different types of dependencies --
857  # source files in 'srcs' and headers in 'generated_headers' -- and it's not
858  # valid to generate .h files from a source dependency and vice versa.
859  source_module_name = target_module_name
860  source_module = Module('cc_genrule', source_module_name, target.name)
861  blueprint.add_module(source_module)
862  source_module.srcs.update(
863      gn_utils.label_to_path(src) for src in target.sources)
864
865  header_module = Module('cc_genrule', source_module_name + '_headers',
866                         target.name)
867  blueprint.add_module(header_module)
868  header_module.srcs = set(source_module.srcs)
869
870  # TODO(primiano): at some point we should remove this. This was introduced
871  # by aosp/1108421 when adding "protos/" to .proto include paths, in order to
872  # avoid doing multi-repo changes and allow old clients in the android tree
873  # to still do the old #include "perfetto/..." rather than
874  # #include "protos/perfetto/...".
875  header_module.export_include_dirs = {'.', 'protos'}
876  # Since the .cc file and .h get created by a different gerule target, they
877  # are not put in the same intermediate path, so local includes do not work
878  # without explictily exporting the include dir.
879  header_module.export_include_dirs.add(target.proto_in_dir)
880
881  # This function does not return header_module so setting apex_available attribute here.
882  header_module.apex_available.add(tethering_apex)
883
884  source_module.genrule_srcs.add(':' + source_module.name)
885  source_module.genrule_headers.add(header_module.name)
886
887  if target.proto_plugin == 'proto':
888    suffixes = ['pb']
889    source_module.genrule_shared_libs.add('libprotobuf-cpp-lite')
890    cmd += ['--cpp_out=lite=true:' + cpp_out_dir]
891  else:
892    raise Exception('Unsupported proto plugin: %s' % target.proto_plugin)
893
894  cmd += ['$(in)']
895  source_module.cmd = ' '.join(cmd)
896  header_module.cmd = source_module.cmd
897  source_module.tools = tools
898  header_module.tools = tools
899
900  for sfx in suffixes:
901    source_module.out.update('%s/%s' %
902                             (tree_path, src.replace('.proto', '.%s.cc' % sfx))
903                             for src in source_module.srcs)
904    header_module.out.update('%s/%s' %
905                             (tree_path, src.replace('.proto', '.%s.h' % sfx))
906                             for src in header_module.srcs)
907  # This has proto files that will be used for reference resolution
908  # but not compiled into cpp files. These additional sources has no output.
909  proto_data_sources = sorted([gn_utils.label_to_path(proto_src)
910                               for proto_src in target.inputs if proto_src.endswith(".proto")])
911  source_module.srcs.update(proto_data_sources)
912  header_module.srcs.update(proto_data_sources)
913  return source_module
914
915
916def create_gcc_preprocess_modules(blueprint, target):
917  # gcc_preprocess.py internally execute host gcc which is not allowed in genrule.
918  # So, this function create multiple modules and realize equivalent processing
919  # TODO: Consider to support gcc_preprocess.py in different way
920  # It's not great to have genrule and cc_object in the dependency from java_library
921  assert (len(target.sources) == 1)
922  source = list(target.sources)[0]
923  assert (Path(source).suffix == '.template')
924  stem = Path(source).stem
925
926  bp_module_name = label_to_module_name(target.name)
927
928  # Rename .template to .cc since cc_object does not accept .template file as srcs
929  rename_module = Module('genrule', bp_module_name + '_rename', target.name)
930  rename_module.srcs.add(gn_utils.label_to_path(source))
931  rename_module.out.add(stem + '.cc')
932  rename_module.cmd = 'cp $(in) $(out)'
933  blueprint.add_module(rename_module)
934
935  # Preprocess template file and generates java file
936  preprocess_module = Module('cc_object', bp_module_name + '_preprocess', target.name)
937  # -E: stop after preprocessing.
938  # -P: disable line markers, i.e. '#line 309'
939  preprocess_module.cflags.update(['-E', '-P', '-DANDROID'])
940  preprocess_module.srcs.add(':' + rename_module.name)
941  defines = ['-D' + target.args[i+1] for i, arg in enumerate(target.args) if arg == '--define']
942  preprocess_module.cflags.update(defines)
943  # HACK: Specifying compile_multilib to build cc_object only once.
944  # Without this, soong complain to genrule that depends on cc_object when built for 64bit target.
945  # It seems this is because cc object is a module with per-architecture variants and genrule is a
946  # module with default variant. For 64bit target, cc_object is built multiple times for 32/64bit
947  # modes and genrule doesn't know which one to depend on.
948  preprocess_module.compile_multilib = 'first'
949  blueprint.add_module(preprocess_module)
950
951  # Generates srcjar using soong_zip
952  module = Module('genrule', bp_module_name, target.name)
953  module.srcs.add(':' + preprocess_module.name)
954  module.out.add(stem + '.srcjar')
955  module.cmd = NEWLINE.join([
956    f'cp $(in) $(genDir)/{stem}.java &&',
957    f'$(location soong_zip) -o $(out) -srcjar -C $(genDir) -f $(genDir)/{stem}.java'
958  ])
959  module.tools.add('soong_zip')
960  blueprint.add_module(module)
961  return module
962
963
964class BaseActionSanitizer():
965  def __init__(self, target, arch):
966    # Just to be on the safe side, create a deep-copy.
967    self.target = copy.deepcopy(target)
968    if arch:
969      # Merge arch specific attributes
970      self.target.sources |= arch.sources
971      self.target.inputs |= arch.inputs
972      self.target.outputs |= arch.outputs
973      self.target.script = self.target.script or arch.script
974      self.target.args = self.target.args or arch.args
975      self.target.response_file_contents = \
976        self.target.response_file_contents or arch.response_file_contents
977    self.target.args = self._normalize_args()
978
979  def get_name(self):
980    return label_to_module_name(self.target.name)
981
982  def _normalize_args(self):
983    # Convert ['--param=value'] to ['--param', 'value'] for consistency.
984    # Escape quotations.
985    normalized_args = []
986    for arg in self.target.args:
987      arg = arg.replace('"', r'\"')
988      if arg.startswith('-'):
989        normalized_args.extend(arg.split('='))
990      else:
991        normalized_args.append(arg)
992    return normalized_args
993
994  # There are three types of args:
995  # - flags (--flag)
996  # - value args (--arg value)
997  # - list args (--arg value1 --arg value2)
998  # value args have exactly one arg value pair and list args have one or more arg value pairs.
999  # Note that the set of list args contains the set of value args.
1000  # This is because list and value args are identical when the list args has only one arg value pair
1001  # Some functions provide special implementations for each type, while others
1002  # work on all of them.
1003  def _has_arg(self, arg):
1004    return arg in self.target.args
1005
1006  def _get_arg_indices(self, target_arg):
1007    return [i for i, arg in enumerate(self.target.args) if arg == target_arg]
1008
1009  # Whether an arg value pair appears once or more times
1010  def _is_list_arg(self, arg):
1011    indices = self._get_arg_indices(arg)
1012    return len(indices) > 0 and all([not self.target.args[i + 1].startswith('--') for i in indices])
1013
1014  def _update_list_arg(self, arg, func, throw_if_absent = True):
1015    if self._should_fail_silently(arg, throw_if_absent):
1016      return
1017    assert(self._is_list_arg(arg))
1018    indices = self._get_arg_indices(arg)
1019    for i in indices:
1020      self._set_arg_at(i + 1, func(self.target.args[i + 1]))
1021
1022  # Whether an arg value pair appears exactly once
1023  def _is_value_arg(self, arg):
1024    return operator.countOf(self.target.args, arg) == 1 and self._is_list_arg(arg)
1025
1026  def _get_value_arg(self, arg):
1027    assert(self._is_value_arg(arg))
1028    i = self.target.args.index(arg)
1029    return self.target.args[i + 1]
1030
1031  # used to check whether a function call should cause an error when an arg is
1032  # missing.
1033  def _should_fail_silently(self, arg, throw_if_absent):
1034    return not throw_if_absent and not self._has_arg(arg)
1035
1036  def _set_value_arg(self, arg, value, throw_if_absent = True):
1037    if self._should_fail_silently(arg, throw_if_absent):
1038      return
1039    assert(self._is_value_arg(arg))
1040    i = self.target.args.index(arg)
1041    self.target.args[i + 1] = value
1042
1043  def _update_value_arg(self, arg, func, throw_if_absent = True):
1044    if self._should_fail_silently(arg, throw_if_absent):
1045      return
1046    self._set_value_arg(arg, func(self._get_value_arg(arg)))
1047
1048  def _set_arg_at(self, position, value):
1049    self.target.args[position] = value
1050
1051  def _update_arg_at(self, position, func):
1052    self.target.args[position] = func(self.target.args[position])
1053
1054  def _delete_value_arg(self, arg, throw_if_absent = True):
1055    if self._should_fail_silently(arg, throw_if_absent):
1056      return
1057    assert(self._is_value_arg(arg))
1058    i = self.target.args.index(arg)
1059    self.target.args.pop(i)
1060    self.target.args.pop(i)
1061
1062  def _append_arg(self, arg, value):
1063    self.target.args.append(arg)
1064    self.target.args.append(value)
1065
1066  def _sanitize_filepath_with_location_tag(self, arg):
1067    if arg.startswith('../../'):
1068      arg = self._sanitize_filepath(arg)
1069      arg = self._add_location_tag(arg)
1070    return arg
1071
1072  # wrap filename in location tag.
1073  def _add_location_tag(self, filename):
1074    return '$(location %s)' % filename
1075
1076  # applies common directory transformation that *should* be universally applicable.
1077  # TODO: verify if it actually *is* universally applicable.
1078  def _sanitize_filepath(self, filepath):
1079    # Careful, order matters!
1080    # delete all leading ../
1081    filepath = re.sub('^(\.\./)+', '', filepath)
1082    filepath = re.sub('^gen/jni_headers', '$(genDir)', filepath)
1083    filepath = re.sub('^gen', '$(genDir)', filepath)
1084    return filepath
1085
1086  # Iterate through all the args and apply function
1087  def _update_all_args(self, func):
1088    self.target.args = [func(arg) for arg in self.target.args]
1089
1090  def get_pre_cmd(self):
1091    pre_cmd = []
1092    out_dirs = [out[:out.rfind("/")] for out in self.target.outputs if "/" in out]
1093    # Sort the list to make the output deterministic.
1094    for out_dir in sorted(set(out_dirs)):
1095        pre_cmd.append("mkdir -p $(genDir)/{} && ".format(out_dir))
1096    return NEWLINE.join(pre_cmd)
1097
1098  def get_base_cmd(self):
1099    arg_string = NEWLINE.join(self.target.args)
1100    cmd = '$(location %s) %s' % (
1101    gn_utils.label_to_path(self.target.script), arg_string)
1102
1103    if self.use_response_file:
1104      # Pipe response file contents into script
1105      cmd = 'echo \'%s\' |%s%s' % (self.target.response_file_contents, NEWLINE, cmd)
1106    return cmd
1107
1108  def get_cmd(self):
1109      return self.get_pre_cmd() + self.get_base_cmd()
1110
1111  def get_outputs(self):
1112    return self.target.outputs
1113
1114  def get_srcs(self):
1115    # gn treats inputs and sources for actions equally.
1116    # soong only supports source files inside srcs, non-source files are added as
1117    # tool_files dependency.
1118    files = self.target.sources.union(self.target.inputs)
1119    return {gn_utils.label_to_path(file) for file in files if is_supported_source_file(file)}
1120
1121  def get_tools(self):
1122    return set()
1123
1124  def get_tool_files(self):
1125    # gn treats inputs and sources for actions equally.
1126    # soong only supports source files inside srcs, non-source files are added as
1127    # tool_files dependency.
1128    files = self.target.sources.union(self.target.inputs)
1129    tool_files = {gn_utils.label_to_path(file)
1130                  for file in files if not is_supported_source_file(file)}
1131    tool_files.add(gn_utils.label_to_path(self.target.script))
1132    return tool_files
1133
1134  def _sanitize_args(self):
1135    # Handle passing parameters via response file by piping them into the script
1136    # and reading them from /dev/stdin.
1137
1138    self.use_response_file = gn_utils.RESPONSE_FILE in self.target.args
1139    if self.use_response_file:
1140      # Replace {{response_file_contents}} with /dev/stdin
1141      self.target.args = ['/dev/stdin' if it == gn_utils.RESPONSE_FILE else it
1142                          for it in self.target.args]
1143
1144  def _sanitize_inputs(self):
1145    pass
1146
1147  def get_deps(self):
1148    return self.target.deps
1149
1150  def sanitize(self):
1151    self._sanitize_args()
1152    self._sanitize_inputs()
1153
1154  # Whether this target generates header files
1155  def is_header_generated(self):
1156    return any(os.path.splitext(it)[1] == '.h' for it in self.target.outputs)
1157
1158class WriteBuildDateHeaderSanitizer(BaseActionSanitizer):
1159  def _sanitize_args(self):
1160    self._set_arg_at(0, '$(out)')
1161    super()._sanitize_args()
1162
1163class WriteBuildFlagHeaderSanitizer(BaseActionSanitizer):
1164  def _sanitize_args(self):
1165    self._set_value_arg('--gen-dir', '.')
1166    self._set_value_arg('--output', '$(out)')
1167    super()._sanitize_args()
1168
1169class GnRunBinarySanitizer(BaseActionSanitizer):
1170  def __init__(self, target, arch):
1171    super().__init__(target, arch)
1172    self.binary_to_target = {
1173      "clang_x64/transport_security_state_generator":
1174            "cronet_aml_net_tools_transport_security_state_generator_transport_security_state_generator__testing",
1175    }
1176    self.binary = self.binary_to_target[self.target.args[0]]
1177
1178  def _replace_gen_with_location_tag(self, arg):
1179    if arg.startswith("gen/"):
1180      return "$(location %s)" % arg.replace("gen/", "")
1181    return arg
1182
1183  def _replace_binary(self, arg):
1184    if arg in self.binary_to_target:
1185      return '$(location %s)' % self.binary
1186    return arg
1187
1188  def _remove_python_args(self):
1189    self.target.args = [arg for arg in self.target.args if "python3" not in arg]
1190
1191  def _sanitize_args(self):
1192    self._update_all_args(self._sanitize_filepath_with_location_tag)
1193    self._update_all_args(self._replace_gen_with_location_tag)
1194    self._update_all_args(self._replace_binary)
1195    self._remove_python_args()
1196    super()._sanitize_args()
1197
1198  def get_tools(self):
1199    tools = super().get_tools()
1200    tools.add(self.binary)
1201    return tools
1202
1203  def get_cmd(self):
1204    # Remove the script and use the binary right away
1205    return self.get_pre_cmd() + NEWLINE.join(self.target.args)
1206
1207class JniGeneratorSanitizer(BaseActionSanitizer):
1208  def __init__(self, target, arch, is_test_target):
1209    self.is_test_target = is_test_target
1210    super().__init__(target, arch)
1211
1212  def get_srcs(self):
1213    all_srcs = super().get_srcs()
1214    all_srcs.update({gn_utils.label_to_path(file)
1215                     for file in self.target.transitive_jni_java_sources
1216                     if is_supported_source_file(file)})
1217    return set(src for src in all_srcs if src.endswith(".java"))
1218
1219  def _add_location_tag_to_filepath(self, arg):
1220    if not arg.endswith('.class'):
1221      # --input_file supports both .class specifiers or source files as arguments.
1222      # Only source files need to be wrapped inside a $(location <label>) tag.
1223      arg = self._add_location_tag(arg)
1224    return arg
1225
1226  def _sanitize_args(self):
1227    self._set_value_arg('--jar-file', '$(location :current_android_jar)', False)
1228    if self._has_arg('--jar-file'):
1229      self._set_value_arg('--javap', '$(location :javap)')
1230    self._update_value_arg('--srcjar-path', self._sanitize_filepath, False)
1231    self._update_value_arg('--output-dir', self._sanitize_filepath)
1232    self._update_value_arg('--extra-include', self._sanitize_filepath, False)
1233    self._update_list_arg('--input-file', self._sanitize_filepath)
1234    self._update_list_arg('--input-file', self._add_location_tag_to_filepath)
1235    if not self.is_test_target and not self._has_arg('--jar-file'):
1236      # Don't jarjar classes that already exists within the java SDK. The headers generated
1237      # from those genrule can simply call into the original class as it exists outside
1238      # of cronet's jar.
1239      # Only jarjar platform code
1240      self._append_arg('--package-prefix', 'android.net.connectivity')
1241    super()._sanitize_args()
1242
1243  def get_outputs(self):
1244    # fix target.output directory to match #include statements.
1245    return {re.sub('^jni_headers/', '', out) for out in super().get_outputs()}
1246
1247  def get_tool_files(self):
1248    tool_files = super().get_tool_files()
1249
1250    # Filter android.jar and add :current_android_jar
1251    tool_files = {file if not file.endswith('android.jar') else ':current_android_jar'
1252                  for file in tool_files }
1253    # Filter bin/javap
1254    tool_files = {file for file in tool_files if not file.endswith('bin/javap') }
1255    return tool_files
1256
1257  def get_tools(self):
1258    tools = super().get_tools()
1259    if self._has_arg('--jar-file'):
1260      tools.add(":javap")
1261    return tools
1262
1263
1264class JavaJniGeneratorSanitizer(JniGeneratorSanitizer):
1265  def __init__(self, target, arch, is_test_target):
1266    self.is_test_target = is_test_target
1267    super().__init__(target, arch, is_test_target)
1268
1269  def get_outputs(self):
1270    # fix target.output directory to match #include statements.
1271    outputs = {re.sub('^jni_headers/', '', out) for out in super().get_outputs()}
1272    self.target.outputs = [out for out in outputs if
1273                           out.endswith(".srcjar")]
1274    return outputs
1275
1276  def get_deps(self):
1277    return {}
1278
1279  def get_name(self):
1280    name = super().get_name() + "__java"
1281    return name
1282
1283class JniRegistrationGeneratorSanitizer(BaseActionSanitizer):
1284  def __init__(self, target, arch, is_test_target):
1285    self.is_test_target = is_test_target
1286    super().__init__(target, arch)
1287
1288  def get_srcs(self):
1289    all_srcs = super().get_srcs()
1290    all_srcs.update({gn_utils.label_to_path(file)
1291                     for file in self.target.transitive_jni_java_sources
1292                     if is_supported_source_file(file)})
1293    return set(src for src in all_srcs if src.endswith(".java"))
1294
1295  def _sanitize_inputs(self):
1296    self.target.inputs = [file for file in self.target.inputs if not file.startswith('//out/')]
1297
1298  def get_outputs(self):
1299    return {re.sub('^jni_headers/', '', out) for out in super().get_outputs()}
1300
1301  def _sanitize_args(self):
1302    self._update_value_arg('--depfile', self._sanitize_filepath)
1303    self._update_value_arg('--srcjar-path', self._sanitize_filepath)
1304    self._update_value_arg('--header-path', self._sanitize_filepath)
1305    self._delete_value_arg('--depfile', False)
1306    self._set_value_arg('--java-sources-file', '$(genDir)/java.sources')
1307    if not self.is_test_target:
1308      # Only jarjar platform code
1309      self._append_arg('--package-prefix', 'android.net.connectivity')
1310    super()._sanitize_args()
1311
1312  def get_cmd(self):
1313    # jni_registration_generator.py doesn't work with python2
1314    cmd = "python3 " + super().get_base_cmd()
1315    # Path in the original sources file does not work in genrule.
1316    # So creating sources file in cmd based on the srcs of this target.
1317    # Adding ../$(current_dir)/ to the head because jni_registration_generator.py uses the files
1318    # whose path startswith(..)
1319    commands = ["current_dir=`basename \\\`pwd\\\``;",
1320                "for f in $(in);",
1321                "do",
1322                "echo \\\"../$$current_dir/$$f\\\" >> $(genDir)/java.sources;",
1323                "done;",
1324                cmd]
1325
1326    return self.get_pre_cmd() + NEWLINE.join(commands)
1327
1328class JavaJniRegistrationGeneratorSanitizer(JniRegistrationGeneratorSanitizer):
1329  def get_name(self):
1330    name = super().get_name() + "__java"
1331    return name
1332
1333  def get_outputs(self):
1334    return [out for out in super().get_outputs() if
1335                           out.endswith(".srcjar")]
1336
1337  def get_deps(self):
1338    return {}
1339
1340class VersionSanitizer(BaseActionSanitizer):
1341  def _sanitize_args(self):
1342    self._set_value_arg('-o', '$(out)')
1343    # args for the version.py contain file path without leading --arg key. So apply sanitize
1344    # function for all the args.
1345    self._update_all_args(self._sanitize_filepath_with_location_tag)
1346    self._set_value_arg('-e', "'%s'" % self._get_value_arg('-e'))
1347    super()._sanitize_args()
1348
1349  def get_tool_files(self):
1350    tool_files = super().get_tool_files()
1351    # android_chrome_version.py is not specified in anywhere but version.py imports this file
1352    tool_files.add('build/util/android_chrome_version.py')
1353    return tool_files
1354
1355class JavaCppEnumSanitizer(BaseActionSanitizer):
1356  def _sanitize_args(self):
1357    self._update_all_args(self._sanitize_filepath_with_location_tag)
1358    self._set_value_arg('--srcjar', '$(out)')
1359    super()._sanitize_args()
1360
1361class MakeDafsaSanitizer(BaseActionSanitizer):
1362  def is_header_generated(self):
1363    # This script generates .cc files but they are #included by other sources
1364    # (e.g. registry_controlled_domain.cc)
1365    return True
1366
1367class JavaCppFeatureSanitizer(BaseActionSanitizer):
1368  def _sanitize_args(self):
1369    self._update_all_args(self._sanitize_filepath_with_location_tag)
1370    self._set_value_arg('--srcjar', '$(out)')
1371    super()._sanitize_args()
1372
1373class JavaCppStringSanitizer(BaseActionSanitizer):
1374  def _sanitize_args(self):
1375    self._update_all_args(self._sanitize_filepath_with_location_tag)
1376    self._set_value_arg('--srcjar', '$(out)')
1377    super()._sanitize_args()
1378
1379class WriteNativeLibrariesJavaSanitizer(BaseActionSanitizer):
1380  def _sanitize_args(self):
1381    self._set_value_arg('--output', '$(out)')
1382    super()._sanitize_args()
1383
1384
1385class ProtocJavaSanitizer(BaseActionSanitizer):
1386  def __init__(self, target, arch, gn):
1387    super().__init__(target, arch)
1388    self._protoc = get_protoc_module_name(gn)
1389
1390  def _sanitize_proto_path(self, arg):
1391    arg = self._sanitize_filepath(arg)
1392    return tree_path + '/' + arg
1393
1394  def _sanitize_args(self):
1395    super()._sanitize_args()
1396    self._delete_value_arg('--depfile')
1397    self._set_value_arg('--protoc', '$(location %s)' % self._protoc)
1398    self._update_value_arg('--proto-path', self._sanitize_proto_path)
1399    self._set_value_arg('--srcjar', '$(out)')
1400    self._update_arg_at(-1, self._sanitize_filepath_with_location_tag)
1401
1402  def get_tools(self):
1403    tools = super().get_tools()
1404    tools.add(self._protoc)
1405    return tools
1406
1407
1408def get_action_sanitizer(gn, target, type, arch, is_test_target):
1409  if target.script == "//build/write_buildflag_header.py":
1410    return WriteBuildFlagHeaderSanitizer(target, arch)
1411  elif target.script == "//base/write_build_date_header.py":
1412    return WriteBuildDateHeaderSanitizer(target, arch)
1413  elif target.script == "//build/util/version.py":
1414    return VersionSanitizer(target, arch)
1415  elif target.script == "//build/android/gyp/java_cpp_enum.py":
1416    return JavaCppEnumSanitizer(target, arch)
1417  elif target.script == "//net/tools/dafsa/make_dafsa.py":
1418    return MakeDafsaSanitizer(target, arch)
1419  elif target.script == '//build/android/gyp/java_cpp_features.py':
1420    return JavaCppFeatureSanitizer(target, arch)
1421  elif target.script == '//build/android/gyp/java_cpp_strings.py':
1422    return JavaCppStringSanitizer(target, arch)
1423  elif target.script == '//build/android/gyp/write_native_libraries_java.py':
1424    return WriteNativeLibrariesJavaSanitizer(target, arch)
1425  elif target.script == '//build/gn_run_binary.py':
1426    return GnRunBinarySanitizer(target, arch)
1427  elif target.script == '//build/protoc_java.py':
1428    return ProtocJavaSanitizer(target, arch, gn)
1429  elif target.script == '//third_party/jni_zero/jni_zero.py':
1430    if target.args[0] == 'generate-final':
1431      if type == 'java_genrule':
1432        # Fill up the sources of the target for JniRegistrationGenerator
1433        # actions with all the java sources found under targets of type
1434        # `generate_jni`. Note 1: Only do this for the java part in order to
1435        # generate a complete GEN_JNI. The C++ part MUST only include java
1436        # source files that are listed explicitly in `generate_jni` targets
1437        # in the transitive dependency, this is handled inside the action
1438        # sanitizer itself (See `get_srcs`). Adding java sources that are not
1439        # listed to the C++ version of JniRegistrationGenerator will result
1440        # in undefined symbols as the C++ part generates declarations that
1441        # would have no definitions. Note 2: This is only done for the
1442        # testing targets because their JniRegistration is not complete,
1443        # Chromium generates Jni files for testing targets implicitly (See
1444        # https://source.chromium.org/chromium/chromium/src/+/main:testing
1445        # /test.gni;l=422;bpv=1;bpt=0;drc
1446        # =02820c1b362c3a00f426d7c4eab18703d89cda03) to avoid having to
1447        # replicate the same setup, just fill up the java JniRegistration
1448        # with all  java sources found under `generate_jni` targets and fill
1449        # the C++ version with the exact files.
1450        if is_test_target:
1451          target.sources.update(gn.jni_java_sources)
1452        return JavaJniRegistrationGeneratorSanitizer(target, arch, is_test_target)
1453      else:
1454        return JniRegistrationGeneratorSanitizer(target, arch, is_test_target)
1455    else:
1456      if type == 'cc_genrule':
1457        return JniGeneratorSanitizer(target, arch, is_test_target)
1458      else:
1459        return JavaJniGeneratorSanitizer(target, arch, is_test_target)
1460  else:
1461    raise Exception('Unsupported action %s from %s' % (target.script, target.name))
1462
1463def create_action_foreach_modules(blueprint, gn, target, is_test_target):
1464  """ The following assumes that rebase_path exists in the args.
1465  The args of an action_foreach contains hints about which output files are generated
1466  by which source files.
1467  This is copied directly from the args
1468  "gen/net/base/registry_controlled_domains/{{source_name_part}}-reversed-inc.cc"
1469  So each source file will generate an output whose name is the {source_name-reversed-inc.cc}
1470  """
1471  new_args = []
1472  for i, src in enumerate(sorted(target.sources)):
1473    # don't add script arg for the first source -- create_action_module
1474    # already does this.
1475    if i != 0:
1476      new_args.append('&&')
1477      new_args.append('python3 $(location %s)' %
1478                   gn_utils.label_to_path(target.script))
1479    for arg in target.args:
1480      if '{{source}}' in arg:
1481        new_args.append('$(location %s)' % (gn_utils.label_to_path(src)))
1482      elif '{{source_name_part}}' in arg:
1483        source_name_part = src.split("/")[-1] # Get the file name only
1484        source_name_part = source_name_part.split(".")[0] # Remove the extension (Ex: .cc)
1485        file_name = arg.replace('{{source_name_part}}', source_name_part).split("/")[-1]
1486        # file_name represent the output file name. But we need the whole path
1487        # This can be found from target.outputs.
1488        for out in target.outputs:
1489          if out.endswith(file_name):
1490            new_args.append('$(location %s)' % out)
1491
1492        for file in (target.sources | target.inputs):
1493          if file.endswith(file_name):
1494            new_args.append('$(location %s)' % gn_utils.label_to_path(file))
1495      else:
1496        new_args.append(arg)
1497
1498  target.args = new_args
1499  return create_action_module(blueprint, gn, target, 'cc_genrule', is_test_target)
1500
1501def create_action_module_internal(gn, target, type, is_test_target, blueprint, arch=None):
1502  if target.script == '//build/android/gyp/gcc_preprocess.py':
1503    return create_gcc_preprocess_modules(blueprint, target)
1504  sanitizer = get_action_sanitizer(gn, target, type, arch, is_test_target)
1505  sanitizer.sanitize()
1506
1507  module = Module(type, sanitizer.get_name(), target.name)
1508  module.cmd = sanitizer.get_cmd()
1509  module.out = sanitizer.get_outputs()
1510  if sanitizer.is_header_generated():
1511    module.genrule_headers.add(module.name)
1512  module.srcs = sanitizer.get_srcs()
1513  module.tool_files = sanitizer.get_tool_files()
1514  module.tools = sanitizer.get_tools()
1515  target.deps = sanitizer.get_deps()
1516
1517  return module
1518
1519def get_cmd_condition(arch):
1520  '''
1521  :param arch: archtecture name e.g. android_x86_64, android_arm64
1522  :return: condition that can be used in cc_genrule cmd to switch the behavior based on arch
1523  '''
1524  if arch == "android_x86_64":
1525    return "( $$CC_ARCH == 'x86_64' && $$CC_OS == 'android' )"
1526  elif arch == "android_x86":
1527    return "( $$CC_ARCH == 'x86' && $$CC_OS == 'android' )"
1528  elif arch == "android_arm":
1529    return "( $$CC_ARCH == 'arm' && $$CC_OS == 'android' )"
1530  elif arch == "android_arm64":
1531    return "( $$CC_ARCH == 'arm64' && $$CC_OS == 'android' )"
1532  elif arch == "android_riscv64":
1533    return "( $$CC_ARCH == 'riscv64' && $$CC_OS == 'android' )"
1534  elif arch == "host":
1535    return "$$CC_OS != 'android'"
1536  else:
1537    raise Exception(f'Unknown architecture type {arch}')
1538
1539def merge_cmd(modules, genrule_type):
1540  '''
1541  :param modules: dictionary whose key is arch name and value is module
1542  :param genrule_type: cc_genrule or java_genrule
1543  :return: merged command or common command if all the archs have the same command.
1544  '''
1545  commands = list({module.cmd for module in modules.values()})
1546  if len(commands) == 1:
1547    # If all the archs have the same command, return the command
1548    return commands[0]
1549
1550  if genrule_type != 'cc_genrule':
1551    raise Exception(f'{genrule_type} can not have different cmd between archs')
1552
1553  merged_cmd = []
1554  for arch, module in sorted(modules.items()):
1555    merged_cmd.append(f'if [[ {get_cmd_condition(arch)} ]];')
1556    merged_cmd.append('then')
1557    merged_cmd.append(module.cmd + ';')
1558    merged_cmd.append('fi;')
1559  return NEWLINE.join(merged_cmd)
1560
1561def merge_modules(modules, genrule_type):
1562  '''
1563  :param modules: dictionary whose key is arch name and value is module
1564  :param genrule_type: cc_genrule or java_genrule
1565  :return: merged module of input modules
1566  '''
1567  merged_module = list(modules.values())[0]
1568
1569  # Following attributes must be the same between archs
1570  for key in ('out', 'genrule_headers', 'srcs', 'tool_files'):
1571    if any([getattr(merged_module, key) != getattr(module, key) for module in modules.values()]):
1572      raise Exception(f'{merged_module.name} has different values for {key} between archs')
1573
1574  merged_module.cmd = merge_cmd(modules, genrule_type)
1575  return merged_module
1576
1577def create_action_module(blueprint, gn, target, genrule_type, is_test_target):
1578  '''
1579  Create module for action target and add to the blueprint. If target has arch specific attributes
1580  this function merge them and create a single module.
1581  :param blueprint:
1582  :param target: target which is converted to the module.
1583  :param genrule_type: cc_genrule or java_genrule
1584  :return: created module
1585  '''
1586  # TODO: Handle this target correctly, this target generates java_genrule but this target has
1587  # different value for cpu-family arg between archs
1588  if re.match('//build/android:native_libraries_gen(__testing)?$', target.name):
1589    module = create_action_module_internal(gn, target, genrule_type,
1590                                           is_test_target, blueprint,
1591                                           target.arch['android_arm'])
1592    blueprint.add_module(module)
1593    return module
1594
1595  modules = {arch_name: create_action_module_internal(gn, target, genrule_type,
1596                                                      is_test_target, blueprint, arch)
1597             for arch_name, arch in target.get_archs().items()}
1598  module = merge_modules(modules, genrule_type)
1599  blueprint.add_module(module)
1600  return module
1601
1602
1603def _get_cflags(cflags, defines):
1604  cflags = {flag for flag in cflags if flag in cflag_allowlist}
1605  # Consider proper allowlist or denylist if needed
1606  cflags |= set("-D%s" % define.replace("\"", "\\\"") for define in defines)
1607  return cflags
1608
1609def _set_linker_script(module, libs):
1610  for lib in libs:
1611    if lib.endswith(".lds"):
1612      module.ldflags.add(get_linker_script_ldflag(gn_utils.label_to_path(lib)))
1613
1614def set_module_flags(module, module_type, cflags, defines, ldflags, libs):
1615  module.cflags.update(_get_cflags(cflags, defines))
1616  module.ldflags.update({flag for flag in ldflags
1617          if flag in ldflag_allowlist or flag.startswith("-Wl,-wrap,")})
1618  _set_linker_script(module, libs)
1619  # TODO: implement proper cflag parsing.
1620  for flag in cflags:
1621    if '-std=' in flag:
1622      module.cpp_std = flag[len('-std='):]
1623    if '-fexceptions' in flag:
1624      module.cppflags.add('-fexceptions')
1625
1626def set_module_include_dirs(module, cflags, include_dirs):
1627  for flag in cflags:
1628    if '-isystem' in flag:
1629      module.local_include_dirs.add(flag[len('-isystem../../'):])
1630
1631  # Adding local_include_dirs is necessary due to source_sets / filegroups
1632  # which do not properly propagate include directories.
1633  # Filter any directory inside //out as a) this directory does not exist for
1634  # aosp / soong builds and b) the include directory should already be
1635  # configured via library dependency.
1636  module.local_include_dirs.update([gn_utils.label_to_path(d)
1637                                 for d in include_dirs if not d.startswith('//out')])
1638  # Remove prohibited include directories
1639  module.local_include_dirs = [d for d in module.local_include_dirs
1640                               if d not in local_include_dirs_denylist]
1641
1642
1643def create_modules_from_target(blueprint, gn, gn_target_name, is_descendant_of_java, is_test_target):
1644  """Generate module(s) for a given GN target.
1645
1646    Given a GN target name, generate one or more corresponding modules into a
1647    blueprint. The only case when this generates >1 module is proto libraries.
1648
1649    Args:
1650        blueprint: Blueprint instance which is being generated.
1651        gn: gn_utils.GnParser object.
1652        gn_target_name: GN target for module generation.
1653    """
1654  bp_module_name = label_to_module_name(gn_target_name)
1655  target = gn.get_target(gn_target_name)
1656  is_descendant_of_java = is_descendant_of_java or target.type == "java_library"
1657
1658  # Append __java suffix to actions reachable from java_library. This is necessary
1659  # to differentiate them from cc actions.
1660  # This means that a GN action of name X will be translated to two different modules of names
1661  # X and X__java(only if X is reachable from a java target).
1662  if target.type == "action" and is_descendant_of_java:
1663    bp_module_name += "__java"
1664
1665  if bp_module_name in blueprint.modules:
1666    return blueprint.modules[bp_module_name]
1667
1668  log.info('create modules for %s (%s)', target.name, target.type)
1669
1670  if target.type == 'executable':
1671    if target.testonly:
1672      module_type = 'cc_test'
1673    else:
1674      # Can be used for both host and device targets.
1675      module_type = 'cc_binary'
1676    module = Module(module_type, bp_module_name, gn_target_name)
1677  elif target.type in ['static_library', 'source_set']:
1678    module = Module('cc_library_static', bp_module_name, gn_target_name)
1679  elif target.type == 'shared_library':
1680    module = Module('cc_library_shared', bp_module_name, gn_target_name)
1681  elif target.type == 'group':
1682    # "group" targets are resolved recursively by gn_utils.get_target().
1683    # There's nothing we need to do at this level for them.
1684    return None
1685  elif target.type == 'proto_library':
1686    module = create_proto_modules(blueprint, gn, target)
1687    if module is None:
1688      return None
1689  elif target.type == 'action':
1690    module = create_action_module(blueprint, gn, target, 'java_genrule' if is_descendant_of_java else 'cc_genrule', is_test_target)
1691  elif target.type == 'action_foreach':
1692    module = create_action_foreach_modules(blueprint, gn, target, is_test_target)
1693  elif target.type == 'copy':
1694    # TODO: careful now! copy targets are not supported yet, but this will stop
1695    # traversing the dependency tree. For //base:base, this is not a big
1696    # problem as libicu contains the only copy target which happens to be a
1697    # leaf node.
1698    return None
1699  elif target.type == 'java_library':
1700    if target.jar_path:
1701      module = Module('java_import', bp_module_name, gn_target_name)
1702      module.jars.add(target.jar_path)
1703    else:
1704      module = Module('java_library', bp_module_name, gn_target_name)
1705      # Don't remove GEN_JNI from those modules as they have the real GEN_JNI that we want to include
1706      if gn_target_name not in ['//components/cronet/android:cronet_jni_registration_java',
1707                                '//components/cronet/android:cronet_jni_registration_java__testing',
1708                                '//components/cronet/android:cronet_tests_jni_registration_java__testing']:
1709          module.jarjar_rules = REMOVE_GEN_JNI_JARJAR_RULES_FILE
1710    module.min_sdk_version = 30
1711    module.apex_available = [tethering_apex]
1712    module.sdk_version = target.sdk_version
1713  else:
1714    raise Exception('Unknown target %s (%s)' % (target.name, target.type))
1715
1716  blueprint.add_module(module)
1717  if target.type not in ['action', 'action_foreach']:
1718      # Actions should get their srcs from their corresponding ActionSanitizer as actionSanitizer
1719      # filters srcs differently according to the type of the action.
1720      module.srcs.update(gn_utils.label_to_path(src)
1721                         for src in target.sources if is_supported_source_file(src))
1722
1723  # Add arch-specific properties
1724  for arch_name, arch in target.get_archs().items():
1725    module.target[arch_name].srcs.update(gn_utils.label_to_path(src)
1726                                         for src in arch.sources if is_supported_source_file(src))
1727
1728  module.rtti = target.rtti
1729
1730  if target.type in gn_utils.LINKER_UNIT_TYPES:
1731    set_module_flags(module, module.type, target.cflags, target.defines, target.ldflags, target.libs)
1732    set_module_include_dirs(module, target.cflags, target.include_dirs)
1733    # TODO: set_module_xxx is confusing, apply similar function to module and target in better way.
1734    for arch_name, arch in target.get_archs().items():
1735      # TODO(aymanm): Make libs arch-specific.
1736      set_module_flags(module.target[arch_name], module.type,
1737                       arch.cflags, arch.defines, arch.ldflags, [])
1738      # -Xclang -target-feature -Xclang +mte are used to enable MTE (Memory Tagging Extensions).
1739      # Flags which does not start with '-' could not be in the cflags so enabling MTE by
1740      # -march and -mcpu Feature Modifiers. MTE is only available on arm64. This is needed for
1741      # building //base/allocator/partition_allocator:partition_alloc for arm64.
1742      if '+mte' in arch.cflags and arch_name == 'android_arm64':
1743        module.target[arch_name].cflags.add('-march=armv8-a+memtag')
1744      set_module_include_dirs(module.target[arch_name], arch.cflags, arch.include_dirs)
1745
1746  module.host_supported = target.host_supported()
1747  module.device_supported = target.device_supported()
1748  module.gn_type = target.type
1749
1750  if module.is_genrule():
1751    module.apex_available.add(tethering_apex)
1752
1753  if module.type == "java_library":
1754    if gn_utils.contains_aidl(target.sources):
1755      # frameworks/base/core/java includes the source files that are used to compile framework.aidl.
1756      # framework.aidl is added implicitly as a dependency to every AIDL GN action, this can be
1757      # identified by third_party/android_sdk/public/platforms/android-34/framework.aidl.
1758      module.aidl["include_dirs"] = {"frameworks/base/core/java/"}
1759      module.aidl["local_include_dirs"] = target.local_aidl_includes
1760    module.sdk_version = target.sdk_version
1761
1762  if module.is_compiled() and not module.type.startswith("java"):
1763    # Don't try to inject library/source dependencies into genrules or
1764    # filegroups because they are not compiled in the traditional sense.
1765    module.defaults = [defaults_module]
1766    for lib in target.libs:
1767      # Generally library names should be mangled as 'libXXX', unless they
1768      # are HAL libraries (e.g., android.hardware.health@2.0) or AIDL c++ / NDK
1769      # libraries (e.g. "android.hardware.power.stats-V1-cpp")
1770      android_lib = lib if '@' in lib or "-cpp" in lib or "-ndk" in lib \
1771        else 'lib' + lib
1772      if lib in shared_library_allowlist:
1773        module.add_android_shared_lib(android_lib)
1774
1775  # If the module is a static library, export all the generated headers.
1776  if module.type == 'cc_library_static':
1777    module.export_generated_headers = module.generated_headers
1778
1779  if module.name in ['cronet_aml_components_cronet_android_cronet',
1780                     'cronet_aml_components_cronet_android_cronet' + gn_utils.TESTING_SUFFIX]:
1781    if target.output_name is None:
1782      raise Exception('Failed to get output_name for libcronet name')
1783    # .so file name needs to match with CronetLibraryLoader.java (e.g. libcronet.109.0.5386.0.so)
1784    # So setting the output name based on the output_name from the desc.json
1785    module.stem = 'libmainline' + target.output_name
1786  elif module.is_test() and module.type == 'cc_library_shared':
1787    if target.output_name:
1788      # If we have an output name already declared, use it.
1789      module.stem = 'lib' + target.output_name
1790    else:
1791      # Tests output should be a shared library in the format of 'lib[module_name]'
1792      module.stem = 'lib' + target.get_target_name()[
1793                            :target.get_target_name().find(gn_utils.TESTING_SUFFIX)]
1794
1795  # dep_name is an unmangled GN target name (e.g. //foo:bar(toolchain)).
1796  all_deps = [(dep_name, 'common') for dep_name in target.proto_deps]
1797  for arch_name, arch in target.arch.items():
1798    all_deps += [(dep_name, arch_name) for dep_name in arch.deps]
1799
1800  # Sort deps before iteration to make result deterministic.
1801  for (dep_name, arch_name) in sorted(all_deps):
1802    module_target = module.target[arch_name] if arch_name != 'common' else module
1803    # |builtin_deps| override GN deps with Android-specific ones. See the
1804    # config in the top of this file.
1805    if dep_name in builtin_deps:
1806      builtin_deps[dep_name](module, arch_name)
1807      continue
1808
1809    dep_module = create_modules_from_target(blueprint, gn, dep_name, is_descendant_of_java, is_test_target)
1810
1811    if dep_module is None:
1812      continue
1813
1814    # TODO: Proper dependency check for genrule.
1815    # Currently, only propagating genrule dependencies.
1816    # Also, currently, all the dependencies are propagated upwards.
1817    # in gn, public_deps should be propagated but deps should not.
1818    # Not sure this information is available in the desc.json.
1819    # Following rule works for adding android_runtime_jni_headers to base:base.
1820    # If this doesn't work for other target, hardcoding for specific target
1821    # might be better.
1822    if module.is_genrule() and dep_module.is_genrule():
1823      if module_target.gn_type != "proto_library":
1824        # proto_library are treated differently because each proto action
1825        # is split into two different targets, a cpp target and a header target.
1826        # the cpp target is used as the entry point to the proto action, hence
1827        # it should not be propagated as a genrule header because it generates
1828        # cpp files only.
1829        module_target.genrule_headers.add(dep_module.name)
1830      module_target.genrule_headers.update(dep_module.genrule_headers)
1831
1832    # For filegroups, and genrule, recurse but don't apply the
1833    # deps.
1834    if not module.is_compiled() or module.is_genrule():
1835      continue
1836
1837    # Drop compiled modules that doesn't provide any benefit. This is mostly
1838    # applicable to source_sets when converted to cc_static_library, sometimes
1839    # the source set only has header files which are dropped so the module becomes empty.
1840    # is_compiled is there to prevent dropping of genrules.
1841    if dep_module.is_compiled() and not dep_module.has_input_files():
1842      continue
1843
1844    if dep_module.type == 'cc_library_shared':
1845      module_target.shared_libs.add(dep_module.name)
1846    elif dep_module.type == 'cc_library_static':
1847      if module.type in ['cc_library_shared', 'cc_binary']:
1848        module_target.whole_static_libs.add(dep_module.name)
1849      elif module.type == 'cc_library_static':
1850        module_target.generated_headers.update(dep_module.generated_headers)
1851        module_target.shared_libs.update(dep_module.shared_libs)
1852        module_target.header_libs.update(dep_module.header_libs)
1853    elif dep_module.type == 'cc_genrule':
1854      module_target.generated_headers.update(dep_module.genrule_headers)
1855      module_target.srcs.update(dep_module.genrule_srcs)
1856      module_target.shared_libs.update(dep_module.genrule_shared_libs)
1857      module_target.header_libs.update(dep_module.genrule_header_libs)
1858    elif dep_module.type in ['java_library', 'java_import']:
1859      # A module depending on a module with system_current sdk version should also compile against
1860      # the system sdk. This is because a module's SDK API surface should be >= its deps SDK API surface.
1861      # And system_current has a larger API surface than current or module_current.
1862      # We currently only have system_current, current or no sdk_versions in the generated bp file.
1863      assert(dep_module.sdk_version in ['system_current', 'current'])
1864      if dep_module.sdk_version == 'system_current':
1865        module.sdk_version = 'system_current'
1866      if module.type not in ["cc_library_static"]:
1867        # This is needed to go around the case where `url` component depends
1868        # on `url_java`.
1869        # TODO(aymanm): Remove the if condition once crrev/4902547 has been imported downstream
1870        module_target.static_libs.add(dep_module.name)
1871    elif dep_module.type in ['genrule', 'java_genrule']:
1872      module_target.srcs.add(":" + dep_module.name)
1873    else:
1874      raise Exception('Unsupported arch-specific dependency %s of target %s with type %s' %
1875                      (dep_module.name, target.name, dep_module.type))
1876  return module
1877
1878def turn_off_allocator_shim_for_musl(module):
1879  allocation_shim = "base/allocator/partition_allocator/shim/allocator_shim.cc"
1880  allocator_shim_files = {
1881    allocation_shim,
1882    "base/allocator/partition_allocator/shim/allocator_shim_default_dispatch_to_glibc.cc",
1883  }
1884  module.srcs -= allocator_shim_files
1885  for arch in module.target.values():
1886    arch.srcs -= allocator_shim_files
1887  module.target['android'].srcs.add(allocation_shim)
1888  if gn_utils.TESTING_SUFFIX in module.name:
1889    # allocator_shim_default_dispatch_to_glibc is only added to the __testing version of base
1890    # since base_base__testing is compiled for host. When compiling for host. Soong compiles
1891    # using glibc or musl(experimental). We currently only support compiling for glibc.
1892    module.target['glibc'].srcs.update(allocator_shim_files)
1893  else:
1894    # allocator_shim_default_dispatch_to_glibc does not exist in the prod version of base
1895    # `base_base` since this only compiles for android and bionic is used. Bionic is the equivalent
1896    # of glibc but for android.
1897    module.target['glibc'].srcs.add(allocation_shim)
1898
1899def create_blueprint_for_targets(gn, targets, test_targets):
1900  """Generate a blueprint for a list of GN targets."""
1901  blueprint = Blueprint()
1902
1903  # Default settings used by all modules.
1904  defaults = Module('cc_defaults', defaults_module, '//gn:default_deps')
1905  defaults.cflags = [
1906      '-DGOOGLE_PROTOBUF_NO_RTTI',
1907      '-DBORINGSSL_SHARED_LIBRARY',
1908      '-Wno-error=return-type',
1909      '-Wno-non-virtual-dtor',
1910      '-Wno-macro-redefined',
1911      '-Wno-missing-field-initializers',
1912      '-Wno-sign-compare',
1913      '-Wno-sign-promo',
1914      '-Wno-unused-parameter',
1915      '-Wno-null-pointer-subtraction', # Needed to libevent
1916      '-Wno-ambiguous-reversed-operator', # needed for icui18n
1917      '-Wno-unreachable-code-loop-increment', # needed for icui18n
1918      '-fPIC',
1919      '-Wno-c++11-narrowing',
1920      # b/330508686 disable coverage profiling for files or function in this list.
1921      '-fprofile-list=external/cronet/exclude_coverage.list',
1922  ]
1923  defaults.c_std = 'gnu11'
1924  # Chromium builds do not add a dependency for headers found inside the
1925  # sysroot, so they are added globally via defaults.
1926  defaults.target['android'].header_libs = [
1927      'jni_headers',
1928  ]
1929  defaults.target['android'].shared_libs = [
1930      'libmediandk'
1931  ]
1932  defaults.target['host'].cflags = [
1933      # -DANDROID is added by default but target.defines contain -DANDROID if
1934      # it's required.  So adding -UANDROID to cancel default -DANDROID if it's
1935      # not specified.
1936      # Note: -DANDROID is not consistently applied across the chromium code
1937      # base, so it is removed unconditionally for host targets.
1938      '-UANDROID',
1939  ]
1940  defaults.stl = 'none'
1941  defaults.cpp_std = 'c++17'
1942  defaults.min_sdk_version = 29
1943  defaults.apex_available.add(tethering_apex)
1944  blueprint.add_module(defaults)
1945
1946  for target in targets:
1947    module = create_modules_from_target(blueprint, gn, target, is_descendant_of_java=False, is_test_target=False)
1948    if module:
1949      module.visibility.update(root_modules_visibility)
1950
1951  for test_target in test_targets:
1952    module = create_modules_from_target(blueprint, gn, test_target + gn_utils.TESTING_SUFFIX, is_descendant_of_java=False, is_test_target=True)
1953    if module:
1954      module.visibility.update(root_modules_visibility)
1955
1956
1957
1958  # Merge in additional hardcoded arguments.
1959  for module in blueprint.modules.values():
1960    for key, add_val in additional_args.get(module.name, []):
1961      curr = getattr(module, key)
1962      if add_val and isinstance(add_val, set) and isinstance(curr, set):
1963        curr.update(add_val)
1964      elif isinstance(add_val, str) and (not curr or isinstance(curr, str)):
1965        setattr(module, key, add_val)
1966      elif isinstance(add_val, bool) and (not curr or isinstance(curr, bool)):
1967        setattr(module, key, add_val)
1968      elif isinstance(add_val, dict) and isinstance(curr, dict):
1969        curr.update(add_val)
1970      elif isinstance(add_val[1], dict) and isinstance(curr[add_val[0]], Module.Target):
1971        curr[add_val[0]].__dict__.update(add_val[1])
1972      else:
1973        raise Exception('Unimplemented type %r of additional_args: %r' % (type(add_val), key))
1974
1975  return blueprint
1976
1977def create_package_module(blueprint):
1978  package = Module("package", "", "PACKAGE")
1979  package.comment = "The actual license can be found in Android.extras.bp"
1980  package.default_applicable_licenses.add(CRONET_LICENSE_NAME)
1981  package.default_visibility.append(package_default_visibility)
1982  blueprint.add_module(package)
1983
1984def main():
1985  parser = argparse.ArgumentParser(
1986      description='Generate Android.bp from a GN description.')
1987  parser.add_argument(
1988      '--desc',
1989      help='GN description (e.g., gn desc out --format=json --all-toolchains "//*".' +
1990           'You can specify multiple --desc options for different target_cpu',
1991      required=True,
1992      action='append'
1993  )
1994  parser.add_argument(
1995      '--extras',
1996      help='Extra targets to include at the end of the Blueprint file',
1997      default=os.path.join(gn_utils.repo_root(), 'Android.bp.extras'),
1998  )
1999  parser.add_argument(
2000      '--output',
2001      help='Blueprint file to create',
2002      default=os.path.join(gn_utils.repo_root(), 'Android.bp'),
2003  )
2004  parser.add_argument(
2005      '-v',
2006      '--verbose',
2007      help='Print debug logs.',
2008      action='store_true',
2009  )
2010  parser.add_argument(
2011      'targets',
2012      nargs=argparse.REMAINDER,
2013      help='Targets to include in the blueprint (e.g., "//:perfetto_tests")'
2014  )
2015  args = parser.parse_args()
2016
2017  if args.verbose:
2018    log.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level=log.DEBUG)
2019
2020  targets = args.targets or DEFAULT_TARGETS
2021  gn = gn_utils.GnParser(builtin_deps)
2022  for desc_file in args.desc:
2023    with open(desc_file) as f:
2024      desc = json.load(f)
2025    for target in targets:
2026      gn.parse_gn_desc(desc, target)
2027    for test_target in DEFAULT_TESTS:
2028      gn.parse_gn_desc(desc, test_target, is_test_target=True)
2029  blueprint = create_blueprint_for_targets(gn, targets, DEFAULT_TESTS)
2030  project_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
2031  tool_name = os.path.relpath(os.path.abspath(__file__), project_root)
2032
2033  create_package_module(blueprint)
2034  output = [
2035      """// Copyright (C) 2022 The Android Open Source Project
2036//
2037// Licensed under the Apache License, Version 2.0 (the "License");
2038// you may not use this file except in compliance with the License.
2039// You may obtain a copy of the License at
2040//
2041//      http://www.apache.org/licenses/LICENSE-2.0
2042//
2043// Unless required by applicable law or agreed to in writing, software
2044// distributed under the License is distributed on an "AS IS" BASIS,
2045// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2046// See the License for the specific language governing permissions and
2047// limitations under the License.
2048//
2049// This file is automatically generated by %s. Do not edit.
2050
2051build = ["Android.extras.bp"]
2052""" % (tool_name)
2053  ]
2054  blueprint.to_string(output)
2055  if os.path.exists(args.extras):
2056    with open(args.extras, 'r') as r:
2057      for line in r:
2058        output.append(line.rstrip("\n\r"))
2059
2060  out_files = []
2061
2062  # Generate the Android.bp file.
2063  out_files.append(args.output + '.swp')
2064  with open(out_files[-1], 'w') as f:
2065    f.write('\n'.join(output))
2066    # Text files should have a trailing EOL.
2067    f.write('\n')
2068
2069  return 0
2070
2071
2072if __name__ == '__main__':
2073  sys.exit(main())
2074