blob: 4f9041a794a5e94ec85eb610dfde88f1668ce5bd [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
Tarek Ziadédd7bef92010-03-05 00:16:02 +000014import os
Greg Ward9ddaaa11999-01-06 14:46:06 +000015import re
Tarek Ziadédd7bef92010-03-05 00:16:02 +000016import string
17import sys
Greg Ward1190ee31998-12-18 23:46:33 +000018
Tarek Ziadédd7bef92010-03-05 00:16:02 +000019from distutils.errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000020
Tarek Ziadédd7bef92010-03-05 00:16:02 +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)
Tarek Ziadé0276c7a2010-01-26 21:21:54 +000024
Tarek Ziadédd7bef92010-03-05 00:16:02 +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))
Fred Drakec1ee39a2000-03-09 15:54:52 +000039
Tarek Ziadédd7bef92010-03-05 00:16:02 +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
Marc-André Lemburg2db7cd32008-02-05 14:50:40 +000045def _python_build():
Tarek Ziadédd7bef92010-03-05 00:16:02 +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
Marc-André Lemburg2db7cd32008-02-05 14:50:40 +000050python_build = _python_build()
Fred Drakec1ee39a2000-03-09 15:54:52 +000051
Tarek Ziadé5633a802010-01-23 09:23:15 +000052
Tarek Ziadédd7bef92010-03-05 00:16:02 +000053def get_python_version():
54 """Return a string containing the major and minor Python version,
55 leaving off the patchlevel. Sample return values could be '1.5'
56 or '2.2'.
57 """
58 return sys.version[:3]
59
60
61def get_python_inc(plat_specific=0, prefix=None):
62 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000063
64 If 'plat_specific' is false (the default), this is the path to the
65 non-platform-specific header files, i.e. Python.h and so on;
66 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000067 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000068
Greg Wardd38e6f72000-04-10 01:17:49 +000069 If 'prefix' is supplied, use it instead of sys.prefix or
70 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000071 """
Tarek Ziadédd7bef92010-03-05 00:16:02 +000072 if prefix is None:
73 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadéa5cd1822010-04-30 12:15:12 +000074
Tarek Ziadédd7bef92010-03-05 00:16:02 +000075 if os.name == "posix":
76 if python_build:
Tarek Ziadéa5cd1822010-04-30 12:15:12 +000077 buildir = os.path.dirname(sys.executable)
Tarek Ziadédd7bef92010-03-05 00:16:02 +000078 if plat_specific:
Tarek Ziadéa5cd1822010-04-30 12:15:12 +000079 # python.h is located in the buildir
80 inc_dir = buildir
Tarek Ziadédd7bef92010-03-05 00:16:02 +000081 else:
Tarek Ziadéa5cd1822010-04-30 12:15:12 +000082 # the source dir is relative to the buildir
83 srcdir = os.path.abspath(os.path.join(buildir,
84 get_config_var('srcdir')))
85 # Include is located in the srcdir
86 inc_dir = os.path.join(srcdir, "Include")
Tarek Ziadédd7bef92010-03-05 00:16:02 +000087 return inc_dir
88 return os.path.join(prefix, "include", "python" + get_python_version())
89 elif os.name == "nt":
90 return os.path.join(prefix, "include")
Tarek Ziadédd7bef92010-03-05 00:16:02 +000091 elif os.name == "os2":
92 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000093 else:
Tarek Ziadédd7bef92010-03-05 00:16:02 +000094 raise DistutilsPlatformError(
95 "I don't know where Python installs its C header files "
96 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000097
98
Tarek Ziadédd7bef92010-03-05 00:16:02 +000099def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
100 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000101 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000102
Fred Drakec1ee39a2000-03-09 15:54:52 +0000103 If 'plat_specific' is true, return the directory containing
104 platform-specific modules, i.e. any module from a non-pure-Python
105 module distribution; otherwise, return the platform-shared library
106 directory. If 'standard_lib' is true, return the directory
107 containing standard Python library modules; otherwise, return the
108 directory for site-specific modules.
109
Greg Wardd38e6f72000-04-10 01:17:49 +0000110 If 'prefix' is supplied, use it instead of sys.prefix or
111 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000112 """
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000113 if prefix is None:
114 prefix = plat_specific and EXEC_PREFIX or PREFIX
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000115
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000116 if os.name == "posix":
117 libpython = os.path.join(prefix,
118 "lib", "python" + get_python_version())
119 if standard_lib:
120 return libpython
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000121 else:
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000122 return os.path.join(libpython, "site-packages")
123
124 elif os.name == "nt":
125 if standard_lib:
126 return os.path.join(prefix, "Lib")
127 else:
128 if get_python_version() < "2.2":
129 return prefix
130 else:
131 return os.path.join(prefix, "Lib", "site-packages")
132
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000133 elif os.name == "os2":
134 if standard_lib:
135 return os.path.join(prefix, "Lib")
136 else:
137 return os.path.join(prefix, "Lib", "site-packages")
138
Greg Ward7d73b9e2000-03-09 03:16:05 +0000139 else:
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000140 raise DistutilsPlatformError(
141 "I don't know where Python installs its library "
142 "on platform '%s'" % os.name)
143
Ned Deily0d0ea482012-02-10 12:59:06 +0100144_USE_CLANG = None
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000145
146def customize_compiler(compiler):
147 """Do any platform-specific customization of a CCompiler instance.
148
149 Mainly needed on Unix, so we can plug in the information that
150 varies across Unices and is stored in Python's Makefile.
151 """
152 if compiler.compiler_type == "unix":
153 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
154 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
155 'CCSHARED', 'LDSHARED', 'SO')
156
Ned Deily0d0ea482012-02-10 12:59:06 +0100157 newcc = None
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000158 if 'CC' in os.environ:
Ned Deily0d0ea482012-02-10 12:59:06 +0100159 newcc = os.environ['CC']
160 elif sys.platform == 'darwin' and cc == 'gcc-4.2':
161 # Issue #13590:
162 # Since Apple removed gcc-4.2 in Xcode 4.2, we can no
163 # longer assume it is available for extension module builds.
164 # If Python was built with gcc-4.2, check first to see if
165 # it is available on this system; if not, try to use clang
166 # instead unless the caller explicitly set CC.
167 global _USE_CLANG
168 if _USE_CLANG is None:
169 from distutils import log
170 from subprocess import Popen, PIPE
171 p = Popen("! type gcc-4.2 && type clang && exit 2",
172 shell=True, stdout=PIPE, stderr=PIPE)
173 p.wait()
174 if p.returncode == 2:
175 _USE_CLANG = True
176 log.warn("gcc-4.2 not found, using clang instead")
177 else:
178 _USE_CLANG = False
179 if _USE_CLANG:
180 newcc = 'clang'
181 if newcc:
182 # On OS X, if CC is overridden, use that as the default
183 # command for LDSHARED as well
184 if (sys.platform == 'darwin'
185 and 'LDSHARED' not in os.environ
186 and ldshared.startswith(cc)):
187 ldshared = newcc + ldshared[len(cc):]
188 cc = newcc
Tarek Ziadédd7bef92010-03-05 00:16:02 +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édd7bef92010-03-05 00:16:02 +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
207 cc_cmd = cc + ' ' + cflags
208 compiler.set_executables(
209 preprocessor=cpp,
210 compiler=cc_cmd,
211 compiler_so=cc_cmd + ' ' + ccshared,
212 compiler_cxx=cxx,
213 linker_so=ldshared,
214 linker_exe=cc)
215
216 compiler.shared_lib_extension = so_ext
217
218
219def get_config_h_filename():
220 """Return full pathname of installed pyconfig.h file."""
221 if python_build:
222 if os.name == "nt":
223 inc_dir = os.path.join(project_base, "PC")
224 else:
225 inc_dir = project_base
226 else:
227 inc_dir = get_python_inc(plat_specific=1)
228 if get_python_version() < '2.2':
229 config_h = 'config.h'
230 else:
231 # The name of the config.h file changed in 2.2
232 config_h = 'pyconfig.h'
233 return os.path.join(inc_dir, config_h)
234
Greg Ward1190ee31998-12-18 23:46:33 +0000235
Greg Ward9ddaaa11999-01-06 14:46:06 +0000236def get_makefile_filename():
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000237 """Return full pathname of installed Makefile from the Python build."""
238 if python_build:
239 return os.path.join(os.path.dirname(sys.executable), "Makefile")
240 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
241 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000242
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000243
244def parse_config_h(fp, g=None):
245 """Parse a config.h-style file.
246
247 A dictionary containing name/value pairs is returned. If an
248 optional dictionary is passed in as the second argument, it is
249 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000250 """
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000251 if g is None:
252 g = {}
253 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
254 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
255 #
256 while 1:
257 line = fp.readline()
258 if not line:
259 break
260 m = define_rx.match(line)
261 if m:
262 n, v = m.group(1, 2)
263 try: v = int(v)
264 except ValueError: pass
265 g[n] = v
266 else:
267 m = undef_rx.match(line)
268 if m:
269 g[m.group(1)] = 0
270 return g
271
Greg Wardd283ce72000-09-17 00:53:02 +0000272
273# Regexes needed for parsing Makefile (and similar syntaxes,
274# like old-style Setup files).
275_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
276_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
277_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
278
Greg Ward3fff8d22000-09-15 00:03:13 +0000279def parse_makefile(fn, g=None):
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000280 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000281
282 A dictionary containing name/value pairs is returned. If an
283 optional dictionary is passed in as the second argument, it is
284 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000285 """
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000286 from distutils.text_file import TextFile
287 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
288
289 if g is None:
290 g = {}
291 done = {}
292 notdone = {}
293
294 while 1:
295 line = fp.readline()
296 if line is None: # eof
297 break
298 m = _variable_rx.match(line)
299 if m:
300 n, v = m.group(1, 2)
301 v = v.strip()
302 # `$$' is a literal `$' in make
303 tmpv = v.replace('$$', '')
304
305 if "$" in tmpv:
306 notdone[n] = v
307 else:
308 try:
309 v = int(v)
310 except ValueError:
311 # insert literal `$'
312 done[n] = v.replace('$$', '$')
313 else:
314 done[n] = v
315
316 # do variable interpolation here
317 while notdone:
318 for name in notdone.keys():
319 value = notdone[name]
320 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
321 if m:
322 n = m.group(1)
323 found = True
324 if n in done:
325 item = str(done[n])
326 elif n in notdone:
327 # get it on a subsequent round
328 found = False
329 elif n in os.environ:
330 # do it like make: fall back to environment
331 item = os.environ[n]
332 else:
333 done[n] = item = ""
334 if found:
335 after = value[m.end():]
336 value = value[:m.start()] + item + after
337 if "$" in after:
338 notdone[name] = value
339 else:
340 try: value = int(value)
341 except ValueError:
342 done[name] = value.strip()
343 else:
344 done[name] = value
345 del notdone[name]
346 else:
347 # bogus variable reference; just drop it since we can't deal
348 del notdone[name]
349
350 fp.close()
351
Antoine Pitrou58dab672010-10-10 09:54:59 +0000352 # strip spurious spaces
353 for k, v in done.items():
354 if isinstance(v, str):
355 done[k] = v.strip()
356
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000357 # save the results in the global dictionary
358 g.update(done)
359 return g
360
Greg Ward1190ee31998-12-18 23:46:33 +0000361
Greg Wardd283ce72000-09-17 00:53:02 +0000362def expand_makefile_vars(s, vars):
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000363 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000364 'string' according to 'vars' (a dictionary mapping variable names to
365 values). Variables not present in 'vars' are silently expanded to the
366 empty string. The variable values in 'vars' should not contain further
367 variable expansions; if 'vars' is the output of 'parse_makefile()',
368 you're fine. Returns a variable-expanded version of 's'.
369 """
370
371 # This algorithm does multiple expansion, so if vars['foo'] contains
372 # "${bar}", it will expand ${foo} to ${bar}, and then expand
373 # ${bar}... and so forth. This is fine as long as 'vars' comes from
374 # 'parse_makefile()', which takes care of such expansions eagerly,
375 # according to make's variable expansion semantics.
376
377 while 1:
378 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
379 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000380 (beg, end) = m.span()
381 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
382 else:
383 break
384 return s
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000385
386
387_config_vars = None
388
389def _init_posix():
390 """Initialize the module as appropriate for POSIX systems."""
391 g = {}
392 # load the installed Makefile:
393 try:
394 filename = get_makefile_filename()
395 parse_makefile(filename, g)
396 except IOError, msg:
397 my_msg = "invalid Python installation: unable to open %s" % filename
398 if hasattr(msg, "strerror"):
399 my_msg = my_msg + " (%s)" % msg.strerror
400
401 raise DistutilsPlatformError(my_msg)
402
403 # load the installed pyconfig.h:
404 try:
405 filename = get_config_h_filename()
406 parse_config_h(file(filename), g)
407 except IOError, msg:
408 my_msg = "invalid Python installation: unable to open %s" % filename
409 if hasattr(msg, "strerror"):
410 my_msg = my_msg + " (%s)" % msg.strerror
411
412 raise DistutilsPlatformError(my_msg)
413
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000414 # On AIX, there are wrong paths to the linker scripts in the Makefile
415 # -- these paths are relative to the Python source, but when installed
416 # the scripts are in another directory.
417 if python_build:
418 g['LDSHARED'] = g['BLDSHARED']
419
420 elif get_python_version() < '2.1':
421 # The following two branches are for 1.5.2 compatibility.
422 if sys.platform == 'aix4': # what about AIX 3.x ?
423 # Linker script is in the config directory, not in Modules as the
424 # Makefile says.
425 python_lib = get_python_lib(standard_lib=1)
426 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
427 python_exp = os.path.join(python_lib, 'config', 'python.exp')
428
429 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
430
431 elif sys.platform == 'beos':
432 # Linker script is in the config directory. In the Makefile it is
433 # relative to the srcdir, which after installation no longer makes
434 # sense.
435 python_lib = get_python_lib(standard_lib=1)
436 linkerscript_path = string.split(g['LDSHARED'])[0]
437 linkerscript_name = os.path.basename(linkerscript_path)
438 linkerscript = os.path.join(python_lib, 'config',
439 linkerscript_name)
440
441 # XXX this isn't the right place to do this: adding the Python
442 # library to the link, if needed, should be in the "build_ext"
443 # command. (It's also needed for non-MS compilers on Windows, and
444 # it's taken care of for them by the 'build_ext.get_libraries()'
445 # method.)
446 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
447 (linkerscript, PREFIX, get_python_version()))
448
449 global _config_vars
450 _config_vars = g
451
452
453def _init_nt():
454 """Initialize the module as appropriate for NT"""
455 g = {}
456 # set basic install directories
457 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
458 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
459
460 # XXX hmmm.. a normal install puts include files here
461 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
462
463 g['SO'] = '.pyd'
464 g['EXE'] = ".exe"
465 g['VERSION'] = get_python_version().replace(".", "")
466 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
467
468 global _config_vars
469 _config_vars = g
470
471
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000472def _init_os2():
473 """Initialize the module as appropriate for OS/2"""
474 g = {}
475 # set basic install directories
476 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
477 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
478
479 # XXX hmmm.. a normal install puts include files here
480 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
481
482 g['SO'] = '.pyd'
483 g['EXE'] = ".exe"
484
485 global _config_vars
486 _config_vars = g
487
488
489def get_config_vars(*args):
490 """With no arguments, return a dictionary of all configuration
491 variables relevant for the current platform. Generally this includes
492 everything needed to build extensions and install both pure modules and
493 extensions. On Unix, this means every variable defined in Python's
494 installed Makefile; on Windows and Mac OS it's a much smaller set.
495
496 With arguments, return a list of values that result from looking up
497 each argument in the configuration variable dictionary.
498 """
499 global _config_vars
500 if _config_vars is None:
501 func = globals().get("_init_" + os.name)
502 if func:
503 func()
504 else:
505 _config_vars = {}
506
507 # Normalized versions of prefix and exec_prefix are handy to have;
508 # in fact, these are the standard versions used most places in the
509 # Distutils.
510 _config_vars['prefix'] = PREFIX
511 _config_vars['exec_prefix'] = EXEC_PREFIX
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', 'LDSHARED',
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
531 else:
532
533 # Allow the user to override the architecture flags using
534 # an environment variable.
535 # NOTE: This name was introduced by Apple in OSX 10.5 and
536 # is used by several scripting languages distributed with
537 # that OS release.
538
539 if 'ARCHFLAGS' in os.environ:
540 arch = os.environ['ARCHFLAGS']
541 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
542 # a number of derived variables. These need to be
543 # patched up as well.
544 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
545
546 flags = _config_vars[key]
547 flags = re.sub('-arch\s+\w+\s', ' ', flags)
548 flags = flags + ' ' + arch
549 _config_vars[key] = flags
550
551 # If we're on OSX 10.5 or later and the user tries to
552 # compiles an extension using an SDK that is not present
553 # on the current machine it is better to not use an SDK
554 # than to fail.
555 #
556 # The major usecase for this is users using a Python.org
557 # binary installer on OSX 10.6: that installer uses
558 # the 10.4u SDK, but that SDK is not installed by default
559 # when you install Xcode.
560 #
561 m = re.search('-isysroot\s+(\S+)', _config_vars['CFLAGS'])
562 if m is not None:
563 sdk = m.group(1)
564 if not os.path.exists(sdk):
565 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
566 # a number of derived variables. These need to be
567 # patched up as well.
568 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
569
570 flags = _config_vars[key]
571 flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags)
572 _config_vars[key] = flags
573
574 if args:
575 vals = []
576 for name in args:
577 vals.append(_config_vars.get(name))
578 return vals
579 else:
580 return _config_vars
581
582def get_config_var(name):
583 """Return the value of a single variable using the dictionary
584 returned by 'get_config_vars()'. Equivalent to
585 get_config_vars().get(name)
586 """
587 return get_config_vars().get(name)