blob: dcc7231ac5a4f3cd1edc8294c677d751637c7f00 [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
Greg Ward9ddaaa11999-01-06 14:46:06 +000016import sys
Greg Ward1190ee31998-12-18 23:46:33 +000017
Guido van Rossumf8480a72006-03-15 23:08:13 +000018from distutils.errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000019
Greg Ward879f0f12000-09-15 01:15:08 +000020# These are needed in a couple of spots, so just compute them once.
Greg Wardcf6bea32000-04-10 01:15:06 +000021PREFIX = os.path.normpath(sys.prefix)
22EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000023
Christian Heimesd3fc07a42007-12-06 13:15:13 +000024# Path to the base directory of the project. On Windows the binary may
Trent Nelsonb27745f2008-03-19 06:28:24 +000025# live in project/PCBuild9. If we're dealing with an x64 Windows build,
26# it'll live in project/PCbuild/amd64.
Christian Heimesd3fc07a42007-12-06 13:15:13 +000027project_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))
Christian Heimes9a1d8ce2008-01-01 14:37:32 +000030# 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))
Trent Nelsonb27745f2008-03-19 06:28:24 +000034# 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))
Christian Heimesd3fc07a42007-12-06 13:15:13 +000038
Fred Drake16c8d702002-06-04 15:28:21 +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.
Christian Heimesc67a15d2007-12-14 23:42:36 +000042# Setup.local is available for Makefile builds including VPATH builds,
43# Setup.dist is available on Windows
Marc-André Lemburg2db7cd32008-02-05 14:50:40 +000044def _python_build():
45 for fn in ("Setup.dist", "Setup.local"):
46 if os.path.isfile(os.path.join(project_base, "Modules", fn)):
47 return True
48 return False
49python_build = _python_build()
Fred Drakec1ee39a2000-03-09 15:54:52 +000050
Fred Drakec916cdc2001-08-02 20:03:12 +000051
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000052def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000053 """Return a string containing the major and minor Python version,
54 leaving off the patchlevel. Sample return values could be '1.5'
55 or '2.2'.
56 """
57 return sys.version[:3]
58
59
Greg Wardd38e6f72000-04-10 01:17:49 +000060def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000061 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000062
63 If 'plat_specific' is false (the default), this is the path to the
64 non-platform-specific header files, i.e. Python.h and so on;
65 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000066 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000067
Greg Wardd38e6f72000-04-10 01:17:49 +000068 If 'prefix' is supplied, use it instead of sys.prefix or
69 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000070 """
Greg Wardd38e6f72000-04-10 01:17:49 +000071 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000072 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000073 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000074 if python_build:
Neil Schemenauer444df452009-02-05 16:14:39 +000075 # Assume the executable is in the build directory. The
76 # pyconfig.h file should be in the same directory. Since
77 # the build directory may not be the source directory, we
78 # must use "srcdir" from the makefile to find the "Include"
79 # directory.
Fred Drake16c8d702002-06-04 15:28:21 +000080 base = os.path.dirname(os.path.abspath(sys.executable))
81 if plat_specific:
Neil Schemenauer444df452009-02-05 16:14:39 +000082 return base
Fred Drake16c8d702002-06-04 15:28:21 +000083 else:
Neil Schemenauer444df452009-02-05 16:14:39 +000084 incdir = os.path.join(get_config_var('srcdir'), 'Include')
85 return os.path.normpath(incdir)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000086 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000087 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000088 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000089 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000090 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000091 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000092 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000093 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000094 elif os.name == "os2":
95 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000096 else:
Fred Drake70b014d2001-07-18 18:39:56 +000097 raise DistutilsPlatformError(
98 "I don't know where Python installs its C header files "
99 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000100
101
Greg Wardd38e6f72000-04-10 01:17:49 +0000102def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +0000103 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000104 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000105
Fred Drakec1ee39a2000-03-09 15:54:52 +0000106 If 'plat_specific' is true, return the directory containing
107 platform-specific modules, i.e. any module from a non-pure-Python
108 module distribution; otherwise, return the platform-shared library
109 directory. If 'standard_lib' is true, return the directory
110 containing standard Python library modules; otherwise, return the
111 directory for site-specific modules.
112
Greg Wardd38e6f72000-04-10 01:17:49 +0000113 If 'prefix' is supplied, use it instead of sys.prefix or
114 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000115 """
Greg Wardd38e6f72000-04-10 01:17:49 +0000116 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000117 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000118
Greg Ward7d73b9e2000-03-09 03:16:05 +0000119 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000120 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000121 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000122 if standard_lib:
123 return libpython
124 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000125 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000126
127 elif os.name == "nt":
128 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000129 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000130 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000131 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000132 return prefix
133 else:
Tarek Ziadé74fbf602009-02-10 12:31:09 +0000134 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000135
136 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000137 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000138 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000139 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000140 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000141 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000142 else:
143 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000144 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000145 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000146 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000147
148 elif os.name == "os2":
149 if standard_lib:
Tarek Ziadé74fbf602009-02-10 12:31:09 +0000150 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000151 else:
Tarek Ziadé74fbf602009-02-10 12:31:09 +0000152 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000153
Greg Ward7d73b9e2000-03-09 03:16:05 +0000154 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000155 raise DistutilsPlatformError(
156 "I don't know where Python installs its library "
157 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000158
Greg Ward7d73b9e2000-03-09 03:16:05 +0000159
Fred Drake70b014d2001-07-18 18:39:56 +0000160def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000161 """Do any platform-specific customization of a CCompiler instance.
162
163 Mainly needed on Unix, so we can plug in the information that
164 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000165 """
166 if compiler.compiler_type == "unix":
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000167 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000168 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000169 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
Greg Wardbb7baa72000-06-25 02:09:14 +0000170
Guido van Rossum8bc09652008-02-21 18:18:37 +0000171 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000172 cc = os.environ['CC']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000173 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000174 cxx = os.environ['CXX']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000175 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000176 ldshared = os.environ['LDSHARED']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000177 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000178 cpp = os.environ['CPP']
179 else:
180 cpp = cc + " -E" # not always
Guido van Rossum8bc09652008-02-21 18:18:37 +0000181 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000182 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000183 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000184 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000185 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000186 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000187 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000188 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000189 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
Tarek Ziadé05adf072009-02-06 01:15:51 +0000190 if 'AR' in os.environ:
191 ar = os.environ['AR']
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000192 if 'ARFLAGS' in os.environ:
193 archiver = ar + ' ' + os.environ['ARFLAGS']
194 else:
195 archiver = ar + ' ' + ar_flags
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000196
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000197 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000198 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000199 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000200 compiler=cc_cmd,
201 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000202 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000203 linker_so=ldshared,
Tarek Ziadé05adf072009-02-06 01:15:51 +0000204 linker_exe=cc,
Tarek Ziadé99f660a2009-05-07 21:20:34 +0000205 archiver=archiver)
Greg Ward879f0f12000-09-15 01:15:08 +0000206
207 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000208
209
Greg Ward9ddaaa11999-01-06 14:46:06 +0000210def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000211 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000212 if python_build:
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000213 if os.name == "nt":
214 inc_dir = os.path.join(project_base, "PC")
215 else:
216 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000217 else:
218 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000219 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000220 config_h = 'config.h'
221 else:
222 # The name of the config.h file changed in 2.2
223 config_h = 'pyconfig.h'
224 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000225
Greg Ward1190ee31998-12-18 23:46:33 +0000226
Greg Ward9ddaaa11999-01-06 14:46:06 +0000227def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000228 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000229 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000230 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000231 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
232 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000233
Greg Ward1190ee31998-12-18 23:46:33 +0000234
Greg Ward9ddaaa11999-01-06 14:46:06 +0000235def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000236 """Parse a config.h-style file.
237
238 A dictionary containing name/value pairs is returned. If an
239 optional dictionary is passed in as the second argument, it is
240 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000241 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000242 if g is None:
243 g = {}
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000244 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
245 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000246 #
Greg Ward1190ee31998-12-18 23:46:33 +0000247 while 1:
248 line = fp.readline()
249 if not line:
250 break
251 m = define_rx.match(line)
252 if m:
253 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000254 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000255 except ValueError: pass
256 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000257 else:
258 m = undef_rx.match(line)
259 if m:
260 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000261 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000262
Greg Wardd283ce72000-09-17 00:53:02 +0000263
264# Regexes needed for parsing Makefile (and similar syntaxes,
265# like old-style Setup files).
266_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
267_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
268_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
269
Greg Ward3fff8d22000-09-15 00:03:13 +0000270def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000271 """Parse a Makefile-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 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000277 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000278 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000279
Greg Ward9ddaaa11999-01-06 14:46:06 +0000280 if g is None:
281 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000282 done = {}
283 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000284
Greg Ward1190ee31998-12-18 23:46:33 +0000285 while 1:
286 line = fp.readline()
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000287 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000288 break
Greg Wardd283ce72000-09-17 00:53:02 +0000289 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000290 if m:
291 n, v = m.group(1, 2)
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000292 v = v.strip()
293 # `$$' is a literal `$' in make
294 tmpv = v.replace('$$', '')
295
296 if "$" in tmpv:
Greg Ward1190ee31998-12-18 23:46:33 +0000297 notdone[n] = v
298 else:
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000299 try:
300 v = int(v)
301 except ValueError:
302 # insert literal `$'
303 done[n] = v.replace('$$', '$')
304 else:
305 done[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000306
307 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000308 while notdone:
309 for name in notdone.keys():
310 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000311 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000312 if m:
313 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000314 found = True
Guido van Rossum8bc09652008-02-21 18:18:37 +0000315 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000316 item = str(done[n])
Guido van Rossum8bc09652008-02-21 18:18:37 +0000317 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000318 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000319 found = False
Guido van Rossum8bc09652008-02-21 18:18:37 +0000320 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000321 # do it like make: fall back to environment
322 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000323 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000324 done[n] = item = ""
325 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000326 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000327 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000328 if "$" in after:
329 notdone[name] = value
330 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000331 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000332 except ValueError:
Tarek Ziadé25d2bae2009-06-11 08:12:20 +0000333 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000334 else:
335 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000336 del notdone[name]
337 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000338 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000339 del notdone[name]
340
Greg Wardd283ce72000-09-17 00:53:02 +0000341 fp.close()
342
Greg Ward1190ee31998-12-18 23:46:33 +0000343 # save the results in the global dictionary
344 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000345 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000346
347
Greg Wardd283ce72000-09-17 00:53:02 +0000348def expand_makefile_vars(s, vars):
349 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
350 'string' according to 'vars' (a dictionary mapping variable names to
351 values). Variables not present in 'vars' are silently expanded to the
352 empty string. The variable values in 'vars' should not contain further
353 variable expansions; if 'vars' is the output of 'parse_makefile()',
354 you're fine. Returns a variable-expanded version of 's'.
355 """
356
357 # This algorithm does multiple expansion, so if vars['foo'] contains
358 # "${bar}", it will expand ${foo} to ${bar}, and then expand
359 # ${bar}... and so forth. This is fine as long as 'vars' comes from
360 # 'parse_makefile()', which takes care of such expansions eagerly,
361 # according to make's variable expansion semantics.
362
363 while 1:
364 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
365 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000366 (beg, end) = m.span()
367 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
368 else:
369 break
370 return s
371
372
Greg Ward879f0f12000-09-15 01:15:08 +0000373_config_vars = None
374
Greg Ward9ddaaa11999-01-06 14:46:06 +0000375def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000376 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000377 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000378 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000379 try:
380 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000381 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000382 except IOError, msg:
383 my_msg = "invalid Python installation: unable to open %s" % filename
384 if hasattr(msg, "strerror"):
385 my_msg = my_msg + " (%s)" % msg.strerror
386
Fred Drake70b014d2001-07-18 18:39:56 +0000387 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000388
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000389 # load the installed pyconfig.h:
390 try:
391 filename = get_config_h_filename()
392 parse_config_h(file(filename), g)
393 except IOError, msg:
394 my_msg = "invalid Python installation: unable to open %s" % filename
395 if hasattr(msg, "strerror"):
396 my_msg = my_msg + " (%s)" % msg.strerror
397
398 raise DistutilsPlatformError(my_msg)
399
Jack Jansen6b08a402004-06-03 12:41:45 +0000400 # On MacOSX we need to check the setting of the environment variable
401 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
402 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000403 # If it isn't set we set it to the configure-time value
Guido van Rossum8bc09652008-02-21 18:18:37 +0000404 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Ronald Oussoren988117f2006-04-29 11:31:35 +0000405 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000406 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000407 if cur_target == '':
408 cur_target = cfg_target
409 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Ronald Oussoren59075eb2006-04-17 14:43:30 +0000410 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000411 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
412 % (cur_target, cfg_target))
413 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000414
Greg Ward4f880282000-06-27 01:59:06 +0000415 # On AIX, there are wrong paths to the linker scripts in the Makefile
416 # -- these paths are relative to the Python source, but when installed
417 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000418 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000419 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000420
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000421 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000422 # The following two branches are for 1.5.2 compatibility.
423 if sys.platform == 'aix4': # what about AIX 3.x ?
424 # Linker script is in the config directory, not in Modules as the
425 # Makefile says.
426 python_lib = get_python_lib(standard_lib=1)
427 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
428 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000429
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000430 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
431
432 elif sys.platform == 'beos':
433 # Linker script is in the config directory. In the Makefile it is
434 # relative to the srcdir, which after installation no longer makes
435 # sense.
436 python_lib = get_python_lib(standard_lib=1)
Tarek Ziadé2d36afd2009-06-11 08:43:26 +0000437 linkerscript_path = g['LDSHARED'].split()[0]
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000438 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000439 linkerscript = os.path.join(python_lib, 'config',
440 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000441
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000442 # XXX this isn't the right place to do this: adding the Python
443 # library to the link, if needed, should be in the "build_ext"
444 # command. (It's also needed for non-MS compilers on Windows, and
445 # it's taken care of for them by the 'build_ext.get_libraries()'
446 # method.)
447 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000448 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000449
Greg Ward879f0f12000-09-15 01:15:08 +0000450 global _config_vars
451 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000452
Greg Ward9ddaaa11999-01-06 14:46:06 +0000453
Greg Ward4d74d731999-06-08 01:58:36 +0000454def _init_nt():
455 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000456 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000457 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000458 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
459 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000460
Greg Ward32162e81999-08-29 18:22:13 +0000461 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000462 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000463
Fred Drake69e2c6e2000-02-08 15:55:42 +0000464 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000465 g['EXE'] = ".exe"
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000466 g['VERSION'] = get_python_version().replace(".", "")
467 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000468
469 global _config_vars
470 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000471
Fred Drake69e2c6e2000-02-08 15:55:42 +0000472
Greg Ward0eff87a2000-03-07 03:30:09 +0000473def _init_mac():
474 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000475 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000476 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000477 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
478 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000479
480 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000481 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000482
Jack Jansendd13a202001-05-17 12:52:01 +0000483 import MacOS
484 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000485 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000486 else:
487 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000488
489 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000490 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
491 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000492
Jack Jansenab5320b2002-06-26 15:42:49 +0000493 # These are used by the extension module build
494 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000495 global _config_vars
496 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000497
Fred Drake69e2c6e2000-02-08 15:55:42 +0000498
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000499def _init_os2():
500 """Initialize the module as appropriate for OS/2"""
501 g = {}
502 # set basic install directories
503 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
504 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
505
506 # XXX hmmm.. a normal install puts include files here
507 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
508
509 g['SO'] = '.pyd'
510 g['EXE'] = ".exe"
511
512 global _config_vars
513 _config_vars = g
514
515
Greg Ward879f0f12000-09-15 01:15:08 +0000516def get_config_vars(*args):
517 """With no arguments, return a dictionary of all configuration
518 variables relevant for the current platform. Generally this includes
519 everything needed to build extensions and install both pure modules and
520 extensions. On Unix, this means every variable defined in Python's
521 installed Makefile; on Windows and Mac OS it's a much smaller set.
522
523 With arguments, return a list of values that result from looking up
524 each argument in the configuration variable dictionary.
525 """
526 global _config_vars
527 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000528 func = globals().get("_init_" + os.name)
529 if func:
530 func()
531 else:
532 _config_vars = {}
533
534 # Normalized versions of prefix and exec_prefix are handy to have;
535 # in fact, these are the standard versions used most places in the
536 # Distutils.
537 _config_vars['prefix'] = PREFIX
538 _config_vars['exec_prefix'] = EXEC_PREFIX
539
Neil Schemenauer444df452009-02-05 16:14:39 +0000540 if 'srcdir' not in _config_vars:
541 _config_vars['srcdir'] = project_base
542
Neil Schemenaueraa397d12009-02-06 21:33:45 +0000543 # Convert srcdir into an absolute path if it appears necessary.
544 # Normally it is relative to the build directory. However, during
545 # testing, for example, we might be running a non-installed python
546 # from a different directory.
547 if python_build and os.name == "posix":
548 base = os.path.dirname(os.path.abspath(sys.executable))
549 if (not os.path.isabs(_config_vars['srcdir']) and
550 base != os.getcwd()):
551 # srcdir is relative and we are not in the same directory
552 # as the executable. Assume executable is in the build
553 # directory and make srcdir absolute.
554 srcdir = os.path.join(base, _config_vars['srcdir'])
555 _config_vars['srcdir'] = os.path.normpath(srcdir)
556
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000557 if sys.platform == 'darwin':
558 kernel_version = os.uname()[2] # Kernel version (8.4.3)
559 major_version = int(kernel_version.split('.')[0])
560
561 if major_version < 8:
562 # On Mac OS X before 10.4, check if -arch and -isysroot
563 # are in CFLAGS or LDFLAGS and remove them if they are.
564 # This is needed when building extensions on a 10.3 system
565 # using a universal build of python.
Ronald Oussorend6103692006-10-08 17:49:52 +0000566 for key in ('LDFLAGS', 'BASECFLAGS',
567 # a number of derived variables. These need to be
568 # patched up as well.
569 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000570 flags = _config_vars[key]
571 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Ronald Oussoren7b9053a2006-06-27 10:08:25 +0000572 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000573 _config_vars[key] = flags
574
Ronald Oussoren5640ce22008-06-05 12:58:24 +0000575 else:
576
577 # Allow the user to override the architecture flags using
578 # an environment variable.
579 # NOTE: This name was introduced by Apple in OSX 10.5 and
580 # is used by several scripting languages distributed with
581 # that OS release.
582
583 if 'ARCHFLAGS' in os.environ:
584 arch = os.environ['ARCHFLAGS']
585 for key in ('LDFLAGS', 'BASECFLAGS',
586 # a number of derived variables. These need to be
587 # patched up as well.
588 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
589
590 flags = _config_vars[key]
591 flags = re.sub('-arch\s+\w+\s', ' ', flags)
592 flags = flags + ' ' + arch
593 _config_vars[key] = flags
594
Greg Ward879f0f12000-09-15 01:15:08 +0000595 if args:
596 vals = []
597 for name in args:
598 vals.append(_config_vars.get(name))
599 return vals
600 else:
601 return _config_vars
602
603def get_config_var(name):
604 """Return the value of a single variable using the dictionary
605 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000606 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000607 """
608 return get_config_vars().get(name)