# -*- Python -*-

# Configuration file for the 'lit' test runner.

import os
import platform
import shlex

import lit.formats

# name: The name of this test suite.
config.name = 'lldb-Suite'

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

# test_source_root: The root path where tests are located.
# test_exec_root: The root path where tests should be run.
config.test_source_root = os.path.join(config.lldb_src_root, 'packages',
                                       'Python', 'lldbsuite', 'test')
config.test_exec_root = config.test_source_root

# macOS flags needed for LLDB built with address sanitizer.
if 'Address' in config.llvm_use_sanitizer and \
   'Darwin' in config.host_os and \
   'x86' in config.host_triple:
  import subprocess
  resource_dir = subprocess.check_output(
    config.cmake_cxx_compiler +' -print-resource-dir', shell=True).strip()
  runtime = os.path.join(resource_dir, 'lib', 'darwin',
                         'libclang_rt.asan_osx_dynamic.dylib')
  config.environment['ASAN_OPTIONS'] = \
    'detect_stack_use_after_return=1'
  config.environment['DYLD_INSERT_LIBRARIES'] = runtime

# Shared library build of LLVM may require LD_LIBRARY_PATH or equivalent.
def find_shlibpath_var():
  if platform.system() in ['Linux', 'FreeBSD', 'NetBSD', 'SunOS']:
    yield 'LD_LIBRARY_PATH'
  elif platform.system() == 'Darwin':
    yield 'DYLD_LIBRARY_PATH'
  elif platform.system() == 'Windows':
    yield 'PATH'

for shlibpath_var in find_shlibpath_var():
  # In stand-alone build llvm_shlib_dir specifies LLDB's lib directory
  # while llvm_libs_dir specifies LLVM's lib directory.
  shlibpath = os.path.pathsep.join(
    (config.llvm_shlib_dir,
     config.llvm_libs_dir,
     config.environment.get(shlibpath_var, '')))
  config.environment[shlibpath_var] = shlibpath
  break
else:
  lit_config.warning("unable to inject shared library path on '{}'"
                     .format(platform.system()))

# Build dotest command.
dotest_cmd = [config.dotest_path, '-q']
dotest_cmd.extend(config.dotest_args_str.split(';'))

# We don't want to force users passing arguments to lit to use `;` as a
# separator. We use Python's simple lexical analyzer to turn the args into a
# list.
if config.dotest_lit_args_str:
  dotest_cmd.extend(shlex.split(config.dotest_lit_args_str))

# Library path may be needed to locate just-built clang.
if config.llvm_libs_dir:
  dotest_cmd += ['--env', 'LLVM_LIBS_DIR=' + config.llvm_libs_dir]

# Load LLDB test format.
sys.path.append(os.path.join(config.lldb_src_root, "lit", "Suite"))
import lldbtest

# testFormat: The test format to use to interpret tests.
config.test_format = lldbtest.LLDBTest(dotest_cmd)
