# 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("//base/inputmethod/imf/inputmethod.gni")
import("//build/config/components/ets_frontend/ets2abc_config.gni")
import("//build/ohos.gni")
import("//build/ohos/taihe_idl/taihe.gni")

subsystem_name = "inputmethod"
part_name = "imf"
taihe_generated_file_path =
    "$taihe_file_path/out/$subsystem_name/$part_name/inputMethodEngine"

copy_taihe_idl("copy_taihe_deps") {
  sources = [ "idl/ohos.inputMethod.ExtraConfig.taihe" ]
}

copy_taihe_idl("inputMethodEngine_taihe") {
  sources = [ "idl/ohos.inputMethodEngine.taihe" ]
  deps = [
    ":copy_taihe_deps",
    "${inputmethod_path}/frameworks/ets/taihe/inputMethod:copy_taihe_deps",
  ]
  external_deps = [
    "input:input_event_taihe",
    "input:key_code_taihe",
    "input:key_event_taihe",
  ]
}

ohos_taihe("run_taihe") {
  taihe_generated_file_path = "$taihe_generated_file_path"
  deps = [ ":inputMethodEngine_taihe" ]
  outputs = [
    "$taihe_generated_file_path/src/ohos.inputMethodEngine.ani.cpp",
    "$taihe_generated_file_path/src/ohos.inputMethodEngine.abi.c",
    "$taihe_generated_file_path/src/ohos.inputMethod.ExtraConfig.ani.cpp",
    "$taihe_generated_file_path/src/ohos.inputMethod.ExtraConfig.abi.c",
  ]
}

taihe_shared_library("inputmethod_engine_taihe_native") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
    integer_overflow = true
    ubsan = true
  }
  cflags_cc = [
    "-fvisibility=hidden",
    "-fvisibility-inlines-hidden",
    "-fdata-sections",
    "-ffunction-sections",
    "-Os",
  ]
  taihe_generated_file_path = "$taihe_generated_file_path"
  subsystem_name = "$subsystem_name"
  part_name = "$part_name"
  include_dirs = [
    "include",
    "${inputmethod_path}/frameworks/ets/taihe/include",
    "${inputmethod_path}/frameworks/ets/taihe/inputMethod/include",
    "${inputmethod_path}/frameworks/js/napi/inputmethodability",
    "${inputmethod_path}/frameworks/js/napi/inputmethodclient",
    "${inputmethod_path}/frameworks/native/inputmethod_controller/include",
  ]
  sources = get_target_outputs(":run_taihe")
  sources += [
    "${inputmethod_path}/frameworks/js/napi/inputmethodclient/js_utils.cpp",
    "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_method_utils.cpp",
    "src/ani_common_engine.cpp",
    "src/ani_constructor.cpp",
    "src/input_client_impl.cpp",
    "src/input_method_ability_impl.cpp",
    "src/input_method_client_impl.cpp",
    "src/input_method_keyboard_delegate_impl.cpp",
    "src/input_method_panel_impl.cpp",
    "src/input_method_panel_listener.cpp",
    "src/keyboard_controller_impl.cpp",
    "src/ohos.inputMethodEngine.impl.cpp",
  ]
  deps = [
    ":run_taihe",
    "${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common",
    "${inputmethod_path}/interfaces/inner_api/inputmethod_ability:inputmethod_ability",
    "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client",
  ]
  external_deps = [
    "ability_base:want",
    "ability_runtime:abilitykit_native",
    "ability_runtime:ani_base_context",
    "ability_runtime:extensionkit_native",
    "ability_runtime:napi_common",
    "bundle_framework:bms_ani_common",
    "c_utils:utils",
    "eventhandler:libeventhandler",
    "ffrt:libffrt",
    "hilog:libhilog",
    "icu:shared_icui18n",
    "icu:shared_icuuc",
    "ipc:ipc_core",
    "ipc:ipc_single",
    "samgr:samgr_proxy",
    "napi:ace_napi",
    "node:node_header_notice",
  ]
  defines = []
  if (window_manager_use_sceneboard) {
    defines += [ "SCENE_BOARD_ENABLE" ]
  }
  shlib_type = "ani"
}

generate_static_abc("inputmethod_engine_abc") {
  base_url = "$taihe_generated_file_path"
  files = [ "$taihe_generated_file_path/@ohos.inputMethodEngine.ets" ]
  is_boot_abc = "True"
  device_dst_file = "/system/framework/inputmethod_engine_abc.abc"
  dependencies = [ ":run_taihe" ]
}

ohos_prebuilt_etc("inputmethod_engine_etc") {
  source = "$target_out_dir/inputmethod_engine_abc.abc"
  module_install_dir = "framework"
  part_name = "$part_name"
  subsystem_name = "$subsystem_name"
  deps = [ ":inputmethod_engine_abc" ]
}

generate_static_abc("inputmethod_extraconfig_abc") {
  base_url = "$taihe_generated_file_path"
  files = [ "$taihe_generated_file_path/@ohos.inputMethod.ExtraConfig.ets" ]
  is_boot_abc = "True"
  device_dst_file = "/system/framework/inputmethod_extraconfig_abc.abc"
  dependencies = [ ":run_taihe" ]
}

ohos_prebuilt_etc("inputmethod_extraconfig_etc") {
  source = "$target_out_dir/inputmethod_extraconfig_abc.abc"
  module_install_dir = "framework"
  part_name = "$part_name"
  subsystem_name = "$subsystem_name"
  deps = [ ":inputmethod_extraconfig_abc" ]
}

group("inputmethod_engine_taihe") {
  deps = [
    ":inputmethod_engine_etc",
    ":inputmethod_engine_taihe_native",
    ":inputmethod_extraconfig_etc",
  ]
}