import os
import re
import sys
import sysconfig
import pathlib
from .errors import DistutilsPlatformError
from . import py39compat
from ._functools import pass_none
IS_PYPY = '__pypy__' in sys.builtin_module_names
PREFIX = os.path.normpath(sys.prefix)
EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
BASE_PREFIX = os.path.normpath(sys.base_prefix)
BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
if "_PYTHON_PROJECT_BASE" in os.environ:
project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
else:
if sys.executable:
project_base = os.path.dirname(os.path.abspath(sys.executable))
else:
project_base = os.getcwd()
def _is_python_source_dir(d):
modules = pathlib.Path(d).joinpath('Modules')
return any(modules.joinpath(fn).is_file() for fn in ('Setup', 'Setup.local'))
_sys_home = getattr(sys, '_home', None)
def _is_parent(dir_a, dir_b):
return os.path.normcase(dir_a).startswith(os.path.normcase(dir_b))
if os.name == 'nt':
@pass_none
def _fix_pcbuild(d):
prefixes = PREFIX, BASE_PREFIX
matched = (
prefix
for prefix in prefixes
if _is_parent(d, os.path.join(prefix, "PCbuild"))
)
return next(matched, d)
project_base = _fix_pcbuild(project_base)
_sys_home = _fix_pcbuild(_sys_home)
def _python_build():
if _sys_home:
return _is_python_source_dir(_sys_home)
return _is_python_source_dir(project_base)
python_build = _python_build()
build_flags = ''
try:
if not python_build:
build_flags = sys.abiflags
except AttributeError:
pass
def get_python_version():
return '%d.%d' % sys.version_info[:2]
def get_python_inc(plat_specific=0, prefix=None):
default_prefix = BASE_EXEC_PREFIX if plat_specific else BASE_PREFIX
resolved_prefix = prefix if prefix is not None else default_prefix
try:
getter = globals()[f'_get_python_inc_{os.name}']
except KeyError:
raise DistutilsPlatformError(
"I don't know where Python installs its C header files "
"on platform '%s'" % os.name
)
return getter(resolved_prefix, prefix, plat_specific)
def _get_python_inc_posix(prefix, spec_prefix, plat_specific):
if IS_PYPY and sys.version_info < (3, 8):
return os.path.join(prefix, 'include')
return (
_get_python_inc_posix_python(plat_specific)
or _get_python_inc_from_config(plat_specific, spec_prefix)
or _get_python_inc_posix_prefix(prefix)
)
def _get_python_inc_posix_python(plat_specific):
if not python_build:
return
if plat_specific:
return _sys_home or project_base
incdir = os.path.join(get_config_var('srcdir'), 'Include')
return os.path.normpath(incdir)
def _get_python_inc_from_config(plat_specific, spec_prefix):
if spec_prefix is None:
return get_config_var('CONF' * plat_specific + 'INCLUDEPY')
def _get_python_inc_posix_prefix(prefix):
implementation = 'pypy' if IS_PYPY else 'python'
python_dir = implementation + get_python_version() + build_flags
return os.path.join(prefix, "include", python_dir)
def _get_python_inc_nt(prefix, spec_prefix, plat_specific):
if python_build:
return (
os.path.join(prefix, "include")
+ os.path.pathsep
+ os.path.join(prefix, "PC")
)
return os.path.join(prefix, "include")
def _posix_lib(standard_lib, libpython, early_prefix, prefix):
if standard_lib:
return libpython
else:
return os.path.join(libpython, "site-packages")
def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
if IS_PYPY and sys.version_info < (3, 8):
if prefix is None:
prefix = PREFIX
if standard_lib:
return os.path.join(prefix, "lib-python", sys.version[0])
return os.path.join(prefix, 'site-packages')
early_prefix = prefix
if prefix is None:
if standard_lib:
prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
else:
prefix = plat_specific and EXEC_PREFIX or PREFIX
if os.name == "posix":
if plat_specific or standard_lib:
libdir = getattr(sys, "platlibdir", "lib")
else:
libdir = "lib"
implementation = 'pypy' if IS_PYPY else 'python'
libpython = os.path.join(prefix, libdir, implementation + get_python_version())
return _posix_lib(standard_lib, libpython, early_prefix, prefix)
elif os.name == "nt":
if standard_lib:
return os.path.join(prefix, "Lib")
else:
return os.path.join(prefix, "Lib", "site-packages")
else:
raise DistutilsPlatformError(
"I don't know where Python installs its library "
"on platform '%s'" % os.name
)
def customize_compiler(compiler):
if compiler.compiler_type == "unix":
if sys.platform == "darwin":
global _config_vars
if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
import _osx_support
_osx_support.customize_compiler(_config_vars)
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
(
cc,
cxx,
cflags,
ccshared,
ldshared,
shlib_suffix,
ar,
ar_flags,
) = get_config_vars(
'CC',
'CXX',
'CFLAGS',
'CCSHARED',
'LDSHARED',
'SHLIB_SUFFIX',
'AR',
'ARFLAGS',
)
if 'CC' in os.environ:
newcc = os.environ['CC']
if 'LDSHARED' not in os.environ and ldshared.startswith(cc):
ldshared = newcc + ldshared[len(cc) :]
cc = newcc
if 'CXX' in os.environ:
cxx = os.environ['CXX']
if 'LDSHARED' in os.environ:
ldshared = os.environ['LDSHARED']
if 'CPP' in os.environ:
cpp = os.environ['CPP']
else:
cpp = cc + " -E" if 'LDFLAGS' in os.environ:
ldshared = ldshared + ' ' + os.environ['LDFLAGS']
if 'CFLAGS' in os.environ:
cflags = cflags + ' ' + os.environ['CFLAGS']
ldshared = ldshared + ' ' + os.environ['CFLAGS']
if 'CPPFLAGS' in os.environ:
cpp = cpp + ' ' + os.environ['CPPFLAGS']
cflags = cflags + ' ' + os.environ['CPPFLAGS']
ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
if 'AR' in os.environ:
ar = os.environ['AR']
if 'ARFLAGS' in os.environ:
archiver = ar + ' ' + os.environ['ARFLAGS']
else:
archiver = ar + ' ' + ar_flags
cc_cmd = cc + ' ' + cflags
compiler.set_executables(
preprocessor=cpp,
compiler=cc_cmd,
compiler_so=cc_cmd + ' ' + ccshared,
compiler_cxx=cxx,
linker_so=ldshared,
linker_exe=cc,
archiver=archiver,
)
if 'RANLIB' in os.environ and compiler.executables.get('ranlib', None):
compiler.set_executables(ranlib=os.environ['RANLIB'])
compiler.shared_lib_extension = shlib_suffix
def get_config_h_filename():
if python_build:
if os.name == "nt":
inc_dir = os.path.join(_sys_home or project_base, "PC")
else:
inc_dir = _sys_home or project_base
return os.path.join(inc_dir, 'pyconfig.h')
else:
return sysconfig.get_config_h_filename()
def get_makefile_filename():
return sysconfig.get_makefile_filename()
def parse_config_h(fp, g=None):
return sysconfig.parse_config_h(fp, vars=g)
_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
def parse_makefile(fn, g=None):
from distutils.text_file import TextFile
fp = TextFile(
fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape"
)
if g is None:
g = {}
done = {}
notdone = {}
while True:
line = fp.readline()
if line is None: break
m = _variable_rx.match(line)
if m:
n, v = m.group(1, 2)
v = v.strip()
tmpv = v.replace('$$', '')
if "$" in tmpv:
notdone[n] = v
else:
try:
v = int(v)
except ValueError:
done[n] = v.replace('$$', '$')
else:
done[n] = v
renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
while notdone:
for name in list(notdone):
value = notdone[name]
m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
if m:
n = m.group(1)
found = True
if n in done:
item = str(done[n])
elif n in notdone:
found = False
elif n in os.environ:
item = os.environ[n]
elif n in renamed_variables:
if name.startswith('PY_') and name[3:] in renamed_variables:
item = ""
elif 'PY_' + n in notdone:
found = False
else:
item = str(done['PY_' + n])
else:
done[n] = item = ""
if found:
after = value[m.end() :]
value = value[: m.start()] + item + after
if "$" in after:
notdone[name] = value
else:
try:
value = int(value)
except ValueError:
done[name] = value.strip()
else:
done[name] = value
del notdone[name]
if name.startswith('PY_') and name[3:] in renamed_variables:
name = name[3:]
if name not in done:
done[name] = value
else:
del notdone[name]
fp.close()
for k, v in done.items():
if isinstance(v, str):
done[k] = v.strip()
g.update(done)
return g
def expand_makefile_vars(s, vars):
while True:
m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
if m:
(beg, end) = m.span()
s = s[0:beg] + vars.get(m.group(1)) + s[end:]
else:
break
return s
_config_vars = None
def get_config_vars(*args):
global _config_vars
if _config_vars is None:
_config_vars = sysconfig.get_config_vars().copy()
py39compat.add_ext_suffix(_config_vars)
if args:
vals = []
for name in args:
vals.append(_config_vars.get(name))
return vals
else:
return _config_vars
def get_config_var(name):
if name == 'SO':
import warnings
warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
return get_config_vars().get(name)