#
# Copyright (c) 2024-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/ohos.gni")
import("//foundation/ability/ability_runtime/ability_runtime.gni")

group("ams_common_target") {
  deps = [
    ":perm_verification",
    ":task_handler_wrap",
    ":image_native_wrap",
  ]
}

config("common_config") {
  visibility = [ ":*" ]
  visibility += [
    "${ability_runtime_innerkits_path}/*",
    "${ability_runtime_napi_path}/*",
    "${ability_runtime_native_path}/ability/native/*",
    "${ability_runtime_native_path}/child_process/*",
    "${ability_runtime_path}/frameworks/c/ability_runtime/*",
    "${ability_runtime_path}/frameworks/simulator/ability_simulator/*",
    "${ability_runtime_path}/tools/aa/*",
    "${ability_runtime_path}/tools/ohos-aa/*",
    "${ability_runtime_path}/tools/cc/*",
    "${ability_runtime_services_path}/common/*",
    "${ability_runtime_services_path}/quickfixmgr/*",
    "${ability_runtime_services_path}/uripermmgr/*",
    "${ability_runtime_test_path}/*",
    "${ability_runtime_utils_path}/global/common/*",
    "${hiebpf_path}/*",
    "${power_manager_path}/utils/*",
    "${request_path}/common/*",
  ]
  include_dirs = [
    "include",
    "${ability_runtime_innerkits_path}/ability_manager/include",
  ]
  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }
}

config("optimize_config") {
  cflags = [
    "-flto=full",
    "-fdata-sections",
    "-ffunction-sections",
    "-fstack-protector-strong",
    "-D_FORTIFY_SOURCE=2",
    "-Oz",
  ]
  cflags_cc = [
    "-flto=full",
    "-fdata-sections",
    "-ffunction-sections",
    "-fstack-protector-strong",
    "-D_FORTIFY_SOURCE=2",
    "-Oz",
  ]
  ldflags = [
    "-flto=full",
    "-Wl",
  ]
}

# Config for TDD static libraries - no Bsymbolic flag
# Used by static library targets to enable symbol mocking in tests
config("tdd_config") {
  cflags = [
    "-flto=full",
    "-fdata-sections",
    "-ffunction-sections",
    "-fstack-protector-strong",
    "-D_FORTIFY_SOURCE=2",
    "-Oz",
  ]
  cflags_cc = [
    "-flto=full",
    "-fdata-sections",
    "-ffunction-sections",
    "-fstack-protector-strong",
    "-D_FORTIFY_SOURCE=2",
    "-Oz",
  ]
  # NOTE: No Bsymbolic flag here to allow symbol mocking
  ldflags = [
    "-flto=full",
    "-Wl",
  ]
}

#build so
ohos_shared_library("perm_verification") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  include_dirs = [ "${ability_runtime_utils_path}/server/constant" ]

  public_configs = [ ":common_config" ]

  sources = [ "src/permission_verification.cpp" ]

  deps = [
    ":app_util",
    ":hisysevent_report",
  ]

  external_deps = [
    "ability_base:want",
    "access_token:libaccesstoken_sdk",
    "access_token:libtokenid_sdk",
    "c_utils:utils",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "init:libbegetutil",
    "ipc:ipc_core",
  ]

  cflags_cc = []
  if (os_dlp_part_enabled) {
    cflags_cc += [ "-DWITH_DLP" ]
  }

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

