# Copyright (c) 2025 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("//build/ohos.gni")
import("//build/ohos/taihe_idl/taihe_args.gni")

declare_args() {
  taihe_file_path = "$root_out_dir/taihe"
  taihe_toolchain_path = "//prebuilts/taihe/ohos/linux-x86_64/taihe"
}

template("ohos_taihe") {
  assert(defined(invoker.taihe_generated_file_path),
         "please provide taihe_generated_file_path for taihec")
  forward_variables_from(invoker, [ "taihe_generated_file_path" ])

  _taihe_outputs = [ "$taihe_generated_file_path/src/taihe.platform.ani.abi.c" ]
  if (defined(invoker.outputs)) {
    _taihe_outputs += invoker.outputs
  }

  if (taihe_template_bypass) {
    not_needed(invoker, [ "deps", "external_deps", "module_prefix", "path_prefix" ])
    action(target_name) {
      script = rebase_path("//build/scripts/touch_outputs.py")
      args = rebase_path(_taihe_outputs, root_build_dir)
      outputs = _taihe_outputs
      forward_variables_from(invoker, [ "visibility", "testonly" ])
    }
  } else {
    action(target_name) {
      script = rebase_path("//build/ohos/taihe_idl/taihe_retry.sh")
      args = [
        rebase_path("${taihe_toolchain_path}/bin/taihec"),
        "-I",
        rebase_path("$taihe_file_path", root_build_dir),
        "-O",
        rebase_path("$taihe_generated_file_path", root_build_dir),
        "-G",
        "cpp-author",
        "-G",
        "ani-bridge",
      ]

      if (!defined(invoker.enable_exceptions) || !invoker.enable_exceptions) {
        args += [ "-Cnoexcept-all" ]
      }

      if (defined(invoker.module_prefix)) {
        args += [ "-Carkts:module-prefix=${invoker.module_prefix}" ]
      }

      if (defined(invoker.path_prefix)) {
        args += [ "-Carkts:path-prefix=${invoker.path_prefix}" ]
      }

      if (defined(invoker.deps)) {
        deps = invoker.deps
      }

      if (defined(invoker.external_deps)) {
        external_deps = invoker.external_deps
      }

      outputs = _taihe_outputs
      forward_variables_from(invoker, [ "visibility", "testonly" ])
    }
  }
}

template("copy_taihe_idl") {
  assert(defined(invoker.sources), "please provide sources for copy")
  if (taihe_template_bypass) {
    not_needed(invoker, [ "sources", "deps", "external_deps" ])
    group(target_name) {
      forward_variables_from(invoker, [ "visibility", "testonly" ])
    }
  } else {
    ohos_copy(target_name) {
      sources = invoker.sources
      outputs = [ "$taihe_file_path/{{source_file_part}}" ]
      if (defined(invoker.external_deps)) {
        external_deps = invoker.external_deps
      }
      if (defined(invoker.deps)) {
        deps = invoker.deps
      }
      forward_variables_from(invoker, [ "visibility", "testonly" ])
    }
  }
}

template("taihe_shared_library") {
  if (taihe_template_bypass) {
    not_needed(invoker, "*")
    group(target_name) {
      forward_variables_from(invoker, [ "visibility", "testonly" ])
    }
  } else {
    ohos_shared_library(target_name) {
      forward_variables_from(invoker,
                             "*",
                             [
                               "sources",
                               "external_deps",
                               "include_dirs",
                             ])
      include_dirs = [
        "$taihe_generated_file_path/include",
        "$taihe_toolchain_path/include",
      ]
      if (defined(invoker.include_dirs)) {
        include_dirs += invoker.include_dirs
      }

      sources = [
        "$taihe_toolchain_path/src/taihe/runtime/object.cpp",
        "$taihe_toolchain_path/src/taihe/runtime/runtime_ani.cpp",
        "$taihe_toolchain_path/src/taihe/runtime/string.cpp",
      ]
      sources += invoker.sources

      if (!defined(defines)) {
        defines = []
      }

      if (defined(invoker.enable_perf_trace)) {
        defines += [ "TH_ANI_ENABLE_PERF_TRACE" ]
      }

      if (defined(invoker.enable_checked_call)) {
        defines += [ "TH_ANI_ENABLE_CHECKED_CALL" ]
      }

      external_deps = [
        "runtime_core:ani",
        "runtime_core:libarkruntime",
      ]
      if (defined(invoker.external_deps)) {
        external_deps += invoker.external_deps
      }
    }
  }
}