chromium/components/module_installer/android/module_desc_java.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/python.gni")

# Writes an implementation of
# |org.chromium.components.module_installer.builder.ModuleDescriptor| for a
# particular module to Java. The module loader backend expects such an
# implementation for each module to automate module setup on first access.
# Instantiations of this template can be depended on like |android_library|
# targets.
#
# Supports the following variables:
#   module_name: Name of the module.
#   shared_libraries: (Optional) List of shared_library targets the module
#     requires at runtime. Will consider all transitively depended on
#     shared_libraries.
#   paks: (Optional) PAK files going into the module.
#   load_native_on_get_impl: (Optional) Whether the module's native libraries /
#     resources are automatically loaded when Module.getImpl() is called.
template("module_desc_java") {
  _target_name = target_name

  _libraries_file = "${target_gen_dir}/${_target_name}.libraries"
  generated_file("${_target_name}__libraries") {
    if (defined(invoker.shared_libraries)) {
      deps = invoker.shared_libraries
    }
    outputs = [ _libraries_file ]
    data_keys = [ "shared_libraries" ]
    walk_keys = [ "shared_libraries_barrier" ]
    rebase = root_build_dir
    output_conversion = "value"
  }

  _srcjar = "$target_gen_dir/${_target_name}__srcjar.srcjar"
  action_with_pydeps("${_target_name}__srcjar") {
    script = "//components/module_installer/android/module_desc_java.py"
    outputs = [ _srcjar ]

    # Do not add a dep on the generated_file target in order to avoid having
    # to build the native libraries before this target. The dependency is
    # instead captured via a depfile.
    depfile = "$target_gen_dir/$target_name.d"
    args = [
      "--module",
      invoker.module_name,
      "--libraries-file",
      rebase_path(_libraries_file, root_out_dir),
      "--output",
      rebase_path(_srcjar, root_out_dir),
      "--depfile",
      rebase_path(depfile, root_out_dir),
    ]

    if (defined(invoker.load_native_on_get_impl) &&
        invoker.load_native_on_get_impl) {
      args += [ "--load-native-on-get-impl" ]
    }

    if (defined(invoker.paks)) {
      _rebased_paks = []
      foreach(_pak, invoker.paks) {
        _rebased_paks += [ rebase_path(_pak, root_out_dir) ]
      }
      args += [ "--paks=$_rebased_paks" ]
    }
  }

  android_library(_target_name) {
    deps = [ "//components/module_installer/android:module_installer_java" ]
    srcjar_deps = [ ":${_target_name}__srcjar" ]
  }
}