2021-11-16 13:50:18 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import argparse
|
|
|
|
|
import 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)
|
2023-09-12 08:46:34 +01:00
|
|
|
if parts and parts[0] == "..":
|
2021-11-16 13:50:18 -08:00
|
|
|
raise ValueError(f"{path} is not under {base}")
|
|
|
|
|
return rel
|
|
|
|
|
|
2023-09-12 08:46:34 +01:00
|
|
|
|
2021-11-16 13:50:18 -08:00
|
|
|
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":
|
[lldb] remove usage of distutils, fix python path on debian/ubuntu
distutils is deprecated and will be removed, so we shouldn't be
using it.
We were using it to compute LLDB_PYTHON_RELATIVE_PATH.
Discussing a similar issue
[at python.org](https://bugs.python.org/issue41282), Filipe Laíns said:
If you are relying on the value of distutils.sysconfig.get_python_lib()
as you shown in your system, you probably don't want to. That
directory (dist-packages) should be for Debian provided packages
only, so moving to sysconfig.get_path() would be a good thing,
as it has the correct value for user installed packages on your
system.
So I propose using a relative path from `sys.prefix` to
`sysconfig.get_path("platlib")` instead.
On Mac and windows, this results in the same paths as we had before,
which are `lib/python3.9/site-packages` and `Lib\site-packages`,
respectively.
On ubuntu however, this will change the path from
`lib/python3/dist-packages` to `lib/python3.9/site-packages`.
This change seems to be correct, as Filipe said above, `dist-packages`
belongs to the distribution, not us.
Reviewed By: labath
Differential Revision: https://reviews.llvm.org/D114106
2021-11-17 13:14:10 -08:00
|
|
|
# LLDB_PYTHON_RELATIVE_PATH is the relative path from lldb's prefix
|
|
|
|
|
# to where lldb's python libraries will be installed.
|
|
|
|
|
#
|
|
|
|
|
# The way we're going to compute this is to take the relative path from
|
|
|
|
|
# PYTHON'S prefix to where python libraries are supposed to be
|
|
|
|
|
# installed.
|
|
|
|
|
#
|
|
|
|
|
# The result is if LLDB and python are give the same prefix, then
|
|
|
|
|
# lldb's python lib will be put in the correct place for python to find it.
|
|
|
|
|
# If not, you'll have to use lldb -P or lldb -print-script-interpreter-info
|
|
|
|
|
# to figure out where it is.
|
2021-12-02 21:13:09 -08:00
|
|
|
try:
|
|
|
|
|
print(relpath_nodots(sysconfig.get_path("platlib"), sys.prefix))
|
|
|
|
|
except ValueError:
|
|
|
|
|
# Try to fall back to something reasonable if sysconfig's platlib
|
|
|
|
|
# is outside of sys.prefix
|
2023-09-12 08:46:34 +01:00
|
|
|
if os.name == "posix":
|
|
|
|
|
print("lib/python%d.%d/site-packages" % sys.version_info[:2])
|
|
|
|
|
elif os.name == "nt":
|
|
|
|
|
print("Lib\\site-packages")
|
2021-12-02 21:13:09 -08:00
|
|
|
else:
|
|
|
|
|
raise
|
2021-11-16 13:50:18 -08:00
|
|
|
elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH":
|
|
|
|
|
tried = list()
|
|
|
|
|
exe = sys.executable
|
2021-11-17 09:16:50 -08:00
|
|
|
prefix = os.path.realpath(sys.prefix)
|
2021-11-16 13:50:18 -08:00
|
|
|
while True:
|
|
|
|
|
try:
|
2021-11-17 09:16:50 -08:00
|
|
|
print(relpath_nodots(exe, prefix))
|
2021-11-16 13:50:18 -08:00
|
|
|
break
|
|
|
|
|
except ValueError:
|
|
|
|
|
tried.append(exe)
|
2023-01-06 09:54:55 +01:00
|
|
|
# Retry if the executable is symlinked or similar.
|
|
|
|
|
# This is roughly equal to os.path.islink, except it also works for junctions on Windows.
|
|
|
|
|
if os.path.realpath(exe) != exe:
|
|
|
|
|
exe = os.path.realpath(exe)
|
2021-11-16 13:50:18 -08:00
|
|
|
continue
|
|
|
|
|
else:
|
2023-09-12 08:46:34 +01:00
|
|
|
print(
|
|
|
|
|
"Could not find a relative path to sys.executable under sys.prefix",
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
2021-11-16 13:50:18 -08:00
|
|
|
for e in tried:
|
|
|
|
|
print("tried:", e, file=sys.stderr)
|
2021-11-17 09:16:50 -08:00
|
|
|
print("realpath(sys.prefix):", prefix, file=sys.stderr)
|
2021-11-16 13:50:18 -08:00
|
|
|
print("sys.prefix:", sys.prefix, file=sys.stderr)
|
|
|
|
|
sys.exit(1)
|
2021-11-16 14:32:03 -08:00
|
|
|
elif args.variable_name == "LLDB_PYTHON_EXT_SUFFIX":
|
2023-09-12 08:46:34 +01:00
|
|
|
print(sysconfig.get_config_var("EXT_SUFFIX"))
|
2021-11-16 13:50:18 -08:00
|
|
|
else:
|
|
|
|
|
parser.error(f"unknown variable {args.variable_name}")
|
|
|
|
|
|
2023-09-12 08:46:34 +01:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-12-02 21:13:09 -08:00
|
|
|
main()
|