blob: 4da376898c9987fe1f2a35219143366ac4dc1fe9 [file] [log] [blame]
Fred Drake522af3a1999-01-06 16:28:34 +00001"""Provide access to Python's configuration information. The specific names
2defined in the module depend heavily on the platform and configuration.
Greg Ward1190ee31998-12-18 23:46:33 +00003
4Written by: Fred L. Drake, Jr.
5Email: <fdrake@acm.org>
6Initial date: 17-Dec-1998
7"""
8
Greg Ward82d71ca2000-06-03 00:44:30 +00009__revision__ = "$Id$"
Greg Ward1190ee31998-12-18 23:46:33 +000010
Greg Ward9ddaaa11999-01-06 14:46:06 +000011import os
12import re
13import string
14import sys
Greg Ward1190ee31998-12-18 23:46:33 +000015
Fred Drakec1ee39a2000-03-09 15:54:52 +000016from errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000017
Greg Ward879f0f12000-09-15 01:15:08 +000018# These are needed in a couple of spots, so just compute them once.
Greg Wardcf6bea32000-04-10 01:15:06 +000019PREFIX = os.path.normpath(sys.prefix)
20EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000021
22
Greg Wardd38e6f72000-04-10 01:17:49 +000023def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000024 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000025
26 If 'plat_specific' is false (the default), this is the path to the
27 non-platform-specific header files, i.e. Python.h and so on;
28 otherwise, this is the path to platform-specific header files
29 (namely config.h).
30
Greg Wardd38e6f72000-04-10 01:17:49 +000031 If 'prefix' is supplied, use it instead of sys.prefix or
32 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000033 """
Greg Wardd38e6f72000-04-10 01:17:49 +000034 if prefix is None:
35 prefix = (plat_specific and EXEC_PREFIX or PREFIX)
Greg Ward7d73b9e2000-03-09 03:16:05 +000036 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +000037 return os.path.join(prefix, "include", "python" + sys.version[:3])
Greg Ward7d73b9e2000-03-09 03:16:05 +000038 elif os.name == "nt":
Greg Wardcf6bea32000-04-10 01:15:06 +000039 return os.path.join(prefix, "Include") # include or Include?
Greg Ward7d73b9e2000-03-09 03:16:05 +000040 elif os.name == "mac":
Greg Wardcf6bea32000-04-10 01:15:06 +000041 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000042 else:
43 raise DistutilsPlatformError, \
44 ("I don't know where Python installs its C header files " +
45 "on platform '%s'") % os.name
46
47
Greg Wardd38e6f72000-04-10 01:17:49 +000048def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000049 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000050 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000051
Fred Drakec1ee39a2000-03-09 15:54:52 +000052 If 'plat_specific' is true, return the directory containing
53 platform-specific modules, i.e. any module from a non-pure-Python
54 module distribution; otherwise, return the platform-shared library
55 directory. If 'standard_lib' is true, return the directory
56 containing standard Python library modules; otherwise, return the
57 directory for site-specific modules.
58
Greg Wardd38e6f72000-04-10 01:17:49 +000059 If 'prefix' is supplied, use it instead of sys.prefix or
60 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000061 """
Greg Wardd38e6f72000-04-10 01:17:49 +000062 if prefix is None:
63 prefix = (plat_specific and EXEC_PREFIX or PREFIX)
Greg Ward7d73b9e2000-03-09 03:16:05 +000064
65 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +000066 libpython = os.path.join(prefix,
Fred Drakec1ee39a2000-03-09 15:54:52 +000067 "lib", "python" + sys.version[:3])
Greg Ward7d73b9e2000-03-09 03:16:05 +000068 if standard_lib:
69 return libpython
70 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +000071 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +000072
73 elif os.name == "nt":
74 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000075 return os.path.join(PREFIX, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +000076 else:
Greg Wardcf6bea32000-04-10 01:15:06 +000077 return prefix
Greg Ward7d73b9e2000-03-09 03:16:05 +000078
79 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +000080 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +000081 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000082 return os.path.join(EXEC_PREFIX, "Mac", "Plugins")
Greg Ward7d73b9e2000-03-09 03:16:05 +000083 else:
84 raise DistutilsPlatformError, \
85 "OK, where DO site-specific extensions go on the Mac?"
86 else:
87 if standard_lib:
Greg Wardcf6bea32000-04-10 01:15:06 +000088 return os.path.join(PREFIX, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +000089 else:
90 raise DistutilsPlatformError, \
91 "OK, where DO site-specific modules go on the Mac?"
92 else:
93 raise DistutilsPlatformError, \
94 ("I don't know where Python installs its library " +
95 "on platform '%s'") % os.name
96
Fred Drakec1ee39a2000-03-09 15:54:52 +000097# get_python_lib()
Greg Ward7d73b9e2000-03-09 03:16:05 +000098
99
Greg Wardbb7baa72000-06-25 02:09:14 +0000100def customize_compiler (compiler):
101 """Do any platform-specific customization of the CCompiler instance
102 'compiler'. Mainly needed on Unix, so we can plug in the information
103 that varies across Unices and is stored in Python's Makefile.
104 """
105 if compiler.compiler_type == "unix":
Greg Ward879f0f12000-09-15 01:15:08 +0000106 (cc, opt, ccshared, ldshared, so_ext) = \
107 get_config_vars('CC', 'OPT', 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000108
Greg Ward879f0f12000-09-15 01:15:08 +0000109 cc_cmd = cc + ' ' + opt
110 compiler.set_executables(
111 preprocessor=cc + " -E", # not always!
112 compiler=cc_cmd,
113 compiler_so=cc_cmd + ' ' + ccshared,
114 linker_so=ldshared,
115 linker_exe=cc)
116
117 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000118
119
Greg Ward9ddaaa11999-01-06 14:46:06 +0000120def get_config_h_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000121 """Return full pathname of installed config.h file."""
Fred Drakec1ee39a2000-03-09 15:54:52 +0000122 inc_dir = get_python_inc(plat_specific=1)
123 return os.path.join(inc_dir, "config.h")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000124
Greg Ward1190ee31998-12-18 23:46:33 +0000125
Greg Ward9ddaaa11999-01-06 14:46:06 +0000126def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000127 """Return full pathname of installed Makefile from the Python build."""
Fred Drakec1ee39a2000-03-09 15:54:52 +0000128 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
129 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000130
Greg Ward1190ee31998-12-18 23:46:33 +0000131
Greg Ward9ddaaa11999-01-06 14:46:06 +0000132def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000133 """Parse a config.h-style file.
134
135 A dictionary containing name/value pairs is returned. If an
136 optional dictionary is passed in as the second argument, it is
137 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000138 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000139 if g is None:
140 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000141 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
142 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000143 #
Greg Ward1190ee31998-12-18 23:46:33 +0000144 while 1:
145 line = fp.readline()
146 if not line:
147 break
148 m = define_rx.match(line)
149 if m:
150 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000151 try: v = string.atoi(v)
152 except ValueError: pass
153 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000154 else:
155 m = undef_rx.match(line)
156 if m:
157 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000158 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000159
Greg Wardd283ce72000-09-17 00:53:02 +0000160
161# Regexes needed for parsing Makefile (and similar syntaxes,
162# like old-style Setup files).
163_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
164_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
165_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
166
Greg Ward3fff8d22000-09-15 00:03:13 +0000167def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000168 """Parse a Makefile-style file.
169
170 A dictionary containing name/value pairs is returned. If an
171 optional dictionary is passed in as the second argument, it is
172 used instead of a new dictionary.
173
Fred Drake522af3a1999-01-06 16:28:34 +0000174 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000175 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000176 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000177
Greg Ward9ddaaa11999-01-06 14:46:06 +0000178 if g is None:
179 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000180 done = {}
181 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000182
Greg Ward1190ee31998-12-18 23:46:33 +0000183 while 1:
184 line = fp.readline()
Greg Wardd283ce72000-09-17 00:53:02 +0000185 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000186 break
Greg Wardd283ce72000-09-17 00:53:02 +0000187 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000188 if m:
189 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000190 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000191 if "$" in v:
192 notdone[n] = v
193 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000194 try: v = string.atoi(v)
195 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000196 done[n] = v
197
198 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000199 while notdone:
200 for name in notdone.keys():
201 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000202 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000203 if m:
204 n = m.group(1)
205 if done.has_key(n):
206 after = value[m.end():]
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000207 value = value[:m.start()] + str(done[n]) + after
Greg Ward1190ee31998-12-18 23:46:33 +0000208 if "$" in after:
209 notdone[name] = value
210 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000211 try: value = string.atoi(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000212 except ValueError:
213 done[name] = string.strip(value)
214 else:
215 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000216 del notdone[name]
217 elif notdone.has_key(n):
218 # get it on a subsequent round
219 pass
220 else:
221 done[n] = ""
222 after = value[m.end():]
223 value = value[:m.start()] + after
224 if "$" in after:
225 notdone[name] = value
226 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000227 try: value = string.atoi(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000228 except ValueError:
229 done[name] = string.strip(value)
230 else:
231 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000232 del notdone[name]
233 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000234 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000235 del notdone[name]
236
Greg Wardd283ce72000-09-17 00:53:02 +0000237 fp.close()
238
Greg Ward1190ee31998-12-18 23:46:33 +0000239 # save the results in the global dictionary
240 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000241 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000242
243
Greg Wardd283ce72000-09-17 00:53:02 +0000244def expand_makefile_vars(s, vars):
245 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
246 'string' according to 'vars' (a dictionary mapping variable names to
247 values). Variables not present in 'vars' are silently expanded to the
248 empty string. The variable values in 'vars' should not contain further
249 variable expansions; if 'vars' is the output of 'parse_makefile()',
250 you're fine. Returns a variable-expanded version of 's'.
251 """
252
253 # This algorithm does multiple expansion, so if vars['foo'] contains
254 # "${bar}", it will expand ${foo} to ${bar}, and then expand
255 # ${bar}... and so forth. This is fine as long as 'vars' comes from
256 # 'parse_makefile()', which takes care of such expansions eagerly,
257 # according to make's variable expansion semantics.
258
259 while 1:
260 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
261 if m:
262 name = m.group(1)
263 (beg, end) = m.span()
264 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
265 else:
266 break
267 return s
268
269
Greg Ward879f0f12000-09-15 01:15:08 +0000270_config_vars = None
271
Greg Ward9ddaaa11999-01-06 14:46:06 +0000272def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000273 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000274 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000275 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000276 try:
277 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000278 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000279 except IOError, msg:
280 my_msg = "invalid Python installation: unable to open %s" % filename
281 if hasattr(msg, "strerror"):
282 my_msg = my_msg + " (%s)" % msg.strerror
283
284 raise DistutilsPlatformError, my_msg
285
Greg Ward4f880282000-06-27 01:59:06 +0000286
287 # On AIX, there are wrong paths to the linker scripts in the Makefile
288 # -- these paths are relative to the Python source, but when installed
289 # the scripts are in another directory.
Greg Wardb231e1a2000-06-27 01:59:43 +0000290 if sys.platform == 'aix4': # what about AIX 3.x ?
Greg Ward4f880282000-06-27 01:59:06 +0000291 # Linker script is in the config directory, not in Modules as the
292 # Makefile says.
293 python_lib = get_python_lib(standard_lib=1)
294 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
295 python_exp = os.path.join(python_lib, 'config', 'python.exp')
296
297 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000298
Greg Ward879f0f12000-09-15 01:15:08 +0000299 elif sys.platform == 'beos':
Greg Ward66e966f2000-09-01 01:23:26 +0000300
301 # Linker script is in the config directory. In the Makefile it is
302 # relative to the srcdir, which after installation no longer makes
303 # sense.
304 python_lib = get_python_lib(standard_lib=1)
305 linkerscript_name = os.path.basename(string.split(g['LDSHARED'])[0])
306 linkerscript = os.path.join(python_lib, 'config', linkerscript_name)
307
308 # XXX this isn't the right place to do this: adding the Python
309 # library to the link, if needed, should be in the "build_ext"
310 # command. (It's also needed for non-MS compilers on Windows, and
311 # it's taken care of for them by the 'build_ext.get_libraries()'
312 # method.)
313 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Greg Ward879f0f12000-09-15 01:15:08 +0000314 (linkerscript, PREFIX, sys.version[0:3]))
315
316 global _config_vars
317 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000318
Greg Ward9ddaaa11999-01-06 14:46:06 +0000319
Greg Ward4d74d731999-06-08 01:58:36 +0000320def _init_nt():
321 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000322 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000323 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000324 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
325 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000326
Greg Ward32162e81999-08-29 18:22:13 +0000327 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000328 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000329
Fred Drake69e2c6e2000-02-08 15:55:42 +0000330 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000331 g['EXE'] = ".exe"
Greg Ward879f0f12000-09-15 01:15:08 +0000332
333 global _config_vars
334 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000335
Fred Drake69e2c6e2000-02-08 15:55:42 +0000336
Greg Ward0eff87a2000-03-07 03:30:09 +0000337def _init_mac():
338 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000339 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000340 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000341 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
342 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000343
344 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000345 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000346
347 g['SO'] = '.ppc.slb'
Greg Ward7d73b9e2000-03-09 03:16:05 +0000348
349 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000350 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
351 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000352
Greg Ward879f0f12000-09-15 01:15:08 +0000353 global _config_vars
354 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000355
Fred Drake69e2c6e2000-02-08 15:55:42 +0000356
Greg Ward879f0f12000-09-15 01:15:08 +0000357def get_config_vars(*args):
358 """With no arguments, return a dictionary of all configuration
359 variables relevant for the current platform. Generally this includes
360 everything needed to build extensions and install both pure modules and
361 extensions. On Unix, this means every variable defined in Python's
362 installed Makefile; on Windows and Mac OS it's a much smaller set.
363
364 With arguments, return a list of values that result from looking up
365 each argument in the configuration variable dictionary.
366 """
367 global _config_vars
368 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000369 func = globals().get("_init_" + os.name)
370 if func:
371 func()
372 else:
373 _config_vars = {}
374
375 # Normalized versions of prefix and exec_prefix are handy to have;
376 # in fact, these are the standard versions used most places in the
377 # Distutils.
378 _config_vars['prefix'] = PREFIX
379 _config_vars['exec_prefix'] = EXEC_PREFIX
380
381 if args:
382 vals = []
383 for name in args:
384 vals.append(_config_vars.get(name))
385 return vals
386 else:
387 return _config_vars
388
389def get_config_var(name):
390 """Return the value of a single variable using the dictionary
391 returned by 'get_config_vars()'. Equivalent to
392 get_config_vars().get(name)
393 """
394 return get_config_vars().get(name)