1# -*- Python -*-
2
3import os
4import platform
5import re
6import subprocess
7
8import lit.formats
9import lit.util
10
11# Configuration file for the 'lit' test runner.
12
13# name: The name of this test suite.
14config.name = 'Clang Tools'
15
16# Tweak PATH for Win32
17if platform.system() == 'Windows':
18    # Seek sane tools in directories and set to $PATH.
19    path = getattr(config, 'lit_tools_dir', None)
20    path = lit_config.getToolsPath(path,
21                                   config.environment['PATH'],
22                                   ['cmp.exe', 'grep.exe', 'sed.exe'])
23    if path is not None:
24        path = os.path.pathsep.join((path,
25                                     config.environment['PATH']))
26        config.environment['PATH'] = path
27
28# Choose between lit's internal shell pipeline runner and a real shell.  If
29# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
30use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
31if use_lit_shell:
32    # 0 is external, "" is default, and everything else is internal.
33    execute_external = (use_lit_shell == "0")
34else:
35    # Otherwise we default to internal on Windows and external elsewhere, as
36    # bash on Windows is usually very slow.
37    execute_external = (not sys.platform in ['win32'])
38
39# testFormat: The test format to use to interpret tests.
40#
41# For now we require '&&' between commands, until they get globally killed and
42# the test runner updated.
43config.test_format = lit.formats.ShTest(execute_external)
44
45# suffixes: A list of file extensions to treat as test files.
46config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
47  '.modularize', '.module-map-checker', '.test']
48
49# Test-time dependencies located in directories called 'Inputs' are excluded
50# from test suites; there won't be any lit tests within them.
51config.excludes = ['Inputs']
52
53# test_source_root: The root path where tests are located.
54config.test_source_root = os.path.dirname(__file__)
55
56# test_exec_root: The root path where tests should be run.
57config.test_exec_root = os.path.join(config.clang_tools_binary_dir, 'test')
58
59# Clear some environment variables that might affect Clang.
60#
61# This first set of vars are read by Clang, but shouldn't affect tests
62# that aren't specifically looking for these features, or are required
63# simply to run the tests at all.
64#
65# FIXME: Should we have a tool that enforces this?
66
67# safe_env_vars = ('TMPDIR', 'TEMP', 'TMP', 'USERPROFILE', 'PWD',
68#                  'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET',
69#                  'IOS_SIMULATOR_DEPLOYMENT_TARGET',
70#                  'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS',
71#                  'VC80COMNTOOLS')
72possibly_dangerous_env_vars = ['COMPILER_PATH', 'RC_DEBUG_OPTIONS',
73                               'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH',
74                               'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH',
75                               'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH',
76                               'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING',
77                               'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX',
78                               'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS',
79                               'LIBCLANG_RESOURCE_USAGE',
80                               'LIBCLANG_CODE_COMPLETION_LOGGING']
81# Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it.
82if platform.system() != 'Windows':
83    possibly_dangerous_env_vars.append('INCLUDE')
84for name in possibly_dangerous_env_vars:
85  if name in config.environment:
86    del config.environment[name]
87
88# Tweak the PATH to include the tools dir and the scripts dir.
89path = os.path.pathsep.join((
90        config.clang_tools_dir, config.llvm_tools_dir, config.environment['PATH']))
91config.environment['PATH'] = path
92
93path = os.path.pathsep.join((config.clang_libs_dir, config.llvm_libs_dir,
94                              config.environment.get('LD_LIBRARY_PATH','')))
95config.environment['LD_LIBRARY_PATH'] = path
96
97# When running under valgrind, we mangle '-vg' onto the end of the triple so we
98# can check it with XFAIL and XTARGET.
99if lit_config.useValgrind:
100    config.target_triple += '-vg'
101
102config.available_features.add('crash-recovery')
103# Set available features we allow tests to conditionalize on.
104#
105
106# Shell execution
107if execute_external:
108    config.available_features.add('shell')
109
110# Exclude MSYS due to transforming '/' to 'X:/mingwroot/'.
111if not platform.system() in ['Windows'] or not execute_external:
112    config.available_features.add('shell-preserves-root')
113
114# ANSI escape sequences in non-dumb terminal
115if platform.system() not in ['Windows']:
116    config.available_features.add('ansi-escape-sequences')
117
118if config.clang_staticanalyzer:
119    config.available_features.add('static-analyzer')
120
121# Get shlex.quote if available (added in 3.3), and fall back to pipes.quote if
122# it's not available.
123try:
124    import shlex
125    sh_quote = shlex.quote
126except:
127    import pipes
128    sh_quote = pipes.quote
129python_exec = sh_quote(config.python_executable)
130
131check_clang_tidy = os.path.join(
132    config.test_source_root, "clang-tidy", "check_clang_tidy.py")
133config.substitutions.append(
134    ('%check_clang_tidy',
135     '%s %s' % (python_exec, check_clang_tidy)) )
136clang_tidy_diff = os.path.join(
137    config.test_source_root, "..", "clang-tidy", "tool", "clang-tidy-diff.py")
138config.substitutions.append(
139    ('%clang_tidy_diff',
140     '%s %s' % (python_exec, clang_tidy_diff)) )
141run_clang_tidy = os.path.join(
142    config.test_source_root, "..", "clang-tidy", "tool", "run-clang-tidy.py")
143config.substitutions.append(
144    ('%run_clang_tidy',
145     '%s %s' % (python_exec, run_clang_tidy)) )
146
147clangd_benchmarks_dir = os.path.join(os.path.dirname(config.clang_tools_dir),
148                                     "tools", "clang", "tools", "extra",
149                                     "clangd", "benchmarks")
150config.substitutions.append(('%clangd-benchmark-dir',
151                             '%s' % (clangd_benchmarks_dir)))
152