# Copyright (c) 2021-2022 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("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni")
import("//build/ohos.gni")
import("//foundation/ability/ability_runtime/ability_runtime.gni")

es2abc_gen_abc("gen_delegator_mgmt_abc") {
  src_js = rebase_path(
          "${ability_runtime_native_path}/appkit/ability_delegator/runner_runtime/delegator_mgmt.js")
  dst_file = rebase_path(target_out_dir + "/delegator_mgmt.abc")
  in_puts = [ "${ability_runtime_native_path}/appkit/ability_delegator/runner_runtime/delegator_mgmt.js" ]
  out_puts = [ target_out_dir + "/delegator_mgmt.abc" ]
  extra_args = [ "--module" ]
}

gen_js_obj("delegator_mgmt_js") {
  input = "${ability_runtime_native_path}/appkit/ability_delegator/runner_runtime/delegator_mgmt.js"
  output = target_out_dir + "/delegator_mgmt.o"
}

gen_js_obj("delegator_mgmt_abc") {
  input = get_label_info(":gen_delegator_mgmt_abc", "target_out_dir") +
          "/delegator_mgmt.abc"
  output = target_out_dir + "/delegator_mgmt_abc.o"
  dep = ":gen_delegator_mgmt_abc"
}

config("appkit_exception_config") {
  cflags_cc = [ "-fexceptions" ]
}

config("appkit_config") {
  defines = [ "AMS_LOG_TAG = \"Appkit\"" ]
  if (target_cpu == "arm64") {
    defines += [ "_ARM64_" ]
  }

  include_dirs = [
    "${ability_runtime_path}/interfaces/kits/native/ability/native",
    "//third_party/json/include",
  ]
}

config("appkit_public_config") {
  visibility = [ ":*" ]
  include_dirs = [
    "${ability_base_kits_path}/configuration/include",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_delegator",
    "${global_path}/resource_management/interfaces/inner_api/include",
  ]

  if (ability_runtime_graphics) {
    include_dirs += [
      "${global_path}/i18n/frameworks/intl/include",
      "//third_party/icu/icu4c/source/common",
    ]

    defines = [ "SUPPORT_GRAPHICS" ]
  }
}

