blob: 3567db83497f5e8930c79048197662013f80f670 [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
Vinay Sajip61c3f0d2010-09-20 10:13:13 +000059def _get_build_dir(name, plat_specific):
60 # Assume the executable is in the build directory. The
61 # pyconfig.h file should be in the same directory. Since
62 # the build directory may not be the source directory, we
63 # must use "srcdir" from the makefile to find the "Include"
64 # directory.
65 base = os.path.dirname(os.path.abspath(sys.executable))
66 if plat_specific:
67 return base
68 else:
69 thedir = os.path.join(get_config_var('srcdir'), name)
70 return os.path.normpath(thedir)
Tarek Ziadé36797272010-07-22 12:50:05 +000071
72def get_python_inc(plat_specific=0, prefix=None):
73 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000074
75 If 'plat_specific' is false (the default), this is the path to the
76 non-platform-specific header files, i.e. Python.h and so on;
77 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000078 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000079
Greg Wardd38e6f72000-04-10 01:17:49 +000080 If 'prefix' is supplied, use it instead of sys.prefix or
81 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000082 """
Tarek Ziadé36797272010-07-22 12:50:05 +000083 if prefix is None:
84 prefix = plat_specific and EXEC_PREFIX or PREFIX
85 if os.name == "posix":
86 if python_build:
Vinay Sajip61c3f0d2010-09-20 10:13:13 +000087 return _get_build_dir('Include', plat_specific)
Tarek Ziadé36797272010-07-22 12:50:05 +000088 return os.path.join(prefix, "include", "python" + get_python_version())
89 elif os.name == "nt":
90 return os.path.join(prefix, "include")
91 elif os.name == "mac":
92 if plat_specific:
93 return os.path.join(prefix, "Mac", "Include")
94 else:
95 return os.path.join(prefix, "Include")
96 elif os.name == "os2":
97 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000098 else:
Tarek Ziadé36797272010-07-22 12:50:05 +000099 raise DistutilsPlatformError(
100 "I don't know where Python installs its C header files "
101 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000102
103
Tarek Ziadé36797272010-07-22 12:50:05 +0000104def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
105 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000106 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000107
Fred Drakec1ee39a2000-03-09 15:54:52 +0000108 If 'plat_specific' is true, return the directory containing
109 platform-specific modules, i.e. any module from a non-pure-Python
110 module distribution; otherwise, return the platform-shared library
111 directory. If 'standard_lib' is true, return the directory
112 containing standard Python library modules; otherwise, return the
113 directory for site-specific modules.
114
Greg Wardd38e6f72000-04-10 01:17:49 +0000115 If 'prefix' is supplied, use it instead of sys.prefix or
116 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000117 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000118 if prefix is None:
119 prefix = plat_specific and EXEC_PREFIX or PREFIX
120
121 if os.name == "posix":
Vinay Sajip61c3f0d2010-09-20 10:13:13 +0000122 if python_build:
123 return _get_build_dir('Lib', plat_specific)
Tarek Ziadé36797272010-07-22 12:50:05 +0000124 libpython = os.path.join(prefix,
125 "lib", "python" + get_python_version())
126 if standard_lib:
127 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000128 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000129 return os.path.join(libpython, "site-packages")
130 elif os.name == "nt":
131 if standard_lib:
132 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000133 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000134 if get_python_version() < "2.2":
135 return prefix
136 else:
137 return os.path.join(prefix, "Lib", "site-packages")
138 elif os.name == "mac":
139 if plat_specific:
140 if standard_lib:
141 return os.path.join(prefix, "Lib", "lib-dynload")
142 else:
143 return os.path.join(prefix, "Lib", "site-packages")
144 else:
145 if standard_lib:
146 return os.path.join(prefix, "Lib")
147 else:
148 return os.path.join(prefix, "Lib", "site-packages")
149 elif os.name == "os2":
150 if standard_lib:
151 return os.path.join(prefix, "Lib")
152 else:
153 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000154 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000155 raise DistutilsPlatformError(
156 "I don't know where Python installs its library "
157 "on platform '%s'" % os.name)
158
159
160def customize_compiler(compiler):
161 """Do any platform-specific customization of a CCompiler instance.
162
163 Mainly needed on Unix, so we can plug in the information that
164 varies across Unices and is stored in Python's Makefile.
165 """
166 if compiler.compiler_type == "unix":
167 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
168 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
169 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
170
171 if 'CC' in os.environ:
172 cc = os.environ['CC']
173 if 'CXX' in os.environ:
174 cxx = os.environ['CXX']
175 if 'LDSHARED' in os.environ:
176 ldshared = os.environ['LDSHARED']
177 if 'CPP' in os.environ:
178 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000179 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000180 cpp = cc + " -E" # not always
181 if 'LDFLAGS' in os.environ:
182 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
183 if 'CFLAGS' in os.environ:
184 cflags = opt + ' ' + os.environ['CFLAGS']
185 ldshared = ldshared + ' ' + os.environ['CFLAGS']
186 if 'CPPFLAGS' in os.environ:
187 cpp = cpp + ' ' + os.environ['CPPFLAGS']
188 cflags = cflags + ' ' + os.environ['CPPFLAGS']
189 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
190 if 'AR' in os.environ:
191 ar = os.environ['AR']
192 if 'ARFLAGS' in os.environ:
193 archiver = ar + ' ' + os.environ['ARFLAGS']
194 else:
195 archiver = ar + ' ' + ar_flags
196
197 cc_cmd = cc + ' ' + cflags
198 compiler.set_executables(
199 preprocessor=cpp,
200 compiler=cc_cmd,
201 compiler_so=cc_cmd + ' ' + ccshared,
202 compiler_cxx=cxx,
203 linker_so=ldshared,
204 linker_exe=cc,
205 archiver=archiver)
206
207 compiler.shared_lib_extension = so_ext
208
209
210def get_config_h_filename():
211 """Return full pathname of installed pyconfig.h file."""
212 if python_build:
213 if os.name == "nt":
214 inc_dir = os.path.join(project_base, "PC")
215 else:
216 inc_dir = project_base
217 else:
218 inc_dir = get_python_inc(plat_specific=1)
219 if get_python_version() < '2.2':
220 config_h = 'config.h'
221 else:
222 # The name of the config.h file changed in 2.2
223 config_h = 'pyconfig.h'
224 return os.path.join(inc_dir, config_h)
225
Greg Ward1190ee31998-12-18 23:46:33 +0000226
Greg Ward9ddaaa11999-01-06 14:46:06 +0000227def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000228 """Return full pathname of installed Makefile from the Python build."""
229 if python_build:
230 return os.path.join(os.path.dirname(sys.executable), "Makefile")
231 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
232 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000233
Tarek Ziadé36797272010-07-22 12:50:05 +0000234
235def parse_config_h(fp, g=None):
236 """Parse a config.h-style file.
237
238 A dictionary containing name/value pairs is returned. If an
239 optional dictionary is passed in as the second argument, it is
240 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000241 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000242 if g is None:
243 g = {}
244 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
245 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
246 #
247 while True:
248 line = fp.readline()
249 if not line:
250 break
251 m = define_rx.match(line)
252 if m:
253 n, v = m.group(1, 2)
254 try: v = int(v)
255 except ValueError: pass
256 g[n] = v
257 else:
258 m = undef_rx.match(line)
259 if m:
260 g[m.group(1)] = 0
261 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000262
Greg Wardd283ce72000-09-17 00:53:02 +0000263
264# Regexes needed for parsing Makefile (and similar syntaxes,
265# like old-style Setup files).
266_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
267_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
268_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
269
Greg Ward3fff8d22000-09-15 00:03:13 +0000270def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000271 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000272
273 A dictionary containing name/value pairs is returned. If an
274 optional dictionary is passed in as the second argument, it is
275 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000276 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000277 from distutils.text_file import TextFile
278 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
279
280 if g is None:
281 g = {}
282 done = {}
283 notdone = {}
284
285 while True:
286 line = fp.readline()
287 if line is None: # eof
288 break
289 m = _variable_rx.match(line)
290 if m:
291 n, v = m.group(1, 2)
292 v = v.strip()
293 # `$$' is a literal `$' in make
294 tmpv = v.replace('$$', '')
295
296 if "$" in tmpv:
297 notdone[n] = v
298 else:
299 try:
300 v = int(v)
301 except ValueError:
302 # insert literal `$'
303 done[n] = v.replace('$$', '$')
304 else:
305 done[n] = v
306
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000307 # Variables with a 'PY_' prefix in the makefile. These need to
308 # be made available without that prefix through sysconfig.
309 # Special care is needed to ensure that variable expansion works, even
310 # if the expansion uses the name without a prefix.
311 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
312
Tarek Ziadé36797272010-07-22 12:50:05 +0000313 # do variable interpolation here
314 while notdone:
315 for name in list(notdone):
316 value = notdone[name]
317 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
318 if m:
319 n = m.group(1)
320 found = True
321 if n in done:
322 item = str(done[n])
323 elif n in notdone:
324 # get it on a subsequent round
325 found = False
326 elif n in os.environ:
327 # do it like make: fall back to environment
328 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000329
330 elif n in renamed_variables:
331 if name.startswith('PY_') and name[3:] in renamed_variables:
332 item = ""
333
334 elif 'PY_' + n in notdone:
335 found = False
336
337 else:
338 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000339 else:
340 done[n] = item = ""
341 if found:
342 after = value[m.end():]
343 value = value[:m.start()] + item + after
344 if "$" in after:
345 notdone[name] = value
346 else:
347 try: value = int(value)
348 except ValueError:
349 done[name] = value.strip()
350 else:
351 done[name] = value
352 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000353
354 if name.startswith('PY_') \
355 and name[3:] in renamed_variables:
356
357 name = name[3:]
358 if name not in done:
359 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000360 else:
361 # bogus variable reference; just drop it since we can't deal
362 del notdone[name]
363
364 fp.close()
365
366 # save the results in the global dictionary
367 g.update(done)
368 return g
369
Greg Ward1190ee31998-12-18 23:46:33 +0000370
Greg Wardd283ce72000-09-17 00:53:02 +0000371def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000372 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000373 'string' according to 'vars' (a dictionary mapping variable names to
374 values). Variables not present in 'vars' are silently expanded to the
375 empty string. The variable values in 'vars' should not contain further
376 variable expansions; if 'vars' is the output of 'parse_makefile()',
377 you're fine. Returns a variable-expanded version of 's'.
378 """
379
380 # This algorithm does multiple expansion, so if vars['foo'] contains
381 # "${bar}", it will expand ${foo} to ${bar}, and then expand
382 # ${bar}... and so forth. This is fine as long as 'vars' comes from
383 # 'parse_makefile()', which takes care of such expansions eagerly,
384 # according to make's variable expansion semantics.
385
Collin Winter5b7e9d72007-08-30 03:52:21 +0000386 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000387 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
388 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000389 (beg, end) = m.span()
390 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
391 else:
392 break
393 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000394
395
396_config_vars = None
397
398def _init_posix():
399 """Initialize the module as appropriate for POSIX systems."""
400 g = {}
401 # load the installed Makefile:
402 try:
403 filename = get_makefile_filename()
404 parse_makefile(filename, g)
405 except IOError as msg:
406 my_msg = "invalid Python installation: unable to open %s" % filename
407 if hasattr(msg, "strerror"):
408 my_msg = my_msg + " (%s)" % msg.strerror
409
410 raise DistutilsPlatformError(my_msg)
411
412 # load the installed pyconfig.h:
413 try:
414 filename = get_config_h_filename()
415 parse_config_h(io.open(filename), g)
416 except IOError as msg:
417 my_msg = "invalid Python installation: unable to open %s" % filename
418 if hasattr(msg, "strerror"):
419 my_msg = my_msg + " (%s)" % msg.strerror
420
421 raise DistutilsPlatformError(my_msg)
422
423 # On MacOSX we need to check the setting of the environment variable
424 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
425 # it needs to be compatible.
426 # If it isn't set we set it to the configure-time value
427 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
428 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
429 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
430 if cur_target == '':
431 cur_target = cfg_target
432 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
433 elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
434 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
435 % (cur_target, cfg_target))
436 raise DistutilsPlatformError(my_msg)
437
438 # On AIX, there are wrong paths to the linker scripts in the Makefile
439 # -- these paths are relative to the Python source, but when installed
440 # the scripts are in another directory.
441 if python_build:
442 g['LDSHARED'] = g['BLDSHARED']
443
444 elif get_python_version() < '2.1':
445 # The following two branches are for 1.5.2 compatibility.
446 if sys.platform == 'aix4': # what about AIX 3.x ?
447 # Linker script is in the config directory, not in Modules as the
448 # Makefile says.
449 python_lib = get_python_lib(standard_lib=1)
450 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
451 python_exp = os.path.join(python_lib, 'config', 'python.exp')
452
453 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
454
455 global _config_vars
456 _config_vars = g
457
458
459def _init_nt():
460 """Initialize the module as appropriate for NT"""
461 g = {}
462 # set basic install directories
463 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
464 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
465
466 # XXX hmmm.. a normal install puts include files here
467 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
468
469 g['SO'] = '.pyd'
470 g['EXE'] = ".exe"
471 g['VERSION'] = get_python_version().replace(".", "")
472 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
473
474 global _config_vars
475 _config_vars = g
476
477
478def _init_mac():
479 """Initialize the module as appropriate for Macintosh systems"""
480 g = {}
481 # set basic install directories
482 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
483 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
484
485 # XXX hmmm.. a normal install puts include files here
486 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
487
488 import MacOS
489 if not hasattr(MacOS, 'runtimemodel'):
490 g['SO'] = '.ppc.slb'
491 else:
492 g['SO'] = '.%s.slb' % MacOS.runtimemodel
493
494 # XXX are these used anywhere?
495 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
496 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
497
498 # These are used by the extension module build
499 g['srcdir'] = ':'
500 global _config_vars
501 _config_vars = g
502
503
504def _init_os2():
505 """Initialize the module as appropriate for OS/2"""
506 g = {}
507 # set basic install directories
508 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
509 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
510
511 # XXX hmmm.. a normal install puts include files here
512 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
513
514 g['SO'] = '.pyd'
515 g['EXE'] = ".exe"
516
517 global _config_vars
518 _config_vars = g
519
520
521def get_config_vars(*args):
522 """With no arguments, return a dictionary of all configuration
523 variables relevant for the current platform. Generally this includes
524 everything needed to build extensions and install both pure modules and
525 extensions. On Unix, this means every variable defined in Python's
526 installed Makefile; on Windows and Mac OS it's a much smaller set.
527
528 With arguments, return a list of values that result from looking up
529 each argument in the configuration variable dictionary.
530 """
531 global _config_vars
532 if _config_vars is None:
533 func = globals().get("_init_" + os.name)
534 if func:
535 func()
536 else:
537 _config_vars = {}
538
539 # Normalized versions of prefix and exec_prefix are handy to have;
540 # in fact, these are the standard versions used most places in the
541 # Distutils.
542 _config_vars['prefix'] = PREFIX
543 _config_vars['exec_prefix'] = EXEC_PREFIX
544
545 # Convert srcdir into an absolute path if it appears necessary.
546 # Normally it is relative to the build directory. However, during
547 # testing, for example, we might be running a non-installed python
548 # from a different directory.
549 if python_build and os.name == "posix":
550 base = os.path.dirname(os.path.abspath(sys.executable))
551 if (not os.path.isabs(_config_vars['srcdir']) and
552 base != os.getcwd()):
553 # srcdir is relative and we are not in the same directory
554 # as the executable. Assume executable is in the build
555 # directory and make srcdir absolute.
556 srcdir = os.path.join(base, _config_vars['srcdir'])
557 _config_vars['srcdir'] = os.path.normpath(srcdir)
558
559 if sys.platform == 'darwin':
560 kernel_version = os.uname()[2] # Kernel version (8.4.3)
561 major_version = int(kernel_version.split('.')[0])
562
563 if major_version < 8:
564 # On Mac OS X before 10.4, check if -arch and -isysroot
565 # are in CFLAGS or LDFLAGS and remove them if they are.
566 # This is needed when building extensions on a 10.3 system
567 # using a universal build of python.
568 for key in ('LDFLAGS', 'BASECFLAGS',
569 # a number of derived variables. These need to be
570 # patched up as well.
571 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
572 flags = _config_vars[key]
573 flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
574 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
575 _config_vars[key] = flags
576
577 else:
578
579 # Allow the user to override the architecture flags using
580 # an environment variable.
581 # NOTE: This name was introduced by Apple in OSX 10.5 and
582 # is used by several scripting languages distributed with
583 # that OS release.
584
585 if 'ARCHFLAGS' in os.environ:
586 arch = os.environ['ARCHFLAGS']
587 for key in ('LDFLAGS', 'BASECFLAGS',
588 # a number of derived variables. These need to be
589 # patched up as well.
590 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
591
592 flags = _config_vars[key]
593 flags = re.sub('-arch\s+\w+\s', ' ', flags)
594 flags = flags + ' ' + arch
595 _config_vars[key] = flags
596
597 if args:
598 vals = []
599 for name in args:
600 vals.append(_config_vars.get(name))
601 return vals
602 else:
603 return _config_vars
604
605def get_config_var(name):
606 """Return the value of a single variable using the dictionary
607 returned by 'get_config_vars()'. Equivalent to
608 get_config_vars().get(name)
609 """
610 return get_config_vars().get(name)