# 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")

vulkan_validation_layers = [
  "VkLayer_core_validation",
  "VkLayer_object_lifetimes",
  "VkLayer_stateless_validation",
  "VkLayer_thread_safety",
  "VkLayer_unique_objects",
]

angle_libraries = [
  "EGL",
  "GLESv1_CM",
  "GLESv2",
  "feature_support",
]

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_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"
    min_sdk_version = 26
    target_sdk_version = 28
    deps = [
      ":${invoker.package_name}_assets",
    ]
    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 = []
        foreach(_library, angle_libraries) {
          secondary_abi_shared_libraries +=
              [ "${angle_root}:lib${_library}${angle_libs_suffix}" +
                "($android_secondary_abi_toolchain)" ]
        }
        if (angle_enable_vulkan_validation_layers) {
          foreach(_layer, vulkan_validation_layers) {
            secondary_abi_shared_libraries += [
              "${angle_root}/third_party/vulkan-validation-layers:${_layer}" +
                  "($android_secondary_abi_toolchain)",
            ]
          }
        }
      } else {
        _secondary_out_dir = get_label_info(
                ":compressed_symbols($android_secondary_abi_toolchain)",
                "root_out_dir")
        secondary_abi_loadable_modules = []
        foreach(_library, angle_libraries) {
          secondary_abi_loadable_modules += [ "${_secondary_out_dir}/lib.compressed/lib${_library}${angle_libs_suffix}.so" ]
        }
        if (angle_enable_vulkan_validation_layers) {
          foreach(_layer, vulkan_validation_layers) {
            secondary_abi_loadable_modules +=
                [ "${_secondary_out_dir}/lib.compressed/lib${_layer}.so" ]
          }
        }
      }
    }

    if (symbol_level == 0) {
      shared_libraries = []
      foreach(_library, angle_libraries) {
        shared_libraries +=
            [ "${angle_root}:lib${_library}${angle_libs_suffix}" ]
      }
      if (angle_enable_vulkan_validation_layers) {
        foreach(_layer, vulkan_validation_layers) {
          shared_libraries +=
              [ "${angle_root}/third_party/vulkan-validation-layers:${_layer}" ]
        }
      }
    } else {
      loadable_modules = []
      foreach(_library, angle_libraries) {
        loadable_modules += [ "${root_out_dir}/lib.compressed/lib${_library}${angle_libs_suffix}.so" ]
      }
      if (angle_enable_vulkan_validation_layers) {
        foreach(_layer, vulkan_validation_layers) {
          loadable_modules +=
              [ "${root_out_dir}/lib.compressed/lib${_layer}.so" ]
        }
      }
    }
  }
}
