# Copyright 2013 The Flutter 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("config/config.gni")

# Template for running the pm tool for packaging fuchsia packages.
#
# Parameters
#   package_name - defaults to target_name
#   archive_manifest - required
#   command - the packaging step to perform valid steps are build, archive
#
# Forwarded parameters
#       testonly
#       deps
#       public_deps
#       visibility
template("fuchsia_pm_tool") {
  forward_variables_from(invoker, [ "*" ])
  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "deps",
                             "public_deps",
                             "visibility",
                           ])
    _valid_commands = [
      "build",
      "archive",
    ]
    assert(defined(invoker.archive_manifest), "archive_manifest is required.")
    archive_manifest = invoker.archive_manifest

    assert(defined(invoker.command), "command is required")
    assert(invoker.command == "build" || invoker.command == "archive",
           "invalid command. valid commands are ${_valid_commands}")
    command = invoker.command

    package_name = target_name
    if (defined(invoker.package_name)) {
      package_name = invoker.package_name
    }

    # tool path
    _pm_tool_path = "${fuchsia_tool_dir}/pm"
    script = "//build/gn_run_binary.py"

    # output files
    _pkg_out_dir = "${target_gen_dir}/${package_name}"
    _meta_far_file = "${_pkg_out_dir}/meta.far"

    inputs = [
      # Depend on the SDK hash, to ensure rebuild if the SDK tools change.
      fuchsia_sdk_manifest_file,
      _pm_tool_path,
      archive_manifest,
    ]

    if (command == "build") {
      _pkg_output_manifest = "${_pkg_out_dir}/package_manifest.json"
      outputs = [
        _meta_far_file,
        "${_pkg_out_dir}/meta/contents",
        "${_meta_far_file}.merkle",
        _pkg_output_manifest,
      ]
      depfile = "${_meta_far_file}.d"
    } else {
      inputs += [ _meta_far_file ]

      _final_far_file = "$_pkg_out_dir/${package_name}.far"
      outputs = [ _final_far_file ]
    }

    args = [
      rebase_path(_pm_tool_path, root_build_dir),
      "-o",
      rebase_path(_pkg_out_dir, root_build_dir),
      "-m",
      rebase_path(archive_manifest, root_build_dir),
      "-n",
      package_name,
    ]
    if (command == "build") {
      assert(fuchsia_target_api_level != -1,
             "Must set a target api level when creating an archive")
      args += [
        "--api-level=${fuchsia_target_api_level}",
        command,
        "-output-package-manifest",
        rebase_path(_pkg_output_manifest, root_build_dir),
        "-depfile",
      ]
    } else if (command == "archive") {
      args += [
        command,
        "--output",
        rebase_path("${_pkg_out_dir}/${package_name}"),
      ]
    }
  }
}
