llvm-project/lldb/bindings/python/get-python-config.py
Lawrence D'Anna 4c2cf3a314 [lldb] fix -print-script-interpreter-info on windows
Apparently "{sys.prefix}/bin/python3" isn't where you find the
python interpreter on windows, so the test I wrote for
-print-script-interpreter-info is failing.

We can't rely on sys.executable at runtime, because that will point
to lldb.exe not python.exe.

We can't just record sys.executable from build time, because python
could have been moved to a different location.

But it should be OK to apply relative path from sys.prefix to sys.executable
from build-time to the sys.prefix at runtime.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D113650
2021-11-16 13:50:20 -08:00

47 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import sys
import argparse
import sysconfig
import distutils.sysconfig
def relpath_nodots(path, base):
rel = os.path.normpath(os.path.relpath(path, base))
assert not os.path.isabs(rel)
parts = rel.split(os.path.sep)
if parts and parts[0] == '..':
raise ValueError(f"{path} is not under {base}")
return rel
def main():
parser = argparse.ArgumentParser(description="extract cmake variables from python")
parser.add_argument("variable_name")
args = parser.parse_args()
if args.variable_name == "LLDB_PYTHON_RELATIVE_PATH":
print(distutils.sysconfig.get_python_lib(True, False, ''))
elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":
tried = list()
exe = sys.executable
while True:
try:
print(relpath_nodots(exe, sys.prefix))
break
except ValueError:
tried.append(exe)
if os.path.islink(exe):
exe = os.path.join(os.path.dirname(exe), os.readlink(exe))
continue
else:
print("Could not find a relative path to sys.executable under sys.prefix", file=sys.stderr)
for e in tried:
print("tried:", e, file=sys.stderr)
print("sys.prefix:", sys.prefix, file=sys.stderr)
sys.exit(1)
else:
parser.error(f"unknown variable {args.variable_name}")
if __name__ == '__main__':
main()