blob: 317640ca89c184973c6138a2bb05bf5ee0e2f501 [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
Vinay Sajip048b0632012-07-16 18:24:55 +0100104 if _sys_home:
105 incdir = os.path.join(_sys_home, get_config_var('AST_H_DIR'))
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000106 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100107 incdir = os.path.join(get_config_var('srcdir'), 'Include')
108 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000109 python_dir = 'python' + get_python_version() + build_flags
110 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000111 elif os.name == "nt":
112 return os.path.join(prefix, "include")
Tarek Ziadé36797272010-07-22 12:50:05 +0000113 elif os.name == "os2":
114 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000115 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000116 raise DistutilsPlatformError(
117 "I don't know where Python installs its C header files "
118 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000119
120
Tarek Ziadé36797272010-07-22 12:50:05 +0000121def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
122 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000123 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000124
Fred Drakec1ee39a2000-03-09 15:54:52 +0000125 If 'plat_specific' is true, return the directory containing
126 platform-specific modules, i.e. any module from a non-pure-Python
127 module distribution; otherwise, return the platform-shared library
128 directory. If 'standard_lib' is true, return the directory
129 containing standard Python library modules; otherwise, return the
130 directory for site-specific modules.
131
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100132 If 'prefix' is supplied, use it instead of sys.base_prefix or
133 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000134 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000135 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100136 if standard_lib:
137 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
138 else:
139 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000140
141 if os.name == "posix":
142 libpython = os.path.join(prefix,
143 "lib", "python" + get_python_version())
144 if standard_lib:
145 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000146 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000147 return os.path.join(libpython, "site-packages")
148 elif os.name == "nt":
149 if standard_lib:
150 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000151 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000152 if get_python_version() < "2.2":
153 return prefix
154 else:
155 return os.path.join(prefix, "Lib", "site-packages")
Tarek Ziadé36797272010-07-22 12:50:05 +0000156 elif os.name == "os2":
157 if standard_lib:
158 return os.path.join(prefix, "Lib")
159 else:
160 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000161 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000162 raise DistutilsPlatformError(
163 "I don't know where Python installs its library "
164 "on platform '%s'" % os.name)
165
Ned Deilycbfb9a52012-06-23 16:02:19 -0700166
Tarek Ziadé36797272010-07-22 12:50:05 +0000167
168def customize_compiler(compiler):
169 """Do any platform-specific customization of a CCompiler instance.
170
171 Mainly needed on Unix, so we can plug in the information that
172 varies across Unices and is stored in Python's Makefile.
173 """
174 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700175 if sys.platform == "darwin":
176 # Perform first-time customization of compiler-related
177 # config vars on OS X now that we know we need a compiler.
178 # This is primarily to support Pythons from binary
179 # installers. The kind and paths to build tools on
180 # the user system may vary significantly from the system
181 # that Python itself was built on. Also the user OS
182 # version and build tools may not support the same set
183 # of CPU architectures for universal builds.
184 global _config_vars
185 if not _config_vars.get('CUSTOMIZED_OSX_COMPILER', ''):
186 import _osx_support
187 _osx_support.customize_compiler(_config_vars)
188 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
189
Tarek Ziadé36797272010-07-22 12:50:05 +0000190 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
191 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
192 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
193
Ned Deily99377482012-02-10 13:01:08 +0100194 newcc = None
Tarek Ziadé36797272010-07-22 12:50:05 +0000195 if 'CC' in os.environ:
Ned Deilycbfb9a52012-06-23 16:02:19 -0700196 cc = os.environ['CC']
Tarek Ziadé36797272010-07-22 12:50:05 +0000197 if 'CXX' in os.environ:
198 cxx = os.environ['CXX']
199 if 'LDSHARED' in os.environ:
200 ldshared = os.environ['LDSHARED']
201 if 'CPP' in os.environ:
202 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000203 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000204 cpp = cc + " -E" # not always
205 if 'LDFLAGS' in os.environ:
206 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
207 if 'CFLAGS' in os.environ:
208 cflags = opt + ' ' + os.environ['CFLAGS']
209 ldshared = ldshared + ' ' + os.environ['CFLAGS']
210 if 'CPPFLAGS' in os.environ:
211 cpp = cpp + ' ' + os.environ['CPPFLAGS']
212 cflags = cflags + ' ' + os.environ['CPPFLAGS']
213 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
214 if 'AR' in os.environ:
215 ar = os.environ['AR']
216 if 'ARFLAGS' in os.environ:
217 archiver = ar + ' ' + os.environ['ARFLAGS']
218 else:
219 archiver = ar + ' ' + ar_flags
220
221 cc_cmd = cc + ' ' + cflags
222 compiler.set_executables(
223 preprocessor=cpp,
224 compiler=cc_cmd,
225 compiler_so=cc_cmd + ' ' + ccshared,
226 compiler_cxx=cxx,
227 linker_so=ldshared,
228 linker_exe=cc,
229 archiver=archiver)
230
231 compiler.shared_lib_extension = so_ext
232
233
234def get_config_h_filename():
235 """Return full pathname of installed pyconfig.h file."""
236 if python_build:
237 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100238 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000239 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100240 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000241 else:
242 inc_dir = get_python_inc(plat_specific=1)
243 if get_python_version() < '2.2':
244 config_h = 'config.h'
245 else:
246 # The name of the config.h file changed in 2.2
247 config_h = 'pyconfig.h'
248 return os.path.join(inc_dir, config_h)
249
Greg Ward1190ee31998-12-18 23:46:33 +0000250
Greg Ward9ddaaa11999-01-06 14:46:06 +0000251def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000252 """Return full pathname of installed Makefile from the Python build."""
253 if python_build:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100254 return os.path.join(_sys_home or os.path.dirname(sys.executable),
255 "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200256 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000257 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
258 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000259
Tarek Ziadé36797272010-07-22 12:50:05 +0000260
261def parse_config_h(fp, g=None):
262 """Parse a config.h-style file.
263
264 A dictionary containing name/value pairs is returned. If an
265 optional dictionary is passed in as the second argument, it is
266 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000267 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000268 if g is None:
269 g = {}
270 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
271 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
272 #
273 while True:
274 line = fp.readline()
275 if not line:
276 break
277 m = define_rx.match(line)
278 if m:
279 n, v = m.group(1, 2)
280 try: v = int(v)
281 except ValueError: pass
282 g[n] = v
283 else:
284 m = undef_rx.match(line)
285 if m:
286 g[m.group(1)] = 0
287 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000288
Greg Wardd283ce72000-09-17 00:53:02 +0000289
290# Regexes needed for parsing Makefile (and similar syntaxes,
291# like old-style Setup files).
292_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
293_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
294_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
295
Greg Ward3fff8d22000-09-15 00:03:13 +0000296def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000297 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000298
299 A dictionary containing name/value pairs is returned. If an
300 optional dictionary is passed in as the second argument, it is
301 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000302 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000303 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000304 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000305
306 if g is None:
307 g = {}
308 done = {}
309 notdone = {}
310
311 while True:
312 line = fp.readline()
313 if line is None: # eof
314 break
315 m = _variable_rx.match(line)
316 if m:
317 n, v = m.group(1, 2)
318 v = v.strip()
319 # `$$' is a literal `$' in make
320 tmpv = v.replace('$$', '')
321
322 if "$" in tmpv:
323 notdone[n] = v
324 else:
325 try:
326 v = int(v)
327 except ValueError:
328 # insert literal `$'
329 done[n] = v.replace('$$', '$')
330 else:
331 done[n] = v
332
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000333 # Variables with a 'PY_' prefix in the makefile. These need to
334 # be made available without that prefix through sysconfig.
335 # Special care is needed to ensure that variable expansion works, even
336 # if the expansion uses the name without a prefix.
337 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
338
Tarek Ziadé36797272010-07-22 12:50:05 +0000339 # do variable interpolation here
340 while notdone:
341 for name in list(notdone):
342 value = notdone[name]
343 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
344 if m:
345 n = m.group(1)
346 found = True
347 if n in done:
348 item = str(done[n])
349 elif n in notdone:
350 # get it on a subsequent round
351 found = False
352 elif n in os.environ:
353 # do it like make: fall back to environment
354 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000355
356 elif n in renamed_variables:
357 if name.startswith('PY_') and name[3:] in renamed_variables:
358 item = ""
359
360 elif 'PY_' + n in notdone:
361 found = False
362
363 else:
364 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000365 else:
366 done[n] = item = ""
367 if found:
368 after = value[m.end():]
369 value = value[:m.start()] + item + after
370 if "$" in after:
371 notdone[name] = value
372 else:
373 try: value = int(value)
374 except ValueError:
375 done[name] = value.strip()
376 else:
377 done[name] = value
378 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000379
380 if name.startswith('PY_') \
381 and name[3:] in renamed_variables:
382
383 name = name[3:]
384 if name not in done:
385 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000386 else:
387 # bogus variable reference; just drop it since we can't deal
388 del notdone[name]
389
390 fp.close()
391
Antoine Pitroudbec7802010-10-10 09:37:12 +0000392 # strip spurious spaces
393 for k, v in done.items():
394 if isinstance(v, str):
395 done[k] = v.strip()
396
Tarek Ziadé36797272010-07-22 12:50:05 +0000397 # save the results in the global dictionary
398 g.update(done)
399 return g
400
Greg Ward1190ee31998-12-18 23:46:33 +0000401
Greg Wardd283ce72000-09-17 00:53:02 +0000402def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000403 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000404 'string' according to 'vars' (a dictionary mapping variable names to
405 values). Variables not present in 'vars' are silently expanded to the
406 empty string. The variable values in 'vars' should not contain further
407 variable expansions; if 'vars' is the output of 'parse_makefile()',
408 you're fine. Returns a variable-expanded version of 's'.
409 """
410
411 # This algorithm does multiple expansion, so if vars['foo'] contains
412 # "${bar}", it will expand ${foo} to ${bar}, and then expand
413 # ${bar}... and so forth. This is fine as long as 'vars' comes from
414 # 'parse_makefile()', which takes care of such expansions eagerly,
415 # according to make's variable expansion semantics.
416
Collin Winter5b7e9d72007-08-30 03:52:21 +0000417 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000418 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
419 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000420 (beg, end) = m.span()
421 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
422 else:
423 break
424 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000425
426
427_config_vars = None
428
429def _init_posix():
430 """Initialize the module as appropriate for POSIX systems."""
431 g = {}
432 # load the installed Makefile:
433 try:
434 filename = get_makefile_filename()
435 parse_makefile(filename, g)
436 except IOError as msg:
437 my_msg = "invalid Python installation: unable to open %s" % filename
438 if hasattr(msg, "strerror"):
439 my_msg = my_msg + " (%s)" % msg.strerror
440
441 raise DistutilsPlatformError(my_msg)
442
443 # load the installed pyconfig.h:
444 try:
445 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000446 with open(filename) as file:
447 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000448 except IOError as msg:
449 my_msg = "invalid Python installation: unable to open %s" % filename
450 if hasattr(msg, "strerror"):
451 my_msg = my_msg + " (%s)" % msg.strerror
452
453 raise DistutilsPlatformError(my_msg)
454
Tarek Ziadé36797272010-07-22 12:50:05 +0000455 # On AIX, there are wrong paths to the linker scripts in the Makefile
456 # -- these paths are relative to the Python source, but when installed
457 # the scripts are in another directory.
458 if python_build:
459 g['LDSHARED'] = g['BLDSHARED']
460
461 elif get_python_version() < '2.1':
462 # The following two branches are for 1.5.2 compatibility.
463 if sys.platform == 'aix4': # what about AIX 3.x ?
464 # Linker script is in the config directory, not in Modules as the
465 # Makefile says.
466 python_lib = get_python_lib(standard_lib=1)
467 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
468 python_exp = os.path.join(python_lib, 'config', 'python.exp')
469
470 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
471
472 global _config_vars
473 _config_vars = g
474
475
476def _init_nt():
477 """Initialize the module as appropriate for NT"""
478 g = {}
479 # set basic install directories
480 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
481 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
482
483 # XXX hmmm.. a normal install puts include files here
484 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
485
486 g['SO'] = '.pyd'
487 g['EXE'] = ".exe"
488 g['VERSION'] = get_python_version().replace(".", "")
489 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
490
491 global _config_vars
492 _config_vars = g
493
494
Tarek Ziadé36797272010-07-22 12:50:05 +0000495def _init_os2():
496 """Initialize the module as appropriate for OS/2"""
497 g = {}
498 # set basic install directories
499 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
500 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
501
502 # XXX hmmm.. a normal install puts include files here
503 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
504
505 g['SO'] = '.pyd'
506 g['EXE'] = ".exe"
507
508 global _config_vars
509 _config_vars = g
510
511
512def get_config_vars(*args):
513 """With no arguments, return a dictionary of all configuration
514 variables relevant for the current platform. Generally this includes
515 everything needed to build extensions and install both pure modules and
516 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700517 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000518
519 With arguments, return a list of values that result from looking up
520 each argument in the configuration variable dictionary.
521 """
522 global _config_vars
523 if _config_vars is None:
524 func = globals().get("_init_" + os.name)
525 if func:
526 func()
527 else:
528 _config_vars = {}
529
530 # Normalized versions of prefix and exec_prefix are handy to have;
531 # in fact, these are the standard versions used most places in the
532 # Distutils.
533 _config_vars['prefix'] = PREFIX
534 _config_vars['exec_prefix'] = EXEC_PREFIX
535
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100536 # Always convert srcdir to an absolute path
537 srcdir = _config_vars.get('srcdir', project_base)
538 if os.name == 'posix':
539 if python_build:
540 # If srcdir is a relative path (typically '.' or '..')
541 # then it should be interpreted relative to the directory
542 # containing Makefile.
543 base = os.path.dirname(get_makefile_filename())
544 srcdir = os.path.join(base, srcdir)
545 else:
546 # srcdir is not meaningful since the installation is
547 # spread about the filesystem. We choose the
548 # directory containing the Makefile since we know it
549 # exists.
550 srcdir = os.path.dirname(get_makefile_filename())
551 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
552
Tarek Ziadé36797272010-07-22 12:50:05 +0000553 # Convert srcdir into an absolute path if it appears necessary.
554 # Normally it is relative to the build directory. However, during
555 # testing, for example, we might be running a non-installed python
556 # from a different directory.
557 if python_build and os.name == "posix":
558 base = os.path.dirname(os.path.abspath(sys.executable))
559 if (not os.path.isabs(_config_vars['srcdir']) and
560 base != os.getcwd()):
561 # srcdir is relative and we are not in the same directory
562 # as the executable. Assume executable is in the build
563 # directory and make srcdir absolute.
564 srcdir = os.path.join(base, _config_vars['srcdir'])
565 _config_vars['srcdir'] = os.path.normpath(srcdir)
566
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700567 # OS X platforms require special customization to handle
568 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000569 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700570 import _osx_support
571 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700572
Tarek Ziadé36797272010-07-22 12:50:05 +0000573 if args:
574 vals = []
575 for name in args:
576 vals.append(_config_vars.get(name))
577 return vals
578 else:
579 return _config_vars
580
581def get_config_var(name):
582 """Return the value of a single variable using the dictionary
583 returned by 'get_config_vars()'. Equivalent to
584 get_config_vars().get(name)
585 """
586 return get_config_vars().get(name)