# 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.

import("//build/config/android/config.gni")

assert(is_android)

rebased_android_sdk = rebase_path(android_sdk, root_build_dir)
rebased_android_sdk_root = rebase_path(android_sdk_root, 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_aapt_path = "$rebased_android_sdk_build_tools/aapt"

template("android_lint") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

  jar_path = invoker.jar_path
  android_manifest = invoker.android_manifest
  java_files = invoker.java_files
  base_path = "$target_gen_dir/$target_name"

  action(target_name) {
    script = "//build/android/gyp/lint.py"
    result_path = base_path + "/result.xml"
    config_path = base_path + "/config.xml"
    suppressions_file = "//build/android/lint/suppressions.xml"
    inputs = [
               suppressions_file,
               android_manifest,
               jar_path,
             ] + java_files

    outputs = [
      config_path,
      result_path,
    ]

    rebased_java_files = rebase_path(java_files, root_build_dir)

    args = [
      "--lint-path=$rebased_android_sdk_root/tools/lint",
      "--config-path",
      rebase_path(suppressions_file, root_build_dir),
      "--manifest-path",
      rebase_path(android_manifest, root_build_dir),
      "--product-dir=.",
      "--jar-path",
      rebase_path(jar_path, root_build_dir),
      "--processed-config-path",
      rebase_path(config_path, root_build_dir),
      "--result-path",
      rebase_path(result_path, root_build_dir),
      "--java-files=$rebased_java_files",
      "--enable",
    ]

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
    if (defined(invoker.public_deps)) {
      public_deps = invoker.public_deps
    }
    if (defined(invoker.data_deps)) {
      data_deps = invoker.data_deps
    }
  }
}

template("findbugs") {
  jar_path = invoker.jar_path

  build_config = invoker.build_config

  action(target_name) {
    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(build_config, root_build_dir)

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }

    if (defined(invoker.testonly)) {
      testonly = invoker.testonly
    }

    inputs = [
      "//build/android/pylib/utils/findbugs.py",
      exclusions_file,
      jar_path,
    ]

    outputs = [
      depfile,
      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(jar_path, root_build_dir),
    ]
  }
}

template("dex") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

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

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }

    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 (defined(invoker.no_locals) && invoker.no_locals) {
      args += [ "--no-locals=1" ]
    }

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

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

# Creates a zip archive of the inputs.
# If base_dir is provided, the archive paths will be relative to it.
template("zip") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

  assert(defined(invoker.inputs))
  assert(defined(invoker.output))

  rebase_inputs = rebase_path(invoker.inputs, root_build_dir)
  rebase_output = rebase_path(invoker.output, root_build_dir)
  action(target_name) {
    script = "//build/android/gn/zip.py"
    depfile = "$target_gen_dir/$target_name.d"
    inputs = invoker.inputs
    outputs = [
      depfile,
      invoker.output,
    ]
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--inputs=$rebase_inputs",
      "--output=$rebase_output",
    ]
    if (defined(invoker.base_dir)) {
      args += [
        "--base-dir",
        rebase_path(invoker.base_dir, root_build_dir),
      ]
    }

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
    if (defined(invoker.public_deps)) {
      public_deps = invoker.public_deps
    }
    if (defined(invoker.data_deps)) {
      data_deps = invoker.data_deps
    }

    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }
  }
}

