@LIT_SITE_CFG_IN_HEADER@

import os

#config.llvm_src_root = "@LLVM_SOURCE_DIR@"
config.llvm_ver = "@LLVM_VERSION_MAJOR@"
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
config.llvm_tools_dir = "@LLVM_TOOLS_BINARY_DIR@"
if len("@ENZYME_BINARY_DIR@") == 0:
  config.llvm_tools_dir = os.getcwd() + "/" + config.llvm_tools_dir
config.llvm_libs_dir = "@LLVM_LIBS_DIR@"
config.enzyme_obj_root = "@ENZYME_BINARY_DIR@"
config.target_triple = "@TARGET_TRIPLE@"

config.llvm_shlib_ext = "@LLVM_SHLIBEXT@"

config.targets_to_build = "@TARGETS_TO_BUILD@"

has_mpfr_h = "@HAS_MPFR_H@"
mpfr_lib_path = "@MPFR_LIB_PATH@"
has_mpfr = "yes" if mpfr_lib_path != "MPFR_LIB_PATH-NOTFOUND" and has_mpfr_h == "1" else "no"

## Check the current platform with regex
import re
EAT_ERR_ON_X86 = ' '
if (re.match(r'^x86_64*', '@TARGET_TRIPLE@') == None) :
  EAT_ERR_ON_X86 = '|| echo \"error is eaten\"'

for arch in config.targets_to_build.split():
    config.available_features.add(arch.lower() + '-registered-target')

# Support substitution of the tools and libs dirs with user parameters. This is
# used when we can't determine the tool dir at configuration time.
try:
    config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params
    config.llvm_libs_dir = config.llvm_libs_dir % lit_config.params
except KeyError:
    e = sys.exc_info()[1]
    key, = e.args
    lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))

# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
# subdirectories contain auxiliary inputs for various tests in their parent
# directories.
config.excludes = ['Inputs']

config.substitutions.append(('%shlibext', config.llvm_shlib_ext))
config.substitutions.append(('%lli', config.llvm_tools_dir + "/lli" + (" --jit-kind=mcjit" if int(config.llvm_ver) >= 13 else "")
))

opt = config.llvm_tools_dir + "/opt"
if len("@ENZYME_BINARY_DIR@") == 0:
  opt = os.path.dirname(os.path.abspath(__file__)) + "/../enzyme-opt"

config.substitutions.append(('%opt', opt))

emopt = config.enzyme_obj_root + "/Enzyme/MLIR/enzymemlir-opt"
if len("@ENZYME_BINARY_DIR@") == 0:
  emopt = os.path.dirname(os.path.abspath(__file__)) + "/../enzymemlir-opt"

eclang = config.llvm_tools_dir + "/clang"
if len("@ENZYME_BINARY_DIR@") == 0:
  eclang = os.path.dirname(os.path.abspath(__file__)) + "/../enzyme-clang"
  resource = config.llvm_tools_dir + "/../clang/staging"
  eclang += " -resource-dir " + resource + " "
  eclang += "-I " + os.path.dirname(os.path.abspath(__file__)) + "/Integration" 

config.substitutions.append(('%eopt', emopt))
config.substitutions.append(('%llvmver', config.llvm_ver))
config.substitutions.append(('%FileCheck', config.llvm_tools_dir + "/FileCheck"))
config.substitutions.append(('%clang', eclang))
config.substitutions.append(('%O0TBAA', "-O1 -Xclang -disable-llvm-passes"))

oldPM = ((" --enable-new-pm=0" if int(config.llvm_ver) >= 13 else "")
                                 + ' -load=@ENZYME_BINARY_DIR@/Enzyme/LLVMEnzyme-' + config.llvm_ver + config.llvm_shlib_ext 
                                 + (" --enzyme-attributor=0" if int(config.llvm_ver) >= 13 else ""))
