blob: ccd3d01023c9a19a97dfddad03deaedda40e168e [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
Augusto Noronha7cabcdb2025-03-19 12:42:47 -070037lldb_python_path = os.path.join(
38 config.llvm_libs_dir,
39 f"python{sys.version_info.major}.{sys.version_info.minor}",
40 "site-packages",
41)
42python_exec_path = sys.executable
Reid Kleckner75d38f12019-05-28 23:03:33 +000043tools = [
Tobias Hietaf98ee402023-05-17 16:59:41 +020044 ToolSubst(
45 "%test_debuginfo",
Augusto Noronha7cabcdb2025-03-19 12:42:47 -070046 command="PYTHON_EXEC_PATH="
47 + python_exec_path
48 + " LLDB_PYTHON_PATH="
49 + lldb_python_path
50 + " "
51 + os.path.join(
Tobias Hietaf98ee402023-05-17 16:59:41 +020052 config.cross_project_tests_src_root,
53 "debuginfo-tests",
54 "llgdb-tests",
55 "test_debuginfo.pl",
56 ),
57 ),
Christian Sigg60346bd2020-01-11 08:47:41 +010058 ToolSubst("%llvm_src_root", config.llvm_src_root),
59 ToolSubst("%llvm_tools_dir", config.llvm_tools_dir),
Reid Kleckner75d38f12019-05-28 23:03:33 +000060]
61
Tobias Hietaf98ee402023-05-17 16:59:41 +020062
Reid Kleckner75d38f12019-05-28 23:03:33 +000063def get_required_attr(config, attr_name):
Tobias Hietaf98ee402023-05-17 16:59:41 +020064 attr_value = getattr(config, attr_name, None)
Eisuke Kawashimaca92bdf2025-01-13 21:03:04 +090065 if attr_value is None:
Tobias Hietaf98ee402023-05-17 16:59:41 +020066 lit_config.fatal(
67 "No attribute %r in test configuration! You may need to run "
68 "tests from your build directory or add this attribute "
69 "to lit.site.cfg " % attr_name
70 )
71 return attr_value
72
Reid Kleckner75d38f12019-05-28 23:03:33 +000073
74# If this is an MSVC environment, the tests at the root of the tree are
75# unsupported. The local win_cdb test suite, however, is supported.
76is_msvc = get_required_attr(config, "is_msvc")
77if is_msvc:
Tobias Hietaf98ee402023-05-17 16:59:41 +020078 config.available_features.add("msvc")
Reid Kleckner75d38f12019-05-28 23:03:33 +000079 # FIXME: We should add some llvm lit utility code to find the Windows SDK
80 # and set up the environment appopriately.
Tobias Hietaf98ee402023-05-17 16:59:41 +020081 win_sdk = "C:/Program Files (x86)/Windows Kits/10/"
82 arch = "x64"
83 llvm_config.with_system_environment(["LIB", "LIBPATH", "INCLUDE"])
Reid Kleckner75d38f12019-05-28 23:03:33 +000084 # Clear _NT_SYMBOL_PATH to prevent cdb from attempting to load symbols from
85 # the network.
Tobias Hietaf98ee402023-05-17 16:59:41 +020086 llvm_config.with_environment("_NT_SYMBOL_PATH", "")
87 tools.append(
88 ToolSubst("%cdb", '"%s"' % os.path.join(win_sdk, "Debuggers", arch, "cdb.exe"))
89 )
Reid Kleckner75d38f12019-05-28 23:03:33 +000090
James Henderson4446a722021-02-09 14:57:03 +000091# clang_src_dir and lld_src_dir are not used by these tests, but are required by
92# use_clang() and use_lld() respectively, so set them to "", if needed.
Tobias Hietaf98ee402023-05-17 16:59:41 +020093if not hasattr(config, "clang_src_dir"):
Don Hinton3a58f672017-12-12 16:54:20 +000094 config.clang_src_dir = ""
Amir Ayupov9fec33a2024-01-18 19:59:09 -080095llvm_config.use_clang(required=("clang" in config.llvm_enabled_projects))
James Henderson38276002021-02-09 15:19:27 +000096
Tobias Hietaf98ee402023-05-17 16:59:41 +020097if not hasattr(config, "lld_src_dir"):
James Henderson4446a722021-02-09 14:57:03 +000098 config.lld_src_dir = ""
Tobias Hietaf98ee402023-05-17 16:59:41 +020099llvm_config.use_lld(required=("lld" in config.llvm_enabled_projects))
Don Hinton3a58f672017-12-12 16:54:20 +0000100
Tobias Hietaf98ee402023-05-17 16:59:41 +0200101if "compiler-rt" in config.llvm_enabled_projects:
102 config.available_features.add("compiler-rt")
OCHyamsac0f3292022-02-09 17:08:43 +0000103
OCHyams18ee8982021-12-16 13:41:29 +0000104# Check which debuggers are available:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200105lldb_path = llvm_config.use_llvm_tool("lldb", search_env="LLDB")
OCHyams18ee8982021-12-16 13:41:29 +0000106
107if lldb_path is not None:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200108 config.available_features.add("lldb")
109
OCHyams18ee8982021-12-16 13:41:29 +0000110
111def configure_dexter_substitutions():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200112 """Configure substitutions for host platform and return list of dependencies"""
113 # Produce dexter path, lldb path, and combine into the %dexter substitution
114 # for running a test.
115 dexter_path = os.path.join(
116 config.cross_project_tests_src_root, "debuginfo-tests", "dexter", "dexter.py"
117 )
118 dexter_test_cmd = '"{}" "{}" test'.format(sys.executable, dexter_path)
119 if lldb_path is not None:
120 dexter_test_cmd += ' --lldb-executable "{}"'.format(lldb_path)
121 tools.append(ToolSubst("%dexter", dexter_test_cmd))
OCHyams18ee8982021-12-16 13:41:29 +0000122
Tobias Hietaf98ee402023-05-17 16:59:41 +0200123 # For testing other bits of dexter that aren't under the "test" subcommand,
124 # have a %dexter_base substitution.
125 dexter_base_cmd = '"{}" "{}"'.format(sys.executable, dexter_path)
126 tools.append(ToolSubst("%dexter_base", dexter_base_cmd))
OCHyams18ee8982021-12-16 13:41:29 +0000127
Tobias Hietaf98ee402023-05-17 16:59:41 +0200128 # Set up commands for DexTer regression tests.
129 # Builder, debugger, optimisation level and several other flags differ
130 # depending on whether we're running a unix like or windows os.
131 if platform.system() == "Windows":
132 # The Windows builder script uses lld.
133 dependencies = ["clang", "lld-link"]
Stephen Tozer45a40c12023-09-05 16:04:53 +0100134 dexter_regression_test_builder = "clang-cl"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200135 dexter_regression_test_debugger = "dbgeng"
Stephen Tozer45a40c12023-09-05 16:04:53 +0100136 dexter_regression_test_flags = "/Zi /Od"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200137 else:
138 # Use lldb as the debugger on non-Windows platforms.
139 dependencies = ["clang", "lldb"]
Stephen Tozer45a40c12023-09-05 16:04:53 +0100140 dexter_regression_test_builder = "clang++"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200141 dexter_regression_test_debugger = "lldb"
Stephen Tozer45a40c12023-09-05 16:04:53 +0100142 dexter_regression_test_flags = "-O0 -glldb -std=gnu++11"
OCHyams18ee8982021-12-16 13:41:29 +0000143
Tobias Hietaf98ee402023-05-17 16:59:41 +0200144 tools.append(
145 ToolSubst("%dexter_regression_test_builder", dexter_regression_test_builder)
146 )
147 tools.append(
148 ToolSubst("%dexter_regression_test_debugger", dexter_regression_test_debugger)
149 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100150 # We don't need to distinguish cflags and ldflags because for Dexter
151 # regression tests we use clang to drive the linker, and so all flags will be
152 # passed in a single command.
Tobias Hietaf98ee402023-05-17 16:59:41 +0200153 tools.append(
Stephen Tozer45a40c12023-09-05 16:04:53 +0100154 ToolSubst("%dexter_regression_test_flags", dexter_regression_test_flags)
Tobias Hietaf98ee402023-05-17 16:59:41 +0200155 )
OCHyamsde3f8152022-01-26 11:09:21 +0000156
Tobias Hietaf98ee402023-05-17 16:59:41 +0200157 # Typical command would take the form:
Stephen Tozer45a40c12023-09-05 16:04:53 +0100158 # ./path_to_py/python.exe ./path_to_dex/dexter.py test --fail-lt 1.0 -w --binary %t --debugger lldb --cflags '-O0 -g'
159 dexter_regression_test_run = " ".join(
Tobias Hietaf98ee402023-05-17 16:59:41 +0200160 # "python", "dexter.py", test, fail_mode, builder, debugger, cflags, ldflags
161 [
162 '"{}"'.format(sys.executable),
163 '"{}"'.format(dexter_path),
164 "test",
165 "--fail-lt 1.0 -w",
166 "--debugger",
167 dexter_regression_test_debugger,
168 ]
169 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100170 tools.append(ToolSubst("%dexter_regression_test_run", dexter_regression_test_run))
OCHyams18ee8982021-12-16 13:41:29 +0000171
Tobias Hietaf98ee402023-05-17 16:59:41 +0200172 # Include build flags for %dexter_regression_test.
173 dexter_regression_test_build = " ".join(
174 [
Tobias Hietaf98ee402023-05-17 16:59:41 +0200175 dexter_regression_test_builder,
Stephen Tozer45a40c12023-09-05 16:04:53 +0100176 dexter_regression_test_flags,
Tobias Hietaf98ee402023-05-17 16:59:41 +0200177 ]
178 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100179 tools.append(ToolSubst("%dexter_regression_test_build", dexter_regression_test_build))
Tobias Hietaf98ee402023-05-17 16:59:41 +0200180 return dependencies
181
OCHyams18ee8982021-12-16 13:41:29 +0000182
OCHyams52bc1c12021-02-24 11:09:18 +0000183def add_host_triple(clang):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200184 return "{} --target={}".format(clang, config.host_triple)
185
OCHyams52bc1c12021-02-24 11:09:18 +0000186
187# The set of arches we can build.
188targets = set(config.targets_to_build)
189# Add aliases to the target set.
Tobias Hietaf98ee402023-05-17 16:59:41 +0200190if "AArch64" in targets:
191 targets.add("arm64")
192if "ARM" in config.targets_to_build:
193 targets.add("thumbv7")
194
OCHyams52bc1c12021-02-24 11:09:18 +0000195
196def can_target_host():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200197 # Check if the targets set contains anything that looks like our host arch.
198 # The arch name in the triple and targets set may be spelled differently
199 # (e.g. x86 vs X86).
200 return any(config.host_triple.lower().startswith(x.lower()) for x in targets)
201
OCHyams52bc1c12021-02-24 11:09:18 +0000202
203# Dexter tests run on the host machine. If the host arch is supported add
204# 'dexter' as an available feature and force the dexter tests to use the host
205# triple.
206if can_target_host():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200207 if config.host_triple != config.target_triple:
208 print("Forcing dexter tests to use host triple {}.".format(config.host_triple))
209 dependencies = configure_dexter_substitutions()
210 if all(d in config.available_features for d in dependencies):
211 config.available_features.add("dexter")
212 llvm_config.with_environment(
213 "PATHTOCLANG", add_host_triple(llvm_config.config.clang)
214 )
215 llvm_config.with_environment(
216 "PATHTOCLANGPP", add_host_triple(llvm_config.use_llvm_tool("clang++"))
217 )
218 llvm_config.with_environment(
219 "PATHTOCLANGCL", add_host_triple(llvm_config.use_llvm_tool("clang-cl"))
220 )
OCHyams52bc1c12021-02-24 11:09:18 +0000221else:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200222 print(
223 "Host triple {} not supported. Skipping dexter tests in the "
224 "debuginfo-tests project.".format(config.host_triple)
225 )
Jeremy Morse984fad22019-10-31 16:51:53 +0000226
Don Hinton3a58f672017-12-12 16:54:20 +0000227tool_dirs = [config.llvm_tools_dir]
228
Don Hinton3a58f672017-12-12 16:54:20 +0000229llvm_config.add_tool_substitutions(tools, tool_dirs)
230
231lit.util.usePlatformSdkOnDarwin(config, lit_config)
Vedant Kumarefab30c2018-08-04 00:02:48 +0000232
Tobias Hietaf98ee402023-05-17 16:59:41 +0200233if platform.system() == "Darwin":
234 xcode_lldb_vers = subprocess.check_output(["xcrun", "lldb", "--version"]).decode(
235 "utf-8"
236 )
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900237 match = re.search(r"lldb-(\d+)", xcode_lldb_vers)
Vedant Kumarefab30c2018-08-04 00:02:48 +0000238 if match:
239 apple_lldb_vers = int(match.group(1))
240 if apple_lldb_vers < 1000:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200241 config.available_features.add("apple-lldb-pre-1000")
242
Jeremy Morse984fad22019-10-31 16:51:53 +0000243
OCHyams5257efd2022-02-09 10:47:07 +0000244def get_gdb_version_string():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200245 """Return gdb's version string, or None if gdb cannot be found or the
246 --version output is formatted unexpectedly.
247 """
248 # See if we can get a gdb version, e.g.
249 # $ gdb --version
250 # GNU gdb (GDB) 10.2
251 # ...More stuff...
252 try:
253 gdb_vers_lines = (
254 subprocess.check_output(["gdb", "--version"]).decode().splitlines()
255 )
256 except:
257 return None # We coudln't find gdb or something went wrong running it.
258 if len(gdb_vers_lines) < 1:
259 print("Unkown GDB version format (too few lines)", file=sys.stderr)
260 return None
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900261 match = re.search(r"GNU gdb \(.*?\) ((\d|\.)+)", gdb_vers_lines[0].strip())
Tobias Hietaf98ee402023-05-17 16:59:41 +0200262 if match is None:
263 print(f"Unkown GDB version format: {gdb_vers_lines[0]}", file=sys.stderr)
264 return None
265 return match.group(1)
266
OCHyams5257efd2022-02-09 10:47:07 +0000267
268def get_clang_default_dwarf_version_string(triple):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200269 """Return the default dwarf version string for clang on this (host) platform
270 or None if we can't work it out.
271 """
272 # Get the flags passed by the driver and look for -dwarf-version.
273 cmd = f'{llvm_config.use_llvm_tool("clang")} -g -xc -c - -v -### --target={triple}'
274 stderr = subprocess.run(cmd.split(), stderr=subprocess.PIPE).stderr.decode()
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900275 match = re.search(r"-dwarf-version=(\d+)", stderr)
Tobias Hietaf98ee402023-05-17 16:59:41 +0200276 if match is None:
277 print("Cannot determine default dwarf version", file=sys.stderr)
278 return None
279 return match.group(1)
280
OCHyams5257efd2022-02-09 10:47:07 +0000281
282# Some cross-project-tests use gdb, but not all versions of gdb are compatible
283# with clang's dwarf. Add feature `gdb-clang-incompatibility` to signal that
284# there's an incompatibility between clang's default dwarf version for this
285# platform and the installed gdb version.
286dwarf_version_string = get_clang_default_dwarf_version_string(config.host_triple)
287gdb_version_string = get_gdb_version_string()
288if dwarf_version_string and gdb_version_string:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200289 if int(dwarf_version_string) >= 5:
dyung93742162024-07-22 11:28:11 -0700290 try:
291 from packaging import version
292 except:
293 lit_config.fatal("Running gdb tests requires the packaging package")
294 if version.parse(gdb_version_string) < version.parse("10.1"):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200295 # Example for llgdb-tests, which use lldb on darwin but gdb elsewhere:
296 # XFAIL: !system-darwin && gdb-clang-incompatibility
297 config.available_features.add("gdb-clang-incompatibility")
298 print(
299 "XFAIL some tests: use gdb version >= 10.1 to restore test coverage",
300 file=sys.stderr,
301 )
OCHyams5257efd2022-02-09 10:47:07 +0000302
Tobias Hietaf98ee402023-05-17 16:59:41 +0200303llvm_config.feature_config([("--build-mode", {"Debug|RelWithDebInfo": "debug-info"})])
Thomas Lively70620942022-03-17 15:22:17 -0700304
305# Allow 'REQUIRES: XXX-registered-target' in tests.
306for arch in config.targets_to_build:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200307 config.available_features.add(arch.lower() + "-registered-target")