blob: 9842d26c470a024962c3490ecdf776b47822844a [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
Guido van Rossum63236cf2007-05-25 18:39:29 +000014import io
Greg Ward9ddaaa11999-01-06 14:46:06 +000015import os
16import re
Greg Ward9ddaaa11999-01-06 14:46:06 +000017import sys
Greg Ward1190ee31998-12-18 23:46:33 +000018
Guido van Rossum45aecf42006-03-15 04:58:47 +000019from .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 Heimes255f53b2007-12-08 15:33:56 +000025# Path to the base directory of the project. On Windows the binary may
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000026# live in project/PCBuild9. If we're dealing with an x64 Windows build,
27# it'll live in project/PCbuild/amd64.
Mark Dickinson1f0f2782010-08-03 21:33:04 +000028project_base = os.path.dirname(os.path.realpath(sys.executable))
Christian Heimes255f53b2007-12-08 15:33:56 +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 Heimesd9a4d1d2008-01-01 14:42:15 +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))
Christian Heimesd5e2b6f2008-03-19 21:50:51 +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 Heimes255f53b2007-12-08 15:33:56 +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 Heimes0449f632007-12-15 01:27:15 +000043# Setup.local is available for Makefile builds including VPATH builds,
44# Setup.dist is available on Windows
Christian Heimes2202f872008-02-06 14:31:34 +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 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 Schemenauer47dc7512009-02-05 16:33:41 +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.
Mark Dickinson1f0f2782010-08-03 21:33:04 +000080 base = os.path.dirname(os.path.realpath(sys.executable))
Fred Drake16c8d702002-06-04 15:28:21 +000081 if plat_specific:
Neil Schemenauer47dc7512009-02-05 16:33:41 +000082 return base
Fred Drake16c8d702002-06-04 15:28:21 +000083 else:
Neil Schemenauer47dc7512009-02-05 16:33:41 +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 elif os.name == "nt":
127 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000128 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000129 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000130 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000131 return prefix
132 else:
Tarek Ziadé7d5e9872009-02-10 12:36:33 +0000133 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000134 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000135 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000136 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000137 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000138 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000139 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000140 else:
141 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000142 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000143 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000144 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000145 elif os.name == "os2":
146 if standard_lib:
Tarek Ziadé7d5e9872009-02-10 12:36:33 +0000147 return os.path.join(prefix, "Lib")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000148 else:
Tarek Ziadé7d5e9872009-02-10 12:36:33 +0000149 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000150 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000151 raise DistutilsPlatformError(
152 "I don't know where Python installs its library "
153 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000154
Greg Ward7d73b9e2000-03-09 03:16:05 +0000155
Fred Drake70b014d2001-07-18 18:39:56 +0000156def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000157 """Do any platform-specific customization of a CCompiler instance.
158
159 Mainly needed on Unix, so we can plug in the information that
160 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000161 """
162 if compiler.compiler_type == "unix":
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000163 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000164 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000165 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS')
Greg Wardbb7baa72000-06-25 02:09:14 +0000166
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000167 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000168 cc = os.environ['CC']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000169 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000170 cxx = os.environ['CXX']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000171 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000172 ldshared = os.environ['LDSHARED']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000173 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000174 cpp = os.environ['CPP']
175 else:
176 cpp = cc + " -E" # not always
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000177 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000178 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000179 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000180 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000181 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000182 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000183 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000184 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000185 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
Tarek Ziadéd3409de2009-02-06 01:18:36 +0000186 if 'AR' in os.environ:
187 ar = os.environ['AR']
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000188 if 'ARFLAGS' in os.environ:
189 archiver = ar + ' ' + os.environ['ARFLAGS']
190 else:
191 archiver = ar + ' ' + ar_flags
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000192
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,
Tarek Ziadéd3409de2009-02-06 01:18:36 +0000200 linker_exe=cc,
Tarek Ziadé5662d3e2009-05-07 21:24:43 +0000201 archiver=archiver)
Greg Ward879f0f12000-09-15 01:15:08 +0000202
203 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000204
205
Greg Ward9ddaaa11999-01-06 14:46:06 +0000206def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000207 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000208 if python_build:
Christian Heimes255f53b2007-12-08 15:33:56 +0000209 if os.name == "nt":
210 inc_dir = os.path.join(project_base, "PC")
211 else:
212 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000213 else:
214 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000215 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000216 config_h = 'config.h'
217 else:
218 # The name of the config.h file changed in 2.2
219 config_h = 'pyconfig.h'
220 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000221
Greg Ward1190ee31998-12-18 23:46:33 +0000222
Greg Ward9ddaaa11999-01-06 14:46:06 +0000223def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000224 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000225 if python_build:
Mark Dickinson1f0f2782010-08-03 21:33:04 +0000226 return os.path.join(os.path.dirname(os.path.realpath(sys.executable)),
227 "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000228 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
229 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000230
Greg Ward1190ee31998-12-18 23:46:33 +0000231
Greg Ward9ddaaa11999-01-06 14:46:06 +0000232def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000233 """Parse a config.h-style file.
234
235 A dictionary containing name/value pairs is returned. If an
236 optional dictionary is passed in as the second argument, it is
237 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000238 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000239 if g is None:
240 g = {}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
242 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000243 #
Collin Winter5b7e9d72007-08-30 03:52:21 +0000244 while True:
Greg Ward1190ee31998-12-18 23:46:33 +0000245 line = fp.readline()
246 if not line:
247 break
248 m = define_rx.match(line)
249 if m:
250 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000251 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000252 except ValueError: pass
253 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000254 else:
255 m = undef_rx.match(line)
256 if m:
257 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000258 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000259
Greg Wardd283ce72000-09-17 00:53:02 +0000260
261# Regexes needed for parsing Makefile (and similar syntaxes,
262# like old-style Setup files).
263_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
264_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
265_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
266
Greg Ward3fff8d22000-09-15 00:03:13 +0000267def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000268 """Parse a Makefile-style file.
269
270 A dictionary containing name/value pairs is returned. If an
271 optional dictionary is passed in as the second argument, it is
272 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000273 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000274 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000275 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000276
Greg Ward9ddaaa11999-01-06 14:46:06 +0000277 if g is None:
278 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000279 done = {}
280 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000281
Collin Winter5b7e9d72007-08-30 03:52:21 +0000282 while True:
Greg Ward1190ee31998-12-18 23:46:33 +0000283 line = fp.readline()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000284 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000285 break
Greg Wardd283ce72000-09-17 00:53:02 +0000286 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000287 if m:
288 n, v = m.group(1, 2)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000289 v = v.strip()
Tarek Ziadéabcc3f42009-06-11 08:31:17 +0000290 # `$$' is a literal `$' in make
291 tmpv = v.replace('$$', '')
292
293 if "$" in tmpv:
Greg Ward1190ee31998-12-18 23:46:33 +0000294 notdone[n] = v
295 else:
Tarek Ziadéabcc3f42009-06-11 08:31:17 +0000296 try:
297 v = int(v)
298 except ValueError:
299 # insert literal `$'
300 done[n] = v.replace('$$', '$')
301 else:
302 done[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000303
304 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000305 while notdone:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000306 for name in list(notdone):
Greg Ward1190ee31998-12-18 23:46:33 +0000307 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000308 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000309 if m:
310 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000311 found = True
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000312 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000313 item = str(done[n])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000314 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000315 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000316 found = False
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000317 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000318 # do it like make: fall back to environment
319 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000320 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000321 done[n] = item = ""
322 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000323 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000324 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000325 if "$" in after:
326 notdone[name] = value
327 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000328 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000329 except ValueError:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000330 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000331 else:
332 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000333 del notdone[name]
334 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000335 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000336 del notdone[name]
337
Greg Wardd283ce72000-09-17 00:53:02 +0000338 fp.close()
339
Greg Ward1190ee31998-12-18 23:46:33 +0000340 # save the results in the global dictionary
341 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000342 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000343
344
Greg Wardd283ce72000-09-17 00:53:02 +0000345def expand_makefile_vars(s, vars):
346 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
347 'string' according to 'vars' (a dictionary mapping variable names to
348 values). Variables not present in 'vars' are silently expanded to the
349 empty string. The variable values in 'vars' should not contain further
350 variable expansions; if 'vars' is the output of 'parse_makefile()',
351 you're fine. Returns a variable-expanded version of 's'.
352 """
353
354 # This algorithm does multiple expansion, so if vars['foo'] contains
355 # "${bar}", it will expand ${foo} to ${bar}, and then expand
356 # ${bar}... and so forth. This is fine as long as 'vars' comes from
357 # 'parse_makefile()', which takes care of such expansions eagerly,
358 # according to make's variable expansion semantics.
359
Collin Winter5b7e9d72007-08-30 03:52:21 +0000360 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000361 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
362 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000363 (beg, end) = m.span()
364 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
365 else:
366 break
367 return s
368
369
Greg Ward879f0f12000-09-15 01:15:08 +0000370_config_vars = None
371
Greg Ward9ddaaa11999-01-06 14:46:06 +0000372def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000373 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000374 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000375 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000376 try:
377 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000378 parse_makefile(filename, g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000379 except IOError as msg:
Greg Warda570c052000-05-23 23:14:00 +0000380 my_msg = "invalid Python installation: unable to open %s" % filename
381 if hasattr(msg, "strerror"):
382 my_msg = my_msg + " (%s)" % msg.strerror
383
Fred Drake70b014d2001-07-18 18:39:56 +0000384 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 # load the installed pyconfig.h:
387 try:
388 filename = get_config_h_filename()
Guido van Rossum63236cf2007-05-25 18:39:29 +0000389 parse_config_h(io.open(filename), g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000390 except IOError as msg:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391 my_msg = "invalid Python installation: unable to open %s" % filename
392 if hasattr(msg, "strerror"):
393 my_msg = my_msg + " (%s)" % msg.strerror
394
395 raise DistutilsPlatformError(my_msg)
396
Jack Jansen6b08a402004-06-03 12:41:45 +0000397 # On MacOSX we need to check the setting of the environment variable
398 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
399 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000400 # If it isn't set we set it to the configure-time value
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000401 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000402 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000403 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000404 if cur_target == '':
405 cur_target = cfg_target
406 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000407 elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
Jack Jansen6b08a402004-06-03 12:41:45 +0000408 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
409 % (cur_target, cfg_target))
410 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000411
Greg Ward4f880282000-06-27 01:59:06 +0000412 # On AIX, there are wrong paths to the linker scripts in the Makefile
413 # -- these paths are relative to the Python source, but when installed
414 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000415 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000416 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000417
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000418 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000419 # The following two branches are for 1.5.2 compatibility.
420 if sys.platform == 'aix4': # what about AIX 3.x ?
421 # Linker script is in the config directory, not in Modules as the
422 # Makefile says.
423 python_lib = get_python_lib(standard_lib=1)
424 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
425 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000426
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000427 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
428
Greg Ward879f0f12000-09-15 01:15:08 +0000429 global _config_vars
430 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000431
Greg Ward9ddaaa11999-01-06 14:46:06 +0000432
Greg Ward4d74d731999-06-08 01:58:36 +0000433def _init_nt():
434 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000435 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000436 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000437 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
438 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000439
Greg Ward32162e81999-08-29 18:22:13 +0000440 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000441 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000442
Fred Drake69e2c6e2000-02-08 15:55:42 +0000443 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000444 g['EXE'] = ".exe"
Christian Heimes255f53b2007-12-08 15:33:56 +0000445 g['VERSION'] = get_python_version().replace(".", "")
Mark Dickinson1f0f2782010-08-03 21:33:04 +0000446 g['BINDIR'] = os.path.dirname(os.path.realpath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000447
448 global _config_vars
449 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000450
Fred Drake69e2c6e2000-02-08 15:55:42 +0000451
Greg Ward0eff87a2000-03-07 03:30:09 +0000452def _init_mac():
453 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000454 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000455 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000456 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
457 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000458
459 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000460 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000461
Jack Jansendd13a202001-05-17 12:52:01 +0000462 import MacOS
463 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000464 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000465 else:
466 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000467
468 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000469 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
470 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000471
Jack Jansenab5320b2002-06-26 15:42:49 +0000472 # These are used by the extension module build
473 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000474 global _config_vars
475 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000476
Fred Drake69e2c6e2000-02-08 15:55:42 +0000477
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000478def _init_os2():
479 """Initialize the module as appropriate for OS/2"""
480 g = {}
481 # set basic install directories
482 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
483 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
484
485 # XXX hmmm.. a normal install puts include files here
486 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
487
488 g['SO'] = '.pyd'
489 g['EXE'] = ".exe"
490
491 global _config_vars
492 _config_vars = g
493
494
Greg Ward879f0f12000-09-15 01:15:08 +0000495def get_config_vars(*args):
496 """With no arguments, return a dictionary of all configuration
497 variables relevant for the current platform. Generally this includes
498 everything needed to build extensions and install both pure modules and
499 extensions. On Unix, this means every variable defined in Python's
500 installed Makefile; on Windows and Mac OS it's a much smaller set.
501
502 With arguments, return a list of values that result from looking up
503 each argument in the configuration variable dictionary.
504 """
505 global _config_vars
506 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000507 func = globals().get("_init_" + os.name)
508 if func:
509 func()
510 else:
511 _config_vars = {}
512
513 # Normalized versions of prefix and exec_prefix are handy to have;
514 # in fact, these are the standard versions used most places in the
515 # Distutils.
516 _config_vars['prefix'] = PREFIX
517 _config_vars['exec_prefix'] = EXEC_PREFIX
518
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000519 # Convert srcdir into an absolute path if it appears necessary.
520 # Normally it is relative to the build directory. However, during
521 # testing, for example, we might be running a non-installed python
522 # from a different directory.
523 if python_build and os.name == "posix":
524 base = os.path.dirname(os.path.abspath(sys.executable))
525 if (not os.path.isabs(_config_vars['srcdir']) and
526 base != os.getcwd()):
527 # srcdir is relative and we are not in the same directory
528 # as the executable. Assume executable is in the build
529 # directory and make srcdir absolute.
530 srcdir = os.path.join(base, _config_vars['srcdir'])
531 _config_vars['srcdir'] = os.path.normpath(srcdir)
532
Thomas Wouters477c8d52006-05-27 19:21:47 +0000533 if sys.platform == 'darwin':
534 kernel_version = os.uname()[2] # Kernel version (8.4.3)
535 major_version = int(kernel_version.split('.')[0])
536
537 if major_version < 8:
538 # On Mac OS X before 10.4, check if -arch and -isysroot
539 # are in CFLAGS or LDFLAGS and remove them if they are.
540 # This is needed when building extensions on a 10.3 system
541 # using a universal build of python.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000542 for key in ('LDFLAGS', 'BASECFLAGS',
543 # a number of derived variables. These need to be
544 # patched up as well.
545 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000546 flags = _config_vars[key]
Antoine Pitroufd036452008-08-19 17:56:33 +0000547 flags = re.sub('-arch\s+\w+\s', ' ', flags, re.ASCII)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000548 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000549 _config_vars[key] = flags
550
Georg Brandlfcaf9102008-07-16 02:17:56 +0000551 else:
552
553 # Allow the user to override the architecture flags using
554 # an environment variable.
555 # NOTE: This name was introduced by Apple in OSX 10.5 and
556 # is used by several scripting languages distributed with
557 # that OS release.
558
559 if 'ARCHFLAGS' in os.environ:
560 arch = os.environ['ARCHFLAGS']
561 for key in ('LDFLAGS', 'BASECFLAGS',
562 # a number of derived variables. These need to be
563 # patched up as well.
564 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
565
566 flags = _config_vars[key]
567 flags = re.sub('-arch\s+\w+\s', ' ', flags)
568 flags = flags + ' ' + arch
569 _config_vars[key] = flags
570
Greg Ward879f0f12000-09-15 01:15:08 +0000571 if args:
572 vals = []
573 for name in args:
574 vals.append(_config_vars.get(name))
575 return vals
576 else:
577 return _config_vars
578
579def get_config_var(name):
580 """Return the value of a single variable using the dictionary
581 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000582 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000583 """
584 return get_config_vars().get(name)