ohos_static_library("perm_verification_static") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:tdd_config" ]
  public_configs = [ ":common_config" ]
  include_dirs = [ "${ability_runtime_utils_path}/server/constant" ]

  sources = [ "src/permission_verification.cpp" ]

  deps = [
    ":app_util_static",
    ":hisysevent_report_static",
  ]

  external_deps = [
    "ability_base:want",
    "access_token:libaccesstoken_sdk",
    "access_token:libtokenid_sdk",
    "c_utils:utils",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "init:libbegetutil",
    "ipc:ipc_core",
  ]

  cflags_cc = []
  if (os_dlp_part_enabled) {
    cflags_cc += [ "-DWITH_DLP" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("task_handler_wrap") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:optimize_config" ]
  public_configs = [ ":common_config" ]
  include_dirs = [ "include" ]

  sources = [
    "src/event_handler_wrap.cpp",
    "src/ffrt_task_handler_wrap.cpp",
    "src/queue_task_handler_wrap.cpp",
    "src/task_handler_wrap.cpp",
  ]

  external_deps = [
    "ffrt:libffrt",
    "hilog:libhilog",
  ]

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_static_library("task_handler_wrap_static") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:tdd_config" ]
  public_configs = [ ":common_config" ]
  include_dirs = [ "include" ]

  sources = [
    "src/event_handler_wrap.cpp",
    "src/ffrt_task_handler_wrap.cpp",
    "src/queue_task_handler_wrap.cpp",
    "src/task_handler_wrap.cpp",
  ]

  external_deps = [
    "ffrt:libffrt",
    "hilog:libhilog",
  ]

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("hisysevent_report") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    cfi_policy = "adaptive"
    debug = false
  }
  include_dirs = [ "include" ]

  sources = [
    "src/hisysevent_report.cpp",
  ]

  external_deps = [
    "hilog:libhilog",
    "c_utils:utils",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
  ]

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_static_library("hisysevent_report_static") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }
  configs = [ "${ability_runtime_services_path}/common:tdd_config" ]
  include_dirs = [ "include" ]

  sources = [
    "src/hisysevent_report.cpp",
  ]

  external_deps = [
    "hilog:libhilog",
    "c_utils:utils",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
  ]

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("app_util") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:optimize_config" ]
  public_configs = [ ":common_config" ]
  include_dirs = [ "include" ]

  sources = [
    "src/ability_manager_radar.cpp",
    "src/ability_manager_xcollie.cpp",
    "src/app_utils.cpp",
    "src/event_report.cpp",
    "src/hitrace_chain_utils.cpp",
    "src/json_utils.cpp",
    "src/rate_limiter.cpp",
    "src/record_cost_time_util.cpp",
    "src/res_sched_util.cpp",
  ]

  deps = [
    "${ability_runtime_path}/utils/global/common:runtime_utils",
    ":hisysevent_report",
  ]

  external_deps = [
    "bundle_framework:appexecfwk_base",
    "c_utils:utils",
    "config_policy:configpolicy_util",
    "hicollie:libhicollie",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "hitrace:libhitracechain",
    "init:libbeget_proxy",
    "init:libbegetutil",
    "ipc:ipc_core",
    "ipc:ipc_single",
    "json:nlohmann_json_static",
    "samgr:samgr_proxy",
  ]

  if (ability_runtime_graphics) {
    defines = [ "SUPPORT_GRAPHICS" ]
    external_deps += [ "window_manager:libwsutils" ]
  }

  if (resource_schedule_service_enable) {
    defines += [ "RESOURCE_SCHEDULE_SERVICE_ENABLE" ]
    external_deps += [ "resource_schedule_service:ressched_client" ]
  }

  if (ability_runtime_media_library_enable) {
    defines += [ "ABILITY_RUNTIME_MEDIA_LIBRARY_ENABLE" ]
  }

  if (ability_runtime_forbid_start_enabled) {
    defines += [ "ABILITY_RUNTIME_FORBID_START_ENABLED" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_static_library("app_util_static") {
  branch_protector_ret = "pac_ret"
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  configs = [ "${ability_runtime_services_path}/common:tdd_config" ]
  public_configs = [ ":common_config" ]
  include_dirs = [ "include" ]

  sources = [
    "src/ability_manager_radar.cpp",
    "src/ability_manager_xcollie.cpp",
    "src/app_utils.cpp",
    "src/event_report.cpp",
    "src/hitrace_chain_utils.cpp",
    "src/json_utils.cpp",
    "src/rate_limiter.cpp",
    "src/record_cost_time_util.cpp",
    "src/res_sched_util.cpp",
  ]

  deps = [
    "${ability_runtime_path}/utils/global/common:runtime_utils",
    ":hisysevent_report_static",
  ]

  external_deps = [
    "bundle_framework:appexecfwk_base",
    "c_utils:utils",
    "config_policy:configpolicy_util",
    "hicollie:libhicollie",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "hitrace:libhitracechain",
    "init:libbeget_proxy",
    "init:libbegetutil",
    "ipc:ipc_core",
    "ipc:ipc_single",
    "json:nlohmann_json_static",
    "samgr:samgr_proxy",
  ]

  if (ability_runtime_graphics) {
    defines = [ "SUPPORT_GRAPHICS" ]
    external_deps += [ "window_manager:libwsutils" ]
  }

  if (resource_schedule_service_enable) {
    defines += [ "RESOURCE_SCHEDULE_SERVICE_ENABLE" ]
    external_deps += [ "resource_schedule_service:ressched_client" ]
  }

  if (ability_runtime_media_library_enable) {
    defines += [ "ABILITY_RUNTIME_MEDIA_LIBRARY_ENABLE" ]
  }

  if (ability_runtime_forbid_start_enabled) {
    defines += [ "ABILITY_RUNTIME_FORBID_START_ENABLED" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("user_controller") {
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    cfi_policy = "adaptive"
    debug = false
  }
  branch_protector_ret = "pac_ret"

  include_dirs = [
    "include/user_controller",
    "${ability_runtime_innerkits_path}/ability_manager/include",
    "${ability_runtime_services_path}/common/include",
  ]

  defines = []

  sources = [ "src/user_controller/user_controller.cpp" ]

  deps =
      [ "${ability_runtime_innerkits_path}/deps_wrapper:ability_deps_wrapper" ]

  external_deps = [
    "c_utils:utils",
    "ffrt:libffrt",
    "hilog:libhilog",
    "ipc:ipc_single",
  ]

  if (ability_runtime_graphics) {
    defines += [
      "SUPPORT_GRAPHICS",
      "SUPPORT_SCREEN",
    ]

    deps += []
    external_deps += [
      "window_manager:libdm",
      "window_manager:libwsutils",
    ]
  }

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

ohos_static_library("user_controller_static") {
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }
  branch_protector_ret = "pac_ret"
  configs = [ "${ability_runtime_services_path}/common:tdd_config" ]

  include_dirs = [
    "include/user_controller",
    "${ability_runtime_innerkits_path}/ability_manager/include",
    "${ability_runtime_services_path}/common/include",
  ]

  defines = []

  sources = [ "src/user_controller/user_controller.cpp" ]

  deps =
      [ "${ability_runtime_innerkits_path}/deps_wrapper:ability_deps_wrapper" ]

  external_deps = [
    "c_utils:utils",
    "ffrt:libffrt",
    "hilog:libhilog",
    "ipc:ipc_single",
  ]

  if (ability_runtime_graphics) {
    defines += [
      "SUPPORT_GRAPHICS",
      "SUPPORT_SCREEN",
    ]

    deps += []
    external_deps += [
      "window_manager:libdm",
      "window_manager:libwsutils",
    ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("image_native_bridge") {
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    cfi_vcall_icall_only = true
    cfi_policy = "adaptive"
    debug = false
  }
  branch_protector_ret = "pac_ret"

  public_configs = [ ":common_config" ]
  include_dirs = [ "include" ]

  sources = [ "src/pixel_map_bridge.cpp" ]

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

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

ohos_shared_library("image_native_wrap") {
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    cfi_vcall_icall_only = true
    cfi_policy = "adaptive"
    debug = false
  }
  branch_protector_ret = "pac_ret"

  include_dirs = [ "include" ]

  sources = [ "src/pixel_map_wrap.cpp" ]

  external_deps = [
    "c_utils:utils",
    "hilog:libhilog",
    "image_framework:image_native",
    "ipc:ipc_single",
  ]

  subsystem_name = "ability"
  part_name = "ability_runtime"
}