blob: 32b165ffd119d9ef0dda0faadf42a3dedb1e5dee [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 Heimesc67a15d2007-12-14 23:42:36 +000034# Setup.local is available for Makefile builds including VPATH builds,
35# Setup.dist is available on Windows
36python_build = any(os.path.isfile(os.path.join(project_base, "Modules", fn))
37 for fn in ("Setup.dist", "Setup.local"))
Fred Drakec1ee39a2000-03-09 15:54:52 +000038
Fred Drakec916cdc2001-08-02 20:03:12 +000039
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000040def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000041 """Return a string containing the major and minor Python version,
42 leaving off the patchlevel. Sample return values could be '1.5'
43 or '2.2'.
44 """
45 return sys.version[:3]
46
47
Greg Wardd38e6f72000-04-10 01:17:49 +000048def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000049 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000050
51 If 'plat_specific' is false (the default), this is the path to the
52 non-platform-specific header files, i.e. Python.h and so on;
53 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000054 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000055
Greg Wardd38e6f72000-04-10 01:17:49 +000056 If 'prefix' is supplied, use it instead of sys.prefix or
57 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000058 """
Greg Wardd38e6f72000-04-10 01:17:49 +000059 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000060 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000061 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000062 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +000063 base = os.path.dirname(os.path.abspath(sys.executable))
64 if plat_specific:
65 inc_dir = base
66 else:
67 inc_dir = os.path.join(base, "Include")
68 if not os.path.exists(inc_dir):
69 inc_dir = os.path.join(os.path.dirname(base), "Include")
70 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000071 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000072 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000073 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000074 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000075 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000076 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000077 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000078 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000079 elif os.name == "os2":
80 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000081 else:
Fred Drake70b014d2001-07-18 18:39:56 +000082 raise DistutilsPlatformError(
83 "I don't know where Python installs its C header files "
84 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000085
86
Greg Wardd38e6f72000-04-10 01:17:49 +000087def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000088 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000089 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000090
Fred Drakec1ee39a2000-03-09 15:54:52 +000091 If 'plat_specific' is true, return the directory containing
92 platform-specific modules, i.e. any module from a non-pure-Python
93 module distribution; otherwise, return the platform-shared library
94 directory. If 'standard_lib' is true, return the directory
95 containing standard Python library modules; otherwise, return the
96 directory for site-specific modules.
97
Greg Wardd38e6f72000-04-10 01:17:49 +000098 If 'prefix' is supplied, use it instead of sys.prefix or
99 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000100 """
Greg Wardd38e6f72000-04-10 01:17:49 +0000101 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +0000102 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +0000103
Greg Ward7d73b9e2000-03-09 03:16:05 +0000104 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +0000105 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +0000106 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000107 if standard_lib:
108 return libpython
109 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000110 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000111
112 elif os.name == "nt":
113 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000114 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000115 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000116 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000117 return prefix
118 else:
119 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000120
121 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000122 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000123 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000124 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000125 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000126 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000127 else:
128 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000129 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000130 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000131 return os.path.join(prefix, "Lib", "site-packages")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000132
133 elif os.name == "os2":
134 if standard_lib:
135 return os.path.join(PREFIX, "Lib")
136 else:
137 return os.path.join(PREFIX, "Lib", "site-packages")
138
Greg Ward7d73b9e2000-03-09 03:16:05 +0000139 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000140 raise DistutilsPlatformError(
141 "I don't know where Python installs its library "
142 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000143
Greg Ward7d73b9e2000-03-09 03:16:05 +0000144
Fred Drake70b014d2001-07-18 18:39:56 +0000145def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000146 """Do any platform-specific customization of a CCompiler instance.
147
148 Mainly needed on Unix, so we can plug in the information that
149 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000150 """
151 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000152 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000153 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000154 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000155
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000156 if os.environ.has_key('CC'):
157 cc = os.environ['CC']
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000158 if os.environ.has_key('CXX'):
159 cxx = os.environ['CXX']
Anthony Baxter22dcf662004-10-13 15:54:17 +0000160 if os.environ.has_key('LDSHARED'):
161 ldshared = os.environ['LDSHARED']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000162 if os.environ.has_key('CPP'):
163 cpp = os.environ['CPP']
164 else:
165 cpp = cc + " -E" # not always
166 if os.environ.has_key('LDFLAGS'):
167 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
168 if os.environ.has_key('CFLAGS'):
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000169 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000170 ldshared = ldshared + ' ' + os.environ['CFLAGS']
171 if os.environ.has_key('CPPFLAGS'):
172 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000173 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000174 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
175
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000176 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000177 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000178 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000179 compiler=cc_cmd,
180 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000181 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000182 linker_so=ldshared,
183 linker_exe=cc)
184
185 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000186
187
Greg Ward9ddaaa11999-01-06 14:46:06 +0000188def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000189 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000190 if python_build:
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000191 if os.name == "nt":
192 inc_dir = os.path.join(project_base, "PC")
193 else:
194 inc_dir = project_base
Fred Drakec916cdc2001-08-02 20:03:12 +0000195 else:
196 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000197 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000198 config_h = 'config.h'
199 else:
200 # The name of the config.h file changed in 2.2
201 config_h = 'pyconfig.h'
202 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000203
Greg Ward1190ee31998-12-18 23:46:33 +0000204
Greg Ward9ddaaa11999-01-06 14:46:06 +0000205def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000206 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000207 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000208 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000209 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
210 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000211
Greg Ward1190ee31998-12-18 23:46:33 +0000212
Greg Ward9ddaaa11999-01-06 14:46:06 +0000213def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000214 """Parse a config.h-style file.
215
216 A dictionary containing name/value pairs is returned. If an
217 optional dictionary is passed in as the second argument, it is
218 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000219 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000220 if g is None:
221 g = {}
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000222 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
223 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000224 #
Greg Ward1190ee31998-12-18 23:46:33 +0000225 while 1:
226 line = fp.readline()
227 if not line:
228 break
229 m = define_rx.match(line)
230 if m:
231 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000232 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000233 except ValueError: pass
234 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000235 else:
236 m = undef_rx.match(line)
237 if m:
238 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000239 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000240
Greg Wardd283ce72000-09-17 00:53:02 +0000241
242# Regexes needed for parsing Makefile (and similar syntaxes,
243# like old-style Setup files).
244_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
245_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
246_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
247
Greg Ward3fff8d22000-09-15 00:03:13 +0000248def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000249 """Parse a Makefile-style file.
250
251 A dictionary containing name/value pairs is returned. If an
252 optional dictionary is passed in as the second argument, it is
253 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000254 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000255 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000256 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000257
Greg Ward9ddaaa11999-01-06 14:46:06 +0000258 if g is None:
259 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000260 done = {}
261 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000262
Greg Ward1190ee31998-12-18 23:46:33 +0000263 while 1:
264 line = fp.readline()
Greg Wardd283ce72000-09-17 00:53:02 +0000265 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000266 break
Greg Wardd283ce72000-09-17 00:53:02 +0000267 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000268 if m:
269 n, v = m.group(1, 2)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000270 v = string.strip(v)
Greg Ward1190ee31998-12-18 23:46:33 +0000271 if "$" in v:
272 notdone[n] = v
273 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000274 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000275 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000276 done[n] = v
277
278 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000279 while notdone:
280 for name in notdone.keys():
281 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000282 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000283 if m:
284 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000285 found = True
Greg Ward1190ee31998-12-18 23:46:33 +0000286 if done.has_key(n):
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000287 item = str(done[n])
Greg Ward1190ee31998-12-18 23:46:33 +0000288 elif notdone.has_key(n):
289 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000290 found = False
291 elif os.environ.has_key(n):
292 # do it like make: fall back to environment
293 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000294 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000295 done[n] = item = ""
296 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000297 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000298 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000299 if "$" in after:
300 notdone[name] = value
301 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000302 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000303 except ValueError:
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000304 done[name] = string.strip(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000305 else:
306 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000307 del notdone[name]
308 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000309 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000310 del notdone[name]
311
Greg Wardd283ce72000-09-17 00:53:02 +0000312 fp.close()
313
Greg Ward1190ee31998-12-18 23:46:33 +0000314 # save the results in the global dictionary
315 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000316 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000317
318
Greg Wardd283ce72000-09-17 00:53:02 +0000319def expand_makefile_vars(s, vars):
320 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
321 'string' according to 'vars' (a dictionary mapping variable names to
322 values). Variables not present in 'vars' are silently expanded to the
323 empty string. The variable values in 'vars' should not contain further
324 variable expansions; if 'vars' is the output of 'parse_makefile()',
325 you're fine. Returns a variable-expanded version of 's'.
326 """
327
328 # This algorithm does multiple expansion, so if vars['foo'] contains
329 # "${bar}", it will expand ${foo} to ${bar}, and then expand
330 # ${bar}... and so forth. This is fine as long as 'vars' comes from
331 # 'parse_makefile()', which takes care of such expansions eagerly,
332 # according to make's variable expansion semantics.
333
334 while 1:
335 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
336 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000337 (beg, end) = m.span()
338 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
339 else:
340 break
341 return s
342
343
Greg Ward879f0f12000-09-15 01:15:08 +0000344_config_vars = None
345
Greg Ward9ddaaa11999-01-06 14:46:06 +0000346def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000347 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000348 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000349 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000350 try:
351 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000352 parse_makefile(filename, g)
Greg Warda570c052000-05-23 23:14:00 +0000353 except IOError, msg:
354 my_msg = "invalid Python installation: unable to open %s" % filename
355 if hasattr(msg, "strerror"):
356 my_msg = my_msg + " (%s)" % msg.strerror
357
Fred Drake70b014d2001-07-18 18:39:56 +0000358 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000359
Martin v. Löwis10acfd02006-04-10 12:39:36 +0000360 # load the installed pyconfig.h:
361 try:
362 filename = get_config_h_filename()
363 parse_config_h(file(filename), g)
364 except IOError, msg:
365 my_msg = "invalid Python installation: unable to open %s" % filename
366 if hasattr(msg, "strerror"):
367 my_msg = my_msg + " (%s)" % msg.strerror
368
369 raise DistutilsPlatformError(my_msg)
370
Jack Jansen6b08a402004-06-03 12:41:45 +0000371 # On MacOSX we need to check the setting of the environment variable
372 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
373 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000374 # If it isn't set we set it to the configure-time value
Ronald Oussoren988117f2006-04-29 11:31:35 +0000375 if sys.platform == 'darwin' and g.has_key('MACOSX_DEPLOYMENT_TARGET'):
376 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000377 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000378 if cur_target == '':
379 cur_target = cfg_target
380 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Ronald Oussoren59075eb2006-04-17 14:43:30 +0000381 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
Jack Jansen6b08a402004-06-03 12:41:45 +0000382 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
383 % (cur_target, cfg_target))
384 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000385
Greg Ward4f880282000-06-27 01:59:06 +0000386 # On AIX, there are wrong paths to the linker scripts in the Makefile
387 # -- these paths are relative to the Python source, but when installed
388 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000389 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000390 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000391
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000392 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000393 # The following two branches are for 1.5.2 compatibility.
394 if sys.platform == 'aix4': # what about AIX 3.x ?
395 # Linker script is in the config directory, not in Modules as the
396 # Makefile says.
397 python_lib = get_python_lib(standard_lib=1)
398 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
399 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000400
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000401 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
402
403 elif sys.platform == 'beos':
404 # Linker script is in the config directory. In the Makefile it is
405 # relative to the srcdir, which after installation no longer makes
406 # sense.
407 python_lib = get_python_lib(standard_lib=1)
Andrew M. Kuchling33635aa2002-11-13 17:03:05 +0000408 linkerscript_path = string.split(g['LDSHARED'])[0]
409 linkerscript_name = os.path.basename(linkerscript_path)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000410 linkerscript = os.path.join(python_lib, 'config',
411 linkerscript_name)
Fred Drakeb94b8492001-12-06 20:51:35 +0000412
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000413 # XXX this isn't the right place to do this: adding the Python
414 # library to the link, if needed, should be in the "build_ext"
415 # command. (It's also needed for non-MS compilers on Windows, and
416 # it's taken care of for them by the 'build_ext.get_libraries()'
417 # method.)
418 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000419 (linkerscript, PREFIX, get_python_version()))
Fred Drakeb94b8492001-12-06 20:51:35 +0000420
Greg Ward879f0f12000-09-15 01:15:08 +0000421 global _config_vars
422 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000423
Greg Ward9ddaaa11999-01-06 14:46:06 +0000424
Greg Ward4d74d731999-06-08 01:58:36 +0000425def _init_nt():
426 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000427 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000428 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000429 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
430 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000431
Greg Ward32162e81999-08-29 18:22:13 +0000432 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000433 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000434
Fred Drake69e2c6e2000-02-08 15:55:42 +0000435 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000436 g['EXE'] = ".exe"
Christian Heimesd3fc07a42007-12-06 13:15:13 +0000437 g['VERSION'] = get_python_version().replace(".", "")
438 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Greg Ward879f0f12000-09-15 01:15:08 +0000439
440 global _config_vars
441 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000442
Fred Drake69e2c6e2000-02-08 15:55:42 +0000443
Greg Ward0eff87a2000-03-07 03:30:09 +0000444def _init_mac():
445 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000446 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000447 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000448 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
449 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000450
451 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000452 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000453
Jack Jansendd13a202001-05-17 12:52:01 +0000454 import MacOS
455 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000456 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000457 else:
458 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000459
460 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000461 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
462 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000463
Jack Jansenab5320b2002-06-26 15:42:49 +0000464 # These are used by the extension module build
465 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000466 global _config_vars
467 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000468
Fred Drake69e2c6e2000-02-08 15:55:42 +0000469
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000470def _init_os2():
471 """Initialize the module as appropriate for OS/2"""
472 g = {}
473 # set basic install directories
474 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
475 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
476
477 # XXX hmmm.. a normal install puts include files here
478 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
479
480 g['SO'] = '.pyd'
481 g['EXE'] = ".exe"
482
483 global _config_vars
484 _config_vars = g
485
486
Greg Ward879f0f12000-09-15 01:15:08 +0000487def get_config_vars(*args):
488 """With no arguments, return a dictionary of all configuration
489 variables relevant for the current platform. Generally this includes
490 everything needed to build extensions and install both pure modules and
491 extensions. On Unix, this means every variable defined in Python's
492 installed Makefile; on Windows and Mac OS it's a much smaller set.
493
494 With arguments, return a list of values that result from looking up
495 each argument in the configuration variable dictionary.
496 """
497 global _config_vars
498 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000499 func = globals().get("_init_" + os.name)
500 if func:
501 func()
502 else:
503 _config_vars = {}
504
505 # Normalized versions of prefix and exec_prefix are handy to have;
506 # in fact, these are the standard versions used most places in the
507 # Distutils.
508 _config_vars['prefix'] = PREFIX
509 _config_vars['exec_prefix'] = EXEC_PREFIX
510
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000511 if sys.platform == 'darwin':
512 kernel_version = os.uname()[2] # Kernel version (8.4.3)
513 major_version = int(kernel_version.split('.')[0])
514
515 if major_version < 8:
516 # On Mac OS X before 10.4, check if -arch and -isysroot
517 # are in CFLAGS or LDFLAGS and remove them if they are.
518 # This is needed when building extensions on a 10.3 system
519 # using a universal build of python.
Ronald Oussorend6103692006-10-08 17:49:52 +0000520 for key in ('LDFLAGS', 'BASECFLAGS',
521 # a number of derived variables. These need to be
522 # patched up as well.
523 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000524 flags = _config_vars[key]
525 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Ronald Oussoren7b9053a2006-06-27 10:08:25 +0000526 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Ronald Oussorenb02daf72006-05-23 12:01:11 +0000527 _config_vars[key] = flags
528
Greg Ward879f0f12000-09-15 01:15:08 +0000529 if args:
530 vals = []
531 for name in args:
532 vals.append(_config_vars.get(name))
533 return vals
534 else:
535 return _config_vars
536
537def get_config_var(name):
538 """Return the value of a single variable using the dictionary
539 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000540 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000541 """
542 return get_config_vars().get(name)