# -*- Python -*-

# Configuration file for the 'lit' test runner.

import os
import sys
import re
import platform

import lit.util
import lit.formats

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

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 = ['.smt2']

# 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 = []

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

# test_exec_root: The root path where tests should be run.
config.test_exec_root = config.test_source_root

# Propagate 'HOME' through the environment.
if 'HOME' in os.environ:
    config.environment['HOME'] = os.environ['HOME']

# Propagate 'LD_LIBRARY_PATH' through the environment.
if 'LD_LIBRARY_PATH' in os.environ:
    config.environment['LD_LIBRARY_PATH'] = os.environ['LD_LIBRARY_PATH']

# Propagate 'INCLUDE' through the environment.
if 'INCLUDE' in os.environ:
    config.environment['INCLUDE'] = os.environ['INCLUDE']

# Propagate 'LIB' through the environment.
if 'LIB' in os.environ:
    config.environment['LIB'] = os.environ['LIB']

# Propagate the temp directory. Windows requires this because it uses \Windows\
# if none of these are present.
if 'TMP' in os.environ:
    config.environment['TMP'] = os.environ['TMP']
if 'TEMP' in os.environ:
    config.environment['TEMP'] = os.environ['TEMP']

# Propagate PYTHON_EXECUTABLE into the environment
config.environment['PYTHON_EXECUTABLE'] = getattr(config, 'python_executable', '')

# Propage user's PATH into the environment
config.environment['PATH'] = os.environ['PATH']

# Check that the object root is known.
if config.test_exec_root is None:
    lit_config.fatal('Could not determine execution root for tests!')

### Add substitutions
def python_executable_sub(path):
    if os.name == 'nt':
       return '{} {}'.format(sys.excutable, path)
    else:
       return path
up = os.path.dirname
repositoryRoot = up(
                     up( os.path.abspath(__file__) )
                   )
lit_config.note('Repository root is {}'.format(repositoryRoot))

dump_executable = os.path.join(repositoryRoot, 'dump.py')
if not os.path.exists(dump_executable):
    lit_config.fatal('Failed to find dump executable {}'.format(dump_executable))
dump_substitution = python_executable_sub(dump_executable)
dump_extra_flags = lit_config.params.get('dump_params','')
if len(dump_extra_flags) > 0:
  dump_substitution += ' {}'.format(dump_extra_flags)
lit_config.note('%dump is "{}"'.format(dump_substitution))
config.substitutions.append( ('%smt2coral-dump', dump_substitution))

coral_executable = os.path.join(repositoryRoot, 'coral.py')
if not os.path.exists(coral_executable):
    lit_config.fatal('Failed to find coral executable {}'.format(coral_executable))
coral_substitution = python_executable_sub(coral_executable)
coral_extra_flags = lit_config.params.get('coral_params', '')
if len(coral_extra_flags) > 0:
  coral_substitution += ' {}'.format(coral_extra_flags)
lit_config.note('%coral is "{}"'.format(coral_substitution))
config.substitutions.append( ('%coral', coral_substitution))

# Add substitution for not utility
not_tool = os.path.join(repositoryRoot, 'utils', 'not.py')
config.substitutions.append( ('%not', python_executable_sub(not_tool)))

# Add substitutions for FileCheck
file_check_tool = lit.util.which('FileCheck')
if file_check_tool is None:
      lit_config.fatal('Failed to find FileCheck')
config.substitutions.append( ('%FileCheck', file_check_tool))
