# -*- 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)

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

delimiter = "===============================================================";
print(delimiter)
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(delimiter)
warns = None
if not config.cuda_dnn_root:
    config.excludes.append('cudnn_convolution_forward.cu')
    config.excludes.append('cudnn_softmax.cu')
    print("WARN: cuDNN tests are excluded due to unset CUDA_DNN_ROOT_DIR")
    warns = True
if not config.cuda_cub_root:
    config.excludes.append('cub_01.cu')
    config.excludes.append('cub_02.cu')
    config.excludes.append('cub_03.cu')
    print("WARN: CUB tests are excluded due to unset CUDA_CUB_ROOT_DIR")
    warns = True
if warns:
    print(delimiter)

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
    # CUDA SDK ROOT
    clang_arguments += " -isystem'%s'/common/inc"
else:
    run_test_ext = ".sh"
    # CUDA SDK ROOT
    clang_arguments += " -isystem'%s'/samples/common/inc"
if config.pointer_size == 8:
    clang_arguments += " -D__LP64__"

# Set max clang's CudaArch for corresponding CUDA version
# to support maximum CUDA features in offline tests
if config.cuda_version_major == 7:
    if config.cuda_version_minor == 5:
        clang_arguments += " --cuda-gpu-arch=sm_53"
    else:
        clang_arguments += " --cuda-gpu-arch=sm_52"
elif config.cuda_version_major == 8:
    clang_arguments += " --cuda-gpu-arch=sm_62"
elif config.cuda_version_major == 9:
    if config.cuda_version_minor == 2:
        clang_arguments += " --cuda-gpu-arch=sm_72"
    else:
        clang_arguments += " --cuda-gpu-arch=sm_70"
elif config.cuda_version_major == 10:
    clang_arguments += " --cuda-gpu-arch=sm_75"

# cuDNN ROOT
if config.cuda_dnn_root:
    clang_arguments += " -I'%s'/include"
# CUB ROOT
if config.cuda_cub_root:
    clang_arguments += " -I'%s'"

if config.cuda_dnn_root and config.cuda_cub_root:
    config.substitutions.append(("%clang_args", clang_arguments % (config.cuda_sdk_root, config.cuda_dnn_root, config.cuda_cub_root)))
elif config.cuda_dnn_root:
    config.substitutions.append(("%clang_args", clang_arguments % (config.cuda_sdk_root, config.cuda_dnn_root)))
elif config.cuda_cub_root:
    config.substitutions.append(("%clang_args", clang_arguments % (config.cuda_sdk_root, config.cuda_cub_root)))
else:
    config.substitutions.append(("%clang_args", clang_arguments % config.cuda_sdk_root))

if config.llvm_version_major < 4:
    hipify_arguments = "-I'%s'/include"
else:
    hipify_arguments = "--cuda-path='%s'"

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 + '"'))
