# -*- Python -*-
import os
import platform
import re
import subprocess
import struct

import lit.formats
import lit.util

# Configuration file for the 'lit' test runner.
site_cfg = lit_config.params.get('site_config', None)
lit_config.load_config(config, site_cfg)

print(str("========================================"))
print("CUDA " + config.cuda_version + " - will be used for testing")
print("LLVM " + config.llvm_version + " - will be used for testing")
print(platform.machine() + " - Platform architecture")
print(platform.system() + " " + platform.release() + " - Platform OS")
print(str(config.pointer_size * 8) + " - hipify-clang binary bitness")
print(str(struct.calcsize("P") * 8) + " - python " + str(platform.python_version()) + " binary bitness")
print(str("========================================"))

config.excludes = ['cmdparser.hpp']
config.excludes.append('spatial_batch_norm_op.h')
config.excludes.append('common_cudnn.h')

if config.cuda_version_major == 7 and config.cuda_version_minor == 0:
    config.excludes.append('headers_test_09.cu')
    config.excludes.append('cudnn_convolution_forward.cu')
if config.cuda_version_major < 8:
    config.excludes.append('cuSPARSE_02.cu')
if config.cuda_version_major < 9:
    config.excludes.append('cuSPARSE_04.cu')
    config.excludes.append('cuSPARSE_05.cu')
    config.excludes.append('cuSPARSE_06.cu')
    config.excludes.append('cuSPARSE_07.cu')
    config.excludes.append('benchmark_curand_kernel.cpp')
if config.cuda_version_major < 10:
    config.excludes.append('cuSPARSE_08.cu')
    config.excludes.append('cuSPARSE_09.cu')
    config.excludes.append('cuSPARSE_10.cu')
    config.excludes.append('cuSPARSE_11.cu')

if config.llvm_version_major < 10:
    config.excludes.append('pp_if_else_conditionals_LLVM_10.cu')
    config.excludes.append('pp_if_else_conditionals_01_LLVM_10.cu')

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

# suffixes: CUDA source is only supported
config.suffixes = ['.cu','.cuh','.cpp','.c','.hpp','.h']

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

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

# test_exec_root: The path where tests are located (default is the test suite root).
#config.test_exec_root = config.test_source_root

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

# available_features: Used by ShTest and TclTest formats for REQUIRES checks.
config.available_features = []

obj_root = getattr(config, 'obj_root', None)
if obj_root is not None:
    config.test_exec_root = obj_root

if obj_root is not None:
    llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
    if not llvm_tools_dir:
        lit_config.fatal('No LLVM tools dir set!')
    path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH']))
    config.environment['PATH'] = path

hipify_path = obj_root

clang_arguments = "-v"
if sys.platform in ['win32']:
    run_test_ext = ".bat"
    hipify_path += "/" + config.build_type
    clang_arguments += " -isystem'%s'/common/inc"
else:
    run_test_ext = ".sh"
    clang_arguments += " -isystem'%s'/samples/common/inc"
clang_arguments += " -I'%s'/include"
if config.pointer_size == 8:
    clang_arguments += " -D__LP64__"

hipify_arguments = "--cuda-path='%s'"

config.substitutions.append(("%clang_args", clang_arguments % (config.cuda_sdk_root, config.cuda_dnn_root)))
config.substitutions.append(("%hipify_args", hipify_arguments % (config.cuda_root)))
config.substitutions.append(("hipify", '"' + hipify_path + "/hipify-clang" + '"'))
config.substitutions.append(("%run_test", '"' + config.test_source_root + "/run_test" + run_test_ext + '"'))
