blob: 439040deb5619da0f4b05614e4417d1cdf99dc6b [file] [log] [blame]
Fred Drake70b014d2001-07-18 18:39:56 +00001"""Provide access to Python's configuration information. The specific
2configuration variables available depend heavily on the platform and
3configuration. The values may be retrieved using
4get_config_var(name), and the list of variables is available via
5get_config_vars().keys(). Additional convenience functions are also
6available.
Greg Ward1190ee31998-12-18 23:46:33 +00007
8Written by: Fred L. Drake, Jr.
9Email: <fdrake@acm.org>
Greg Ward1190ee31998-12-18 23:46:33 +000010"""
11
Steve Dower65e4cb12014-11-22 12:54:57 -080012import _imp
Greg Ward9ddaaa11999-01-06 14:46:06 +000013import os
14import re
Tarek Ziadé36797272010-07-22 12:50:05 +000015import sys
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020016import warnings
17
18from functools import partial
Greg Ward1190ee31998-12-18 23:46:33 +000019
Tarek Ziadé36797272010-07-22 12:50:05 +000020from .errors import DistutilsPlatformError
Greg Warda0ca3f22000-02-02 00:05:14 +000021
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020022from 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 Drakec1ee39a2000-03-09 15:54:52 +000032
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020033 _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
56if os.name == "nt":
57 from sysconfig import _fix_pcbuild
58
59warnings.warn(
60 'the distutils.sysconfig module is deprecated, use sysconfig instead',
61 DeprecationWarning,
62 stacklevel=2
63)
Steve Dower85e102a2019-02-04 17:15:13 -080064
Tarek Ziadé8b441d02010-01-29 11:46:31 +000065
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020066# Following functions are the same as in sysconfig but with different API
67def parse_config_h(fp, g=None):
68 return sysconfig_parse_config_h(fp, vars=g)
Steve Dower85e102a2019-02-04 17:15:13 -080069
Steve Dower85e102a2019-02-04 17:15:13 -080070
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020071def parse_makefile(fn, g=None):
72 return sysconfig_parse_makefile(fn, vars=g, keep_unresolved=False)
Steve Dower85e102a2019-02-04 17:15:13 -080073
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020074_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 Dower85e102a2019-02-04 17:15:13 -080077
Fred Drakec916cdc2001-08-02 20:03:12 +000078
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +020079# Following functions are deprecated together with this module and they
80# have no direct replacement
Steve Dower85e102a2019-02-04 17:15:13 -080081
Barry Warsaw14d98ac2010-11-24 19:43:47 +000082# 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.
85build_flags = ''
86try:
87 if not python_build:
88 build_flags = sys.abiflags
89except 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' Balhar90d02e52021-04-23 14:02:41 +020094
95def 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é36797272010-07-22 12:50:05 +0000100 """
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +0200101 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éedacea32010-01-29 11:41:03 +0000164
Tarek Ziadé36797272010-07-22 12:50:05 +0000165
166def get_python_inc(plat_specific=0, prefix=None):
167 """Return the directory containing installed Python header files.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000168
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öwis4f1cd8b2001-07-26 13:41:06 +0000172 (namely pyconfig.h).
Fred Drakec1ee39a2000-03-09 15:54:52 +0000173
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100174 If 'prefix' is supplied, use it instead of sys.base_prefix or
175 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakeb94b8492001-12-06 20:51:35 +0000176 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000177 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100178 prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Tarek Ziadé36797272010-07-22 12:50:05 +0000179 if os.name == "posix":
180 if python_build:
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000181 # 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 Sajipae7d7fa2010-09-20 10:29:54 +0000186 if plat_specific:
Jeremy Klothdbdea622017-05-09 09:24:13 -0600187 return _sys_home or project_base
Vinay Sajipae7d7fa2010-09-20 10:29:54 +0000188 else:
Vinay Sajip048b0632012-07-16 18:24:55 +0100189 incdir = os.path.join(get_config_var('srcdir'), 'Include')
Jeremy Klothdbdea622017-05-09 09:24:13 -0600190 return os.path.normpath(incdir)
Barry Warsaw14d98ac2010-11-24 19:43:47 +0000191 python_dir = 'python' + get_python_version() + build_flags
192 return os.path.join(prefix, "include", python_dir)
Tarek Ziadé36797272010-07-22 12:50:05 +0000193 elif os.name == "nt":
Steve Dower85e102a2019-02-04 17:15:13 -0800194 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é36797272010-07-22 12:50:05 +0000199 return os.path.join(prefix, "include")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000200 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000201 raise DistutilsPlatformError(
202 "I don't know where Python installs its C header files "
203 "on platform '%s'" % os.name)
Greg Ward7d73b9e2000-03-09 03:16:05 +0000204
205
Tarek Ziadé36797272010-07-22 12:50:05 +0000206def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
207 """Return the directory containing the Python library (standard or
Fred Drakec1ee39a2000-03-09 15:54:52 +0000208 site additions).
Greg Ward7d73b9e2000-03-09 03:16:05 +0000209
Fred Drakec1ee39a2000-03-09 15:54:52 +0000210 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 Sajip7ded1f02012-05-26 03:45:29 +0100217 If 'prefix' is supplied, use it instead of sys.base_prefix or
218 sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
Fred Drakec1ee39a2000-03-09 15:54:52 +0000219 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000220 if prefix is None:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100221 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é36797272010-07-22 12:50:05 +0000225
226 if os.name == "posix":
Victor Stinner8510f432020-03-10 09:53:09 +0100227 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é36797272010-07-22 12:50:05 +0000236 if standard_lib:
237 return libpython
Greg Ward7d73b9e2000-03-09 03:16:05 +0000238 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000239 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é Lemburg2544f512002-01-31 18:56:00 +0000243 else:
Benjamin Petersondf0eb952014-09-06 17:24:12 -0400244 return os.path.join(prefix, "Lib", "site-packages")
Greg Ward7d73b9e2000-03-09 03:16:05 +0000245 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000246 raise DistutilsPlatformError(
247 "I don't know where Python installs its library "
248 "on platform '%s'" % os.name)