sysconfig.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. """Provide access to Python's configuration information. The specific
  2. configuration variables available depend heavily on the platform and
  3. configuration. The values may be retrieved using
  4. get_config_var(name), and the list of variables is available via
  5. get_config_vars().keys(). Additional convenience functions are also
  6. available.
  7. Written by: Fred L. Drake, Jr.
  8. Email: <fdrake@acm.org>
  9. """
  10. from __future__ import annotations
  11. import functools
  12. import os
  13. import pathlib
  14. import re
  15. import sys
  16. import sysconfig
  17. from typing import TYPE_CHECKING, Literal, overload
  18. from jaraco.functools import pass_none
  19. from .ccompiler import CCompiler
  20. from .compat import py39
  21. from .errors import DistutilsPlatformError
  22. from .util import is_mingw
  23. if TYPE_CHECKING:
  24. from typing_extensions import deprecated
  25. else:
  26. def deprecated(message):
  27. return lambda fn: fn
  28. IS_PYPY = '__pypy__' in sys.builtin_module_names
  29. # These are needed in a couple of spots, so just compute them once.
  30. PREFIX = os.path.normpath(sys.prefix)
  31. EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
  32. BASE_PREFIX = os.path.normpath(sys.base_prefix)
  33. BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
  34. # Path to the base directory of the project. On Windows the binary may
  35. # live in project/PCbuild/win32 or project/PCbuild/amd64.
  36. # set for cross builds
  37. if "_PYTHON_PROJECT_BASE" in os.environ:
  38. project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
  39. else:
  40. if sys.executable:
  41. project_base = os.path.dirname(os.path.abspath(sys.executable))
  42. else:
  43. # sys.executable can be empty if argv[0] has been changed and Python is
  44. # unable to retrieve the real program name
  45. project_base = os.getcwd()
  46. def _is_python_source_dir(d):
  47. """
  48. Return True if the target directory appears to point to an
  49. un-installed Python.
  50. """
  51. modules = pathlib.Path(d).joinpath('Modules')
  52. return any(modules.joinpath(fn).is_file() for fn in ('Setup', 'Setup.local'))
  53. _sys_home = getattr(sys, '_home', None)
  54. def _is_parent(dir_a, dir_b):
  55. """
  56. Return True if a is a parent of b.
  57. """
  58. return os.path.normcase(dir_a).startswith(os.path.normcase(dir_b))
  59. if os.name == 'nt':
  60. @pass_none
  61. def _fix_pcbuild(d):
  62. # In a venv, sys._home will be inside BASE_PREFIX rather than PREFIX.
  63. prefixes = PREFIX, BASE_PREFIX
  64. matched = (
  65. prefix
  66. for prefix in prefixes
  67. if _is_parent(d, os.path.join(prefix, "PCbuild"))
  68. )
  69. return next(matched, d)
  70. project_base = _fix_pcbuild(project_base)
  71. _sys_home = _fix_pcbuild(_sys_home)
  72. def _python_build():
  73. if _sys_home:
  74. return _is_python_source_dir(_sys_home)
  75. return _is_python_source_dir(project_base)
  76. python_build = _python_build()
  77. # Calculate the build qualifier flags if they are defined. Adding the flags
  78. # to the include and lib directories only makes sense for an installation, not
  79. # an in-source build.
  80. build_flags = ''
  81. try:
  82. if not python_build:
  83. build_flags = sys.abiflags
  84. except AttributeError:
  85. # It's not a configure-based build, so the sys module doesn't have
  86. # this attribute, which is fine.
  87. pass
  88. def get_python_version():
  89. """Return a string containing the major and minor Python version,
  90. leaving off the patchlevel. Sample return values could be '1.5'
  91. or '2.2'.
  92. """
  93. return f'{sys.version_info.major}.{sys.version_info.minor}'
  94. def get_python_inc(plat_specific: bool = False, prefix: str | None = None) -> str:
  95. """Return the directory containing installed Python header files.
  96. If 'plat_specific' is false (the default), this is the path to the
  97. non-platform-specific header files, i.e. Python.h and so on;
  98. otherwise, this is the path to platform-specific header files
  99. (namely pyconfig.h).
  100. If 'prefix' is supplied, use it instead of sys.base_prefix or
  101. sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
  102. """
  103. default_prefix = BASE_EXEC_PREFIX if plat_specific else BASE_PREFIX
  104. resolved_prefix = prefix if prefix is not None else default_prefix
  105. # MinGW imitates posix like layout, but os.name != posix
  106. os_name = "posix" if is_mingw() else os.name
  107. try:
  108. getter = globals()[f'_get_python_inc_{os_name}']
  109. except KeyError:
  110. raise DistutilsPlatformError(
  111. "I don't know where Python installs its C header files "
  112. f"on platform '{os.name}'"
  113. )
  114. return getter(resolved_prefix, prefix, plat_specific)
  115. @pass_none
  116. def _extant(path):
  117. """
  118. Replace path with None if it doesn't exist.
  119. """
  120. return path if os.path.exists(path) else None
  121. def _get_python_inc_posix(prefix, spec_prefix, plat_specific):
  122. return (
  123. _get_python_inc_posix_python(plat_specific)
  124. or _extant(_get_python_inc_from_config(plat_specific, spec_prefix))
  125. or _get_python_inc_posix_prefix(prefix)
  126. )
  127. def _get_python_inc_posix_python(plat_specific):
  128. """
  129. Assume the executable is in the build directory. The
  130. pyconfig.h file should be in the same directory. Since
  131. the build directory may not be the source directory,
  132. use "srcdir" from the makefile to find the "Include"
  133. directory.
  134. """
  135. if not python_build:
  136. return
  137. if plat_specific:
  138. return _sys_home or project_base
  139. incdir = os.path.join(get_config_var('srcdir'), 'Include')
  140. return os.path.normpath(incdir)
  141. def _get_python_inc_from_config(plat_specific, spec_prefix):
  142. """
  143. If no prefix was explicitly specified, provide the include
  144. directory from the config vars. Useful when
  145. cross-compiling, since the config vars may come from
  146. the host
  147. platform Python installation, while the current Python
  148. executable is from the build platform installation.
  149. >>> monkeypatch = getfixture('monkeypatch')
  150. >>> gpifc = _get_python_inc_from_config
  151. >>> monkeypatch.setitem(gpifc.__globals__, 'get_config_var', str.lower)
  152. >>> gpifc(False, '/usr/bin/')
  153. >>> gpifc(False, '')
  154. >>> gpifc(False, None)
  155. 'includepy'
  156. >>> gpifc(True, None)
  157. 'confincludepy'
  158. """
  159. if spec_prefix is None:
  160. return get_config_var('CONF' * plat_specific + 'INCLUDEPY')
  161. def _get_python_inc_posix_prefix(prefix):
  162. implementation = 'pypy' if IS_PYPY else 'python'
  163. python_dir = implementation + get_python_version() + build_flags
  164. return os.path.join(prefix, "include", python_dir)
  165. def _get_python_inc_nt(prefix, spec_prefix, plat_specific):
  166. if python_build:
  167. # Include both include dirs to ensure we can find pyconfig.h
  168. return (
  169. os.path.join(prefix, "include")
  170. + os.path.pathsep
  171. + os.path.dirname(sysconfig.get_config_h_filename())
  172. )
  173. return os.path.join(prefix, "include")
  174. # allow this behavior to be monkey-patched. Ref pypa/distutils#2.
  175. def _posix_lib(standard_lib, libpython, early_prefix, prefix):
  176. if standard_lib:
  177. return libpython
  178. else:
  179. return os.path.join(libpython, "site-packages")
  180. def get_python_lib(
  181. plat_specific: bool = False, standard_lib: bool = False, prefix: str | None = None
  182. ) -> str:
  183. """Return the directory containing the Python library (standard or
  184. site additions).
  185. If 'plat_specific' is true, return the directory containing
  186. platform-specific modules, i.e. any module from a non-pure-Python
  187. module distribution; otherwise, return the platform-shared library
  188. directory. If 'standard_lib' is true, return the directory
  189. containing standard Python library modules; otherwise, return the
  190. directory for site-specific modules.
  191. If 'prefix' is supplied, use it instead of sys.base_prefix or
  192. sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
  193. """
  194. early_prefix = prefix
  195. if prefix is None:
  196. if standard_lib:
  197. prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
  198. else:
  199. prefix = plat_specific and EXEC_PREFIX or PREFIX
  200. if os.name == "posix" or is_mingw():
  201. if plat_specific or standard_lib:
  202. # Platform-specific modules (any module from a non-pure-Python
  203. # module distribution) or standard Python library modules.
  204. libdir = getattr(sys, "platlibdir", "lib")
  205. else:
  206. # Pure Python
  207. libdir = "lib"
  208. implementation = 'pypy' if IS_PYPY else 'python'
  209. libpython = os.path.join(prefix, libdir, implementation + get_python_version())
  210. return _posix_lib(standard_lib, libpython, early_prefix, prefix)
  211. elif os.name == "nt":
  212. if standard_lib:
  213. return os.path.join(prefix, "Lib")
  214. else:
  215. return os.path.join(prefix, "Lib", "site-packages")
  216. else:
  217. raise DistutilsPlatformError(
  218. f"I don't know where Python installs its library on platform '{os.name}'"
  219. )
  220. @functools.lru_cache
  221. def _customize_macos():
  222. """
  223. Perform first-time customization of compiler-related
  224. config vars on macOS. Use after a compiler is known
  225. to be needed. This customization exists primarily to support Pythons
  226. from binary installers. The kind and paths to build tools on
  227. the user system may vary significantly from the system
  228. that Python itself was built on. Also the user OS
  229. version and build tools may not support the same set
  230. of CPU architectures for universal builds.
  231. """
  232. sys.platform == "darwin" and __import__('_osx_support').customize_compiler(
  233. get_config_vars()
  234. )
  235. def customize_compiler(compiler: CCompiler) -> None:
  236. """Do any platform-specific customization of a CCompiler instance.
  237. Mainly needed on Unix, so we can plug in the information that
  238. varies across Unices and is stored in Python's Makefile.
  239. """
  240. if compiler.compiler_type in ["unix", "cygwin"] or (
  241. compiler.compiler_type == "mingw32" and is_mingw()
  242. ):
  243. _customize_macos()
  244. (
  245. cc,
  246. cxx,
  247. cflags,
  248. ccshared,
  249. ldshared,
  250. ldcxxshared,
  251. shlib_suffix,
  252. ar,
  253. ar_flags,
  254. ) = get_config_vars(
  255. 'CC',
  256. 'CXX',
  257. 'CFLAGS',
  258. 'CCSHARED',
  259. 'LDSHARED',
  260. 'LDCXXSHARED',
  261. 'SHLIB_SUFFIX',
  262. 'AR',
  263. 'ARFLAGS',
  264. )
  265. cxxflags = cflags
  266. if 'CC' in os.environ:
  267. newcc = os.environ['CC']
  268. if 'LDSHARED' not in os.environ and ldshared.startswith(cc):
  269. # If CC is overridden, use that as the default
  270. # command for LDSHARED as well
  271. ldshared = newcc + ldshared[len(cc) :]
  272. cc = newcc
  273. cxx = os.environ.get('CXX', cxx)
  274. ldshared = os.environ.get('LDSHARED', ldshared)
  275. ldcxxshared = os.environ.get('LDCXXSHARED', ldcxxshared)
  276. cpp = os.environ.get(
  277. 'CPP',
  278. cc + " -E", # not always
  279. )
  280. ldshared = _add_flags(ldshared, 'LD')
  281. ldcxxshared = _add_flags(ldcxxshared, 'LD')
  282. cflags = os.environ.get('CFLAGS', cflags)
  283. ldshared = _add_flags(ldshared, 'C')
  284. cxxflags = os.environ.get('CXXFLAGS', cxxflags)
  285. ldcxxshared = _add_flags(ldcxxshared, 'CXX')
  286. cpp = _add_flags(cpp, 'CPP')
  287. cflags = _add_flags(cflags, 'CPP')
  288. cxxflags = _add_flags(cxxflags, 'CPP')
  289. ldshared = _add_flags(ldshared, 'CPP')
  290. ldcxxshared = _add_flags(ldcxxshared, 'CPP')
  291. ar = os.environ.get('AR', ar)
  292. archiver = ar + ' ' + os.environ.get('ARFLAGS', ar_flags)
  293. cc_cmd = cc + ' ' + cflags
  294. cxx_cmd = cxx + ' ' + cxxflags
  295. compiler.set_executables(
  296. preprocessor=cpp,
  297. compiler=cc_cmd,
  298. compiler_so=cc_cmd + ' ' + ccshared,
  299. compiler_cxx=cxx_cmd,
  300. compiler_so_cxx=cxx_cmd + ' ' + ccshared,
  301. linker_so=ldshared,
  302. linker_so_cxx=ldcxxshared,
  303. linker_exe=cc,
  304. linker_exe_cxx=cxx,
  305. archiver=archiver,
  306. )
  307. if 'RANLIB' in os.environ and compiler.executables.get('ranlib', None):
  308. compiler.set_executables(ranlib=os.environ['RANLIB'])
  309. compiler.shared_lib_extension = shlib_suffix
  310. def get_config_h_filename() -> str:
  311. """Return full pathname of installed pyconfig.h file."""
  312. return sysconfig.get_config_h_filename()
  313. def get_makefile_filename() -> str:
  314. """Return full pathname of installed Makefile from the Python build."""
  315. return sysconfig.get_makefile_filename()
  316. def parse_config_h(fp, g=None):
  317. """Parse a config.h-style file.
  318. A dictionary containing name/value pairs is returned. If an
  319. optional dictionary is passed in as the second argument, it is
  320. used instead of a new dictionary.
  321. """
  322. return sysconfig.parse_config_h(fp, vars=g)
  323. # Regexes needed for parsing Makefile (and similar syntaxes,
  324. # like old-style Setup files).
  325. _variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
  326. _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
  327. _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  328. def parse_makefile(fn, g=None): # noqa: C901
  329. """Parse a Makefile-style file.
  330. A dictionary containing name/value pairs is returned. If an
  331. optional dictionary is passed in as the second argument, it is
  332. used instead of a new dictionary.
  333. """
  334. from distutils.text_file import TextFile
  335. fp = TextFile(
  336. fn,
  337. strip_comments=True,
  338. skip_blanks=True,
  339. join_lines=True,
  340. errors="surrogateescape",
  341. )
  342. if g is None:
  343. g = {}
  344. done = {}
  345. notdone = {}
  346. while True:
  347. line = fp.readline()
  348. if line is None: # eof
  349. break
  350. m = _variable_rx.match(line)
  351. if m:
  352. n, v = m.group(1, 2)
  353. v = v.strip()
  354. # `$$' is a literal `$' in make
  355. tmpv = v.replace('$$', '')
  356. if "$" in tmpv:
  357. notdone[n] = v
  358. else:
  359. try:
  360. v = int(v)
  361. except ValueError:
  362. # insert literal `$'
  363. done[n] = v.replace('$$', '$')
  364. else:
  365. done[n] = v
  366. # Variables with a 'PY_' prefix in the makefile. These need to
  367. # be made available without that prefix through sysconfig.
  368. # Special care is needed to ensure that variable expansion works, even
  369. # if the expansion uses the name without a prefix.
  370. renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
  371. # do variable interpolation here
  372. while notdone:
  373. for name in list(notdone):
  374. value = notdone[name]
  375. m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
  376. if m:
  377. n = m.group(1)
  378. found = True
  379. if n in done:
  380. item = str(done[n])
  381. elif n in notdone:
  382. # get it on a subsequent round
  383. found = False
  384. elif n in os.environ:
  385. # do it like make: fall back to environment
  386. item = os.environ[n]
  387. elif n in renamed_variables:
  388. if name.startswith('PY_') and name[3:] in renamed_variables:
  389. item = ""
  390. elif 'PY_' + n in notdone:
  391. found = False
  392. else:
  393. item = str(done['PY_' + n])
  394. else:
  395. done[n] = item = ""
  396. if found:
  397. after = value[m.end() :]
  398. value = value[: m.start()] + item + after
  399. if "$" in after:
  400. notdone[name] = value
  401. else:
  402. try:
  403. value = int(value)
  404. except ValueError:
  405. done[name] = value.strip()
  406. else:
  407. done[name] = value
  408. del notdone[name]
  409. if name.startswith('PY_') and name[3:] in renamed_variables:
  410. name = name[3:]
  411. if name not in done:
  412. done[name] = value
  413. else:
  414. # bogus variable reference; just drop it since we can't deal
  415. del notdone[name]
  416. fp.close()
  417. # strip spurious spaces
  418. for k, v in done.items():
  419. if isinstance(v, str):
  420. done[k] = v.strip()
  421. # save the results in the global dictionary
  422. g.update(done)
  423. return g
  424. def expand_makefile_vars(s, vars):
  425. """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
  426. 'string' according to 'vars' (a dictionary mapping variable names to
  427. values). Variables not present in 'vars' are silently expanded to the
  428. empty string. The variable values in 'vars' should not contain further
  429. variable expansions; if 'vars' is the output of 'parse_makefile()',
  430. you're fine. Returns a variable-expanded version of 's'.
  431. """
  432. # This algorithm does multiple expansion, so if vars['foo'] contains
  433. # "${bar}", it will expand ${foo} to ${bar}, and then expand
  434. # ${bar}... and so forth. This is fine as long as 'vars' comes from
  435. # 'parse_makefile()', which takes care of such expansions eagerly,
  436. # according to make's variable expansion semantics.
  437. while True:
  438. m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
  439. if m:
  440. (beg, end) = m.span()
  441. s = s[0:beg] + vars.get(m.group(1)) + s[end:]
  442. else:
  443. break
  444. return s
  445. _config_vars = None
  446. @overload
  447. def get_config_vars() -> dict[str, str | int]: ...
  448. @overload
  449. def get_config_vars(arg: str, /, *args: str) -> list[str | int]: ...
  450. def get_config_vars(*args: str) -> list[str | int] | dict[str, str | int]:
  451. """With no arguments, return a dictionary of all configuration
  452. variables relevant for the current platform. Generally this includes
  453. everything needed to build extensions and install both pure modules and
  454. extensions. On Unix, this means every variable defined in Python's
  455. installed Makefile; on Windows it's a much smaller set.
  456. With arguments, return a list of values that result from looking up
  457. each argument in the configuration variable dictionary.
  458. """
  459. global _config_vars
  460. if _config_vars is None:
  461. _config_vars = sysconfig.get_config_vars().copy()
  462. py39.add_ext_suffix(_config_vars)
  463. return [_config_vars.get(name) for name in args] if args else _config_vars
  464. @overload
  465. @deprecated(
  466. "SO is deprecated, use EXT_SUFFIX. Support will be removed when this module is synchronized with stdlib Python 3.11"
  467. )
  468. def get_config_var(name: Literal["SO"]) -> int | str | None: ...
  469. @overload
  470. def get_config_var(name: str) -> int | str | None: ...
  471. def get_config_var(name: str) -> int | str | None:
  472. """Return the value of a single variable using the dictionary
  473. returned by 'get_config_vars()'. Equivalent to
  474. get_config_vars().get(name)
  475. """
  476. if name == 'SO':
  477. import warnings
  478. warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
  479. return get_config_vars().get(name)
  480. @pass_none
  481. def _add_flags(value: str, type: str) -> str:
  482. """
  483. Add any flags from the environment for the given type.
  484. type is the prefix to FLAGS in the environment key (e.g. "C" for "CFLAGS").
  485. """
  486. flags = os.environ.get(f'{type}FLAGS')
  487. return f'{value} {flags}' if flags else value