# Copyright (c) 2021-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("//arkcompiler/runtime_core/ark_config.gni")

config("arkfile_public_config") {
  include_dirs = [
    "$ark_root/libpandafile",
    "$ark_root/libziparchive",
    "$target_gen_dir",
    "$target_gen_dir/include",
  ]

  defines = []
  if (is_ohos && !is_standard_system) {
    defines += [ "ENABLE_FULL_FILE_FIELDS" ]
  }
}

config("arkfile_frontend_public_config") {
  defines = [ "ETS_FRONTEND_USE "]
}

if (!ark_standalone_build && !is_arkui_x) {
  ohos_shared_headers("arkfile_public_headers") {
    include_dirs = [
      "$ark_root/libpandafile",
      "$ark_root/libziparchive",
      "$target_gen_dir",
      "$target_gen_dir/include",
    ]

    part_name = "runtime_core"
    subsystem_name = "arkcompiler"
  }
}

config("arkfile_runtime_public_config") {
  include_dirs = [
    "$ark_root/libpandafile",
    "$ark_root/libziparchive",
    "$target_gen_dir",
    "$target_gen_dir/include",
  ]

  defines = [ "ERROR_AS_FATAL" ]
  if (is_ohos && !is_standard_system) {
    defines += [ "ENABLE_FULL_FILE_FIELDS" ]
  }
}

config("data_protect_configs") {
  cflags_cc = [ "-march=armv8.4-a" ]
  defines = [ "PANDA_ENABLE_DATA_PROTECT" ]
}

config("arkfile_fuzz_config") {
  configs = [ "$build_root/config/compiler:exceptions" ]
  defines = [ "SUPPORT_KNOWN_EXCEPTION" ]
}

config("arkfile_verifier_config") {
  include_dirs = [
    "$ark_root/libpandafile",
    "$ark_root/libziparchive",
    "$target_gen_dir",
    "$target_gen_dir/include",
  ]

  defines = []
  if (is_ohos && !is_standard_system) {
    defines += [ "ENABLE_FULL_FILE_FIELDS" ]
  }

  defines += [ "FATAL_AS_ERROR" ]
}

libarkfile_sources = [
  "annotation_data_accessor.cpp",
  "bytecode_emitter.cpp",
  "class_data_accessor.cpp",
  "code_data_accessor.cpp",
  "debug_data_accessor.cpp",
  "debug_info_extractor.cpp",
  "field_data_accessor.cpp",
  "file.cpp",
  "file_format_version.cpp",
  "file_item_container.cpp",
  "file_items.cpp",
  "file_writer.cpp",
  "literal_data_accessor.cpp",
  "method_data_accessor.cpp",
  "method_handle_data_accessor.cpp",
  "module_data_accessor.cpp",
  "pgo.cpp",
  "util/collect_util.cpp",
]

libarkfile_frontend_sources = [
  "annotation_data_accessor.cpp",
  "bytecode_emitter.cpp",
  "class_data_accessor.cpp",
  "code_data_accessor.cpp",
  "debug_data_accessor.cpp",
  "debug_info_extractor.cpp",
  "field_data_accessor.cpp",
  "file.cpp",
  "file_format_version.cpp",
  "file_item_container.cpp",
  "file_items.cpp",
  "file_writer.cpp",
  "literal_data_accessor.cpp",
  "method_data_accessor.cpp",
  "method_handle_data_accessor.cpp",
  "module_data_accessor.cpp",
  "util/collect_util.cpp",
]

libarkfile_configs = [
  "$ark_root:ark_public_config",
  "$ark_root/libpandabase:arkbase_public_config",
  ":arkfile_public_config",
]

libarkfile_runtime_configs = [
  "$ark_root:ark_public_config",
  "$ark_root/libpandabase:arkbase_public_config",
  ":arkfile_runtime_public_config",
]

