blob: 70a279938ca0cfe729e81b438ee3588eb6c5ac8a [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
Guido van Rossum63236cf2007-05-25 18:39:29 +000014import io
Greg Ward9ddaaa11999-01-06 14:46:06 +000015import os
16import re
Greg Ward9ddaaa11999-01-06 14:46:06 +000017import sys
Greg Ward1190ee31998-12-18 23:46:33 +000018
Guido van Rossum45aecf42006-03-15 04:58:47 +000019from .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
Fred Drake16c8d702002-06-04 15:28:21 +000025# python_build: (Boolean) if true, we're either building Python or
26# building an extension with an un-installed Python, so we use
27# different (hard-wired) directories.
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000028
Fred Drake16c8d702002-06-04 15:28:21 +000029argv0_path = os.path.dirname(os.path.abspath(sys.executable))
Collin Winter5b7e9d72007-08-30 03:52:21 +000030python_build = os.path.isfile(os.path.join(argv0_path, "Modules", "Setup"))
Fred Drakec1ee39a2000-03-09 15:54:52 +000031
Fred Drakec916cdc2001-08-02 20:03:12 +000032
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000033def get_python_version():
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000034 """Return a string containing the major and minor Python version,
35 leaving off the patchlevel. Sample return values could be '1.5'
36 or '2.2'.
37 """
38 return sys.version[:3]
39
40
Greg Wardd38e6f72000-04-10 01:17:49 +000041def get_python_inc(plat_specific=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000042 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +000043
44 If 'plat_specific' is false (the default), this is the path to the
45 non-platform-specific header files, i.e. Python.h and so on;
46 otherwise, this is the path to platform-specific header files
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000047 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +000048
Greg Wardd38e6f72000-04-10 01:17:49 +000049 If 'prefix' is supplied, use it instead of sys.prefix or
50 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +000051 """
Greg Wardd38e6f72000-04-10 01:17:49 +000052 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000053 prefix = plat_specific and EXEC_PREFIX or PREFIX
Greg Ward7d73b9e2000-03-09 03:16:05 +000054 if os.name == "posix":
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +000055 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +000056 base = os.path.dirname(os.path.abspath(sys.executable))
57 if plat_specific:
58 inc_dir = base
59 else:
60 inc_dir = os.path.join(base, "Include")
61 if not os.path.exists(inc_dir):
62 inc_dir = os.path.join(os.path.dirname(base), "Include")
63 return inc_dir
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +000064 return os.path.join(prefix, "include", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +000065 elif os.name == "nt":
Fred Drakec916cdc2001-08-02 20:03:12 +000066 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000067 elif os.name == "mac":
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000068 if plat_specific:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000069 return os.path.join(prefix, "Mac", "Include")
Neal Norwitz80a3e0a2002-06-26 22:05:33 +000070 else:
Martin v. Löwis23b44a32003-10-24 20:09:23 +000071 return os.path.join(prefix, "Include")
Marc-André Lemburg2544f512002-01-31 18:56:00 +000072 elif os.name == "os2":
73 return os.path.join(prefix, "Include")
Greg Ward7d73b9e2000-03-09 03:16:05 +000074 else:
Fred Drake70b014d2001-07-18 18:39:56 +000075 raise DistutilsPlatformError(
76 "I don't know where Python installs its C header files "
77 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +000078
79
Greg Wardd38e6f72000-04-10 01:17:49 +000080def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
Greg Ward7d73b9e2000-03-09 03:16:05 +000081 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +000082 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +000083
Fred Drakec1ee39a2000-03-09 15:54:52 +000084 If 'plat_specific' is true, return the directory containing
85 platform-specific modules, i.e. any module from a non-pure-Python
86 module distribution; otherwise, return the platform-shared library
87 directory. If 'standard_lib' is true, return the directory
88 containing standard Python library modules; otherwise, return the
89 directory for site-specific modules.
90
Greg Wardd38e6f72000-04-10 01:17:49 +000091 If 'prefix' is supplied, use it instead of sys.prefix or
92 sys.exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +000093 """
Greg Wardd38e6f72000-04-10 01:17:49 +000094 if prefix is None:
Fred Drake70b014d2001-07-18 18:39:56 +000095 prefix = plat_specific and EXEC_PREFIX or PREFIX
Fred Drakec916cdc2001-08-02 20:03:12 +000096
Greg Ward7d73b9e2000-03-09 03:16:05 +000097 if os.name == "posix":
Greg Wardcf6bea32000-04-10 01:15:06 +000098 libpython = os.path.join(prefix,
Andrew M. Kuchling0ff98b92002-11-14 01:43:00 +000099 "lib", "python" + get_python_version())
Greg Ward7d73b9e2000-03-09 03:16:05 +0000100 if standard_lib:
101 return libpython
102 else:
Fred Drakec1ee39a2000-03-09 15:54:52 +0000103 return os.path.join(libpython, "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000104 elif os.name == "nt":
105 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000106 return os.path.join(prefix, "Lib")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000107 else:
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000108 if get_python_version() < "2.2":
Greg Wardf17efb92001-08-23 20:53:27 +0000109 return prefix
110 else:
111 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000112 elif os.name == "mac":
Greg Warddc9fe8a2000-08-02 01:49:40 +0000113 if plat_specific:
Greg Ward7d73b9e2000-03-09 03:16:05 +0000114 if standard_lib:
Jack Jansen212a2e12001-09-04 12:01:49 +0000115 return os.path.join(prefix, "Lib", "lib-dynload")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000116 else:
Jack Jansen212a2e12001-09-04 12:01:49 +0000117 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000118 else:
119 if standard_lib:
Fred Drakec916cdc2001-08-02 20:03:12 +0000120 return os.path.join(prefix, "Lib")
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")
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000123 elif os.name == "os2":
124 if standard_lib:
125 return os.path.join(PREFIX, "Lib")
126 else:
127 return os.path.join(PREFIX, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000128 else:
Fred Drake70b014d2001-07-18 18:39:56 +0000129 raise DistutilsPlatformError(
130 "I don't know where Python installs its library "
131 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000132
Greg Ward7d73b9e2000-03-09 03:16:05 +0000133
Fred Drake70b014d2001-07-18 18:39:56 +0000134def customize_compiler(compiler):
Fred Drakec916cdc2001-08-02 20:03:12 +0000135 """Do any platform-specific customization of a CCompiler instance.
136
137 Mainly needed on Unix, so we can plug in the information that
138 varies across Unices and is stored in Python's Makefile.
Greg Wardbb7baa72000-06-25 02:09:14 +0000139 """
140 if compiler.compiler_type == "unix":
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000141 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
Tim Petersfffc4b72005-05-18 02:18:09 +0000142 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
Brett Cannon08cd5982005-04-24 22:26:38 +0000143 'CCSHARED', 'LDSHARED', 'SO')
Greg Wardbb7baa72000-06-25 02:09:14 +0000144
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000145 if 'CC' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000146 cc = os.environ['CC']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000147 if 'CXX' in os.environ:
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000148 cxx = os.environ['CXX']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000149 if 'LDSHARED' in os.environ:
Anthony Baxter22dcf662004-10-13 15:54:17 +0000150 ldshared = os.environ['LDSHARED']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000151 if 'CPP' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000152 cpp = os.environ['CPP']
153 else:
154 cpp = cc + " -E" # not always
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000155 if 'LDFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000156 ldshared = ldshared + ' ' + os.environ['LDFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000157 if 'CFLAGS' in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000158 cflags = opt + ' ' + os.environ['CFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000159 ldshared = ldshared + ' ' + os.environ['CFLAGS']
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000160 if 'CPPFLAGS' in os.environ:
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000161 cpp = cpp + ' ' + os.environ['CPPFLAGS']
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000162 cflags = cflags + ' ' + os.environ['CPPFLAGS']
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000163 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
164
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000165 cc_cmd = cc + ' ' + cflags
Greg Ward879f0f12000-09-15 01:15:08 +0000166 compiler.set_executables(
Andrew M. Kuchling29c86232002-11-04 19:53:24 +0000167 preprocessor=cpp,
Greg Ward879f0f12000-09-15 01:15:08 +0000168 compiler=cc_cmd,
169 compiler_so=cc_cmd + ' ' + ccshared,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000170 compiler_cxx=cxx,
Greg Ward879f0f12000-09-15 01:15:08 +0000171 linker_so=ldshared,
172 linker_exe=cc)
173
174 compiler.shared_lib_extension = so_ext
Greg Wardbb7baa72000-06-25 02:09:14 +0000175
176
Greg Ward9ddaaa11999-01-06 14:46:06 +0000177def get_config_h_filename():
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000178 """Return full pathname of installed pyconfig.h file."""
Fred Drakec916cdc2001-08-02 20:03:12 +0000179 if python_build:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180 inc_dir = argv0_path
Fred Drakec916cdc2001-08-02 20:03:12 +0000181 else:
182 inc_dir = get_python_inc(plat_specific=1)
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000183 if get_python_version() < '2.2':
Marc-André Lemburg7cf92fa2001-07-26 18:06:58 +0000184 config_h = 'config.h'
185 else:
186 # The name of the config.h file changed in 2.2
187 config_h = 'pyconfig.h'
188 return os.path.join(inc_dir, config_h)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000189
Greg Ward1190ee31998-12-18 23:46:33 +0000190
Greg Ward9ddaaa11999-01-06 14:46:06 +0000191def get_makefile_filename():
Fred Drake522af3a1999-01-06 16:28:34 +0000192 """Return full pathname of installed Makefile from the Python build."""
Andrew M. Kuchlingc14fa302001-01-17 15:16:52 +0000193 if python_build:
Fred Drake16c8d702002-06-04 15:28:21 +0000194 return os.path.join(os.path.dirname(sys.executable), "Makefile")
Fred Drakec1ee39a2000-03-09 15:54:52 +0000195 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
196 return os.path.join(lib_dir, "config", "Makefile")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000197
Greg Ward1190ee31998-12-18 23:46:33 +0000198
Greg Ward9ddaaa11999-01-06 14:46:06 +0000199def parse_config_h(fp, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000200 """Parse a config.h-style file.
201
202 A dictionary containing name/value pairs is returned. If an
203 optional dictionary is passed in as the second argument, it is
204 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000205 """
Greg Ward9ddaaa11999-01-06 14:46:06 +0000206 if g is None:
207 g = {}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
209 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
Greg Ward9ddaaa11999-01-06 14:46:06 +0000210 #
Collin Winter5b7e9d72007-08-30 03:52:21 +0000211 while True:
Greg Ward1190ee31998-12-18 23:46:33 +0000212 line = fp.readline()
213 if not line:
214 break
215 m = define_rx.match(line)
216 if m:
217 n, v = m.group(1, 2)
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000218 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000219 except ValueError: pass
220 g[n] = v
Greg Ward1190ee31998-12-18 23:46:33 +0000221 else:
222 m = undef_rx.match(line)
223 if m:
224 g[m.group(1)] = 0
Greg Ward9ddaaa11999-01-06 14:46:06 +0000225 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000226
Greg Wardd283ce72000-09-17 00:53:02 +0000227
228# Regexes needed for parsing Makefile (and similar syntaxes,
229# like old-style Setup files).
230_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
231_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
232_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
233
Greg Ward3fff8d22000-09-15 00:03:13 +0000234def parse_makefile(fn, g=None):
Fred Drakec1ee39a2000-03-09 15:54:52 +0000235 """Parse a Makefile-style file.
236
237 A dictionary containing name/value pairs is returned. If an
238 optional dictionary is passed in as the second argument, it is
239 used instead of a new dictionary.
Fred Drake522af3a1999-01-06 16:28:34 +0000240 """
Greg Ward3fff8d22000-09-15 00:03:13 +0000241 from distutils.text_file import TextFile
Greg Wardd283ce72000-09-17 00:53:02 +0000242 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
Greg Ward3fff8d22000-09-15 00:03:13 +0000243
Greg Ward9ddaaa11999-01-06 14:46:06 +0000244 if g is None:
245 g = {}
Greg Ward1190ee31998-12-18 23:46:33 +0000246 done = {}
247 notdone = {}
Greg Ward3fff8d22000-09-15 00:03:13 +0000248
Collin Winter5b7e9d72007-08-30 03:52:21 +0000249 while True:
Greg Ward1190ee31998-12-18 23:46:33 +0000250 line = fp.readline()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000251 if line is None: # eof
Greg Ward1190ee31998-12-18 23:46:33 +0000252 break
Greg Wardd283ce72000-09-17 00:53:02 +0000253 m = _variable_rx.match(line)
Greg Ward1190ee31998-12-18 23:46:33 +0000254 if m:
255 n, v = m.group(1, 2)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000256 v = v.strip()
Greg Ward1190ee31998-12-18 23:46:33 +0000257 if "$" in v:
258 notdone[n] = v
259 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000260 try: v = int(v)
Greg Ward3c8e54b1998-12-22 12:42:04 +0000261 except ValueError: pass
Greg Ward1190ee31998-12-18 23:46:33 +0000262 done[n] = v
263
264 # do variable interpolation here
Greg Ward1190ee31998-12-18 23:46:33 +0000265 while notdone:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000266 for name in list(notdone):
Greg Ward1190ee31998-12-18 23:46:33 +0000267 value = notdone[name]
Greg Wardd283ce72000-09-17 00:53:02 +0000268 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
Greg Ward1190ee31998-12-18 23:46:33 +0000269 if m:
270 n = m.group(1)
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000271 found = True
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000272 if n in done:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000273 item = str(done[n])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000274 elif n in notdone:
Greg Ward1190ee31998-12-18 23:46:33 +0000275 # get it on a subsequent round
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000276 found = False
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000277 elif n in os.environ:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000278 # do it like make: fall back to environment
279 item = os.environ[n]
Greg Ward1190ee31998-12-18 23:46:33 +0000280 else:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000281 done[n] = item = ""
282 if found:
Greg Ward1190ee31998-12-18 23:46:33 +0000283 after = value[m.end():]
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000284 value = value[:m.start()] + item + after
Greg Ward1190ee31998-12-18 23:46:33 +0000285 if "$" in after:
286 notdone[name] = value
287 else:
Jeremy Hyltona5f4c072002-11-05 20:11:08 +0000288 try: value = int(value)
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000289 except ValueError:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000290 done[name] = value.strip()
Andrew M. Kuchlingb11bd032001-01-16 16:33:28 +0000291 else:
292 done[name] = value
Greg Ward1190ee31998-12-18 23:46:33 +0000293 del notdone[name]
294 else:
Greg Ward3c8e54b1998-12-22 12:42:04 +0000295 # bogus variable reference; just drop it since we can't deal
Greg Ward1190ee31998-12-18 23:46:33 +0000296 del notdone[name]
297
Greg Wardd283ce72000-09-17 00:53:02 +0000298 fp.close()
299
Greg Ward1190ee31998-12-18 23:46:33 +0000300 # save the results in the global dictionary
301 g.update(done)
Greg Ward9ddaaa11999-01-06 14:46:06 +0000302 return g
Greg Ward1190ee31998-12-18 23:46:33 +0000303
304
Greg Wardd283ce72000-09-17 00:53:02 +0000305def expand_makefile_vars(s, vars):
306 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
307 'string' according to 'vars' (a dictionary mapping variable names to
308 values). Variables not present in 'vars' are silently expanded to the
309 empty string. The variable values in 'vars' should not contain further
310 variable expansions; if 'vars' is the output of 'parse_makefile()',
311 you're fine. Returns a variable-expanded version of 's'.
312 """
313
314 # This algorithm does multiple expansion, so if vars['foo'] contains
315 # "${bar}", it will expand ${foo} to ${bar}, and then expand
316 # ${bar}... and so forth. This is fine as long as 'vars' comes from
317 # 'parse_makefile()', which takes care of such expansions eagerly,
318 # according to make's variable expansion semantics.
319
Collin Winter5b7e9d72007-08-30 03:52:21 +0000320 while True:
Greg Wardd283ce72000-09-17 00:53:02 +0000321 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
322 if m:
Greg Wardd283ce72000-09-17 00:53:02 +0000323 (beg, end) = m.span()
324 s = s[0:beg] + vars.get(m.group(1)) + s[end:]
325 else:
326 break
327 return s
328
329
Greg Ward879f0f12000-09-15 01:15:08 +0000330_config_vars = None
331
Greg Ward9ddaaa11999-01-06 14:46:06 +0000332def _init_posix():
Fred Drake522af3a1999-01-06 16:28:34 +0000333 """Initialize the module as appropriate for POSIX systems."""
Greg Ward879f0f12000-09-15 01:15:08 +0000334 g = {}
Greg Warda0ca3f22000-02-02 00:05:14 +0000335 # load the installed Makefile:
Greg Warda570c052000-05-23 23:14:00 +0000336 try:
337 filename = get_makefile_filename()
Greg Ward3fff8d22000-09-15 00:03:13 +0000338 parse_makefile(filename, g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000339 except IOError as msg:
Greg Warda570c052000-05-23 23:14:00 +0000340 my_msg = "invalid Python installation: unable to open %s" % filename
341 if hasattr(msg, "strerror"):
342 my_msg = my_msg + " (%s)" % msg.strerror
343
Fred Drake70b014d2001-07-18 18:39:56 +0000344 raise DistutilsPlatformError(my_msg)
Fred Drakec916cdc2001-08-02 20:03:12 +0000345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346 # load the installed pyconfig.h:
347 try:
348 filename = get_config_h_filename()
Guido van Rossum63236cf2007-05-25 18:39:29 +0000349 parse_config_h(io.open(filename), g)
Guido van Rossumb940e112007-01-10 16:19:56 +0000350 except IOError as msg:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 my_msg = "invalid Python installation: unable to open %s" % filename
352 if hasattr(msg, "strerror"):
353 my_msg = my_msg + " (%s)" % msg.strerror
354
355 raise DistutilsPlatformError(my_msg)
356
Jack Jansen6b08a402004-06-03 12:41:45 +0000357 # On MacOSX we need to check the setting of the environment variable
358 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
359 # it needs to be compatible.
Jack Jansenbe954622004-12-26 23:07:48 +0000360 # If it isn't set we set it to the configure-time value
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000361 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
Jack Jansen6b08a402004-06-03 12:41:45 +0000363 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
Jack Jansenbe954622004-12-26 23:07:48 +0000364 if cur_target == '':
365 cur_target = cfg_target
366 os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000367 elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
Jack Jansen6b08a402004-06-03 12:41:45 +0000368 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
369 % (cur_target, cfg_target))
370 raise DistutilsPlatformError(my_msg)
Tim Peters182b5ac2004-07-18 06:16:08 +0000371
Greg Ward4f880282000-06-27 01:59:06 +0000372 # On AIX, there are wrong paths to the linker scripts in the Makefile
373 # -- these paths are relative to the Python source, but when installed
374 # the scripts are in another directory.
Neil Schemenauer1a020862001-02-16 03:31:13 +0000375 if python_build:
Andrew M. Kuchling63357732001-02-28 19:40:27 +0000376 g['LDSHARED'] = g['BLDSHARED']
Fred Drakeb94b8492001-12-06 20:51:35 +0000377
Martin v. Löwisdf37c8c2005-03-03 11:08:03 +0000378 elif get_python_version() < '2.1':
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000379 # The following two branches are for 1.5.2 compatibility.
380 if sys.platform == 'aix4': # what about AIX 3.x ?
381 # Linker script is in the config directory, not in Modules as the
382 # Makefile says.
383 python_lib = get_python_lib(standard_lib=1)
384 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
385 python_exp = os.path.join(python_lib, 'config', 'python.exp')
Greg Ward879f0f12000-09-15 01:15:08 +0000386
Andrew M. Kuchling045af6f2001-09-05 12:02:59 +0000387 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
388
Greg Ward879f0f12000-09-15 01:15:08 +0000389 global _config_vars
390 _config_vars = g
Greg Ward66e966f2000-09-01 01:23:26 +0000391
Greg Ward9ddaaa11999-01-06 14:46:06 +0000392
Greg Ward4d74d731999-06-08 01:58:36 +0000393def _init_nt():
394 """Initialize the module as appropriate for NT"""
Greg Ward879f0f12000-09-15 01:15:08 +0000395 g = {}
Greg Ward4d74d731999-06-08 01:58:36 +0000396 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000397 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
398 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward4d74d731999-06-08 01:58:36 +0000399
Greg Ward32162e81999-08-29 18:22:13 +0000400 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000401 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward32162e81999-08-29 18:22:13 +0000402
Fred Drake69e2c6e2000-02-08 15:55:42 +0000403 g['SO'] = '.pyd'
Greg Ward82d71ca2000-06-03 00:44:30 +0000404 g['EXE'] = ".exe"
Greg Ward879f0f12000-09-15 01:15:08 +0000405
406 global _config_vars
407 _config_vars = g
Greg Ward82d71ca2000-06-03 00:44:30 +0000408
Fred Drake69e2c6e2000-02-08 15:55:42 +0000409
Greg Ward0eff87a2000-03-07 03:30:09 +0000410def _init_mac():
411 """Initialize the module as appropriate for Macintosh systems"""
Greg Ward879f0f12000-09-15 01:15:08 +0000412 g = {}
Greg Ward0eff87a2000-03-07 03:30:09 +0000413 # set basic install directories
Fred Drakec1ee39a2000-03-09 15:54:52 +0000414 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
415 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
Greg Ward0eff87a2000-03-07 03:30:09 +0000416
417 # XXX hmmm.. a normal install puts include files here
Fred Drakec1ee39a2000-03-09 15:54:52 +0000418 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
Greg Ward0eff87a2000-03-07 03:30:09 +0000419
Jack Jansendd13a202001-05-17 12:52:01 +0000420 import MacOS
421 if not hasattr(MacOS, 'runtimemodel'):
Guido van Rossum99f9baa2001-05-17 15:03:14 +0000422 g['SO'] = '.ppc.slb'
Jack Jansendd13a202001-05-17 12:52:01 +0000423 else:
424 g['SO'] = '.%s.slb' % MacOS.runtimemodel
Greg Ward7d73b9e2000-03-09 03:16:05 +0000425
426 # XXX are these used anywhere?
Greg Wardcf6bea32000-04-10 01:15:06 +0000427 g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
428 g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
Greg Ward0eff87a2000-03-07 03:30:09 +0000429
Jack Jansenab5320b2002-06-26 15:42:49 +0000430 # These are used by the extension module build
431 g['srcdir'] = ':'
Greg Ward879f0f12000-09-15 01:15:08 +0000432 global _config_vars
433 _config_vars = g
Greg Ward9ddaaa11999-01-06 14:46:06 +0000434
Fred Drake69e2c6e2000-02-08 15:55:42 +0000435
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000436def _init_os2():
437 """Initialize the module as appropriate for OS/2"""
438 g = {}
439 # set basic install directories
440 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
441 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
442
443 # XXX hmmm.. a normal install puts include files here
444 g['INCLUDEPY'] = get_python_inc(plat_specific=0)
445
446 g['SO'] = '.pyd'
447 g['EXE'] = ".exe"
448
449 global _config_vars
450 _config_vars = g
451
452
Greg Ward879f0f12000-09-15 01:15:08 +0000453def get_config_vars(*args):
454 """With no arguments, return a dictionary of all configuration
455 variables relevant for the current platform. Generally this includes
456 everything needed to build extensions and install both pure modules and
457 extensions. On Unix, this means every variable defined in Python's
458 installed Makefile; on Windows and Mac OS it's a much smaller set.
459
460 With arguments, return a list of values that result from looking up
461 each argument in the configuration variable dictionary.
462 """
463 global _config_vars
464 if _config_vars is None:
Greg Ward879f0f12000-09-15 01:15:08 +0000465 func = globals().get("_init_" + os.name)
466 if func:
467 func()
468 else:
469 _config_vars = {}
470
471 # Normalized versions of prefix and exec_prefix are handy to have;
472 # in fact, these are the standard versions used most places in the
473 # Distutils.
474 _config_vars['prefix'] = PREFIX
475 _config_vars['exec_prefix'] = EXEC_PREFIX
476
Thomas Wouters477c8d52006-05-27 19:21:47 +0000477 if sys.platform == 'darwin':
478 kernel_version = os.uname()[2] # Kernel version (8.4.3)
479 major_version = int(kernel_version.split('.')[0])
480
481 if major_version < 8:
482 # On Mac OS X before 10.4, check if -arch and -isysroot
483 # are in CFLAGS or LDFLAGS and remove them if they are.
484 # This is needed when building extensions on a 10.3 system
485 # using a universal build of python.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000486 for key in ('LDFLAGS', 'BASECFLAGS',
487 # a number of derived variables. These need to be
488 # patched up as well.
489 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000490 flags = _config_vars[key]
491 flags = re.sub('-arch\s+\w+\s', ' ', flags)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000492 flags = re.sub('-isysroot [^ \t]*', ' ', flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000493 _config_vars[key] = flags
494
Greg Ward879f0f12000-09-15 01:15:08 +0000495 if args:
496 vals = []
497 for name in args:
498 vals.append(_config_vars.get(name))
499 return vals
500 else:
501 return _config_vars
502
503def get_config_var(name):
504 """Return the value of a single variable using the dictionary
505 returned by 'get_config_vars()'. Equivalent to
Fred Drakec916cdc2001-08-02 20:03:12 +0000506 get_config_vars().get(name)
Greg Ward879f0f12000-09-15 01:15:08 +0000507 """
508 return get_config_vars().get(name)