blob: b9479889f44159a78bdac536ea94cb3cf785c098 [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.
doko@python.org97313302013-01-25 14:33:33 +010027# set for cross builds
28if "_PYTHON_PROJECT_BASE" in os.environ:
29 project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
30else:
31 project_base = os.path.dirname(os.path.abspath(sys.executable))
Tarek Ziadé36797272010-07-22 12:50:05 +000032if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
33 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
34# PC/VS7.1
35if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
36 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
37 os.path.pardir))
38# PC/AMD64
39if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
40 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
41 os.path.pardir))
Tarek Ziadé8b441d02010-01-29 11:46:31 +000042
Tarek Ziadé36797272010-07-22 12:50:05 +000043# python_build: (Boolean) if true, we're either building Python or
44# building an extension with an un-installed Python, so we use
45# different (hard-wired) directories.
46# Setup.local is available for Makefile builds including VPATH builds,
47# Setup.dist is available on Windows
Vinay Sajip7ded1f02012-05-26 03:45:29 +010048def _is_python_source_dir(d):
Tarek Ziadé36797272010-07-22 12:50:05 +000049 for fn in ("Setup.dist", "Setup.local"):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010050 if os.path.isfile(os.path.join(d, "Modules", fn)):
Tarek Ziadé36797272010-07-22 12:50:05 +000051 return True
52 return False
Vinay Sajip7ded1f02012-05-26 03:45:29 +010053_sys_home = getattr(sys, '_home', None)
Vinay Sajip42211422012-05-26 20:36:12 +010054if _sys_home and os.name == 'nt' and \
55 _sys_home.lower().endswith(('pcbuild', 'pcbuild\\amd64')):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010056 _sys_home = os.path.dirname(_sys_home)
Vinay Sajip7e203492012-05-27 17:30:09 +010057 if _sys_home.endswith('pcbuild'): # must be amd64
58 _sys_home = os.path.dirname(_sys_home)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010059def _python_build():
60 if _sys_home:
61 return _is_python_source_dir(_sys_home)
62 return _is_python_source_dir(project_base)
Christian Heimes2202f872008-02-06 14:31:34 +000063python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000064
Barry Warsaw14d98ac2010-11-24 19:43:47 +000065# Calculate the build qualifier flags if they are defined. Adding the flags
66# to the include and lib directories only makes sense for an installation, not
67# an in-source build.
68build_flags = ''
69try:
70 if not python_build:
71 build_flags = sys.abiflags
72except AttributeError:
73 # It's not a configure-based build, so the sys module doesn't have
74 # this attribute, which is fine.
75 pass
76
Tarek Ziadé36797272010-07-22 12:50:05 +000077def get_python_version():
78 """Return a string containing the major and minor Python version,
79 leaving off the patchlevel. Sample return values could be '1.5'
80 or '2.2'.
81 """
82 return sys.version[:3]
Tarek Ziadéedacea32010-01-29 11:41:03 +000083
Tarek Ziadé36797272010-07-22 12:50:05 +000084
85def get_python_inc(plat_specific=0, prefix=None):
86 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000087
88 If 'plat_specific' is false (the default), this is the path to the
89 non-platform-specific header files, i.e. Python.h and so on;
90 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000091 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000092
Vinay Sajip7ded1f02012-05-26 03:45:29 +010093 If 'prefix' is supplied, use it instead of sys.base_prefix or
94 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000095 """
Tarek Ziadé36797272010-07-22 12:50:05 +000096 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010097 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +000098 if os.name == "posix":
99 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000100 # Assume the executable is in the build directory. The
101 # pyconfig.h file should be in the same directory. Since
102 # the build directory may not be the source directory, we
103 # must use "srcdir" from the makefile to find the "Include"
104 # directory.
doko@python.org97313302013-01-25 14:33:33 +0100105 base = _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000106 if plat_specific:
107 return base
Vinay Sajip048b0632012-07-16 18:24:55 +0100108 if _sys_home:
109 incdir = os.path.join(_sys_home, get_config_var('AST_H_DIR'))
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000110 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100111 incdir = os.path.join(get_config_var('srcdir'), 'Include')
112 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000113 python_dir = 'python' + get_python_version() + build_flags
114 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000115 elif os.name == "nt":
116 return os.path.join(prefix, "include")
Tarek Ziadé36797272010-07-22 12:50:05 +0000117 elif os.name == "os2":
118 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000119 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000120 raise DistutilsPlatformError(
121 "I don't know where Python installs its C header files "
122 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000123
124
Tarek Ziadé36797272010-07-22 12:50:05 +0000125def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
126 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000127 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000128
Fred Drakec1ee39a2000-03-09 15:54:52 +0000129 If 'plat_specific' is true, return the directory containing
130 platform-specific modules, i.e. any module from a non-pure-Python
131 module distribution; otherwise, return the platform-shared library
132 directory. If 'standard_lib' is true, return the directory
133 containing standard Python library modules; otherwise, return the
134 directory for site-specific modules.
135
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100136 If 'prefix' is supplied, use it instead of sys.base_prefix or
137 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000138 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000139 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100140 if standard_lib:
141 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
142 else:
143 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000144
145 if os.name == "posix":
146 libpython = os.path.join(prefix,
147 "lib", "python" + get_python_version())
148 if standard_lib:
149 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000150 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000151 return os.path.join(libpython, "site-packages")
152 elif os.name == "nt":
153 if standard_lib:
154 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000155 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000156 if get_python_version() < "2.2":
157 return prefix
158 else:
159 return os.path.join(prefix, "Lib", "site-packages")
Tarek Ziadé36797272010-07-22 12:50:05 +0000160 elif os.name == "os2":
161 if standard_lib:
162 return os.path.join(prefix, "Lib")
163 else:
164 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000165 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000166 raise DistutilsPlatformError(
167 "I don't know where Python installs its library "
168 "on platform '%s'" % os.name)
169
Ned Deilycbfb9a52012-06-23 16:02:19 -0700170
Tarek Ziadé36797272010-07-22 12:50:05 +0000171
172def customize_compiler(compiler):
173 """Do any platform-specific customization of a CCompiler instance.
174
175 Mainly needed on Unix, so we can plug in the information that
176 varies across Unices and is stored in Python's Makefile.
177 """
178 if compiler.compiler_type == "unix":
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700179 if sys.platform == "darwin":
180 # Perform first-time customization of compiler-related
181 # config vars on OS X now that we know we need a compiler.
182 # This is primarily to support Pythons from binary
183 # installers. The kind and paths to build tools on
184 # the user system may vary significantly from the system
185 # that Python itself was built on. Also the user OS
186 # version and build tools may not support the same set
187 # of CPU architectures for universal builds.
188 global _config_vars
189 if not _config_vars.get('CUSTOMIZED_OSX_COMPILER', ''):
190 import _osx_support
191 _osx_support.customize_compiler(_config_vars)
192 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
193
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700194 (cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
Tarek Ziadé36797272010-07-22 12:50:05 +0000195 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700196 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000197
198 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700199 newcc = os.environ['CC']
200 if (sys.platform == 'darwin'
201 and 'LDSHARED' not in os.environ
202 and ldshared.startswith(cc)):
203 # On OS X, if CC is overridden, use that as the default
204 # command for LDSHARED as well
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
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700241 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000242
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:
doko@python.org97313302013-01-25 14:33:33 +0100264 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200265 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000266 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
267 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000268
Tarek Ziadé36797272010-07-22 12:50:05 +0000269
270def parse_config_h(fp, g=None):
271 """Parse a config.h-style file.
272
273 A dictionary containing name/value pairs is returned. If an
274 optional dictionary is passed in as the second argument, it is
275 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000276 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000277 if g is None:
278 g = {}
279 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
280 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
281 #
282 while True:
283 line = fp.readline()
284 if not line:
285 break
286 m = define_rx.match(line)
287 if m:
288 n, v = m.group(1, 2)
289 try: v = int(v)
290 except ValueError: pass
291 g[n] = v
292 else:
293 m = undef_rx.match(line)
294 if m:
295 g[m.group(1)] = 0
296 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000297
Greg Wardd283ce72000-09-17 00:53:02 +0000298
299# Regexes needed for parsing Makefile (and similar syntaxes,
300# like old-style Setup files).
301_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
302_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
303_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
304
Greg Ward3fff8d22000-09-15 00:03:13 +0000305def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000306 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000307
308 A dictionary containing name/value pairs is returned. If an
309 optional dictionary is passed in as the second argument, it is
310 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000311 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000312 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000313 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000314
315 if g is None:
316 g = {}
317 done = {}
318 notdone = {}
319
320 while True:
321 line = fp.readline()
322 if line is None: # eof
323 break
324 m = _variable_rx.match(line)
325 if m:
326 n, v = m.group(1, 2)
327 v = v.strip()
328 # `$$' is a literal `$' in make
329 tmpv = v.replace('$$', '')
330
331 if "$" in tmpv:
332 notdone[n] = v
333 else:
334 try:
335 v = int(v)
336 except ValueError:
337 # insert literal `$'
338 done[n] = v.replace('$$', '$')
339 else:
340 done[n] = v
341
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000342 # Variables with a 'PY_' prefix in the makefile. These need to
343 # be made available without that prefix through sysconfig.
344 # Special care is needed to ensure that variable expansion works, even
345 # if the expansion uses the name without a prefix.
346 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
347
Tarek Ziadé36797272010-07-22 12:50:05 +0000348 # do variable interpolation here
349 while notdone:
350 for name in list(notdone):
351 value = notdone[name]
352 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
353 if m:
354 n = m.group(1)
355 found = True
356 if n in done:
357 item = str(done[n])
358 elif n in notdone:
359 # get it on a subsequent round
360 found = False
361 elif n in os.environ:
362 # do it like make: fall back to environment
363 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000364
365 elif n in renamed_variables:
366 if name.startswith('PY_') and name[3:] in renamed_variables:
367 item = ""
368
369 elif 'PY_' + n in notdone:
370 found = False
371
372 else:
373 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000374 else:
375 done[n] = item = ""
376 if found:
377 after = value[m.end():]
378 value = value[:m.start()] + item + after
379 if "$" in after:
380 notdone[name] = value
381 else:
382 try: value = int(value)
383 except ValueError:
384 done[name] = value.strip()
385 else:
386 done[name] = value
387 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000388
389 if name.startswith('PY_') \
390 and name[3:] in renamed_variables:
391
392 name = name[3:]
393 if name not in done:
394 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000395 else:
396 # bogus variable reference; just drop it since we can't deal
397 del notdone[name]
398
399 fp.close()
400
Antoine Pitroudbec7802010-10-10 09:37:12 +0000401 # strip spurious spaces
402 for k, v in done.items():
403 if isinstance(v, str):
404 done[k] = v.strip()
405
Tarek Ziadé36797272010-07-22 12:50:05 +0000406 # save the results in the global dictionary
407 g.update(done)
408 return g
409
Greg Ward1190ee31998-12-18 23:46:33 +0000410
Greg Wardd283ce72000-09-17 00:53:02 +0000411def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000412 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000413 'string' according to 'vars' (a dictionary mapping variable names to
414 values). Variables not present in 'vars' are silently expanded to the
415 empty string. The variable values in 'vars' should not contain further
416 variable expansions; if 'vars' is the output of 'parse_makefile()',
417 you're fine. Returns a variable-expanded version of 's'.
418 """
419
420 # This algorithm does multiple expansion, so if vars['foo'] contains
421 # "${bar}", it will expand ${foo} to ${bar}, and then expand
422 # ${bar}... and so forth. This is fine as long as 'vars' comes from
423 # 'parse_makefile()', which takes care of such expansions eagerly,
424 # according to make's variable expansion semantics.
425
Collin Winter5b7e9d72007-08-30 03:52:21 +0000426 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000427 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
428 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000429 (beg, end) = m.span()
430 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
431 else:
432 break
433 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000434
435
436_config_vars = None
437
438def _init_posix():
439 """Initialize the module as appropriate for POSIX systems."""
440 g = {}
441 # load the installed Makefile:
442 try:
443 filename = get_makefile_filename()
444 parse_makefile(filename, g)
445 except IOError as msg:
446 my_msg = "invalid Python installation: unable to open %s" % filename
447 if hasattr(msg, "strerror"):
448 my_msg = my_msg + " (%s)" % msg.strerror
449
450 raise DistutilsPlatformError(my_msg)
451
452 # load the installed pyconfig.h:
453 try:
454 filename = get_config_h_filename()
Brett Cannon5c035c02010-10-29 22:36:08 +0000455 with open(filename) as file:
456 parse_config_h(file, g)
Tarek Ziadé36797272010-07-22 12:50:05 +0000457 except IOError as msg:
458 my_msg = "invalid Python installation: unable to open %s" % filename
459 if hasattr(msg, "strerror"):
460 my_msg = my_msg + " (%s)" % msg.strerror
461
462 raise DistutilsPlatformError(my_msg)
463
Tarek Ziadé36797272010-07-22 12:50:05 +0000464 # On AIX, there are wrong paths to the linker scripts in the Makefile
465 # -- these paths are relative to the Python source, but when installed
466 # the scripts are in another directory.
467 if python_build:
468 g['LDSHARED'] = g['BLDSHARED']
469
470 elif get_python_version() < '2.1':
471 # The following two branches are for 1.5.2 compatibility.
472 if sys.platform == 'aix4': # what about AIX 3.x ?
473 # Linker script is in the config directory, not in Modules as the
474 # Makefile says.
475 python_lib = get_python_lib(standard_lib=1)
476 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
477 python_exp = os.path.join(python_lib, 'config', 'python.exp')
478
479 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
480
481 global _config_vars
482 _config_vars = g
483
484
485def _init_nt():
486 """Initialize the module as appropriate for NT"""
487 g = {}
488 # set basic install directories
489 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
490 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
491
492 # XXX hmmm.. a normal install puts include files here
493 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
494
495 g['SO'] = '.pyd'
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700496 g['EXT_SUFFIX'] = '.pyd'
Tarek Ziadé36797272010-07-22 12:50:05 +0000497 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'
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700516 g['EXT_SUFFIX'] = '.pyd'
Tarek Ziadé36797272010-07-22 12:50:05 +0000517 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
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700528 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000529
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
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100547 # Always convert srcdir to an absolute path
548 srcdir = _config_vars.get('srcdir', project_base)
549 if os.name == 'posix':
550 if python_build:
551 # If srcdir is a relative path (typically '.' or '..')
552 # then it should be interpreted relative to the directory
553 # containing Makefile.
554 base = os.path.dirname(get_makefile_filename())
555 srcdir = os.path.join(base, srcdir)
556 else:
557 # srcdir is not meaningful since the installation is
558 # spread about the filesystem. We choose the
559 # directory containing the Makefile since we know it
560 # exists.
561 srcdir = os.path.dirname(get_makefile_filename())
562 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
563
Tarek Ziadé36797272010-07-22 12:50:05 +0000564 # Convert srcdir into an absolute path if it appears necessary.
565 # Normally it is relative to the build directory. However, during
566 # testing, for example, we might be running a non-installed python
567 # from a different directory.
568 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100569 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000570 if (not os.path.isabs(_config_vars['srcdir']) and
571 base != os.getcwd()):
572 # srcdir is relative and we are not in the same directory
573 # as the executable. Assume executable is in the build
574 # directory and make srcdir absolute.
575 srcdir = os.path.join(base, _config_vars['srcdir'])
576 _config_vars['srcdir'] = os.path.normpath(srcdir)
577
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700578 # OS X platforms require special customization to handle
579 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000580 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700581 import _osx_support
582 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700583
Tarek Ziadé36797272010-07-22 12:50:05 +0000584 if args:
585 vals = []
586 for name in args:
587 vals.append(_config_vars.get(name))
588 return vals
589 else:
590 return _config_vars
591
592def get_config_var(name):
593 """Return the value of a single variable using the dictionary
594 returned by 'get_config_vars()'. Equivalent to
595 get_config_vars().get(name)
596 """
597 return get_config_vars().get(name)