blob: 3b6549fcccc3104a911cf50cdb123e98b0eb45d5 [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")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000113 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000114 raise DistutilsPlatformError(
115 "I don't know where Python installs its C header files "
116 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000117
118
Tarek Ziadé36797272010-07-22 12:50:05 +0000119def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
120 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000121 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000122
Fred Drakec1ee39a2000-03-09 15:54:52 +0000123 If 'plat_specific' is true, return the directory containing
124 platform-specific modules, i.e. any module from a non-pure-Python
125 module distribution; otherwise, return the platform-shared library
126 directory. If 'standard_lib' is true, return the directory
127 containing standard Python library modules; otherwise, return the
128 directory for site-specific modules.
129
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100130 If 'prefix' is supplied, use it instead of sys.base_prefix or
131 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000132 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000133 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100134 if standard_lib:
135 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
136 else:
137 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000138
139 if os.name == "posix":
140 libpython = os.path.join(prefix,
141 "lib", "python" + get_python_version())
142 if standard_lib:
143 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000144 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000145 return os.path.join(libpython, "site-packages")
146 elif os.name == "nt":
147 if standard_lib:
148 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000149 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000150 if get_python_version() < "2.2":
151 return prefix
152 else:
153 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000154 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000155 raise DistutilsPlatformError(
156 "I don't know where Python installs its library "
157 "on platform '%s'" % os.name)
158
Ned Deilycbfb9a52012-06-23 16:02:19 -0700159
Tarek Ziadé36797272010-07-22 12:50:05 +0000160
161def customize_compiler(compiler):
162 """Do any platform-specific customization of a CCompiler instance.
163
164 Mainly needed on Unix, so we can plug in the information that
165 varies across Unices and is stored in Python's Makefile.
166 """
167 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700168 if sys.platform == "darwin":
169 # Perform first-time customization of compiler-related
170 # config vars on OS X now that we know we need a compiler.
171 # This is primarily to support Pythons from binary
172 # installers. The kind and paths to build tools on
173 # the user system may vary significantly from the system
174 # that Python itself was built on. Also the user OS
175 # version and build tools may not support the same set
176 # of CPU architectures for universal builds.
177 global _config_vars
178 if not _config_vars.get('CUSTOMIZED_OSX_COMPILER', ''):
179 import _osx_support
180 _osx_support.customize_compiler(_config_vars)
181 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
182
Tarek Ziadé36797272010-07-22 12:50:05 +0000183 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
184 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
185 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
186
Ned Deily99377482012-02-10 13:01:08 +0100187 newcc = None
Tarek Ziadé36797272010-07-22 12:50:05 +0000188 if 'CC' in os.environ:
Ned Deilycbfb9a52012-06-23 16:02:19 -0700189 cc = os.environ['CC']
Tarek Ziadé36797272010-07-22 12:50:05 +0000190 if 'CXX' in os.environ:
191 cxx = os.environ['CXX']
192 if 'LDSHARED' in os.environ:
193 ldshared = os.environ['LDSHARED']
194 if 'CPP' in os.environ:
195 cpp = os.environ['CPP']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000196 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000197 cpp = cc + " -E" # not always
198 if 'LDFLAGS' in os.environ:
199 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
200 if 'CFLAGS' in os.environ:
201 cflags = opt + ' ' + os.environ['CFLAGS']
202 ldshared = ldshared + ' ' + os.environ['CFLAGS']
203 if 'CPPFLAGS' in os.environ:
204 cpp = cpp + ' ' + os.environ['CPPFLAGS']
205 cflags = cflags + ' ' + os.environ['CPPFLAGS']
206 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
207 if 'AR' in os.environ:
208 ar = os.environ['AR']
209 if 'ARFLAGS' in os.environ:
210 archiver = ar + ' ' + os.environ['ARFLAGS']
211 else:
212 archiver = ar + ' ' + ar_flags
213
214 cc_cmd = cc + ' ' + cflags
215 compiler.set_executables(
216 preprocessor=cpp,
217 compiler=cc_cmd,
218 compiler_so=cc_cmd + ' ' + ccshared,
219 compiler_cxx=cxx,
220 linker_so=ldshared,
221 linker_exe=cc,
222 archiver=archiver)
223
224 compiler.shared_lib_extension = so_ext
225
226
227def get_config_h_filename():
228 """Return full pathname of installed pyconfig.h file."""
229 if python_build:
230 if os.name == "nt":
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100231 inc_dir = os.path.join(_sys_home or project_base, "PC")
Tarek Ziadé36797272010-07-22 12:50:05 +0000232 else:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100233 inc_dir = _sys_home or project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000234 else:
235 inc_dir = get_python_inc(plat_specific=1)
236 if get_python_version() < '2.2':
237 config_h = 'config.h'
238 else:
239 # The name of the config.h file changed in 2.2
240 config_h = 'pyconfig.h'
241 return os.path.join(inc_dir, config_h)
242
Greg Ward1190ee31998-12-18 23:46:33 +0000243
Greg Ward9ddaaa11999-01-06 14:46:06 +0000244def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000245 """Return full pathname of installed Makefile from the Python build."""
246 if python_build:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100247 return os.path.join(_sys_home or os.path.dirname(sys.executable),
248 "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200249 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000250 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
251 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000252
Tarek Ziadé36797272010-07-22 12:50:05 +0000253
254def parse_config_h(fp, g=None):
255 """Parse a config.h-style file.
256
257 A dictionary containing name/value pairs is returned. If an
258 optional dictionary is passed in as the second argument, it is
259 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000260 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000261 if g is None:
262 g = {}
263 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
264 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
265 #
266 while True:
267 line = fp.readline()
268 if not line:
269 break
270 m = define_rx.match(line)
271 if m:
272 n, v = m.group(1, 2)
273 try: v = int(v)
274 except ValueError: pass
275 g[n] = v
276 else:
277 m = undef_rx.match(line)
278 if m:
279 g[m.group(1)] = 0
280 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000281
Greg Wardd283ce72000-09-17 00:53:02 +0000282
283# Regexes needed for parsing Makefile (and similar syntaxes,
284# like old-style Setup files).
285_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
286_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
287_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
288
Greg Ward3fff8d22000-09-15 00:03:13 +0000289def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000290 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000291
292 A dictionary containing name/value pairs is returned. If an
293 optional dictionary is passed in as the second argument, it is
294 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000295 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000296 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000297 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000298
299 if g is None:
300 g = {}
301 done = {}
302 notdone = {}
303
304 while True:
305 line = fp.readline()
306 if line is None: # eof
307 break
308 m = _variable_rx.match(line)
309 if m:
310 n, v = m.group(1, 2)
311 v = v.strip()
312 # `$$' is a literal `$' in make
313 tmpv = v.replace('$$', '')
314
315 if "$" in tmpv:
316 notdone[n] = v
317 else:
318 try:
319 v = int(v)
320 except ValueError:
321 # insert literal `$'
322 done[n] = v.replace('$$', '$')
323 else:
324 done[n] = v
325
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000326 # Variables with a 'PY_' prefix in the makefile. These need to
327 # be made available without that prefix through sysconfig.
328 # Special care is needed to ensure that variable expansion works, even
329 # if the expansion uses the name without a prefix.
330 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
331
Tarek Ziadé36797272010-07-22 12:50:05 +0000332 # do variable interpolation here
333 while notdone:
334 for name in list(notdone):
335 value = notdone[name]
336 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
337 if m:
338 n = m.group(1)
339 found = True
340 if n in done:
341 item = str(done[n])
342 elif n in notdone:
343 # get it on a subsequent round
344 found = False
345 elif n in os.environ:
346 # do it like make: fall back to environment
347 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000348
349 elif n in renamed_variables:
350 if name.startswith('PY_') and name[3:] in renamed_variables:
351 item = ""
352
353 elif 'PY_' + n in notdone:
354 found = False
355
356 else:
357 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000358 else:
359 done[n] = item = ""
360 if found:
361 after = value[m.end():]
362 value = value[:m.start()] + item + after
363 if "$" in after:
364 notdone[name] = value
365 else:
366 try: value = int(value)
367 except ValueError:
368 done[name] = value.strip()
369 else:
370 done[name] = value
371 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000372
373 if name.startswith('PY_') \
374 and name[3:] in renamed_variables:
375
376 name = name[3:]
377 if name not in done:
378 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000379 else:
380 # bogus variable reference; just drop it since we can't deal
381 del notdone[name]
382
383 fp.close()
384
Antoine Pitroudbec7802010-10-10 09:37:12 +0000385 # strip spurious spaces
386 for k, v in done.items():
387 if isinstance(v, str):
388 done[k] = v.strip()
389
Tarek Ziadé36797272010-07-22 12:50:05 +0000390 # save the results in the global dictionary
391 g.update(done)
392 return g
393
Greg Ward1190ee31998-12-18 23:46:33 +0000394
Greg Wardd283ce72000-09-17 00:53:02 +0000395def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000396 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000397 'string' according to 'vars' (a dictionary mapping variable names to
398 values). Variables not present in 'vars' are silently expanded to the
399 empty string. The variable values in 'vars' should not contain further
400 variable expansions; if 'vars' is the output of 'parse_makefile()',
401 you're fine. Returns a variable-expanded version of 's'.
402 """
403
404 # This algorithm does multiple expansion, so if vars['foo'] contains
405 # "${bar}", it will expand ${foo} to ${bar}, and then expand
406 # ${bar}... and so forth. This is fine as long as 'vars' comes from
407 # 'parse_makefile()', which takes care of such expansions eagerly,
408 # according to make's variable expansion semantics.
409
Collin Winter5b7e9d72007-08-30 03:52:21 +0000410 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000411 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
412 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000413 (beg, end) = m.span()
414 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
415 else:
416 break
417 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000418
419
420_config_vars = None
421
422def _init_posix():
423 """Initialize the module as appropriate for POSIX systems."""
424 g = {}
425 # load the installed Makefile:
426 try:
427 filename = get_makefile_filename()
428 parse_makefile(filename, g)
429 except IOError as msg:
430 my_msg = "invalid Python installation: unable to open %s" % filename
431 if hasattr(msg, "strerror"):
432 my_msg = my_msg + " (%s)" % msg.strerror
433
434 raise DistutilsPlatformError(my_msg)
435
436 # load the installed pyconfig.h:
437 try:
438 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000439 with open(filename) as file:
440 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000441 except IOError as msg:
442 my_msg = "invalid Python installation: unable to open %s" % filename
443 if hasattr(msg, "strerror"):
444 my_msg = my_msg + " (%s)" % msg.strerror
445
446 raise DistutilsPlatformError(my_msg)
447
Tarek Ziadé36797272010-07-22 12:50:05 +0000448 # On AIX, there are wrong paths to the linker scripts in the Makefile
449 # -- these paths are relative to the Python source, but when installed
450 # the scripts are in another directory.
451 if python_build:
452 g['LDSHARED'] = g['BLDSHARED']
453
454 elif get_python_version() < '2.1':
455 # The following two branches are for 1.5.2 compatibility.
456 if sys.platform == 'aix4': # what about AIX 3.x ?
457 # Linker script is in the config directory, not in Modules as the
458 # Makefile says.
459 python_lib = get_python_lib(standard_lib=1)
460 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
461 python_exp = os.path.join(python_lib, 'config', 'python.exp')
462
463 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
464
465 global _config_vars
466 _config_vars = g
467
468
469def _init_nt():
470 """Initialize the module as appropriate for NT"""
471 g = {}
472 # set basic install directories
473 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
474 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
475
476 # XXX hmmm.. a normal install puts include files here
477 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
478
479 g['SO'] = '.pyd'
480 g['EXE'] = ".exe"
481 g['VERSION'] = get_python_version().replace(".", "")
482 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
483
484 global _config_vars
485 _config_vars = g
486
487
Tarek Ziadé36797272010-07-22 12:50:05 +0000488def get_config_vars(*args):
489 """With no arguments, return a dictionary of all configuration
490 variables relevant for the current platform. Generally this includes
491 everything needed to build extensions and install both pure modules and
492 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700493 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000494
495 With arguments, return a list of values that result from looking up
496 each argument in the configuration variable dictionary.
497 """
498 global _config_vars
499 if _config_vars is None:
500 func = globals().get("_init_" + os.name)
501 if func:
502 func()
503 else:
504 _config_vars = {}
505
506 # Normalized versions of prefix and exec_prefix are handy to have;
507 # in fact, these are the standard versions used most places in the
508 # Distutils.
509 _config_vars['prefix'] = PREFIX
510 _config_vars['exec_prefix'] = EXEC_PREFIX
511
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100512 # Always convert srcdir to an absolute path
513 srcdir = _config_vars.get('srcdir', project_base)
514 if os.name == 'posix':
515 if python_build:
516 # If srcdir is a relative path (typically '.' or '..')
517 # then it should be interpreted relative to the directory
518 # containing Makefile.
519 base = os.path.dirname(get_makefile_filename())
520 srcdir = os.path.join(base, srcdir)
521 else:
522 # srcdir is not meaningful since the installation is
523 # spread about the filesystem. We choose the
524 # directory containing the Makefile since we know it
525 # exists.
526 srcdir = os.path.dirname(get_makefile_filename())
527 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
528
Tarek Ziadé36797272010-07-22 12:50:05 +0000529 # Convert srcdir into an absolute path if it appears necessary.
530 # Normally it is relative to the build directory. However, during
531 # testing, for example, we might be running a non-installed python
532 # from a different directory.
533 if python_build and os.name == "posix":
534 base = os.path.dirname(os.path.abspath(sys.executable))
535 if (not os.path.isabs(_config_vars['srcdir']) and
536 base != os.getcwd()):
537 # srcdir is relative and we are not in the same directory
538 # as the executable. Assume executable is in the build
539 # directory and make srcdir absolute.
540 srcdir = os.path.join(base, _config_vars['srcdir'])
541 _config_vars['srcdir'] = os.path.normpath(srcdir)
542
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700543 # OS X platforms require special customization to handle
544 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000545 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700546 import _osx_support
547 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700548
Tarek Ziadé36797272010-07-22 12:50:05 +0000549 if args:
550 vals = []
551 for name in args:
552 vals.append(_config_vars.get(name))
553 return vals
554 else:
555 return _config_vars
556
557def get_config_var(name):
558 """Return the value of a single variable using the dictionary
559 returned by 'get_config_vars()'. Equivalent to
560 get_config_vars().get(name)
561 """
562 return get_config_vars().get(name)