# 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") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

  assert(defined(invoker.type))
  assert(defined(invoker.build_config))

  type = invoker.type
  build_config = invoker.build_config

  assert(type == "android_apk" || type == "java_library" ||
         type == "android_resources" || type == "deps_dex")

  action(target_name) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }

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

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

    possible_deps_configs = []
    foreach(d, deps) {
      dep_gen_dir = get_label_info(d, "target_gen_dir")
      dep_name = get_label_info(d, "name")
      possible_deps_configs += [ "$dep_gen_dir/$dep_name.build_config" ]
    }
    rebase_possible_deps_configs =
        rebase_path(possible_deps_configs, root_build_dir)

    outputs = [
      depfile,
      build_config,
    ]

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

    is_java_library = type == "java_library"
    is_apk = type == "android_apk"
    is_android_resources = type == "android_resources"
    is_deps_dex = type == "deps_dex"

    supports_android = is_apk || is_android_resources || is_deps_dex ||
                       (is_java_library && defined(invoker.supports_android) &&
                        invoker.supports_android)
    requires_android = is_apk || is_android_resources || is_deps_dex ||
                       (is_java_library && 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_library || true)
    assert(is_apk || true)
    assert(is_android_resources || true)
    assert(is_deps_dex || true)

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

    if (is_apk || is_deps_dex || (is_java_library && 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 ]
      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_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_apk) {
      if (defined(invoker.native_libs)) {
        inputs += invoker.native_libs
        rebased_native_libs = rebase_path(invoker.native_libs, root_build_dir)
        rebased_android_readelf = rebase_path(android_readelf, root_build_dir)
        args += [
          "--native-libs=$rebased_native_libs",
          "--readelf-path=$rebased_android_readelf",
        ]
      }
    }

    if (defined(invoker.srcjar)) {
      args += [
        "--srcjar",
        rebase_path(invoker.srcjar, root_build_dir),
      ]
    }
  }
}

template("process_java_prebuilt") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

  _input_jar_path = invoker.input_jar_path
  _output_jar_path = invoker.output_jar_path
  _jar_toc_path = _output_jar_path + ".TOC"

  assert(invoker.build_config != "")

  if (defined(invoker.proguard_preprocess) && invoker.proguard_preprocess) {
    _proguard_jar_path = "$android_sdk_root/tools/proguard/lib/proguard.jar"
    _proguard_config_path = invoker.proguard_config
    _build_config = invoker.build_config
    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    _output_jar_target = "${target_name}__proguard_process"
    action(_output_jar_target) {
      script = "//build/android/gyp/proguard.py"
      inputs = [
        android_sdk_jar,
        _proguard_jar_path,
        _build_config,
        _input_jar_path,
        _proguard_config_path,
      ]
      depfile = "${target_gen_dir}/${target_name}.d"
      outputs = [
        depfile,
        _output_jar_path,
      ]
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--proguard-path",
        rebase_path(_proguard_jar_path, root_build_dir),
        "--input-path",
        rebase_path(_input_jar_path, root_build_dir),
        "--output-path",
        rebase_path(_output_jar_path, root_build_dir),
        "--proguard-config",
        rebase_path(_proguard_config_path, root_build_dir),
        "--classpath",
        rebased_android_sdk_jar,
        "--classpath=@FileArg($_rebased_build_config:javac:classpath)",
      ]

      if (defined(invoker.deps)) {
        deps = invoker.deps
      }
      if (defined(invoker.public_deps)) {
        public_deps = invoker.public_deps
      }
      if (defined(invoker.data_deps)) {
        data_deps = invoker.data_deps
      }
    }
  } else {
    _output_jar_target = "${target_name}__copy_jar"
    copy(_output_jar_target) {
      sources = [
        _input_jar_path,
      ]
      outputs = [
        _output_jar_path,
      ]

      if (defined(invoker.deps)) {
        deps = invoker.deps
      }
      if (defined(invoker.public_deps)) {
        public_deps = invoker.public_deps
      }
      if (defined(invoker.data_deps)) {
        data_deps = invoker.data_deps
      }
    }
  }

  action("${target_name}__jar_toc") {
    script = "//build/android/gyp/jar_toc.py"
    depfile = "$target_gen_dir/$target_name.d"
    outputs = [
      depfile,
      _jar_toc_path,
      _jar_toc_path + ".md5.stamp",
    ]
    inputs = [
      _output_jar_path,
    ]
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--jar-path",
      rebase_path(_output_jar_path, root_build_dir),
      "--toc-path",
      rebase_path(_jar_toc_path, root_build_dir),
    ]
    public_deps = [
      ":$_output_jar_target",
    ]
  }

  group(target_name) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }
    public_deps = [
      ":${target_name}__jar_toc",
      ":$_output_jar_target",
    ]
  }
}

