1# -*- Python -*-
2
3import os
4import platform
5import re
6import shutil
7import site
8import subprocess
9import sys
10
11import lit.formats
12from lit.llvm import llvm_config
13from lit.llvm.subst import FindTool
14from lit.llvm.subst import ToolSubst
15from distutils.spawn import find_executable
16
17site.addsitedir(os.path.dirname(__file__))
18from helper import toolchain
19
20# name: The name of this test suite.
21config.name = 'lldb-shell'
22
23# testFormat: The test format to use to interpret tests.
24config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
25
26# suffixes: A list of file extensions to treat as test files. This is overriden
27# by individual lit.local.cfg files in the test subdirectories.
28config.suffixes = ['.test', '.cpp', '.s']
29
30# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
31# subdirectories contain auxiliary inputs for various tests in their parent
32# directories.
33config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']
34
35# test_source_root: The root path where tests are located.
36config.test_source_root = os.path.dirname(__file__)
37
38# test_exec_root: The root path where tests should be run.
39config.test_exec_root = os.path.join(config.lldb_obj_root, 'test')
40
41# Propagate reproducer environment vars.
42if 'LLDB_CAPTURE_REPRODUCER' in os.environ:
43  config.environment['LLDB_CAPTURE_REPRODUCER'] = os.environ[
44      'LLDB_CAPTURE_REPRODUCER']
45
46# Support running the test suite under the lldb-repro wrapper. This makes it
47# possible to capture a test suite run and then rerun all the test from the
48# just captured reproducer.
49lldb_repro_mode = lit_config.params.get('lldb-run-with-repro', None)
50if lldb_repro_mode:
51  config.available_features.add('lldb-repro')
52  lit_config.note("Running Shell test with lldb-repo in {} mode.".format(lldb_repro_mode))
53  toolchain.use_lldb_repro_substitutions(config, lldb_repro_mode)
54
55llvm_config.use_default_substitutions()
56toolchain.use_lldb_substitutions(config)
57toolchain.use_support_substitutions(config)
58
59if re.match(r'^arm(hf.*-linux)|(.*-linux-gnuabihf)', config.target_triple):
60    config.available_features.add("armhf-linux")
61
62def calculate_arch_features(arch_string):
63    # This will add a feature such as x86, arm, mips, etc for each built
64    # target
65    features = []
66    for arch in arch_string.split():
67        features.append(arch.lower())
68    return features
69
70# Run llvm-config and add automatically add features for whether we have
71# assertions enabled, whether we are in debug mode, and what targets we
72# are built for.
73llvm_config.feature_config(
74    [('--assertion-mode', {'ON': 'asserts'}),
75     ('--build-mode', {'DEBUG': 'debug'}),
76     ('--targets-built', calculate_arch_features)
77     ])
78
79# Clean the module caches in the test build directory. This is necessary in an
80# incremental build whenever clang changes underneath, so doing it once per
81# lit.py invocation is close enough.
82for cachedir in [config.clang_module_cache, config.lldb_module_cache]:
83  if os.path.isdir(cachedir):
84     print("Deleting module cache at %s."%cachedir)
85     shutil.rmtree(cachedir)
86
87# Set a default per-test timeout of 10 minutes. Setting a timeout per test
88# requires that killProcessAndChildren() is supported on the platform and
89# lit complains if the value is set but it is not supported.
90supported, errormsg = lit_config.maxIndividualTestTimeIsSupported
91if supported:
92    lit_config.maxIndividualTestTime = 600
93else:
94    lit_config.warning("Could not set a default per-test timeout. " + errormsg)
95
96
97# If running tests natively, check for CPU features needed for some tests.
98
99if 'native' in config.available_features:
100    cpuid_exe = lit.util.which('lit-cpuid', config.lldb_tools_dir)
101    if cpuid_exe is None:
102        lit_config.warning("lit-cpuid not found, tests requiring CPU extensions will be skipped")
103    else:
104        out, err, exitcode = lit.util.executeCommand([cpuid_exe])
105        if exitcode == 0:
106            for x in out.split():
107                config.available_features.add('native-cpu-%s' % x)
108        else:
109            lit_config.warning("lit-cpuid failed: %s" % err)
110
111if config.lldb_enable_python:
112    config.available_features.add('python')
113
114if config.lldb_enable_lua:
115    config.available_features.add('lua')
116
117if config.lldb_enable_lzma:
118    config.available_features.add('lzma')
119
120if find_executable('xz') != None:
121    config.available_features.add('xz')
122
123# NetBSD permits setting dbregs either if one is root
124# or if user_set_dbregs is enabled
125can_set_dbregs = True
126if platform.system() == 'NetBSD' and os.geteuid() != 0:
127    try:
128        output = subprocess.check_output(["/sbin/sysctl", "-n",
129          "security.models.extensions.user_set_dbregs"]).decode().strip()
130        if output != "1":
131            can_set_dbregs = False
132    except subprocess.CalledProcessError:
133        can_set_dbregs = False
134if can_set_dbregs:
135    config.available_features.add('dbregs-set')
136