blob: 59315f72799e5411eeaff193e732a60aaf60ff5f [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)
53def _python_build():
54 if _sys_home:
55 return _is_python_source_dir(_sys_home)
56 return _is_python_source_dir(project_base)
Christian Heimes2202f872008-02-06 14:31:34 +000057python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000058
Barry Warsaw14d98ac2010-11-24 19:43:47 +000059# Calculate the build qualifier flags if they are defined. Adding the flags
60# to the include and lib directories only makes sense for an installation, not
61# an in-source build.
62build_flags = ''
63try:
64 if not python_build:
65 build_flags = sys.abiflags
66except AttributeError:
67 # It's not a configure-based build, so the sys module doesn't have
68 # this attribute, which is fine.
69 pass
70
Tarek Ziadé36797272010-07-22 12:50:05 +000071def get_python_version():
72 """Return a string containing the major and minor Python version,
73 leaving off the patchlevel. Sample return values could be '1.5'
74 or '2.2'.
75 """
76 return sys.version[:3]
Tarek Ziadéedacea32010-01-29 11:41:03 +000077
Tarek Ziadé36797272010-07-22 12:50:05 +000078
79def get_python_inc(plat_specific=0, prefix=None):
80 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000081
82 If 'plat_specific' is false (the default), this is the path to the
83 non-platform-specific header files, i.e. Python.h and so on;
84 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000085 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000086
Vinay Sajip7ded1f02012-05-26 03:45:29 +010087 If 'prefix' is supplied, use it instead of sys.base_prefix or
88 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000089 """
Tarek Ziadé36797272010-07-22 12:50:05 +000090 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010091 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +000092 if os.name == "posix":
93 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000094 # Assume the executable is in the build directory. The
95 # pyconfig.h file should be in the same directory. Since
96 # the build directory may not be the source directory, we
97 # must use "srcdir" from the makefile to find the "Include"
98 # directory.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010099 base = _sys_home or os.path.dirname(os.path.abspath(sys.executable))
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000100 if plat_specific:
101 return base
102 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100103 incdir = os.path.join(_sys_home or get_config_var('srcdir'),
104 'Include')
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000105 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000106 python_dir = 'python' + get_python_version() + build_flags
107 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000108 elif os.name == "nt":
109 return os.path.join(prefix, "include")
Tarek Ziadé36797272010-07-22 12:50:05 +0000110 elif os.name == "os2":
111 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000112 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000113 raise DistutilsPlatformError(
114 "I don't know where Python installs its C header files "
115 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000116
117
Tarek Ziadé36797272010-07-22 12:50:05 +0000118def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
119 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000120 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121
Fred Drakec1ee39a2000-03-09 15:54:52 +0000122 If 'plat_specific' is true, return the directory containing
123 platform-specific modules, i.e. any module from a non-pure-Python
124 module distribution; otherwise, return the platform-shared library
125 directory. If 'standard_lib' is true, return the directory
126 containing standard Python library modules; otherwise, return the
127 directory for site-specific modules.
128
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100129 If 'prefix' is supplied, use it instead of sys.base_prefix or
130 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000131 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000132 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100133 if standard_lib:
134 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
135 else:
136 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000137
138 if os.name == "posix":
139 libpython = os.path.join(prefix,
140 "lib", "python" + get_python_version())
141 if standard_lib:
142 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000143 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000144 return os.path.join(libpython, "site-packages")
145 elif os.name == "nt":
146 if standard_lib:
147 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000148 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000149 if get_python_version() < "2.2":
150 return prefix
151 else:
152 return os.path.join(prefix, "Lib", "site-packages")
Tarek Ziadé36797272010-07-22 12:50:05 +0000153 elif os.name == "os2":
154 if standard_lib:
155 return os.path.join(prefix, "Lib")
156 else:
157 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000158 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000159 raise DistutilsPlatformError(
160 "I don't know where Python installs its library "
161 "on platform '%s'" % os.name)
162
Ned Deily99377482012-02-10 13:01:08 +0100163_USE_CLANG = None
Tarek Ziadé36797272010-07-22 12:50:05 +0000164
165def customize_compiler(compiler):
166 """Do any platform-specific customization of a CCompiler instance.
167
168 Mainly needed on Unix, so we can plug in the information that
169 varies across Unices and is stored in Python's Makefile.
170 """
171 if compiler.compiler_type == "unix":
172 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
173 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
174 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
175
Ned Deily99377482012-02-10 13:01:08 +0100176 newcc = None
Tarek Ziadé36797272010-07-22 12:50:05 +0000177 if 'CC' in os.environ:
Ned Deily99377482012-02-10 13:01:08 +0100178 newcc = os.environ['CC']
179 elif sys.platform == 'darwin' and cc == 'gcc-4.2':
180 # Issue #13590:
181 # Since Apple removed gcc-4.2 in Xcode 4.2, we can no
182 # longer assume it is available for extension module builds.
183 # If Python was built with gcc-4.2, check first to see if
184 # it is available on this system; if not, try to use clang
185 # instead unless the caller explicitly set CC.
186 global _USE_CLANG
187 if _USE_CLANG is None:
188 from distutils import log
189 from subprocess import Popen, PIPE
190 p = Popen("! type gcc-4.2 && type clang && exit 2",
191 shell=True, stdout=PIPE, stderr=PIPE)
192 p.wait()
193 if p.returncode == 2:
194 _USE_CLANG = True
195 log.warn("gcc-4.2 not found, using clang instead")
196 else:
197 _USE_CLANG = False
198 if _USE_CLANG:
199 newcc = 'clang'
200 if newcc:
201 # On OS X, if CC is overridden, use that as the default
202 # command for LDSHARED as well
203 if (sys.platform == 'darwin'
204 and 'LDSHARED' not in os.environ
205 and ldshared.startswith(cc)):
206 ldshared = newcc + ldshared[len(cc):]
207 cc = newcc
Tarek Ziadé36797272010-07-22 12:50:05 +0000208 if 'CXX' in os.environ:
209 cxx = os.environ['CXX']
210 if 'LDSHARED' in os.environ:
211 ldshared = os.environ['LDSHARED']
212 if 'CPP' in os.environ:
213 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000214 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000215 cpp = cc + " -E" # not always
216 if 'LDFLAGS' in os.environ:
217 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
218 if 'CFLAGS' in os.environ:
219 cflags = opt + ' ' + os.environ['CFLAGS']
220 ldshared = ldshared + ' ' + os.environ['CFLAGS']
221 if 'CPPFLAGS' in os.environ:
222 cpp = cpp + ' ' + os.environ['CPPFLAGS']
223 cflags = cflags + ' ' + os.environ['CPPFLAGS']
224 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
225 if 'AR' in os.environ:
226 ar = os.environ['AR']
227 if 'ARFLAGS' in os.environ:
228 archiver = ar + ' ' + os.environ['ARFLAGS']
229 else:
230 archiver = ar + ' ' + ar_flags
231
232 cc_cmd = cc + ' ' + cflags
233 compiler.set_executables(
234 preprocessor=cpp,
235 compiler=cc_cmd,
236 compiler_so=cc_cmd + ' ' + ccshared,
237 compiler_cxx=cxx,
238 linker_so=ldshared,
239 linker_exe=cc,
240 archiver=archiver)
241
242 compiler.shared_lib_extension = so_ext
243
244
245def get_config_h_filename():
246 """Return full pathname of installed pyconfig.h file."""
247 if python_build:
248 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100249 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000250 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100251 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000252 else:
253 inc_dir = get_python_inc(plat_specific=1)
254 if get_python_version() < '2.2':
255 config_h = 'config.h'
256 else:
257 # The name of the config.h file changed in 2.2
258 config_h = 'pyconfig.h'
259 return os.path.join(inc_dir, config_h)
260
Greg Ward1190ee31998-12-18 23:46:33 +0000261
Greg Ward9ddaaa11999-01-06 14:46:06 +0000262def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000263 """Return full pathname of installed Makefile from the Python build."""
264 if python_build:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100265 return os.path.join(_sys_home or os.path.dirname(sys.executable),
266 "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200267 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000268 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
269 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000270
Tarek Ziadé36797272010-07-22 12:50:05 +0000271
272def parse_config_h(fp, g=None):
273 """Parse a config.h-style file.
274
275 A dictionary containing name/value pairs is returned. If an
276 optional dictionary is passed in as the second argument, it is
277 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000278 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000279 if g is None:
280 g = {}
281 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
282 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
283 #
284 while True:
285 line = fp.readline()
286 if not line:
287 break
288 m = define_rx.match(line)
289 if m:
290 n, v = m.group(1, 2)
291 try: v = int(v)
292 except ValueError: pass
293 g[n] = v
294 else:
295 m = undef_rx.match(line)
296 if m:
297 g[m.group(1)] = 0
298 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000299
Greg Wardd283ce72000-09-17 00:53:02 +0000300
301# Regexes needed for parsing Makefile (and similar syntaxes,
302# like old-style Setup files).
303_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
304_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
305_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
306
Greg Ward3fff8d22000-09-15 00:03:13 +0000307def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000308 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000309
310 A dictionary containing name/value pairs is returned. If an
311 optional dictionary is passed in as the second argument, it is
312 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000313 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000314 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000315 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000316
317 if g is None:
318 g = {}
319 done = {}
320 notdone = {}
321
322 while True:
323 line = fp.readline()
324 if line is None: # eof
325 break
326 m = _variable_rx.match(line)
327 if m:
328 n, v = m.group(1, 2)
329 v = v.strip()
330 # `$$' is a literal `$' in make
331 tmpv = v.replace('$$', '')
332
333 if "$" in tmpv:
334 notdone[n] = v
335 else:
336 try:
337 v = int(v)
338 except ValueError:
339 # insert literal `$'
340 done[n] = v.replace('$$', '$')
341 else:
342 done[n] = v
343
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000344 # Variables with a 'PY_' prefix in the makefile. These need to
345 # be made available without that prefix through sysconfig.
346 # Special care is needed to ensure that variable expansion works, even
347 # if the expansion uses the name without a prefix.
348 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
349
Tarek Ziadé36797272010-07-22 12:50:05 +0000350 # do variable interpolation here
351 while notdone:
352 for name in list(notdone):
353 value = notdone[name]
354 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
355 if m:
356 n = m.group(1)
357 found = True
358 if n in done:
359 item = str(done[n])
360 elif n in notdone:
361 # get it on a subsequent round
362 found = False
363 elif n in os.environ:
364 # do it like make: fall back to environment
365 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000366
367 elif n in renamed_variables:
368 if name.startswith('PY_') and name[3:] in renamed_variables:
369 item = ""
370
371 elif 'PY_' + n in notdone:
372 found = False
373
374 else:
375 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000376 else:
377 done[n] = item = ""
378 if found:
379 after = value[m.end():]
380 value = value[:m.start()] + item + after
381 if "$" in after:
382 notdone[name] = value
383 else:
384 try: value = int(value)
385 except ValueError:
386 done[name] = value.strip()
387 else:
388 done[name] = value
389 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000390
391 if name.startswith('PY_') \
392 and name[3:] in renamed_variables:
393
394 name = name[3:]
395 if name not in done:
396 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000397 else:
398 # bogus variable reference; just drop it since we can't deal
399 del notdone[name]
400
401 fp.close()
402
Antoine Pitroudbec7802010-10-10 09:37:12 +0000403 # strip spurious spaces
404 for k, v in done.items():
405 if isinstance(v, str):
406 done[k] = v.strip()
407
Tarek Ziadé36797272010-07-22 12:50:05 +0000408 # save the results in the global dictionary
409 g.update(done)
410 return g
411
Greg Ward1190ee31998-12-18 23:46:33 +0000412
Greg Wardd283ce72000-09-17 00:53:02 +0000413def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000414 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000415 'string' according to 'vars' (a dictionary mapping variable names to
416 values). Variables not present in 'vars' are silently expanded to the
417 empty string. The variable values in 'vars' should not contain further
418 variable expansions; if 'vars' is the output of 'parse_makefile()',
419 you're fine. Returns a variable-expanded version of 's'.
420 """
421
422 # This algorithm does multiple expansion, so if vars['foo'] contains
423 # "${bar}", it will expand ${foo} to ${bar}, and then expand
424 # ${bar}... and so forth. This is fine as long as 'vars' comes from
425 # 'parse_makefile()', which takes care of such expansions eagerly,
426 # according to make's variable expansion semantics.
427
Collin Winter5b7e9d72007-08-30 03:52:21 +0000428 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000429 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
430 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000431 (beg, end) = m.span()
432 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
433 else:
434 break
435 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000436
437
438_config_vars = None
439
440def _init_posix():
441 """Initialize the module as appropriate for POSIX systems."""
442 g = {}
443 # load the installed Makefile:
444 try:
445 filename = get_makefile_filename()
446 parse_makefile(filename, g)
447 except IOError as msg:
448 my_msg = "invalid Python installation: unable to open %s" % filename
449 if hasattr(msg, "strerror"):
450 my_msg = my_msg + " (%s)" % msg.strerror
451
452 raise DistutilsPlatformError(my_msg)
453
454 # load the installed pyconfig.h:
455 try:
456 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000457 with open(filename) as file:
458 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000459 except IOError as msg:
460 my_msg = "invalid Python installation: unable to open %s" % filename
461 if hasattr(msg, "strerror"):
462 my_msg = my_msg + " (%s)" % msg.strerror
463
464 raise DistutilsPlatformError(my_msg)
465
Tarek Ziadé36797272010-07-22 12:50:05 +0000466 # On AIX, there are wrong paths to the linker scripts in the Makefile
467 # -- these paths are relative to the Python source, but when installed
468 # the scripts are in another directory.
469 if python_build:
470 g['LDSHARED'] = g['BLDSHARED']
471
472 elif get_python_version() < '2.1':
473 # The following two branches are for 1.5.2 compatibility.
474 if sys.platform == 'aix4': # what about AIX 3.x ?
475 # Linker script is in the config directory, not in Modules as the
476 # Makefile says.
477 python_lib = get_python_lib(standard_lib=1)
478 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
479 python_exp = os.path.join(python_lib, 'config', 'python.exp')
480
481 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
482
483 global _config_vars
484 _config_vars = g
485
486
487def _init_nt():
488 """Initialize the module as appropriate for NT"""
489 g = {}
490 # set basic install directories
491 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
492 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
493
494 # XXX hmmm.. a normal install puts include files here
495 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
496
497 g['SO'] = '.pyd'
498 g['EXE'] = ".exe"
499 g['VERSION'] = get_python_version().replace(".", "")
500 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
501
502 global _config_vars
503 _config_vars = g
504
505
Tarek Ziadé36797272010-07-22 12:50:05 +0000506def _init_os2():
507 """Initialize the module as appropriate for OS/2"""
508 g = {}
509 # set basic install directories
510 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
511 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
512
513 # XXX hmmm.. a normal install puts include files here
514 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
515
516 g['SO'] = '.pyd'
517 g['EXE'] = ".exe"
518
519 global _config_vars
520 _config_vars = g
521
522
523def get_config_vars(*args):
524 """With no arguments, return a dictionary of all configuration
525 variables relevant for the current platform. Generally this includes
526 everything needed to build extensions and install both pure modules and
527 extensions. On Unix, this means every variable defined in Python's
528 installed Makefile; on Windows and Mac OS it's a much smaller set.
529
530 With arguments, return a list of values that result from looking up
531 each argument in the configuration variable dictionary.
532 """
533 global _config_vars
534 if _config_vars is None:
535 func = globals().get("_init_" + os.name)
536 if func:
537 func()
538 else:
539 _config_vars = {}
540
541 # Normalized versions of prefix and exec_prefix are handy to have;
542 # in fact, these are the standard versions used most places in the
543 # Distutils.
544 _config_vars['prefix'] = PREFIX
545 _config_vars['exec_prefix'] = EXEC_PREFIX
546
547 # Convert srcdir into an absolute path if it appears necessary.
548 # Normally it is relative to the build directory. However, during
549 # testing, for example, we might be running a non-installed python
550 # from a different directory.
551 if python_build and os.name == "posix":
552 base = os.path.dirname(os.path.abspath(sys.executable))
553 if (not os.path.isabs(_config_vars['srcdir']) and
554 base != os.getcwd()):
555 # srcdir is relative and we are not in the same directory
556 # as the executable. Assume executable is in the build
557 # directory and make srcdir absolute.
558 srcdir = os.path.join(base, _config_vars['srcdir'])
559 _config_vars['srcdir'] = os.path.normpath(srcdir)
560
561 if sys.platform == 'darwin':
562 kernel_version = os.uname()[2] # Kernel version (8.4.3)
563 major_version = int(kernel_version.split('.')[0])
564
565 if major_version < 8:
566 # On Mac OS X before 10.4, check if -arch and -isysroot
567 # are in CFLAGS or LDFLAGS and remove them if they are.
568 # This is needed when building extensions on a 10.3 system
569 # using a universal build of python.
570 for key in ('LDFLAGS', 'BASECFLAGS',
571 # a number of derived variables. These need to be
572 # patched up as well.
573 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
574 flags = _config_vars[key]
575 flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
576 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
577 _config_vars[key] = flags
578
579 else:
580
581 # Allow the user to override the architecture flags using
582 # an environment variable.
583 # NOTE: This name was introduced by Apple in OSX 10.5 and
584 # is used by several scripting languages distributed with
585 # that OS release.
586
587 if 'ARCHFLAGS' in os.environ:
588 arch = os.environ['ARCHFLAGS']
589 for key in ('LDFLAGS', 'BASECFLAGS',
590 # a number of derived variables. These need to be
591 # patched up as well.
592 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
593
594 flags = _config_vars[key]
595 flags = re.sub('-arch\s+\w+\s', ' ', flags)
596 flags = flags + ' ' + arch
597 _config_vars[key] = flags
598
599 if args:
600 vals = []
601 for name in args:
602 vals.append(_config_vars.get(name))
603 return vals
604 else:
605 return _config_vars
606
607def get_config_var(name):
608 """Return the value of a single variable using the dictionary
609 returned by 'get_config_vars()'. Equivalent to
610 get_config_vars().get(name)
611 """
612 return get_config_vars().get(name)