blob: e86cb23236ff13d2681eb6011a934bf7e6ff01f0 [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 Ward9ddaaa11999-01-06 14:46:06 +000012import os
13import re
Tarek Ziadé36797272010-07-22 12:50:05 +000014import sys
Greg Ward1190ee31998-12-18 23:46:33 +000015
Tarek Ziadé36797272010-07-22 12:50:05 +000016from .errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000017
Tarek Ziadé36797272010-07-22 12:50:05 +000018# These are needed in a couple of spots, so just compute them once.
19PREFIX = os.path.normpath(sys.prefix)
20EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010021BASE_PREFIX = os.path.normpath(sys.base_prefix)
22BASE_EXEC_PREFIX = os.path.normpath(sys.base_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
Vinay Sajip7ded1f02012-05-26 03:45:29 +010044def _is_python_source_dir(d):
Tarek Ziadé36797272010-07-22 12:50:05 +000045 for fn in ("Setup.dist", "Setup.local"):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010046 if os.path.isfile(os.path.join(d, "Modules", fn)):
Tarek Ziadé36797272010-07-22 12:50:05 +000047 return True
48 return False
Vinay Sajip7ded1f02012-05-26 03:45:29 +010049_sys_home = getattr(sys, '_home', None)
Vinay Sajip42211422012-05-26 20:36:12 +010050if _sys_home and os.name == 'nt' and \
51 _sys_home.lower().endswith(('pcbuild', 'pcbuild\\amd64')):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010052 _sys_home = os.path.dirname(_sys_home)
Vinay Sajip7e203492012-05-27 17:30:09 +010053 if _sys_home.endswith('pcbuild'): # must be amd64
54 _sys_home = os.path.dirname(_sys_home)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010055def _python_build():
56 if _sys_home:
57 return _is_python_source_dir(_sys_home)
58 return _is_python_source_dir(project_base)
Christian Heimes2202f872008-02-06 14:31:34 +000059python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000060
Barry Warsaw14d98ac2010-11-24 19:43:47 +000061# Calculate the build qualifier flags if they are defined. Adding the flags
62# to the include and lib directories only makes sense for an installation, not
63# an in-source build.
64build_flags = ''
65try:
66 if not python_build:
67 build_flags = sys.abiflags
68except AttributeError:
69 # It's not a configure-based build, so the sys module doesn't have
70 # this attribute, which is fine.
71 pass
72
Tarek Ziadé36797272010-07-22 12:50:05 +000073def get_python_version():
74 """Return a string containing the major and minor Python version,
75 leaving off the patchlevel. Sample return values could be '1.5'
76 or '2.2'.
77 """
78 return sys.version[:3]
Tarek Ziadéedacea32010-01-29 11:41:03 +000079
Tarek Ziadé36797272010-07-22 12:50:05 +000080
81def get_python_inc(plat_specific=0, prefix=None):
82 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000083
84 If 'plat_specific' is false (the default), this is the path to the
85 non-platform-specific header files, i.e. Python.h and so on;
86 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000087 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000088
Vinay Sajip7ded1f02012-05-26 03:45:29 +010089 If 'prefix' is supplied, use it instead of sys.base_prefix or
90 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000091 """
Tarek Ziadé36797272010-07-22 12:50:05 +000092 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010093 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +000094 if os.name == "posix":
95 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000096 # Assume the executable is in the build directory. The
97 # pyconfig.h file should be in the same directory. Since
98 # the build directory may not be the source directory, we
99 # must use "srcdir" from the makefile to find the "Include"
100 # directory.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100101 base = _sys_home or os.path.dirname(os.path.abspath(sys.executable))
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000102 if plat_specific:
103 return base
104 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100105 incdir = os.path.join(_sys_home or get_config_var('srcdir'),
106 'Include')
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000107 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000108 python_dir = 'python' + get_python_version() + build_flags
109 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000110 elif os.name == "nt":
111 return os.path.join(prefix, "include")
Tarek Ziadé36797272010-07-22 12:50:05 +0000112 elif os.name == "os2":
113 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000114 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000115 raise DistutilsPlatformError(
116 "I don't know where Python installs its C header files "
117 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000118
119
Tarek Ziadé36797272010-07-22 12:50:05 +0000120def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
121 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000122 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000123
Fred Drakec1ee39a2000-03-09 15:54:52 +0000124 If 'plat_specific' is true, return the directory containing
125 platform-specific modules, i.e. any module from a non-pure-Python
126 module distribution; otherwise, return the platform-shared library
127 directory. If 'standard_lib' is true, return the directory
128 containing standard Python library modules; otherwise, return the
129 directory for site-specific modules.
130
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100131 If 'prefix' is supplied, use it instead of sys.base_prefix or
132 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000133 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000134 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100135 if standard_lib:
136 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
137 else:
138 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000139
140 if os.name == "posix":
141 libpython = os.path.join(prefix,
142 "lib", "python" + get_python_version())
143 if standard_lib:
144 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000145 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000146 return os.path.join(libpython, "site-packages")
147 elif os.name == "nt":
148 if standard_lib:
149 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000150 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000151 if get_python_version() < "2.2":
152 return prefix
153 else:
154 return os.path.join(prefix, "Lib", "site-packages")
Tarek Ziadé36797272010-07-22 12:50:05 +0000155 elif os.name == "os2":
156 if standard_lib:
157 return os.path.join(prefix, "Lib")
158 else:
159 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000160 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000161 raise DistutilsPlatformError(
162 "I don't know where Python installs its library "
163 "on platform '%s'" % os.name)
164
Ned Deily99377482012-02-10 13:01:08 +0100165_USE_CLANG = None
Tarek Ziadé36797272010-07-22 12:50:05 +0000166
167def customize_compiler(compiler):
168 """Do any platform-specific customization of a CCompiler instance.
169
170 Mainly needed on Unix, so we can plug in the information that
171 varies across Unices and is stored in Python's Makefile.
172 """
173 if compiler.compiler_type == "unix":
174 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
175 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
176 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
177
Ned Deily99377482012-02-10 13:01:08 +0100178 newcc = None
Tarek Ziadé36797272010-07-22 12:50:05 +0000179 if 'CC' in os.environ:
Ned Deily99377482012-02-10 13:01:08 +0100180 newcc = os.environ['CC']
181 elif sys.platform == 'darwin' and cc == 'gcc-4.2':
182 # Issue #13590:
183 # Since Apple removed gcc-4.2 in Xcode 4.2, we can no
184 # longer assume it is available for extension module builds.
185 # If Python was built with gcc-4.2, check first to see if
186 # it is available on this system; if not, try to use clang
187 # instead unless the caller explicitly set CC.
188 global _USE_CLANG
189 if _USE_CLANG is None:
190 from distutils import log
191 from subprocess import Popen, PIPE
192 p = Popen("! type gcc-4.2 && type clang && exit 2",
193 shell=True, stdout=PIPE, stderr=PIPE)
194 p.wait()
195 if p.returncode == 2:
196 _USE_CLANG = True
197 log.warn("gcc-4.2 not found, using clang instead")
198 else:
199 _USE_CLANG = False
200 if _USE_CLANG:
201 newcc = 'clang'
202 if newcc:
203 # On OS X, if CC is overridden, use that as the default
204 # command for LDSHARED as well
205 if (sys.platform == 'darwin'
206 and 'LDSHARED' not in os.environ
207 and ldshared.startswith(cc)):
208 ldshared = newcc + ldshared[len(cc):]
209 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000210 if 'CXX' in os.environ:
211 cxx = os.environ['CXX']
212 if 'LDSHARED' in os.environ:
213 ldshared = os.environ['LDSHARED']
214 if 'CPP' in os.environ:
215 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000216 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000217 cpp = cc + " -E" # not always
218 if 'LDFLAGS' in os.environ:
219 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
220 if 'CFLAGS' in os.environ:
221 cflags = opt + ' ' + os.environ['CFLAGS']
222 ldshared = ldshared + ' ' + os.environ['CFLAGS']
223 if 'CPPFLAGS' in os.environ:
224 cpp = cpp + ' ' + os.environ['CPPFLAGS']
225 cflags = cflags + ' ' + os.environ['CPPFLAGS']
226 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
227 if 'AR' in os.environ:
228 ar = os.environ['AR']
229 if 'ARFLAGS' in os.environ:
230 archiver = ar + ' ' + os.environ['ARFLAGS']
231 else:
232 archiver = ar + ' ' + ar_flags
233
234 cc_cmd = cc + ' ' + cflags
235 compiler.set_executables(
236 preprocessor=cpp,
237 compiler=cc_cmd,
238 compiler_so=cc_cmd + ' ' + ccshared,
239 compiler_cxx=cxx,
240 linker_so=ldshared,
241 linker_exe=cc,
242 archiver=archiver)
243
244 compiler.shared_lib_extension = so_ext
245
246
247def get_config_h_filename():
248 """Return full pathname of installed pyconfig.h file."""
249 if python_build:
250 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100251 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000252 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100253 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000254 else:
255 inc_dir = get_python_inc(plat_specific=1)
256 if get_python_version() < '2.2':
257 config_h = 'config.h'
258 else:
259 # The name of the config.h file changed in 2.2
260 config_h = 'pyconfig.h'
261 return os.path.join(inc_dir, config_h)
262
Greg Ward1190ee31998-12-18 23:46:33 +0000263
Greg Ward9ddaaa11999-01-06 14:46:06 +0000264def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000265 """Return full pathname of installed Makefile from the Python build."""
266 if python_build:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100267 return os.path.join(_sys_home or os.path.dirname(sys.executable),
268 "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200269 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000270 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
271 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000272
Tarek Ziadé36797272010-07-22 12:50:05 +0000273
274def parse_config_h(fp, g=None):
275 """Parse a config.h-style file.
276
277 A dictionary containing name/value pairs is returned. If an
278 optional dictionary is passed in as the second argument, it is
279 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000280 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000281 if g is None:
282 g = {}
283 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
284 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
285 #
286 while True:
287 line = fp.readline()
288 if not line:
289 break
290 m = define_rx.match(line)
291 if m:
292 n, v = m.group(1, 2)
293 try: v = int(v)
294 except ValueError: pass
295 g[n] = v
296 else:
297 m = undef_rx.match(line)
298 if m:
299 g[m.group(1)] = 0
300 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000301
Greg Wardd283ce72000-09-17 00:53:02 +0000302
303# Regexes needed for parsing Makefile (and similar syntaxes,
304# like old-style Setup files).
305_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
306_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
307_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
308
Greg Ward3fff8d22000-09-15 00:03:13 +0000309def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000310 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000311
312 A dictionary containing name/value pairs is returned. If an
313 optional dictionary is passed in as the second argument, it is
314 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000315 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000316 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000317 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000318
319 if g is None:
320 g = {}
321 done = {}
322 notdone = {}
323
324 while True:
325 line = fp.readline()
326 if line is None: # eof
327 break
328 m = _variable_rx.match(line)
329 if m:
330 n, v = m.group(1, 2)
331 v = v.strip()
332 # `$$' is a literal `$' in make
333 tmpv = v.replace('$$', '')
334
335 if "$" in tmpv:
336 notdone[n] = v
337 else:
338 try:
339 v = int(v)
340 except ValueError:
341 # insert literal `$'
342 done[n] = v.replace('$$', '$')
343 else:
344 done[n] = v
345
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000346 # Variables with a 'PY_' prefix in the makefile. These need to
347 # be made available without that prefix through sysconfig.
348 # Special care is needed to ensure that variable expansion works, even
349 # if the expansion uses the name without a prefix.
350 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
351
Tarek Ziadé36797272010-07-22 12:50:05 +0000352 # do variable interpolation here
353 while notdone:
354 for name in list(notdone):
355 value = notdone[name]
356 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
357 if m:
358 n = m.group(1)
359 found = True
360 if n in done:
361 item = str(done[n])
362 elif n in notdone:
363 # get it on a subsequent round
364 found = False
365 elif n in os.environ:
366 # do it like make: fall back to environment
367 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000368
369 elif n in renamed_variables:
370 if name.startswith('PY_') and name[3:] in renamed_variables:
371 item = ""
372
373 elif 'PY_' + n in notdone:
374 found = False
375
376 else:
377 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000378 else:
379 done[n] = item = ""
380 if found:
381 after = value[m.end():]
382 value = value[:m.start()] + item + after
383 if "$" in after:
384 notdone[name] = value
385 else:
386 try: value = int(value)
387 except ValueError:
388 done[name] = value.strip()
389 else:
390 done[name] = value
391 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000392
393 if name.startswith('PY_') \
394 and name[3:] in renamed_variables:
395
396 name = name[3:]
397 if name not in done:
398 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000399 else:
400 # bogus variable reference; just drop it since we can't deal
401 del notdone[name]
402
403 fp.close()
404
Antoine Pitroudbec7802010-10-10 09:37:12 +0000405 # strip spurious spaces
406 for k, v in done.items():
407 if isinstance(v, str):
408 done[k] = v.strip()
409
Tarek Ziadé36797272010-07-22 12:50:05 +0000410 # save the results in the global dictionary
411 g.update(done)
412 return g
413
Greg Ward1190ee31998-12-18 23:46:33 +0000414
Greg Wardd283ce72000-09-17 00:53:02 +0000415def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000416 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000417 'string' according to 'vars' (a dictionary mapping variable names to
418 values). Variables not present in 'vars' are silently expanded to the
419 empty string. The variable values in 'vars' should not contain further
420 variable expansions; if 'vars' is the output of 'parse_makefile()',
421 you're fine. Returns a variable-expanded version of 's'.
422 """
423
424 # This algorithm does multiple expansion, so if vars['foo'] contains
425 # "${bar}", it will expand ${foo} to ${bar}, and then expand
426 # ${bar}... and so forth. This is fine as long as 'vars' comes from
427 # 'parse_makefile()', which takes care of such expansions eagerly,
428 # according to make's variable expansion semantics.
429
Collin Winter5b7e9d72007-08-30 03:52:21 +0000430 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000431 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
432 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000433 (beg, end) = m.span()
434 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
435 else:
436 break
437 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000438
439
440_config_vars = None
441
442def _init_posix():
443 """Initialize the module as appropriate for POSIX systems."""
444 g = {}
445 # load the installed Makefile:
446 try:
447 filename = get_makefile_filename()
448 parse_makefile(filename, g)
449 except IOError as msg:
450 my_msg = "invalid Python installation: unable to open %s" % filename
451 if hasattr(msg, "strerror"):
452 my_msg = my_msg + " (%s)" % msg.strerror
453
454 raise DistutilsPlatformError(my_msg)
455
456 # load the installed pyconfig.h:
457 try:
458 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000459 with open(filename) as file:
460 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000461 except IOError as msg:
462 my_msg = "invalid Python installation: unable to open %s" % filename
463 if hasattr(msg, "strerror"):
464 my_msg = my_msg + " (%s)" % msg.strerror
465
466 raise DistutilsPlatformError(my_msg)
467
Tarek Ziadé36797272010-07-22 12:50:05 +0000468 # On AIX, there are wrong paths to the linker scripts in the Makefile
469 # -- these paths are relative to the Python source, but when installed
470 # the scripts are in another directory.
471 if python_build:
472 g['LDSHARED'] = g['BLDSHARED']
473
474 elif get_python_version() < '2.1':
475 # The following two branches are for 1.5.2 compatibility.
476 if sys.platform == 'aix4': # what about AIX 3.x ?
477 # Linker script is in the config directory, not in Modules as the
478 # Makefile says.
479 python_lib = get_python_lib(standard_lib=1)
480 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
481 python_exp = os.path.join(python_lib, 'config', 'python.exp')
482
483 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
484
485 global _config_vars
486 _config_vars = g
487
488
489def _init_nt():
490 """Initialize the module as appropriate for NT"""
491 g = {}
492 # set basic install directories
493 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
494 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
495
496 # XXX hmmm.. a normal install puts include files here
497 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
498
499 g['SO'] = '.pyd'
500 g['EXE'] = ".exe"
501 g['VERSION'] = get_python_version().replace(".", "")
502 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
503
504 global _config_vars
505 _config_vars = g
506
507
Tarek Ziadé36797272010-07-22 12:50:05 +0000508def _init_os2():
509 """Initialize the module as appropriate for OS/2"""
510 g = {}
511 # set basic install directories
512 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
513 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
514
515 # XXX hmmm.. a normal install puts include files here
516 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
517
518 g['SO'] = '.pyd'
519 g['EXE'] = ".exe"
520
521 global _config_vars
522 _config_vars = g
523
524
525def get_config_vars(*args):
526 """With no arguments, return a dictionary of all configuration
527 variables relevant for the current platform. Generally this includes
528 everything needed to build extensions and install both pure modules and
529 extensions. On Unix, this means every variable defined in Python's
530 installed Makefile; on Windows and Mac OS it's a much smaller set.
531
532 With arguments, return a list of values that result from looking up
533 each argument in the configuration variable dictionary.
534 """
535 global _config_vars
536 if _config_vars is None:
537 func = globals().get("_init_" + os.name)
538 if func:
539 func()
540 else:
541 _config_vars = {}
542
543 # Normalized versions of prefix and exec_prefix are handy to have;
544 # in fact, these are the standard versions used most places in the
545 # Distutils.
546 _config_vars['prefix'] = PREFIX
547 _config_vars['exec_prefix'] = EXEC_PREFIX
548
549 # Convert srcdir into an absolute path if it appears necessary.
550 # Normally it is relative to the build directory. However, during
551 # testing, for example, we might be running a non-installed python
552 # from a different directory.
553 if python_build and os.name == "posix":
554 base = os.path.dirname(os.path.abspath(sys.executable))
555 if (not os.path.isabs(_config_vars['srcdir']) and
556 base != os.getcwd()):
557 # srcdir is relative and we are not in the same directory
558 # as the executable. Assume executable is in the build
559 # directory and make srcdir absolute.
560 srcdir = os.path.join(base, _config_vars['srcdir'])
561 _config_vars['srcdir'] = os.path.normpath(srcdir)
562
563 if sys.platform == 'darwin':
564 kernel_version = os.uname()[2] # Kernel version (8.4.3)
565 major_version = int(kernel_version.split('.')[0])
566
567 if major_version < 8:
568 # On Mac OS X before 10.4, check if -arch and -isysroot
569 # are in CFLAGS or LDFLAGS and remove them if they are.
570 # This is needed when building extensions on a 10.3 system
571 # using a universal build of python.
572 for key in ('LDFLAGS', 'BASECFLAGS',
573 # a number of derived variables. These need to be
574 # patched up as well.
575 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
576 flags = _config_vars[key]
577 flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
578 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
579 _config_vars[key] = flags
580
581 else:
582
583 # Allow the user to override the architecture flags using
584 # an environment variable.
585 # NOTE: This name was introduced by Apple in OSX 10.5 and
586 # is used by several scripting languages distributed with
587 # that OS release.
588
589 if 'ARCHFLAGS' in os.environ:
590 arch = os.environ['ARCHFLAGS']
591 for key in ('LDFLAGS', 'BASECFLAGS',
592 # a number of derived variables. These need to be
593 # patched up as well.
594 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
595
596 flags = _config_vars[key]
597 flags = re.sub('-arch\s+\w+\s', ' ', flags)
598 flags = flags + ' ' + arch
599 _config_vars[key] = flags
600
601 if args:
602 vals = []
603 for name in args:
604 vals.append(_config_vars.get(name))
605 return vals
606 else:
607 return _config_vars
608
609def get_config_var(name):
610 """Return the value of a single variable using the dictionary
611 returned by 'get_config_vars()'. Equivalent to
612 get_config_vars().get(name)
613 """
614 return get_config_vars().get(name)