# 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/config/python.gni")
import("//build/templates/cangjie/cjc_toolchain.gni")

declare_args() {
  sdk_build_cangjie_previewer = false
  sdk_build_cangjie_ohos_arm = false
}

template("cj_ohos_sdk_header_copy") {
  forward_variables_from(invoker,
                         [
                           "source_dir",
                           "out_relative_dir",
                           "exculde_dirs",
                         ])

  action_with_pydeps(target_name) {
    script =
        "//interface/sdk_cangjie/build-tools/script/copy_cangjie_headers.py"
    depfile = "${target_gen_dir}/${target_name}.d"
    inputs = [ source_dir ]
    args = [
      "--input",
      rebase_path("${source_dir}", root_build_dir),
      "--output",
      rebase_path("${target_out_dir}/${out_relative_dir}", root_build_dir),
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--stamp",
      rebase_path("$target_gen_dir/$target_name.stamp", root_build_dir),
    ]
    if (defined(exculde_dirs)) {
      args += [
        "--exclude-dirs",
        string_join(",", exculde_dirs),
      ]
    }
    outputs = [ "${target_out_dir}/${out_relative_dir}" ]
  }
}

template("cj_ohos_sdk_shared_lib") {
  forward_variables_from(invoker,
                         [
                           "platform",
                           "toolchain",
                         ])

  _output_dir = "${target_out_dir}/cangjie_sdk/api/lib/${platform}/"

  action_with_pydeps(target_name) {
    script = "//interface/sdk_cangjie/build-tools/script/process_libs.py"
    depfile = "${target_gen_dir}/${target_name}.d"
    external_deps = invoker.external_deps
    deps = [ "build-tools/lib/mocks/mock:ohos.mock(${toolchain})" ]
    _mock_t_out = get_label_info(deps[0], "target_out_dir")
    _mock_so_target = "${_mock_t_out}/libohos.mock.so"
    _source_dir = "${invoker.target_root_out_dir}/obj/interface/sdk_cangjie/cangjie_sdk/api/lib/${platform}"

    args = [
      "--copy-cjo-dir",
      rebase_path(_source_dir, root_build_dir),
      "--mock",
      rebase_path(_mock_so_target, root_build_dir),
      "--output-dir",
      rebase_path(_output_dir, root_build_dir),
      "--depfile",
      rebase_path(depfile, root_build_dir),
    ]

    outputs = [ _output_dir ]
  }
}

cangjie_sdk_macro_libs = [
  "arkui_cangjie_wrapper:ohos.arkui.state_macro_manage",
  "cangjie_ark_interop:ohos.ark_interop_macro",
]
template("cj_ohos_sdk_macro") {
  ohos_copy(target_name) {
    forward_variables_from(invoker,
                           [
                             "toolchain",
                             "module_name",
                             "dyn_extension",
                           ])

    sources = []
    external_deps = []
    foreach(item, cangjie_sdk_macro_libs) {
      external_deps += [
        "${item}(${toolchain})",
        "${item}_cjo(${toolchain})",
      ]
      t_out = get_label_info("${item}(${toolchain})", "target_out_dir")
      t_name = get_label_info("${item}(${toolchain})", "name")

      sources += [
        "${t_out}/${t_name}.cjo",
        "${t_out}/lib-macro_${t_name}${dyn_extension}",
      ]
    }

    outputs = [ "${target_out_dir}/cangjie_sdk/api/macro/${module_name}/{{source_file_part}}" ]
    module_source_dir =
        "${target_out_dir}/cangjie_sdk/api/macro/${module_name}/"
    module_install_name = ""
  }
}

template("cj_ohos_sdk_copy") {
  forward_variables_from(invoker, [ "out_relative_dir" ])

  _module_info_target = "${target_name}_info"

  group(target_name) {
    deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    deps += [ ":${_module_info_target}" ]
  }

  _target_name = target_name
  generate_module_info(_module_info_target) {
    module_type = "unknown"
    module_install_name = ""
    module_name = _target_name
    module_source_dir = "${target_out_dir}/${out_relative_dir}"
    install_enable = false
  }
}