# 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("//flutter/build/zip_bundle.gni")
import("//flutter/common/config.gni")

generated_file("artifacts_entitlement_config") {
  outputs = [ "$target_gen_dir/entitlements.txt" ]

  data_keys = [ "entitlement_file_path" ]

  deps = [ "//flutter/lib/snapshot:generate_snapshot_bin" ]
  if (flutter_runtime_mode == "debug") {
    deps += [
      "//flutter/impeller/compiler:impellerc",
      "//flutter/impeller/tessellator:tessellator_shared",
      "//flutter/shell/testing:testing",
      "//flutter/tools/path_ops:path_ops",
    ]
  }
}

if (build_engine_artifacts && !is_android) {
  zip_bundle("artifacts") {
    deps = [
      "//flutter/flutter_frontend_server:frontend_server",
      "//flutter/impeller/compiler:impellerc",
      "//flutter/impeller/tessellator:tessellator_shared",
      "//flutter/lib/snapshot:generate_snapshot_bin",
      "//flutter/shell/testing:testing",
      "//flutter/tools/path_ops:path_ops",
      "//third_party/dart/runtime/bin:gen_snapshot",
    ]
    if (is_mac) {
      # TODO(godofredoc): Remove after paths are standardized flutter/flutter#105351.
      # Mac artifacts sometimes use mac and sometimes darwin. Standardizing the
      # names will require changes in the list of artifacts the tool is downloading.
      if (flutter_runtime_mode == "debug") {
        prefix = "darwin-$target_cpu/"
      } else {
        prefix = "darwin-$target_cpu-$flutter_runtime_mode/"
      }
    } else if (flutter_runtime_mode != "debug") {
      prefix = "$full_target_platform_name-$flutter_runtime_mode/"
    } else {
      prefix = "$full_target_platform_name/"
    }
    output = "$prefix/artifacts.zip"

    exe = ""
    if (host_os == "win") {
      exe = ".exe"
    }

    # Only include gen_snapshot for all runtime modes.
    files = [
      {
        source = "$root_out_dir/gen_snapshot$exe"
        destination = "gen_snapshot$exe"
      },
    ]
    if (flutter_runtime_mode == "debug") {
      dll = ".so"
      lib_prefix = "lib"
      if (host_os == "win") {
        dll = ".dll"
        exe = ".exe"
        lib_prefix = ""
      } else if (host_os == "mac") {
        dll = ".dylib"
      }
      files += [
        {
          source = "//third_party/icu/flutter/icudtl.dat"
          destination = "icudtl.dat"
        },
        {
          source = "$root_out_dir/flutter_tester$exe"
          destination = "flutter_tester$exe"
        },
        {
          source = "$root_out_dir/impellerc$exe"
          destination = "impellerc$exe"
        },
        {
          source = "//flutter/impeller/compiler/shader_lib"
          destination = "shader_lib"
        },
        {
          source = "$root_out_dir/${lib_prefix}path_ops$dll"
          destination = "${lib_prefix}path_ops$dll"
        },
        {
          source = "$root_out_dir/libtessellator$dll"
          destination = "libtessellator$dll"
        },
        {
          source = "$root_gen_dir/flutter/lib/snapshot/isolate_snapshot.bin"
          destination = "isolate_snapshot.bin"
        },
        {
          source = "$root_gen_dir/flutter/lib/snapshot/vm_isolate_snapshot.bin"
          destination = "vm_isolate_snapshot.bin"
        },
        {
          source = "$root_gen_dir/frontend_server.dart.snapshot"
          destination = "frontend_server.dart.snapshot"
        },
      ]
    }
    if (host_os == "mac") {
      deps += [ ":artifacts_entitlement_config" ]
      files += [
        {
          source = "$target_gen_dir/entitlements.txt"
          destination = "entitlements.txt"
        },
      ]
    }
  }
}

if (host_os == "linux" || host_os == "win") {
  group("embedder") {
    deps = [ "//flutter/shell/platform/embedder:embedder-archive" ]
  }
}

# Generates flutter_patched_sdk.zip for debug and profile runtime modes
# and saves it in the zips_archive folder. For release runtime mode it
# generates flutter_patched_sdk_product.zip and saves it to
# zips_archive/flutter_patched_sdk_product/ folder.
zip_bundle("flutter_patched_sdk") {
  file_suffix = ""
  path_prefix = "flutter_patched_sdk/"
  if (flutter_runtime_mode == "release") {
    file_suffix = "_product"
    path_prefix = "flutter_patched_sdk_product/"
  }
  output = "flutter_patched_sdk${file_suffix}.zip"
  deps = [ "//flutter/lib/snapshot:strong_platform" ]
  files = [
    {
      source = "$root_out_dir/flutter_patched_sdk/vm_outline_strong.dill"
      destination = "${path_prefix}vm_outline_strong.dill"
    },
    {
      source = "$root_out_dir/flutter_patched_sdk/platform_strong.dill"
      destination = "${path_prefix}platform_strong.dill"
    },
  ]
}