template("finalize_apk") {
  action(target_name) {
    script = "//build/android/gyp/finalize_apk.py"
    depfile = "$target_gen_dir/$target_name.d"

    if (defined(invoker.testonly)) {
      testonly = invoker.testonly
    }

    sources = [
      invoker.input_apk_path,
    ]
    inputs = [
      invoker.keystore_path,
    ]
    outputs = [
      depfile,
      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) {
      _rezip_jar_path = "$root_build_dir/lib.java/rezip_apk.jar"
      inputs += [ _rezip_jar_path ]
      args += [
        "--load-library-from-zip=1",
        "--rezip-apk-jar-path",
        rebase_path(_rezip_jar_path, root_build_dir),
      ]
    }

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
    if (defined(invoker.public_deps)) {
      public_deps = invoker.public_deps
    }
    if (defined(invoker.data_deps)) {
      data_deps = invoker.data_deps
    }
  }
}

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

  _android_manifest = invoker.android_manifest
  _base_path = invoker.base_path
  _final_apk_path = invoker.apk_path

  if (defined(invoker.resources_zip)) {
    _resources_zip = invoker.resources_zip
  }
  if (defined(invoker.dex_path)) {
    _dex_path = invoker.dex_path
  }
  _load_library_from_apk = invoker.load_library_from_apk

  _package_deps = []
  if (defined(invoker.deps)) {
    _package_deps = invoker.deps
  }

  _native_libs_dir = "//build/android/empty/res"
  if (defined(invoker.native_libs_dir)) {
    _native_libs_dir = invoker.native_libs_dir
  }

  if (defined(invoker.asset_location)) {
    _asset_location = invoker.asset_location
  }

  _version_code = invoker.version_code
  _version_name = invoker.version_name

  _base_apk_path = _base_path + ".apk_intermediates"

  _resource_packaged_apk_path = _base_apk_path + ".ap_"
  _packaged_apk_path = _base_apk_path + ".unfinished.apk"
  _shared_resources =
      defined(invoker.shared_resources) && invoker.shared_resources

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

  _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
  }

  _package_resources_target_name = "${target_name}__package_resources"
  action(_package_resources_target_name) {
    deps = _package_deps

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

    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--android-sdk",
      rebased_android_sdk,
      "--aapt-path",
      android_aapt_path,
      "--configuration-name=$_configuration_name",
      "--android-manifest",
      rebase_path(_android_manifest, root_build_dir),
      "--version-code",
      _version_code,
      "--version-name",
      _version_name,
      "--apk-path",
      rebase_path(_resource_packaged_apk_path, root_build_dir),
    ]

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

  package_target = "${target_name}__package"
  action(package_target) {
    script = "//build/android/gyp/ant.py"
    _ant_script = "//build/android/ant/apk-package.xml"

    deps = [
      ":${_package_resources_target_name}",
    ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    depfile = "$target_gen_dir/$target_name.d"

    inputs = [
      _resource_packaged_apk_path,
      _ant_script,
    ]
    if (defined(_dex_path)) {
      inputs += [ _dex_path ]
    }

    outputs = [
      depfile,
      _packaged_apk_path,
    ]

    _rebased_emma_jar = ""
    _rebased_resource_packaged_apk_path =
        rebase_path(_resource_packaged_apk_path, root_build_dir)
    _rebased_packaged_apk_path = rebase_path(_packaged_apk_path, root_build_dir)
    _rebased_native_libs_dir = rebase_path(_native_libs_dir, root_build_dir)
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--",
      "-quiet",
      "-DANDROID_SDK_ROOT=$rebased_android_sdk_root",
      "-DANDROID_SDK_TOOLS=$rebased_android_sdk_build_tools",
      "-DRESOURCE_PACKAGED_APK_NAME=$_rebased_resource_packaged_apk_path",
      "-DCONFIGURATION_NAME=$_configuration_name",
      "-DNATIVE_LIBS_DIR=$_rebased_native_libs_dir",
      "-DOUT_DIR=",
      "-DUNSIGNED_APK_PATH=$_rebased_packaged_apk_path",
      "-DEMMA_INSTRUMENT=0",
      "-DEMMA_DEVICE_JAR=$_rebased_emma_jar",
      "-Dbasedir=.",
      "-buildfile",
      rebase_path(_ant_script, root_build_dir),
    ]
    if (defined(_dex_path)) {
      _rebased_dex_path = rebase_path(_dex_path, root_build_dir)
      args += [
        "-DDEX_FILE_PATH=$_rebased_dex_path",
        "-DHAS_CODE=true",
      ]
    } else {
      args += [ "-DHAS_CODE=false" ]
    }
  }

  _finalize_apk_rule_name = "${target_name}__finalize"
  finalize_apk(_finalize_apk_rule_name) {
    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",
    ]
  }

  _final_deps = [ ":${_finalize_apk_rule_name}" ]

  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
    }
    _final_deps += [ ":$_split_rule" ]
  }
  foreach(_split, _split_languages) {
    _split_rule = "${target_name}__finalize_${_split}_split"
    finalize_split(_split_rule) {
      split_type = "lang"
      split_config = _split
    }
    _final_deps += [ ":$_split_rule" ]
  }

  group(target_name) {
    public_deps = _final_deps
  }
}

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

  assert(defined(invoker.jar_path))
  _base_path = "${target_gen_dir}/$target_name"
  _jar_path = _base_path + ".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"
  if (_supports_android) {
    dex_target_name = "${_template_name}__dex"
  }

  write_build_config(build_config_target_name) {
    type = "java_library"
    supports_android = _supports_android
    requires_android =
        defined(invoker.requires_android) && invoker.requires_android

    deps = _deps
    build_config = _build_config
    jar_path = _jar_path
    if (_supports_android) {
      dex_path = _dex_path
    }
  }

  process_java_prebuilt(process_jar_target_name) {
    visibility = [ ":$_template_name" ]
    if (_supports_android) {
      visibility += [ ":$dex_target_name" ]
    }

    if (defined(invoker.proguard_preprocess) && invoker.proguard_preprocess) {
      proguard_preprocess = true
      proguard_config = invoker.proguard_config
    }

    build_config = _build_config
    input_jar_path = invoker.jar_path
    output_jar_path = _jar_path

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

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

  group(target_name) {
    deps = [
      ":$process_jar_target_name",
    ]
    if (defined(invoker.data_deps)) {
      data_deps = invoker.data_deps
    }
    if (_supports_android) {
      deps += [ ":$dex_target_name" ]
    }
  }
}

