Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 1 | """Provide access to Python's configuration information. The specific |
| 2 | configuration variables available depend heavily on the platform and |
| 3 | configuration. The values may be retrieved using |
| 4 | get_config_var(name), and the list of variables is available via |
| 5 | get_config_vars().keys(). Additional convenience functions are also |
| 6 | available. |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 7 | |
| 8 | Written by: Fred L. Drake, Jr. |
| 9 | Email: <fdrake@acm.org> |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 12 | __revision__ = "$Id$" |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 13 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 14 | import os |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 15 | import re |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 16 | import string |
| 17 | import sys |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 18 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 19 | from distutils.errors import DistutilsPlatformError |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 20 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 21 | # These are needed in a couple of spots, so just compute them once. |
| 22 | PREFIX = os.path.normpath(sys.prefix) |
| 23 | EXEC_PREFIX = os.path.normpath(sys.exec_prefix) |
Tarek Ziadé | 0276c7a | 2010-01-26 21:21:54 +0000 | [diff] [blame] | 24 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 25 | # Path to the base directory of the project. On Windows the binary may |
| 26 | # live in project/PCBuild9. If we're dealing with an x64 Windows build, |
| 27 | # it'll live in project/PCbuild/amd64. |
| 28 | project_base = os.path.dirname(os.path.abspath(sys.executable)) |
| 29 | if os.name == "nt" and "pcbuild" in project_base[-8:].lower(): |
| 30 | project_base = os.path.abspath(os.path.join(project_base, os.path.pardir)) |
| 31 | # PC/VS7.1 |
| 32 | if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower(): |
| 33 | project_base = os.path.abspath(os.path.join(project_base, os.path.pardir, |
| 34 | os.path.pardir)) |
| 35 | # PC/AMD64 |
| 36 | if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower(): |
| 37 | project_base = os.path.abspath(os.path.join(project_base, os.path.pardir, |
| 38 | os.path.pardir)) |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 39 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 40 | # python_build: (Boolean) if true, we're either building Python or |
| 41 | # building an extension with an un-installed Python, so we use |
| 42 | # different (hard-wired) directories. |
| 43 | # Setup.local is available for Makefile builds including VPATH builds, |
| 44 | # Setup.dist is available on Windows |
Marc-André Lemburg | 2db7cd3 | 2008-02-05 14:50:40 +0000 | [diff] [blame] | 45 | def _python_build(): |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 46 | for fn in ("Setup.dist", "Setup.local"): |
| 47 | if os.path.isfile(os.path.join(project_base, "Modules", fn)): |
| 48 | return True |
| 49 | return False |
Marc-André Lemburg | 2db7cd3 | 2008-02-05 14:50:40 +0000 | [diff] [blame] | 50 | python_build = _python_build() |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 51 | |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 52 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 53 | def get_python_version(): |
| 54 | """Return a string containing the major and minor Python version, |
| 55 | leaving off the patchlevel. Sample return values could be '1.5' |
| 56 | or '2.2'. |
| 57 | """ |
| 58 | return sys.version[:3] |
| 59 | |
| 60 | |
| 61 | def get_python_inc(plat_specific=0, prefix=None): |
| 62 | """Return the directory containing installed Python header files. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 63 | |
| 64 | If 'plat_specific' is false (the default), this is the path to the |
| 65 | non-platform-specific header files, i.e. Python.h and so on; |
| 66 | otherwise, this is the path to platform-specific header files |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 67 | (namely pyconfig.h). |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 68 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 69 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 70 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 71 | """ |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 72 | if prefix is None: |
| 73 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Tarek Ziadé | a5cd182 | 2010-04-30 12:15:12 +0000 | [diff] [blame^] | 74 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 75 | if os.name == "posix": |
| 76 | if python_build: |
Tarek Ziadé | a5cd182 | 2010-04-30 12:15:12 +0000 | [diff] [blame^] | 77 | buildir = os.path.dirname(sys.executable) |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 78 | if plat_specific: |
Tarek Ziadé | a5cd182 | 2010-04-30 12:15:12 +0000 | [diff] [blame^] | 79 | # python.h is located in the buildir |
| 80 | inc_dir = buildir |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 81 | else: |
Tarek Ziadé | a5cd182 | 2010-04-30 12:15:12 +0000 | [diff] [blame^] | 82 | # the source dir is relative to the buildir |
| 83 | srcdir = os.path.abspath(os.path.join(buildir, |
| 84 | get_config_var('srcdir'))) |
| 85 | # Include is located in the srcdir |
| 86 | inc_dir = os.path.join(srcdir, "Include") |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 87 | return inc_dir |
| 88 | return os.path.join(prefix, "include", "python" + get_python_version()) |
| 89 | elif os.name == "nt": |
| 90 | return os.path.join(prefix, "include") |
| 91 | elif os.name == "mac": |
| 92 | if plat_specific: |
| 93 | return os.path.join(prefix, "Mac", "Include") |
| 94 | else: |
| 95 | return os.path.join(prefix, "Include") |
| 96 | elif os.name == "os2": |
| 97 | return os.path.join(prefix, "Include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 98 | else: |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 99 | raise DistutilsPlatformError( |
| 100 | "I don't know where Python installs its C header files " |
| 101 | "on platform '%s'" % os.name) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 102 | |
| 103 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 104 | def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
| 105 | """Return the directory containing the Python library (standard or |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 106 | site additions). |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 107 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 108 | If 'plat_specific' is true, return the directory containing |
| 109 | platform-specific modules, i.e. any module from a non-pure-Python |
| 110 | module distribution; otherwise, return the platform-shared library |
| 111 | directory. If 'standard_lib' is true, return the directory |
| 112 | containing standard Python library modules; otherwise, return the |
| 113 | directory for site-specific modules. |
| 114 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 115 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 116 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 117 | """ |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 118 | if prefix is None: |
| 119 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 120 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 121 | if os.name == "posix": |
| 122 | libpython = os.path.join(prefix, |
| 123 | "lib", "python" + get_python_version()) |
| 124 | if standard_lib: |
| 125 | return libpython |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 126 | else: |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 127 | return os.path.join(libpython, "site-packages") |
| 128 | |
| 129 | elif os.name == "nt": |
| 130 | if standard_lib: |
| 131 | return os.path.join(prefix, "Lib") |
| 132 | else: |
| 133 | if get_python_version() < "2.2": |
| 134 | return prefix |
| 135 | else: |
| 136 | return os.path.join(prefix, "Lib", "site-packages") |
| 137 | |
| 138 | elif os.name == "mac": |
| 139 | if plat_specific: |
| 140 | if standard_lib: |
| 141 | return os.path.join(prefix, "Lib", "lib-dynload") |
| 142 | else: |
| 143 | return os.path.join(prefix, "Lib", "site-packages") |
| 144 | else: |
| 145 | if standard_lib: |
| 146 | return os.path.join(prefix, "Lib") |
| 147 | else: |
| 148 | return os.path.join(prefix, "Lib", "site-packages") |
| 149 | |
| 150 | elif os.name == "os2": |
| 151 | if standard_lib: |
| 152 | return os.path.join(prefix, "Lib") |
| 153 | else: |
| 154 | return os.path.join(prefix, "Lib", "site-packages") |
| 155 | |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 156 | else: |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 157 | raise DistutilsPlatformError( |
| 158 | "I don't know where Python installs its library " |
| 159 | "on platform '%s'" % os.name) |
| 160 | |
| 161 | |
| 162 | def customize_compiler(compiler): |
| 163 | """Do any platform-specific customization of a CCompiler instance. |
| 164 | |
| 165 | Mainly needed on Unix, so we can plug in the information that |
| 166 | varies across Unices and is stored in Python's Makefile. |
| 167 | """ |
| 168 | if compiler.compiler_type == "unix": |
| 169 | (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \ |
| 170 | get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS', |
| 171 | 'CCSHARED', 'LDSHARED', 'SO') |
| 172 | |
| 173 | if 'CC' in os.environ: |
| 174 | cc = os.environ['CC'] |
| 175 | if 'CXX' in os.environ: |
| 176 | cxx = os.environ['CXX'] |
| 177 | if 'LDSHARED' in os.environ: |
| 178 | ldshared = os.environ['LDSHARED'] |
| 179 | if 'CPP' in os.environ: |
| 180 | cpp = os.environ['CPP'] |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 181 | else: |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 182 | cpp = cc + " -E" # not always |
| 183 | if 'LDFLAGS' in os.environ: |
| 184 | ldshared = ldshared + ' ' + os.environ['LDFLAGS'] |
| 185 | if 'CFLAGS' in os.environ: |
| 186 | cflags = opt + ' ' + os.environ['CFLAGS'] |
| 187 | ldshared = ldshared + ' ' + os.environ['CFLAGS'] |
| 188 | if 'CPPFLAGS' in os.environ: |
| 189 | cpp = cpp + ' ' + os.environ['CPPFLAGS'] |
| 190 | cflags = cflags + ' ' + os.environ['CPPFLAGS'] |
| 191 | ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] |
| 192 | |
| 193 | cc_cmd = cc + ' ' + cflags |
| 194 | compiler.set_executables( |
| 195 | preprocessor=cpp, |
| 196 | compiler=cc_cmd, |
| 197 | compiler_so=cc_cmd + ' ' + ccshared, |
| 198 | compiler_cxx=cxx, |
| 199 | linker_so=ldshared, |
| 200 | linker_exe=cc) |
| 201 | |
| 202 | compiler.shared_lib_extension = so_ext |
| 203 | |
| 204 | |
| 205 | def get_config_h_filename(): |
| 206 | """Return full pathname of installed pyconfig.h file.""" |
| 207 | if python_build: |
| 208 | if os.name == "nt": |
| 209 | inc_dir = os.path.join(project_base, "PC") |
| 210 | else: |
| 211 | inc_dir = project_base |
| 212 | else: |
| 213 | inc_dir = get_python_inc(plat_specific=1) |
| 214 | if get_python_version() < '2.2': |
| 215 | config_h = 'config.h' |
| 216 | else: |
| 217 | # The name of the config.h file changed in 2.2 |
| 218 | config_h = 'pyconfig.h' |
| 219 | return os.path.join(inc_dir, config_h) |
| 220 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 221 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 222 | def get_makefile_filename(): |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 223 | """Return full pathname of installed Makefile from the Python build.""" |
| 224 | if python_build: |
| 225 | return os.path.join(os.path.dirname(sys.executable), "Makefile") |
| 226 | lib_dir = get_python_lib(plat_specific=1, standard_lib=1) |
| 227 | return os.path.join(lib_dir, "config", "Makefile") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 228 | |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 229 | |
| 230 | def parse_config_h(fp, g=None): |
| 231 | """Parse a config.h-style file. |
| 232 | |
| 233 | A dictionary containing name/value pairs is returned. If an |
| 234 | optional dictionary is passed in as the second argument, it is |
| 235 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 236 | """ |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 237 | if g is None: |
| 238 | g = {} |
| 239 | define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") |
| 240 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") |
| 241 | # |
| 242 | while 1: |
| 243 | line = fp.readline() |
| 244 | if not line: |
| 245 | break |
| 246 | m = define_rx.match(line) |
| 247 | if m: |
| 248 | n, v = m.group(1, 2) |
| 249 | try: v = int(v) |
| 250 | except ValueError: pass |
| 251 | g[n] = v |
| 252 | else: |
| 253 | m = undef_rx.match(line) |
| 254 | if m: |
| 255 | g[m.group(1)] = 0 |
| 256 | return g |
| 257 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 258 | |
| 259 | # Regexes needed for parsing Makefile (and similar syntaxes, |
| 260 | # like old-style Setup files). |
| 261 | _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") |
| 262 | _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
| 263 | _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
| 264 | |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 265 | def parse_makefile(fn, g=None): |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 266 | """Parse a Makefile-style file. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 267 | |
| 268 | A dictionary containing name/value pairs is returned. If an |
| 269 | optional dictionary is passed in as the second argument, it is |
| 270 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 271 | """ |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 272 | from distutils.text_file import TextFile |
| 273 | fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1) |
| 274 | |
| 275 | if g is None: |
| 276 | g = {} |
| 277 | done = {} |
| 278 | notdone = {} |
| 279 | |
| 280 | while 1: |
| 281 | line = fp.readline() |
| 282 | if line is None: # eof |
| 283 | break |
| 284 | m = _variable_rx.match(line) |
| 285 | if m: |
| 286 | n, v = m.group(1, 2) |
| 287 | v = v.strip() |
| 288 | # `$$' is a literal `$' in make |
| 289 | tmpv = v.replace('$$', '') |
| 290 | |
| 291 | if "$" in tmpv: |
| 292 | notdone[n] = v |
| 293 | else: |
| 294 | try: |
| 295 | v = int(v) |
| 296 | except ValueError: |
| 297 | # insert literal `$' |
| 298 | done[n] = v.replace('$$', '$') |
| 299 | else: |
| 300 | done[n] = v |
| 301 | |
| 302 | # do variable interpolation here |
| 303 | while notdone: |
| 304 | for name in notdone.keys(): |
| 305 | value = notdone[name] |
| 306 | m = _findvar1_rx.search(value) or _findvar2_rx.search(value) |
| 307 | if m: |
| 308 | n = m.group(1) |
| 309 | found = True |
| 310 | if n in done: |
| 311 | item = str(done[n]) |
| 312 | elif n in notdone: |
| 313 | # get it on a subsequent round |
| 314 | found = False |
| 315 | elif n in os.environ: |
| 316 | # do it like make: fall back to environment |
| 317 | item = os.environ[n] |
| 318 | else: |
| 319 | done[n] = item = "" |
| 320 | if found: |
| 321 | after = value[m.end():] |
| 322 | value = value[:m.start()] + item + after |
| 323 | if "$" in after: |
| 324 | notdone[name] = value |
| 325 | else: |
| 326 | try: value = int(value) |
| 327 | except ValueError: |
| 328 | done[name] = value.strip() |
| 329 | else: |
| 330 | done[name] = value |
| 331 | del notdone[name] |
| 332 | else: |
| 333 | # bogus variable reference; just drop it since we can't deal |
| 334 | del notdone[name] |
| 335 | |
| 336 | fp.close() |
| 337 | |
| 338 | # save the results in the global dictionary |
| 339 | g.update(done) |
| 340 | return g |
| 341 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 342 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 343 | def expand_makefile_vars(s, vars): |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 344 | """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 345 | 'string' according to 'vars' (a dictionary mapping variable names to |
| 346 | values). Variables not present in 'vars' are silently expanded to the |
| 347 | empty string. The variable values in 'vars' should not contain further |
| 348 | variable expansions; if 'vars' is the output of 'parse_makefile()', |
| 349 | you're fine. Returns a variable-expanded version of 's'. |
| 350 | """ |
| 351 | |
| 352 | # This algorithm does multiple expansion, so if vars['foo'] contains |
| 353 | # "${bar}", it will expand ${foo} to ${bar}, and then expand |
| 354 | # ${bar}... and so forth. This is fine as long as 'vars' comes from |
| 355 | # 'parse_makefile()', which takes care of such expansions eagerly, |
| 356 | # according to make's variable expansion semantics. |
| 357 | |
| 358 | while 1: |
| 359 | m = _findvar1_rx.search(s) or _findvar2_rx.search(s) |
| 360 | if m: |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 361 | (beg, end) = m.span() |
| 362 | s = s[0:beg] + vars.get(m.group(1)) + s[end:] |
| 363 | else: |
| 364 | break |
| 365 | return s |
Tarek Ziadé | dd7bef9 | 2010-03-05 00:16:02 +0000 | [diff] [blame] | 366 | |
| 367 | |
| 368 | _config_vars = None |
| 369 | |
| 370 | def _init_posix(): |
| 371 | """Initialize the module as appropriate for POSIX systems.""" |
| 372 | g = {} |
| 373 | # load the installed Makefile: |
| 374 | try: |
| 375 | filename = get_makefile_filename() |
| 376 | parse_makefile(filename, g) |
| 377 | except IOError, msg: |
| 378 | my_msg = "invalid Python installation: unable to open %s" % filename |
| 379 | if hasattr(msg, "strerror"): |
| 380 | my_msg = my_msg + " (%s)" % msg.strerror |
| 381 | |
| 382 | raise DistutilsPlatformError(my_msg) |
| 383 | |
| 384 | # load the installed pyconfig.h: |
| 385 | try: |
| 386 | filename = get_config_h_filename() |
| 387 | parse_config_h(file(filename), g) |
| 388 | except IOError, msg: |
| 389 | my_msg = "invalid Python installation: unable to open %s" % filename |
| 390 | if hasattr(msg, "strerror"): |
| 391 | my_msg = my_msg + " (%s)" % msg.strerror |
| 392 | |
| 393 | raise DistutilsPlatformError(my_msg) |
| 394 | |
| 395 | # On MacOSX we need to check the setting of the environment variable |
| 396 | # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so |
| 397 | # it needs to be compatible. |
| 398 | # If it isn't set we set it to the configure-time value |
| 399 | if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g: |
| 400 | cfg_target = g['MACOSX_DEPLOYMENT_TARGET'] |
| 401 | cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '') |
| 402 | if cur_target == '': |
| 403 | cur_target = cfg_target |
| 404 | os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target) |
| 405 | elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')): |
| 406 | my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure' |
| 407 | % (cur_target, cfg_target)) |
| 408 | raise DistutilsPlatformError(my_msg) |
| 409 | |
| 410 | # On AIX, there are wrong paths to the linker scripts in the Makefile |
| 411 | # -- these paths are relative to the Python source, but when installed |
| 412 | # the scripts are in another directory. |
| 413 | if python_build: |
| 414 | g['LDSHARED'] = g['BLDSHARED'] |
| 415 | |
| 416 | elif get_python_version() < '2.1': |
| 417 | # The following two branches are for 1.5.2 compatibility. |
| 418 | if sys.platform == 'aix4': # what about AIX 3.x ? |
| 419 | # Linker script is in the config directory, not in Modules as the |
| 420 | # Makefile says. |
| 421 | python_lib = get_python_lib(standard_lib=1) |
| 422 | ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix') |
| 423 | python_exp = os.path.join(python_lib, 'config', 'python.exp') |
| 424 | |
| 425 | g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp) |
| 426 | |
| 427 | elif sys.platform == 'beos': |
| 428 | # Linker script is in the config directory. In the Makefile it is |
| 429 | # relative to the srcdir, which after installation no longer makes |
| 430 | # sense. |
| 431 | python_lib = get_python_lib(standard_lib=1) |
| 432 | linkerscript_path = string.split(g['LDSHARED'])[0] |
| 433 | linkerscript_name = os.path.basename(linkerscript_path) |
| 434 | linkerscript = os.path.join(python_lib, 'config', |
| 435 | linkerscript_name) |
| 436 | |
| 437 | # XXX this isn't the right place to do this: adding the Python |
| 438 | # library to the link, if needed, should be in the "build_ext" |
| 439 | # command. (It's also needed for non-MS compilers on Windows, and |
| 440 | # it's taken care of for them by the 'build_ext.get_libraries()' |
| 441 | # method.) |
| 442 | g['LDSHARED'] = ("%s -L%s/lib -lpython%s" % |
| 443 | (linkerscript, PREFIX, get_python_version())) |
| 444 | |
| 445 | global _config_vars |
| 446 | _config_vars = g |
| 447 | |
| 448 | |
| 449 | def _init_nt(): |
| 450 | """Initialize the module as appropriate for NT""" |
| 451 | g = {} |
| 452 | # set basic install directories |
| 453 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 454 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
| 455 | |
| 456 | # XXX hmmm.. a normal install puts include files here |
| 457 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
| 458 | |
| 459 | g['SO'] = '.pyd' |
| 460 | g['EXE'] = ".exe" |
| 461 | g['VERSION'] = get_python_version().replace(".", "") |
| 462 | g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable)) |
| 463 | |
| 464 | global _config_vars |
| 465 | _config_vars = g |
| 466 | |
| 467 | |
| 468 | def _init_mac(): |
| 469 | """Initialize the module as appropriate for Macintosh systems""" |
| 470 | g = {} |
| 471 | # set basic install directories |
| 472 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 473 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
| 474 | |
| 475 | # XXX hmmm.. a normal install puts include files here |
| 476 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
| 477 | |
| 478 | import MacOS |
| 479 | if not hasattr(MacOS, 'runtimemodel'): |
| 480 | g['SO'] = '.ppc.slb' |
| 481 | else: |
| 482 | g['SO'] = '.%s.slb' % MacOS.runtimemodel |
| 483 | |
| 484 | # XXX are these used anywhere? |
| 485 | g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib") |
| 486 | g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib") |
| 487 | |
| 488 | # These are used by the extension module build |
| 489 | g['srcdir'] = ':' |
| 490 | global _config_vars |
| 491 | _config_vars = g |
| 492 | |
| 493 | |
| 494 | def _init_os2(): |
| 495 | """Initialize the module as appropriate for OS/2""" |
| 496 | g = {} |
| 497 | # set basic install directories |
| 498 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 499 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
| 500 | |
| 501 | # XXX hmmm.. a normal install puts include files here |
| 502 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
| 503 | |
| 504 | g['SO'] = '.pyd' |
| 505 | g['EXE'] = ".exe" |
| 506 | |
| 507 | global _config_vars |
| 508 | _config_vars = g |
| 509 | |
| 510 | |
| 511 | def get_config_vars(*args): |
| 512 | """With no arguments, return a dictionary of all configuration |
| 513 | variables relevant for the current platform. Generally this includes |
| 514 | everything needed to build extensions and install both pure modules and |
| 515 | extensions. On Unix, this means every variable defined in Python's |
| 516 | installed Makefile; on Windows and Mac OS it's a much smaller set. |
| 517 | |
| 518 | With arguments, return a list of values that result from looking up |
| 519 | each argument in the configuration variable dictionary. |
| 520 | """ |
| 521 | global _config_vars |
| 522 | if _config_vars is None: |
| 523 | func = globals().get("_init_" + os.name) |
| 524 | if func: |
| 525 | func() |
| 526 | else: |
| 527 | _config_vars = {} |
| 528 | |
| 529 | # Normalized versions of prefix and exec_prefix are handy to have; |
| 530 | # in fact, these are the standard versions used most places in the |
| 531 | # Distutils. |
| 532 | _config_vars['prefix'] = PREFIX |
| 533 | _config_vars['exec_prefix'] = EXEC_PREFIX |
| 534 | |
| 535 | if sys.platform == 'darwin': |
| 536 | kernel_version = os.uname()[2] # Kernel version (8.4.3) |
| 537 | major_version = int(kernel_version.split('.')[0]) |
| 538 | |
| 539 | if major_version < 8: |
| 540 | # On Mac OS X before 10.4, check if -arch and -isysroot |
| 541 | # are in CFLAGS or LDFLAGS and remove them if they are. |
| 542 | # This is needed when building extensions on a 10.3 system |
| 543 | # using a universal build of python. |
| 544 | for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED', |
| 545 | # a number of derived variables. These need to be |
| 546 | # patched up as well. |
| 547 | 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'): |
| 548 | flags = _config_vars[key] |
| 549 | flags = re.sub('-arch\s+\w+\s', ' ', flags) |
| 550 | flags = re.sub('-isysroot [^ \t]*', ' ', flags) |
| 551 | _config_vars[key] = flags |
| 552 | |
| 553 | else: |
| 554 | |
| 555 | # Allow the user to override the architecture flags using |
| 556 | # an environment variable. |
| 557 | # NOTE: This name was introduced by Apple in OSX 10.5 and |
| 558 | # is used by several scripting languages distributed with |
| 559 | # that OS release. |
| 560 | |
| 561 | if 'ARCHFLAGS' in os.environ: |
| 562 | arch = os.environ['ARCHFLAGS'] |
| 563 | for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED', |
| 564 | # a number of derived variables. These need to be |
| 565 | # patched up as well. |
| 566 | 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'): |
| 567 | |
| 568 | flags = _config_vars[key] |
| 569 | flags = re.sub('-arch\s+\w+\s', ' ', flags) |
| 570 | flags = flags + ' ' + arch |
| 571 | _config_vars[key] = flags |
| 572 | |
| 573 | # If we're on OSX 10.5 or later and the user tries to |
| 574 | # compiles an extension using an SDK that is not present |
| 575 | # on the current machine it is better to not use an SDK |
| 576 | # than to fail. |
| 577 | # |
| 578 | # The major usecase for this is users using a Python.org |
| 579 | # binary installer on OSX 10.6: that installer uses |
| 580 | # the 10.4u SDK, but that SDK is not installed by default |
| 581 | # when you install Xcode. |
| 582 | # |
| 583 | m = re.search('-isysroot\s+(\S+)', _config_vars['CFLAGS']) |
| 584 | if m is not None: |
| 585 | sdk = m.group(1) |
| 586 | if not os.path.exists(sdk): |
| 587 | for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED', |
| 588 | # a number of derived variables. These need to be |
| 589 | # patched up as well. |
| 590 | 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'): |
| 591 | |
| 592 | flags = _config_vars[key] |
| 593 | flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags) |
| 594 | _config_vars[key] = flags |
| 595 | |
| 596 | if args: |
| 597 | vals = [] |
| 598 | for name in args: |
| 599 | vals.append(_config_vars.get(name)) |
| 600 | return vals |
| 601 | else: |
| 602 | return _config_vars |
| 603 | |
| 604 | def get_config_var(name): |
| 605 | """Return the value of a single variable using the dictionary |
| 606 | returned by 'get_config_vars()'. Equivalent to |
| 607 | get_config_vars().get(name) |
| 608 | """ |
| 609 | return get_config_vars().get(name) |