blob: 573724ddd778d191f56eaadbcc3de822d89deae2 [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 """
73 return sys.version[:3]
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)
245 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000246
Tarek Ziadé36797272010-07-22 12:50:05 +0000247
248def parse_config_h(fp, g=None):
249 """Parse a config.h-style file.
250
251 A dictionary containing name/value pairs is returned. If an
252 optional dictionary is passed in as the second argument, it is
253 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000254 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000255 if g is None:
256 g = {}
257 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
258 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
259 #
260 while True:
261 line = fp.readline()
262 if not line:
263 break
264 m = define_rx.match(line)
265 if m:
266 n, v = m.group(1, 2)
267 try: v = int(v)
268 except ValueError: pass
269 g[n] = v
270 else:
271 m = undef_rx.match(line)
272 if m:
273 g[m.group(1)] = 0
274 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000275
Greg Wardd283ce72000-09-17 00:53:02 +0000276
277# Regexes needed for parsing Makefile (and similar syntaxes,
278# like old-style Setup files).
279_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
280_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
281_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
282
Greg Ward3fff8d22000-09-15 00:03:13 +0000283def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000284 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000285
286 A dictionary containing name/value pairs is returned. If an
287 optional dictionary is passed in as the second argument, it is
288 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000289 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000290 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000291 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000292
293 if g is None:
294 g = {}
295 done = {}
296 notdone = {}
297
298 while True:
299 line = fp.readline()
300 if line is None: # eof
301 break
302 m = _variable_rx.match(line)
303 if m:
304 n, v = m.group(1, 2)
305 v = v.strip()
306 # `$$' is a literal `$' in make
307 tmpv = v.replace('$$', '')
308
309 if "$" in tmpv:
310 notdone[n] = v
311 else:
312 try:
313 v = int(v)
314 except ValueError:
315 # insert literal `$'
316 done[n] = v.replace('$$', '$')
317 else:
318 done[n] = v
319
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000320 # Variables with a 'PY_' prefix in the makefile. These need to
321 # be made available without that prefix through sysconfig.
322 # Special care is needed to ensure that variable expansion works, even
323 # if the expansion uses the name without a prefix.
324 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
325
Tarek Ziadé36797272010-07-22 12:50:05 +0000326 # do variable interpolation here
327 while notdone:
328 for name in list(notdone):
329 value = notdone[name]
330 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
331 if m:
332 n = m.group(1)
333 found = True
334 if n in done:
335 item = str(done[n])
336 elif n in notdone:
337 # get it on a subsequent round
338 found = False
339 elif n in os.environ:
340 # do it like make: fall back to environment
341 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000342
343 elif n in renamed_variables:
344 if name.startswith('PY_') and name[3:] in renamed_variables:
345 item = ""
346
347 elif 'PY_' + n in notdone:
348 found = False
349
350 else:
351 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000352 else:
353 done[n] = item = ""
354 if found:
355 after = value[m.end():]
356 value = value[:m.start()] + item + after
357 if "$" in after:
358 notdone[name] = value
359 else:
360 try: value = int(value)
361 except ValueError:
362 done[name] = value.strip()
363 else:
364 done[name] = value
365 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000366
367 if name.startswith('PY_') \
368 and name[3:] in renamed_variables:
369
370 name = name[3:]
371 if name not in done:
372 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000373 else:
374 # bogus variable reference; just drop it since we can't deal
375 del notdone[name]
376
377 fp.close()
378
Antoine Pitroudbec7802010-10-10 09:37:12 +0000379 # strip spurious spaces
380 for k, v in done.items():
381 if isinstance(v, str):
382 done[k] = v.strip()
383
Tarek Ziadé36797272010-07-22 12:50:05 +0000384 # save the results in the global dictionary
385 g.update(done)
386 return g
387
Greg Ward1190ee31998-12-18 23:46:33 +0000388
Greg Wardd283ce72000-09-17 00:53:02 +0000389def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000390 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000391 'string' according to 'vars' (a dictionary mapping variable names to
392 values). Variables not present in 'vars' are silently expanded to the
393 empty string. The variable values in 'vars' should not contain further
394 variable expansions; if 'vars' is the output of 'parse_makefile()',
395 you're fine. Returns a variable-expanded version of 's'.
396 """
397
398 # This algorithm does multiple expansion, so if vars['foo'] contains
399 # "${bar}", it will expand ${foo} to ${bar}, and then expand
400 # ${bar}... and so forth. This is fine as long as 'vars' comes from
401 # 'parse_makefile()', which takes care of such expansions eagerly,
402 # according to make's variable expansion semantics.
403
Collin Winter5b7e9d72007-08-30 03:52:21 +0000404 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000405 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
406 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000407 (beg, end) = m.span()
408 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
409 else:
410 break
411 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000412
413
414_config_vars = None
415
416def _init_posix():
417 """Initialize the module as appropriate for POSIX systems."""
418 g = {}
419 # load the installed Makefile:
420 try:
421 filename = get_makefile_filename()
422 parse_makefile(filename, g)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200423 except OSError as msg:
Tarek Ziadé36797272010-07-22 12:50:05 +0000424 my_msg = "invalid Python installation: unable to open %s" % filename
425 if hasattr(msg, "strerror"):
426 my_msg = my_msg + " (%s)" % msg.strerror
427
428 raise DistutilsPlatformError(my_msg)
429
430 # load the installed pyconfig.h:
431 try:
432 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000433 with open(filename) as file:
434 parse_config_h(file, g)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200435 except OSError as msg:
Tarek Ziadé36797272010-07-22 12:50:05 +0000436 my_msg = "invalid Python installation: unable to open %s" % filename
437 if hasattr(msg, "strerror"):
438 my_msg = my_msg + " (%s)" % msg.strerror
439
440 raise DistutilsPlatformError(my_msg)
441
Tarek Ziadé36797272010-07-22 12:50:05 +0000442 # On AIX, there are wrong paths to the linker scripts in the Makefile
443 # -- these paths are relative to the Python source, but when installed
444 # the scripts are in another directory.
445 if python_build:
446 g['LDSHARED'] = g['BLDSHARED']
447
Tarek Ziadé36797272010-07-22 12:50:05 +0000448 global _config_vars
449 _config_vars = g
450
451
452def _init_nt():
453 """Initialize the module as appropriate for NT"""
454 g = {}
455 # set basic install directories
456 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
457 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
458
459 # XXX hmmm.. a normal install puts include files here
460 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
461
Steve Dower65e4cb12014-11-22 12:54:57 -0800462 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000463 g['EXE'] = ".exe"
464 g['VERSION'] = get_python_version().replace(".", "")
465 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
466
467 global _config_vars
468 _config_vars = g
469
470
Tarek Ziadé36797272010-07-22 12:50:05 +0000471def get_config_vars(*args):
472 """With no arguments, return a dictionary of all configuration
473 variables relevant for the current platform. Generally this includes
474 everything needed to build extensions and install both pure modules and
475 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700476 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000477
478 With arguments, return a list of values that result from looking up
479 each argument in the configuration variable dictionary.
480 """
481 global _config_vars
482 if _config_vars is None:
483 func = globals().get("_init_" + os.name)
484 if func:
485 func()
486 else:
487 _config_vars = {}
488
489 # Normalized versions of prefix and exec_prefix are handy to have;
490 # in fact, these are the standard versions used most places in the
491 # Distutils.
492 _config_vars['prefix'] = PREFIX
493 _config_vars['exec_prefix'] = EXEC_PREFIX
494
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500495 # For backward compatibility, see issue19555
496 SO = _config_vars.get('EXT_SUFFIX')
497 if SO is not None:
498 _config_vars['SO'] = SO
499
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100500 # Always convert srcdir to an absolute path
501 srcdir = _config_vars.get('srcdir', project_base)
502 if os.name == 'posix':
503 if python_build:
504 # If srcdir is a relative path (typically '.' or '..')
505 # then it should be interpreted relative to the directory
506 # containing Makefile.
507 base = os.path.dirname(get_makefile_filename())
508 srcdir = os.path.join(base, srcdir)
509 else:
510 # srcdir is not meaningful since the installation is
511 # spread about the filesystem. We choose the
512 # directory containing the Makefile since we know it
513 # exists.
514 srcdir = os.path.dirname(get_makefile_filename())
515 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
516
Tarek Ziadé36797272010-07-22 12:50:05 +0000517 # Convert srcdir into an absolute path if it appears necessary.
518 # Normally it is relative to the build directory. However, during
519 # testing, for example, we might be running a non-installed python
520 # from a different directory.
521 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100522 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000523 if (not os.path.isabs(_config_vars['srcdir']) and
524 base != os.getcwd()):
525 # srcdir is relative and we are not in the same directory
526 # as the executable. Assume executable is in the build
527 # directory and make srcdir absolute.
528 srcdir = os.path.join(base, _config_vars['srcdir'])
529 _config_vars['srcdir'] = os.path.normpath(srcdir)
530
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700531 # OS X platforms require special customization to handle
532 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000533 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700534 import _osx_support
535 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700536
Tarek Ziadé36797272010-07-22 12:50:05 +0000537 if args:
538 vals = []
539 for name in args:
540 vals.append(_config_vars.get(name))
541 return vals
542 else:
543 return _config_vars
544
545def get_config_var(name):
546 """Return the value of a single variable using the dictionary
547 returned by 'get_config_vars()'. Equivalent to
548 get_config_vars().get(name)
549 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500550 if name == 'SO':
551 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200552 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000553 return get_config_vars().get(name)