blob: b51629eb94f825d8d0f857b8aa5b431814199d72 [file] [log] [blame]
Fred Drake70b014d2001-07-18 18:39:56 +00001"""Provide access to Python's configuration information. The specific
2configuration variables available depend heavily on the platform and
3configuration. The values may be retrieved using
4get_config_var(name), and the list of variables is available via
5get_config_vars().keys(). Additional convenience functions are also
6available.
Greg Ward1190ee31998-12-18 23:46:33 +00007
8Written by: Fred L. Drake, Jr.
9Email: <fdrake@acm.org>
Greg Ward1190ee31998-12-18 23:46:33 +000010"""
11
Steve Dower65e4cb12014-11-22 12:54:57 -080012import _imp
Greg Ward9ddaaa11999-01-06 14:46:06 +000013import os
14import re
Tarek Ziadé36797272010-07-22 12:50:05 +000015import sys
Greg Ward1190ee31998-12-18 23:46:33 +000016
Tarek Ziadé36797272010-07-22 12:50:05 +000017from .errors import DistutilsPlatformError
Paul Monson62dfd7d2019-04-25 11:36:45 -070018from .util import get_platform, get_host_platform
Greg Warda0ca3f22000-02-02 00:05:14 +000019
Tarek Ziadé36797272010-07-22 12:50:05 +000020# These are needed in a couple of spots, so just compute them once.
21PREFIX = os.path.normpath(sys.prefix)
22EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010023BASE_PREFIX = os.path.normpath(sys.base_prefix)
24BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000025
Tarek Ziadé36797272010-07-22 12:50:05 +000026# Path to the base directory of the project. On Windows the binary may
Stefan Grönkef1502d02017-09-25 18:58:10 +020027# live in project/PCbuild/win32 or project/PCbuild/amd64.
doko@python.org97313302013-01-25 14:33:33 +010028# set for cross builds
29if "_PYTHON_PROJECT_BASE" in os.environ:
30 project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
31else:
Victor Stinner0ef8c152019-04-25 11:59:34 +020032 if sys.executable:
33 project_base = os.path.dirname(os.path.abspath(sys.executable))
34 else:
35 # sys.executable can be empty if argv[0] has been changed and Python is
36 # unable to retrieve the real program name
37 project_base = os.getcwd()
Steve Dower85e102a2019-02-04 17:15:13 -080038
Tarek Ziadé8b441d02010-01-29 11:46:31 +000039
Tarek Ziadé36797272010-07-22 12:50:05 +000040# python_build: (Boolean) if true, we're either building Python or
41# building an extension with an un-installed Python, so we use
42# different (hard-wired) directories.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010043def _is_python_source_dir(d):
Antoine Pitrou961d54c2018-07-16 19:03:03 +020044 for fn in ("Setup", "Setup.local"):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010045 if os.path.isfile(os.path.join(d, "Modules", fn)):
Tarek Ziadé36797272010-07-22 12:50:05 +000046 return True
47 return False
Steve Dower85e102a2019-02-04 17:15:13 -080048
Vinay Sajip7ded1f02012-05-26 03:45:29 +010049_sys_home = getattr(sys, '_home', None)
Steve Dower85e102a2019-02-04 17:15:13 -080050
51if os.name == 'nt':
52 def _fix_pcbuild(d):
53 if d and os.path.normcase(d).startswith(
54 os.path.normcase(os.path.join(PREFIX, "PCbuild"))):
55 return PREFIX
56 return d
57 project_base = _fix_pcbuild(project_base)
58 _sys_home = _fix_pcbuild(_sys_home)
59
Vinay Sajip7ded1f02012-05-26 03:45:29 +010060def _python_build():
61 if _sys_home:
62 return _is_python_source_dir(_sys_home)
63 return _is_python_source_dir(project_base)
Steve Dower85e102a2019-02-04 17:15:13 -080064
Christian Heimes2202f872008-02-06 14:31:34 +000065python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000066
Steve Dower85e102a2019-02-04 17:15:13 -080067
Barry Warsaw14d98ac2010-11-24 19:43:47 +000068# Calculate the build qualifier flags if they are defined. Adding the flags
69# to the include and lib directories only makes sense for an installation, not
70# an in-source build.
71build_flags = ''
72try:
73 if not python_build:
74 build_flags = sys.abiflags
75except AttributeError:
76 # It's not a configure-based build, so the sys module doesn't have
77 # this attribute, which is fine.
78 pass
79
Tarek Ziadé36797272010-07-22 12:50:05 +000080def get_python_version():
81 """Return a string containing the major and minor Python version,
82 leaving off the patchlevel. Sample return values could be '1.5'
83 or '2.2'.
84 """
Serhiy Storchaka885bdc42016-02-11 13:10:36 +020085 return '%d.%d' % sys.version_info[:2]
Tarek Ziadéedacea32010-01-29 11:41:03 +000086
Tarek Ziadé36797272010-07-22 12:50:05 +000087
88def get_python_inc(plat_specific=0, prefix=None):
89 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000090
91 If 'plat_specific' is false (the default), this is the path to the
92 non-platform-specific header files, i.e. Python.h and so on;
93 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000094 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000095
Vinay Sajip7ded1f02012-05-26 03:45:29 +010096 If 'prefix' is supplied, use it instead of sys.base_prefix or
97 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000098 """
Tarek Ziadé36797272010-07-22 12:50:05 +000099 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100100 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000101 if os.name == "posix":
102 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000103 # Assume the executable is in the build directory. The
104 # pyconfig.h file should be in the same directory. Since
105 # the build directory may not be the source directory, we
106 # must use "srcdir" from the makefile to find the "Include"
107 # directory.
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000108 if plat_specific:
Jeremy Klothdbdea622017-05-09 09:24:13 -0600109 return _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000110 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100111 incdir = os.path.join(get_config_var('srcdir'), 'Include')
Jeremy Klothdbdea622017-05-09 09:24:13 -0600112 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000113 python_dir = 'python' + get_python_version() + build_flags
114 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000115 elif os.name == "nt":
Steve Dower85e102a2019-02-04 17:15:13 -0800116 if python_build:
117 # Include both the include and PC dir to ensure we can find
118 # pyconfig.h
119 return (os.path.join(prefix, "include") + os.path.pathsep +
120 os.path.join(prefix, "PC"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000121 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000122 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000123 raise DistutilsPlatformError(
124 "I don't know where Python installs its C header files "
125 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000126
127
Tarek Ziadé36797272010-07-22 12:50:05 +0000128def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
129 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000130 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000131
Fred Drakec1ee39a2000-03-09 15:54:52 +0000132 If 'plat_specific' is true, return the directory containing
133 platform-specific modules, i.e. any module from a non-pure-Python
134 module distribution; otherwise, return the platform-shared library
135 directory. If 'standard_lib' is true, return the directory
136 containing standard Python library modules; otherwise, return the
137 directory for site-specific modules.
138
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100139 If 'prefix' is supplied, use it instead of sys.base_prefix or
140 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000141 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000142 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100143 if standard_lib:
144 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
145 else:
146 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000147
148 if os.name == "posix":
149 libpython = os.path.join(prefix,
150 "lib", "python" + get_python_version())
151 if standard_lib:
152 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000153 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000154 return os.path.join(libpython, "site-packages")
155 elif os.name == "nt":
156 if standard_lib:
157 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000158 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400159 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000160 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000161 raise DistutilsPlatformError(
162 "I don't know where Python installs its library "
163 "on platform '%s'" % os.name)
164
Ned Deilycbfb9a52012-06-23 16:02:19 -0700165
Tarek Ziadé36797272010-07-22 12:50:05 +0000166
167def customize_compiler(compiler):
168 """Do any platform-specific customization of a CCompiler instance.
169
170 Mainly needed on Unix, so we can plug in the information that
171 varies across Unices and is stored in Python's Makefile.
172 """
173 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700174 if sys.platform == "darwin":
175 # Perform first-time customization of compiler-related
176 # config vars on OS X now that we know we need a compiler.
177 # This is primarily to support Pythons from binary
178 # installers. The kind and paths to build tools on
179 # the user system may vary significantly from the system
180 # that Python itself was built on. Also the user OS
181 # version and build tools may not support the same set
182 # of CPU architectures for universal builds.
183 global _config_vars
Ned Deily7bc5fb62014-07-06 16:14:33 -0700184 # Use get_config_var() to ensure _config_vars is initialized.
185 if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700186 import _osx_support
187 _osx_support.customize_compiler(_config_vars)
188 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
189
Victor Stinner86082c22019-03-15 14:57:52 +0100190 (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
191 get_config_vars('CC', 'CXX', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700192 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000193
194 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700195 newcc = os.environ['CC']
196 if (sys.platform == 'darwin'
197 and 'LDSHARED' not in os.environ
198 and ldshared.startswith(cc)):
199 # On OS X, if CC is overridden, use that as the default
200 # command for LDSHARED as well
201 ldshared = newcc + ldshared[len(cc):]
202 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000203 if 'CXX' in os.environ:
204 cxx = os.environ['CXX']
205 if 'LDSHARED' in os.environ:
206 ldshared = os.environ['LDSHARED']
207 if 'CPP' in os.environ:
208 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000209 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000210 cpp = cc + " -E" # not always
211 if 'LDFLAGS' in os.environ:
212 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
213 if 'CFLAGS' in os.environ:
Victor Stinner86082c22019-03-15 14:57:52 +0100214 cflags = cflags + ' ' + os.environ['CFLAGS']
Tarek Ziadé36797272010-07-22 12:50:05 +0000215 ldshared = ldshared + ' ' + os.environ['CFLAGS']
216 if 'CPPFLAGS' in os.environ:
217 cpp = cpp + ' ' + os.environ['CPPFLAGS']
218 cflags = cflags + ' ' + os.environ['CPPFLAGS']
219 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
220 if 'AR' in os.environ:
221 ar = os.environ['AR']
222 if 'ARFLAGS' in os.environ:
223 archiver = ar + ' ' + os.environ['ARFLAGS']
224 else:
225 archiver = ar + ' ' + ar_flags
226
227 cc_cmd = cc + ' ' + cflags
228 compiler.set_executables(
229 preprocessor=cpp,
230 compiler=cc_cmd,
231 compiler_so=cc_cmd + ' ' + ccshared,
232 compiler_cxx=cxx,
233 linker_so=ldshared,
234 linker_exe=cc,
235 archiver=archiver)
236
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700237 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000238
239
240def get_config_h_filename():
241 """Return full pathname of installed pyconfig.h file."""
242 if python_build:
243 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100244 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000245 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100246 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000247 else:
248 inc_dir = get_python_inc(plat_specific=1)
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400249
250 return os.path.join(inc_dir, 'pyconfig.h')
Tarek Ziadé36797272010-07-22 12:50:05 +0000251
Greg Ward1190ee31998-12-18 23:46:33 +0000252
Greg Ward9ddaaa11999-01-06 14:46:06 +0000253def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000254 """Return full pathname of installed Makefile from the Python build."""
255 if python_build:
doko@python.org97313302013-01-25 14:33:33 +0100256 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200257 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000258 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
doko@ubuntu.com55532312016-06-14 08:55:19 +0200259 if hasattr(sys.implementation, '_multiarch'):
260 config_file += '-%s' % sys.implementation._multiarch
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000261 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000262
Tarek Ziadé36797272010-07-22 12:50:05 +0000263
264def parse_config_h(fp, g=None):
265 """Parse a config.h-style file.
266
267 A dictionary containing name/value pairs is returned. If an
268 optional dictionary is passed in as the second argument, it is
269 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000270 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000271 if g is None:
272 g = {}
273 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
274 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
275 #
276 while True:
277 line = fp.readline()
278 if not line:
279 break
280 m = define_rx.match(line)
281 if m:
282 n, v = m.group(1, 2)
283 try: v = int(v)
284 except ValueError: pass
285 g[n] = v
286 else:
287 m = undef_rx.match(line)
288 if m:
289 g[m.group(1)] = 0
290 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000291
Greg Wardd283ce72000-09-17 00:53:02 +0000292
293# Regexes needed for parsing Makefile (and similar syntaxes,
294# like old-style Setup files).
R David Murray44b548d2016-09-08 13:59:53 -0400295_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
Greg Wardd283ce72000-09-17 00:53:02 +0000296_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
297_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
298
Greg Ward3fff8d22000-09-15 00:03:13 +0000299def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000300 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000301
302 A dictionary containing name/value pairs is returned. If an
303 optional dictionary is passed in as the second argument, it is
304 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000305 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000306 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000307 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000308
309 if g is None:
310 g = {}
311 done = {}
312 notdone = {}
313
314 while True:
315 line = fp.readline()
316 if line is None: # eof
317 break
318 m = _variable_rx.match(line)
319 if m:
320 n, v = m.group(1, 2)
321 v = v.strip()
322 # `$$' is a literal `$' in make
323 tmpv = v.replace('$$', '')
324
325 if "$" in tmpv:
326 notdone[n] = v
327 else:
328 try:
329 v = int(v)
330 except ValueError:
331 # insert literal `$'
332 done[n] = v.replace('$$', '$')
333 else:
334 done[n] = v
335
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000336 # Variables with a 'PY_' prefix in the makefile. These need to
337 # be made available without that prefix through sysconfig.
338 # Special care is needed to ensure that variable expansion works, even
339 # if the expansion uses the name without a prefix.
340 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
341
Tarek Ziadé36797272010-07-22 12:50:05 +0000342 # do variable interpolation here
343 while notdone:
344 for name in list(notdone):
345 value = notdone[name]
346 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
347 if m:
348 n = m.group(1)
349 found = True
350 if n in done:
351 item = str(done[n])
352 elif n in notdone:
353 # get it on a subsequent round
354 found = False
355 elif n in os.environ:
356 # do it like make: fall back to environment
357 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000358
359 elif n in renamed_variables:
360 if name.startswith('PY_') and name[3:] in renamed_variables:
361 item = ""
362
363 elif 'PY_' + n in notdone:
364 found = False
365
366 else:
367 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000368 else:
369 done[n] = item = ""
370 if found:
371 after = value[m.end():]
372 value = value[:m.start()] + item + after
373 if "$" in after:
374 notdone[name] = value
375 else:
376 try: value = int(value)
377 except ValueError:
378 done[name] = value.strip()
379 else:
380 done[name] = value
381 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000382
383 if name.startswith('PY_') \
384 and name[3:] in renamed_variables:
385
386 name = name[3:]
387 if name not in done:
388 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000389 else:
390 # bogus variable reference; just drop it since we can't deal
391 del notdone[name]
392
393 fp.close()
394
Antoine Pitroudbec7802010-10-10 09:37:12 +0000395 # strip spurious spaces
396 for k, v in done.items():
397 if isinstance(v, str):
398 done[k] = v.strip()
399
Tarek Ziadé36797272010-07-22 12:50:05 +0000400 # save the results in the global dictionary
401 g.update(done)
402 return g
403
Greg Ward1190ee31998-12-18 23:46:33 +0000404
Greg Wardd283ce72000-09-17 00:53:02 +0000405def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000406 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000407 'string' according to 'vars' (a dictionary mapping variable names to
408 values). Variables not present in 'vars' are silently expanded to the
409 empty string. The variable values in 'vars' should not contain further
410 variable expansions; if 'vars' is the output of 'parse_makefile()',
411 you're fine. Returns a variable-expanded version of 's'.
412 """
413
414 # This algorithm does multiple expansion, so if vars['foo'] contains
415 # "${bar}", it will expand ${foo} to ${bar}, and then expand
416 # ${bar}... and so forth. This is fine as long as 'vars' comes from
417 # 'parse_makefile()', which takes care of such expansions eagerly,
418 # according to make's variable expansion semantics.
419
Collin Winter5b7e9d72007-08-30 03:52:21 +0000420 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000421 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
422 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000423 (beg, end) = m.span()
424 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
425 else:
426 break
427 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000428
429
430_config_vars = None
431
432def _init_posix():
433 """Initialize the module as appropriate for POSIX systems."""
doko@ubuntu.com40948222016-06-05 01:17:57 +0200434 # _sysconfigdata is generated at build time, see the sysconfig module
Xavier de Gaye92dec542016-09-11 22:22:24 +0200435 name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
436 '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
Zachary Ware80da9932016-09-09 18:29:10 -0700437 abi=sys.abiflags,
438 platform=sys.platform,
439 multiarch=getattr(sys.implementation, '_multiarch', ''),
Xavier de Gaye92dec542016-09-11 22:22:24 +0200440 ))
doko@ubuntu.comeea86b02016-06-14 09:22:16 +0200441 _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
442 build_time_vars = _temp.build_time_vars
Tarek Ziadé36797272010-07-22 12:50:05 +0000443 global _config_vars
doko@ubuntu.com40948222016-06-05 01:17:57 +0200444 _config_vars = {}
445 _config_vars.update(build_time_vars)
Tarek Ziadé36797272010-07-22 12:50:05 +0000446
447
448def _init_nt():
449 """Initialize the module as appropriate for NT"""
450 g = {}
451 # set basic install directories
452 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
453 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
454
455 # XXX hmmm.. a normal install puts include files here
456 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
457
Steve Dower65e4cb12014-11-22 12:54:57 -0800458 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000459 g['EXE'] = ".exe"
460 g['VERSION'] = get_python_version().replace(".", "")
461 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
462
463 global _config_vars
464 _config_vars = g
465
466
Tarek Ziadé36797272010-07-22 12:50:05 +0000467def get_config_vars(*args):
468 """With no arguments, return a dictionary of all configuration
469 variables relevant for the current platform. Generally this includes
470 everything needed to build extensions and install both pure modules and
471 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700472 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000473
474 With arguments, return a list of values that result from looking up
475 each argument in the configuration variable dictionary.
476 """
477 global _config_vars
478 if _config_vars is None:
479 func = globals().get("_init_" + os.name)
480 if func:
481 func()
482 else:
483 _config_vars = {}
484
485 # Normalized versions of prefix and exec_prefix are handy to have;
486 # in fact, these are the standard versions used most places in the
487 # Distutils.
488 _config_vars['prefix'] = PREFIX
489 _config_vars['exec_prefix'] = EXEC_PREFIX
490
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500491 # For backward compatibility, see issue19555
492 SO = _config_vars.get('EXT_SUFFIX')
493 if SO is not None:
494 _config_vars['SO'] = SO
495
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100496 # Always convert srcdir to an absolute path
497 srcdir = _config_vars.get('srcdir', project_base)
498 if os.name == 'posix':
499 if python_build:
500 # If srcdir is a relative path (typically '.' or '..')
501 # then it should be interpreted relative to the directory
502 # containing Makefile.
503 base = os.path.dirname(get_makefile_filename())
504 srcdir = os.path.join(base, srcdir)
505 else:
506 # srcdir is not meaningful since the installation is
507 # spread about the filesystem. We choose the
508 # directory containing the Makefile since we know it
509 # exists.
510 srcdir = os.path.dirname(get_makefile_filename())
511 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
512
Tarek Ziadé36797272010-07-22 12:50:05 +0000513 # Convert srcdir into an absolute path if it appears necessary.
514 # Normally it is relative to the build directory. However, during
515 # testing, for example, we might be running a non-installed python
516 # from a different directory.
517 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100518 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000519 if (not os.path.isabs(_config_vars['srcdir']) and
520 base != os.getcwd()):
521 # srcdir is relative and we are not in the same directory
522 # as the executable. Assume executable is in the build
523 # directory and make srcdir absolute.
524 srcdir = os.path.join(base, _config_vars['srcdir'])
525 _config_vars['srcdir'] = os.path.normpath(srcdir)
526
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700527 # OS X platforms require special customization to handle
528 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000529 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700530 import _osx_support
531 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700532
Tarek Ziadé36797272010-07-22 12:50:05 +0000533 if args:
534 vals = []
535 for name in args:
536 vals.append(_config_vars.get(name))
537 return vals
538 else:
539 return _config_vars
540
541def get_config_var(name):
542 """Return the value of a single variable using the dictionary
543 returned by 'get_config_vars()'. Equivalent to
544 get_config_vars().get(name)
545 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500546 if name == 'SO':
547 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200548 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000549 return get_config_vars().get(name)