blob: 66fdd636328851a4cc334514c7265f33db4d4bb9 [file] [log] [blame]
Don Hinton3a58f672017-12-12 16:54:20 +00001import os
2import platform
3import re
4import subprocess
Jeremy Morse984fad22019-10-31 16:51:53 +00005import sys
Thomas Lively70620942022-03-17 15:22:17 -07006
Don Hinton3a58f672017-12-12 16:54:20 +00007import lit.formats
8import lit.util
9
10from lit.llvm import llvm_config
11from lit.llvm.subst import ToolSubst
Don Hinton3a58f672017-12-12 16:54:20 +000012
13# Configuration file for the 'lit' test runner.
14
15# name: The name of this test suite.
Tobias Hietaf98ee402023-05-17 16:59:41 +020016config.name = "cross-project-tests"
Don Hinton3a58f672017-12-12 16:54:20 +000017
18# testFormat: The test format to use to interpret tests.
Don Hinton3a58f672017-12-12 16:54:20 +000019config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
20
21# suffixes: A list of file extensions to treat as test files.
Tobias Hietaf98ee402023-05-17 16:59:41 +020022config.suffixes = [".c", ".cl", ".cpp", ".m"]
Don Hinton3a58f672017-12-12 16:54:20 +000023
24# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
25# subdirectories contain auxiliary inputs for various tests in their parent
26# directories.
Tobias Hietaf98ee402023-05-17 16:59:41 +020027config.excludes = ["Inputs"]
Don Hinton3a58f672017-12-12 16:54:20 +000028
29# test_source_root: The root path where tests are located.
James Henderson24af0992021-02-11 15:41:32 +000030config.test_source_root = config.cross_project_tests_src_root
Don Hinton3a58f672017-12-12 16:54:20 +000031
32# test_exec_root: The root path where tests should be run.
James Henderson24af0992021-02-11 15:41:32 +000033config.test_exec_root = config.cross_project_tests_obj_root
Don Hinton3a58f672017-12-12 16:54:20 +000034
Jeremy Morse984fad22019-10-31 16:51:53 +000035llvm_config.use_default_substitutions()
36
Reid Kleckner75d38f12019-05-28 23:03:33 +000037tools = [
Tobias Hietaf98ee402023-05-17 16:59:41 +020038 ToolSubst(
39 "%test_debuginfo",
40 command=os.path.join(
41 config.cross_project_tests_src_root,
42 "debuginfo-tests",
43 "llgdb-tests",
44 "test_debuginfo.pl",
45 ),
46 ),
Christian Sigg60346bd2020-01-11 08:47:41 +010047 ToolSubst("%llvm_src_root", config.llvm_src_root),
48 ToolSubst("%llvm_tools_dir", config.llvm_tools_dir),
Reid Kleckner75d38f12019-05-28 23:03:33 +000049]
50
Tobias Hietaf98ee402023-05-17 16:59:41 +020051
Reid Kleckner75d38f12019-05-28 23:03:33 +000052def get_required_attr(config, attr_name):
Tobias Hietaf98ee402023-05-17 16:59:41 +020053 attr_value = getattr(config, attr_name, None)
Eisuke Kawashimaca92bdf2025-01-13 21:03:04 +090054 if attr_value is None:
Tobias Hietaf98ee402023-05-17 16:59:41 +020055 lit_config.fatal(
56 "No attribute %r in test configuration! You may need to run "
57 "tests from your build directory or add this attribute "
58 "to lit.site.cfg " % attr_name
59 )
60 return attr_value
61
Reid Kleckner75d38f12019-05-28 23:03:33 +000062
63# If this is an MSVC environment, the tests at the root of the tree are
64# unsupported. The local win_cdb test suite, however, is supported.
65is_msvc = get_required_attr(config, "is_msvc")
66if is_msvc:
Tobias Hietaf98ee402023-05-17 16:59:41 +020067 config.available_features.add("msvc")
Reid Kleckner75d38f12019-05-28 23:03:33 +000068 # FIXME: We should add some llvm lit utility code to find the Windows SDK
69 # and set up the environment appopriately.
Tobias Hietaf98ee402023-05-17 16:59:41 +020070 win_sdk = "C:/Program Files (x86)/Windows Kits/10/"
71 arch = "x64"
72 llvm_config.with_system_environment(["LIB", "LIBPATH", "INCLUDE"])
Reid Kleckner75d38f12019-05-28 23:03:33 +000073 # Clear _NT_SYMBOL_PATH to prevent cdb from attempting to load symbols from
74 # the network.
Tobias Hietaf98ee402023-05-17 16:59:41 +020075 llvm_config.with_environment("_NT_SYMBOL_PATH", "")
76 tools.append(
77 ToolSubst("%cdb", '"%s"' % os.path.join(win_sdk, "Debuggers", arch, "cdb.exe"))
78 )
Reid Kleckner75d38f12019-05-28 23:03:33 +000079
James Henderson4446a722021-02-09 14:57:03 +000080# clang_src_dir and lld_src_dir are not used by these tests, but are required by
81# use_clang() and use_lld() respectively, so set them to "", if needed.
Tobias Hietaf98ee402023-05-17 16:59:41 +020082if not hasattr(config, "clang_src_dir"):
Don Hinton3a58f672017-12-12 16:54:20 +000083 config.clang_src_dir = ""
Amir Ayupov9fec33a2024-01-18 19:59:09 -080084llvm_config.use_clang(required=("clang" in config.llvm_enabled_projects))
James Henderson38276002021-02-09 15:19:27 +000085
Tobias Hietaf98ee402023-05-17 16:59:41 +020086if not hasattr(config, "lld_src_dir"):
James Henderson4446a722021-02-09 14:57:03 +000087 config.lld_src_dir = ""
Tobias Hietaf98ee402023-05-17 16:59:41 +020088llvm_config.use_lld(required=("lld" in config.llvm_enabled_projects))
Don Hinton3a58f672017-12-12 16:54:20 +000089
Tobias Hietaf98ee402023-05-17 16:59:41 +020090if "compiler-rt" in config.llvm_enabled_projects:
91 config.available_features.add("compiler-rt")
OCHyamsac0f3292022-02-09 17:08:43 +000092
OCHyams18ee8982021-12-16 13:41:29 +000093# Check which debuggers are available:
Tobias Hietaf98ee402023-05-17 16:59:41 +020094lldb_path = llvm_config.use_llvm_tool("lldb", search_env="LLDB")
OCHyams18ee8982021-12-16 13:41:29 +000095
96if lldb_path is not None:
Tobias Hietaf98ee402023-05-17 16:59:41 +020097 config.available_features.add("lldb")
98
OCHyams18ee8982021-12-16 13:41:29 +000099
100def configure_dexter_substitutions():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200101 """Configure substitutions for host platform and return list of dependencies"""
102 # Produce dexter path, lldb path, and combine into the %dexter substitution
103 # for running a test.
104 dexter_path = os.path.join(
105 config.cross_project_tests_src_root, "debuginfo-tests", "dexter", "dexter.py"
106 )
107 dexter_test_cmd = '"{}" "{}" test'.format(sys.executable, dexter_path)
108 if lldb_path is not None:
109 dexter_test_cmd += ' --lldb-executable "{}"'.format(lldb_path)
110 tools.append(ToolSubst("%dexter", dexter_test_cmd))
OCHyams18ee8982021-12-16 13:41:29 +0000111
Tobias Hietaf98ee402023-05-17 16:59:41 +0200112 # For testing other bits of dexter that aren't under the "test" subcommand,
113 # have a %dexter_base substitution.
114 dexter_base_cmd = '"{}" "{}"'.format(sys.executable, dexter_path)
115 tools.append(ToolSubst("%dexter_base", dexter_base_cmd))
OCHyams18ee8982021-12-16 13:41:29 +0000116
Tobias Hietaf98ee402023-05-17 16:59:41 +0200117 # Set up commands for DexTer regression tests.
118 # Builder, debugger, optimisation level and several other flags differ
119 # depending on whether we're running a unix like or windows os.
120 if platform.system() == "Windows":
121 # The Windows builder script uses lld.
122 dependencies = ["clang", "lld-link"]
Stephen Tozer45a40c12023-09-05 16:04:53 +0100123 dexter_regression_test_builder = "clang-cl"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200124 dexter_regression_test_debugger = "dbgeng"
Stephen Tozer45a40c12023-09-05 16:04:53 +0100125 dexter_regression_test_flags = "/Zi /Od"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200126 else:
127 # Use lldb as the debugger on non-Windows platforms.
128 dependencies = ["clang", "lldb"]
Stephen Tozer45a40c12023-09-05 16:04:53 +0100129 dexter_regression_test_builder = "clang++"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200130 dexter_regression_test_debugger = "lldb"
Stephen Tozer45a40c12023-09-05 16:04:53 +0100131 dexter_regression_test_flags = "-O0 -glldb -std=gnu++11"
OCHyams18ee8982021-12-16 13:41:29 +0000132
Tobias Hietaf98ee402023-05-17 16:59:41 +0200133 tools.append(
134 ToolSubst("%dexter_regression_test_builder", dexter_regression_test_builder)
135 )
136 tools.append(
137 ToolSubst("%dexter_regression_test_debugger", dexter_regression_test_debugger)
138 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100139 # We don't need to distinguish cflags and ldflags because for Dexter
140 # regression tests we use clang to drive the linker, and so all flags will be
141 # passed in a single command.
Tobias Hietaf98ee402023-05-17 16:59:41 +0200142 tools.append(
Stephen Tozer45a40c12023-09-05 16:04:53 +0100143 ToolSubst("%dexter_regression_test_flags", dexter_regression_test_flags)
Tobias Hietaf98ee402023-05-17 16:59:41 +0200144 )
OCHyamsde3f8152022-01-26 11:09:21 +0000145
Tobias Hietaf98ee402023-05-17 16:59:41 +0200146 # Typical command would take the form:
Stephen Tozer45a40c12023-09-05 16:04:53 +0100147 # ./path_to_py/python.exe ./path_to_dex/dexter.py test --fail-lt 1.0 -w --binary %t --debugger lldb --cflags '-O0 -g'
148 dexter_regression_test_run = " ".join(
Tobias Hietaf98ee402023-05-17 16:59:41 +0200149 # "python", "dexter.py", test, fail_mode, builder, debugger, cflags, ldflags
150 [
151 '"{}"'.format(sys.executable),
152 '"{}"'.format(dexter_path),
153 "test",
154 "--fail-lt 1.0 -w",
155 "--debugger",
156 dexter_regression_test_debugger,
157 ]
158 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100159 tools.append(ToolSubst("%dexter_regression_test_run", dexter_regression_test_run))
OCHyams18ee8982021-12-16 13:41:29 +0000160
Tobias Hietaf98ee402023-05-17 16:59:41 +0200161 # Include build flags for %dexter_regression_test.
162 dexter_regression_test_build = " ".join(
163 [
Tobias Hietaf98ee402023-05-17 16:59:41 +0200164 dexter_regression_test_builder,
Stephen Tozer45a40c12023-09-05 16:04:53 +0100165 dexter_regression_test_flags,
Tobias Hietaf98ee402023-05-17 16:59:41 +0200166 ]
167 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100168 tools.append(ToolSubst("%dexter_regression_test_build", dexter_regression_test_build))
Tobias Hietaf98ee402023-05-17 16:59:41 +0200169 return dependencies
170
OCHyams18ee8982021-12-16 13:41:29 +0000171
OCHyams52bc1c12021-02-24 11:09:18 +0000172def add_host_triple(clang):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200173 return "{} --target={}".format(clang, config.host_triple)
174
OCHyams52bc1c12021-02-24 11:09:18 +0000175
176# The set of arches we can build.
177targets = set(config.targets_to_build)
178# Add aliases to the target set.
Tobias Hietaf98ee402023-05-17 16:59:41 +0200179if "AArch64" in targets:
180 targets.add("arm64")
181if "ARM" in config.targets_to_build:
182 targets.add("thumbv7")
183
OCHyams52bc1c12021-02-24 11:09:18 +0000184
185def can_target_host():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200186 # Check if the targets set contains anything that looks like our host arch.
187 # The arch name in the triple and targets set may be spelled differently
188 # (e.g. x86 vs X86).
189 return any(config.host_triple.lower().startswith(x.lower()) for x in targets)
190
OCHyams52bc1c12021-02-24 11:09:18 +0000191
192# Dexter tests run on the host machine. If the host arch is supported add
193# 'dexter' as an available feature and force the dexter tests to use the host
194# triple.
195if can_target_host():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200196 if config.host_triple != config.target_triple:
197 print("Forcing dexter tests to use host triple {}.".format(config.host_triple))
198 dependencies = configure_dexter_substitutions()
199 if all(d in config.available_features for d in dependencies):
200 config.available_features.add("dexter")
201 llvm_config.with_environment(
202 "PATHTOCLANG", add_host_triple(llvm_config.config.clang)
203 )
204 llvm_config.with_environment(
205 "PATHTOCLANGPP", add_host_triple(llvm_config.use_llvm_tool("clang++"))
206 )
207 llvm_config.with_environment(
208 "PATHTOCLANGCL", add_host_triple(llvm_config.use_llvm_tool("clang-cl"))
209 )
OCHyams52bc1c12021-02-24 11:09:18 +0000210else:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200211 print(
212 "Host triple {} not supported. Skipping dexter tests in the "
213 "debuginfo-tests project.".format(config.host_triple)
214 )
Jeremy Morse984fad22019-10-31 16:51:53 +0000215
Don Hinton3a58f672017-12-12 16:54:20 +0000216tool_dirs = [config.llvm_tools_dir]
217
Don Hinton3a58f672017-12-12 16:54:20 +0000218llvm_config.add_tool_substitutions(tools, tool_dirs)
219
220lit.util.usePlatformSdkOnDarwin(config, lit_config)
Vedant Kumarefab30c2018-08-04 00:02:48 +0000221
Tobias Hietaf98ee402023-05-17 16:59:41 +0200222if platform.system() == "Darwin":
223 xcode_lldb_vers = subprocess.check_output(["xcrun", "lldb", "--version"]).decode(
224 "utf-8"
225 )
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900226 match = re.search(r"lldb-(\d+)", xcode_lldb_vers)
Vedant Kumarefab30c2018-08-04 00:02:48 +0000227 if match:
228 apple_lldb_vers = int(match.group(1))
229 if apple_lldb_vers < 1000:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200230 config.available_features.add("apple-lldb-pre-1000")
231
Jeremy Morse984fad22019-10-31 16:51:53 +0000232
OCHyams5257efd2022-02-09 10:47:07 +0000233def get_gdb_version_string():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200234 """Return gdb's version string, or None if gdb cannot be found or the
235 --version output is formatted unexpectedly.
236 """
237 # See if we can get a gdb version, e.g.
238 # $ gdb --version
239 # GNU gdb (GDB) 10.2
240 # ...More stuff...
241 try:
242 gdb_vers_lines = (
243 subprocess.check_output(["gdb", "--version"]).decode().splitlines()
244 )
245 except:
246 return None # We coudln't find gdb or something went wrong running it.
247 if len(gdb_vers_lines) < 1:
248 print("Unkown GDB version format (too few lines)", file=sys.stderr)
249 return None
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900250 match = re.search(r"GNU gdb \(.*?\) ((\d|\.)+)", gdb_vers_lines[0].strip())
Tobias Hietaf98ee402023-05-17 16:59:41 +0200251 if match is None:
252 print(f"Unkown GDB version format: {gdb_vers_lines[0]}", file=sys.stderr)
253 return None
254 return match.group(1)
255
OCHyams5257efd2022-02-09 10:47:07 +0000256
257def get_clang_default_dwarf_version_string(triple):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200258 """Return the default dwarf version string for clang on this (host) platform
259 or None if we can't work it out.
260 """
261 # Get the flags passed by the driver and look for -dwarf-version.
262 cmd = f'{llvm_config.use_llvm_tool("clang")} -g -xc -c - -v -### --target={triple}'
263 stderr = subprocess.run(cmd.split(), stderr=subprocess.PIPE).stderr.decode()
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900264 match = re.search(r"-dwarf-version=(\d+)", stderr)
Tobias Hietaf98ee402023-05-17 16:59:41 +0200265 if match is None:
266 print("Cannot determine default dwarf version", file=sys.stderr)
267 return None
268 return match.group(1)
269
OCHyams5257efd2022-02-09 10:47:07 +0000270
271# Some cross-project-tests use gdb, but not all versions of gdb are compatible
272# with clang's dwarf. Add feature `gdb-clang-incompatibility` to signal that
273# there's an incompatibility between clang's default dwarf version for this
274# platform and the installed gdb version.
275dwarf_version_string = get_clang_default_dwarf_version_string(config.host_triple)
276gdb_version_string = get_gdb_version_string()
277if dwarf_version_string and gdb_version_string:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200278 if int(dwarf_version_string) >= 5:
dyung93742162024-07-22 11:28:11 -0700279 try:
280 from packaging import version
281 except:
282 lit_config.fatal("Running gdb tests requires the packaging package")
283 if version.parse(gdb_version_string) < version.parse("10.1"):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200284 # Example for llgdb-tests, which use lldb on darwin but gdb elsewhere:
285 # XFAIL: !system-darwin && gdb-clang-incompatibility
286 config.available_features.add("gdb-clang-incompatibility")
287 print(
288 "XFAIL some tests: use gdb version >= 10.1 to restore test coverage",
289 file=sys.stderr,
290 )
OCHyams5257efd2022-02-09 10:47:07 +0000291
Tobias Hietaf98ee402023-05-17 16:59:41 +0200292llvm_config.feature_config([("--build-mode", {"Debug|RelWithDebInfo": "debug-info"})])
Thomas Lively70620942022-03-17 15:22:17 -0700293
294# Allow 'REQUIRES: XXX-registered-target' in tests.
295for arch in config.targets_to_build:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200296 config.available_features.add(arch.lower() + "-registered-target")