# Copyright (c) 2022 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("//build/ohos/ace/ace.gni")
import("//commonlibrary/ets_utils/ets_utils_config.gni")
import("//foundation/arkui/ace_engine/ace_config.gni")

# shall be moved to "//build/ohos/ace/ace.gni" in the future
import("//foundation/arkui/ace_engine/build/ace_gen_obj.gni")

# compile .ts to .js.
action("build_ts_js") {
  script = "${ets_util_path}/js_api_module/build_ts_js.py"

  outFile_Path = target_out_dir + "/" + current_cpu
  args = [
    "--dst-file",
    rebase_path(target_out_dir + "/js_convertxml.js"),
    "--module-path",
    rebase_path("/commonlibrary/ets_utils/js_api_module/convertxml"),
    "--out-file",
    rebase_path(outFile_Path + "/js_convertxml.js"),
    "--out-filePath",
    rebase_path(outFile_Path),
    "--relative-path",
    rebase_path("//", root_build_dir),
  ]
  outputs = [ target_out_dir + "/js_convertxml.js" ]
}
base_output_path = get_label_info(":js_convertxml", "target_out_dir")
if (use_ios || use_mac || use_mingw_win || use_linux) {
  js_xml_obj_path = base_output_path + "/convertxml.c"
} else {
  js_xml_obj_path = base_output_path + "/convertxml.o"
}
gen_obj("js_convertxml") {
  input = "$target_out_dir/js_convertxml.js"
  output = js_xml_obj_path
  snapshot_dep = [ ":build_ts_js" ]
}

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

abc_output_path = get_label_info(":convertxml_abc", "target_out_dir")
if (use_ios || use_mac || use_mingw_win || use_linux) {
  convertxml_abc_obj_path = abc_output_path + "/convertxml_abc.c"
} else {
  convertxml_abc_obj_path = abc_output_path + "/convertxml_abc.o"
}
gen_obj("convertxml_abc") {
  input = "$target_out_dir/convertxml.abc"
  output = convertxml_abc_obj_path
  snapshot_dep = [ ":gen_convertxml_abc" ]
}

ohos_shared_library("convertxml") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }
  deps = [ ":convertxml_static" ]
  external_deps = [ "hilog:libhilog" ]
  subsystem_name = "commonlibrary"
  part_name = "ets_utils"

  relative_install_dir = "module"
}

ohos_source_set("convertxml_static") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }
  include_dirs = [
    "include",
    "//third_party/icu/icu4c/source/common",
    "//third_party/node/src",
    ets_util_path,
  ]

  sources = [
    "js_convertxml.cpp",
    "native_module_convertxml.cpp",
  ]

  deps = [
    ":gen_obj_src_convertxml_abc",
    ":gen_obj_src_js_convertxml",
  ]

  if (defined(is_arkui_x) && is_arkui_x) {
    include_dirs += [
      "$plugins_root/hilog/include",
      "$plugins_root/interfaces",
      "//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
    ]
    deps += [
      "$plugins_root/interfaces/native:plugin_log",
      "//third_party/libxml2:libxml2",
    ]
    if (target_os == "android") {
      deps += [ "$plugins_root/interfaces/native:ace_plugin_util_android" ]
      defines = [ "ANDROID_PLATFORM" ]
    }
    deps += [
      "$plugins_root/libs/icu:icu_${target_os}",
      "$plugins_root/libs/napi:napi_${target_os}",
      "$plugins_root/libs/securec:sec_${target_os}",
    ]
  } else {
    external_deps = [ "napi:ace_napi" ]
    if (is_standard_system) {
      external_deps += [ "hilog:libhilog" ]
    } else {
      external_deps += [
        "c_utils:utils",
        "hilog:libhilog",
      ]
    }
    deps += [
      "//third_party/icu/icu4c:shared_icuuc",
      "//third_party/libxml2:static_libxml2",
    ]
  }

  subsystem_name = "commonlibrary"
  part_name = "ets_utils"
}

group("convertxml_packages") {
  deps = [ ":convertxml" ]
}