xref: /aosp_15_r20/external/cronet/build/config/android/internal_rules.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2014 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Do not add any imports to non-//build directories here.
6# Some projects (e.g. V8) do not have non-build directories DEPS'ed in.
7import("//build/config/android/channel.gni")
8import("//build/config/android/config.gni")
9import("//build/config/compiler/compiler.gni")
10import("//build/config/compute_inputs_for_analyze.gni")
11import("//build/config/coverage/coverage.gni")
12import("//build/config/python.gni")
13import("//build/config/sanitizers/sanitizers.gni")
14import("//build/toolchain/goma.gni")
15import("//build/toolchain/kythe.gni")
16import("//build/util/generate_wrapper.gni")
17import("//build_overrides/build.gni")
18if (current_toolchain == default_toolchain) {
19  import("//build/toolchain/concurrent_links.gni")
20}
21assert(is_android)
22
23default_android_sdk_dep = "//third_party/android_sdk:android_sdk_java"
24_kotlin_stdlib_dep = "//third_party/kotlin_stdlib:kotlin_stdlib_java"
25_jacoco_dep = "//third_party/jacoco:jacocoagent_java"
26_jacoco_host_jar =
27    "$root_build_dir/lib.java/third_party/jacoco/jacocoagent_java.jar"
28_robolectric_libs_dir =
29    rebase_path(
30        get_label_info("//:foo($robolectric_toolchain)", "root_out_dir"),
31        root_build_dir)
32
33# The following _java_*_types variables capture all the existing target types.
34# If a new type is introduced, please add it to one of these categories,
35# preferring the more specific resource/library types.
36_java_resource_types = [
37  "android_assets",
38  "android_resources",
39]
40
41_java_library_types = [
42  "java_library",
43  "system_java_library",
44  "android_app_bundle_module",
45]
46
47# These are leaf java target types. They cannot be passed as deps to other
48# targets. Thus their naming schemes are not enforced.
49_java_leaf_types = [
50  "android_apk",
51  "android_app_bundle",
52  "dist_aar",
53  "dist_jar",
54  "java_annotation_processor",
55  "java_binary",
56  "robolectric_binary",
57]
58
59# All _java_resource_types targets must conform to these patterns.
60java_resource_patterns = [
61  "*_assets",
62  "*_grd",
63  "*_java_strings",
64  "*locale_paks",
65  "*_resources",
66  "*strings_java",
67  "*android*:assets",
68  "*:*_apk_*resources",
69  "*android*:resources",
70]
71
72# All _java_library_types targets must conform to these patterns. This includes
73# all non-leaf targets that use java_library_impl.
74java_library_patterns = [
75  "*_java",
76  "*_javalib",
77  "*javatests",
78  "*_bundle_module",
79  "*:*_java_*",  # E.g. chrome_java_test_support
80  "*:java",
81  "*/java",  # to allow filtering without expanding labels //a/java ->
82             # //a/java:java
83  "*:junit",
84  "*/junit",
85  "*:junit_*",
86  "*:*_junit_*",
87
88  # TODO(agrieve): Rename to glue_java
89  "//android_webview/glue",
90  "//android_webview/glue:glue",
91]
92
93# These identify all non-leaf targets that have .build_config.json files. This is the
94# set of patterns that other targets can use to filter out java targets.
95java_target_patterns = java_library_patterns + java_resource_patterns
96
97_r8_path = "//third_party/r8/lib/r8.jar"
98_custom_r8_path = "//third_party/r8/custom_r8.jar"
99
100# This duplication is intentional, so we avoid updating the r8.jar used by
101# dexing unless necessary, since each update invalidates all incremental dexing
102# and unnecessarily slows down all bots.
103_d8_path = "//third_party/r8/d8/lib/r8.jar"
104_custom_d8_path = "//third_party/r8/custom_d8.jar"
105_default_lint_jar_path = "//third_party/android_build_tools/lint/lint.jar"
106_custom_lint_jar_path = "//third_party/android_build_tools/lint/custom_lint.jar"
107_manifest_merger_jar_path =
108    "//third_party/android_build_tools/manifest_merger/manifest-merger.jar"
109
110# Put the bug number in the target name so that false-positives have a hint in
111# the error message about why non-existent dependencies are there.
112build_config_target_suffix = "__build_config_crbug_908819"
113
114# Write the target's .build_config.json file. This is a json file that contains a
115# dictionary of information about how to build this target (things that
116# require knowledge about this target's dependencies and cannot be calculated
117# at gn-time). There is a special syntax to add a value in that dictionary to
118# an action/action_foreachs args:
119#   --python-arg=@FileArg($rebased_build_config_path:key0:key1)
120# At runtime, such an arg will be replaced by the value in the build_config.
121# See build/android/gyp/write_build_config.py and
122# build/android/gyp/util/build_utils.py:ExpandFileArgs
123template("write_build_config") {
124  action_with_pydeps(target_name) {
125    forward_variables_from(invoker, [ "testonly" ])
126    _type = invoker.type
127    _parent_invoker = invoker.invoker
128    _target_label =
129        get_label_info(":${_parent_invoker.target_name}", "label_no_toolchain")
130
131    # Ensure targets match naming patterns so that __assetres, __header, __host,
132    # and __validate targets work properly.
133    if (filter_exclude([ _type ], _java_resource_types) == []) {
134      if (filter_exclude([ _target_label ], java_resource_patterns) != []) {
135        assert(false, "Invalid java resource target name: $_target_label")
136      }
137    } else if (filter_exclude([ _type ], _java_library_types) == []) {
138      if (filter_exclude([ _target_label ], java_library_patterns) != [] ||
139          filter_exclude([ _target_label ], java_resource_patterns) == []) {
140        assert(false, "Invalid java library target name: $_target_label")
141      }
142    } else if (_type == "group") {
143      if (filter_exclude([ _target_label ], java_target_patterns) != []) {
144        assert(false, "Invalid java target name: $_target_label")
145      }
146    } else if (filter_exclude([ _type ], _java_leaf_types) != []) {
147      assert(false, "This java type needs a category: $_type")
148    }
149
150    if (defined(invoker.public_target_label)) {
151      _target_label = invoker.public_target_label
152    }
153
154    deps = []
155    if (defined(invoker.deps)) {
156      deps = invoker.deps
157    }
158    if (defined(invoker.android_manifest_dep)) {
159      deps += [ invoker.android_manifest_dep ]
160    }
161
162    script = "//build/android/gyp/write_build_config.py"
163    depfile = "$target_gen_dir/$target_name.d"
164    inputs = []
165    outputs = [ invoker.build_config ]
166
167    _deps_configs = []
168    if (defined(invoker.possible_config_deps)) {
169      foreach(_possible_dep, invoker.possible_config_deps) {
170        _dep_label = get_label_info(_possible_dep, "label_no_toolchain")
171        if (filter_exclude([ _dep_label ], java_target_patterns) == []) {
172          deps += [ "$_dep_label$build_config_target_suffix" ]
173          _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir")
174          _dep_name = get_label_info(_possible_dep, "name")
175          _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
176
177          _deps_configs += [ _dep_config ]
178        }
179      }
180    }
181    _public_deps_configs = []
182    if (defined(invoker.possible_config_public_deps)) {
183      foreach(_possible_dep, invoker.possible_config_public_deps) {
184        _dep_label = get_label_info(_possible_dep, "label_no_toolchain")
185
186        # E.g. Adding an action that generates a .java file that is then
187        # consumed by a subsequent java_library() target would not work
188        # because the libraries depend only on the nested targets of one
189        # another. It is simplest to just ban non-java public_deps.
190        assert(filter_exclude([ _dep_label ], java_target_patterns) == [],
191               "Only java_library targets can be used as public_deps. " +
192                   "Found:\n${_dep_label}\non Target:\n" +
193                   get_label_info(":$target_name", "label_no_toolchain"))
194
195        # Put the bug number in the target name so that false-positives
196        # have a hint in the error message about non-existent dependencies.
197        deps += [ "$_dep_label$build_config_target_suffix" ]
198        _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir")
199        _dep_name = get_label_info(_possible_dep, "name")
200        _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
201
202        _public_deps_configs += [ _dep_config ]
203      }
204    }
205    inputs += _deps_configs
206    inputs += _public_deps_configs
207    _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)
208    _rebased_public_deps_configs =
209        rebase_path(_public_deps_configs, root_build_dir)
210
211    args = [
212      "--type=$_type",
213      "--depfile",
214      rebase_path(depfile, root_build_dir),
215      "--deps-configs=$_rebased_deps_configs",
216      "--public-deps-configs=$_rebased_public_deps_configs",
217      "--build-config",
218      rebase_path(invoker.build_config, root_build_dir),
219      "--gn-target",
220      _target_label,
221    ]
222
223    if (defined(invoker.preferred_dep) && invoker.preferred_dep) {
224      args += [ "--preferred-dep" ]
225    }
226
227    if (defined(invoker.aar_path)) {
228      args += [
229        "--aar-path",
230        rebase_path(invoker.aar_path, root_build_dir),
231      ]
232    }
233
234    if (defined(invoker.chromium_code) && !invoker.chromium_code) {
235      # Default to chromium code if invoker did not pass anything.
236      args += [ "--non-chromium-code" ]
237    }
238
239    if (defined(invoker.device_jar_path)) {
240      args += [
241        "--device-jar-path",
242        rebase_path(invoker.device_jar_path, root_build_dir),
243      ]
244    }
245    if (defined(invoker.host_jar_path)) {
246      args += [
247        "--host-jar-path",
248        rebase_path(invoker.host_jar_path, root_build_dir),
249      ]
250    }
251    if (defined(invoker.unprocessed_jar_path)) {
252      args += [
253        "--unprocessed-jar-path",
254        rebase_path(invoker.unprocessed_jar_path, root_build_dir),
255      ]
256    }
257    if (defined(invoker.ijar_path)) {
258      args += [
259        "--interface-jar-path",
260        rebase_path(invoker.ijar_path, root_build_dir),
261      ]
262    }
263    if (defined(invoker.kotlinc_jar_path)) {
264      args += [
265        "--kotlinc-jar-path",
266        rebase_path(invoker.kotlinc_jar_path, root_build_dir),
267      ]
268    }
269    if (defined(invoker.java_resources_jar)) {
270      args += [
271        "--java-resources-jar-path",
272        rebase_path(invoker.java_resources_jar, root_build_dir),
273      ]
274    }
275    if (defined(invoker.annotation_processor_deps) &&
276        invoker.annotation_processor_deps != []) {
277      _processor_configs = []
278      foreach(_dep_label, invoker.annotation_processor_deps) {
279        deps += [ "$_dep_label$build_config_target_suffix" ]
280        _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
281        _dep_name = get_label_info(_dep_label, "name")
282        _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
283        _processor_configs += [ _dep_config ]
284      }
285      _rebased_processor_configs =
286          rebase_path(_processor_configs, root_build_dir)
287      inputs += _processor_configs
288      args += [ "--annotation-processor-configs=$_rebased_processor_configs" ]
289    }
290
291    # Dex path for library targets, or the the intermediate library for apks.
292    if (defined(invoker.dex_path)) {
293      args += [
294        "--dex-path",
295        rebase_path(invoker.dex_path, root_build_dir),
296      ]
297    }
298
299    # Dex path for the final apk.
300    if (defined(invoker.final_dex_path)) {
301      args += [
302        "--final-dex-path",
303        rebase_path(invoker.final_dex_path, root_build_dir),
304      ]
305    }
306    if (defined(invoker.supports_android) && invoker.supports_android) {
307      args += [ "--supports-android" ]
308    }
309    if (defined(invoker.requires_android) && invoker.requires_android) {
310      args += [ "--requires-android" ]
311    }
312    if (defined(invoker.is_prebuilt) && invoker.is_prebuilt) {
313      args += [ "--is-prebuilt" ]
314    }
315    if (defined(invoker.bypass_platform_checks) &&
316        invoker.bypass_platform_checks) {
317      args += [ "--bypass-platform-checks" ]
318    }
319    if (defined(invoker.is_robolectric) && invoker.is_robolectric) {
320      args += [ "--is-robolectric" ]
321    }
322
323    if (defined(invoker.apk_under_test)) {
324      _dep_label = invoker.apk_under_test
325      _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
326      _dep_name = get_label_info(_dep_label, "name")
327      _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
328      inputs += [ _dep_config ]
329      deps += [ "$_dep_label$build_config_target_suffix" ]
330      args += [
331        "--tested-apk-config",
332        rebase_path(_dep_config, root_build_dir),
333      ]
334    }
335
336    if (defined(invoker.asset_sources)) {
337      _rebased_asset_sources =
338          rebase_path(invoker.asset_sources, root_build_dir)
339      args += [ "--asset-sources=$_rebased_asset_sources" ]
340    }
341    if (defined(invoker.asset_renaming_sources)) {
342      _rebased_asset_renaming_sources =
343          rebase_path(invoker.asset_renaming_sources, root_build_dir)
344      args += [ "--asset-renaming-sources=$_rebased_asset_renaming_sources" ]
345
346      # These are zip paths, so no need to rebase.
347      args += [
348        "--asset-renaming-destinations=${invoker.asset_renaming_destinations}",
349      ]
350    }
351    if (defined(invoker.disable_compression) && invoker.disable_compression) {
352      args += [ "--disable-asset-compression" ]
353    }
354    if (defined(invoker.treat_as_locale_paks) && invoker.treat_as_locale_paks) {
355      args += [ "--treat-as-locale-paks" ]
356    }
357
358    if (defined(invoker.merged_android_manifest)) {
359      args += [
360        "--merged-android-manifest",
361        rebase_path(invoker.merged_android_manifest, root_build_dir),
362      ]
363    }
364    if (defined(invoker.android_manifest)) {
365      inputs += [ invoker.android_manifest ]
366      args += [
367        "--android-manifest",
368        rebase_path(invoker.android_manifest, root_build_dir),
369      ]
370    }
371    if (defined(invoker.resources_zip)) {
372      args += [
373        "--resources-zip",
374        rebase_path(invoker.resources_zip, root_build_dir),
375      ]
376    }
377
378    if (defined(invoker.resource_overlay) && invoker.resource_overlay) {
379      args += [ "--resource-overlay" ]
380    }
381
382    if (defined(invoker.custom_package)) {
383      args += [
384        "--package-name",
385        invoker.custom_package,
386      ]
387    }
388    if (defined(invoker.r_text)) {
389      args += [
390        "--r-text-path",
391        rebase_path(invoker.r_text, root_build_dir),
392      ]
393    }
394    if (defined(invoker.res_size_info_path)) {
395      args += [
396        "--res-size-info",
397        rebase_path(invoker.res_size_info_path, root_build_dir),
398      ]
399    }
400    if (defined(invoker.res_sources_path)) {
401      _res_sources_path = rebase_path(invoker.res_sources_path, root_build_dir)
402      args += [ "--res-sources-path=$_res_sources_path" ]
403    }
404    if (defined(invoker.proto_resources_path)) {
405      _rebased_proto_resources =
406          rebase_path(invoker.proto_resources_path, root_build_dir)
407      args += [ "--apk-proto-resources=$_rebased_proto_resources" ]
408    }
409    if (defined(invoker.r_text_path)) {
410      _rebased_rtxt_path = rebase_path(invoker.r_text_path, root_build_dir)
411      args += [ "--r-text-path=$_rebased_rtxt_path" ]
412    }
413    if (defined(invoker.module_pathmap_path)) {
414      _rebased_pathmap_path =
415          rebase_path(invoker.module_pathmap_path, root_build_dir)
416      args += [ "--module-pathmap-path=$_rebased_pathmap_path" ]
417    }
418
419    if (defined(invoker.shared_libraries_runtime_deps_file)) {
420      # Don't list shared_libraries_runtime_deps_file as an input in order to
421      # avoid having to depend on the runtime_deps target. See comment in
422      # rules.gni for why we do this.
423      args += [
424        "--shared-libraries-runtime-deps",
425        rebase_path(invoker.shared_libraries_runtime_deps_file, root_build_dir),
426      ]
427    }
428
429    if (defined(invoker.base_allowlist_rtxt_path)) {
430      args += [
431        "--base-allowlist-rtxt-path",
432        rebase_path(invoker.base_allowlist_rtxt_path, root_build_dir),
433      ]
434    }
435
436    if (defined(invoker.loadable_modules)) {
437      _rebased_loadable_modules =
438          rebase_path(invoker.loadable_modules, root_build_dir)
439      args += [ "--loadable-modules=$_rebased_loadable_modules" ]
440    }
441
442    if (defined(invoker.secondary_abi_shared_libraries_runtime_deps_file)) {
443      # Don't list secondary_abi_shared_libraries_runtime_deps_file as an
444      # input in order to avoid having to depend on the runtime_deps target.
445      # See comment in rules.gni for why we do this.
446      args += [
447        "--secondary-abi-shared-libraries-runtime-deps",
448        rebase_path(invoker.secondary_abi_shared_libraries_runtime_deps_file,
449                    root_build_dir),
450      ]
451    }
452
453    if (defined(invoker.secondary_abi_loadable_modules) &&
454        invoker.secondary_abi_loadable_modules != []) {
455      _rebased_secondary_abi_loadable_modules =
456          rebase_path(invoker.secondary_abi_loadable_modules, root_build_dir)
457      args += [ "--secondary-abi-loadable-modules=$_rebased_secondary_abi_loadable_modules" ]
458    }
459
460    if (defined(invoker.native_lib_placeholders) &&
461        invoker.native_lib_placeholders != []) {
462      args += [ "--native-lib-placeholders=${invoker.native_lib_placeholders}" ]
463    }
464
465    if (defined(invoker.secondary_native_lib_placeholders) &&
466        invoker.secondary_native_lib_placeholders != []) {
467      args += [ "--secondary-native-lib-placeholders=${invoker.secondary_native_lib_placeholders}" ]
468    }
469
470    if (defined(invoker.library_always_compress)) {
471      args += [ "--library-always-compress=${invoker.library_always_compress}" ]
472    }
473
474    if (defined(invoker.apk_path)) {
475      # TODO(tiborg): Remove APK path from build config and use
476      # install_artifacts from metadata instead.
477      _rebased_apk_path = rebase_path(invoker.apk_path, root_build_dir)
478      args += [ "--apk-path=$_rebased_apk_path" ]
479      if (defined(invoker.incremental_apk_path)) {
480        _rebased_incremental_apk_path =
481            rebase_path(invoker.incremental_apk_path, root_build_dir)
482        _rebased_incremental_install_json_path =
483            rebase_path(invoker.incremental_install_json_path, root_build_dir)
484        args += [
485          "--incremental-install-json-path=$_rebased_incremental_install_json_path",
486          "--incremental-apk-path=$_rebased_incremental_apk_path",
487        ]
488      }
489    }
490
491    if (defined(invoker.target_sources_file)) {
492      args += [
493        "--target-sources-file",
494        rebase_path(invoker.target_sources_file, root_build_dir),
495      ]
496    }
497    if (defined(invoker.srcjar)) {
498      args += [
499        "--srcjar",
500        rebase_path(invoker.srcjar, root_build_dir),
501      ]
502    }
503    if (defined(invoker.bundled_srcjars)) {
504      _rebased_bundled_srcjars =
505          rebase_path(invoker.bundled_srcjars, root_build_dir)
506      args += [ "--bundled-srcjars=$_rebased_bundled_srcjars" ]
507    }
508    if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) {
509      args += [ "--proguard-enabled" ]
510    }
511    if (defined(invoker.proguard_mapping_path)) {
512      _rebased_proguard_mapping_path =
513          rebase_path(invoker.proguard_mapping_path, root_build_dir)
514      args += [ "--proguard-mapping-path=$_rebased_proguard_mapping_path" ]
515    }
516    if (defined(invoker.input_jars_paths)) {
517      _rebased_input_jars_paths =
518          rebase_path(invoker.input_jars_paths, root_build_dir)
519      args += [ "--extra-classpath-jars=$_rebased_input_jars_paths" ]
520    }
521    if (defined(invoker.low_classpath_priority) &&
522        invoker.low_classpath_priority) {
523      args += [ "--low-classpath-priority" ]
524    }
525    if (defined(invoker.mergeable_android_manifests)) {
526      _rebased_mergeable_android_manifests =
527          rebase_path(invoker.mergeable_android_manifests, root_build_dir)
528      args += [
529        "--mergeable-android-manifests=$_rebased_mergeable_android_manifests",
530      ]
531    }
532    if (defined(invoker.proguard_configs)) {
533      _rebased_proguard_configs =
534          rebase_path(invoker.proguard_configs, root_build_dir)
535      args += [ "--proguard-configs=$_rebased_proguard_configs" ]
536    }
537    if (defined(invoker.gradle_treat_as_prebuilt) &&
538        invoker.gradle_treat_as_prebuilt) {
539      args += [ "--gradle-treat-as-prebuilt" ]
540    }
541    if (defined(invoker.main_class)) {
542      args += [
543        "--main-class",
544        invoker.main_class,
545      ]
546    }
547    if (defined(invoker.base_module_target)) {
548      _dep_label = invoker.base_module_target
549      _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
550      _dep_name = get_label_info(_dep_label, "name")
551      _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
552      deps += [ "$_dep_label$build_config_target_suffix" ]
553      inputs += [ _dep_config ]
554      args += [
555        "--base-module-build-config",
556        rebase_path(_dep_config, root_build_dir),
557      ]
558    }
559    if (defined(invoker.parent_module_target)) {
560      _dep_label = invoker.parent_module_target
561      _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
562      _dep_name = get_label_info(_dep_label, "name")
563      _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
564      deps += [ "$_dep_label$build_config_target_suffix" ]
565      inputs += [ _dep_config ]
566      args += [
567        "--parent-module-build-config",
568        rebase_path(_dep_config, root_build_dir),
569      ]
570    }
571    if (defined(invoker.module_name)) {
572      args += [
573        "--module-name",
574        invoker.module_name,
575      ]
576    }
577    if (defined(invoker.modules)) {
578      foreach(_module, invoker.modules) {
579        if (defined(_module.uses_split)) {
580          args += [ "--uses-split=${_module.name}:${_module.uses_split}" ]
581        }
582      }
583    }
584    if (defined(invoker.module_build_configs)) {
585      inputs += invoker.module_build_configs
586      _rebased_configs =
587          rebase_path(invoker.module_build_configs, root_build_dir)
588      args += [ "--module-build-configs=$_rebased_configs" ]
589    }
590    if (defined(invoker.add_view_trace_events) &&
591        invoker.add_view_trace_events) {
592      # Adding trace events involves rewriting bytecode and generating a new set
593      # of jar files. In order to avoid conflicts between bundles we save the
594      # new jars in a bundle specific gen/ directory. The build config for the
595      # bundle, and each one of its modules need a path to a bundle specific
596      # gen/ directory in order to generate a list of rewritten jar paths.
597      # We use the base module's target_gen_dir because non-base modules and the
598      # app bundle targets have a reference to it (base_module_target).
599      if (_type == "android_app_bundle") {
600        _trace_events_target_name =
601            get_label_info(_parent_invoker.base_module_target, "name")
602      } else if (defined(invoker.base_module_target)) {
603        _trace_events_target_name =
604            get_label_info(invoker.base_module_target, "name")
605      } else {
606        _grandparent_invoker = _parent_invoker.invoker
607        _trace_events_target_name = _grandparent_invoker.target_name
608      }
609
610      # FIXME: This should likely be using the base module's target_out_dir
611      #     rather than the current target's.
612      args += [
613        "--trace-events-jar-dir",
614        rebase_path("$target_out_dir/$_trace_events_target_name",
615                    root_build_dir),
616      ]
617    }
618    if (defined(invoker.version_name)) {
619      args += [
620        "--version-name",
621        invoker.version_name,
622      ]
623    }
624    if (defined(invoker.version_code)) {
625      args += [
626        "--version-code",
627        invoker.version_code,
628      ]
629    }
630    if (defined(invoker.recursive_resource_deps) &&
631        invoker.recursive_resource_deps) {
632      args += [ "--recursive-resource-deps" ]
633    }
634    if (current_toolchain != default_toolchain) {
635      # This has to be a built-time error rather than a GN assert because many
636      # packages have a mix of java and non-java targets. For example, the
637      # following would fail even though nothing depends on :bar(//baz):
638      #
639      # shared_library("foo") {
640      # }
641      #
642      # android_library("bar") {
643      #   deps = [ ":foo(//baz)" ]
644      #   assert(current_toolchain == default_toolchain)
645      # }
646      _msg = [
647        "Tried to build an Android target in a non-default toolchain.",
648        "target: $_target_label",
649        "current_toolchain: $current_toolchain",
650        "default_toolchain: $default_toolchain",
651      ]
652      args += [ "--fail=$_msg" ]
653    }
654  }
655}
656
657template("generate_android_wrapper") {
658  generate_wrapper(target_name) {
659    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
660    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
661    generator_script = "//build/android/gyp/generate_android_wrapper.py"
662    sources = [
663      "//build/android/gyp/util/build_utils.py",
664      "//build/gn_helpers.py",
665      "//build/util/generate_wrapper.py",
666    ]
667  }
668}
669
670template("generate_r_java") {
671  action_with_pydeps(target_name) {
672    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
673    depfile = "$target_gen_dir/${invoker.target_name}.d"
674    inputs = [ invoker.build_config ]
675    outputs = [ invoker.srcjar_path ]
676    _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
677    script = "//build/android/gyp/create_r_java.py"
678    args = [
679      "--depfile",
680      rebase_path(depfile, root_build_dir),
681      "--srcjar-out",
682      rebase_path(invoker.srcjar_path, root_build_dir),
683      "--deps-rtxts=@FileArg($_rebased_build_config:deps_info:dependency_r_txt_files)",
684      "--r-package=${invoker.package}",
685    ]
686  }
687}
688
689# Generates a script in the build bin directory which runs the test
690# target using the test runner script in build/android/test_runner.py.
691template("test_runner_script") {
692  testonly = true
693  _test_name = invoker.test_name
694  _test_type = invoker.test_type
695  _is_unit_test = defined(invoker.is_unit_test) && invoker.is_unit_test
696  _incremental_apk = defined(invoker.incremental_apk) && invoker.incremental_apk
697
698  _runtime_deps =
699      !defined(invoker.ignore_all_data_deps) || !invoker.ignore_all_data_deps
700
701  if (_runtime_deps) {
702    # This runtime_deps file is used at runtime and thus cannot go in
703    # target_gen_dir.
704    _target_dir_name = get_label_info(":$target_name", "dir")
705    _runtime_deps_file =
706        "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.runtime_deps"
707    _runtime_deps_target = "${target_name}__write_deps"
708    group(_runtime_deps_target) {
709      forward_variables_from(invoker,
710                             [
711                               "data",
712                               "deps",
713                               "public_deps",
714                             ])
715      data_deps = []
716      if (defined(invoker.data_deps)) {
717        data_deps += invoker.data_deps
718      }
719      if (defined(invoker.additional_apks)) {
720        data_deps += invoker.additional_apks
721      }
722      write_runtime_deps = _runtime_deps_file
723    }
724  }
725
726  if (defined(invoker.apk_under_test)) {
727    _install_artifacts_json =
728        "${target_gen_dir}/${target_name}.install_artifacts"
729    _install_artifacts_target_name = "${target_name}__install_artifacts"
730    generated_file(_install_artifacts_target_name) {
731      deps = [ invoker.apk_under_test ]
732      output_conversion = "json"
733      outputs = [ _install_artifacts_json ]
734      data_keys = [ "install_artifacts" ]
735      walk_keys = [ "install_artifacts_barrier" ]
736      rebase = root_build_dir
737    }
738  }
739
740  generate_android_wrapper(target_name) {
741    forward_variables_from(invoker,
742                           [
743                             "assert_no_deps",
744                             "metadata",
745                             "public_deps",
746                             "visibility",
747                           ])
748    wrapper_script = "$root_build_dir/bin/run_${_test_name}"
749
750    executable = "//testing/test_env.py"
751
752    if (defined(invoker.android_test_runner_script)) {
753      _runner_script = invoker.android_test_runner_script
754    } else {
755      _runner_script = "//build/android/test_runner.py"
756    }
757
758    deps = []
759    if (defined(invoker.deps)) {
760      deps = invoker.deps
761    }
762    data_deps = [
763      "//build/android:test_runner_core_py",
764      "//testing:test_scripts_shared",
765    ]
766    if (_test_type != "junit") {
767      data_deps += [ "//build/android:test_runner_device_support" ]
768    }
769    if (defined(invoker.data_deps)) {
770      data_deps += invoker.data_deps
771    }
772    data = []
773    if (defined(invoker.data)) {
774      data += invoker.data
775    }
776
777    executable_args = [
778      "@WrappedPath(" + rebase_path(_runner_script, root_build_dir) + ")",
779      _test_type,
780      "--output-directory",
781      "@WrappedPath(.)",
782      "--wrapper-script-args",
783    ]
784
785    if (_is_unit_test) {
786      executable_args += [ "--is-unit-test" ]
787    }
788
789    if (_runtime_deps) {
790      deps += [ ":$_runtime_deps_target" ]
791      data += [ _runtime_deps_file ]
792      _rebased_runtime_deps_file =
793          rebase_path(_runtime_deps_file, root_build_dir)
794      executable_args += [
795        "--runtime-deps-path",
796        "@WrappedPath(${_rebased_runtime_deps_file})",
797      ]
798    }
799
800    # apk_target is not used for native executable tests
801    # (e.g. breakpad_unittests).
802    if (defined(invoker.apk_target)) {
803      assert(!defined(invoker.executable_dist_dir))
804      deps += [ "${invoker.apk_target}$build_config_target_suffix" ]
805      _apk_build_config =
806          get_label_info(invoker.apk_target, "target_gen_dir") + "/" +
807          get_label_info(invoker.apk_target, "name") + ".build_config.json"
808      _rebased_apk_build_config = rebase_path(_apk_build_config, root_build_dir)
809      not_needed([ "_rebased_apk_build_config" ])
810    } else if (_test_type == "gtest") {
811      assert(
812          defined(invoker.executable_dist_dir),
813          "Must define either apk_target or executable_dist_dir for test_runner_script()")
814      _rebased_executable_dist_dir =
815          rebase_path(invoker.executable_dist_dir, root_build_dir)
816      executable_args += [
817        "--executable-dist-dir",
818        "@WrappedPath(${_rebased_executable_dist_dir})",
819      ]
820    }
821
822    if (use_jacoco_coverage) {
823      # Keep in sync with recipe constant for recipe to find coverage data
824      # files from local java tests: https://bit.ly/3Zul6do
825      _jacoco_coverage_dir_name = "java_coverage"
826    }
827
828    _device_test = true
829    if (_test_type == "gtest") {
830      assert(defined(invoker.test_suite))
831      executable_args += [
832        "--suite",
833        invoker.test_suite,
834      ]
835      if (use_clang_coverage) {
836        # Set a default coverage output directory (can be overridden by user
837        # passing the same flag).
838        _rebased_coverage_dir =
839            rebase_path("$root_out_dir/coverage", root_build_dir)
840        executable_args += [
841          "--coverage-dir",
842          "@WrappedPath(${_rebased_coverage_dir})",
843        ]
844      }
845    } else if (_test_type == "instrumentation") {
846      _test_apk = "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:apk_path))"
847      if (_incremental_apk) {
848        _test_apk = "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:incremental_apk_path))"
849      }
850      executable_args += [
851        "--test-apk",
852        _test_apk,
853      ]
854      if (defined(invoker.apk_under_test)) {
855        if (_incremental_apk) {
856          deps += [ "${invoker.apk_under_test}$build_config_target_suffix" ]
857          _apk_under_test_build_config =
858              get_label_info(invoker.apk_under_test, "target_gen_dir") + "/" +
859              get_label_info(invoker.apk_under_test, "name") +
860              ".build_config.json"
861          _rebased_apk_under_test_build_config =
862              rebase_path(_apk_under_test_build_config, root_build_dir)
863          _apk_under_test = "@WrappedPath(@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_apk_path))"
864        } else {
865          deps += [ ":${_install_artifacts_target_name}" ]
866          _rebased_install_artifacts_json =
867              rebase_path(_install_artifacts_json, root_build_dir)
868          _apk_under_test =
869              "@WrappedPath(@FileArg($_rebased_install_artifacts_json[]))"
870        }
871        executable_args += [
872          "--apk-under-test",
873          _apk_under_test,
874        ]
875      }
876      if (defined(invoker.use_webview_provider)) {
877        deps += [ "${invoker.use_webview_provider}$build_config_target_suffix" ]
878        _build_config =
879            get_label_info(invoker.use_webview_provider, "target_gen_dir") +
880            "/" + get_label_info(invoker.use_webview_provider, "name") +
881            ".build_config.json"
882        _rebased_build_config = rebase_path(_build_config, root_build_dir)
883        executable_args += [
884          "--use-webview-provider",
885          "@WrappedPath(@FileArg($_rebased_build_config:deps_info:apk_path))",
886        ]
887      }
888      if (defined(invoker.proguard_mapping_path)) {
889        if (_incremental_apk) {
890          not_needed(invoker, [ "proguard_mapping_path" ])
891        } else {
892          data += [ invoker.proguard_mapping_path ]
893          _rebased_mapping_path =
894              rebase_path(invoker.proguard_mapping_path, root_build_dir)
895          executable_args += [
896            "--proguard-mapping-path",
897            "@WrappedPath($_rebased_mapping_path)",
898          ]
899        }
900      }
901      if (use_jacoco_coverage) {
902        # Set a default coverage output directory (can be overridden by user
903        # passing the same flag).
904        _rebased_coverage_dir =
905            rebase_path("$root_out_dir/$_jacoco_coverage_dir_name",
906                        root_build_dir)
907        executable_args += [
908          "--coverage-dir",
909          "@WrappedPath(${_rebased_coverage_dir})",
910        ]
911      }
912    } else if (_test_type == "junit") {
913      assert(defined(invoker.test_suite))
914      _device_test = false
915      executable_args += [
916        "--test-suite",
917        invoker.test_suite,
918        "--native-libs-dir",
919        "@WrappedPath($_robolectric_libs_dir)",
920      ]
921
922      # Test runner uses this generated wrapper script.
923      data += [ "$root_build_dir/bin/helper/${invoker.test_suite}" ]
924
925      deps += [ ":${invoker.test_suite}$build_config_target_suffix" ]
926
927      _rebased_robolectric_runtime_deps_dir =
928          rebase_path("//third_party/robolectric/lib", root_build_dir)
929      _rebased_resource_apk = rebase_path(invoker.resource_apk, root_build_dir)
930      executable_args += [
931        "--resource-apk",
932        "@WrappedPath(${_rebased_resource_apk})",
933        "--robolectric-runtime-deps-dir",
934        "@WrappedPath(${_rebased_robolectric_runtime_deps_dir})",
935      ]
936      if (use_jacoco_coverage) {
937        # Set a default coverage output directory (can be overridden by user
938        # passing the same flag).
939        _rebased_coverage_dir =
940            rebase_path("$root_out_dir/$_jacoco_coverage_dir_name",
941                        root_build_dir)
942        executable_args += [
943          "--coverage-dir",
944          "@WrappedPath(${_rebased_coverage_dir})",
945        ]
946      }
947    } else if (_test_type == "linker") {
948      executable_args += [
949        "--test-apk",
950        "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:apk_path))",
951      ]
952    } else {
953      assert(false, "Invalid test type: $_test_type.")
954    }
955
956    # Devil does not reliably work with component builds of its tools.
957    # There's no benefit to them, so fall back to prebuilts for component builds.
958    # https://crbug.com/1404180
959    if (_test_type != "junit" && !is_component_build) {
960      executable_args += [ "--use-local-devil-tools" ]
961    }
962
963    if (defined(invoker.additional_apks)) {
964      foreach(additional_apk, invoker.additional_apks) {
965        deps += [ "$additional_apk$build_config_target_suffix" ]
966        _build_config =
967            get_label_info(additional_apk, "target_gen_dir") + "/" +
968            get_label_info(additional_apk, "name") + ".build_config.json"
969        _rebased_build_config = rebase_path(_build_config, root_build_dir)
970        executable_args += [
971          "--additional-apk",
972          "@WrappedPath(@FileArg($_rebased_build_config:deps_info:apk_path))",
973        ]
974      }
975    }
976    if (defined(invoker.shard_timeout)) {
977      executable_args += [ "--shard-timeout=${invoker.shard_timeout}" ]
978    }
979    if (_incremental_apk) {
980      executable_args += [
981        "--test-apk-incremental-install-json",
982        "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:incremental_install_json_path))",
983      ]
984      if (defined(invoker.apk_under_test)) {
985        executable_args += [
986          "--apk-under-test-incremental-install-json",
987          "@WrappedPath(@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_install_json_path))",
988        ]
989      }
990      executable_args += [ "--fast-local-dev" ]
991    }
992    if (_device_test && is_asan) {
993      executable_args += [ "--tool=asan" ]
994    }
995
996    if (defined(invoker.modules)) {
997      foreach(module, invoker.modules) {
998        executable_args += [
999          "--module",
1000          module,
1001        ]
1002      }
1003    }
1004
1005    if (defined(invoker.fake_modules)) {
1006      foreach(fake_module, invoker.fake_modules) {
1007        executable_args += [
1008          "--fake-module",
1009          fake_module,
1010        ]
1011      }
1012    }
1013
1014    if (defined(invoker.additional_locales)) {
1015      foreach(locale, invoker.additional_locales) {
1016        executable_args += [
1017          "--additional-locale",
1018          locale,
1019        ]
1020      }
1021    }
1022
1023    if (defined(invoker.extra_args)) {
1024      executable_args += invoker.extra_args
1025    }
1026  }
1027}
1028
1029if (enable_java_templates) {
1030  template("android_lint") {
1031    action_with_pydeps(target_name) {
1032      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1033
1034      # https://crbug.com/1098752 Fix for bot OOM (https://crbug.com/1098333).
1035      if (defined(java_cmd_pool_size)) {
1036        pool = "//build/config/android:java_cmd_pool($default_toolchain)"
1037      } else {
1038        pool = "//build/toolchain:link_pool($default_toolchain)"
1039      }
1040
1041      # Lint requires generated sources and generated resources from the build.
1042      # Turbine __header targets depend on all generated sources, and the
1043      # __assetres targets depend on all generated resources.
1044      deps = []
1045      if (defined(invoker.deps)) {
1046        _lib_deps =
1047            filter_exclude(filter_include(invoker.deps, java_library_patterns),
1048                           java_resource_patterns)
1049        foreach(_lib_dep, _lib_deps) {
1050          # Expand //foo/java -> //foo/java:java
1051          _lib_dep = get_label_info(_lib_dep, "label_no_toolchain")
1052          deps += [
1053            "${_lib_dep}__assetres",
1054            "${_lib_dep}__header",
1055          ]
1056        }
1057
1058        # Keep non-java deps as they may generate files used only by lint.
1059        # e.g. generated suppressions.xml files.
1060        deps += filter_exclude(invoker.deps, _lib_deps)
1061      }
1062
1063      if (defined(invoker.min_sdk_version)) {
1064        _min_sdk_version = invoker.min_sdk_version
1065      } else {
1066        _min_sdk_version = default_min_sdk_version
1067      }
1068
1069      if (defined(invoker.lint_jar_path)) {
1070        _lint_jar_path = invoker.lint_jar_path
1071      } else {
1072        _lint_jar_path = _default_lint_jar_path
1073      }
1074
1075      # It is not safe to run two lint versions concurrently since they will
1076      # wipe the cache on version mismatch. When using a non-default lint
1077      # version, make each target use their own cache directory.
1078      _use_custom_cache_dir = _lint_jar_path != _default_lint_jar_path
1079
1080      # Save generated xml files in a consistent location for debugging.
1081      if (defined(invoker.lint_gen_dir)) {
1082        _lint_gen_dir = invoker.lint_gen_dir
1083      } else {
1084        _lint_gen_dir = "$target_gen_dir/$target_name"
1085      }
1086      _backported_methods = "//third_party/r8/backported_methods.txt"
1087
1088      script = "//build/android/gyp/lint.py"
1089      depfile = "$target_gen_dir/$target_name.d"
1090      inputs = java_paths_for_inputs + [
1091                 _lint_jar_path,
1092                 _custom_lint_jar_path,
1093                 _backported_methods,
1094               ]
1095
1096      args = [
1097        "--target-name",
1098        get_label_info(":${target_name}", "label_no_toolchain"),
1099        "--depfile",
1100        rebase_path(depfile, root_build_dir),
1101        "--lint-jar-path",
1102        rebase_path(_lint_jar_path, root_build_dir),
1103        "--custom-lint-jar-path",
1104        rebase_path(_custom_lint_jar_path, root_build_dir),
1105        "--lint-gen-dir",
1106        rebase_path(_lint_gen_dir, root_build_dir),
1107        "--android-sdk-version=${lint_android_sdk_version}",
1108        "--min-sdk-version=$_min_sdk_version",
1109        "--android-sdk-root",
1110        rebase_path(lint_android_sdk_root, root_build_dir),
1111        "--backported-methods",
1112        rebase_path(_backported_methods, root_build_dir),
1113      ]
1114
1115      if (!_use_custom_cache_dir) {
1116        _cache_dir = "$root_build_dir/android_lint_cache"
1117        _create_cache_stamp_path = "$_cache_dir/build.lint.stamp"
1118
1119        # By default, lint.py will use "$_lint_gen_dir/cache".
1120        args += [
1121          "--cache-dir",
1122          rebase_path(_cache_dir, root_build_dir),
1123        ]
1124      }
1125
1126      if (defined(invoker.skip_build_server) && invoker.skip_build_server) {
1127        # Nocompile tests need lint to fail through ninja.
1128        args += [ "--skip-build-server" ]
1129      } else if (android_static_analysis == "build_server") {
1130        args += [ "--use-build-server" ]
1131      }
1132
1133      if (defined(invoker.lint_suppressions_file)) {
1134        inputs += [ invoker.lint_suppressions_file ]
1135
1136        args += [
1137          "--config-path",
1138          rebase_path(invoker.lint_suppressions_file, root_build_dir),
1139        ]
1140      }
1141
1142      if (defined(testonly) && testonly) {
1143        # Allows us to ignore unnecessary checks when linting test targets.
1144        args += [ "--testonly" ]
1145      }
1146
1147      if (defined(invoker.manifest_package)) {
1148        args += [ "--manifest-package=${invoker.manifest_package}" ]
1149      }
1150
1151      if (treat_warnings_as_errors) {
1152        args += [ "--warnings-as-errors" ]
1153      }
1154
1155      if (defined(invoker.lint_baseline_file)) {
1156        if (compute_inputs_for_analyze) {
1157          # The baseline file is included in lint.py as a depfile dep. Since
1158          # removing it regenerates the file, it is useful to not have this as
1159          # a gn input during local development. Add it only for bots' analyze.
1160          inputs += [ invoker.lint_baseline_file ]
1161        }
1162        args += [
1163          # Baseline allows us to turn on lint warnings without fixing all the
1164          # pre-existing issues. This stops the flood of new issues while the
1165          # existing ones are being fixed.
1166          "--baseline",
1167          rebase_path(invoker.lint_baseline_file, root_build_dir),
1168        ]
1169      }
1170
1171      if (defined(invoker.create_cache) && invoker.create_cache) {
1172        # Putting the stamp file in the cache dir allows us to depend on ninja
1173        # to create the cache dir for us.
1174        args += [ "--create-cache" ]
1175        _stamp_path = _create_cache_stamp_path
1176      } else {
1177        _stamp_path = "$target_out_dir/$target_name/build.lint.stamp"
1178        deps += [ invoker.build_config_dep ]
1179        if (!_use_custom_cache_dir) {
1180          deps += [ "//build/android:prepare_android_lint_cache" ]
1181          inputs += [ _create_cache_stamp_path ]
1182        }
1183        inputs += [ invoker.build_config ]
1184        _rebased_build_config =
1185            rebase_path(invoker.build_config, root_build_dir)
1186
1187        args += [
1188          "--manifest-path=@FileArg($_rebased_build_config:deps_info:lint_android_manifest)",
1189          "--extra-manifest-paths=@FileArg($_rebased_build_config:deps_info:lint_extra_android_manifests)",
1190
1191          # Lint requires all source and all resource files to be passed in the
1192          # same invocation for checks like UnusedResources.
1193          "--sources=@FileArg($_rebased_build_config:deps_info:lint_sources)",
1194          "--aars=@FileArg($_rebased_build_config:deps_info:lint_aars)",
1195          "--srcjars=@FileArg($_rebased_build_config:deps_info:lint_srcjars)",
1196          "--resource-sources=@FileArg($_rebased_build_config:deps_info:lint_resource_sources)",
1197          "--resource-zips=@FileArg($_rebased_build_config:deps_info:lint_resource_zips)",
1198
1199          # The full classpath is required for annotation checks like @IntDef.
1200          "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)",
1201        ]
1202      }
1203
1204      outputs = [ _stamp_path ]
1205      args += [
1206        "--stamp",
1207        rebase_path(_stamp_path, root_build_dir),
1208      ]
1209    }
1210  }
1211
1212  template("proguard") {
1213    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1214    _script = "//build/android/gyp/proguard.py"
1215    _deps = invoker.deps
1216
1217    _inputs = java_paths_for_inputs + [
1218                invoker.build_config,
1219                _r8_path,
1220                _custom_r8_path,
1221              ]
1222    if (defined(invoker.inputs)) {
1223      _inputs += invoker.inputs
1224    }
1225    if (defined(invoker.proguard_mapping_path)) {
1226      _mapping_path = invoker.proguard_mapping_path
1227    } else {
1228      _mapping_path = "${invoker.output_path}.mapping"
1229    }
1230
1231    _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
1232
1233    # This is generally the apk name, and serves to identify the mapping
1234    # file that would be required to deobfuscate a stacktrace.
1235    _mapping_basename = get_path_info(_mapping_path, "name")
1236    _version_code = "@FileArg($_rebased_build_config:deps_info:version_code)"
1237    _package_name = "@FileArg($_rebased_build_config:deps_info:package_name)"
1238    if (defined(invoker.package_name)) {
1239      _package_name = invoker.package_name
1240    }
1241    if (defined(invoker.version_code)) {
1242      _version_code = invoker.version_code
1243    }
1244
1245    # The Mapping ID is parsed to when uploading mapping files.
1246    # See: https://crbug.com/1417308
1247    _source_file_template =
1248        "chromium-$_mapping_basename-$android_channel-$_version_code"
1249
1250    _args = [
1251      "--min-api=${invoker.min_sdk_version}",
1252      "--mapping-output",
1253      rebase_path(_mapping_path, root_build_dir),
1254      "--classpath",
1255      "@FileArg($_rebased_build_config:deps_info:proguard_classpath_jars)",
1256      "--classpath",
1257      "@FileArg($_rebased_build_config:android:sdk_jars)",
1258      "--r8-path",
1259      rebase_path(_r8_path, root_build_dir),
1260      "--custom-r8-path",
1261      rebase_path(_custom_r8_path, root_build_dir),
1262      "--package-name=$_package_name",
1263      "--source-file",
1264      _source_file_template,
1265      "--proguard-configs=@FileArg($_rebased_build_config:deps_info:proguard_all_configs)",
1266    ]
1267    if (treat_warnings_as_errors) {
1268      _args += [ "--warnings-as-errors" ]
1269    }
1270
1271    if ((!defined(invoker.proguard_enable_obfuscation) ||
1272         invoker.proguard_enable_obfuscation) && enable_proguard_obfuscation) {
1273      _args += [ "--enable-obfuscation" ]
1274    }
1275    if (defined(invoker.repackage_classes)) {
1276      _args += [ "--repackage-classes=" + invoker.repackage_classes ]
1277    }
1278    if (defined(invoker.apply_mapping)) {
1279      _inputs += [ invoker.apply_mapping ]
1280      _rebased_apply_mapping_path =
1281          rebase_path(invoker.apply_mapping, root_build_dir)
1282      args += [ "--apply-mapping=$_rebased_apply_mapping_path" ]
1283    }
1284
1285    if (defined(invoker.proguard_configs)) {
1286      _inputs += invoker.proguard_configs
1287      _rebased_proguard_configs =
1288          rebase_path(invoker.proguard_configs, root_build_dir)
1289      _args += [ "--proguard-configs=$_rebased_proguard_configs" ]
1290    }
1291
1292    if (defined(invoker.modules)) {
1293      foreach(_feature_module, invoker.modules) {
1294        _rebased_module_build_config =
1295            rebase_path(_feature_module.build_config, root_build_dir)
1296        _args += [
1297          "--feature-name=${_feature_module.name}",
1298          "--dex-dest=@FileArg($_rebased_module_build_config:final_dex:path)",
1299        ]
1300
1301        # The bundle's build config has the correct classpaths - the individual
1302        # modules' build configs may double-use some jars.
1303        if (defined(invoker.add_view_trace_events) &&
1304            invoker.add_view_trace_events) {
1305          _args += [ "--feature-jars=@FileArg($_rebased_build_config:modules:${_feature_module.name}:trace_event_rewritten_device_classpath)" ]
1306        } else {
1307          _args += [ "--feature-jars=@FileArg($_rebased_build_config:modules:${_feature_module.name}:device_classpath)" ]
1308        }
1309
1310        if (defined(_feature_module.uses_split)) {
1311          _args += [ "--uses-split=${_feature_module.name}:${_feature_module.uses_split}" ]
1312        }
1313        _deps += [ _feature_module.build_config_target ]
1314      }
1315      _stamp = "${target_gen_dir}/${target_name}.r8.stamp"
1316      _outputs = [ _stamp ]
1317      _output_arg = [
1318        "--stamp",
1319        rebase_path(_stamp, root_build_dir),
1320      ]
1321    } else {
1322      # We don't directly set the output arg on the _args variable since it is
1323      # shared with the expectation target that uses its own stamp file and
1324      # does not take an --output-path.
1325      _output_arg = [
1326        "--output-path",
1327        rebase_path(invoker.output_path, root_build_dir),
1328      ]
1329      _outputs = [ invoker.output_path ]
1330    }
1331    _outputs += [ _mapping_path ]
1332
1333    if (defined(invoker.input_art_profile)) {
1334      _inputs += [ invoker.input_art_profile ]
1335      _args += [ "--input-art-profile=" +
1336                 rebase_path(invoker.input_art_profile, root_build_dir) ]
1337      if (defined(invoker.output_art_profile)) {
1338        _outputs += [ invoker.output_art_profile ]
1339        _args += [ "--output-art-profile=" +
1340                   rebase_path(invoker.output_art_profile, root_build_dir) ]
1341      }
1342      if (defined(invoker.enable_startup_profile) &&
1343          invoker.enable_startup_profile) {
1344        _args += [ "--apply-startup-profile" ]
1345      }
1346    }
1347
1348    if (defined(invoker.enable_proguard_checks) &&
1349        !invoker.enable_proguard_checks) {
1350      _args += [ "--disable-checks" ]
1351    }
1352
1353    _ignore_desugar_missing_deps =
1354        defined(invoker.ignore_desugar_missing_deps) &&
1355        invoker.ignore_desugar_missing_deps
1356    if (!_ignore_desugar_missing_deps) {
1357      _args += [ "--show-desugar-default-interface-warnings" ]
1358    }
1359
1360    if (defined(invoker.custom_assertion_handler)) {
1361      _args += [
1362        "--assertion-handler",
1363        invoker.custom_assertion_handler,
1364      ]
1365    } else if (enable_java_asserts) {
1366      # The default for generating dex file format is
1367      # --force-disable-assertions.
1368      _args += [ "--force-enable-assertions" ]
1369    }
1370
1371    if (defined(invoker.args)) {
1372      _args += invoker.args
1373    }
1374
1375    if (defined(invoker.expected_proguard_config)) {
1376      _expectations_target =
1377          "${invoker.top_target_name}_validate_proguard_config"
1378      action_with_pydeps(_expectations_target) {
1379        script = _script
1380
1381        # Need to depend on all deps so that proguard.txt within .aar files get
1382        # extracted.
1383        deps = _deps
1384        depfile = "${target_gen_dir}/${target_name}.d"
1385        inputs = [
1386          invoker.build_config,
1387          invoker.expected_proguard_config,
1388        ]
1389        _actual_file = "$target_gen_dir/$target_name.proguard_configs"
1390        _failure_file =
1391            "$expectations_failure_dir/" +
1392            string_replace(invoker.expected_proguard_config, "/", "_")
1393        outputs = [
1394          _actual_file,
1395          _failure_file,
1396        ]
1397        args = _args + [
1398                 "--depfile",
1399                 rebase_path(depfile, root_build_dir),
1400                 "--failure-file",
1401                 rebase_path(_failure_file, root_build_dir),
1402                 "--expected-file",
1403                 rebase_path(invoker.expected_proguard_config, root_build_dir),
1404                 "--actual-file",
1405                 rebase_path(_actual_file, root_build_dir),
1406                 "--only-verify-expectations",
1407               ]
1408        if (defined(invoker.expected_proguard_config_base)) {
1409          inputs += [ invoker.expected_proguard_config_base ]
1410          args += [
1411            "--expected-file-base",
1412            rebase_path(invoker.expected_proguard_config_base, root_build_dir),
1413          ]
1414        }
1415        if (fail_on_android_expectations) {
1416          args += [ "--fail-on-expectations" ]
1417        }
1418      }
1419      _deps += [ ":$_expectations_target" ]
1420    }
1421    action_with_pydeps(target_name) {
1422      forward_variables_from(invoker,
1423                             [
1424                               "data",
1425                               "data_deps",
1426                               "public_deps",
1427                             ])
1428      script = _script
1429      deps = _deps
1430      inputs = _inputs
1431      outputs = _outputs
1432      depfile = "${target_gen_dir}/${target_name}.d"
1433      args = _args + _output_arg + [
1434               "--depfile",
1435               rebase_path(depfile, root_build_dir),
1436             ]
1437
1438      # http://crbug.com/725224. Fix for bots running out of memory.
1439      if (defined(java_cmd_pool_size)) {
1440        pool = "//build/config/android:java_cmd_pool($default_toolchain)"
1441      } else {
1442        pool = "//build/toolchain:link_pool($default_toolchain)"
1443      }
1444    }
1445  }
1446
1447  # Generates a script in the build bin directory to run a java binary.
1448  #
1449  # Variables
1450  #   main_class: The class containing the program entry point.
1451  #   build_config: Path to .build_config.json for the jar (contains classpath).
1452  #   script_name: Name of the script to generate.
1453  #   wrapper_script_args: List of extra arguments to pass to the executable.
1454  #   tiered_stop_at_level_one: Whether to pass --tiered-stop-at-level-one
1455  #
1456  template("java_binary_script") {
1457    action_with_pydeps(target_name) {
1458      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1459
1460      _main_class = invoker.main_class
1461      _build_config = invoker.build_config
1462      _script_name = invoker.script_name
1463      if (defined(invoker.max_heap_size)) {
1464        _max_heap_size = invoker.max_heap_size
1465      } else {
1466        _max_heap_size = "1G"
1467      }
1468
1469      script = "//build/android/gyp/create_java_binary_script.py"
1470      inputs = [ _build_config ]
1471      _java_script = "$root_build_dir/bin/$_script_name"
1472      outputs = [ _java_script ]
1473      _rebased_build_config = rebase_path(_build_config, root_build_dir)
1474      args = [
1475        "--output",
1476        rebase_path(_java_script, root_build_dir),
1477        "--main-class",
1478        _main_class,
1479        "--classpath=@FileArg($_rebased_build_config:deps_info:host_classpath)",
1480        "--max-heap-size=$_max_heap_size",
1481      ]
1482      data = []
1483      deps = [ "//third_party/jdk:java_data" ]
1484      if (defined(invoker.deps)) {
1485        deps += invoker.deps
1486      }
1487
1488      if (enable_java_asserts) {
1489        args += [ "--enable-asserts" ]
1490      }
1491      if (use_jacoco_coverage) {
1492        args += [
1493          "--classpath",
1494          rebase_path(_jacoco_host_jar, root_build_dir),
1495        ]
1496        data += [ _jacoco_host_jar ]
1497      }
1498      if (defined(invoker.tiered_stop_at_level_one) &&
1499          invoker.tiered_stop_at_level_one) {
1500        args += [ "--tiered-stop-at-level-one" ]
1501      }
1502      if (defined(invoker.extra_classpath_jars)) {
1503        _rebased_extra_classpath_jars =
1504            rebase_path(invoker.extra_classpath_jars, root_build_dir)
1505        args += [ "--classpath=${_rebased_extra_classpath_jars}" ]
1506        data += invoker.extra_classpath_jars
1507      }
1508      if (defined(invoker.wrapper_script_args)) {
1509        args += [ "--" ] + invoker.wrapper_script_args
1510      }
1511    }
1512  }
1513
1514  # Variables
1515  #   apply_mapping: The path to the ProGuard mapping file to apply.
1516  #   disable_incremental: Disable incremental dexing.
1517  template("dex") {
1518    _min_sdk_version = default_min_sdk_version
1519    if (defined(invoker.min_sdk_version)) {
1520      _min_sdk_version = invoker.min_sdk_version
1521    }
1522    assert(
1523        _min_sdk_version >= min_supported_sdk_version,
1524        get_label_info(":$target_name", "label_no_toolchain") + " has an unsupported min_sdk_version of $_min_sdk_version (min is $min_supported_sdk_version)")
1525
1526    _proguard_enabled =
1527        defined(invoker.proguard_enabled) && invoker.proguard_enabled
1528    _is_dex_merging = defined(invoker.input_dex_filearg)
1529    _enable_desugar = !defined(invoker.enable_desugar) || invoker.enable_desugar
1530    _desugar_needs_classpath = _enable_desugar
1531
1532    # It's not safe to dex merge with libraries dex'ed at higher api versions.
1533    assert(!_is_dex_merging || _min_sdk_version >= default_min_sdk_version)
1534
1535    # For D8's backported method desugaring to work properly, the dex merge step
1536    # must not be set to a higher minSdkVersion than it was for the libraries.
1537    if (_enable_desugar && _is_dex_merging) {
1538      _min_sdk_version = default_min_sdk_version
1539    }
1540
1541    assert(defined(invoker.output) ||
1542           (_proguard_enabled && defined(invoker.modules)))
1543    assert(!_proguard_enabled || !(defined(invoker.input_dex_filearg) ||
1544                                       defined(invoker.input_classes_filearg) ||
1545                                       defined(invoker.input_class_jars)),
1546           "Cannot explicitly set inputs when proguarding a dex.")
1547
1548    # Dex merging should not also be dexing.
1549    assert(!(_is_dex_merging && defined(invoker.input_classes_filearg)))
1550    assert(!(_is_dex_merging && defined(invoker.input_class_jars)))
1551
1552    assert(!(defined(invoker.apply_mapping) && !_proguard_enabled),
1553           "apply_mapping can only be specified if proguard is enabled.")
1554    if (defined(invoker.custom_assertion_handler)) {
1555      assert(_proguard_enabled,
1556             "Proguard is required to support the custom assertion handler.")
1557    }
1558
1559    if (_desugar_needs_classpath || _proguard_enabled) {
1560      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
1561    }
1562
1563    if (_proguard_enabled) {
1564      _proguard_target_name = target_name
1565
1566      proguard(_proguard_target_name) {
1567        forward_variables_from(invoker,
1568                               TESTONLY_AND_VISIBILITY + [
1569                                     "add_view_trace_events",
1570                                     "apply_mapping",
1571                                     "input_art_profile",
1572                                     "output_art_profile",
1573                                     "enable_startup_profile",
1574                                     "build_config",
1575                                     "custom_assertion_handler",
1576                                     "data",
1577                                     "data_deps",
1578                                     "deps",
1579                                     "enable_proguard_checks",
1580                                     "expected_proguard_config",
1581                                     "expected_proguard_config_base",
1582                                     "ignore_desugar_missing_deps",
1583                                     "inputs",
1584                                     "modules",
1585                                     "package_name",
1586                                     "proguard_configs",
1587                                     "proguard_enable_obfuscation",
1588                                     "proguard_mapping_path",
1589                                     "proguard_sourcefile_suffix",
1590                                     "repackage_classes",
1591                                     "top_target_name",
1592                                     "version_code",
1593                                   ])
1594        min_sdk_version = _min_sdk_version
1595
1596        args = []
1597        if (defined(invoker.has_apk_under_test) && invoker.has_apk_under_test) {
1598          args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:device_classpath_extended)" ]
1599        } else if (defined(invoker.add_view_trace_events) &&
1600                   invoker.add_view_trace_events && defined(invoker.modules)) {
1601          args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:trace_event_rewritten_device_classpath)" ]
1602        } else {
1603          args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:device_classpath)" ]
1604        }
1605        if (defined(invoker.proguard_args)) {
1606          args += invoker.proguard_args
1607        }
1608
1609        if (defined(invoker.output)) {
1610          output_path = invoker.output
1611        } else if (!defined(proguard_mapping_path)) {
1612          proguard_mapping_path = "$target_out_dir/$target_name.mapping"
1613        }
1614      }
1615    } else {  # !_proguard_enabled
1616      _is_library = defined(invoker.is_library) && invoker.is_library
1617      assert(!(defined(invoker.input_classes_filearg) && _is_library))
1618      assert(_is_library == defined(invoker.unprocessed_jar_path))
1619      _input_class_jars = []
1620      if (defined(invoker.input_class_jars)) {
1621        _input_class_jars = invoker.input_class_jars
1622      }
1623      _deps = invoker.deps
1624
1625      if (_input_class_jars != []) {
1626        _rebased_input_class_jars =
1627            rebase_path(_input_class_jars, root_build_dir)
1628      }
1629
1630      action_with_pydeps(target_name) {
1631        forward_variables_from(invoker,
1632                               TESTONLY_AND_VISIBILITY + [
1633                                     "data",
1634                                     "data_deps",
1635                                   ])
1636        script = "//build/android/gyp/dex.py"
1637        deps = _deps
1638        depfile = "$target_gen_dir/$target_name.d"
1639        outputs = [ invoker.output ]
1640        inputs = [
1641                   "$android_sdk/optional/android.test.base.jar",
1642                   "$android_sdk/optional/org.apache.http.legacy.jar",
1643                   "//third_party/jdk/current/bin/java",
1644                   _custom_d8_path,
1645                   _d8_path,
1646                   android_sdk_jar,
1647                 ] + java_paths_for_inputs
1648        if (defined(invoker.inputs)) {
1649          inputs += invoker.inputs
1650        }
1651
1652        if (!_is_library) {
1653          # http://crbug.com/725224. Fix for bots running out of memory.
1654          if (defined(java_cmd_pool_size)) {
1655            pool = "//build/config/android:java_cmd_pool($default_toolchain)"
1656          } else {
1657            pool = "//build/toolchain:link_pool($default_toolchain)"
1658          }
1659        }
1660
1661        args = [
1662          "--depfile",
1663          rebase_path(depfile, root_build_dir),
1664          "--output",
1665          rebase_path(invoker.output, root_build_dir),
1666          "--min-api=$_min_sdk_version",
1667          "--r8-jar-path",
1668          rebase_path(_d8_path, root_build_dir),
1669          "--custom-d8-jar-path",
1670          rebase_path(_custom_d8_path, root_build_dir),
1671
1672          # Uncomment when rebuilding custom_d8.jar.
1673          #"--skip-custom-d8",
1674        ]
1675        if (treat_warnings_as_errors) {
1676          args += [ "--warnings-as-errors" ]
1677        }
1678
1679        if (enable_incremental_d8 && !(defined(invoker.disable_incremental) &&
1680                                       invoker.disable_incremental)) {
1681          # Don't use incremental dexing for ProGuarded inputs as a precaution.
1682          args += [
1683            "--incremental-dir",
1684            rebase_path("$target_out_dir/$target_name", root_build_dir),
1685          ]
1686        }
1687        if (_is_library) {
1688          args += [ "--library" ]
1689        }
1690        if (defined(invoker.input_dex_filearg)) {
1691          inputs += [ invoker.build_config ]
1692          args += [ "--dex-inputs-filearg=${invoker.input_dex_filearg}" ]
1693        }
1694        if (defined(invoker.input_classes_filearg)) {
1695          inputs += [ invoker.build_config ]
1696          args += [ "--class-inputs-filearg=${invoker.input_classes_filearg}" ]
1697
1698          # Required for the same reason as unprocessed_jar_path is added to
1699          # classpath (see note below).
1700          args += [ "--classpath=${invoker.input_classes_filearg}" ]
1701        }
1702        if (_input_class_jars != []) {
1703          inputs += _input_class_jars
1704          args += [ "--class-inputs=${_rebased_input_class_jars}" ]
1705        }
1706
1707        # Never compile intemediates with --release in order to:
1708        # 1) not require recompiles when toggling is_java_debug,
1709        # 2) allow incremental_install=1 to still have local variable
1710        #    information even when is_java_debug=false.
1711        if (!is_java_debug && !_is_library) {
1712          args += [ "--release" ]
1713        }
1714
1715        if (_enable_desugar) {
1716          args += [ "--desugar" ]
1717
1718          _ignore_desugar_missing_deps =
1719              defined(invoker.ignore_desugar_missing_deps) &&
1720              invoker.ignore_desugar_missing_deps
1721          if (!_ignore_desugar_missing_deps) {
1722            args += [ "--show-desugar-default-interface-warnings" ]
1723          }
1724        }
1725        if (_desugar_needs_classpath) {
1726          # Cannot use header jar for the active jar, because it does not
1727          # contain anonymous classes. https://crbug.com/1342018#c5
1728          # Cannot use processed .jar here because it might have classes
1729          # filtered out via jar_excluded_patterns.
1730          # Must come first in classpath in order to take precedence over
1731          # deps that defined the same classes (via jar_excluded_patterns).
1732          if (defined(invoker.unprocessed_jar_path)) {
1733            args += [
1734              "--classpath",
1735              rebase_path(invoker.unprocessed_jar_path, root_build_dir),
1736
1737              # Pass the full classpath to find new dependencies that are not in
1738              # the .desugardeps file.
1739              "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)",
1740            ]
1741            inputs += [ invoker.unprocessed_jar_path ]
1742          }
1743          _desugar_dependencies_path =
1744              "$target_gen_dir/$target_name.desugardeps"
1745          args += [
1746            "--desugar-dependencies",
1747            rebase_path(_desugar_dependencies_path, root_build_dir),
1748            "--bootclasspath=@FileArg($_rebased_build_config:android:sdk_jars)",
1749          ]
1750        }
1751
1752        if (defined(invoker.custom_assertion_handler)) {
1753          args += [
1754            "--assertion-handler",
1755            invoker.custom_assertion_handler,
1756          ]
1757        } else if (enable_java_asserts) {
1758          # The default for generating dex file format is
1759          # --force-disable-assertions.
1760          args += [ "--force-enable-assertions" ]
1761        }
1762      }
1763    }
1764  }
1765
1766  template("jacoco_instr") {
1767    action_with_pydeps(target_name) {
1768      forward_variables_from(invoker,
1769                             TESTONLY_AND_VISIBILITY + [
1770                                   "deps",
1771                                   "public_deps",
1772                                 ])
1773
1774      # The name needs to match the SOURCES_JSON_FILES_SUFFIX in
1775      # generate_coverage_metadata_for_java.py.
1776      _sources_json_file = "$target_out_dir/${target_name}__jacoco_sources.json"
1777      _jacococli_jar = "//third_party/jacoco/lib/jacococli.jar"
1778
1779      script = "//build/android/gyp/jacoco_instr.py"
1780      inputs = invoker.source_files + java_paths_for_inputs + [
1781                 _jacococli_jar,
1782                 invoker.input_jar_path,
1783               ]
1784      outputs = [
1785        _sources_json_file,
1786        invoker.output_jar_path,
1787      ]
1788      args = [
1789        "--input-path",
1790        rebase_path(invoker.input_jar_path, root_build_dir),
1791        "--output-path",
1792        rebase_path(invoker.output_jar_path, root_build_dir),
1793        "--sources-json-file",
1794        rebase_path(_sources_json_file, root_build_dir),
1795        "--target-sources-file",
1796        rebase_path(invoker.target_sources_file, root_build_dir),
1797        "--jacococli-jar",
1798        rebase_path(_jacococli_jar, root_build_dir),
1799      ]
1800      if (coverage_instrumentation_input_file != "") {
1801        args += [
1802          "--files-to-instrument",
1803          rebase_path(coverage_instrumentation_input_file, root_build_dir),
1804        ]
1805      }
1806    }
1807  }
1808
1809  template("filter_jar") {
1810    action_with_pydeps(target_name) {
1811      script = "//build/android/gyp/filter_zip.py"
1812      forward_variables_from(invoker,
1813                             TESTONLY_AND_VISIBILITY + [
1814                                   "deps",
1815                                   "data",
1816                                   "data_deps",
1817                                 ])
1818      inputs = [ invoker.input_jar ]
1819      if (defined(invoker.inputs)) {
1820        inputs += invoker.inputs
1821      }
1822      outputs = [ invoker.output_jar ]
1823
1824      _jar_excluded_patterns = []
1825      if (defined(invoker.jar_excluded_patterns)) {
1826        _jar_excluded_patterns = invoker.jar_excluded_patterns
1827      }
1828      _jar_included_patterns = []
1829      if (defined(invoker.jar_included_patterns)) {
1830        _jar_included_patterns = invoker.jar_included_patterns
1831      }
1832      args = [
1833        "--input",
1834        rebase_path(invoker.input_jar, root_build_dir),
1835        "--output",
1836        rebase_path(invoker.output_jar, root_build_dir),
1837        "--exclude-globs=${_jar_excluded_patterns}",
1838        "--include-globs=${_jar_included_patterns}",
1839      ]
1840    }
1841  }
1842
1843  template("process_java_library") {
1844    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1845
1846    _previous_output_jar = invoker.input_jar_path
1847
1848    if (invoker.jacoco_instrument) {
1849      _filter_jar_target_name = "${target_name}__filter_jar"
1850      _filter_jar_output_jar = "$target_out_dir/$target_name.filter.jar"
1851    } else {
1852      _filter_jar_target_name = target_name
1853      _filter_jar_output_jar = invoker.output_jar_path
1854    }
1855
1856    filter_jar(_filter_jar_target_name) {
1857      forward_variables_from(invoker,
1858                             [
1859                               "data",
1860                               "data_deps",
1861                               "jar_excluded_patterns",
1862                               "jar_included_patterns",
1863                             ])
1864      deps = invoker.deps
1865      input_jar = _previous_output_jar
1866      output_jar = _filter_jar_output_jar
1867    }
1868
1869    if (invoker.jacoco_instrument) {
1870      # Jacoco must run after desugar (or else desugar sometimes fails).
1871      # It must run after filtering to avoid the same (filtered) class mapping
1872      # to multiple .jar files.
1873      # We run offline code coverage processing here rather than with a
1874      # javaagent as the desired coverage data was not being generated.
1875      # See crbug.com/1097815.
1876      jacoco_instr(target_name) {
1877        deps = [ ":$_filter_jar_target_name" ] + invoker.deps
1878        forward_variables_from(invoker,
1879                               [
1880                                 "source_files",
1881                                 "target_sources_file",
1882                               ])
1883
1884        input_jar_path = _filter_jar_output_jar
1885        output_jar_path = invoker.output_jar_path
1886      }
1887    }
1888  }
1889
1890  template("bytecode_processor") {
1891    action_with_pydeps(target_name) {
1892      forward_variables_from(invoker,
1893                             TESTONLY_AND_VISIBILITY + [
1894                                   "data_deps",
1895                                   "deps",
1896                                 ])
1897      script = "//build/android/gyp/bytecode_processor.py"
1898      inputs = java_paths_for_inputs + [
1899                 invoker.build_config,
1900                 invoker.input_jar,
1901               ]
1902      outputs = [ "$target_out_dir/$target_name.bytecode.stamp" ]
1903      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
1904      args = [
1905        "--target-name",
1906        get_label_info(":${target_name}", "label_no_toolchain"),
1907        "--gn-target=${invoker.target_label}",
1908        "--input-jar",
1909        rebase_path(invoker.input_jar, root_build_dir),
1910        "--stamp",
1911        rebase_path(outputs[0], root_build_dir),
1912        "--chromium-output-dir",
1913        rebase_path(root_build_dir, root_build_dir),
1914        "--direct-classpath-jars=@FileArg($_rebased_build_config:javac:classpath)",
1915        "--full-classpath-jars=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
1916        "--full-classpath-gn-targets=@FileArg($_rebased_build_config:deps_info:javac_full_classpath_targets)",
1917      ]
1918      if (auto_add_missing_java_deps) {
1919        args += [ "--auto-add-deps" ]
1920      }
1921      if (android_static_analysis == "build_server") {
1922        args += [ "--use-build-server" ]
1923      }
1924      if (invoker.include_android_sdk) {
1925        args += [ "--sdk-classpath-jars=@FileArg($_rebased_build_config:android:sdk_jars)" ]
1926      }
1927      if (treat_warnings_as_errors) {
1928        args += [ "--warnings-as-errors" ]
1929      }
1930    }
1931  }
1932
1933  template("merge_manifests") {
1934    action_with_pydeps(target_name) {
1935      assert(
1936          invoker.min_sdk_version >= min_supported_sdk_version,
1937          get_label_info(":$target_name", "label_no_toolchain") + " has an unsupported min_sdk_version of ${invoker.min_sdk_version} (min is $min_supported_sdk_version)")
1938      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
1939      script = "//build/android/gyp/merge_manifest.py"
1940      depfile = "$target_gen_dir/$target_name.d"
1941
1942      inputs = java_paths_for_inputs + [
1943                 invoker.build_config,
1944                 invoker.input_manifest,
1945                 _manifest_merger_jar_path,
1946               ]
1947
1948      outputs = [ invoker.output_manifest ]
1949      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
1950
1951      args = [
1952        "--depfile",
1953        rebase_path(depfile, root_build_dir),
1954        "--manifest-merger-jar",
1955        rebase_path(_manifest_merger_jar_path, root_build_dir),
1956        "--root-manifest",
1957        rebase_path(invoker.input_manifest, root_build_dir),
1958        "--output",
1959        rebase_path(invoker.output_manifest, root_build_dir),
1960        "--extras",
1961        "@FileArg($_rebased_build_config:extra_android_manifests)",
1962        "--min-sdk-version=${invoker.min_sdk_version}",
1963        "--target-sdk-version=${invoker.target_sdk_version}",
1964      ]
1965
1966      if (defined(invoker.manifest_package)) {
1967        args += [ "--manifest-package=${invoker.manifest_package}" ]
1968      }
1969
1970      if (defined(invoker.max_sdk_version)) {
1971        args += [ "--max-sdk-version=${invoker.max_sdk_version}" ]
1972      }
1973
1974      if (treat_warnings_as_errors) {
1975        args += [ "--warnings-as-errors" ]
1976      }
1977    }
1978  }
1979
1980  # This template is used to parse a set of resource directories and
1981  # create the R.txt, .srcjar and .resources.zip for it.
1982  #
1983  # Input variables:
1984  #   deps: Specifies the input dependencies for this target.
1985  #
1986  #   build_config: Path to the .build_config.json file corresponding to the target.
1987  #
1988  #   sources:
1989  #     List of input resource files.
1990  #
1991  #   custom_package: (optional)
1992  #     Package name for the generated R.java source file. Optional if
1993  #     android_manifest is not provided.
1994  #
1995  #   android_manifest: (optional)
1996  #     If custom_package is not provided, path to an AndroidManifest.xml file
1997  #     that is only used to extract a package name out of it.
1998  #
1999  #   r_text_in_path: (optional)
2000  #     Path to an input R.txt file to use to generate the R.java file.
2001  #     The default is to use 'aapt' to generate the file from the content
2002  #     of the resource directories.
2003  #
2004  # Output variables:
2005  #   resources_zip:
2006  #     Path to a .resources.zip that will simply contain all the
2007  #     input resources, collected in a single archive.
2008  #
2009  #   r_text_out_path: Path for the generated R.txt file.
2010  #
2011  template("prepare_resources") {
2012    action_with_pydeps(target_name) {
2013      forward_variables_from(invoker,
2014                             TESTONLY_AND_VISIBILITY + [
2015                                   "deps",
2016                                   "public_deps",
2017                                   "sources",
2018                                 ])
2019      script = "//build/android/gyp/prepare_resources.py"
2020
2021      depfile = "$target_gen_dir/${invoker.target_name}.d"
2022      outputs = [
2023        invoker.resources_zip,
2024        invoker.resources_zip + ".info",
2025        invoker.r_text_out_path,
2026      ]
2027
2028      inputs = [ invoker.res_sources_path ]
2029
2030      _rebased_res_sources_path =
2031          rebase_path(invoker.res_sources_path, root_build_dir)
2032
2033      args = [
2034        "--depfile",
2035        rebase_path(depfile, root_build_dir),
2036        "--res-sources-path=$_rebased_res_sources_path",
2037        "--resource-zip-out",
2038        rebase_path(invoker.resources_zip, root_build_dir),
2039        "--r-text-out",
2040        rebase_path(invoker.r_text_out_path, root_build_dir),
2041      ]
2042
2043      if (defined(invoker.r_text_in_path)) {
2044        _r_text_in_path = invoker.r_text_in_path
2045        inputs += [ _r_text_in_path ]
2046        args += [
2047          "--r-text-in",
2048          rebase_path(_r_text_in_path, root_build_dir),
2049        ]
2050      }
2051
2052      if (defined(invoker.strip_drawables) && invoker.strip_drawables) {
2053        args += [ "--strip-drawables" ]
2054      }
2055      if (defined(invoker.allow_missing_resources) &&
2056          invoker.allow_missing_resources) {
2057        args += [ "--allow-missing-resources" ]
2058      }
2059    }
2060  }
2061
2062  # A template that is used to compile all resources needed by a binary
2063  # (e.g. an android_apk or a robolectric_binary) into an intermediate .ar_
2064  # archive. It can also generate an associated .srcjar that contains the
2065  # final R.java sources for all resource packages the binary depends on.
2066  #
2067  # Input variables:
2068  #   android_sdk_dep: The sdk dep that these resources should compile against.
2069  #
2070  #   deps: Specifies the input dependencies for this target.
2071  #
2072  #   build_config: Path to the .build_config.json file corresponding to the target.
2073  #
2074  #   build_config_dep: Dep target to generate the .build_config.json file.
2075  #
2076  #   android_manifest: Path to root manifest for the binary.
2077  #
2078  #   version_code: (optional)
2079  #
2080  #   version_name: (optional)
2081  #
2082  #   shared_resources: (optional)
2083  #     If true, make all variables in each generated R.java file non-final,
2084  #     and provide an onResourcesLoaded() method that can be used to reset
2085  #     their package index at load time. Useful when the APK corresponds to
2086  #     a library that is loaded at runtime, like system_webview_apk or
2087  #     monochrome_apk.
2088  #
2089  #   app_as_shared_lib: (optional)
2090  #     If true, same effect as shared_resources, but also ensures that the
2091  #     resources can be used by the APK when it is loaded as a regular
2092  #     application as well. Useful for the monochrome_public_apk target
2093  #     which is both an application and a shared runtime library that
2094  #     implements the system webview feature.
2095  #
2096  #   shared_resources_allowlist: (optional)
2097  #     Path to an R.txt file. If provided, acts similar to shared_resources
2098  #     except that it restricts the list of non-final resource variables
2099  #     to the list from the input R.txt file. Overrides shared_resources
2100  #     when both are specified.
2101  #
2102  #   shared_resources_allowlist_locales: (optional)
2103  #     If shared_resources_allowlist is used, provide an optional list of
2104  #     Chromium locale names to determine which localized shared string
2105  #     resources to put in the final output, even if aapt_locale_allowlist
2106  #     is defined to a smaller subset.
2107  #
2108  #   aapt_locale_allowlist: (optional)
2109  #     Restrict compiled locale-dependent resources to a specific allowlist.
2110  #     NOTE: This is a list of Chromium locale names, not Android ones.
2111  #
2112  #   r_java_root_package_name: (optional)
2113  #     Short package name for this target's root R java file (ex. input of
2114  #     "base" would become "gen.base_module" for the root R java package name).
2115  #     Optional as defaults to "base".
2116  #
2117  #   resource_exclusion_regex: (optional)
2118  #
2119  #   resource_exclusion_exceptions: (optional)
2120  #
2121  #   resource_values_filter_rules: (optional)
2122  #
2123  #   png_to_webp: (optional)
2124  #     If true, convert all PNG resources (except 9-patch files) to WebP.
2125  #
2126  #   post_process_script: (optional)
2127  #
2128  #   package_name: (optional)
2129  #     Name of the package for the purpose of creating R class.
2130  #
2131  #   package_id: (optional)
2132  #     Use a custom package ID in resource IDs.
2133  #
2134  #   arsc_package_name: (optional)
2135  #     Use this package name in the arsc file rather than the package name
2136  #     found in the AndroidManifest.xml. Does not affect the package name
2137  #     used in AndroidManifest.xml.
2138  #
2139  #   resource_ids_provider_dep: (optional)
2140  #     Use resource IDs provided by another APK target when compiling resources
2141  #     (via. "aapt2 link --stable-ids")
2142  #
2143  #   override_target_sdk: (optional)
2144  #     Update the manifest to target this SDK
2145  #
2146  # Output variables:
2147  #   arsc_output: Path to output .ap_ file (optional).
2148  #
2149  #   proto_output: Path to output .proto.ap_ file (optional).
2150  #
2151  #   r_text_out_path: (optional):
2152  #       Path for the corresponding generated R.txt file.
2153  #
2154  #   proguard_file: (optional)
2155  #       Path to proguard configuration file for this apk target.
2156  #
2157  template("compile_resources") {
2158    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
2159
2160    _deps = invoker.deps + [ invoker.build_config_dep ]
2161    if (defined(invoker.android_manifest_dep)) {
2162      _deps += [ invoker.android_manifest_dep ]
2163    }
2164
2165    if (defined(invoker.arsc_output)) {
2166      _arsc_output = invoker.arsc_output
2167    }
2168    _final_srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
2169
2170    _script = "//build/android/gyp/compile_resources.py"
2171
2172    _target_sdk_version = invoker.target_sdk_version
2173    if (defined(invoker.override_target_sdk)) {
2174      _target_sdk_version = invoker.override_target_sdk
2175    }
2176
2177    _common_inputs = [
2178      invoker.build_config,
2179      android_sdk_tools_bundle_aapt2,
2180      android_sdk_jar,
2181
2182      # TODO(b/315080809#comment4): remove these files after fixing
2183      # build/print_python_deps.py.
2184      "//third_party/protobuf/python/google/__init__.py",
2185      "//third_party/protobuf/python/google/protobuf/__init__.py",
2186      "//third_party/protobuf/python/google/protobuf/compiler/__init__.py",
2187      "//third_party/protobuf/python/google/protobuf/compiler/plugin_pb2.py",
2188      "//third_party/protobuf/python/google/protobuf/descriptor.py",
2189      "//third_party/protobuf/python/google/protobuf/descriptor_database.py",
2190      "//third_party/protobuf/python/google/protobuf/descriptor_pb2.py",
2191      "//third_party/protobuf/python/google/protobuf/descriptor_pool.py",
2192      "//third_party/protobuf/python/google/protobuf/internal/__init__.py",
2193      "//third_party/protobuf/python/google/protobuf/internal/_parameterized.py",
2194      "//third_party/protobuf/python/google/protobuf/internal/api_implementation.py",
2195      "//third_party/protobuf/python/google/protobuf/internal/builder.py",
2196      "//third_party/protobuf/python/google/protobuf/internal/containers.py",
2197      "//third_party/protobuf/python/google/protobuf/internal/decoder.py",
2198      "//third_party/protobuf/python/google/protobuf/internal/descriptor_database_test.py",
2199      "//third_party/protobuf/python/google/protobuf/internal/descriptor_pool_test.py",
2200      "//third_party/protobuf/python/google/protobuf/internal/descriptor_test.py",
2201      "//third_party/protobuf/python/google/protobuf/internal/encoder.py",
2202      "//third_party/protobuf/python/google/protobuf/internal/enum_type_wrapper.py",
2203      "//third_party/protobuf/python/google/protobuf/internal/extension_dict.py",
2204      "//third_party/protobuf/python/google/protobuf/internal/generator_test.py",
2205      "//third_party/protobuf/python/google/protobuf/internal/import_test.py",
2206      "//third_party/protobuf/python/google/protobuf/internal/import_test_package/__init__.py",
2207      "//third_party/protobuf/python/google/protobuf/internal/json_format_test.py",
2208      "//third_party/protobuf/python/google/protobuf/internal/keywords_test.py",
2209      "//third_party/protobuf/python/google/protobuf/internal/message_factory_test.py",
2210      "//third_party/protobuf/python/google/protobuf/internal/message_listener.py",
2211      "//third_party/protobuf/python/google/protobuf/internal/message_test.py",
2212      "//third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py",
2213      "//third_party/protobuf/python/google/protobuf/internal/python_message.py",
2214      "//third_party/protobuf/python/google/protobuf/internal/reflection_test.py",
2215      "//third_party/protobuf/python/google/protobuf/internal/service_reflection_test.py",
2216      "//third_party/protobuf/python/google/protobuf/internal/symbol_database_test.py",
2217      "//third_party/protobuf/python/google/protobuf/internal/test_util.py",
2218      "//third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py",
2219      "//third_party/protobuf/python/google/protobuf/internal/text_encoding_test.py",
2220      "//third_party/protobuf/python/google/protobuf/internal/text_format_test.py",
2221      "//third_party/protobuf/python/google/protobuf/internal/type_checkers.py",
2222      "//third_party/protobuf/python/google/protobuf/internal/unknown_fields_test.py",
2223      "//third_party/protobuf/python/google/protobuf/internal/well_known_types.py",
2224      "//third_party/protobuf/python/google/protobuf/internal/well_known_types_test.py",
2225      "//third_party/protobuf/python/google/protobuf/internal/wire_format.py",
2226      "//third_party/protobuf/python/google/protobuf/internal/wire_format_test.py",
2227      "//third_party/protobuf/python/google/protobuf/json_format.py",
2228      "//third_party/protobuf/python/google/protobuf/message.py",
2229      "//third_party/protobuf/python/google/protobuf/message_factory.py",
2230      "//third_party/protobuf/python/google/protobuf/proto_builder.py",
2231      "//third_party/protobuf/python/google/protobuf/pyext/__init__.py",
2232      "//third_party/protobuf/python/google/protobuf/pyext/cpp_message.py",
2233      "//third_party/protobuf/python/google/protobuf/reflection.py",
2234      "//third_party/protobuf/python/google/protobuf/service.py",
2235      "//third_party/protobuf/python/google/protobuf/service_reflection.py",
2236      "//third_party/protobuf/python/google/protobuf/symbol_database.py",
2237      "//third_party/protobuf/python/google/protobuf/text_encoding.py",
2238      "//third_party/protobuf/python/google/protobuf/text_format.py",
2239      "//third_party/protobuf/python/google/protobuf/unknown_fields.py",
2240      "//third_party/protobuf/python/google/protobuf/util/__init__.py",
2241    ]
2242
2243    _inputs = _common_inputs
2244
2245    _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
2246
2247    _rebased_android_sdk_jar = rebase_path(android_sdk_jar, root_build_dir)
2248    _args = [
2249      "--include-resources=$_rebased_android_sdk_jar",
2250      "--aapt2-path",
2251      rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
2252      "--dependencies-res-zips=@FileArg($_rebased_build_config:deps_info:dependency_zips)",
2253      "--extra-res-packages=@FileArg($_rebased_build_config:deps_info:extra_package_names)",
2254      "--min-sdk-version=${invoker.min_sdk_version}",
2255      "--target-sdk-version=${_target_sdk_version}",
2256      "--webp-cache-dir=obj/android-webp-cache",
2257    ]
2258
2259    _inputs += [ invoker.android_manifest ]
2260    _outputs = [ _final_srcjar_path ]
2261    _args += [
2262      "--android-manifest",
2263      rebase_path(invoker.android_manifest, root_build_dir),
2264      "--srcjar-out",
2265      rebase_path(_final_srcjar_path, root_build_dir),
2266    ]
2267    if (defined(invoker.version_code)) {
2268      _args += [
2269        "--version-code",
2270        invoker.version_code,
2271      ]
2272    }
2273    if (defined(invoker.version_name)) {
2274      _args += [
2275        "--version-name",
2276        invoker.version_name,
2277      ]
2278    }
2279    if (defined(_arsc_output)) {
2280      _outputs += [ _arsc_output ]
2281      _args += [
2282        "--arsc-path",
2283        rebase_path(_arsc_output, root_build_dir),
2284      ]
2285    }
2286    if (defined(invoker.proto_output)) {
2287      _outputs += [ invoker.proto_output ]
2288      _args += [
2289        "--proto-path",
2290        rebase_path(invoker.proto_output, root_build_dir),
2291      ]
2292    }
2293    if (defined(invoker.size_info_path)) {
2294      _outputs += [ invoker.size_info_path ]
2295      _args += [
2296        "--info-path",
2297        rebase_path(invoker.size_info_path, root_build_dir),
2298      ]
2299    }
2300
2301    if (defined(invoker.r_java_root_package_name)) {
2302      _args += [
2303        "--r-java-root-package-name",
2304        invoker.r_java_root_package_name,
2305      ]
2306    }
2307
2308    # Useful to have android:debuggable in the manifest even for Release
2309    # builds. Just omit it for officai
2310    if (debuggable_apks) {
2311      _args += [ "--debuggable" ]
2312    }
2313
2314    if (defined(invoker.r_text_out_path)) {
2315      _outputs += [ invoker.r_text_out_path ]
2316      _args += [
2317        "--r-text-out",
2318        rebase_path(invoker.r_text_out_path, root_build_dir),
2319      ]
2320    }
2321
2322    if (defined(invoker.rename_manifest_package)) {
2323      _args += [
2324        "--rename-manifest-package",
2325        invoker.rename_manifest_package,
2326      ]
2327    }
2328
2329    # Define the flags related to shared resources.
2330    #
2331    # Note the small sanity check to ensure that the package ID of the
2332    # generated resources table is correct. It should be 0x02 for runtime
2333    # shared libraries, and 0x7f otherwise.
2334
2335    if (defined(invoker.shared_resources) && invoker.shared_resources) {
2336      _args += [ "--shared-resources" ]
2337    }
2338    if (defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib) {
2339      _args += [ "--app-as-shared-lib" ]
2340    }
2341    if (defined(invoker.package_id)) {
2342      _args += [ "--package-id=${invoker.package_id}" ]
2343    }
2344    if (defined(invoker.package_name)) {
2345      _args += [
2346        "--package-name",
2347        invoker.package_name,
2348      ]
2349    }
2350    if (defined(invoker.arsc_package_name)) {
2351      _args += [
2352        "--arsc-package-name",
2353        invoker.arsc_package_name,
2354      ]
2355    }
2356
2357    if (defined(invoker.shared_resources_allowlist)) {
2358      _inputs += [ invoker.shared_resources_allowlist ]
2359      _args += [
2360        "--shared-resources-allowlist",
2361        rebase_path(invoker.shared_resources_allowlist, root_build_dir),
2362      ]
2363    }
2364    if (defined(invoker.shared_resources_allowlist_locales)) {
2365      _args += [ "--shared-resources-allowlist-locales=" +
2366                 "${invoker.shared_resources_allowlist_locales}" ]
2367    }
2368
2369    if (!defined(testonly) || !testonly ||
2370        (defined(invoker.enforce_resource_overlays_in_tests) &&
2371         invoker.enforce_resource_overlays_in_tests)) {
2372      _args += [ "--dependencies-res-zip-overlays=@FileArg($_rebased_build_config:deps_info:dependency_zip_overlays)" ]
2373    } else {
2374      _args += [ "--dependencies-res-zip-overlays=@FileArg($_rebased_build_config:deps_info:dependency_zips)" ]
2375    }
2376
2377    if (defined(invoker.proguard_file)) {
2378      _outputs += [ invoker.proguard_file ]
2379      _args += [
2380        "--proguard-file",
2381        rebase_path(invoker.proguard_file, root_build_dir),
2382      ]
2383    }
2384
2385    if (defined(invoker.aapt_locale_allowlist)) {
2386      _args += [ "--locale-allowlist=${invoker.aapt_locale_allowlist}" ]
2387    }
2388    if (defined(invoker.png_to_webp) && invoker.png_to_webp) {
2389      _webp_target = "//third_party/libwebp:cwebp($host_toolchain)"
2390      _webp_binary = get_label_info(_webp_target, "root_out_dir") + "/cwebp"
2391      _deps += [ _webp_target ]
2392      _inputs += [ _webp_binary ]
2393      _args += [
2394        "--png-to-webp",
2395        "--webp-binary",
2396        rebase_path(_webp_binary, root_build_dir),
2397      ]
2398    }
2399    if (defined(invoker.resource_exclusion_regex)) {
2400      _args +=
2401          [ "--resource-exclusion-regex=${invoker.resource_exclusion_regex}" ]
2402      if (defined(invoker.resource_exclusion_exceptions)) {
2403        _args += [ "--resource-exclusion-exceptions=${invoker.resource_exclusion_exceptions}" ]
2404      }
2405    }
2406    if (defined(invoker.resource_values_filter_rules)) {
2407      _args +=
2408          [ "--values-filter-rules=${invoker.resource_values_filter_rules}" ]
2409    }
2410
2411    if (defined(invoker.include_resource)) {
2412      _inputs += [ invoker.include_resource ]
2413      _rebased_include_resources =
2414          rebase_path(invoker.include_resource, root_build_dir)
2415      _args += [ "--include-resources=$_rebased_include_resources" ]
2416    }
2417
2418    if (defined(invoker._args)) {
2419      _args += invoker._args
2420    }
2421
2422    if (defined(invoker.emit_ids_out_path)) {
2423      _outputs += [ invoker.emit_ids_out_path ]
2424      _rebased_emit_ids_path =
2425          rebase_path(invoker.emit_ids_out_path, root_out_dir)
2426      _args += [ "--emit-ids-out=$_rebased_emit_ids_path" ]
2427    }
2428
2429    if (defined(invoker.resource_ids_provider_dep)) {
2430      _compile_res_dep =
2431          "${invoker.resource_ids_provider_dep}__compile_resources"
2432      _gen_dir = get_label_info(_compile_res_dep, "target_gen_dir")
2433      _name = get_label_info(_compile_res_dep, "name")
2434      _resource_ids_path = "$_gen_dir/$_name.resource_ids"
2435      _inputs += [ _resource_ids_path ]
2436      _rebased_ids_path = rebase_path(_resource_ids_path, root_out_dir)
2437      _args += [ "--use-resource-ids-path=$_rebased_ids_path" ]
2438      _deps += [ _compile_res_dep ]
2439    }
2440
2441    if (defined(invoker.max_sdk_version)) {
2442      _max_sdk_version = invoker.max_sdk_version
2443      _args += [ "--max-sdk-version=$_max_sdk_version" ]
2444    }
2445
2446    if (defined(invoker.manifest_package)) {
2447      _args += [ "--manifest-package=${invoker.manifest_package}" ]
2448    }
2449
2450    if (defined(invoker.is_bundle_module) && invoker.is_bundle_module) {
2451      _args += [ "--is-bundle-module" ]
2452    }
2453
2454    if (defined(invoker.uses_split)) {
2455      assert(invoker.is_bundle_module)
2456      _args += [ "--uses-split=${invoker.uses_split}" ]
2457    }
2458
2459    if (defined(invoker.expected_android_manifest)) {
2460      _expectations_target =
2461          "${invoker.top_target_name}_validate_android_manifest"
2462      action_with_pydeps(_expectations_target) {
2463        _actual_file = "${invoker.android_manifest}.normalized"
2464        _failure_file =
2465            "$expectations_failure_dir/" +
2466            string_replace(invoker.expected_android_manifest, "/", "_")
2467        inputs = [
2468                   invoker.android_manifest,
2469                   invoker.expected_android_manifest,
2470                 ] + _common_inputs
2471        outputs = [
2472          _actual_file,
2473          _failure_file,
2474        ]
2475        deps = [
2476          invoker.android_manifest_dep,
2477          invoker.build_config_dep,
2478        ]
2479        script = _script
2480        args = _args + [
2481                 "--expected-file",
2482                 rebase_path(invoker.expected_android_manifest, root_build_dir),
2483                 "--actual-file",
2484                 rebase_path(_actual_file, root_build_dir),
2485                 "--failure-file",
2486                 rebase_path(_failure_file, root_build_dir),
2487                 "--only-verify-expectations",
2488               ]
2489        if (defined(invoker.expected_android_manifest_base)) {
2490          args += [
2491            "--expected-file-base",
2492            rebase_path(invoker.expected_android_manifest_base, root_build_dir),
2493          ]
2494          inputs += [ invoker.expected_android_manifest_base ]
2495        }
2496        if (defined(invoker.expected_android_manifest_version_code_offset)) {
2497          args += [
2498            "--verification-version-code-offset",
2499            invoker.expected_android_manifest_version_code_offset,
2500          ]
2501        }
2502        if (defined(invoker.expected_android_manifest_library_version_offset)) {
2503          args += [
2504            "--verification-library-version-offset",
2505            invoker.expected_android_manifest_library_version_offset,
2506          ]
2507        }
2508        if (fail_on_android_expectations) {
2509          args += [ "--fail-on-expectations" ]
2510        }
2511      }
2512      _deps += [ ":$_expectations_target" ]
2513    }
2514
2515    action_with_pydeps(target_name) {
2516      script = _script
2517      depfile = "$target_gen_dir/${target_name}.d"
2518      inputs = _inputs
2519      outputs = _outputs
2520      deps = _deps
2521      args = _args + [
2522               "--depfile",
2523               rebase_path(depfile, root_build_dir),
2524             ]
2525    }
2526  }
2527
2528  # A template that is used to optimize compiled resources using aapt2 optimize.
2529  #
2530  #   proto_input_path:
2531  #     Path to input compiled .proto.ap_ file.
2532  #
2533  #   short_resource_paths: (optional)
2534  #     Rename the paths within a the apk to be randomly generated short
2535  #     strings to reduce binary size.
2536  #
2537  #   strip_resource_names: (optional)
2538  #     Strip resource names from the resources table of the apk.
2539  #
2540  #   resources_configs_paths: (optional)
2541  #     List of resource configs to use for optimization.
2542  #
2543  #   optimized_proto_output:
2544  #     Path to output optimized .proto.ap_ file.
2545  #
2546  #   resources_path_map_out_path: (optional):
2547  #       Path for the generated map between original resource paths and
2548  #       shortened resource paths.
2549  template("optimize_resources") {
2550    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
2551    action_with_pydeps(target_name) {
2552      forward_variables_from(invoker, [ "deps" ])
2553      script = "//build/android/gyp/optimize_resources.py"
2554      outputs = [ invoker.optimized_proto_output ]
2555      inputs = [
2556        android_sdk_tools_bundle_aapt2,
2557        invoker.r_text_path,
2558        invoker.proto_input_path,
2559      ]
2560      args = [
2561        "--aapt2-path",
2562        rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
2563        "--r-text-in",
2564        rebase_path(invoker.r_text_path, root_build_dir),
2565        "--proto-path",
2566        rebase_path(invoker.proto_input_path, root_build_dir),
2567        "--optimized-proto-path",
2568        rebase_path(invoker.optimized_proto_output, root_build_dir),
2569      ]
2570
2571      if (defined(invoker.resources_config_paths)) {
2572        inputs += invoker.resources_config_paths
2573        _rebased_resource_configs =
2574            rebase_path(invoker.resources_config_paths, root_build_dir)
2575        args += [ "--resources-config-paths=${_rebased_resource_configs}" ]
2576      }
2577
2578      if (defined(invoker.short_resource_paths) &&
2579          invoker.short_resource_paths) {
2580        args += [ "--short-resource-paths" ]
2581        if (defined(invoker.resources_path_map_out_path)) {
2582          outputs += [ invoker.resources_path_map_out_path ]
2583          args += [
2584            "--resources-path-map-out-path",
2585            rebase_path(invoker.resources_path_map_out_path, root_build_dir),
2586          ]
2587        }
2588      }
2589
2590      if (defined(invoker.strip_resource_names) &&
2591          invoker.strip_resource_names) {
2592        args += [ "--strip-resource-names" ]
2593      }
2594    }
2595  }
2596
2597  # A template that is used to find unused resources.
2598  template("unused_resources") {
2599    action_with_pydeps(target_name) {
2600      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
2601      script = "//build/android/gyp/unused_resources.py"
2602      depfile = "$target_gen_dir/${target_name}.d"
2603      _unused_resources_script = "$root_build_dir/bin/helper/unused_resources"
2604      inputs = [ _unused_resources_script ] + java_paths_for_inputs
2605      outputs = [
2606        invoker.output_config,
2607        invoker.output_r_txt,
2608      ]
2609      if (!defined(deps)) {
2610        deps = []
2611      }
2612      deps += [ "//build/android/unused_resources:unused_resources" ]
2613      _rebased_module_build_config =
2614          rebase_path(invoker.build_config, root_build_dir)
2615      args = [
2616        "--script",
2617        rebase_path(_unused_resources_script, root_build_dir),
2618        "--output-config",
2619        rebase_path(invoker.output_config, root_build_dir),
2620        "--r-text-in=@FileArg($_rebased_module_build_config:deps_info:r_text_path)",
2621        "--r-text-out",
2622        rebase_path(invoker.output_r_txt, root_build_dir),
2623        "--dependencies-res-zips=@FileArg($_rebased_module_build_config:deps_info:dependency_zips)",
2624        "--depfile",
2625        rebase_path(depfile, root_build_dir),
2626      ]
2627
2628      if (defined(invoker.proguard_mapping_path)) {
2629        inputs += [ invoker.proguard_mapping_path ]
2630        args += [
2631          "--proguard-mapping",
2632          rebase_path(invoker.proguard_mapping_path, root_build_dir),
2633        ]
2634      }
2635
2636      foreach(_build_config, invoker.all_module_build_configs) {
2637        inputs += [ _build_config ]
2638        _rebased_build_config = rebase_path(_build_config, root_build_dir)
2639        args += [
2640          "--dexes=@FileArg($_rebased_build_config:final_dex:path)",
2641          "--android-manifests=@FileArg($_rebased_build_config:deps_info:merged_android_manifest)",
2642        ]
2643      }
2644    }
2645  }
2646
2647  # Create an .jar.info file by merging several .jar.info files into one.
2648  #
2649  # Variables:
2650  #   build_config: Path to APK's build config file. Used to extract the
2651  #       list of input .jar files from its dependencies.
2652  #   name: Name of the apk or app bundle (e.g. "Foo.apk").
2653  #   res_size_info_path: Path to input .ap_.info file (for apks).
2654  #
2655  template("create_size_info_files") {
2656    action_with_pydeps(target_name) {
2657      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
2658      script = "//build/android/gyp/create_size_info_files.py"
2659      _jar_info_path = "$root_build_dir/size-info/${invoker.name}.jar.info"
2660      _pak_info_path = "$root_build_dir/size-info/${invoker.name}.pak.info"
2661      _res_info_path = "$root_build_dir/size-info/${invoker.name}.res.info"
2662      outputs = [
2663        _jar_info_path,
2664        _pak_info_path,
2665        _res_info_path,
2666      ]
2667      depfile = "$target_gen_dir/$target_name.d"
2668      args = [
2669        "--depfile",
2670        rebase_path(depfile, root_build_dir),
2671        "--jar-info-path",
2672        rebase_path(_jar_info_path, root_build_dir),
2673        "--pak-info-path",
2674        rebase_path(_pak_info_path, root_build_dir),
2675        "--res-info-path",
2676        rebase_path(_res_info_path, root_build_dir),
2677      ]
2678      _is_bundle = defined(invoker.module_build_configs)
2679      if (_is_bundle) {
2680        inputs = invoker.module_build_configs
2681        foreach(_build_config, invoker.module_build_configs) {
2682          _rebased_build_config = rebase_path(_build_config, root_build_dir)
2683          args += [
2684            "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)",
2685            "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
2686            "--in-res-info-path=@FileArg($_rebased_build_config:deps_info:res_size_info)",
2687            "--assets=@FileArg($_rebased_build_config:assets)",
2688            "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
2689          ]
2690        }
2691      } else {
2692        inputs = [
2693          invoker.build_config,
2694          invoker.res_size_info_path,
2695        ]
2696        _rebased_build_config =
2697            rebase_path(invoker.build_config, root_build_dir)
2698        args += [
2699          "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)",
2700          "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
2701          "--in-res-info-path",
2702          rebase_path(invoker.res_size_info_path, root_build_dir),
2703          "--assets=@FileArg($_rebased_build_config:assets)",
2704          "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
2705        ]
2706      }
2707    }
2708  }
2709
2710  template("create_binary_profile") {
2711    action_with_pydeps(target_name) {
2712      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
2713      forward_variables_from(invoker, [ "deps" ])
2714      script = "//build/android/gyp/binary_baseline_profile.py"
2715      depfile = "$target_gen_dir/$target_name.d"
2716      outputs = [
2717        invoker.binary_baseline_profile_path,
2718        invoker.binary_baseline_profile_metadata_path,
2719      ]
2720      _profgen_path =
2721          "$public_android_sdk_root/cmdline-tools/latest/bin/profgen"
2722      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
2723      inputs = [
2724        invoker.build_config,
2725        invoker.input_profile_path,
2726        _profgen_path,
2727      ]
2728      args = [
2729        "--profgen",
2730        rebase_path(_profgen_path, root_build_dir),
2731        "--output-profile",
2732        rebase_path(invoker.binary_baseline_profile_path, root_build_dir),
2733        "--output-metadata",
2734        rebase_path(invoker.binary_baseline_profile_metadata_path,
2735                    root_build_dir),
2736        "--dex=@FileArg($_rebased_build_config:final_dex:path)",
2737        "--input-profile-path",
2738        rebase_path(invoker.input_profile_path, root_build_dir),
2739        "--depfile",
2740        rebase_path(depfile, root_build_dir),
2741      ]
2742      if (defined(invoker.proguard_mapping_path)) {
2743        args += [
2744          "--proguard-mapping",
2745          rebase_path(invoker.proguard_mapping_path, root_build_dir),
2746        ]
2747        inputs += [ invoker.proguard_mapping_path ]
2748      }
2749    }
2750  }
2751
2752  # Creates a signed and aligned .apk.
2753  #
2754  # Variables
2755  #   apk_name: (optional) APK name (without .apk suffix). If provided, will
2756  #       be used to generate .info files later used by the supersize tool.
2757  #   assets_build_config: Path to android_apk .build_config.json containing merged
2758  #       asset information.
2759  #   deps: Specifies the dependencies of this target.
2760  #   dex_path: Path to classes.dex file to include (optional).
2761  #   expected_libs_and_assets: Verify the list of included native libraries
2762  #     and assets is consistent with the given expectation file.
2763  #   expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff
2764  #     with this file as the base.
2765  #   packaged_resources_path: Path to .ap_ to use.
2766  #   output_apk_path: Output path for the generated .apk.
2767  #   min_sdk_version: The minimum Android SDK version this target supports.
2768  #   native_lib_placeholders: List of placeholder filenames to add to the apk
2769  #     (optional).
2770  #   secondary_native_lib_placeholders: List of placeholder filenames to add to
2771  #     the apk for the secondary ABI (optional).
2772  #   loadable_modules: List of native libraries.
2773  #   native_libs_filearg: @FileArg() of additionally native libraries.
2774  #   secondary_abi_loadable_modules: (optional) List of native libraries for
2775  #     secondary ABI.
2776  #   secondary_abi_native_libs_filearg: (optional). @FileArg() of additional
2777  #     secondary ABI native libs.
2778  #   keystore_path: Path to keystore to use for signing.
2779  #   keystore_name: Key alias to use.
2780  #   keystore_password: Keystore password.
2781  template("package_apk") {
2782    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "public_deps" ])
2783    _is_robolectric_apk =
2784        defined(invoker.is_robolectric_apk) && invoker.is_robolectric_apk
2785    _deps = invoker.deps
2786    _native_lib_placeholders = []
2787    if (defined(invoker.native_lib_placeholders)) {
2788      _native_lib_placeholders = invoker.native_lib_placeholders
2789    }
2790    _secondary_native_lib_placeholders = []
2791    if (defined(invoker.secondary_native_lib_placeholders)) {
2792      _secondary_native_lib_placeholders =
2793          invoker.secondary_native_lib_placeholders
2794    }
2795
2796    _script = "//build/android/gyp/apkbuilder.py"
2797
2798    _inputs = [ invoker.packaged_resources_path ]
2799
2800    _outputs = [ invoker.output_apk_path ]
2801    _data = [ invoker.output_apk_path ]
2802
2803    _rebased_compiled_resources_path =
2804        rebase_path(invoker.packaged_resources_path, root_build_dir)
2805    _rebased_packaged_apk_path =
2806        rebase_path(invoker.output_apk_path, root_build_dir)
2807    _args = [
2808      "--resource-apk=$_rebased_compiled_resources_path",
2809      "--output-apk=$_rebased_packaged_apk_path",
2810      "--min-sdk-version=${invoker.min_sdk_version}",
2811    ]
2812
2813    # system_image_stub_apk does not use a build_config.json.
2814    if (defined(invoker.build_config)) {
2815      _inputs += [ invoker.build_config ]
2816      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
2817      _args += [
2818        "--assets=@FileArg($_rebased_build_config:assets)",
2819        "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
2820      ]
2821      if (!_is_robolectric_apk) {
2822        _args += [ "--java-resources=@FileArg($_rebased_build_config:java_resources_jars)" ]
2823      }
2824    }
2825    if (defined(invoker.extra_assets)) {
2826      _args += [ "--assets=${invoker.extra_assets}" ]
2827    }
2828    if (!_is_robolectric_apk) {
2829      _apksigner = "$android_sdk_build_tools/lib/apksigner.jar"
2830      _zipalign = "$android_sdk_build_tools/zipalign"
2831      _keystore_path = android_keystore_path
2832      _keystore_name = android_keystore_name
2833      _keystore_password = android_keystore_password
2834
2835      if (defined(invoker.keystore_path)) {
2836        _keystore_path = invoker.keystore_path
2837        _keystore_name = invoker.keystore_name
2838        _keystore_password = invoker.keystore_password
2839      }
2840
2841      _inputs += [
2842        _apksigner,
2843        _zipalign,
2844        _keystore_path,
2845      ]
2846      _args += [
2847        "--apksigner-jar",
2848        rebase_path(_apksigner, root_build_dir),
2849        "--zipalign-path",
2850        rebase_path(_zipalign, root_build_dir),
2851        "--key-path",
2852        rebase_path(_keystore_path, root_build_dir),
2853        "--key-name",
2854        _keystore_name,
2855        "--key-passwd",
2856        _keystore_password,
2857      ]
2858      if (is_official_build) {
2859        _args += [ "--best-compression" ]
2860      }
2861    }
2862    if (defined(invoker.uncompress_dex)) {
2863      _uncompress_dex = invoker.uncompress_dex
2864    } else {
2865      # Uncompressed dex support started on Android P.
2866      _uncompress_dex = invoker.min_sdk_version >= 28
2867    }
2868
2869    if (_uncompress_dex) {
2870      _args += [ "--uncompress-dex" ]
2871    }
2872    if (defined(invoker.library_always_compress)) {
2873      _args +=
2874          [ "--library-always-compress=${invoker.library_always_compress}" ]
2875    }
2876    if (defined(invoker.dex_path)) {
2877      _inputs += [ invoker.dex_path ]
2878      _args += [
2879        "--dex-file",
2880        rebase_path(invoker.dex_path, root_build_dir),
2881      ]
2882    }
2883    if ((defined(invoker.loadable_modules) && invoker.loadable_modules != []) ||
2884        defined(invoker.native_libs_filearg) ||
2885        _native_lib_placeholders != []) {
2886      _args += [ "--android-abi=$android_app_abi" ]
2887    }
2888    if (defined(android_app_secondary_abi)) {
2889      _args += [ "--secondary-android-abi=$android_app_secondary_abi" ]
2890    }
2891    if (defined(invoker.loadable_modules) && invoker.loadable_modules != []) {
2892      _inputs += invoker.loadable_modules
2893      _rebased_loadable_modules =
2894          rebase_path(invoker.loadable_modules, root_build_dir)
2895      _args += [ "--native-libs=$_rebased_loadable_modules" ]
2896    }
2897    if (defined(invoker.native_libs_filearg)) {
2898      _args += [ "--native-libs=${invoker.native_libs_filearg}" ]
2899    }
2900    if (_native_lib_placeholders != []) {
2901      _args += [ "--native-lib-placeholders=$_native_lib_placeholders" ]
2902    }
2903
2904    if (defined(invoker.secondary_abi_native_libs_filearg)) {
2905      _args += [
2906        "--secondary-native-libs=${invoker.secondary_abi_native_libs_filearg}",
2907      ]
2908    }
2909    if (defined(invoker.secondary_abi_loadable_modules)) {
2910      _rebased_secondary_abi_loadable_modules =
2911          rebase_path(invoker.secondary_abi_loadable_modules, root_build_dir)
2912      _args +=
2913          [ "--secondary-native-libs=$_rebased_secondary_abi_loadable_modules" ]
2914    }
2915    if (_secondary_native_lib_placeholders != []) {
2916      _args += [ "--secondary-native-lib-placeholders=$_secondary_native_lib_placeholders" ]
2917    }
2918    if (treat_warnings_as_errors) {
2919      _args += [ "--warnings-as-errors" ]
2920    }
2921
2922    if (defined(invoker.expected_libs_and_assets)) {
2923      _expectations_target =
2924          "${invoker.top_target_name}_validate_libs_and_assets"
2925      action_with_pydeps(_expectations_target) {
2926        _actual_file = "$target_gen_dir/$target_name.libs_and_assets"
2927        _failure_file =
2928            "$expectations_failure_dir/" +
2929            string_replace(invoker.expected_libs_and_assets, "/", "_")
2930        inputs = [ invoker.expected_libs_and_assets ]
2931        if (defined(invoker.build_config)) {
2932          inputs += [ invoker.build_config ]
2933        }
2934        deps = [ invoker.build_config_dep ]
2935        outputs = [
2936          _actual_file,
2937          _failure_file,
2938        ]
2939        script = _script
2940        args = _args + [
2941                 "--expected-file",
2942                 rebase_path(invoker.expected_libs_and_assets, root_build_dir),
2943                 "--actual-file",
2944                 rebase_path(_actual_file, root_build_dir),
2945                 "--failure-file",
2946                 rebase_path(_failure_file, root_build_dir),
2947                 "--only-verify-expectations",
2948               ]
2949        if (defined(invoker.expected_libs_and_assets_base)) {
2950          inputs += [ invoker.expected_libs_and_assets_base ]
2951          args += [
2952            "--expected-file-base",
2953            rebase_path(invoker.expected_libs_and_assets_base, root_build_dir),
2954          ]
2955        }
2956        if (fail_on_android_expectations) {
2957          args += [ "--fail-on-expectations" ]
2958        }
2959      }
2960      _deps += [ ":$_expectations_target" ]
2961    }
2962    action_with_pydeps(target_name) {
2963      depfile = "$target_gen_dir/$target_name.d"
2964      inputs = _inputs
2965      deps = _deps
2966      data = _data
2967      outputs = _outputs
2968      script = _script
2969      args = _args + [
2970               "--depfile",
2971               rebase_path(depfile, root_build_dir),
2972             ]
2973    }
2974  }
2975
2976  # Compile Java source files into a .jar file, potentially using an
2977  # annotation processor, and/or the errorprone compiler. Also includes Kotlin
2978  # source files in the resulting info file.
2979  #
2980  # Note that the only way to specify custom annotation processors is
2981  # by using build_config to point to a file that corresponds to a java-related
2982  # target that includes javac:processor_classes entries (i.e. there is no
2983  # variable here that can be used for this purpose).
2984  #
2985  # Note also the peculiar use of source_files / target_sources_file. The content
2986  # of the source_files list and the source files in target_sources_file file must
2987  # match exactly.
2988  #
2989  # Variables:
2990  #  main_target_name: Used when extracting srcjars for codesearch.
2991  #  source_files: Optional list of Java and Kotlin source file paths.
2992  #  srcjar_deps: Optional list of .srcjar dependencies (not file paths).
2993  #    The corresponding source files they contain will be compiled too.
2994  #  target_sources_file: Optional path to file containing list of source file
2995  #    paths. This must always be provided if java_files is not empty and the
2996  #    .java files in it must match the list of java_files exactly.
2997  #  build_config: Path to the .build_config.json file of the corresponding
2998  #    java_library_impl() target. The following entries will be used by this
2999  #    template: javac:srcjars, deps_info:javac_full_classpath,
3000  #    deps_info:javac_full_interface_classpath, javac:processor_classpath,
3001  #    javac:processor_classes
3002  #  javac_jar_path: Path to the final output .jar file.
3003  #  javac_args: Optional list of extra arguments to pass to javac.
3004  #  chromium_code: Whether this corresponds to Chromium-specific sources.
3005  #  requires_android: True if these sources can only run on Android.
3006  #  additional_jar_files: Optional list of files to copy into the resulting
3007  #    .jar file (by default, only .class files are put there). Each entry
3008  #    has the 'srcPath:dstPath' format.
3009  #  enable_errorprone: If True, use the errorprone compiler to check for
3010  #    error-prone constructs in the language. If not provided, whether this is
3011  #    enabled depends on chromium_code and the global
3012  #    use_errorprone_java_compiler variable.
3013  #  use_turbine: If True, compile headers using turbine.py.
3014  #  apk_name: Optional APK name. If provided, will tell compile_java.py to also
3015  #    generate an .apk.jar.info file under size-info/${apk_name}.apk.jar.info
3016  #  processor_args_javac: List of annotation processor arguments, each one
3017  #    will be passed to javac as -A<entry>.
3018  #  deps: Dependencies for the corresponding target.
3019  #  testonly: Usual meaning (should be True for test-only targets)
3020  #
3021  # [1] https://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html
3022  #
3023  template("compile_java") {
3024    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3025
3026    _build_config = invoker.build_config
3027    _chromium_code = invoker.chromium_code
3028
3029    _processor_args = []
3030    if (defined(invoker.processor_args_javac)) {
3031      _processor_args = invoker.processor_args_javac
3032    }
3033
3034    _additional_jar_files = []
3035    if (defined(invoker.additional_jar_files)) {
3036      _additional_jar_files = invoker.additional_jar_files
3037    }
3038
3039    _srcjar_deps = []
3040    if (defined(invoker.srcjar_deps)) {
3041      _srcjar_deps = invoker.srcjar_deps
3042    }
3043
3044    _java_srcjars = []
3045    foreach(dep, _srcjar_deps) {
3046      _dep_gen_dir = get_label_info(dep, "target_gen_dir")
3047      _dep_name = get_label_info(dep, "name")
3048      _java_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
3049    }
3050    if (defined(invoker.srcjars)) {
3051      _java_srcjars += invoker.srcjars
3052    }
3053
3054    # generated_jar_path is an output when use_turbine and an input otherwise.
3055    if (!invoker.use_turbine && defined(invoker.generated_jar_path)) {
3056      _java_srcjars += [ invoker.generated_jar_path ]
3057    }
3058
3059    _javac_args = []
3060    if (defined(invoker.javac_args)) {
3061      _javac_args = invoker.javac_args
3062    }
3063
3064    action_with_pydeps(target_name) {
3065      if (invoker.use_turbine) {
3066        script = "//build/android/gyp/turbine.py"
3067        inputs = [
3068          "//third_party/jdk/current/bin/java",
3069          android_sdk_jar,
3070        ]
3071      } else {
3072        script = "//build/android/gyp/compile_java.py"
3073        inputs = javac_paths_for_inputs
3074      }
3075
3076      if (target_name == "chrome_java__header") {
3077        # Regression test for: https://crbug.com/1154302
3078        # Ensures that header jars never depend on non-header jars.
3079        assert_no_deps = [ "//base:base_java__compile_java" ]
3080      }
3081
3082      depfile = "$target_gen_dir/$target_name.d"
3083      deps = _srcjar_deps
3084      if (defined(invoker.deps)) {
3085        deps += invoker.deps
3086      }
3087
3088      outputs = [ invoker.output_jar_path ]
3089      if (!invoker.enable_errorprone && !invoker.use_turbine) {
3090        outputs += [ invoker.output_jar_path + ".info" ]
3091      }
3092      inputs += invoker.source_files + _java_srcjars + [
3093                  "$android_sdk/optional/android.test.base.jar",
3094                  "$android_sdk/optional/org.apache.http.legacy.jar",
3095                  _build_config,
3096                ] + java_paths_for_inputs
3097
3098      if (invoker.source_files != []) {
3099        inputs += [ invoker.target_sources_file ]
3100      }
3101
3102      _rebased_build_config = rebase_path(_build_config, root_build_dir)
3103      _rebased_output_jar_path =
3104          rebase_path(invoker.output_jar_path, root_build_dir)
3105      _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
3106      _rebased_depfile = rebase_path(depfile, root_build_dir)
3107      _rebased_generated_dir = rebase_path(
3108              "$target_gen_dir/${invoker.main_target_name}/generated_java",
3109              root_build_dir)
3110      args = [
3111        "--depfile=$_rebased_depfile",
3112        "--generated-dir=$_rebased_generated_dir",
3113        "--jar-path=$_rebased_output_jar_path",
3114        "--java-srcjars=$_rebased_java_srcjars",
3115        "--target-name",
3116        get_label_info(":${target_name}", "label_no_toolchain"),
3117      ]
3118
3119      # SDK jar must be first on classpath.
3120      if (invoker.include_android_sdk) {
3121        args += [ "--classpath=@FileArg($_rebased_build_config:android:sdk_interface_jars)" ]
3122      }
3123
3124      if (defined(invoker.header_jar_path)) {
3125        inputs += [ invoker.header_jar_path ]
3126        args += [
3127          "--header-jar",
3128          rebase_path(invoker.header_jar_path, root_build_dir),
3129        ]
3130        _header_jar_classpath =
3131            [ rebase_path(invoker.header_jar_path, root_build_dir) ]
3132        args += [ "--classpath=$_header_jar_classpath" ]
3133      }
3134
3135      if (defined(invoker.kotlin_jar_path)) {
3136        inputs += [ invoker.kotlin_jar_path ]
3137        _rebased_kotlin_jar_path =
3138            rebase_path(invoker.kotlin_jar_path, root_build_dir)
3139        args += [
3140          "--kotlin-jar-path=$_rebased_kotlin_jar_path",
3141          "--classpath=$_rebased_kotlin_jar_path",
3142        ]
3143      }
3144
3145      if (invoker.use_turbine) {
3146        # Prefer direct deps for turbine as much as possible.
3147        args += [ "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)" ]
3148      } else {
3149        args += [ "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)" ]
3150      }
3151
3152      if (invoker.use_turbine) {
3153        args += [
3154          "--processorpath=@FileArg($_rebased_build_config:javac:processor_classpath)",
3155          "--processors=@FileArg($_rebased_build_config:javac:processor_classes)",
3156        ]
3157      }
3158
3159      if (invoker.use_turbine) {
3160        _turbine_jar_path = "//third_party/turbine/turbine.jar"
3161        inputs += [ _turbine_jar_path ]
3162        outputs += [ invoker.generated_jar_path ]
3163        args += [
3164          "--turbine-jar-path",
3165          rebase_path(_turbine_jar_path, root_build_dir),
3166          "--generated-jar-path",
3167          rebase_path(invoker.generated_jar_path, root_build_dir),
3168        ]
3169      }
3170
3171      # Flag enable_kythe_annotations requires
3172      # checkout_android_prebuilts_build_tools=True in .gclient.
3173      if (enable_kythe_annotations && !invoker.enable_errorprone) {
3174        args += [ "--enable-kythe-annotations" ]
3175      }
3176      if (_chromium_code) {
3177        args += [ "--chromium-code=1" ]
3178        if (treat_warnings_as_errors) {
3179          args += [ "--warnings-as-errors" ]
3180        }
3181      }
3182      if (defined(invoker.jar_excluded_patterns)) {
3183        args += [ "--jar-info-exclude-globs=${invoker.jar_excluded_patterns}" ]
3184      }
3185
3186      if (invoker.enable_errorprone) {
3187        # Our custom plugin pulls in the main errorprone dep transitively.
3188        _errorprone_dep = "//tools/android/errorprone_plugin:errorprone_plugin"
3189        deps += [ _errorprone_dep ]
3190        _dep_gen_dir = get_label_info(_errorprone_dep, "target_gen_dir")
3191        _dep_name = get_label_info(_errorprone_dep, "name")
3192        _rebased_errorprone_buildconfig =
3193            rebase_path("$_dep_gen_dir/$_dep_name.build_config.json",
3194                        root_build_dir)
3195        args += [
3196          "--processorpath=@FileArg($_rebased_errorprone_buildconfig:deps_info:host_classpath)",
3197          "--enable-errorprone",
3198        ]
3199        inputs += [
3200          # errorprone requires the plugin directory to detect src dir.
3201          # https://source.chromium.org/chromium/chromium/src/+/main:tools/android/errorprone_plugin/src/org/chromium/tools/errorprone/plugin/UseNetworkAnnotations.java;l=84;drc=dfd88085261b662a5c0a1abea1a3b120b08e8e48
3202          "//tools/android/errorprone_plugin/OWNERS",
3203        ]
3204      }
3205      if (defined(invoker.skip_build_server) && invoker.skip_build_server) {
3206        # Nocompile tests need lint to fail through ninja.
3207        args += [ "--skip-build-server" ]
3208      } else if (android_static_analysis == "build_server") {
3209        args += [ "--use-build-server" ]
3210      }
3211
3212      foreach(e, _processor_args) {
3213        args += [ "--processor-arg=" + e ]
3214      }
3215
3216      foreach(file_tuple, _additional_jar_files) {
3217        # Each element is of length two, [ path_to_file, path_to_put_in_jar ]
3218        inputs += [ file_tuple[0] ]
3219        args +=
3220            [ "--additional-jar-file=" +
3221              rebase_path(file_tuple[0], root_build_dir) + ":" + file_tuple[1] ]
3222      }
3223      if (invoker.source_files != []) {
3224        args +=
3225            [ "@" + rebase_path(invoker.target_sources_file, root_build_dir) ]
3226      }
3227      foreach(e, _javac_args) {
3228        args += [ "--javac-arg=" + e ]
3229      }
3230    }
3231  }
3232
3233  # Compile Kotlin source files into .class files and store them in a .jar.
3234  # This explicitly does not run annotation processing on the Kotlin files.
3235  # Java files and srcjars are also passed to kotlinc for reference, although
3236  # no .class files will be generated for any Java files. A subsequent call to
3237  # javac will be required to actually compile Java files into .class files.
3238  #
3239  # This action also creates a "header" .jar file for the Kotlin source files.
3240  # It is similar to using turbine to create headers for Java files, but since
3241  # turbine does not support Kotlin files, this is done via a plugin for
3242  # kotlinc instead, at the same time as compilation (whereas turbine is run as
3243  # a separate action before javac compilation).
3244  template("compile_kt") {
3245    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3246
3247    _build_config = invoker.build_config
3248    _chromium_code = invoker.chromium_code
3249
3250    _srcjar_deps = []
3251    if (defined(invoker.srcjar_deps)) {
3252      _srcjar_deps = invoker.srcjar_deps
3253    }
3254
3255    _java_srcjars = []
3256    foreach(dep, _srcjar_deps) {
3257      _dep_gen_dir = get_label_info(dep, "target_gen_dir")
3258      _dep_name = get_label_info(dep, "name")
3259      _java_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
3260    }
3261
3262    if (defined(invoker.srcjars)) {
3263      _java_srcjars += invoker.srcjars
3264    }
3265
3266    action_with_pydeps(target_name) {
3267      script = "//build/android/gyp/compile_kt.py"
3268      depfile = "$target_gen_dir/$target_name.d"
3269      deps = _srcjar_deps
3270      if (defined(invoker.deps)) {
3271        deps += invoker.deps
3272      }
3273
3274      outputs = [
3275        invoker.output_jar_path,
3276        invoker.output_interface_jar_path,
3277      ]
3278      inputs = invoker.source_files + _java_srcjars + [
3279                 _build_config,
3280                 invoker.target_sources_file,
3281               ]
3282
3283      _rebased_build_config = rebase_path(_build_config, root_build_dir)
3284      _rebased_output_jar_path =
3285          rebase_path(invoker.output_jar_path, root_build_dir)
3286      _rebased_output_interface_jar_path =
3287          rebase_path(invoker.output_interface_jar_path, root_build_dir)
3288      _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
3289      _rebased_depfile = rebase_path(depfile, root_build_dir)
3290      _rebased_generated_dir = rebase_path(
3291              "$target_gen_dir/${invoker.main_target_name}/generated_java",
3292              root_build_dir)
3293      args = [
3294        "--depfile=$_rebased_depfile",
3295        "--generated-dir=$_rebased_generated_dir",
3296        "--jar-path=$_rebased_output_jar_path",
3297        "--interface-jar-path=$_rebased_output_interface_jar_path",
3298        "--java-srcjars=$_rebased_java_srcjars",
3299      ]
3300
3301      # SDK jar must be first on classpath.
3302      if (invoker.include_android_sdk) {
3303        args += [ "--classpath=@FileArg($_rebased_build_config:android:sdk_interface_jars)" ]
3304      }
3305
3306      args += [ "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)" ]
3307
3308      if (_chromium_code) {
3309        args += [ "--chromium-code" ]
3310        if (treat_warnings_as_errors) {
3311          args += [ "--warnings-as-errors" ]
3312        }
3313      }
3314
3315      args += [ "@" + rebase_path(invoker.target_sources_file, root_build_dir) ]
3316    }
3317  }
3318
3319  # Create an interface jar from a normal jar.
3320  #
3321  # Variables
3322  #   input_jar: Path to input .jar.
3323  #   output_jar: Path to output .ijar.
3324  #
3325  template("generate_interface_jar") {
3326    action_with_pydeps(target_name) {
3327      _ijar_target = "//third_party/ijar:ijar($host_toolchain)"
3328      _ijar_executable = get_label_info(_ijar_target, "root_out_dir") + "/ijar"
3329      forward_variables_from(invoker,
3330                             TESTONLY_AND_VISIBILITY + [
3331                                   "data",
3332                                   "data_deps",
3333                                   "public_deps",
3334                                 ])
3335      script = "//build/android/gyp/ijar.py"
3336      deps = [ _ijar_target ]
3337      if (defined(invoker.deps)) {
3338        deps += invoker.deps
3339      }
3340      inputs = [
3341        invoker.input_jar,
3342        _ijar_executable,
3343      ]
3344      if (defined(invoker.inputs)) {
3345        inputs += invoker.inputs
3346      }
3347      outputs = [ invoker.output_jar ]
3348      args = [
3349        rebase_path(_ijar_executable, root_build_dir),
3350        rebase_path(invoker.input_jar, root_build_dir),
3351        rebase_path(invoker.output_jar, root_build_dir),
3352      ]
3353    }
3354  }
3355
3356  # A rule that will handle multiple Java-related targets.
3357  #
3358  # The caller can provide a list of source files with 'java_files'
3359  # and 'srcjar_deps', or a prebuilt .jar file through 'jar_path'.
3360  #
3361  # In the case of a 'java_binary' target type, it can even provide none of
3362  # that (and the rule will just generate its wrapper script).
3363  #
3364  # The template will process the input .jar file (either the prebuilt one,
3365  # or the result of compiling the sources), for example to apply Proguard,
3366  # but also other ranges of bytecode-level rewriting schemes.
3367  #
3368  # Variables:
3369  #  type: type of Java target, valid values: 'java_library', 'java_binary',
3370  #    'robolectric_binary', 'java_annotation_processor', and 'android_apk'
3371  #  main_target_name: optional. If provided, overrides target_name when
3372  #    creating sub-targets (e.g. "${main_target_name}__dex") and
3373  #    some output files (e.g. "${main_target_name}.sources"). Only used
3374  #    for 'android_apk' types at the moment, where main_target_name will
3375  #    be the name of the main APK target.
3376  #  supports_android: Optional. True if target can run on Android.
3377  #  requires_android: Optional. True if target can only run on Android.
3378  #  source_files: Optional list of Java source file paths for this target.
3379  #  javac_args: Optional list of extra arguments to pass to javac.
3380  #  errorprone_args: Optional list of extra arguments to pass to.
3381  #  srcjar_deps: Optional list of .srcjar targets (not file paths). The Java
3382  #    source files they contain will also be compiled for this target.
3383  #  target_sources_file: Optional path to a file which will be written with
3384  #    the content of source_files. If not provided, the file will be written
3385  #    under $target_gen_dir/$main_target_name.sources. Ignored if
3386  #    sources_files is empty. If not
3387  #  jar_path: Optional path to a prebuilt .jar file for this target.
3388  #    Mutually exclusive with java_files and srcjar_deps.
3389  #  output_name: Optional output name for the final jar path. Used to
3390  #    determine the name of the final jar. Default is to use the same
3391  #    name as jar_path, if provided, or main_target_name.
3392  #  main_class: Main Java class name for 'java_binary', 'robolectric_binary' and
3393  #    'java_annotation_processor' target types. Should not be set for other
3394  #    ones.
3395  #  deps: Dependencies for this target.
3396  #  public_deps: Dependencies that this target exposes as part of its public API.
3397  #    public_deps do not need to be listed in both the 'deps' and 'public_deps' lists.
3398  #  testonly: True iff target should only be used for tests.
3399  #  chromium_code: Optional. Whether this is Chromium-specific code. If not
3400  #    provided, this is determined automatically, based on the location of
3401  #    the source files (i.e. anything under third_party/ is not
3402  #    Chromium-specific unless it is in a 'chromium' sub-directory).
3403  #  jacoco_never_instrument: Optional. If provided, whether to forbid
3404  #    instrumentation with the Jacoco coverage processor. If not provided,
3405  #    this is controlled by the global use_jacoco_coverage build arg variable
3406  #    and only used for non-test Chromium code.
3407  #  include_android_sdk: Optional. Whether or not the android SDK dep
3408  #    should be added to deps. Defaults to true for non-system libraries
3409  #    that support android.
3410  #  alternative_android_sdk_dep: Optional. Alternative Android system
3411  #    android java target to use.
3412  #  annotation_processor_deps: Optional list of dependencies corresponding
3413  #    to annotation processors used to compile these sources.
3414  #  input_jars_paths: Optional list of additional .jar file paths, which will
3415  #    be added to the compile-time classpath when building this target (but
3416  #    not to the runtime classpath).
3417  #  gradle_treat_as_prebuilt: Cause generate_gradle.py to reference this
3418  #    library via its built .jar rather than including its .java sources.
3419  #  proguard_enabled: Optional. True to enable ProGuard obfuscation.
3420  #  proguard_configs: Optional list of additional proguard config file paths.
3421  #  is_robolectric: Optional. If True, this is a host side android test binary
3422  #    which is allowed to depend on other android targets.
3423  #  include_java_resources: Optional. If True, include Java (not Android)
3424  #    resources into final .jar file.
3425  #  jar_excluded_patterns: Optional list of .class file patterns to exclude
3426  #    from the final .jar file.
3427  #  jar_included_patterns: Optional list of .class file patterns to include
3428  #    in the final .jar file. jar_excluded_patterns take precedence over this.
3429  #  low_classpath_priority: Indicates that the library should be placed at the
3430  #    end of the classpath. The default classpath order has libraries ordered
3431  #    before the libraries that they depend on. 'low_classpath_priority' is
3432  #    useful when one java_library() overrides another via
3433  #    'jar_excluded_patterns' and the overriding library does not depend on the
3434  #    overridee.
3435  #
3436  # For 'android_apk' and 'android_app_bundle_module' targets only:
3437  #
3438  #  apk_path: Path to the final APK file.
3439  #  android_manifest: Path to AndroidManifest.xml file for the APK.
3440  #  android_manifest_dep: Optional. Dependency target that generates
3441  #    android_manifest.
3442  #  apk_under_test: For 'android_apk' targets used to test other APKs,
3443  #    this is the target name of APK being tested.
3444  #  incremental_apk_path: Path to the incremental APK.
3445  #  incremental_install_json_path: Path to the incremental install json.
3446  #  native_lib_placeholders: Optional. List of placeholder filenames to add to
3447  #    the APK.
3448  #  proguard_mapping_path: Path to .mapping file produced from ProGuard step.
3449  #  shared_libraries_runtime_deps_file: Optional. Path to a file listing the
3450  #    native shared libraries required at runtime by the APK.
3451  #  secondary_abi_shared_libraries_runtime_deps_file:
3452  #  secondary_native_lib_placeholders: Optional. List of placeholder filenames
3453  #    to add to the APK for the secondary ABI.
3454  #  loadable_modules: Optional list of extra native libraries to
3455  #    be stored in the APK.
3456  #  secondary_abi_loadable_modules: Optional list of native libraries for
3457  #    secondary ABI.
3458  #  proto_resources_path: The path of an zip archive containing the APK's
3459  #    resources compiled to the protocol buffer format (instead of regular
3460  #    binary xml + resources.arsc).
3461  #  r_text_path: The path of the R.txt file generated when compiling the
3462  #    resources for this target.
3463  #  module_pathmap_path: The path of the pathmap file generated when compiling
3464  #    the resources for the bundle module, if path shortening is enabled.
3465  #  base_allowlist_rtxt_path: The path of the R.txt file containing the
3466  #    list of string resources to keep in the base split APK for any bundle
3467  #    that uses this target.
3468  #
3469  # For 'java_binary' and 'robolectric_binary' targets only. Ignored by others:
3470  #
3471  #  wrapper_script_name: Optional name for the generated wrapper script.
3472  #    Default is main target name.
3473  #  wrapper_script_args: Optional list of extra arguments used by the
3474  #    generated wrapper script.
3475  #
3476  template("java_library_impl") {
3477    # TODO(crbug.com/1042017): Remove.
3478    not_needed(invoker, [ "no_build_hooks" ])
3479
3480    forward_variables_from(invoker, [ "testonly" ])
3481    _is_prebuilt = defined(invoker.jar_path)
3482    _type = invoker.type
3483    _is_annotation_processor = _type == "java_annotation_processor"
3484    _is_java_binary = _type == "java_binary" || _type == "robolectric_binary"
3485    _is_library = _type == "java_library"
3486    _supports_android =
3487        defined(invoker.supports_android) && invoker.supports_android
3488    _requires_android =
3489        defined(invoker.requires_android) && invoker.requires_android
3490    _supports_host = !_requires_android
3491    if (_is_java_binary || _is_annotation_processor) {
3492      assert(!_requires_android && !_supports_android)
3493    }
3494
3495    _bypass_platform_checks = defined(invoker.bypass_platform_checks) &&
3496                              invoker.bypass_platform_checks
3497    _is_robolectric = defined(invoker.is_robolectric) && invoker.is_robolectric
3498
3499    _invoker_deps = []
3500    if (defined(invoker.deps)) {
3501      _invoker_deps += invoker.deps
3502    }
3503    if (defined(invoker.public_deps)) {
3504      _invoker_deps += invoker.public_deps
3505    }
3506
3507    _main_target_name = target_name
3508    if (defined(invoker.main_target_name)) {
3509      _main_target_name = invoker.main_target_name
3510    }
3511
3512    _source_files = []
3513    if (defined(invoker.sources)) {
3514      _source_files = invoker.sources
3515    }
3516
3517    _srcjar_deps = []
3518    if (defined(invoker.srcjar_deps)) {
3519      _srcjar_deps = invoker.srcjar_deps
3520    }
3521    _srcjars = []
3522    if (defined(invoker.srcjars)) {
3523      _srcjars = invoker.srcjars
3524    }
3525    _has_sources = _source_files != [] || _srcjar_deps != [] || _srcjars != []
3526    if (_is_prebuilt || _has_sources) {
3527      # This allows us to use jar_excluded_patterns and prevent even the
3528      # interface jars from having these classes. This means that, with this
3529      # flag, nobody depending on this java library will be able to see these
3530      # classes. These excluded classes are only used for the exact target they
3531      # are compiled in. We do this by not making a header jar, and replacing
3532      # all usages of the header jar with the processed (post-exclusion) jar.
3533      _skip_header_jar =
3534          defined(invoker.prevent_excluded_classes_from_classpath) &&
3535          invoker.prevent_excluded_classes_from_classpath
3536    }
3537
3538    if (_is_prebuilt) {
3539      assert(!_has_sources)
3540    } else {
3541      # Allow java_binary to not specify any sources. This is needed when a prebuilt
3542      # is needed as a library as well as a binary.
3543      assert(_is_annotation_processor || _is_java_binary || _has_sources)
3544    }
3545
3546    if (_is_java_binary) {
3547      assert(defined(invoker.main_class), "${_type}() must set main_class")
3548    } else if (_is_annotation_processor) {
3549      assert(defined(invoker.main_class),
3550             "java_annotation_processor() must set main_class")
3551    } else {
3552      assert(!defined(invoker.main_class),
3553             "main_class cannot be used for target of type ${_type}")
3554    }
3555
3556    if (defined(invoker.chromium_code)) {
3557      _chromium_code = invoker.chromium_code
3558    } else {
3559      # Default based on whether target is in third_party.
3560      _chromium_code =
3561          filter_exclude([ get_label_info(":$_main_target_name", "dir") ],
3562                         [ "*\bthird_party\b*" ]) != []
3563      if (!_chromium_code && !_is_prebuilt && _source_files != []) {
3564        # Unless third_party code has an org.chromium file in it.
3565        _chromium_code =
3566            filter_exclude(_source_files, [ "*\bchromium\b*" ]) != _source_files
3567      }
3568    }
3569
3570    # Define build_config_deps which will be a list of targets required to
3571    # build the _build_config.
3572    _build_config = "$target_gen_dir/$_main_target_name.build_config.json"
3573    _build_config_target_name =
3574        "${_main_target_name}$build_config_target_suffix"
3575
3576    # The only target that might have no prebuilt and no sources is a java_binary.
3577    _build_host_jar = false
3578    _build_device_jar = false
3579    if (_is_prebuilt || _has_sources) {
3580      if (defined(invoker.output_name)) {
3581        _output_name = invoker.output_name
3582      } else {
3583        _output_name = _main_target_name
3584      }
3585
3586      _build_host_jar =
3587          _is_java_binary || _is_annotation_processor || _type == "java_library"
3588      _build_device_jar = _type != "system_java_library" && _supports_android
3589
3590      _jacoco_instrument =
3591          use_jacoco_coverage && _chromium_code && _source_files != [] &&
3592          _build_device_jar && (!defined(invoker.testonly) || !invoker.testonly)
3593      if (defined(invoker.jacoco_never_instrument)) {
3594        _jacoco_instrument =
3595            !invoker.jacoco_never_instrument && _jacoco_instrument
3596      }
3597      if (_jacoco_instrument) {
3598        _invoker_deps += [ _jacoco_dep ]
3599      }
3600
3601      if (_build_host_jar) {
3602        # Jar files can be needed at runtime (by Robolectric tests or java binaries),
3603        # so do not put them under obj/.
3604        # TODO(agrieve): I suspect it would be better to use dist_jar for java_binary
3605        #     rather than archiving unnecessary .jar files within lib.java.
3606        _target_dir_name = get_label_info(":$_main_target_name", "dir")
3607        _host_processed_jar_path =
3608            "$root_out_dir/lib.java$_target_dir_name/$_output_name.jar"
3609      }
3610      if (_build_device_jar) {
3611        _dex_path = "$target_out_dir/$_main_target_name.dex.jar"
3612        _enable_desugar =
3613            !defined(invoker.enable_desugar) || invoker.enable_desugar
3614
3615        # Build speed optimization: Skip "process device" step if the step
3616        # would be just a copy and avoid the copy.
3617        _process_device_jar =
3618            defined(invoker.bytecode_rewriter_target) || _jacoco_instrument ||
3619            defined(invoker.jar_excluded_patterns) ||
3620            defined(invoker.jar_included_patterns)
3621        if (!_process_device_jar && _is_prebuilt) {
3622          _device_processed_jar_path = invoker.jar_path
3623        } else {
3624          _device_processed_jar_path =
3625              "$target_out_dir/$_output_name.processed.jar"
3626        }
3627      }
3628
3629      # For static libraries, the javac jar output is created at the intermediate
3630      # path so that it can be processed by another target and moved to the final
3631      # spot that the .build_config.json knows about. Technically this should be done
3632      # for the ijar as well, but this is only used for APK targets where
3633      # the ijar path isn't actually used.
3634      if (_has_sources) {
3635        _final_ijar_path = "$target_out_dir/$_output_name.turbine.jar"
3636      } else {
3637        _final_ijar_path = "$target_out_dir/$_output_name.ijar.jar"
3638      }
3639
3640      if (_has_sources) {
3641        if (_build_device_jar && !_process_device_jar) {
3642          _javac_jar_path = _device_processed_jar_path
3643        } else {
3644          _javac_jar_path = "$target_out_dir/$_main_target_name.javac.jar"
3645        }
3646        _generated_jar_path =
3647            "$target_gen_dir/$_main_target_name.generated.srcjar"
3648      }
3649
3650      if (_is_prebuilt) {
3651        _unprocessed_jar_path = invoker.jar_path
3652      } else {
3653        _unprocessed_jar_path = _javac_jar_path
3654      }
3655    }
3656
3657    _java_assetres_deps = filter_include(_invoker_deps, java_resource_patterns)
3658
3659    # Cannot use minus operator because it does not work when the operand has
3660    # repeated entries.
3661    _invoker_deps_minus_assetres =
3662        filter_exclude(_invoker_deps, _java_assetres_deps)
3663    _lib_deps =
3664        filter_include(_invoker_deps_minus_assetres, java_library_patterns)
3665    _non_java_deps = filter_exclude(_invoker_deps_minus_assetres, _lib_deps)
3666
3667    _java_header_deps = []  # Turbine / ijar
3668
3669    # It would be more ideal to split this into __host and __javac, but we
3670    # combine the two concepts to save on a group() target.
3671    _java_host_deps = []  # Processed host .jar + javac .jar.
3672    _java_validate_deps = []  # Bytecode checker & errorprone.
3673
3674    foreach(_lib_dep, _lib_deps) {
3675      # Expand //foo/java -> //foo/java:java
3676      _lib_dep = get_label_info(_lib_dep, "label_no_toolchain")
3677      _java_assetres_deps += [ "${_lib_dep}__assetres" ]
3678      _java_header_deps += [ "${_lib_dep}__header" ]
3679      _java_host_deps += [ "${_lib_dep}__host" ]
3680      _java_validate_deps += [ "${_lib_dep}__validate" ]
3681    }
3682
3683    # APK and base module targets are special because:
3684    # 1) They do not follow java target naming scheme (since they are not
3685    #    generally deps, there is no need for them to).
3686    # 2) They do not bother to define a __host target.
3687    # Since __host is used as an indirect dep for the compile_java artifacts,
3688    # add the __compile_java target directly for them.
3689    if (defined(invoker.apk_under_test)) {
3690      _java_assetres_deps += [ "${invoker.apk_under_test}__java__assetres" ]
3691      _java_header_deps += [ "${invoker.apk_under_test}__java__header" ]
3692      _java_validate_deps += [ "${invoker.apk_under_test}__java__validate" ]
3693      _java_host_deps += [ "${invoker.apk_under_test}__compile_java" ]
3694    }
3695    if (defined(invoker.base_module_target)) {
3696      _java_assetres_deps += [ "${invoker.base_module_target}__java__assetres" ]
3697      _java_header_deps += [ "${invoker.base_module_target}__java__header" ]
3698      _java_validate_deps += [ "${invoker.base_module_target}__java__validate" ]
3699      _java_host_deps += [ "${invoker.base_module_target}__compile_java" ]
3700    }
3701
3702    not_needed([ "_non_java_deps" ])
3703
3704    if (_is_prebuilt || _has_sources) {
3705      # Classpath deps are used for header and dex targets, they do not need
3706      # __assetres deps.
3707      # _non_java_deps are needed for input_jars_paths that are generated.
3708      _header_classpath_deps =
3709          _java_header_deps + _non_java_deps + [ ":$_build_config_target_name" ]
3710
3711      _javac_classpath_deps =
3712          _java_host_deps + _non_java_deps + [ ":$_build_config_target_name" ]
3713
3714      _include_android_sdk = _build_device_jar
3715      if (defined(invoker.include_android_sdk)) {
3716        _include_android_sdk = invoker.include_android_sdk
3717      }
3718      if (_include_android_sdk) {
3719        if (defined(invoker.alternative_android_sdk_dep)) {
3720          _android_sdk_dep = invoker.alternative_android_sdk_dep
3721        } else {
3722          _android_sdk_dep = default_android_sdk_dep
3723        }
3724
3725        _header_classpath_deps += [ "${_android_sdk_dep}__header" ]
3726        _javac_classpath_deps += [ "${_android_sdk_dep}" ]
3727      }
3728    }
3729
3730    # Often needed, but too hard to figure out when ahead of time.
3731    not_needed([
3732                 "_header_classpath_deps",
3733                 "_javac_classpath_deps",
3734               ])
3735
3736    if (_source_files != []) {
3737      _target_sources_file = "$target_gen_dir/$_main_target_name.sources"
3738      write_file(_target_sources_file,
3739                 rebase_path(_source_files, root_build_dir))
3740    }
3741
3742    write_build_config(_build_config_target_name) {
3743      forward_variables_from(invoker,
3744                             [
3745                               "aar_path",
3746                               "annotation_processor_deps",
3747                               "base_allowlist_rtxt_path",
3748                               "gradle_treat_as_prebuilt",
3749                               "input_jars_paths",
3750                               "preferred_dep",
3751                               "low_classpath_priority",
3752                               "main_class",
3753                               "mergeable_android_manifests",
3754                               "module_name",
3755                               "parent_module_target",
3756                               "proguard_configs",
3757                               "proguard_enabled",
3758                               "proguard_mapping_path",
3759                               "public_target_label",
3760                               "r_text_path",
3761                               "type",
3762                               "version_code",
3763                               "version_name",
3764                             ])
3765      if (_type == "android_apk" || _type == "android_app_bundle_module") {
3766        forward_variables_from(
3767            invoker,
3768            [
3769              "android_manifest",
3770              "android_manifest_dep",
3771              "merged_android_manifest",
3772              "final_dex_path",
3773              "loadable_modules",
3774              "native_lib_placeholders",
3775              "res_size_info_path",
3776              "secondary_abi_loadable_modules",
3777              "secondary_abi_shared_libraries_runtime_deps_file",
3778              "secondary_native_lib_placeholders",
3779              "shared_libraries_runtime_deps_file",
3780              "library_always_compress",
3781            ])
3782      }
3783      if (_type == "android_apk") {
3784        forward_variables_from(invoker,
3785                               [
3786                                 "apk_path",
3787                                 "apk_under_test",
3788                                 "incremental_apk_path",
3789                                 "incremental_install_json_path",
3790                               ])
3791      }
3792      if (_type == "android_app_bundle_module") {
3793        forward_variables_from(invoker,
3794                               [
3795                                 "add_view_trace_events",
3796                                 "base_module_target",
3797                                 "module_pathmap_path",
3798                                 "proto_resources_path",
3799                               ])
3800      }
3801      chromium_code = _chromium_code
3802      build_config = _build_config
3803      is_prebuilt = _is_prebuilt
3804
3805      # Specifically avoid passing in invoker.base_module_target as one of the
3806      # possible_config_deps.
3807      possible_config_deps = []
3808      if (defined(invoker.deps)) {
3809        possible_config_deps = invoker.deps
3810      }
3811      if (defined(invoker.public_deps)) {
3812        possible_config_public_deps = invoker.public_deps
3813      }
3814      if (defined(invoker.asset_deps)) {
3815        possible_config_deps += invoker.asset_deps
3816      }
3817      if (defined(apk_under_test)) {
3818        possible_config_deps += [ apk_under_test ]
3819      }
3820      if (defined(_jacoco_instrument) && _jacoco_instrument) {
3821        possible_config_deps += [ _jacoco_dep ]
3822      }
3823      if (defined(_android_sdk_dep)) {
3824        possible_config_deps += [ _android_sdk_dep ]
3825      }
3826
3827      supports_android = _supports_android
3828      requires_android = _requires_android
3829      is_robolectric = _is_robolectric
3830      bypass_platform_checks = _bypass_platform_checks
3831
3832      if (defined(invoker.resources_package)) {
3833        custom_package = invoker.resources_package
3834      }
3835      if (_is_prebuilt || _has_sources) {
3836        if (_skip_header_jar) {
3837          # We are tricking everything that is looking for an ijar into looking
3838          # at the processed jar path, which is has the excluded classes
3839          # removed.
3840          ijar_path = _device_processed_jar_path
3841        } else {
3842          ijar_path = _final_ijar_path
3843        }
3844        unprocessed_jar_path = _unprocessed_jar_path
3845      }
3846      if (_build_host_jar) {
3847        host_jar_path = _host_processed_jar_path
3848      }
3849      if (_build_device_jar) {
3850        device_jar_path = _device_processed_jar_path
3851        dex_path = _dex_path
3852      }
3853      if (_source_files != []) {
3854        target_sources_file = _target_sources_file
3855      }
3856
3857      bundled_srcjars = []
3858      foreach(d, _srcjar_deps) {
3859        _dep_gen_dir = get_label_info(d, "target_gen_dir")
3860        _dep_name = get_label_info(d, "name")
3861        bundled_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
3862      }
3863      bundled_srcjars += _srcjars
3864      if (defined(invoker.include_java_resources) &&
3865          invoker.include_java_resources) {
3866        java_resources_jar = _unprocessed_jar_path
3867        if (defined(invoker.jar_path)) {
3868          # Use original jar_path because _jar_path points to a library without
3869          # resources.
3870        } else {
3871          java_resources_jar = _device_processed_jar_path
3872        }
3873      }
3874    }
3875
3876    if (_is_prebuilt || _has_sources) {
3877      _header_target_name = "${target_name}__header"
3878    }
3879
3880    if (_has_sources) {
3881      _kt_files = filter_include(_source_files, [ "*.kt" ])
3882      _java_files = filter_exclude(_source_files, [ "*.kt" ])
3883
3884      if (defined(invoker.enable_errorprone)) {
3885        _enable_errorprone = invoker.enable_errorprone
3886      } else {
3887        _enable_errorprone =
3888            _java_files != [] && _chromium_code && use_errorprone_java_compiler
3889      }
3890
3891      if (defined(invoker.resources_package) && _type == "java_library") {
3892        # TODO(crbug.com/1296632): remove _bypass_platform_checks from the list
3893        # once all robolectric targets have migrated to robolectric_library.
3894        assert(_requires_android || _bypass_platform_checks || _is_robolectric,
3895               "Setting resources_package applicable only for " +
3896                   "android_library(), or robolectric_library(). " +
3897                   "Target=$target_name")
3898
3899        # Serves double purpose: Generating R.java, as well as being the
3900        #__assetres target (instead of using a separate group).
3901        _fake_rjava_target = "${target_name}__assetres"
3902        generate_r_java(_fake_rjava_target) {
3903          deps = [ ":$_build_config_target_name" ] + _java_assetres_deps +
3904                 _non_java_deps
3905          build_config = _build_config
3906
3907          # Filepath has to be exactly this because compile_java looks for the
3908          # srcjar of srcjar_deps at this location $gen_dir/$target_name.srcjar
3909          srcjar_path = "$target_gen_dir/$target_name.srcjar"
3910          package = invoker.resources_package
3911        }
3912        _srcjar_deps += [ ":$_fake_rjava_target" ]
3913      }
3914
3915      if (_kt_files != []) {
3916        _kt_allowlist = [
3917          "android/java/src/org/chromium/chrome/browser/tabmodel/AsyncTabParamsManagerImpl.kt",
3918          "java/androidx/core/os/BuildCompat.kt",
3919          "webengine_shell_apk/src/org/chromium/webengine/shell/*.kt",
3920        ]
3921        _found_kt = filter_exclude(_kt_files, _kt_allowlist)
3922        assert(
3923            _found_kt == [],
3924            "Only a files in the allowlist can be included for now. Feel " + "free to remove this assert when experimenting locally. Found: $_found_kt")
3925        _compile_kt_target_name = "${_main_target_name}__compile_kt"
3926        _kotlinc_jar_path = "$target_out_dir/$_output_name.kotlinc.jar"
3927        _kotlin_interface_jar_path =
3928            "$target_out_dir/$_output_name.kt-jvm-abi.jar"
3929        assert(filter_include(_lib_deps, [ _kotlin_stdlib_dep ]) != [],
3930               "${_main_target_name} is missing dep: $_kotlin_stdlib_dep")
3931        compile_kt(_compile_kt_target_name) {
3932          deps = _header_classpath_deps
3933          output_jar_path = _kotlinc_jar_path
3934          output_interface_jar_path = _kotlin_interface_jar_path
3935          main_target_name = _main_target_name
3936          build_config = _build_config
3937          srcjar_deps = _srcjar_deps
3938          source_files = _source_files
3939          target_sources_file = _target_sources_file
3940          chromium_code = _chromium_code
3941          include_android_sdk = _is_robolectric || _requires_android
3942        }
3943      }
3944
3945      template("compile_java_helper") {
3946        _enable_errorprone =
3947            defined(invoker.enable_errorprone) && invoker.enable_errorprone
3948        if (_enable_errorprone) {
3949          # Rely on the header jar to provide all .class files so that it is
3950          # safe to omit generated files entirely for errorprone.
3951          _filtered_source_files =
3952              filter_exclude(_source_files, [ "$root_gen_dir*" ])
3953        }
3954        if (_enable_errorprone && _filtered_source_files == []) {
3955          # Filtering out generated files resulted in no files left.
3956          group(target_name) {
3957            not_needed(invoker, "*")
3958            deps = _header_classpath_deps
3959          }
3960        } else {
3961          compile_java(target_name) {
3962            forward_variables_from(invoker,
3963                                   "*",
3964                                   TESTONLY_AND_VISIBILITY + [ "deps" ])
3965            deps = _header_classpath_deps
3966            if (defined(invoker.deps)) {
3967              deps += invoker.deps
3968            }
3969            output_jar_path = invoker.output_jar_path
3970            if (defined(invoker.kotlin_jar_path)) {
3971              deps += [ ":$_compile_kt_target_name" ]
3972              kotlin_jar_path = invoker.kotlin_jar_path
3973            }
3974            enable_errorprone = _enable_errorprone
3975            use_turbine = defined(invoker.use_turbine) && invoker.use_turbine
3976
3977            main_target_name = _main_target_name
3978            build_config = _build_config
3979
3980            if (_enable_errorprone) {
3981              source_files = _filtered_source_files
3982            } else {
3983              source_files = _source_files
3984              srcjar_deps = _srcjar_deps
3985            }
3986
3987            if (source_files != []) {
3988              target_sources_file = _target_sources_file
3989            }
3990            chromium_code = _chromium_code
3991            include_android_sdk = _is_robolectric || _requires_android
3992          }
3993        }
3994      }
3995      _compile_java_forward_variables = [
3996        "additional_jar_files",
3997        "apk_name",
3998        "jar_excluded_patterns",
3999        "javac_args",
4000        "processor_args_javac",
4001        "skip_build_server",
4002        "srcjars",
4003      ]
4004
4005      if (!_skip_header_jar) {
4006        _annotation_processor_deps = []
4007        if (defined(invoker.annotation_processor_deps)) {
4008          _annotation_processor_deps = invoker.annotation_processor_deps
4009        }
4010
4011        compile_java_helper(_header_target_name) {
4012          forward_variables_from(invoker, _compile_java_forward_variables)
4013          use_turbine = true
4014          output_jar_path = _final_ijar_path
4015          generated_jar_path = _generated_jar_path
4016          deps = _annotation_processor_deps
4017          if (_kt_files != []) {
4018            kotlin_jar_path = _kotlin_interface_jar_path
4019          }
4020        }
4021      }
4022
4023      _compile_java_target = "${_main_target_name}__compile_java"
4024      compile_java_helper(_compile_java_target) {
4025        forward_variables_from(invoker, _compile_java_forward_variables)
4026        output_jar_path = _javac_jar_path
4027        if (!_skip_header_jar) {
4028          deps = [ ":$_header_target_name" ]
4029          header_jar_path = _final_ijar_path
4030          generated_jar_path = _generated_jar_path
4031        }
4032        if (_kt_files != []) {
4033          kotlin_jar_path = _kotlinc_jar_path
4034        }
4035      }
4036
4037      if (_skip_header_jar) {
4038        group(_header_target_name) {
4039          public_deps = [ ":$_compile_java_target" ]
4040        }
4041      }
4042
4043      if (_enable_errorprone) {
4044        _compile_java_errorprone_target = "${_main_target_name}__errorprone"
4045        compile_java_helper(_compile_java_errorprone_target) {
4046          forward_variables_from(invoker, _compile_java_forward_variables)
4047          enable_errorprone = true
4048          if (defined(invoker.errorprone_args)) {
4049            if (!defined(javac_args)) {
4050              javac_args = []
4051            }
4052            javac_args += invoker.errorprone_args
4053          }
4054          if (_kt_files != []) {
4055            kotlin_jar_path = _kotlinc_jar_path
4056          }
4057          if (!_skip_header_jar) {
4058            deps = [ ":$_header_target_name" ]
4059            header_jar_path = _final_ijar_path
4060            generated_jar_path = _generated_jar_path
4061          }
4062          output_jar_path = "$target_out_dir/$target_name.errorprone.stamp"
4063        }
4064        _java_validate_deps += [ ":$_compile_java_errorprone_target" ]
4065      }
4066    }  # _has_sources
4067
4068    if (_is_prebuilt || _build_device_jar || _build_host_jar) {
4069      if (_has_sources) {
4070        _unprocessed_jar_deps = [ ":$_compile_java_target" ]
4071      } else {
4072        # jars might be generated by a dep.
4073        _unprocessed_jar_deps = _non_java_deps
4074      }
4075    }
4076
4077    if (defined(invoker.bytecode_rewriter_target)) {
4078      assert(_build_host_jar || _build_device_jar,
4079             "A host or device jar must be created to use bytecode rewriting")
4080
4081      _rewritten_jar = "$target_out_dir/${target_name}_rewritten.jar"
4082      _rewritten_jar_target_name = "${target_name}__rewritten"
4083      _rewriter_path = root_build_dir + "/bin/helper/" +
4084                       get_label_info(invoker.bytecode_rewriter_target, "name")
4085      _rebased_build_config = rebase_path(_build_config, root_build_dir)
4086      action_with_pydeps(_rewritten_jar_target_name) {
4087        script = "//build/android/gyp/bytecode_rewriter.py"
4088        inputs = java_paths_for_inputs + [
4089                   _rewriter_path,
4090                   _build_config,
4091                   _unprocessed_jar_path,
4092                 ]
4093        outputs = [ _rewritten_jar ]
4094        depfile = "$target_gen_dir/$target_name.d"
4095        args = [
4096          "--depfile",
4097          rebase_path(depfile, root_build_dir),
4098          "--script",
4099          rebase_path(_rewriter_path, root_build_dir),
4100          "--classpath",
4101          "@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
4102          "--classpath",
4103          "@FileArg($_rebased_build_config:android:sdk_jars)",
4104          "--input-jar",
4105          rebase_path(_unprocessed_jar_path, root_build_dir),
4106          "--output-jar",
4107          rebase_path(_rewritten_jar, root_build_dir),
4108        ]
4109        deps = _unprocessed_jar_deps + _javac_classpath_deps +
4110               [ invoker.bytecode_rewriter_target ]
4111      }
4112
4113      _unprocessed_jar_deps = []
4114      _unprocessed_jar_deps = [ ":$_rewritten_jar_target_name" ]
4115      _unprocessed_jar_path = _rewritten_jar
4116    }
4117
4118    if (_is_prebuilt) {
4119      generate_interface_jar(_header_target_name) {
4120        # Always used the unfiltered .jar to create the interface jar so that
4121        # other targets will resolve filtered classes when depending on
4122        # BuildConfig, NativeLibraries, etc.
4123        input_jar = _unprocessed_jar_path
4124        output_jar = _final_ijar_path
4125
4126        # ijar needs only _unprocessed_jar_deps, but this also needs to export
4127        # __header target from deps.
4128        deps = _unprocessed_jar_deps + _java_header_deps
4129      }
4130    }
4131
4132    if (_build_host_jar || _build_device_jar) {
4133      _enable_bytecode_checks =
4134          (!defined(invoker.enable_bytecode_checks) ||
4135           invoker.enable_bytecode_checks) && !_is_prebuilt &&
4136          android_static_analysis != "off"
4137      if (_enable_bytecode_checks) {
4138        _validate_target_name = "${target_name}__validate"
4139        bytecode_processor(_validate_target_name) {
4140          forward_variables_from(invoker, [ "missing_classes_allowlist" ])
4141          deps = _unprocessed_jar_deps + _javac_classpath_deps +
4142                 [ ":$_build_config_target_name" ]
4143          data_deps = _java_validate_deps
4144          if (defined(_compile_java_errorprone_target)) {
4145            data_deps += [ ":$_compile_java_errorprone_target" ]
4146          }
4147
4148          include_android_sdk = _requires_android || _is_robolectric
4149          target_label =
4150              get_label_info(":${invoker.target_name}", "label_no_toolchain")
4151          input_jar = _unprocessed_jar_path
4152          build_config = _build_config
4153        }
4154      } else {
4155        not_needed(invoker, [ "missing_classes_allowlist" ])
4156      }
4157
4158      if (_build_host_jar) {
4159        _process_host_jar_target_name = "${target_name}__host"
4160        process_java_library(_process_host_jar_target_name) {
4161          forward_variables_from(invoker,
4162                                 [
4163                                   "jar_excluded_patterns",
4164                                   "jar_included_patterns",
4165                                 ])
4166
4167          # Robolectric tests require these to be on swarming.
4168          data = [ _host_processed_jar_path ]
4169          input_jar_path = _unprocessed_jar_path
4170          deps = _unprocessed_jar_deps + _javac_classpath_deps
4171          output_jar_path = _host_processed_jar_path
4172          jacoco_instrument = _jacoco_instrument
4173          if (_jacoco_instrument) {
4174            source_files = _source_files
4175            target_sources_file = _target_sources_file
4176          }
4177
4178          # _java_host_deps isn't necessary for process_java_library(), but is
4179          # necessary so that this target can be used to depend on transitive
4180          # __device targets without the need to create a separate group()
4181          # target. This trade-off works because process_java_library is fast.
4182          deps += _java_host_deps
4183
4184          # Add runtime_deps here since robolectric_binary does not depend on top-level group.
4185          if (defined(invoker.data)) {
4186            data += invoker.data
4187          }
4188          if (defined(invoker.data_deps)) {
4189            data_deps = invoker.data_deps
4190          }
4191        }
4192      }
4193
4194      if (_build_device_jar) {
4195        if (_process_device_jar) {
4196          _process_device_jar_target_name = "${target_name}__process_device"
4197          process_java_library(_process_device_jar_target_name) {
4198            forward_variables_from(invoker,
4199                                   [
4200                                     "jar_excluded_patterns",
4201                                     "jar_included_patterns",
4202                                   ])
4203            input_jar_path = _unprocessed_jar_path
4204
4205            deps = _unprocessed_jar_deps + _javac_classpath_deps
4206            output_jar_path = _device_processed_jar_path
4207            jacoco_instrument = _jacoco_instrument
4208            if (_jacoco_instrument) {
4209              source_files = _source_files
4210              target_sources_file = _target_sources_file
4211            }
4212          }
4213          _process_device_jar_deps = [ ":${_process_device_jar_target_name}" ]
4214        } else {
4215          assert(_unprocessed_jar_path == _device_processed_jar_path)
4216          _process_device_jar_deps = _unprocessed_jar_deps
4217        }
4218
4219        _dex_target_name = "${target_name}__dex"
4220        dex(_dex_target_name) {
4221          forward_variables_from(invoker,
4222                                 [
4223                                   "proguard_enable_obfuscation",
4224                                   "repackage_classes",
4225                                 ])
4226          input_class_jars = [ _device_processed_jar_path ]
4227          enable_desugar = _enable_desugar
4228          ignore_desugar_missing_deps = !_enable_bytecode_checks
4229
4230          # There's no value in per-class dexing prebuilts since they never
4231          # change just one class at a time.
4232          disable_incremental = _is_prebuilt
4233          output = _dex_path
4234          deps = _process_device_jar_deps
4235
4236          if (enable_desugar) {
4237            # Desugaring with D8 requires full classpath.
4238            build_config = _build_config
4239            unprocessed_jar_path = _unprocessed_jar_path
4240            deps += _header_classpath_deps + _unprocessed_jar_deps
4241          }
4242
4243          is_library = true
4244
4245          # proguard_configs listed on java_library targets need to be marked
4246          # as inputs to at least one target so that "gn analyze" will know
4247          # about them. Although this target doesn't use them, it's a convenient spot
4248          # to list them.
4249          # https://crbug.com/827197
4250          if (compute_inputs_for_analyze && defined(invoker.proguard_configs)) {
4251            inputs = invoker.proguard_configs
4252
4253            # For the aapt-generated proguard rules.
4254            deps += _non_java_deps + _srcjar_deps
4255          }
4256        }
4257      }
4258    }
4259
4260    if (_is_java_binary) {
4261      # Targets might use the generated script while building, so make it a dep
4262      # rather than a data_dep.
4263      _java_binary_script_target_name = "${target_name}__java_binary_script"
4264      java_binary_script(_java_binary_script_target_name) {
4265        forward_variables_from(invoker,
4266                               [
4267                                 "tiered_stop_at_level_one",
4268                                 "main_class",
4269                                 "max_heap_size",
4270                                 "wrapper_script_args",
4271                               ])
4272        build_config = _build_config
4273        script_name = _main_target_name
4274        if (defined(invoker.wrapper_script_name)) {
4275          script_name = invoker.wrapper_script_name
4276        }
4277        deps = [ ":$_build_config_target_name" ]
4278        if (_is_robolectric) {
4279          # For robolectric tests, we add the sdk stub jars so that classes
4280          # that reference Android types can be loaded without throwing
4281          # NoClassDefFoundErrors. The Robolectric sandbox makes these types
4282          # available in non-stub form, but not until test classes are loaded
4283          # into it. Before being loaded into the sandbox, they must be loaded
4284          # outside of it in order to read their annotations (which configure
4285          # the sandbox), and to enumerate test methods.
4286          extra_classpath_jars = [
4287            android_sdk_jar,
4288            "$android_sdk/optional/android.test.base.jar",
4289            "$android_sdk/optional/android.test.runner.jar",
4290          ]
4291        }
4292      }
4293    }
4294
4295    if (!defined(_validate_target_name)) {
4296      _validate_target_name = "${target_name}__validate"
4297
4298      # Allow other targets to depend on this __validate one.
4299      group(_validate_target_name) {
4300        deps = _java_validate_deps
4301      }
4302    }
4303
4304    if (_supports_host && !defined(_process_host_jar_target_name)) {
4305      group("${target_name}__host") {
4306        deps = _java_host_deps
4307      }
4308    }
4309
4310    # robolectric_library can depend on java_library, so java_library must
4311    # define __assetres.
4312    if ((_is_library || _supports_android || _is_robolectric) &&
4313        !defined(_fake_rjava_target)) {
4314      group("${target_name}__assetres") {
4315        if (_supports_android || _is_robolectric) {
4316          deps = _java_assetres_deps
4317        }
4318      }
4319    }
4320
4321    # The top-level group is used:
4322    # 1) To allow building the target explicitly via ninja,
4323    # 2) To trigger all analysis deps,
4324    # 3) By custom action() targets that want to use artifacts as inputs.
4325    group(target_name) {
4326      forward_variables_from(invoker,
4327                             [
4328                               "assert_no_deps",
4329                               "data",
4330                               "data_deps",
4331                               "visibility",
4332                             ])
4333      if (_requires_android || (_supports_android && _is_library)) {
4334        # For non-robolectric targets, depend on other java target's top-level
4335        # groups so that the __dex step gets depended on.
4336        forward_variables_from(invoker,
4337                               [
4338                                 "deps",
4339                                 "public_deps",
4340                               ])
4341        if (!defined(deps)) {
4342          deps = []
4343        }
4344        if (is_cronet_build) {
4345          _abs_deps = []
4346          if (defined(invoker.deps)) {
4347            foreach(dep, invoker.deps) {
4348              _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
4349            }
4350          }
4351          if (defined(invoker.public_deps)) {
4352            foreach(dep, invoker.public_deps) {
4353              _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
4354            }
4355          }
4356          if (defined(invoker.srcjar_deps)) {
4357            foreach(dep, invoker.srcjar_deps) {
4358              _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
4359            }
4360          }
4361          _abs_path_source_files = []
4362          if (defined(invoker.sources)) {
4363            foreach(source_file, invoker.sources) {
4364              _abs_path_source_files +=
4365                  [ get_path_info(source_file, "abspath") ]
4366            }
4367          }
4368          _abs_jar_path = ""
4369          if (defined(invoker.jar_path)) {
4370            _abs_jar_path = get_path_info(invoker.jar_path, "abspath")
4371          }
4372          _sdk_version = "current"
4373          if (defined(invoker.alternative_android_sdk_dep)) {
4374            _sdk_version = "system_current"
4375          }
4376
4377          # See crbug/1449896 for more details about the metadata fields
4378          # and why they are added.
4379          metadata = {
4380            jar_path = [ _abs_jar_path ]
4381            source_files = _abs_path_source_files
4382            all_deps = _abs_deps
4383            target_type = [ _type ]
4384            sdk_version = [ _sdk_version ]
4385          }
4386        }
4387        if (!defined(public_deps)) {
4388          public_deps = []
4389        }
4390      } else {
4391        # For robolectric targets, depend only on non-java deps and the specific
4392        # subtargets below, which will not include __dex.
4393        deps = _non_java_deps
4394        public_deps = []
4395        if (defined(invoker.public_deps)) {
4396          public_deps +=
4397              filter_exclude(invoker.public_deps, java_target_patterns)
4398        }
4399      }
4400      if (defined(_jacoco_instrument) && _jacoco_instrument) {
4401        deps += [ _jacoco_dep ]
4402      }
4403      if (defined(invoker.apk_under_test)) {
4404        deps += [ invoker.apk_under_test ]
4405      }
4406      if (defined(_process_device_jar_target_name)) {
4407        public_deps += [ ":$_process_device_jar_target_name" ]
4408      }
4409      if (defined(_dex_target_name)) {
4410        public_deps += [ ":$_dex_target_name" ]
4411      }
4412      if (_supports_android && _is_library) {
4413        # Robolectric targets define __assetres, but there's no need to build it
4414        # by default.
4415        public_deps += [ ":${target_name}__assetres" ]
4416      }
4417      if (_supports_host) {
4418        # android_* targets define __host, but there's no need to build it by
4419        # default.
4420        public_deps += [ ":${target_name}__host" ]
4421      }
4422      if (_is_java_binary) {
4423        public_deps += [ ":$_java_binary_script_target_name" ]
4424      }
4425      if (!defined(data_deps)) {
4426        data_deps = []
4427      }
4428      if (defined(_validate_target_name)) {
4429        data_deps += [ ":$_validate_target_name" ]
4430      } else {
4431        data_deps += _java_validate_deps
4432      }
4433    }
4434  }
4435}
4436
4437# Create a zip archive corresponding to an application bundle module.
4438#
4439# Compile all the components of a given android_apk_or_module() target into a
4440# zip archive suitable to later create an android_app_bundle() target. This
4441# archive's format is very similar to that on an APK, except for a few
4442# differences in internal directory layouts, and the fact that resources, as
4443# well as xml files, are compiled using a protocol-buffer based format (instead
4444# of the regular binary xml + resources.arsc).
4445#
4446# A final application bundle is built from one or more module bundle modules,
4447# plus some configuration file.
4448#
4449# Variables:
4450#   module_zip_path: Output module path.
4451#   build_config: Path to build_config of the android_apk_or_module() target.
4452#   dex_path: If module is proguarded separately from the base module, dex_path
4453#     is the path to its dex file and is passed directly to the creation script.
4454#     Otherwise, dex_path is undefined and we retrieve the module's dex file
4455#     using its build_config.
4456#   expected_libs_and_assets: Verify the list of included native libraries
4457#     and assets is consistent with the given expectation file.
4458#   expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff
4459#     with this file as the base.
4460#   is_multi_abi: If true will add a library placeholder for the missing ABI if
4461#     either the primary or the secondary ABI has no native libraries set.
4462#   module_name: The module's name.
4463#   native_libraries_config: Path to file listing native libraries to be
4464#     packaged into each module.
4465#   proguard_enabled: Optional. True if proguarding is enabled for this
4466#     bundle. Default is to enable this only for release builds. Note that
4467#     this will always perform synchronized proguarding.
4468template("create_android_app_bundle_module") {
4469  _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
4470
4471  forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
4472  _deps = invoker.deps
4473  _script = "//build/android/gyp/apkbuilder.py"
4474
4475  # NOTE: Compared to the inputs of the "package_apk" template action,
4476  #       this list is much smaller, since finalize_apk is never called
4477  #       by apkbuild.py --format=bundle-module. This means not using
4478  #       apksigner and zipalign as well, nor the keystore. Other
4479  #       dependencies like extra native libraries are all pulled from the
4480  #       .build_config.json through @FileArg() references (see below) and
4481  #       will be listed in the generated depfile instead.
4482  _inputs = [ invoker.build_config ]
4483  _outputs = [ invoker.module_zip_path ]
4484  _args = [
4485    "--format=bundle-module",
4486    "--output-apk",
4487    rebase_path(invoker.module_zip_path, root_build_dir),
4488    "--resource-apk=@FileArg(" +
4489        "$_rebased_build_config:deps_info:proto_resources_path)",
4490    "--assets=@FileArg($_rebased_build_config:assets)",
4491    "--uncompressed-assets=@FileArg(" +
4492        "$_rebased_build_config:uncompressed_assets)",
4493    "--native-libs=@FileArg($_rebased_build_config:native:libraries)",
4494    "--native-libs=@FileArg($_rebased_build_config:native:loadable_modules)",
4495    "--native-lib-placeholders=@FileArg($_rebased_build_config" +
4496        ":native:native_library_placeholders)",
4497    "--secondary-native-lib-placeholders=@FileArg($_rebased_build_config" +
4498        ":native:secondary_native_library_placeholders)",
4499    "--android-abi=$android_app_abi",
4500    "--min-sdk-version=${invoker.min_sdk_version}",
4501    "--library-always-compress=@FileArg($_rebased_build_config:native:library_always_compress)",
4502  ]
4503  if (defined(android_app_secondary_abi)) {
4504    _args += [
4505      "--secondary-native-libs=@FileArg(" +
4506          "$_rebased_build_config:native:secondary_abi_libraries)",
4507      "--secondary-native-libs=@FileArg(" +
4508          "$_rebased_build_config:native:secondary_abi_loadable_modules)",
4509      "--secondary-android-abi=$android_app_secondary_abi",
4510    ]
4511  }
4512  if (defined(invoker.is_multi_abi) && invoker.is_multi_abi) {
4513    _args += [ "--is-multi-abi" ]
4514  }
4515  if (defined(invoker.uncompress_dex) && invoker.uncompress_dex) {
4516    _args += [ "--uncompress-dex" ]
4517  }
4518  if (defined(invoker.extra_assets)) {
4519    _args += [ "--assets=${invoker.extra_assets}" ]
4520  }
4521
4522  # Use either provided dex path or build config path based on type of module.
4523  if (defined(invoker.dex_path)) {
4524    _inputs += [ invoker.dex_path ]
4525    _rebased_dex_path = rebase_path(invoker.dex_path, root_build_dir)
4526    _args += [ "--dex-file=$_rebased_dex_path" ]
4527  } else {
4528    _args += [ "--dex-file=@FileArg($_rebased_build_config:final_dex:path)" ]
4529  }
4530
4531  if (treat_warnings_as_errors) {
4532    _args += [ "--warnings-as-errors" ]
4533  }
4534
4535  if (defined(invoker.expected_libs_and_assets)) {
4536    _expectations_target = "${invoker.top_target_name}_validate_libs_and_assets"
4537    action_with_pydeps(_expectations_target) {
4538      _actual_file = "$target_gen_dir/$target_name.libs_and_assets"
4539      _failure_file = "$expectations_failure_dir/" +
4540                      string_replace(invoker.expected_libs_and_assets, "/", "_")
4541      inputs = [
4542        invoker.expected_libs_and_assets,
4543        invoker.build_config,
4544      ]
4545      deps = [ invoker.build_config_target ]
4546      outputs = [
4547        _actual_file,
4548        _failure_file,
4549      ]
4550      script = _script
4551      args = _args + [
4552               "--expected-file",
4553               rebase_path(invoker.expected_libs_and_assets, root_build_dir),
4554               "--actual-file",
4555               rebase_path(_actual_file, root_build_dir),
4556               "--failure-file",
4557               rebase_path(_failure_file, root_build_dir),
4558               "--only-verify-expectations",
4559             ]
4560      if (defined(invoker.expected_libs_and_assets_base)) {
4561        inputs += [ invoker.expected_libs_and_assets_base ]
4562        args += [
4563          "--expected-file-base",
4564          rebase_path(invoker.expected_libs_and_assets_base, root_build_dir),
4565        ]
4566      }
4567      if (fail_on_android_expectations) {
4568        args += [ "--fail-on-expectations" ]
4569      }
4570    }
4571    _deps += [ ":$_expectations_target" ]
4572  }
4573
4574  action_with_pydeps(target_name) {
4575    deps = _deps
4576    inputs = _inputs
4577    outputs = _outputs
4578    script = _script
4579    depfile = "$target_gen_dir/$target_name.d"
4580    args = _args + [
4581             "--depfile",
4582             rebase_path(depfile, root_build_dir),
4583           ]
4584  }
4585}
4586