blob: 8b55f217cdc5226720bddc67e4bc2938350debea [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
Greg Ward82d71ca2000-06-03 00:44:30 +000012__revision__ = "$Id$"
Greg Ward1190ee31998-12-18 23:46:33 +000013
Guido van Rossum63236cf2007-05-25 18:39:29 +000014import io
Greg Ward9ddaaa11999-01-06 14:46:06 +000015import os
16import re
Tarek Ziadé36797272010-07-22 12:50:05 +000017import sys
Greg Ward1190ee31998-12-18 23:46:33 +000018
Tarek Ziadé36797272010-07-22 12:50:05 +000019from .errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000020
Tarek Ziadé36797272010-07-22 12:50:05 +000021# These are needed in a couple of spots, so just compute them once.
22PREFIX = os.path.normpath(sys.prefix)
23EXEC_PREFIX = os.path.normpath(sys.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
26# live in project/PCBuild9. If we're dealing with an x64 Windows build,
27# it'll live in project/PCbuild/amd64.
28project_base = os.path.dirname(os.path.abspath(sys.executable))
29if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
30 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
31# PC/VS7.1
32if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
33 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
34 os.path.pardir))
35# PC/AMD64
36if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
37 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
38 os.path.pardir))
Tarek Ziadé8b441d02010-01-29 11:46:31 +000039
Tarek Ziadé36797272010-07-22 12:50:05 +000040# python_build: (Boolean) if true, we're either building Python or
41# building an extension with an un-installed Python, so we use
42# different (hard-wired) directories.
43# Setup.local is available for Makefile builds including VPATH builds,
44# Setup.dist is available on Windows
Christian Heimes2202f872008-02-06 14:31:34 +000045def _python_build():
Tarek Ziadé36797272010-07-22 12:50:05 +000046 for fn in ("Setup.dist", "Setup.local"):
47 if os.path.isfile(os.path.join(project_base, "Modules", fn)):
48 return True
49 return False
Christian Heimes2202f872008-02-06 14:31:34 +000050python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000051
Tarek Ziadé36797272010-07-22 12:50:05 +000052def get_python_version():
53 """Return a string containing the major and minor Python version,
54 leaving off the patchlevel. Sample return values could be '1.5'
55 or '2.2'.
56 """
57 return sys.version[:3]
Tarek Ziadéedacea32010-01-29 11:41:03 +000058
Tarek Ziadé36797272010-07-22 12:50:05 +000059
60def get_python_inc(plat_specific=0, prefix=None):
61 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000062
63 If 'plat_specific' is false (the default), this is the path to the
64 non-platform-specific header files, i.e. Python.h and so on;
65 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000066 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000067
Greg Wardd38e6f72000-04-10 01:17:49 +000068 If 'prefix' is supplied, use it instead of sys.prefix or
69 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000070 """
Tarek Ziadé36797272010-07-22 12:50:05 +000071 if prefix is None:
72 prefix = plat_specific and EXEC_PREFIX or PREFIX
73 if os.name == "posix":
74 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000075 # Assume the executable is in the build directory. The
76 # pyconfig.h file should be in the same directory. Since
77 # the build directory may not be the source directory, we
78 # must use "srcdir" from the makefile to find the "Include"
79 # directory.
80 base = os.path.dirname(os.path.abspath(sys.executable))
81 if plat_specific:
82 return base
83 else:
84 incdir = os.path.join(get_config_var('srcdir'), 'Include')
85 return os.path.normpath(incdir)
Tarek Ziadé36797272010-07-22 12:50:05 +000086 return os.path.join(prefix, "include", "python" + get_python_version())
87 elif os.name == "nt":
88 return os.path.join(prefix, "include")
89 elif os.name == "mac":
90 if plat_specific:
91 return os.path.join(prefix, "Mac", "Include")
92 else:
93 return os.path.join(prefix, "Include")
94 elif os.name == "os2":
95 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000096 else:
Tarek Ziadé36797272010-07-22 12:50:05 +000097 raise DistutilsPlatformError(
98 "I don't know where Python installs its C header files "
99 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000100
101
Tarek Ziadé36797272010-07-22 12:50:05 +0000102def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
103 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000104 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000105
Fred Drakec1ee39a2000-03-09 15:54:52 +0000106 If 'plat_specific' is true, return the directory containing
107 platform-specific modules, i.e. any module from a non-pure-Python
108 module distribution; otherwise, return the platform-shared library
109 directory. If 'standard_lib' is true, return the directory
110 containing standard Python library modules; otherwise, return the
111 directory for site-specific modules.
112
Greg Wardd38e6f72000-04-10 01:17:49 +0000113 If 'prefix' is supplied, use it instead of sys.prefix or
114 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000115 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000116 if prefix is None:
117 prefix = plat_specific and EXEC_PREFIX or PREFIX
118
119 if os.name == "posix":
120 libpython = os.path.join(prefix,
121 "lib", "python" + get_python_version())
122 if standard_lib:
123 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000124 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000125 return os.path.join(libpython, "site-packages")
126 elif os.name == "nt":
127 if standard_lib:
128 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000129 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000130 if get_python_version() < "2.2":
131 return prefix
132 else:
133 return os.path.join(prefix, "Lib", "site-packages")
134 elif os.name == "mac":
135 if plat_specific:
136 if standard_lib:
137 return os.path.join(prefix, "Lib", "lib-dynload")
138 else:
139 return os.path.join(prefix, "Lib", "site-packages")
140 else:
141 if standard_lib:
142 return os.path.join(prefix, "Lib")
143 else:
144 return os.path.join(prefix, "Lib", "site-packages")
145 elif os.name == "os2":
146 if standard_lib:
147 return os.path.join(prefix, "Lib")
148 else:
149 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000150 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000151 raise DistutilsPlatformError(
152 "I don't know where Python installs its library "
153 "on platform '%s'" % os.name)
154
155
156def customize_compiler(compiler):
157 """Do any platform-specific customization of a CCompiler instance.
158
159 Mainly needed on Unix, so we can plug in the information that
160 varies across Unices and is stored in Python's Makefile.
161 """
162 if compiler.compiler_type == "unix":
163 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
164 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
165 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
166
167 if 'CC' in os.environ:
168 cc = os.environ['CC']
169 if 'CXX' in os.environ:
170 cxx = os.environ['CXX']
171 if 'LDSHARED' in os.environ:
172 ldshared = os.environ['LDSHARED']
173 if 'CPP' in os.environ:
174 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000175 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000176 cpp = cc + " -E" # not always
177 if 'LDFLAGS' in os.environ:
178 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
179 if 'CFLAGS' in os.environ:
180 cflags = opt + ' ' + os.environ['CFLAGS']
181 ldshared = ldshared + ' ' + os.environ['CFLAGS']
182 if 'CPPFLAGS' in os.environ:
183 cpp = cpp + ' ' + os.environ['CPPFLAGS']
184 cflags = cflags + ' ' + os.environ['CPPFLAGS']
185 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
186 if 'AR' in os.environ:
187 ar = os.environ['AR']
188 if 'ARFLAGS' in os.environ:
189 archiver = ar + ' ' + os.environ['ARFLAGS']
190 else:
191 archiver = ar + ' ' + ar_flags
192
193 cc_cmd = cc + ' ' + cflags
194 compiler.set_executables(
195 preprocessor=cpp,
196 compiler=cc_cmd,
197 compiler_so=cc_cmd + ' ' + ccshared,
198 compiler_cxx=cxx,
199 linker_so=ldshared,
200 linker_exe=cc,
201 archiver=archiver)
202
203 compiler.shared_lib_extension = so_ext
204
205
206def get_config_h_filename():
207 """Return full pathname of installed pyconfig.h file."""
208 if python_build:
209 if os.name == "nt":
210 inc_dir = os.path.join(project_base, "PC")
211 else:
212 inc_dir = project_base
213 else:
214 inc_dir = get_python_inc(plat_specific=1)
215 if get_python_version() < '2.2':
216 config_h = 'config.h'
217 else:
218 # The name of the config.h file changed in 2.2
219 config_h = 'pyconfig.h'
220 return os.path.join(inc_dir, config_h)
221
Greg Ward1190ee31998-12-18 23:46:33 +0000222
Greg Ward9ddaaa11999-01-06 14:46:06 +0000223def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000224 """Return full pathname of installed Makefile from the Python build."""
225 if python_build:
226 return os.path.join(os.path.dirname(sys.executable), "Makefile")
227 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
228 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000229
Tarek Ziadé36797272010-07-22 12:50:05 +0000230
231def parse_config_h(fp, g=None):
232 """Parse a config.h-style file.
233
234 A dictionary containing name/value pairs is returned. If an
235 optional dictionary is passed in as the second argument, it is
236 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000237 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000238 if g is None:
239 g = {}
240 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
241 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
242 #
243 while True:
244 line = fp.readline()
245 if not line:
246 break
247 m = define_rx.match(line)
248 if m:
249 n, v = m.group(1, 2)
250 try: v = int(v)
251 except ValueError: pass
252 g[n] = v
253 else:
254 m = undef_rx.match(line)
255 if m:
256 g[m.group(1)] = 0
257 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000258
Greg Wardd283ce72000-09-17 00:53:02 +0000259
260# Regexes needed for parsing Makefile (and similar syntaxes,
261# like old-style Setup files).
262_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
263_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
264_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
265
Greg Ward3fff8d22000-09-15 00:03:13 +0000266def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000267 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000268
269 A dictionary containing name/value pairs is returned. If an
270 optional dictionary is passed in as the second argument, it is
271 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000272 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000273 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000274 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000275
276 if g is None:
277 g = {}
278 done = {}
279 notdone = {}
280
281 while True:
282 line = fp.readline()
283 if line is None: # eof
284 break
285 m = _variable_rx.match(line)
286 if m:
287 n, v = m.group(1, 2)
288 v = v.strip()
289 # `$$' is a literal `$' in make
290 tmpv = v.replace('$$', '')
291
292 if "$" in tmpv:
293 notdone[n] = v
294 else:
295 try:
296 v = int(v)
297 except ValueError:
298 # insert literal `$'
299 done[n] = v.replace('$$', '$')
300 else:
301 done[n] = v
302
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000303 # Variables with a 'PY_' prefix in the makefile. These need to
304 # be made available without that prefix through sysconfig.
305 # Special care is needed to ensure that variable expansion works, even
306 # if the expansion uses the name without a prefix.
307 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
308
Tarek Ziadé36797272010-07-22 12:50:05 +0000309 # do variable interpolation here
310 while notdone:
311 for name in list(notdone):
312 value = notdone[name]
313 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
314 if m:
315 n = m.group(1)
316 found = True
317 if n in done:
318 item = str(done[n])
319 elif n in notdone:
320 # get it on a subsequent round
321 found = False
322 elif n in os.environ:
323 # do it like make: fall back to environment
324 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000325
326 elif n in renamed_variables:
327 if name.startswith('PY_') and name[3:] in renamed_variables:
328 item = ""
329
330 elif 'PY_' + n in notdone:
331 found = False
332
333 else:
334 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000335 else:
336 done[n] = item = ""
337 if found:
338 after = value[m.end():]
339 value = value[:m.start()] + item + after
340 if "$" in after:
341 notdone[name] = value
342 else:
343 try: value = int(value)
344 except ValueError:
345 done[name] = value.strip()
346 else:
347 done[name] = value
348 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000349
350 if name.startswith('PY_') \
351 and name[3:] in renamed_variables:
352
353 name = name[3:]
354 if name not in done:
355 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000356 else:
357 # bogus variable reference; just drop it since we can't deal
358 del notdone[name]
359
360 fp.close()
361
Antoine Pitroudbec7802010-10-10 09:37:12 +0000362 # strip spurious spaces
363 for k, v in done.items():
364 if isinstance(v, str):
365 done[k] = v.strip()
366
Tarek Ziadé36797272010-07-22 12:50:05 +0000367 # save the results in the global dictionary
368 g.update(done)
369 return g
370
Greg Ward1190ee31998-12-18 23:46:33 +0000371
Greg Wardd283ce72000-09-17 00:53:02 +0000372def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000373 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000374 'string' according to 'vars' (a dictionary mapping variable names to
375 values). Variables not present in 'vars' are silently expanded to the
376 empty string. The variable values in 'vars' should not contain further
377 variable expansions; if 'vars' is the output of 'parse_makefile()',
378 you're fine. Returns a variable-expanded version of 's'.
379 """
380
381 # This algorithm does multiple expansion, so if vars['foo'] contains
382 # "${bar}", it will expand ${foo} to ${bar}, and then expand
383 # ${bar}... and so forth. This is fine as long as 'vars' comes from
384 # 'parse_makefile()', which takes care of such expansions eagerly,
385 # according to make's variable expansion semantics.
386
Collin Winter5b7e9d72007-08-30 03:52:21 +0000387 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000388 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
389 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000390 (beg, end) = m.span()
391 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
392 else:
393 break
394 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000395
396
397_config_vars = None
398
399def _init_posix():
400 """Initialize the module as appropriate for POSIX systems."""
401 g = {}
402 # load the installed Makefile:
403 try:
404 filename = get_makefile_filename()
405 parse_makefile(filename, g)
406 except IOError as msg:
407 my_msg = "invalid Python installation: unable to open %s" % filename
408 if hasattr(msg, "strerror"):
409 my_msg = my_msg + " (%s)" % msg.strerror
410
411 raise DistutilsPlatformError(my_msg)
412
413 # load the installed pyconfig.h:
414 try:
415 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000416 with open(filename) as file:
417 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000418 except IOError as msg:
419 my_msg = "invalid Python installation: unable to open %s" % filename
420 if hasattr(msg, "strerror"):
421 my_msg = my_msg + " (%s)" % msg.strerror
422
423 raise DistutilsPlatformError(my_msg)
424
425 # On MacOSX we need to check the setting of the environment variable
426 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
427 # it needs to be compatible.
428 # If it isn't set we set it to the configure-time value
429 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
430 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
431 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
432 if cur_target == '':
433 cur_target = cfg_target
434 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
435 elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
436 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
437 % (cur_target, cfg_target))
438 raise DistutilsPlatformError(my_msg)
439
440 # On AIX, there are wrong paths to the linker scripts in the Makefile
441 # -- these paths are relative to the Python source, but when installed
442 # the scripts are in another directory.
443 if python_build:
444 g['LDSHARED'] = g['BLDSHARED']
445
446 elif get_python_version() < '2.1':
447 # The following two branches are for 1.5.2 compatibility.
448 if sys.platform == 'aix4': # what about AIX 3.x ?
449 # Linker script is in the config directory, not in Modules as the
450 # Makefile says.
451 python_lib = get_python_lib(standard_lib=1)
452 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
453 python_exp = os.path.join(python_lib, 'config', 'python.exp')
454
455 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
456
457 global _config_vars
458 _config_vars = g
459
460
461def _init_nt():
462 """Initialize the module as appropriate for NT"""
463 g = {}
464 # set basic install directories
465 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
466 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
467
468 # XXX hmmm.. a normal install puts include files here
469 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
470
471 g['SO'] = '.pyd'
472 g['EXE'] = ".exe"
473 g['VERSION'] = get_python_version().replace(".", "")
474 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
475
476 global _config_vars
477 _config_vars = g
478
479
480def _init_mac():
481 """Initialize the module as appropriate for Macintosh systems"""
482 g = {}
483 # set basic install directories
484 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
485 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
486
487 # XXX hmmm.. a normal install puts include files here
488 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
489
490 import MacOS
491 if not hasattr(MacOS, 'runtimemodel'):
492 g['SO'] = '.ppc.slb'
493 else:
494 g['SO'] = '.%s.slb' % MacOS.runtimemodel
495
496 # XXX are these used anywhere?
497 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
498 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
499
500 # These are used by the extension module build
501 g['srcdir'] = ':'
502 global _config_vars
503 _config_vars = g
504
505
506def _init_os2():
507 """Initialize the module as appropriate for OS/2"""
508 g = {}
509 # set basic install directories
510 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
511 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
512
513 # XXX hmmm.. a normal install puts include files here
514 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
515
516 g['SO'] = '.pyd'
517 g['EXE'] = ".exe"
518
519 global _config_vars
520 _config_vars = g
521
522
523def get_config_vars(*args):
524 """With no arguments, return a dictionary of all configuration
525 variables relevant for the current platform. Generally this includes
526 everything needed to build extensions and install both pure modules and
527 extensions. On Unix, this means every variable defined in Python's
528 installed Makefile; on Windows and Mac OS it's a much smaller set.
529
530 With arguments, return a list of values that result from looking up
531 each argument in the configuration variable dictionary.
532 """
533 global _config_vars
534 if _config_vars is None:
535 func = globals().get("_init_" + os.name)
536 if func:
537 func()
538 else:
539 _config_vars = {}
540
541 # Normalized versions of prefix and exec_prefix are handy to have;
542 # in fact, these are the standard versions used most places in the
543 # Distutils.
544 _config_vars['prefix'] = PREFIX
545 _config_vars['exec_prefix'] = EXEC_PREFIX
546
547 # Convert srcdir into an absolute path if it appears necessary.
548 # Normally it is relative to the build directory. However, during
549 # testing, for example, we might be running a non-installed python
550 # from a different directory.
551 if python_build and os.name == "posix":
552 base = os.path.dirname(os.path.abspath(sys.executable))
553 if (not os.path.isabs(_config_vars['srcdir']) and
554 base != os.getcwd()):
555 # srcdir is relative and we are not in the same directory
556 # as the executable. Assume executable is in the build
557 # directory and make srcdir absolute.
558 srcdir = os.path.join(base, _config_vars['srcdir'])
559 _config_vars['srcdir'] = os.path.normpath(srcdir)
560
561 if sys.platform == 'darwin':
562 kernel_version = os.uname()[2] # Kernel version (8.4.3)
563 major_version = int(kernel_version.split('.')[0])
564
565 if major_version < 8:
566 # On Mac OS X before 10.4, check if -arch and -isysroot
567 # are in CFLAGS or LDFLAGS and remove them if they are.
568 # This is needed when building extensions on a 10.3 system
569 # using a universal build of python.
570 for key in ('LDFLAGS', 'BASECFLAGS',
571 # a number of derived variables. These need to be
572 # patched up as well.
573 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
574 flags = _config_vars[key]
575 flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
576 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
577 _config_vars[key] = flags
578
579 else:
580
581 # Allow the user to override the architecture flags using
582 # an environment variable.
583 # NOTE: This name was introduced by Apple in OSX 10.5 and
584 # is used by several scripting languages distributed with
585 # that OS release.
586
587 if 'ARCHFLAGS' in os.environ:
588 arch = os.environ['ARCHFLAGS']
589 for key in ('LDFLAGS', 'BASECFLAGS',
590 # a number of derived variables. These need to be
591 # patched up as well.
592 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
593
594 flags = _config_vars[key]
595 flags = re.sub('-arch\s+\w+\s', ' ', flags)
596 flags = flags + ' ' + arch
597 _config_vars[key] = flags
598
599 if args:
600 vals = []
601 for name in args:
602 vals.append(_config_vars.get(name))
603 return vals
604 else:
605 return _config_vars
606
607def get_config_var(name):
608 """Return the value of a single variable using the dictionary
609 returned by 'get_config_vars()'. Equivalent to
610 get_config_vars().get(name)
611 """
612 return get_config_vars().get(name)