blob: 39216a532fc71625cbf8715600cc9b4c27df057d [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
26# live in project/PCBuild9
27project_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 Heimesd9a4d1d2008-01-01 14:42:15 +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))
Christian Heimes255f53b2007-12-08 15:33:56 +000034
Fred Drake16c8d702002-06-04 15:28:21 +000035# python_build: (Boolean) if true, we're either building Python or
36# building an extension with an un-installed Python, so we use
37# different (hard-wired) directories.
Christian Heimes0449f632007-12-15 01:27:15 +000038# Setup.local is available for Makefile builds including VPATH builds,
39# Setup.dist is available on Windows
Christian Heimes2202f872008-02-06 14:31:34 +000040def _python_build():
41 for fn in ("Setup.dist", "Setup.local"):
42 if os.path.isfile(os.path.join(project_base, "Modules", fn)):
43 return True
44 return False
45python_build = _python_build()
Fred Drakec916cdc2001-08-02 20:03:12 +000046
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000047def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000048 """Return a string containing the major and minor Python version,
49 leaving off the patchlevel. Sample return values could be '1.5'
50 or '2.2'.
51 """
52 return sys.version[:3]
53
54
Greg Wardd38e6f72000-04-10 01:17:49 +000055def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000056 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000057
58 If 'plat_specific' is false (the default), this is the path to the
59 non-platform-specific header files, i.e. Python.h and so on;
60 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000061 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000062
Greg Wardd38e6f72000-04-10 01:17:49 +000063 If 'prefix' is supplied, use it instead of sys.prefix or
64 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000065 """
Greg Wardd38e6f72000-04-10 01:17:49 +000066 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000067 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000068 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000069 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +000070 base = os.path.dirname(os.path.abspath(sys.executable))
71 if plat_specific:
72 inc_dir = base
73 else:
74 inc_dir = os.path.join(base, "Include")
75 if not os.path.exists(inc_dir):
76 inc_dir = os.path.join(os.path.dirname(base), "Include")
77 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000078 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000079 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000080 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000081 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000082 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000083 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000084 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000085 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000086 elif os.name == "os2":
87 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000088 else:
Fred Drake70b014d2001-07-18 18:39:56 +000089 raise DistutilsPlatformError(
90 "I don't know where Python installs its C header files "
91 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000092
93
Greg Wardd38e6f72000-04-10 01:17:49 +000094def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000095 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000096 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000097
Fred Drakec1ee39a2000-03-09 15:54:52 +000098 If 'plat_specific' is true, return the directory containing
99 platform-specific modules, i.e. any module from a non-pure-Python
100 module distribution; otherwise, return the platform-shared library
101 directory. If 'standard_lib' is true, return the directory
102 containing standard Python library modules; otherwise, return the
103 directory for site-specific modules.
104
Greg Wardd38e6f72000-04-10 01:17:49 +0000105 If 'prefix' is supplied, use it instead of sys.prefix or
106 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000107 """
Greg Wardd38e6f72000-04-10 01:17:49 +0000108 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000109 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000110
Greg Ward7d73b9e2000-03-09 03:16:05 +0000111 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000112 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000113 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000114 if standard_lib:
115 return libpython
116 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000117 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000118 elif os.name == "nt":
119 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000120 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000122 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000123 return prefix
124 else:
125 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000126 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000127 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000128 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000129 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000130 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000131 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000132 else:
133 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000134 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000135 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000136 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000137 elif os.name == "os2":
138 if standard_lib:
139 return os.path.join(PREFIX, "Lib")
140 else:
141 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000142 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000143 raise DistutilsPlatformError(
144 "I don't know where Python installs its library "
145 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000146
Greg Ward7d73b9e2000-03-09 03:16:05 +0000147
Fred Drake70b014d2001-07-18 18:39:56 +0000148def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000149 """Do any platform-specific customization of a CCompiler instance.
150
151 Mainly needed on Unix, so we can plug in the information that
152 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000153 """
154 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000155 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000156 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000157 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000158
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000159 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000160 cc = os.environ['CC']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000161 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000162 cxx = os.environ['CXX']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000163 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000164 ldshared = os.environ['LDSHARED']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000165 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000166 cpp = os.environ['CPP']
167 else:
168 cpp = cc + " -E" # not always
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000169 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000170 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000171 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000172 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000173 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000174 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000175 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000176 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000177 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
178
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000179 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000180 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000181 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000182 compiler=cc_cmd,
183 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000184 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000185 linker_so=ldshared,
186 linker_exe=cc)
187
188 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000189
190
Greg Ward9ddaaa11999-01-06 14:46:06 +0000191def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000192 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000193 if python_build:
Christian Heimes255f53b2007-12-08 15:33:56 +0000194 if os.name == "nt":
195 inc_dir = os.path.join(project_base, "PC")
196 else:
197 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000198 else:
199 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000200 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000201 config_h = 'config.h'
202 else:
203 # The name of the config.h file changed in 2.2
204 config_h = 'pyconfig.h'
205 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000206
Greg Ward1190ee31998-12-18 23:46:33 +0000207
Greg Ward9ddaaa11999-01-06 14:46:06 +0000208def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000209 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000210 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000211 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000212 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
213 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000214
Greg Ward1190ee31998-12-18 23:46:33 +0000215
Greg Ward9ddaaa11999-01-06 14:46:06 +0000216def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000217 """Parse a config.h-style file.
218
219 A dictionary containing name/value pairs is returned. If an
220 optional dictionary is passed in as the second argument, it is
221 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000222 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000223 if g is None:
224 g = {}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
226 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000227 #
Collin Winter5b7e9d72007-08-30 03:52:21 +0000228 while True:
Greg Ward1190ee31998-12-18 23:46:33 +0000229 line = fp.readline()
230 if not line:
231 break
232 m = define_rx.match(line)
233 if m:
234 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000235 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000236 except ValueError: pass
237 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000238 else:
239 m = undef_rx.match(line)
240 if m:
241 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000242 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000243
Greg Wardd283ce72000-09-17 00:53:02 +0000244
245# Regexes needed for parsing Makefile (and similar syntaxes,
246# like old-style Setup files).
247_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
248_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
249_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
250
Greg Ward3fff8d22000-09-15 00:03:13 +0000251def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000252 """Parse a Makefile-style file.
253
254 A dictionary containing name/value pairs is returned. If an
255 optional dictionary is passed in as the second argument, it is
256 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000257 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000258 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000259 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000260
Greg Ward9ddaaa11999-01-06 14:46:06 +0000261 if g is None:
262 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000263 done = {}
264 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000265
Collin Winter5b7e9d72007-08-30 03:52:21 +0000266 while True:
Greg Ward1190ee31998-12-18 23:46:33 +0000267 line = fp.readline()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000268 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000269 break
Greg Wardd283ce72000-09-17 00:53:02 +0000270 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000271 if m:
272 n, v = m.group(1, 2)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000273 v = v.strip()
Greg Ward1190ee31998-12-18 23:46:33 +0000274 if "$" in v:
275 notdone[n] = v
276 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000277 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000278 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000279 done[n] = v
280
281 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000282 while notdone:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000283 for name in list(notdone):
Greg Ward1190ee31998-12-18 23:46:33 +0000284 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000285 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000286 if m:
287 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000288 found = True
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000289 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000290 item = str(done[n])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000291 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000292 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000293 found = False
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000294 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000295 # do it like make: fall back to environment
296 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000297 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000298 done[n] = item = ""
299 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000300 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000301 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000302 if "$" in after:
303 notdone[name] = value
304 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000305 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000306 except ValueError:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000307 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000308 else:
309 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000310 del notdone[name]
311 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000312 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000313 del notdone[name]
314
Greg Wardd283ce72000-09-17 00:53:02 +0000315 fp.close()
316
Greg Ward1190ee31998-12-18 23:46:33 +0000317 # save the results in the global dictionary
318 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000319 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000320
321
Greg Wardd283ce72000-09-17 00:53:02 +0000322def expand_makefile_vars(s, vars):
323 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
324 'string' according to 'vars' (a dictionary mapping variable names to
325 values). Variables not present in 'vars' are silently expanded to the
326 empty string. The variable values in 'vars' should not contain further
327 variable expansions; if 'vars' is the output of 'parse_makefile()',
328 you're fine. Returns a variable-expanded version of 's'.
329 """
330
331 # This algorithm does multiple expansion, so if vars['foo'] contains
332 # "${bar}", it will expand ${foo} to ${bar}, and then expand
333 # ${bar}... and so forth. This is fine as long as 'vars' comes from
334 # 'parse_makefile()', which takes care of such expansions eagerly,
335 # according to make's variable expansion semantics.
336
Collin Winter5b7e9d72007-08-30 03:52:21 +0000337 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000338 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
339 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000340 (beg, end) = m.span()
341 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
342 else:
343 break
344 return s
345
346
Greg Ward879f0f12000-09-15 01:15:08 +0000347_config_vars = None
348
Greg Ward9ddaaa11999-01-06 14:46:06 +0000349def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000350 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000351 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000352 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000353 try:
354 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000355 parse_makefile(filename, g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000356 except IOError as msg:
Greg Warda570c052000-05-23 23:14:00 +0000357 my_msg = "invalid Python installation: unable to open %s" % filename
358 if hasattr(msg, "strerror"):
359 my_msg = my_msg + " (%s)" % msg.strerror
360
Fred Drake70b014d2001-07-18 18:39:56 +0000361 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000362
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 # load the installed pyconfig.h:
364 try:
365 filename = get_config_h_filename()
Guido van Rossum63236cf2007-05-25 18:39:29 +0000366 parse_config_h(io.open(filename), g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000367 except IOError as msg:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 my_msg = "invalid Python installation: unable to open %s" % filename
369 if hasattr(msg, "strerror"):
370 my_msg = my_msg + " (%s)" % msg.strerror
371
372 raise DistutilsPlatformError(my_msg)
373
Jack Jansen6b08a402004-06-03 12:41:45 +0000374 # On MacOSX we need to check the setting of the environment variable
375 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
376 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000377 # If it isn't set we set it to the configure-time value
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000378 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000379 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000380 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000381 if cur_target == '':
382 cur_target = cfg_target
383 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000384 elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
Jack Jansen6b08a402004-06-03 12:41:45 +0000385 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
386 % (cur_target, cfg_target))
387 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000388
Greg Ward4f880282000-06-27 01:59:06 +0000389 # On AIX, there are wrong paths to the linker scripts in the Makefile
390 # -- these paths are relative to the Python source, but when installed
391 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000392 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000393 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000394
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000395 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000396 # The following two branches are for 1.5.2 compatibility.
397 if sys.platform == 'aix4': # what about AIX 3.x ?
398 # Linker script is in the config directory, not in Modules as the
399 # Makefile says.
400 python_lib = get_python_lib(standard_lib=1)
401 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
402 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000403
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000404 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
405
Greg Ward879f0f12000-09-15 01:15:08 +0000406 global _config_vars
407 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000408
Greg Ward9ddaaa11999-01-06 14:46:06 +0000409
Greg Ward4d74d731999-06-08 01:58:36 +0000410def _init_nt():
411 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000412 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000413 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000414 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
415 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000416
Greg Ward32162e81999-08-29 18:22:13 +0000417 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000418 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000419
Fred Drake69e2c6e2000-02-08 15:55:42 +0000420 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000421 g['EXE'] = ".exe"
Christian Heimes255f53b2007-12-08 15:33:56 +0000422 g['VERSION'] = get_python_version().replace(".", "")
423 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000424
425 global _config_vars
426 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000427
Fred Drake69e2c6e2000-02-08 15:55:42 +0000428
Greg Ward0eff87a2000-03-07 03:30:09 +0000429def _init_mac():
430 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000431 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000432 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000433 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
434 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000435
436 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000437 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000438
Jack Jansendd13a202001-05-17 12:52:01 +0000439 import MacOS
440 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000441 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000442 else:
443 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000444
445 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000446 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
447 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000448
Jack Jansenab5320b2002-06-26 15:42:49 +0000449 # These are used by the extension module build
450 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000451 global _config_vars
452 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000453
Fred Drake69e2c6e2000-02-08 15:55:42 +0000454
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000455def _init_os2():
456 """Initialize the module as appropriate for OS/2"""
457 g = {}
458 # set basic install directories
459 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
460 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
461
462 # XXX hmmm.. a normal install puts include files here
463 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
464
465 g['SO'] = '.pyd'
466 g['EXE'] = ".exe"
467
468 global _config_vars
469 _config_vars = g
470
471
Greg Ward879f0f12000-09-15 01:15:08 +0000472def get_config_vars(*args):
473 """With no arguments, return a dictionary of all configuration
474 variables relevant for the current platform. Generally this includes
475 everything needed to build extensions and install both pure modules and
476 extensions. On Unix, this means every variable defined in Python's
477 installed Makefile; on Windows and Mac OS it's a much smaller set.
478
479 With arguments, return a list of values that result from looking up
480 each argument in the configuration variable dictionary.
481 """
482 global _config_vars
483 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000484 func = globals().get("_init_" + os.name)
485 if func:
486 func()
487 else:
488 _config_vars = {}
489
490 # Normalized versions of prefix and exec_prefix are handy to have;
491 # in fact, these are the standard versions used most places in the
492 # Distutils.
493 _config_vars['prefix'] = PREFIX
494 _config_vars['exec_prefix'] = EXEC_PREFIX
495
Thomas Wouters477c8d52006-05-27 19:21:47 +0000496 if sys.platform == 'darwin':
497 kernel_version = os.uname()[2] # Kernel version (8.4.3)
498 major_version = int(kernel_version.split('.')[0])
499
500 if major_version < 8:
501 # On Mac OS X before 10.4, check if -arch and -isysroot
502 # are in CFLAGS or LDFLAGS and remove them if they are.
503 # This is needed when building extensions on a 10.3 system
504 # using a universal build of python.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000505 for key in ('LDFLAGS', 'BASECFLAGS',
506 # a number of derived variables. These need to be
507 # patched up as well.
508 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000509 flags = _config_vars[key]
510 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512 _config_vars[key] = flags
513
Greg Ward879f0f12000-09-15 01:15:08 +0000514 if args:
515 vals = []
516 for name in args:
517 vals.append(_config_vars.get(name))
518 return vals
519 else:
520 return _config_vars
521
522def get_config_var(name):
523 """Return the value of a single variable using the dictionary
524 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000525 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000526 """
527 return get_config_vars().get(name)