blob: ea2e0597788ec9c2940f15f2b449dec2cdd59dc2 [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
Greg Ward9ddaaa11999-01-06 14:46:06 +000016import sys
Greg Ward1190ee31998-12-18 23:46:33 +000017
Guido van Rossum45aecf42006-03-15 04:58:47 +000018from .errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000019
Greg Ward879f0f12000-09-15 01:15:08 +000020# These are needed in a couple of spots, so just compute them once.
Greg Wardcf6bea32000-04-10 01:15:06 +000021PREFIX = os.path.normpath(sys.prefix)
22EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
Fred Drakec1ee39a2000-03-09 15:54:52 +000023
Fred Drake16c8d702002-06-04 15:28:21 +000024# python_build: (Boolean) if true, we're either building Python or
25# building an extension with an un-installed Python, so we use
26# different (hard-wired) directories.
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000027
Fred Drake16c8d702002-06-04 15:28:21 +000028argv0_path = os.path.dirname(os.path.abspath(sys.executable))
29landmark = os.path.join(argv0_path, "Modules", "Setup")
Michael W. Hudson6b7d69d2002-07-12 09:16:44 +000030
31python_build = os.path.isfile(landmark)
32
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033del landmark
Fred Drakec1ee39a2000-03-09 15:54:52 +000034
Fred Drakec916cdc2001-08-02 20:03:12 +000035
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000036def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000037 """Return a string containing the major and minor Python version,
38 leaving off the patchlevel. Sample return values could be '1.5'
39 or '2.2'.
40 """
41 return sys.version[:3]
42
43
Greg Wardd38e6f72000-04-10 01:17:49 +000044def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000045 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000046
47 If 'plat_specific' is false (the default), this is the path to the
48 non-platform-specific header files, i.e. Python.h and so on;
49 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000050 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000051
Greg Wardd38e6f72000-04-10 01:17:49 +000052 If 'prefix' is supplied, use it instead of sys.prefix or
53 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000054 """
Greg Wardd38e6f72000-04-10 01:17:49 +000055 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000056 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000057 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000058 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +000059 base = os.path.dirname(os.path.abspath(sys.executable))
60 if plat_specific:
61 inc_dir = base
62 else:
63 inc_dir = os.path.join(base, "Include")
64 if not os.path.exists(inc_dir):
65 inc_dir = os.path.join(os.path.dirname(base), "Include")
66 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000067 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000068 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000069 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000070 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000071 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000072 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000073 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000074 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000075 elif os.name == "os2":
76 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000077 else:
Fred Drake70b014d2001-07-18 18:39:56 +000078 raise DistutilsPlatformError(
79 "I don't know where Python installs its C header files "
80 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000081
82
Greg Wardd38e6f72000-04-10 01:17:49 +000083def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000084 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000085 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000086
Fred Drakec1ee39a2000-03-09 15:54:52 +000087 If 'plat_specific' is true, return the directory containing
88 platform-specific modules, i.e. any module from a non-pure-Python
89 module distribution; otherwise, return the platform-shared library
90 directory. If 'standard_lib' is true, return the directory
91 containing standard Python library modules; otherwise, return the
92 directory for site-specific modules.
93
Greg Wardd38e6f72000-04-10 01:17:49 +000094 If 'prefix' is supplied, use it instead of sys.prefix or
95 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000096 """
Greg Wardd38e6f72000-04-10 01:17:49 +000097 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000098 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +000099
Greg Ward7d73b9e2000-03-09 03:16:05 +0000100 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000101 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000102 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000103 if standard_lib:
104 return libpython
105 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000106 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000107
108 elif os.name == "nt":
109 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000110 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000111 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000112 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000113 return prefix
114 else:
115 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000116
117 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000118 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000119 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000120 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000121 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000122 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000123 else:
124 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000125 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000126 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000127 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000128
129 elif os.name == "os2":
130 if standard_lib:
131 return os.path.join(PREFIX, "Lib")
132 else:
133 return os.path.join(PREFIX, "Lib", "site-packages")
134
Greg Ward7d73b9e2000-03-09 03:16:05 +0000135 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000136 raise DistutilsPlatformError(
137 "I don't know where Python installs its library "
138 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000139
Greg Ward7d73b9e2000-03-09 03:16:05 +0000140
Fred Drake70b014d2001-07-18 18:39:56 +0000141def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000142 """Do any platform-specific customization of a CCompiler instance.
143
144 Mainly needed on Unix, so we can plug in the information that
145 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000146 """
147 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000148 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000149 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000150 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000151
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000152 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000153 cc = os.environ['CC']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000154 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000155 cxx = os.environ['CXX']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000156 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000157 ldshared = os.environ['LDSHARED']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000158 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000159 cpp = os.environ['CPP']
160 else:
161 cpp = cc + " -E" # not always
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000162 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000163 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000164 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000165 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000166 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000167 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000168 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000169 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000170 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
171
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000172 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000173 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000174 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000175 compiler=cc_cmd,
176 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000177 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000178 linker_so=ldshared,
179 linker_exe=cc)
180
181 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000182
183
Greg Ward9ddaaa11999-01-06 14:46:06 +0000184def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000185 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000186 if python_build:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000187 inc_dir = argv0_path
Fred Drakec916cdc2001-08-02 20:03:12 +0000188 else:
189 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000190 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000191 config_h = 'config.h'
192 else:
193 # The name of the config.h file changed in 2.2
194 config_h = 'pyconfig.h'
195 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000196
Greg Ward1190ee31998-12-18 23:46:33 +0000197
Greg Ward9ddaaa11999-01-06 14:46:06 +0000198def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000199 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000200 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000201 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000202 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
203 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000204
Greg Ward1190ee31998-12-18 23:46:33 +0000205
Greg Ward9ddaaa11999-01-06 14:46:06 +0000206def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000207 """Parse a config.h-style file.
208
209 A dictionary containing name/value pairs is returned. If an
210 optional dictionary is passed in as the second argument, it is
211 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000212 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000213 if g is None:
214 g = {}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
216 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000217 #
Greg Ward1190ee31998-12-18 23:46:33 +0000218 while 1:
219 line = fp.readline()
220 if not line:
221 break
222 m = define_rx.match(line)
223 if m:
224 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000225 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000226 except ValueError: pass
227 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000228 else:
229 m = undef_rx.match(line)
230 if m:
231 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000232 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000233
Greg Wardd283ce72000-09-17 00:53:02 +0000234
235# Regexes needed for parsing Makefile (and similar syntaxes,
236# like old-style Setup files).
237_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
238_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
239_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
240
Greg Ward3fff8d22000-09-15 00:03:13 +0000241def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000242 """Parse a Makefile-style file.
243
244 A dictionary containing name/value pairs is returned. If an
245 optional dictionary is passed in as the second argument, it is
246 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000247 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000248 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000249 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000250
Greg Ward9ddaaa11999-01-06 14:46:06 +0000251 if g is None:
252 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000253 done = {}
254 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000255
Greg Ward1190ee31998-12-18 23:46:33 +0000256 while 1:
257 line = fp.readline()
Greg Wardd283ce72000-09-17 00:53:02 +0000258 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000259 break
Greg Wardd283ce72000-09-17 00:53:02 +0000260 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000261 if m:
262 n, v = m.group(1, 2)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000263 v = v.strip()
Greg Ward1190ee31998-12-18 23:46:33 +0000264 if "$" in v:
265 notdone[n] = v
266 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000267 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000268 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000269 done[n] = v
270
271 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000272 while notdone:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000273 for name in list(notdone):
Greg Ward1190ee31998-12-18 23:46:33 +0000274 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000275 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000276 if m:
277 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000278 found = True
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000279 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000280 item = str(done[n])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000281 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000282 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000283 found = False
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000284 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000285 # do it like make: fall back to environment
286 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000287 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000288 done[n] = item = ""
289 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000290 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000291 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000292 if "$" in after:
293 notdone[name] = value
294 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000295 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000296 except ValueError:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000297 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000298 else:
299 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000300 del notdone[name]
301 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000302 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000303 del notdone[name]
304
Greg Wardd283ce72000-09-17 00:53:02 +0000305 fp.close()
306
Greg Ward1190ee31998-12-18 23:46:33 +0000307 # save the results in the global dictionary
308 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000309 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000310
311
Greg Wardd283ce72000-09-17 00:53:02 +0000312def expand_makefile_vars(s, vars):
313 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
314 'string' according to 'vars' (a dictionary mapping variable names to
315 values). Variables not present in 'vars' are silently expanded to the
316 empty string. The variable values in 'vars' should not contain further
317 variable expansions; if 'vars' is the output of 'parse_makefile()',
318 you're fine. Returns a variable-expanded version of 's'.
319 """
320
321 # This algorithm does multiple expansion, so if vars['foo'] contains
322 # "${bar}", it will expand ${foo} to ${bar}, and then expand
323 # ${bar}... and so forth. This is fine as long as 'vars' comes from
324 # 'parse_makefile()', which takes care of such expansions eagerly,
325 # according to make's variable expansion semantics.
326
327 while 1:
328 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
329 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000330 (beg, end) = m.span()
331 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
332 else:
333 break
334 return s
335
336
Greg Ward879f0f12000-09-15 01:15:08 +0000337_config_vars = None
338
Greg Ward9ddaaa11999-01-06 14:46:06 +0000339def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000340 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000341 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000342 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000343 try:
344 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000345 parse_makefile(filename, g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000346 except IOError as msg:
Greg Warda570c052000-05-23 23:14:00 +0000347 my_msg = "invalid Python installation: unable to open %s" % filename
348 if hasattr(msg, "strerror"):
349 my_msg = my_msg + " (%s)" % msg.strerror
350
Fred Drake70b014d2001-07-18 18:39:56 +0000351 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000352
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353 # load the installed pyconfig.h:
354 try:
355 filename = get_config_h_filename()
Alex Martelli01c77c62006-08-24 02:58:11 +0000356 parse_config_h(open(filename), g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000357 except IOError as msg:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358 my_msg = "invalid Python installation: unable to open %s" % filename
359 if hasattr(msg, "strerror"):
360 my_msg = my_msg + " (%s)" % msg.strerror
361
362 raise DistutilsPlatformError(my_msg)
363
Jack Jansen6b08a402004-06-03 12:41:45 +0000364 # On MacOSX we need to check the setting of the environment variable
365 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
366 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000367 # If it isn't set we set it to the configure-time value
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000368 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000370 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000371 if cur_target == '':
372 cur_target = cfg_target
373 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000375 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
376 % (cur_target, cfg_target))
377 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000378
Greg Ward4f880282000-06-27 01:59:06 +0000379 # On AIX, there are wrong paths to the linker scripts in the Makefile
380 # -- these paths are relative to the Python source, but when installed
381 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000382 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000383 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000384
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000385 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000386 # The following two branches are for 1.5.2 compatibility.
387 if sys.platform == 'aix4': # what about AIX 3.x ?
388 # Linker script is in the config directory, not in Modules as the
389 # Makefile says.
390 python_lib = get_python_lib(standard_lib=1)
391 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
392 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000393
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000394 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
395
396 elif sys.platform == 'beos':
397 # Linker script is in the config directory. In the Makefile it is
398 # relative to the srcdir, which after installation no longer makes
399 # sense.
400 python_lib = get_python_lib(standard_lib=1)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000401 linkerscript_path = g['LDSHARED'].split()[0]
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000402 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000403 linkerscript = os.path.join(python_lib, 'config',
404 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000405
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000406 # XXX this isn't the right place to do this: adding the Python
407 # library to the link, if needed, should be in the "build_ext"
408 # command. (It's also needed for non-MS compilers on Windows, and
409 # it's taken care of for them by the 'build_ext.get_libraries()'
410 # method.)
411 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000412 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000413
Greg Ward879f0f12000-09-15 01:15:08 +0000414 global _config_vars
415 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000416
Greg Ward9ddaaa11999-01-06 14:46:06 +0000417
Greg Ward4d74d731999-06-08 01:58:36 +0000418def _init_nt():
419 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000420 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000421 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000422 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
423 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000424
Greg Ward32162e81999-08-29 18:22:13 +0000425 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000426 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000427
Fred Drake69e2c6e2000-02-08 15:55:42 +0000428 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000429 g['EXE'] = ".exe"
Greg Ward879f0f12000-09-15 01:15:08 +0000430
431 global _config_vars
432 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000433
Fred Drake69e2c6e2000-02-08 15:55:42 +0000434
Greg Ward0eff87a2000-03-07 03:30:09 +0000435def _init_mac():
436 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000437 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000438 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000439 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
440 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000441
442 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000443 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000444
Jack Jansendd13a202001-05-17 12:52:01 +0000445 import MacOS
446 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000447 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000448 else:
449 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000450
451 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000452 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
453 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000454
Jack Jansenab5320b2002-06-26 15:42:49 +0000455 # These are used by the extension module build
456 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000457 global _config_vars
458 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000459
Fred Drake69e2c6e2000-02-08 15:55:42 +0000460
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000461def _init_os2():
462 """Initialize the module as appropriate for OS/2"""
463 g = {}
464 # set basic install directories
465 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
466 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
467
468 # XXX hmmm.. a normal install puts include files here
469 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
470
471 g['SO'] = '.pyd'
472 g['EXE'] = ".exe"
473
474 global _config_vars
475 _config_vars = g
476
477
Greg Ward879f0f12000-09-15 01:15:08 +0000478def get_config_vars(*args):
479 """With no arguments, return a dictionary of all configuration
480 variables relevant for the current platform. Generally this includes
481 everything needed to build extensions and install both pure modules and
482 extensions. On Unix, this means every variable defined in Python's
483 installed Makefile; on Windows and Mac OS it's a much smaller set.
484
485 With arguments, return a list of values that result from looking up
486 each argument in the configuration variable dictionary.
487 """
488 global _config_vars
489 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000490 func = globals().get("_init_" + os.name)
491 if func:
492 func()
493 else:
494 _config_vars = {}
495
496 # Normalized versions of prefix and exec_prefix are handy to have;
497 # in fact, these are the standard versions used most places in the
498 # Distutils.
499 _config_vars['prefix'] = PREFIX
500 _config_vars['exec_prefix'] = EXEC_PREFIX
501
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502 if sys.platform == 'darwin':
503 kernel_version = os.uname()[2] # Kernel version (8.4.3)
504 major_version = int(kernel_version.split('.')[0])
505
506 if major_version < 8:
507 # On Mac OS X before 10.4, check if -arch and -isysroot
508 # are in CFLAGS or LDFLAGS and remove them if they are.
509 # This is needed when building extensions on a 10.3 system
510 # using a universal build of python.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000511 for key in ('LDFLAGS', 'BASECFLAGS',
512 # a number of derived variables. These need to be
513 # patched up as well.
514 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000515 flags = _config_vars[key]
516 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000518 _config_vars[key] = flags
519
Greg Ward879f0f12000-09-15 01:15:08 +0000520 if args:
521 vals = []
522 for name in args:
523 vals.append(_config_vars.get(name))
524 return vals
525 else:
526 return _config_vars
527
528def get_config_var(name):
529 """Return the value of a single variable using the dictionary
530 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000531 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000532 """
533 return get_config_vars().get(name)