1# -*- Python -*-
2
3import os
4import platform
5import re
6import subprocess
7import sys
8import tempfile
9
10import lit.formats
11import lit.util
12
13from lit.llvm import llvm_config
14from lit.llvm.subst import ToolSubst
15from lit.llvm.subst import FindTool
16
17# Configuration file for the 'lit' test runner.
18
19# name: The name of this test suite.
20config.name = 'debuginfo-tests'
21
22# testFormat: The test format to use to interpret tests.
23#
24# For now we require '&&' between commands, until they get globally killed and
25# the test runner updated.
26config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
27
28# suffixes: A list of file extensions to treat as test files.
29config.suffixes = ['.c', '.cpp', '.m']
30
31# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
32# subdirectories contain auxiliary inputs for various tests in their parent
33# directories.
34config.excludes = ['Inputs']
35
36# test_source_root: The root path where tests are located.
37config.test_source_root = os.path.join(config.debuginfo_tests_src_root)
38
39# test_exec_root: The root path where tests should be run.
40config.test_exec_root = config.debuginfo_tests_obj_root
41
42llvm_config.use_default_substitutions()
43
44tools = [
45    ToolSubst('%test_debuginfo', command=os.path.join(
46        config.debuginfo_tests_src_root, 'llgdb-tests', 'test_debuginfo.pl')),
47    ToolSubst("%llvm_src_root", config.llvm_src_root),
48    ToolSubst("%llvm_tools_dir", config.llvm_tools_dir),
49]
50
51def get_required_attr(config, attr_name):
52  attr_value = getattr(config, attr_name, None)
53  if attr_value == None:
54    lit_config.fatal(
55      "No attribute %r in test configuration! You may need to run "
56      "tests from your build directory or add this attribute "
57      "to lit.site.cfg " % attr_name)
58  return attr_value
59
60# If this is an MSVC environment, the tests at the root of the tree are
61# unsupported. The local win_cdb test suite, however, is supported.
62is_msvc = get_required_attr(config, "is_msvc")
63if is_msvc:
64    config.available_features.add('msvc')
65    # FIXME: We should add some llvm lit utility code to find the Windows SDK
66    # and set up the environment appopriately.
67    win_sdk = 'C:/Program Files (x86)/Windows Kits/10/'
68    arch = 'x64'
69    llvm_config.with_system_environment(['LIB', 'LIBPATH', 'INCLUDE'])
70    # Clear _NT_SYMBOL_PATH to prevent cdb from attempting to load symbols from
71    # the network.
72    llvm_config.with_environment('_NT_SYMBOL_PATH', '')
73    tools.append(ToolSubst('%cdb', '"%s"' % os.path.join(win_sdk, 'Debuggers',
74                                                         arch, 'cdb.exe')))
75
76# clang_src_dir is not used by these tests, but is required by
77# use_clang(), so set it to "".
78if not hasattr(config, 'clang_src_dir'):
79    config.clang_src_dir = ""
80llvm_config.use_clang()
81
82if config.llvm_use_sanitizer:
83    # Propagate path to symbolizer for ASan/MSan.
84    llvm_config.with_system_environment(
85        ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH'])
86llvm_config.with_environment('PATHTOCLANG', llvm_config.config.clang)
87llvm_config.with_environment('PATHTOCLANGPP', llvm_config.use_llvm_tool('clang++'))
88llvm_config.with_environment('PATHTOCLANGCL', llvm_config.use_llvm_tool('clang-cl'))
89
90# Check which debuggers are available:
91built_lldb = llvm_config.use_llvm_tool('lldb', search_env='CLANG')
92lldb_path = None
93if built_lldb is not None:
94    lldb_path = built_lldb
95elif lit.util.which('lldb') is not None:
96    lldb_path = lit.util.which('lldb')
97
98if lldb_path is not None:
99    config.available_features.add('lldb')
100
101# Produce dexter path, lldb path, and combine into the %dexter substitution
102# for running a test.
103dexter_path = os.path.join(config.debuginfo_tests_src_root,
104                           'dexter', 'dexter.py')
105dexter_test_cmd = '"{}" "{}" test'.format(config.python3_executable, dexter_path)
106if lldb_path is not None:
107  dexter_test_cmd += ' --lldb-executable {}'.format(lldb_path)
108tools.append(ToolSubst('%dexter', dexter_test_cmd))
109
110# For testing other bits of dexter that aren't under the "test" subcommand,
111# have a %dexter_base substitution.
112dexter_base_cmd = '"{}" "{}"'.format(config.python3_executable, dexter_path)
113tools.append(ToolSubst('%dexter_base', dexter_base_cmd))
114
115tool_dirs = [config.llvm_tools_dir]
116
117llvm_config.add_tool_substitutions(tools, tool_dirs)
118
119lit.util.usePlatformSdkOnDarwin(config, lit_config)
120
121# available_features: REQUIRES/UNSUPPORTED lit commands look at this list.
122if platform.system() == 'Darwin':
123    import subprocess
124    xcode_lldb_vers = subprocess.check_output(['xcrun', 'lldb', '--version']).decode("utf-8")
125    match = re.search('lldb-(\d+)', xcode_lldb_vers)
126    if match:
127        apple_lldb_vers = int(match.group(1))
128        if apple_lldb_vers < 1000:
129            config.available_features.add('apple-lldb-pre-1000')
130
131