blob: 3cd647becc11b719fb70fff0668a261636ac0b7f [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))
Christian Heimes9a1d8ce2008-01-01 14:37:32 +000030# PC/VS7.1
31if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
32 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
33 os.path.pardir))
Christian Heimesd3fc07a42007-12-06 13:15:13 +000034
Fred Drake16c8d702002-06-04 15:28:21 +000035# python_build: (Boolean) if true, we're either building Python or
36# building an extension with an un-installed Python, so we use
37# different (hard-wired) directories.
Christian Heimesc67a15d2007-12-14 23:42:36 +000038# Setup.local is available for Makefile builds including VPATH builds,
39# Setup.dist is available on Windows
40python_build = any(os.path.isfile(os.path.join(project_base, "Modules", fn))
41 for fn in ("Setup.dist", "Setup.local"))
Fred Drakec1ee39a2000-03-09 15:54:52 +000042
Fred Drakec916cdc2001-08-02 20:03:12 +000043
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000044def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000045 """Return a string containing the major and minor Python version,
46 leaving off the patchlevel. Sample return values could be '1.5'
47 or '2.2'.
48 """
49 return sys.version[:3]
50
51
Greg Wardd38e6f72000-04-10 01:17:49 +000052def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000053 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000054
55 If 'plat_specific' is false (the default), this is the path to the
56 non-platform-specific header files, i.e. Python.h and so on;
57 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000058 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000059
Greg Wardd38e6f72000-04-10 01:17:49 +000060 If 'prefix' is supplied, use it instead of sys.prefix or
61 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000062 """
Greg Wardd38e6f72000-04-10 01:17:49 +000063 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000064 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000065 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000066 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +000067 base = os.path.dirname(os.path.abspath(sys.executable))
68 if plat_specific:
69 inc_dir = base
70 else:
71 inc_dir = os.path.join(base, "Include")
72 if not os.path.exists(inc_dir):
73 inc_dir = os.path.join(os.path.dirname(base), "Include")
74 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000075 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000076 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000077 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000078 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000079 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000080 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000081 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000082 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000083 elif os.name == "os2":
84 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000085 else:
Fred Drake70b014d2001-07-18 18:39:56 +000086 raise DistutilsPlatformError(
87 "I don't know where Python installs its C header files "
88 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000089
90
Greg Wardd38e6f72000-04-10 01:17:49 +000091def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000092 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000093 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000094
Fred Drakec1ee39a2000-03-09 15:54:52 +000095 If 'plat_specific' is true, return the directory containing
96 platform-specific modules, i.e. any module from a non-pure-Python
97 module distribution; otherwise, return the platform-shared library
98 directory. If 'standard_lib' is true, return the directory
99 containing standard Python library modules; otherwise, return the
100 directory for site-specific modules.
101
Greg Wardd38e6f72000-04-10 01:17:49 +0000102 If 'prefix' is supplied, use it instead of sys.prefix or
103 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000104 """
Greg Wardd38e6f72000-04-10 01:17:49 +0000105 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000106 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000107
Greg Ward7d73b9e2000-03-09 03:16:05 +0000108 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000109 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000110 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000111 if standard_lib:
112 return libpython
113 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000114 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000115
116 elif os.name == "nt":
117 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000118 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000119 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000120 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000121 return prefix
122 else:
123 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000124
125 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000126 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000127 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000128 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000129 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000130 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000131 else:
132 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000133 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000134 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000135 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000136
137 elif os.name == "os2":
138 if standard_lib:
139 return os.path.join(PREFIX, "Lib")
140 else:
141 return os.path.join(PREFIX, "Lib", "site-packages")
142
Greg Ward7d73b9e2000-03-09 03:16:05 +0000143 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000144 raise DistutilsPlatformError(
145 "I don't know where Python installs its library "
146 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000147
Greg Ward7d73b9e2000-03-09 03:16:05 +0000148
Fred Drake70b014d2001-07-18 18:39:56 +0000149def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000150 """Do any platform-specific customization of a CCompiler instance.
151
152 Mainly needed on Unix, so we can plug in the information that
153 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000154 """
155 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000156 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000157 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000158 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000159
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000160 if os.environ.has_key('CC'):
161 cc = os.environ['CC']
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000162 if os.environ.has_key('CXX'):
163 cxx = os.environ['CXX']
Anthony Baxter22dcf662004-10-13 15:54:17 +0000164 if os.environ.has_key('LDSHARED'):
165 ldshared = os.environ['LDSHARED']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000166 if os.environ.has_key('CPP'):
167 cpp = os.environ['CPP']
168 else:
169 cpp = cc + " -E" # not always
170 if os.environ.has_key('LDFLAGS'):
171 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
172 if os.environ.has_key('CFLAGS'):
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000173 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000174 ldshared = ldshared + ' ' + os.environ['CFLAGS']
175 if os.environ.has_key('CPPFLAGS'):
176 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000177 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000178 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
179
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000180 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000181 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000182 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000183 compiler=cc_cmd,
184 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000185 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000186 linker_so=ldshared,
187 linker_exe=cc)
188
189 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000190
191
Greg Ward9ddaaa11999-01-06 14:46:06 +0000192def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000193 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000194 if python_build:
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000195 if os.name == "nt":
196 inc_dir = os.path.join(project_base, "PC")
197 else:
198 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000199 else:
200 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000201 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000202 config_h = 'config.h'
203 else:
204 # The name of the config.h file changed in 2.2
205 config_h = 'pyconfig.h'
206 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000207
Greg Ward1190ee31998-12-18 23:46:33 +0000208
Greg Ward9ddaaa11999-01-06 14:46:06 +0000209def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000210 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000211 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000212 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000213 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
214 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000215
Greg Ward1190ee31998-12-18 23:46:33 +0000216
Greg Ward9ddaaa11999-01-06 14:46:06 +0000217def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000218 """Parse a config.h-style file.
219
220 A dictionary containing name/value pairs is returned. If an
221 optional dictionary is passed in as the second argument, it is
222 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000223 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000224 if g is None:
225 g = {}
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000226 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
227 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000228 #
Greg Ward1190ee31998-12-18 23:46:33 +0000229 while 1:
230 line = fp.readline()
231 if not line:
232 break
233 m = define_rx.match(line)
234 if m:
235 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000236 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000237 except ValueError: pass
238 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000239 else:
240 m = undef_rx.match(line)
241 if m:
242 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000243 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000244
Greg Wardd283ce72000-09-17 00:53:02 +0000245
246# Regexes needed for parsing Makefile (and similar syntaxes,
247# like old-style Setup files).
248_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
249_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
250_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
251
Greg Ward3fff8d22000-09-15 00:03:13 +0000252def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000253 """Parse a Makefile-style file.
254
255 A dictionary containing name/value pairs is returned. If an
256 optional dictionary is passed in as the second argument, it is
257 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000258 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000259 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000260 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000261
Greg Ward9ddaaa11999-01-06 14:46:06 +0000262 if g is None:
263 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000264 done = {}
265 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000266
Greg Ward1190ee31998-12-18 23:46:33 +0000267 while 1:
268 line = fp.readline()
Greg Wardd283ce72000-09-17 00:53:02 +0000269 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000270 break
Greg Wardd283ce72000-09-17 00:53:02 +0000271 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000272 if m:
273 n, v = m.group(1, 2)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000274 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000275 if "$" in v:
276 notdone[n] = v
277 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000278 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000279 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000280 done[n] = v
281
282 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000283 while notdone:
284 for name in notdone.keys():
285 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000286 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000287 if m:
288 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000289 found = True
Greg Ward1190ee31998-12-18 23:46:33 +0000290 if done.has_key(n):
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000291 item = str(done[n])
Greg Ward1190ee31998-12-18 23:46:33 +0000292 elif notdone.has_key(n):
293 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000294 found = False
295 elif os.environ.has_key(n):
296 # do it like make: fall back to environment
297 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000298 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000299 done[n] = item = ""
300 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000301 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000302 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000303 if "$" in after:
304 notdone[name] = value
305 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000306 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000307 except ValueError:
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000308 done[name] = string.strip(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000309 else:
310 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000311 del notdone[name]
312 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000313 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000314 del notdone[name]
315
Greg Wardd283ce72000-09-17 00:53:02 +0000316 fp.close()
317
Greg Ward1190ee31998-12-18 23:46:33 +0000318 # save the results in the global dictionary
319 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000320 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000321
322
Greg Wardd283ce72000-09-17 00:53:02 +0000323def expand_makefile_vars(s, vars):
324 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
325 'string' according to 'vars' (a dictionary mapping variable names to
326 values). Variables not present in 'vars' are silently expanded to the
327 empty string. The variable values in 'vars' should not contain further
328 variable expansions; if 'vars' is the output of 'parse_makefile()',
329 you're fine. Returns a variable-expanded version of 's'.
330 """
331
332 # This algorithm does multiple expansion, so if vars['foo'] contains
333 # "${bar}", it will expand ${foo} to ${bar}, and then expand
334 # ${bar}... and so forth. This is fine as long as 'vars' comes from
335 # 'parse_makefile()', which takes care of such expansions eagerly,
336 # according to make's variable expansion semantics.
337
338 while 1:
339 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
340 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000341 (beg, end) = m.span()
342 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
343 else:
344 break
345 return s
346
347
Greg Ward879f0f12000-09-15 01:15:08 +0000348_config_vars = None
349
Greg Ward9ddaaa11999-01-06 14:46:06 +0000350def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000351 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000352 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000353 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000354 try:
355 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000356 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000357 except IOError, msg:
358 my_msg = "invalid Python installation: unable to open %s" % filename
359 if hasattr(msg, "strerror"):
360 my_msg = my_msg + " (%s)" % msg.strerror
361
Fred Drake70b014d2001-07-18 18:39:56 +0000362 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000363
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000364 # load the installed pyconfig.h:
365 try:
366 filename = get_config_h_filename()
367 parse_config_h(file(filename), g)
368 except IOError, msg:
369 my_msg = "invalid Python installation: unable to open %s" % filename
370 if hasattr(msg, "strerror"):
371 my_msg = my_msg + " (%s)" % msg.strerror
372
373 raise DistutilsPlatformError(my_msg)
374
Jack Jansen6b08a402004-06-03 12:41:45 +0000375 # On MacOSX we need to check the setting of the environment variable
376 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
377 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000378 # If it isn't set we set it to the configure-time value
Ronald Oussoren988117f2006-04-29 11:31:35 +0000379 if sys.platform == 'darwin' and g.has_key('MACOSX_DEPLOYMENT_TARGET'):
380 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000381 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000382 if cur_target == '':
383 cur_target = cfg_target
384 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Ronald Oussoren59075eb2006-04-17 14:43:30 +0000385 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000386 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
387 % (cur_target, cfg_target))
388 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000389
Greg Ward4f880282000-06-27 01:59:06 +0000390 # On AIX, there are wrong paths to the linker scripts in the Makefile
391 # -- these paths are relative to the Python source, but when installed
392 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000393 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000394 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000395
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000396 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000397 # The following two branches are for 1.5.2 compatibility.
398 if sys.platform == 'aix4': # what about AIX 3.x ?
399 # Linker script is in the config directory, not in Modules as the
400 # Makefile says.
401 python_lib = get_python_lib(standard_lib=1)
402 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
403 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000404
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000405 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
406
407 elif sys.platform == 'beos':
408 # Linker script is in the config directory. In the Makefile it is
409 # relative to the srcdir, which after installation no longer makes
410 # sense.
411 python_lib = get_python_lib(standard_lib=1)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000412 linkerscript_path = string.split(g['LDSHARED'])[0]
413 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000414 linkerscript = os.path.join(python_lib, 'config',
415 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000416
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000417 # XXX this isn't the right place to do this: adding the Python
418 # library to the link, if needed, should be in the "build_ext"
419 # command. (It's also needed for non-MS compilers on Windows, and
420 # it's taken care of for them by the 'build_ext.get_libraries()'
421 # method.)
422 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000423 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000424
Greg Ward879f0f12000-09-15 01:15:08 +0000425 global _config_vars
426 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000427
Greg Ward9ddaaa11999-01-06 14:46:06 +0000428
Greg Ward4d74d731999-06-08 01:58:36 +0000429def _init_nt():
430 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000431 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000432 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000433 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
434 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000435
Greg Ward32162e81999-08-29 18:22:13 +0000436 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000437 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000438
Fred Drake69e2c6e2000-02-08 15:55:42 +0000439 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000440 g['EXE'] = ".exe"
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000441 g['VERSION'] = get_python_version().replace(".", "")
442 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000443
444 global _config_vars
445 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000446
Fred Drake69e2c6e2000-02-08 15:55:42 +0000447
Greg Ward0eff87a2000-03-07 03:30:09 +0000448def _init_mac():
449 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000450 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000451 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000452 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
453 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000454
455 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000456 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000457
Jack Jansendd13a202001-05-17 12:52:01 +0000458 import MacOS
459 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000460 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000461 else:
462 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000463
464 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000465 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
466 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000467
Jack Jansenab5320b2002-06-26 15:42:49 +0000468 # These are used by the extension module build
469 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000470 global _config_vars
471 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000472
Fred Drake69e2c6e2000-02-08 15:55:42 +0000473
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000474def _init_os2():
475 """Initialize the module as appropriate for OS/2"""
476 g = {}
477 # set basic install directories
478 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
479 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
480
481 # XXX hmmm.. a normal install puts include files here
482 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
483
484 g['SO'] = '.pyd'
485 g['EXE'] = ".exe"
486
487 global _config_vars
488 _config_vars = g
489
490
Greg Ward879f0f12000-09-15 01:15:08 +0000491def get_config_vars(*args):
492 """With no arguments, return a dictionary of all configuration
493 variables relevant for the current platform. Generally this includes
494 everything needed to build extensions and install both pure modules and
495 extensions. On Unix, this means every variable defined in Python's
496 installed Makefile; on Windows and Mac OS it's a much smaller set.
497
498 With arguments, return a list of values that result from looking up
499 each argument in the configuration variable dictionary.
500 """
501 global _config_vars
502 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000503 func = globals().get("_init_" + os.name)
504 if func:
505 func()
506 else:
507 _config_vars = {}
508
509 # Normalized versions of prefix and exec_prefix are handy to have;
510 # in fact, these are the standard versions used most places in the
511 # Distutils.
512 _config_vars['prefix'] = PREFIX
513 _config_vars['exec_prefix'] = EXEC_PREFIX
514
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000515 if sys.platform == 'darwin':
516 kernel_version = os.uname()[2] # Kernel version (8.4.3)
517 major_version = int(kernel_version.split('.')[0])
518
519 if major_version < 8:
520 # On Mac OS X before 10.4, check if -arch and -isysroot
521 # are in CFLAGS or LDFLAGS and remove them if they are.
522 # This is needed when building extensions on a 10.3 system
523 # using a universal build of python.
Ronald Oussorend6103692006-10-08 17:49:52 +0000524 for key in ('LDFLAGS', 'BASECFLAGS',
525 # a number of derived variables. These need to be
526 # patched up as well.
527 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000528 flags = _config_vars[key]
529 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Ronald Oussoren7b9053a2006-06-27 10:08:25 +0000530 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000531 _config_vars[key] = flags
532
Greg Ward879f0f12000-09-15 01:15:08 +0000533 if args:
534 vals = []
535 for name in args:
536 vals.append(_config_vars.get(name))
537 return vals
538 else:
539 return _config_vars
540
541def get_config_var(name):
542 """Return the value of a single variable using the dictionary
543 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000544 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000545 """
546 return get_config_vars().get(name)