Debian Codegraph @

mesonbuild/scripts/python_info.py

raw
1 #!/usr/bin/env python
2
3 # ignore all lints for this file, since it is run by python2 as well
4
5 # type: ignore
6 # pylint: disable=deprecated-module
7
8 import sys
9
10 # do not inject mesonbuild.scripts
11 # python -P would work too, but is exclusive to >=3.11
12 if sys.path[0].endswith('scripts'):
13 del sys.path[0]
14
15 import json, os, sysconfig
16
17 def get_distutils_paths(scheme=None, prefix=None):
18 import distutils.dist
19 distribution = distutils.dist.Distribution()
20 install_cmd = distribution.get_command_obj('install')
21 if prefix is not None:
22 install_cmd.prefix = prefix
23 if scheme:
24 install_cmd.select_scheme(scheme)
25 install_cmd.finalize_options()
26 return {
27 'data': install_cmd.install_data,
28 'include': os.path.dirname(install_cmd.install_headers),
29 'platlib': install_cmd.install_platlib,
30 'purelib': install_cmd.install_purelib,
31 'scripts': install_cmd.install_scripts,
32 }
33
34 # On Debian derivatives, the Python interpreter shipped by the distribution uses
35 # a custom install scheme, deb_system, for the system install, and changes the
36 # default scheme to a custom one pointing to /usr/local and replacing
37 # site-packages with dist-packages.
38 # See https://github.com/mesonbuild/meson/issues/8739.
39 #
40 # We should be using sysconfig, but before 3.10.3, Debian only patches distutils.
41 # So we may end up falling back.
42
43 def get_install_paths():
44 if sys.version_info >= (3, 10):
45 scheme = sysconfig.get_default_scheme()
46 else:
47 scheme = sysconfig._get_default_scheme()
48
49 if sys.version_info >= (3, 10, 3):
50 if 'deb_system' in sysconfig.get_scheme_names():
51 scheme = 'deb_system'
52 else:
53 import distutils.command.install
54 if 'deb_system' in distutils.command.install.INSTALL_SCHEMES:
55 paths = get_distutils_paths(scheme='deb_system')
56 install_paths = get_distutils_paths(scheme='deb_system', prefix='')
57 return paths, install_paths
58
59 paths = sysconfig.get_paths(scheme=scheme)
60 empty_vars = {'base': '', 'platbase': '', 'installed_base': ''}
61 install_paths = sysconfig.get_paths(scheme=scheme, vars=empty_vars)
62 return paths, install_paths
63
64 paths, install_paths = get_install_paths()
65
66 def links_against_libpython():
67 # on versions supporting python-embed.pc, this is the non-embed lib
68 if is_pypy:
69 # PyPy work is still pending to export the relevant information (it
70 # doesn't automatically provide arbitrary Makefile vars). However,
71 # the only PyPy supported platform that requires linking to libpython
72 # is Windows, thus it is easy enough to hardcode the answer.
73 return sysconfig.get_platform() == "win-amd64"
74 elif sys.version_info >= (3, 8):
75 variables = sysconfig.get_config_vars()
76 return bool(variables.get('LIBPYTHON', 'yes'))
77 else:
78 from distutils.core import Distribution, Extension
79 cmd = Distribution().get_command_obj('build_ext')
80 cmd.ensure_finalized()
81 return bool(cmd.get_libraries(Extension('dummy', [])))
82
83 variables = sysconfig.get_config_vars()
84 variables.update({'base_prefix': getattr(sys, 'base_prefix', sys.prefix)})
85
86 is_pypy = '__pypy__' in sys.builtin_module_names
87
88 if sys.version_info < (3, 0):
89 suffix = variables.get('SO')
90 elif sys.version_info < (3, 8, 7):
91 # https://bugs.python.org/issue?@action=redirect&bpo=39825
92 from distutils.sysconfig import get_config_var
93 suffix = get_config_var('EXT_SUFFIX')
94 else:
95 suffix = variables.get('EXT_SUFFIX')
96
97 limited_api_suffix = None
98 if sys.version_info >= (3, 2):
99 try:
100 from importlib.machinery import EXTENSION_SUFFIXES
101 limited_api_suffix = EXTENSION_SUFFIXES[1]
102 except Exception:
103 pass
104
105 # pypy supports modules targeting the limited api but
106 # does not use a special suffix to distinguish them:
107 # https://doc.pypy.org/en/latest/cpython_differences.html#permitted-abi-tags-in-extensions
108 if is_pypy:
109 limited_api_suffix = suffix
110
111 # Whether we're targeting a free-threaded CPython interpreter
112 is_freethreaded = bool(variables.get('Py_GIL_DISABLED', False))
113
114 print(json.dumps({
115 'variables': variables,
116 'paths': paths,
117 'sysconfig_paths': sysconfig.get_paths(),
118 'install_paths': install_paths,
119 'version': sysconfig.get_python_version(),
120 'platform': sysconfig.get_platform(),
121 'is_pypy': is_pypy,
122 'is_venv': sys.prefix != variables['base_prefix'],
123 'link_libpython': links_against_libpython(),
124 'suffix': suffix,
125 'limited_api_suffix': limited_api_suffix,
126 'is_freethreaded': is_freethreaded,
127 }))