ohos_static_library("arkfile_header_deps") {
  deps = [
    ":isa_gen_libarkfile_bytecode_emitter_def_gen_h",
    ":isa_gen_libarkfile_bytecode_emitter_gen_h",
    ":isa_gen_libarkfile_bytecode_instruction-inl_gen_h",
    ":isa_gen_libarkfile_bytecode_instruction_enum_gen_h",
    ":isa_gen_libarkfile_file_format_version_h",
    ":isa_gen_libarkfile_tests_bytecode_emitter_tests_gen_h",
    ":libarkfile_type_gen_h",
    ":source_lang_enum_h",
    "$ark_root/libpandabase:arkbase_header_deps",
  ]
  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_static_library("libarkfile_static") {
  sources = libarkfile_sources

  configs = [ "$ark_root:ark_config" ]
  public_configs = libarkfile_configs

  if (enabled_pac_data_protect) {
    configs += libarkfile_configs
    configs += [ ":data_protect_configs" ]
  }

  deps = [
    ":arkfile_header_deps",
    "$ark_root/libpandabase:libarkbase_static",
    "$ark_root/libziparchive:libarkziparchive_static",
  ]

  if (is_arkui_x) {
    deps += [
      "$ark_third_party_root/bounds_checking_function:libsec_static",
      "$ark_third_party_root/zlib:libz",
    ]
  } else {
    external_deps = [
      sdk_libc_secshared_dep,
      zlib_deps,
    ]
  }

  if (enable_hilog) {
    external_deps += [ "hilog:libhilog" ]
  }

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_static_library("libarkfile_runtime_static") {
  sources = libarkfile_sources

  configs = [ "$ark_root:ark_config" ]
  public_configs = libarkfile_runtime_configs

  if (enabled_pac_data_protect) {
    configs += libarkfile_runtime_configs
    configs += [ ":data_protect_configs" ]
  }

  deps = [
    ":arkfile_header_deps",
    "$ark_root/libpandabase:libarkbase_static",
    "$ark_root/libziparchive:libarkziparchive_static",
  ]

  if (is_arkui_x) {
    deps += [
      "$ark_third_party_root/bounds_checking_function:libsec_static",
      "$ark_third_party_root/zlib:libz",
    ]
  } else {
    external_deps = [
      sdk_libc_secshared_dep,
      zlib_deps,
    ]
  }

  if (enable_hilog) {
    external_deps += [ "hilog:libhilog" ]
  }

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_source_set("libarkfile_static_fuzz") {
  sources = libarkfile_sources

  public_configs = libarkfile_configs
  public_configs += [ "$ark_root:ark_config" ]
  public_configs += [ ":arkfile_fuzz_config" ]

  deps = [
    ":arkfile_header_deps",
    "$ark_root/libpandabase:libarkbase_static",
    "$ark_root/libziparchive:libarkziparchive_static",
  ]

  external_deps = [
    "zlib:libz",
    sdk_libc_secshared_dep,
  ]

  if (enable_hilog) {
    external_deps += [ "hilog:libhilog" ]
  }

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_source_set("libarkfile_static_verifier") {
  sources = libarkfile_sources

  public_configs = [
    "$ark_root:ark_config",
    "$ark_root/libpandabase:arkbase_public_config",
    ":arkfile_verifier_config",
  ]

  deps = [
    ":arkfile_header_deps",
    "$ark_root/libpandabase:libarkbase_static",
    "$ark_root/libziparchive:libarkziparchive_static",
  ]

  if (!ark_standalone_build) {
    external_deps = [
      sdk_libc_secshared_dep,
      zlib_deps,
    ]
    if (enable_hilog) {
      external_deps += [ "hilog:libhilog" ]
    }
  } else {
    external_deps = [
      sdk_libc_secshared_dep,
      zlib_deps,
    ]
  }

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_shared_library("libarkfile") {
  stack_protector_ret = false
  deps = [ ":libarkfile_static" ]

  if (!is_standard_system) {
    relative_install_dir = "ark"
  }
  if (!is_mingw && !is_mac) {
    output_extension = "so"
  }

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_source_set("libarkfile_frontend_set_static") {
  sources = libarkfile_frontend_sources

  public_configs = libarkfile_configs
  public_configs += [ "$ark_root:ark_config" ]
  public_configs += [ ":arkfile_frontend_public_config" ]

  deps = [
    ":arkfile_header_deps",
    "$ark_root/libpandabase:libarkbase_frontend_static",
    "$ark_root/libziparchive:libarkziparchive_frontend_static",
  ]

  if (is_arkui_x) {
    deps += [
      "$ark_third_party_root/bounds_checking_function:libsec_static",
      "$ark_third_party_root/zlib:libz",
    ]
  } else if (ark_standalone_build) {
    external_deps = [
      "zlib:libz",
      sdk_libc_secshared_dep,
    ]
  } else {
    external_deps = [
      "zlib:libz",
      sdk_libc_secshared_dep,
    ]
    if (enable_hilog) {
      external_deps += [ "hilog:libhilog" ]
    }
  }

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ohos_static_library("libarkfile_frontend_static") {
  stack_protector_ret = false
  deps = [ ":libarkfile_frontend_set_static" ]

  public_configs = [
    ":arkfile_public_config",
    ":arkfile_frontend_public_config",
  ]

  part_name = "runtime_core"
  subsystem_name = "arkcompiler"
}

ark_gen_file("libarkfile_type_gen_h") {
  template_file = "templates/type.h.erb"
  data_file = "types.yaml"
  requires = [ "types.rb" ]
  output_file = "$target_gen_dir/type.h"
}

ark_isa_gen("isa_gen_libarkfile") {
  template_files = [
    "arkcompiler.para.erb",
    "bytecode_instruction_enum_gen.h.erb",
    "bytecode_instruction-inl_gen.h.erb",
    "bytecode_emitter_def_gen.h.erb",
    "bytecode_emitter_gen.h.erb",
    "file_format_version.h.erb",
    "tests/bytecode_emitter_tests_gen.h.erb",
  ]
  sources = "templates"  ## ark_root/templates
  destination = "$target_gen_dir/include"  ## target_gen_dir/include
  requires = [ "pandafile_isapi.rb" ]
}

ark_gen_file("source_lang_enum_h") {
  extra_dependencies = [ "$ark_root:concat_plugins_yamls" ]
  template_file = "templates/source_lang_enum.h.erb"
  data_file = "$target_gen_dir/../plugin_options.yaml"
  requires = [ "$ark_root/templates/plugin_options.rb" ]
  output_file = "$target_gen_dir/include/source_lang_enum.h"
}

group("arkcompiler_params") {
  deps = []

  if (!ark_standalone_build) {
    deps += [ ":arkcompiler.para" ]
  }
}

if (!ark_standalone_build) {
  ohos_prebuilt_etc("arkcompiler.para") {
    deps = [ ":isa_gen_libarkfile_arkcompiler_para" ]
    source = "$target_gen_dir/include/arkcompiler.para"
    relative_install_dir = "param"
    part_name = "runtime_core"
    subsystem_name = "arkcompiler"
  }
}