blob: a3494670db18c1a7f43f4122ef14774bb5bf2fcb [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
Steve Dower65e4cb12014-11-22 12:54:57 -080012import _imp
Greg Ward9ddaaa11999-01-06 14:46:06 +000013import os
14import re
Tarek Ziadé36797272010-07-22 12:50:05 +000015import sys
Greg Ward1190ee31998-12-18 23:46:33 +000016
Tarek Ziadé36797272010-07-22 12:50:05 +000017from .errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000018
Tarek Ziadé36797272010-07-22 12:50:05 +000019# These are needed in a couple of spots, so just compute them once.
20PREFIX = os.path.normpath(sys.prefix)
21EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010022BASE_PREFIX = os.path.normpath(sys.base_prefix)
23BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000024
Tarek Ziadé36797272010-07-22 12:50:05 +000025# Path to the base directory of the project. On Windows the binary may
Stefan Grönkef1502d02017-09-25 18:58:10 +020026# live in project/PCbuild/win32 or 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))
Steve Dower85e102a2019-02-04 17:15:13 -080032
Tarek Ziadé8b441d02010-01-29 11:46:31 +000033
Tarek Ziadé36797272010-07-22 12:50:05 +000034# python_build: (Boolean) if true, we're either building Python or
35# building an extension with an un-installed Python, so we use
36# different (hard-wired) directories.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010037def _is_python_source_dir(d):
Antoine Pitrou961d54c2018-07-16 19:03:03 +020038 for fn in ("Setup", "Setup.local"):
Vinay Sajip7ded1f02012-05-26 03:45:29 +010039 if os.path.isfile(os.path.join(d, "Modules", fn)):
Tarek Ziadé36797272010-07-22 12:50:05 +000040 return True
41 return False
Steve Dower85e102a2019-02-04 17:15:13 -080042
Vinay Sajip7ded1f02012-05-26 03:45:29 +010043_sys_home = getattr(sys, '_home', None)
Steve Dower85e102a2019-02-04 17:15:13 -080044
45if os.name == 'nt':
46 def _fix_pcbuild(d):
47 if d and os.path.normcase(d).startswith(
48 os.path.normcase(os.path.join(PREFIX, "PCbuild"))):
49 return PREFIX
50 return d
51 project_base = _fix_pcbuild(project_base)
52 _sys_home = _fix_pcbuild(_sys_home)
53
Vinay Sajip7ded1f02012-05-26 03:45:29 +010054def _python_build():
55 if _sys_home:
56 return _is_python_source_dir(_sys_home)
57 return _is_python_source_dir(project_base)
Steve Dower85e102a2019-02-04 17:15:13 -080058
Christian Heimes2202f872008-02-06 14:31:34 +000059python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000060
Steve Dower85e102a2019-02-04 17:15:13 -080061
Barry Warsaw14d98ac2010-11-24 19:43:47 +000062# Calculate the build qualifier flags if they are defined. Adding the flags
63# to the include and lib directories only makes sense for an installation, not
64# an in-source build.
65build_flags = ''
66try:
67 if not python_build:
68 build_flags = sys.abiflags
69except AttributeError:
70 # It's not a configure-based build, so the sys module doesn't have
71 # this attribute, which is fine.
72 pass
73
Tarek Ziadé36797272010-07-22 12:50:05 +000074def get_python_version():
75 """Return a string containing the major and minor Python version,
76 leaving off the patchlevel. Sample return values could be '1.5'
77 or '2.2'.
78 """
Serhiy Storchaka885bdc42016-02-11 13:10:36 +020079 return '%d.%d' % sys.version_info[:2]
Tarek Ziadéedacea32010-01-29 11:41:03 +000080
Tarek Ziadé36797272010-07-22 12:50:05 +000081
82def get_python_inc(plat_specific=0, prefix=None):
83 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000084
85 If 'plat_specific' is false (the default), this is the path to the
86 non-platform-specific header files, i.e. Python.h and so on;
87 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000088 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000089
Vinay Sajip7ded1f02012-05-26 03:45:29 +010090 If 'prefix' is supplied, use it instead of sys.base_prefix or
91 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000092 """
Tarek Ziadé36797272010-07-22 12:50:05 +000093 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010094 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +000095 if os.name == "posix":
96 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +000097 # Assume the executable is in the build directory. The
98 # pyconfig.h file should be in the same directory. Since
99 # the build directory may not be the source directory, we
100 # must use "srcdir" from the makefile to find the "Include"
101 # directory.
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000102 if plat_specific:
Jeremy Klothdbdea622017-05-09 09:24:13 -0600103 return _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000104 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100105 incdir = os.path.join(get_config_var('srcdir'), 'Include')
Jeremy Klothdbdea622017-05-09 09:24:13 -0600106 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000107 python_dir = 'python' + get_python_version() + build_flags
108 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000109 elif os.name == "nt":
Steve Dower85e102a2019-02-04 17:15:13 -0800110 if python_build:
111 # Include both the include and PC dir to ensure we can find
112 # pyconfig.h
113 return (os.path.join(prefix, "include") + os.path.pathsep +
114 os.path.join(prefix, "PC"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000115 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000116 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000117 raise DistutilsPlatformError(
118 "I don't know where Python installs its C header files "
119 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000120
121
Tarek Ziadé36797272010-07-22 12:50:05 +0000122def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
123 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000124 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000125
Fred Drakec1ee39a2000-03-09 15:54:52 +0000126 If 'plat_specific' is true, return the directory containing
127 platform-specific modules, i.e. any module from a non-pure-Python
128 module distribution; otherwise, return the platform-shared library
129 directory. If 'standard_lib' is true, return the directory
130 containing standard Python library modules; otherwise, return the
131 directory for site-specific modules.
132
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100133 If 'prefix' is supplied, use it instead of sys.base_prefix or
134 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000135 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000136 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100137 if standard_lib:
138 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
139 else:
140 prefix = plat_specific and EXEC_PREFIX or PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000141
142 if os.name == "posix":
143 libpython = os.path.join(prefix,
144 "lib", "python" + get_python_version())
145 if standard_lib:
146 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000147 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000148 return os.path.join(libpython, "site-packages")
149 elif os.name == "nt":
150 if standard_lib:
151 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000152 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400153 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
Ned Deily7bc5fb62014-07-06 16:14:33 -0700178 # Use get_config_var() to ensure _config_vars is initialized.
179 if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700180 import _osx_support
181 _osx_support.customize_compiler(_config_vars)
182 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
183
Victor Stinner86082c22019-03-15 14:57:52 +0100184 (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
185 get_config_vars('CC', 'CXX', 'CFLAGS',
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700186 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
Tarek Ziadé36797272010-07-22 12:50:05 +0000187
188 if 'CC' in os.environ:
Ned Deily97345682013-05-28 16:35:30 -0700189 newcc = os.environ['CC']
190 if (sys.platform == 'darwin'
191 and 'LDSHARED' not in os.environ
192 and ldshared.startswith(cc)):
193 # On OS X, if CC is overridden, use that as the default
194 # command for LDSHARED as well
195 ldshared = newcc + ldshared[len(cc):]
196 cc = newcc
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:
Victor Stinner86082c22019-03-15 14:57:52 +0100208 cflags = cflags + ' ' + os.environ['CFLAGS']
Tarek Ziadé36797272010-07-22 12:50:05 +0000209 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
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700231 compiler.shared_lib_extension = shlib_suffix
Tarek Ziadé36797272010-07-22 12:50:05 +0000232
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)
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400243
244 return os.path.join(inc_dir, 'pyconfig.h')
Tarek Ziadé36797272010-07-22 12:50:05 +0000245
Greg Ward1190ee31998-12-18 23:46:33 +0000246
Greg Ward9ddaaa11999-01-06 14:46:06 +0000247def get_makefile_filename():
Tarek Ziadé36797272010-07-22 12:50:05 +0000248 """Return full pathname of installed Makefile from the Python build."""
249 if python_build:
doko@python.org97313302013-01-25 14:33:33 +0100250 return os.path.join(_sys_home or project_base, "Makefile")
Éric Araujofea2d042011-10-08 01:56:52 +0200251 lib_dir = get_python_lib(plat_specific=0, standard_lib=1)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000252 config_file = 'config-{}{}'.format(get_python_version(), build_flags)
doko@ubuntu.com55532312016-06-14 08:55:19 +0200253 if hasattr(sys.implementation, '_multiarch'):
254 config_file += '-%s' % sys.implementation._multiarch
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000255 return os.path.join(lib_dir, config_file, 'Makefile')
Greg Ward7d73b9e2000-03-09 03:16:05 +0000256
Tarek Ziadé36797272010-07-22 12:50:05 +0000257
258def parse_config_h(fp, g=None):
259 """Parse a config.h-style file.
260
261 A dictionary containing name/value pairs is returned. If an
262 optional dictionary is passed in as the second argument, it is
263 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000264 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000265 if g is None:
266 g = {}
267 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
268 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
269 #
270 while True:
271 line = fp.readline()
272 if not line:
273 break
274 m = define_rx.match(line)
275 if m:
276 n, v = m.group(1, 2)
277 try: v = int(v)
278 except ValueError: pass
279 g[n] = v
280 else:
281 m = undef_rx.match(line)
282 if m:
283 g[m.group(1)] = 0
284 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000285
Greg Wardd283ce72000-09-17 00:53:02 +0000286
287# Regexes needed for parsing Makefile (and similar syntaxes,
288# like old-style Setup files).
R David Murray44b548d2016-09-08 13:59:53 -0400289_variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
Greg Wardd283ce72000-09-17 00:53:02 +0000290_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
291_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
292
Greg Ward3fff8d22000-09-15 00:03:13 +0000293def parse_makefile(fn, g=None):
Tarek Ziadé36797272010-07-22 12:50:05 +0000294 """Parse a Makefile-style file.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000295
296 A dictionary containing name/value pairs is returned. If an
297 optional dictionary is passed in as the second argument, it is
298 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000299 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000300 from distutils.text_file import TextFile
Victor Stinner75d8c5c2010-10-23 17:02:31 +0000301 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
Tarek Ziadé36797272010-07-22 12:50:05 +0000302
303 if g is None:
304 g = {}
305 done = {}
306 notdone = {}
307
308 while True:
309 line = fp.readline()
310 if line is None: # eof
311 break
312 m = _variable_rx.match(line)
313 if m:
314 n, v = m.group(1, 2)
315 v = v.strip()
316 # `$$' is a literal `$' in make
317 tmpv = v.replace('$$', '')
318
319 if "$" in tmpv:
320 notdone[n] = v
321 else:
322 try:
323 v = int(v)
324 except ValueError:
325 # insert literal `$'
326 done[n] = v.replace('$$', '$')
327 else:
328 done[n] = v
329
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000330 # Variables with a 'PY_' prefix in the makefile. These need to
331 # be made available without that prefix through sysconfig.
332 # Special care is needed to ensure that variable expansion works, even
333 # if the expansion uses the name without a prefix.
334 renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
335
Tarek Ziadé36797272010-07-22 12:50:05 +0000336 # do variable interpolation here
337 while notdone:
338 for name in list(notdone):
339 value = notdone[name]
340 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
341 if m:
342 n = m.group(1)
343 found = True
344 if n in done:
345 item = str(done[n])
346 elif n in notdone:
347 # get it on a subsequent round
348 found = False
349 elif n in os.environ:
350 # do it like make: fall back to environment
351 item = os.environ[n]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000352
353 elif n in renamed_variables:
354 if name.startswith('PY_') and name[3:] in renamed_variables:
355 item = ""
356
357 elif 'PY_' + n in notdone:
358 found = False
359
360 else:
361 item = str(done['PY_' + n])
Tarek Ziadé36797272010-07-22 12:50:05 +0000362 else:
363 done[n] = item = ""
364 if found:
365 after = value[m.end():]
366 value = value[:m.start()] + item + after
367 if "$" in after:
368 notdone[name] = value
369 else:
370 try: value = int(value)
371 except ValueError:
372 done[name] = value.strip()
373 else:
374 done[name] = value
375 del notdone[name]
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000376
377 if name.startswith('PY_') \
378 and name[3:] in renamed_variables:
379
380 name = name[3:]
381 if name not in done:
382 done[name] = value
Tarek Ziadé36797272010-07-22 12:50:05 +0000383 else:
384 # bogus variable reference; just drop it since we can't deal
385 del notdone[name]
386
387 fp.close()
388
Antoine Pitroudbec7802010-10-10 09:37:12 +0000389 # strip spurious spaces
390 for k, v in done.items():
391 if isinstance(v, str):
392 done[k] = v.strip()
393
Tarek Ziadé36797272010-07-22 12:50:05 +0000394 # save the results in the global dictionary
395 g.update(done)
396 return g
397
Greg Ward1190ee31998-12-18 23:46:33 +0000398
Greg Wardd283ce72000-09-17 00:53:02 +0000399def expand_makefile_vars(s, vars):
Tarek Ziadé36797272010-07-22 12:50:05 +0000400 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
Greg Wardd283ce72000-09-17 00:53:02 +0000401 'string' according to 'vars' (a dictionary mapping variable names to
402 values). Variables not present in 'vars' are silently expanded to the
403 empty string. The variable values in 'vars' should not contain further
404 variable expansions; if 'vars' is the output of 'parse_makefile()',
405 you're fine. Returns a variable-expanded version of 's'.
406 """
407
408 # This algorithm does multiple expansion, so if vars['foo'] contains
409 # "${bar}", it will expand ${foo} to ${bar}, and then expand
410 # ${bar}... and so forth. This is fine as long as 'vars' comes from
411 # 'parse_makefile()', which takes care of such expansions eagerly,
412 # according to make's variable expansion semantics.
413
Collin Winter5b7e9d72007-08-30 03:52:21 +0000414 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000415 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
416 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000417 (beg, end) = m.span()
418 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
419 else:
420 break
421 return s
Tarek Ziadé36797272010-07-22 12:50:05 +0000422
423
424_config_vars = None
425
426def _init_posix():
427 """Initialize the module as appropriate for POSIX systems."""
doko@ubuntu.com40948222016-06-05 01:17:57 +0200428 # _sysconfigdata is generated at build time, see the sysconfig module
Xavier de Gaye92dec542016-09-11 22:22:24 +0200429 name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME',
430 '_sysconfigdata_{abi}_{platform}_{multiarch}'.format(
Zachary Ware80da9932016-09-09 18:29:10 -0700431 abi=sys.abiflags,
432 platform=sys.platform,
433 multiarch=getattr(sys.implementation, '_multiarch', ''),
Xavier de Gaye92dec542016-09-11 22:22:24 +0200434 ))
doko@ubuntu.comeea86b02016-06-14 09:22:16 +0200435 _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
436 build_time_vars = _temp.build_time_vars
Tarek Ziadé36797272010-07-22 12:50:05 +0000437 global _config_vars
doko@ubuntu.com40948222016-06-05 01:17:57 +0200438 _config_vars = {}
439 _config_vars.update(build_time_vars)
Tarek Ziadé36797272010-07-22 12:50:05 +0000440
441
442def _init_nt():
443 """Initialize the module as appropriate for NT"""
444 g = {}
445 # set basic install directories
446 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
447 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
448
449 # XXX hmmm.. a normal install puts include files here
450 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
451
Steve Dower65e4cb12014-11-22 12:54:57 -0800452 g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
Tarek Ziadé36797272010-07-22 12:50:05 +0000453 g['EXE'] = ".exe"
454 g['VERSION'] = get_python_version().replace(".", "")
455 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
456
457 global _config_vars
458 _config_vars = g
459
460
Tarek Ziadé36797272010-07-22 12:50:05 +0000461def get_config_vars(*args):
462 """With no arguments, return a dictionary of all configuration
463 variables relevant for the current platform. Generally this includes
464 everything needed to build extensions and install both pure modules and
465 extensions. On Unix, this means every variable defined in Python's
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700466 installed Makefile; on Windows it's a much smaller set.
Tarek Ziadé36797272010-07-22 12:50:05 +0000467
468 With arguments, return a list of values that result from looking up
469 each argument in the configuration variable dictionary.
470 """
471 global _config_vars
472 if _config_vars is None:
473 func = globals().get("_init_" + os.name)
474 if func:
475 func()
476 else:
477 _config_vars = {}
478
479 # Normalized versions of prefix and exec_prefix are handy to have;
480 # in fact, these are the standard versions used most places in the
481 # Distutils.
482 _config_vars['prefix'] = PREFIX
483 _config_vars['exec_prefix'] = EXEC_PREFIX
484
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500485 # For backward compatibility, see issue19555
486 SO = _config_vars.get('EXT_SUFFIX')
487 if SO is not None:
488 _config_vars['SO'] = SO
489
Richard Oudkerk46874ad2012-07-27 12:06:55 +0100490 # Always convert srcdir to an absolute path
491 srcdir = _config_vars.get('srcdir', project_base)
492 if os.name == 'posix':
493 if python_build:
494 # If srcdir is a relative path (typically '.' or '..')
495 # then it should be interpreted relative to the directory
496 # containing Makefile.
497 base = os.path.dirname(get_makefile_filename())
498 srcdir = os.path.join(base, srcdir)
499 else:
500 # srcdir is not meaningful since the installation is
501 # spread about the filesystem. We choose the
502 # directory containing the Makefile since we know it
503 # exists.
504 srcdir = os.path.dirname(get_makefile_filename())
505 _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
506
Tarek Ziadé36797272010-07-22 12:50:05 +0000507 # Convert srcdir into an absolute path if it appears necessary.
508 # Normally it is relative to the build directory. However, during
509 # testing, for example, we might be running a non-installed python
510 # from a different directory.
511 if python_build and os.name == "posix":
doko@python.org97313302013-01-25 14:33:33 +0100512 base = project_base
Tarek Ziadé36797272010-07-22 12:50:05 +0000513 if (not os.path.isabs(_config_vars['srcdir']) and
514 base != os.getcwd()):
515 # srcdir is relative and we are not in the same directory
516 # as the executable. Assume executable is in the build
517 # directory and make srcdir absolute.
518 srcdir = os.path.join(base, _config_vars['srcdir'])
519 _config_vars['srcdir'] = os.path.normpath(srcdir)
520
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700521 # OS X platforms require special customization to handle
522 # multi-architecture, multi-os-version installers
Tarek Ziadé36797272010-07-22 12:50:05 +0000523 if sys.platform == 'darwin':
Ned Deilydf8aa2b2012-07-21 05:36:30 -0700524 import _osx_support
525 _osx_support.customize_config_vars(_config_vars)
Ned Deily27471772012-07-15 21:30:03 -0700526
Tarek Ziadé36797272010-07-22 12:50:05 +0000527 if args:
528 vals = []
529 for name in args:
530 vals.append(_config_vars.get(name))
531 return vals
532 else:
533 return _config_vars
534
535def get_config_var(name):
536 """Return the value of a single variable using the dictionary
537 returned by 'get_config_vars()'. Equivalent to
538 get_config_vars().get(name)
539 """
Barry Warsaw9121f8d2013-11-22 15:31:35 -0500540 if name == 'SO':
541 import warnings
Serhiy Storchakaeaec3592013-11-26 17:08:24 +0200542 warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
Tarek Ziadé36797272010-07-22 12:50:05 +0000543 return get_config_vars().get(name)