# Copyright 2016 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("//mojo/public/mojo_constants.gni")

# Used to produce a Mojo Application Manifest for an application.
#
# Parameters:
#
#   source
#       The manifest file template for this application, must be valid JSON with
#       a valid 'url' key matching application_name.
#
#   base_manifest (optional)
#       A manifest file template to use as a base for |source|. Any properties
#       defined in |source| will overwrite or be merged with properties defined
#       in |base_manifest|.
#
#   application_name
#       The host portion of the mojo: URL of the application. The script
#       validates that the value of this parameter matches the host name portion
#       of the 'url' property set in the manifest and throws a ValueError if
#       they do not.
#
#   base_deps (optional)
#       Dependencies required to generate |base_manifest| if applicable.
#
#   deps (optional)
#       An array of dependent instances of this template. This template enforces
#       that dependencies can only be instances of this template.
#
#   packaged_applications (optional)
#       An array of application_names of the dependent applications.
#
#   type (default is mojo)
#       Possible values are 'mojo' and 'exe'. Default is 'mojo'.
#
# Outputs:
#
#   An instantiation of this template produces in
#       $outdir/<application_name>/manifest.json
#   a meta manifest from the source template and the output manifest of all
#   dependent children.
#
template("mojo_application_manifest") {
  assert(defined(invoker.source),
         "\"source\" must be defined for the $target_name template")
  assert(defined(invoker.application_name),
         "\"application_name\" must be defined for the $target_name template")
  if (defined(invoker.deps)) {
    assert(defined(invoker.packaged_applications),
           "\"packaged_applications\" listing the directory containing the " +
               "manifest.json of dependent applications must be provided.")
  }
  if (defined(invoker.packaged_applications)) {
    assert(defined(invoker.deps),
           "\"deps\" building the dependent packaged applications must be " +
               "provided.")
  }
  if (defined(invoker.type)) {
    assert(invoker.type == "mojo" || invoker.type == "exe",
           "\"type\" must be one of \"mojo\" or \"exe\".")
  }

  action(target_name) {
    script = "//mojo/public/tools/manifest/manifest_collator.py"

    type = "mojo"
    if (defined(invoker.type)) {
      type = invoker.type
    }

    application_name = invoker.application_name
    inputs = [
      invoker.source,
    ]

    if (type == "mojo") {
      output = "$root_out_dir/$mojo_application_subdir/$application_name/manifest.json"
    } else {
      output = "$root_out_dir/${application_name}_manifest.json"
    }
    outputs = [
      output,
    ]

    rebase_parent = rebase_path(invoker.source, root_build_dir)
    rebase_output = rebase_path(output, root_build_dir)

    args = [
      "--application-name=$application_name",
      "--parent=$rebase_parent",
      "--output=$rebase_output",
    ]

    if (defined(invoker.base_manifest)) {
      rebase_base = rebase_path(invoker.base_manifest, root_build_dir)
      args += [ "--base-manifest=$rebase_base" ]
    }

    if (defined(invoker.packaged_applications)) {
      foreach(application_name, invoker.packaged_applications) {
        input = "$root_out_dir/$mojo_application_subdir/$application_name/manifest.json"
        inputs += [ input ]
        args += [ rebase_path(input, root_build_dir) ]
      }
    }
    deps = []
    data_deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
      data_deps += invoker.deps
    }
    if (defined(invoker.base_deps)) {
      deps += invoker.base_deps
      data_deps += invoker.base_deps
    }
  }

  all_deps = []
  if (defined(invoker.deps)) {
    all_deps += invoker.deps
  }

  group("${target_name}__is_mojo_application_manifest") {
  }

  # Explicitly ensure that all dependencies are mojo_application_manifest
  # targets themselves.
  group("${target_name}__check_deps_are_all_mojo_application_manifest") {
    deps = []
    foreach(d, all_deps) {
      name = get_label_info(d, "label_no_toolchain")
      toolchain = get_label_info(d, "toolchain")
      deps += [ "${name}__is_mojo_application_manifest(${toolchain})" ]
    }
  }
}