Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 1 | import os |
| 2 | import platform |
| 3 | import re |
| 4 | import subprocess |
Jeremy Morse | 984fad2 | 2019-10-31 16:51:53 | [diff] [blame] | 5 | import sys |
OCHyams | 5257efd | 2022-02-09 10:47:07 | [diff] [blame] | 6 | from distutils.version import StrictVersion |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 7 | |
| 8 | import lit.formats |
| 9 | import lit.util |
| 10 | |
| 11 | from lit.llvm import llvm_config |
| 12 | from lit.llvm.subst import ToolSubst |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 13 | |
| 14 | # Configuration file for the 'lit' test runner. |
| 15 | |
| 16 | # name: The name of this test suite. |
James Henderson | 24af099 | 2021-02-11 15:41:32 | [diff] [blame] | 17 | config.name = 'cross-project-tests' |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 18 | |
| 19 | # testFormat: The test format to use to interpret tests. |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 20 | config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell) |
| 21 | |
| 22 | # suffixes: A list of file extensions to treat as test files. |
| 23 | config.suffixes = ['.c', '.cpp', '.m'] |
| 24 | |
| 25 | # excludes: A list of directories to exclude from the testsuite. The 'Inputs' |
| 26 | # subdirectories contain auxiliary inputs for various tests in their parent |
| 27 | # directories. |
| 28 | config.excludes = ['Inputs'] |
| 29 | |
| 30 | # test_source_root: The root path where tests are located. |
James Henderson | 24af099 | 2021-02-11 15:41:32 | [diff] [blame] | 31 | config.test_source_root = config.cross_project_tests_src_root |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 32 | |
| 33 | # test_exec_root: The root path where tests should be run. |
James Henderson | 24af099 | 2021-02-11 15:41:32 | [diff] [blame] | 34 | config.test_exec_root = config.cross_project_tests_obj_root |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 35 | |
Jeremy Morse | 984fad2 | 2019-10-31 16:51:53 | [diff] [blame] | 36 | llvm_config.use_default_substitutions() |
| 37 | |
Reid Kleckner | 75d38f1 | 2019-05-28 23:03:33 | [diff] [blame] | 38 | tools = [ |
| 39 | ToolSubst('%test_debuginfo', command=os.path.join( |
James Henderson | 24af099 | 2021-02-11 15:41:32 | [diff] [blame] | 40 | config.cross_project_tests_src_root, 'debuginfo-tests', |
James Henderson | 1364750 | 2021-02-08 15:40:55 | [diff] [blame] | 41 | 'llgdb-tests', 'test_debuginfo.pl')), |
Christian Sigg | 60346bd | 2020-01-11 07:47:41 | [diff] [blame] | 42 | ToolSubst("%llvm_src_root", config.llvm_src_root), |
| 43 | ToolSubst("%llvm_tools_dir", config.llvm_tools_dir), |
Reid Kleckner | 75d38f1 | 2019-05-28 23:03:33 | [diff] [blame] | 44 | ] |
| 45 | |
| 46 | def get_required_attr(config, attr_name): |
| 47 | attr_value = getattr(config, attr_name, None) |
| 48 | if attr_value == None: |
| 49 | lit_config.fatal( |
| 50 | "No attribute %r in test configuration! You may need to run " |
| 51 | "tests from your build directory or add this attribute " |
| 52 | "to lit.site.cfg " % attr_name) |
| 53 | return attr_value |
| 54 | |
| 55 | # If this is an MSVC environment, the tests at the root of the tree are |
| 56 | # unsupported. The local win_cdb test suite, however, is supported. |
| 57 | is_msvc = get_required_attr(config, "is_msvc") |
| 58 | if is_msvc: |
Jeremy Morse | 984fad2 | 2019-10-31 16:51:53 | [diff] [blame] | 59 | config.available_features.add('msvc') |
Reid Kleckner | 75d38f1 | 2019-05-28 23:03:33 | [diff] [blame] | 60 | # FIXME: We should add some llvm lit utility code to find the Windows SDK |
| 61 | # and set up the environment appopriately. |
| 62 | win_sdk = 'C:/Program Files (x86)/Windows Kits/10/' |
| 63 | arch = 'x64' |
Reid Kleckner | 75d38f1 | 2019-05-28 23:03:33 | [diff] [blame] | 64 | llvm_config.with_system_environment(['LIB', 'LIBPATH', 'INCLUDE']) |
| 65 | # Clear _NT_SYMBOL_PATH to prevent cdb from attempting to load symbols from |
| 66 | # the network. |
| 67 | llvm_config.with_environment('_NT_SYMBOL_PATH', '') |
| 68 | tools.append(ToolSubst('%cdb', '"%s"' % os.path.join(win_sdk, 'Debuggers', |
| 69 | arch, 'cdb.exe'))) |
| 70 | |
James Henderson | 4446a72 | 2021-02-09 14:57:03 | [diff] [blame] | 71 | # clang_src_dir and lld_src_dir are not used by these tests, but are required by |
| 72 | # use_clang() and use_lld() respectively, so set them to "", if needed. |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 73 | if not hasattr(config, 'clang_src_dir'): |
| 74 | config.clang_src_dir = "" |
James Henderson | 3827600 | 2021-02-09 15:19:27 | [diff] [blame] | 75 | llvm_config.use_clang(required=('clang' in config.llvm_enabled_projects)) |
| 76 | |
James Henderson | 4446a72 | 2021-02-09 14:57:03 | [diff] [blame] | 77 | if not hasattr(config, 'lld_src_dir'): |
| 78 | config.lld_src_dir = "" |
| 79 | llvm_config.use_lld(required=('lld' in config.llvm_enabled_projects)) |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 80 | |
OCHyams | ac0f329 | 2022-02-09 17:08:43 | [diff] [blame] | 81 | if 'compiler-rt' in config.llvm_enabled_projects: |
| 82 | config.available_features.add('compiler-rt') |
| 83 | |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 84 | if config.llvm_use_sanitizer: |
| 85 | # Propagate path to symbolizer for ASan/MSan. |
| 86 | llvm_config.with_system_environment( |
| 87 | ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH']) |
OCHyams | 52bc1c1 | 2021-02-24 11:09:18 | [diff] [blame] | 88 | |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 89 | # Check which debuggers are available: |
| 90 | lldb_path = llvm_config.use_llvm_tool('lldb', search_env='LLDB') |
| 91 | |
| 92 | if lldb_path is not None: |
| 93 | config.available_features.add('lldb') |
| 94 | |
| 95 | def configure_dexter_substitutions(): |
| 96 | """Configure substitutions for host platform and return list of dependencies |
| 97 | """ |
| 98 | # Produce dexter path, lldb path, and combine into the %dexter substitution |
| 99 | # for running a test. |
| 100 | dexter_path = os.path.join(config.cross_project_tests_src_root, |
| 101 | 'debuginfo-tests', 'dexter', 'dexter.py') |
| 102 | dexter_test_cmd = '"{}" "{}" test'.format(sys.executable, dexter_path) |
| 103 | if lldb_path is not None: |
| 104 | dexter_test_cmd += ' --lldb-executable "{}"'.format(lldb_path) |
| 105 | tools.append(ToolSubst('%dexter', dexter_test_cmd)) |
| 106 | |
| 107 | # For testing other bits of dexter that aren't under the "test" subcommand, |
| 108 | # have a %dexter_base substitution. |
| 109 | dexter_base_cmd = '"{}" "{}"'.format(sys.executable, dexter_path) |
| 110 | tools.append(ToolSubst('%dexter_base', dexter_base_cmd)) |
| 111 | |
| 112 | # Set up commands for DexTer regression tests. |
| 113 | # Builder, debugger, optimisation level and several other flags differ |
| 114 | # depending on whether we're running a unix like or windows os. |
| 115 | if platform.system() == 'Windows': |
| 116 | # The Windows builder script uses lld. |
| 117 | dependencies = ['clang', 'lld-link'] |
OCHyams | de3f815 | 2022-01-26 11:09:21 | [diff] [blame] | 118 | dexter_regression_test_builder = 'clang-cl_vs2015' |
| 119 | dexter_regression_test_debugger = 'dbgeng' |
| 120 | dexter_regression_test_cflags = '/Zi /Od' |
| 121 | dexter_regression_test_ldflags = '/Zi' |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 122 | else: |
| 123 | # Use lldb as the debugger on non-Windows platforms. |
| 124 | dependencies = ['clang', 'lldb'] |
OCHyams | de3f815 | 2022-01-26 11:09:21 | [diff] [blame] | 125 | dexter_regression_test_builder = 'clang' |
| 126 | dexter_regression_test_debugger = 'lldb' |
| 127 | dexter_regression_test_cflags = '-O0 -glldb' |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 128 | dexter_regression_test_ldflags = '' |
| 129 | |
OCHyams | de3f815 | 2022-01-26 11:09:21 | [diff] [blame] | 130 | tools.append(ToolSubst('%dexter_regression_test_builder', dexter_regression_test_builder)) |
| 131 | tools.append(ToolSubst('%dexter_regression_test_debugger', dexter_regression_test_debugger)) |
| 132 | tools.append(ToolSubst('%dexter_regression_test_cflags', dexter_regression_test_cflags)) |
| 133 | tools.append(ToolSubst('%dexter_regression_test_ldflags', dexter_regression_test_cflags)) |
| 134 | |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 135 | # Typical command would take the form: |
| 136 | # ./path_to_py/python.exe ./path_to_dex/dexter.py test --fail-lt 1.0 -w --builder clang --debugger lldb --cflags '-O0 -g' |
| 137 | # Exclude build flags for %dexter_regression_base. |
| 138 | dexter_regression_test_base = ' '.join( |
| 139 | # "python", "dexter.py", test, fail_mode, builder, debugger, cflags, ldflags |
| 140 | ['"{}"'.format(sys.executable), |
| 141 | '"{}"'.format(dexter_path), |
| 142 | 'test', |
| 143 | '--fail-lt 1.0 -w', |
OCHyams | de3f815 | 2022-01-26 11:09:21 | [diff] [blame] | 144 | '--debugger', dexter_regression_test_debugger]) |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 145 | tools.append(ToolSubst('%dexter_regression_base', dexter_regression_test_base)) |
| 146 | |
| 147 | # Include build flags for %dexter_regression_test. |
| 148 | dexter_regression_test_build = ' '.join([ |
| 149 | dexter_regression_test_base, |
OCHyams | de3f815 | 2022-01-26 11:09:21 | [diff] [blame] | 150 | '--builder', dexter_regression_test_builder, |
| 151 | '--cflags "', dexter_regression_test_cflags + '"', |
| 152 | '--ldflags "', dexter_regression_test_ldflags + '"']) |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 153 | tools.append(ToolSubst('%dexter_regression_test', dexter_regression_test_build)) |
| 154 | return dependencies |
| 155 | |
OCHyams | 52bc1c1 | 2021-02-24 11:09:18 | [diff] [blame] | 156 | def add_host_triple(clang): |
| 157 | return '{} --target={}'.format(clang, config.host_triple) |
| 158 | |
| 159 | # The set of arches we can build. |
| 160 | targets = set(config.targets_to_build) |
| 161 | # Add aliases to the target set. |
| 162 | if 'AArch64' in targets: |
| 163 | targets.add('arm64') |
| 164 | if 'ARM' in config.targets_to_build: |
| 165 | targets.add('thumbv7') |
| 166 | |
| 167 | def can_target_host(): |
| 168 | # Check if the targets set contains anything that looks like our host arch. |
| 169 | # The arch name in the triple and targets set may be spelled differently |
| 170 | # (e.g. x86 vs X86). |
| 171 | return any(config.host_triple.lower().startswith(x.lower()) |
| 172 | for x in targets) |
| 173 | |
| 174 | # Dexter tests run on the host machine. If the host arch is supported add |
| 175 | # 'dexter' as an available feature and force the dexter tests to use the host |
| 176 | # triple. |
| 177 | if can_target_host(): |
OCHyams | 52bc1c1 | 2021-02-24 11:09:18 | [diff] [blame] | 178 | if config.host_triple != config.target_triple: |
| 179 | print('Forcing dexter tests to use host triple {}.'.format(config.host_triple)) |
OCHyams | 18ee898 | 2021-12-16 13:41:29 | [diff] [blame] | 180 | dependencies = configure_dexter_substitutions() |
| 181 | if all(d in config.available_features for d in dependencies): |
| 182 | config.available_features.add('dexter') |
| 183 | llvm_config.with_environment('PATHTOCLANG', |
| 184 | add_host_triple(llvm_config.config.clang)) |
| 185 | llvm_config.with_environment('PATHTOCLANGPP', |
| 186 | add_host_triple(llvm_config.use_llvm_tool('clang++'))) |
| 187 | llvm_config.with_environment('PATHTOCLANGCL', |
| 188 | add_host_triple(llvm_config.use_llvm_tool('clang-cl'))) |
OCHyams | 52bc1c1 | 2021-02-24 11:09:18 | [diff] [blame] | 189 | else: |
| 190 | print('Host triple {} not supported. Skipping dexter tests in the ' |
| 191 | 'debuginfo-tests project.'.format(config.host_triple)) |
Jeremy Morse | 984fad2 | 2019-10-31 16:51:53 | [diff] [blame] | 192 | |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 193 | tool_dirs = [config.llvm_tools_dir] |
| 194 | |
Don Hinton | 3a58f67 | 2017-12-12 16:54:20 | [diff] [blame] | 195 | llvm_config.add_tool_substitutions(tools, tool_dirs) |
| 196 | |
| 197 | lit.util.usePlatformSdkOnDarwin(config, lit_config) |
Vedant Kumar | efab30c | 2018-08-04 00:02:48 | [diff] [blame] | 198 | |
| 199 | if platform.system() == 'Darwin': |
Jonas Devlieghere | 635eb80 | 2019-06-25 15:58:32 | [diff] [blame] | 200 | xcode_lldb_vers = subprocess.check_output(['xcrun', 'lldb', '--version']).decode("utf-8") |
Vedant Kumar | efab30c | 2018-08-04 00:02:48 | [diff] [blame] | 201 | match = re.search('lldb-(\d+)', xcode_lldb_vers) |
| 202 | if match: |
| 203 | apple_lldb_vers = int(match.group(1)) |
| 204 | if apple_lldb_vers < 1000: |
| 205 | config.available_features.add('apple-lldb-pre-1000') |
Jeremy Morse | 984fad2 | 2019-10-31 16:51:53 | [diff] [blame] | 206 | |
OCHyams | 5257efd | 2022-02-09 10:47:07 | [diff] [blame] | 207 | def get_gdb_version_string(): |
| 208 | """Return gdb's version string, or None if gdb cannot be found or the |
| 209 | --version output is formatted unexpectedly. |
| 210 | """ |
| 211 | # See if we can get a gdb version, e.g. |
| 212 | # $ gdb --version |
| 213 | # GNU gdb (GDB) 10.2 |
| 214 | # ...More stuff... |
| 215 | try: |
| 216 | gdb_vers_lines = subprocess.check_output(['gdb', '--version']).decode().splitlines() |
| 217 | except: |
| 218 | return None # We coudln't find gdb or something went wrong running it. |
| 219 | if len(gdb_vers_lines) < 1: |
| 220 | print("Unkown GDB version format (too few lines)", file=sys.stderr) |
| 221 | return None |
OCHyams | 00b2a9c | 2022-02-09 11:32:29 | [diff] [blame] | 222 | match = re.search('GNU gdb \(.*?\) ((\d|\.)+)', gdb_vers_lines[0].strip()) |
| 223 | if match is None: |
| 224 | print(f"Unkown GDB version format: {gdb_vers_lines[0]}", file=sys.stderr) |
OCHyams | 5257efd | 2022-02-09 10:47:07 | [diff] [blame] | 225 | return None |
OCHyams | 00b2a9c | 2022-02-09 11:32:29 | [diff] [blame] | 226 | return match.group(1) |
OCHyams | 5257efd | 2022-02-09 10:47:07 | [diff] [blame] | 227 | |
| 228 | def get_clang_default_dwarf_version_string(triple): |
| 229 | """Return the default dwarf version string for clang on this (host) platform |
| 230 | or None if we can't work it out. |
| 231 | """ |
| 232 | # Get the flags passed by the driver and look for -dwarf-version. |
| 233 | cmd = f'{llvm_config.use_llvm_tool("clang")} -g -xc -c - -v -### --target={triple}' |
| 234 | stderr = subprocess.run(cmd.split(), stderr=subprocess.PIPE).stderr.decode() |
| 235 | match = re.search('-dwarf-version=(\d+)', stderr) |
| 236 | if match is None: |
| 237 | print("Cannot determine default dwarf version", file=sys.stderr) |
| 238 | return None |
| 239 | return match.group(1) |
| 240 | |
| 241 | # Some cross-project-tests use gdb, but not all versions of gdb are compatible |
| 242 | # with clang's dwarf. Add feature `gdb-clang-incompatibility` to signal that |
| 243 | # there's an incompatibility between clang's default dwarf version for this |
| 244 | # platform and the installed gdb version. |
| 245 | dwarf_version_string = get_clang_default_dwarf_version_string(config.host_triple) |
| 246 | gdb_version_string = get_gdb_version_string() |
| 247 | if dwarf_version_string and gdb_version_string: |
| 248 | if int(dwarf_version_string) >= 5: |
| 249 | if StrictVersion(gdb_version_string) < StrictVersion('10.1'): |
| 250 | # Example for llgdb-tests, which use lldb on darwin but gdb elsewhere: |
| 251 | # XFAIL: !system-darwin && gdb-clang-incompatibility |
| 252 | config.available_features.add('gdb-clang-incompatibility') |
| 253 | print("XFAIL some tests: use gdb version >= 10.1 to restore test coverage", file=sys.stderr) |
| 254 | |
Christian Sigg | e9b3884 | 2020-09-29 13:19:54 | [diff] [blame] | 255 | llvm_config.feature_config( |
| 256 | [('--build-mode', {'Debug|RelWithDebInfo': 'debug-info'})] |
| 257 | ) |