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

config("upms_config") {
  visibility = [ ":*" ]
  visibility += [ "${ability_runtime_test_path}/unittest/*" ]
  include_dirs = [
    "include",
    "${ability_runtime_utils_path}/global/constant",
  ]
  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }
}

libupms_sources = [
  "src/batch_uri.cpp",
  "src/dynamic_feature_manager.cpp",
  "src/file_permission_manager.cpp",
  "src/file_uri_distribution_utils.cpp",
  "src/tokenid_permission.cpp",
  "src/uri_permission_manager_service.cpp",
  "src/uri_permission_manager_stub_impl.cpp",
]

#build so
ohos_shared_library("libupms") {
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    cfi_policy = "adaptive"
    debug = false
  }
  branch_protector_ret = "pac_ret"
  shlib_type = "sa"
  configs = [
    "${ability_runtime_innerkits_path}/app_manager:appmgr_sdk_config",
    "${ability_runtime_services_path}/common:common_config",
    "${ability_runtime_services_path}/abilitymgr:abilityms_config",
  ]
  public_configs = [ ":upms_config" ]

  include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper" ]

  sources = libupms_sources

  deps = [
    "${ability_runtime_innerkits_path}/ability_manager:ability_manager",
    "${ability_runtime_innerkits_path}/deps_wrapper:ability_deps_wrapper",
    "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr",
    "${ability_runtime_native_path}/appkit:appkit_manager_helper",
    "${ability_runtime_services_path}/common:app_util",
    "${ability_runtime_services_path}/common:perm_verification",
  ]

  external_deps = [
    "ability_base:want",
    "ability_base:zuri",
    "app_file_service:fileuri_native",
    "access_token:libaccesstoken_sdk",
    "access_token:libtokenid_sdk",
    "bundle_framework:appexecfwk_base",
    "bundle_framework:appexecfwk_core",
    "c_utils:utils",
    "ffrt:libffrt",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "ipc:ipc_core",
    "safwk:system_ability_fwk",
    "samgr:samgr_proxy",
  ]

  defines = []
  if (ability_runtime_feature_sandboxmanager) {
    external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ]
    defines += [ "ABILITY_RUNTIME_FEATURE_SANDBOXMANAGER" ]
  }
  if (ability_runtime_media_library_enable) {
    sources += [ "src/media_permission_manager.cpp" ]
    defines += [ "ABILITY_RUNTIME_MEDIA_LIBRARY_ENABLE" ]
  }

  if (ability_runtime_udmf_enable) {
    sources += [ "src/upms_udmf_utils.cpp" ]
    external_deps += [ "udmf:udmf_client" ]
    defines += [ "ABILITY_RUNTIME_UDMF_ENABLE" ]
  }

  ldflags = [ "-Wl,-Bsymbolic" ]

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

# Note: Just for test
ohos_static_library("libupms_static") {
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../../test/cfi_blocklist.txt"
  }
  branch_protector_ret = "pac_ret"
  configs = [
    "${ability_runtime_innerkits_path}/app_manager:appmgr_sdk_config",
    "${ability_runtime_services_path}/common:common_config",
  ]
  public_configs = [ ":upms_config" ]

  include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper" ]

  sources = libupms_sources

  deps = [
    "${ability_runtime_innerkits_path}/ability_manager:ability_manager",
    "${ability_runtime_innerkits_path}/deps_wrapper:ability_deps_wrapper",
    "${ability_runtime_services_path}/common:app_util",
    "${ability_runtime_services_path}/common:perm_verification",
  ]

  external_deps = [
    "ability_base:want",
    "ability_base:zuri",
    "access_token:libaccesstoken_sdk",
    "access_token:libtokenid_sdk",
    "app_file_service:fileuri_native",
    "bundle_framework:appexecfwk_base",
    "bundle_framework:appexecfwk_core",
    "c_utils:utils",
    "ffrt:libffrt",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "ipc:ipc_core",
    "safwk:system_ability_fwk",
    "samgr:samgr_proxy",
  ]
  if (ability_runtime_upms) {
    deps += [
      "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr",
    ]
  }

  defines = []

  if (ability_runtime_feature_sandboxmanager) {
    external_deps += [ "sandbox_manager:libsandbox_manager_sdk" ]
    defines += [ "ABILITY_RUNTIME_FEATURE_SANDBOXMANAGER" ]
  }

  if (ability_runtime_media_library_enable) {
    sources += [ "src/media_permission_manager.cpp" ]
    defines += [ "ABILITY_RUNTIME_MEDIA_LIBRARY_ENABLE" ]
  }

  if (ability_runtime_udmf_enable) {
    sources += [ "src/upms_udmf_utils.cpp" ]
    external_deps += [ "udmf:udmf_client" ]
    defines += [ "ABILITY_RUNTIME_UDMF_ENABLE" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}