# Copyright (c) 2024 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_runtime/js_runtime_config.gni")
import("//build/ohos.gni")
import("//build/ohos/sa_profile/sa_profile.gni")

declare_args() {
  code_sign_enable = true

  if (defined(global_parts_info) &&
      !defined(global_parts_info.security_code_signature)) {
    code_sign_enable = false
  }
}

config("aot_compiler_service_config") {
  visibility = [ ":*" ]
  include_dirs = [
    "include",
    "interface",
  ]
}

ohos_shared_library("libcompiler_service") {
  public_configs = [ ":aot_compiler_service_config" ]
  configs = [
    "$js_root:ark_jsruntime_public_config",
    "$js_root:ark_jsruntime_common_config",
  ]
  shlib_type = "sa"
  version_script = "libaot_compiler_service.map"
  sources = [
    "${js_root}/common_components/log/log.cpp",
    "${js_root}/ecmascript/compiler/aot_compiler_stats.cpp",
    "interface/aot_compiler_interface_proxy.cpp",
    "interface/aot_compiler_interface_stub.cpp",
    "src/aot_args_handler.cpp",
    "src/aot_args_verify.cpp",
    "src/aot_compiler_client.cpp",
    "src/aot_compiler_error_utils.cpp",
    "src/aot_compiler_impl.cpp",
    "src/aot_compiler_load_callback.cpp",
    "src/aot_compiler_service.cpp",
    "src/power_disconnected_listener.cpp",
    "src/screen_status_listener.cpp",
    "src/thermal_mgr_listener.cpp",
  ]
  if (enable_target_compilation) {
    sources += [
      "${js_root}/ecmascript/platform/unix/ohos/directory.cpp",
      "${js_root}/ecmascript/platform/unix/linux/os.cpp",
    ]
  } else if (is_mingw) {
    sources += [
      "${js_root}/ecmascript/platform/windows/directory.cpp",
      "${js_root}/ecmascript/platform/windows/os.cpp",
    ]
  } else {
    sources += [
      "${js_root}/ecmascript/platform/common/filesystem.cpp",
      "${js_root}/ecmascript/platform/unix/directory.cpp",
      "${js_root}/ecmascript/platform/unix/linux/os.cpp",
    ]
  }
  if (is_mingw) {
    sources += [
      "${js_root}/ecmascript/platform/windows/file.cpp",
    ]
  } else {
    sources += [
      "${js_root}/ecmascript/platform/unix/file.cpp",
    ]
  }
  external_deps = [
    "ability_base:want",
    "access_token:libaccesstoken_sdk",
    "access_token:libtokenid_sdk",
    "c_utils:utils",
    "common_event_service:cesfwk_innerkits",
    "eventhandler:libeventhandler",
    "hilog:libhilog",
    "hisysevent:libhisysevent",
    "hitrace:hitrace_meter",
    "icu:shared_icui18n",
    "icu:shared_icuuc",
    "ipc:ipc_core",
    "runtime_core:libarkfile_static",
    "safwk:system_ability_fwk",
    "samgr:samgr_proxy",
  ]

  defines = []
  if (code_sign_enable) {
    external_deps += [ "code_signature:liblocal_code_sign_sdk" ]
    defines += [ "CODE_SIGN_ENABLE" ]
  }

  if (!ark_standalone_build && !(defined(is_arkui_x) && is_arkui_x) &&
      is_ohos && is_standard_system) {
    external_deps += [ "init:libbegetutil" ]
    defines += [ "ENABLE_COMPILER_SERVICE_GET_PARAMETER" ]
  }

  install_enable = true
  install_images = [ "system" ]

  part_name = "ets_runtime"
  subsystem_name = "arkcompiler"
}

ohos_prebuilt_etc("compiler_service_cfg") {
  source = "compiler_service.cfg"
  relative_install_dir = "init"
  part_name = "ets_runtime"
  subsystem_name = "arkcompiler"
}

ohos_prebuilt_etc("ark_aot_compiler.cfg") {
  source = "ark_aot_compiler.cfg"
  relative_install_dir = "init"

  # Set the subsystem name
  part_name = "ets_runtime"
  subsystem_name = "arkcompiler"
}

ohos_prebuilt_etc("system_framework_aot_enable_list.conf") {
  relative_install_dir = "ark"
  source = "system_framework_aot_enable_list.conf"

  # Set the subsystem name
  part_name = "ets_runtime"
  subsystem_name = "arkcompiler"
}

ohos_sa_profile("compiler_sa_profile") {
  sources = [ "sa_profile/5300.json" ]
  part_name = "ets_runtime"
}

group("compiler_service") {
  deps = [
    ":compiler_sa_profile",
    ":compiler_service_cfg",
    ":libcompiler_service",
  ]
}