# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Do not add any imports to non-//build directories here.
# Some projects (e.g. V8) do not have non-build directories DEPS'ed in.
import("//build_overrides/build.gni")
import("//build/config/android/config.gni")
import("//build/config/dcheck_always_on.gni")
import("//build/config/sanitizers/sanitizers.gni")

assert(is_android)

# These identify targets that have .build_config files (except for android_apk,
# java_binary, resource_rewriter, since we never need to depend on these).
_java_target_whitelist = [
  "*:*_java",
  "*:*_javalib",
  "*:*_java_*",  # e.g. java_test_support
  "*:java",
  "*:junit",
  "*:junit_*",
  "*:*_junit_*",
  "*:*javatests",
  "*:*_assets",
  "*android*:assets",
  "*:*_apk_*resources",
  "*android*:resources",
  "*:*_resources",
  "*:*_grd",
  "*:*locale_paks",

  # TODO(agrieve): Rename targets below to match above patterns.
  "*android_webview/glue:glue",
  "//build/android/rezip:rezip",
  "//chrome/test/android/cast_emulator:cast_emulator",
]

# Targets that match the whitelist but are not actually java targets.
_java_target_blacklist = [
  "//chrome:packed_resources",
  "//remoting/android:remoting_android_raw_resources",
  "*:*_unpack_aar",
]

# Write the target's .build_config file. This is a json file that contains a
# dictionary of information about how to build this target (things that
# require knowledge about this target's dependencies and cannot be calculated
# at gn-time). There is a special syntax to add a value in that dictionary to
# an action/action_foreachs args:
#   --python-arg=@FileArg($rebased_build_config_path:key0:key1)
# At runtime, such an arg will be replaced by the value in the build_config.
# See build/android/gyp/write_build_config.py and
# build/android/gyp/util/build_utils.py:ExpandFileArgs
template("write_build_config") {
  type = invoker.type
  _is_prebuilt_binary =
      defined(invoker.is_prebuilt_binary) && invoker.is_prebuilt_binary

  # Don't need to enforce naming scheme for these targets since we never
  # consider them in dependency chains.
  if (!_is_prebuilt_binary && type != "android_apk" && type != "java_binary" &&
      type != "resource_rewriter") {
    set_sources_assignment_filter(_java_target_whitelist)
    _parent_invoker = invoker.invoker
    _target_label =
        get_label_info(":${_parent_invoker.target_name}", "label_no_toolchain")
    sources = [
      _target_label,
    ]
    if (sources != []) {
      set_sources_assignment_filter(_java_target_blacklist)
      sources = []
      sources = [
        _target_label,
      ]
      if (sources != []) {
        assert(false, "Invalid java target name: $_target_label")
      }
    }
    sources = []
  }

  action(target_name) {
    set_sources_assignment_filter([])
    build_config = invoker.build_config

    assert(type == "android_apk" || type == "java_library" ||
           type == "android_resources" || type == "deps_dex" ||
           type == "android_assets" || type == "resource_rewriter" ||
           type == "java_binary" || type == "group" || type == "java_prebuilt")

    forward_variables_from(invoker,
                           [
                             "deps",
                             "testonly",
                           ])
    if (!defined(deps)) {
      deps = []
    }

    script = "//build/android/gyp/write_build_config.py"
    depfile = "$target_gen_dir/$target_name.d"
    inputs = []

    _deps_configs = []
    if (defined(invoker.possible_config_deps)) {
      foreach(_possible_dep, invoker.possible_config_deps) {
        set_sources_assignment_filter(_java_target_whitelist)
        _target_label = get_label_info(_possible_dep, "label_no_toolchain")
        sources = [
          _target_label,
        ]
        if (sources == []) {
          set_sources_assignment_filter(_java_target_blacklist)
          sources = []
          sources = [
            _target_label,
          ]
          if (sources != []) {
            deps += [ "${_target_label}__build_config" ]
            _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir")
            _dep_name = get_label_info(_possible_dep, "name")
            _deps_configs += [ "$_dep_gen_dir/$_dep_name.build_config" ]
          }
        }
        sources = []
      }
      set_sources_assignment_filter([])
    }
    _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)

    outputs = [
      build_config,
    ]

    args = [
      "--type",
      type,
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--deps-configs=$_rebased_deps_configs",
      "--build-config",
      rebase_path(build_config, root_build_dir),
    ]

    is_java = type == "java_library" || type == "java_binary" ||
              type == "java_prebuilt"
    is_apk = type == "android_apk"
    is_android_assets = type == "android_assets"
    is_android_resources = type == "android_resources"
    is_deps_dex = type == "deps_dex"
    is_group = type == "group"

    supports_android = is_apk || is_android_assets || is_android_resources ||
                       is_deps_dex || is_group ||
                       (is_java && defined(invoker.supports_android) &&
                        invoker.supports_android)
    requires_android =
        is_apk || is_android_assets || is_android_resources || is_deps_dex ||
        (is_java && defined(invoker.requires_android) &&
         invoker.requires_android)

    assert(!requires_android || supports_android,
           "requires_android requires" + " supports_android")

    # Mark these variables as used.
    assert(is_java || true)
    assert(is_apk || true)
    assert(is_android_resources || true)
    assert(is_deps_dex || true)
    assert(is_group || true)

    if (is_java || is_apk) {
      args += [
        "--jar-path",
        rebase_path(invoker.jar_path, root_build_dir),
      ]
    }

    if (is_apk || is_deps_dex || (is_java && supports_android)) {
      args += [
        "--dex-path",
        rebase_path(invoker.dex_path, root_build_dir),
      ]
    }
    if (supports_android) {
      args += [ "--supports-android" ]
    }
    if (requires_android) {
      args += [ "--requires-android" ]
    }
    if (defined(invoker.bypass_platform_checks) &&
        invoker.bypass_platform_checks) {
      args += [ "--bypass-platform-checks" ]
    }

    if (defined(invoker.apk_under_test)) {
      deps += [ "${invoker.apk_under_test}__build_config" ]
      apk_under_test_gen_dir =
          get_label_info(invoker.apk_under_test, "target_gen_dir")
      apk_under_test_name = get_label_info(invoker.apk_under_test, "name")
      apk_under_test_config =
          "$apk_under_test_gen_dir/$apk_under_test_name.build_config"
      args += [
        "--tested-apk-config",
        rebase_path(apk_under_test_config, root_build_dir),
      ]
    }

    if (is_android_assets) {
      if (defined(invoker.asset_sources)) {
        _rebased_asset_sources =
            rebase_path(invoker.asset_sources, root_build_dir)
        args += [ "--asset-sources=$_rebased_asset_sources" ]
      }
      if (defined(invoker.asset_renaming_sources)) {
        _rebased_asset_renaming_sources =
            rebase_path(invoker.asset_renaming_sources, root_build_dir)
        args += [ "--asset-renaming-sources=$_rebased_asset_renaming_sources" ]

        # These are zip paths, so no need to rebase.
        args += [ "--asset-renaming-destinations=${invoker.asset_renaming_destinations}" ]
      }
      if (defined(invoker.disable_compression) && invoker.disable_compression) {
        args += [ "--disable-asset-compression" ]
      }
    }

    if (is_android_resources || is_apk) {
      assert(defined(invoker.resources_zip))
      args += [
        "--resources-zip",
        rebase_path(invoker.resources_zip, root_build_dir),
      ]
      if (defined(invoker.android_manifest)) {
        inputs += [ invoker.android_manifest ]
        args += [
          "--android-manifest",
          rebase_path(invoker.android_manifest, root_build_dir),
        ]
      } else {
        assert(!is_apk, "apk build configs require an android_manifest")
      }
      if (defined(invoker.custom_package)) {
        args += [
          "--package-name",
          invoker.custom_package,
        ]
      }
      if (defined(invoker.r_text)) {
        args += [
          "--r-text",
          rebase_path(invoker.r_text, root_build_dir),
        ]
      }
    }

    if (is_android_resources && defined(invoker.resource_dirs)) {
      resource_dirs = rebase_path(invoker.resource_dirs, root_build_dir)
      args += [ "--resource-dirs=$resource_dirs" ]
    }

    if (is_apk) {
      if (defined(invoker.shared_libraries_runtime_deps_file)) {
        # Don't list shared_libraries_runtime_deps_file as an input in order to
        # avoid having to depend on the runtime_deps target. See comment in
        # rules.gni for why we do this.
        args += [
          "--shared-libraries-runtime-deps",
          rebase_path(invoker.shared_libraries_runtime_deps_file,
                      root_build_dir),
        ]
      }

      if (defined(invoker.secondary_abi_shared_libraries_runtime_deps_file)) {
        # Don't list secondary_abi_shared_libraries_runtime_deps_file as an
        # input in order to avoid having to depend on the runtime_deps target.
        # See comment in rules.gni for why we do this.
        args += [
          "--secondary-abi-shared-libraries-runtime-deps",
          rebase_path(invoker.secondary_abi_shared_libraries_runtime_deps_file,
                      root_build_dir),
        ]
      }

      if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) {
        args += [
          "--proguard-enabled",
          "--proguard-info",
          rebase_path(invoker.proguard_info, root_build_dir),
        ]
      }

      if (defined(invoker.apk_path)) {
        _rebased_apk_path = rebase_path(invoker.apk_path, root_build_dir)
        _rebased_incremental_apk_path =
            rebase_path(invoker.incremental_apk_path, root_build_dir)
        _rebased_incremental_install_script_path =
            rebase_path(invoker.incremental_install_script_path, root_build_dir)
        args += [ "--apk-path=$_rebased_apk_path" ]
        args += [ "--incremental-apk-path=$_rebased_incremental_apk_path" ]
        args += [ "--incremental-install-script-path=$_rebased_incremental_install_script_path" ]
      }
    }

    if (defined(invoker.java_sources_file)) {
      args += [
        "--java-sources-file",
        rebase_path(invoker.java_sources_file, root_build_dir),
      ]
    }
    if (defined(invoker.srcjar)) {
      args += [
        "--srcjar",
        rebase_path(invoker.srcjar, root_build_dir),
      ]
    }
    if (defined(invoker.bundled_srcjars)) {
      _rebased_bundled_srcjars =
          rebase_path(invoker.bundled_srcjars, root_build_dir)
      args += [ "--bundled-srcjars=$_rebased_bundled_srcjars" ]
    }
    if (defined(invoker.input_jars_paths)) {
      _rebased_input_jars_paths =
          rebase_path(invoker.input_jars_paths, root_build_dir)
      args += [ "--extra-classpath-jars=$_rebased_input_jars_paths" ]
    }
    if (defined(invoker.proguard_configs)) {
      _rebased_proguard_configs =
          rebase_path(invoker.proguard_configs, root_build_dir)
      args += [ "--proguard-configs=$_rebased_proguard_configs" ]
    }
    if (defined(invoker.gradle_treat_as_prebuilt) &&
        invoker.gradle_treat_as_prebuilt) {
      args += [ "--gradle-treat-as-prebuilt" ]
    }
    if (defined(invoker.main_class)) {
      args += [
        "--main-class",
        invoker.main_class,
      ]
    }
    if (current_toolchain != default_toolchain) {
      # This has to be a built-time error rather than a GN assert because many
      # packages have a mix of java and non-java targets. For example, the
      # following would fail even though nothing depends on :bar(//baz):
      #
      # shared_library("foo") {
      # }
      #
      # android_library("bar") {
      #   deps = [ ":foo(//baz)" ]
      #   assert(current_toolchain == default_toolchain)
      # }
      _msg = [
        "Tried to build an Android target in a non-default toolchain.",
        "target: " + get_label_info(":$target_name", "label_with_toolchain"),
        "default_toolchain: $default_toolchain",
      ]
      args += [ "--fail=$_msg" ]
    }
  }
}

