blob: aead1a19ba6d4720698c08c3bef1d916ba9e6c58 [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
Greg Ward9ddaaa11999-01-06 14:46:06 +000014import os
15import re
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +000016import string
Greg Ward9ddaaa11999-01-06 14:46:06 +000017import sys
Greg Ward1190ee31998-12-18 23:46:33 +000018
Guido van Rossumf8480a72006-03-15 23:08:13 +000019from distutils.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 Heimesd3fc07a42007-12-06 13:15:13 +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))
30
Fred Drake16c8d702002-06-04 15:28:21 +000031# python_build: (Boolean) if true, we're either building Python or
32# building an extension with an un-installed Python, so we use
33# different (hard-wired) directories.
Christian Heimesd3fc07a42007-12-06 13:15:13 +000034python_build = os.path.isfile(os.path.join(project_base, "Modules",
Christian Heimes0a0e5832007-12-13 19:23:16 +000035 "Setup.local"))
Fred Drakec1ee39a2000-03-09 15:54:52 +000036
Fred Drakec916cdc2001-08-02 20:03:12 +000037
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000038def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000039 """Return a string containing the major and minor Python version,
40 leaving off the patchlevel. Sample return values could be '1.5'
41 or '2.2'.
42 """
43 return sys.version[:3]
44
45
Greg Wardd38e6f72000-04-10 01:17:49 +000046def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000047 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000048
49 If 'plat_specific' is false (the default), this is the path to the
50 non-platform-specific header files, i.e. Python.h and so on;
51 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000052 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000053
Greg Wardd38e6f72000-04-10 01:17:49 +000054 If 'prefix' is supplied, use it instead of sys.prefix or
55 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000056 """
Greg Wardd38e6f72000-04-10 01:17:49 +000057 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000058 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000059 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000060 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +000061 base = os.path.dirname(os.path.abspath(sys.executable))
62 if plat_specific:
63 inc_dir = base
64 else:
65 inc_dir = os.path.join(base, "Include")
66 if not os.path.exists(inc_dir):
67 inc_dir = os.path.join(os.path.dirname(base), "Include")
68 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000069 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000070 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000071 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000072 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000073 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000074 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000075 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000076 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000077 elif os.name == "os2":
78 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000079 else:
Fred Drake70b014d2001-07-18 18:39:56 +000080 raise DistutilsPlatformError(
81 "I don't know where Python installs its C header files "
82 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000083
84
Greg Wardd38e6f72000-04-10 01:17:49 +000085def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000086 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000087 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000088
Fred Drakec1ee39a2000-03-09 15:54:52 +000089 If 'plat_specific' is true, return the directory containing
90 platform-specific modules, i.e. any module from a non-pure-Python
91 module distribution; otherwise, return the platform-shared library
92 directory. If 'standard_lib' is true, return the directory
93 containing standard Python library modules; otherwise, return the
94 directory for site-specific modules.
95
Greg Wardd38e6f72000-04-10 01:17:49 +000096 If 'prefix' is supplied, use it instead of sys.prefix or
97 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000098 """
Greg Wardd38e6f72000-04-10 01:17:49 +000099 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000100 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000101
Greg Ward7d73b9e2000-03-09 03:16:05 +0000102 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000103 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000104 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000105 if standard_lib:
106 return libpython
107 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000108 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000109
110 elif os.name == "nt":
111 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000112 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000113 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000114 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000115 return prefix
116 else:
117 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000118
119 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000120 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000122 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000123 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000124 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000125 else:
126 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000127 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000128 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000129 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000130
131 elif os.name == "os2":
132 if standard_lib:
133 return os.path.join(PREFIX, "Lib")
134 else:
135 return os.path.join(PREFIX, "Lib", "site-packages")
136
Greg Ward7d73b9e2000-03-09 03:16:05 +0000137 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000138 raise DistutilsPlatformError(
139 "I don't know where Python installs its library "
140 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000141
Greg Ward7d73b9e2000-03-09 03:16:05 +0000142
Fred Drake70b014d2001-07-18 18:39:56 +0000143def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000144 """Do any platform-specific customization of a CCompiler instance.
145
146 Mainly needed on Unix, so we can plug in the information that
147 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000148 """
149 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000150 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000151 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000152 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000153
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000154 if os.environ.has_key('CC'):
155 cc = os.environ['CC']
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000156 if os.environ.has_key('CXX'):
157 cxx = os.environ['CXX']
Anthony Baxter22dcf662004-10-13 15:54:17 +0000158 if os.environ.has_key('LDSHARED'):
159 ldshared = os.environ['LDSHARED']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000160 if os.environ.has_key('CPP'):
161 cpp = os.environ['CPP']
162 else:
163 cpp = cc + " -E" # not always
164 if os.environ.has_key('LDFLAGS'):
165 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
166 if os.environ.has_key('CFLAGS'):
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000167 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000168 ldshared = ldshared + ' ' + os.environ['CFLAGS']
169 if os.environ.has_key('CPPFLAGS'):
170 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000171 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000172 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
173
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000174 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000175 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000176 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000177 compiler=cc_cmd,
178 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000179 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000180 linker_so=ldshared,
181 linker_exe=cc)
182
183 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000184
185
Greg Ward9ddaaa11999-01-06 14:46:06 +0000186def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000187 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000188 if python_build:
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000189 if os.name == "nt":
190 inc_dir = os.path.join(project_base, "PC")
191 else:
192 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000193 else:
194 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000195 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000196 config_h = 'config.h'
197 else:
198 # The name of the config.h file changed in 2.2
199 config_h = 'pyconfig.h'
200 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000201
Greg Ward1190ee31998-12-18 23:46:33 +0000202
Greg Ward9ddaaa11999-01-06 14:46:06 +0000203def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000204 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000205 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000206 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000207 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
208 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000209
Greg Ward1190ee31998-12-18 23:46:33 +0000210
Greg Ward9ddaaa11999-01-06 14:46:06 +0000211def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000212 """Parse a config.h-style file.
213
214 A dictionary containing name/value pairs is returned. If an
215 optional dictionary is passed in as the second argument, it is
216 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000217 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000218 if g is None:
219 g = {}
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000220 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
221 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000222 #
Greg Ward1190ee31998-12-18 23:46:33 +0000223 while 1:
224 line = fp.readline()
225 if not line:
226 break
227 m = define_rx.match(line)
228 if m:
229 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000230 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000231 except ValueError: pass
232 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000233 else:
234 m = undef_rx.match(line)
235 if m:
236 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000237 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000238
Greg Wardd283ce72000-09-17 00:53:02 +0000239
240# Regexes needed for parsing Makefile (and similar syntaxes,
241# like old-style Setup files).
242_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
243_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
244_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
245
Greg Ward3fff8d22000-09-15 00:03:13 +0000246def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000247 """Parse a Makefile-style file.
248
249 A dictionary containing name/value pairs is returned. If an
250 optional dictionary is passed in as the second argument, it is
251 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000252 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000253 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000254 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000255
Greg Ward9ddaaa11999-01-06 14:46:06 +0000256 if g is None:
257 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000258 done = {}
259 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000260
Greg Ward1190ee31998-12-18 23:46:33 +0000261 while 1:
262 line = fp.readline()
Greg Wardd283ce72000-09-17 00:53:02 +0000263 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000264 break
Greg Wardd283ce72000-09-17 00:53:02 +0000265 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000266 if m:
267 n, v = m.group(1, 2)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000268 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000269 if "$" in v:
270 notdone[n] = v
271 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000272 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000273 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000274 done[n] = v
275
276 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000277 while notdone:
278 for name in notdone.keys():
279 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000280 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000281 if m:
282 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000283 found = True
Greg Ward1190ee31998-12-18 23:46:33 +0000284 if done.has_key(n):
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000285 item = str(done[n])
Greg Ward1190ee31998-12-18 23:46:33 +0000286 elif notdone.has_key(n):
287 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000288 found = False
289 elif os.environ.has_key(n):
290 # do it like make: fall back to environment
291 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000292 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000293 done[n] = item = ""
294 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000295 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000296 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000297 if "$" in after:
298 notdone[name] = value
299 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000300 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000301 except ValueError:
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000302 done[name] = string.strip(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000303 else:
304 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000305 del notdone[name]
306 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000307 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000308 del notdone[name]
309
Greg Wardd283ce72000-09-17 00:53:02 +0000310 fp.close()
311
Greg Ward1190ee31998-12-18 23:46:33 +0000312 # save the results in the global dictionary
313 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000314 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000315
316
Greg Wardd283ce72000-09-17 00:53:02 +0000317def expand_makefile_vars(s, vars):
318 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
319 'string' according to 'vars' (a dictionary mapping variable names to
320 values). Variables not present in 'vars' are silently expanded to the
321 empty string. The variable values in 'vars' should not contain further
322 variable expansions; if 'vars' is the output of 'parse_makefile()',
323 you're fine. Returns a variable-expanded version of 's'.
324 """
325
326 # This algorithm does multiple expansion, so if vars['foo'] contains
327 # "${bar}", it will expand ${foo} to ${bar}, and then expand
328 # ${bar}... and so forth. This is fine as long as 'vars' comes from
329 # 'parse_makefile()', which takes care of such expansions eagerly,
330 # according to make's variable expansion semantics.
331
332 while 1:
333 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
334 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000335 (beg, end) = m.span()
336 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
337 else:
338 break
339 return s
340
341
Greg Ward879f0f12000-09-15 01:15:08 +0000342_config_vars = None
343
Greg Ward9ddaaa11999-01-06 14:46:06 +0000344def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000345 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000346 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000347 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000348 try:
349 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000350 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000351 except IOError, msg:
352 my_msg = "invalid Python installation: unable to open %s" % filename
353 if hasattr(msg, "strerror"):
354 my_msg = my_msg + " (%s)" % msg.strerror
355
Fred Drake70b014d2001-07-18 18:39:56 +0000356 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000357
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000358 # load the installed pyconfig.h:
359 try:
360 filename = get_config_h_filename()
361 parse_config_h(file(filename), g)
362 except IOError, msg:
363 my_msg = "invalid Python installation: unable to open %s" % filename
364 if hasattr(msg, "strerror"):
365 my_msg = my_msg + " (%s)" % msg.strerror
366
367 raise DistutilsPlatformError(my_msg)
368
Jack Jansen6b08a402004-06-03 12:41:45 +0000369 # On MacOSX we need to check the setting of the environment variable
370 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
371 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000372 # If it isn't set we set it to the configure-time value
Ronald Oussoren988117f2006-04-29 11:31:35 +0000373 if sys.platform == 'darwin' and g.has_key('MACOSX_DEPLOYMENT_TARGET'):
374 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000375 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000376 if cur_target == '':
377 cur_target = cfg_target
378 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Ronald Oussoren59075eb2006-04-17 14:43:30 +0000379 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000380 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
381 % (cur_target, cfg_target))
382 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000383
Greg Ward4f880282000-06-27 01:59:06 +0000384 # On AIX, there are wrong paths to the linker scripts in the Makefile
385 # -- these paths are relative to the Python source, but when installed
386 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000387 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000388 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000389
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000390 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000391 # The following two branches are for 1.5.2 compatibility.
392 if sys.platform == 'aix4': # what about AIX 3.x ?
393 # Linker script is in the config directory, not in Modules as the
394 # Makefile says.
395 python_lib = get_python_lib(standard_lib=1)
396 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
397 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000398
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000399 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
400
401 elif sys.platform == 'beos':
402 # Linker script is in the config directory. In the Makefile it is
403 # relative to the srcdir, which after installation no longer makes
404 # sense.
405 python_lib = get_python_lib(standard_lib=1)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000406 linkerscript_path = string.split(g['LDSHARED'])[0]
407 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000408 linkerscript = os.path.join(python_lib, 'config',
409 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000410
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000411 # XXX this isn't the right place to do this: adding the Python
412 # library to the link, if needed, should be in the "build_ext"
413 # command. (It's also needed for non-MS compilers on Windows, and
414 # it's taken care of for them by the 'build_ext.get_libraries()'
415 # method.)
416 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000417 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000418
Greg Ward879f0f12000-09-15 01:15:08 +0000419 global _config_vars
420 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000421
Greg Ward9ddaaa11999-01-06 14:46:06 +0000422
Greg Ward4d74d731999-06-08 01:58:36 +0000423def _init_nt():
424 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000425 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000426 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000427 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
428 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000429
Greg Ward32162e81999-08-29 18:22:13 +0000430 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000431 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000432
Fred Drake69e2c6e2000-02-08 15:55:42 +0000433 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000434 g['EXE'] = ".exe"
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000435 g['VERSION'] = get_python_version().replace(".", "")
436 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000437
438 global _config_vars
439 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000440
Fred Drake69e2c6e2000-02-08 15:55:42 +0000441
Greg Ward0eff87a2000-03-07 03:30:09 +0000442def _init_mac():
443 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000444 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000445 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000446 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
447 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000448
449 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000450 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000451
Jack Jansendd13a202001-05-17 12:52:01 +0000452 import MacOS
453 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000454 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000455 else:
456 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000457
458 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000459 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
460 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000461
Jack Jansenab5320b2002-06-26 15:42:49 +0000462 # These are used by the extension module build
463 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000464 global _config_vars
465 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000466
Fred Drake69e2c6e2000-02-08 15:55:42 +0000467
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000468def _init_os2():
469 """Initialize the module as appropriate for OS/2"""
470 g = {}
471 # set basic install directories
472 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
473 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
474
475 # XXX hmmm.. a normal install puts include files here
476 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
477
478 g['SO'] = '.pyd'
479 g['EXE'] = ".exe"
480
481 global _config_vars
482 _config_vars = g
483
484
Greg Ward879f0f12000-09-15 01:15:08 +0000485def get_config_vars(*args):
486 """With no arguments, return a dictionary of all configuration
487 variables relevant for the current platform. Generally this includes
488 everything needed to build extensions and install both pure modules and
489 extensions. On Unix, this means every variable defined in Python's
490 installed Makefile; on Windows and Mac OS it's a much smaller set.
491
492 With arguments, return a list of values that result from looking up
493 each argument in the configuration variable dictionary.
494 """
495 global _config_vars
496 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000497 func = globals().get("_init_" + os.name)
498 if func:
499 func()
500 else:
501 _config_vars = {}
502
503 # Normalized versions of prefix and exec_prefix are handy to have;
504 # in fact, these are the standard versions used most places in the
505 # Distutils.
506 _config_vars['prefix'] = PREFIX
507 _config_vars['exec_prefix'] = EXEC_PREFIX
508
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000509 if sys.platform == 'darwin':
510 kernel_version = os.uname()[2] # Kernel version (8.4.3)
511 major_version = int(kernel_version.split('.')[0])
512
513 if major_version < 8:
514 # On Mac OS X before 10.4, check if -arch and -isysroot
515 # are in CFLAGS or LDFLAGS and remove them if they are.
516 # This is needed when building extensions on a 10.3 system
517 # using a universal build of python.
Ronald Oussorend6103692006-10-08 17:49:52 +0000518 for key in ('LDFLAGS', 'BASECFLAGS',
519 # a number of derived variables. These need to be
520 # patched up as well.
521 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000522 flags = _config_vars[key]
523 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Ronald Oussoren7b9053a2006-06-27 10:08:25 +0000524 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000525 _config_vars[key] = flags
526
Greg Ward879f0f12000-09-15 01:15:08 +0000527 if args:
528 vals = []
529 for name in args:
530 vals.append(_config_vars.get(name))
531 return vals
532 else:
533 return _config_vars
534
535def get_config_var(name):
536 """Return the value of a single variable using the dictionary
537 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000538 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000539 """
540 return get_config_vars().get(name)