# Bazel BUILD file generator for system LLVM.

import os

llvm_libs = [
    "AArch64AsmParser",
    "AArch64CodeGen",
    "AArch64Desc",
    "AArch64Disassembler",
    "AArch64Info",
    "AArch64Utils",
    "AggressiveInstCombine",
    "AMDGPUAsmParser",
    "AMDGPUCodeGen",
    "AMDGPUDesc",
    "AMDGPUDisassembler",
    "AMDGPUInfo",
    "AMDGPUUtils",
    "Analysis",
    "ARMAsmParser",
    "ARMCodeGen",
    "ARMDesc",
    "ARMDisassembler",
    "ARMInfo",
    "ARMUtils",
    "AsmParser",
    "AsmPrinter",
    "BinaryFormat",
    "BitReader",
    "BitstreamReader",
    "BitWriter",
    "BPFAsmParser",
    "BPFCodeGen",
    "BPFDesc",
    "BPFDisassembler",
    "BPFInfo",
    "CodeGen",
    "Core",
    "Coroutines",
    "Coverage",
    "DebugInfoCodeView",
    "DebugInfoDWARF",
    "DebugInfoGSYM",
    "DebugInfoMSF",
    "DebugInfoPDB",
    "Demangle",
    "DlltoolDriver",
    "ExecutionEngine",
    "FuzzMutate",
    "GlobalISel",
    "HexagonAsmParser",
    "HexagonCodeGen",
    "HexagonDesc",
    "HexagonDisassembler",
    "HexagonInfo",
    "InstCombine",
    "Instrumentation",
    "IntelJITEvents",
    "Interpreter",
    "ipo",
    "IRReader",
    "JITLink",
    "LanaiAsmParser",
    "LanaiCodeGen",
    "LanaiDesc",
    "LanaiDisassembler",
    "LanaiInfo",
    "LibDriver",
    "LineEditor",
    "Linker",
    "LTO",
    "MC",
    "MCA",
    "MCDisassembler",
    "MCJIT",
    "MCParser",
    "MipsAsmParser",
    "MipsCodeGen",
    "MipsDesc",
    "MipsDisassembler",
    "MipsInfo",
    "MIRParser",
    "MSP430AsmParser",
    "MSP430CodeGen",
    "MSP430Desc",
    "MSP430Disassembler",
    "MSP430Info",
    "NVPTXCodeGen",
    "NVPTXDesc",
    "NVPTXInfo",
    "ObjCARCOpts",
    "Object",
    "ObjectYAML",
    "Option",
    "OrcJIT",
    "Passes",
    "PowerPCAsmParser",
    "PowerPCCodeGen",
    "PowerPCDesc",
    "PowerPCDisassembler",
    "PowerPCInfo",
    "ProfileData",
    "Remarks",
    "RISCVAsmParser",
    "RISCVCodeGen",
    "RISCVDesc",
    "RISCVDisassembler",
    "RISCVInfo",
    "RISCVUtils",
    "RuntimeDyld",
    "ScalarOpts",
    "SelectionDAG",
    "SparcAsmParser",
    "SparcCodeGen",
    "SparcDesc",
    "SparcDisassembler",
    "SparcInfo",
    "Support",
    "Symbolize",
    "SystemZAsmParser",
    "SystemZAsmPrinter",
    "SystemZCodeGen",
    "SystemZDesc",
    "SystemZDisassembler",
    "SystemZInfo",
    "TableGen",
    "Target",
    "TextAPI",
    "TransformUtils",
    "Vectorize",
    "WebAssemblyAsmParser",
    "WebAssemblyCodeGen",
    "WebAssemblyDesc",
    "WebAssemblyDisassembler",
    "WebAssemblyInfo",
    "WindowsManifest",
    "X86AsmParser",
    "X86CodeGen",
    "X86Desc",
    "X86Disassembler",
    "X86Info",
    "X86Utils",
    "XCoreCodeGen",
    "XCoreDesc",
    "XCoreDisassembler",
    "XCoreInfo",
    "XRay"
]

