ยปCore Development>Code coverage>Lib/distutils/sysconfig.py

Python code coverage for Lib/distutils/sysconfig.py

#countcontent
1n/a"""Provide access to Python's configuration information. The specific
2n/aconfiguration variables available depend heavily on the platform and
3n/aconfiguration. The values may be retrieved using
4n/aget_config_var(name), and the list of variables is available via
5n/aget_config_vars().keys(). Additional convenience functions are also
6n/aavailable.
7n/a
8n/aWritten by: Fred L. Drake, Jr.
9n/aEmail: <fdrake@acm.org>
10n/a"""
11n/a
12n/aimport _imp
13n/aimport os
14n/aimport re
15n/aimport sys
16n/a
17n/afrom .errors import DistutilsPlatformError
18n/a
19n/a# These are needed in a couple of spots, so just compute them once.
20n/aPREFIX = os.path.normpath(sys.prefix)
21n/aEXEC_PREFIX = os.path.normpath(sys.exec_prefix)
22n/aBASE_PREFIX = os.path.normpath(sys.base_prefix)
23n/aBASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
24n/a
25n/a# Path to the base directory of the project. On Windows the binary may
26n/a# live in project/PCBuild/win32 or project/PCBuild/amd64.
27n/a# set for cross builds
28n/aif "_PYTHON_PROJECT_BASE" in os.environ:
29n/a project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
30n/aelse:
31n/a project_base = os.path.dirname(os.path.abspath(sys.executable))
32n/aif (os.name == 'nt' and
33n/a project_base.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
34n/a project_base = os.path.dirname(os.path.dirname(project_base))
35n/a
36n/a# python_build: (Boolean) if true, we're either building Python or
37n/a# building an extension with an un-installed Python, so we use
38n/a# different (hard-wired) directories.
39n/a# Setup.local is available for Makefile builds including VPATH builds,
40n/a# Setup.dist is available on Windows
41n/adef _is_python_source_dir(d):
42n/a for fn in ("Setup.dist", "Setup.local"):
43n/a if os.path.isfile(os.path.join(d, "Modules", fn)):
44n/a return True
45n/a return False
46n/a_sys_home = getattr(sys, '_home', None)
47n/aif (_sys_home and os.name == 'nt' and
48n/a _sys_home.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
49n/a _sys_home = os.path.dirname(os.path.dirname(_sys_home))
50n/adef _python_build():
51n/a if _sys_home:
52n/a return _is_python_source_dir(_sys_home)
53n/a return _is_python_source_dir(project_base)
54n/apython_build = _python_build()
55n/a
56n/a# Calculate the build qualifier flags if they are defined. Adding the flags
57n/a# to the include and lib directories only makes sense for an installation, not
58n/a# an in-source build.
59n/abuild_flags = ''
60n/atry:
61n/a if not python_build:
62n/a build_flags = sys.abiflags
63n/aexcept AttributeError:
64n/a # It's not a configure-based build, so the sys module doesn't have
65n/a # this attribute, which is fine.
66n/a pass
67n/a
68n/adef get_python_version():
69n/a """Return a string containing the major and minor Python version,
70n/a leaving off the patchlevel. Sample return values could be '1.5'
71n/a or '2.2'.
72n/a """
73n/a return '%d.%d' % sys.version_info[:2]
74n/a
75n/a
76n/adef get_python_inc(plat_specific=0, prefix=None):
77n/a """Return the directory containing installed Python header files.
78n/a
79n/a If 'plat_specific' is false (the default), this is the path to the
80n/a non-platform-specific header files, i.e. Python.h and so on;
81n/a otherwise, this is the path to platform-specific header files
82n/a (namely pyconfig.h).
83n/a
84n/a If 'prefix' is supplied, use it instead of sys.base_prefix or
85n/a sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
86n/a """
87n/a if prefix is None:
88n/a prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
89n/a if os.name == "posix":
90n/a if python_build:
91n/a # Assume the executable is in the build directory. The
92n/a # pyconfig.h file should be in the same directory. Since
93n/a # the build directory may not be the source directory, we
94n/a # must use "srcdir" from the makefile to find the "Include"
95n/a # directory.
96n/a base = _sys_home or project_base
97n/a if plat_specific:
98n/a return base
99n/a if _sys_home:
100n/a incdir = os.path.join(_sys_home, get_config_var('AST_H_DIR'))
101n/a else:
102n/a incdir = os.path.join(get_config_var('srcdir'), 'Include')
103n/a return os.path.normpath(incdir)
104n/a python_dir = 'python' + get_python_version() + build_flags
105n/a return os.path.join(prefix, "include", python_dir)
106n/a elif os.name == "nt":
107n/a return os.path.join(prefix, "include")
108n/a else:
109n/a raise DistutilsPlatformError(
110n/a "I don't know where Python installs its C header files "
111n/a "on platform '%s'" % os.name)
112n/a
113n/a
114n/adef get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
115n/a """Return the directory containing the Python library (standard or
116n/a site additions).
117n/a
118n/a If 'plat_specific' is true, return the directory containing
119n/a platform-specific modules, i.e. any module from a non-pure-Python
120n/a module distribution; otherwise, return the platform-shared library
121n/a directory. If 'standard_lib' is true, return the directory
122n/a containing standard Python library modules; otherwise, return the
123n/a directory for site-specific modules.
124n/a
125n/a If 'prefix' is supplied, use it instead of sys.base_prefix or
126n/a sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
127n/a """
128n/a if prefix is None:
129n/a if standard_lib:
130n/a prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
131n/a else:
132n/a prefix = plat_specific and EXEC_PREFIX or PREFIX
133n/a
134n/a if os.name == "posix":
135n/a libpython = os.path.join(prefix,
136n/a "lib", "python" + get_python_version())
137n/a if standard_lib:
138n/a return libpython
139n/a else:
140n/a return os.path.join(libpython, "site-packages")
141n/a elif os.name == "nt":
142n/a if standard_lib:
143n/a return os.path.join(prefix, "Lib")
144n/a else:
145n/a return os.path.join(prefix, "Lib", "site-packages")
146n/a else:
147n/a raise DistutilsPlatformError(
148n/a "I don't know where Python installs its library "
149n/a "on platform '%s'" % os.name)
150n/a
151n/a
152n/a
153n/adef customize_compiler(compiler):
154n/a """Do any platform-specific customization of a CCompiler instance.
155n/a
156n/a Mainly needed on Unix, so we can plug in the information that
157n/a varies across Unices and is stored in Python's Makefile.
158n/a """
159n/a if compiler.compiler_type == "unix":
160n/a if sys.platform == "darwin":
161n/a # Perform first-time customization of compiler-related
162n/a # config vars on OS X now that we know we need a compiler.
163n/a # This is primarily to support Pythons from binary
164n/a # installers. The kind and paths to build tools on
165n/a # the user system may vary significantly from the system
166n/a # that Python itself was built on. Also the user OS
167n/a # version and build tools may not support the same set
168n/a # of CPU architectures for universal builds.
169n/a global _config_vars
170n/a # Use get_config_var() to ensure _config_vars is initialized.
171n/a if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
172n/a import _osx_support
173n/a _osx_support.customize_compiler(_config_vars)
174n/a _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
175n/a
176n/a (cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
177n/a get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
178n/a 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
179n/a
180n/a if 'CC' in os.environ:
181n/a newcc = os.environ['CC']
182n/a if (sys.platform == 'darwin'
183n/a and 'LDSHARED' not in os.environ
184n/a and ldshared.startswith(cc)):
185n/a # On OS X, if CC is overridden, use that as the default
186n/a # command for LDSHARED as well
187n/a ldshared = newcc + ldshared[len(cc):]
188n/a cc = newcc
189n/a if 'CXX' in os.environ:
190n/a cxx = os.environ['CXX']
191n/a if 'LDSHARED' in os.environ:
192n/a ldshared = os.environ['LDSHARED']
193n/a if 'CPP' in os.environ:
194n/a cpp = os.environ['CPP']
195n/a else:
196n/a cpp = cc + " -E" # not always
197n/a if 'LDFLAGS' in os.environ:
198n/a ldshared = ldshared + ' ' + os.environ['LDFLAGS']
199n/a if 'CFLAGS' in os.environ:
200n/a cflags = opt + ' ' + os.environ['CFLAGS']
201n/a ldshared = ldshared + ' ' + os.environ['CFLAGS']
202n/a if 'CPPFLAGS' in os.environ:
203n/a cpp = cpp + ' ' + os.environ['CPPFLAGS']
204n/a cflags = cflags + ' ' + os.environ['CPPFLAGS']
205n/a ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
206n/a if 'AR' in os.environ:
207n/a ar = os.environ['AR']
208n/a if 'ARFLAGS' in os.environ:
209n/a archiver = ar + ' ' + os.environ['ARFLAGS']
210n/a else:
211n/a archiver = ar + ' ' + ar_flags
212n/a
213n/a cc_cmd = cc + ' ' + cflags
214n/a compiler.set_executables(
215n/a preprocessor=cpp,
216n/a compiler=cc_cmd,
217n/a compiler_so=cc_cmd + ' ' + ccshared,
218n/a compiler_cxx=cxx,
219n/a linker_so=ldshared,
220n/a linker_exe=cc,
221n/a archiver=archiver)
222n/a
223n/a compiler.shared_lib_extension = shlib_suffix
224n/a
225n/a
226n/adef get_config_h_filename():
227n/a """Return full pathname of installed pyconfig.h file."""
228n/a if python_build:
229n/a if os.name == "nt":
230n/a inc_dir = os.path.join(_sys_home or project_base, "PC")
231n/a else:
232n/a inc_dir = _sys_home or project_base
233n/a else:
234n/a inc_dir = get_python_inc(plat_specific=1)
235n/a
236n/a return os.path.join(inc_dir, 'pyconfig.h')
237n/a
238n/a
239n/adef get_makefile_filename():
240n/a """Return full pathname of installed Makefile from the Python build."""
241n/a if python_build:
242n/a return os.path.join(_sys_home or project_base, "Makefile")
243n/a lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
244n/a config_file = 'config-{}{}'.format(get_python_version(), build_flags)
245n/a if hasattr(sys.implementation, '_multiarch'):
246n/a config_file += '-%s' % sys.implementation._multiarch
247n/a return os.path.join(lib_dir, config_file, 'Makefile')
248n/a
249n/a
250n/adef parse_config_h(fp, g=None):
251n/a """Parse a config.h-style file.
252n/a
253n/a A dictionary containing name/value pairs is returned. If an
254n/a optional dictionary is passed in as the second argument, it is
255n/a used instead of a new dictionary.
256n/a """
257n/a if g is None:
258n/a g = {}
259n/a define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
260n/a undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
261n/a #
262n/a while True:
263n/a line = fp.readline()
264n/a if not line:
265n/a break
266n/a m = define_rx.match(line)
267n/a if m:
268n/a n, v = m.group(1, 2)
269n/a try: v = int(v)
270n/a except ValueError: pass
271n/a g[n] = v
272n/a else:
273n/a m = undef_rx.match(line)
274n/a if m:
275n/a g[m.group(1)] = 0
276n/a return g
277n/a
278n/a
279n/a# Regexes needed for parsing Makefile (and similar syntaxes,
280n/a# like old-style Setup files).
281n/a_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
282n/a_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
283n/a_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
284n/a
285n/adef parse_makefile(fn, g=None):
286n/a """Parse a Makefile-style file.
287n/a
288n/a A dictionary containing name/value pairs is returned. If an
289n/a optional dictionary is passed in as the second argument, it is
290n/a used instead of a new dictionary.
291n/a """
292n/a from distutils.text_file import TextFile
293n/a fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
294n/a
295n/a if g is None:
296n/a g = {}
297n/a done = {}
298n/a notdone = {}
299n/a
300n/a while True:
301n/a line = fp.readline()
302n/a if line is None: # eof
303n/a break
304n/a m = _variable_rx.match(line)
305n/a if m:
306n/a n, v = m.group(1, 2)
307n/a v = v.strip()
308n/a # `$$' is a literal `$' in make
309n/a tmpv = v.replace('$$', '')
310n/a
311n/a if "$" in tmpv:
312n/a notdone[n] = v
313n/a else:
314n/a try:
315n/a v = int(v)
316n/a except ValueError:
317n/a # insert literal `$'
318n/a done[n] = v.replace('$$', '$')
319n/a else:
320n/a done[n] = v
321n/a
322n/a # Variables with a 'PY_' prefix in the makefile. These need to
323n/a # be made available without that prefix through sysconfig.
324n/a # Special care is needed to ensure that variable expansion works, even
325n/a # if the expansion uses the name without a prefix.
326n/a renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
327n/a
328n/a # do variable interpolation here
329n/a while notdone:
330n/a for name in list(notdone):
331n/a value = notdone[name]
332n/a m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
333n/a if m:
334n/a n = m.group(1)
335n/a found = True
336n/a if n in done:
337n/a item = str(done[n])
338n/a elif n in notdone:
339n/a # get it on a subsequent round
340n/a found = False
341n/a elif n in os.environ:
342n/a # do it like make: fall back to environment
343n/a item = os.environ[n]
344n/a
345n/a elif n in renamed_variables:
346n/a if name.startswith('PY_') and name[3:] in renamed_variables:
347n/a item = ""
348n/a
349n/a elif 'PY_' + n in notdone:
350n/a found = False
351n/a
352n/a else:
353n/a item = str(done['PY_' + n])
354n/a else:
355n/a done[n] = item = ""
356n/a if found:
357n/a after = value[m.end():]
358n/a value = value[:m.start()] + item + after
359n/a if "$" in after:
360n/a notdone[name] = value
361n/a else:
362n/a try: value = int(value)
363n/a except ValueError:
364n/a done[name] = value.strip()
365n/a else:
366n/a done[name] = value
367n/a del notdone[name]
368n/a
369n/a if name.startswith('PY_') \
370n/a and name[3:] in renamed_variables:
371n/a
372n/a name = name[3:]
373n/a if name not in done:
374n/a done[name] = value
375n/a else:
376n/a # bogus variable reference; just drop it since we can't deal
377n/a del notdone[name]
378n/a
379n/a fp.close()
380n/a
381n/a # strip spurious spaces
382n/a for k, v in done.items():
383n/a if isinstance(v, str):
384n/a done[k] = v.strip()
385n/a
386n/a # save the results in the global dictionary
387n/a g.update(done)
388n/a return g
389n/a
390n/a
391n/adef expand_makefile_vars(s, vars):
392n/a """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
393n/a 'string' according to 'vars' (a dictionary mapping variable names to
394n/a values). Variables not present in 'vars' are silently expanded to the
395n/a empty string. The variable values in 'vars' should not contain further
396n/a variable expansions; if 'vars' is the output of 'parse_makefile()',
397n/a you're fine. Returns a variable-expanded version of 's'.
398n/a """
399n/a
400n/a # This algorithm does multiple expansion, so if vars['foo'] contains
401n/a # "${bar}", it will expand ${foo} to ${bar}, and then expand
402n/a # ${bar}... and so forth. This is fine as long as 'vars' comes from
403n/a # 'parse_makefile()', which takes care of such expansions eagerly,
404n/a # according to make's variable expansion semantics.
405n/a
406n/a while True:
407n/a m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
408n/a if m:
409n/a (beg, end) = m.span()
410n/a s = s[0:beg] + vars.get(m.group(1)) + s[end:]
411n/a else:
412n/a break
413n/a return s
414n/a
415n/a
416n/a_config_vars = None
417n/a
418n/adef _init_posix():
419n/a """Initialize the module as appropriate for POSIX systems."""
420n/a # _sysconfigdata is generated at build time, see the sysconfig module
421n/a name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
422n/a '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
423n/a abi=sys.abiflags,
424n/a platform=sys.platform,
425n/a multiarch=getattr(sys.implementation, '_multiarch', ''),
426n/a ))
427n/a _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
428n/a build_time_vars = _temp.build_time_vars
429n/a global _config_vars
430n/a _config_vars = {}
431n/a _config_vars.update(build_time_vars)
432n/a
433n/a
434n/adef _init_nt():
435n/a """Initialize the module as appropriate for NT"""
436n/a g = {}
437n/a # set basic install directories
438n/a g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
439n/a g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
440n/a
441n/a # XXX hmmm.. a normal install puts include files here
442n/a g['INCLUDEPY'] = get_python_inc(plat_specific=0)
443n/a
444n/a g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
445n/a g['EXE'] = ".exe"
446n/a g['VERSION'] = get_python_version().replace(".", "")
447n/a g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
448n/a
449n/a global _config_vars
450n/a _config_vars = g
451n/a
452n/a
453n/adef get_config_vars(*args):
454n/a """With no arguments, return a dictionary of all configuration
455n/a variables relevant for the current platform. Generally this includes
456n/a everything needed to build extensions and install both pure modules and
457n/a extensions. On Unix, this means every variable defined in Python's
458n/a installed Makefile; on Windows it's a much smaller set.
459n/a
460n/a With arguments, return a list of values that result from looking up
461n/a each argument in the configuration variable dictionary.
462n/a """
463n/a global _config_vars
464n/a if _config_vars is None:
465n/a func = globals().get("_init_" + os.name)
466n/a if func:
467n/a func()
468n/a else:
469n/a _config_vars = {}
470n/a
471n/a # Normalized versions of prefix and exec_prefix are handy to have;
472n/a # in fact, these are the standard versions used most places in the
473n/a # Distutils.
474n/a _config_vars['prefix'] = PREFIX
475n/a _config_vars['exec_prefix'] = EXEC_PREFIX
476n/a
477n/a # For backward compatibility, see issue19555
478n/a SO = _config_vars.get('EXT_SUFFIX')
479n/a if SO is not None:
480n/a _config_vars['SO'] = SO
481n/a
482n/a # Always convert srcdir to an absolute path
483n/a srcdir = _config_vars.get('srcdir', project_base)
484n/a if os.name == 'posix':
485n/a if python_build:
486n/a # If srcdir is a relative path (typically '.' or '..')
487n/a # then it should be interpreted relative to the directory
488n/a # containing Makefile.
489n/a base = os.path.dirname(get_makefile_filename())
490n/a srcdir = os.path.join(base, srcdir)
491n/a else:
492n/a # srcdir is not meaningful since the installation is
493n/a # spread about the filesystem. We choose the
494n/a # directory containing the Makefile since we know it
495n/a # exists.
496n/a srcdir = os.path.dirname(get_makefile_filename())
497n/a _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
498n/a
499n/a # Convert srcdir into an absolute path if it appears necessary.
500n/a # Normally it is relative to the build directory. However, during
501n/a # testing, for example, we might be running a non-installed python
502n/a # from a different directory.
503n/a if python_build and os.name == "posix":
504n/a base = project_base
505n/a if (not os.path.isabs(_config_vars['srcdir']) and
506n/a base != os.getcwd()):
507n/a # srcdir is relative and we are not in the same directory
508n/a # as the executable. Assume executable is in the build
509n/a # directory and make srcdir absolute.
510n/a srcdir = os.path.join(base, _config_vars['srcdir'])
511n/a _config_vars['srcdir'] = os.path.normpath(srcdir)
512n/a
513n/a # OS X platforms require special customization to handle
514n/a # multi-architecture, multi-os-version installers
515n/a if sys.platform == 'darwin':
516n/a import _osx_support
517n/a _osx_support.customize_config_vars(_config_vars)
518n/a
519n/a if args:
520n/a vals = []
521n/a for name in args:
522n/a vals.append(_config_vars.get(name))
523n/a return vals
524n/a else:
525n/a return _config_vars
526n/a
527n/adef get_config_var(name):
528n/a """Return the value of a single variable using the dictionary
529n/a returned by 'get_config_vars()'. Equivalent to
530n/a get_config_vars().get(name)
531n/a """
532n/a if name == 'SO':
533n/a import warnings
534n/a warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
535n/a return get_config_vars().get(name)