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 |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 16 | import warnings |
| 17 | |
| 18 | from functools import partial |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 19 | |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 20 | from .errors import DistutilsPlatformError |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 21 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 22 | from sysconfig import ( |
| 23 | _PREFIX as PREFIX, |
| 24 | _BASE_PREFIX as BASE_PREFIX, |
| 25 | _EXEC_PREFIX as EXEC_PREFIX, |
| 26 | _BASE_EXEC_PREFIX as BASE_EXEC_PREFIX, |
| 27 | _PROJECT_BASE as project_base, |
| 28 | _PYTHON_BUILD as python_build, |
| 29 | _init_posix as sysconfig_init_posix, |
| 30 | parse_config_h as sysconfig_parse_config_h, |
| 31 | _parse_makefile as sysconfig_parse_makefile, |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 32 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 33 | _init_non_posix, |
| 34 | _is_python_source_dir, |
| 35 | _sys_home, |
| 36 | |
| 37 | _variable_rx, |
| 38 | _findvar1_rx, |
| 39 | _findvar2_rx, |
| 40 | |
| 41 | expand_makefile_vars, |
| 42 | is_python_build, |
| 43 | get_config_h_filename, |
| 44 | get_config_var, |
| 45 | get_config_vars, |
| 46 | get_makefile_filename, |
| 47 | get_python_version, |
| 48 | ) |
| 49 | |
| 50 | # This is better than |
| 51 | # from sysconfig import _CONFIG_VARS as _config_vars |
| 52 | # because it makes sure that the global dictionary is initialized |
| 53 | # which might not be true in the time of import. |
| 54 | _config_vars = get_config_vars() |
| 55 | |
| 56 | if os.name == "nt": |
| 57 | from sysconfig import _fix_pcbuild |
| 58 | |
| 59 | warnings.warn( |
| 60 | 'the distutils.sysconfig module is deprecated, use sysconfig instead', |
| 61 | DeprecationWarning, |
| 62 | stacklevel=2 |
| 63 | ) |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 64 | |
Tarek Ziadé | 8b441d0 | 2010-01-29 11:46:31 +0000 | [diff] [blame] | 65 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 66 | # Following functions are the same as in sysconfig but with different API |
| 67 | def parse_config_h(fp, g=None): |
| 68 | return sysconfig_parse_config_h(fp, vars=g) |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 69 | |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 70 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 71 | def parse_makefile(fn, g=None): |
| 72 | return sysconfig_parse_makefile(fn, vars=g, keep_unresolved=False) |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 73 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 74 | _python_build = partial(is_python_build, check_home=True) |
| 75 | _init_posix = partial(sysconfig_init_posix, _config_vars) |
| 76 | _init_nt = partial(_init_non_posix, _config_vars) |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 77 | |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 78 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 79 | # Following functions are deprecated together with this module and they |
| 80 | # have no direct replacement |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 81 | |
Barry Warsaw | 14d98ac | 2010-11-24 19:43:47 +0000 | [diff] [blame] | 82 | # Calculate the build qualifier flags if they are defined. Adding the flags |
| 83 | # to the include and lib directories only makes sense for an installation, not |
| 84 | # an in-source build. |
| 85 | build_flags = '' |
| 86 | try: |
| 87 | if not python_build: |
| 88 | build_flags = sys.abiflags |
| 89 | except AttributeError: |
| 90 | # It's not a configure-based build, so the sys module doesn't have |
| 91 | # this attribute, which is fine. |
| 92 | pass |
| 93 | |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 94 | |
| 95 | def customize_compiler(compiler): |
| 96 | """Do any platform-specific customization of a CCompiler instance. |
| 97 | |
| 98 | Mainly needed on Unix, so we can plug in the information that |
| 99 | varies across Unices and is stored in Python's Makefile. |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 100 | """ |
Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame^] | 101 | if compiler.compiler_type == "unix": |
| 102 | if sys.platform == "darwin": |
| 103 | # Perform first-time customization of compiler-related |
| 104 | # config vars on OS X now that we know we need a compiler. |
| 105 | # This is primarily to support Pythons from binary |
| 106 | # installers. The kind and paths to build tools on |
| 107 | # the user system may vary significantly from the system |
| 108 | # that Python itself was built on. Also the user OS |
| 109 | # version and build tools may not support the same set |
| 110 | # of CPU architectures for universal builds. |
| 111 | if not _config_vars.get('CUSTOMIZED_OSX_COMPILER'): |
| 112 | import _osx_support |
| 113 | _osx_support.customize_compiler(_config_vars) |
| 114 | _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True' |
| 115 | |
| 116 | (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \ |
| 117 | get_config_vars('CC', 'CXX', 'CFLAGS', |
| 118 | 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS') |
| 119 | |
| 120 | if 'CC' in os.environ: |
| 121 | newcc = os.environ['CC'] |
| 122 | if (sys.platform == 'darwin' |
| 123 | and 'LDSHARED' not in os.environ |
| 124 | and ldshared.startswith(cc)): |
| 125 | # On OS X, if CC is overridden, use that as the default |
| 126 | # command for LDSHARED as well |
| 127 | ldshared = newcc + ldshared[len(cc):] |
| 128 | cc = newcc |
| 129 | if 'CXX' in os.environ: |
| 130 | cxx = os.environ['CXX'] |
| 131 | if 'LDSHARED' in os.environ: |
| 132 | ldshared = os.environ['LDSHARED'] |
| 133 | if 'CPP' in os.environ: |
| 134 | cpp = os.environ['CPP'] |
| 135 | else: |
| 136 | cpp = cc + " -E" # not always |
| 137 | if 'LDFLAGS' in os.environ: |
| 138 | ldshared = ldshared + ' ' + os.environ['LDFLAGS'] |
| 139 | if 'CFLAGS' in os.environ: |
| 140 | cflags = cflags + ' ' + os.environ['CFLAGS'] |
| 141 | ldshared = ldshared + ' ' + os.environ['CFLAGS'] |
| 142 | if 'CPPFLAGS' in os.environ: |
| 143 | cpp = cpp + ' ' + os.environ['CPPFLAGS'] |
| 144 | cflags = cflags + ' ' + os.environ['CPPFLAGS'] |
| 145 | ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] |
| 146 | if 'AR' in os.environ: |
| 147 | ar = os.environ['AR'] |
| 148 | if 'ARFLAGS' in os.environ: |
| 149 | archiver = ar + ' ' + os.environ['ARFLAGS'] |
| 150 | else: |
| 151 | archiver = ar + ' ' + ar_flags |
| 152 | |
| 153 | cc_cmd = cc + ' ' + cflags |
| 154 | compiler.set_executables( |
| 155 | preprocessor=cpp, |
| 156 | compiler=cc_cmd, |
| 157 | compiler_so=cc_cmd + ' ' + ccshared, |
| 158 | compiler_cxx=cxx, |
| 159 | linker_so=ldshared, |
| 160 | linker_exe=cc, |
| 161 | archiver=archiver) |
| 162 | |
| 163 | compiler.shared_lib_extension = shlib_suffix |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 164 | |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 165 | |
| 166 | def get_python_inc(plat_specific=0, prefix=None): |
| 167 | """Return the directory containing installed Python header files. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 168 | |
| 169 | If 'plat_specific' is false (the default), this is the path to the |
| 170 | non-platform-specific header files, i.e. Python.h and so on; |
| 171 | otherwise, this is the path to platform-specific header files |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 172 | (namely pyconfig.h). |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 173 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 174 | If 'prefix' is supplied, use it instead of sys.base_prefix or |
| 175 | sys.base_exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 176 | """ |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 177 | if prefix is None: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 178 | prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 179 | if os.name == "posix": |
| 180 | if python_build: |
Vinay Sajip | ae7d7fa | 2010-09-20 10:29:54 +0000 | [diff] [blame] | 181 | # Assume the executable is in the build directory. The |
| 182 | # pyconfig.h file should be in the same directory. Since |
| 183 | # the build directory may not be the source directory, we |
| 184 | # must use "srcdir" from the makefile to find the "Include" |
| 185 | # directory. |
Vinay Sajip | ae7d7fa | 2010-09-20 10:29:54 +0000 | [diff] [blame] | 186 | if plat_specific: |
Jeremy Kloth | dbdea62 | 2017-05-09 09:24:13 -0600 | [diff] [blame] | 187 | return _sys_home or project_base |
Vinay Sajip | ae7d7fa | 2010-09-20 10:29:54 +0000 | [diff] [blame] | 188 | else: |
Vinay Sajip | 048b063 | 2012-07-16 18:24:55 +0100 | [diff] [blame] | 189 | incdir = os.path.join(get_config_var('srcdir'), 'Include') |
Jeremy Kloth | dbdea62 | 2017-05-09 09:24:13 -0600 | [diff] [blame] | 190 | return os.path.normpath(incdir) |
Barry Warsaw | 14d98ac | 2010-11-24 19:43:47 +0000 | [diff] [blame] | 191 | python_dir = 'python' + get_python_version() + build_flags |
| 192 | return os.path.join(prefix, "include", python_dir) |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 193 | elif os.name == "nt": |
Steve Dower | 85e102a | 2019-02-04 17:15:13 -0800 | [diff] [blame] | 194 | if python_build: |
| 195 | # Include both the include and PC dir to ensure we can find |
| 196 | # pyconfig.h |
| 197 | return (os.path.join(prefix, "include") + os.path.pathsep + |
| 198 | os.path.join(prefix, "PC")) |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 199 | return os.path.join(prefix, "include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 200 | else: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 201 | raise DistutilsPlatformError( |
| 202 | "I don't know where Python installs its C header files " |
| 203 | "on platform '%s'" % os.name) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 204 | |
| 205 | |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 206 | def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
| 207 | """Return the directory containing the Python library (standard or |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 208 | site additions). |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 209 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 210 | If 'plat_specific' is true, return the directory containing |
| 211 | platform-specific modules, i.e. any module from a non-pure-Python |
| 212 | module distribution; otherwise, return the platform-shared library |
| 213 | directory. If 'standard_lib' is true, return the directory |
| 214 | containing standard Python library modules; otherwise, return the |
| 215 | directory for site-specific modules. |
| 216 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 217 | If 'prefix' is supplied, use it instead of sys.base_prefix or |
| 218 | sys.base_exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 219 | """ |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 220 | if prefix is None: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 221 | if standard_lib: |
| 222 | prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX |
| 223 | else: |
| 224 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 225 | |
| 226 | if os.name == "posix": |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 227 | if plat_specific or standard_lib: |
| 228 | # Platform-specific modules (any module from a non-pure-Python |
| 229 | # module distribution) or standard Python library modules. |
| 230 | libdir = sys.platlibdir |
| 231 | else: |
| 232 | # Pure Python |
| 233 | libdir = "lib" |
| 234 | libpython = os.path.join(prefix, libdir, |
| 235 | "python" + get_python_version()) |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 236 | if standard_lib: |
| 237 | return libpython |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 238 | else: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 239 | return os.path.join(libpython, "site-packages") |
| 240 | elif os.name == "nt": |
| 241 | if standard_lib: |
| 242 | return os.path.join(prefix, "Lib") |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 243 | else: |
Benjamin Peterson | df0eb95 | 2014-09-06 17:24:12 -0400 | [diff] [blame] | 244 | return os.path.join(prefix, "Lib", "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 245 | else: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 246 | raise DistutilsPlatformError( |
| 247 | "I don't know where Python installs its library " |
| 248 | "on platform '%s'" % os.name) |