# build so
ohos_shared_library("appkit_native") {
  include_dirs = [
    "native",
    "${ability_runtime_path}/interfaces/kits/native/appkit",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/app",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context",
    "${ability_runtime_path}/interfaces/kits/native/appkit/app",
    "${ability_runtime_path}/interfaces/kits/native/appkit/app/task",
    "${ability_runtime_services_path}/common/include",
    "${ability_runtime_path}/interfaces/kits/native/ability/native",
    "${ability_runtime_services_path}/abilitymgr/include",
    "${ability_runtime_napi_path}/inner/napi_common",
  ]

  configs = [
    ":appkit_config",
    ":appkit_exception_config",
  ]

  public_configs = [ ":appkit_public_config" ]

  sources = [
    "${ability_runtime_native_path}/appkit/ability_runtime/app/ability_stage.cpp",
    "${ability_runtime_native_path}/appkit/ability_runtime/app/js_ability_stage.cpp",
    "${ability_runtime_native_path}/appkit/ability_runtime/app/js_ability_stage_context.cpp",
    "${ability_runtime_native_path}/appkit/app/ability_manager.cpp",
    "${ability_runtime_native_path}/appkit/app/ability_record_mgr.cpp",
    "${ability_runtime_native_path}/appkit/app/app_loader.cpp",
    "${ability_runtime_native_path}/appkit/app/application_data_manager.cpp",
    "${ability_runtime_native_path}/appkit/app/application_env.cpp",
    "${ability_runtime_native_path}/appkit/app/application_env_impl.cpp",
    "${ability_runtime_native_path}/appkit/app/application_impl.cpp",
    "${ability_runtime_native_path}/appkit/app/main_thread.cpp",
    "${ability_runtime_native_path}/appkit/app/mix_stack_dumper.cpp",
    "${ability_runtime_native_path}/appkit/app/ohos_application.cpp",
    "${ability_runtime_native_path}/appkit/app/watchdog.cpp",
  ]
  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }
  deps = [
    ":appkit_delegator",
    "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr",
    "${ability_runtime_native_path}/appkit:app_context",
    "${ability_runtime_native_path}/appkit:app_context_utils",
    "${ability_runtime_native_path}/runtime:string_utils",
  ]

  public_deps =
      [ "${global_path}/resource_management/frameworks/resmgr:global_resmgr" ]
  external_deps = [
    "ability_base:configuration",
    "ability_base:want",
    "ability_runtime:ability_manager",
    "ability_runtime:abilitykit_native",
    "ability_runtime:app_manager",
    "ability_runtime:form_extension",
    "ability_runtime:runtime",
    "ability_runtime:static_subscriber_extension",
    "bundle_framework:appexecfwk_base",
    "c_utils:utils",
    "common_event_service:cesfwk_innerkits",
    "eventhandler:libeventhandler",
    "faultloggerd:lib_dfx_dump_catcher",
    "faultloggerd:libfaultloggerd",
    "hicollie_native:libhicollie",
    "hisysevent_native:libhisysevent",
    "hitrace_native:hitrace_meter",
    "hiviewdfx_hilog_native:libhilog",
    "init:libbegetutil",
    "ipc:ipc_core",
    "samgr:samgr_proxy",
  ]

  if (ability_runtime_graphics) {
    deps += [ "//third_party/icu/icu4c:shared_icuuc" ]
    public_deps += [ "${global_path}/i18n/frameworks/intl:intl_util" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

# build so
ohos_shared_library("app_context") {
  include_dirs = [
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context",
    "${ability_runtime_path}/interfaces/kits/native/appkit/app",
  ]

  configs = [ ":appkit_config" ]

  public_configs = [ ":appkit_public_config" ]

  sources = [
    "${ability_runtime_native_path}/appkit/ability_runtime/context/ability_lifecycle_callback.cpp",
    "${ability_runtime_native_path}/appkit/ability_runtime/context/application_context.cpp",
    "${ability_runtime_native_path}/appkit/ability_runtime/context/context_impl.cpp",
    "${ability_runtime_native_path}/appkit/ability_runtime/context/environment_callback.cpp",
    "${ability_runtime_native_path}/appkit/app/sys_mgr_client.cpp",
  ]
  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }
  deps = [ "//base/hiviewdfx/hiview/adapter/utility:hiview_adapter_utility" ]

  public_deps =
      [ "${global_path}/resource_management/frameworks/resmgr:global_resmgr" ]

  external_deps = [
    "ability_base:configuration",
    "ability_runtime:ability_deps_wrapper",
    "ability_runtime:runtime",
    "bundle_framework:appexecfwk_base",
    "bundle_framework:appexecfwk_core",
    "c_utils:utils",
    "hiviewdfx_hilog_native:libhilog",
    "ipc:ipc_core",
    "napi:ace_napi",
    "samgr:samgr_proxy",
  ]

  if (ability_runtime_graphics) {
    deps += [ "//third_party/icu/icu4c:shared_icuuc" ]
    public_deps += [ "${global_path}/i18n/frameworks/intl:intl_util" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

# build so
ohos_shared_library("app_context_utils") {
  include_dirs = [
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/app",
  ]

  configs = [ ":appkit_config" ]

  public_configs = [ ":appkit_public_config" ]

  sources = [
    "ability_runtime/context/js_application_context_utils.cpp",
    "ability_runtime/context/js_context_utils.cpp",
    "ability_runtime/context/js_hap_module_info_utils.cpp",
    "ability_runtime/context/js_resource_manager_utils.cpp",
  ]
  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }
  deps = [
    "${ability_runtime_native_path}/appkit:app_context",
    "//base/hiviewdfx/hiview/adapter/utility:hiview_adapter_utility",
  ]

  public_deps =
      [ "${global_path}/resource_management/frameworks/resmgr:global_resmgr" ]

  external_deps = [
    "ability_runtime:runtime",
    "bundle_framework:appexecfwk_base",
    "bundle_framework:appexecfwk_core",
    "c_utils:utils",
    "hiviewdfx_hilog_native:libhilog",
    "ipc:ipc_core",
    "napi:ace_napi",
    "resource_management:resmgr_napi_core",
  ]

  if (ability_runtime_graphics) {
    deps += [ "//third_party/icu/icu4c:shared_icuuc" ]
    public_deps += [ "${global_path}/i18n/frameworks/intl:intl_util" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}

# build so
ohos_shared_library("appkit_delegator") {
  include_dirs = [
    "native",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/app",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context",
    "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/ability_delegator/include",
    "${ability_runtime_services_path}/common/include",
    "${ability_runtime_path}/interfaces/kits/native/ability/native",
    "${multimodalinput_path}/frameworks/proxy:libmmi-client",
  ]

  configs = [ ":appkit_config" ]

  public_configs = [ ":appkit_public_config" ]

  sources = [
    "${ability_runtime_native_path}/appkit/ability_delegator/ability_delegator.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/ability_delegator_args.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/ability_delegator_registry.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/delegator_thread.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/iability_monitor.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/iability_stage_monitor.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/runner_runtime/js_test_runner.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/shell_cmd_result.cpp",
    "${ability_runtime_native_path}/appkit/ability_delegator/test_runner.cpp",
    "${ability_runtime_path}/tools/aa/src/shell_command_result.cpp",
    "${ability_runtime_path}/tools/aa/src/test_observer_proxy.cpp",
  ]

  cflags = []
  if (target_cpu == "arm") {
    cflags += [ "-DBINDER_IPC_32BIT" ]
  }

  deps = [
    ":delegator_mgmt_abc",
    ":delegator_mgmt_js",
    "${ability_runtime_native_path}/appkit:app_context",
  ]

  external_deps = [
    "ability_base:want",
    "ability_runtime:ability_manager",
    "ability_runtime:runtime",
    "bundle_framework:appexecfwk_base",
    "c_utils:utils",
    "eventhandler:libeventhandler",
    "hiviewdfx_hilog_native:libhilog",
    "ipc:ipc_core",
  ]

  public_deps = [ "//foundation/arkui/napi:ace_napi" ]

  if (ability_runtime_graphics) {
    deps += [ "//third_party/icu/icu4c:shared_icuuc" ]
  }

  subsystem_name = "ability"
  part_name = "ability_runtime"
}
