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