# Copyright 2025 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Template to package a .app bundle into a .ipa.
#
# Arguments
#   bundle_target:
#       Target that generates the .app
#
#   bundle_app_path:
#       (optional) Path to the .app generated by bundle_target. Defaults to the
#       last output of `bundle_target` if it can be automatically determined.
template("ios_app_ipa") {
  assert(defined(invoker.bundle_target),
         "bundle_target must be defined for ios_app_ipa(\"$target_name\")")

  if (defined(invoker.bundle_app_path)) {
    _bundle_app_path = invoker.bundle_app_path
  } else {
    _bundle_target_outputs = get_target_outputs(invoker.bundle_target)
    assert(len(_bundle_target_outputs) > 0,
           "ios_app_ipa(\"$target_name\") couldn't get the outputs of " +
               "${invoker.bundle_target}")
    _bundle_app_path = _bundle_target_outputs[len(_bundle_target_outputs) - 1]
  }
  _bundle_app_name = get_label_info(invoker.bundle_target, "name")

  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "visibility",
                             "testonly",
                           ])
    script = "//build/config/ios/package_ipa.py"
    sources = [ "$_bundle_app_path" ]
    outputs = [ "$root_out_dir/$_bundle_app_name.ipa" ]
    args = rebase_path(sources, root_build_dir) +
           rebase_path(outputs, root_build_dir)
    deps = [ invoker.bundle_target ]
  }
}
