# 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")

config("libark_llvmcodegen_set_config") {
  if (compile_llvm_online) {
    lib_dirs = [ "//third_party/third_party_llvm-project/build/lib" ]
  } else {
    if (is_mingw) {
      lib_dirs = [
        "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts_windows/build/lib",
        "//prebuilts/mingw-w64/ohos/linux-x86_64/clang-mingw/x86_64-w64-mingw32",
      ]
    } else if (is_mac) {
      if (host_cpu == "arm64") {
        lib_dirs = [ "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts_darwin_arm64/build/lib" ]
      } else {
        lib_dirs = [ "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts_darwin_x64/build/lib" ]
      }
    } else {
      if (host_cpu == "arm64" && host_os == "linux") {
        if (current_os == "ohos") {
          lib_dirs = [ "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts_aarch64/build/lib" ]
        } else {
          lib_dirs = [ "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts_linux_aarch64/build/lib" ]
        }
      } else {
        if (current_cpu == "arm64") {
          lib_dirs = [ "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts_aarch64/build/lib" ]
        } else {
          lib_dirs = [
            "//prebuilts/ark_tools/ark_js_prebuilts/llvm_prebuilts/build/lib",
          ]
        }
      }
    }
  }

  libs = [
    "LLVMTarget",
    "LLVMObject",
    "LLVMMC",
    "LLVMSupport",
    "LLVMCore",
    "LLVMExecutionEngine",
    "LLVMInterpreter",
    "LLVMMCJIT",
    "LLVMExegesis",
    "LLVMRuntimeDyld",
    "LLVMInstCombine",
    "LLVMAnalysis",
    "LLVMScalarOpts",
    "LLVMBinaryFormat",
    "LLVMDebugInfoDWARF",
    "LLVMRemarks",
    "LLVMTextAPI",
    "LLVMScalarOpts",
    "LLVMTransformUtils",
    "LLVMBitReader",
    "LLVMAsmPrinter",
    "LLVMProfileData",
    "LLVMBitstreamReader",
    "LLVMSelectionDAG",
    "LLVMGlobalISel",
    "LLVMLTO",
    "LLVMCFGuard",
    "LLVMVectorize",
    "LLVMDemangle",
    "LLVMipo",
    "LLVMInstrumentation",
    "LLVMDebugInfoCodeView",
    "LLVMAggressiveInstCombine",
    "LLVMAsmParser",
    "LLVMMCParser",
    "LLVMMIRParser",
    "LLVMMCDisassembler",
    "LLVMCodeGen",
    "LLVMTransformUtils",
    "LLVMIRReader",
    "LLVMAArch64Utils",
    "LLVMAArch64CodeGen",
    "LLVMAArch64Info",
    "LLVMAArch64Desc",
    "LLVMAArch64Disassembler",
    "LLVMAArch64AsmParser",
    "LLVMFrontendOpenMP",
    "LLVMBitWriter",
  ]

  if (!is_mac && !is_ios) {
    libs += [ "LLVMParts" ]
  }

  # Only support compiling aarch64 target at device-side(arm64 platform).
  # So these os-related libs of x86 are not needed on arm64 platform.
  if (is_mac || current_cpu != "arm64") {
    libs += [
      "LLVMX86AsmParser",
      "LLVMX86CodeGen",
      "LLVMX86Desc",
      "LLVMX86Disassembler",
      "LLVMX86Info",
    ]
  }
}

ohos_source_set("libark_llvmcodegen_set") {
  stack_protector_ret = false
  public_configs = [
    "$js_root:include_llvm",
    "$js_root:ark_jsruntime_common_config",
    ":libark_llvmcodegen_set_config",
  ]
  sources = [ "llvm_interface.cpp" ]
  part_name = "ets_runtime"
  subsystem_name = "arkcompiler"
}

ohos_shared_library("libark_llvmcodegen") {
  stack_protector_ret = false
  deps = [ ":libark_llvmcodegen_set" ]
  _used_symbols = [
    "LLVMGetInlineAsm",
    "LLVMPassManagerBuilderCreate",
    "LLVMCreateSimpleMCJITMemoryManager",
    "LLVMLinkInMCJIT",
    "LLVMVerifyModule",
    "LLVMCreateDisasm",
    "LLVMCreateRewriteStatepointsForGCLegacyPass",
    "LLVMLinkAllBuiltinGCs",

    "LLVMInitializeAArch64Target",
    "LLVMInitializeAArch64AsmParser",
    "LLVMInitializeAArch64Disassembler",
    "LLVMInitializeAArch64AsmPrinter",
  ]
  ldflags = []
  foreach(symbol, _used_symbols) {
    ldflags += [ "-Wl,-u," + symbol ]
  }
  ldflags += [ "-Wl,-mllvm,-import-instr-limit=0" ]
  install_enable = true
  if (!is_mingw && !is_mac) {
    output_extension = "so"
  }
  part_name = "ets_runtime"
  subsystem_name = "arkcompiler"
}