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