blob: bbf7c4afabbd29ebc06072c0627584cf0313eec8 [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>
10Initial date: 17-Dec-1998
11"""
12
Greg Ward82d71ca2000-06-03 00:44:30 +000013__revision__ = "$Id$"
Greg Ward1190ee31998-12-18 23:46:33 +000014
Greg Ward9ddaaa11999-01-06 14:46:06 +000015import os
16import re
17import string
18import sys
Greg Ward1190ee31998-12-18 23:46:33 +000019
Fred Drakec1ee39a2000-03-09 15:54:52 +000020from errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000021
Greg Ward879f0f12000-09-15 01:15:08 +000022# These are needed in a couple of spots, so just compute them once.
Greg Wardcf6bea32000-04-10 01:15:06 +000023PREFIX = os.path.normpath(sys.prefix)
24EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000025
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000026# Boolean; if it's true, we're still building Python, so
27# we use different (hard-wired) directories.
28
29python_build = 0
30
31def set_python_build():
32 """Set the python_build flag to true; this means that we're
33 building Python itself. Only called from the setup.py script
34 shipped with Python.
35 """
36
37 global python_build
38 python_build = 1
Fred Drakec1ee39a2000-03-09 15:54:52 +000039
Greg Wardd38e6f72000-04-10 01:17:49 +000040def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000041 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000042
43 If 'plat_specific' is false (the default), this is the path to the
44 non-platform-specific header files, i.e. Python.h and so on;
45 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000046 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000047
Greg Wardd38e6f72000-04-10 01:17:49 +000048 If 'prefix' is supplied, use it instead of sys.prefix or
49 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000050 """
Greg Wardd38e6f72000-04-10 01:17:49 +000051 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000052 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000053 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000054 if python_build:
55 return "Include/"
Greg Wardcf6bea32000-04-10 01:15:06 +000056 return os.path.join(prefix, "include", "python" + sys.version[:3])
Greg Ward7d73b9e2000-03-09 03:16:05 +000057 elif os.name == "nt":
Greg Wardcf6bea32000-04-10 01:15:06 +000058 return os.path.join(prefix, "Include") # include or Include?
Greg Ward7d73b9e2000-03-09 03:16:05 +000059 elif os.name == "mac":
Greg Wardcf6bea32000-04-10 01:15:06 +000060 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000061 else:
Fred Drake70b014d2001-07-18 18:39:56 +000062 raise DistutilsPlatformError(
63 "I don't know where Python installs its C header files "
64 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000065
66
Greg Wardd38e6f72000-04-10 01:17:49 +000067def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000068 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000069 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000070
Fred Drakec1ee39a2000-03-09 15:54:52 +000071 If 'plat_specific' is true, return the directory containing
72 platform-specific modules, i.e. any module from a non-pure-Python
73 module distribution; otherwise, return the platform-shared library
74 directory. If 'standard_lib' is true, return the directory
75 containing standard Python library modules; otherwise, return the
76 directory for site-specific modules.
77
Greg Wardd38e6f72000-04-10 01:17:49 +000078 If 'prefix' is supplied, use it instead of sys.prefix or
79 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000080 """
Greg Wardd38e6f72000-04-10 01:17:49 +000081 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000082 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000083
84 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +000085 libpython = os.path.join(prefix,
Fred Drakec1ee39a2000-03-09 15:54:52 +000086 "lib", "python" + sys.version[:3])
Greg Ward7d73b9e2000-03-09 03:16:05 +000087 if standard_lib:
88 return libpython
89 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +000090 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +000091
92 elif os.name == "nt":
93 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000094 return os.path.join(PREFIX, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +000095 else:
Greg Wardcf6bea32000-04-10 01:15:06 +000096 return prefix
Greg Ward7d73b9e2000-03-09 03:16:05 +000097
98 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +000099 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000100 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +0000101 return os.path.join(EXEC_PREFIX, "Mac", "Plugins")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000102 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000103 raise DistutilsPlatformError(
104 "OK, where DO site-specific extensions go on the Mac?")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000105 else:
106 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +0000107 return os.path.join(PREFIX, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000108 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000109 raise DistutilsPlatformError(
110 "OK, where DO site-specific modules go on the Mac?")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000111 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000112 raise DistutilsPlatformError(
113 "I don't know where Python installs its library "
114 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000115
Fred Drakec1ee39a2000-03-09 15:54:52 +0000116# get_python_lib()
Greg Ward7d73b9e2000-03-09 03:16:05 +0000117
118
Fred Drake70b014d2001-07-18 18:39:56 +0000119def customize_compiler(compiler):
Greg Wardbb7baa72000-06-25 02:09:14 +0000120 """Do any platform-specific customization of the CCompiler instance
121 'compiler'. Mainly needed on Unix, so we can plug in the information
122 that varies across Unices and is stored in Python's Makefile.
123 """
124 if compiler.compiler_type == "unix":
Greg Ward879f0f12000-09-15 01:15:08 +0000125 (cc, opt, ccshared, ldshared, so_ext) = \
126 get_config_vars('CC', 'OPT', 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000127
Greg Ward879f0f12000-09-15 01:15:08 +0000128 cc_cmd = cc + ' ' + opt
129 compiler.set_executables(
130 preprocessor=cc + " -E", # not always!
131 compiler=cc_cmd,
132 compiler_so=cc_cmd + ' ' + ccshared,
133 linker_so=ldshared,
134 linker_exe=cc)
135
136 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000137
138
Greg Ward9ddaaa11999-01-06 14:46:06 +0000139def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000140 """Return full pathname of installed pyconfig.h file."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000141 if python_build: inc_dir = '.'
142 else: inc_dir = get_python_inc(plat_specific=1)
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000143 if sys.version < '2.2':
144 config_h = 'config.h'
145 else:
146 # The name of the config.h file changed in 2.2
147 config_h = 'pyconfig.h'
148 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000149
Greg Ward1190ee31998-12-18 23:46:33 +0000150
Greg Ward9ddaaa11999-01-06 14:46:06 +0000151def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000152 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000153 if python_build:
Neil Schemenauer84d14ba2001-01-24 17:17:20 +0000154 return './Makefile'
Fred Drakec1ee39a2000-03-09 15:54:52 +0000155 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
156 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000157
Greg Ward1190ee31998-12-18 23:46:33 +0000158
Greg Ward9ddaaa11999-01-06 14:46:06 +0000159def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000160 """Parse a config.h-style file.
161
162 A dictionary containing name/value pairs is returned. If an
163 optional dictionary is passed in as the second argument, it is
164 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000165 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000166 if g is None:
167 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000168 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
169 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000170 #
Greg Ward1190ee31998-12-18 23:46:33 +0000171 while 1:
172 line = fp.readline()
173 if not line:
174 break
175 m = define_rx.match(line)
176 if m:
177 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000178 try: v = string.atoi(v)
179 except ValueError: pass
180 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000181 else:
182 m = undef_rx.match(line)
183 if m:
184 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000185 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000186
Greg Wardd283ce72000-09-17 00:53:02 +0000187
188# Regexes needed for parsing Makefile (and similar syntaxes,
189# like old-style Setup files).
190_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
191_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
192_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
193
Greg Ward3fff8d22000-09-15 00:03:13 +0000194def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000195 """Parse a Makefile-style file.
196
197 A dictionary containing name/value pairs is returned. If an
198 optional dictionary is passed in as the second argument, it is
199 used instead of a new dictionary.
200
Fred Drake522af3a1999-01-06 16:28:34 +0000201 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000202 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000203 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000204
Greg Ward9ddaaa11999-01-06 14:46:06 +0000205 if g is None:
206 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000207 done = {}
208 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000209
Greg Ward1190ee31998-12-18 23:46:33 +0000210 while 1:
211 line = fp.readline()
Greg Wardd283ce72000-09-17 00:53:02 +0000212 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000213 break
Greg Wardd283ce72000-09-17 00:53:02 +0000214 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000215 if m:
216 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000217 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000218 if "$" in v:
219 notdone[n] = v
220 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000221 try: v = string.atoi(v)
222 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000223 done[n] = v
224
225 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000226 while notdone:
227 for name in notdone.keys():
228 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000229 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000230 if m:
231 n = m.group(1)
232 if done.has_key(n):
233 after = value[m.end():]
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000234 value = value[:m.start()] + str(done[n]) + after
Greg Ward1190ee31998-12-18 23:46:33 +0000235 if "$" in after:
236 notdone[name] = value
237 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000238 try: value = string.atoi(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000239 except ValueError:
240 done[name] = string.strip(value)
241 else:
242 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000243 del notdone[name]
244 elif notdone.has_key(n):
245 # get it on a subsequent round
246 pass
247 else:
248 done[n] = ""
249 after = value[m.end():]
250 value = value[:m.start()] + after
251 if "$" in after:
252 notdone[name] = value
253 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000254 try: value = string.atoi(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000255 except ValueError:
256 done[name] = string.strip(value)
257 else:
258 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000259 del notdone[name]
260 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000261 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000262 del notdone[name]
263
Greg Wardd283ce72000-09-17 00:53:02 +0000264 fp.close()
265
Greg Ward1190ee31998-12-18 23:46:33 +0000266 # save the results in the global dictionary
267 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000268 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000269
270
Greg Wardd283ce72000-09-17 00:53:02 +0000271def expand_makefile_vars(s, vars):
272 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
273 'string' according to 'vars' (a dictionary mapping variable names to
274 values). Variables not present in 'vars' are silently expanded to the
275 empty string. The variable values in 'vars' should not contain further
276 variable expansions; if 'vars' is the output of 'parse_makefile()',
277 you're fine. Returns a variable-expanded version of 's'.
278 """
279
280 # This algorithm does multiple expansion, so if vars['foo'] contains
281 # "${bar}", it will expand ${foo} to ${bar}, and then expand
282 # ${bar}... and so forth. This is fine as long as 'vars' comes from
283 # 'parse_makefile()', which takes care of such expansions eagerly,
284 # according to make's variable expansion semantics.
285
286 while 1:
287 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
288 if m:
289 name = m.group(1)
290 (beg, end) = m.span()
291 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
292 else:
293 break
294 return s
295
296
Greg Ward879f0f12000-09-15 01:15:08 +0000297_config_vars = None
298
Greg Ward9ddaaa11999-01-06 14:46:06 +0000299def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000300 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000301 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000302 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000303 try:
304 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000305 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000306 except IOError, msg:
307 my_msg = "invalid Python installation: unable to open %s" % filename
308 if hasattr(msg, "strerror"):
309 my_msg = my_msg + " (%s)" % msg.strerror
310
Fred Drake70b014d2001-07-18 18:39:56 +0000311 raise DistutilsPlatformError(my_msg)
Greg Warda570c052000-05-23 23:14:00 +0000312
Greg Ward4f880282000-06-27 01:59:06 +0000313
314 # On AIX, there are wrong paths to the linker scripts in the Makefile
315 # -- these paths are relative to the Python source, but when installed
316 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000317 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000318 g['LDSHARED'] = g['BLDSHARED']
Greg Ward879f0f12000-09-15 01:15:08 +0000319
320 global _config_vars
321 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000322
Greg Ward9ddaaa11999-01-06 14:46:06 +0000323
Greg Ward4d74d731999-06-08 01:58:36 +0000324def _init_nt():
325 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000326 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000327 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000328 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
329 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000330
Greg Ward32162e81999-08-29 18:22:13 +0000331 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000332 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000333
Fred Drake69e2c6e2000-02-08 15:55:42 +0000334 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000335 g['EXE'] = ".exe"
Greg Ward879f0f12000-09-15 01:15:08 +0000336
337 global _config_vars
338 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000339
Fred Drake69e2c6e2000-02-08 15:55:42 +0000340
Greg Ward0eff87a2000-03-07 03:30:09 +0000341def _init_mac():
342 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000343 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000344 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000345 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
346 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000347
348 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000349 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000350
Jack Jansendd13a202001-05-17 12:52:01 +0000351 import MacOS
352 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000353 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000354 else:
355 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000356
357 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000358 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
359 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000360
Greg Ward879f0f12000-09-15 01:15:08 +0000361 global _config_vars
362 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000363
Fred Drake69e2c6e2000-02-08 15:55:42 +0000364
Greg Ward879f0f12000-09-15 01:15:08 +0000365def get_config_vars(*args):
366 """With no arguments, return a dictionary of all configuration
367 variables relevant for the current platform. Generally this includes
368 everything needed to build extensions and install both pure modules and
369 extensions. On Unix, this means every variable defined in Python's
370 installed Makefile; on Windows and Mac OS it's a much smaller set.
371
372 With arguments, return a list of values that result from looking up
373 each argument in the configuration variable dictionary.
374 """
375 global _config_vars
376 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000377 func = globals().get("_init_" + os.name)
378 if func:
379 func()
380 else:
381 _config_vars = {}
382
383 # Normalized versions of prefix and exec_prefix are handy to have;
384 # in fact, these are the standard versions used most places in the
385 # Distutils.
386 _config_vars['prefix'] = PREFIX
387 _config_vars['exec_prefix'] = EXEC_PREFIX
388
389 if args:
390 vals = []
391 for name in args:
392 vals.append(_config_vars.get(name))
393 return vals
394 else:
395 return _config_vars
396
397def get_config_var(name):
398 """Return the value of a single variable using the dictionary
399 returned by 'get_config_vars()'. Equivalent to
400 get_config_vars().get(name)
401 """
402 return get_config_vars().get(name)