template("copy_ex") {
  set_sources_assignment_filter([])
  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "data",
                             "deps",
                             "inputs",
                             "sources",
                             "testonly",
                             "visibility",
                           ])
    if (!defined(sources)) {
      sources = []
    }
    script = "//build/android/gyp/copy_ex.py"
    depfile = "$target_gen_dir/$target_name.d"

    _stamp_file = "$target_gen_dir/$target_name.stamp"
    outputs = [
      _stamp_file,
    ]

    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--stamp",
      rebase_path(_stamp_file, root_build_dir),
      "--dest",
      rebase_path(invoker.dest, root_build_dir),
    ]
    rebased_sources = rebase_path(sources, root_build_dir)
    args += [ "--files=$rebased_sources" ]

    if (defined(invoker.clear_dir) && invoker.clear_dir) {
      args += [ "--clear" ]
    }

    if (defined(invoker.args)) {
      args += invoker.args
    }

    if (defined(invoker.renaming_sources) &&
        defined(invoker.renaming_destinations)) {
      sources += invoker.renaming_sources
      rebased_renaming_sources =
          rebase_path(invoker.renaming_sources, root_build_dir)
      args += [ "--renaming-sources=$rebased_renaming_sources" ]

      renaming_destinations = invoker.renaming_destinations
      args += [ "--renaming-destinations=$renaming_destinations" ]
    }
  }
}

# Generates a script in the build bin directory which runs the test
# target using the test runner script in build/android/test_runner.py.
template("test_runner_script") {
  testonly = true
  _test_name = invoker.test_name
  _test_type = invoker.test_type
  _incremental_install =
      defined(invoker.incremental_install) && invoker.incremental_install

  _runtime_deps =
      !defined(invoker.ignore_all_data_deps) || !invoker.ignore_all_data_deps

  if (_runtime_deps) {
    # This runtime_deps file is used at runtime and thus cannot go in
    # target_gen_dir.
    _target_dir_name = get_label_info(":$target_name", "dir")
    _runtime_deps_file =
        "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.runtime_deps"
    _runtime_deps_target = "${target_name}__write_deps"
    group(_runtime_deps_target) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "deps",
                               "public_deps",
                             ])
      write_runtime_deps = _runtime_deps_file
    }
  }

  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(data_deps)) {
      data_deps = []
    }

    script = "//build/android/gyp/create_test_runner_script.py"
    depfile = "$target_gen_dir/$target_name.d"

    data_deps += [ "//build/android:test_runner_py" ]
    data = []

    test_runner_args = [
      _test_type,
      "--output-directory",
      rebase_path(root_build_dir, root_build_dir),
    ]

    if (_runtime_deps) {
      deps += [ ":$_runtime_deps_target" ]
      data += [ _runtime_deps_file ]
      test_runner_args += [
        "--runtime-deps-path",
        rebase_path(_runtime_deps_file, root_build_dir),
      ]
    }

    # apk_target is not used for native executable tests
    # (e.g. breakpad_unittests).
    if (defined(invoker.apk_target)) {
      assert(!defined(invoker.executable_dist_dir))
      deps += [ "${invoker.apk_target}__build_config" ]
      _apk_build_config =
          get_label_info(invoker.apk_target, "target_gen_dir") + "/" +
          get_label_info(invoker.apk_target, "name") + ".build_config"
      _rebased_apk_build_config = rebase_path(_apk_build_config, root_build_dir)
      assert(_rebased_apk_build_config != "")  # Mark as used.
    } else if (_test_type == "gtest") {
      assert(
          defined(invoker.executable_dist_dir),
          "Must define either apk_target or executable_dist_dir for test_runner_script()")
      test_runner_args += [
        "--executable-dist-dir",
        rebase_path(invoker.executable_dist_dir, root_build_dir),
      ]
    }

    if (_test_type == "gtest") {
      assert(defined(invoker.test_suite))
      test_runner_args += [
        "--suite",
        invoker.test_suite,
      ]
    } else if (_test_type == "instrumentation") {
      _test_apk = "@FileArg($_rebased_apk_build_config:deps_info:apk_path)"
      if (_incremental_install) {
        _test_apk = "@FileArg($_rebased_apk_build_config:deps_info:incremental_apk_path)"
      }
      test_runner_args += [
        "--test-apk=$_test_apk",
        "--test-jar",
        rebase_path(invoker.test_jar, root_build_dir),
      ]
      if (defined(invoker.apk_under_test)) {
        deps += [ "${invoker.apk_under_test}__build_config" ]
        _apk_under_test_build_config =
            get_label_info(invoker.apk_under_test, "target_gen_dir") + "/" +
            get_label_info(invoker.apk_under_test, "name") + ".build_config"
        _rebased_apk_under_test_build_config =
            rebase_path(_apk_under_test_build_config, root_build_dir)
        _apk_under_test =
            "@FileArg($_rebased_apk_under_test_build_config:deps_info:apk_path)"
        if (_incremental_install) {
          _apk_under_test = "@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_apk_path)"
        }
        test_runner_args += [ "--apk-under-test=$_apk_under_test" ]
      }
      if (emma_coverage) {
        # Set a default coverage output directory (can be overridden by user
        # passing the same flag).
        test_runner_args += [
          "--coverage-dir",
          rebase_path("$root_out_dir/coverage", root_build_dir),
        ]
      }
    } else if (_test_type == "junit") {
      assert(defined(invoker.test_suite))
      test_runner_args += [
        "--test-suite",
        invoker.test_suite,
      ]
    } else {
      assert(false, "Invalid test type: $_test_type.")
    }

    if (defined(invoker.additional_apks)) {
      foreach(additional_apk, invoker.additional_apks) {
        deps += [ "${additional_apk}__build_config" ]
        _build_config = get_label_info(additional_apk, "target_gen_dir") + "/" +
                        get_label_info(additional_apk, "name") + ".build_config"
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        test_runner_args += [
          "--additional-apk",
          "@FileArg($_rebased_build_config:deps_info:apk_path)",
        ]
      }
    }
    if (defined(invoker.shard_timeout)) {
      test_runner_args += [ "--shard-timeout=${invoker.shard_timeout}" ]
    }
    if (_incremental_install) {
      test_runner_args += [
        "--test-apk-incremental-install-script",
        "@FileArg($_rebased_apk_build_config:deps_info:incremental_install_script_path)",
      ]
      if (defined(invoker.apk_under_test)) {
        test_runner_args += [
          "--apk-under-test-incremental-install-script",
          "@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_install_script_path)",
        ]
      }
      test_runner_args += [ "--fast-local-dev" ]
    }
    if (is_asan) {
      test_runner_args += [ "--tool=asan" ]
    }

    generated_script = "$root_build_dir/bin/run_${_test_name}"
    outputs = [
      generated_script,
    ]
    data += [ generated_script ]

    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--script-output-path",
      rebase_path(generated_script, root_build_dir),
    ]
    if (defined(android_test_runner_script)) {
      args += [
        "--test-runner-path",
        android_test_runner_script,
      ]
    }

    args += test_runner_args
  }
}

