| 1 |
"""Provide access to Python's configuration information. |
| 2 |
This is actually PyPy's minimal configuration information. |
| 3 |
|
| 4 |
The specific configuration variables available depend heavily on the |
| 5 |
platform and configuration. The values may be retrieved using |
| 6 |
get_config_var(name), and the list of variables is available via |
| 7 |
get_config_vars().keys(). Additional convenience functions are also |
| 8 |
available. |
| 9 |
""" |
| 10 |
|
| 11 |
import _imp |
| 12 |
import os |
| 13 |
import re |
| 14 |
import sys |
| 15 |
import warnings |
| 16 |
|
| 17 |
from functools import partial |
| 18 |
|
| 19 |
from .errors import DistutilsPlatformError |
| 20 |
|
| 21 |
from sysconfig import ( |
| 22 |
_PREFIX as PREFIX, |
| 23 |
_BASE_PREFIX as BASE_PREFIX, |
| 24 |
_EXEC_PREFIX as EXEC_PREFIX, |
| 25 |
_BASE_EXEC_PREFIX as BASE_EXEC_PREFIX, |
| 26 |
_PROJECT_BASE as project_base, |
| 27 |
_PYTHON_BUILD as python_build, |
| 28 |
_init_posix as sysconfig_init_posix, |
| 29 |
parse_config_h as sysconfig_parse_config_h, |
| 30 |
|
| 31 |
_init_non_posix, |
| 32 |
_sys_home, |
| 33 |
|
| 34 |
_variable_rx, |
| 35 |
_findvar1_rx, |
| 36 |
_findvar2_rx, |
| 37 |
|
| 38 |
expand_makefile_vars, |
| 39 |
is_python_build, |
| 40 |
get_config_h_filename, |
| 41 |
get_config_var, |
| 42 |
get_config_vars, |
| 43 |
get_makefile_filename, |
| 44 |
get_python_version, |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
_config_vars = get_config_vars() |
| 52 |
|
| 53 |
warnings.warn( |
| 54 |
'The distutils.sysconfig module is deprecated, use sysconfig instead', |
| 55 |
DeprecationWarning, |
| 56 |
stacklevel=2 |
| 57 |
) |
| 58 |
|
| 59 |
|
| •60 |
def is_virtual_environment(): |
| 61 |
return sys.base_prefix != sys.prefix or hasattr(sys, "real_prefix") |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
def parse_config_h(fp, g=None): |
| 66 |
return sysconfig_parse_config_h(fp, vars=g) |
| 67 |
|
| 68 |
|
| 69 |
_python_build = partial(is_python_build, check_home=True) |
| 70 |
_init_posix = partial(sysconfig_init_posix, _config_vars) |
| 71 |
_init_nt = partial(_init_non_posix, _config_vars) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
def parse_makefile(fn, g=None): |
| 77 |
"""Parse a Makefile-style file. |
| 78 |
A dictionary containing name/value pairs is returned. If an |
| 79 |
optional dictionary is passed in as the second argument, it is |
| 80 |
used instead of a new dictionary. |
| 81 |
""" |
| 82 |
from distutils.text_file import TextFile |
| 83 |
fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape") |
| 84 |
|
| 85 |
if g is None: |
| 86 |
g = {} |
| 87 |
done = {} |
| 88 |
notdone = {} |
| 89 |
|
| 90 |
while True: |
| 91 |
line = fp.readline() |
| 92 |
if line is None: |
| 93 |
break |
| 94 |
m = re.match(_variable_rx, line) |
| 95 |
if m: |
| 96 |
n, v = m.group(1, 2) |
| 97 |
v = v.strip() |
| 98 |
|
| 99 |
tmpv = v.replace('$$', '') |
| 100 |
|
| 101 |
if "$" in tmpv: |
| 102 |
notdone[n] = v |
| 103 |
else: |
| 104 |
try: |
| 105 |
v = int(v) |
| 106 |
except ValueError: |
| 107 |
|
| 108 |
done[n] = v.replace('$$', '$') |
| 109 |
else: |
| 110 |
done[n] = v |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS') |
| 117 |
|
| 118 |
|
| 119 |
while notdone: |
| 120 |
for name in list(notdone): |
| 121 |
value = notdone[name] |
| 122 |
m = re.search(_findvar1_rx, value) or re.search(_findvar2_rx, value) |
| 123 |
if m: |
| 124 |
n = m.group(1) |
| 125 |
found = True |
| 126 |
if n in done: |
| 127 |
item = str(done[n]) |
| 128 |
elif n in notdone: |
| 129 |
|
| 130 |
found = False |
| 131 |
elif n in os.environ: |
| 132 |
|
| 133 |
item = os.environ[n] |
| 134 |
|
| 135 |
elif n in renamed_variables: |
| 136 |
if name.startswith('PY_') and name[3:] in renamed_variables: |
| 137 |
item = "" |
| 138 |
|
| 139 |
elif 'PY_' + n in notdone: |
| 140 |
found = False |
| 141 |
|
| 142 |
else: |
| 143 |
item = str(done['PY_' + n]) |
| 144 |
else: |
| 145 |
done[n] = item = "" |
| 146 |
if found: |
| 147 |
after = value[m.end():] |
| 148 |
value = value[:m.start()] + item + after |
| 149 |
if "$" in after: |
| 150 |
notdone[name] = value |
| 151 |
else: |
| 152 |
try: value = int(value) |
| 153 |
except ValueError: |
| 154 |
done[name] = value.strip() |
| 155 |
else: |
| 156 |
done[name] = value |
| 157 |
del notdone[name] |
| 158 |
|
| 159 |
if name.startswith('PY_') \ |
| 160 |
and name[3:] in renamed_variables: |
| 161 |
|
| 162 |
name = name[3:] |
| 163 |
if name not in done: |
| 164 |
done[name] = value |
| 165 |
else: |
| 166 |
|
| 167 |
del notdone[name] |
| 168 |
|
| 169 |
fp.close() |
| 170 |
|
| 171 |
|
| 172 |
for k, v in done.items(): |
| 173 |
if isinstance(v, str): |
| 174 |
done[k] = v.strip() |
| 175 |
|
| 176 |
|
| 177 |
g.update(done) |
| 178 |
return g |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
build_flags = '' |
| 188 |
try: |
| 189 |
if not python_build: |
| 190 |
build_flags = sys.abiflags |
| 191 |
except AttributeError: |
| 192 |
|
| 193 |
|
| 194 |
pass |
| 195 |
|
| 196 |
|
| 197 |
def customize_compiler(compiler): |
| 198 |
"""Do any platform-specific customization of a CCompiler instance. |
| 199 |
|
| 200 |
Mainly needed on Unix, so we can plug in the information that |
| 201 |
varies across Unices and is stored in Python's Makefile. |
| 202 |
""" |
| 203 |
if compiler.compiler_type == "unix": |
| 204 |
if sys.platform == "darwin": |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if not _config_vars.get('CUSTOMIZED_OSX_COMPILER'): |
| 214 |
import _osx_support |
| 215 |
_osx_support.customize_compiler(_config_vars) |
| 216 |
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True' |
| 217 |
|
| 218 |
(cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \ |
| 219 |
get_config_vars('CC', 'CXX', 'CFLAGS', |
| 220 |
'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS') |
| 221 |
|
| 222 |
if 'CC' in os.environ: |
| 223 |
newcc = os.environ['CC'] |
| 224 |
if (sys.platform == 'darwin' |
| 225 |
and 'LDSHARED' not in os.environ |
| 226 |
and ldshared.startswith(cc)): |
| 227 |
|
| 228 |
|
| 229 |
ldshared = newcc + ldshared[len(cc):] |
| 230 |
cc = newcc |
| 231 |
if 'CXX' in os.environ: |
| 232 |
cxx = os.environ['CXX'] |
| 233 |
if 'LDSHARED' in os.environ: |
| 234 |
ldshared = os.environ['LDSHARED'] |
| 235 |
if 'CPP' in os.environ: |
| 236 |
cpp = os.environ['CPP'] |
| 237 |
else: |
| 238 |
cpp = cc + " -E" |
| 239 |
if 'LDFLAGS' in os.environ: |
| 240 |
ldshared = ldshared + ' ' + os.environ['LDFLAGS'] |
| 241 |
if 'CFLAGS' in os.environ: |
| 242 |
cflags = cflags + ' ' + os.environ['CFLAGS'] |
| 243 |
ldshared = ldshared + ' ' + os.environ['CFLAGS'] |
| 244 |
if 'CPPFLAGS' in os.environ: |
| 245 |
cpp = cpp + ' ' + os.environ['CPPFLAGS'] |
| 246 |
cflags = cflags + ' ' + os.environ['CPPFLAGS'] |
| 247 |
ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] |
| 248 |
if 'AR' in os.environ: |
| 249 |
ar = os.environ['AR'] |
| 250 |
if 'ARFLAGS' in os.environ: |
| 251 |
archiver = ar + ' ' + os.environ['ARFLAGS'] |
| 252 |
else: |
| 253 |
archiver = ar + ' ' + ar_flags |
| 254 |
|
| 255 |
cc_cmd = cc + ' ' + cflags |
| 256 |
compiler.set_executables( |
| 257 |
preprocessor=cpp, |
| 258 |
compiler=cc_cmd, |
| 259 |
compiler_so=cc_cmd + ' ' + ccshared, |
| 260 |
compiler_cxx=cxx, |
| 261 |
linker_so=ldshared, |
| 262 |
linker_exe=cc, |
| 263 |
archiver=archiver) |
| 264 |
|
| 265 |
compiler.shared_lib_extension = shlib_suffix |
| 266 |
|
| 267 |
|
| 268 |
def get_python_inc(plat_specific=0, prefix=None): |
| 269 |
"""Return the directory containing installed Python header files. |
| 270 |
|
| 271 |
If 'plat_specific' is false (the default), this is the path to the |
| 272 |
non-platform-specific header files, i.e. Python.h and so on; |
| 273 |
otherwise, this is the path to platform-specific header files |
| 274 |
(namely pyconfig.h). |
| 275 |
|
| 276 |
If 'prefix' is supplied, use it instead of sys.base_prefix or |
| 277 |
sys.base_exec_prefix -- i.e., ignore 'plat_specific'. |
| 278 |
""" |
| 279 |
if prefix is None: |
| 280 |
prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX |
| 281 |
if os.name == "posix": |
| 282 |
if python_build: |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if plat_specific: |
| 289 |
return _sys_home or project_base |
| 290 |
else: |
| 291 |
incdir = os.path.join(get_config_var('srcdir'), 'Include') |
| 292 |
return os.path.normpath(incdir) |
| 293 |
python_dir = 'pypy' + get_python_version() + build_flags |
| 294 |
return os.path.join(prefix, "include", python_dir) |
| 295 |
elif os.name == "nt": |
| 296 |
if python_build: |
| 297 |
|
| 298 |
|
| 299 |
return (os.path.join(prefix, "include") + os.path.pathsep + |
| 300 |
os.path.join(prefix, "PC")) |
| 301 |
return os.path.join(prefix, "include") |
| 302 |
else: |
| 303 |
raise DistutilsPlatformError( |
| 304 |
"I don't know where Python installs its C header files " |
| 305 |
"on platform '%s'" % os.name) |
| 306 |
|
| 307 |
|
| 308 |
def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
| 309 |
"""Return the directory containing the Python library (standard or |
| 310 |
site additions). |
| 311 |
|
| 312 |
If 'plat_specific' is true, return the directory containing |
| 313 |
platform-specific modules, i.e. any module from a non-pure-Python |
| 314 |
module distribution; otherwise, return the platform-shared library |
| 315 |
directory. If 'standard_lib' is true, return the directory |
| 316 |
containing standard Python library modules; otherwise, return the |
| 317 |
directory for site-specific modules. |
| 318 |
|
| 319 |
If 'prefix' is supplied, use it instead of sys.base_prefix or |
| 320 |
sys.base_exec_prefix -- i.e., ignore 'plat_specific'. |
| 321 |
""" |
| 322 |
if prefix is None: |
| 323 |
if standard_lib: |
| 324 |
prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX |
| 325 |
else: |
| 326 |
prefix = plat_specific and EXEC_PREFIX or PREFIX |
| 327 |
|
| 328 |
if os.name == "posix": |
| 329 |
if plat_specific or standard_lib: |
| 330 |
|
| 331 |
|
| 332 |
libdir = sys.platlibdir |
| 333 |
else: |
| 334 |
|
| 335 |
libdir = "lib" |
| 336 |
libpython = os.path.join(prefix, libdir, |
| 337 |
"pypy" + get_python_version()) |
| 338 |
if standard_lib: |
| 339 |
return libpython |
| 340 |
else: |
| 341 |
return os.path.join(libpython, "site-packages") |
| 342 |
elif os.name == "nt": |
| 343 |
if standard_lib: |
| 344 |
return os.path.join(prefix, "Lib") |
| 345 |
else: |
| 346 |
return os.path.join(prefix, "Lib", "site-packages") |
| 347 |
else: |
| 348 |
raise DistutilsPlatformError( |
| 349 |
"I don't know where Python installs its library " |
| 350 |
"on platform '%s'" % os.name) |