xref: /llvm-project/clang/test/lit.cfg.py (revision 00132643744237d2398fea1777114f3497629bd5)
1# -*- Python -*-
2
3import os
4import platform
5import re
6import subprocess
7import tempfile
8
9import lit.formats
10import lit.util
11
12from lit.llvm import llvm_config
13from lit.llvm.subst import ToolSubst
14from lit.llvm.subst import FindTool
15
16# Configuration file for the 'lit' test runner.
17
18# name: The name of this test suite.
19config.name = "Clang"
20
21# testFormat: The test format to use to interpret tests.
22#
23# For now we require '&&' between commands, until they get globally killed and
24# the test runner updated.
25config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
26
27# suffixes: A list of file extensions to treat as test files.
28config.suffixes = [
29    ".c",
30    ".cpp",
31    ".i",
32    ".cppm",
33    ".m",
34    ".mm",
35    ".cu",
36    ".cuh",
37    ".hip",
38    ".hlsl",
39    ".ll",
40    ".cl",
41    ".clcpp",
42    ".s",
43    ".S",
44    ".modulemap",
45    ".test",
46    ".rs",
47    ".ifs",
48    ".rc",
49]
50
51# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
52# subdirectories contain auxiliary inputs for various tests in their parent
53# directories.
54config.excludes = [
55    "Inputs",
56    "CMakeLists.txt",
57    "README.txt",
58    "LICENSE.txt",
59    "debuginfo-tests",
60]
61
62# test_source_root: The root path where tests are located.
63config.test_source_root = os.path.dirname(__file__)
64
65# test_exec_root: The root path where tests should be run.
66config.test_exec_root = os.path.join(config.clang_obj_root, "test")
67
68llvm_config.use_default_substitutions()
69
70llvm_config.use_clang()
71
72config.substitutions.append(("%src_include_dir", config.clang_src_dir + "/include"))
73
74config.substitutions.append(("%target_triple", config.target_triple))
75
76config.substitutions.append(("%PATH%", config.environment["PATH"]))
77
78
79# For each occurrence of a clang tool name, replace it with the full path to
80# the build directory holding that tool.  We explicitly specify the directories
81# to search to ensure that we get the tools just built and not some random
82# tools that might happen to be in the user's PATH.
83tool_dirs = [config.clang_tools_dir, config.llvm_tools_dir]
84
85tools = [
86    "apinotes-test",
87    "c-index-test",
88    "clang-diff",
89    "clang-format",
90    "clang-repl",
91    "clang-offload-packager",
92    "clang-tblgen",
93    "clang-scan-deps",
94    "clang-installapi",
95    "opt",
96    "llvm-ifs",
97    "yaml2obj",
98    "clang-linker-wrapper",
99    "clang-nvlink-wrapper",
100    "clang-sycl-linker",
101    "llvm-lto",
102    "llvm-lto2",
103    "llvm-profdata",
104    "llvm-readtapi",
105    ToolSubst(
106        "%clang_extdef_map",
107        command=FindTool("clang-extdef-mapping"),
108        unresolved="ignore",
109    ),
110]
111
112if config.clang_examples:
113    config.available_features.add("examples")
114
115
116def have_host_jit_feature_support(feature_name):
117    clang_repl_exe = lit.util.which("clang-repl", config.clang_tools_dir)
118
119    if not clang_repl_exe:
120        return False
121
122    try:
123        clang_repl_cmd = subprocess.Popen(
124            [clang_repl_exe, "--host-supports-" + feature_name], stdout=subprocess.PIPE
125        )
126    except OSError:
127        print("could not exec clang-repl")
128        return False
129
130    clang_repl_out = clang_repl_cmd.stdout.read().decode("ascii")
131    clang_repl_cmd.wait()
132
133    return "true" in clang_repl_out
134
135def have_host_clang_repl_cuda():
136    clang_repl_exe = lit.util.which('clang-repl', config.clang_tools_dir)
137
138    if not clang_repl_exe:
139        return False
140
141    testcode = b'\n'.join([
142        b"__global__ void test_func() {}",
143        b"test_func<<<1,1>>>();",
144        b"extern \"C\" int puts(const char *s);",
145        b"puts(cudaGetLastError() ? \"failure\" : \"success\");",
146        b"%quit"
147    ])
148    try:
149        clang_repl_cmd = subprocess.run([clang_repl_exe, '--cuda'],
150                                        stdout=subprocess.PIPE,
151                                        stderr=subprocess.PIPE,
152                                        input=testcode)
153    except OSError:
154        return False
155
156    if clang_repl_cmd.returncode == 0:
157        if clang_repl_cmd.stdout.find(b"success") != -1:
158            return True
159
160    return False
161
162if have_host_jit_feature_support('jit'):
163    config.available_features.add('host-supports-jit')
164
165    if have_host_clang_repl_cuda():
166        config.available_features.add('host-supports-cuda')
167
168if config.clang_staticanalyzer:
169    config.available_features.add("staticanalyzer")
170    tools.append("clang-check")
171
172    if config.clang_staticanalyzer_z3:
173        config.available_features.add("z3")
174    else:
175        config.available_features.add("no-z3")
176
177    check_analyzer_fixit_path = os.path.join(
178        config.test_source_root, "Analysis", "check-analyzer-fixit.py"
179    )
180    config.substitutions.append(
181        (
182            "%check_analyzer_fixit",
183            '"%s" %s' % (config.python_executable, check_analyzer_fixit_path),
184        )
185    )
186
187llvm_config.add_tool_substitutions(tools, tool_dirs)
188
189config.substitutions.append(
190    (
191        "%hmaptool",
192        "'%s' %s"
193        % (
194            config.python_executable,
195            os.path.join(config.clang_src_dir, "utils", "hmaptool", "hmaptool"),
196        ),
197    )
198)
199
200config.substitutions.append(
201    (
202        "%deps-to-rsp",
203        '"%s" %s'
204        % (
205            config.python_executable,
206            os.path.join(config.clang_src_dir, "utils", "module-deps-to-rsp.py"),
207        ),
208    )
209)
210
211config.substitutions.append(("%host_cc", config.host_cc))
212config.substitutions.append(("%host_cxx", config.host_cxx))
213
214# Determine whether the test target is compatible with execution on the host.
215if "aarch64" in config.host_arch:
216    config.available_features.add("aarch64-host")
217
218# Plugins (loadable modules)
219if config.has_plugins and config.llvm_plugin_ext:
220    config.available_features.add("plugins")
221
222if config.clang_default_pie_on_linux:
223    config.available_features.add("default-pie-on-linux")
224
225# Set available features we allow tests to conditionalize on.
226#
227if config.clang_default_cxx_stdlib != "":
228    config.available_features.add(
229        "default-cxx-stdlib={}".format(config.clang_default_cxx_stdlib)
230    )
231
232# As of 2011.08, crash-recovery tests still do not pass on FreeBSD.
233if platform.system() not in ["FreeBSD"]:
234    config.available_features.add("crash-recovery")
235
236# ANSI escape sequences in non-dumb terminal
237if platform.system() not in ["Windows"]:
238    config.available_features.add("ansi-escape-sequences")
239
240# Capability to print utf8 to the terminal.
241# Windows expects codepage, unless Wide API.
242if platform.system() not in ["Windows"]:
243    config.available_features.add("utf8-capable-terminal")
244
245# Support for libgcc runtime. Used to rule out tests that require
246# clang to run with -rtlib=libgcc.
247if platform.system() not in ["Darwin", "Fuchsia"]:
248    config.available_features.add("libgcc")
249
250# Case-insensitive file system
251
252
253def is_filesystem_case_insensitive():
254    handle, path = tempfile.mkstemp(prefix="case-test", dir=config.test_exec_root)
255    isInsensitive = os.path.exists(
256        os.path.join(os.path.dirname(path), os.path.basename(path).upper())
257    )
258    os.close(handle)
259    os.remove(path)
260    return isInsensitive
261
262
263if is_filesystem_case_insensitive():
264    config.available_features.add("case-insensitive-filesystem")
265
266# Tests that require the /dev/fd filesystem.
267if os.path.exists("/dev/fd/0") and sys.platform not in ["cygwin"]:
268    config.available_features.add("dev-fd-fs")
269
270# Set on native MS environment.
271if re.match(r".*-(windows-msvc)$", config.target_triple):
272    config.available_features.add("ms-sdk")
273
274# [PR8833] LLP64-incompatible tests
275if not re.match(
276    r"^(aarch64|x86_64).*-(windows-msvc|windows-gnu)$", config.target_triple
277):
278    config.available_features.add("LP64")
279
280# Tests that are specific to the Apple Silicon macOS.
281if re.match(r"^arm64(e)?-apple-(macos|darwin)", config.target_triple):
282    config.available_features.add("apple-silicon-mac")
283
284# [PR18856] Depends to remove opened file. On win32, a file could be removed
285# only if all handles were closed.
286if platform.system() not in ["Windows"]:
287    config.available_features.add("can-remove-opened-file")
288
289# Features
290known_arches = ["x86_64", "mips64", "ppc64", "aarch64"]
291if any(config.target_triple.startswith(x) for x in known_arches):
292    config.available_features.add("clang-target-64-bits")
293
294
295def calculate_arch_features(arch_string):
296    features = []
297    for arch in arch_string.split():
298        features.append(arch.lower() + "-registered-target")
299    return features
300
301
302llvm_config.feature_config(
303    [
304        ("--assertion-mode", {"ON": "asserts"}),
305        ("--cxxflags", {r"-D_GLIBCXX_DEBUG\b": "libstdcxx-safe-mode"}),
306        ("--targets-built", calculate_arch_features),
307    ]
308)
309
310if lit.util.which("xmllint"):
311    config.available_features.add("xmllint")
312
313if config.enable_backtrace:
314    config.available_features.add("backtrace")
315
316if config.enable_threads:
317    config.available_features.add("thread_support")
318
319# Check if we should allow outputs to console.
320run_console_tests = int(lit_config.params.get("enable_console", "0"))
321if run_console_tests != 0:
322    config.available_features.add("console")
323
324lit.util.usePlatformSdkOnDarwin(config, lit_config)
325macOSSDKVersion = lit.util.findPlatformSdkVersionOnMacOS(config, lit_config)
326if macOSSDKVersion is not None:
327    config.available_features.add("macos-sdk-" + str(macOSSDKVersion))
328
329if os.path.exists("/etc/gentoo-release"):
330    config.available_features.add("gentoo")
331
332if config.enable_shared:
333    config.available_features.add("enable_shared")
334
335# Add a vendor-specific feature.
336if config.clang_vendor_uti:
337    config.available_features.add("clang-vendor=" + config.clang_vendor_uti)
338
339if config.have_llvm_driver:
340    config.available_features.add("llvm-driver")
341
342
343# Some tests perform deep recursion, which requires a larger pthread stack size
344# than the relatively low default of 192 KiB for 64-bit processes on AIX. The
345# `AIXTHREAD_STK` environment variable provides a non-intrusive way to request
346# a larger pthread stack size for the tests. Various applications and runtime
347# libraries on AIX use a default pthread stack size of 4 MiB, so we will use
348# that as a default value here.
349if "AIXTHREAD_STK" in os.environ:
350    config.environment["AIXTHREAD_STK"] = os.environ["AIXTHREAD_STK"]
351elif platform.system() == "AIX":
352    config.environment["AIXTHREAD_STK"] = "4194304"
353
354# Some tools support an environment variable "OBJECT_MODE" on AIX OS, which
355# controls the kind of objects they will support. If there is no "OBJECT_MODE"
356# environment variable specified, the default behaviour is to support 32-bit
357# objects only. In order to not affect most test cases, which expect to support
358# 32-bit and 64-bit objects by default, set the environment variable
359# "OBJECT_MODE" to "any" by default on AIX OS.
360
361if "system-aix" in config.available_features:
362   config.substitutions.append(("llvm-nm", "env OBJECT_MODE=any llvm-nm"))
363   config.substitutions.append(("llvm-ar", "env OBJECT_MODE=any llvm-ar"))
364   config.substitutions.append(("llvm-ranlib", "env OBJECT_MODE=any llvm-ranlib"))
365
366# It is not realistically possible to account for all options that could
367# possibly be present in system and user configuration files, so disable
368# default configs for the test runs.
369config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"
370