logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

sysconfig.py (12550B)


  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. import _imp
  11. import os
  12. import re
  13. import sys
  14. import warnings
  15. from functools import partial
  16. from .errors import DistutilsPlatformError
  17. from sysconfig import (
  18. _PREFIX as PREFIX,
  19. _BASE_PREFIX as BASE_PREFIX,
  20. _EXEC_PREFIX as EXEC_PREFIX,
  21. _BASE_EXEC_PREFIX as BASE_EXEC_PREFIX,
  22. _PROJECT_BASE as project_base,
  23. _PYTHON_BUILD as python_build,
  24. _init_posix as sysconfig_init_posix,
  25. parse_config_h as sysconfig_parse_config_h,
  26. _init_non_posix,
  27. _is_python_source_dir,
  28. _sys_home,
  29. _variable_rx,
  30. _findvar1_rx,
  31. _findvar2_rx,
  32. expand_makefile_vars,
  33. is_python_build,
  34. get_config_h_filename,
  35. get_config_var,
  36. get_config_vars,
  37. get_makefile_filename,
  38. get_python_version,
  39. )
  40. # This is better than
  41. # from sysconfig import _CONFIG_VARS as _config_vars
  42. # because it makes sure that the global dictionary is initialized
  43. # which might not be true in the time of import.
  44. _config_vars = get_config_vars()
  45. if os.name == "nt":
  46. from sysconfig import _fix_pcbuild
  47. warnings.warn(
  48. 'The distutils.sysconfig module is deprecated, use sysconfig instead',
  49. DeprecationWarning,
  50. stacklevel=2
  51. )
  52. # Following functions are the same as in sysconfig but with different API
  53. def parse_config_h(fp, g=None):
  54. return sysconfig_parse_config_h(fp, vars=g)
  55. _python_build = partial(is_python_build, check_home=True)
  56. _init_posix = partial(sysconfig_init_posix, _config_vars)
  57. _init_nt = partial(_init_non_posix, _config_vars)
  58. # Similar function is also implemented in sysconfig as _parse_makefile
  59. # but without the parsing capabilities of distutils.text_file.TextFile.
  60. def parse_makefile(fn, g=None):
  61. """Parse a Makefile-style file.
  62. A dictionary containing name/value pairs is returned. If an
  63. optional dictionary is passed in as the second argument, it is
  64. used instead of a new dictionary.
  65. """
  66. from distutils.text_file import TextFile
  67. fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
  68. if g is None:
  69. g = {}
  70. done = {}
  71. notdone = {}
  72. while True:
  73. line = fp.readline()
  74. if line is None: # eof
  75. break
  76. m = re.match(_variable_rx, line)
  77. if m:
  78. n, v = m.group(1, 2)
  79. v = v.strip()
  80. # `$$' is a literal `$' in make
  81. tmpv = v.replace('$$', '')
  82. if "$" in tmpv:
  83. notdone[n] = v
  84. else:
  85. try:
  86. v = int(v)
  87. except ValueError:
  88. # insert literal `$'
  89. done[n] = v.replace('$$', '$')
  90. else:
  91. done[n] = v
  92. # Variables with a 'PY_' prefix in the makefile. These need to
  93. # be made available without that prefix through sysconfig.
  94. # Special care is needed to ensure that variable expansion works, even
  95. # if the expansion uses the name without a prefix.
  96. renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
  97. # do variable interpolation here
  98. while notdone:
  99. for name in list(notdone):
  100. value = notdone[name]
  101. m = re.search(_findvar1_rx, value) or re.search(_findvar2_rx, value)
  102. if m:
  103. n = m.group(1)
  104. found = True
  105. if n in done:
  106. item = str(done[n])
  107. elif n in notdone:
  108. # get it on a subsequent round
  109. found = False
  110. elif n in os.environ:
  111. # do it like make: fall back to environment
  112. item = os.environ[n]
  113. elif n in renamed_variables:
  114. if name.startswith('PY_') and name[3:] in renamed_variables:
  115. item = ""
  116. elif 'PY_' + n in notdone:
  117. found = False
  118. else:
  119. item = str(done['PY_' + n])
  120. else:
  121. done[n] = item = ""
  122. if found:
  123. after = value[m.end():]
  124. value = value[:m.start()] + item + after
  125. if "$" in after:
  126. notdone[name] = value
  127. else:
  128. try: value = int(value)
  129. except ValueError:
  130. done[name] = value.strip()
  131. else:
  132. done[name] = value
  133. del notdone[name]
  134. if name.startswith('PY_') \
  135. and name[3:] in renamed_variables:
  136. name = name[3:]
  137. if name not in done:
  138. done[name] = value
  139. else:
  140. # bogus variable reference; just drop it since we can't deal
  141. del notdone[name]
  142. fp.close()
  143. # strip spurious spaces
  144. for k, v in done.items():
  145. if isinstance(v, str):
  146. done[k] = v.strip()
  147. # save the results in the global dictionary
  148. g.update(done)
  149. return g
  150. # Following functions are deprecated together with this module and they
  151. # have no direct replacement
  152. # Calculate the build qualifier flags if they are defined. Adding the flags
  153. # to the include and lib directories only makes sense for an installation, not
  154. # an in-source build.
  155. build_flags = ''
  156. try:
  157. if not python_build:
  158. build_flags = sys.abiflags
  159. except AttributeError:
  160. # It's not a configure-based build, so the sys module doesn't have
  161. # this attribute, which is fine.
  162. pass
  163. def customize_compiler(compiler):
  164. """Do any platform-specific customization of a CCompiler instance.
  165. Mainly needed on Unix, so we can plug in the information that
  166. varies across Unices and is stored in Python's Makefile.
  167. """
  168. if compiler.compiler_type == "unix":
  169. if sys.platform == "darwin":
  170. # Perform first-time customization of compiler-related
  171. # config vars on OS X now that we know we need a compiler.
  172. # This is primarily to support Pythons from binary
  173. # installers. The kind and paths to build tools on
  174. # the user system may vary significantly from the system
  175. # that Python itself was built on. Also the user OS
  176. # version and build tools may not support the same set
  177. # of CPU architectures for universal builds.
  178. if not _config_vars.get('CUSTOMIZED_OSX_COMPILER'):
  179. import _osx_support
  180. _osx_support.customize_compiler(_config_vars)
  181. _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
  182. (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
  183. get_config_vars('CC', 'CXX', 'CFLAGS',
  184. 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
  185. if 'CC' in os.environ:
  186. newcc = os.environ['CC']
  187. if (sys.platform == 'darwin'
  188. and 'LDSHARED' not in os.environ
  189. and ldshared.startswith(cc)):
  190. # On OS X, if CC is overridden, use that as the default
  191. # command for LDSHARED as well
  192. ldshared = newcc + ldshared[len(cc):]
  193. cc = newcc
  194. if 'CXX' in os.environ:
  195. cxx = os.environ['CXX']
  196. if 'LDSHARED' in os.environ:
  197. ldshared = os.environ['LDSHARED']
  198. if 'CPP' in os.environ:
  199. cpp = os.environ['CPP']
  200. else:
  201. cpp = cc + " -E" # not always
  202. if 'LDFLAGS' in os.environ:
  203. ldshared = ldshared + ' ' + os.environ['LDFLAGS']
  204. if 'CFLAGS' in os.environ:
  205. cflags = cflags + ' ' + os.environ['CFLAGS']
  206. ldshared = ldshared + ' ' + os.environ['CFLAGS']
  207. if 'CPPFLAGS' in os.environ:
  208. cpp = cpp + ' ' + os.environ['CPPFLAGS']
  209. cflags = cflags + ' ' + os.environ['CPPFLAGS']
  210. ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
  211. if 'AR' in os.environ:
  212. ar = os.environ['AR']
  213. if 'ARFLAGS' in os.environ:
  214. archiver = ar + ' ' + os.environ['ARFLAGS']
  215. else:
  216. archiver = ar + ' ' + ar_flags
  217. cc_cmd = cc + ' ' + cflags
  218. compiler.set_executables(
  219. preprocessor=cpp,
  220. compiler=cc_cmd,
  221. compiler_so=cc_cmd + ' ' + ccshared,
  222. compiler_cxx=cxx,
  223. linker_so=ldshared,
  224. linker_exe=cc,
  225. archiver=archiver)
  226. compiler.shared_lib_extension = shlib_suffix
  227. def get_python_inc(plat_specific=0, prefix=None):
  228. """Return the directory containing installed Python header files.
  229. If 'plat_specific' is false (the default), this is the path to the
  230. non-platform-specific header files, i.e. Python.h and so on;
  231. otherwise, this is the path to platform-specific header files
  232. (namely pyconfig.h).
  233. If 'prefix' is supplied, use it instead of sys.base_prefix or
  234. sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
  235. """
  236. if prefix is None:
  237. prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
  238. if os.name == "posix":
  239. if python_build:
  240. # Assume the executable is in the build directory. The
  241. # pyconfig.h file should be in the same directory. Since
  242. # the build directory may not be the source directory, we
  243. # must use "srcdir" from the makefile to find the "Include"
  244. # directory.
  245. if plat_specific:
  246. return _sys_home or project_base
  247. else:
  248. incdir = os.path.join(get_config_var('srcdir'), 'Include')
  249. return os.path.normpath(incdir)
  250. python_dir = 'python' + get_python_version() + build_flags
  251. return os.path.join(prefix, "include", python_dir)
  252. elif os.name == "nt":
  253. if python_build:
  254. # Include both the include and PC dir to ensure we can find
  255. # pyconfig.h
  256. return (os.path.join(prefix, "include") + os.path.pathsep +
  257. os.path.join(prefix, "PC"))
  258. return os.path.join(prefix, "include")
  259. else:
  260. raise DistutilsPlatformError(
  261. "I don't know where Python installs its C header files "
  262. "on platform '%s'" % os.name)
  263. def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
  264. """Return the directory containing the Python library (standard or
  265. site additions).
  266. If 'plat_specific' is true, return the directory containing
  267. platform-specific modules, i.e. any module from a non-pure-Python
  268. module distribution; otherwise, return the platform-shared library
  269. directory. If 'standard_lib' is true, return the directory
  270. containing standard Python library modules; otherwise, return the
  271. directory for site-specific modules.
  272. If 'prefix' is supplied, use it instead of sys.base_prefix or
  273. sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
  274. """
  275. if prefix is None:
  276. if standard_lib:
  277. prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
  278. else:
  279. prefix = plat_specific and EXEC_PREFIX or PREFIX
  280. if os.name == "posix":
  281. if plat_specific or standard_lib:
  282. # Platform-specific modules (any module from a non-pure-Python
  283. # module distribution) or standard Python library modules.
  284. libdir = sys.platlibdir
  285. else:
  286. # Pure Python
  287. libdir = "lib"
  288. libpython = os.path.join(prefix, libdir,
  289. "python" + get_python_version())
  290. if standard_lib:
  291. return libpython
  292. else:
  293. return os.path.join(libpython, "site-packages")
  294. elif os.name == "nt":
  295. if standard_lib:
  296. return os.path.join(prefix, "Lib")
  297. else:
  298. return os.path.join(prefix, "Lib", "site-packages")
  299. else:
  300. raise DistutilsPlatformError(
  301. "I don't know where Python installs its library "
  302. "on platform '%s'" % os.name)