Debian Codegraph @

lib-python/3/distutils/sysconfig_cpython.py

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