brintos

brintos / llvm-project-archived public Read only

0
0
Text · 11.5 KiB · e702a77 Raw
318 lines · python
1import os2import platform3import re4import subprocess5import sys6 7import lit.formats8import lit.util9 10from lit.llvm import llvm_config11from lit.llvm.subst import ToolSubst12 13# Configuration file for the 'lit' test runner.14 15# name: The name of this test suite.16config.name = "cross-project-tests"17 18# testFormat: The test format to use to interpret tests.19config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)20 21# suffixes: A list of file extensions to treat as test files.22config.suffixes = [".c", ".cl", ".cpp", ".m", ".test"]23 24# excludes: A list of directories to exclude from the testsuite. The 'Inputs'25# subdirectories contain auxiliary inputs for various tests in their parent26# directories.27config.excludes = ["Inputs"]28 29# test_source_root: The root path where tests are located.30config.test_source_root = config.cross_project_tests_src_root31 32# test_exec_root: The root path where tests should be run.33config.test_exec_root = config.cross_project_tests_obj_root34 35llvm_config.use_default_substitutions()36 37lldb_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.executable43tools = [44    ToolSubst(45        "%test_debuginfo",46        command="PYTHON_EXEC_PATH="47        + python_exec_path48        + " LLDB_PYTHON_PATH="49        + lldb_python_path50        + " "51        + os.path.join(52            config.cross_project_tests_src_root,53            "debuginfo-tests",54            "llgdb-tests",55            "test_debuginfo.pl",56        ),57    ),58    ToolSubst("%llvm_src_root", config.llvm_src_root),59    ToolSubst("%llvm_tools_dir", config.llvm_tools_dir),60]61 62 63def get_required_attr(config, attr_name):64    attr_value = getattr(config, attr_name, None)65    if attr_value is None:66        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_name70        )71    return attr_value72 73 74# If this is an MSVC environment, the tests at the root of the tree are75# unsupported. The local win_cdb test suite, however, is supported.76is_msvc = get_required_attr(config, "is_msvc")77if is_msvc:78    config.available_features.add("msvc")79    # FIXME: We should add some llvm lit utility code to find the Windows SDK80    # and set up the environment appopriately.81    win_sdk = "C:/Program Files (x86)/Windows Kits/10/"82    arch = "x64"83    llvm_config.with_system_environment(["LIB", "LIBPATH", "INCLUDE"])84    # Clear _NT_SYMBOL_PATH to prevent cdb from attempting to load symbols from85    # the network.86    llvm_config.with_environment("_NT_SYMBOL_PATH", "")87    tools.append(88        ToolSubst("%cdb", '"%s"' % os.path.join(win_sdk, "Debuggers", arch, "cdb.exe"))89    )90 91# clang_src_dir and lld_src_dir are not used by these tests, but are required by92# use_clang() and use_lld() respectively, so set them to "", if needed.93if not hasattr(config, "clang_src_dir"):94    config.clang_src_dir = ""95llvm_config.use_clang(required=("clang" in config.llvm_enabled_projects))96 97if not hasattr(config, "lld_src_dir"):98    config.lld_src_dir = ""99llvm_config.use_lld(required=("lld" in config.llvm_enabled_projects))100 101if "compiler-rt" in config.llvm_enabled_projects:102    config.available_features.add("compiler-rt")103 104# Check which debuggers are available:105lldb_dap_path = llvm_config.use_llvm_tool("lldb-dap")106if lldb_dap_path is not None:107    config.available_features.add("lldb")108 109if llvm_config.use_llvm_tool("llvm-ar"):110    config.available_features.add("llvm-ar")111 112def configure_dexter_substitutions():113    """Configure substitutions for host platform and return list of dependencies"""114    # Produce dexter path, lldb path, and combine into the %dexter substitution115    # for running a test.116    dexter_path = os.path.join(117        config.cross_project_tests_src_root, "debuginfo-tests", "dexter", "dexter.py"118    )119    tools.append(ToolSubst("%dexter", f'"{sys.executable}" "{dexter_path}" test -v'))120    if lldb_dap_path is not None:121        tools.append(122            ToolSubst(123                "%dexter_lldb_args",124                f'--lldb-executable "{lldb_dap_path}" --debugger lldb-dap --dap-message-log=-e',125            )126        )127 128    # For testing other bits of dexter that aren't under the "test" subcommand,129    # have a %dexter_base substitution.130    dexter_base_cmd = '"{}" "{}"'.format(sys.executable, dexter_path)131    tools.append(ToolSubst("%dexter_base", dexter_base_cmd))132 133    # Set up commands for DexTer regression tests.134    # Builder, debugger, optimisation level and several other flags differ135    # depending on whether we're running a unix like or windows os.136    if platform.system() == "Windows":137        # The Windows builder script uses lld.138        dependencies = ["clang", "lld-link"]139        dexter_regression_test_c_builder = "clang-cl"140        dexter_regression_test_cxx_builder = "clang-cl"141        dexter_regression_test_debugger = "dbgeng"142        dexter_regression_test_c_flags = "/Zi /Od"143        dexter_regression_test_cxx_flags = "/Zi /Od"144        dexter_regression_test_additional_flags = ""145    else:146        # Use lldb as the debugger on non-Windows platforms.147        dependencies = ["clang", "lldb"]148        dexter_regression_test_c_builder = "clang"149        dexter_regression_test_cxx_builder = "clang++"150        dexter_regression_test_debugger = "lldb-dap"151        dexter_regression_test_additional_flags = (152            f'--lldb-executable "{lldb_dap_path}" --dap-message-log=-e'153        )154        dexter_regression_test_c_flags = "-O0 -glldb -std=gnu11"155        dexter_regression_test_cxx_flags = "-O0 -glldb -std=gnu++11"156 157    tools.append(158        ToolSubst(159            "%dexter_regression_test_debugger_args",160            f"--debugger {dexter_regression_test_debugger} {dexter_regression_test_additional_flags}",161        )162    )163 164    # Typical command would take the form:165    # ./path_to_py/python.exe ./path_to_dex/dexter.py test --fail-lt 1.0 -w --binary %t --debugger lldb --cflags '-O0 -g'166    dexter_regression_test_run = " ".join(167        # "python", "dexter.py", test, fail_mode, builder, debugger, cflags, ldflags168        [169            '"{}"'.format(sys.executable),170            '"{}"'.format(dexter_path),171            "test",172            "--fail-lt 1.0 -w -v",173            "--debugger",174            dexter_regression_test_debugger,175            dexter_regression_test_additional_flags,176        ]177    )178    tools.append(ToolSubst("%dexter_regression_test_run", dexter_regression_test_run))179 180    # Include build flags for %dexter_regression_test.181    dexter_regression_test_c_build = " ".join(182        [183            dexter_regression_test_c_builder,184            dexter_regression_test_c_flags,185        ]186    )187    dexter_regression_test_cxx_build = " ".join(188        [189            dexter_regression_test_cxx_builder,190            dexter_regression_test_cxx_flags,191        ]192    )193    tools.append(194        ToolSubst("%dexter_regression_test_c_build", dexter_regression_test_c_build)195    )196    tools.append(197        ToolSubst("%dexter_regression_test_cxx_build", dexter_regression_test_cxx_build)198    )199    return dependencies200 201 202def add_host_triple(clang):203    return "{} --target={}".format(clang, config.host_triple)204 205 206# The set of arches we can build.207targets = set(config.targets_to_build)208# Add aliases to the target set.209if "AArch64" in targets:210    targets.add("arm64")211if "ARM" in config.targets_to_build:212    targets.add("thumbv7")213 214 215def can_target_host():216    # Check if the targets set contains anything that looks like our host arch.217    # The arch name in the triple and targets set may be spelled differently218    # (e.g. x86 vs X86).219    return any(config.host_triple.lower().startswith(x.lower()) for x in targets)220 221 222# Dexter tests run on the host machine. If the host arch is supported add223# 'dexter' as an available feature and force the dexter tests to use the host224# triple.225if can_target_host():226    if config.host_triple != config.target_triple:227        print("Forcing dexter tests to use host triple {}.".format(config.host_triple))228    dependencies = configure_dexter_substitutions()229    if all(d in config.available_features for d in dependencies):230        config.available_features.add("dexter")231else:232    print(233        "Host triple {} not supported. Skipping dexter tests in the "234        "debuginfo-tests project.".format(config.host_triple)235    )236 237tool_dirs = [config.llvm_tools_dir]238 239llvm_config.add_tool_substitutions(tools, tool_dirs)240 241lit.util.usePlatformSdkOnDarwin(config, lit_config)242 243if platform.system() == "Darwin":244    xcode_lldb_vers = subprocess.check_output(["xcrun", "lldb", "--version"]).decode(245        "utf-8"246    )247    match = re.search(r"lldb-(\d+)", xcode_lldb_vers)248    if match:249        apple_lldb_vers = int(match.group(1))250        if apple_lldb_vers < 1000:251            config.available_features.add("apple-lldb-pre-1000")252 253 254def get_gdb_version_string():255    """Return gdb's version string, or None if gdb cannot be found or the256    --version output is formatted unexpectedly.257    """258    # See if we can get a gdb version, e.g.259    #   $ gdb --version260    #   GNU gdb (GDB) 10.2261    #   ...More stuff...262    try:263        gdb_vers_lines = (264            subprocess.check_output(["gdb", "--version"]).decode().splitlines()265        )266    except:267        return None  # We coudln't find gdb or something went wrong running it.268    if len(gdb_vers_lines) < 1:269        print("Unkown GDB version format (too few lines)", file=sys.stderr)270        return None271    match = re.search(r"GNU gdb \(.*?\) ((\d|\.)+)", gdb_vers_lines[0].strip())272    if match is None:273        print(f"Unkown GDB version format: {gdb_vers_lines[0]}", file=sys.stderr)274        return None275    return match.group(1)276 277 278def get_clang_default_dwarf_version_string(triple):279    """Return the default dwarf version string for clang on this (host) platform280    or None if we can't work it out.281    """282    # Get the flags passed by the driver and look for -dwarf-version.283    cmd = f'{llvm_config.use_llvm_tool("clang")} -g -xc  -c - -v -### --target={triple}'284    stderr = subprocess.run(cmd.split(), stderr=subprocess.PIPE).stderr.decode()285    match = re.search(r"-dwarf-version=(\d+)", stderr)286    if match is None:287        print("Cannot determine default dwarf version", file=sys.stderr)288        return None289    return match.group(1)290 291 292# Some cross-project-tests use gdb, but not all versions of gdb are compatible293# with clang's dwarf. Add feature `gdb-clang-incompatibility` to signal that294# there's an incompatibility between clang's default dwarf version for this295# platform and the installed gdb version.296dwarf_version_string = get_clang_default_dwarf_version_string(config.host_triple)297gdb_version_string = get_gdb_version_string()298if dwarf_version_string and gdb_version_string:299    if int(dwarf_version_string) >= 5:300        try:301            from packaging import version302        except:303            lit_config.fatal("Running gdb tests requires the packaging package")304        if version.parse(gdb_version_string) < version.parse("10.1"):305            # Example for llgdb-tests, which use lldb on darwin but gdb elsewhere:306            # XFAIL: !system-darwin && gdb-clang-incompatibility307            config.available_features.add("gdb-clang-incompatibility")308            print(309                "XFAIL some tests: use gdb version >= 10.1 to restore test coverage",310                file=sys.stderr,311            )312 313llvm_config.feature_config([("--build-mode", {"Debug|RelWithDebInfo": "debug-info"})])314 315# Allow 'REQUIRES: XXX-registered-target' in tests.316for arch in config.targets_to_build:317    config.available_features.add(arch.lower() + "-registered-target")318