# Copyright 2018 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/rules.gni")
import("//third_party/angle/gni/angle.gni")

template("angle_apk") {
  manifest_target_name = "${target_name}__manifest"
  manifest_path = "${target_gen_dir}/${target_name}/android/AndroidManifest.xml"

  jinja_template(manifest_target_name) {
    input = "//third_party/angle/android/AndroidManifest.xml.jinja2"
    output = manifest_path
    variables = [ "manifest_package=${invoker.package_name}" ]
  }

  android_resources("${invoker.package_name}_resources") {
    resource_dirs = [ "android/res" ]

    custom_package = "com.android.angle.common"
    deps = [
      "//third_party/android_deps:android_support_v14_preference_java",
      "//third_party/android_deps:android_support_v7_preference_java",
    ]
  }

  android_assets("${invoker.package_name}_assets") {
    disable_compression = true
    sources = [
      "src/feature_support_util/a4a_rules.json",
    ]
  }

  android_apk(target_name) {
    forward_variables_from(invoker, "*")
    android_manifest = manifest_path
    android_manifest_dep = ":$manifest_target_name"
    deps = [
      ":${invoker.package_name}_assets",
      ":${invoker.package_name}_resources",
      "//base:base_java",
      "//third_party/android_deps:android_support_v14_preference_java",
      "//third_party/android_deps:android_support_v7_preference_java",
    ]
    if (symbol_level != 0) {
      deps += [ ":compressed_symbols" ]
      if (build_apk_secondary_abi && android_64bit_target_cpu) {
        deps += [ ":compressed_symbols($android_secondary_abi_toolchain)" ]
      }
    }

    uncompress_shared_libraries = true

    if (build_apk_secondary_abi && android_64bit_target_cpu) {
      if (symbol_level == 0) {
        secondary_abi_shared_libraries = [
          "//third_party/angle:libEGL${angle_libs_suffix}($android_secondary_abi_toolchain)",
          "//third_party/angle:libGLESv1_CM${angle_libs_suffix}($android_secondary_abi_toolchain)",
          "//third_party/angle:libGLESv2${angle_libs_suffix}($android_secondary_abi_toolchain)",
          "//third_party/angle:libfeature_support${angle_libs_suffix}" +
              "($android_secondary_abi_toolchain)",
        ]
      } else {
        _secondary_out_dir = get_label_info(
                ":compressed_symbols($android_secondary_abi_toolchain)",
                "root_out_dir")
        secondary_abi_loadable_modules = [
          "$_secondary_out_dir/lib.compressed/libEGL${angle_libs_suffix}.so",
          "$_secondary_out_dir/lib.compressed/libGLESv1_CM${angle_libs_suffix}.so",
          "$_secondary_out_dir/lib.compressed/libGLESv2${angle_libs_suffix}.so",
          "$_secondary_out_dir/lib.compressed/libfeature_support${angle_libs_suffix}.so",
        ]
      }
    }

    if (symbol_level == 0) {
      shared_libraries = [
        "//third_party/angle:libEGL${angle_libs_suffix}",
        "//third_party/angle:libGLESv1_CM${angle_libs_suffix}",
        "//third_party/angle:libGLESv2${angle_libs_suffix}",
        "//third_party/angle:libfeature_support${angle_libs_suffix}",
      ]
    } else {
      loadable_modules = [
        "$root_out_dir/lib.compressed/libEGL${angle_libs_suffix}.so",
        "$root_out_dir/lib.compressed/libGLESv1_CM${angle_libs_suffix}.so",
        "$root_out_dir/lib.compressed/libGLESv2${angle_libs_suffix}.so",
        "$root_out_dir/lib.compressed/libfeature_support${angle_libs_suffix}.so",
      ]
    }

    # Common files required by all builds
    java_files = [
      "android/src/com/android/angle/common/GlobalSettings.java",
      "android/src/com/android/angle/common/MainFragment.java",
      "android/src/com/android/angle/common/Receiver.java",
    ]

    if (target_name == "aosp_apk") {
      java_files += [ "android/src/com/android/angle/MainActivity.java" ]
    } else if (target_name == "google_apk") {
      java_files += [ "android/src/com/google/android/angle/MainActivity.java" ]
    }
  }
}
