blob: 46d23ecc781df5e3e350836b7110bc87810e3d41 [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.
Mark Dickinson6f09ea82010-08-03 21:18:06 +000028project_base = os.path.dirname(os.path.realpath(sys.executable))
Christian Heimesd3fc07a42007-12-06 13:15:13 +000029if 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
Tarek Ziadé68ca24b2010-04-30 12:18:51 +000074
Greg Ward7d73b9e2000-03-09 03:16:05 +000075 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000076 if python_build:
Mark Dickinson6f09ea82010-08-03 21:18:06 +000077 buildir = os.path.dirname(os.path.realpath(sys.executable))
Fred Drake16c8d702002-06-04 15:28:21 +000078 if plat_specific:
Tarek Ziadé68ca24b2010-04-30 12:18:51 +000079 # python.h is located in the buildir
80 inc_dir = buildir
Fred Drake16c8d702002-06-04 15:28:21 +000081 else:
Tarek Ziadé68ca24b2010-04-30 12:18:51 +000082 # the source dir is relative to the buildir
83 srcdir = os.path.abspath(os.path.join(buildir,
84 get_config_var('srcdir')))
85 # Include is located in the srcdir
86 inc_dir = os.path.join(srcdir, "Include")
Fred Drake16c8d702002-06-04 15:28:21 +000087 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000088 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000089 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000090 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000091 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000092 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000093 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000094 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000095 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000096 elif os.name == "os2":
97 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000098 else:
Fred Drake70b014d2001-07-18 18:39:56 +000099 raise DistutilsPlatformError(
100 "I don't know where Python installs its C header files "
101 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000102
103
Greg Wardd38e6f72000-04-10 01:17:49 +0000104def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +0000105 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000106 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000107
Fred Drakec1ee39a2000-03-09 15:54:52 +0000108 If 'plat_specific' is true, return the directory containing
109 platform-specific modules, i.e. any module from a non-pure-Python
110 module distribution; otherwise, return the platform-shared library
111 directory. If 'standard_lib' is true, return the directory
112 containing standard Python library modules; otherwise, return the
113 directory for site-specific modules.
114
Greg Wardd38e6f72000-04-10 01:17:49 +0000115 If 'prefix' is supplied, use it instead of sys.prefix or
116 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000117 """
Greg Wardd38e6f72000-04-10 01:17:49 +0000118 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000119 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000120
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000122 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000123 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000124 if standard_lib:
125 return libpython
126 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000127 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000128
129 elif os.name == "nt":
130 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000131 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000132 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000133 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000134 return prefix
135 else:
Tarek Ziadé436eb082009-02-10 12:33:42 +0000136 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000137
138 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000139 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000140 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000141 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000142 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000143 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000144 else:
145 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000146 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000147 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000148 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000149
150 elif os.name == "os2":
151 if standard_lib:
Tarek Ziadé436eb082009-02-10 12:33:42 +0000152 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000153 else:
Tarek Ziadé436eb082009-02-10 12:33:42 +0000154 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000155
Greg Ward7d73b9e2000-03-09 03:16:05 +0000156 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000157 raise DistutilsPlatformError(
158 "I don't know where Python installs its library "
159 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000160
Greg Ward7d73b9e2000-03-09 03:16:05 +0000161
Fred Drake70b014d2001-07-18 18:39:56 +0000162def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000163 """Do any platform-specific customization of a CCompiler instance.
164
165 Mainly needed on Unix, so we can plug in the information that
166 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000167 """
168 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000169 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000170 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000171 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000172
Guido van Rossum8bc09652008-02-21 18:18:37 +0000173 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000174 cc = os.environ['CC']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000175 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000176 cxx = os.environ['CXX']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000177 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000178 ldshared = os.environ['LDSHARED']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000179 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000180 cpp = os.environ['CPP']
181 else:
182 cpp = cc + " -E" # not always
Guido van Rossum8bc09652008-02-21 18:18:37 +0000183 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000184 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000185 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000186 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000187 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossum8bc09652008-02-21 18:18:37 +0000188 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000189 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000190 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000191 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
192
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000193 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000194 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000195 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000196 compiler=cc_cmd,
197 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000198 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000199 linker_so=ldshared,
200 linker_exe=cc)
201
202 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000203
204
Greg Ward9ddaaa11999-01-06 14:46:06 +0000205def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000206 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000207 if python_build:
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000208 if os.name == "nt":
209 inc_dir = os.path.join(project_base, "PC")
210 else:
211 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000212 else:
213 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000214 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000215 config_h = 'config.h'
216 else:
217 # The name of the config.h file changed in 2.2
218 config_h = 'pyconfig.h'
219 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000220
Greg Ward1190ee31998-12-18 23:46:33 +0000221
Greg Ward9ddaaa11999-01-06 14:46:06 +0000222def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000223 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000224 if python_build:
Mark Dickinson6f09ea82010-08-03 21:18:06 +0000225 return os.path.join(os.path.dirname(os.path.realpath(sys.executable)),
226 "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000227 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
228 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000229
Greg Ward1190ee31998-12-18 23:46:33 +0000230
Greg Ward9ddaaa11999-01-06 14:46:06 +0000231def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000232 """Parse a config.h-style file.
233
234 A dictionary containing name/value pairs is returned. If an
235 optional dictionary is passed in as the second argument, it is
236 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000237 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000238 if g is None:
239 g = {}
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000240 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
241 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000242 #
Greg Ward1190ee31998-12-18 23:46:33 +0000243 while 1:
244 line = fp.readline()
245 if not line:
246 break
247 m = define_rx.match(line)
248 if m:
249 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000250 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000251 except ValueError: pass
252 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000253 else:
254 m = undef_rx.match(line)
255 if m:
256 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000257 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000258
Greg Wardd283ce72000-09-17 00:53:02 +0000259
260# Regexes needed for parsing Makefile (and similar syntaxes,
261# like old-style Setup files).
262_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
263_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
264_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
265
Greg Ward3fff8d22000-09-15 00:03:13 +0000266def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000267 """Parse a Makefile-style file.
268
269 A dictionary containing name/value pairs is returned. If an
270 optional dictionary is passed in as the second argument, it is
271 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000272 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000273 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000274 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000275
Greg Ward9ddaaa11999-01-06 14:46:06 +0000276 if g is None:
277 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000278 done = {}
279 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000280
Greg Ward1190ee31998-12-18 23:46:33 +0000281 while 1:
282 line = fp.readline()
Tarek Ziadé6b16c002009-06-11 08:26:40 +0000283 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000284 break
Greg Wardd283ce72000-09-17 00:53:02 +0000285 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000286 if m:
287 n, v = m.group(1, 2)
Tarek Ziadé6b16c002009-06-11 08:26:40 +0000288 v = v.strip()
289 # `$$' is a literal `$' in make
290 tmpv = v.replace('$$', '')
291
292 if "$" in tmpv:
Greg Ward1190ee31998-12-18 23:46:33 +0000293 notdone[n] = v
294 else:
Tarek Ziadé6b16c002009-06-11 08:26:40 +0000295 try:
296 v = int(v)
297 except ValueError:
298 # insert literal `$'
299 done[n] = v.replace('$$', '$')
300 else:
301 done[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000302
303 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000304 while notdone:
305 for name in notdone.keys():
306 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000307 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000308 if m:
309 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000310 found = True
Guido van Rossum8bc09652008-02-21 18:18:37 +0000311 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000312 item = str(done[n])
Guido van Rossum8bc09652008-02-21 18:18:37 +0000313 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000314 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000315 found = False
Guido van Rossum8bc09652008-02-21 18:18:37 +0000316 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000317 # do it like make: fall back to environment
318 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000319 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000320 done[n] = item = ""
321 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000322 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000323 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000324 if "$" in after:
325 notdone[name] = value
326 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000327 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000328 except ValueError:
Tarek Ziadé6b16c002009-06-11 08:26:40 +0000329 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000330 else:
331 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000332 del notdone[name]
333 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000334 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000335 del notdone[name]
336
Greg Wardd283ce72000-09-17 00:53:02 +0000337 fp.close()
338
Greg Ward1190ee31998-12-18 23:46:33 +0000339 # save the results in the global dictionary
340 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000341 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000342
343
Greg Wardd283ce72000-09-17 00:53:02 +0000344def expand_makefile_vars(s, vars):
345 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
346 'string' according to 'vars' (a dictionary mapping variable names to
347 values). Variables not present in 'vars' are silently expanded to the
348 empty string. The variable values in 'vars' should not contain further
349 variable expansions; if 'vars' is the output of 'parse_makefile()',
350 you're fine. Returns a variable-expanded version of 's'.
351 """
352
353 # This algorithm does multiple expansion, so if vars['foo'] contains
354 # "${bar}", it will expand ${foo} to ${bar}, and then expand
355 # ${bar}... and so forth. This is fine as long as 'vars' comes from
356 # 'parse_makefile()', which takes care of such expansions eagerly,
357 # according to make's variable expansion semantics.
358
359 while 1:
360 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
361 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000362 (beg, end) = m.span()
363 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
364 else:
365 break
366 return s
367
368
Greg Ward879f0f12000-09-15 01:15:08 +0000369_config_vars = None
370
Greg Ward9ddaaa11999-01-06 14:46:06 +0000371def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000372 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000373 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000374 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000375 try:
376 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000377 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000378 except IOError, msg:
379 my_msg = "invalid Python installation: unable to open %s" % filename
380 if hasattr(msg, "strerror"):
381 my_msg = my_msg + " (%s)" % msg.strerror
382
Fred Drake70b014d2001-07-18 18:39:56 +0000383 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000384
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000385 # load the installed pyconfig.h:
386 try:
387 filename = get_config_h_filename()
388 parse_config_h(file(filename), g)
389 except IOError, msg:
390 my_msg = "invalid Python installation: unable to open %s" % filename
391 if hasattr(msg, "strerror"):
392 my_msg = my_msg + " (%s)" % msg.strerror
393
394 raise DistutilsPlatformError(my_msg)
395
Jack Jansen6b08a402004-06-03 12:41:45 +0000396 # On MacOSX we need to check the setting of the environment variable
397 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
398 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000399 # If it isn't set we set it to the configure-time value
Guido van Rossum8bc09652008-02-21 18:18:37 +0000400 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Ronald Oussoren988117f2006-04-29 11:31:35 +0000401 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000402 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000403 if cur_target == '':
404 cur_target = cfg_target
405 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Ronald Oussoren59075eb2006-04-17 14:43:30 +0000406 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000407 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
408 % (cur_target, cfg_target))
409 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000410
Greg Ward4f880282000-06-27 01:59:06 +0000411 # On AIX, there are wrong paths to the linker scripts in the Makefile
412 # -- these paths are relative to the Python source, but when installed
413 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000414 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000415 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000416
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000417 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000418 # The following two branches are for 1.5.2 compatibility.
419 if sys.platform == 'aix4': # what about AIX 3.x ?
420 # Linker script is in the config directory, not in Modules as the
421 # Makefile says.
422 python_lib = get_python_lib(standard_lib=1)
423 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
424 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000425
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000426 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
427
428 elif sys.platform == 'beos':
429 # Linker script is in the config directory. In the Makefile it is
430 # relative to the srcdir, which after installation no longer makes
431 # sense.
432 python_lib = get_python_lib(standard_lib=1)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000433 linkerscript_path = string.split(g['LDSHARED'])[0]
434 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000435 linkerscript = os.path.join(python_lib, 'config',
436 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000437
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000438 # XXX this isn't the right place to do this: adding the Python
439 # library to the link, if needed, should be in the "build_ext"
440 # command. (It's also needed for non-MS compilers on Windows, and
441 # it's taken care of for them by the 'build_ext.get_libraries()'
442 # method.)
443 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000444 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000445
Greg Ward879f0f12000-09-15 01:15:08 +0000446 global _config_vars
447 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000448
Greg Ward9ddaaa11999-01-06 14:46:06 +0000449
Greg Ward4d74d731999-06-08 01:58:36 +0000450def _init_nt():
451 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000452 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000453 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000454 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
455 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000456
Greg Ward32162e81999-08-29 18:22:13 +0000457 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000458 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000459
Fred Drake69e2c6e2000-02-08 15:55:42 +0000460 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000461 g['EXE'] = ".exe"
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000462 g['VERSION'] = get_python_version().replace(".", "")
Mark Dickinson6f09ea82010-08-03 21:18:06 +0000463 g['BINDIR'] = os.path.dirname(os.path.realpath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000464
465 global _config_vars
466 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000467
Fred Drake69e2c6e2000-02-08 15:55:42 +0000468
Greg Ward0eff87a2000-03-07 03:30:09 +0000469def _init_mac():
470 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000471 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000472 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000473 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
474 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000475
476 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000477 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000478
Jack Jansendd13a202001-05-17 12:52:01 +0000479 import MacOS
480 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000481 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000482 else:
483 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000484
485 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000486 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
487 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000488
Jack Jansenab5320b2002-06-26 15:42:49 +0000489 # These are used by the extension module build
490 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000491 global _config_vars
492 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000493
Fred Drake69e2c6e2000-02-08 15:55:42 +0000494
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000495def _init_os2():
496 """Initialize the module as appropriate for OS/2"""
497 g = {}
498 # set basic install directories
499 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
500 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
501
502 # XXX hmmm.. a normal install puts include files here
503 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
504
505 g['SO'] = '.pyd'
506 g['EXE'] = ".exe"
507
508 global _config_vars
509 _config_vars = g
510
511
Greg Ward879f0f12000-09-15 01:15:08 +0000512def get_config_vars(*args):
513 """With no arguments, return a dictionary of all configuration
514 variables relevant for the current platform. Generally this includes
515 everything needed to build extensions and install both pure modules and
516 extensions. On Unix, this means every variable defined in Python's
517 installed Makefile; on Windows and Mac OS it's a much smaller set.
518
519 With arguments, return a list of values that result from looking up
520 each argument in the configuration variable dictionary.
521 """
522 global _config_vars
523 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000524 func = globals().get("_init_" + os.name)
525 if func:
526 func()
527 else:
528 _config_vars = {}
529
530 # Normalized versions of prefix and exec_prefix are handy to have;
531 # in fact, these are the standard versions used most places in the
532 # Distutils.
533 _config_vars['prefix'] = PREFIX
534 _config_vars['exec_prefix'] = EXEC_PREFIX
535
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000536 if sys.platform == 'darwin':
537 kernel_version = os.uname()[2] # Kernel version (8.4.3)
538 major_version = int(kernel_version.split('.')[0])
539
540 if major_version < 8:
541 # On Mac OS X before 10.4, check if -arch and -isysroot
542 # are in CFLAGS or LDFLAGS and remove them if they are.
543 # This is needed when building extensions on a 10.3 system
544 # using a universal build of python.
Ronald Oussoren61f6a1b2009-12-10 10:29:05 +0000545 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
Ronald Oussorend6103692006-10-08 17:49:52 +0000546 # a number of derived variables. These need to be
547 # patched up as well.
548 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000549 flags = _config_vars[key]
550 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Ronald Oussoren7b9053a2006-06-27 10:08:25 +0000551 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000552 _config_vars[key] = flags
553
Ronald Oussoren5640ce22008-06-05 12:58:24 +0000554 else:
555
556 # Allow the user to override the architecture flags using
557 # an environment variable.
558 # NOTE: This name was introduced by Apple in OSX 10.5 and
559 # is used by several scripting languages distributed with
560 # that OS release.
561
562 if 'ARCHFLAGS' in os.environ:
563 arch = os.environ['ARCHFLAGS']
Ronald Oussoren61f6a1b2009-12-10 10:29:05 +0000564 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
Ronald Oussoren5640ce22008-06-05 12:58:24 +0000565 # a number of derived variables. These need to be
566 # patched up as well.
567 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
568
569 flags = _config_vars[key]
570 flags = re.sub('-arch\s+\w+\s', ' ', flags)
571 flags = flags + ' ' + arch
572 _config_vars[key] = flags
573
Ronald Oussorend787abf2009-09-22 19:31:34 +0000574 # If we're on OSX 10.5 or later and the user tries to
575 # compiles an extension using an SDK that is not present
576 # on the current machine it is better to not use an SDK
577 # than to fail.
578 #
579 # The major usecase for this is users using a Python.org
580 # binary installer on OSX 10.6: that installer uses
581 # the 10.4u SDK, but that SDK is not installed by default
582 # when you install Xcode.
583 #
584 m = re.search('-isysroot\s+(\S+)', _config_vars['CFLAGS'])
585 if m is not None:
586 sdk = m.group(1)
587 if not os.path.exists(sdk):
Ronald Oussoren61f6a1b2009-12-10 10:29:05 +0000588 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
Ronald Oussorend787abf2009-09-22 19:31:34 +0000589 # a number of derived variables. These need to be
590 # patched up as well.
591 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
592
593 flags = _config_vars[key]
594 flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags)
595 _config_vars[key] = flags
596
Greg Ward879f0f12000-09-15 01:15:08 +0000597 if args:
598 vals = []
599 for name in args:
600 vals.append(_config_vars.get(name))
601 return vals
602 else:
603 return _config_vars
604
605def get_config_var(name):
606 """Return the value of a single variable using the dictionary
607 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000608 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000609 """
610 return get_config_vars().get(name)