blob: 897b7d63d6cee0715c6a946ea22b30b3b25fc814 [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
Greg Ward9ddaaa11999-01-06 14:46:06 +000014import os
15import re
Tarek Ziadé36797272010-07-22 12:50:05 +000016import sys
Greg Ward1190ee31998-12-18 23:46:33 +000017
Tarek Ziadé36797272010-07-22 12:50:05 +000018from .errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000019
Tarek Ziadé36797272010-07-22 12:50:05 +000020# These are needed in a couple of spots, so just compute them once.
21PREFIX = os.path.normpath(sys.prefix)
22EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000023
Tarek Ziadé36797272010-07-22 12:50:05 +000024# Path to the base directory of the project. On Windows the binary may
25# live in project/PCBuild9. If we're dealing with an x64 Windows build,
26# it'll live in project/PCbuild/amd64.
27project_base = os.path.dirname(os.path.abspath(sys.executable))
28if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
29 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
30# PC/VS7.1
31if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
32 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
33 os.path.pardir))
34# PC/AMD64
35if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
36 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
37 os.path.pardir))
Tarek Ziadé8b441d02010-01-29 11:46:31 +000038
Tarek Ziadé36797272010-07-22 12:50:05 +000039# python_build: (Boolean) if true, we're either building Python or
40# building an extension with an un-installed Python, so we use
41# different (hard-wired) directories.
42# Setup.local is available for Makefile builds including VPATH builds,
43# Setup.dist is available on Windows
Christian Heimes2202f872008-02-06 14:31:34 +000044def _python_build():
Tarek Ziadé36797272010-07-22 12:50:05 +000045 for fn in ("Setup.dist", "Setup.local"):
46 if os.path.isfile(os.path.join(project_base, "Modules", fn)):
47 return True
48 return False
Christian Heimes2202f872008-02-06 14:31:34 +000049python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000050
Barry Warsaw14d98ac2010-11-24 19:43:47 +000051# Calculate the build qualifier flags if they are defined. Adding the flags
52# to the include and lib directories only makes sense for an installation, not
53# an in-source build.
54build_flags = ''
55try:
56 if not python_build:
57 build_flags = sys.abiflags
58except AttributeError:
59 # It's not a configure-based build, so the sys module doesn't have
60 # this attribute, which is fine.
61 pass
62
Tarek Ziadé36797272010-07-22 12:50:05 +000063def get_python_version():
64 """Return a string containing the major and minor Python version,
65 leaving off the patchlevel. Sample return values could be '1.5'
66 or '2.2'.
67 """
68 return sys.version[:3]
Tarek Ziadéedacea32010-01-29 11:41:03 +000069
Tarek Ziadé36797272010-07-22 12:50:05 +000070
71def get_python_inc(plat_specific=0, prefix=None):
72 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000073
74 If 'plat_specific' is false (the default), this is the path to the
75 non-platform-specific header files, i.e. Python.h and so on;
76 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000077 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000078
Greg Wardd38e6f72000-04-10 01:17:49 +000079 If 'prefix' is supplied, use it instead of sys.prefix or
80 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000081 """
Tarek Ziadé36797272010-07-22 12:50:05 +000082 if prefix is None:
83 prefix = plat_specific and EXEC_PREFIX or PREFIX
84 if os.name == "posix":
85 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000086 # Assume the executable is in the build directory. The
87 # pyconfig.h file should be in the same directory. Since
88 # the build directory may not be the source directory, we
89 # must use "srcdir" from the makefile to find the "Include"
90 # directory.
91 base = os.path.dirname(os.path.abspath(sys.executable))
92 if plat_specific:
93 return base
94 else:
95 incdir = os.path.join(get_config_var('srcdir'), 'Include')
96 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +000097 python_dir = 'python' + get_python_version() + build_flags
98 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +000099 elif os.name == "nt":
100 return os.path.join(prefix, "include")
Tarek Ziadé36797272010-07-22 12:50:05 +0000101 elif os.name == "os2":
102 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000103 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000104 raise DistutilsPlatformError(
105 "I don't know where Python installs its C header files "
106 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000107
108
Tarek Ziadé36797272010-07-22 12:50:05 +0000109def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
110 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000111 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000112
Fred Drakec1ee39a2000-03-09 15:54:52 +0000113 If 'plat_specific' is true, return the directory containing
114 platform-specific modules, i.e. any module from a non-pure-Python
115 module distribution; otherwise, return the platform-shared library
116 directory. If 'standard_lib' is true, return the directory
117 containing standard Python library modules; otherwise, return the
118 directory for site-specific modules.
119
Greg Wardd38e6f72000-04-10 01:17:49 +0000120 If 'prefix' is supplied, use it instead of sys.prefix or
121 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000122 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000123 if prefix is None:
124 prefix = plat_specific and EXEC_PREFIX or PREFIX
125
126 if os.name == "posix":
127 libpython = os.path.join(prefix,
128 "lib", "python" + get_python_version())
129 if standard_lib:
130 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000131 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000132 return os.path.join(libpython, "site-packages")
133 elif os.name == "nt":
134 if standard_lib:
135 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000136 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000137 if get_python_version() < "2.2":
138 return prefix
139 else:
140 return os.path.join(prefix, "Lib", "site-packages")
Tarek Ziadé36797272010-07-22 12:50:05 +0000141 elif os.name == "os2":
142 if standard_lib:
143 return os.path.join(prefix, "Lib")
144 else:
145 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000146 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000147 raise DistutilsPlatformError(
148 "I don't know where Python installs its library "
149 "on platform '%s'" % os.name)
150
151
152def customize_compiler(compiler):
153 """Do any platform-specific customization of a CCompiler instance.
154
155 Mainly needed on Unix, so we can plug in the information that
156 varies across Unices and is stored in Python's Makefile.
157 """
158 if compiler.compiler_type == "unix":
159 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
160 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
161 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
162
163 if 'CC' in os.environ:
164 cc = os.environ['CC']
165 if 'CXX' in os.environ:
166 cxx = os.environ['CXX']
167 if 'LDSHARED' in os.environ:
168 ldshared = os.environ['LDSHARED']
169 if 'CPP' in os.environ:
170 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000171 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000172 cpp = cc + " -E" # not always
173 if 'LDFLAGS' in os.environ:
174 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
175 if 'CFLAGS' in os.environ:
176 cflags = opt + ' ' + os.environ['CFLAGS']
177 ldshared = ldshared + ' ' + os.environ['CFLAGS']
178 if 'CPPFLAGS' in os.environ:
179 cpp = cpp + ' ' + os.environ['CPPFLAGS']
180 cflags = cflags + ' ' + os.environ['CPPFLAGS']
181 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
182 if 'AR' in os.environ:
183 ar = os.environ['AR']
184 if 'ARFLAGS' in os.environ:
185 archiver = ar + ' ' + os.environ['ARFLAGS']
186 else:
187 archiver = ar + ' ' + ar_flags
188
189 cc_cmd = cc + ' ' + cflags
190 compiler.set_executables(
191 preprocessor=cpp,
192 compiler=cc_cmd,
193 compiler_so=cc_cmd + ' ' + ccshared,
194 compiler_cxx=cxx,
195 linker_so=ldshared,
196 linker_exe=cc,
197 archiver=archiver)
198
199 compiler.shared_lib_extension = so_ext
200
201
202def get_config_h_filename():
203 """Return full pathname of installed pyconfig.h file."""
204 if python_build:
205 if os.name == "nt":
206 inc_dir = os.path.join(project_base, "PC")
207 else:
208 inc_dir = project_base
209 else:
210 inc_dir = get_python_inc(plat_specific=1)
211 if get_python_version() < '2.2':
212 config_h = 'config.h'
213 else:
214 # The name of the config.h file changed in 2.2
215 config_h = 'pyconfig.h'
216 return os.path.join(inc_dir, config_h)
217
Greg Ward1190ee31998-12-18 23:46:33 +0000218
Greg Ward9ddaaa11999-01-06 14:46:06 +0000219def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000220 """Return full pathname of installed Makefile from the Python build."""
221 if python_build:
222 return os.path.join(os.path.dirname(sys.executable), "Makefile")
223 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000224 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
225 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000226
Tarek Ziadé36797272010-07-22 12:50:05 +0000227
228def parse_config_h(fp, g=None):
229 """Parse a config.h-style file.
230
231 A dictionary containing name/value pairs is returned. If an
232 optional dictionary is passed in as the second argument, it is
233 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000234 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000235 if g is None:
236 g = {}
237 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
238 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
239 #
240 while True:
241 line = fp.readline()
242 if not line:
243 break
244 m = define_rx.match(line)
245 if m:
246 n, v = m.group(1, 2)
247 try: v = int(v)
248 except ValueError: pass
249 g[n] = v
250 else:
251 m = undef_rx.match(line)
252 if m:
253 g[m.group(1)] = 0
254 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000255
Greg Wardd283ce72000-09-17 00:53:02 +0000256
257# Regexes needed for parsing Makefile (and similar syntaxes,
258# like old-style Setup files).
259_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
260_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
261_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
262
Greg Ward3fff8d22000-09-15 00:03:13 +0000263def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000264 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000265
266 A dictionary containing name/value pairs is returned. If an
267 optional dictionary is passed in as the second argument, it is
268 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000269 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000270 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000271 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000272
273 if g is None:
274 g = {}
275 done = {}
276 notdone = {}
277
278 while True:
279 line = fp.readline()
280 if line is None: # eof
281 break
282 m = _variable_rx.match(line)
283 if m:
284 n, v = m.group(1, 2)
285 v = v.strip()
286 # `$$' is a literal `$' in make
287 tmpv = v.replace('$$', '')
288
289 if "$" in tmpv:
290 notdone[n] = v
291 else:
292 try:
293 v = int(v)
294 except ValueError:
295 # insert literal `$'
296 done[n] = v.replace('$$', '$')
297 else:
298 done[n] = v
299
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000300 # Variables with a 'PY_' prefix in the makefile. These need to
301 # be made available without that prefix through sysconfig.
302 # Special care is needed to ensure that variable expansion works, even
303 # if the expansion uses the name without a prefix.
304 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
305
Tarek Ziadé36797272010-07-22 12:50:05 +0000306 # do variable interpolation here
307 while notdone:
308 for name in list(notdone):
309 value = notdone[name]
310 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
311 if m:
312 n = m.group(1)
313 found = True
314 if n in done:
315 item = str(done[n])
316 elif n in notdone:
317 # get it on a subsequent round
318 found = False
319 elif n in os.environ:
320 # do it like make: fall back to environment
321 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000322
323 elif n in renamed_variables:
324 if name.startswith('PY_') and name[3:] in renamed_variables:
325 item = ""
326
327 elif 'PY_' + n in notdone:
328 found = False
329
330 else:
331 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000332 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]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000346
347 if name.startswith('PY_') \
348 and name[3:] in renamed_variables:
349
350 name = name[3:]
351 if name not in done:
352 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000353 else:
354 # bogus variable reference; just drop it since we can't deal
355 del notdone[name]
356
357 fp.close()
358
Antoine Pitroudbec7802010-10-10 09:37:12 +0000359 # strip spurious spaces
360 for k, v in done.items():
361 if isinstance(v, str):
362 done[k] = v.strip()
363
Tarek Ziadé36797272010-07-22 12:50:05 +0000364 # save the results in the global dictionary
365 g.update(done)
366 return g
367
Greg Ward1190ee31998-12-18 23:46:33 +0000368
Greg Wardd283ce72000-09-17 00:53:02 +0000369def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000370 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000371 'string' according to 'vars' (a dictionary mapping variable names to
372 values). Variables not present in 'vars' are silently expanded to the
373 empty string. The variable values in 'vars' should not contain further
374 variable expansions; if 'vars' is the output of 'parse_makefile()',
375 you're fine. Returns a variable-expanded version of 's'.
376 """
377
378 # This algorithm does multiple expansion, so if vars['foo'] contains
379 # "${bar}", it will expand ${foo} to ${bar}, and then expand
380 # ${bar}... and so forth. This is fine as long as 'vars' comes from
381 # 'parse_makefile()', which takes care of such expansions eagerly,
382 # according to make's variable expansion semantics.
383
Collin Winter5b7e9d72007-08-30 03:52:21 +0000384 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000385 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
386 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000387 (beg, end) = m.span()
388 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
389 else:
390 break
391 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000392
393
394_config_vars = None
395
396def _init_posix():
397 """Initialize the module as appropriate for POSIX systems."""
398 g = {}
399 # load the installed Makefile:
400 try:
401 filename = get_makefile_filename()
402 parse_makefile(filename, g)
403 except IOError as msg:
404 my_msg = "invalid Python installation: unable to open %s" % filename
405 if hasattr(msg, "strerror"):
406 my_msg = my_msg + " (%s)" % msg.strerror
407
408 raise DistutilsPlatformError(my_msg)
409
410 # load the installed pyconfig.h:
411 try:
412 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000413 with open(filename) as file:
414 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000415 except IOError as msg:
416 my_msg = "invalid Python installation: unable to open %s" % filename
417 if hasattr(msg, "strerror"):
418 my_msg = my_msg + " (%s)" % msg.strerror
419
420 raise DistutilsPlatformError(my_msg)
421
422 # On MacOSX we need to check the setting of the environment variable
423 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
424 # it needs to be compatible.
425 # If it isn't set we set it to the configure-time value
426 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
427 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
428 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
429 if cur_target == '':
430 cur_target = cfg_target
431 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
432 elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
433 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
434 % (cur_target, cfg_target))
435 raise DistutilsPlatformError(my_msg)
436
437 # On AIX, there are wrong paths to the linker scripts in the Makefile
438 # -- these paths are relative to the Python source, but when installed
439 # the scripts are in another directory.
440 if python_build:
441 g['LDSHARED'] = g['BLDSHARED']
442
443 elif get_python_version() < '2.1':
444 # The following two branches are for 1.5.2 compatibility.
445 if sys.platform == 'aix4': # what about AIX 3.x ?
446 # Linker script is in the config directory, not in Modules as the
447 # Makefile says.
448 python_lib = get_python_lib(standard_lib=1)
449 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
450 python_exp = os.path.join(python_lib, 'config', 'python.exp')
451
452 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
453
454 global _config_vars
455 _config_vars = g
456
457
458def _init_nt():
459 """Initialize the module as appropriate for NT"""
460 g = {}
461 # set basic install directories
462 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
463 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
464
465 # XXX hmmm.. a normal install puts include files here
466 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
467
468 g['SO'] = '.pyd'
469 g['EXE'] = ".exe"
470 g['VERSION'] = get_python_version().replace(".", "")
471 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
472
473 global _config_vars
474 _config_vars = g
475
476
Tarek Ziadé36797272010-07-22 12:50:05 +0000477def _init_os2():
478 """Initialize the module as appropriate for OS/2"""
479 g = {}
480 # set basic install directories
481 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
482 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
483
484 # XXX hmmm.. a normal install puts include files here
485 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
486
487 g['SO'] = '.pyd'
488 g['EXE'] = ".exe"
489
490 global _config_vars
491 _config_vars = g
492
493
494def get_config_vars(*args):
495 """With no arguments, return a dictionary of all configuration
496 variables relevant for the current platform. Generally this includes
497 everything needed to build extensions and install both pure modules and
498 extensions. On Unix, this means every variable defined in Python's
499 installed Makefile; on Windows and Mac OS it's a much smaller set.
500
501 With arguments, return a list of values that result from looking up
502 each argument in the configuration variable dictionary.
503 """
504 global _config_vars
505 if _config_vars is None:
506 func = globals().get("_init_" + os.name)
507 if func:
508 func()
509 else:
510 _config_vars = {}
511
512 # Normalized versions of prefix and exec_prefix are handy to have;
513 # in fact, these are the standard versions used most places in the
514 # Distutils.
515 _config_vars['prefix'] = PREFIX
516 _config_vars['exec_prefix'] = EXEC_PREFIX
517
518 # Convert srcdir into an absolute path if it appears necessary.
519 # Normally it is relative to the build directory. However, during
520 # testing, for example, we might be running a non-installed python
521 # from a different directory.
522 if python_build and os.name == "posix":
523 base = os.path.dirname(os.path.abspath(sys.executable))
524 if (not os.path.isabs(_config_vars['srcdir']) and
525 base != os.getcwd()):
526 # srcdir is relative and we are not in the same directory
527 # as the executable. Assume executable is in the build
528 # directory and make srcdir absolute.
529 srcdir = os.path.join(base, _config_vars['srcdir'])
530 _config_vars['srcdir'] = os.path.normpath(srcdir)
531
532 if sys.platform == 'darwin':
533 kernel_version = os.uname()[2] # Kernel version (8.4.3)
534 major_version = int(kernel_version.split('.')[0])
535
536 if major_version < 8:
537 # On Mac OS X before 10.4, check if -arch and -isysroot
538 # are in CFLAGS or LDFLAGS and remove them if they are.
539 # This is needed when building extensions on a 10.3 system
540 # using a universal build of python.
541 for key in ('LDFLAGS', 'BASECFLAGS',
542 # a number of derived variables. These need to be
543 # patched up as well.
544 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
545 flags = _config_vars[key]
546 flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
547 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
548 _config_vars[key] = flags
549
550 else:
551
552 # Allow the user to override the architecture flags using
553 # an environment variable.
554 # NOTE: This name was introduced by Apple in OSX 10.5 and
555 # is used by several scripting languages distributed with
556 # that OS release.
557
558 if 'ARCHFLAGS' in os.environ:
559 arch = os.environ['ARCHFLAGS']
560 for key in ('LDFLAGS', 'BASECFLAGS',
561 # a number of derived variables. These need to be
562 # patched up as well.
563 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
564
565 flags = _config_vars[key]
566 flags = re.sub('-arch\s+\w+\s', ' ', flags)
567 flags = flags + ' ' + arch
568 _config_vars[key] = flags
569
570 if args:
571 vals = []
572 for name in args:
573 vals.append(_config_vars.get(name))
574 return vals
575 else:
576 return _config_vars
577
578def get_config_var(name):
579 """Return the value of a single variable using the dictionary
580 returned by 'get_config_vars()'. Equivalent to
581 get_config_vars().get(name)
582 """
583 return get_config_vars().get(name)