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