def process_name(name):
  if name.endswith("Opts"):
    return name[:-4].lower()
  return name.lower()

llvm_libs_dict = dict((process_name(name), name) for name in llvm_libs)

llvm_libs_dict["scalar"] = "ScalarOpts"


INIT_TEXT = """
# Bazel BUILD file for system LLVM
#
# Auto-generated by generate_llvm_build.py

licenses(["notice"])

filegroup(
    name = "LICENSE.TXT",
    visibility = ["//visibility:public"],
)

load(
    "@org_tensorflow//third_party/llvm:llvm.bzl",
    "cmake_var_string",
    "expand_cmake_vars",
    "gentbl",
    "llvm_all_cmake_vars",
    "llvm_copts",
    "llvm_defines",
    "llvm_linkopts",
    "llvm_support_platform_specific_srcs_glob",
)

# Binary targets used by Tensorflow.
genrule(
    name = "lnllvmtblgenlink",
    outs = ["llvmtblgenlink"],
    cmd = "ln -s $$(which llvm-tblgen) $@",
)

sh_binary(
    name = "llvm-tblgen",
    srcs = ["llvmtblgenlink"],
    visibility = ["//visibility:public"],
)

genrule(
    name = "lnfilecheck",
    outs = ["filechecklink"],
    cmd = "ln -s $$(which FileCheck) $@",
)

sh_binary(
    name = "FileCheck",
    srcs = ["filechecklink"],
    visibility = ["//visibility:public"],
)

cc_library(
   name = "config",
   defines = llvm_defines,
   visibility = ["//visibility:public"],
)

cc_library(
   name = "x86_defs",
   visibility = ["//visibility:public"],
   deps = [
       ":config",
   ]
)
"""

skipped_deps = [
    ":attributes_compat_gen",
    ":attributes_gen",
    ":intrinsic_enums_gen",
    ":intrinsics_impl_gen",
    ":aarch64_target_gen",
    ":x86_target_gen",
    ":arm_target_gen",
    ":instcombine_transforms_gen",
]

def cc_library(**kwargs):
  name = kwargs.get("name")
  if name in ["config", "x86_defs", "gtest", "gtest_main"]:
    return
  libname = name
  if libname.startswith("arc_"):
    libname = "sp" + libname
  libname = llvm_libs_dict.get(libname.replace("_", ""), name)
  deps = kwargs.get("deps", [])
  deps = [d for d in deps if d not in skipped_deps]
  if not deps:
    print(f"""
cc_library(
    name = "{name}",
    linkopts = ["-lLLVM{libname}"],
    visibility = ["//visibility:public"],
)""")
  else:
    deps_line = "\",\n        \"".join(deps)
    print(f"""
cc_library(
    name = "{name}",
    linkopts = ["-lLLVM{libname}"],
    visibility = ["//visibility:public"],
    deps = [
        "{deps_line}",
    ],
)""")

def glob(*args, **kwargs):     # pylint: disable=unused-argument
  return []


def genrule(*args, **kwargs):  # pylint: disable=unused-argument
  pass

llvm_support_platform_specific_srcs_glob = glob
licenses = exports_files = load = package = expand_cmake_vars = genrule
py_binary = template_rule = gentbl = cc_binary = filegroup = genrule
llvm_copts = llvm_all_cmake_vars = llvm_defines = llvm_linkopts = []

licenses(["notice"])

exports_files(["LICENSE.TXT"])

load(
    "@org_tensorflow//third_party/llvm:llvm.bzl",
    "cmake_var_string",
    "expand_cmake_vars",
    "gentbl",
    "llvm_all_cmake_vars",
    "llvm_copts",
    "llvm_defines",
    "llvm_linkopts",
    "llvm_support_platform_specific_srcs_glob",
)
load(
    "@org_tensorflow//third_party:common.bzl",
    "template_rule",
)

third_party_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
build_path = os.path.join(third_party_dir, "llvm", "llvm.autogenerated.BUILD")

print(INIT_TEXT)
with open(build_path, "r") as f:
  exec(f.read())