# Compiles and jars a set of java files.
#
# Outputs:
#  $jar_path.jar
#  $jar_path.jar.TOC
#
# Variables
#   java_files: List of .java files to compile.
#   java_deps: List of java dependencies. These should all have a .jar output
#     at "${target_gen_dir}/${target_name}.jar.
#   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([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

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

  _java_files = invoker.java_files
  _final_jar_path = invoker.jar_path
  _intermediate_jar_path = "$target_gen_dir/$target_name.initial.jar"

  _build_config = invoker.build_config

  _jar_excluded_patterns = []
  if (defined(invoker.jar_excluded_patterns)) {
    _jar_excluded_patterns += invoker.jar_excluded_patterns
  }

  _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
  }

  _enable_errorprone = use_errorprone_java_compiler
  if (defined(invoker.enable_errorprone)) {
    _enable_errorprone = invoker.enable_errorprone
  }

  _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)

  _system_jars = []
  if (defined(invoker.android) && invoker.android) {
    _system_jars += [ android_sdk_jar ]
  }

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

  javac_target_name = "${target_name}__javac"
  finish_target_name = "${target_name}__finish"
  final_target_name = target_name

  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 = [
      depfile,
      _intermediate_jar_path,
      _intermediate_jar_path + ".md5.stamp",
    ]
    sources = _java_files + _java_srcjars
    inputs = _system_jars + [ _build_config ]

    _rebased_system_jars = rebase_path(_system_jars, root_build_dir)
    _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
    _rebased_depfile = rebase_path(depfile, root_build_dir)
    args = [
      "--depfile=$_rebased_depfile",
      "--classpath=$_rebased_system_jars",
      "--classpath=@FileArg($_rebased_build_config:javac:classpath)",
      "--jar-path=$_rebased_jar_path",
      "--java-srcjars=$_rebased_java_srcjars",
      "--java-srcjars=@FileArg($_rebased_build_config:javac:srcjars)",
      "--jar-excluded-classes=$_jar_excluded_patterns",
    ]
    if (_supports_android) {
      _rebased_android_sdk_jar = rebase_path(android_sdk_jar, root_build_dir)
      args += [ "--bootclasspath=$_rebased_android_sdk_jar" ]
    }
    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",
      ]
    }
    args += rebase_path(_java_files, root_build_dir)
  }

  process_java_prebuilt(finish_target_name) {
    visibility = [ ":$final_target_name" ]

    build_config = _build_config
    input_jar_path = _intermediate_jar_path
    output_jar_path = _final_jar_path
    if (defined(invoker.proguard_preprocess) && invoker.proguard_preprocess) {
      proguard_preprocess = invoker.proguard_preprocess
      proguard_config = invoker.proguard_config
    }
    deps = [
      ":$javac_target_name",
    ]
  }

  group(final_target_name) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }
    public_deps = [
      ":$finish_target_name",
    ]
  }
}

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

  assert(
      defined(invoker.java_files) || defined(invoker.DEPRECATED_java_in_dir) ||
      defined(invoker.srcjars) || defined(invoker.srcjar_deps))
  _base_path = "$target_gen_dir/$target_name"
  _jar_path = _base_path + ".jar"
  if (defined(invoker.jar_path)) {
    _jar_path = invoker.jar_path
  }
  _template_name = target_name

  _final_deps = []
  _final_datadeps = []
  if (defined(invoker.datadeps)) {
    _final_datadeps = invoker.datadeps
  }

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

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

  # 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

    # When a custom build config file is specified, we need to use the deps
    # supplied by the invoker any time we reference the build config file.
    assert(defined(invoker.deps),
           "If you specify a build config file for " +
               "java_library_impl($target_name), you should " +
               "also specify the target that made it in the deps")
    build_config_deps = invoker.deps
  } else {
    _build_config = _base_path + ".build_config"
    build_config_target_name = "${_template_name}__build_config"
    build_config_deps = [ ":$build_config_target_name" ]

    write_build_config(build_config_target_name) {
      type = "java_library"
      supports_android = _supports_android
      requires_android = _requires_android
      bypass_platform_checks = defined(invoker.bypass_platform_checks) &&
                               invoker.bypass_platform_checks

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

      build_config = _build_config
      jar_path = _jar_path
      if (_supports_android) {
        dex_path = _dex_path
      }
    }
  }

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

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

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

  _java_files = []
  if (defined(invoker.java_files)) {
    _java_files = invoker.java_files
  } else if (defined(invoker.DEPRECATED_java_in_dir)) {
    _src_dir = invoker.DEPRECATED_java_in_dir + "/src"
    _src_dir_exists = exec_script("//build/dir_exists.py",
                                  [ rebase_path(_src_dir, root_build_dir) ],
                                  "string")
    assert(_src_dir_exists == "False",
           "In GN, java_in_dir should be the fully specified java directory " +
               "(i.e. including the trailing \"/src\")")

    _java_files_build_rel = exec_script(
            "//build/android/gyp/find.py",
            [
              "--pattern",
              "*.java",
              rebase_path(invoker.DEPRECATED_java_in_dir, root_build_dir),
            ],
            "list lines")
    _java_files = rebase_path(_java_files_build_rel, ".", root_build_dir)
  }
  assert(_java_files != [] || _srcjar_deps != [] || _srcjars != [])

  _compile_java_target = "${_template_name}__compile_java"
  _final_deps += [ ":$_compile_java_target" ]
  compile_java(_compile_java_target) {
    jar_path = _jar_path
    build_config = _build_config
    java_files = _java_files
    srcjar_deps = _srcjar_deps
    srcjars = _srcjars
    chromium_code = _chromium_code
    android = _requires_android

    if (defined(invoker.enable_errorprone)) {
      enable_errorprone = invoker.enable_errorprone
    }
    if (defined(invoker.jar_excluded_patterns)) {
      jar_excluded_patterns = invoker.jar_excluded_patterns
    }
    if (defined(invoker.proguard_preprocess)) {
      proguard_preprocess = invoker.proguard_preprocess
    }
    if (defined(invoker.proguard_config)) {
      proguard_config = invoker.proguard_config
    }
    if (defined(invoker.dist_jar_path)) {
      dist_jar_path = invoker.dist_jar_path
    }
    if (defined(invoker.manifest_entries)) {
      manifest_entries = invoker.manifest_entries
    }

    supports_android = _supports_android
    deps = build_config_deps
  }

  if (defined(invoker.main_class)) {
    _final_deps += [ ":${_template_name}__binary_script" ]
    action("${_template_name}__binary_script") {
      script = "//build/android/gyp/create_java_binary_script.py"
      depfile = "$target_gen_dir/$target_name.d"
      java_script = "$root_build_dir/bin/$_template_name"
      inputs = [
        _build_config,
      ]
      outputs = [
        depfile,
        java_script,
      ]
      _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:java:full_classpath)",
        "--jar-path",
        rebase_path(_jar_path, root_build_dir),
        "--main-class",
        invoker.main_class,
      ]

      deps = build_config_deps
    }
  }

  if (_supports_android) {
    if (defined(invoker.chromium_code) && invoker.chromium_code) {
      _android_manifest = "//build/android/AndroidManifest.xml"
      if (defined(invoker.android_manifest)) {
        _android_manifest = invoker.android_manifest
      }

      _final_datadeps += [ ":${_template_name}__lint" ]
      android_lint("${_template_name}__lint") {
        android_manifest = _android_manifest
        jar_path = _jar_path
        java_files = _java_files
        deps = [
          ":$_compile_java_target",
        ]
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
      }

      if (run_findbugs) {
        _final_datadeps += [ ":${_template_name}__findbugs" ]
        findbugs("${_template_name}__findbugs") {
          build_config = _build_config
          jar_path = _jar_path
          deps = build_config_deps
        }
      }
    }

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

  group(target_name) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }
    public_deps = _final_deps
    data_deps = _final_datadeps
  }
}

