# 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_fuchsia)

import("//build/fuchsia/sdk.gni")
import("//flutter/common/config.gni")
import("//flutter/testing/testing.gni")

config("utils_config") {
  include_dirs = [ "../../.." ]
}

template("make_utils") {
  source_set(target_name) {
    sources = [
      "$target_gen_dir/build_info.cc",
      "build_info.h",
      "files.cc",
      "files.h",
      "handle_exception.cc",
      "handle_exception.h",
      "inlines.h",
      "logging.h",
      "mapped_resource.cc",
      "mapped_resource.h",
      "root_inspect_node.cc",
      "root_inspect_node.h",
      "tempfs.cc",
      "tempfs.h",
      "vmo.cc",
      "vmo.h",
      "vmservice_object.cc",
      "vmservice_object.h",
    ]

    deps = invoker.deps + [
             ":generate_build_info_cc_file",
             "$fuchsia_sdk_root/fidl:fuchsia.feedback",
             "$fuchsia_sdk_root/fidl:fuchsia.mem",
             "$fuchsia_sdk_root/pkg:async-loop",
             "$fuchsia_sdk_root/pkg:async-loop-cpp",
             "$fuchsia_sdk_root/pkg:async-loop-default",
             "$fuchsia_sdk_root/pkg:fdio",
             "$fuchsia_sdk_root/pkg:sys_cpp",
             "$fuchsia_sdk_root/pkg:sys_inspect_cpp",
             "$fuchsia_sdk_root/pkg:syslog",
             "$fuchsia_sdk_root/pkg:trace",
             "$fuchsia_sdk_root/pkg:vfs_cpp",
             "$fuchsia_sdk_root/pkg:zx",
             "//flutter/third_party/tonic",
           ]

    public_configs = [ ":utils_config" ]
  }
}

action("generate_build_info_cc_file") {
  inputs = [
    "build_info_in.cc",
    "//flutter/.git/logs/HEAD",
    "//fuchsia/sdk/$host_os/meta/manifest.json",
    "//third_party/dart/.git/logs/HEAD",
  ]
  outputs = [ "$target_gen_dir/build_info.cc" ]

  script = "//flutter/tools/fuchsia/make_build_info.py"
  args = [
    "--input",
    rebase_path("build_info_in.cc", root_build_dir),
    "--output",
    rebase_path(outputs[0], root_build_dir),
    "--buildroot",
    rebase_path("//", root_build_dir),
  ]
}

make_utils("utils") {
  deps = [ "//third_party/dart/runtime/bin:elf_loader" ]
}

test_fixtures("utils_fixtures") {
  fixtures = []
}

executable("dart_utils_unittests") {
  testonly = true

  output_name = "dart_utils_unittests"

  sources = [ "build_info_unittests.cc" ]

  libs = [
    # This is needed for //third_party/googletest for linking zircon symbols.
    "$fuchsia_sdk_path/arch/$target_cpu/sysroot/lib/libzircon.so",
  ]

  deps = [
    ":utils",
    ":utils_fixtures",
    "$fuchsia_sdk_root/pkg:sys_inspect_cpp",
    "//flutter/testing",
    "//third_party/dart/runtime:libdart_jit",
    "//third_party/dart/runtime/bin:dart_io_api",
  ]
}

make_utils("utils_product") {
  deps = [ "//third_party/dart/runtime/bin:elf_loader_product" ]
}
