11364750dSJames Hendersonimport os 21364750dSJames Hendersonimport platform 31364750dSJames Hendersonimport re 41364750dSJames Hendersonimport subprocess 51364750dSJames Hendersonimport sys 67062094bSThomas Lively 71364750dSJames Hendersonimport lit.formats 81364750dSJames Hendersonimport lit.util 91364750dSJames Henderson 101364750dSJames Hendersonfrom lit.llvm import llvm_config 111364750dSJames Hendersonfrom lit.llvm.subst import ToolSubst 121364750dSJames Henderson 131364750dSJames Henderson# Configuration file for the 'lit' test runner. 141364750dSJames Henderson 151364750dSJames Henderson# name: The name of this test suite. 16f98ee40fSTobias Hietaconfig.name = "cross-project-tests" 171364750dSJames Henderson 181364750dSJames Henderson# testFormat: The test format to use to interpret tests. 191364750dSJames Hendersonconfig.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell) 201364750dSJames Henderson 211364750dSJames Henderson# suffixes: A list of file extensions to treat as test files. 22f98ee40fSTobias Hietaconfig.suffixes = [".c", ".cl", ".cpp", ".m"] 231364750dSJames Henderson 241364750dSJames Henderson# excludes: A list of directories to exclude from the testsuite. The 'Inputs' 251364750dSJames Henderson# subdirectories contain auxiliary inputs for various tests in their parent 261364750dSJames Henderson# directories. 27f98ee40fSTobias Hietaconfig.excludes = ["Inputs"] 281364750dSJames Henderson 291364750dSJames Henderson# test_source_root: The root path where tests are located. 3024af0990SJames Hendersonconfig.test_source_root = config.cross_project_tests_src_root 311364750dSJames Henderson 321364750dSJames Henderson# test_exec_root: The root path where tests should be run. 3324af0990SJames Hendersonconfig.test_exec_root = config.cross_project_tests_obj_root 341364750dSJames Henderson 351364750dSJames Hendersonllvm_config.use_default_substitutions() 361364750dSJames Henderson 371364750dSJames Hendersontools = [ 38f98ee40fSTobias Hieta ToolSubst( 39f98ee40fSTobias Hieta "%test_debuginfo", 40f98ee40fSTobias Hieta command=os.path.join( 41f98ee40fSTobias Hieta config.cross_project_tests_src_root, 42f98ee40fSTobias Hieta "debuginfo-tests", 43f98ee40fSTobias Hieta "llgdb-tests", 44f98ee40fSTobias Hieta "test_debuginfo.pl", 45f98ee40fSTobias Hieta ), 46f98ee40fSTobias Hieta ), 471364750dSJames Henderson ToolSubst("%llvm_src_root", config.llvm_src_root), 481364750dSJames Henderson ToolSubst("%llvm_tools_dir", config.llvm_tools_dir), 491364750dSJames Henderson] 501364750dSJames Henderson 51f98ee40fSTobias Hieta 521364750dSJames Hendersondef get_required_attr(config, attr_name): 531364750dSJames Henderson attr_value = getattr(config, attr_name, None) 54ca92bdfaSEisuke Kawashima if attr_value is None: 551364750dSJames Henderson lit_config.fatal( 561364750dSJames Henderson "No attribute %r in test configuration! You may need to run " 571364750dSJames Henderson "tests from your build directory or add this attribute " 58f98ee40fSTobias Hieta "to lit.site.cfg " % attr_name 59f98ee40fSTobias Hieta ) 601364750dSJames Henderson return attr_value 611364750dSJames Henderson 62f98ee40fSTobias Hieta 631364750dSJames Henderson# If this is an MSVC environment, the tests at the root of the tree are 641364750dSJames Henderson# unsupported. The local win_cdb test suite, however, is supported. 651364750dSJames Hendersonis_msvc = get_required_attr(config, "is_msvc") 661364750dSJames Hendersonif is_msvc: 67f98ee40fSTobias Hieta config.available_features.add("msvc") 681364750dSJames Henderson # FIXME: We should add some llvm lit utility code to find the Windows SDK 691364750dSJames Henderson # and set up the environment appopriately. 70f98ee40fSTobias Hieta win_sdk = "C:/Program Files (x86)/Windows Kits/10/" 71f98ee40fSTobias Hieta arch = "x64" 72f98ee40fSTobias Hieta llvm_config.with_system_environment(["LIB", "LIBPATH", "INCLUDE"]) 731364750dSJames Henderson # Clear _NT_SYMBOL_PATH to prevent cdb from attempting to load symbols from 741364750dSJames Henderson # the network. 75f98ee40fSTobias Hieta llvm_config.with_environment("_NT_SYMBOL_PATH", "") 76f98ee40fSTobias Hieta tools.append( 77f98ee40fSTobias Hieta ToolSubst("%cdb", '"%s"' % os.path.join(win_sdk, "Debuggers", arch, "cdb.exe")) 78f98ee40fSTobias Hieta ) 791364750dSJames Henderson 804446a72aSJames Henderson# clang_src_dir and lld_src_dir are not used by these tests, but are required by 814446a72aSJames Henderson# use_clang() and use_lld() respectively, so set them to "", if needed. 82f98ee40fSTobias Hietaif not hasattr(config, "clang_src_dir"): 831364750dSJames Henderson config.clang_src_dir = "" 84f98ee40fSTobias Hietallvm_config.use_clang(required=("clang" in config.llvm_enabled_projects)) 853827600fSJames Henderson 86f98ee40fSTobias Hietaif not hasattr(config, "lld_src_dir"): 874446a72aSJames Henderson config.lld_src_dir = "" 88f98ee40fSTobias Hietallvm_config.use_lld(required=("lld" in config.llvm_enabled_projects)) 891364750dSJames Henderson 90f98ee40fSTobias Hietaif "compiler-rt" in config.llvm_enabled_projects: 91f98ee40fSTobias Hieta config.available_features.add("compiler-rt") 92ac0f3297SOCHyams 931364750dSJames Henderson# Check which debuggers are available: 94f98ee40fSTobias Hietalldb_path = llvm_config.use_llvm_tool("lldb", search_env="LLDB") 951364750dSJames Henderson 961364750dSJames Hendersonif lldb_path is not None: 97f98ee40fSTobias Hieta config.available_features.add("lldb") 98f98ee40fSTobias Hieta 991364750dSJames Henderson 10018ee898cSOCHyamsdef configure_dexter_substitutions(): 101f98ee40fSTobias Hieta """Configure substitutions for host platform and return list of dependencies""" 1021364750dSJames Henderson # Produce dexter path, lldb path, and combine into the %dexter substitution 1031364750dSJames Henderson # for running a test. 104f98ee40fSTobias Hieta dexter_path = os.path.join( 105f98ee40fSTobias Hieta config.cross_project_tests_src_root, "debuginfo-tests", "dexter", "dexter.py" 106f98ee40fSTobias Hieta ) 1071364750dSJames Henderson dexter_test_cmd = '"{}" "{}" test'.format(sys.executable, dexter_path) 1081364750dSJames Henderson if lldb_path is not None: 1091364750dSJames Henderson dexter_test_cmd += ' --lldb-executable "{}"'.format(lldb_path) 110f98ee40fSTobias Hieta tools.append(ToolSubst("%dexter", dexter_test_cmd)) 1111364750dSJames Henderson 1121364750dSJames Henderson # For testing other bits of dexter that aren't under the "test" subcommand, 1131364750dSJames Henderson # have a %dexter_base substitution. 1141364750dSJames Henderson dexter_base_cmd = '"{}" "{}"'.format(sys.executable, dexter_path) 115f98ee40fSTobias Hieta tools.append(ToolSubst("%dexter_base", dexter_base_cmd)) 1161364750dSJames Henderson 1171364750dSJames Henderson # Set up commands for DexTer regression tests. 1181364750dSJames Henderson # Builder, debugger, optimisation level and several other flags differ 1191364750dSJames Henderson # depending on whether we're running a unix like or windows os. 120f98ee40fSTobias Hieta if platform.system() == "Windows": 12118ee898cSOCHyams # The Windows builder script uses lld. 122f98ee40fSTobias Hieta dependencies = ["clang", "lld-link"] 12345a40c16SStephen Tozer dexter_regression_test_builder = "clang-cl" 124f98ee40fSTobias Hieta dexter_regression_test_debugger = "dbgeng" 12545a40c16SStephen Tozer dexter_regression_test_flags = "/Zi /Od" 1261364750dSJames Henderson else: 12718ee898cSOCHyams # Use lldb as the debugger on non-Windows platforms. 128f98ee40fSTobias Hieta dependencies = ["clang", "lldb"] 12945a40c16SStephen Tozer dexter_regression_test_builder = "clang++" 130f98ee40fSTobias Hieta dexter_regression_test_debugger = "lldb" 13145a40c16SStephen Tozer dexter_regression_test_flags = "-O0 -glldb -std=gnu++11" 1321364750dSJames Henderson 133f98ee40fSTobias Hieta tools.append( 134f98ee40fSTobias Hieta ToolSubst("%dexter_regression_test_builder", dexter_regression_test_builder) 135f98ee40fSTobias Hieta ) 136f98ee40fSTobias Hieta tools.append( 137f98ee40fSTobias Hieta ToolSubst("%dexter_regression_test_debugger", dexter_regression_test_debugger) 138f98ee40fSTobias Hieta ) 13945a40c16SStephen Tozer # We don't need to distinguish cflags and ldflags because for Dexter 14045a40c16SStephen Tozer # regression tests we use clang to drive the linker, and so all flags will be 14145a40c16SStephen Tozer # passed in a single command. 142f98ee40fSTobias Hieta tools.append( 14345a40c16SStephen Tozer ToolSubst("%dexter_regression_test_flags", dexter_regression_test_flags) 144f98ee40fSTobias Hieta ) 145de3f8155SOCHyams 1461364750dSJames Henderson # Typical command would take the form: 14745a40c16SStephen Tozer # ./path_to_py/python.exe ./path_to_dex/dexter.py test --fail-lt 1.0 -w --binary %t --debugger lldb --cflags '-O0 -g' 14845a40c16SStephen Tozer dexter_regression_test_run = " ".join( 1491364750dSJames Henderson # "python", "dexter.py", test, fail_mode, builder, debugger, cflags, ldflags 150f98ee40fSTobias Hieta [ 151f98ee40fSTobias Hieta '"{}"'.format(sys.executable), 1521364750dSJames Henderson '"{}"'.format(dexter_path), 153f98ee40fSTobias Hieta "test", 154f98ee40fSTobias Hieta "--fail-lt 1.0 -w", 155f98ee40fSTobias Hieta "--debugger", 156f98ee40fSTobias Hieta dexter_regression_test_debugger, 157f98ee40fSTobias Hieta ] 158f98ee40fSTobias Hieta ) 15945a40c16SStephen Tozer tools.append(ToolSubst("%dexter_regression_test_run", dexter_regression_test_run)) 1606b1599d7SOCHyams 1616b1599d7SOCHyams # Include build flags for %dexter_regression_test. 162f98ee40fSTobias Hieta dexter_regression_test_build = " ".join( 163f98ee40fSTobias Hieta [ 164f98ee40fSTobias Hieta dexter_regression_test_builder, 16545a40c16SStephen Tozer dexter_regression_test_flags, 166f98ee40fSTobias Hieta ] 167f98ee40fSTobias Hieta ) 16845a40c16SStephen Tozer tools.append(ToolSubst("%dexter_regression_test_build", dexter_regression_test_build)) 16918ee898cSOCHyams return dependencies 17018ee898cSOCHyams 171f98ee40fSTobias Hieta 17218ee898cSOCHyamsdef add_host_triple(clang): 173f98ee40fSTobias Hieta return "{} --target={}".format(clang, config.host_triple) 174f98ee40fSTobias Hieta 17518ee898cSOCHyams 17618ee898cSOCHyams# The set of arches we can build. 17718ee898cSOCHyamstargets = set(config.targets_to_build) 17818ee898cSOCHyams# Add aliases to the target set. 179f98ee40fSTobias Hietaif "AArch64" in targets: 180f98ee40fSTobias Hieta targets.add("arm64") 181f98ee40fSTobias Hietaif "ARM" in config.targets_to_build: 182f98ee40fSTobias Hieta targets.add("thumbv7") 183f98ee40fSTobias Hieta 18418ee898cSOCHyams 18518ee898cSOCHyamsdef can_target_host(): 18618ee898cSOCHyams # Check if the targets set contains anything that looks like our host arch. 18718ee898cSOCHyams # The arch name in the triple and targets set may be spelled differently 18818ee898cSOCHyams # (e.g. x86 vs X86). 189f98ee40fSTobias Hieta return any(config.host_triple.lower().startswith(x.lower()) for x in targets) 190f98ee40fSTobias Hieta 19118ee898cSOCHyams 19218ee898cSOCHyams# Dexter tests run on the host machine. If the host arch is supported add 19318ee898cSOCHyams# 'dexter' as an available feature and force the dexter tests to use the host 19418ee898cSOCHyams# triple. 19518ee898cSOCHyamsif can_target_host(): 19618ee898cSOCHyams if config.host_triple != config.target_triple: 197f98ee40fSTobias Hieta print("Forcing dexter tests to use host triple {}.".format(config.host_triple)) 19818ee898cSOCHyams dependencies = configure_dexter_substitutions() 19918ee898cSOCHyams if all(d in config.available_features for d in dependencies): 200f98ee40fSTobias Hieta config.available_features.add("dexter") 201f98ee40fSTobias Hieta llvm_config.with_environment( 202f98ee40fSTobias Hieta "PATHTOCLANG", add_host_triple(llvm_config.config.clang) 203f98ee40fSTobias Hieta ) 204f98ee40fSTobias Hieta llvm_config.with_environment( 205f98ee40fSTobias Hieta "PATHTOCLANGPP", add_host_triple(llvm_config.use_llvm_tool("clang++")) 206f98ee40fSTobias Hieta ) 207f98ee40fSTobias Hieta llvm_config.with_environment( 208f98ee40fSTobias Hieta "PATHTOCLANGCL", add_host_triple(llvm_config.use_llvm_tool("clang-cl")) 209f98ee40fSTobias Hieta ) 21018ee898cSOCHyamselse: 211f98ee40fSTobias Hieta print( 212f98ee40fSTobias Hieta "Host triple {} not supported. Skipping dexter tests in the " 213f98ee40fSTobias Hieta "debuginfo-tests project.".format(config.host_triple) 214f98ee40fSTobias Hieta ) 2151364750dSJames Henderson 2161364750dSJames Hendersontool_dirs = [config.llvm_tools_dir] 2171364750dSJames Henderson 2181364750dSJames Hendersonllvm_config.add_tool_substitutions(tools, tool_dirs) 2191364750dSJames Henderson 2201364750dSJames Hendersonlit.util.usePlatformSdkOnDarwin(config, lit_config) 2211364750dSJames Henderson 222f98ee40fSTobias Hietaif platform.system() == "Darwin": 223f98ee40fSTobias Hieta xcode_lldb_vers = subprocess.check_output(["xcrun", "lldb", "--version"]).decode( 224f98ee40fSTobias Hieta "utf-8" 225f98ee40fSTobias Hieta ) 226*a1a3e019SEisuke Kawashima match = re.search(r"lldb-(\d+)", xcode_lldb_vers) 2271364750dSJames Henderson if match: 2281364750dSJames Henderson apple_lldb_vers = int(match.group(1)) 2291364750dSJames Henderson if apple_lldb_vers < 1000: 230f98ee40fSTobias Hieta config.available_features.add("apple-lldb-pre-1000") 231f98ee40fSTobias Hieta 2321364750dSJames Henderson 2335257efdcSOCHyamsdef get_gdb_version_string(): 2345257efdcSOCHyams """Return gdb's version string, or None if gdb cannot be found or the 2355257efdcSOCHyams --version output is formatted unexpectedly. 2365257efdcSOCHyams """ 2375257efdcSOCHyams # See if we can get a gdb version, e.g. 2385257efdcSOCHyams # $ gdb --version 2395257efdcSOCHyams # GNU gdb (GDB) 10.2 2405257efdcSOCHyams # ...More stuff... 2415257efdcSOCHyams try: 242f98ee40fSTobias Hieta gdb_vers_lines = ( 243f98ee40fSTobias Hieta subprocess.check_output(["gdb", "--version"]).decode().splitlines() 244f98ee40fSTobias Hieta ) 2455257efdcSOCHyams except: 2465257efdcSOCHyams return None # We coudln't find gdb or something went wrong running it. 2475257efdcSOCHyams if len(gdb_vers_lines) < 1: 2485257efdcSOCHyams print("Unkown GDB version format (too few lines)", file=sys.stderr) 2495257efdcSOCHyams return None 250*a1a3e019SEisuke Kawashima match = re.search(r"GNU gdb \(.*?\) ((\d|\.)+)", gdb_vers_lines[0].strip()) 25100b2a9c9SOCHyams if match is None: 25200b2a9c9SOCHyams print(f"Unkown GDB version format: {gdb_vers_lines[0]}", file=sys.stderr) 2535257efdcSOCHyams return None 25400b2a9c9SOCHyams return match.group(1) 2555257efdcSOCHyams 256f98ee40fSTobias Hieta 2575257efdcSOCHyamsdef get_clang_default_dwarf_version_string(triple): 2585257efdcSOCHyams """Return the default dwarf version string for clang on this (host) platform 2595257efdcSOCHyams or None if we can't work it out. 2605257efdcSOCHyams """ 2615257efdcSOCHyams # Get the flags passed by the driver and look for -dwarf-version. 2625257efdcSOCHyams cmd = f'{llvm_config.use_llvm_tool("clang")} -g -xc -c - -v -### --target={triple}' 2635257efdcSOCHyams stderr = subprocess.run(cmd.split(), stderr=subprocess.PIPE).stderr.decode() 264*a1a3e019SEisuke Kawashima match = re.search(r"-dwarf-version=(\d+)", stderr) 2655257efdcSOCHyams if match is None: 2665257efdcSOCHyams print("Cannot determine default dwarf version", file=sys.stderr) 2675257efdcSOCHyams return None 2685257efdcSOCHyams return match.group(1) 2695257efdcSOCHyams 270f98ee40fSTobias Hieta 2715257efdcSOCHyams# Some cross-project-tests use gdb, but not all versions of gdb are compatible 2725257efdcSOCHyams# with clang's dwarf. Add feature `gdb-clang-incompatibility` to signal that 2735257efdcSOCHyams# there's an incompatibility between clang's default dwarf version for this 2745257efdcSOCHyams# platform and the installed gdb version. 2755257efdcSOCHyamsdwarf_version_string = get_clang_default_dwarf_version_string(config.host_triple) 2765257efdcSOCHyamsgdb_version_string = get_gdb_version_string() 2775257efdcSOCHyamsif dwarf_version_string and gdb_version_string: 2785257efdcSOCHyams if int(dwarf_version_string) >= 5: 2799374216dSdyung try: 2809374216dSdyung from packaging import version 2819374216dSdyung except: 2829374216dSdyung lit_config.fatal("Running gdb tests requires the packaging package") 2839374216dSdyung if version.parse(gdb_version_string) < version.parse("10.1"): 2845257efdcSOCHyams # Example for llgdb-tests, which use lldb on darwin but gdb elsewhere: 2855257efdcSOCHyams # XFAIL: !system-darwin && gdb-clang-incompatibility 286f98ee40fSTobias Hieta config.available_features.add("gdb-clang-incompatibility") 287f98ee40fSTobias Hieta print( 288f98ee40fSTobias Hieta "XFAIL some tests: use gdb version >= 10.1 to restore test coverage", 289f98ee40fSTobias Hieta file=sys.stderr, 2901364750dSJames Henderson ) 2917062094bSThomas Lively 292f98ee40fSTobias Hietallvm_config.feature_config([("--build-mode", {"Debug|RelWithDebInfo": "debug-info"})]) 293f98ee40fSTobias Hieta 2947062094bSThomas Lively# Allow 'REQUIRES: XXX-registered-target' in tests. 2957062094bSThomas Livelyfor arch in config.targets_to_build: 296f98ee40fSTobias Hieta config.available_features.add(arch.lower() + "-registered-target") 297