# Runs process_resources.py
template("process_resources") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

  zip_path = invoker.zip_path
  srcjar_path = invoker.srcjar_path
  r_text_path = invoker.r_text_path
  build_config = invoker.build_config
  resource_dirs = invoker.resource_dirs
  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) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }

    script = "//build/android/gyp/process_resources.py"

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

    sources_build_rel = exec_script("//build/android/gyp/find.py",
                                    rebase_path(resource_dirs, root_build_dir),
                                    "list lines")
    sources = rebase_path(sources_build_rel, ".", root_build_dir)

    inputs = [
      build_config,
      android_manifest,
    ]

    rebase_resource_dirs = rebase_path(resource_dirs, root_build_dir)
    rebase_build_config = rebase_path(build_config, root_build_dir)
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--android-sdk",
      rebase_path(android_sdk, root_build_dir),
      "--aapt-path",
      android_aapt_path,
      "--android-manifest",
      rebase_path(android_manifest, root_build_dir),
      "--resource-dirs=$rebase_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.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.args)) {
      args += invoker.args
    }
    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
  }
}

template("copy_ex") {
  set_sources_assignment_filter([])
  if (defined(invoker.testonly)) {
    testonly = invoker.testonly
  }

  action(target_name) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }

    script = "//build/android/gyp/copy_ex.py"

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }

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

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

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

    args = [
      "--depfile",
      rebase_path(depfile, 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
    }
  }
}

