brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · bf8cc48 Raw
84 lines · python
1#!/usr/bin/env python32 3import os4import sys5import argparse6import sysconfig7 8 9def relpath_nodots(path, base):10    rel = os.path.normpath(os.path.relpath(path, base))11    assert not os.path.isabs(rel)12    parts = rel.split(os.path.sep)13    if parts and parts[0] == "..":14        raise ValueError(f"{path} is not under {base}")15    return rel16 17 18def main():19    parser = argparse.ArgumentParser(description="extract cmake variables from python")20    parser.add_argument("variable_name")21    parser.add_argument(22        "--stable-abi", action="store_true", help="Target the Stable C ABI"23    )24    args = parser.parse_args()25    if args.variable_name == "LLDB_PYTHON_RELATIVE_PATH":26        # LLDB_PYTHON_RELATIVE_PATH is the relative path from lldb's prefix27        # to where lldb's python libraries will be installed.28        #29        # The way we're going to compute this is to take the relative path from30        # PYTHON'S prefix to where python libraries are supposed to be31        # installed.32        #33        # The result is if LLDB and python are give the same prefix, then34        # lldb's python lib will be put in the correct place for python to find it.35        # If not, you'll have to use lldb -P or lldb -print-script-interpreter-info36        # to figure out where it is.37        try:38            print(relpath_nodots(sysconfig.get_path("platlib"), sys.prefix))39        except ValueError:40            # Try to fall back to something reasonable if sysconfig's platlib41            # is outside of sys.prefix42            if os.name == "posix":43                print("lib/python%d.%d/site-packages" % sys.version_info[:2])44            elif os.name == "nt":45                print("Lib\\site-packages")46            else:47                raise48    elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":49        tried = list()50        exe = sys.executable51        prefix = os.path.realpath(sys.prefix)52        while True:53            try:54                print(relpath_nodots(exe, prefix))55                break56            except ValueError:57                tried.append(exe)58                # Retry if the executable is symlinked or similar.59                # This is roughly equal to os.path.islink, except it also works for junctions on Windows.60                if os.path.realpath(exe) != exe:61                    exe = os.path.realpath(exe)62                    continue63                else:64                    print(65                        "Could not find a relative path to sys.executable under sys.prefix",66                        file=sys.stderr,67                    )68                    for e in tried:69                        print("tried:", e, file=sys.stderr)70                    print("realpath(sys.prefix):", prefix, file=sys.stderr)71                    print("sys.prefix:", sys.prefix, file=sys.stderr)72                    sys.exit(1)73    elif args.variable_name == "LLDB_PYTHON_EXT_SUFFIX":74        if args.stable_abi:75            print(".abi3%s" % sysconfig.get_config_var("SHLIB_SUFFIX"))76        else:77            print(sysconfig.get_config_var("EXT_SUFFIX"))78    else:79        parser.error(f"unknown variable {args.variable_name}")80 81 82if __name__ == "__main__":83    main()84