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