# 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.

assert(is_ios)

import("//build/config/ios/ios_sdk.gni")
import("$flutter_root/common/config.gni")
import("$flutter_root/shell/gpu/gpu.gni")
import("$flutter_root/shell/platform/darwin/common/framework_shared.gni")

_flutter_framework_dir = "$root_out_dir/Flutter.framework"

shell_gpu_configuration("ios_gpu_configuration") {
  enable_software = true
  enable_gl = true
  enable_vulkan = false
  enable_metal = shell_enable_metal
}

# The headers that will be copied to the Flutter.framework and be accessed
# from outside the Flutter engine source root.
_flutter_framework_headers = [
  "framework/Headers/Flutter.h",
  "framework/Headers/FlutterAppDelegate.h",
  "framework/Headers/FlutterCallbackCache.h",
  "framework/Headers/FlutterDartProject.h",
  "framework/Headers/FlutterEngine.h",
  "framework/Headers/FlutterHeadlessDartRunner.h",
  "framework/Headers/FlutterPlatformViews.h",
  "framework/Headers/FlutterPlugin.h",
  "framework/Headers/FlutterPluginAppLifeCycleDelegate.h",
  "framework/Headers/FlutterTexture.h",
  "framework/Headers/FlutterViewController.h",
]

_flutter_framework_headers_copy_dir = "$_flutter_framework_dir/Headers"

shared_library("create_flutter_framework_dylib") {
  visibility = [ ":*" ]

  output_name = "Flutter"

  sources = [
    "framework/Source/FlutterAppDelegate.mm",
    "framework/Source/FlutterAppDelegate_Internal.h",
    "framework/Source/FlutterBinaryMessengerRelay.mm",
    "framework/Source/FlutterCallbackCache.mm",
    "framework/Source/FlutterCallbackCache_Internal.h",
    "framework/Source/FlutterDartProject.mm",
    "framework/Source/FlutterDartProject_Internal.h",
    "framework/Source/FlutterEngine.mm",
    "framework/Source/FlutterEngine_Internal.h",
    "framework/Source/FlutterHeadlessDartRunner.mm",
    "framework/Source/FlutterObservatoryPublisher.h",
    "framework/Source/FlutterObservatoryPublisher.mm",
    "framework/Source/FlutterOverlayView.h",
    "framework/Source/FlutterOverlayView.mm",
    "framework/Source/FlutterPlatformPlugin.h",
    "framework/Source/FlutterPlatformPlugin.mm",
    "framework/Source/FlutterPlatformViews.mm",
    "framework/Source/FlutterPlatformViews_Internal.h",
    "framework/Source/FlutterPlatformViews_Internal.mm",
    "framework/Source/FlutterPluginAppLifeCycleDelegate.mm",
    "framework/Source/FlutterTextInputDelegate.h",
    "framework/Source/FlutterTextInputPlugin.h",
    "framework/Source/FlutterTextInputPlugin.mm",
    "framework/Source/FlutterView.h",
    "framework/Source/FlutterView.mm",
    "framework/Source/FlutterViewController.mm",
    "framework/Source/FlutterViewController_Internal.h",
    "framework/Source/accessibility_bridge.h",
    "framework/Source/accessibility_bridge.mm",
    "framework/Source/accessibility_text_entry.h",
    "framework/Source/accessibility_text_entry.mm",
    "framework/Source/platform_message_response_darwin.h",
    "framework/Source/platform_message_response_darwin.mm",
    "framework/Source/platform_message_router.h",
    "framework/Source/platform_message_router.mm",
    "framework/Source/vsync_waiter_ios.h",
    "framework/Source/vsync_waiter_ios.mm",
    "ios_external_texture_gl.h",
    "ios_external_texture_gl.mm",
    "ios_gl_context.h",
    "ios_gl_context.mm",
    "ios_gl_render_target.h",
    "ios_gl_render_target.mm",
    "ios_surface.h",
    "ios_surface.mm",
    "ios_surface_gl.h",
    "ios_surface_gl.mm",
    "ios_surface_software.h",
    "ios_surface_software.mm",
    "platform_view_ios.h",
    "platform_view_ios.mm",
  ]

  if (shell_enable_metal) {
    sources += [
      "ios_surface_metal.h",
      "ios_surface_metal.mm",
    ]
  }

  sources += _flutter_framework_headers

  deps = [
    ":ios_gpu_configuration",
    "$flutter_root/common",
    "$flutter_root/flow",
    "$flutter_root/fml",
    "$flutter_root/lib/ui",
    "$flutter_root/runtime",
    "$flutter_root/runtime:libdart",
    "$flutter_root/shell/common",
    "$flutter_root/shell/platform/darwin/common",
    "$flutter_root/shell/platform/darwin/common:framework_shared",
    "//third_party/skia",
  ]

  public_configs = [ "$flutter_root:config" ]

  defines = [ "FLUTTER_FRAMEWORK=1" ]

  if (shell_enable_metal) {
    defines += [ "FLUTTER_SHELL_ENABLE_METAL=1" ]
  }

  libs = [
    "CoreMedia.framework",
    "CoreVideo.framework",
    "UIKit.framework",
    "OpenGLES.framework",
    "AudioToolbox.framework",
    "QuartzCore.framework",
  ]
}

