| 1 | n/a | """Provide access to Python's configuration information. The specific |
|---|
| 2 | n/a | configuration variables available depend heavily on the platform and |
|---|
| 3 | n/a | configuration. The values may be retrieved using |
|---|
| 4 | n/a | get_config_var(name), and the list of variables is available via |
|---|
| 5 | n/a | get_config_vars().keys(). Additional convenience functions are also |
|---|
| 6 | n/a | available. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | Written by: Fred L. Drake, Jr. |
|---|
| 9 | n/a | Email: <fdrake@acm.org> |
|---|
| 10 | n/a | """ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | import _imp |
|---|
| 13 | n/a | import os |
|---|
| 14 | n/a | import re |
|---|
| 15 | n/a | import sys |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | from .errors import DistutilsPlatformError |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | # These are needed in a couple of spots, so just compute them once. |
|---|
| 20 | n/a | PREFIX = os.path.normpath(sys.prefix) |
|---|
| 21 | n/a | EXEC_PREFIX = os.path.normpath(sys.exec_prefix) |
|---|
| 22 | n/a | BASE_PREFIX = os.path.normpath(sys.base_prefix) |
|---|
| 23 | n/a | BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # Path to the base directory of the project. On Windows the binary may |
|---|
| 26 | n/a | # live in project/PCBuild/win32 or project/PCBuild/amd64. |
|---|
| 27 | n/a | # set for cross builds |
|---|
| 28 | n/a | if "_PYTHON_PROJECT_BASE" in os.environ: |
|---|
| 29 | n/a | project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"]) |
|---|
| 30 | n/a | else: |
|---|
| 31 | n/a | project_base = os.path.dirname(os.path.abspath(sys.executable)) |
|---|
| 32 | n/a | if (os.name == 'nt' and |
|---|
| 33 | n/a | project_base.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))): |
|---|
| 34 | n/a | project_base = os.path.dirname(os.path.dirname(project_base)) |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | # python_build: (Boolean) if true, we're either building Python or |
|---|
| 37 | n/a | # building an extension with an un-installed Python, so we use |
|---|
| 38 | n/a | # different (hard-wired) directories. |
|---|
| 39 | n/a | # Setup.local is available for Makefile builds including VPATH builds, |
|---|
| 40 | n/a | # Setup.dist is available on Windows |
|---|
| 41 | n/a | def _is_python_source_dir(d): |
|---|
| 42 | n/a | for fn in ("Setup.dist", "Setup.local"): |
|---|
| 43 | n/a | if os.path.isfile(os.path.join(d, "Modules", fn)): |
|---|
| 44 | n/a | return True |
|---|
| 45 | n/a | return False |
|---|
| 46 | n/a | _sys_home = getattr(sys, '_home', None) |
|---|
| 47 | n/a | if (_sys_home and os.name == 'nt' and |
|---|
| 48 | n/a | _sys_home.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))): |
|---|
| 49 | n/a | _sys_home = os.path.dirname(os.path.dirname(_sys_home)) |
|---|
| 50 | n/a | def _python_build(): |
|---|
| 51 | n/a | if _sys_home: |
|---|
| 52 | n/a | return _is_python_source_dir(_sys_home) |
|---|
| 53 | n/a | return _is_python_source_dir(project_base) |
|---|
| 54 | n/a | python_build = _python_build() |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | # Calculate the build qualifier flags if they are defined. Adding the flags |
|---|
| 57 | n/a | # to the include and lib directories only makes sense for an installation, not |
|---|
| 58 | n/a | # an in-source build. |
|---|
| 59 | n/a | build_flags = '' |
|---|
| 60 | n/a | try: |
|---|
| 61 | n/a | if not python_build: |
|---|
| 62 | n/a | build_flags = sys.abiflags |
|---|
| 63 | n/a | except AttributeError: |
|---|
| 64 | n/a | # It's not a configure-based build, so the sys module doesn't have |
|---|
| 65 | n/a | # this attribute, which is fine. |
|---|
| 66 | n/a | pass |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def get_python_version(): |
|---|
| 69 | n/a | """Return a string containing the major and minor Python version, |
|---|
| 70 | n/a | leaving off the patchlevel. Sample return values could be '1.5' |
|---|
| 71 | n/a | or '2.2'. |
|---|
| 72 | n/a | """ |
|---|
| 73 | n/a | return '%d.%d' % sys.version_info[:2] |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def get_python_inc(plat_specific=0, prefix=None): |
|---|
| 77 | n/a | """Return the directory containing installed Python header files. |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | If 'plat_specific' is false (the default), this is the path to the |
|---|
| 80 | n/a | non-platform-specific header files, i.e. Python.h and so on; |
|---|
| 81 | n/a | otherwise, this is the path to platform-specific header files |
|---|
| 82 | n/a | (namely pyconfig.h). |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | If 'prefix' is supplied, use it instead of sys.base_prefix or |
|---|
| 85 | n/a | sys.base_exec_prefix -- i.e., ignore 'plat_specific'. |
|---|
| 86 | n/a | """ |
|---|
| 87 | n/a | if prefix is None: |
|---|
| 88 | n/a | prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX |
|---|
| 89 | n/a | if os.name == "posix": |
|---|
| 90 | n/a | if python_build: |
|---|
| 91 | n/a | # Assume the executable is in the build directory. The |
|---|
| 92 | n/a | # pyconfig.h file should be in the same directory. Since |
|---|
| 93 | n/a | # the build directory may not be the source directory, we |
|---|
| 94 | n/a | # must use "srcdir" from the makefile to find the "Include" |
|---|
| 95 | n/a | # directory. |
|---|
| 96 | n/a | base = _sys_home or project_base |
|---|
| 97 | n/a | if plat_specific: |
|---|
| 98 | n/a | return base |
|---|
| 99 | n/a | if _sys_home: |
|---|
| 100 | n/a | incdir = os.path.join(_sys_home, get_config_var('AST_H_DIR')) |
|---|
| 101 | n/a | else: |
|---|
| 102 | n/a | incdir = os.path.join(get_config_var('srcdir'), 'Include') |
|---|
| 103 | n/a | return os.path.normpath(incdir) |
|---|
| 104 | n/a | python_dir = 'python' + get_python_version() + build_flags |
|---|
| 105 | n/a | return os.path.join(prefix, "include", python_dir) |
|---|
| 106 | n/a | elif os.name == "nt": |
|---|
| 107 | n/a | return os.path.join(prefix, "include") |
|---|
| 108 | n/a | else: |
|---|
| 109 | n/a | raise DistutilsPlatformError( |
|---|
| 110 | n/a | "I don't know where Python installs its C header files " |
|---|
| 111 | n/a | "on platform '%s'" % os.name) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
|---|
| 115 | n/a | """Return the directory containing the Python library (standard or |
|---|
| 116 | n/a | site additions). |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | If 'plat_specific' is true, return the directory containing |
|---|
| 119 | n/a | platform-specific modules, i.e. any module from a non-pure-Python |
|---|
| 120 | n/a | module distribution; otherwise, return the platform-shared library |
|---|
| 121 | n/a | directory. If 'standard_lib' is true, return the directory |
|---|
| 122 | n/a | containing standard Python library modules; otherwise, return the |
|---|
| 123 | n/a | directory for site-specific modules. |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | If 'prefix' is supplied, use it instead of sys.base_prefix or |
|---|
| 126 | n/a | sys.base_exec_prefix -- i.e., ignore 'plat_specific'. |
|---|
| 127 | n/a | """ |
|---|
| 128 | n/a | if prefix is None: |
|---|
| 129 | n/a | if standard_lib: |
|---|
| 130 | n/a | prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX |
|---|
| 131 | n/a | else: |
|---|
| 132 | n/a | prefix = plat_specific and EXEC_PREFIX or PREFIX |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | if os.name == "posix": |
|---|
| 135 | n/a | libpython = os.path.join(prefix, |
|---|
| 136 | n/a | "lib", "python" + get_python_version()) |
|---|
| 137 | n/a | if standard_lib: |
|---|
| 138 | n/a | return libpython |
|---|
| 139 | n/a | else: |
|---|
| 140 | n/a | return os.path.join(libpython, "site-packages") |
|---|
| 141 | n/a | elif os.name == "nt": |
|---|
| 142 | n/a | if standard_lib: |
|---|
| 143 | n/a | return os.path.join(prefix, "Lib") |
|---|
| 144 | n/a | else: |
|---|
| 145 | n/a | return os.path.join(prefix, "Lib", "site-packages") |
|---|
| 146 | n/a | else: |
|---|
| 147 | n/a | raise DistutilsPlatformError( |
|---|
| 148 | n/a | "I don't know where Python installs its library " |
|---|
| 149 | n/a | "on platform '%s'" % os.name) |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def customize_compiler(compiler): |
|---|
| 154 | n/a | """Do any platform-specific customization of a CCompiler instance. |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | Mainly needed on Unix, so we can plug in the information that |
|---|
| 157 | n/a | varies across Unices and is stored in Python's Makefile. |
|---|
| 158 | n/a | """ |
|---|
| 159 | n/a | if compiler.compiler_type == "unix": |
|---|
| 160 | n/a | if sys.platform == "darwin": |
|---|
| 161 | n/a | # Perform first-time customization of compiler-related |
|---|
| 162 | n/a | # config vars on OS X now that we know we need a compiler. |
|---|
| 163 | n/a | # This is primarily to support Pythons from binary |
|---|
| 164 | n/a | # installers. The kind and paths to build tools on |
|---|
| 165 | n/a | # the user system may vary significantly from the system |
|---|
| 166 | n/a | # that Python itself was built on. Also the user OS |
|---|
| 167 | n/a | # version and build tools may not support the same set |
|---|
| 168 | n/a | # of CPU architectures for universal builds. |
|---|
| 169 | n/a | global _config_vars |
|---|
| 170 | n/a | # Use get_config_var() to ensure _config_vars is initialized. |
|---|
| 171 | n/a | if not get_config_var('CUSTOMIZED_OSX_COMPILER'): |
|---|
| 172 | n/a | import _osx_support |
|---|
| 173 | n/a | _osx_support.customize_compiler(_config_vars) |
|---|
| 174 | n/a | _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True' |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | (cc, cxx, opt, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \ |
|---|
| 177 | n/a | get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS', |
|---|
| 178 | n/a | 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS') |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | if 'CC' in os.environ: |
|---|
| 181 | n/a | newcc = os.environ['CC'] |
|---|
| 182 | n/a | if (sys.platform == 'darwin' |
|---|
| 183 | n/a | and 'LDSHARED' not in os.environ |
|---|
| 184 | n/a | and ldshared.startswith(cc)): |
|---|
| 185 | n/a | # On OS X, if CC is overridden, use that as the default |
|---|
| 186 | n/a | # command for LDSHARED as well |
|---|
| 187 | n/a | ldshared = newcc + ldshared[len(cc):] |
|---|
| 188 | n/a | cc = newcc |
|---|
| 189 | n/a | if 'CXX' in os.environ: |
|---|
| 190 | n/a | cxx = os.environ['CXX'] |
|---|
| 191 | n/a | if 'LDSHARED' in os.environ: |
|---|
| 192 | n/a | ldshared = os.environ['LDSHARED'] |
|---|
| 193 | n/a | if 'CPP' in os.environ: |
|---|
| 194 | n/a | cpp = os.environ['CPP'] |
|---|
| 195 | n/a | else: |
|---|
| 196 | n/a | cpp = cc + " -E" # not always |
|---|
| 197 | n/a | if 'LDFLAGS' in os.environ: |
|---|
| 198 | n/a | ldshared = ldshared + ' ' + os.environ['LDFLAGS'] |
|---|
| 199 | n/a | if 'CFLAGS' in os.environ: |
|---|
| 200 | n/a | cflags = opt + ' ' + os.environ['CFLAGS'] |
|---|
| 201 | n/a | ldshared = ldshared + ' ' + os.environ['CFLAGS'] |
|---|
| 202 | n/a | if 'CPPFLAGS' in os.environ: |
|---|
| 203 | n/a | cpp = cpp + ' ' + os.environ['CPPFLAGS'] |
|---|
| 204 | n/a | cflags = cflags + ' ' + os.environ['CPPFLAGS'] |
|---|
| 205 | n/a | ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] |
|---|
| 206 | n/a | if 'AR' in os.environ: |
|---|
| 207 | n/a | ar = os.environ['AR'] |
|---|
| 208 | n/a | if 'ARFLAGS' in os.environ: |
|---|
| 209 | n/a | archiver = ar + ' ' + os.environ['ARFLAGS'] |
|---|
| 210 | n/a | else: |
|---|
| 211 | n/a | archiver = ar + ' ' + ar_flags |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | cc_cmd = cc + ' ' + cflags |
|---|
| 214 | n/a | compiler.set_executables( |
|---|
| 215 | n/a | preprocessor=cpp, |
|---|
| 216 | n/a | compiler=cc_cmd, |
|---|
| 217 | n/a | compiler_so=cc_cmd + ' ' + ccshared, |
|---|
| 218 | n/a | compiler_cxx=cxx, |
|---|
| 219 | n/a | linker_so=ldshared, |
|---|
| 220 | n/a | linker_exe=cc, |
|---|
| 221 | n/a | archiver=archiver) |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | compiler.shared_lib_extension = shlib_suffix |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | def get_config_h_filename(): |
|---|
| 227 | n/a | """Return full pathname of installed pyconfig.h file.""" |
|---|
| 228 | n/a | if python_build: |
|---|
| 229 | n/a | if os.name == "nt": |
|---|
| 230 | n/a | inc_dir = os.path.join(_sys_home or project_base, "PC") |
|---|
| 231 | n/a | else: |
|---|
| 232 | n/a | inc_dir = _sys_home or project_base |
|---|
| 233 | n/a | else: |
|---|
| 234 | n/a | inc_dir = get_python_inc(plat_specific=1) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | return os.path.join(inc_dir, 'pyconfig.h') |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | def get_makefile_filename(): |
|---|
| 240 | n/a | """Return full pathname of installed Makefile from the Python build.""" |
|---|
| 241 | n/a | if python_build: |
|---|
| 242 | n/a | return os.path.join(_sys_home or project_base, "Makefile") |
|---|
| 243 | n/a | lib_dir = get_python_lib(plat_specific=0, standard_lib=1) |
|---|
| 244 | n/a | config_file = 'config-{}{}'.format(get_python_version(), build_flags) |
|---|
| 245 | n/a | if hasattr(sys.implementation, '_multiarch'): |
|---|
| 246 | n/a | config_file += '-%s' % sys.implementation._multiarch |
|---|
| 247 | n/a | return os.path.join(lib_dir, config_file, 'Makefile') |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | def parse_config_h(fp, g=None): |
|---|
| 251 | n/a | """Parse a config.h-style file. |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | A dictionary containing name/value pairs is returned. If an |
|---|
| 254 | n/a | optional dictionary is passed in as the second argument, it is |
|---|
| 255 | n/a | used instead of a new dictionary. |
|---|
| 256 | n/a | """ |
|---|
| 257 | n/a | if g is None: |
|---|
| 258 | n/a | g = {} |
|---|
| 259 | n/a | define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") |
|---|
| 260 | n/a | undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") |
|---|
| 261 | n/a | # |
|---|
| 262 | n/a | while True: |
|---|
| 263 | n/a | line = fp.readline() |
|---|
| 264 | n/a | if not line: |
|---|
| 265 | n/a | break |
|---|
| 266 | n/a | m = define_rx.match(line) |
|---|
| 267 | n/a | if m: |
|---|
| 268 | n/a | n, v = m.group(1, 2) |
|---|
| 269 | n/a | try: v = int(v) |
|---|
| 270 | n/a | except ValueError: pass |
|---|
| 271 | n/a | g[n] = v |
|---|
| 272 | n/a | else: |
|---|
| 273 | n/a | m = undef_rx.match(line) |
|---|
| 274 | n/a | if m: |
|---|
| 275 | n/a | g[m.group(1)] = 0 |
|---|
| 276 | n/a | return g |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | # Regexes needed for parsing Makefile (and similar syntaxes, |
|---|
| 280 | n/a | # like old-style Setup files). |
|---|
| 281 | n/a | _variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") |
|---|
| 282 | n/a | _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
|---|
| 283 | n/a | _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def parse_makefile(fn, g=None): |
|---|
| 286 | n/a | """Parse a Makefile-style file. |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | A dictionary containing name/value pairs is returned. If an |
|---|
| 289 | n/a | optional dictionary is passed in as the second argument, it is |
|---|
| 290 | n/a | used instead of a new dictionary. |
|---|
| 291 | n/a | """ |
|---|
| 292 | n/a | from distutils.text_file import TextFile |
|---|
| 293 | n/a | fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape") |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | if g is None: |
|---|
| 296 | n/a | g = {} |
|---|
| 297 | n/a | done = {} |
|---|
| 298 | n/a | notdone = {} |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | while True: |
|---|
| 301 | n/a | line = fp.readline() |
|---|
| 302 | n/a | if line is None: # eof |
|---|
| 303 | n/a | break |
|---|
| 304 | n/a | m = _variable_rx.match(line) |
|---|
| 305 | n/a | if m: |
|---|
| 306 | n/a | n, v = m.group(1, 2) |
|---|
| 307 | n/a | v = v.strip() |
|---|
| 308 | n/a | # `$$' is a literal `$' in make |
|---|
| 309 | n/a | tmpv = v.replace('$$', '') |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | if "$" in tmpv: |
|---|
| 312 | n/a | notdone[n] = v |
|---|
| 313 | n/a | else: |
|---|
| 314 | n/a | try: |
|---|
| 315 | n/a | v = int(v) |
|---|
| 316 | n/a | except ValueError: |
|---|
| 317 | n/a | # insert literal `$' |
|---|
| 318 | n/a | done[n] = v.replace('$$', '$') |
|---|
| 319 | n/a | else: |
|---|
| 320 | n/a | done[n] = v |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | # Variables with a 'PY_' prefix in the makefile. These need to |
|---|
| 323 | n/a | # be made available without that prefix through sysconfig. |
|---|
| 324 | n/a | # Special care is needed to ensure that variable expansion works, even |
|---|
| 325 | n/a | # if the expansion uses the name without a prefix. |
|---|
| 326 | n/a | renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS') |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | # do variable interpolation here |
|---|
| 329 | n/a | while notdone: |
|---|
| 330 | n/a | for name in list(notdone): |
|---|
| 331 | n/a | value = notdone[name] |
|---|
| 332 | n/a | m = _findvar1_rx.search(value) or _findvar2_rx.search(value) |
|---|
| 333 | n/a | if m: |
|---|
| 334 | n/a | n = m.group(1) |
|---|
| 335 | n/a | found = True |
|---|
| 336 | n/a | if n in done: |
|---|
| 337 | n/a | item = str(done[n]) |
|---|
| 338 | n/a | elif n in notdone: |
|---|
| 339 | n/a | # get it on a subsequent round |
|---|
| 340 | n/a | found = False |
|---|
| 341 | n/a | elif n in os.environ: |
|---|
| 342 | n/a | # do it like make: fall back to environment |
|---|
| 343 | n/a | item = os.environ[n] |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | elif n in renamed_variables: |
|---|
| 346 | n/a | if name.startswith('PY_') and name[3:] in renamed_variables: |
|---|
| 347 | n/a | item = "" |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | elif 'PY_' + n in notdone: |
|---|
| 350 | n/a | found = False |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | else: |
|---|
| 353 | n/a | item = str(done['PY_' + n]) |
|---|
| 354 | n/a | else: |
|---|
| 355 | n/a | done[n] = item = "" |
|---|
| 356 | n/a | if found: |
|---|
| 357 | n/a | after = value[m.end():] |
|---|
| 358 | n/a | value = value[:m.start()] + item + after |
|---|
| 359 | n/a | if "$" in after: |
|---|
| 360 | n/a | notdone[name] = value |
|---|
| 361 | n/a | else: |
|---|
| 362 | n/a | try: value = int(value) |
|---|
| 363 | n/a | except ValueError: |
|---|
| 364 | n/a | done[name] = value.strip() |
|---|
| 365 | n/a | else: |
|---|
| 366 | n/a | done[name] = value |
|---|
| 367 | n/a | del notdone[name] |
|---|
| 368 | n/a | |
|---|
| 369 | n/a | if name.startswith('PY_') \ |
|---|
| 370 | n/a | and name[3:] in renamed_variables: |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | name = name[3:] |
|---|
| 373 | n/a | if name not in done: |
|---|
| 374 | n/a | done[name] = value |
|---|
| 375 | n/a | else: |
|---|
| 376 | n/a | # bogus variable reference; just drop it since we can't deal |
|---|
| 377 | n/a | del notdone[name] |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | fp.close() |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | # strip spurious spaces |
|---|
| 382 | n/a | for k, v in done.items(): |
|---|
| 383 | n/a | if isinstance(v, str): |
|---|
| 384 | n/a | done[k] = v.strip() |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | # save the results in the global dictionary |
|---|
| 387 | n/a | g.update(done) |
|---|
| 388 | n/a | return g |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | def expand_makefile_vars(s, vars): |
|---|
| 392 | n/a | """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in |
|---|
| 393 | n/a | 'string' according to 'vars' (a dictionary mapping variable names to |
|---|
| 394 | n/a | values). Variables not present in 'vars' are silently expanded to the |
|---|
| 395 | n/a | empty string. The variable values in 'vars' should not contain further |
|---|
| 396 | n/a | variable expansions; if 'vars' is the output of 'parse_makefile()', |
|---|
| 397 | n/a | you're fine. Returns a variable-expanded version of 's'. |
|---|
| 398 | n/a | """ |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | # This algorithm does multiple expansion, so if vars['foo'] contains |
|---|
| 401 | n/a | # "${bar}", it will expand ${foo} to ${bar}, and then expand |
|---|
| 402 | n/a | # ${bar}... and so forth. This is fine as long as 'vars' comes from |
|---|
| 403 | n/a | # 'parse_makefile()', which takes care of such expansions eagerly, |
|---|
| 404 | n/a | # according to make's variable expansion semantics. |
|---|
| 405 | n/a | |
|---|
| 406 | n/a | while True: |
|---|
| 407 | n/a | m = _findvar1_rx.search(s) or _findvar2_rx.search(s) |
|---|
| 408 | n/a | if m: |
|---|
| 409 | n/a | (beg, end) = m.span() |
|---|
| 410 | n/a | s = s[0:beg] + vars.get(m.group(1)) + s[end:] |
|---|
| 411 | n/a | else: |
|---|
| 412 | n/a | break |
|---|
| 413 | n/a | return s |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | _config_vars = None |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | def _init_posix(): |
|---|
| 419 | n/a | """Initialize the module as appropriate for POSIX systems.""" |
|---|
| 420 | n/a | # _sysconfigdata is generated at build time, see the sysconfig module |
|---|
| 421 | n/a | name = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME', |
|---|
| 422 | n/a | '_sysconfigdata_{abi}_{platform}_{multiarch}'.format( |
|---|
| 423 | n/a | abi=sys.abiflags, |
|---|
| 424 | n/a | platform=sys.platform, |
|---|
| 425 | n/a | multiarch=getattr(sys.implementation, '_multiarch', ''), |
|---|
| 426 | n/a | )) |
|---|
| 427 | n/a | _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0) |
|---|
| 428 | n/a | build_time_vars = _temp.build_time_vars |
|---|
| 429 | n/a | global _config_vars |
|---|
| 430 | n/a | _config_vars = {} |
|---|
| 431 | n/a | _config_vars.update(build_time_vars) |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | def _init_nt(): |
|---|
| 435 | n/a | """Initialize the module as appropriate for NT""" |
|---|
| 436 | n/a | g = {} |
|---|
| 437 | n/a | # set basic install directories |
|---|
| 438 | n/a | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
|---|
| 439 | n/a | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
|---|
| 440 | n/a | |
|---|
| 441 | n/a | # XXX hmmm.. a normal install puts include files here |
|---|
| 442 | n/a | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | g['EXT_SUFFIX'] = _imp.extension_suffixes()[0] |
|---|
| 445 | n/a | g['EXE'] = ".exe" |
|---|
| 446 | n/a | g['VERSION'] = get_python_version().replace(".", "") |
|---|
| 447 | n/a | g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable)) |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | global _config_vars |
|---|
| 450 | n/a | _config_vars = g |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | def get_config_vars(*args): |
|---|
| 454 | n/a | """With no arguments, return a dictionary of all configuration |
|---|
| 455 | n/a | variables relevant for the current platform. Generally this includes |
|---|
| 456 | n/a | everything needed to build extensions and install both pure modules and |
|---|
| 457 | n/a | extensions. On Unix, this means every variable defined in Python's |
|---|
| 458 | n/a | installed Makefile; on Windows it's a much smaller set. |
|---|
| 459 | n/a | |
|---|
| 460 | n/a | With arguments, return a list of values that result from looking up |
|---|
| 461 | n/a | each argument in the configuration variable dictionary. |
|---|
| 462 | n/a | """ |
|---|
| 463 | n/a | global _config_vars |
|---|
| 464 | n/a | if _config_vars is None: |
|---|
| 465 | n/a | func = globals().get("_init_" + os.name) |
|---|
| 466 | n/a | if func: |
|---|
| 467 | n/a | func() |
|---|
| 468 | n/a | else: |
|---|
| 469 | n/a | _config_vars = {} |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | # Normalized versions of prefix and exec_prefix are handy to have; |
|---|
| 472 | n/a | # in fact, these are the standard versions used most places in the |
|---|
| 473 | n/a | # Distutils. |
|---|
| 474 | n/a | _config_vars['prefix'] = PREFIX |
|---|
| 475 | n/a | _config_vars['exec_prefix'] = EXEC_PREFIX |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | # For backward compatibility, see issue19555 |
|---|
| 478 | n/a | SO = _config_vars.get('EXT_SUFFIX') |
|---|
| 479 | n/a | if SO is not None: |
|---|
| 480 | n/a | _config_vars['SO'] = SO |
|---|
| 481 | n/a | |
|---|
| 482 | n/a | # Always convert srcdir to an absolute path |
|---|
| 483 | n/a | srcdir = _config_vars.get('srcdir', project_base) |
|---|
| 484 | n/a | if os.name == 'posix': |
|---|
| 485 | n/a | if python_build: |
|---|
| 486 | n/a | # If srcdir is a relative path (typically '.' or '..') |
|---|
| 487 | n/a | # then it should be interpreted relative to the directory |
|---|
| 488 | n/a | # containing Makefile. |
|---|
| 489 | n/a | base = os.path.dirname(get_makefile_filename()) |
|---|
| 490 | n/a | srcdir = os.path.join(base, srcdir) |
|---|
| 491 | n/a | else: |
|---|
| 492 | n/a | # srcdir is not meaningful since the installation is |
|---|
| 493 | n/a | # spread about the filesystem. We choose the |
|---|
| 494 | n/a | # directory containing the Makefile since we know it |
|---|
| 495 | n/a | # exists. |
|---|
| 496 | n/a | srcdir = os.path.dirname(get_makefile_filename()) |
|---|
| 497 | n/a | _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir)) |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | # Convert srcdir into an absolute path if it appears necessary. |
|---|
| 500 | n/a | # Normally it is relative to the build directory. However, during |
|---|
| 501 | n/a | # testing, for example, we might be running a non-installed python |
|---|
| 502 | n/a | # from a different directory. |
|---|
| 503 | n/a | if python_build and os.name == "posix": |
|---|
| 504 | n/a | base = project_base |
|---|
| 505 | n/a | if (not os.path.isabs(_config_vars['srcdir']) and |
|---|
| 506 | n/a | base != os.getcwd()): |
|---|
| 507 | n/a | # srcdir is relative and we are not in the same directory |
|---|
| 508 | n/a | # as the executable. Assume executable is in the build |
|---|
| 509 | n/a | # directory and make srcdir absolute. |
|---|
| 510 | n/a | srcdir = os.path.join(base, _config_vars['srcdir']) |
|---|
| 511 | n/a | _config_vars['srcdir'] = os.path.normpath(srcdir) |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | # OS X platforms require special customization to handle |
|---|
| 514 | n/a | # multi-architecture, multi-os-version installers |
|---|
| 515 | n/a | if sys.platform == 'darwin': |
|---|
| 516 | n/a | import _osx_support |
|---|
| 517 | n/a | _osx_support.customize_config_vars(_config_vars) |
|---|
| 518 | n/a | |
|---|
| 519 | n/a | if args: |
|---|
| 520 | n/a | vals = [] |
|---|
| 521 | n/a | for name in args: |
|---|
| 522 | n/a | vals.append(_config_vars.get(name)) |
|---|
| 523 | n/a | return vals |
|---|
| 524 | n/a | else: |
|---|
| 525 | n/a | return _config_vars |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | def get_config_var(name): |
|---|
| 528 | n/a | """Return the value of a single variable using the dictionary |
|---|
| 529 | n/a | returned by 'get_config_vars()'. Equivalent to |
|---|
| 530 | n/a | get_config_vars().get(name) |
|---|
| 531 | n/a | """ |
|---|
| 532 | n/a | if name == 'SO': |
|---|
| 533 | n/a | import warnings |
|---|
| 534 | n/a | warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2) |
|---|
| 535 | n/a | return get_config_vars().get(name) |
|---|