if (enable_java_templates) {
  import("//build/config/zip.gni")
  import("//third_party/ijar/ijar.gni")
  import("//third_party/android_platform/config.gni")

  rebased_android_sdk = rebase_path(android_sdk, root_build_dir)
  rebased_android_sdk_build_tools =
      rebase_path(android_sdk_build_tools, root_build_dir)

  android_sdk_jar = "$android_sdk/android.jar"
  rebased_android_sdk_jar = rebase_path(android_sdk_jar, root_build_dir)
  android_default_aapt_path = "$rebased_android_sdk_build_tools/aapt"

  android_configuration_name = "Release"
  if (is_debug) {
    android_configuration_name = "Debug"
  }

  template("android_lint") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "data_deps",
                               "public_deps",
                               "testonly",
                             ])
      if (!defined(deps)) {
        deps = []
      }

      if (!defined(lint_suppressions_file)) {
        lint_suppressions_file = "//build/android/lint/suppressions.xml"
      }

      _cache_dir = "$root_build_dir/android_lint_cache"
      _result_path = "$target_gen_dir/$target_name/result.xml"
      _config_path = "$target_gen_dir/$target_name/config.xml"
      _suppressions_file = lint_suppressions_file
      _platform_xml_path =
          "${android_sdk_root}/platform-tools/api/api-versions.xml"
      _rebased_lint_android_sdk_root =
          rebase_path(lint_android_sdk_root, root_build_dir)

      script = "//build/android/gyp/lint.py"
      depfile = "$target_gen_dir/$target_name.d"
      inputs = [
        _platform_xml_path,
        _suppressions_file,
        invoker.android_manifest,
      ]

      outputs = [
        _result_path,
        _config_path,
      ]

      args = [
        "--lint-path=$_rebased_lint_android_sdk_root/tools/lint",
        "--cache-dir",
        rebase_path(_cache_dir, root_build_dir),
        "--platform-xml-path",
        rebase_path(_platform_xml_path, root_build_dir),
        "--android-sdk-version=${lint_android_sdk_version}",
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--config-path",
        rebase_path(_suppressions_file, root_build_dir),
        "--manifest-path",
        rebase_path(invoker.android_manifest, root_build_dir),
        "--product-dir=.",
        "--processed-config-path",
        rebase_path(_config_path, root_build_dir),
        "--result-path",
        rebase_path(_result_path, root_build_dir),
        "--enable",
      ]

      if (defined(invoker.create_cache) && invoker.create_cache) {
        args += [
          "--create-cache",
          "--silent",
        ]
      } else {
        inputs += invoker.java_files
        inputs += [
          invoker.jar_path,
          invoker.build_config,
        ]
        if (invoker.java_files != []) {
          inputs += [ invoker.java_sources_file ]
          _rebased_java_sources_file =
              rebase_path(invoker.java_sources_file, root_build_dir)
          args += [ "--java-sources-file=$_rebased_java_sources_file" ]
        }
        deps += [ "//build/android:prepare_android_lint_cache" ]

        _rebased_build_config =
            rebase_path(invoker.build_config, root_build_dir)
        args += [
          "--jar-path",
          rebase_path(invoker.jar_path, root_build_dir),
          "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)",
          "--resource-sources=@FileArg($_rebased_build_config:deps_info:owned_resources_dirs)",
          "--resource-sources=@FileArg($_rebased_build_config:deps_info:owned_resources_zips)",
          "--can-fail-build",
        ]
      }
    }
  }

  template("proguard") {
    action(target_name) {
      set_sources_assignment_filter([])
      forward_variables_from(invoker,
                             [
                               "deps",
                               "data_deps",
                               "public_deps",
                               "testonly",
                             ])
      script = "//build/android/gyp/proguard.py"
      if (defined(invoker.proguard_jar_path)) {
        _proguard_jar_path = invoker.proguard_jar_path
      } else {
        _proguard_jar_path = "//third_party/proguard/lib/proguard.jar"
      }
      _output_jar_path = invoker.output_jar_path
      inputs = [
        _proguard_jar_path,
      ]
      if (defined(invoker.alternative_android_sdk_jar)) {
        inputs += [ invoker.alternative_android_sdk_jar ]
        _rebased_android_sdk_jar =
            rebase_path(invoker.alternative_android_sdk_jar)
      } else {
        inputs += [ android_sdk_jar ]
        _rebased_android_sdk_jar = rebased_android_sdk_jar
      }
      if (defined(invoker.inputs)) {
        inputs += invoker.inputs
      }
      depfile = "${target_gen_dir}/${target_name}.d"
      outputs = [
        _output_jar_path,
        "$_output_jar_path.dump",
        "$_output_jar_path.seeds",
        "$_output_jar_path.mapping",
        "$_output_jar_path.usage",
      ]
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--proguard-path",
        rebase_path(_proguard_jar_path, root_build_dir),
        "--output-path",
        rebase_path(_output_jar_path, root_build_dir),
        "--classpath",
        _rebased_android_sdk_jar,
      ]
      if (proguard_verbose) {
        args += [ "--verbose" ]
      }
      if (defined(invoker.args)) {
        args += invoker.args
      }
      if (defined(invoker.proguard_jar_path)) {
        # We assume that if we are using a different ProGuard, this new version
        # can handle the 'dangerous' optimizaions.
        args += [ "--enable-dangerous-optimizations" ]
      }
    }
  }

  template("findbugs") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                             ])
      script = "//build/android/findbugs_diff.py"
      depfile = "$target_gen_dir/$target_name.d"
      _result_path = "$target_gen_dir/$target_name/result.xml"
      _exclusions_file = "//build/android/findbugs_filter/findbugs_exclude.xml"

      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)

      inputs = [
        "//build/android/pylib/utils/findbugs.py",
        _exclusions_file,
        invoker.jar_path,
        invoker.build_config,
      ]

      outputs = [
        _result_path,
      ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--exclude",
        rebase_path(_exclusions_file, root_build_dir),
        "--auxclasspath-gyp",
        "@FileArg($_rebased_build_config:javac:classpath)",
        "--output-file",
        rebase_path(_result_path, root_build_dir),
        rebase_path(invoker.jar_path, root_build_dir),
      ]

      if (findbugs_verbose) {
        args += [ "-vv" ]
      }
    }
  }

  # Generates a script in the build bin directory to run a java binary.
  #
  # Variables
  #   main_class: The class containing the program entry point.
  #   jar_path: The path to the jar to run.
  #   script_name: Name of the script to generate.
  #   build_config: Path to .build_config for the jar (contains classpath).
  #   wrapper_script_args: List of extra arguments to pass to the executable.
  #
  template("java_binary_script") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    _main_class = invoker.main_class
    _build_config = invoker.build_config
    _jar_path = invoker.jar_path
    _script_name = invoker.script_name

    action(target_name) {
      script = "//build/android/gyp/create_java_binary_script.py"
      depfile = "$target_gen_dir/$_script_name.d"
      java_script = "$root_build_dir/bin/$_script_name"
      inputs = [
        _build_config,
      ]
      outputs = [
        java_script,
      ]
      forward_variables_from(invoker, [ "deps" ])
      _rebased_build_config = rebase_path(_build_config, root_build_dir)
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--output",
        rebase_path(java_script, root_build_dir),
        "--classpath=@FileArg($_rebased_build_config:deps_info:java:full_classpath)",
        "--jar-path",
        rebase_path(_jar_path, root_build_dir),
        "--main-class",
        _main_class,
      ]
      if (emma_coverage) {
        args += [
          "--classpath",
          rebase_path("//third_party/android_tools/sdk/tools/lib/emma.jar",
                      root_build_dir),
        ]
        args += [ "--noverify" ]
      }
      if (defined(invoker.wrapper_script_args)) {
        args += [ "--" ] + invoker.wrapper_script_args
      }
      if (defined(invoker.bootclasspath)) {
        args += [
          "--bootclasspath",
          rebase_path(invoker.bootclasspath, root_build_dir),
        ]
      }
    }
  }

  template("dex") {
    set_sources_assignment_filter([])

    _enable_multidex =
        defined(invoker.enable_multidex) && invoker.enable_multidex

    if (_enable_multidex) {
      _main_dex_list_path = invoker.output + ".main_dex_list"
      _main_dex_list_target_name = "${target_name}__main_dex_list"
      action(_main_dex_list_target_name) {
        forward_variables_from(invoker,
                               [
                                 "deps",
                                 "inputs",
                                 "sources",
                                 "testonly",
                               ])

        script = "//build/android/gyp/main_dex_list.py"
        depfile = "$target_gen_dir/$target_name.d"

        main_dex_rules = "//build/android/main_dex_classes.flags"

        if (!defined(inputs)) {
          inputs = []
        }
        inputs += [ main_dex_rules ]

        outputs = [
          _main_dex_list_path,
        ]

        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--android-sdk-tools",
          rebased_android_sdk_build_tools,
          "--main-dex-list-path",
          rebase_path(_main_dex_list_path, root_build_dir),
          "--main-dex-rules-path",
          rebase_path(main_dex_rules, root_build_dir),
        ]

        if (defined(invoker.args)) {
          args += invoker.args
        }

        if (defined(invoker.sources)) {
          args += rebase_path(invoker.sources, root_build_dir)
        }
      }
    }

    assert(defined(invoker.output))
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "inputs",
                               "sources",
                               "testonly",
                             ])
      script = "//build/android/gyp/dex.py"
      depfile = "$target_gen_dir/$target_name.d"
      outputs = [
        invoker.output,
      ]

      rebased_output = rebase_path(invoker.output, root_build_dir)

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--android-sdk-tools",
        rebased_android_sdk_build_tools,
        "--dex-path",
        rebased_output,
      ]

      if (enable_incremental_dx) {
        args += [ "--incremental" ]
      }

      # EMMA requires --no-locals.
      if (emma_coverage) {
        args += [ "--no-locals=1" ]
      }

      if (_enable_multidex) {
        args += [
          "--multi-dex",
          "--main-dex-list-path",
          rebase_path(_main_dex_list_path, root_build_dir),
        ]
        deps += [ ":${_main_dex_list_target_name}" ]
        inputs += [ _main_dex_list_path ]
      }

      if (defined(invoker.args)) {
        args += invoker.args
      }

      if (defined(invoker.sources)) {
        args += rebase_path(invoker.sources, root_build_dir)
      }
    }
  }

  template("process_java_prebuilt") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    assert(invoker.build_config != "")
    _build_config = invoker.build_config
    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    assert(_rebased_build_config != "" || true)  # Mark used.

    _input_jar_path = invoker.input_jar_path
    _output_jar_path = invoker.output_jar_path

    _jar_excluded_patterns = []
    if (defined(invoker.jar_excluded_patterns)) {
      _jar_excluded_patterns = invoker.jar_excluded_patterns
    }
    _strip_resource_classes = defined(invoker.strip_resource_classes) &&
                              invoker.strip_resource_classes
    _filter_jar = _jar_excluded_patterns != [] || _strip_resource_classes

    _proguard_preprocess =
        defined(invoker.proguard_preprocess) && invoker.proguard_preprocess

    _enable_assert =
        defined(invoker.supports_android) && invoker.supports_android &&
        (is_java_debug || dcheck_always_on)

    _retrolambda = defined(invoker.supports_android) &&
                   invoker.supports_android && use_java8

    _deps = []
    _previous_output_jar = _input_jar_path

    if (_filter_jar) {
      _filter_target = "${target_name}__filter"
      _filter_input_jar = _previous_output_jar
      _filter_output_jar = "$target_out_dir/$target_name-filtered.jar"

      action(_filter_target) {
        script = "//build/android/gyp/jar.py"
        deps = _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        inputs = [
          _build_config,
          _filter_input_jar,
        ]
        outputs = [
          _filter_output_jar,
        ]
        args = [
          "--input-jar",
          rebase_path(_filter_input_jar, root_build_dir),
          "--jar-path",
          rebase_path(_filter_output_jar, root_build_dir),
          "--excluded-classes=$_jar_excluded_patterns",
        ]
        if (_strip_resource_classes) {
          args += [ "--strip-resource-classes-for=@FileArg($_rebased_build_config:javac:resource_packages)" ]
        }
      }

      _deps = []
      _deps = [ ":$_filter_target" ]
      _previous_output_jar = _filter_output_jar
    }

    if (_proguard_preprocess) {
      _proguard_target = "${target_name}__proguard_process"
      _proguard_input_jar = _previous_output_jar
      _proguard_output_jar = "$target_out_dir/$target_name-proguarded.jar"
      _proguard_config_path = invoker.proguard_preprocess_config

      proguard(_proguard_target) {
        deps = _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        inputs = [
          _build_config,
          _proguard_config_path,
          _proguard_input_jar,
        ]
        output_jar_path = _proguard_output_jar

        _rebased_input_paths =
            [ rebase_path(_proguard_input_jar, root_build_dir) ]
        _rebased_proguard_configs =
            [ rebase_path(_proguard_config_path, root_build_dir) ]
        args = [
          "--input-paths=$_rebased_input_paths",
          "--proguard-configs=$_rebased_proguard_configs",
          "--classpath=@FileArg($_rebased_build_config:javac:classpath)",
        ]
      }

      _deps = []
      _deps = [ ":$_proguard_target" ]
      _previous_output_jar = _proguard_output_jar
    }

    if (_enable_assert) {
      _assert_target = "${target_name}__assert"
      _assert_input_jar = _previous_output_jar
      _assert_output_jar = "$target_out_dir/$target_name-asserted.jar"

      action(_assert_target) {
        script = "$root_build_dir/bin/helper/java_assertion_enabler"
        deps = [
          "//build/android/java_assertion_enabler($default_toolchain)",
        ]
        deps += _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        inputs = [
          _assert_input_jar,
        ]
        outputs = [
          _assert_output_jar,
        ]
        args = [
          rebase_path(_assert_input_jar, root_build_dir),
          rebase_path(_assert_output_jar, root_build_dir),
        ]
      }

      _deps = []
      _deps = [ ":$_assert_target" ]
      _previous_output_jar = _assert_output_jar
    }

    if (_retrolambda) {
      _retrolambda_target = "${target_name}__retrolambda"
      _retrolambda_input_jar = _previous_output_jar
      _retrolambda_output_jar = "$target_out_dir/$target_name-retrolambda.jar"

      android_sdk_jar = "$android_sdk/android.jar"
      action(_retrolambda_target) {
        script = "//build/android/gyp/retrolambda.py"
        deps = _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        inputs = [
          _build_config,
          _retrolambda_input_jar,
        ]
        outputs = [
          _retrolambda_output_jar,
        ]
        args = [
          "--input-jar",
          rebase_path(_retrolambda_input_jar, root_build_dir),
          "--output-jar",
          rebase_path(_retrolambda_output_jar, root_build_dir),
          "--classpath=@FileArg($_rebased_build_config:javac:classpath)",
          "--android-sdk-jar",
          rebase_path(android_sdk_jar, root_build_dir),
        ]
      }

      _deps = []
      _deps = [ ":$_retrolambda_target" ]
      _previous_output_jar = _retrolambda_output_jar
    }

    _output_jar_target = "${target_name}__copy"
    copy(_output_jar_target) {
      deps = _deps
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      if (defined(invoker.public_deps)) {
        public_deps = invoker.public_deps
      }
      sources = [
        _previous_output_jar,
      ]
      outputs = [
        _output_jar_path,
      ]
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "visibility",
                             ])
      public_deps = [
        ":$_output_jar_target",
      ]
    }
  }

  template("emma_instr") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                             ])

      _coverage_file = "$target_out_dir/${target_name}.em"
      _source_dirs_listing_file = "$target_out_dir/${target_name}_sources.txt"
      _emma_jar = "${android_sdk_root}/tools/lib/emma.jar"

      script = "//build/android/gyp/emma_instr.py"
      depfile = "${target_gen_dir}/${target_name}.d"
      inputs = invoker.java_files + [
                 _emma_jar,
                 invoker.input_jar_path,
               ]
      outputs = [
        _coverage_file,
        _source_dirs_listing_file,
        invoker.output_jar_path,
      ]
      args = [
        "instrument_jar",
        "--input-path",
        rebase_path(invoker.input_jar_path, root_build_dir),
        "--output-path",
        rebase_path(invoker.output_jar_path, root_build_dir),
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--coverage-file",
        rebase_path(_coverage_file, root_build_dir),
        "--sources-list-file",
        rebase_path(_source_dirs_listing_file, root_build_dir),
        "--src-root",
        rebase_path("//", root_build_dir),
        "--emma-jar",
        rebase_path(_emma_jar, root_build_dir),
      ]
      _rebased_java_sources_file =
          rebase_path(invoker.java_sources_file, root_build_dir)
      args += [ "--java-sources-file=$_rebased_java_sources_file" ]

      if (emma_filter != "") {
        args += [
          "--filter-string",
          emma_filter,
        ]
      }
    }
  }

  # Creates an unsigned .apk.
  #
  # Variables
  #   assets_build_config: Path to android_apk .build_config containing merged
  #       asset information.
  #   deps: Specifies the dependencies of this target.
  #   dex_path: Path to classes.dex file to include (optional).
  #   resource_packaged_apk_path: Path to .ap_ to use.
  #   output_apk_path: Output path for the generated .apk.
  #   native_lib_placeholders: List of placeholder filenames to add to the apk
  #     (optional).
  #   native_libs: List of native libraries.
  #   native_libs_filearg: @FileArg() of additionaly native libraries.
  #   write_asset_list: Adds an extra file to the assets, which contains a list of
  #     all other asset files.
  template("package_apk") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "public_deps",
                               "testonly",
                             ])
      _native_lib_placeholders = []
      if (defined(invoker.native_lib_placeholders)) {
        _native_lib_placeholders = invoker.native_lib_placeholders
      }

      script = "//build/android/gyp/apkbuilder.py"
      depfile = "$target_gen_dir/$target_name.d"
      data_deps = [
        "//tools/android/md5sum",
      ]  # Used when deploying APKs

      inputs = invoker.native_libs + [ invoker.resource_packaged_apk_path ]
      if (defined(invoker.dex_path)) {
        inputs += [ invoker.dex_path ]
      }

      outputs = [
        invoker.output_apk_path,
      ]

      _rebased_resource_packaged_apk_path =
          rebase_path(invoker.resource_packaged_apk_path, root_build_dir)
      _rebased_packaged_apk_path =
          rebase_path(invoker.output_apk_path, root_build_dir)
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--resource-apk=$_rebased_resource_packaged_apk_path",
        "--output-apk=$_rebased_packaged_apk_path",
      ]
      if (defined(invoker.assets_build_config)) {
        inputs += [ invoker.assets_build_config ]
        _rebased_build_config =
            rebase_path(invoker.assets_build_config, root_build_dir)
        args += [
          "--assets=@FileArg($_rebased_build_config:assets)",
          "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
        ]
      }
      if (defined(invoker.write_asset_list) && invoker.write_asset_list) {
        args += [ "--write-asset-list" ]
      }
      if (defined(invoker.dex_path)) {
        _rebased_dex_path = rebase_path(invoker.dex_path, root_build_dir)
        args += [ "--dex-file=$_rebased_dex_path" ]
      }
      if (invoker.native_libs != [] || defined(invoker.native_libs_filearg) ||
          _native_lib_placeholders != []) {
        args += [ "--android-abi=$android_app_abi" ]
      }
      if (invoker.native_libs != []) {
        _rebased_native_libs = rebase_path(invoker.native_libs, root_build_dir)
        args += [ "--native-libs=$_rebased_native_libs" ]
      }
      if (defined(invoker.native_libs_filearg)) {
        args += [ "--native-libs=${invoker.native_libs_filearg}" ]
      }
      if (_native_lib_placeholders != []) {
        args += [ "--native-lib-placeholders=$_native_lib_placeholders" ]
      }

      # TODO (michaelbai): Remove the secondary_native_libs variable.
      if (defined(invoker.secondary_abi_native_libs_filearg)) {
        assert(defined(android_app_secondary_abi))
        args += [
          "--secondary-native-libs=${invoker.secondary_abi_native_libs_filearg}",
          "--secondary-android-abi=$android_app_secondary_abi",
        ]
      } else if (defined(invoker.secondary_native_libs) &&
                 invoker.secondary_native_libs != []) {
        assert(defined(android_app_secondary_abi))
        inputs += invoker.secondary_native_libs
        _secondary_native_libs = rebase_path(invoker.secondary_native_libs)
        args += [
          "--secondary-native-libs=$_secondary_native_libs",
          "--secondary-android-abi=$android_app_secondary_abi",
        ]
      }

      if (defined(invoker.emma_instrument) && invoker.emma_instrument) {
        _emma_device_jar = "$android_sdk_root/tools/lib/emma_device.jar"
        _rebased_emma_device_jar = rebase_path(_emma_device_jar, root_build_dir)
        args += [ "--emma-device-jar=$_rebased_emma_device_jar" ]
      }

      if (defined(invoker.uncompress_shared_libraries) &&
          invoker.uncompress_shared_libraries) {
        args += [ "--uncompress-shared-libraries" ]
      }
    }
  }

  # Signs & zipaligns an apk.
  #
  # Variables
  #   input_apk_path: Path of the .apk to be finalized.
  #   output_apk_path: Output path for the generated .apk.
  #   keystore_path: Path to keystore to use for signing.
  #   keystore_name: Key alias to use.
  #   keystore_password: Keystore password.
  #   rezip_apk: Whether to add crazy-linker alignment.
  template("finalize_apk") {
    action(target_name) {
      deps = []
      script = "//build/android/gyp/finalize_apk.py"
      depfile = "$target_gen_dir/$target_name.d"
      forward_variables_from(invoker,
                             [
                               "deps",
                               "data_deps",
                               "public_deps",
                               "testonly",
                             ])

      sources = [
        invoker.input_apk_path,
      ]
      inputs = [
        invoker.keystore_path,
      ]
      outputs = [
        invoker.output_apk_path,
      ]
      data = [
        invoker.output_apk_path,
      ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--zipalign-path",
        rebase_path(zipalign_path, root_build_dir),
        "--unsigned-apk-path",
        rebase_path(invoker.input_apk_path, root_build_dir),
        "--final-apk-path",
        rebase_path(invoker.output_apk_path, root_build_dir),
        "--key-path",
        rebase_path(invoker.keystore_path, root_build_dir),
        "--key-name",
        invoker.keystore_name,
        "--key-passwd",
        invoker.keystore_password,
      ]
      if (defined(invoker.rezip_apk) && invoker.rezip_apk) {
        deps += [ "//build/android/rezip" ]
        _rezip_jar_path = "$root_build_dir/lib.java/rezip_apk.jar"
        args += [
          "--load-library-from-zip=1",
          "--rezip-apk-jar-path",
          rebase_path(_rezip_jar_path, root_build_dir),
        ]
      }

      if (defined(invoker.page_align_shared_libraries) &&
          invoker.page_align_shared_libraries) {
        args += [ "--page-align-shared-libraries" ]
      }
    }
  }

  # Packages resources, assets, dex, and native libraries into an apk. Signs and
  # zipaligns the apk.
  template("create_apk") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    _android_manifest = invoker.android_manifest
    _base_path = invoker.base_path
    _final_apk_path = invoker.apk_path
    _incremental_final_apk_path_helper =
        process_file_template(
            [ _final_apk_path ],
            "{{source_dir}}/{{source_name_part}}_incremental.apk")
    _incremental_final_apk_path = _incremental_final_apk_path_helper[0]

    if (defined(invoker.resources_zip)) {
      _resources_zip = invoker.resources_zip
      assert(_resources_zip != "")  # Mark as used.
    }
    if (defined(invoker.dex_path)) {
      _dex_path = invoker.dex_path
    }
    _load_library_from_apk = invoker.load_library_from_apk

    _deps = []
    if (defined(invoker.deps)) {
      _deps = invoker.deps
    }
    _incremental_deps = []
    if (defined(invoker.incremental_deps)) {
      _incremental_deps = invoker.incremental_deps
    }
    _native_libs = []
    if (defined(invoker.native_libs)) {
      _native_libs = invoker.native_libs
    }
    _native_libs_even_when_incremental = []
    if (defined(invoker.native_libs_even_when_incremental)) {
      _native_libs_even_when_incremental =
          invoker.native_libs_even_when_incremental
    }

    _version_code = invoker.version_code
    _version_name = invoker.version_name
    assert(_version_code != -1)  # Mark as used.
    assert(_version_name != "")  # Mark as used.

    _base_apk_path = _base_path + ".apk_intermediates"

    _resource_packaged_apk_path = _base_apk_path + ".ap_"
    _incremental_resource_packaged_apk_path =
        _base_apk_path + "_incremental.ap_"
    _packaged_apk_path = _base_apk_path + ".unfinished.apk"
    _incremental_packaged_apk_path =
        _base_apk_path + "_incremental.unfinished.apk"
    _shared_resources =
        defined(invoker.shared_resources) && invoker.shared_resources
    assert(_shared_resources || true)  # Mark as used.
    _app_as_shared_lib =
        defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib
    assert(_app_as_shared_lib || true)  # Mark as used.
    assert(!(_shared_resources && _app_as_shared_lib))

    _keystore_path = invoker.keystore_path
    _keystore_name = invoker.keystore_name
    _keystore_password = invoker.keystore_password

    _split_densities = []
    if (defined(invoker.create_density_splits) &&
        invoker.create_density_splits) {
      _split_densities = [
        "hdpi",
        "xhdpi",
        "xxhdpi",
        "xxxhdpi",
        "tvdpi",
      ]
    }

    _split_languages = []
    if (defined(invoker.language_splits)) {
      _split_languages = invoker.language_splits
    }

    template("package_resources_helper") {
      action(target_name) {
        deps = invoker.deps

        script = "//build/android/gyp/package_resources.py"
        depfile = "${target_gen_dir}/${target_name}.d"
        inputs = [
          invoker.android_manifest,
        ]
        if (defined(_resources_zip)) {
          inputs += [ _resources_zip ]
        }
        outputs = [
          invoker.resource_packaged_apk_path,
        ]

        if (defined(invoker.android_aapt_path)) {
          _android_aapt_path = invoker.android_aapt_path
        } else {
          _android_aapt_path = android_default_aapt_path
        }

        if (defined(invoker.alternative_android_sdk_jar)) {
          _rebased_android_sdk_jar =
              rebase_path(invoker.alternative_android_sdk_jar)
        } else {
          _rebased_android_sdk_jar = rebased_android_sdk_jar
        }

        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--android-sdk-jar",
          _rebased_android_sdk_jar,
          "--aapt-path",
          _android_aapt_path,
          "--configuration-name=$android_configuration_name",
          "--android-manifest",
          rebase_path(invoker.android_manifest, root_build_dir),
          "--version-code",
          _version_code,
          "--version-name",
          _version_name,
          "--apk-path",
          rebase_path(invoker.resource_packaged_apk_path, root_build_dir),
        ]

        if (defined(_resources_zip)) {
          args += [
            "--resource-zips",
            rebase_path(_resources_zip, root_build_dir),
          ]
        }
        if (_shared_resources) {
          args += [ "--shared-resources" ]
        }
        if (_app_as_shared_lib) {
          args += [ "--app-as-shared-lib" ]
        }
        if (_split_densities != []) {
          args += [ "--create-density-splits" ]
          foreach(_density, _split_densities) {
            outputs += [ "${invoker.resource_packaged_apk_path}_${_density}" ]
          }
        }
        if (_split_languages != []) {
          args += [ "--language-splits=$_split_languages" ]
          foreach(_language, _split_languages) {
            outputs += [ "${invoker.resource_packaged_apk_path}_${_language}" ]
          }
        }
        if (defined(invoker.extensions_to_not_compress)) {
          args += [
            "--no-compress",
            invoker.extensions_to_not_compress,
          ]
        }
      }
    }

    _package_resources_target_name = "${target_name}__package_resources"
    package_resources_helper(_package_resources_target_name) {
      forward_variables_from(invoker,
                             [
                               "alternative_android_sdk_jar",
                               "android_aapt_path",
                               "extensions_to_not_compress",
                             ])
      deps = _deps
      android_manifest = _android_manifest
      resource_packaged_apk_path = _resource_packaged_apk_path
    }

    _generate_incremental_manifest_target_name =
        "${target_name}_incremental_generate_manifest"
    _incremental_android_manifest =
        get_label_info(_generate_incremental_manifest_target_name,
                       "target_gen_dir") + "/AndroidManifest.xml"
    action(_generate_incremental_manifest_target_name) {
      deps = _incremental_deps
      script =
          "//build/android/incremental_install/generate_android_manifest.py"
      depfile = "${target_gen_dir}/${target_name}.d"
      inputs = [
        _android_manifest,
      ]
      outputs = [
        _incremental_android_manifest,
      ]

      _rebased_src_manifest = rebase_path(_android_manifest, root_build_dir)
      _rebased_incremental_manifest =
          rebase_path(_incremental_android_manifest, root_build_dir)
      _rebased_depfile = rebase_path(depfile, root_build_dir)
      args = [
        "--src-manifest=$_rebased_src_manifest",
        "--out-manifest=$_rebased_incremental_manifest",
        "--depfile=$_rebased_depfile",
      ]
      if (disable_incremental_isolated_processes) {
        args += [ "--disable-isolated-processes" ]
      }
    }

    _incremental_package_resources_target_name =
        "${target_name}_incremental__package_resources"

    # TODO(agrieve): See if we can speed up this step by swapping the manifest
    # from the result of the main package_resources step.
    package_resources_helper(_incremental_package_resources_target_name) {
      forward_variables_from(invoker,
                             [
                               "alternative_android_sdk_jar",
                               "android_aapt_path",
                               "extensions_to_not_compress",
                             ])
      deps =
          _incremental_deps + [ ":$_generate_incremental_manifest_target_name" ]
      android_manifest = _incremental_android_manifest
      resource_packaged_apk_path = _incremental_resource_packaged_apk_path
    }

    package_target = "${target_name}__package"
    package_apk(package_target) {
      forward_variables_from(invoker,
                             [
                               "assets_build_config",
                               "emma_instrument",
                               "native_lib_placeholders",
                               "native_libs_filearg",
                               "secondary_abi_native_libs_filearg",
                               "secondary_native_libs",
                               "uncompress_shared_libraries",
                               "write_asset_list",
                             ])
      deps = _deps + [ ":${_package_resources_target_name}" ]
      native_libs = _native_libs + _native_libs_even_when_incremental

      if (defined(_dex_path)) {
        dex_path = _dex_path
      }

      output_apk_path = _packaged_apk_path
      resource_packaged_apk_path = _resource_packaged_apk_path
    }

    _incremental_package_target = "${target_name}_incremental__package"
    package_apk(_incremental_package_target) {
      forward_variables_from(invoker,
                             [
                               "assets_build_config",
                               "emma_instrument",
                               "secondary_native_libs",
                               "uncompress_shared_libraries",
                             ])
      _dex_target = "//build/android/incremental_install:bootstrap_java__dex"
      deps = _incremental_deps + [
               ":${_incremental_package_resources_target_name}",
               _dex_target,
             ]

      if (defined(_dex_path)) {
        dex_path =
            get_label_info(_dex_target, "target_gen_dir") + "/bootstrap.dex"
      }

      native_libs = _native_libs_even_when_incremental

      # http://crbug.com/384638
      _has_native_libs =
          defined(invoker.native_libs_filearg) || _native_libs != []
      if (_has_native_libs && _native_libs_even_when_incremental == []) {
        native_lib_placeholders = [ "libfix.crbug.384638.so" ]
      }

      output_apk_path = _incremental_packaged_apk_path
      resource_packaged_apk_path = _incremental_resource_packaged_apk_path
    }

    _finalize_apk_rule_name = "${target_name}__finalize"
    finalize_apk(_finalize_apk_rule_name) {
      forward_variables_from(invoker, [ "page_align_shared_libraries" ])

      input_apk_path = _packaged_apk_path
      output_apk_path = _final_apk_path
      keystore_path = _keystore_path
      keystore_name = _keystore_name
      keystore_password = _keystore_password
      rezip_apk = _load_library_from_apk

      public_deps = [
        # Generator of the _packaged_apk_path this target takes as input.
        ":$package_target",
      ]
    }

    _incremental_finalize_apk_rule_name = "${target_name}_incremental__finalize"
    finalize_apk(_incremental_finalize_apk_rule_name) {
      input_apk_path = _incremental_packaged_apk_path
      output_apk_path = _incremental_final_apk_path
      keystore_path = _keystore_path
      keystore_name = _keystore_name
      keystore_password = _keystore_password

      public_deps = [
        ":$_incremental_package_target",
      ]
    }

    _split_deps = []

    template("finalize_split") {
      finalize_apk(target_name) {
        _config = invoker.split_config
        _type = invoker.split_type
        input_apk_path = "${_resource_packaged_apk_path}_${_config}"
        _output_paths = process_file_template(
                [ _final_apk_path ],
                "{{source_dir}}/{{source_name_part}}-${_type}-${_config}.apk")
        output_apk_path = _output_paths[0]
        keystore_path = _keystore_path
        keystore_name = _keystore_name
        keystore_password = _keystore_password
        deps = [
          ":${_package_resources_target_name}",
        ]
      }
    }

    foreach(_split, _split_densities) {
      _split_rule = "${target_name}__finalize_${_split}_split"
      finalize_split(_split_rule) {
        split_type = "density"
        split_config = _split
      }
      _split_deps += [ ":$_split_rule" ]
    }
    foreach(_split, _split_languages) {
      _split_rule = "${target_name}__finalize_${_split}_split"
      finalize_split(_split_rule) {
        split_type = "lang"
        split_config = _split
      }
      _split_deps += [ ":$_split_rule" ]
    }

    group(target_name) {
      public_deps = [ ":${_finalize_apk_rule_name}" ] + _split_deps
    }
    group("${target_name}_incremental") {
      public_deps = [ ":${_incremental_finalize_apk_rule_name}" ] + _split_deps
    }
  }

  template("java_prebuilt_impl") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])
    _supports_android =
        defined(invoker.supports_android) && invoker.supports_android

    assert(defined(invoker.jar_path))
    if (defined(invoker.output_name)) {
      _output_name = invoker.output_name
    } else {
      _output_name = get_path_info(invoker.jar_path, "name")
    }
    _base_path = "${target_gen_dir}/$target_name"

    # Jar files can be needed at runtime (by Robolectric tests or java binaries),
    # so do not put them under gen/.
    _target_dir_name = get_label_info(":$target_name", "dir")
    _jar_path = "$root_out_dir/lib.java$_target_dir_name/$_output_name.jar"
    _ijar_path =
        "$root_out_dir/lib.java$_target_dir_name/$_output_name.interface.jar"
    _build_config = _base_path + ".build_config"

    if (_supports_android) {
      _dex_path = _base_path + ".dex.jar"
    }
    _deps = []
    if (defined(invoker.deps)) {
      _deps = invoker.deps
    }
    _jar_deps = []
    if (defined(invoker.jar_dep)) {
      _jar_deps = [ invoker.jar_dep ]
    }

    _template_name = target_name

    _build_config_target_name = "${_template_name}__build_config"
    _process_jar_target_name = "${_template_name}__process_jar"
    _ijar_target_name = "${_template_name}__ijar"
    if (_supports_android) {
      _dex_target_name = "${_template_name}__dex"
    }

    write_build_config(_build_config_target_name) {
      type = "java_prebuilt"
      is_prebuilt_binary = defined(invoker.main_class)
      forward_variables_from(invoker,
                             [
                               "input_jars_paths",
                               "proguard_configs",
                             ])
      supports_android = _supports_android
      requires_android =
          defined(invoker.requires_android) && invoker.requires_android

      if (defined(invoker.deps)) {
        possible_config_deps = _deps
      }
      build_config = _build_config
      jar_path = _jar_path
      if (_supports_android) {
        dex_path = _dex_path
      }
    }

    process_java_prebuilt(_process_jar_target_name) {
      forward_variables_from(invoker,
                             [
                               "jar_excluded_patterns",
                               "proguard_preprocess",
                               "proguard_preprocess_config",
                               "strip_resource_classes",
                             ])

      visibility = [
        ":$_ijar_target_name",
        ":$_template_name",
      ]
      if (_supports_android) {
        visibility += [ ":$_dex_target_name" ]
      }

      supports_android = _supports_android
      build_config = _build_config
      input_jar_path = invoker.jar_path
      output_jar_path = _jar_path

      deps = [ ":$_build_config_target_name" ] + _deps + _jar_deps
    }

    generate_interface_jar(_ijar_target_name) {
      if (!defined(invoker.proguard_preprocess) ||
          !invoker.proguard_preprocess) {
        # Always used the unfiltered .jar to create the interface jar so that
        # other targets will resolve filtered classes when depending on
        # BuildConfig, NativeLibraries, etc.
        input_jar = invoker.jar_path
        deps = _deps + _jar_deps
      } else {
        # However, still apply pre-proguarding, since ignoring that can break
        # compiles.
        input_jar = _jar_path
        deps = [
          ":$_process_jar_target_name",
        ]
      }

      output_jar = _ijar_path
    }

    if (_supports_android) {
      dex(_dex_target_name) {
        sources = [
          _jar_path,
        ]
        output = _dex_path
        deps = [ ":$_process_jar_target_name" ] + _deps + _jar_deps
      }
    }

    if (defined(invoker.main_class)) {
      _binary_script_target_name = "${_template_name}__java_binary_script"
      java_binary_script(_binary_script_target_name) {
        forward_variables_from(invoker,
                               [
                                 "bootclasspath",
                                 "deps",
                                 "main_class",
                                 "wrapper_script_args",
                               ])
        if (!defined(deps)) {
          deps = []
        }
        build_config = _build_config
        jar_path = _jar_path
        script_name = _template_name
        if (defined(invoker.wrapper_script_name)) {
          script_name = invoker.wrapper_script_name
        }
        deps += [ ":$_build_config_target_name" ]
      }
    }

    group(target_name) {
      forward_variables_from(invoker, [ "data_deps" ])
      public_deps = [
        ":$_ijar_target_name",
        ":$_process_jar_target_name",
      ]
      if (_supports_android) {
        public_deps += [ ":$_dex_target_name" ]
      }
      if (defined(invoker.main_class)) {
        # Some targets use the generated script while building, so make it a dep
        # rather than a data_dep.
        public_deps += [ ":$_binary_script_target_name" ]
      }
    }
  }

  # Compiles and jars a set of java files.
  #
  # Outputs:
  #  $jar_path.jar
  #  $jar_path.interface.jar
  #
  # Variables
  #   java_files: List of .java files to compile (same as exists in java_sources_file)
  #   java_sources_file: Path to file containing list of files to compile.
  #   chromium_code: If true, enable extra warnings.
  #   srcjar_deps: List of srcjar dependencies. The .java files contained in the
  #     dependencies srcjar outputs will be compiled and added to the output jar.
  #   jar_path: Use this to explicitly set the output jar path. Defaults to
  #     "${target_gen_dir}/${target_name}.jar.
  template("compile_java") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    assert(defined(invoker.build_config))
    assert(defined(invoker.jar_path))

    _build_config = invoker.build_config

    _chromium_code = false
    if (defined(invoker.chromium_code)) {
      _chromium_code = invoker.chromium_code
    }

    _supports_android = true
    if (defined(invoker.supports_android)) {
      _supports_android = invoker.supports_android
    }

    _requires_android =
        defined(invoker.requires_android) && invoker.requires_android

    _enable_errorprone = use_errorprone_java_compiler
    if (!_chromium_code) {
      _enable_errorprone = false
    } else if (defined(invoker.enable_errorprone)) {
      _enable_errorprone = invoker.enable_errorprone
    }

    _provider_configurations = []
    if (defined(invoker.provider_configurations)) {
      _provider_configurations = invoker.provider_configurations
    }

    _processors = []
    _enable_interface_jars_javac = true
    if (defined(invoker.processors_javac)) {
      _processors = invoker.processors_javac
      _enable_interface_jars_javac = _processors == []
    }

    _processor_args = []
    if (defined(invoker.processor_args_javac)) {
      _processor_args = invoker.processor_args_javac
    }

    _additional_jar_files = []
    if (defined(invoker.additional_jar_files)) {
      _additional_jar_files = invoker.additional_jar_files
    }

    if (defined(invoker.enable_incremental_javac_override)) {
      # Use invoker-specified override.
      _enable_incremental_javac = invoker.enable_incremental_javac_override
    } else {
      # Default to build arg if not overridden.
      _enable_incremental_javac = enable_incremental_javac
    }

    _manifest_entries = []
    if (defined(invoker.manifest_entries)) {
      _manifest_entries = invoker.manifest_entries
    }

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps += invoker.srcjar_deps
    }

    _java_srcjars = []
    if (defined(invoker.srcjars)) {
      _java_srcjars = invoker.srcjars
    }
    foreach(dep, _srcjar_deps) {
      _dep_gen_dir = get_label_info(dep, "target_gen_dir")
      _dep_name = get_label_info(dep, "name")
      _java_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
    }

    # Mark srcjar_deps as used.
    assert(_srcjar_deps == [] || true)

    _javac_target_name = "${target_name}__javac"
    _process_prebuilt_target_name = "${target_name}__process_prebuilt"
    _ijar_target_name = "${target_name}__ijar"
    _final_target_name = target_name

    _final_jar_path = invoker.jar_path
    _javac_jar_path = "$target_gen_dir/$target_name.javac.jar"
    _process_prebuilt_jar_path = _final_jar_path
    _final_ijar_path = get_path_info(_final_jar_path, "dir") + "/" +
                       get_path_info(_final_jar_path, "name") + ".interface.jar"

    _emma_instrument = defined(invoker.emma_instrument) &&
                       invoker.emma_instrument && invoker.java_files != []
    if (_emma_instrument) {
      _emma_instr_target_name = "${target_name}__emma_instr"
      _process_prebuilt_jar_path =
          "$target_gen_dir/$target_name.process_prebuilt.jar"
    }

    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    _rebased_jar_path = rebase_path(_javac_jar_path, root_build_dir)

    action(_javac_target_name) {
      script = "//build/android/gyp/javac.py"
      depfile = "$target_gen_dir/$target_name.d"
      deps = _srcjar_deps
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      outputs = [
        _javac_jar_path,
        _javac_jar_path + ".md5.stamp",
      ]
      sources = invoker.java_files + _java_srcjars
      inputs = [
        _build_config,
      ]
      if (invoker.java_files != []) {
        inputs += [ invoker.java_sources_file ]
      }

      _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
      _rebased_depfile = rebase_path(depfile, root_build_dir)
      args = [
        "--depfile=$_rebased_depfile",
        "--jar-path=$_rebased_jar_path",
        "--java-srcjars=$_rebased_java_srcjars",
        "--java-srcjars=@FileArg($_rebased_build_config:javac:srcjars)",
      ]
      if (_enable_interface_jars_javac) {
        args += [ "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)" ]
      } else {
        args +=
            [ "--classpath=@FileArg($_rebased_build_config:javac:classpath)" ]
      }
      if (_enable_incremental_javac) {
        args += [ "--incremental" ]
        deps += [ "//third_party/jmake($default_toolchain)" ]
        inputs += [ "$root_build_dir/bin/jmake" ]
        outputs += [ "${_javac_jar_path}.pdb" ]
      }
      if (_requires_android) {
        if (defined(invoker.alternative_android_sdk_ijar)) {
          deps += [ invoker.alternative_android_sdk_ijar_dep ]
          _android_sdk_ijar = invoker.alternative_android_sdk_ijar
        } else {
          deps += [ "//build/android:android_ijar" ]
          _android_sdk_ijar = "$root_out_dir/lib.java/android.interface.jar"
        }
        inputs += [ _android_sdk_ijar ]
        _rebased_android_sdk_ijar =
            rebase_path(_android_sdk_ijar, root_build_dir)
        args += [ "--bootclasspath=$_rebased_android_sdk_ijar" ]
      }
      if (use_java8) {
        args += [ "--java-version=1.8" ]
      } else if (_supports_android) {
        args += [ "--java-version=1.7" ]
      }
      foreach(e, _manifest_entries) {
        args += [ "--manifest-entry=" + e ]
      }
      if (_chromium_code) {
        args += [ "--chromium-code=1" ]
      }
      if (_enable_errorprone) {
        deps += [ "//third_party/errorprone:chromium_errorprone" ]
        args += [
          "--use-errorprone-path",
          "bin/chromium_errorprone",
        ]
      }
      foreach(e, _provider_configurations) {
        args += [ "--provider-configuration=" + rebase_path(e, root_build_dir) ]
      }
      foreach(e, _processors) {
        args += [ "--processor=" + e ]
      }
      foreach(e, _processor_args) {
        args += [ "--processor-arg=" + e ]
      }
      foreach(file_tuple, _additional_jar_files) {
        # Each element is of length two, [ path_to_file, path_to_put_in_jar ]
        inputs += [ file_tuple[0] ]
        args +=
            [ "--additional-jar-file=" + file_tuple[0] + ":" + file_tuple[1] ]
      }
      if (invoker.java_files != []) {
        args += [ "@" + rebase_path(invoker.java_sources_file, root_build_dir) ]
      }
    }

    process_java_prebuilt(_process_prebuilt_target_name) {
      forward_variables_from(invoker,
                             [
                               "jar_excluded_patterns",
                               "proguard_preprocess",
                               "proguard_preprocess_config",
                             ])
      supports_android = _supports_android
      build_config = _build_config
      input_jar_path = _javac_jar_path
      output_jar_path = _process_prebuilt_jar_path

      deps = [
        ":$_javac_target_name",
      ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
    }

    if (_emma_instrument) {
      emma_instr(_emma_instr_target_name) {
        forward_variables_from(invoker,
                               [
                                 "deps",
                                 "java_files",
                                 "java_sources_file",
                               ])

        input_jar_path = _process_prebuilt_jar_path
        output_jar_path = _final_jar_path

        if (!defined(deps)) {
          deps = []
        }
        deps += [ ":$_process_prebuilt_target_name" ]
      }
    }

    generate_interface_jar(_ijar_target_name) {
      if (!defined(invoker.proguard_preprocess) ||
          !invoker.proguard_preprocess) {
        # Always used the unfiltered .jar to create the interface jar so that
        # other targets will resolve filtered classes when depending on
        # BuildConfig, NativeLibraries, etc.
        input_jar = _javac_jar_path
        deps = [
          ":$_javac_target_name",
        ]
      } else {
        # However, still apply pre-proguarding, since ignoring that can break
        # compiles.
        input_jar = _process_prebuilt_jar_path
        deps = [
          ":$_process_prebuilt_target_name",
        ]
      }
      output_jar = _final_ijar_path
    }

    group(_final_target_name) {
      forward_variables_from(invoker, [ "visibility" ])
      public_deps = [
        ":$_ijar_target_name",
      ]
      if (_emma_instrument) {
        public_deps += [ ":$_emma_instr_target_name" ]
      } else {
        public_deps += [ ":$_process_prebuilt_target_name" ]
      }
    }
  }

  template("java_library_impl") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])
    _accumulated_deps = []
    if (defined(invoker.deps)) {
      _accumulated_deps = invoker.deps
    }

    assert(defined(invoker.java_files) || defined(invoker.srcjars) ||
           defined(invoker.srcjar_deps))
    _base_path = "$target_gen_dir/$target_name"

    if (defined(invoker.output_name)) {
      _output_name = invoker.output_name
    } else {
      _output_name = target_name
    }

    # Jar files can be needed at runtime (by Robolectric tests or java binaries),
    # so do not put them under gen/.
    target_dir_name = get_label_info(":$target_name", "dir")
    _jar_path = "$root_out_dir/lib.java$target_dir_name/$_output_name.jar"
    if (defined(invoker.jar_path)) {
      _jar_path = invoker.jar_path
    }
    _template_name = target_name

    _final_deps = []

    _supports_android =
        defined(invoker.supports_android) && invoker.supports_android
    _requires_android =
        defined(invoker.requires_android) && invoker.requires_android
    assert(_requires_android || true)  # Mark as used.
    _android_manifest = "//build/android/AndroidManifest.xml"
    if (defined(invoker.android_manifest)) {
      _android_manifest = invoker.android_manifest
    }
    assert(_android_manifest != "")  # Mark as used.

    if (defined(invoker.run_findbugs_override)) {
      _run_findbugs = invoker.run_findbugs_override
    } else {
      _run_findbugs = run_findbugs  # Default to build arg if not overridden.
    }
    assert(_run_findbugs || true)  # Mark as used.

    # Don't enable coverage, lint, findbugs unless the target has some
    # non-generated files.
    if (defined(invoker.chromium_code)) {
      _chromium_code = invoker.chromium_code
    } else {
      _chromium_code = defined(invoker.java_files) && invoker.java_files != []
      if (_chromium_code) {
        # Make chromium_code = false be the default for targets within
        # third_party which contain no chromium-namespaced java files.
        set_sources_assignment_filter([ "*\bthird_party\b*" ])
        sources = [
          get_label_info(":$target_name", "dir"),
        ]
        if (sources == []) {
          set_sources_assignment_filter([ "*\bchromium\b*" ])
          sources = invoker.java_files
          _chromium_code = invoker.java_files != sources
        }
        set_sources_assignment_filter([])
        sources = []
      }
    }

    _emma_never_instrument = !_chromium_code
    if (defined(invoker.emma_never_instrument)) {
      _emma_never_instrument = invoker.emma_never_instrument
    }
    assert(_emma_never_instrument || true)  # Mark as used
    _emma_instrument = emma_coverage && !_emma_never_instrument

    if (_supports_android) {
      _dex_path = _base_path + ".dex.jar"
      if (defined(invoker.dex_path)) {
        _dex_path = invoker.dex_path
      }
    }

    _java_files = []
    if (defined(invoker.java_files)) {
      _java_files += invoker.java_files
    }
    if (_java_files != []) {
      _java_sources_file = "$_base_path.sources"
      write_file(_java_sources_file, rebase_path(_java_files, root_build_dir))
    }

    # Define build_config_deps which will be a list of targets required to
    # build the _build_config.
    if (defined(invoker.override_build_config)) {
      _build_config = invoker.override_build_config
    } else {
      _build_config = _base_path + ".build_config"
      build_config_target_name = "${_template_name}__build_config"

      write_build_config(build_config_target_name) {
        forward_variables_from(invoker,
                               [
                                 "gradle_treat_as_prebuilt",
                                 "input_jars_paths",
                                 "main_class",
                                 "proguard_configs",
                               ])
        if (defined(invoker.is_java_binary) && invoker.is_java_binary) {
          type = "java_binary"
        } else {
          type = "java_library"
        }
        if (defined(invoker.deps)) {
          possible_config_deps = invoker.deps
        }
        supports_android = _supports_android
        requires_android = _requires_android
        bypass_platform_checks = defined(invoker.bypass_platform_checks) &&
                                 invoker.bypass_platform_checks

        build_config = _build_config
        jar_path = _jar_path
        if (_supports_android) {
          dex_path = _dex_path
        }
        if (_java_files != []) {
          java_sources_file = _java_sources_file
        }

        if (defined(invoker.srcjar_deps)) {
          bundled_srcjars = []
          foreach(d, invoker.srcjar_deps) {
            _dep_gen_dir = get_label_info(d, "target_gen_dir")
            _dep_name = get_label_info(d, "name")
            bundled_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
          }
        }
      }
      _accumulated_deps += [ ":$build_config_target_name" ]
    }

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps = invoker.srcjar_deps
    }

    _srcjars = []
    if (defined(invoker.srcjars)) {
      _srcjars = invoker.srcjars
    }

    assert(_java_files != [] || _srcjar_deps != [] || _srcjars != [])

    _compile_java_target = "${_template_name}__compile_java"
    _final_deps += [ ":$_compile_java_target" ]
    compile_java(_compile_java_target) {
      forward_variables_from(invoker,
                             [
                               "additional_jar_files",
                               "alternative_android_sdk_ijar",
                               "alternative_android_sdk_ijar_dep",
                               "dist_jar_path",
                               "enable_errorprone",
                               "enable_incremental_javac_override",
                               "jar_excluded_patterns",
                               "manifest_entries",
                               "processors_javac",
                               "processor_args_javac",
                               "proguard_preprocess_config",
                               "proguard_preprocess",
                               "provider_configurations",
                             ])
      jar_path = _jar_path
      build_config = _build_config
      java_files = _java_files
      if (_java_files != []) {
        java_sources_file = _java_sources_file
      }
      srcjar_deps = _srcjar_deps
      srcjars = _srcjars
      chromium_code = _chromium_code
      supports_android = _supports_android
      requires_android = _requires_android
      emma_instrument = _emma_instrument
      deps = _accumulated_deps
    }
    _accumulated_deps += [ ":$_compile_java_target" ]
    assert(_accumulated_deps != [])  # Mark used.

    if (defined(invoker.main_class)) {
      # Targets might use the generated script while building, so make it a dep
      # rather than a data_dep.
      _final_deps += [ ":${_template_name}__java_binary_script" ]
      java_binary_script("${_template_name}__java_binary_script") {
        forward_variables_from(invoker,
                               [
                                 "bootclasspath",
                                 "main_class",
                                 "wrapper_script_args",
                               ])
        build_config = _build_config
        jar_path = _jar_path
        script_name = _template_name
        if (defined(invoker.wrapper_script_name)) {
          script_name = invoker.wrapper_script_name
        }
        deps = _accumulated_deps
      }
    }

    _has_lint_target = false
    if (_supports_android) {
      if (_chromium_code) {
        _has_lint_target = true
        android_lint("${_template_name}__lint") {
          android_manifest = _android_manifest
          build_config = _build_config
          jar_path = _jar_path
          java_files = _java_files
          if (_java_files != []) {
            java_sources_file = _java_sources_file
          }
          deps = _accumulated_deps
        }

        if (_run_findbugs) {
          findbugs("${_template_name}__findbugs") {
            build_config = _build_config
            jar_path = _jar_path
            deps = _accumulated_deps
          }
        }

        # Use an intermediate group() rather as the data_deps target in order to
        # avoid lint artifacts showing up as runtime_deps (while still having lint
        # run in parallel to other targets).
        group("${_template_name}__analysis") {
          public_deps = [
            ":${_template_name}__lint",
          ]
          if (_run_findbugs) {
            public_deps += [ ":${_template_name}__findbugs" ]
          }
        }
      }

      _final_deps += [ ":${_template_name}__dex" ]
      dex("${_template_name}__dex") {
        sources = [
          _jar_path,
        ]
        output = _dex_path
        deps = [
          ":$_compile_java_target",
        ]
      }
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "visibility",
                             ])
      if (!defined(data_deps)) {
        data_deps = []
      }
      public_deps = _final_deps
      if (_has_lint_target) {
        data_deps += [ ":${_template_name}__analysis" ]
      }
    }
  }

  # Runs process_resources.py
  template("process_resources") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    zip_path = invoker.zip_path
    srcjar_path = invoker.srcjar_path
    r_text_path = invoker.r_text_path
    build_config = invoker.build_config
    android_manifest = invoker.android_manifest

    non_constant_id = true
    if (defined(invoker.generate_constant_ids) &&
        invoker.generate_constant_ids) {
      non_constant_id = false
    }

    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "visibility",
                             ])
      script = "//build/android/gyp/process_resources.py"

      depfile = "$target_gen_dir/$target_name.d"
      outputs = [
        zip_path,
        srcjar_path,
        r_text_path,
      ]

      _all_resource_dirs = []
      sources = []

      if (defined(invoker.resource_dirs)) {
        _all_resource_dirs += invoker.resource_dirs

        # Speed up "gn gen" by short-circuiting the empty directory.
        if (invoker.resource_dirs != [ "//build/android/ant/empty/res" ] &&
            invoker.resource_dirs != []) {
          _sources_build_rel =
              exec_script("//build/android/gyp/find.py",
                          rebase_path(invoker.resource_dirs, root_build_dir),
                          "list lines")
          sources += rebase_path(_sources_build_rel, ".", root_build_dir)
        }
      }

      if (defined(invoker.generated_resource_dirs)) {
        assert(defined(invoker.generated_resource_files))
        _all_resource_dirs += invoker.generated_resource_dirs
        sources += invoker.generated_resource_files
      }

      inputs = [
        build_config,
        android_manifest,
      ]

      _rebased_all_resource_dirs =
          rebase_path(_all_resource_dirs, root_build_dir)
      rebase_build_config = rebase_path(build_config, root_build_dir)

      if (defined(invoker.android_aapt_path)) {
        _android_aapt_path = invoker.android_aapt_path
      } else {
        _android_aapt_path = android_default_aapt_path
      }

      if (defined(invoker.alternative_android_sdk_jar)) {
        _rebased_android_sdk_jar =
            rebase_path(invoker.alternative_android_sdk_jar)
      } else {
        _rebased_android_sdk_jar = rebased_android_sdk_jar
      }

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--android-sdk-jar",
        _rebased_android_sdk_jar,
        "--aapt-path",
        _android_aapt_path,
        "--android-manifest",
        rebase_path(android_manifest, root_build_dir),
        "--resource-dirs=$_rebased_all_resource_dirs",
        "--srcjar-out",
        rebase_path(srcjar_path, root_build_dir),
        "--resource-zip-out",
        rebase_path(zip_path, root_build_dir),
        "--r-text-out",
        rebase_path(r_text_path, root_build_dir),
        "--dependencies-res-zips=@FileArg($rebase_build_config:resources:dependency_zips)",
        "--extra-res-packages=@FileArg($rebase_build_config:resources:extra_package_names)",
        "--extra-r-text-files=@FileArg($rebase_build_config:resources:extra_r_text_files)",
      ]

      if (non_constant_id) {
        args += [ "--non-constant-id" ]
      }

      if (defined(invoker.custom_package)) {
        args += [
          "--custom-package",
          invoker.custom_package,
        ]
      }

      if (defined(invoker.v14_skip) && invoker.v14_skip) {
        args += [ "--v14-skip" ]
      }

      if (defined(invoker.shared_resources) && invoker.shared_resources) {
        args += [ "--shared-resources" ]
      }

      if (defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib) {
        args += [ "--app-as-shared-lib" ]
      }

      if (defined(invoker.include_all_resources) &&
          invoker.include_all_resources) {
        args += [ "--include-all-resources" ]
      }

      if (defined(invoker.all_resources_zip_path)) {
        all_resources_zip = invoker.all_resources_zip_path
        outputs += [ all_resources_zip ]
        args += [
          "--all-resources-zip-out",
          rebase_path(all_resources_zip, root_build_dir),
        ]
      }

      if (defined(invoker.proguard_file)) {
        outputs += [ invoker.proguard_file ]
        args += [
          "--proguard-file",
          rebase_path(invoker.proguard_file, root_build_dir),
        ]
      }

      if (defined(invoker.args)) {
        args += invoker.args
      }
    }
  }

  # Produces a single .dex.jar out of a set of Java dependencies.
  template("deps_dex") {
    set_sources_assignment_filter([])
    build_config = "$target_gen_dir/${target_name}.build_config"
    build_config_target_name = "${target_name}__build_config"

    write_build_config(build_config_target_name) {
      forward_variables_from(invoker, [ "dex_path" ])
      if (defined(invoker.deps)) {
        possible_config_deps = invoker.deps
      }
      type = "deps_dex"
      build_config = build_config
    }

    rebased_build_config = rebase_path(build_config, root_build_dir)
    dex(target_name) {
      inputs = [
        build_config,
      ]
      output = invoker.dex_path
      dex_arg_key = "${rebased_build_config}:final_dex:dependency_dex_files"
      args = [ "--inputs=@FileArg($dex_arg_key)" ]
      if (defined(invoker.excluded_jars)) {
        excluded_jars = rebase_path(invoker.excluded_jars, root_build_dir)
        args += [ "--excluded-paths=${excluded_jars}" ]
      }
      deps = [
        ":$build_config_target_name",
      ]
    }
  }

  # Creates an AndroidManifest.xml for an APK split.
  template("generate_split_manifest") {
    assert(defined(invoker.main_manifest))
    assert(defined(invoker.out_manifest))
    assert(defined(invoker.split_name))

    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                             ])
      depfile = "$target_gen_dir/$target_name.d"
      args = [
        "--main-manifest",
        rebase_path(invoker.main_manifest, root_build_dir),
        "--out-manifest",
        rebase_path(invoker.out_manifest, root_build_dir),
        "--split",
        invoker.split_name,
      ]
      if (defined(invoker.version_code)) {
        args += [
          "--version-code",
          invoker.version_code,
        ]
      }
      if (defined(invoker.version_name)) {
        args += [
          "--version-name",
          invoker.version_name,
        ]
      }
      if (defined(invoker.has_code)) {
        args += [
          "--has-code",
          invoker.has_code,
        ]
      }
      args += [
        "--depfile",
        rebase_path(depfile, root_build_dir),
      ]

      script = "//build/android/gyp/generate_split_manifest.py"
      outputs = [
        invoker.out_manifest,
      ]
      inputs = [
        invoker.main_manifest,
      ]
    }
  }

  template("pack_relocation_section") {
    assert(defined(invoker.file_list_json))
    assert(defined(invoker.libraries_filearg))
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "public_deps",
                               "inputs",
                               "testonly",
                             ])
      script = "//build/android/gyp/pack_relocations.py"
      depfile = "$target_gen_dir/$target_name.d"
      _packed_libraries_dir = "$target_gen_dir/$target_name/packed-libs"
      outputs = [
        invoker.file_list_json,
      ]
      deps += [ relocation_packer_target ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--enable-packing=1",
        "--android-pack-relocations",
        rebase_path(relocation_packer_exe, root_build_dir),
        "--stripped-libraries-dir",
        rebase_path(root_build_dir, root_build_dir),
        "--packed-libraries-dir",
        rebase_path(_packed_libraries_dir, root_build_dir),
        "--libraries=${invoker.libraries_filearg}",
        "--filelistjson",
        rebase_path(invoker.file_list_json, root_build_dir),
      ]
    }
  }
}
