#  vim: set ft=python ts=8 sw=4 tw=0 et :

import os
import sys
import re
import platform
import subprocess

import lit.util
import lit.formats

config.name = 'mytest'
config.test_format = lit.formats.ShTest()
config.suffixes = ['.c']

curdir = os.path.dirname(os.path.realpath(__file__))
project_root = os.path.normpath(os.path.join(curdir, os.path.pardir))
build_root = os.path.join(project_root, 'build')
plugin_path = os.path.join(build_root, 'bin')
config.test_root = curdir

config.environment['PATH'] = os.environ['PATH']

if 'LLVMCC' in os.environ:
    config.environment['LLVMCC'] = os.getenv('LLVMCC')

config.target_triple = subprocess.check_output(
    'llvm-config --host-target'.split())


os_lib_dict = {
    'Linux': '.so',
    'Darwin': '.dylib'
}
libext = os_lib_dict[platform.system()]

plugin_list = []
for l in os.listdir(plugin_path):
    if l.endswith(libext):
        plugin_list.append(os.path.join(plugin_path, l))

load_list = ['-load {}'.format(i) for i in plugin_list]
clang_load_list = ['-Xclang -load -Xclang {}'.format(i) for i in plugin_list]
clang_cc1 = 'clang -cc1 {}'.format(' '.join(load_list))
clang = 'clang {}'.format(' '.join(clang_load_list))
config.substitutions.append(('%clang_cc1', clang_cc1))
config.substitutions.append(('%clang', clang))

###
print('=' * 80)
for c in config.substitutions:
    print('{:20} {}'.format(c[0], c[1]))
print('=' * 80)
