blob: 37feae5df72c93d11410f8357a3aaeabd1b31acc [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
Greg Warda0ca3f22000-02-02 00:05:14 +000018
Tarek Ziadé36797272010-07-22 12:50:05 +000019# These are needed in a couple of spots, so just compute them once.
20PREFIX = os.path.normpath(sys.prefix)
21EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010022BASE_PREFIX = os.path.normpath(sys.base_prefix)
23BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000024
Tarek Ziadé36797272010-07-22 12:50:05 +000025# Path to the base directory of the project. On Windows the binary may
Stefan Grönkef1502d02017-09-25 18:58:10 +020026# live in project/PCbuild/win32 or project/PCbuild/amd64.
doko@python.org97313302013-01-25 14:33:33 +010027# set for cross builds
28if "_PYTHON_PROJECT_BASE" in os.environ:
29 project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
30else:
Victor Stinner0ef8c152019-04-25 11:59:34 +020031 if sys.executable:
32 project_base = os.path.dirname(os.path.abspath(sys.executable))
33 else:
34 # sys.executable can be empty if argv[0] has been changed and Python is
35 # unable to retrieve the real program name
36 project_base = os.getcwd()
Steve Dower85e102a2019-02-04 17:15:13 -080037
Tarek Ziadé8b441d02010-01-29 11:46:31 +000038
Tarek Ziadé36797272010-07-22 12:50:05 +000039# python_build: (Boolean) if true, we're either building Python or
40# building an extension with an un-installed Python, so we use
41# different (hard-wired) directories.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010042def _is_python_source_dir(d):
Antoine Pitrou961d54c2018-07-16 19:03:03 +020043 for fn in ("Setup", "Setup.local"):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010044 if os.path.isfile(os.path.join(d, "Modules", fn)):
Tarek Ziadé36797272010-07-22 12:50:05 +000045 return True
46 return False
Steve Dower85e102a2019-02-04 17:15:13 -080047
Vinay Sajip7ded1f02012-05-26 03:45:29 +010048_sys_home = getattr(sys, '_home', None)
Steve Dower85e102a2019-02-04 17:15:13 -080049
50if os.name == 'nt':
51 def _fix_pcbuild(d):
52 if d and os.path.normcase(d).startswith(
53 os.path.normcase(os.path.join(PREFIX, "PCbuild"))):
54 return PREFIX
55 return d
56 project_base = _fix_pcbuild(project_base)
57 _sys_home = _fix_pcbuild(_sys_home)
58
Vinay Sajip7ded1f02012-05-26 03:45:29 +010059def _python_build():
60 if _sys_home:
61 return _is_python_source_dir(_sys_home)
62 return _is_python_source_dir(project_base)
Steve Dower85e102a2019-02-04 17:15:13 -080063
Christian Heimes2202f872008-02-06 14:31:34 +000064python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000065
Steve Dower85e102a2019-02-04 17:15:13 -080066
Barry Warsaw14d98ac2010-11-24 19:43:47 +000067# Calculate the build qualifier flags if they are defined. Adding the flags
68# to the include and lib directories only makes sense for an installation, not
69# an in-source build.
70build_flags = ''
71try:
72 if not python_build:
73 build_flags = sys.abiflags
74except AttributeError:
75 # It's not a configure-based build, so the sys module doesn't have
76 # this attribute, which is fine.
77 pass
78
Tarek Ziadé36797272010-07-22 12:50:05 +000079def get_python_version():
80 """Return a string containing the major and minor Python version,
81 leaving off the patchlevel. Sample return values could be '1.5'
82 or '2.2'.
83 """
Serhiy Storchaka885bdc42016-02-11 13:10:36 +020084 return '%d.%d' % sys.version_info[:2]
Tarek Ziadéedacea32010-01-29 11:41:03 +000085
Tarek Ziadé36797272010-07-22 12:50:05 +000086
87def get_python_inc(plat_specific=0, prefix=None):
88 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000089
90 If 'plat_specific' is false (the default), this is the path to the
91 non-platform-specific header files, i.e. Python.h and so on;
92 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000093 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000094
Vinay Sajip7ded1f02012-05-26 03:45:29 +010095 If 'prefix' is supplied, use it instead of sys.base_prefix or
96 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000097 """
Tarek Ziadé36797272010-07-22 12:50:05 +000098 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010099 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000100 if os.name == "posix":
101 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000102 # Assume the executable is in the build directory. The
103 # pyconfig.h file should be in the same directory. Since
104 # the build directory may not be the source directory, we
105 # must use "srcdir" from the makefile to find the "Include"
106 # directory.
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000107 if plat_specific:
Jeremy Klothdbdea622017-05-09 09:24:13 -0600108 return _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000109 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100110 incdir = os.path.join(get_config_var('srcdir'), 'Include')
Jeremy Klothdbdea622017-05-09 09:24:13 -0600111 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000112 python_dir = 'python' + get_python_version() + build_flags
113 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000114 elif os.name == "nt":
Steve Dower85e102a2019-02-04 17:15:13 -0800115 if python_build:
116 # Include both the include and PC dir to ensure we can find
117 # pyconfig.h
118 return (os.path.join(prefix, "include") + os.path.pathsep +
119 os.path.join(prefix, "PC"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000120 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000122 raise DistutilsPlatformError(
123 "I don't know where Python installs its C header files "
124 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000125
126
Tarek Ziadé36797272010-07-22 12:50:05 +0000127def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
128 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000129 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000130
Fred Drakec1ee39a2000-03-09 15:54:52 +0000131 If 'plat_specific' is true, return the directory containing
132 platform-specific modules, i.e. any module from a non-pure-Python
133 module distribution; otherwise, return the platform-shared library
134 directory. If 'standard_lib' is true, return the directory
135 containing standard Python library modules; otherwise, return the
136 directory for site-specific modules.
137
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100138 If 'prefix' is supplied, use it instead of sys.base_prefix or
139 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000140 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000141 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100142 if standard_lib:
143 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
144 else:
145 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000146
147 if os.name == "posix":
Victor Stinner8510f432020-03-10 09:53:09 +0100148 if plat_specific or standard_lib:
149 # Platform-specific modules (any module from a non-pure-Python
150 # module distribution) or standard Python library modules.
151 libdir = sys.platlibdir
152 else:
153 # Pure Python
154 libdir = "lib"
155 libpython = os.path.join(prefix, libdir,
156 "python" + get_python_version())
Tarek Ziadé36797272010-07-22 12:50:05 +0000157 if standard_lib:
158 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000159 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000160 return os.path.join(libpython, "site-packages")
161 elif os.name == "nt":
162 if standard_lib:
163 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000164 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400165 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000166 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000167 raise DistutilsPlatformError(
168 "I don't know where Python installs its library "
169 "on platform '%s'" % os.name)
170
Ned Deilycbfb9a52012-06-23 16:02:19 -0700171
Tarek Ziadé36797272010-07-22 12:50:05 +0000172
173def customize_compiler(compiler):
174 """Do any platform-specific customization of a CCompiler instance.
175
176 Mainly needed on Unix, so we can plug in the information that
177 varies across Unices and is stored in Python's Makefile.
178 """
179 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700180 if sys.platform == "darwin":
181 # Perform first-time customization of compiler-related
182 # config vars on OS X now that we know we need a compiler.
183 # This is primarily to support Pythons from binary
184 # installers. The kind and paths to build tools on
185 # the user system may vary significantly from the system
186 # that Python itself was built on. Also the user OS
187 # version and build tools may not support the same set
188 # of CPU architectures for universal builds.
189 global _config_vars
Ned Deily7bc5fb62014-07-06 16:14:33 -0700190 # Use get_config_var() to ensure _config_vars is initialized.
191 if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700192 import _osx_support
193 _osx_support.customize_compiler(_config_vars)
194 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
195
Victor Stinner86082c22019-03-15 14:57:52 +0100196 (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
197 get_config_vars('CC', 'CXX', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700198 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000199
200 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700201 newcc = os.environ['CC']
202 if (sys.platform == 'darwin'
203 and 'LDSHARED' not in os.environ
204 and ldshared.startswith(cc)):
205 # On OS X, if CC is overridden, use that as the default
206 # command for LDSHARED as well
207 ldshared = newcc + ldshared[len(cc):]
208 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000209 if 'CXX' in os.environ:
210 cxx = os.environ['CXX']
211 if 'LDSHARED' in os.environ:
212 ldshared = os.environ['LDSHARED']
213 if 'CPP' in os.environ:
214 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000215 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000216 cpp = cc + " -E" # not always
217 if 'LDFLAGS' in os.environ:
218 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
219 if 'CFLAGS' in os.environ:
Victor Stinner86082c22019-03-15 14:57:52 +0100220 cflags = cflags + ' ' + os.environ['CFLAGS']
Tarek Ziadé36797272010-07-22 12:50:05 +0000221 ldshared = ldshared + ' ' + os.environ['CFLAGS']
222 if 'CPPFLAGS' in os.environ:
223 cpp = cpp + ' ' + os.environ['CPPFLAGS']
224 cflags = cflags + ' ' + os.environ['CPPFLAGS']
225 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
226 if 'AR' in os.environ:
227 ar = os.environ['AR']
228 if 'ARFLAGS' in os.environ:
229 archiver = ar + ' ' + os.environ['ARFLAGS']
230 else:
231 archiver = ar + ' ' + ar_flags
232
233 cc_cmd = cc + ' ' + cflags
234 compiler.set_executables(
235 preprocessor=cpp,
236 compiler=cc_cmd,
237 compiler_so=cc_cmd + ' ' + ccshared,
238 compiler_cxx=cxx,
239 linker_so=ldshared,
240 linker_exe=cc,
241 archiver=archiver)
242
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700243 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000244
245
246def get_config_h_filename():
247 """Return full pathname of installed pyconfig.h file."""
248 if python_build:
249 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100250 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000251 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100252 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000253 else:
254 inc_dir = get_python_inc(plat_specific=1)
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400255
256 return os.path.join(inc_dir, 'pyconfig.h')
Tarek Ziadé36797272010-07-22 12:50:05 +0000257
Greg Ward1190ee31998-12-18 23:46:33 +0000258
Greg Ward9ddaaa11999-01-06 14:46:06 +0000259def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000260 """Return full pathname of installed Makefile from the Python build."""
261 if python_build:
doko@python.org97313302013-01-25 14:33:33 +0100262 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200263 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000264 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
doko@ubuntu.com55532312016-06-14 08:55:19 +0200265 if hasattr(sys.implementation, '_multiarch'):
266 config_file += '-%s' % sys.implementation._multiarch
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000267 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000268
Tarek Ziadé36797272010-07-22 12:50:05 +0000269
270def parse_config_h(fp, g=None):
271 """Parse a config.h-style file.
272
273 A dictionary containing name/value pairs is returned. If an
274 optional dictionary is passed in as the second argument, it is
275 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000276 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000277 if g is None:
278 g = {}
279 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
280 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
281 #
282 while True:
283 line = fp.readline()
284 if not line:
285 break
286 m = define_rx.match(line)
287 if m:
288 n, v = m.group(1, 2)
289 try: v = int(v)
290 except ValueError: pass
291 g[n] = v
292 else:
293 m = undef_rx.match(line)
294 if m:
295 g[m.group(1)] = 0
296 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000297
Greg Wardd283ce72000-09-17 00:53:02 +0000298
299# Regexes needed for parsing Makefile (and similar syntaxes,
300# like old-style Setup files).
R David Murray44b548d2016-09-08 13:59:53 -0400301_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
Greg Wardd283ce72000-09-17 00:53:02 +0000302_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
303_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
304
Greg Ward3fff8d22000-09-15 00:03:13 +0000305def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000306 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000307
308 A dictionary containing name/value pairs is returned. If an
309 optional dictionary is passed in as the second argument, it is
310 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000311 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000312 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000313 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000314
315 if g is None:
316 g = {}
317 done = {}
318 notdone = {}
319
320 while True:
321 line = fp.readline()
322 if line is None: # eof
323 break
324 m = _variable_rx.match(line)
325 if m:
326 n, v = m.group(1, 2)
327 v = v.strip()
328 # `$$' is a literal `$' in make
329 tmpv = v.replace('$$', '')
330
331 if "$" in tmpv:
332 notdone[n] = v
333 else:
334 try:
335 v = int(v)
336 except ValueError:
337 # insert literal `$'
338 done[n] = v.replace('$$', '$')
339 else:
340 done[n] = v
341
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000342 # Variables with a 'PY_' prefix in the makefile. These need to
343 # be made available without that prefix through sysconfig.
344 # Special care is needed to ensure that variable expansion works, even
345 # if the expansion uses the name without a prefix.
346 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
347
Tarek Ziadé36797272010-07-22 12:50:05 +0000348 # do variable interpolation here
349 while notdone:
350 for name in list(notdone):
351 value = notdone[name]
352 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
353 if m:
354 n = m.group(1)
355 found = True
356 if n in done:
357 item = str(done[n])
358 elif n in notdone:
359 # get it on a subsequent round
360 found = False
361 elif n in os.environ:
362 # do it like make: fall back to environment
363 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000364
365 elif n in renamed_variables:
366 if name.startswith('PY_') and name[3:] in renamed_variables:
367 item = ""
368
369 elif 'PY_' + n in notdone:
370 found = False
371
372 else:
373 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000374 else:
375 done[n] = item = ""
376 if found:
377 after = value[m.end():]
378 value = value[:m.start()] + item + after
379 if "$" in after:
380 notdone[name] = value
381 else:
382 try: value = int(value)
383 except ValueError:
384 done[name] = value.strip()
385 else:
386 done[name] = value
387 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000388
389 if name.startswith('PY_') \
390 and name[3:] in renamed_variables:
391
392 name = name[3:]
393 if name not in done:
394 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000395 else:
396 # bogus variable reference; just drop it since we can't deal
397 del notdone[name]
398
399 fp.close()
400
Antoine Pitroudbec7802010-10-10 09:37:12 +0000401 # strip spurious spaces
402 for k, v in done.items():
403 if isinstance(v, str):
404 done[k] = v.strip()
405
Tarek Ziadé36797272010-07-22 12:50:05 +0000406 # save the results in the global dictionary
407 g.update(done)
408 return g
409
Greg Ward1190ee31998-12-18 23:46:33 +0000410
Greg Wardd283ce72000-09-17 00:53:02 +0000411def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000412 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000413 'string' according to 'vars' (a dictionary mapping variable names to
414 values). Variables not present in 'vars' are silently expanded to the
415 empty string. The variable values in 'vars' should not contain further
416 variable expansions; if 'vars' is the output of 'parse_makefile()',
417 you're fine. Returns a variable-expanded version of 's'.
418 """
419
420 # This algorithm does multiple expansion, so if vars['foo'] contains
421 # "${bar}", it will expand ${foo} to ${bar}, and then expand
422 # ${bar}... and so forth. This is fine as long as 'vars' comes from
423 # 'parse_makefile()', which takes care of such expansions eagerly,
424 # according to make's variable expansion semantics.
425
Collin Winter5b7e9d72007-08-30 03:52:21 +0000426 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000427 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
428 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000429 (beg, end) = m.span()
430 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
431 else:
432 break
433 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000434
435
436_config_vars = None
437
438def _init_posix():
439 """Initialize the module as appropriate for POSIX systems."""
doko@ubuntu.com40948222016-06-05 01:17:57 +0200440 # _sysconfigdata is generated at build time, see the sysconfig module
Xavier de Gaye92dec542016-09-11 22:22:24 +0200441 name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
442 '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
Zachary Ware80da9932016-09-09 18:29:10 -0700443 abi=sys.abiflags,
444 platform=sys.platform,
445 multiarch=getattr(sys.implementation, '_multiarch', ''),
Xavier de Gaye92dec542016-09-11 22:22:24 +0200446 ))
doko@ubuntu.comeea86b02016-06-14 09:22:16 +0200447 _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
448 build_time_vars = _temp.build_time_vars
Tarek Ziadé36797272010-07-22 12:50:05 +0000449 global _config_vars
doko@ubuntu.com40948222016-06-05 01:17:57 +0200450 _config_vars = {}
451 _config_vars.update(build_time_vars)
Tarek Ziadé36797272010-07-22 12:50:05 +0000452
453
454def _init_nt():
455 """Initialize the module as appropriate for NT"""
456 g = {}
457 # set basic install directories
458 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
459 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
460
461 # XXX hmmm.. a normal install puts include files here
462 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
463
Steve Dower65e4cb12014-11-22 12:54:57 -0800464 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000465 g['EXE'] = ".exe"
466 g['VERSION'] = get_python_version().replace(".", "")
467 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
468
469 global _config_vars
470 _config_vars = g
471
472
Tarek Ziadé36797272010-07-22 12:50:05 +0000473def get_config_vars(*args):
474 """With no arguments, return a dictionary of all configuration
475 variables relevant for the current platform. Generally this includes
476 everything needed to build extensions and install both pure modules and
477 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700478 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000479
480 With arguments, return a list of values that result from looking up
481 each argument in the configuration variable dictionary.
482 """
483 global _config_vars
484 if _config_vars is None:
485 func = globals().get("_init_" + os.name)
486 if func:
487 func()
488 else:
489 _config_vars = {}
490
491 # Normalized versions of prefix and exec_prefix are handy to have;
492 # in fact, these are the standard versions used most places in the
493 # Distutils.
494 _config_vars['prefix'] = PREFIX
495 _config_vars['exec_prefix'] = EXEC_PREFIX
496
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500497 # For backward compatibility, see issue19555
498 SO = _config_vars.get('EXT_SUFFIX')
499 if SO is not None:
500 _config_vars['SO'] = SO
501
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100502 # Always convert srcdir to an absolute path
503 srcdir = _config_vars.get('srcdir', project_base)
504 if os.name == 'posix':
505 if python_build:
506 # If srcdir is a relative path (typically '.' or '..')
507 # then it should be interpreted relative to the directory
508 # containing Makefile.
509 base = os.path.dirname(get_makefile_filename())
510 srcdir = os.path.join(base, srcdir)
511 else:
512 # srcdir is not meaningful since the installation is
513 # spread about the filesystem. We choose the
514 # directory containing the Makefile since we know it
515 # exists.
516 srcdir = os.path.dirname(get_makefile_filename())
517 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
518
Tarek Ziadé36797272010-07-22 12:50:05 +0000519 # Convert srcdir into an absolute path if it appears necessary.
520 # Normally it is relative to the build directory. However, during
521 # testing, for example, we might be running a non-installed python
522 # from a different directory.
523 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100524 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000525 if (not os.path.isabs(_config_vars['srcdir']) and
526 base != os.getcwd()):
527 # srcdir is relative and we are not in the same directory
528 # as the executable. Assume executable is in the build
529 # directory and make srcdir absolute.
530 srcdir = os.path.join(base, _config_vars['srcdir'])
531 _config_vars['srcdir'] = os.path.normpath(srcdir)
532
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700533 # OS X platforms require special customization to handle
534 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000535 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700536 import _osx_support
537 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700538
Tarek Ziadé36797272010-07-22 12:50:05 +0000539 if args:
540 vals = []
541 for name in args:
542 vals.append(_config_vars.get(name))
543 return vals
544 else:
545 return _config_vars
546
547def get_config_var(name):
548 """Return the value of a single variable using the dictionary
549 returned by 'get_config_vars()'. Equivalent to
550 get_config_vars().get(name)
551 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500552 if name == 'SO':
553 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200554 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000555 return get_config_vars().get(name)