blob: fe8c990fb193024ed52086b1c187ccc746fb07ad [file] [log] [blame]
Tarek Ziadéedacea32010-01-29 11:41:03 +00001"""Provide access to Python's configuration information.
2
3"""
4import sys
5import os
Florent Xiclunaa4707382010-03-11 00:05:17 +00006from os.path import pardir, realpath
Tarek Ziadéedacea32010-01-29 11:41:03 +00007
Barry Warsawebbef6f2010-09-20 15:29:53 +00008__all__ = [
9 'get_config_h_filename',
10 'get_config_var',
11 'get_config_vars',
12 'get_makefile_filename',
13 'get_path',
14 'get_path_names',
15 'get_paths',
16 'get_platform',
17 'get_python_version',
18 'get_scheme_names',
19 'parse_config_h',
20 ]
Tarek Ziadé16ed6cb2010-05-25 09:47:06 +000021
Tarek Ziadéedacea32010-01-29 11:41:03 +000022_INSTALL_SCHEMES = {
23 'posix_prefix': {
24 'stdlib': '{base}/lib/python{py_version_short}',
25 'platstdlib': '{platbase}/lib/python{py_version_short}',
26 'purelib': '{base}/lib/python{py_version_short}/site-packages',
27 'platlib': '{platbase}/lib/python{py_version_short}/site-packages',
Barry Warsaw14d98ac2010-11-24 19:43:47 +000028 'include':
29 '{base}/include/python{py_version_short}{abiflags}',
30 'platinclude':
31 '{platbase}/include/python{py_version_short}{abiflags}',
Tarek Ziadéedacea32010-01-29 11:41:03 +000032 'scripts': '{base}/bin',
33 'data': '{base}',
34 },
35 'posix_home': {
36 'stdlib': '{base}/lib/python',
37 'platstdlib': '{base}/lib/python',
38 'purelib': '{base}/lib/python',
39 'platlib': '{base}/lib/python',
40 'include': '{base}/include/python',
41 'platinclude': '{base}/include/python',
42 'scripts': '{base}/bin',
43 'data' : '{base}',
44 },
45 'nt': {
46 'stdlib': '{base}/Lib',
47 'platstdlib': '{base}/Lib',
48 'purelib': '{base}/Lib/site-packages',
49 'platlib': '{base}/Lib/site-packages',
50 'include': '{base}/Include',
51 'platinclude': '{base}/Include',
52 'scripts': '{base}/Scripts',
53 'data' : '{base}',
54 },
55 'os2': {
56 'stdlib': '{base}/Lib',
57 'platstdlib': '{base}/Lib',
58 'purelib': '{base}/Lib/site-packages',
59 'platlib': '{base}/Lib/site-packages',
60 'include': '{base}/Include',
61 'platinclude': '{base}/Include',
62 'scripts': '{base}/Scripts',
63 'data' : '{base}',
64 },
65 'os2_home': {
Tarek Ziadé06710a82010-05-19 22:25:00 +000066 'stdlib': '{userbase}/lib/python{py_version_short}',
67 'platstdlib': '{userbase}/lib/python{py_version_short}',
68 'purelib': '{userbase}/lib/python{py_version_short}/site-packages',
69 'platlib': '{userbase}/lib/python{py_version_short}/site-packages',
Tarek Ziadéedacea32010-01-29 11:41:03 +000070 'include': '{userbase}/include/python{py_version_short}',
71 'scripts': '{userbase}/bin',
72 'data' : '{userbase}',
73 },
74 'nt_user': {
75 'stdlib': '{userbase}/Python{py_version_nodot}',
76 'platstdlib': '{userbase}/Python{py_version_nodot}',
77 'purelib': '{userbase}/Python{py_version_nodot}/site-packages',
78 'platlib': '{userbase}/Python{py_version_nodot}/site-packages',
79 'include': '{userbase}/Python{py_version_nodot}/Include',
80 'scripts': '{userbase}/Scripts',
81 'data' : '{userbase}',
82 },
83 'posix_user': {
Tarek Ziadé06710a82010-05-19 22:25:00 +000084 'stdlib': '{userbase}/lib/python{py_version_short}',
85 'platstdlib': '{userbase}/lib/python{py_version_short}',
86 'purelib': '{userbase}/lib/python{py_version_short}/site-packages',
87 'platlib': '{userbase}/lib/python{py_version_short}/site-packages',
Tarek Ziadéedacea32010-01-29 11:41:03 +000088 'include': '{userbase}/include/python{py_version_short}',
89 'scripts': '{userbase}/bin',
90 'data' : '{userbase}',
91 },
Ronald Oussoren4cda46a2010-05-08 10:49:43 +000092 'osx_framework_user': {
93 'stdlib': '{userbase}/lib/python',
94 'platstdlib': '{userbase}/lib/python',
95 'purelib': '{userbase}/lib/python/site-packages',
96 'platlib': '{userbase}/lib/python/site-packages',
97 'include': '{userbase}/include',
98 'scripts': '{userbase}/bin',
99 'data' : '{userbase}',
100 },
Tarek Ziadéedacea32010-01-29 11:41:03 +0000101 }
102
103_SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include',
104 'scripts', 'data')
105_PY_VERSION = sys.version.split()[0]
106_PY_VERSION_SHORT = sys.version[:3]
107_PY_VERSION_SHORT_NO_DOT = _PY_VERSION[0] + _PY_VERSION[2]
108_PREFIX = os.path.normpath(sys.prefix)
109_EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
110_CONFIG_VARS = None
111_USER_BASE = None
Victor Stinnerb103a932010-10-12 22:23:23 +0000112
113def _safe_realpath(path):
114 try:
115 return realpath(path)
116 except OSError:
117 return path
118
Victor Stinner171ba052010-03-12 14:20:59 +0000119if sys.executable:
Victor Stinnerb103a932010-10-12 22:23:23 +0000120 _PROJECT_BASE = os.path.dirname(_safe_realpath(sys.executable))
Victor Stinner171ba052010-03-12 14:20:59 +0000121else:
122 # sys.executable can be empty if argv[0] has been changed and Python is
123 # unable to retrieve the real program name
Victor Stinnerb103a932010-10-12 22:23:23 +0000124 _PROJECT_BASE = _safe_realpath(os.getcwd())
Tarek Ziadéedacea32010-01-29 11:41:03 +0000125
126if os.name == "nt" and "pcbuild" in _PROJECT_BASE[-8:].lower():
Victor Stinnerb103a932010-10-12 22:23:23 +0000127 _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000128# PC/VS7.1
129if os.name == "nt" and "\\pc\\v" in _PROJECT_BASE[-10:].lower():
Victor Stinnerb103a932010-10-12 22:23:23 +0000130 _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000131# PC/AMD64
132if os.name == "nt" and "\\pcbuild\\amd64" in _PROJECT_BASE[-14:].lower():
Victor Stinnerb103a932010-10-12 22:23:23 +0000133 _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000134
135def is_python_build():
136 for fn in ("Setup.dist", "Setup.local"):
137 if os.path.isfile(os.path.join(_PROJECT_BASE, "Modules", fn)):
138 return True
139 return False
140
141_PYTHON_BUILD = is_python_build()
142
143if _PYTHON_BUILD:
144 for scheme in ('posix_prefix', 'posix_home'):
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000145 _INSTALL_SCHEMES[scheme]['include'] = '{srcdir}/Include'
Ronald Oussorene41a19e2010-06-15 16:05:20 +0000146 _INSTALL_SCHEMES[scheme]['platinclude'] = '{projectbase}/.'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000147
148def _subst_vars(s, local_vars):
149 try:
150 return s.format(**local_vars)
151 except KeyError:
152 try:
153 return s.format(**os.environ)
154 except KeyError as var:
155 raise AttributeError('{%s}' % var)
156
157def _extend_dict(target_dict, other_dict):
158 target_keys = target_dict.keys()
159 for key, value in other_dict.items():
160 if key in target_keys:
161 continue
162 target_dict[key] = value
163
164def _expand_vars(scheme, vars):
165 res = {}
166 if vars is None:
167 vars = {}
168 _extend_dict(vars, get_config_vars())
169
170 for key, value in _INSTALL_SCHEMES[scheme].items():
171 if os.name in ('posix', 'nt'):
172 value = os.path.expanduser(value)
173 res[key] = os.path.normpath(_subst_vars(value, vars))
174 return res
175
176def _get_default_scheme():
177 if os.name == 'posix':
178 # the default scheme for posix is posix_prefix
179 return 'posix_prefix'
180 return os.name
181
182def _getuserbase():
183 env_base = os.environ.get("PYTHONUSERBASE", None)
184 def joinuser(*args):
185 return os.path.expanduser(os.path.join(*args))
186
187 # what about 'os2emx', 'riscos' ?
188 if os.name == "nt":
189 base = os.environ.get("APPDATA") or "~"
190 return env_base if env_base else joinuser(base, "Python")
191
Ronald Oussoren4cda46a2010-05-08 10:49:43 +0000192 if sys.platform == "darwin":
193 framework = get_config_var("PYTHONFRAMEWORK")
194 if framework:
Ronald Oussorenbda46722010-08-01 09:02:50 +0000195 return env_base if env_base else joinuser("~", "Library", framework, "%d.%d"%(
Ronald Oussoren4cda46a2010-05-08 10:49:43 +0000196 sys.version_info[:2]))
197
Tarek Ziadéedacea32010-01-29 11:41:03 +0000198 return env_base if env_base else joinuser("~", ".local")
199
200
201def _parse_makefile(filename, vars=None):
202 """Parse a Makefile-style file.
203
204 A dictionary containing name/value pairs is returned. If an
205 optional dictionary is passed in as the second argument, it is
206 used instead of a new dictionary.
207 """
208 import re
209 # Regexes needed for parsing Makefile (and similar syntaxes,
210 # like old-style Setup files).
211 _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
212 _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
213 _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
214
215 if vars is None:
216 vars = {}
217 done = {}
218 notdone = {}
219
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000220 with open(filename, errors="surrogateescape") as f:
Tarek Ziadéedacea32010-01-29 11:41:03 +0000221 lines = f.readlines()
222
223 for line in lines:
224 if line.startswith('#') or line.strip() == '':
225 continue
226 m = _variable_rx.match(line)
227 if m:
228 n, v = m.group(1, 2)
229 v = v.strip()
230 # `$$' is a literal `$' in make
231 tmpv = v.replace('$$', '')
232
233 if "$" in tmpv:
234 notdone[n] = v
235 else:
236 try:
237 v = int(v)
238 except ValueError:
239 # insert literal `$'
240 done[n] = v.replace('$$', '$')
241 else:
242 done[n] = v
243
244 # do variable interpolation here
245 variables = list(notdone.keys())
246
Ronald Oussorend21886c2010-07-20 16:07:10 +0000247 # Variables with a 'PY_' prefix in the makefile. These need to
248 # be made available without that prefix through sysconfig.
249 # Special care is needed to ensure that variable expansion works, even
250 # if the expansion uses the name without a prefix.
251 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
252
Tarek Ziadéedacea32010-01-29 11:41:03 +0000253 while len(variables) > 0:
254 for name in tuple(variables):
255 value = notdone[name]
256 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
257 if m is not None:
258 n = m.group(1)
259 found = True
260 if n in done:
261 item = str(done[n])
262 elif n in notdone:
263 # get it on a subsequent round
264 found = False
265 elif n in os.environ:
266 # do it like make: fall back to environment
267 item = os.environ[n]
Ronald Oussorend21886c2010-07-20 16:07:10 +0000268
269 elif n in renamed_variables:
270 if name.startswith('PY_') and name[3:] in renamed_variables:
271 item = ""
272
273 elif 'PY_' + n in notdone:
274 found = False
275
276 else:
277 item = str(done['PY_' + n])
278
Tarek Ziadéedacea32010-01-29 11:41:03 +0000279 else:
280 done[n] = item = ""
Ronald Oussorend21886c2010-07-20 16:07:10 +0000281
Tarek Ziadéedacea32010-01-29 11:41:03 +0000282 if found:
283 after = value[m.end():]
284 value = value[:m.start()] + item + after
285 if "$" in after:
286 notdone[name] = value
287 else:
288 try:
289 value = int(value)
290 except ValueError:
291 done[name] = value.strip()
292 else:
293 done[name] = value
294 variables.remove(name)
Ronald Oussorend21886c2010-07-20 16:07:10 +0000295
296 if name.startswith('PY_') \
297 and name[3:] in renamed_variables:
298
299 name = name[3:]
300 if name not in done:
301 done[name] = value
302
303
Tarek Ziadéedacea32010-01-29 11:41:03 +0000304 else:
305 # bogus variable reference; just drop it since we can't deal
306 variables.remove(name)
307
Antoine Pitroudbec7802010-10-10 09:37:12 +0000308 # strip spurious spaces
309 for k, v in done.items():
310 if isinstance(v, str):
311 done[k] = v.strip()
312
Tarek Ziadéedacea32010-01-29 11:41:03 +0000313 # save the results in the global dictionary
314 vars.update(done)
315 return vars
316
Tarek Ziadéedacea32010-01-29 11:41:03 +0000317
Barry Warsawebbef6f2010-09-20 15:29:53 +0000318def get_makefile_filename():
Éric Araujo300623d2010-11-22 01:19:20 +0000319 """Return the path of the Makefile."""
Tarek Ziadéedacea32010-01-29 11:41:03 +0000320 if _PYTHON_BUILD:
321 return os.path.join(_PROJECT_BASE, "Makefile")
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000322 return os.path.join(get_path('stdlib'),
323 'config-{}{}'.format(_PY_VERSION_SHORT, sys.abiflags),
324 'Makefile')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000325
Tarek Ziadéedacea32010-01-29 11:41:03 +0000326
327def _init_posix(vars):
328 """Initialize the module as appropriate for POSIX systems."""
329 # load the installed Makefile:
Barry Warsawebbef6f2010-09-20 15:29:53 +0000330 makefile = get_makefile_filename()
Tarek Ziadéedacea32010-01-29 11:41:03 +0000331 try:
332 _parse_makefile(makefile, vars)
333 except IOError as e:
334 msg = "invalid Python installation: unable to open %s" % makefile
335 if hasattr(e, "strerror"):
336 msg = msg + " (%s)" % e.strerror
337 raise IOError(msg)
338 # load the installed pyconfig.h:
339 config_h = get_config_h_filename()
340 try:
Antoine Pitroub86680e2010-10-14 21:15:17 +0000341 with open(config_h) as f:
342 parse_config_h(f, vars)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000343 except IOError as e:
344 msg = "invalid Python installation: unable to open %s" % config_h
345 if hasattr(e, "strerror"):
346 msg = msg + " (%s)" % e.strerror
347 raise IOError(msg)
348 # On MacOSX we need to check the setting of the environment variable
349 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
350 # it needs to be compatible.
351 # If it isn't set we set it to the configure-time value
352 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in vars:
353 cfg_target = vars['MACOSX_DEPLOYMENT_TARGET']
354 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
355 if cur_target == '':
356 cur_target = cfg_target
357 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
358 elif (list(map(int, cfg_target.split('.'))) >
359 list(map(int, cur_target.split('.')))):
360 msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" '
361 'during configure' % (cur_target, cfg_target))
362 raise IOError(msg)
363 # On AIX, there are wrong paths to the linker scripts in the Makefile
364 # -- these paths are relative to the Python source, but when installed
365 # the scripts are in another directory.
366 if _PYTHON_BUILD:
367 vars['LDSHARED'] = vars['BLDSHARED']
368
369def _init_non_posix(vars):
370 """Initialize the module as appropriate for NT"""
371 # set basic install directories
372 vars['LIBDEST'] = get_path('stdlib')
373 vars['BINLIBDEST'] = get_path('platstdlib')
374 vars['INCLUDEPY'] = get_path('include')
375 vars['SO'] = '.pyd'
376 vars['EXE'] = '.exe'
377 vars['VERSION'] = _PY_VERSION_SHORT_NO_DOT
Victor Stinnerb103a932010-10-12 22:23:23 +0000378 vars['BINDIR'] = os.path.dirname(_safe_realpath(sys.executable))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000379
380#
381# public APIs
382#
383
Tarek Ziadébd797682010-02-02 23:16:13 +0000384
385def parse_config_h(fp, vars=None):
386 """Parse a config.h-style file.
387
388 A dictionary containing name/value pairs is returned. If an
389 optional dictionary is passed in as the second argument, it is
390 used instead of a new dictionary.
391 """
392 import re
393 if vars is None:
394 vars = {}
395 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
396 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
397
398 while True:
399 line = fp.readline()
400 if not line:
401 break
402 m = define_rx.match(line)
403 if m:
404 n, v = m.group(1, 2)
405 try: v = int(v)
406 except ValueError: pass
407 vars[n] = v
408 else:
409 m = undef_rx.match(line)
410 if m:
411 vars[m.group(1)] = 0
412 return vars
413
414def get_config_h_filename():
Éric Araujo300623d2010-11-22 01:19:20 +0000415 """Return the path of pyconfig.h."""
Tarek Ziadébd797682010-02-02 23:16:13 +0000416 if _PYTHON_BUILD:
417 if os.name == "nt":
418 inc_dir = os.path.join(_PROJECT_BASE, "PC")
419 else:
420 inc_dir = _PROJECT_BASE
421 else:
422 inc_dir = get_path('platinclude')
423 return os.path.join(inc_dir, 'pyconfig.h')
424
Tarek Ziadéedacea32010-01-29 11:41:03 +0000425def get_scheme_names():
Éric Araujo300623d2010-11-22 01:19:20 +0000426 """Return a tuple containing the schemes names."""
Tarek Ziadébd797682010-02-02 23:16:13 +0000427 schemes = list(_INSTALL_SCHEMES.keys())
428 schemes.sort()
429 return tuple(schemes)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000430
431def get_path_names():
Éric Araujo300623d2010-11-22 01:19:20 +0000432 """Return a tuple containing the paths names."""
Tarek Ziadéedacea32010-01-29 11:41:03 +0000433 return _SCHEME_KEYS
434
435def get_paths(scheme=_get_default_scheme(), vars=None, expand=True):
Éric Araujo300623d2010-11-22 01:19:20 +0000436 """Return a mapping containing an install scheme.
Tarek Ziadéedacea32010-01-29 11:41:03 +0000437
438 ``scheme`` is the install scheme name. If not provided, it will
439 return the default scheme for the current platform.
440 """
441 if expand:
442 return _expand_vars(scheme, vars)
443 else:
444 return _INSTALL_SCHEMES[scheme]
445
446def get_path(name, scheme=_get_default_scheme(), vars=None, expand=True):
Éric Araujo300623d2010-11-22 01:19:20 +0000447 """Return a path corresponding to the scheme.
Tarek Ziadéedacea32010-01-29 11:41:03 +0000448
449 ``scheme`` is the install scheme name.
450 """
451 return get_paths(scheme, vars, expand)[name]
452
453def get_config_vars(*args):
454 """With no arguments, return a dictionary of all configuration
455 variables relevant for the current platform.
456
457 On Unix, this means every variable defined in Python's installed Makefile;
458 On Windows and Mac OS it's a much smaller set.
459
460 With arguments, return a list of values that result from looking up
461 each argument in the configuration variable dictionary.
462 """
463 import re
464 global _CONFIG_VARS
465 if _CONFIG_VARS is None:
466 _CONFIG_VARS = {}
467 # Normalized versions of prefix and exec_prefix are handy to have;
468 # in fact, these are the standard versions used most places in the
469 # Distutils.
470 _CONFIG_VARS['prefix'] = _PREFIX
471 _CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX
472 _CONFIG_VARS['py_version'] = _PY_VERSION
473 _CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT
474 _CONFIG_VARS['py_version_nodot'] = _PY_VERSION[0] + _PY_VERSION[2]
475 _CONFIG_VARS['base'] = _PREFIX
476 _CONFIG_VARS['platbase'] = _EXEC_PREFIX
Tarek Ziadéedacea32010-01-29 11:41:03 +0000477 _CONFIG_VARS['projectbase'] = _PROJECT_BASE
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000478 _CONFIG_VARS['abiflags'] = sys.abiflags
Tarek Ziadéedacea32010-01-29 11:41:03 +0000479
480 if os.name in ('nt', 'os2'):
481 _init_non_posix(_CONFIG_VARS)
482 if os.name == 'posix':
483 _init_posix(_CONFIG_VARS)
Ronald Oussoren4cda46a2010-05-08 10:49:43 +0000484 # Setting 'userbase' is done below the call to the
485 # init function to enable using 'get_config_var' in
486 # the init-function.
487 _CONFIG_VARS['userbase'] = _getuserbase()
488
Tarek Ziadéedacea32010-01-29 11:41:03 +0000489 if 'srcdir' not in _CONFIG_VARS:
490 _CONFIG_VARS['srcdir'] = _PROJECT_BASE
Ronald Oussorenab4fd612010-06-15 21:19:50 +0000491 else:
Victor Stinnerb103a932010-10-12 22:23:23 +0000492 _CONFIG_VARS['srcdir'] = _safe_realpath(_CONFIG_VARS['srcdir'])
Tarek Ziadéedacea32010-01-29 11:41:03 +0000493
494
495 # Convert srcdir into an absolute path if it appears necessary.
496 # Normally it is relative to the build directory. However, during
497 # testing, for example, we might be running a non-installed python
498 # from a different directory.
499 if _PYTHON_BUILD and os.name == "posix":
500 base = _PROJECT_BASE
Victor Stinnerb103a932010-10-12 22:23:23 +0000501 try:
502 cwd = os.getcwd()
503 except OSError:
504 cwd = None
Tarek Ziadéedacea32010-01-29 11:41:03 +0000505 if (not os.path.isabs(_CONFIG_VARS['srcdir']) and
Victor Stinnerb103a932010-10-12 22:23:23 +0000506 base != cwd):
Tarek Ziadéedacea32010-01-29 11:41:03 +0000507 # 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
513 if sys.platform == 'darwin':
514 kernel_version = os.uname()[2] # Kernel version (8.4.3)
515 major_version = int(kernel_version.split('.')[0])
516
517 if major_version < 8:
518 # On Mac OS X before 10.4, check if -arch and -isysroot
519 # are in CFLAGS or LDFLAGS and remove them if they are.
520 # This is needed when building extensions on a 10.3 system
521 # using a universal build of python.
522 for key in ('LDFLAGS', 'BASECFLAGS',
523 # a number of derived variables. These need to be
524 # patched up as well.
525 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
526 flags = _CONFIG_VARS[key]
527 flags = re.sub('-arch\s+\w+\s', ' ', flags)
528 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
529 _CONFIG_VARS[key] = flags
530 else:
531 # Allow the user to override the architecture flags using
532 # an environment variable.
533 # NOTE: This name was introduced by Apple in OSX 10.5 and
534 # is used by several scripting languages distributed with
535 # that OS release.
536 if 'ARCHFLAGS' in os.environ:
537 arch = os.environ['ARCHFLAGS']
538 for key in ('LDFLAGS', 'BASECFLAGS',
539 # a number of derived variables. These need to be
540 # patched up as well.
541 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
542
543 flags = _CONFIG_VARS[key]
544 flags = re.sub('-arch\s+\w+\s', ' ', flags)
545 flags = flags + ' ' + arch
546 _CONFIG_VARS[key] = flags
547
548 # If we're on OSX 10.5 or later and the user tries to
549 # compiles an extension using an SDK that is not present
550 # on the current machine it is better to not use an SDK
551 # than to fail.
552 #
553 # The major usecase for this is users using a Python.org
554 # binary installer on OSX 10.6: that installer uses
555 # the 10.4u SDK, but that SDK is not installed by default
556 # when you install Xcode.
557 #
558 CFLAGS = _CONFIG_VARS.get('CFLAGS', '')
559 m = re.search('-isysroot\s+(\S+)', CFLAGS)
560 if m is not None:
561 sdk = m.group(1)
562 if not os.path.exists(sdk):
563 for key in ('LDFLAGS', 'BASECFLAGS',
564 # a number of derived variables. These need to be
565 # patched up as well.
566 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
567
568 flags = _CONFIG_VARS[key]
569 flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags)
570 _CONFIG_VARS[key] = flags
571
572 if args:
573 vals = []
574 for name in args:
575 vals.append(_CONFIG_VARS.get(name))
576 return vals
577 else:
578 return _CONFIG_VARS
579
580def get_config_var(name):
581 """Return the value of a single variable using the dictionary returned by
582 'get_config_vars()'.
583
584 Equivalent to get_config_vars().get(name)
585 """
586 return get_config_vars().get(name)
587
588def get_platform():
589 """Return a string that identifies the current platform.
590
591 This is used mainly to distinguish platform-specific build directories and
592 platform-specific built distributions. Typically includes the OS name
593 and version and the architecture (as supplied by 'os.uname()'),
594 although the exact information included depends on the OS; eg. for IRIX
595 the architecture isn't particularly important (IRIX only runs on SGI
596 hardware), but for Linux the kernel version isn't particularly
597 important.
598
599 Examples of returned values:
600 linux-i586
601 linux-alpha (?)
602 solaris-2.6-sun4u
603 irix-5.3
604 irix64-6.2
605
606 Windows will return one of:
607 win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
608 win-ia64 (64bit Windows on Itanium)
609 win32 (all others - specifically, sys.platform is returned)
610
611 For other non-POSIX platforms, currently just returns 'sys.platform'.
612 """
613 import re
614 if os.name == 'nt':
615 # sniff sys.version for architecture.
616 prefix = " bit ("
617 i = sys.version.find(prefix)
618 if i == -1:
619 return sys.platform
620 j = sys.version.find(")", i)
621 look = sys.version[i+len(prefix):j].lower()
622 if look == 'amd64':
623 return 'win-amd64'
624 if look == 'itanium':
625 return 'win-ia64'
626 return sys.platform
627
628 if os.name != "posix" or not hasattr(os, 'uname'):
629 # XXX what about the architecture? NT is Intel or Alpha,
630 # Mac OS is M68k or PPC, etc.
631 return sys.platform
632
633 # Try to distinguish various flavours of Unix
634 osname, host, release, version, machine = os.uname()
635
636 # Convert the OS name to lowercase, remove '/' characters
637 # (to accommodate BSD/OS), and translate spaces (for "Power Macintosh")
638 osname = osname.lower().replace('/', '')
639 machine = machine.replace(' ', '_')
640 machine = machine.replace('/', '-')
641
642 if osname[:5] == "linux":
643 # At least on Linux/Intel, 'machine' is the processor --
644 # i386, etc.
645 # XXX what about Alpha, SPARC, etc?
646 return "%s-%s" % (osname, machine)
647 elif osname[:5] == "sunos":
648 if release[0] >= "5": # SunOS 5 == Solaris 2
649 osname = "solaris"
650 release = "%d.%s" % (int(release[0]) - 3, release[2:])
651 # fall through to standard osname-release-machine representation
652 elif osname[:4] == "irix": # could be "irix64"!
653 return "%s-%s" % (osname, release)
654 elif osname[:3] == "aix":
655 return "%s-%s.%s" % (osname, version, release)
656 elif osname[:6] == "cygwin":
657 osname = "cygwin"
658 rel_re = re.compile (r'[\d.]+')
659 m = rel_re.match(release)
660 if m:
661 release = m.group()
662 elif osname[:6] == "darwin":
663 #
664 # For our purposes, we'll assume that the system version from
665 # distutils' perspective is what MACOSX_DEPLOYMENT_TARGET is set
666 # to. This makes the compatibility story a bit more sane because the
667 # machine is going to compile and link as if it were
668 # MACOSX_DEPLOYMENT_TARGET.
669 cfgvars = get_config_vars()
670 macver = os.environ.get('MACOSX_DEPLOYMENT_TARGET')
671 if not macver:
672 macver = cfgvars.get('MACOSX_DEPLOYMENT_TARGET')
673
674 if 1:
675 # Always calculate the release of the running machine,
676 # needed to determine if we can build fat binaries or not.
677
678 macrelease = macver
679 # Get the system version. Reading this plist is a documented
680 # way to get the system version (see the documentation for
681 # the Gestalt Manager)
682 try:
683 f = open('/System/Library/CoreServices/SystemVersion.plist')
684 except IOError:
685 # We're on a plain darwin box, fall back to the default
686 # behaviour.
687 pass
688 else:
Éric Araujobee5cef2010-11-05 23:51:56 +0000689 try:
690 m = re.search(
691 r'<key>ProductUserVisibleVersion</key>\s*' +
692 r'<string>(.*?)</string>', f.read())
693 f.close()
694 if m is not None:
695 macrelease = '.'.join(m.group(1).split('.')[:2])
696 # else: fall back to the default behaviour
697 finally:
698 f.close()
Tarek Ziadéedacea32010-01-29 11:41:03 +0000699
700 if not macver:
701 macver = macrelease
702
703 if macver:
704 release = macver
705 osname = "macosx"
706
707 if (macrelease + '.') >= '10.4.' and \
708 '-arch' in get_config_vars().get('CFLAGS', '').strip():
709 # The universal build will build fat binaries, but not on
710 # systems before 10.4
711 #
712 # Try to detect 4-way universal builds, those have machine-type
713 # 'universal' instead of 'fat'.
714
715 machine = 'fat'
716 cflags = get_config_vars().get('CFLAGS')
717
718 archs = re.findall('-arch\s+(\S+)', cflags)
Ronald Oussorend3950522010-07-11 09:05:07 +0000719 archs = tuple(sorted(set(archs)))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000720
721 if len(archs) == 1:
722 machine = archs[0]
723 elif archs == ('i386', 'ppc'):
724 machine = 'fat'
725 elif archs == ('i386', 'x86_64'):
726 machine = 'intel'
727 elif archs == ('i386', 'ppc', 'x86_64'):
728 machine = 'fat3'
729 elif archs == ('ppc64', 'x86_64'):
730 machine = 'fat64'
731 elif archs == ('i386', 'ppc', 'ppc64', 'x86_64'):
732 machine = 'universal'
733 else:
734 raise ValueError(
735 "Don't know machine value for archs=%r"%(archs,))
736
737 elif machine == 'i386':
738 # On OSX the machine type returned by uname is always the
739 # 32-bit variant, even if the executable architecture is
740 # the 64-bit variant
741 if sys.maxsize >= 2**32:
742 machine = 'x86_64'
743
744 elif machine in ('PowerPC', 'Power_Macintosh'):
745 # Pick a sane name for the PPC architecture.
746 # See 'i386' case
747 if sys.maxsize >= 2**32:
748 machine = 'ppc64'
749 else:
750 machine = 'ppc'
751
752 return "%s-%s-%s" % (osname, release, machine)
753
754
755def get_python_version():
756 return _PY_VERSION_SHORT
Tarek Ziadéa7514992010-05-25 09:44:36 +0000757
758def _print_dict(title, data):
759 for index, (key, value) in enumerate(sorted(data.items())):
760 if index == 0:
761 print('{0}: '.format(title))
762 print('\t{0} = "{1}"'.format(key, value))
763
764def _main():
Éric Araujo300623d2010-11-22 01:19:20 +0000765 """Display all information sysconfig detains."""
Tarek Ziadéa7514992010-05-25 09:44:36 +0000766 print('Platform: "{0}"'.format(get_platform()))
767 print('Python version: "{0}"'.format(get_python_version()))
768 print('Current installation scheme: "{0}"'.format(_get_default_scheme()))
769 print('')
770 _print_dict('Paths', get_paths())
771 print('')
772 _print_dict('Variables', get_config_vars())
773
774if __name__ == '__main__':
775 _main()