# -*- Python -*-

# Configuration file for the 'lit' test runner.
import os
import platform
import subprocess

import lit.formats

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

# disable pipefail by default
config.pipefail = False

# 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 = True)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.cpp','.ll']

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

# target_triple: Used by ShTest and TclTest formats for XFAIL checks.
config.target_triple = platform.system()

if os.environ.get('KM_USE_AMDGPU'):
    config.environment['KM_USE_AMDGPU'] = os.environ['KM_USE_AMDGPU']

if os.environ.get('HCC_AMDGPU_TARGET'):
    config.environment['HCC_AMDGPU_TARGET'] = os.environ['HCC_AMDGPU_TARGET']

if os.environ.get('HSA_ENABLE_SDMA'):
    config.environment['HSA_ENABLE_SDMA'] = os.environ['HSA_ENABLE_SDMA']

if os.environ.get('HSA_ENABLE_INTERRUPT'):
    config.environment['HSA_ENABLE_INTERRUPT'] = os.environ['HSA_ENABLE_INTERRUPT']

if os.environ.get('HSA_TOOLS_LIB'):
    config.environment['HSA_TOOLS_LIB'] = os.environ['HSA_TOOLS_LIB']

config.environment['LD_LIBRARY_PATH'] = config.library_output_path
if os.environ.get('LD_LIBRARY_PATH'):
    config.environment['LD_LIBRARY_PATH'] = config.environment['LD_LIBRARY_PATH'] + ":" + os.environ.get('LD_LIBRARY_PATH')

if os.environ.get('HCC_DEFAULT_GPU'):
    config.environment['HCC_DEFAULT_GPU'] = os.environ['HCC_DEFAULT_GPU']

if os.environ.get('HCC_ENABLE_PRINTF'):
    config.environment['HCC_ENABLE_PRINTF'] = os.environ['HCC_ENABLE_PRINTF']
else:
    config.environment['HCC_ENABLE_PRINTF'] = "1"
 
if os.environ.get('AMDGPU_OBJ_CODEGEN'):
    config.environment['AMDGPU_OBJ_CODEGEN'] = os.environ['AMDGPU_OBJ_CODEGEN']

if os.environ.get('HCC_EXTRA_GPU_ARCH'):
    config.environment['HCC_EXTRA_GPU_ARCH'] = os.environ['HCC_EXTRA_GPU_ARCH']

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


###
# Check that the site specific configuration exists.
#site_cfg = os.path.join(config.test_source_root, 'lit.site.cfg')
#if not os.path.exists(site_cfg):
#  lit.fatal('No site specific configuration available!')

# Discover the 'clang' and 'clangcc' to use.

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.fatal("couldn't find 'clang' program, try setting "
                  "CLANG in your environment")

    return clang

cxx_options = ' ' + ' '.join([
  "-I%s" % config.gtest_src_dir,
  "-DGTEST_HAS_TR1_TUPLE=0"
]) + ' '

link_options = ' ' + ' '.join([
]) + ' '

gtest_link_options = ' ' + ' '.join([
  "-lmcwamp_gtest",
]) + ' '

config.clang = inferClang(config.llvm_tools_dir)
config.clang_cc1 = config.clang + "++"
config.clang_cxx11  = config.clang_cc1 + cxx_options + "-std=c++11"
config.clang_cxxamp = config.clang_cc1 + cxx_options + "-std=c++amp" + link_options
config.clang_hc = config.clang_cc1 + cxx_options + "-hc" + link_options
config.clang_cxxamp_device = config.clang_cxxamp + " -Xclang -famp-is-device -fno-builtin "
config.clang_gtest_amp = config.clang_cxxamp + gtest_link_options

if not lit_config.quiet:
    lit_config.note('using clang: %r' % config.clang)

# Note that when substituting %clang_cc1 also fill in the include directory of
# the builtin headers. Those are part of even a freestanding environment, but
# Clang relies on the driver to locate them.
def getClangBuiltinIncludeDir(clang):
    # FIXME: Rather than just getting the version, we should have clang print
    # out its resource dir here in an easy to scrape form.
    cmd = subprocess.Popen([clang, '-print-file-name=include'],
                           stdout=subprocess.PIPE)
    if not cmd.stdout:
      lit.fatal("Couldn't find the include dir for Clang ('%s')" % clang)
    return cmd.stdout.read().strip()
config.substitutions.append( ('%clang_cc1', '%s -cc1 -internal-isystem %s'
                              % (config.clang, getClangBuiltinIncludeDir(config.clang))) )

config.substitutions.append( ('%ampneg', cxx_options + "-I/usr/local/include -I/usr/include") )
config.substitutions.append( ('%link', link_options) )
config.substitutions.append( ('%cxx11', config.clang_cxx11) )
config.substitutions.append( ('%cxxamp', config.clang_cxxamp) )
config.substitutions.append( ('%hc', config.clang_hc) )
config.substitutions.append( ('%llvm_libs_dir', config.llvm_libs_dir) )
config.substitutions.append( ('%clamp-device', os.path.join(config.mcwamp_tool_dir, "clamp-device") ) )
config.substitutions.append( ('%amp_device', config.clang_cxxamp_device) )
config.substitutions.append( ('%gtest_amp', config.clang_gtest_amp) )
config.substitutions.append( ('%FileCheck', os.path.join(config.llvm_tools_dir,'FileCheck') ) )
config.substitutions.append( ('%llc', os.path.join(config.llvm_tools_dir,'llc') ) )
config.substitutions.append( ('%llvm-as', os.path.join(config.llvm_tools_dir, 'llvm-as') ) )
config.substitutions.append( ('%llvm-dis', os.path.join(config.llvm_tools_dir, 'llvm-dis') ) )
config.substitutions.append( ('%opt', os.path.join(config.llvm_tools_dir, 'opt') ) )
config.substitutions.append( ('%clang', config.clang) )
config.substitutions.append( ('%cppfilt', 'c++filt -n') )
config.substitutions.append( ('%embed_kernel', os.path.join(config.mcwamp_tool_dir, "clamp-embed") ) )
config.substitutions.append( ('%extractkernel', os.path.join(config.executable_output_path,'extractkernel') ) )
config.substitutions.append( ('%not', os.path.join(config.llvm_tools_dir, 'not') ) )
config.substitutions.append( ('%target_all_gpus', "--amdgpu-target=gfx701 --amdgpu-target=gfx803 --amdgpu-target=gfx900 --amdgpu-target=gfx906") )
config.substitutions.append( ('%lib_output_path', config.library_output_path ) )
