# Copyright 2024 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/features.gni")  # For `use_blink`

declare_args() {
  # Configure the environment for which to build. Could be either "device",
  # "simulator" or "catalyst". Must be specified.
  target_environment = ""

  # Valid values: "iphoneos" (default), "tvos", "watchos".
  # Indicates the kind of iOS or iOS-based platform that is being targeted.
  # Note that this value is available only when is_ios is also true (i.e. it
  # cannot be used with the host toolchain).
  target_platform = "iphoneos"

  # Control whether codesiging is enabled (ignored for simulator builds).
  # TODO(crbug.com/378918882): Prefix with apple_mobile_ instead of ios_.
  ios_enable_code_signing = true

  # Explicitly select the identity to use for codesigning. If defined, must
  # be set to a non-empty string that will be passed to codesigning. Can be
  # left unspecified if ios_code_signing_identity_description is used instead.
  # TODO(crbug.com/378918882): Prefix with apple_mobile_ instead of ios_.
  ios_code_signing_identity = ""

  # Pattern used to select the identity to use for codesigning. If defined,
  # must be a substring of the description of exactly one of the identities by
  # `security find-identity -v -p codesigning`.
  # TODO(crbug.com/378918882): Prefix with apple_mobile_ instead of ios_.
  ios_code_signing_identity_description = "Apple Development"

  # Prefix for CFBundleIdentifier property of iOS bundles (correspond to the
  # "Organization Identifier" in Xcode). Code signing will fail if no mobile
  # provisioning for the selected code signing identify support that prefix.
  # TODO(crbug.com/378918882): Prefix with apple_mobile_ instead of ios_.
  ios_app_bundle_id_prefix = "org.chromium.ost"

  # Suffix for CFBundleIdentifier property of iOS Chrome signed bundles
  # (main bundle and extensions). Code signing will fail if no mobile
  # provisioning for the selected code signing identify support that suffix.
  # For extension, the suffix will be added before the extension identifier.
  # The suffix is not added to test applications.
  # No dot is added before the suffix, so add one if needed.
  apple_mobile_app_bundle_id_suffix = ".dev"

  # Paths to the mobileprovision files for the chosen code signing
  # identity description and app bundle id prefix.
  # TODO(crbug.com/378918882): Prefix with apple_mobile_ instead of ios_.
  ios_mobileprovision_files = []
}

# Generates ios_mobileprovision_files_filelist that will contain one file
# name per line. This is used to reduce the length of the commande line
# to codesign.py script. There is a different file per toolchain.
ios_mobileprovision_files_filelist = "$root_out_dir/mobileprovisions.filelist"
write_file(ios_mobileprovision_files_filelist,
           rebase_path(ios_mobileprovision_files, root_build_dir),
           "list lines")

# As entitlements are tied to a specific bundle identifier, all the
# test applications share the same identifier. This simplifies adding
# new test application (since there is no need to investigate which
# entitlements they need, nor to wait for the mobile provision with
# those entitlements to be generated by Apple and then deployed to the
# infrastructure, ...). The drawback is that only one test application
# can be installed at a time on a device/simulator (as the bundle
# identifier uniquely identify an application).
#
# This variable corresponds to the test bundle identifier.
shared_bundle_id_for_test_apps =
    "$ios_app_bundle_id_prefix.chrome.unittests.dev"

# This file is included on all platforms, but the automatic configuration of
# the variables can only be executed if building for ios or watchos (as they
# either have no meaning or depend on tools that are only available on macOS).
if (is_ios || is_watchos) {
  # Check that target_platform and target_environment are set to supported
  # values.
  _target_platforms = []
  if (is_ios) {
    _target_platforms += [
      "iphoneos",
      "tvos",
    ]
  } else if (is_watchos) {
    _target_platforms += [
      "iphoneos",
      "watchos",
    ]
  }
  assert(filter_include([ target_platform ], _target_platforms) != [],
         "target_platform '$target_platform' does not match a target value: " +
             "$_target_platforms")
  _target_environments = [
    "simulator",
    "device",
  ]
  if (is_ios && target_platform == "iphoneos") {
    _target_environments += [ "catalyst" ]
  }
  assert(filter_include([ target_environment ], _target_environments) != [],
         "target_environment must be in $_target_environments: " +
             "$target_environment")

  if (target_environment == "device" && ios_enable_code_signing) {
    # If codesigning is enabled, user must configure either a codesigning
    # identity or a filter to automatically select the codesigning identity.
    assert(ios_code_signing_identity == "" ||
               ios_code_signing_identity_description == "",
           "You should either specify the precise identity to use with " +
               "ios_code_signing_identity or let the code select an identity " +
               "automatically (via find_signing_identity.py which use the " +
               "variable ios_code_signing_identity_description to set the " +
               "pattern to match the identity to use).")

    # Automatically select a codesigning identity if no identity is configured.
    # This only applies to device build as simulator builds are not signed.
    if (ios_code_signing_identity == "") {
      find_signing_identity_args = []
      if (ios_code_signing_identity_description != "") {
        find_signing_identity_args = [
          "--matching-pattern",
          ios_code_signing_identity_description,
        ]
      }
      ios_code_signing_identity =
          exec_script("//build/config/apple/find_signing_identity.py",
                      find_signing_identity_args,
                      "trim string")
    }
  }

  # Sanity check: tvOS builds require `use_blink`.
  if (target_platform == "tvos") {
    assert(use_blink, "tvOS builds require use_blink=true")
  }
}
