# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni")
import("//build/ohos.gni")
import("//commonlibrary/ets_utils/ets_utils_config.gni")
import("//commonlibrary/ets_utils/js_util_module/stream/stream_gen_obj.gni")

# compile .ts to .js.
action("build_ts_js") {
  script = "$util_module/build_ts_js.py"
  outFile_Path = target_out_dir + "/" + current_cpu
  args = [
    "--dst-file",
    rebase_path(target_out_dir + "/stream_js.js"),
    "--module-path",
    rebase_path("/commonlibrary/ets_utils/js_util_module/stream"),
    "--out-file",
    rebase_path(outFile_Path + "/stream_js.js"),
    "--out-filePath",
    rebase_path(outFile_Path),
    "--relative-path",
    rebase_path("//", root_build_dir),
  ]
  outputs = [ target_out_dir + "/stream_js.js" ]
}

# compile .js to .abc.
es2abc_gen_abc("gen_stream_abc") {
  extra_visibility = [ ":*" ]
  src_js = rebase_path(target_out_dir + "/stream_js.js")
  dst_file = rebase_path(target_out_dir + "/stream.abc")
  in_puts = [ target_out_dir + "/stream_js.js" ]
  out_puts = [ target_out_dir + "/stream.abc" ]
  extra_args = [ "--module" ]
  extra_dependencies = [ ":build_ts_js" ]
}

abc_output_path = get_label_info(":stream_abc", "target_out_dir")

gen_obj("stream_js") {
  input = "$target_out_dir/stream_js.js"
  if (use_mac || use_mingw_win || use_ios || use_linux) {
    stream_js_obj_path = abc_output_path + "/stream.c"
  } else {
    stream_js_obj_path = abc_output_path + "/stream.o"
  }
  output = stream_js_obj_path
  snapshot_dep = [ ":build_ts_js" ]
}

gen_obj("stream_abc") {
  input = "$target_out_dir/stream.abc"
  if (use_mac || use_mingw_win || use_ios || use_linux) {
    stream_js_obj_path = abc_output_path + "/stream_abc.c"
  } else {
    stream_js_obj_path = abc_output_path + "/stream_abc.o"
  }
  output = stream_js_obj_path
  snapshot_dep = [ ":gen_stream_abc" ]
}

stream_sources = [ "native_module_stream.cpp" ]

ohos_shared_library("stream") {
  deps = [ ":stream_static" ]
  subsystem_name = "commonlibrary"
  part_name = "ets_utils"
  relative_install_dir = "module/util"
}

ohos_source_set("stream_static") {
  include_dirs = [ "include" ]
  sources = stream_sources

  deps = [
    ":gen_obj_src_stream_abc",
    ":gen_obj_src_stream_js",
  ]

  if (is_arkui_x) {
    include_dirs += [
      "$plugins_root/hilog/include",
      "$plugins_root/interfaces",
    ]

    if (target_os == "android") {
      defines = [ "ANDROID_PLATFORM" ]
    } else if (target_os == "ios") {
      defines = [ "IOS_PLATFORM" ]
    }
    deps += [ "$plugins_root/libs/napi:napi_${target_os}" ]
  } else {
    external_deps = [
      "hilog:libhilog",
      "napi:ace_napi",
    ]
  }
  subsystem_name = "commonlibrary"
  part_name = "ets_utils"
}

group("stream_packages") {
  public_deps = [ ":stream" ]
}