blob: b35c643ac898c70b263e5b86f41671826d8fcdbf [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 Tozerb8fc2882025-04-03 15:37:43 +0100134 dexter_regression_test_c_builder = "clang-cl"
135 dexter_regression_test_cxx_builder = "clang-cl"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200136 dexter_regression_test_debugger = "dbgeng"
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100137 dexter_regression_test_c_flags = "/Zi /Od"
138 dexter_regression_test_cxx_flags = "/Zi /Od"
139 dexter_regression_test_additional_flags = ""
Tobias Hietaf98ee402023-05-17 16:59:41 +0200140 else:
141 # Use lldb as the debugger on non-Windows platforms.
142 dependencies = ["clang", "lldb"]
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100143 dexter_regression_test_c_builder = "clang"
144 dexter_regression_test_cxx_builder = "clang++"
Tobias Hietaf98ee402023-05-17 16:59:41 +0200145 dexter_regression_test_debugger = "lldb"
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100146 dexter_regression_test_c_flags = "-O0 -glldb -std=gnu11"
147 dexter_regression_test_cxx_flags = "-O0 -glldb -std=gnu++11"
148 dexter_regression_test_additional_flags = '--lldb-executable "{}"'.format(
149 lldb_path
150 )
OCHyams18ee8982021-12-16 13:41:29 +0000151
Tobias Hietaf98ee402023-05-17 16:59:41 +0200152 tools.append(
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100153 ToolSubst("%dexter_regression_test_c_builder", dexter_regression_test_c_builder)
154 )
155 tools.append(
156 ToolSubst(
157 "%dexter_regression_test_cxx_builder", dexter_regression_test_cxx_builder
158 )
Tobias Hietaf98ee402023-05-17 16:59:41 +0200159 )
160 tools.append(
161 ToolSubst("%dexter_regression_test_debugger", dexter_regression_test_debugger)
162 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100163 # We don't need to distinguish cflags and ldflags because for Dexter
164 # regression tests we use clang to drive the linker, and so all flags will be
165 # passed in a single command.
Tobias Hietaf98ee402023-05-17 16:59:41 +0200166 tools.append(
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100167 ToolSubst("%dexter_regression_test_c_flags", dexter_regression_test_c_flags)
168 )
169 tools.append(
170 ToolSubst("%dexter_regression_test_cxx_flags", dexter_regression_test_cxx_flags)
Tobias Hietaf98ee402023-05-17 16:59:41 +0200171 )
OCHyamsde3f8152022-01-26 11:09:21 +0000172
Tobias Hietaf98ee402023-05-17 16:59:41 +0200173 # Typical command would take the form:
Stephen Tozer45a40c12023-09-05 16:04:53 +0100174 # ./path_to_py/python.exe ./path_to_dex/dexter.py test --fail-lt 1.0 -w --binary %t --debugger lldb --cflags '-O0 -g'
175 dexter_regression_test_run = " ".join(
Tobias Hietaf98ee402023-05-17 16:59:41 +0200176 # "python", "dexter.py", test, fail_mode, builder, debugger, cflags, ldflags
177 [
178 '"{}"'.format(sys.executable),
179 '"{}"'.format(dexter_path),
180 "test",
181 "--fail-lt 1.0 -w",
182 "--debugger",
183 dexter_regression_test_debugger,
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100184 dexter_regression_test_additional_flags,
Tobias Hietaf98ee402023-05-17 16:59:41 +0200185 ]
186 )
Stephen Tozer45a40c12023-09-05 16:04:53 +0100187 tools.append(ToolSubst("%dexter_regression_test_run", dexter_regression_test_run))
OCHyams18ee8982021-12-16 13:41:29 +0000188
Tobias Hietaf98ee402023-05-17 16:59:41 +0200189 # Include build flags for %dexter_regression_test.
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100190 dexter_regression_test_c_build = " ".join(
Tobias Hietaf98ee402023-05-17 16:59:41 +0200191 [
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100192 dexter_regression_test_c_builder,
193 dexter_regression_test_c_flags,
Tobias Hietaf98ee402023-05-17 16:59:41 +0200194 ]
195 )
Stephen Tozerb8fc2882025-04-03 15:37:43 +0100196 dexter_regression_test_cxx_build = " ".join(
197 [
198 dexter_regression_test_cxx_builder,
199 dexter_regression_test_cxx_flags,
200 ]
201 )
202 tools.append(
203 ToolSubst("%dexter_regression_test_c_build", dexter_regression_test_c_build)
204 )
205 tools.append(
206 ToolSubst("%dexter_regression_test_cxx_build", dexter_regression_test_cxx_build)
207 )
Tobias Hietaf98ee402023-05-17 16:59:41 +0200208 return dependencies
209
OCHyams18ee8982021-12-16 13:41:29 +0000210
OCHyams52bc1c12021-02-24 11:09:18 +0000211def add_host_triple(clang):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200212 return "{} --target={}".format(clang, config.host_triple)
213
OCHyams52bc1c12021-02-24 11:09:18 +0000214
215# The set of arches we can build.
216targets = set(config.targets_to_build)
217# Add aliases to the target set.
Tobias Hietaf98ee402023-05-17 16:59:41 +0200218if "AArch64" in targets:
219 targets.add("arm64")
220if "ARM" in config.targets_to_build:
221 targets.add("thumbv7")
222
OCHyams52bc1c12021-02-24 11:09:18 +0000223
224def can_target_host():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200225 # Check if the targets set contains anything that looks like our host arch.
226 # The arch name in the triple and targets set may be spelled differently
227 # (e.g. x86 vs X86).
228 return any(config.host_triple.lower().startswith(x.lower()) for x in targets)
229
OCHyams52bc1c12021-02-24 11:09:18 +0000230
231# Dexter tests run on the host machine. If the host arch is supported add
232# 'dexter' as an available feature and force the dexter tests to use the host
233# triple.
234if can_target_host():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200235 if config.host_triple != config.target_triple:
236 print("Forcing dexter tests to use host triple {}.".format(config.host_triple))
237 dependencies = configure_dexter_substitutions()
238 if all(d in config.available_features for d in dependencies):
239 config.available_features.add("dexter")
240 llvm_config.with_environment(
241 "PATHTOCLANG", add_host_triple(llvm_config.config.clang)
242 )
243 llvm_config.with_environment(
244 "PATHTOCLANGPP", add_host_triple(llvm_config.use_llvm_tool("clang++"))
245 )
246 llvm_config.with_environment(
247 "PATHTOCLANGCL", add_host_triple(llvm_config.use_llvm_tool("clang-cl"))
248 )
OCHyams52bc1c12021-02-24 11:09:18 +0000249else:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200250 print(
251 "Host triple {} not supported. Skipping dexter tests in the "
252 "debuginfo-tests project.".format(config.host_triple)
253 )
Jeremy Morse984fad22019-10-31 16:51:53 +0000254
Don Hinton3a58f672017-12-12 16:54:20 +0000255tool_dirs = [config.llvm_tools_dir]
256
Don Hinton3a58f672017-12-12 16:54:20 +0000257llvm_config.add_tool_substitutions(tools, tool_dirs)
258
259lit.util.usePlatformSdkOnDarwin(config, lit_config)
Vedant Kumarefab30c2018-08-04 00:02:48 +0000260
Tobias Hietaf98ee402023-05-17 16:59:41 +0200261if platform.system() == "Darwin":
262 xcode_lldb_vers = subprocess.check_output(["xcrun", "lldb", "--version"]).decode(
263 "utf-8"
264 )
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900265 match = re.search(r"lldb-(\d+)", xcode_lldb_vers)
Vedant Kumarefab30c2018-08-04 00:02:48 +0000266 if match:
267 apple_lldb_vers = int(match.group(1))
268 if apple_lldb_vers < 1000:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200269 config.available_features.add("apple-lldb-pre-1000")
270
Jeremy Morse984fad22019-10-31 16:51:53 +0000271
OCHyams5257efd2022-02-09 10:47:07 +0000272def get_gdb_version_string():
Tobias Hietaf98ee402023-05-17 16:59:41 +0200273 """Return gdb's version string, or None if gdb cannot be found or the
274 --version output is formatted unexpectedly.
275 """
276 # See if we can get a gdb version, e.g.
277 # $ gdb --version
278 # GNU gdb (GDB) 10.2
279 # ...More stuff...
280 try:
281 gdb_vers_lines = (
282 subprocess.check_output(["gdb", "--version"]).decode().splitlines()
283 )
284 except:
285 return None # We coudln't find gdb or something went wrong running it.
286 if len(gdb_vers_lines) < 1:
287 print("Unkown GDB version format (too few lines)", file=sys.stderr)
288 return None
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900289 match = re.search(r"GNU gdb \(.*?\) ((\d|\.)+)", gdb_vers_lines[0].strip())
Tobias Hietaf98ee402023-05-17 16:59:41 +0200290 if match is None:
291 print(f"Unkown GDB version format: {gdb_vers_lines[0]}", file=sys.stderr)
292 return None
293 return match.group(1)
294
OCHyams5257efd2022-02-09 10:47:07 +0000295
296def get_clang_default_dwarf_version_string(triple):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200297 """Return the default dwarf version string for clang on this (host) platform
298 or None if we can't work it out.
299 """
300 # Get the flags passed by the driver and look for -dwarf-version.
301 cmd = f'{llvm_config.use_llvm_tool("clang")} -g -xc -c - -v -### --target={triple}'
302 stderr = subprocess.run(cmd.split(), stderr=subprocess.PIPE).stderr.decode()
Eisuke Kawashimaa1a3e012025-01-13 21:15:22 +0900303 match = re.search(r"-dwarf-version=(\d+)", stderr)
Tobias Hietaf98ee402023-05-17 16:59:41 +0200304 if match is None:
305 print("Cannot determine default dwarf version", file=sys.stderr)
306 return None
307 return match.group(1)
308
OCHyams5257efd2022-02-09 10:47:07 +0000309
310# Some cross-project-tests use gdb, but not all versions of gdb are compatible
311# with clang's dwarf. Add feature `gdb-clang-incompatibility` to signal that
312# there's an incompatibility between clang's default dwarf version for this
313# platform and the installed gdb version.
314dwarf_version_string = get_clang_default_dwarf_version_string(config.host_triple)
315gdb_version_string = get_gdb_version_string()
316if dwarf_version_string and gdb_version_string:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200317 if int(dwarf_version_string) >= 5:
dyung93742162024-07-22 11:28:11 -0700318 try:
319 from packaging import version
320 except:
321 lit_config.fatal("Running gdb tests requires the packaging package")
322 if version.parse(gdb_version_string) < version.parse("10.1"):
Tobias Hietaf98ee402023-05-17 16:59:41 +0200323 # Example for llgdb-tests, which use lldb on darwin but gdb elsewhere:
324 # XFAIL: !system-darwin && gdb-clang-incompatibility
325 config.available_features.add("gdb-clang-incompatibility")
326 print(
327 "XFAIL some tests: use gdb version >= 10.1 to restore test coverage",
328 file=sys.stderr,
329 )
OCHyams5257efd2022-02-09 10:47:07 +0000330
Tobias Hietaf98ee402023-05-17 16:59:41 +0200331llvm_config.feature_config([("--build-mode", {"Debug|RelWithDebInfo": "debug-info"})])
Thomas Lively70620942022-03-17 15:22:17 -0700332
333# Allow 'REQUIRES: XXX-registered-target' in tests.
334for arch in config.targets_to_build:
Tobias Hietaf98ee402023-05-17 16:59:41 +0200335 config.available_features.add(arch.lower() + "-registered-target")