# -*- Python -*-

# Configuration file for the 'lit' test runner.

import os

import lit

# name: The name of this test suite.
config.name = 'CLANGDELTA'

# Choose between lit's internal shell pipeline runner and a real shell.  If
# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
if use_lit_shell:
    # 0 is external, "" is default, and everything else is internal.
    execute_external = (use_lit_shell == "0")
else:
    # Otherwise we default to internal on Windows and external elsewhere, as
    # bash on Windows is usually very slow.
    execute_external = (not sys.platform in ['win32'])

# testFormat: The test format to use to interpret tests.
#
# For now we require '&&' between commands, until they get globally killed and
# the test runner updated.
config.test_format = lit.formats.ShTest(execute_external)

# 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 = ['.c', '.cpp', '.cxx', '.test', '.txt']

# 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', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)

# test_exec_root: The root path where tests should be run.
clangdelta_obj_root = getattr(config, 'clangdelta_obj_root', None)
if clangdelta_obj_root is not None:
    config.test_exec_root = os.path.join(clangdelta_obj_root, 'test')
    path = os.path.pathsep.join((clangdelta_obj_root, config.environment['PATH']))
    config.environment['PATH'] = path

llvm_tools_bin_dir = config.llvm_tools_bin_dir
if llvm_tools_bin_dir is not None:
    path = os.path.pathsep.join((llvm_tools_bin_dir, config.environment['PATH']))
    config.environment['PATH'] = path

# Propogate 'HOME' through the environment.
config.environment['HOME'] = os.environ['HOME']

###

import os

def inferClangDelta(PATH):
    # Determine which clangdelta to use.
    clangdelta = os.getenv('CLANGDELTA')

    # If the user set clangdelta in the environment, definitely use that and
    # don't try to validate.
    if clangdelta:
        return clangdelta

    # Otherwise look in the path.
    clangdelta = lit.util.which('clang_delta', PATH)

    if not clangdelta:
        lit_config.fatal("couldn't find 'clangdelta' program, try setting "
                         "CLANGDELTA in your environment")

    return clangdelta

def inferClang(PATH):
    # Determine which clang to use.
    clang = os.getenv('CLANG')

    # If the user set clang in the environment, definitely use that and
    # don't try to validate.
    if clang:
        return clang

    # Otherwise look in the path.
    clang = lit.util.which('clang', PATH)

    if not clang:
        lit_config.fatal("couldn't find 'clang' program, try setting "
                         "CLANG in your environment")

    return clang


# Find clang_delta executable.
config.clangdelta = inferClangDelta(config.environment['PATH']).replace('\\', '/')
if not lit_config.quiet:
    lit_config.note('using clangdelta: %r' % config.clangdelta)

# Find clang executable.
config.clang = inferClang(config.environment['PATH']).replace('\\', '/')
if not lit_config.quiet:
    lit_config.note('using clang: %r' % config.clang)

# Add site-specific substitutions.
config.substitutions.append( ('%clangdelta', config.clangdelta) )
config.substitutions.append( ('%clang_cc1', config.clang + ' -cc1 ') )
config.substitutions.append( ('%clang', config.clang) )

llvm_filecheck = lit.util.which('FileCheck', config.llvm_tools_bin_dir)
config.substitutions.append( ('FileCheck', llvm_filecheck) )

# Forbid direct usage of the binary.
config.substitutions.append(
    (' clang_delta ', """*** Do not use 'clang_delta' in tests, use '%clangdelta'. ***""") )


# Check that the object root is known.
if config.test_exec_root is None:
    lit.fatal('test execution root not set!')

###

# Check if we should allow outputs to console.
run_console_tests = int(lit_config.params.get('enable_console', '0'))
if run_console_tests != 0:
  config.available_features.add('console')

lit.util.usePlatformSdkOnDarwin(config, lit_config)