newPM = ((" --enable-new-pm=1" if int(config.llvm_ver) in (12,13) else "")
                                 + ' -load-pass-plugin=@ENZYME_BINARY_DIR@/Enzyme/LLVMEnzyme-' + config.llvm_ver + config.llvm_shlib_ext 
                                 + ' -load=@ENZYME_BINARY_DIR@/Enzyme/LLVMEnzyme-' + config.llvm_ver + config.llvm_shlib_ext 
                                 + (" --enzyme-attributor=0" if int(config.llvm_ver) >= 13 else ""))
if len("@ENZYME_BINARY_DIR@") == 0:
  oldPM = ((" --enable-new-pm=0" if int(config.llvm_ver) >= 13 else "")
                                 + (" --enzyme-attributor=0" if int(config.llvm_ver) >= 13 else ""))
  newPM = ((" --enable-new-pm=1" if int(config.llvm_ver) in (12,13) else "")
                                 + (" --enzyme-attributor=0" if int(config.llvm_ver) >= 13 else ""))

oldPMOP = oldPM
newPMOP = newPM
if int(config.llvm_ver) == 16:
  newPM += " -opaque-pointers=0"
  oldPM += " -opaque-pointers=0"

config.substitutions.append(('%loadEnzyme', oldPM if int(config.llvm_ver) < 16 else newPM))
config.substitutions.append(('%newLoadEnzyme', newPM))
config.substitutions.append(('%OPloadEnzyme', oldPMOP if int(config.llvm_ver) < 16 else newPMOP))
config.substitutions.append(('%OPnewLoadEnzyme', newPMOP))
config.substitutions.append(('%enzyme', ('-enzyme' if int(config.llvm_ver) < 16 else '-passes="enzyme"')))
config.substitutions.append(('%simplifycfg', ("simplify-cfg" if int(config.llvm_ver) < 11 else "simplifycfg")))
config.substitutions.append(('%loopmssa', ("loop" if int(config.llvm_ver) < 11 else "loop-mssa")))

config.substitutions.append(('%loadBC', ''
                                 + ' @ENZYME_BINARY_DIR@/BCLoad/BCPass-' + config.llvm_ver + config.llvm_shlib_ext
                                 ))
config.substitutions.append(('%BClibdir', '@ENZYME_SOURCE_DIR@/bclib/'))

oldPM = (((" -fno-experimental-new-pass-manager" if int(config.llvm_ver) < 14 else "-flegacy-pass-manager") if int(config.llvm_ver) >= 13 else "")
          + ' -Xclang -load -Xclang @ENZYME_BINARY_DIR@/Enzyme/ClangEnzyme-' + config.llvm_ver + config.llvm_shlib_ext)
newPM = ((" -fexperimental-new-pass-manager" if int(config.llvm_ver) < 13 else "")
          + ' -fpass-plugin=@ENZYME_BINARY_DIR@/Enzyme/ClangEnzyme-' + config.llvm_ver + config.llvm_shlib_ext
          + ' -Xclang -load -Xclang @ENZYME_BINARY_DIR@/Enzyme/ClangEnzyme-' + config.llvm_ver + config.llvm_shlib_ext)

if len("@ENZYME_BINARY_DIR@") == 0:
  oldPM = ((" -fno-experimental-new-pass-manager" if int(config.llvm_ver) < 14 else "-flegacy-pass-manager") if int(config.llvm_ver) >= 13 else "")
  newPM = (" -fexperimental-new-pass-manager" if int(config.llvm_ver) < 13 else "")

config.substitutions.append(('%loadClangEnzyme', oldPM if int(config.llvm_ver) < 15 else newPM))
config.substitutions.append(('%newLoadClangEnzyme', newPM))

config.substitutions.append(('%hasMPFR', has_mpfr))

# Let the main config do the real work.
cfgfile = "@ENZYME_SOURCE_DIR@/test/lit.cfg.py"
if len("@ENZYME_SOURCE_DIR@") == 0:
  cfgfile = os.path.dirname(os.path.abspath(__file__)) + "/lit.cfg.py"
lit_config.load_config(config, cfgfile)
