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