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