generated_file("dart_sdk_entitlement_config") {
  outputs = [ "$target_gen_dir/dart_sdk_entitlements.txt" ]

  # Dart SDK is a prebuilt archive. The rule for building
  # Dart SDK is https://github.com/flutter/engine/blob/main/BUILD.gn#L61
  contents = [
    "dart-sdk/bin/dart",
    "dart-sdk/bin/dartaotruntime",
    "dart-sdk/bin/utils/gen_snapshot",
    "dart-sdk/bin/utils/wasm-opt",
  ]

  deps = []
}

# Flutter consumes the dart sdk as a prebuilt. Rather than regenerating
# the zip file we are just copying the original file to the artifacts location.
if (build_engine_artifacts && flutter_prebuilt_dart_sdk) {
  zip_bundle("dart_sdk_archive") {
    deps = [ ":dart_sdk_entitlement_config" ]
    output = "dart-sdk-$full_target_platform_name.zip"
    files = [
      {
        source = prebuilt_dart_sdk
        destination = "dart-sdk"
      },
    ]
    if (is_mac) {
      # Mac artifacts sometimes use mac and sometimes darwin. Standardizing the
      # names will require changes in the list of artifacts the tool is downloading.
      output = "dart-sdk-darwin-$target_cpu.zip"
      files += [
        {
          source = "$target_gen_dir/dart_sdk_entitlements.txt"
          destination = "entitlements.txt"
        },
      ]
    }
  }
}

# If there is no prebuilt sdk, create the dart sdk archive from the built sdk.
if (build_engine_artifacts && !flutter_prebuilt_dart_sdk) {
  zip_bundle_from_file("dart_sdk_archive") {
    deps = [ "//third_party/dart:create_sdk" ]
    output = "dart-sdk-$full_target_platform_name.zip"
    files = [
      {
        source = rebase_path("$root_build_dir/dart-sdk")
        destination = "dart-sdk"
      },
    ]
    if (is_mac) {
      output = "dart-sdk-darwin-$target_cpu.zip"
      deps += [ ":dart_sdk_entitlement_config" ]
      files += [
        {
          source = "$target_gen_dir/dart_sdk_entitlements.txt"
          destination = "entitlements.txt"
        },
      ]
    }
  }
}

# Archives Flutter Windows Artifacts
if (host_os == "win") {
  zip_bundle("windows_flutter") {
    output = "$full_target_platform_name-$flutter_runtime_mode/$full_target_platform_name-flutter.zip"
    deps = [
      "//flutter:gen_snapshot",
      "//flutter/shell/platform/common:publish_headers",
      "//flutter/shell/platform/windows:flutter_windows",
      "//flutter/shell/platform/windows:publish_headers_windows",
    ]
    files = [
      {
        source = "$root_out_dir/flutter_export.h"
        destination = "flutter_export.h"
      },
      {
        source = "$root_out_dir/flutter_windows.h"
        destination = "flutter_windows.h"
      },
      {
        source = "$root_out_dir/flutter_messenger.h"
        destination = "flutter_messenger.h"
      },
      {
        source = "$root_out_dir/flutter_plugin_registrar.h"
        destination = "flutter_plugin_registrar.h"
      },
      {
        source = "$root_out_dir/flutter_texture_registrar.h"
        destination = "flutter_texture_registrar.h"
      },
      {
        source = "$root_out_dir/flutter_windows.dll"
        destination = "flutter_windows.dll"
      },
      {
        source = "$root_out_dir/flutter_windows.dll.exp"
        destination = "flutter_windows.dll.exp"
      },
      {
        source = "$root_out_dir/flutter_windows.dll.lib"
        destination = "flutter_windows.dll.lib"
      },
      {
        source = "$root_out_dir/flutter_windows.dll.pdb"
        destination = "flutter_windows.dll.pdb"
      },
      {
        source = "$root_out_dir/gen_snapshot/gen_snapshot.exe"
        destination = "gen_snapshot.exe"
      },
    ]
  }
}

# Archives Flutter Mac Artifacts
if (is_mac) {
  zip_bundle("archive_gen_snapshot") {
    deps = [ "//flutter/lib/snapshot:create_macos_gen_snapshots" ]
    suffix = "-$flutter_runtime_mode"
    if (flutter_runtime_mode == "debug") {
      suffix = ""
    }
    output = "$full_platform_name$suffix/gen_snapshot.zip"
    files = [
      {
        source = "$root_out_dir/gen_snapshot_$target_cpu"
        destination = "gen_snapshot_$target_cpu"
      },
    ]
  }

  group("flutter_embedder_framework") {
    deps = [
      "//flutter/shell/platform/embedder:flutter_embedder_framework_archive",
    ]
  }
}

if (host_os == "win") {
  zip_bundle("archive_win_gen_snapshot") {
    deps = [ "//flutter:gen_snapshot" ]
    output = "$full_target_platform_name-$flutter_runtime_mode/windows-x64.zip"
    files = [
      {
        source = "$root_out_dir/gen_snapshot/gen_snapshot.exe"
        destination = "gen_snapshot.exe"
      },
    ]
  }
}
