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

import("$updateengine_root_path/foundations/foundations.gni")

copy_taihe_idl("copy_update_framework") {
  sources = [ "idl/ohos.update.taihe" ]
}

subsystem_name = "updater"
part_name = "update_service"
taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name"
ohos_taihe("run_taihe_update") {
  taihe_generated_file_path = "$taihe_generated_file_path"
  deps = [ ":copy_update_framework" ]
  outputs = [
    "$taihe_generated_file_path/src/ohos.update.ani.cpp",
    "$taihe_generated_file_path/src/ohos.update.abi.c",
  ]
}

ani_includes = [
  "include",
  "$updateengine_root_path/frameworks/js/napi/update/include",
  "$updateengine_root_path/frameworks/js/napi/update/common/include",
]
ani_includes += foundations_include

ani_deps = [
  ":run_taihe_update",
  "$updateengine_root_path/foundations:update_foundations",
  "$updateengine_root_path/frameworks/js/napi/update:$updateengine_client_library_name",
  "$updateengine_root_path/interfaces/inner_api/engine:$updateengine_inner_library_name",
]

ani_deps += foundations_deps

ani_external_deps = [
  "cJSON:cjson",
  "c_utils:utils",
  "runtime_core:ani",
  "runtime_core:ani_helpers",
  "runtime_core:libarkruntime",
]
ani_external_deps += foundations_external_deps

taihe_shared_library("update_ani") {
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }
  branch_protector_ret = "pac_ret"
  taihe_generated_file_path = "$taihe_generated_file_path"
  part_name = "$part_name"
  subsystem_name = "$subsystem_name"
  sources = get_target_outputs(":run_taihe_update")
  include_dirs = ani_includes
  sources += [
    "src/ani_base_updater.cpp",
    "src/ani_common_conveter.cpp",
    "src/ani_constructor.cpp",
    "src/ani_local_updater.cpp",
    "src/ani_restorer.cpp",
    "src/ani_updater.cpp",
    "src/taihe_impl.cpp",
  ]
  deps = ani_deps
  external_deps = ani_external_deps
}

generate_static_abc("update_framework_abc") {
  base_url = "$taihe_generated_file_path"
  files = [ "$taihe_generated_file_path/@ohos.update.ets" ]
  is_boot_abc = "True"
  dst_file = "$target_out_dir/update_ani.abc"
  out_puts = [ "$target_out_dir/update_ani.abc" ]
  device_dst_file = "/system/framework/update_ani.abc"
  dependencies = [ ":run_taihe_update" ]
}

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

group("update_framework_taihe") {
  deps = [
    ":update_ani",
    ":update_framework_etc",
  ]
}