import lit.formats
import os
import sys
import platform
import string

## Auto-initialized variables by cmake:
config.ldc2_bin            = "@LDC2_BIN@"
config.ldcprofdata_bin     = "@LDCPROFDATA_BIN@"
config.ldcprunecache_bin   = "@LDCPRUNECACHE_BIN@"
config.ldc2_bin_dir        = "@LDC2_BIN_DIR@"
config.test_source_root    = "@TESTS_IR_DIR@"
config.llvm_tools_dir      = "@LLVM_TOOLS_DIR@"
config.llvm_version        = @LDC_LLVM_VER@
config.llvm_targetsstr     = "@LLVM_TARGETS_TO_BUILD@"
config.default_target_bits = @DEFAULT_TARGET_BITS@
config.with_PGO            = @LDC_WITH_PGO@

config.name = 'LDC'

# testFormat: The test format to use to interpret tests.
config.test_format = lit.formats.ShTest(execute_external=False)

# suffixes: A list of file extensions to treat as test files. This is overriden
# by individual lit.local.cfg files in the test subdirectories.
config.suffixes = ['.d',
                   ]

# 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',
    'd2',
    'CMakeLists.txt',
    'runlit.py',
]

# Exclude profile test dir when PGO is disabled
if not config.with_PGO:
    config.excludes.append('PGO')


# Define available features so that we can disable tests depending on LLVM version
config.available_features.add("llvm%d" % config.llvm_version)
# LLVM version history: 3.8, 3.9, 4.0, ...
# config.llvm_version: 308, 309, 400, ...
# plusoneable_llvmversion: 38, 39, 40, ...
plusoneable_llvmversion = config.llvm_version // 10  + config.llvm_version%10
for version in range(35, plusoneable_llvmversion+1):
    config.available_features.add("atleast_llvm%d0%d" % (version//10, version%10))
for version in range(plusoneable_llvmversion, 41):
    config.available_features.add("atmost_llvm%d0%d" % (version//10, version%10))

# Define OS as available feature (Windows, Darwin, Linux)
config.available_features.add(platform.system())

# Define available features based on what LLVM can target
# Examples: 'target_X86', 'target_ARM', 'target_PowerPC', 'target_AArch64'
for t in config.llvm_targetsstr.split(';'):
    config.available_features.add('target_' + t)

# Add specific features for Windows x86/x64 testing
if (platform.system() == 'Windows') and (config.default_target_bits == 32):
    config.available_features.add('Windows_x86')
if (platform.system() == 'Windows') and (config.default_target_bits == 64):
    config.available_features.add('Windows_x64')

config.target_triple = '(unused)'

# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.dirname(__file__)

# add LLVM tools bin dir to the path
path = os.path.pathsep.join( (config.llvm_tools_dir, config.environment['PATH']) )
config.environment['PATH'] = path

# add test root dir to the path (FileCheck might sit there)
path = os.path.pathsep.join( (config.test_source_root, config.environment['PATH']) )
config.environment['PATH'] = path

# add LDC bin dir to the path
# Give priority to LDC's version of LLVM tools (we need FileCheck with certain bug fixes)
path = os.path.pathsep.join( (config.ldc2_bin_dir, config.environment['PATH']) )
config.environment['PATH'] = path

# Add substitutions
config.substitutions.append( ('%ldc', config.ldc2_bin) )
config.substitutions.append( ('%profdata', config.ldcprofdata_bin) )
config.substitutions.append( ('%prunecache', config.ldcprunecache_bin) )

# Add platform-dependent file extension substitutions
if (platform.system() == 'Windows'):
    config.substitutions.append( ('%obj', '.obj') )
    config.substitutions.append( ('%exe', '.exe') )
else:
    config.substitutions.append( ('%obj', '.o') )
    config.substitutions.append( ('%exe', '') )

# Add cdb substitution
if (platform.system() == 'Windows') and (config.default_target_bits == 32):
    cdb = os.environ['WindowsSDKDir'] + 'Debuggers\\x86\\cdb.exe'
    config.substitutions.append( ('%arch', 'x86') )
if (platform.system() == 'Windows') and (config.default_target_bits == 64):
    cdb = os.environ['WindowsSDKDir'] + 'Debuggers\\x64\\cdb.exe'
    config.substitutions.append( ('%arch', 'x64') )

if (platform.system() == 'Windows') and os.path.isfile( cdb ):
    config.available_features.add('cdb')
    config.substitutions.append( ('%cdb', '"' + string.replace( cdb, '\\', '\\\\') + '"') )