# 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) {
    type = "deps_dex"
    deps = invoker.deps

    build_config = build_config
    dex_path = invoker.dex_path
  }

  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) {
    depfile = "$target_gen_dir/$target_name.d"
    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
    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 = [
      depfile,
      invoker.out_manifest,
    ]
    inputs = [
      invoker.main_manifest,
    ]
  }
}

# Generates a script in the output 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

  action(target_name) {
    script = "//build/android/gyp/create_test_runner_script.py"
    depfile = "$target_gen_dir/$target_name.d"

    test_runner_args = [
      _test_type,
      "--output-directory",
      rebase_path(root_build_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") {
      assert(defined(invoker.test_apk))
      test_runner_args += [
        "--test-apk",
        invoker.test_apk,
      ]
      if (defined(invoker.support_apk_path)) {
        test_runner_args += [
          "--support-apk",
          rebase_path(invoker.support_apk_path, root_build_dir),
        ]
      }
    } else {
      assert(false, "Invalid test type: $_test_type.")
    }

    if (defined(invoker.isolate_file)) {
      test_runner_args += [
        "--isolate-file-path",
        rebase_path(invoker.isolate_file, root_build_dir),
      ]
    }

    generated_script = "$root_build_dir/bin/run_${_test_name}"
    outputs = [
      depfile,
      generated_script,
    ]
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--script-output-path",
      rebase_path(generated_script, root_build_dir),
    ]
    args += test_runner_args
  }
}
