blob: e07a6c8b94ec79a7f1bf82beb61651c7dab2c65b [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:
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.
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000096 if plat_specific:
Jeremy Klothdbdea622017-05-09 09:24:13 -060097 return _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000098 else:
Vinay Sajip048b0632012-07-16 18:24:55 +010099 incdir = os.path.join(get_config_var('srcdir'), 'Include')
Jeremy Klothdbdea622017-05-09 09:24:13 -0600100 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000101 python_dir = 'python' + get_python_version() + build_flags
102 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000103 elif os.name == "nt":
104 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000105 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000106 raise DistutilsPlatformError(
107 "I don't know where Python installs its C header files "
108 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000109
110
Tarek Ziadé36797272010-07-22 12:50:05 +0000111def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
112 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000113 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000114
Fred Drakec1ee39a2000-03-09 15:54:52 +0000115 If 'plat_specific' is true, return the directory containing
116 platform-specific modules, i.e. any module from a non-pure-Python
117 module distribution; otherwise, return the platform-shared library
118 directory. If 'standard_lib' is true, return the directory
119 containing standard Python library modules; otherwise, return the
120 directory for site-specific modules.
121
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100122 If 'prefix' is supplied, use it instead of sys.base_prefix or
123 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000124 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000125 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100126 if standard_lib:
127 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
128 else:
129 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000130
131 if os.name == "posix":
132 libpython = os.path.join(prefix,
133 "lib", "python" + get_python_version())
134 if standard_lib:
135 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000136 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000137 return os.path.join(libpython, "site-packages")
138 elif os.name == "nt":
139 if standard_lib:
140 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000141 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400142 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000143 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000144 raise DistutilsPlatformError(
145 "I don't know where Python installs its library "
146 "on platform '%s'" % os.name)
147
Ned Deilycbfb9a52012-06-23 16:02:19 -0700148
Tarek Ziadé36797272010-07-22 12:50:05 +0000149
150def customize_compiler(compiler):
151 """Do any platform-specific customization of a CCompiler instance.
152
153 Mainly needed on Unix, so we can plug in the information that
154 varies across Unices and is stored in Python's Makefile.
155 """
156 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700157 if sys.platform == "darwin":
158 # Perform first-time customization of compiler-related
159 # config vars on OS X now that we know we need a compiler.
160 # This is primarily to support Pythons from binary
161 # installers. The kind and paths to build tools on
162 # the user system may vary significantly from the system
163 # that Python itself was built on. Also the user OS
164 # version and build tools may not support the same set
165 # of CPU architectures for universal builds.
166 global _config_vars
Ned Deily7bc5fb62014-07-06 16:14:33 -0700167 # Use get_config_var() to ensure _config_vars is initialized.
168 if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700169 import _osx_support
170 _osx_support.customize_compiler(_config_vars)
171 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
172
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700173 (cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
Tarek Ziadé36797272010-07-22 12:50:05 +0000174 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700175 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000176
177 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700178 newcc = os.environ['CC']
179 if (sys.platform == 'darwin'
180 and 'LDSHARED' not in os.environ
181 and ldshared.startswith(cc)):
182 # On OS X, if CC is overridden, use that as the default
183 # command for LDSHARED as well
184 ldshared = newcc + ldshared[len(cc):]
185 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000186 if 'CXX' in os.environ:
187 cxx = os.environ['CXX']
188 if 'LDSHARED' in os.environ:
189 ldshared = os.environ['LDSHARED']
190 if 'CPP' in os.environ:
191 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000192 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000193 cpp = cc + " -E" # not always
194 if 'LDFLAGS' in os.environ:
195 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
196 if 'CFLAGS' in os.environ:
197 cflags = opt + ' ' + os.environ['CFLAGS']
198 ldshared = ldshared + ' ' + os.environ['CFLAGS']
199 if 'CPPFLAGS' in os.environ:
200 cpp = cpp + ' ' + os.environ['CPPFLAGS']
201 cflags = cflags + ' ' + os.environ['CPPFLAGS']
202 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
203 if 'AR' in os.environ:
204 ar = os.environ['AR']
205 if 'ARFLAGS' in os.environ:
206 archiver = ar + ' ' + os.environ['ARFLAGS']
207 else:
208 archiver = ar + ' ' + ar_flags
209
210 cc_cmd = cc + ' ' + cflags
211 compiler.set_executables(
212 preprocessor=cpp,
213 compiler=cc_cmd,
214 compiler_so=cc_cmd + ' ' + ccshared,
215 compiler_cxx=cxx,
216 linker_so=ldshared,
217 linker_exe=cc,
218 archiver=archiver)
219
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700220 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000221
222
223def get_config_h_filename():
224 """Return full pathname of installed pyconfig.h file."""
225 if python_build:
226 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100227 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000228 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100229 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000230 else:
231 inc_dir = get_python_inc(plat_specific=1)
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400232
233 return os.path.join(inc_dir, 'pyconfig.h')
Tarek Ziadé36797272010-07-22 12:50:05 +0000234
Greg Ward1190ee31998-12-18 23:46:33 +0000235
Greg Ward9ddaaa11999-01-06 14:46:06 +0000236def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000237 """Return full pathname of installed Makefile from the Python build."""
238 if python_build:
doko@python.org97313302013-01-25 14:33:33 +0100239 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200240 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000241 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
doko@ubuntu.com55532312016-06-14 08:55:19 +0200242 if hasattr(sys.implementation, '_multiarch'):
243 config_file += '-%s' % sys.implementation._multiarch
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000244 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000245
Tarek Ziadé36797272010-07-22 12:50:05 +0000246
247def parse_config_h(fp, g=None):
248 """Parse a config.h-style file.
249
250 A dictionary containing name/value pairs is returned. If an
251 optional dictionary is passed in as the second argument, it is
252 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000253 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000254 if g is None:
255 g = {}
256 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
257 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
258 #
259 while True:
260 line = fp.readline()
261 if not line:
262 break
263 m = define_rx.match(line)
264 if m:
265 n, v = m.group(1, 2)
266 try: v = int(v)
267 except ValueError: pass
268 g[n] = v
269 else:
270 m = undef_rx.match(line)
271 if m:
272 g[m.group(1)] = 0
273 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000274
Greg Wardd283ce72000-09-17 00:53:02 +0000275
276# Regexes needed for parsing Makefile (and similar syntaxes,
277# like old-style Setup files).
R David Murray44b548d2016-09-08 13:59:53 -0400278_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
Greg Wardd283ce72000-09-17 00:53:02 +0000279_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
280_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
281
Greg Ward3fff8d22000-09-15 00:03:13 +0000282def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000283 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000284
285 A dictionary containing name/value pairs is returned. If an
286 optional dictionary is passed in as the second argument, it is
287 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000288 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000289 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000290 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000291
292 if g is None:
293 g = {}
294 done = {}
295 notdone = {}
296
297 while True:
298 line = fp.readline()
299 if line is None: # eof
300 break
301 m = _variable_rx.match(line)
302 if m:
303 n, v = m.group(1, 2)
304 v = v.strip()
305 # `$$' is a literal `$' in make
306 tmpv = v.replace('$$', '')
307
308 if "$" in tmpv:
309 notdone[n] = v
310 else:
311 try:
312 v = int(v)
313 except ValueError:
314 # insert literal `$'
315 done[n] = v.replace('$$', '$')
316 else:
317 done[n] = v
318
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000319 # Variables with a 'PY_' prefix in the makefile. These need to
320 # be made available without that prefix through sysconfig.
321 # Special care is needed to ensure that variable expansion works, even
322 # if the expansion uses the name without a prefix.
323 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
324
Tarek Ziadé36797272010-07-22 12:50:05 +0000325 # do variable interpolation here
326 while notdone:
327 for name in list(notdone):
328 value = notdone[name]
329 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
330 if m:
331 n = m.group(1)
332 found = True
333 if n in done:
334 item = str(done[n])
335 elif n in notdone:
336 # get it on a subsequent round
337 found = False
338 elif n in os.environ:
339 # do it like make: fall back to environment
340 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000341
342 elif n in renamed_variables:
343 if name.startswith('PY_') and name[3:] in renamed_variables:
344 item = ""
345
346 elif 'PY_' + n in notdone:
347 found = False
348
349 else:
350 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000351 else:
352 done[n] = item = ""
353 if found:
354 after = value[m.end():]
355 value = value[:m.start()] + item + after
356 if "$" in after:
357 notdone[name] = value
358 else:
359 try: value = int(value)
360 except ValueError:
361 done[name] = value.strip()
362 else:
363 done[name] = value
364 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000365
366 if name.startswith('PY_') \
367 and name[3:] in renamed_variables:
368
369 name = name[3:]
370 if name not in done:
371 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000372 else:
373 # bogus variable reference; just drop it since we can't deal
374 del notdone[name]
375
376 fp.close()
377
Antoine Pitroudbec7802010-10-10 09:37:12 +0000378 # strip spurious spaces
379 for k, v in done.items():
380 if isinstance(v, str):
381 done[k] = v.strip()
382
Tarek Ziadé36797272010-07-22 12:50:05 +0000383 # save the results in the global dictionary
384 g.update(done)
385 return g
386
Greg Ward1190ee31998-12-18 23:46:33 +0000387
Greg Wardd283ce72000-09-17 00:53:02 +0000388def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000389 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000390 'string' according to 'vars' (a dictionary mapping variable names to
391 values). Variables not present in 'vars' are silently expanded to the
392 empty string. The variable values in 'vars' should not contain further
393 variable expansions; if 'vars' is the output of 'parse_makefile()',
394 you're fine. Returns a variable-expanded version of 's'.
395 """
396
397 # This algorithm does multiple expansion, so if vars['foo'] contains
398 # "${bar}", it will expand ${foo} to ${bar}, and then expand
399 # ${bar}... and so forth. This is fine as long as 'vars' comes from
400 # 'parse_makefile()', which takes care of such expansions eagerly,
401 # according to make's variable expansion semantics.
402
Collin Winter5b7e9d72007-08-30 03:52:21 +0000403 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000404 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
405 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000406 (beg, end) = m.span()
407 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
408 else:
409 break
410 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000411
412
413_config_vars = None
414
415def _init_posix():
416 """Initialize the module as appropriate for POSIX systems."""
doko@ubuntu.com40948222016-06-05 01:17:57 +0200417 # _sysconfigdata is generated at build time, see the sysconfig module
Xavier de Gaye92dec542016-09-11 22:22:24 +0200418 name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
419 '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
Zachary Ware80da9932016-09-09 18:29:10 -0700420 abi=sys.abiflags,
421 platform=sys.platform,
422 multiarch=getattr(sys.implementation, '_multiarch', ''),
Xavier de Gaye92dec542016-09-11 22:22:24 +0200423 ))
doko@ubuntu.comeea86b02016-06-14 09:22:16 +0200424 _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
425 build_time_vars = _temp.build_time_vars
Tarek Ziadé36797272010-07-22 12:50:05 +0000426 global _config_vars
doko@ubuntu.com40948222016-06-05 01:17:57 +0200427 _config_vars = {}
428 _config_vars.update(build_time_vars)
Tarek Ziadé36797272010-07-22 12:50:05 +0000429
430
431def _init_nt():
432 """Initialize the module as appropriate for NT"""
433 g = {}
434 # set basic install directories
435 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
436 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
437
438 # XXX hmmm.. a normal install puts include files here
439 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
440
Steve Dower65e4cb12014-11-22 12:54:57 -0800441 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000442 g['EXE'] = ".exe"
443 g['VERSION'] = get_python_version().replace(".", "")
444 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
445
446 global _config_vars
447 _config_vars = g
448
449
Tarek Ziadé36797272010-07-22 12:50:05 +0000450def get_config_vars(*args):
451 """With no arguments, return a dictionary of all configuration
452 variables relevant for the current platform. Generally this includes
453 everything needed to build extensions and install both pure modules and
454 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700455 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000456
457 With arguments, return a list of values that result from looking up
458 each argument in the configuration variable dictionary.
459 """
460 global _config_vars
461 if _config_vars is None:
462 func = globals().get("_init_" + os.name)
463 if func:
464 func()
465 else:
466 _config_vars = {}
467
468 # Normalized versions of prefix and exec_prefix are handy to have;
469 # in fact, these are the standard versions used most places in the
470 # Distutils.
471 _config_vars['prefix'] = PREFIX
472 _config_vars['exec_prefix'] = EXEC_PREFIX
473
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500474 # For backward compatibility, see issue19555
475 SO = _config_vars.get('EXT_SUFFIX')
476 if SO is not None:
477 _config_vars['SO'] = SO
478
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100479 # Always convert srcdir to an absolute path
480 srcdir = _config_vars.get('srcdir', project_base)
481 if os.name == 'posix':
482 if python_build:
483 # If srcdir is a relative path (typically '.' or '..')
484 # then it should be interpreted relative to the directory
485 # containing Makefile.
486 base = os.path.dirname(get_makefile_filename())
487 srcdir = os.path.join(base, srcdir)
488 else:
489 # srcdir is not meaningful since the installation is
490 # spread about the filesystem. We choose the
491 # directory containing the Makefile since we know it
492 # exists.
493 srcdir = os.path.dirname(get_makefile_filename())
494 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
495
Tarek Ziadé36797272010-07-22 12:50:05 +0000496 # Convert srcdir into an absolute path if it appears necessary.
497 # Normally it is relative to the build directory. However, during
498 # testing, for example, we might be running a non-installed python
499 # from a different directory.
500 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100501 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000502 if (not os.path.isabs(_config_vars['srcdir']) and
503 base != os.getcwd()):
504 # srcdir is relative and we are not in the same directory
505 # as the executable. Assume executable is in the build
506 # directory and make srcdir absolute.
507 srcdir = os.path.join(base, _config_vars['srcdir'])
508 _config_vars['srcdir'] = os.path.normpath(srcdir)
509
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700510 # OS X platforms require special customization to handle
511 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000512 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700513 import _osx_support
514 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700515
Tarek Ziadé36797272010-07-22 12:50:05 +0000516 if args:
517 vals = []
518 for name in args:
519 vals.append(_config_vars.get(name))
520 return vals
521 else:
522 return _config_vars
523
524def get_config_var(name):
525 """Return the value of a single variable using the dictionary
526 returned by 'get_config_vars()'. Equivalent to
527 get_config_vars().get(name)
528 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500529 if name == 'SO':
530 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200531 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000532 return get_config_vars().get(name)