blob: 8bf1a7016bdf420dd3a0fecd56e9d195477637cb [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
Steve Dower65e4cb12014-11-22 12:54:57 -080026# 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:
31 project_base = os.path.dirname(os.path.abspath(sys.executable))
Steve Dower65e4cb12014-11-22 12:54:57 -080032if (os.name == 'nt' and
33 project_base.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
34 project_base = os.path.dirname(os.path.dirname(project_base))
Tarek Ziadé8b441d02010-01-29 11:46:31 +000035
Tarek Ziadé36797272010-07-22 12:50:05 +000036# python_build: (Boolean) if true, we're either building Python or
37# building an extension with an un-installed Python, so we use
38# different (hard-wired) directories.
39# Setup.local is available for Makefile builds including VPATH builds,
40# Setup.dist is available on Windows
Vinay Sajip7ded1f02012-05-26 03:45:29 +010041def _is_python_source_dir(d):
Tarek Ziadé36797272010-07-22 12:50:05 +000042 for fn in ("Setup.dist", "Setup.local"):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010043 if os.path.isfile(os.path.join(d, "Modules", fn)):
Tarek Ziadé36797272010-07-22 12:50:05 +000044 return True
45 return False
Vinay Sajip7ded1f02012-05-26 03:45:29 +010046_sys_home = getattr(sys, '_home', None)
Steve Dower65e4cb12014-11-22 12:54:57 -080047if (_sys_home and os.name == 'nt' and
48 _sys_home.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
49 _sys_home = os.path.dirname(os.path.dirname(_sys_home))
Vinay Sajip7ded1f02012-05-26 03:45:29 +010050def _python_build():
51 if _sys_home:
52 return _is_python_source_dir(_sys_home)
53 return _is_python_source_dir(project_base)
Christian Heimes2202f872008-02-06 14:31:34 +000054python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000055
Barry Warsaw14d98ac2010-11-24 19:43:47 +000056# Calculate the build qualifier flags if they are defined. Adding the flags
57# to the include and lib directories only makes sense for an installation, not
58# an in-source build.
59build_flags = ''
60try:
61 if not python_build:
62 build_flags = sys.abiflags
63except AttributeError:
64 # It's not a configure-based build, so the sys module doesn't have
65 # this attribute, which is fine.
66 pass
67
Tarek Ziadé36797272010-07-22 12:50:05 +000068def get_python_version():
69 """Return a string containing the major and minor Python version,
70 leaving off the patchlevel. Sample return values could be '1.5'
71 or '2.2'.
72 """
Serhiy Storchaka885bdc42016-02-11 13:10:36 +020073 return '%d.%d' % sys.version_info[:2]
Tarek Ziadéedacea32010-01-29 11:41:03 +000074
Tarek Ziadé36797272010-07-22 12:50:05 +000075
76def get_python_inc(plat_specific=0, prefix=None):
77 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000078
79 If 'plat_specific' is false (the default), this is the path to the
80 non-platform-specific header files, i.e. Python.h and so on;
81 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000082 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000083
Vinay Sajip7ded1f02012-05-26 03:45:29 +010084 If 'prefix' is supplied, use it instead of sys.base_prefix or
85 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000086 """
Tarek Ziadé36797272010-07-22 12:50:05 +000087 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010088 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +000089 if os.name == "posix":
90 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000091 # Assume the executable is in the build directory. The
92 # pyconfig.h file should be in the same directory. Since
93 # the build directory may not be the source directory, we
94 # must use "srcdir" from the makefile to find the "Include"
95 # directory.
doko@python.org97313302013-01-25 14:33:33 +010096 base = _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000097 if plat_specific:
98 return base
Vinay Sajip048b0632012-07-16 18:24:55 +010099 if _sys_home:
100 incdir = os.path.join(_sys_home, get_config_var('AST_H_DIR'))
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000101 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100102 incdir = os.path.join(get_config_var('srcdir'), 'Include')
103 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000104 python_dir = 'python' + get_python_version() + build_flags
105 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000106 elif os.name == "nt":
107 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000108 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000109 raise DistutilsPlatformError(
110 "I don't know where Python installs its C header files "
111 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000112
113
Tarek Ziadé36797272010-07-22 12:50:05 +0000114def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
115 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000116 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000117
Fred Drakec1ee39a2000-03-09 15:54:52 +0000118 If 'plat_specific' is true, return the directory containing
119 platform-specific modules, i.e. any module from a non-pure-Python
120 module distribution; otherwise, return the platform-shared library
121 directory. If 'standard_lib' is true, return the directory
122 containing standard Python library modules; otherwise, return the
123 directory for site-specific modules.
124
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100125 If 'prefix' is supplied, use it instead of sys.base_prefix or
126 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000127 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000128 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100129 if standard_lib:
130 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
131 else:
132 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000133
134 if os.name == "posix":
135 libpython = os.path.join(prefix,
136 "lib", "python" + get_python_version())
137 if standard_lib:
138 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000139 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000140 return os.path.join(libpython, "site-packages")
141 elif os.name == "nt":
142 if standard_lib:
143 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000144 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400145 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000146 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000147 raise DistutilsPlatformError(
148 "I don't know where Python installs its library "
149 "on platform '%s'" % os.name)
150
Ned Deilycbfb9a52012-06-23 16:02:19 -0700151
Tarek Ziadé36797272010-07-22 12:50:05 +0000152
153def customize_compiler(compiler):
154 """Do any platform-specific customization of a CCompiler instance.
155
156 Mainly needed on Unix, so we can plug in the information that
157 varies across Unices and is stored in Python's Makefile.
158 """
159 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700160 if sys.platform == "darwin":
161 # Perform first-time customization of compiler-related
162 # config vars on OS X now that we know we need a compiler.
163 # This is primarily to support Pythons from binary
164 # installers. The kind and paths to build tools on
165 # the user system may vary significantly from the system
166 # that Python itself was built on. Also the user OS
167 # version and build tools may not support the same set
168 # of CPU architectures for universal builds.
169 global _config_vars
Ned Deily7bc5fb62014-07-06 16:14:33 -0700170 # Use get_config_var() to ensure _config_vars is initialized.
171 if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700172 import _osx_support
173 _osx_support.customize_compiler(_config_vars)
174 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
175
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700176 (cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
Tarek Ziadé36797272010-07-22 12:50:05 +0000177 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700178 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000179
180 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700181 newcc = os.environ['CC']
182 if (sys.platform == 'darwin'
183 and 'LDSHARED' not in os.environ
184 and ldshared.startswith(cc)):
185 # On OS X, if CC is overridden, use that as the default
186 # command for LDSHARED as well
187 ldshared = newcc + ldshared[len(cc):]
188 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000189 if 'CXX' in os.environ:
190 cxx = os.environ['CXX']
191 if 'LDSHARED' in os.environ:
192 ldshared = os.environ['LDSHARED']
193 if 'CPP' in os.environ:
194 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000195 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000196 cpp = cc + " -E" # not always
197 if 'LDFLAGS' in os.environ:
198 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
199 if 'CFLAGS' in os.environ:
200 cflags = opt + ' ' + os.environ['CFLAGS']
201 ldshared = ldshared + ' ' + os.environ['CFLAGS']
202 if 'CPPFLAGS' in os.environ:
203 cpp = cpp + ' ' + os.environ['CPPFLAGS']
204 cflags = cflags + ' ' + os.environ['CPPFLAGS']
205 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
206 if 'AR' in os.environ:
207 ar = os.environ['AR']
208 if 'ARFLAGS' in os.environ:
209 archiver = ar + ' ' + os.environ['ARFLAGS']
210 else:
211 archiver = ar + ' ' + ar_flags
212
213 cc_cmd = cc + ' ' + cflags
214 compiler.set_executables(
215 preprocessor=cpp,
216 compiler=cc_cmd,
217 compiler_so=cc_cmd + ' ' + ccshared,
218 compiler_cxx=cxx,
219 linker_so=ldshared,
220 linker_exe=cc,
221 archiver=archiver)
222
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700223 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000224
225
226def get_config_h_filename():
227 """Return full pathname of installed pyconfig.h file."""
228 if python_build:
229 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100230 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000231 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100232 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000233 else:
234 inc_dir = get_python_inc(plat_specific=1)
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400235
236 return os.path.join(inc_dir, 'pyconfig.h')
Tarek Ziadé36797272010-07-22 12:50:05 +0000237
Greg Ward1190ee31998-12-18 23:46:33 +0000238
Greg Ward9ddaaa11999-01-06 14:46:06 +0000239def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000240 """Return full pathname of installed Makefile from the Python build."""
241 if python_build:
doko@python.org97313302013-01-25 14:33:33 +0100242 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200243 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000244 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
doko@ubuntu.com55532312016-06-14 08:55:19 +0200245 if hasattr(sys.implementation, '_multiarch'):
246 config_file += '-%s' % sys.implementation._multiarch
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000247 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000248
Tarek Ziadé36797272010-07-22 12:50:05 +0000249
250def parse_config_h(fp, g=None):
251 """Parse a config.h-style file.
252
253 A dictionary containing name/value pairs is returned. If an
254 optional dictionary is passed in as the second argument, it is
255 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000256 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000257 if g is None:
258 g = {}
259 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
260 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
261 #
262 while True:
263 line = fp.readline()
264 if not line:
265 break
266 m = define_rx.match(line)
267 if m:
268 n, v = m.group(1, 2)
269 try: v = int(v)
270 except ValueError: pass
271 g[n] = v
272 else:
273 m = undef_rx.match(line)
274 if m:
275 g[m.group(1)] = 0
276 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000277
Greg Wardd283ce72000-09-17 00:53:02 +0000278
279# Regexes needed for parsing Makefile (and similar syntaxes,
280# like old-style Setup files).
R David Murray44b548d2016-09-08 13:59:53 -0400281_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
Greg Wardd283ce72000-09-17 00:53:02 +0000282_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
283_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
284
Greg Ward3fff8d22000-09-15 00:03:13 +0000285def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000286 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000287
288 A dictionary containing name/value pairs is returned. If an
289 optional dictionary is passed in as the second argument, it is
290 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000291 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000292 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000293 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000294
295 if g is None:
296 g = {}
297 done = {}
298 notdone = {}
299
300 while True:
301 line = fp.readline()
302 if line is None: # eof
303 break
304 m = _variable_rx.match(line)
305 if m:
306 n, v = m.group(1, 2)
307 v = v.strip()
308 # `$$' is a literal `$' in make
309 tmpv = v.replace('$$', '')
310
311 if "$" in tmpv:
312 notdone[n] = v
313 else:
314 try:
315 v = int(v)
316 except ValueError:
317 # insert literal `$'
318 done[n] = v.replace('$$', '$')
319 else:
320 done[n] = v
321
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000322 # Variables with a 'PY_' prefix in the makefile. These need to
323 # be made available without that prefix through sysconfig.
324 # Special care is needed to ensure that variable expansion works, even
325 # if the expansion uses the name without a prefix.
326 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
327
Tarek Ziadé36797272010-07-22 12:50:05 +0000328 # do variable interpolation here
329 while notdone:
330 for name in list(notdone):
331 value = notdone[name]
332 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
333 if m:
334 n = m.group(1)
335 found = True
336 if n in done:
337 item = str(done[n])
338 elif n in notdone:
339 # get it on a subsequent round
340 found = False
341 elif n in os.environ:
342 # do it like make: fall back to environment
343 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000344
345 elif n in renamed_variables:
346 if name.startswith('PY_') and name[3:] in renamed_variables:
347 item = ""
348
349 elif 'PY_' + n in notdone:
350 found = False
351
352 else:
353 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000354 else:
355 done[n] = item = ""
356 if found:
357 after = value[m.end():]
358 value = value[:m.start()] + item + after
359 if "$" in after:
360 notdone[name] = value
361 else:
362 try: value = int(value)
363 except ValueError:
364 done[name] = value.strip()
365 else:
366 done[name] = value
367 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000368
369 if name.startswith('PY_') \
370 and name[3:] in renamed_variables:
371
372 name = name[3:]
373 if name not in done:
374 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000375 else:
376 # bogus variable reference; just drop it since we can't deal
377 del notdone[name]
378
379 fp.close()
380
Antoine Pitroudbec7802010-10-10 09:37:12 +0000381 # strip spurious spaces
382 for k, v in done.items():
383 if isinstance(v, str):
384 done[k] = v.strip()
385
Tarek Ziadé36797272010-07-22 12:50:05 +0000386 # save the results in the global dictionary
387 g.update(done)
388 return g
389
Greg Ward1190ee31998-12-18 23:46:33 +0000390
Greg Wardd283ce72000-09-17 00:53:02 +0000391def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000392 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000393 'string' according to 'vars' (a dictionary mapping variable names to
394 values). Variables not present in 'vars' are silently expanded to the
395 empty string. The variable values in 'vars' should not contain further
396 variable expansions; if 'vars' is the output of 'parse_makefile()',
397 you're fine. Returns a variable-expanded version of 's'.
398 """
399
400 # This algorithm does multiple expansion, so if vars['foo'] contains
401 # "${bar}", it will expand ${foo} to ${bar}, and then expand
402 # ${bar}... and so forth. This is fine as long as 'vars' comes from
403 # 'parse_makefile()', which takes care of such expansions eagerly,
404 # according to make's variable expansion semantics.
405
Collin Winter5b7e9d72007-08-30 03:52:21 +0000406 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000407 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
408 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000409 (beg, end) = m.span()
410 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
411 else:
412 break
413 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000414
415
416_config_vars = None
417
418def _init_posix():
419 """Initialize the module as appropriate for POSIX systems."""
doko@ubuntu.com40948222016-06-05 01:17:57 +0200420 # _sysconfigdata is generated at build time, see the sysconfig module
Xavier de Gaye92dec542016-09-11 22:22:24 +0200421 name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
422 '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
Zachary Ware80da9932016-09-09 18:29:10 -0700423 abi=sys.abiflags,
424 platform=sys.platform,
425 multiarch=getattr(sys.implementation, '_multiarch', ''),
Xavier de Gaye92dec542016-09-11 22:22:24 +0200426 ))
doko@ubuntu.comeea86b02016-06-14 09:22:16 +0200427 _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
428 build_time_vars = _temp.build_time_vars
Tarek Ziadé36797272010-07-22 12:50:05 +0000429 global _config_vars
doko@ubuntu.com40948222016-06-05 01:17:57 +0200430 _config_vars = {}
431 _config_vars.update(build_time_vars)
Tarek Ziadé36797272010-07-22 12:50:05 +0000432
433
434def _init_nt():
435 """Initialize the module as appropriate for NT"""
436 g = {}
437 # set basic install directories
438 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
439 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
440
441 # XXX hmmm.. a normal install puts include files here
442 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
443
Steve Dower65e4cb12014-11-22 12:54:57 -0800444 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000445 g['EXE'] = ".exe"
446 g['VERSION'] = get_python_version().replace(".", "")
447 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
448
449 global _config_vars
450 _config_vars = g
451
452
Tarek Ziadé36797272010-07-22 12:50:05 +0000453def get_config_vars(*args):
454 """With no arguments, return a dictionary of all configuration
455 variables relevant for the current platform. Generally this includes
456 everything needed to build extensions and install both pure modules and
457 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700458 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000459
460 With arguments, return a list of values that result from looking up
461 each argument in the configuration variable dictionary.
462 """
463 global _config_vars
464 if _config_vars is None:
465 func = globals().get("_init_" + os.name)
466 if func:
467 func()
468 else:
469 _config_vars = {}
470
471 # Normalized versions of prefix and exec_prefix are handy to have;
472 # in fact, these are the standard versions used most places in the
473 # Distutils.
474 _config_vars['prefix'] = PREFIX
475 _config_vars['exec_prefix'] = EXEC_PREFIX
476
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500477 # For backward compatibility, see issue19555
478 SO = _config_vars.get('EXT_SUFFIX')
479 if SO is not None:
480 _config_vars['SO'] = SO
481
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100482 # Always convert srcdir to an absolute path
483 srcdir = _config_vars.get('srcdir', project_base)
484 if os.name == 'posix':
485 if python_build:
486 # If srcdir is a relative path (typically '.' or '..')
487 # then it should be interpreted relative to the directory
488 # containing Makefile.
489 base = os.path.dirname(get_makefile_filename())
490 srcdir = os.path.join(base, srcdir)
491 else:
492 # srcdir is not meaningful since the installation is
493 # spread about the filesystem. We choose the
494 # directory containing the Makefile since we know it
495 # exists.
496 srcdir = os.path.dirname(get_makefile_filename())
497 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
498
Tarek Ziadé36797272010-07-22 12:50:05 +0000499 # Convert srcdir into an absolute path if it appears necessary.
500 # Normally it is relative to the build directory. However, during
501 # testing, for example, we might be running a non-installed python
502 # from a different directory.
503 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100504 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000505 if (not os.path.isabs(_config_vars['srcdir']) and
506 base != os.getcwd()):
507 # srcdir is relative and we are not in the same directory
508 # as the executable. Assume executable is in the build
509 # directory and make srcdir absolute.
510 srcdir = os.path.join(base, _config_vars['srcdir'])
511 _config_vars['srcdir'] = os.path.normpath(srcdir)
512
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700513 # OS X platforms require special customization to handle
514 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000515 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700516 import _osx_support
517 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700518
Tarek Ziadé36797272010-07-22 12:50:05 +0000519 if args:
520 vals = []
521 for name in args:
522 vals.append(_config_vars.get(name))
523 return vals
524 else:
525 return _config_vars
526
527def get_config_var(name):
528 """Return the value of a single variable using the dictionary
529 returned by 'get_config_vars()'. Equivalent to
530 get_config_vars().get(name)
531 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500532 if name == 'SO':
533 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200534 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000535 return get_config_vars().get(name)