# Copyright (c) 2025-2026 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/components/idl_tool/idl.gni")
import("//build/ohos.gni")
import("//foundation/ability/ability_runtime/ability_runtime.gni")

ohos_shared_library("agent_fwk") {
  branch_protector_ret = "pac_ret"

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:optimize_config" ]

  include_dirs = [
    "include",
    "${ability_runtime_innerkits_path}/ability_manager/include",
    "${ability_runtime_services_path}/common/include",
    "${agent_runtime_framework_path}/interfaces/kits/native/agent_extension/connection/include",
  ]
  sources = [
    "src/agent_card.cpp",
  ]

  public_configs = []

  deps = []

  external_deps = [
    "c_utils:utils",
    "hilog:libhilog",
    "json:nlohmann_json_static",
  ]

  innerapi_tags = [ "platformsdk" ]
  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("agent_connection_manager") {
  branch_protector_ret = "pac_ret"

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:optimize_config" ]

  include_dirs = [
    "include",
    "${ability_runtime_innerkits_path}/ability_manager/include",
    "${ability_runtime_services_path}/common/include",
    "${agent_runtime_framework_path}/interfaces/kits/native/agent_extension/connection/include",
  ]
  sources = [
    "src/agent_connection_manager.cpp",
  ]

  public_configs = []

  deps = [
    ":agent_manager",
    "${ability_runtime_innerkits_path}/ability_manager:ability_connect_callback_stub",
    "${ability_runtime_native_path}/ability:ability_context_native",
  ]

  external_deps = [
    "ability_base:want",
    "c_utils:utils",
    "hilog:libhilog",
    "ipc:ipc_core",
  ]

  innerapi_tags = [ "platformsdk" ]
  subsystem_name = "ability"
  part_name = "ability_runtime"
}

idl_interface_sources = [
  "${target_gen_dir}/agent_manager_proxy.cpp",
  "${target_gen_dir}/agent_manager_stub.cpp",
]

idl_gen_interface("agent_manager_interface") {
  src_idl = rebase_path("IAgentManager.idl")
  dst_file = string_join(",", idl_interface_sources)
  hitrace = "HITRACE_TAG_ABILITY_MANAGER"
  log_domainid = "0xD001353"
  log_tag = "AgentManagerService"
}

config("agent_manager_config") {
  visibility = [ ":*" ]
  include_dirs = [
    "include",
    "${target_gen_dir}",
  ]
  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }
}

ohos_shared_library("agent_manager") {
  branch_protector_ret = "pac_ret"

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  include_dirs = [
    "${ability_runtime_innerkits_path}/ability_manager/include",
    "${ability_runtime_services_path}/common/include",
    "${agent_runtime_framework_path}/interfaces/kits/native/agent_extension/connection/include",
  ]
  configs = [ "${ability_runtime_services_path}/common:optimize_config" ]
  public_configs = [ ":agent_manager_config" ]

  output_values = get_target_outputs(":agent_manager_interface")
  sources = [
    "src/agent_load_callback.cpp",
    "src/agent_manager_client.cpp",
  ]
  sources += filter_include(output_values, [ "*.cpp" ])
  defines = [ "AMS_LOG_TAG = \"AgentManagerService\"" ]
  deps = [
    ":agent_fwk",
    ":agent_manager_interface",
  ]

  external_deps = [
    "ability_base:want",
    "c_utils:utils",
    "hilog:libhilog",
    "hitrace:hitrace_meter",
    "ipc:ipc_single",
    "json:nlohmann_json_static",
    "safwk:system_ability_fwk",
    "samgr:samgr_proxy",
  ]

  innerapi_tags = [ "platformsdk" ]
  subsystem_name = "ability"
  part_name = "ability_runtime"
}