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