chromium/chrome/android/modules/chrome_feature_module_tmpl.gni

# Copyright 2019 The Chromium Authors
# 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("//build/config/locales.gni")
import("//chrome/android/modules/chrome_feature_modules.gni")
import("//components/module_installer/android/module_desc_java.gni")

# Instantiates a Chrome-specific app bundle module.
#
# Supports most variables of the android_app_bundle_module, plus:
#   module_desc: Descriptor of this module. See
#     //chrome/android/modules/chrome_feature_modules.gni for the format.
#   is_monochrome: (Optional) Whether this module is Monochrome.
#   is_trichrome: (Optional) Whether this module is Trichrome.
#   is_64_bit_browser: (Optional) Whether Chrome (as opposed to WebView) runs in
#     64 bit.
template("chrome_feature_module") {
  assert(defined(invoker.module_desc))
  _module_desc = invoker.module_desc
  _is_monochrome_or_trichrome =
      (defined(invoker.is_monochrome) && invoker.is_monochrome) ||
      (defined(invoker.is_trichrome) && invoker.is_trichrome)
  not_needed(invoker, [ "is_trichrome" ])

  _is_64_bit_browser =
      android_64bit_target_cpu &&
      (!defined(invoker.is_64_bit_browser) || invoker.is_64_bit_browser)

  _loadable_modules_32_bit = []
  if (defined(_module_desc.loadable_modules_32_bit)) {
    _loadable_modules_32_bit += _module_desc.loadable_modules_32_bit
  }

  _loadable_modules_64_bit = []
  if (defined(_module_desc.loadable_modules_64_bit)) {
    _loadable_modules_64_bit += _module_desc.loadable_modules_64_bit
  }

  not_needed([
               "_is_64_bit_browser",
               "_loadable_modules_32_bit",
               "_loadable_modules_64_bit",
             ])

  _shared_libraries = []
  if (defined(_module_desc.native_deps) && _module_desc.native_deps != []) {
    if (_is_monochrome_or_trichrome) {
      if (_is_64_bit_browser) {
        _base_target_name = "libmonochrome_64"
      } else {
        _base_target_name = "libmonochrome"
      }
    } else {
      _base_target_name = "libchrome"
    }
    if (android_64bit_target_cpu && !_is_64_bit_browser) {
      _toolchain = "($android_secondary_abi_toolchain)"
    } else {
      _toolchain = ""
    }
    _shared_libraries += [
      "//chrome/android:${_base_target_name}_${_module_desc.name}${_toolchain}",
    ]

    if (_is_monochrome_or_trichrome && _is_64_bit_browser &&
        build_hwasan_splits) {
      _hwasan_toolchain = "//build/toolchain/android:android_clang_arm64_hwasan"
      _shared_libraries += [ "//chrome/android:${_base_target_name}_${_module_desc.name}($_hwasan_toolchain)" ]
    }
  } else {
    not_needed([ "_is_monochrome_or_trichrome" ])
  }

  if (defined(_module_desc.pak_deps)) {
    android_assets("${target_name}__pak_assets") {
      sources = _module_desc.paks
      deps = _module_desc.pak_deps
      disable_compression = true
    }
  }

  module_desc_java("${target_name}__module_desc_java") {
    module_name = _module_desc.name
    shared_libraries = _shared_libraries
    if (defined(_module_desc.pak_deps)) {
      paks = _module_desc.paks
    }
    if (defined(_module_desc.load_native_on_get_impl)) {
      load_native_on_get_impl = _module_desc.load_native_on_get_impl
    }
  }

  android_app_bundle_module(target_name) {
    forward_variables_from(invoker,
                           [
                             "add_view_trace_events",
                             "base_module_target",
                             "custom_assertion_handler",
                             "expected_android_manifest",
                             "expected_android_manifest_base",
                             "expected_android_manifest_library_version_offset",
                             "expected_android_manifest_version_code_offset",
                             "manifest_package",
                             "min_sdk_version",
                             "package_id",
                             "parent_module_target",
                             "target_sdk_version",
                             "version_code",
                             "version_name",
                             "override_target_sdk",
                           ])
    forward_variables_from(_module_desc,
                           [
                             "android_manifest",
                             "android_manifest_dep",
                             "uses_split",
                           ])
    deps = [ ":${target_name}__module_desc_java" ]
    if (defined(_module_desc.pak_deps)) {
      deps += [ ":${target_name}__pak_assets" ]
    }
    if (defined(_module_desc.java_deps)) {
      deps += _module_desc.java_deps
    }
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    # Don't embed more translations than required (http://crbug.com/932017).
    aapt_locale_allowlist = platform_pak_locales
    proguard_enabled = !is_java_debug
    module_name = _module_desc.name

    # Determine whether to assign native libraries to the primary or secondary
    # ABI.
    if (android_64bit_target_cpu) {
      if (_is_64_bit_browser) {
        loadable_modules = _loadable_modules_64_bit
        shared_libraries = _shared_libraries
      } else {
        secondary_abi_loadable_modules = _loadable_modules_32_bit
        secondary_abi_shared_libraries = _shared_libraries
      }
    } else {
      loadable_modules = _loadable_modules_32_bit
      shared_libraries = _shared_libraries
    }

    # Adds unwind table asset to the chrome apk for the given library target. This
    # is not part of generic apk assets target since it depends on the main shared
    # library of the apk, to extract unwind tables.
    asset_deps = []
    if (defined(_module_desc.include_unwind_assets) &&
        _module_desc.include_unwind_assets) {
      _needs_32bit_lib =
          target_cpu == "arm" || (target_cpu == "arm64" && !_is_64_bit_browser)

      if (_needs_32bit_lib) {
        if (_is_monochrome_or_trichrome) {
          asset_deps += [ "//chrome/android:libmonochrome_unwind_table_assets" ]
        } else {
          asset_deps += [ "//chrome/android:libchrome_unwind_table_assets" ]
        }
      }
    }
    if (defined(invoker.asset_deps)) {
      asset_deps += invoker.asset_deps
    }
  }
}