copy("copy_framework_dylib") {
  visibility = [ ":*" ]

  sources = [ "$root_out_dir/libFlutter.dylib" ]
  outputs = [ "$_flutter_framework_dir/Flutter" ]

  deps = [ ":create_flutter_framework_dylib" ]
}

action("copy_dylib_and_update_framework_install_name") {
  visibility = [ ":*" ]
  stamp_file = "$root_out_dir/flutter_install_name_stamp"
  script = "$flutter_root/sky/tools/change_install_name.py"

  inputs = [ "$_flutter_framework_dir/Flutter" ]
  outputs = [ stamp_file ]

  args = [
    "--dylib",
    rebase_path("$_flutter_framework_dir/Flutter"),
    "--install_name",
    "@rpath/Flutter.framework/Flutter",
    "--stamp",
    rebase_path(stamp_file),
  ]

  deps = [ ":copy_framework_dylib" ]
}

action("copy_framework_info_plist") {
  script = "$flutter_root/build/copy_info_plist.py"
  visibility = [ ":*" ]
  sources = [ "framework/Info.plist" ]
  outputs = [ "$_flutter_framework_dir/Info.plist" ]
  args = [
    rebase_path(sources[0]),
    rebase_path(outputs[0]),
    "--bitcode=$flutter_enable_bitcode",
  ]
}

copy("copy_framework_module_map") {
  visibility = [ ":*" ]
  sources = [ "framework/module.modulemap" ]
  outputs = [ "$_flutter_framework_dir/Modules/module.modulemap" ]
}

action("copy_framework_headers") {
  script = "$flutter_root/sky/tools/install_framework_headers.py"
  visibility = [ ":*" ]
  sources = get_path_info(_flutter_framework_headers, "abspath") +
            framework_shared_headers
  outputs = []
  foreach(header, sources) {
    header_basename = get_path_info(header, "file")
    outputs += [ "$_flutter_framework_headers_copy_dir/$header_basename" ]
  }
  args = [
           "--location",
           rebase_path("$_flutter_framework_headers_copy_dir"),
           "--headers",
         ] + rebase_path(sources, "", "//")
}

copy("copy_framework_icu") {
  visibility = [ ":*" ]
  sources = [ "//third_party/icu/flutter/icudtl.dat" ]
  outputs = [ "$_flutter_framework_dir/{{source_file_part}}" ]
}

copy("copy_framework_podspec") {
  visibility = [ ":*" ]
  sources = [ "framework/Flutter.podspec" ]
  outputs = [ "$root_out_dir/Flutter.podspec" ]
}

copy("copy_license") {
  visibility = [ ":*" ]
  sources = [ "//LICENSE" ]
  outputs = [ "$root_out_dir/LICENSE" ]
}

shared_library("copy_and_verify_framework_headers") {
  visibility = [ ":*" ]
  include_dirs = [ "$_flutter_framework_headers_copy_dir" ]

  sources = [ "framework/Source/FlutterUmbrellaImport.m" ]
  deps = [ ":copy_framework_headers" ]
}

group("flutter_framework") {
  deps = [
    ":copy_and_verify_framework_headers",
    ":copy_dylib_and_update_framework_install_name",
    ":copy_framework_icu",
    ":copy_framework_info_plist",
    ":copy_framework_module_map",
    ":copy_framework_podspec",
    ":copy_license",
  ]
}