#
#
# Agora Real Time Engagement
# Created by Ender Zheng in 2021-08.
# Copyright (c) 2022 Agora IO. All rights reserved.
#
#
import("//config/package.gni")
import("//config/prebuilt.gni")
import("//config/rte.gni")
import("//exts/rte/tsc.gni")

group("nodejs") {
  deps = [
    ":rte_runtime_nodejs",
    ":rte_runtime_nodejs_js",
  ]
}

nodejs_library("rte_runtime_nodejs_js") {
  tsconfig = "tsconfig.json"
  package_json = "package.json"
  output_dir = "$root_out_dir/nodejs/rte_runtime_nodejs"
  remove_node_modules = false
  remove_tsbuildinfo = true
  remove_src = true
}

rte_shared_library("rte_runtime_nodejs") {
  output_extension = "node"
  output_name = "rte_runtime_nodejs"
  if (is_win) {
    output_name = "librte_runtime_nodejs"
  }
  deps = [
    "src",
    "//src/rte_runtime",
  ]
  if (is_mac || is_linux) {
    add_configs = [ "//.gnfiles/build/toolchain/common:allow_undefined" ]
    remove_configs = [ "//.gnfiles/build/toolchain/common:disallow_undefined" ]
  }
  if (is_win) {
    # link to node api 16
    dst_lib_file = "//third_party/node/lib/win-" + target_cpu + "/node.lib"
    print("Downloading " + dst_lib_file + " from node dist...")
    url = "${prebuilt_url}/node_lib/16.13.0/win/${target_cpu}/node.lib"
    exec_script("//config/download.py",
                [
                  url,
                  rebase_path(dst_lib_file),
                ])
    lib_dirs = [ "//third_party/node/lib/win-x64" ]
    libs = [
      "node.lib",
      "delayimp.lib",
    ]
    ldflags = [ "/DELAYLOAD:node.exe" ]

    # This file is only needed for Windows platform, so that this file can be
    # not put into 'src/' folder. Becuase all files in 'src/' would be build in
    # all platforms.
    sources = [ "win_delay_load_hook.cc" ]
  }
}
