blob: 4a4fadde92834af767ef03a6422e668d646149c2 [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 Ward82d71ca2000-06-03 00:44:30 +000012__revision__ = "$Id$"
Greg Ward1190ee31998-12-18 23:46:33 +000013
Greg Ward9ddaaa11999-01-06 14:46:06 +000014import os
15import re
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +000016import string
Greg Ward9ddaaa11999-01-06 14:46:06 +000017import sys
Greg Ward1190ee31998-12-18 23:46:33 +000018
Guido van Rossumf8480a72006-03-15 23:08:13 +000019from distutils.errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000020
Greg Ward879f0f12000-09-15 01:15:08 +000021# These are needed in a couple of spots, so just compute them once.
Greg Wardcf6bea32000-04-10 01:15:06 +000022PREFIX = os.path.normpath(sys.prefix)
23EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000024
Christian Heimesd3fc07a42007-12-06 13:15:13 +000025# Path to the base directory of the project. On Windows the binary may
Trent Nelsonb27745f2008-03-19 06:28:24 +000026# live in project/PCBuild9. If we're dealing with an x64 Windows build,
27# it'll live in project/PCbuild/amd64.
Christian Heimesd3fc07a42007-12-06 13:15:13 +000028project_base = os.path.dirname(os.path.abspath(sys.executable))
29if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
30 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
Christian Heimes9a1d8ce2008-01-01 14:37:32 +000031# PC/VS7.1
32if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
33 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
34 os.path.pardir))
Trent Nelsonb27745f2008-03-19 06:28:24 +000035# PC/AMD64
36if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
37 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
38 os.path.pardir))
Christian Heimesd3fc07a42007-12-06 13:15:13 +000039
Fred Drake16c8d702002-06-04 15:28:21 +000040# python_build: (Boolean) if true, we're either building Python or
41# building an extension with an un-installed Python, so we use
42# different (hard-wired) directories.
Christian Heimesc67a15d2007-12-14 23:42:36 +000043# Setup.local is available for Makefile builds including VPATH builds,
44# Setup.dist is available on Windows
Marc-André Lemburg2db7cd32008-02-05 14:50:40 +000045def _python_build():
46 for fn in ("Setup.dist", "Setup.local"):
47 if os.path.isfile(os.path.join(project_base, "Modules", fn)):
48 return True
49 return False
50python_build = _python_build()
Fred Drakec1ee39a2000-03-09 15:54:52 +000051
Fred Drakec916cdc2001-08-02 20:03:12 +000052
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000053def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000054 """Return a string containing the major and minor Python version,
55 leaving off the patchlevel. Sample return values could be '1.5'
56 or '2.2'.
57 """
58 return sys.version[:3]
59
60
Greg Wardd38e6f72000-04-10 01:17:49 +000061def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000062 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000063
64 If 'plat_specific' is false (the default), this is the path to the
65 non-platform-specific header files, i.e. Python.h and so on;
66 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000067 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000068
Greg Wardd38e6f72000-04-10 01:17:49 +000069 If 'prefix' is supplied, use it instead of sys.prefix or
70 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000071 """
Greg Wardd38e6f72000-04-10 01:17:49 +000072 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000073 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000074 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000075 if python_build:
Neil Schemenauer444df452009-02-05 16:14:39 +000076 # Assume the executable is in the build directory. The
77 # pyconfig.h file should be in the same directory. Since
78 # the build directory may not be the source directory, we
79 # must use "srcdir" from the makefile to find the "Include"
80 # directory.
Fred Drake16c8d702002-06-04 15:28:21 +000081 base = os.path.dirname(os.path.abspath(sys.executable))
82 if plat_specific:
Neil Schemenauer444df452009-02-05 16:14:39 +000083 return base
Fred Drake16c8d702002-06-04 15:28:21 +000084 else:
Neil Schemenauer444df452009-02-05 16:14:39 +000085 incdir = os.path.join(get_config_var('srcdir'), 'Include')
86 return os.path.normpath(incdir)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000087 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000088 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000089 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000090 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000091 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000092 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000093 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000094 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000095 elif os.name == "os2":
96 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000097 else:
Fred Drake70b014d2001-07-18 18:39:56 +000098 raise DistutilsPlatformError(
99 "I don't know where Python installs its C header files "
100 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000101
102
Greg Wardd38e6f72000-04-10 01:17:49 +0000103def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +0000104 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000105 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000106
Fred Drakec1ee39a2000-03-09 15:54:52 +0000107 If 'plat_specific' is true, return the directory containing
108 platform-specific modules, i.e. any module from a non-pure-Python
109 module distribution; otherwise, return the platform-shared library
110 directory. If 'standard_lib' is true, return the directory
111 containing standard Python library modules; otherwise, return the
112 directory for site-specific modules.
113
Greg Wardd38e6f72000-04-10 01:17:49 +0000114 If 'prefix' is supplied, use it instead of sys.prefix or
115 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000116 """
Greg Wardd38e6f72000-04-10 01:17:49 +0000117 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000118 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000119
Greg Ward7d73b9e2000-03-09 03:16:05 +0000120 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000121 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000122 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000123 if standard_lib:
124 return libpython
125 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000126 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000127
128 elif os.name == "nt":
129 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000130 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000131 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000132 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000133 return prefix
134 else:
Tarek Ziadé74fbf602009-02-10 12:31:09 +0000135 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000136
137 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000138 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000139 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000140 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000141 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000142 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000143 else:
144 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000145 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000146 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000147 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000148
149 elif os.name == "os2":
150 if standard_lib:
Tarek Ziadé74fbf602009-02-10 12:31:09 +0000151 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000152 else:
Tarek Ziadé74fbf602009-02-10 12:31:09 +0000153 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000154
Greg Ward7d73b9e2000-03-09 03:16:05 +0000155 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000156 raise DistutilsPlatformError(
157 "I don't know where Python installs its library "
158 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000159
Greg Ward7d73b9e2000-03-09 03:16:05 +0000160
Fred Drake70b014d2001-07-18 18:39:56 +0000161def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000162 """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.
Greg Wardbb7baa72000-06-25 02:09:14 +0000166 """
167 if compiler.compiler_type == "unix":
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000168 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000169 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000170 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
Greg Wardbb7baa72000-06-25 02:09:14 +0000171
Guido van Rossum8bc09652008-02-21 18:18:37 +0000172 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000173 cc = os.environ['CC']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000174 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000175 cxx = os.environ['CXX']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000176 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000177 ldshared = os.environ['LDSHARED']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000178 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000179 cpp = os.environ['CPP']
180 else:
181 cpp = cc + " -E" # not always
Guido van Rossum8bc09652008-02-21 18:18:37 +0000182 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000183 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000184 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000185 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000186 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000187 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000188 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000189 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000190 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
Tarek Ziadé05adf072009-02-06 01:15:51 +0000191 if 'AR' in os.environ:
192 ar = os.environ['AR']
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000193 if 'ARFLAGS' in os.environ:
194 archiver = ar + ' ' + os.environ['ARFLAGS']
195 else:
196 archiver = ar + ' ' + ar_flags
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000197
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000198 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000199 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000200 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000201 compiler=cc_cmd,
202 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000203 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000204 linker_so=ldshared,
Tarek Ziadé05adf072009-02-06 01:15:51 +0000205 linker_exe=cc,
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000206 archiver=archiver)
Greg Ward879f0f12000-09-15 01:15:08 +0000207
208 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000209
210
Greg Ward9ddaaa11999-01-06 14:46:06 +0000211def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000212 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000213 if python_build:
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000214 if os.name == "nt":
215 inc_dir = os.path.join(project_base, "PC")
216 else:
217 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000218 else:
219 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000220 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000221 config_h = 'config.h'
222 else:
223 # The name of the config.h file changed in 2.2
224 config_h = 'pyconfig.h'
225 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000226
Greg Ward1190ee31998-12-18 23:46:33 +0000227
Greg Ward9ddaaa11999-01-06 14:46:06 +0000228def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000229 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000230 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000231 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000232 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
233 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000234
Greg Ward1190ee31998-12-18 23:46:33 +0000235
Greg Ward9ddaaa11999-01-06 14:46:06 +0000236def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000237 """Parse a config.h-style file.
238
239 A dictionary containing name/value pairs is returned. If an
240 optional dictionary is passed in as the second argument, it is
241 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000242 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000243 if g is None:
244 g = {}
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000245 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
246 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000247 #
Greg Ward1190ee31998-12-18 23:46:33 +0000248 while 1:
249 line = fp.readline()
250 if not line:
251 break
252 m = define_rx.match(line)
253 if m:
254 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000255 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000256 except ValueError: pass
257 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000258 else:
259 m = undef_rx.match(line)
260 if m:
261 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000262 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000263
Greg Wardd283ce72000-09-17 00:53:02 +0000264
265# Regexes needed for parsing Makefile (and similar syntaxes,
266# like old-style Setup files).
267_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
268_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
269_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
270
Greg Ward3fff8d22000-09-15 00:03:13 +0000271def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000272 """Parse a Makefile-style file.
273
274 A dictionary containing name/value pairs is returned. If an
275 optional dictionary is passed in as the second argument, it is
276 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000277 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000278 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000279 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000280
Greg Ward9ddaaa11999-01-06 14:46:06 +0000281 if g is None:
282 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000283 done = {}
284 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000285
Greg Ward1190ee31998-12-18 23:46:33 +0000286 while 1:
287 line = fp.readline()
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000288 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000289 break
Greg Wardd283ce72000-09-17 00:53:02 +0000290 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000291 if m:
292 n, v = m.group(1, 2)
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000293 v = v.strip()
294 # `$$' is a literal `$' in make
295 tmpv = v.replace('$$', '')
296
297 if "$" in tmpv:
Greg Ward1190ee31998-12-18 23:46:33 +0000298 notdone[n] = v
299 else:
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000300 try:
301 v = int(v)
302 except ValueError:
303 # insert literal `$'
304 done[n] = v.replace('$$', '$')
305 else:
306 done[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000307
308 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000309 while notdone:
310 for name in notdone.keys():
311 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000312 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000313 if m:
314 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000315 found = True
Guido van Rossum8bc09652008-02-21 18:18:37 +0000316 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000317 item = str(done[n])
Guido van Rossum8bc09652008-02-21 18:18:37 +0000318 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000319 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000320 found = False
Guido van Rossum8bc09652008-02-21 18:18:37 +0000321 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000322 # do it like make: fall back to environment
323 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000324 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000325 done[n] = item = ""
326 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000327 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000328 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000329 if "$" in after:
330 notdone[name] = value
331 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000332 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000333 except ValueError:
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000334 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000335 else:
336 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000337 del notdone[name]
338 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000339 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000340 del notdone[name]
341
Greg Wardd283ce72000-09-17 00:53:02 +0000342 fp.close()
343
Greg Ward1190ee31998-12-18 23:46:33 +0000344 # save the results in the global dictionary
345 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000346 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000347
348
Greg Wardd283ce72000-09-17 00:53:02 +0000349def expand_makefile_vars(s, vars):
350 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
351 'string' according to 'vars' (a dictionary mapping variable names to
352 values). Variables not present in 'vars' are silently expanded to the
353 empty string. The variable values in 'vars' should not contain further
354 variable expansions; if 'vars' is the output of 'parse_makefile()',
355 you're fine. Returns a variable-expanded version of 's'.
356 """
357
358 # This algorithm does multiple expansion, so if vars['foo'] contains
359 # "${bar}", it will expand ${foo} to ${bar}, and then expand
360 # ${bar}... and so forth. This is fine as long as 'vars' comes from
361 # 'parse_makefile()', which takes care of such expansions eagerly,
362 # according to make's variable expansion semantics.
363
364 while 1:
365 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
366 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000367 (beg, end) = m.span()
368 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
369 else:
370 break
371 return s
372
373
Greg Ward879f0f12000-09-15 01:15:08 +0000374_config_vars = None
375
Greg Ward9ddaaa11999-01-06 14:46:06 +0000376def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000377 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000378 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000379 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000380 try:
381 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000382 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000383 except IOError, msg:
384 my_msg = "invalid Python installation: unable to open %s" % filename
385 if hasattr(msg, "strerror"):
386 my_msg = my_msg + " (%s)" % msg.strerror
387
Fred Drake70b014d2001-07-18 18:39:56 +0000388 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000389
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000390 # load the installed pyconfig.h:
391 try:
392 filename = get_config_h_filename()
393 parse_config_h(file(filename), g)
394 except IOError, msg:
395 my_msg = "invalid Python installation: unable to open %s" % filename
396 if hasattr(msg, "strerror"):
397 my_msg = my_msg + " (%s)" % msg.strerror
398
399 raise DistutilsPlatformError(my_msg)
400
Jack Jansen6b08a402004-06-03 12:41:45 +0000401 # On MacOSX we need to check the setting of the environment variable
402 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
403 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000404 # If it isn't set we set it to the configure-time value
Guido van Rossum8bc09652008-02-21 18:18:37 +0000405 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Ronald Oussoren988117f2006-04-29 11:31:35 +0000406 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000407 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000408 if cur_target == '':
409 cur_target = cfg_target
410 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Ronald Oussoren59075eb2006-04-17 14:43:30 +0000411 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000412 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
413 % (cur_target, cfg_target))
414 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000415
Greg Ward4f880282000-06-27 01:59:06 +0000416 # On AIX, there are wrong paths to the linker scripts in the Makefile
417 # -- these paths are relative to the Python source, but when installed
418 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000419 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000420 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000421
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000422 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000423 # The following two branches are for 1.5.2 compatibility.
424 if sys.platform == 'aix4': # what about AIX 3.x ?
425 # Linker script is in the config directory, not in Modules as the
426 # Makefile says.
427 python_lib = get_python_lib(standard_lib=1)
428 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
429 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000430
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000431 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
432
433 elif sys.platform == 'beos':
434 # Linker script is in the config directory. In the Makefile it is
435 # relative to the srcdir, which after installation no longer makes
436 # sense.
437 python_lib = get_python_lib(standard_lib=1)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000438 linkerscript_path = string.split(g['LDSHARED'])[0]
439 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000440 linkerscript = os.path.join(python_lib, 'config',
441 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000442
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000443 # XXX this isn't the right place to do this: adding the Python
444 # library to the link, if needed, should be in the "build_ext"
445 # command. (It's also needed for non-MS compilers on Windows, and
446 # it's taken care of for them by the 'build_ext.get_libraries()'
447 # method.)
448 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000449 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000450
Greg Ward879f0f12000-09-15 01:15:08 +0000451 global _config_vars
452 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000453
Greg Ward9ddaaa11999-01-06 14:46:06 +0000454
Greg Ward4d74d731999-06-08 01:58:36 +0000455def _init_nt():
456 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000457 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000458 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000459 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
460 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000461
Greg Ward32162e81999-08-29 18:22:13 +0000462 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000463 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000464
Fred Drake69e2c6e2000-02-08 15:55:42 +0000465 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000466 g['EXE'] = ".exe"
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000467 g['VERSION'] = get_python_version().replace(".", "")
468 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000469
470 global _config_vars
471 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000472
Fred Drake69e2c6e2000-02-08 15:55:42 +0000473
Greg Ward0eff87a2000-03-07 03:30:09 +0000474def _init_mac():
475 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000476 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000477 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000478 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
479 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000480
481 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000482 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000483
Jack Jansendd13a202001-05-17 12:52:01 +0000484 import MacOS
485 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000486 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000487 else:
488 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000489
490 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000491 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
492 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000493
Jack Jansenab5320b2002-06-26 15:42:49 +0000494 # These are used by the extension module build
495 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000496 global _config_vars
497 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000498
Fred Drake69e2c6e2000-02-08 15:55:42 +0000499
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000500def _init_os2():
501 """Initialize the module as appropriate for OS/2"""
502 g = {}
503 # set basic install directories
504 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
505 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
506
507 # XXX hmmm.. a normal install puts include files here
508 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
509
510 g['SO'] = '.pyd'
511 g['EXE'] = ".exe"
512
513 global _config_vars
514 _config_vars = g
515
516
Greg Ward879f0f12000-09-15 01:15:08 +0000517def get_config_vars(*args):
518 """With no arguments, return a dictionary of all configuration
519 variables relevant for the current platform. Generally this includes
520 everything needed to build extensions and install both pure modules and
521 extensions. On Unix, this means every variable defined in Python's
522 installed Makefile; on Windows and Mac OS it's a much smaller set.
523
524 With arguments, return a list of values that result from looking up
525 each argument in the configuration variable dictionary.
526 """
527 global _config_vars
528 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000529 func = globals().get("_init_" + os.name)
530 if func:
531 func()
532 else:
533 _config_vars = {}
534
535 # Normalized versions of prefix and exec_prefix are handy to have;
536 # in fact, these are the standard versions used most places in the
537 # Distutils.
538 _config_vars['prefix'] = PREFIX
539 _config_vars['exec_prefix'] = EXEC_PREFIX
540
Neil Schemenauer444df452009-02-05 16:14:39 +0000541 if 'srcdir' not in _config_vars:
542 _config_vars['srcdir'] = project_base
543
Neil Schemenaueraa397d12009-02-06 21:33:45 +0000544 # Convert srcdir into an absolute path if it appears necessary.
545 # Normally it is relative to the build directory. However, during
546 # testing, for example, we might be running a non-installed python
547 # from a different directory.
548 if python_build and os.name == "posix":
549 base = os.path.dirname(os.path.abspath(sys.executable))
550 if (not os.path.isabs(_config_vars['srcdir']) and
551 base != os.getcwd()):
552 # srcdir is relative and we are not in the same directory
553 # as the executable. Assume executable is in the build
554 # directory and make srcdir absolute.
555 srcdir = os.path.join(base, _config_vars['srcdir'])
556 _config_vars['srcdir'] = os.path.normpath(srcdir)
557
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000558 if sys.platform == 'darwin':
559 kernel_version = os.uname()[2] # Kernel version (8.4.3)
560 major_version = int(kernel_version.split('.')[0])
561
562 if major_version < 8:
563 # On Mac OS X before 10.4, check if -arch and -isysroot
564 # are in CFLAGS or LDFLAGS and remove them if they are.
565 # This is needed when building extensions on a 10.3 system
566 # using a universal build of python.
Ronald Oussorend6103692006-10-08 17:49:52 +0000567 for key in ('LDFLAGS', 'BASECFLAGS',
568 # a number of derived variables. These need to be
569 # patched up as well.
570 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000571 flags = _config_vars[key]
572 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Ronald Oussoren7b9053a2006-06-27 10:08:25 +0000573 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000574 _config_vars[key] = flags
575
Ronald Oussoren5640ce22008-06-05 12:58:24 +0000576 else:
577
578 # Allow the user to override the architecture flags using
579 # an environment variable.
580 # NOTE: This name was introduced by Apple in OSX 10.5 and
581 # is used by several scripting languages distributed with
582 # that OS release.
583
584 if 'ARCHFLAGS' in os.environ:
585 arch = os.environ['ARCHFLAGS']
586 for key in ('LDFLAGS', 'BASECFLAGS',
587 # a number of derived variables. These need to be
588 # patched up as well.
589 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
590
591 flags = _config_vars[key]
592 flags = re.sub('-arch\s+\w+\s', ' ', flags)
593 flags = flags + ' ' + arch
594 _config_vars[key] = flags
595
Greg Ward879f0f12000-09-15 01:15:08 +0000596 if args:
597 vals = []
598 for name in args:
599 vals.append(_config_vars.get(name))
600 return vals
601 else:
602 return _config_vars
603
604def get_config_var(name):
605 """Return the value of a single variable using the dictionary
606 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000607 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000608 """
609 return get_config_vars().get(name)