blob: 9ca94ed9c819c1a4ae6714a8b1930f167276d665 [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 Ward1190ee31998-12-18 23:46:33 +000018
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":
80 if platform_specific:
81 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":
106 cc_cmd = CC + ' ' + OPT
107 compiler.set_executables(
108 preprocessor=CC + " -E", # not always!
109 compiler=cc_cmd,
110 compiler_so=cc_cmd + ' ' + CCSHARED,
111 linker_so=LDSHARED,
112 linker_exe=CC)
113
114 compiler.shared_lib_extension = SO
115
116
Greg Ward9ddaaa11999-01-06 14:46:06 +0000117def get_config_h_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000118 """Return full pathname of installed config.h file."""
Fred Drakec1ee39a2000-03-09 15:54:52 +0000119 inc_dir = get_python_inc(plat_specific=1)
120 return os.path.join(inc_dir, "config.h")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121
Greg Ward1190ee31998-12-18 23:46:33 +0000122
Greg Ward9ddaaa11999-01-06 14:46:06 +0000123def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000124 """Return full pathname of installed Makefile from the Python build."""
Fred Drakec1ee39a2000-03-09 15:54:52 +0000125 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
126 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000127
Greg Ward1190ee31998-12-18 23:46:33 +0000128
Greg Ward9ddaaa11999-01-06 14:46:06 +0000129def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000130 """Parse a config.h-style file.
131
132 A dictionary containing name/value pairs is returned. If an
133 optional dictionary is passed in as the second argument, it is
134 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000135 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000136 if g is None:
137 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000138 define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
139 undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000140 #
Greg Ward1190ee31998-12-18 23:46:33 +0000141 while 1:
142 line = fp.readline()
143 if not line:
144 break
145 m = define_rx.match(line)
146 if m:
147 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000148 try: v = string.atoi(v)
149 except ValueError: pass
150 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000151 else:
152 m = undef_rx.match(line)
153 if m:
154 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000155 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000156
Greg Ward9ddaaa11999-01-06 14:46:06 +0000157def parse_makefile(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000158 """Parse a Makefile-style file.
159
160 A dictionary containing name/value pairs is returned. If an
161 optional dictionary is passed in as the second argument, it is
162 used instead of a new dictionary.
163
Fred Drake522af3a1999-01-06 16:28:34 +0000164 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000165 if g is None:
166 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000167 variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
168 done = {}
169 notdone = {}
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 = variable_rx.match(line)
176 if m:
177 n, v = m.group(1, 2)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000178 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000179 if "$" in v:
180 notdone[n] = v
181 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000182 try: v = string.atoi(v)
183 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000184 done[n] = v
185
186 # do variable interpolation here
187 findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
188 findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
189 while notdone:
190 for name in notdone.keys():
191 value = notdone[name]
192 m = findvar1_rx.search(value)
193 if not m:
194 m = findvar2_rx.search(value)
195 if m:
196 n = m.group(1)
197 if done.has_key(n):
198 after = value[m.end():]
199 value = value[:m.start()] + done[n] + after
200 if "$" in after:
201 notdone[name] = value
202 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000203 try: value = string.atoi(value)
204 except ValueError: pass
205 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000206 del notdone[name]
207 elif notdone.has_key(n):
208 # get it on a subsequent round
209 pass
210 else:
211 done[n] = ""
212 after = value[m.end():]
213 value = value[:m.start()] + after
214 if "$" in after:
215 notdone[name] = value
216 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000217 try: value = string.atoi(value)
218 except ValueError: pass
219 done[name] = string.strip(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000220 del notdone[name]
221 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000222 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000223 del notdone[name]
224
225 # save the results in the global dictionary
226 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000227 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000228
229
Greg Ward9ddaaa11999-01-06 14:46:06 +0000230def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000231 """Initialize the module as appropriate for POSIX systems."""
Greg Ward9ddaaa11999-01-06 14:46:06 +0000232 g = globals()
Greg Warda0ca3f22000-02-02 00:05:14 +0000233 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000234 try:
235 filename = get_makefile_filename()
236 file = open(filename)
237 except IOError, msg:
238 my_msg = "invalid Python installation: unable to open %s" % filename
239 if hasattr(msg, "strerror"):
240 my_msg = my_msg + " (%s)" % msg.strerror
241
242 raise DistutilsPlatformError, my_msg
243
244 parse_makefile(file, g)
Greg Ward4f880282000-06-27 01:59:06 +0000245
246 # On AIX, there are wrong paths to the linker scripts in the Makefile
247 # -- these paths are relative to the Python source, but when installed
248 # the scripts are in another directory.
249 if sys.platform: # == 'aix4': # what about AIX 3.x ?
250 # Linker script is in the config directory, not in Modules as the
251 # Makefile says.
252 python_lib = get_python_lib(standard_lib=1)
253 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
254 python_exp = os.path.join(python_lib, 'config', 'python.exp')
255
256 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000257
258
Greg Ward4d74d731999-06-08 01:58:36 +0000259def _init_nt():
260 """Initialize the module as appropriate for NT"""
Fred Drake69e2c6e2000-02-08 15:55:42 +0000261 g = globals()
Greg Ward4d74d731999-06-08 01:58:36 +0000262 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000263 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
264 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000265
Greg Ward32162e81999-08-29 18:22:13 +0000266 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000267 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000268
Fred Drake69e2c6e2000-02-08 15:55:42 +0000269 g['SO'] = '.pyd'
Greg Wardcf6bea32000-04-10 01:15:06 +0000270 g['exec_prefix'] = EXEC_PREFIX
Greg Ward9ddaaa11999-01-06 14:46:06 +0000271
Greg Ward82d71ca2000-06-03 00:44:30 +0000272 # These are needed for the CygwinCCompiler and Mingw32CCompiler
273 # classes, which are just UnixCCompiler classes that happen to work on
274 # Windows. UnixCCompiler expects to find these values in sysconfig, so
275 # here they are. The fact that other Windows compilers don't need
276 # these values is pure luck (hmmm).
Greg Wardbb7baa72000-06-25 02:09:14 +0000277
278 # XXX I think these are now unnecessary...
279
Greg Ward82d71ca2000-06-03 00:44:30 +0000280 g['CC'] = "cc" # not gcc?
281 g['RANLIB'] = "ranlib"
282 g['AR'] = "ar"
283 g['OPT'] = "-O2"
284 g['SO'] = ".pyd"
285 g['LDSHARED'] = "ld"
286 g['CCSHARED'] = ""
287 g['EXE'] = ".exe"
288
Fred Drake69e2c6e2000-02-08 15:55:42 +0000289
Greg Ward0eff87a2000-03-07 03:30:09 +0000290def _init_mac():
291 """Initialize the module as appropriate for Macintosh systems"""
292 g = globals()
Greg Ward0eff87a2000-03-07 03:30:09 +0000293 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000294 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
295 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000296
297 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000298 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000299
300 g['SO'] = '.ppc.slb'
Greg Wardcf6bea32000-04-10 01:15:06 +0000301 g['exec_prefix'] = EXEC_PREFIX
302 print sys.prefix, PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +0000303
304 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000305 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
306 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000307
308
Greg Ward9ddaaa11999-01-06 14:46:06 +0000309try:
310 exec "_init_" + os.name
311except NameError:
312 # not needed for this platform
313 pass
314else:
315 exec "_init_%s()" % os.name
316
Fred Drake69e2c6e2000-02-08 15:55:42 +0000317
Greg Ward1190ee31998-12-18 23:46:33 +0000318del _init_posix
Greg Ward4d74d731999-06-08 01:58:36 +0000319del _init_nt
Greg Ward0eff87a2000-03-07 03:30:09 +0000320del _init_mac