# 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("//foundation/filemanagement/file_api/file_api.gni")
import("//foundation/filemanagement/file_api/interfaces/kits/ts/gen_obj.gni")

# compile .ts to .js.
action("build_streamrw_js") {
  script = "${file_api_path}/interfaces/kits/ts/streamrw/build_ts_js.py"

  outFile_Path = target_out_dir + "/" + current_cpu
  args = [
    "--dst-file",
    rebase_path(target_out_dir + "/streamrw.js"),
    "--module-path",
    rebase_path(
        "/foundation/filemanagement/file_api/interfaces/kits/ts/streamrw"),
    "--out-file",
    rebase_path(outFile_Path + "/streamrw.js"),
    "--out-filePath",
    rebase_path(outFile_Path),
    "--relative-path",
    rebase_path("//", root_build_dir),
  ]
  outputs = [ target_out_dir + "/streamrw.js" ]
}

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

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

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

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

streamrw_sources = [ "native_module_streamrw.cpp" ]

config("optimize-size") {
  cflags = [
    "-fdata-sections",
    "-ffunction-sections",
    "-Oz",
  ]
  cflags_cc = [
    "-fvisibility-inlines-hidden",
    "-Oz",
  ]
}

ohos_shared_library("streamrw") {
  configs = [ ":optimize-size" ]
  branch_protector_ret = "pac_ret"
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  deps = [ ":streamrw_static" ]
  subsystem_name = "filemanagement"
  part_name = "file_api"
  relative_install_dir = "module/file"
}

ohos_source_set("streamrw_static") {
  include_dirs = [ "include" ]
  sources = streamrw_sources

  branch_protector_ret = "pac_ret"
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  deps = [
    ":gen_obj_src_streamrw_abc",
    ":gen_obj_src_streamrw_js",
  ]
  external_deps = [
    "hilog:libhilog",
    "napi:ace_napi",
  ]
  subsystem_name = "filemanagement"
  part_name = "file_api"
}

group("streamrw_packages") {
  public_deps = [ ":streamrw" ]
}