chromium/chrome/android/trichrome.gni

# Copyright 2018 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/abi.gni")
import("//build/config/android/channel.gni")
import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
import("//chrome/android/modules/buildflags.gni")
import("//chrome/version.gni")
import("//device/vr/buildflags/buildflags.gni")

if (!defined(default_trichrome_certdigest)) {
  default_trichrome_certdigest =
      "32a2fc74d731105859e5a85df16d95f102d85b22099b8064c5d8915c61dad1e0"
}

declare_args() {
  # The package name for the Trichrome static shared library on Android.
  trichrome_library_package = "org.chromium.trichromelibrary"

  # The SHA256 certificate digest for the Trichrome static shared library on
  # Android. You can use "apksigner verify --print-certs" on the signed APK to
  # calculate the correct digest.
  trichrome_certdigest = default_trichrome_certdigest
}

trichrome_jinja_variables = [
  "trichrome_library=$trichrome_library_package",
  "trichrome_certdigest=$trichrome_certdigest",
  "zygote_preload_class=org.chromium.chrome.app.TrichromeZygotePreload",
]

template("trichrome_library_apk_tmpl") {
  _is_64_bit_browser = android_64bit_target_cpu && invoker.is_64_bit_browser
  _include_64_bit_webview =
      (!defined(invoker.include_64_bit_webview) ||
       invoker.include_64_bit_webview) && android_64bit_target_cpu
  _include_32_bit_webview =
      !defined(invoker.include_32_bit_webview) || invoker.include_32_bit_webview
  _version_code = TRICHROME_VERSION_MAP["${android_64bit_target_cpu}_${_is_64_bit_browser}_${_include_64_bit_webview}_${_include_32_bit_webview}"]

  if (defined(invoker.version_code)) {
    _version_code = invoker.version_code
  }

  # Need to apply override explicitly to have it apply to library version.
  if (android_override_version_code != "") {
    _version_code = android_override_version_code
  }

  # Include the ABI that is shared between webview & chrome.
  _include_primary_abi = !android_64bit_target_cpu || _is_64_bit_browser

  if (!_include_primary_abi) {
    _secondary_out_dir =
        get_label_info("X($android_secondary_abi_toolchain)", "root_out_dir")
  }

  if (defined(invoker.manifest_package)) {
    _manifest_package = invoker.manifest_package
  } else {
    _manifest_package = trichrome_library_package
  }
  _android_manifest = "$target_gen_dir/$target_name/AndroidManifest.xml"
  _android_manifest_target_name = "${target_name}__android_manifest"
  jinja_template(_android_manifest_target_name) {
    input = "//chrome/android/java/AndroidManifest_trichrome_library.xml"
    output = _android_manifest
    _force_32_bit = _include_32_bit_webview && _include_64_bit_webview &&
                    !_is_64_bit_browser
    variables = trichrome_jinja_variables + [
                  "force_32_bit=$_force_32_bit",
                  "manifest_package=$_manifest_package",
                  "trichrome_version=$_version_code",
                ]

    if (defined(invoker.jinja_extra_variables)) {
      variables += invoker.jinja_extra_variables
    }
  }
  android_apk(target_name) {
    android_manifest = _android_manifest
    android_manifest_dep = ":$_android_manifest_target_name"
    manifest_package = _manifest_package

    if (defined(invoker.expected_android_manifest)) {
      if (android_64bit_target_cpu) {
        _version_code_offset = chrome_32_version_code
      } else {
        _version_code_offset = chrome_version_code
      }
      expected_android_manifest_version_code_offset = _version_code_offset
      expected_android_manifest_library_version_offset = _version_code_offset
    }

    omit_dex = true
    include_size_info = is_official_build
    app_as_shared_lib = true
    version_name = chrome_version_name
    version_code = _version_code
    min_sdk_version = 29

    # Only the .build_config.json is used from this target, and it's fine if
    # it contains more assets than we need (always use the 64_32 variant).
    # https://crbug.com/357131361
    suffix_apk_assets_used_by = system_webview_apk_target

    # No support for this has been added, also not supported by test runner
    # since trichrome library is used in "additional_apks" in the trichrome
    # bundle smoke tests.
    never_incremental = true

    # TODO(torne): using icon_resources just to get a temporary icon
    deps = [
      "//android_webview/nonembedded:icon_resources",
      "//third_party/icu:icu_assets",
    ]
    if (_include_primary_abi) {
      deps += [ "//gin:v8_snapshot_assets" ]
    } else {
      deps += [ "//gin:v8_snapshot_secondary_abi_assets" ]
    }
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    if (android_64bit_target_cpu) {
      # Include the actual browser-bitness libmonochrome library, dependencies
      # (crashpad and linker), and an opposite-bitness placeholder library to
      # ensure that the library is treated as multiarch and gets its Java code
      # precompiled for both architectures.
      if (_is_64_bit_browser) {
        shared_libraries = [ "//chrome/android:libmonochrome_64" ]
        if (_include_32_bit_webview) {
          secondary_native_lib_placeholders = [ "libplaceholder.so" ]
        }
        if (build_hwasan_splits) {
          _hwasan_toolchain =
              "//build/toolchain/android:android_clang_arm64_hwasan"
          shared_libraries +=
              [ "//chrome/android:libmonochrome_64($_hwasan_toolchain)" ]
        }
      } else {
        secondary_abi_shared_libraries =
            [ "//chrome/android:monochrome_secondary_abi_lib" ]
        if (invoker.include_64_bit_webview) {
          native_lib_placeholders = [ "libplaceholder.so" ]
        }
      }
    } else {
      shared_libraries = [ "//chrome/android:libmonochrome" ]
    }

    # https://chromium.googlesource.com/chromium/src/+/main/docs/android_native_libraries.md#Crashpad-Packaging
    loadable_modules = []
    secondary_abi_loadable_modules = []
    if (_include_primary_abi) {
      deps += [
        "//third_party/crashpad/crashpad/handler:crashpad_handler_trampoline",
      ]
      loadable_modules += [ "$root_out_dir/libcrashpad_handler_trampoline.so" ]
    } else {
      deps += [ "//third_party/crashpad/crashpad/handler:crashpad_handler_trampoline($android_secondary_abi_toolchain)" ]
      secondary_abi_loadable_modules +=
          [ "$_secondary_out_dir/libcrashpad_handler_trampoline.so" ]
    }

    if (enable_arcore) {
      _arcore_target = "//third_party/arcore-android-sdk-client:com_google_ar_core_J__unpack_aar"
      _libarcore_dir = get_label_info(_arcore_target, "target_out_dir") +
                       "/com_google_ar_core_java/jni"
      deps += [ _arcore_target ]

      # Secondary ABI is stored in webview when enabled.
      if (_include_primary_abi) {
        loadable_modules +=
            [ "$_libarcore_dir/$android_app_abi/libarcore_sdk_c.so" ]
      } else {
        secondary_abi_loadable_modules +=
            [ "$_libarcore_dir/$android_app_secondary_abi/libarcore_sdk_c.so" ]
      }
    }
    forward_variables_from(invoker,
                           "*",
                           TESTONLY_AND_VISIBILITY + [
                                 "deps",
                                 "include_64_bit_webview",
                                 "include_32_bit_webview",
                                 "is_64_bit_browser",
                               ])
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
  }
}

# An .ssargs file is a text file to specify multiple input files with respective
# parameters, and is used by SuperSize-archive to create multi-container .size
# files. This is used to support SuperSize on Trichrome.
template("write_ssargs_trichrome") {
  _extra_args = " --abi-filter ${invoker.abi_filter}"
  if (defined(invoker.symbols_dir)) {
    _extra_args += " --symbols-dir ${invoker.symbols_dir}"
  }

  # Base names (i.e., no full path) are used because .ssargs files specifies
  # files using paths relative to itself. It is expected for |ssargs_path| to
  # be in the same directory as all Trichrome files whose sizes are measured
  # by SuperSize.
  _lines = [
    "# Written by build target \"${target_name}.\"",
    "Library -f ${invoker.trichrome_library_basename}$_extra_args --no-java",
    "Chrome -f ${invoker.trichrome_chrome_basename}$_extra_args",
    "WebView -f ${invoker.trichrome_webview_basename}$_extra_args",
  ]

  write_file(invoker.ssargs_path, _lines)
}