1#!/usr/bin/env python3 2 3import os 4import sys 5import argparse 6import sysconfig 7 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 rel 16 17def main(): 18 parser = argparse.ArgumentParser(description="extract cmake variables from python") 19 parser.add_argument("variable_name") 20 args = parser.parse_args() 21 if args.variable_name == "LLDB_PYTHON_RELATIVE_PATH": 22 # LLDB_PYTHON_RELATIVE_PATH is the relative path from lldb's prefix 23 # to where lldb's python libraries will be installed. 24 # 25 # The way we're going to compute this is to take the relative path from 26 # PYTHON'S prefix to where python libraries are supposed to be 27 # installed. 28 # 29 # The result is if LLDB and python are give the same prefix, then 30 # lldb's python lib will be put in the correct place for python to find it. 31 # If not, you'll have to use lldb -P or lldb -print-script-interpreter-info 32 # to figure out where it is. 33 print(relpath_nodots(sysconfig.get_path("platlib"), sys.prefix)) 34 elif args.variable_name == "LLDB_PYTHON_EXE_RELATIVE_PATH": 35 tried = list() 36 exe = sys.executable 37 prefix = os.path.realpath(sys.prefix) 38 while True: 39 try: 40 print(relpath_nodots(exe, prefix)) 41 break 42 except ValueError: 43 tried.append(exe) 44 if os.path.islink(exe): 45 exe = os.path.join(os.path.realpath(os.path.dirname(exe)), os.readlink(exe)) 46 continue 47 else: 48 print("Could not find a relative path to sys.executable under sys.prefix", file=sys.stderr) 49 for e in tried: 50 print("tried:", e, file=sys.stderr) 51 print("realpath(sys.prefix):", prefix, file=sys.stderr) 52 print("sys.prefix:", sys.prefix, file=sys.stderr) 53 sys.exit(1) 54 elif args.variable_name == "LLDB_PYTHON_EXT_SUFFIX": 55 print(sysconfig.get_config_var('EXT_SUFFIX')) 56 else: 57 parser.error(f"unknown variable {args.variable_name}") 58 59if __name__ == '__main__': 60 main()