blob: 01ee519792932fbd14406f0d7d16cec9388f34a8 [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":
Victor Stinner8510f432020-03-10 09:53:09 +0100149 if plat_specific or standard_lib:
150 # Platform-specific modules (any module from a non-pure-Python
151 # module distribution) or standard Python library modules.
152 libdir = sys.platlibdir
153 else:
154 # Pure Python
155 libdir = "lib"
156 libpython = os.path.join(prefix, libdir,
157 "python" + get_python_version())
Tarek Ziadé36797272010-07-22 12:50:05 +0000158 if standard_lib:
159 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000160 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000161 return os.path.join(libpython, "site-packages")
162 elif os.name == "nt":
163 if standard_lib:
164 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000165 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400166 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000167 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000168 raise DistutilsPlatformError(
169 "I don't know where Python installs its library "
170 "on platform '%s'" % os.name)
171
Ned Deilycbfb9a52012-06-23 16:02:19 -0700172
Tarek Ziadé36797272010-07-22 12:50:05 +0000173
174def customize_compiler(compiler):
175 """Do any platform-specific customization of a CCompiler instance.
176
177 Mainly needed on Unix, so we can plug in the information that
178 varies across Unices and is stored in Python's Makefile.
179 """
180 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700181 if sys.platform == "darwin":
182 # Perform first-time customization of compiler-related
183 # config vars on OS X now that we know we need a compiler.
184 # This is primarily to support Pythons from binary
185 # installers. The kind and paths to build tools on
186 # the user system may vary significantly from the system
187 # that Python itself was built on. Also the user OS
188 # version and build tools may not support the same set
189 # of CPU architectures for universal builds.
190 global _config_vars
Ned Deily7bc5fb62014-07-06 16:14:33 -0700191 # Use get_config_var() to ensure _config_vars is initialized.
192 if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700193 import _osx_support
194 _osx_support.customize_compiler(_config_vars)
195 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
196
Victor Stinner86082c22019-03-15 14:57:52 +0100197 (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
198 get_config_vars('CC', 'CXX', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700199 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000200
201 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700202 newcc = os.environ['CC']
203 if (sys.platform == 'darwin'
204 and 'LDSHARED' not in os.environ
205 and ldshared.startswith(cc)):
206 # On OS X, if CC is overridden, use that as the default
207 # command for LDSHARED as well
208 ldshared = newcc + ldshared[len(cc):]
209 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000210 if 'CXX' in os.environ:
211 cxx = os.environ['CXX']
212 if 'LDSHARED' in os.environ:
213 ldshared = os.environ['LDSHARED']
214 if 'CPP' in os.environ:
215 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000216 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000217 cpp = cc + " -E" # not always
218 if 'LDFLAGS' in os.environ:
219 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
220 if 'CFLAGS' in os.environ:
Victor Stinner86082c22019-03-15 14:57:52 +0100221 cflags = cflags + ' ' + os.environ['CFLAGS']
Tarek Ziadé36797272010-07-22 12:50:05 +0000222 ldshared = ldshared + ' ' + os.environ['CFLAGS']
223 if 'CPPFLAGS' in os.environ:
224 cpp = cpp + ' ' + os.environ['CPPFLAGS']
225 cflags = cflags + ' ' + os.environ['CPPFLAGS']
226 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
227 if 'AR' in os.environ:
228 ar = os.environ['AR']
229 if 'ARFLAGS' in os.environ:
230 archiver = ar + ' ' + os.environ['ARFLAGS']
231 else:
232 archiver = ar + ' ' + ar_flags
233
234 cc_cmd = cc + ' ' + cflags
235 compiler.set_executables(
236 preprocessor=cpp,
237 compiler=cc_cmd,
238 compiler_so=cc_cmd + ' ' + ccshared,
239 compiler_cxx=cxx,
240 linker_so=ldshared,
241 linker_exe=cc,
242 archiver=archiver)
243
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700244 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000245
246
247def get_config_h_filename():
248 """Return full pathname of installed pyconfig.h file."""
249 if python_build:
250 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100251 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000252 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100253 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000254 else:
255 inc_dir = get_python_inc(plat_specific=1)
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400256
257 return os.path.join(inc_dir, 'pyconfig.h')
Tarek Ziadé36797272010-07-22 12:50:05 +0000258
Greg Ward1190ee31998-12-18 23:46:33 +0000259
Greg Ward9ddaaa11999-01-06 14:46:06 +0000260def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000261 """Return full pathname of installed Makefile from the Python build."""
262 if python_build:
doko@python.org97313302013-01-25 14:33:33 +0100263 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200264 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000265 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
doko@ubuntu.com55532312016-06-14 08:55:19 +0200266 if hasattr(sys.implementation, '_multiarch'):
267 config_file += '-%s' % sys.implementation._multiarch
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000268 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000269
Tarek Ziadé36797272010-07-22 12:50:05 +0000270
271def parse_config_h(fp, g=None):
272 """Parse a config.h-style file.
273
274 A dictionary containing name/value pairs is returned. If an
275 optional dictionary is passed in as the second argument, it is
276 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000277 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000278 if g is None:
279 g = {}
280 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
281 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
282 #
283 while True:
284 line = fp.readline()
285 if not line:
286 break
287 m = define_rx.match(line)
288 if m:
289 n, v = m.group(1, 2)
290 try: v = int(v)
291 except ValueError: pass
292 g[n] = v
293 else:
294 m = undef_rx.match(line)
295 if m:
296 g[m.group(1)] = 0
297 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000298
Greg Wardd283ce72000-09-17 00:53:02 +0000299
300# Regexes needed for parsing Makefile (and similar syntaxes,
301# like old-style Setup files).
R David Murray44b548d2016-09-08 13:59:53 -0400302_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
Greg Wardd283ce72000-09-17 00:53:02 +0000303_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
304_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
305
Greg Ward3fff8d22000-09-15 00:03:13 +0000306def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000307 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000308
309 A dictionary containing name/value pairs is returned. If an
310 optional dictionary is passed in as the second argument, it is
311 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000312 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000313 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000314 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000315
316 if g is None:
317 g = {}
318 done = {}
319 notdone = {}
320
321 while True:
322 line = fp.readline()
323 if line is None: # eof
324 break
325 m = _variable_rx.match(line)
326 if m:
327 n, v = m.group(1, 2)
328 v = v.strip()
329 # `$$' is a literal `$' in make
330 tmpv = v.replace('$$', '')
331
332 if "$" in tmpv:
333 notdone[n] = v
334 else:
335 try:
336 v = int(v)
337 except ValueError:
338 # insert literal `$'
339 done[n] = v.replace('$$', '$')
340 else:
341 done[n] = v
342
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000343 # Variables with a 'PY_' prefix in the makefile. These need to
344 # be made available without that prefix through sysconfig.
345 # Special care is needed to ensure that variable expansion works, even
346 # if the expansion uses the name without a prefix.
347 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
348
Tarek Ziadé36797272010-07-22 12:50:05 +0000349 # do variable interpolation here
350 while notdone:
351 for name in list(notdone):
352 value = notdone[name]
353 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
354 if m:
355 n = m.group(1)
356 found = True
357 if n in done:
358 item = str(done[n])
359 elif n in notdone:
360 # get it on a subsequent round
361 found = False
362 elif n in os.environ:
363 # do it like make: fall back to environment
364 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000365
366 elif n in renamed_variables:
367 if name.startswith('PY_') and name[3:] in renamed_variables:
368 item = ""
369
370 elif 'PY_' + n in notdone:
371 found = False
372
373 else:
374 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000375 else:
376 done[n] = item = ""
377 if found:
378 after = value[m.end():]
379 value = value[:m.start()] + item + after
380 if "$" in after:
381 notdone[name] = value
382 else:
383 try: value = int(value)
384 except ValueError:
385 done[name] = value.strip()
386 else:
387 done[name] = value
388 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000389
390 if name.startswith('PY_') \
391 and name[3:] in renamed_variables:
392
393 name = name[3:]
394 if name not in done:
395 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000396 else:
397 # bogus variable reference; just drop it since we can't deal
398 del notdone[name]
399
400 fp.close()
401
Antoine Pitroudbec7802010-10-10 09:37:12 +0000402 # strip spurious spaces
403 for k, v in done.items():
404 if isinstance(v, str):
405 done[k] = v.strip()
406
Tarek Ziadé36797272010-07-22 12:50:05 +0000407 # save the results in the global dictionary
408 g.update(done)
409 return g
410
Greg Ward1190ee31998-12-18 23:46:33 +0000411
Greg Wardd283ce72000-09-17 00:53:02 +0000412def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000413 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000414 'string' according to 'vars' (a dictionary mapping variable names to
415 values). Variables not present in 'vars' are silently expanded to the
416 empty string. The variable values in 'vars' should not contain further
417 variable expansions; if 'vars' is the output of 'parse_makefile()',
418 you're fine. Returns a variable-expanded version of 's'.
419 """
420
421 # This algorithm does multiple expansion, so if vars['foo'] contains
422 # "${bar}", it will expand ${foo} to ${bar}, and then expand
423 # ${bar}... and so forth. This is fine as long as 'vars' comes from
424 # 'parse_makefile()', which takes care of such expansions eagerly,
425 # according to make's variable expansion semantics.
426
Collin Winter5b7e9d72007-08-30 03:52:21 +0000427 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000428 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
429 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000430 (beg, end) = m.span()
431 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
432 else:
433 break
434 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000435
436
437_config_vars = None
438
439def _init_posix():
440 """Initialize the module as appropriate for POSIX systems."""
doko@ubuntu.com40948222016-06-05 01:17:57 +0200441 # _sysconfigdata is generated at build time, see the sysconfig module
Xavier de Gaye92dec542016-09-11 22:22:24 +0200442 name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
443 '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
Zachary Ware80da9932016-09-09 18:29:10 -0700444 abi=sys.abiflags,
445 platform=sys.platform,
446 multiarch=getattr(sys.implementation, '_multiarch', ''),
Xavier de Gaye92dec542016-09-11 22:22:24 +0200447 ))
doko@ubuntu.comeea86b02016-06-14 09:22:16 +0200448 _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
449 build_time_vars = _temp.build_time_vars
Tarek Ziadé36797272010-07-22 12:50:05 +0000450 global _config_vars
doko@ubuntu.com40948222016-06-05 01:17:57 +0200451 _config_vars = {}
452 _config_vars.update(build_time_vars)
Tarek Ziadé36797272010-07-22 12:50:05 +0000453
454
455def _init_nt():
456 """Initialize the module as appropriate for NT"""
457 g = {}
458 # set basic install directories
459 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
460 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
461
462 # XXX hmmm.. a normal install puts include files here
463 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
464
Steve Dower65e4cb12014-11-22 12:54:57 -0800465 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000466 g['EXE'] = ".exe"
467 g['VERSION'] = get_python_version().replace(".", "")
468 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
469
470 global _config_vars
471 _config_vars = g
472
473
Tarek Ziadé36797272010-07-22 12:50:05 +0000474def get_config_vars(*args):
475 """With no arguments, return a dictionary of all configuration
476 variables relevant for the current platform. Generally this includes
477 everything needed to build extensions and install both pure modules and
478 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700479 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000480
481 With arguments, return a list of values that result from looking up
482 each argument in the configuration variable dictionary.
483 """
484 global _config_vars
485 if _config_vars is None:
486 func = globals().get("_init_" + os.name)
487 if func:
488 func()
489 else:
490 _config_vars = {}
491
492 # Normalized versions of prefix and exec_prefix are handy to have;
493 # in fact, these are the standard versions used most places in the
494 # Distutils.
495 _config_vars['prefix'] = PREFIX
496 _config_vars['exec_prefix'] = EXEC_PREFIX
497
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500498 # For backward compatibility, see issue19555
499 SO = _config_vars.get('EXT_SUFFIX')
500 if SO is not None:
501 _config_vars['SO'] = SO
502
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100503 # Always convert srcdir to an absolute path
504 srcdir = _config_vars.get('srcdir', project_base)
505 if os.name == 'posix':
506 if python_build:
507 # If srcdir is a relative path (typically '.' or '..')
508 # then it should be interpreted relative to the directory
509 # containing Makefile.
510 base = os.path.dirname(get_makefile_filename())
511 srcdir = os.path.join(base, srcdir)
512 else:
513 # srcdir is not meaningful since the installation is
514 # spread about the filesystem. We choose the
515 # directory containing the Makefile since we know it
516 # exists.
517 srcdir = os.path.dirname(get_makefile_filename())
518 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
519
Tarek Ziadé36797272010-07-22 12:50:05 +0000520 # Convert srcdir into an absolute path if it appears necessary.
521 # Normally it is relative to the build directory. However, during
522 # testing, for example, we might be running a non-installed python
523 # from a different directory.
524 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100525 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000526 if (not os.path.isabs(_config_vars['srcdir']) and
527 base != os.getcwd()):
528 # srcdir is relative and we are not in the same directory
529 # as the executable. Assume executable is in the build
530 # directory and make srcdir absolute.
531 srcdir = os.path.join(base, _config_vars['srcdir'])
532 _config_vars['srcdir'] = os.path.normpath(srcdir)
533
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700534 # OS X platforms require special customization to handle
535 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000536 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700537 import _osx_support
538 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700539
Tarek Ziadé36797272010-07-22 12:50:05 +0000540 if args:
541 vals = []
542 for name in args:
543 vals.append(_config_vars.get(name))
544 return vals
545 else:
546 return _config_vars
547
548def get_config_var(name):
549 """Return the value of a single variable using the dictionary
550 returned by 'get_config_vars()'. Equivalent to
551 get_config_vars().get(name)
552 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500553 if name == 'SO':
554 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200555 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000556 return get_config_vars().get(name)