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