blob: 43e807f20d98957fdc31bd5a457280948dbf8f0b [file] [log] [blame]
Andrew M. Kuchling66012fe2001-01-26 21:56:58 +00001# Autodetecting setup.py script for building the Python extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +00002
Victor Stinner625dbf22019-03-01 15:59:39 +01003import argparse
Eric Snow335e14d2014-01-04 15:09:28 -07004import importlib._bootstrap
Victor Stinner625dbf22019-03-01 15:59:39 +01005import importlib.machinery
Eric Snow335e14d2014-01-04 15:09:28 -07006import importlib.util
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -07007import logging
Victor Stinner625dbf22019-03-01 15:59:39 +01008import os
9import re
10import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +000011import sysconfig
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020012import warnings
Serhiy Storchaka93558682020-06-20 11:10:31 +030013from glob import glob, escape
Ronald Oussoren404a7192020-11-22 06:14:25 +010014import _osx_support
Michael W. Hudson529a5052002-12-17 16:47:17 +000015
Victor Stinner1ec63b62020-03-04 14:50:19 +010016
17try:
18 import subprocess
19 del subprocess
20 SUBPROCESS_BOOTSTRAP = False
21except ImportError:
Victor Stinner1ec63b62020-03-04 14:50:19 +010022 # Bootstrap Python: distutils.spawn uses subprocess to build C extensions,
23 # subprocess requires C extensions built by setup.py like _posixsubprocess.
24 #
Victor Stinneraddaaaa2020-03-09 23:45:59 +010025 # Use _bootsubprocess which only uses the os module.
Victor Stinner1ec63b62020-03-04 14:50:19 +010026 #
27 # It is dropped from sys.modules as soon as all C extension modules
28 # are built.
Victor Stinneraddaaaa2020-03-09 23:45:59 +010029 import _bootsubprocess
30 sys.modules['subprocess'] = _bootsubprocess
31 del _bootsubprocess
32 SUBPROCESS_BOOTSTRAP = True
Victor Stinner1ec63b62020-03-04 14:50:19 +010033
34
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020035with warnings.catch_warnings():
36 # bpo-41282 (PEP 632) deprecated distutils but setup.py still uses it
Christian Heimesa460ab32021-04-24 09:55:15 +020037 warnings.filterwarnings(
38 "ignore",
39 "The distutils package is deprecated",
40 DeprecationWarning
41 )
42 warnings.filterwarnings(
43 "ignore",
44 "The distutils.sysconfig module is deprecated, use sysconfig instead",
45 DeprecationWarning
46 )
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020047
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020048 from distutils.command.build_ext import build_ext
49 from distutils.command.build_scripts import build_scripts
50 from distutils.command.install import install
51 from distutils.command.install_lib import install_lib
52 from distutils.core import Extension, setup
53 from distutils.errors import CCompilerError, DistutilsError
54 from distutils.spawn import find_executable
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000055
Antoine Pitrou2c0a9162014-09-26 23:31:59 +020056
Victor Stinnercfe172d2019-03-01 18:21:49 +010057# Compile extensions used to test Python?
pxinwr277ce302020-12-30 20:50:39 +080058TEST_EXTENSIONS = (sysconfig.get_config_var('TEST_MODULES') == 'yes')
Victor Stinnercfe172d2019-03-01 18:21:49 +010059
60# This global variable is used to hold the list of modules to be disabled.
61DISABLED_MODULE_LIST = []
62
Victor Stinnercad80202021-01-19 23:04:49 +010063# --list-module-names option used by Tools/scripts/generate_module_names.py
64LIST_MODULE_NAMES = False
65
Victor Stinnercfe172d2019-03-01 18:21:49 +010066
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -070067logging.basicConfig(format='%(message)s', level=logging.INFO)
68log = logging.getLogger('setup')
69
70
doko@ubuntu.com93df16b2012-06-30 14:32:08 +020071def get_platform():
Victor Stinnerc991f242019-03-01 17:19:04 +010072 # Cross compiling
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +020073 if "_PYTHON_HOST_PLATFORM" in os.environ:
74 return os.environ["_PYTHON_HOST_PLATFORM"]
Victor Stinnerc991f242019-03-01 17:19:04 +010075
doko@ubuntu.com93df16b2012-06-30 14:32:08 +020076 # Get value of sys.platform
77 if sys.platform.startswith('osf1'):
78 return 'osf1'
79 return sys.platform
Victor Stinnerc991f242019-03-01 17:19:04 +010080
81
82CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
Victor Stinner4cbea512019-02-28 17:48:38 +010083HOST_PLATFORM = get_platform()
84MS_WINDOWS = (HOST_PLATFORM == 'win32')
85CYGWIN = (HOST_PLATFORM == 'cygwin')
86MACOS = (HOST_PLATFORM == 'darwin')
Michael Felt08970cb2019-06-21 15:58:00 +020087AIX = (HOST_PLATFORM.startswith('aix'))
Victor Stinner4cbea512019-02-28 17:48:38 +010088VXWORKS = ('vxworks' in HOST_PLATFORM)
Christian Heimes545aebd2021-11-27 22:14:05 +020089CC = os.environ.get("CC")
90if not CC:
91 CC = sysconfig.get_config_var("CC")
pxinwr32f5fdd2019-02-27 19:09:28 +080092
Victor Stinnerc991f242019-03-01 17:19:04 +010093
94SUMMARY = """
95Python is an interpreted, interactive, object-oriented programming
96language. It is often compared to Tcl, Perl, Scheme or Java.
97
98Python combines remarkable power with very clear syntax. It has
99modules, classes, exceptions, very high level dynamic data types, and
100dynamic typing. There are interfaces to many system calls and
101libraries, as well as to various windowing systems (X11, Motif, Tk,
102Mac, MFC). New built-in modules are easily written in C or C++. Python
103is also usable as an extension language for applications that need a
104programmable interface.
105
106The Python implementation is portable: it runs on many brands of UNIX,
107on Windows, DOS, Mac, Amiga... If your favorite system isn't
108listed here, it may still be supported, if there's a C compiler for
109it. Ask around on comp.lang.python -- or just try compiling Python
110yourself.
111"""
112
113CLASSIFIERS = """
114Development Status :: 6 - Mature
115License :: OSI Approved :: Python Software Foundation License
116Natural Language :: English
117Programming Language :: C
118Programming Language :: Python
119Topic :: Software Development
120"""
121
122
Victor Stinner6b982c22020-04-01 01:10:07 +0200123def run_command(cmd):
124 status = os.system(cmd)
Victor Stinner65a796e2020-04-01 18:49:29 +0200125 return os.waitstatus_to_exitcode(status)
Victor Stinner6b982c22020-04-01 01:10:07 +0200126
127
Victor Stinnerc991f242019-03-01 17:19:04 +0100128# Set common compiler and linker flags derived from the Makefile,
129# reserved for building the interpreter and the stdlib modules.
130# See bpo-21121 and bpo-35257
131def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
132 flags = sysconfig.get_config_var(compiler_flags)
133 py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
134 sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
135
136
Michael W. Hudson39230b32002-01-16 15:26:48 +0000137def add_dir_to_list(dirlist, dir):
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000138 """Add the directory 'dir' to the list 'dirlist' (after any relative
139 directories) if:
140
Michael W. Hudson39230b32002-01-16 15:26:48 +0000141 1) 'dir' is not already in 'dirlist'
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000142 2) 'dir' actually exists, and is a directory.
143 """
144 if dir is None or not os.path.isdir(dir) or dir in dirlist:
145 return
146 for i, path in enumerate(dirlist):
147 if not os.path.isabs(path):
148 dirlist.insert(i + 1, dir)
Barry Warsaw34520cd2010-11-27 20:03:03 +0000149 return
150 dirlist.insert(0, dir)
Michael W. Hudson39230b32002-01-16 15:26:48 +0000151
Victor Stinnerc991f242019-03-01 17:19:04 +0100152
xdegaye77f51392017-11-25 17:25:30 +0100153def sysroot_paths(make_vars, subdirs):
154 """Get the paths of sysroot sub-directories.
155
156 * make_vars: a sequence of names of variables of the Makefile where
157 sysroot may be set.
158 * subdirs: a sequence of names of subdirectories used as the location for
159 headers or libraries.
160 """
161
162 dirs = []
163 for var_name in make_vars:
164 var = sysconfig.get_config_var(var_name)
165 if var is not None:
166 m = re.search(r'--sysroot=([^"]\S*|"[^"]+")', var)
167 if m is not None:
168 sysroot = m.group(1).strip('"')
169 for subdir in subdirs:
170 if os.path.isabs(subdir):
171 subdir = subdir[1:]
172 path = os.path.join(sysroot, subdir)
173 if os.path.isdir(path):
174 dirs.append(path)
175 break
176 return dirs
177
Ned Deily1731d6d2020-05-18 04:32:38 -0400178
Ned Deily0288dd62019-06-03 06:34:48 -0400179MACOS_SDK_ROOT = None
Ned Deily1731d6d2020-05-18 04:32:38 -0400180MACOS_SDK_SPECIFIED = None
Victor Stinnerc991f242019-03-01 17:19:04 +0100181
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000182def macosx_sdk_root():
Ned Deily0288dd62019-06-03 06:34:48 -0400183 """Return the directory of the current macOS SDK.
184
185 If no SDK was explicitly configured, call the compiler to find which
186 include files paths are being searched by default. Use '/' if the
187 compiler is searching /usr/include (meaning system header files are
188 installed) or use the root of an SDK if that is being searched.
189 (The SDK may be supplied via Xcode or via the Command Line Tools).
190 The SDK paths used by Apple-supplied tool chains depend on the
191 setting of various variables; see the xcrun man page for more info.
Ned Deily1731d6d2020-05-18 04:32:38 -0400192 Also sets MACOS_SDK_SPECIFIED for use by macosx_sdk_specified().
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000193 """
Ned Deily1731d6d2020-05-18 04:32:38 -0400194 global MACOS_SDK_ROOT, MACOS_SDK_SPECIFIED
Ned Deily0288dd62019-06-03 06:34:48 -0400195
196 # If already called, return cached result.
197 if MACOS_SDK_ROOT:
198 return MACOS_SDK_ROOT
199
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000200 cflags = sysconfig.get_config_var('CFLAGS')
Joshua Rootb3107002020-04-22 17:44:10 +1000201 m = re.search(r'-isysroot\s*(\S+)', cflags)
Ned Deily0288dd62019-06-03 06:34:48 -0400202 if m is not None:
203 MACOS_SDK_ROOT = m.group(1)
Ned Deily29afab62020-12-04 23:02:09 -0500204 MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000205 else:
Ronald Oussoren404a7192020-11-22 06:14:25 +0100206 MACOS_SDK_ROOT = _osx_support._default_sysroot(
207 sysconfig.get_config_var('CC'))
Ned Deily29afab62020-12-04 23:02:09 -0500208 MACOS_SDK_SPECIFIED = False
Ned Deily0288dd62019-06-03 06:34:48 -0400209
210 return MACOS_SDK_ROOT
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000211
Victor Stinnerc991f242019-03-01 17:19:04 +0100212
Ned Deily1731d6d2020-05-18 04:32:38 -0400213def macosx_sdk_specified():
214 """Returns true if an SDK was explicitly configured.
215
216 True if an SDK was selected at configure time, either by specifying
217 --enable-universalsdk=(something other than no or /) or by adding a
218 -isysroot option to CFLAGS. In some cases, like when making
219 decisions about macOS Tk framework paths, we need to be able to
220 know whether the user explicitly asked to build with an SDK versus
221 the implicit use of an SDK when header files are no longer
222 installed on a running system by the Command Line Tools.
223 """
224 global MACOS_SDK_SPECIFIED
225
226 # If already called, return cached result.
227 if MACOS_SDK_SPECIFIED:
228 return MACOS_SDK_SPECIFIED
229
230 # Find the sdk root and set MACOS_SDK_SPECIFIED
231 macosx_sdk_root()
232 return MACOS_SDK_SPECIFIED
233
234
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000235def is_macosx_sdk_path(path):
236 """
Ned Batchelderd52bbde2021-05-02 19:58:57 -0700237 Returns True if 'path' can be located in a macOS SDK
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000238 """
Ned Deily2910a7b2012-07-30 02:35:58 -0700239 return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
Ned Batchelderd52bbde2021-05-02 19:58:57 -0700240 or path.startswith('/System/Library')
241 or path.startswith('/System/iOSSupport') )
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000242
Victor Stinnerc991f242019-03-01 17:19:04 +0100243
Ronald Oussoren41761932020-11-08 10:05:27 +0100244def grep_headers_for(function, headers):
245 for header in headers:
Ronald Oussoren7a27c7e2020-11-14 16:07:47 +0100246 with open(header, 'r', errors='surrogateescape') as f:
Ronald Oussoren41761932020-11-08 10:05:27 +0100247 if function in f.read():
248 return True
249 return False
250
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700251
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000252def find_file(filename, std_dirs, paths):
253 """Searches for the directory where a given file is located,
254 and returns a possibly-empty list of additional directories, or None
255 if the file couldn't be found at all.
Fredrik Lundhade711a2001-01-24 08:00:28 +0000256
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000257 'filename' is the name of a file, such as readline.h or libcrypto.a.
258 'std_dirs' is the list of standard system directories; if the
259 file is found in one of them, no additional directives are needed.
260 'paths' is a list of additional locations to check; if the file is
261 found in one of them, the resulting list will contain the directory.
262 """
Victor Stinner4cbea512019-02-28 17:48:38 +0100263 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000264 # Honor the MacOSX SDK setting when one was specified.
265 # An SDK is a directory with the same structure as a real
266 # system, but with only header files and libraries.
267 sysroot = macosx_sdk_root()
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000268
269 # Check the standard locations
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700270 for dir_ in std_dirs:
271 f = os.path.join(dir_, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000272
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700273 if MACOS and is_macosx_sdk_path(dir_):
274 f = os.path.join(sysroot, dir_[1:], filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000275
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000276 if os.path.exists(f): return []
277
278 # Check the additional directories
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700279 for dir_ in paths:
280 f = os.path.join(dir_, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000281
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700282 if MACOS and is_macosx_sdk_path(dir_):
283 f = os.path.join(sysroot, dir_[1:], filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000284
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000285 if os.path.exists(f):
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700286 return [dir_]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000287
288 # Not found anywhere
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000289 return None
290
Victor Stinnerc991f242019-03-01 17:19:04 +0100291
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000292def find_library_file(compiler, libname, std_dirs, paths):
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000293 result = compiler.find_library_file(std_dirs + paths, libname)
294 if result is None:
295 return None
Fredrik Lundhade711a2001-01-24 08:00:28 +0000296
Victor Stinner4cbea512019-02-28 17:48:38 +0100297 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000298 sysroot = macosx_sdk_root()
299
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000300 # Check whether the found file is in one of the standard directories
301 dirname = os.path.dirname(result)
302 for p in std_dirs:
303 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000304 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000305
Victor Stinner4cbea512019-02-28 17:48:38 +0100306 if MACOS and is_macosx_sdk_path(p):
Ned Deily020250f2016-02-25 00:56:38 +1100307 # Note that, as of Xcode 7, Apple SDKs may contain textual stub
308 # libraries with .tbd extensions rather than the normal .dylib
309 # shared libraries installed in /. The Apple compiler tool
310 # chain handles this transparently but it can cause problems
311 # for programs that are being built with an SDK and searching
312 # for specific libraries. Distutils find_library_file() now
313 # knows to also search for and return .tbd files. But callers
314 # of find_library_file need to keep in mind that the base filename
315 # of the returned SDK library file might have a different extension
316 # from that of the library file installed on the running system,
317 # for example:
318 # /Applications/Xcode.app/Contents/Developer/Platforms/
319 # MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/
320 # usr/lib/libedit.tbd
321 # vs
322 # /usr/lib/libedit.dylib
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000323 if os.path.join(sysroot, p[1:]) == dirname:
324 return [ ]
325
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000326 if p == dirname:
327 return [ ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000328
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000329 # Otherwise, it must have been in one of the additional directories,
330 # so we have to figure out which one.
331 for p in paths:
332 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000333 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000334
Victor Stinner4cbea512019-02-28 17:48:38 +0100335 if MACOS and is_macosx_sdk_path(p):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000336 if os.path.join(sysroot, p[1:]) == dirname:
337 return [ p ]
338
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000339 if p == dirname:
340 return [p]
341 else:
342 assert False, "Internal error: Path not found in std_dirs or paths"
Tim Peters2c60f7a2003-01-29 03:49:43 +0000343
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700344
Paul Ganssle62972d92020-05-16 04:20:06 -0400345def validate_tzpath():
346 base_tzpath = sysconfig.get_config_var('TZPATH')
347 if not base_tzpath:
348 return
349
350 tzpaths = base_tzpath.split(os.pathsep)
351 bad_paths = [tzpath for tzpath in tzpaths if not os.path.isabs(tzpath)]
352 if bad_paths:
353 raise ValueError('TZPATH must contain only absolute paths, '
354 + f'found:\n{tzpaths!r}\nwith invalid paths:\n'
355 + f'{bad_paths!r}')
Victor Stinnerc991f242019-03-01 17:19:04 +0100356
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700357
Jack Jansen144ebcc2001-08-05 22:31:19 +0000358def find_module_file(module, dirlist):
359 """Find a module in a set of possible folders. If it is not found
360 return the unadorned filename"""
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700361 dirs = find_file(module, [], dirlist)
362 if not dirs:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000363 return module
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700364 if len(dirs) > 1:
365 log.info(f"WARNING: multiple copies of {module} found")
366 return os.path.join(dirs[0], module)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000367
Victor Stinnerc991f242019-03-01 17:19:04 +0100368
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000369class PyBuildExt(build_ext):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000370
Guido van Rossumd8faa362007-04-27 19:54:29 +0000371 def __init__(self, dist):
372 build_ext.__init__(self, dist)
Victor Stinner625dbf22019-03-01 15:59:39 +0100373 self.srcdir = None
374 self.lib_dirs = None
375 self.inc_dirs = None
Victor Stinner5ec33a12019-03-01 16:43:28 +0100376 self.config_h_vars = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000377 self.failed = []
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400378 self.failed_on_import = []
Victor Stinner8058bda2019-03-01 15:31:45 +0100379 self.missing = []
Christian Heimes9b60e552020-05-15 23:54:53 +0200380 self.disabled_configure = []
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200381 if '-j' in os.environ.get('MAKEFLAGS', ''):
382 self.parallel = True
Guido van Rossumd8faa362007-04-27 19:54:29 +0000383
Victor Stinner8058bda2019-03-01 15:31:45 +0100384 def add(self, ext):
385 self.extensions.append(ext)
386
Victor Stinner00c77ae2020-03-04 18:44:49 +0100387 def set_srcdir(self):
Victor Stinner625dbf22019-03-01 15:59:39 +0100388 self.srcdir = sysconfig.get_config_var('srcdir')
389 if not self.srcdir:
390 # Maybe running on Windows but not using CYGWIN?
391 raise ValueError("No source directory; cannot proceed.")
392 self.srcdir = os.path.abspath(self.srcdir)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000393
Victor Stinner00c77ae2020-03-04 18:44:49 +0100394 def remove_disabled(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000395 # Remove modules that are present on the disabled list
Christian Heimes679db4a2008-01-18 09:56:22 +0000396 extensions = [ext for ext in self.extensions
Victor Stinner4cbea512019-02-28 17:48:38 +0100397 if ext.name not in DISABLED_MODULE_LIST]
Christian Heimes679db4a2008-01-18 09:56:22 +0000398 # move ctypes to the end, it depends on other modules
399 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
400 if "_ctypes" in ext_map:
401 ctypes = extensions.pop(ext_map["_ctypes"])
402 extensions.append(ctypes)
403 self.extensions = extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +0000404
Victor Stinner00c77ae2020-03-04 18:44:49 +0100405 def update_sources_depends(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000406 # Fix up the autodetected modules, prefixing all the source files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000407 # with Modules/.
Victor Stinner625dbf22019-03-01 15:59:39 +0100408 moddirlist = [os.path.join(self.srcdir, 'Modules')]
Michael W. Hudson5b109102002-01-23 15:04:41 +0000409
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000410 # Fix up the paths for scripts, too
Victor Stinner625dbf22019-03-01 15:59:39 +0100411 self.distribution.scripts = [os.path.join(self.srcdir, filename)
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000412 for filename in self.distribution.scripts]
413
Christian Heimesaf98da12008-01-27 15:18:18 +0000414 # Python header files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000415 headers = [sysconfig.get_config_h_filename()]
Serhiy Storchaka93558682020-06-20 11:10:31 +0300416 headers += glob(os.path.join(escape(sysconfig.get_path('include')), "*.h"))
Christian Heimesaf98da12008-01-27 15:18:18 +0000417
Xavier de Gaye84968b72016-10-29 16:57:20 +0200418 for ext in self.extensions:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000419 ext.sources = [ find_module_file(filename, moddirlist)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000420 for filename in ext.sources ]
Jeremy Hylton340043e2002-06-13 17:38:11 +0000421 if ext.depends is not None:
Neil Schemenauer014bf282009-02-05 16:35:45 +0000422 ext.depends = [find_module_file(filename, moddirlist)
Jeremy Hylton340043e2002-06-13 17:38:11 +0000423 for filename in ext.depends]
Christian Heimesaf98da12008-01-27 15:18:18 +0000424 else:
425 ext.depends = []
426 # re-compile extensions if a header file has been changed
427 ext.depends.extend(headers)
428
Victor Stinner00c77ae2020-03-04 18:44:49 +0100429 def remove_configured_extensions(self):
430 # The sysconfig variables built by makesetup that list the already
431 # built modules and the disabled modules as configured by the Setup
432 # files.
433 sysconf_built = sysconfig.get_config_var('MODBUILT_NAMES').split()
434 sysconf_dis = sysconfig.get_config_var('MODDISABLED_NAMES').split()
435
436 mods_built = []
437 mods_disabled = []
438 for ext in self.extensions:
xdegayec0364fc2017-05-27 18:25:03 +0200439 # If a module has already been built or has been disabled in the
440 # Setup files, don't build it here.
441 if ext.name in sysconf_built:
442 mods_built.append(ext)
443 if ext.name in sysconf_dis:
444 mods_disabled.append(ext)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000445
xdegayec0364fc2017-05-27 18:25:03 +0200446 mods_configured = mods_built + mods_disabled
447 if mods_configured:
Xavier de Gaye84968b72016-10-29 16:57:20 +0200448 self.extensions = [x for x in self.extensions if x not in
xdegayec0364fc2017-05-27 18:25:03 +0200449 mods_configured]
450 # Remove the shared libraries built by a previous build.
451 for ext in mods_configured:
452 fullpath = self.get_ext_fullpath(ext.name)
453 if os.path.exists(fullpath):
454 os.unlink(fullpath)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000455
Victor Stinner00c77ae2020-03-04 18:44:49 +0100456 return (mods_built, mods_disabled)
457
458 def set_compiler_executables(self):
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000459 # When you run "make CC=altcc" or something similar, you really want
460 # those environment variables passed into the setup.py phase. Here's
461 # a small set of useful ones.
462 compiler = os.environ.get('CC')
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000463 args = {}
464 # unfortunately, distutils doesn't let us provide separate C and C++
465 # compilers
466 if compiler is not None:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000467 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
468 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
Tarek Ziadé36797272010-07-22 12:50:05 +0000469 self.compiler.set_executables(**args)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000470
Victor Stinner00c77ae2020-03-04 18:44:49 +0100471 def build_extensions(self):
472 self.set_srcdir()
Christian Heimes545aebd2021-11-27 22:14:05 +0200473 self.set_compiler_executables()
474 self.configure_compiler()
475 self.init_inc_lib_dirs()
Victor Stinner00c77ae2020-03-04 18:44:49 +0100476
477 # Detect which modules should be compiled
478 self.detect_modules()
479
Victor Stinnercad80202021-01-19 23:04:49 +0100480 if not LIST_MODULE_NAMES:
481 self.remove_disabled()
Victor Stinner00c77ae2020-03-04 18:44:49 +0100482
483 self.update_sources_depends()
484 mods_built, mods_disabled = self.remove_configured_extensions()
Victor Stinner00c77ae2020-03-04 18:44:49 +0100485
Victor Stinnercad80202021-01-19 23:04:49 +0100486 if LIST_MODULE_NAMES:
487 for ext in self.extensions:
488 print(ext.name)
489 for name in self.missing:
490 print(name)
491 return
492
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000493 build_ext.build_extensions(self)
494
Victor Stinner1ec63b62020-03-04 14:50:19 +0100495 if SUBPROCESS_BOOTSTRAP:
496 # Drop our custom subprocess module:
497 # use the newly built subprocess module
498 del sys.modules['subprocess']
499
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200500 for ext in self.extensions:
501 self.check_extension_import(ext)
502
Victor Stinner00c77ae2020-03-04 18:44:49 +0100503 self.summary(mods_built, mods_disabled)
504
505 def summary(self, mods_built, mods_disabled):
Berker Peksag1d82a9c2014-10-01 05:11:13 +0300506 longest = max([len(e.name) for e in self.extensions], default=0)
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400507 if self.failed or self.failed_on_import:
508 all_failed = self.failed + self.failed_on_import
509 longest = max(longest, max([len(name) for name in all_failed]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000510
511 def print_three_column(lst):
512 lst.sort(key=str.lower)
513 # guarantee zip() doesn't drop anything
514 while len(lst) % 3:
515 lst.append("")
516 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
517 print("%-*s %-*s %-*s" % (longest, e, longest, f,
518 longest, g))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000519
Victor Stinner8058bda2019-03-01 15:31:45 +0100520 if self.missing:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000521 print()
Brett Cannonae95b4f2013-07-12 11:30:32 -0400522 print("Python build finished successfully!")
523 print("The necessary bits to build these optional modules were not "
524 "found:")
Victor Stinner8058bda2019-03-01 15:31:45 +0100525 print_three_column(self.missing)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000526 print("To find the necessary bits, look in setup.py in"
527 " detect_modules() for the module's name.")
528 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000529
xdegayec0364fc2017-05-27 18:25:03 +0200530 if mods_built:
531 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200532 print("The following modules found by detect_modules() in"
533 " setup.py, have been")
534 print("built by the Makefile instead, as configured by the"
535 " Setup files:")
xdegayec0364fc2017-05-27 18:25:03 +0200536 print_three_column([ext.name for ext in mods_built])
537 print()
538
539 if mods_disabled:
540 print()
541 print("The following modules found by detect_modules() in"
542 " setup.py have not")
543 print("been built, they are *disabled* in the Setup files:")
544 print_three_column([ext.name for ext in mods_disabled])
545 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200546
Christian Heimes9b60e552020-05-15 23:54:53 +0200547 if self.disabled_configure:
548 print()
549 print("The following modules found by detect_modules() in"
550 " setup.py have not")
551 print("been built, they are *disabled* by configure:")
552 print_three_column(self.disabled_configure)
553 print()
554
Guido van Rossumd8faa362007-04-27 19:54:29 +0000555 if self.failed:
556 failed = self.failed[:]
557 print()
558 print("Failed to build these modules:")
559 print_three_column(failed)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000560 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000561
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400562 if self.failed_on_import:
563 failed = self.failed_on_import[:]
564 print()
565 print("Following modules built successfully"
566 " but were removed because they could not be imported:")
567 print_three_column(failed)
568 print()
569
Christian Heimes61d478c2018-01-27 15:51:38 +0100570 if any('_ssl' in l
Victor Stinner8058bda2019-03-01 15:31:45 +0100571 for l in (self.missing, self.failed, self.failed_on_import)):
Christian Heimes61d478c2018-01-27 15:51:38 +0100572 print()
573 print("Could not build the ssl module!")
Christian Heimes39258d32021-04-17 11:36:35 +0200574 print("Python requires a OpenSSL 1.1.1 or newer")
Christian Heimes32eba612021-03-19 10:29:25 +0100575 if sysconfig.get_config_var("OPENSSL_LDFLAGS"):
576 print("Custom linker flags may require --with-openssl-rpath=auto")
Christian Heimes61d478c2018-01-27 15:51:38 +0100577 print()
578
Pablo Galindo Salgadoc2e0b132021-07-30 16:14:28 +0100579 if os.environ.get("PYTHONSTRICTEXTENSIONBUILD") and (self.failed or self.failed_on_import):
580 raise RuntimeError("Failed to build some stdlib modules")
581
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000582 def build_extension(self, ext):
583
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 if ext.name == '_ctypes':
585 if not self.configure_ctypes(ext):
Zachary Waref40d4dd2016-09-17 01:25:24 -0500586 self.failed.append(ext.name)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587 return
588
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000589 try:
590 build_ext.build_extension(self, ext)
Guido van Rossumb940e112007-01-10 16:19:56 +0000591 except (CCompilerError, DistutilsError) as why:
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000592 self.announce('WARNING: building of extension "%s" failed: %s' %
Victor Stinner625dbf22019-03-01 15:59:39 +0100593 (ext.name, why))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000594 self.failed.append(ext.name)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000595 return
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200596
597 def check_extension_import(self, ext):
598 # Don't try to import an extension that has failed to compile
599 if ext.name in self.failed:
600 self.announce(
601 'WARNING: skipping import check for failed build "%s"' %
602 ext.name, level=1)
603 return
604
Jack Jansenf49c6f92001-11-01 14:44:15 +0000605 # Workaround for Mac OS X: The Carbon-based modules cannot be
606 # reliably imported into a command-line Python
607 if 'Carbon' in ext.extra_link_args:
Michael W. Hudson5b109102002-01-23 15:04:41 +0000608 self.announce(
609 'WARNING: skipping import check for Carbon-based "%s"' %
610 ext.name)
611 return
Georg Brandlfcaf9102008-07-16 02:17:56 +0000612
Victor Stinner4cbea512019-02-28 17:48:38 +0100613 if MACOS and (
Benjamin Petersonfc576352008-07-16 02:39:02 +0000614 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
Georg Brandlfcaf9102008-07-16 02:17:56 +0000615 # Don't bother doing an import check when an extension was
616 # build with an explicit '-arch' flag on OSX. That's currently
617 # only used to build 32-bit only extensions in a 4-way
618 # universal build and loading 32-bit code into a 64-bit
619 # process will fail.
620 self.announce(
621 'WARNING: skipping import check for "%s"' %
622 ext.name)
623 return
624
Jason Tishler24cf7762002-05-22 16:46:15 +0000625 # Workaround for Cygwin: Cygwin currently has fork issues when many
626 # modules have been imported
Victor Stinner4cbea512019-02-28 17:48:38 +0100627 if CYGWIN:
Jason Tishler24cf7762002-05-22 16:46:15 +0000628 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
629 % ext.name)
630 return
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000631 ext_filename = os.path.join(
632 self.build_lib,
633 self.get_ext_filename(self.get_ext_fullname(ext.name)))
Guido van Rossumc3fee692008-07-17 16:23:53 +0000634
635 # If the build directory didn't exist when setup.py was
636 # started, sys.path_importer_cache has a negative result
637 # cached. Clear that cache before trying to import.
638 sys.path_importer_cache.clear()
639
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200640 # Don't try to load extensions for cross builds
Victor Stinner4cbea512019-02-28 17:48:38 +0100641 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200642 return
643
Brett Cannonca5ff3a2013-06-15 17:52:59 -0400644 loader = importlib.machinery.ExtensionFileLoader(ext.name, ext_filename)
Eric Snow335e14d2014-01-04 15:09:28 -0700645 spec = importlib.util.spec_from_file_location(ext.name, ext_filename,
646 loader=loader)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000647 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400648 importlib._bootstrap._load(spec)
Guido van Rossumb940e112007-01-10 16:19:56 +0000649 except ImportError as why:
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400650 self.failed_on_import.append(ext.name)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000651 self.announce('*** WARNING: renaming "%s" since importing it'
652 ' failed: %s' % (ext.name, why), level=3)
653 assert not self.inplace
654 basename, tail = os.path.splitext(ext_filename)
655 newname = basename + "_failed" + tail
656 if os.path.exists(newname):
657 os.remove(newname)
658 os.rename(ext_filename, newname)
659
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000660 except:
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000661 exc_type, why, tb = sys.exc_info()
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000662 self.announce('*** WARNING: importing extension "%s" '
663 'failed with %s: %s' % (ext.name, exc_type, why),
664 level=3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000665 self.failed.append(ext.name)
Fred Drake9028d0a2001-12-06 22:59:54 +0000666
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400667 def add_multiarch_paths(self):
668 # Debian/Ubuntu multiarch support.
669 # https://wiki.ubuntu.com/MultiarchSpec
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200670 tmpfile = os.path.join(self.build_temp, 'multiarch')
671 if not os.path.exists(self.build_temp):
672 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200673 ret = run_command(
Christian Heimes545aebd2021-11-27 22:14:05 +0200674 '%s -print-multiarch > %s 2> /dev/null' % (CC, tmpfile))
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200675 multiarch_path_component = ''
676 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200677 if ret == 0:
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200678 with open(tmpfile) as fp:
679 multiarch_path_component = fp.readline().strip()
680 finally:
681 os.unlink(tmpfile)
682
683 if multiarch_path_component != '':
684 add_dir_to_list(self.compiler.library_dirs,
685 '/usr/lib/' + multiarch_path_component)
686 add_dir_to_list(self.compiler.include_dirs,
687 '/usr/include/' + multiarch_path_component)
688 return
689
Barry Warsaw88e19452011-04-07 10:40:36 -0400690 if not find_executable('dpkg-architecture'):
691 return
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200692 opt = ''
Victor Stinner4cbea512019-02-28 17:48:38 +0100693 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200694 opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400695 tmpfile = os.path.join(self.build_temp, 'multiarch')
696 if not os.path.exists(self.build_temp):
697 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200698 ret = run_command(
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200699 'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
700 (opt, tmpfile))
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400701 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200702 if ret == 0:
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400703 with open(tmpfile) as fp:
704 multiarch_path_component = fp.readline().strip()
705 add_dir_to_list(self.compiler.library_dirs,
706 '/usr/lib/' + multiarch_path_component)
707 add_dir_to_list(self.compiler.include_dirs,
708 '/usr/include/' + multiarch_path_component)
709 finally:
710 os.unlink(tmpfile)
711
pxinwr5e45f1c2021-01-22 08:55:52 +0800712 def add_wrcc_search_dirs(self):
713 # add library search path by wr-cc, the compiler wrapper
714
715 def convert_mixed_path(path):
716 # convert path like C:\folder1\folder2/folder3/folder4
717 # to msys style /c/folder1/folder2/folder3/folder4
718 drive = path[0].lower()
719 left = path[2:].replace("\\", "/")
720 return "/" + drive + left
721
722 def add_search_path(line):
723 # On Windows building machine, VxWorks does
724 # cross builds under msys2 environment.
725 pathsep = (";" if sys.platform == "msys" else ":")
726 for d in line.strip().split("=")[1].split(pathsep):
727 d = d.strip()
728 if sys.platform == "msys":
729 # On Windows building machine, compiler
730 # returns mixed style path like:
731 # C:\folder1\folder2/folder3/folder4
732 d = convert_mixed_path(d)
733 d = os.path.normpath(d)
734 add_dir_to_list(self.compiler.library_dirs, d)
735
pxinwr5e45f1c2021-01-22 08:55:52 +0800736 tmpfile = os.path.join(self.build_temp, 'wrccpaths')
737 os.makedirs(self.build_temp, exist_ok=True)
738 try:
Christian Heimes545aebd2021-11-27 22:14:05 +0200739 ret = run_command('%s --print-search-dirs >%s' % (CC, tmpfile))
pxinwr5e45f1c2021-01-22 08:55:52 +0800740 if ret:
741 return
742 with open(tmpfile) as fp:
743 # Parse paths in libraries line. The line is like:
744 # On Linux, "libraries: = path1:path2:path3"
745 # On Windows, "libraries: = path1;path2;path3"
746 for line in fp:
747 if not line.startswith("libraries"):
748 continue
749 add_search_path(line)
750 finally:
751 try:
752 os.unlink(tmpfile)
753 except OSError:
754 pass
755
pxinwr32f5fdd2019-02-27 19:09:28 +0800756 def add_cross_compiling_paths(self):
pxinwr32f5fdd2019-02-27 19:09:28 +0800757 tmpfile = os.path.join(self.build_temp, 'ccpaths')
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200758 if not os.path.exists(self.build_temp):
759 os.makedirs(self.build_temp)
Christian Heimes545aebd2021-11-27 22:14:05 +0200760 ret = run_command('%s -E -v - </dev/null 2>%s 1>/dev/null' % (CC, tmpfile))
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200761 is_gcc = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800762 is_clang = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200763 in_incdirs = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200764 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200765 if ret == 0:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200766 with open(tmpfile) as fp:
767 for line in fp.readlines():
768 if line.startswith("gcc version"):
769 is_gcc = True
pxinwr32f5fdd2019-02-27 19:09:28 +0800770 elif line.startswith("clang version"):
771 is_clang = True
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200772 elif line.startswith("#include <...>"):
773 in_incdirs = True
774 elif line.startswith("End of search list"):
775 in_incdirs = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800776 elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"):
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200777 for d in line.strip().split("=")[1].split(":"):
778 d = os.path.normpath(d)
779 if '/gcc/' not in d:
780 add_dir_to_list(self.compiler.library_dirs,
781 d)
pxinwr32f5fdd2019-02-27 19:09:28 +0800782 elif (is_gcc or is_clang) and in_incdirs and '/gcc/' not in line and '/clang/' not in line:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200783 add_dir_to_list(self.compiler.include_dirs,
784 line.strip())
785 finally:
786 os.unlink(tmpfile)
787
pxinwr5e45f1c2021-01-22 08:55:52 +0800788 if VXWORKS:
789 self.add_wrcc_search_dirs()
790
Victor Stinnercfe172d2019-03-01 18:21:49 +0100791 def add_ldflags_cppflags(self):
Brett Cannon516592f2004-12-07 00:42:59 +0000792 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000793 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000794 # We must get the values from the Makefile and not the environment
795 # directly since an inconsistently reproducible issue comes up where
796 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000797 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000798 for env_var, arg_name, dir_list in (
Tarek Ziadé36797272010-07-22 12:50:05 +0000799 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
800 ('LDFLAGS', '-L', self.compiler.library_dirs),
801 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000802 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000803 if env_val:
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800804 parser = argparse.ArgumentParser()
805 parser.add_argument(arg_name, dest="dirs", action="append")
Miss Islington (bot)b1949e02021-10-18 11:49:28 -0700806
807 # To prevent argparse from raising an exception about any
808 # options in env_val that it mistakes for known option, we
809 # strip out all double dashes and any dashes followed by a
810 # character that is not for the option we are dealing with.
811 #
812 # Please note that order of the regex is important! We must
813 # strip out double-dashes first so that we don't end up with
814 # substituting "--Long" to "-Long" and thus lead to "ong" being
815 # used for a library directory.
816 env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
817 ' ', env_val)
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800818 options, _ = parser.parse_known_args(env_val.split())
Brett Cannon44837712005-01-02 21:54:07 +0000819 if options.dirs:
Christian Heimes292d3512008-02-03 16:51:08 +0000820 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000821 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000822
Victor Stinnercfe172d2019-03-01 18:21:49 +0100823 def configure_compiler(self):
824 # Ensure that /usr/local is always used, but the local build
825 # directories (i.e. '.' and 'Include') must be first. See issue
826 # 10520.
827 if not CROSS_COMPILING:
828 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
829 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
830 # only change this for cross builds for 3.3, issues on Mageia
831 if CROSS_COMPILING:
832 self.add_cross_compiling_paths()
833 self.add_multiarch_paths()
834 self.add_ldflags_cppflags()
835
Victor Stinner5ec33a12019-03-01 16:43:28 +0100836 def init_inc_lib_dirs(self):
Victor Stinner4cbea512019-02-28 17:48:38 +0100837 if (not CROSS_COMPILING and
Xavier de Gaye1351c312016-12-14 11:14:33 +0100838 os.path.normpath(sys.base_prefix) != '/usr' and
839 not sysconfig.get_config_var('PYTHONFRAMEWORK')):
Ronald Oussorenf3500e12010-10-20 13:10:12 +0000840 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
841 # (PYTHONFRAMEWORK is set) to avoid # linking problems when
842 # building a framework with different architectures than
843 # the one that is currently installed (issue #7473)
Tarek Ziadé36797272010-07-22 12:50:05 +0000844 add_dir_to_list(self.compiler.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000845 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000846 add_dir_to_list(self.compiler.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000847 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000848
xdegaye77f51392017-11-25 17:25:30 +0100849 system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
850 system_include_dirs = ['/usr/include']
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000851 # lib_dirs and inc_dirs are used to search for files;
852 # if a file is found in one of those directories, it can
853 # be assumed that no additional -I,-L directives are needed.
Victor Stinner4cbea512019-02-28 17:48:38 +0100854 if not CROSS_COMPILING:
Victor Stinner625dbf22019-03-01 15:59:39 +0100855 self.lib_dirs = self.compiler.library_dirs + system_lib_dirs
856 self.inc_dirs = self.compiler.include_dirs + system_include_dirs
Christian Heimesf19529c2012-12-12 12:41:00 +0100857 else:
xdegaye77f51392017-11-25 17:25:30 +0100858 # Add the sysroot paths. 'sysroot' is a compiler option used to
859 # set the logical path of the standard system headers and
860 # libraries.
Victor Stinner625dbf22019-03-01 15:59:39 +0100861 self.lib_dirs = (self.compiler.library_dirs +
862 sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
863 self.inc_dirs = (self.compiler.include_dirs +
864 sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
865 system_include_dirs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000866
Brett Cannon4454a1f2005-04-15 20:32:39 +0000867 config_h = sysconfig.get_config_h_filename()
Brett Cannon9f5db072010-10-29 20:19:27 +0000868 with open(config_h) as file:
Victor Stinner5ec33a12019-03-01 16:43:28 +0100869 self.config_h_vars = sysconfig.parse_config_h(file)
Brett Cannon4454a1f2005-04-15 20:32:39 +0000870
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000871 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
Victor Stinner4cbea512019-02-28 17:48:38 +0100872 if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']:
Victor Stinner625dbf22019-03-01 15:59:39 +0100873 self.lib_dirs += ['/usr/ccs/lib']
Skip Montanaro22e00c42003-05-06 20:43:34 +0000874
Charles-François Natali5739e102012-04-12 19:07:25 +0200875 # HP-UX11iv3 keeps files in lib/hpux folders.
Victor Stinner4cbea512019-02-28 17:48:38 +0100876 if HOST_PLATFORM == 'hp-ux11':
Victor Stinner625dbf22019-03-01 15:59:39 +0100877 self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
Charles-François Natali5739e102012-04-12 19:07:25 +0200878
Victor Stinner4cbea512019-02-28 17:48:38 +0100879 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000880 # This should work on any unixy platform ;-)
881 # If the user has bothered specifying additional -I and -L flags
882 # in OPT and LDFLAGS we might as well use them here.
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000883 #
884 # NOTE: using shlex.split would technically be more correct, but
885 # also gives a bootstrap problem. Let's hope nobody uses
886 # directories with whitespace in the name to store libraries.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000887 cflags, ldflags = sysconfig.get_config_vars(
888 'CFLAGS', 'LDFLAGS')
889 for item in cflags.split():
890 if item.startswith('-I'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100891 self.inc_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892
893 for item in ldflags.split():
894 if item.startswith('-L'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100895 self.lib_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896
Victor Stinner5ec33a12019-03-01 16:43:28 +0100897 def detect_simple_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000898 #
899 # The following modules are all pretty straightforward, and compile
900 # on pretty much any POSIXish platform.
901 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000902
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000903 # array objects
Victor Stinnercdad2722021-04-22 00:52:52 +0200904 self.add(Extension('array', ['arraymodule.c'],
905 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Martin Panterc9deece2016-02-03 05:19:44 +0000906
Yury Selivanovf23746a2018-01-22 19:11:18 -0500907 # Context Variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100908 self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
Yury Selivanovf23746a2018-01-22 19:11:18 -0500909
Martin Panterc9deece2016-02-03 05:19:44 +0000910 shared_math = 'Modules/_math.o'
Victor Stinnercfe172d2019-03-01 18:21:49 +0100911
912 # math library functions, e.g. sin()
913 self.add(Extension('math', ['mathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100914 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100915 extra_objects=[shared_math],
916 depends=['_math.h', shared_math],
917 libraries=['m']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100918
919 # complex math library functions
920 self.add(Extension('cmath', ['cmathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100921 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100922 extra_objects=[shared_math],
923 depends=['_math.h', shared_math],
924 libraries=['m']))
Victor Stinnere0be4232011-10-25 13:06:09 +0200925
926 # time libraries: librt may be needed for clock_gettime()
927 time_libs = []
928 lib = sysconfig.get_config_var('TIMEMODULE_LIB')
929 if lib:
930 time_libs.append(lib)
931
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000932 # time operations and variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100933 self.add(Extension('time', ['timemodule.c'],
934 libraries=time_libs))
Benjamin Peterson8acaa312017-11-12 20:53:39 -0800935 # libm is needed by delta_new() that uses round() and by accum() that
936 # uses modf().
Victor Stinner8058bda2019-03-01 15:31:45 +0100937 self.add(Extension('_datetime', ['_datetimemodule.c'],
Victor Stinner04fc4f22020-06-16 01:28:07 +0200938 libraries=['m'],
939 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Paul Ganssle62972d92020-05-16 04:20:06 -0400940 # zoneinfo module
Victor Stinner37834132020-10-27 17:12:53 +0100941 self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
942 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000943 # random number generator implemented in C
Victor Stinner9f5fe792020-04-17 19:05:35 +0200944 self.add(Extension("_random", ["_randommodule.c"],
945 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Raymond Hettinger0c410272004-01-05 10:13:35 +0000946 # bisect
Victor Stinner8058bda2019-03-01 15:31:45 +0100947 self.add(Extension("_bisect", ["_bisectmodule.c"]))
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000948 # heapq
Victor Stinnerc45dbe932020-06-22 17:39:32 +0200949 self.add(Extension("_heapq", ["_heapqmodule.c"],
950 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000951 # C-optimized pickle replacement
Victor Stinner5c75f372019-04-17 23:02:26 +0200952 self.add(Extension("_pickle", ["_pickle.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200953 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimes90540002008-05-08 14:29:10 +0000954 # _json speedups
Victor Stinner8058bda2019-03-01 15:31:45 +0100955 self.add(Extension("_json", ["_json.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200956 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100957
Fred Drake0e474a82007-10-11 18:01:43 +0000958 # profiler (_lsprof is for cProfile.py)
Victor Stinner8058bda2019-03-01 15:31:45 +0100959 self.add(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000960 # static Unicode character database
Victor Stinner8058bda2019-03-01 15:31:45 +0100961 self.add(Extension('unicodedata', ['unicodedata.c'],
Victor Stinner47e1afd2020-10-26 16:43:47 +0100962 depends=['unicodedata_db.h', 'unicodename_db.h'],
963 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Larry Hastings3a907972013-11-23 14:49:22 -0800964 # _opcode module
Victor Stinner8058bda2019-03-01 15:31:45 +0100965 self.add(Extension('_opcode', ['_opcode.c']))
INADA Naoki9f2ce252016-10-15 15:39:19 +0900966 # asyncio speedups
Chris Jerdonekda742ba2020-05-17 22:47:31 -0700967 self.add(Extension("_asyncio", ["_asynciomodule.c"],
968 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000969 # _abc speedups
Victor Stinnercdad2722021-04-22 00:52:52 +0200970 self.add(Extension("_abc", ["_abc.c"],
971 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Antoine Pitrou94e16962018-01-16 00:27:16 +0100972 # _queue module
Victor Stinnercdad2722021-04-22 00:52:52 +0200973 self.add(Extension("_queue", ["_queuemodule.c"],
974 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Dong-hee Na0a18ee42019-08-24 07:20:30 +0900975 # _statistics module
976 self.add(Extension("_statistics", ["_statisticsmodule.c"]))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000977
978 # Modules with some UNIX dependencies -- on by default:
979 # (If you have a really backward UNIX, select and socket may not be
980 # supported...)
981
982 # fcntl(2) and ioctl(2)
Antoine Pitroua3000072010-09-07 14:52:42 +0000983 libs = []
Victor Stinner5ec33a12019-03-01 16:43:28 +0100984 if (self.config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
Antoine Pitroua3000072010-09-07 14:52:42 +0000985 # May be necessary on AIX for flock function
986 libs = ['bsd']
Victor Stinner8058bda2019-03-01 15:31:45 +0100987 self.add(Extension('fcntl', ['fcntlmodule.c'],
988 libraries=libs))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000989 # pwd(3)
Victor Stinner8058bda2019-03-01 15:31:45 +0100990 self.add(Extension('pwd', ['pwdmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000991 # grp(3)
pxinwr32f5fdd2019-02-27 19:09:28 +0800992 if not VXWORKS:
Victor Stinner8058bda2019-03-01 15:31:45 +0100993 self.add(Extension('grp', ['grpmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000994 # spwd, shadow passwords
Victor Stinner5ec33a12019-03-01 16:43:28 +0100995 if (self.config_h_vars.get('HAVE_GETSPNAM', False) or
996 self.config_h_vars.get('HAVE_GETSPENT', False)):
Victor Stinner8058bda2019-03-01 15:31:45 +0100997 self.add(Extension('spwd', ['spwdmodule.c']))
Michael Felt08970cb2019-06-21 15:58:00 +0200998 # AIX has shadow passwords, but access is not via getspent(), etc.
999 # module support is not expected so it not 'missing'
1000 elif not AIX:
Victor Stinner8058bda2019-03-01 15:31:45 +01001001 self.missing.append('spwd')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001002
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001003 # select(2); not on ancient System V
Victor Stinner8058bda2019-03-01 15:31:45 +01001004 self.add(Extension('select', ['selectmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001005
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001006 # Memory-mapped files (also works on Win32).
Victor Stinner8058bda2019-03-01 15:31:45 +01001007 self.add(Extension('mmap', ['mmapmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001008
Andrew M. Kuchling57269d02004-08-31 13:37:25 +00001009 # Lance Ellinghaus's syslog module
Ronald Oussoren94f25282010-05-05 19:11:21 +00001010 # syslog daemon interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001011 self.add(Extension('syslog', ['syslogmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001012
Eric Snow7f8bfc92018-01-29 18:23:44 -07001013 # Python interface to subinterpreter C-API.
Eric Snowc11183c2019-03-15 16:35:46 -06001014 self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
Eric Snow7f8bfc92018-01-29 18:23:44 -07001015
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001016 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +00001017 # Here ends the simple stuff. From here on, modules need certain
1018 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001019 #
1020
1021 # Multimedia modules
1022 # These don't work for 64-bit platforms!!!
1023 # These represent audio samples or images as strings:
Victor Stinnerdef80722016-04-19 15:58:11 +02001024 #
Neal Norwitz5e4a3b82004-07-19 16:55:07 +00001025 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +00001026 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001027 # 64-bit platforms.
Victor Stinnerdef80722016-04-19 15:58:11 +02001028 #
Benjamin Peterson8acaa312017-11-12 20:53:39 -08001029 # audioop needs libm for floor() in multiple functions.
Victor Stinner8058bda2019-03-01 15:31:45 +01001030 self.add(Extension('audioop', ['audioop.c'],
1031 libraries=['m']))
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001032
Victor Stinner5ec33a12019-03-01 16:43:28 +01001033 # CSV files
1034 self.add(Extension('_csv', ['_csv.c']))
1035
1036 # POSIX subprocess module helper.
Kyle Evans79925792020-10-13 15:04:44 -05001037 self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'],
1038 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner5ec33a12019-03-01 16:43:28 +01001039
Victor Stinnercfe172d2019-03-01 18:21:49 +01001040 def detect_test_extensions(self):
1041 # Python C API test module
1042 self.add(Extension('_testcapi', ['_testcapimodule.c'],
1043 depends=['testcapi_long.h']))
1044
Victor Stinner23bace22019-04-18 11:37:26 +02001045 # Python Internal C API test module
1046 self.add(Extension('_testinternalcapi', ['_testinternalcapi.c'],
Victor Stinner57491342019-04-23 12:26:33 +02001047 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner23bace22019-04-18 11:37:26 +02001048
Victor Stinnercfe172d2019-03-01 18:21:49 +01001049 # Python PEP-3118 (buffer protocol) test module
1050 self.add(Extension('_testbuffer', ['_testbuffer.c']))
1051
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001052 # Test loading multiple modules from one compiled file (https://bugs.python.org/issue16421)
Victor Stinnercfe172d2019-03-01 18:21:49 +01001053 self.add(Extension('_testimportmultiple', ['_testimportmultiple.c']))
1054
1055 # Test multi-phase extension module init (PEP 489)
1056 self.add(Extension('_testmultiphase', ['_testmultiphase.c']))
1057
1058 # Fuzz tests.
1059 self.add(Extension('_xxtestfuzz',
1060 ['_xxtestfuzz/_xxtestfuzz.c',
1061 '_xxtestfuzz/fuzzer.c']))
1062
Victor Stinner5ec33a12019-03-01 16:43:28 +01001063 def detect_readline_curses(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001064 # readline
Stefan Krah095b2732010-06-08 13:41:44 +00001065 readline_termcap_library = ""
1066 curses_library = ""
doko@ubuntu.com58844492012-06-30 18:25:32 +02001067 # Cannot use os.popen here in py3k.
1068 tmpfile = os.path.join(self.build_temp, 'readline_termcap_lib')
1069 if not os.path.exists(self.build_temp):
1070 os.makedirs(self.build_temp)
Stefan Krah095b2732010-06-08 13:41:44 +00001071 # Determine if readline is already linked against curses or tinfo.
Roland Hiebere1f77692021-02-09 02:05:25 +01001072 if sysconfig.get_config_var('HAVE_LIBREADLINE'):
1073 if sysconfig.get_config_var('WITH_EDITLINE'):
1074 readline_lib = 'edit'
1075 else:
1076 readline_lib = 'readline'
1077 do_readline = self.compiler.find_library_file(self.lib_dirs,
1078 readline_lib)
Victor Stinner4cbea512019-02-28 17:48:38 +01001079 if CROSS_COMPILING:
Victor Stinner6b982c22020-04-01 01:10:07 +02001080 ret = run_command("%s -d %s | grep '(NEEDED)' > %s"
doko@ubuntu.com58844492012-06-30 18:25:32 +02001081 % (sysconfig.get_config_var('READELF'),
1082 do_readline, tmpfile))
1083 elif find_executable('ldd'):
Victor Stinner6b982c22020-04-01 01:10:07 +02001084 ret = run_command("ldd %s > %s" % (do_readline, tmpfile))
doko@ubuntu.com58844492012-06-30 18:25:32 +02001085 else:
Victor Stinner6b982c22020-04-01 01:10:07 +02001086 ret = 1
1087 if ret == 0:
Brett Cannon9f5db072010-10-29 20:19:27 +00001088 with open(tmpfile) as fp:
1089 for ln in fp:
1090 if 'curses' in ln:
1091 readline_termcap_library = re.sub(
1092 r'.*lib(n?cursesw?)\.so.*', r'\1', ln
1093 ).rstrip()
1094 break
1095 # termcap interface split out from ncurses
1096 if 'tinfo' in ln:
1097 readline_termcap_library = 'tinfo'
1098 break
doko@ubuntu.com4c990712012-06-30 23:28:09 +02001099 if os.path.exists(tmpfile):
1100 os.unlink(tmpfile)
Roland Hiebere1f77692021-02-09 02:05:25 +01001101 else:
1102 do_readline = False
Stefan Krah095b2732010-06-08 13:41:44 +00001103 # Issue 7384: If readline is already linked against curses,
1104 # use the same library for the readline and curses modules.
1105 if 'curses' in readline_termcap_library:
1106 curses_library = readline_termcap_library
Victor Stinner625dbf22019-03-01 15:59:39 +01001107 elif self.compiler.find_library_file(self.lib_dirs, 'ncursesw'):
Stefan Krah095b2732010-06-08 13:41:44 +00001108 curses_library = 'ncursesw'
Michael Felt08970cb2019-06-21 15:58:00 +02001109 # Issue 36210: OSS provided ncurses does not link on AIX
1110 # Use IBM supplied 'curses' for successful build of _curses
1111 elif AIX and self.compiler.find_library_file(self.lib_dirs, 'curses'):
1112 curses_library = 'curses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001113 elif self.compiler.find_library_file(self.lib_dirs, 'ncurses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001114 curses_library = 'ncurses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001115 elif self.compiler.find_library_file(self.lib_dirs, 'curses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001116 curses_library = 'curses'
1117
Victor Stinner4cbea512019-02-28 17:48:38 +01001118 if MACOS:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001119 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren961683a2010-03-08 07:09:59 +00001120 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
Ned Deily04cdfa12014-06-25 13:36:14 -07001121 if (dep_target and
Ronald Oussoren49926cf2021-02-01 04:29:44 +01001122 (tuple(int(n) for n in dep_target.split('.')[0:2])
Ned Deily04cdfa12014-06-25 13:36:14 -07001123 < (10, 5) ) ):
Ronald Oussoren961683a2010-03-08 07:09:59 +00001124 os_release = 8
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001125 if os_release < 9:
1126 # MacOSX 10.4 has a broken readline. Don't try to build
1127 # the readline module unless the user has installed a fixed
1128 # readline package
Victor Stinner625dbf22019-03-01 15:59:39 +01001129 if find_file('readline/rlconf.h', self.inc_dirs, []) is None:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001130 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +00001131 if do_readline:
Victor Stinner4cbea512019-02-28 17:48:38 +01001132 if MACOS and os_release < 9:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133 # In every directory on the search path search for a dynamic
1134 # library and then a static library, instead of first looking
Fred Drake0af17612007-09-04 19:43:19 +00001135 # for dynamic libraries on the entire path.
Martin Pantere26da7c2016-06-02 10:07:09 +00001136 # This way a statically linked custom readline gets picked up
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001137 # before the (possibly broken) dynamic library in /usr/lib.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138 readline_extra_link_args = ('-Wl,-search_paths_first',)
1139 else:
1140 readline_extra_link_args = ()
1141
Roland Hiebere1f77692021-02-09 02:05:25 +01001142 readline_libs = [readline_lib]
Stefan Krah095b2732010-06-08 13:41:44 +00001143 if readline_termcap_library:
1144 pass # Issue 7384: Already linked against curses or tinfo.
1145 elif curses_library:
1146 readline_libs.append(curses_library)
Victor Stinner625dbf22019-03-01 15:59:39 +01001147 elif self.compiler.find_library_file(self.lib_dirs +
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001148 ['/usr/lib/termcap'],
1149 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +00001150 readline_libs.append('termcap')
Victor Stinner8058bda2019-03-01 15:31:45 +01001151 self.add(Extension('readline', ['readline.c'],
1152 library_dirs=['/usr/lib/termcap'],
1153 extra_link_args=readline_extra_link_args,
1154 libraries=readline_libs))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001155 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001156 self.missing.append('readline')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001157
Victor Stinner5ec33a12019-03-01 16:43:28 +01001158 # Curses support, requiring the System V version of curses, often
1159 # provided by the ncurses library.
1160 curses_defines = []
1161 curses_includes = []
1162 panel_library = 'panel'
1163 if curses_library == 'ncursesw':
1164 curses_defines.append(('HAVE_NCURSESW', '1'))
1165 if not CROSS_COMPILING:
1166 curses_includes.append('/usr/include/ncursesw')
1167 # Bug 1464056: If _curses.so links with ncursesw,
1168 # _curses_panel.so must link with panelw.
1169 panel_library = 'panelw'
1170 if MACOS:
1171 # On OS X, there is no separate /usr/lib/libncursesw nor
1172 # libpanelw. If we are here, we found a locally-supplied
1173 # version of libncursesw. There should also be a
1174 # libpanelw. _XOPEN_SOURCE defines are usually excluded
1175 # for OS X but we need _XOPEN_SOURCE_EXTENDED here for
1176 # ncurses wide char support
1177 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
1178 elif MACOS and curses_library == 'ncurses':
1179 # Building with the system-suppied combined libncurses/libpanel
1180 curses_defines.append(('HAVE_NCURSESW', '1'))
1181 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
Tim Peters2c60f7a2003-01-29 03:49:43 +00001182
Victor Stinnercfe172d2019-03-01 18:21:49 +01001183 curses_enabled = True
Victor Stinner5ec33a12019-03-01 16:43:28 +01001184 if curses_library.startswith('ncurses'):
1185 curses_libs = [curses_library]
1186 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001187 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001188 include_dirs=curses_includes,
1189 define_macros=curses_defines,
1190 libraries=curses_libs))
1191 elif curses_library == 'curses' and not MACOS:
1192 # OSX has an old Berkeley curses, not good enough for
1193 # the _curses module.
1194 if (self.compiler.find_library_file(self.lib_dirs, 'terminfo')):
1195 curses_libs = ['curses', 'terminfo']
1196 elif (self.compiler.find_library_file(self.lib_dirs, 'termcap')):
1197 curses_libs = ['curses', 'termcap']
1198 else:
1199 curses_libs = ['curses']
1200
1201 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001202 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001203 define_macros=curses_defines,
1204 libraries=curses_libs))
1205 else:
Victor Stinnercfe172d2019-03-01 18:21:49 +01001206 curses_enabled = False
Victor Stinner5ec33a12019-03-01 16:43:28 +01001207 self.missing.append('_curses')
1208
1209 # If the curses module is enabled, check for the panel module
Michael Felt08970cb2019-06-21 15:58:00 +02001210 # _curses_panel needs some form of ncurses
1211 skip_curses_panel = True if AIX else False
1212 if (curses_enabled and not skip_curses_panel and
1213 self.compiler.find_library_file(self.lib_dirs, panel_library)):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001214 self.add(Extension('_curses_panel', ['_curses_panel.c'],
Michael Felt08970cb2019-06-21 15:58:00 +02001215 include_dirs=curses_includes,
1216 define_macros=curses_defines,
1217 libraries=[panel_library, *curses_libs]))
1218 elif not skip_curses_panel:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001219 self.missing.append('_curses_panel')
1220
1221 def detect_crypt(self):
1222 # crypt module.
pxinwr236d0b72019-04-15 17:02:20 +08001223 if VXWORKS:
1224 # bpo-31904: crypt() function is not provided by VxWorks.
1225 # DES_crypt() OpenSSL provides is too weak to implement
1226 # the encryption.
Victor Stinnercad80202021-01-19 23:04:49 +01001227 self.missing.append('_crypt')
pxinwr236d0b72019-04-15 17:02:20 +08001228 return
1229
Victor Stinner625dbf22019-03-01 15:59:39 +01001230 if self.compiler.find_library_file(self.lib_dirs, 'crypt'):
Ronald Oussoren94f25282010-05-05 19:11:21 +00001231 libs = ['crypt']
Guido van Rossumd8faa362007-04-27 19:54:29 +00001232 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +00001233 libs = []
pxinwr32f5fdd2019-02-27 19:09:28 +08001234
Victor Stinnercad80202021-01-19 23:04:49 +01001235 self.add(Extension('_crypt', ['_cryptmodule.c'], libraries=libs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001236
Victor Stinner5ec33a12019-03-01 16:43:28 +01001237 def detect_socket(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001238 # socket(2)
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001239 kwargs = {'depends': ['socketmodule.h']}
pxinwr00a65682020-11-29 06:14:16 +08001240 if MACOS:
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001241 # Issue #35569: Expose RFC 3542 socket options.
1242 kwargs['extra_compile_args'] = ['-D__APPLE_USE_RFC_3542']
Erlend Egeberg Aasland9a45bfe2020-05-17 08:32:46 +02001243
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001244 self.add(Extension('_socket', ['socketmodule.c'], **kwargs))
pxinwr32f5fdd2019-02-27 19:09:28 +08001245
Victor Stinner5ec33a12019-03-01 16:43:28 +01001246 def detect_dbm_gdbm(self):
Georg Brandl489cb4f2009-07-11 10:08:49 +00001247 # Modules that provide persistent dictionary-like semantics. You will
1248 # probably want to arrange for at least one of them to be available on
1249 # your machine, though none are defined by default because of library
1250 # dependencies. The Python module dbm/__init__.py provides an
1251 # implementation independent wrapper for these; dbm/dumb.py provides
1252 # similar functionality (but slower of course) implemented in Python.
1253
1254 # Sleepycat^WOracle Berkeley DB interface.
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001255 # https://www.oracle.com/database/technologies/related/berkeleydb.html
Georg Brandl489cb4f2009-07-11 10:08:49 +00001256 #
1257 # This requires the Sleepycat^WOracle DB code. The supported versions
1258 # are set below. Visit the URL above to download
1259 # a release. Most open source OSes come with one or more
1260 # versions of BerkeleyDB already installed.
1261
doko@ubuntu.com15bac0f2012-07-01 10:35:54 +02001262 max_db_ver = (5, 3)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001263 min_db_ver = (3, 3)
1264 db_setup_debug = False # verbose debug prints from this script?
1265
1266 def allow_db_ver(db_ver):
1267 """Returns a boolean if the given BerkeleyDB version is acceptable.
1268
1269 Args:
1270 db_ver: A tuple of the version to verify.
1271 """
1272 if not (min_db_ver <= db_ver <= max_db_ver):
1273 return False
1274 return True
1275
1276 def gen_db_minor_ver_nums(major):
1277 if major == 4:
1278 for x in range(max_db_ver[1]+1):
1279 if allow_db_ver((4, x)):
1280 yield x
1281 elif major == 3:
1282 for x in (3,):
1283 if allow_db_ver((3, x)):
1284 yield x
1285 else:
1286 raise ValueError("unknown major BerkeleyDB version", major)
1287
1288 # construct a list of paths to look for the header file in on
1289 # top of the normal inc_dirs.
1290 db_inc_paths = [
1291 '/usr/include/db4',
1292 '/usr/local/include/db4',
1293 '/opt/sfw/include/db4',
1294 '/usr/include/db3',
1295 '/usr/local/include/db3',
1296 '/opt/sfw/include/db3',
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001297 # Fink defaults (https://www.finkproject.org/)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001298 '/sw/include/db4',
1299 '/sw/include/db3',
1300 ]
1301 # 4.x minor number specific paths
1302 for x in gen_db_minor_ver_nums(4):
1303 db_inc_paths.append('/usr/include/db4%d' % x)
1304 db_inc_paths.append('/usr/include/db4.%d' % x)
1305 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
1306 db_inc_paths.append('/usr/local/include/db4%d' % x)
1307 db_inc_paths.append('/pkg/db-4.%d/include' % x)
1308 db_inc_paths.append('/opt/db-4.%d/include' % x)
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001309 # MacPorts default (https://www.macports.org/)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001310 db_inc_paths.append('/opt/local/include/db4%d' % x)
1311 # 3.x minor number specific paths
1312 for x in gen_db_minor_ver_nums(3):
1313 db_inc_paths.append('/usr/include/db3%d' % x)
1314 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
1315 db_inc_paths.append('/usr/local/include/db3%d' % x)
1316 db_inc_paths.append('/pkg/db-3.%d/include' % x)
1317 db_inc_paths.append('/opt/db-3.%d/include' % x)
1318
Victor Stinner4cbea512019-02-28 17:48:38 +01001319 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001320 db_inc_paths = []
1321
Georg Brandl489cb4f2009-07-11 10:08:49 +00001322 # Add some common subdirectories for Sleepycat DB to the list,
1323 # based on the standard include directories. This way DB3/4 gets
1324 # picked up when it is installed in a non-standard prefix and
1325 # the user has added that prefix into inc_dirs.
1326 std_variants = []
Victor Stinner625dbf22019-03-01 15:59:39 +01001327 for dn in self.inc_dirs:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001328 std_variants.append(os.path.join(dn, 'db3'))
1329 std_variants.append(os.path.join(dn, 'db4'))
1330 for x in gen_db_minor_ver_nums(4):
1331 std_variants.append(os.path.join(dn, "db4%d"%x))
1332 std_variants.append(os.path.join(dn, "db4.%d"%x))
1333 for x in gen_db_minor_ver_nums(3):
1334 std_variants.append(os.path.join(dn, "db3%d"%x))
1335 std_variants.append(os.path.join(dn, "db3.%d"%x))
1336
1337 db_inc_paths = std_variants + db_inc_paths
1338 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
1339
1340 db_ver_inc_map = {}
1341
Victor Stinner4cbea512019-02-28 17:48:38 +01001342 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001343 sysroot = macosx_sdk_root()
1344
Georg Brandl489cb4f2009-07-11 10:08:49 +00001345 class db_found(Exception): pass
1346 try:
1347 # See whether there is a Sleepycat header in the standard
1348 # search path.
Victor Stinner625dbf22019-03-01 15:59:39 +01001349 for d in self.inc_dirs + db_inc_paths:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001350 f = os.path.join(d, "db.h")
Victor Stinner4cbea512019-02-28 17:48:38 +01001351 if MACOS and is_macosx_sdk_path(d):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001352 f = os.path.join(sysroot, d[1:], "db.h")
1353
Georg Brandl489cb4f2009-07-11 10:08:49 +00001354 if db_setup_debug: print("db: looking for db.h in", f)
1355 if os.path.exists(f):
Brett Cannon9f5db072010-10-29 20:19:27 +00001356 with open(f, 'rb') as file:
1357 f = file.read()
Benjamin Peterson019f3612009-08-12 18:18:03 +00001358 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001359 if m:
1360 db_major = int(m.group(1))
Benjamin Peterson019f3612009-08-12 18:18:03 +00001361 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001362 db_minor = int(m.group(1))
1363 db_ver = (db_major, db_minor)
1364
1365 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
1366 if db_ver == (4, 6):
Benjamin Peterson019f3612009-08-12 18:18:03 +00001367 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001368 db_patch = int(m.group(1))
1369 if db_patch < 21:
1370 print("db.h:", db_ver, "patch", db_patch,
1371 "being ignored (4.6.x must be >= 4.6.21)")
1372 continue
1373
1374 if ( (db_ver not in db_ver_inc_map) and
1375 allow_db_ver(db_ver) ):
1376 # save the include directory with the db.h version
1377 # (first occurrence only)
1378 db_ver_inc_map[db_ver] = d
1379 if db_setup_debug:
1380 print("db.h: found", db_ver, "in", d)
1381 else:
1382 # we already found a header for this library version
1383 if db_setup_debug: print("db.h: ignoring", d)
1384 else:
1385 # ignore this header, it didn't contain a version number
1386 if db_setup_debug:
1387 print("db.h: no version number version in", d)
1388
1389 db_found_vers = list(db_ver_inc_map.keys())
1390 db_found_vers.sort()
1391
1392 while db_found_vers:
1393 db_ver = db_found_vers.pop()
1394 db_incdir = db_ver_inc_map[db_ver]
1395
1396 # check lib directories parallel to the location of the header
1397 db_dirs_to_check = [
1398 db_incdir.replace("include", 'lib64'),
1399 db_incdir.replace("include", 'lib'),
1400 ]
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001401
Victor Stinner4cbea512019-02-28 17:48:38 +01001402 if not MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001403 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check))
1404
1405 else:
1406 # Same as other branch, but takes OSX SDK into account
1407 tmp = []
1408 for dn in db_dirs_to_check:
1409 if is_macosx_sdk_path(dn):
1410 if os.path.isdir(os.path.join(sysroot, dn[1:])):
1411 tmp.append(dn)
1412 else:
1413 if os.path.isdir(dn):
1414 tmp.append(dn)
Ronald Oussorendc969e52010-06-27 12:37:46 +00001415 db_dirs_to_check = tmp
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001416
1417 db_dirs_to_check = tmp
Georg Brandl489cb4f2009-07-11 10:08:49 +00001418
Ezio Melotti42da6632011-03-15 05:18:48 +02001419 # Look for a version specific db-X.Y before an ambiguous dbX
Georg Brandl489cb4f2009-07-11 10:08:49 +00001420 # XXX should we -ever- look for a dbX name? Do any
1421 # systems really not name their library by version and
1422 # symlink to more general names?
1423 for dblib in (('db-%d.%d' % db_ver),
1424 ('db%d%d' % db_ver),
1425 ('db%d' % db_ver[0])):
1426 dblib_file = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001427 db_dirs_to_check + self.lib_dirs, dblib )
Georg Brandl489cb4f2009-07-11 10:08:49 +00001428 if dblib_file:
1429 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
1430 raise db_found
1431 else:
1432 if db_setup_debug: print("db lib: ", dblib, "not found")
1433
1434 except db_found:
1435 if db_setup_debug:
1436 print("bsddb using BerkeleyDB lib:", db_ver, dblib)
1437 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001438 dblibs = [dblib]
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001439 # Only add the found library and include directories if they aren't
1440 # already being searched. This avoids an explicit runtime library
1441 # dependency.
Victor Stinner625dbf22019-03-01 15:59:39 +01001442 if db_incdir in self.inc_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001443 db_incs = None
1444 else:
1445 db_incs = [db_incdir]
Victor Stinner625dbf22019-03-01 15:59:39 +01001446 if dblib_dir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001447 dblib_dir = None
Georg Brandl489cb4f2009-07-11 10:08:49 +00001448 else:
1449 if db_setup_debug: print("db: no appropriate library found")
1450 db_incs = None
1451 dblibs = []
1452 dblib_dir = None
1453
Victor Stinner5ec33a12019-03-01 16:43:28 +01001454 dbm_setup_debug = False # verbose debug prints from this script?
1455 dbm_order = ['gdbm']
1456 # The standard Unix dbm module:
1457 if not CYGWIN:
1458 config_args = [arg.strip("'")
1459 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
1460 dbm_args = [arg for arg in config_args
1461 if arg.startswith('--with-dbmliborder=')]
1462 if dbm_args:
1463 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
1464 else:
1465 dbm_order = "ndbm:gdbm:bdb".split(":")
1466 dbmext = None
1467 for cand in dbm_order:
1468 if cand == "ndbm":
1469 if find_file("ndbm.h", self.inc_dirs, []) is not None:
1470 # Some systems have -lndbm, others have -lgdbm_compat,
1471 # others don't have either
1472 if self.compiler.find_library_file(self.lib_dirs,
1473 'ndbm'):
1474 ndbm_libs = ['ndbm']
1475 elif self.compiler.find_library_file(self.lib_dirs,
1476 'gdbm_compat'):
1477 ndbm_libs = ['gdbm_compat']
1478 else:
1479 ndbm_libs = []
1480 if dbm_setup_debug: print("building dbm using ndbm")
1481 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1482 define_macros=[
1483 ('HAVE_NDBM_H',None),
1484 ],
1485 libraries=ndbm_libs)
1486 break
1487
1488 elif cand == "gdbm":
1489 if self.compiler.find_library_file(self.lib_dirs, 'gdbm'):
1490 gdbm_libs = ['gdbm']
1491 if self.compiler.find_library_file(self.lib_dirs,
1492 'gdbm_compat'):
1493 gdbm_libs.append('gdbm_compat')
1494 if find_file("gdbm/ndbm.h", self.inc_dirs, []) is not None:
1495 if dbm_setup_debug: print("building dbm using gdbm")
1496 dbmext = Extension(
1497 '_dbm', ['_dbmmodule.c'],
1498 define_macros=[
1499 ('HAVE_GDBM_NDBM_H', None),
1500 ],
1501 libraries = gdbm_libs)
1502 break
1503 if find_file("gdbm-ndbm.h", self.inc_dirs, []) is not None:
1504 if dbm_setup_debug: print("building dbm using gdbm")
1505 dbmext = Extension(
1506 '_dbm', ['_dbmmodule.c'],
1507 define_macros=[
1508 ('HAVE_GDBM_DASH_NDBM_H', None),
1509 ],
1510 libraries = gdbm_libs)
1511 break
1512 elif cand == "bdb":
1513 if dblibs:
1514 if dbm_setup_debug: print("building dbm using bdb")
1515 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1516 library_dirs=dblib_dir,
1517 runtime_library_dirs=dblib_dir,
1518 include_dirs=db_incs,
1519 define_macros=[
1520 ('HAVE_BERKDB_H', None),
1521 ('DB_DBM_HSEARCH', None),
1522 ],
1523 libraries=dblibs)
1524 break
1525 if dbmext is not None:
1526 self.add(dbmext)
1527 else:
1528 self.missing.append('_dbm')
1529
1530 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
1531 if ('gdbm' in dbm_order and
1532 self.compiler.find_library_file(self.lib_dirs, 'gdbm')):
1533 self.add(Extension('_gdbm', ['_gdbmmodule.c'],
1534 libraries=['gdbm']))
1535 else:
1536 self.missing.append('_gdbm')
1537
1538 def detect_sqlite(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 # The sqlite interface
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 sqlite_setup_debug = False # verbose debug prints from this script?
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541
1542 # We hunt for #define SQLITE_VERSION "n.n.n"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543 sqlite_incdir = sqlite_libdir = None
1544 sqlite_inc_paths = [ '/usr/include',
1545 '/usr/include/sqlite',
1546 '/usr/include/sqlite3',
1547 '/usr/local/include',
1548 '/usr/local/include/sqlite',
1549 '/usr/local/include/sqlite3',
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001550 ]
Victor Stinner4cbea512019-02-28 17:48:38 +01001551 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001552 sqlite_inc_paths = []
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +01001553 MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554 MIN_SQLITE_VERSION = ".".join([str(x)
1555 for x in MIN_SQLITE_VERSION_NUMBER])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001556
1557 # Scan the default include directories before the SQLite specific
1558 # ones. This allows one to override the copy of sqlite on OSX,
1559 # where /usr/include contains an old version of sqlite.
Victor Stinner4cbea512019-02-28 17:48:38 +01001560 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001561 sysroot = macosx_sdk_root()
1562
Victor Stinner625dbf22019-03-01 15:59:39 +01001563 for d_ in self.inc_dirs + sqlite_inc_paths:
Ned Deily9b635832012-08-05 15:13:33 -07001564 d = d_
Victor Stinner4cbea512019-02-28 17:48:38 +01001565 if MACOS and is_macosx_sdk_path(d):
Ned Deily9b635832012-08-05 15:13:33 -07001566 d = os.path.join(sysroot, d[1:])
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001567
Ned Deily9b635832012-08-05 15:13:33 -07001568 f = os.path.join(d, "sqlite3.h")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001569 if os.path.exists(f):
Guido van Rossum452bf512007-02-09 05:32:43 +00001570 if sqlite_setup_debug: print("sqlite: found %s"%f)
Brett Cannon9f5db072010-10-29 20:19:27 +00001571 with open(f) as file:
1572 incf = file.read()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001573 m = re.search(
Petri Lehtinened909bc2013-02-23 17:05:28 +01001574 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 if m:
1576 sqlite_version = m.group(1)
1577 sqlite_version_tuple = tuple([int(x)
1578 for x in sqlite_version.split(".")])
1579 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
1580 # we win!
Thomas Wouters89f507f2006-12-13 04:49:30 +00001581 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001582 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001583 sqlite_incdir = d
1584 break
1585 else:
1586 if sqlite_setup_debug:
Charles Pigottad0daf52019-04-26 16:38:12 +01001587 print("%s: version %s is too old, need >= %s"%(d,
Guido van Rossum452bf512007-02-09 05:32:43 +00001588 sqlite_version, MIN_SQLITE_VERSION))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001589 elif sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001590 print("sqlite: %s had no SQLITE_VERSION"%(f,))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001591
1592 if sqlite_incdir:
1593 sqlite_dirs_to_check = [
1594 os.path.join(sqlite_incdir, '..', 'lib64'),
1595 os.path.join(sqlite_incdir, '..', 'lib'),
1596 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
1597 os.path.join(sqlite_incdir, '..', '..', 'lib'),
1598 ]
Tarek Ziadé36797272010-07-22 12:50:05 +00001599 sqlite_libfile = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001600 sqlite_dirs_to_check + self.lib_dirs, 'sqlite3')
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001601 if sqlite_libfile:
1602 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603
1604 if sqlite_incdir and sqlite_libdir:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001605 sqlite_srcs = ['_sqlite/cache.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001606 '_sqlite/connection.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001607 '_sqlite/cursor.c',
1608 '_sqlite/microprotocols.c',
1609 '_sqlite/module.c',
1610 '_sqlite/prepare_protocol.c',
1611 '_sqlite/row.c',
1612 '_sqlite/statement.c',
1613 '_sqlite/util.c', ]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001614 sqlite_defines = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001615
Benjamin Peterson076ed002010-10-31 17:11:02 +00001616 # Enable support for loadable extensions in the sqlite3 module
1617 # if --enable-loadable-sqlite-extensions configure option is used.
1618 if '--enable-loadable-sqlite-extensions' not in sysconfig.get_config_var("CONFIG_ARGS"):
1619 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Miss Islington (bot)baa8d482021-08-27 04:29:24 -07001620 elif MACOS and sqlite_incdir == os.path.join(MACOS_SDK_ROOT, "usr/include"):
1621 raise DistutilsError("System version of SQLite does not support loadable extensions")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622
Victor Stinner4cbea512019-02-28 17:48:38 +01001623 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 # In every directory on the search path search for a dynamic
1625 # library and then a static library, instead of first looking
Ezio Melotti13925002011-03-16 11:05:33 +02001626 # for dynamic libraries on the entire path.
1627 # This way a statically linked custom sqlite gets picked up
Thomas Wouters477c8d52006-05-27 19:21:47 +00001628 # before the dynamic library in /usr/lib.
1629 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1630 else:
1631 sqlite_extra_link_args = ()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001632
Brett Cannonc5011fe2011-06-06 20:09:10 -07001633 include_dirs = ["Modules/_sqlite"]
1634 # Only include the directory where sqlite was found if it does
1635 # not already exist in set include directories, otherwise you
1636 # can end up with a bad search path order.
1637 if sqlite_incdir not in self.compiler.include_dirs:
1638 include_dirs.append(sqlite_incdir)
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001639 # avoid a runtime library path for a system library dir
Victor Stinner625dbf22019-03-01 15:59:39 +01001640 if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001641 sqlite_libdir = None
Victor Stinner8058bda2019-03-01 15:31:45 +01001642 self.add(Extension('_sqlite3', sqlite_srcs,
1643 define_macros=sqlite_defines,
1644 include_dirs=include_dirs,
1645 library_dirs=sqlite_libdir,
1646 extra_link_args=sqlite_extra_link_args,
1647 libraries=["sqlite3",]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001648 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001649 self.missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001650
Victor Stinner5ec33a12019-03-01 16:43:28 +01001651 def detect_platform_specific_exts(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001652 # Unix-only modules
Victor Stinner4cbea512019-02-28 17:48:38 +01001653 if not MS_WINDOWS:
pxinwr32f5fdd2019-02-27 19:09:28 +08001654 if not VXWORKS:
1655 # Steen Lumholt's termios module
Victor Stinner8058bda2019-03-01 15:31:45 +01001656 self.add(Extension('termios', ['termios.c']))
pxinwr32f5fdd2019-02-27 19:09:28 +08001657 # Jeremy Hylton's rlimit interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001658 self.add(Extension('resource', ['resource.c']))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001659 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001660 self.missing.extend(['resource', 'termios'])
Christian Heimes29a7df72018-01-26 23:28:46 +01001661
Victor Stinner5ec33a12019-03-01 16:43:28 +01001662 # Platform-specific libraries
1663 if HOST_PLATFORM.startswith(('linux', 'freebsd', 'gnukfreebsd')):
1664 self.add(Extension('ossaudiodev', ['ossaudiodev.c']))
Michael Felt08970cb2019-06-21 15:58:00 +02001665 elif not AIX:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001666 self.missing.append('ossaudiodev')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001667
Victor Stinner5ec33a12019-03-01 16:43:28 +01001668 if MACOS:
Ned Deily951ab582020-05-18 11:31:21 -04001669 self.add(Extension('_scproxy', ['_scproxy.c'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001670 extra_link_args=[
1671 '-framework', 'SystemConfiguration',
Ned Deily951ab582020-05-18 11:31:21 -04001672 '-framework', 'CoreFoundation']))
Fredrik Lundhade711a2001-01-24 08:00:28 +00001673
Victor Stinner5ec33a12019-03-01 16:43:28 +01001674 def detect_compress_exts(self):
Barry Warsaw259b1e12002-08-13 20:09:26 +00001675 # Andrew Kuchling's zlib module. Note that some versions of zlib
1676 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1677 # http://www.cert.org/advisories/CA-2002-07.html
1678 #
1679 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1680 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1681 # now, we still accept 1.1.3, because we think it's difficult to
1682 # exploit this in Python, and we'd rather make it RedHat's problem
1683 # than our problem <wink>.
1684 #
1685 # You can upgrade zlib to version 1.1.4 yourself by going to
1686 # http://www.gzip.org/zlib/
Victor Stinner625dbf22019-03-01 15:59:39 +01001687 zlib_inc = find_file('zlib.h', [], self.inc_dirs)
Christian Heimes1dc54002008-03-24 02:19:29 +00001688 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001689 if zlib_inc is not None:
1690 zlib_h = zlib_inc[0] + '/zlib.h'
1691 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001692 version_req = '"1.1.3"'
Victor Stinner4cbea512019-02-28 17:48:38 +01001693 if MACOS and is_macosx_sdk_path(zlib_h):
Ned Deily507c5912013-10-18 21:32:00 -07001694 zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
Brett Cannon9f5db072010-10-29 20:19:27 +00001695 with open(zlib_h) as fp:
1696 while 1:
1697 line = fp.readline()
1698 if not line:
1699 break
1700 if line.startswith('#define ZLIB_VERSION'):
1701 version = line.split()[2]
1702 break
Guido van Rossume6970912001-04-15 15:16:12 +00001703 if version >= version_req:
Victor Stinner625dbf22019-03-01 15:59:39 +01001704 if (self.compiler.find_library_file(self.lib_dirs, 'z')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001705 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001706 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1707 else:
1708 zlib_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001709 self.add(Extension('zlib', ['zlibmodule.c'],
1710 libraries=['z'],
1711 extra_link_args=zlib_extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001712 have_zlib = True
Guido van Rossumd8faa362007-04-27 19:54:29 +00001713 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001714 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001715 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001716 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001717 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001718 self.missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001719
Christian Heimes1dc54002008-03-24 02:19:29 +00001720 # Helper module for various ascii-encoders. Uses zlib for an optimized
1721 # crc32 if we have it. Otherwise binascii uses its own.
1722 if have_zlib:
1723 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1724 libraries = ['z']
1725 extra_link_args = zlib_extra_link_args
1726 else:
1727 extra_compile_args = []
1728 libraries = []
1729 extra_link_args = []
Victor Stinner8058bda2019-03-01 15:31:45 +01001730 self.add(Extension('binascii', ['binascii.c'],
1731 extra_compile_args=extra_compile_args,
1732 libraries=libraries,
1733 extra_link_args=extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001734
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001735 # Gustavo Niemeyer's bz2 module.
Victor Stinner625dbf22019-03-01 15:59:39 +01001736 if (self.compiler.find_library_file(self.lib_dirs, 'bz2')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001737 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001738 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1739 else:
1740 bz2_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001741 self.add(Extension('_bz2', ['_bz2module.c'],
1742 libraries=['bz2'],
1743 extra_link_args=bz2_extra_link_args))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001744 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001745 self.missing.append('_bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001746
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001747 # LZMA compression support.
Victor Stinner625dbf22019-03-01 15:59:39 +01001748 if self.compiler.find_library_file(self.lib_dirs, 'lzma'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001749 self.add(Extension('_lzma', ['_lzmamodule.c'],
1750 libraries=['lzma']))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001751 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001752 self.missing.append('_lzma')
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001753
Victor Stinner5ec33a12019-03-01 16:43:28 +01001754 def detect_expat_elementtree(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001755 # Interface to the Expat XML parser
1756 #
Benjamin Petersona28e7022010-01-09 18:53:06 +00001757 # Expat was written by James Clark and is now maintained by a group of
1758 # developers on SourceForge; see www.libexpat.org for more information.
1759 # The pyexpat module was written by Paul Prescod after a prototype by
1760 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1761 # of a system shared libexpat.so is possible with --with-system-expat
Benjamin Petersonc73206c2010-10-31 16:38:19 +00001762 # configure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001763 #
1764 # More information on Expat can be found at www.libexpat.org.
1765 #
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001766 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1767 expat_inc = []
1768 define_macros = []
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001769 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001770 expat_lib = ['expat']
1771 expat_sources = []
Christian Heimesd489c7a2013-02-09 17:02:06 +01001772 expat_depends = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001773 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01001774 expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')]
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001775 define_macros = [
1776 ('HAVE_EXPAT_CONFIG_H', '1'),
Victor Stinner93d0cb52017-08-18 23:43:54 +02001777 # bpo-30947: Python uses best available entropy sources to
1778 # call XML_SetHashSalt(), expat entropy sources are not needed
1779 ('XML_POOR_ENTROPY', '1'),
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001780 ]
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001781 extra_compile_args = []
Miss Islington (bot)412ae8a2021-09-29 07:13:41 -07001782 # bpo-44394: libexpat uses isnan() of math.h and needs linkage
1783 # against the libm
1784 expat_lib = ['m']
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001785 expat_sources = ['expat/xmlparse.c',
1786 'expat/xmlrole.c',
1787 'expat/xmltok.c']
Christian Heimesd489c7a2013-02-09 17:02:06 +01001788 expat_depends = ['expat/ascii.h',
1789 'expat/asciitab.h',
1790 'expat/expat.h',
1791 'expat/expat_config.h',
1792 'expat/expat_external.h',
1793 'expat/internal.h',
1794 'expat/latin1tab.h',
1795 'expat/utf8tab.h',
1796 'expat/xmlrole.h',
1797 'expat/xmltok.h',
1798 'expat/xmltok_impl.h'
1799 ]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001801 cc = sysconfig.get_config_var('CC').split()[0]
Victor Stinner6b982c22020-04-01 01:10:07 +02001802 ret = run_command(
Benjamin Peterson95da3102019-06-29 16:00:22 -07001803 '"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc)
Victor Stinner6b982c22020-04-01 01:10:07 +02001804 if ret == 0:
Benjamin Peterson95da3102019-06-29 16:00:22 -07001805 extra_compile_args.append('-Wno-unreachable-code')
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001806
Victor Stinner8058bda2019-03-01 15:31:45 +01001807 self.add(Extension('pyexpat',
1808 define_macros=define_macros,
1809 extra_compile_args=extra_compile_args,
1810 include_dirs=expat_inc,
1811 libraries=expat_lib,
1812 sources=['pyexpat.c'] + expat_sources,
1813 depends=expat_depends))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001814
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001815 # Fredrik Lundh's cElementTree module. Note that this also
1816 # uses expat (via the CAPI hook in pyexpat).
1817
Victor Stinner625dbf22019-03-01 15:59:39 +01001818 if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001819 define_macros.append(('USE_PYEXPAT_CAPI', None))
Victor Stinner8058bda2019-03-01 15:31:45 +01001820 self.add(Extension('_elementtree',
1821 define_macros=define_macros,
1822 include_dirs=expat_inc,
1823 libraries=expat_lib,
1824 sources=['_elementtree.c'],
1825 depends=['pyexpat.c', *expat_sources,
1826 *expat_depends]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001827 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001828 self.missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001829
Victor Stinner5ec33a12019-03-01 16:43:28 +01001830 def detect_multibytecodecs(self):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001831 # Hye-Shik Chang's CJKCodecs modules.
Victor Stinner8058bda2019-03-01 15:31:45 +01001832 self.add(Extension('_multibytecodec',
1833 ['cjkcodecs/multibytecodec.c']))
Walter Dörwalde9eaab42007-05-22 16:02:13 +00001834 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001835 self.add(Extension('_codecs_%s' % loc,
1836 ['cjkcodecs/_codecs_%s.c' % loc]))
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001837
Victor Stinner5ec33a12019-03-01 16:43:28 +01001838 def detect_multiprocessing(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001839 # Richard Oudkerk's multiprocessing module
Victor Stinner4cbea512019-02-28 17:48:38 +01001840 if MS_WINDOWS:
Victor Stinnerc991f242019-03-01 17:19:04 +01001841 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c',
1842 '_multiprocessing/semaphore.c']
Benjamin Petersone711caf2008-06-11 16:44:04 +00001843 else:
Victor Stinnerc991f242019-03-01 17:19:04 +01001844 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c']
Mark Dickinsona614f042009-11-28 12:48:43 +00001845 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
1846 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001847 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
Victor Stinner8058bda2019-03-01 15:31:45 +01001848 self.add(Extension('_multiprocessing', multiprocessing_srcs,
Victor Stinner8058bda2019-03-01 15:31:45 +01001849 include_dirs=["Modules/_multiprocessing"]))
Guido van Rossuma9e20242007-03-08 00:43:48 +00001850
Victor Stinnercad80202021-01-19 23:04:49 +01001851 if (not MS_WINDOWS and
1852 sysconfig.get_config_var('HAVE_SHM_OPEN') and
1853 sysconfig.get_config_var('HAVE_SHM_UNLINK')):
1854 posixshmem_srcs = ['_multiprocessing/posixshmem.c']
1855 libs = []
1856 if sysconfig.get_config_var('SHM_NEEDS_LIBRT'):
1857 # need to link with librt to get shm_open()
1858 libs.append('rt')
1859 self.add(Extension('_posixshmem', posixshmem_srcs,
1860 define_macros={},
1861 libraries=libs,
1862 include_dirs=["Modules/_multiprocessing"]))
1863 else:
1864 self.missing.append('_posixshmem')
1865
Victor Stinner5ec33a12019-03-01 16:43:28 +01001866 def detect_uuid(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001867 # Build the _uuid module if possible
Miss Islington (bot)b71bc052021-11-02 04:49:17 -07001868 uuid_h = sysconfig.get_config_var("HAVE_UUID_H")
1869 uuid_uuid_h = sysconfig.get_config_var("HAVE_UUID_UUID_H")
1870 if uuid_h or uuid_uuid_h:
1871 if sysconfig.get_config_var("HAVE_LIBUUID"):
1872 uuid_libs = ["uuid"]
Antoine Pitroua106aec2017-09-28 23:03:06 +02001873 else:
1874 uuid_libs = []
Victor Stinnercfe172d2019-03-01 18:21:49 +01001875 self.add(Extension('_uuid', ['_uuidmodule.c'],
Miss Islington (bot)b71bc052021-11-02 04:49:17 -07001876 libraries=uuid_libs))
Antoine Pitroua106aec2017-09-28 23:03:06 +02001877 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001878 self.missing.append('_uuid')
Antoine Pitroua106aec2017-09-28 23:03:06 +02001879
Victor Stinner5ec33a12019-03-01 16:43:28 +01001880 def detect_modules(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001881 self.detect_simple_extensions()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001882 if TEST_EXTENSIONS:
1883 self.detect_test_extensions()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001884 self.detect_readline_curses()
1885 self.detect_crypt()
1886 self.detect_socket()
1887 self.detect_openssl_hashlib()
xdegaye2ee077f2019-04-09 17:20:08 +02001888 self.detect_hash_builtins()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001889 self.detect_dbm_gdbm()
1890 self.detect_sqlite()
1891 self.detect_platform_specific_exts()
1892 self.detect_nis()
1893 self.detect_compress_exts()
1894 self.detect_expat_elementtree()
1895 self.detect_multibytecodecs()
1896 self.detect_decimal()
1897 self.detect_ctypes()
1898 self.detect_multiprocessing()
1899 if not self.detect_tkinter():
1900 self.missing.append('_tkinter')
1901 self.detect_uuid()
1902
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001903## # Uncomment these lines if you want to play with xxmodule.c
Victor Stinnercfe172d2019-03-01 18:21:49 +01001904## self.add(Extension('xx', ['xxmodule.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001905
Hai Shi5787ba42021-04-06 20:55:13 +08001906 # The limited C API is not compatible with the Py_TRACE_REFS macro.
1907 if not sysconfig.get_config_var('Py_TRACE_REFS'):
1908 self.add(Extension('xxlimited', ['xxlimited.c']))
1909 self.add(Extension('xxlimited_35', ['xxlimited_35.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001910
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001911 def detect_tkinter_fromenv(self):
1912 # Build _tkinter using the Tcl/Tk locations specified by
1913 # the _TCLTK_INCLUDES and _TCLTK_LIBS environment variables.
1914 # This method is meant to be invoked by detect_tkinter().
Ned Deilyd819b932013-09-06 01:07:05 -07001915 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001916 # The variables can be set via one of the following ways.
Ned Deilyd819b932013-09-06 01:07:05 -07001917 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001918 # - Automatically, at configuration time, by using pkg-config.
1919 # The tool is called by the configure script.
1920 # Additional pkg-config configuration paths can be set via the
1921 # PKG_CONFIG_PATH environment variable.
1922 #
1923 # PKG_CONFIG_PATH=".../lib/pkgconfig" ./configure ...
1924 #
1925 # - Explicitly, at configuration time by setting both
1926 # --with-tcltk-includes and --with-tcltk-libs.
1927 #
1928 # ./configure ... \
Ned Deilyd819b932013-09-06 01:07:05 -07001929 # --with-tcltk-includes="-I/path/to/tclincludes \
1930 # -I/path/to/tkincludes"
1931 # --with-tcltk-libs="-L/path/to/tcllibs -ltclm.n \
1932 # -L/path/to/tklibs -ltkm.n"
1933 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001934 # - Explicitly, at compile time, by passing TCLTK_INCLUDES and
1935 # TCLTK_LIBS to the make target.
1936 # This will override any configuration-time option.
1937 #
1938 # make TCLTK_INCLUDES="..." TCLTK_LIBS="..."
Ned Deilyd819b932013-09-06 01:07:05 -07001939 #
1940 # This can be useful for building and testing tkinter with multiple
1941 # versions of Tcl/Tk. Note that a build of Tk depends on a particular
1942 # build of Tcl so you need to specify both arguments and use care when
1943 # overriding.
1944
1945 # The _TCLTK variables are created in the Makefile sharedmods target.
1946 tcltk_includes = os.environ.get('_TCLTK_INCLUDES')
1947 tcltk_libs = os.environ.get('_TCLTK_LIBS')
1948 if not (tcltk_includes and tcltk_libs):
1949 # Resume default configuration search.
Victor Stinner4cbea512019-02-28 17:48:38 +01001950 return False
Ned Deilyd819b932013-09-06 01:07:05 -07001951
1952 extra_compile_args = tcltk_includes.split()
1953 extra_link_args = tcltk_libs.split()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001954 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1955 define_macros=[('WITH_APPINIT', 1)],
1956 extra_compile_args = extra_compile_args,
1957 extra_link_args = extra_link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01001958 return True
Ned Deilyd819b932013-09-06 01:07:05 -07001959
Victor Stinner625dbf22019-03-01 15:59:39 +01001960 def detect_tkinter_darwin(self):
Ned Deily1731d6d2020-05-18 04:32:38 -04001961 # Build default _tkinter on macOS using Tcl and Tk frameworks.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001962 # This method is meant to be invoked by detect_tkinter().
Ned Deily1731d6d2020-05-18 04:32:38 -04001963 #
1964 # The macOS native Tk (AKA Aqua Tk) and Tcl are most commonly
1965 # built and installed as macOS framework bundles. However,
1966 # for several reasons, we cannot take full advantage of the
1967 # Apple-supplied compiler chain's -framework options here.
1968 # Instead, we need to find and pass to the compiler the
1969 # absolute paths of the Tcl and Tk headers files we want to use
1970 # and the absolute path to the directory containing the Tcl
1971 # and Tk frameworks for linking.
1972 #
1973 # We want to handle here two common use cases on macOS:
1974 # 1. Build and link with system-wide third-party or user-built
1975 # Tcl and Tk frameworks installed in /Library/Frameworks.
1976 # 2. Build and link using a user-specified macOS SDK so that the
1977 # built Python can be exported to other systems. In this case,
1978 # search only the SDK's /Library/Frameworks (normally empty)
1979 # and /System/Library/Frameworks.
1980 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001981 # Any other use cases are handled either by detect_tkinter_fromenv(),
1982 # or detect_tkinter(). The former handles non-standard locations of
1983 # Tcl/Tk, defined via the _TCLTK_INCLUDES and _TCLTK_LIBS environment
1984 # variables. The latter handles any Tcl/Tk versions installed in
1985 # standard Unix directories.
1986 #
1987 # It would be desirable to also handle here the case where
Ned Deily1731d6d2020-05-18 04:32:38 -04001988 # you want to build and link with a framework build of Tcl and Tk
1989 # that is not in /Library/Frameworks, say, in your private
1990 # $HOME/Library/Frameworks directory or elsewhere. It turns
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301991 # out to be difficult to make that work automatically here
Ned Deily1731d6d2020-05-18 04:32:38 -04001992 # without bringing into play more tools and magic. That case
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301993 # can be handled using a recipe with the right arguments
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001994 # to detect_tkinter_fromenv().
Ned Deily1731d6d2020-05-18 04:32:38 -04001995 #
1996 # Note also that the fallback case here is to try to use the
1997 # Apple-supplied Tcl and Tk frameworks in /System/Library but
1998 # be forewarned that they are deprecated by Apple and typically
1999 # out-of-date and buggy; their use should be avoided if at
2000 # all possible by installing a newer version of Tcl and Tk in
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302001 # /Library/Frameworks before building Python without
Ned Deily1731d6d2020-05-18 04:32:38 -04002002 # an explicit SDK or by configuring build arguments explicitly.
2003
Jack Jansen0b06be72002-06-21 14:48:38 +00002004 from os.path import join, exists
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002005
Ned Deily1731d6d2020-05-18 04:32:38 -04002006 sysroot = macosx_sdk_root() # path to the SDK or '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00002007
Ned Deily1731d6d2020-05-18 04:32:38 -04002008 if macosx_sdk_specified():
2009 # Use case #2: an SDK other than '/' was specified.
2010 # Only search there.
2011 framework_dirs = [
2012 join(sysroot, 'Library', 'Frameworks'),
2013 join(sysroot, 'System', 'Library', 'Frameworks'),
2014 ]
2015 else:
2016 # Use case #1: no explicit SDK selected.
2017 # Search the local system-wide /Library/Frameworks,
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302018 # not the one in the default SDK, otherwise fall back to
Ned Deily1731d6d2020-05-18 04:32:38 -04002019 # /System/Library/Frameworks whose header files may be in
2020 # the default SDK or, on older systems, actually installed.
2021 framework_dirs = [
2022 join('/', 'Library', 'Frameworks'),
2023 join(sysroot, 'System', 'Library', 'Frameworks'),
2024 ]
2025
2026 # Find the directory that contains the Tcl.framework and
2027 # Tk.framework bundles.
Jack Jansen0b06be72002-06-21 14:48:38 +00002028 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00002029 # both Tcl.framework and Tk.framework should be present
Jack Jansen0b06be72002-06-21 14:48:38 +00002030 for fw in 'Tcl', 'Tk':
Ned Deily1731d6d2020-05-18 04:32:38 -04002031 if not exists(join(F, fw + '.framework')):
2032 break
Jack Jansen0b06be72002-06-21 14:48:38 +00002033 else:
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302034 # ok, F is now directory with both frameworks. Continue
Jack Jansen0b06be72002-06-21 14:48:38 +00002035 # building
2036 break
2037 else:
2038 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
2039 # will now resume.
Victor Stinner4cbea512019-02-28 17:48:38 +01002040 return False
Tim Peters2c60f7a2003-01-29 03:49:43 +00002041
Jack Jansen0b06be72002-06-21 14:48:38 +00002042 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00002043 join(F, fw + '.framework', H)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002044 for fw in ('Tcl', 'Tk')
Ned Deily1731d6d2020-05-18 04:32:38 -04002045 for H in ('Headers',)
Jack Jansen0b06be72002-06-21 14:48:38 +00002046 ]
2047
Ned Deily1731d6d2020-05-18 04:32:38 -04002048 # Add the base framework directory as well
2049 compile_args = ['-F', F]
Jack Jansen0b06be72002-06-21 14:48:38 +00002050
Ned Deily1731d6d2020-05-18 04:32:38 -04002051 # Do not build tkinter for archs that this Tk was not built with.
Georg Brandlfcaf9102008-07-16 02:17:56 +00002052 cflags = sysconfig.get_config_vars('CFLAGS')[0]
R David Murray44b548d2016-09-08 13:59:53 -04002053 archs = re.findall(r'-arch\s+(\w+)', cflags)
Georg Brandlfcaf9102008-07-16 02:17:56 +00002054
Ronald Oussorend097efe2009-09-15 19:07:58 +00002055 tmpfile = os.path.join(self.build_temp, 'tk.arch')
2056 if not os.path.exists(self.build_temp):
2057 os.makedirs(self.build_temp)
2058
Ned Deily1731d6d2020-05-18 04:32:38 -04002059 run_command(
2060 "file {}/Tk.framework/Tk | grep 'for architecture' > {}".format(F, tmpfile)
2061 )
Brett Cannon9f5db072010-10-29 20:19:27 +00002062 with open(tmpfile) as fp:
2063 detected_archs = []
2064 for ln in fp:
2065 a = ln.split()[-1]
2066 if a in archs:
2067 detected_archs.append(ln.split()[-1])
Ronald Oussorend097efe2009-09-15 19:07:58 +00002068 os.unlink(tmpfile)
2069
Ned Deily1731d6d2020-05-18 04:32:38 -04002070 arch_args = []
Ronald Oussorend097efe2009-09-15 19:07:58 +00002071 for a in detected_archs:
Ned Deily1731d6d2020-05-18 04:32:38 -04002072 arch_args.append('-arch')
2073 arch_args.append(a)
2074
2075 compile_args += arch_args
2076 link_args = [','.join(['-Wl', '-F', F, '-framework', 'Tcl', '-framework', 'Tk']), *arch_args]
2077
2078 # The X11/xlib.h file bundled in the Tk sources can cause function
2079 # prototype warnings from the compiler. Since we cannot easily fix
2080 # that, suppress the warnings here instead.
2081 if '-Wstrict-prototypes' in cflags.split():
2082 compile_args.append('-Wno-strict-prototypes')
Georg Brandlfcaf9102008-07-16 02:17:56 +00002083
Victor Stinnercfe172d2019-03-01 18:21:49 +01002084 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2085 define_macros=[('WITH_APPINIT', 1)],
2086 include_dirs=include_dirs,
2087 libraries=[],
Ned Deily1731d6d2020-05-18 04:32:38 -04002088 extra_compile_args=compile_args,
2089 extra_link_args=link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01002090 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002091
Victor Stinner625dbf22019-03-01 15:59:39 +01002092 def detect_tkinter(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002093 # The _tkinter module.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002094 #
2095 # Detection of Tcl/Tk is attempted in the following order:
2096 # - Through environment variables.
2097 # - Platform specific detection of Tcl/Tk (currently only macOS).
2098 # - Search of various standard Unix header/library paths.
2099 #
2100 # Detection stops at the first successful method.
Michael W. Hudson5b109102002-01-23 15:04:41 +00002101
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002102 # Check for Tcl and Tk at the locations indicated by _TCLTK_INCLUDES
2103 # and _TCLTK_LIBS environment variables.
2104 if self.detect_tkinter_fromenv():
Victor Stinner5ec33a12019-03-01 16:43:28 +01002105 return True
Ned Deilyd819b932013-09-06 01:07:05 -07002106
Jack Jansen0b06be72002-06-21 14:48:38 +00002107 # Rather than complicate the code below, detecting and building
2108 # AquaTk is a separate method. Only one Tkinter will be built on
2109 # Darwin - either AquaTk, if it is found, or X11 based Tk.
Victor Stinner5ec33a12019-03-01 16:43:28 +01002110 if (MACOS and self.detect_tkinter_darwin()):
2111 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002112
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002113 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002114 # The versions with dots are used on Unix, and the versions without
2115 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002116 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polo5d377bd2009-08-16 14:44:14 +00002117 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
2118 '8.2', '82', '8.1', '81', '8.0', '80']:
Victor Stinner625dbf22019-03-01 15:59:39 +01002119 tklib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002120 'tk' + version)
Victor Stinner625dbf22019-03-01 15:59:39 +01002121 tcllib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002122 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00002123 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002124 # Exit the loop when we've found the Tcl/Tk libraries
2125 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002126
Fredrik Lundhade711a2001-01-24 08:00:28 +00002127 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002128 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002129 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002130 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002131 dotversion = version
Victor Stinner4cbea512019-02-28 17:48:38 +01002132 if '.' not in dotversion and "bsd" in HOST_PLATFORM.lower():
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002133 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
2134 # but the include subdirs are named like .../include/tcl8.3.
2135 dotversion = dotversion[:-1] + '.' + dotversion[-1]
2136 tcl_include_sub = []
2137 tk_include_sub = []
Victor Stinner625dbf22019-03-01 15:59:39 +01002138 for dir in self.inc_dirs:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002139 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
2140 tk_include_sub += [dir + os.sep + "tk" + dotversion]
2141 tk_include_sub += tcl_include_sub
Victor Stinner625dbf22019-03-01 15:59:39 +01002142 tcl_includes = find_file('tcl.h', self.inc_dirs, tcl_include_sub)
2143 tk_includes = find_file('tk.h', self.inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002144
Martin v. Löwise86a59a2003-05-03 08:45:51 +00002145 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002146 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002147 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Victor Stinner5ec33a12019-03-01 16:43:28 +01002148 return False
Fredrik Lundhade711a2001-01-24 08:00:28 +00002149
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002150 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002151
Victor Stinnercfe172d2019-03-01 18:21:49 +01002152 include_dirs = []
2153 libs = []
2154 defs = []
2155 added_lib_dirs = []
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002156 for dir in tcl_includes + tk_includes:
2157 if dir not in include_dirs:
2158 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002159
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002160 # Check for various platform-specific directories
Victor Stinner4cbea512019-02-28 17:48:38 +01002161 if HOST_PLATFORM == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002162 include_dirs.append('/usr/openwin/include')
2163 added_lib_dirs.append('/usr/openwin/lib')
2164 elif os.path.exists('/usr/X11R6/include'):
2165 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00002166 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002167 added_lib_dirs.append('/usr/X11R6/lib')
2168 elif os.path.exists('/usr/X11R5/include'):
2169 include_dirs.append('/usr/X11R5/include')
2170 added_lib_dirs.append('/usr/X11R5/lib')
2171 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002172 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002173 include_dirs.append('/usr/X11/include')
2174 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002175
Jason Tishler9181c942003-02-05 15:16:17 +00002176 # If Cygwin, then verify that X is installed before proceeding
Victor Stinner4cbea512019-02-28 17:48:38 +01002177 if CYGWIN:
Jason Tishler9181c942003-02-05 15:16:17 +00002178 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
2179 if x11_inc is None:
Victor Stinner5ec33a12019-03-01 16:43:28 +01002180 return False
Jason Tishler9181c942003-02-05 15:16:17 +00002181
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002182 # Check for BLT extension
Victor Stinner625dbf22019-03-01 15:59:39 +01002183 if self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002184 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002185 defs.append( ('WITH_BLT', 1) )
2186 libs.append('BLT8.0')
Victor Stinner625dbf22019-03-01 15:59:39 +01002187 elif self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002188 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00002189 defs.append( ('WITH_BLT', 1) )
2190 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002191
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002192 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00002193 libs.append('tk'+ version)
2194 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002195
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002196 # Finally, link with the X11 libraries (not appropriate on cygwin)
Victor Stinner4cbea512019-02-28 17:48:38 +01002197 if not CYGWIN:
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002198 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002199
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002200 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002201 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002202 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002203 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002204 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002205 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002206 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002207
Victor Stinnercfe172d2019-03-01 18:21:49 +01002208 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2209 define_macros=[('WITH_APPINIT', 1)] + defs,
2210 include_dirs=include_dirs,
2211 libraries=libs,
2212 library_dirs=added_lib_dirs))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002213 return True
2214
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002215 def configure_ctypes(self, ext):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002216 return True
2217
Victor Stinner625dbf22019-03-01 15:59:39 +01002218 def detect_ctypes(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01002219 # Thomas Heller's _ctypes module
Ronald Oussoren41761932020-11-08 10:05:27 +01002220
2221 if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
2222 self.use_system_libffi = True
2223 else:
2224 self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
2225
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002226 include_dirs = []
Victor Stinner1ae035b2020-04-17 17:47:20 +02002227 extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002228 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00002229 sources = ['_ctypes/_ctypes.c',
2230 '_ctypes/callbacks.c',
2231 '_ctypes/callproc.c',
2232 '_ctypes/stgdict.c',
Thomas Heller864cc672010-08-08 17:58:53 +00002233 '_ctypes/cfield.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00002234 depends = ['_ctypes/ctypes.h']
2235
Victor Stinner4cbea512019-02-28 17:48:38 +01002236 if MACOS:
Ronald Oussoren2decf222010-09-05 18:25:59 +00002237 sources.append('_ctypes/malloc_closure.c')
Ronald Oussoren41761932020-11-08 10:05:27 +01002238 extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
Christian Heimes78644762008-03-04 23:39:23 +00002239 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00002240 include_dirs.append('_ctypes/darwin')
Thomas Hellercf567c12006-03-08 19:51:58 +00002241
Victor Stinner4cbea512019-02-28 17:48:38 +01002242 elif HOST_PLATFORM == 'sunos5':
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002243 # XXX This shouldn't be necessary; it appears that some
2244 # of the assembler code is non-PIC (i.e. it has relocations
2245 # when it shouldn't. The proper fix would be to rewrite
2246 # the assembler code to be PIC.
2247 # This only works with GCC; the Sun compiler likely refuses
2248 # this option. If you want to compile ctypes with the Sun
2249 # compiler, please research a proper solution, instead of
2250 # finding some -z option for the Sun compiler.
2251 extra_link_args.append('-mimpure-text')
2252
Victor Stinner4cbea512019-02-28 17:48:38 +01002253 elif HOST_PLATFORM.startswith('hp-ux'):
Thomas Heller3eaaeb42008-05-23 17:26:46 +00002254 extra_link_args.append('-fPIC')
2255
Thomas Hellercf567c12006-03-08 19:51:58 +00002256 ext = Extension('_ctypes',
2257 include_dirs=include_dirs,
2258 extra_compile_args=extra_compile_args,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002259 extra_link_args=extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002260 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00002261 sources=sources,
2262 depends=depends)
Victor Stinnercfe172d2019-03-01 18:21:49 +01002263 self.add(ext)
2264 if TEST_EXTENSIONS:
2265 # function my_sqrt() needs libm for sqrt()
2266 self.add(Extension('_ctypes_test',
2267 sources=['_ctypes/_ctypes_test.c'],
2268 libraries=['m']))
Thomas Hellercf567c12006-03-08 19:51:58 +00002269
Ronald Oussoren41761932020-11-08 10:05:27 +01002270 ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
2271 ffi_lib = None
2272
Victor Stinner625dbf22019-03-01 15:59:39 +01002273 ffi_inc_dirs = self.inc_dirs.copy()
Victor Stinner4cbea512019-02-28 17:48:38 +01002274 if MACOS:
Ronald Oussoren41761932020-11-08 10:05:27 +01002275 ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
Christian Heimes78644762008-03-04 23:39:23 +00002276
Ronald Oussoren41761932020-11-08 10:05:27 +01002277 if not ffi_inc:
2278 if os.path.exists(ffi_in_sdk):
2279 ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
2280 ffi_inc = ffi_in_sdk
2281 ffi_lib = 'ffi'
2282 else:
2283 # OS X 10.5 comes with libffi.dylib; the include files are
2284 # in /usr/include/ffi
2285 ffi_inc_dirs.append('/usr/include/ffi')
2286
2287 if not ffi_inc:
2288 found = find_file('ffi.h', [], ffi_inc_dirs)
2289 if found:
2290 ffi_inc = found[0]
2291 if ffi_inc:
2292 ffi_h = ffi_inc + '/ffi.h'
Shlomi Fish6d51b872017-09-06 23:19:19 +03002293 if not os.path.exists(ffi_h):
2294 ffi_inc = None
2295 print('Header file {} does not exist'.format(ffi_h))
Ronald Oussoren41761932020-11-08 10:05:27 +01002296 if ffi_lib is None and ffi_inc:
doko@ubuntu.comae683652016-06-05 01:38:29 +02002297 for lib_name in ('ffi', 'ffi_pic'):
Victor Stinner625dbf22019-03-01 15:59:39 +01002298 if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002299 ffi_lib = lib_name
2300 break
2301
2302 if ffi_inc and ffi_lib:
Ronald Oussoren41761932020-11-08 10:05:27 +01002303 ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
2304 if grep_headers_for('ffi_prep_cif_var', ffi_headers):
2305 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
2306 if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
2307 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
2308 if grep_headers_for('ffi_closure_alloc', ffi_headers):
2309 ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
2310
2311 ext.include_dirs.append(ffi_inc)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002312 ext.libraries.append(ffi_lib)
2313 self.use_system_libffi = True
2314
Christian Heimes5bb96922018-02-25 10:22:14 +01002315 if sysconfig.get_config_var('HAVE_LIBDL'):
2316 # for dlopen, see bpo-32647
2317 ext.libraries.append('dl')
2318
Victor Stinner5ec33a12019-03-01 16:43:28 +01002319 def detect_decimal(self):
2320 # Stefan Krah's _decimal module
Stefan Krah60187b52012-03-23 19:06:27 +01002321 extra_compile_args = []
Stefan Kraha10e2fb2012-09-01 14:21:22 +02002322 undef_macros = []
Stefan Krah60187b52012-03-23 19:06:27 +01002323 if '--with-system-libmpdec' in sysconfig.get_config_var("CONFIG_ARGS"):
2324 include_dirs = []
Antoine Pitrou73b20ae2021-03-30 18:11:06 +02002325 libraries = ['mpdec']
Stefan Krah60187b52012-03-23 19:06:27 +01002326 sources = ['_decimal/_decimal.c']
2327 depends = ['_decimal/docstrings.h']
2328 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01002329 include_dirs = [os.path.abspath(os.path.join(self.srcdir,
Ned Deily458a6fb2012-04-01 02:30:46 -07002330 'Modules',
2331 '_decimal',
2332 'libmpdec'))]
Stefan Krahbd4ed772017-12-06 18:24:17 +01002333 libraries = ['m']
Stefan Krah60187b52012-03-23 19:06:27 +01002334 sources = [
2335 '_decimal/_decimal.c',
2336 '_decimal/libmpdec/basearith.c',
2337 '_decimal/libmpdec/constants.c',
2338 '_decimal/libmpdec/context.c',
2339 '_decimal/libmpdec/convolute.c',
2340 '_decimal/libmpdec/crt.c',
2341 '_decimal/libmpdec/difradix2.c',
2342 '_decimal/libmpdec/fnt.c',
2343 '_decimal/libmpdec/fourstep.c',
2344 '_decimal/libmpdec/io.c',
Stefan Krahf117d872019-07-10 18:27:38 +02002345 '_decimal/libmpdec/mpalloc.c',
Stefan Krah60187b52012-03-23 19:06:27 +01002346 '_decimal/libmpdec/mpdecimal.c',
2347 '_decimal/libmpdec/numbertheory.c',
2348 '_decimal/libmpdec/sixstep.c',
2349 '_decimal/libmpdec/transpose.c',
2350 ]
2351 depends = [
2352 '_decimal/docstrings.h',
2353 '_decimal/libmpdec/basearith.h',
2354 '_decimal/libmpdec/bits.h',
2355 '_decimal/libmpdec/constants.h',
2356 '_decimal/libmpdec/convolute.h',
2357 '_decimal/libmpdec/crt.h',
2358 '_decimal/libmpdec/difradix2.h',
2359 '_decimal/libmpdec/fnt.h',
2360 '_decimal/libmpdec/fourstep.h',
2361 '_decimal/libmpdec/io.h',
Stefan Krah8d013a82016-04-26 16:34:41 +02002362 '_decimal/libmpdec/mpalloc.h',
Stefan Krah60187b52012-03-23 19:06:27 +01002363 '_decimal/libmpdec/mpdecimal.h',
2364 '_decimal/libmpdec/numbertheory.h',
2365 '_decimal/libmpdec/sixstep.h',
2366 '_decimal/libmpdec/transpose.h',
2367 '_decimal/libmpdec/typearith.h',
2368 '_decimal/libmpdec/umodarith.h',
2369 ]
2370
Stefan Krah1919b7e2012-03-21 18:25:23 +01002371 config = {
2372 'x64': [('CONFIG_64','1'), ('ASM','1')],
2373 'uint128': [('CONFIG_64','1'), ('ANSI','1'), ('HAVE_UINT128_T','1')],
2374 'ansi64': [('CONFIG_64','1'), ('ANSI','1')],
2375 'ppro': [('CONFIG_32','1'), ('PPRO','1'), ('ASM','1')],
2376 'ansi32': [('CONFIG_32','1'), ('ANSI','1')],
2377 'ansi-legacy': [('CONFIG_32','1'), ('ANSI','1'),
2378 ('LEGACY_COMPILER','1')],
2379 'universal': [('UNIVERSAL','1')]
2380 }
2381
Stefan Krah1919b7e2012-03-21 18:25:23 +01002382 cc = sysconfig.get_config_var('CC')
2383 sizeof_size_t = sysconfig.get_config_var('SIZEOF_SIZE_T')
2384 machine = os.environ.get('PYTHON_DECIMAL_WITH_MACHINE')
2385
2386 if machine:
2387 # Override automatic configuration to facilitate testing.
2388 define_macros = config[machine]
Victor Stinner4cbea512019-02-28 17:48:38 +01002389 elif MACOS:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002390 # Universal here means: build with the same options Python
2391 # was built with.
2392 define_macros = config['universal']
2393 elif sizeof_size_t == 8:
2394 if sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X64'):
2395 define_macros = config['x64']
2396 elif sysconfig.get_config_var('HAVE_GCC_UINT128_T'):
2397 define_macros = config['uint128']
2398 else:
2399 define_macros = config['ansi64']
2400 elif sizeof_size_t == 4:
2401 ppro = sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X87')
2402 if ppro and ('gcc' in cc or 'clang' in cc) and \
Victor Stinner4cbea512019-02-28 17:48:38 +01002403 not 'sunos' in HOST_PLATFORM:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002404 # solaris: problems with register allocation.
2405 # icc >= 11.0 works as well.
2406 define_macros = config['ppro']
Stefan Krahce23dbc2012-09-30 21:12:53 +02002407 extra_compile_args.append('-Wno-unknown-pragmas')
Stefan Krah1919b7e2012-03-21 18:25:23 +01002408 else:
2409 define_macros = config['ansi32']
2410 else:
2411 raise DistutilsError("_decimal: unsupported architecture")
2412
2413 # Workarounds for toolchain bugs:
2414 if sysconfig.get_config_var('HAVE_IPA_PURE_CONST_BUG'):
2415 # Some versions of gcc miscompile inline asm:
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002416 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46491
2417 # https://gcc.gnu.org/ml/gcc/2010-11/msg00366.html
Stefan Krah1919b7e2012-03-21 18:25:23 +01002418 extra_compile_args.append('-fno-ipa-pure-const')
2419 if sysconfig.get_config_var('HAVE_GLIBC_MEMMOVE_BUG'):
2420 # _FORTIFY_SOURCE wrappers for memmove and bcopy are incorrect:
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002421 # https://sourceware.org/ml/libc-alpha/2010-12/msg00009.html
Stefan Krah1919b7e2012-03-21 18:25:23 +01002422 undef_macros.append('_FORTIFY_SOURCE')
2423
Stefan Krah1919b7e2012-03-21 18:25:23 +01002424 # Uncomment for extra functionality:
2425 #define_macros.append(('EXTRA_FUNCTIONALITY', 1))
Victor Stinner8058bda2019-03-01 15:31:45 +01002426 self.add(Extension('_decimal',
2427 include_dirs=include_dirs,
2428 libraries=libraries,
2429 define_macros=define_macros,
2430 undef_macros=undef_macros,
2431 extra_compile_args=extra_compile_args,
2432 sources=sources,
2433 depends=depends))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002434
Victor Stinner5ec33a12019-03-01 16:43:28 +01002435 def detect_openssl_hashlib(self):
2436 # Detect SSL support for the socket module (via _ssl)
Christian Heimesff5be6e2018-01-20 13:19:21 +01002437 config_vars = sysconfig.get_config_vars()
2438
2439 def split_var(name, sep):
2440 # poor man's shlex, the re module is not available yet.
2441 value = config_vars.get(name)
2442 if not value:
2443 return ()
2444 # This trick works because ax_check_openssl uses --libs-only-L,
2445 # --libs-only-l, and --cflags-only-I.
2446 value = ' ' + value
2447 sep = ' ' + sep
2448 return [v.strip() for v in value.split(sep) if v.strip()]
2449
2450 openssl_includes = split_var('OPENSSL_INCLUDES', '-I')
2451 openssl_libdirs = split_var('OPENSSL_LDFLAGS', '-L')
2452 openssl_libs = split_var('OPENSSL_LIBS', '-l')
Christian Heimes32eba612021-03-19 10:29:25 +01002453 openssl_rpath = config_vars.get('OPENSSL_RPATH')
Christian Heimesff5be6e2018-01-20 13:19:21 +01002454 if not openssl_libs:
2455 # libssl and libcrypto not found
Christian Heimes8abc3f42019-04-09 18:40:12 +02002456 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002457 return None, None
2458
2459 # Find OpenSSL includes
2460 ssl_incs = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002461 'openssl/ssl.h', self.inc_dirs, openssl_includes
Christian Heimesff5be6e2018-01-20 13:19:21 +01002462 )
2463 if ssl_incs is None:
Christian Heimes8abc3f42019-04-09 18:40:12 +02002464 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002465 return None, None
2466
Christian Heimes32eba612021-03-19 10:29:25 +01002467 if openssl_rpath == 'auto':
2468 runtime_library_dirs = openssl_libdirs[:]
2469 elif not openssl_rpath:
2470 runtime_library_dirs = []
2471 else:
2472 runtime_library_dirs = [openssl_rpath]
2473
Christian Heimesbacefbf2021-03-27 18:03:54 +01002474 openssl_extension_kwargs = dict(
2475 include_dirs=openssl_includes,
2476 library_dirs=openssl_libdirs,
2477 libraries=openssl_libs,
2478 runtime_library_dirs=runtime_library_dirs,
2479 )
2480
2481 # This static linking is NOT OFFICIALLY SUPPORTED.
2482 # Requires static OpenSSL build with position-independent code. Some
2483 # features like DSO engines or external OSSL providers don't work.
2484 # Only tested on GCC and clang on X86_64.
2485 if os.environ.get("PY_UNSUPPORTED_OPENSSL_BUILD") == "static":
2486 extra_linker_args = []
2487 for lib in openssl_extension_kwargs["libraries"]:
2488 # link statically
2489 extra_linker_args.append(f"-l:lib{lib}.a")
2490 # don't export symbols
2491 extra_linker_args.append(f"-Wl,--exclude-libs,lib{lib}.a")
2492 openssl_extension_kwargs["extra_link_args"] = extra_linker_args
2493 # don't link OpenSSL shared libraries.
Christian Heimes5f879152021-04-26 15:13:34 +02002494 # include libz for OpenSSL build flavors with compression support
2495 openssl_extension_kwargs["libraries"] = ["z"]
Christian Heimesbacefbf2021-03-27 18:03:54 +01002496
Christian Heimes39258d32021-04-17 11:36:35 +02002497 self.add(
2498 Extension(
2499 '_ssl',
2500 ['_ssl.c'],
Christian Heimes666991f2021-04-26 15:01:40 +02002501 depends=[
2502 'socketmodule.h',
2503 '_ssl.h',
2504 '_ssl/debughelpers.c',
2505 '_ssl/misc.c',
2506 '_ssl/cert.c',
2507 ],
Christian Heimes39258d32021-04-17 11:36:35 +02002508 **openssl_extension_kwargs
Christian Heimesc7f70692019-05-31 11:44:05 +02002509 )
Christian Heimes39258d32021-04-17 11:36:35 +02002510 )
Christian Heimesbacefbf2021-03-27 18:03:54 +01002511 self.add(
2512 Extension(
2513 '_hashlib',
2514 ['_hashopenssl.c'],
2515 depends=['hashlib.h'],
2516 **openssl_extension_kwargs,
2517 )
2518 )
Christian Heimesff5be6e2018-01-20 13:19:21 +01002519
xdegaye2ee077f2019-04-09 17:20:08 +02002520 def detect_hash_builtins(self):
Christian Heimes9b60e552020-05-15 23:54:53 +02002521 # By default we always compile these even when OpenSSL is available
2522 # (issue #14693). It's harmless and the object code is tiny
2523 # (40-50 KiB per module, only loaded when actually used). Modules can
2524 # be disabled via the --with-builtin-hashlib-hashes configure flag.
2525 supported = {"md5", "sha1", "sha256", "sha512", "sha3", "blake2"}
Victor Stinner5ec33a12019-03-01 16:43:28 +01002526
Christian Heimes9b60e552020-05-15 23:54:53 +02002527 configured = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
2528 configured = configured.strip('"').lower()
2529 configured = {
2530 m.strip() for m in configured.split(",")
2531 }
Victor Stinner5ec33a12019-03-01 16:43:28 +01002532
Christian Heimes9b60e552020-05-15 23:54:53 +02002533 self.disabled_configure.extend(
2534 sorted(supported.difference(configured))
2535 )
Victor Stinner5ec33a12019-03-01 16:43:28 +01002536
Christian Heimes9b60e552020-05-15 23:54:53 +02002537 if "sha256" in configured:
2538 self.add(Extension(
2539 '_sha256', ['sha256module.c'],
2540 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2541 depends=['hashlib.h']
2542 ))
2543
2544 if "sha512" in configured:
2545 self.add(Extension(
2546 '_sha512', ['sha512module.c'],
2547 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2548 depends=['hashlib.h']
2549 ))
2550
2551 if "md5" in configured:
2552 self.add(Extension(
2553 '_md5', ['md5module.c'],
2554 depends=['hashlib.h']
2555 ))
2556
2557 if "sha1" in configured:
2558 self.add(Extension(
2559 '_sha1', ['sha1module.c'],
2560 depends=['hashlib.h']
2561 ))
2562
2563 if "blake2" in configured:
2564 blake2_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002565 os.path.join(escape(self.srcdir), 'Modules/_blake2/impl/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002566 )
2567 blake2_deps.append('hashlib.h')
2568 self.add(Extension(
2569 '_blake2',
2570 [
2571 '_blake2/blake2module.c',
2572 '_blake2/blake2b_impl.c',
2573 '_blake2/blake2s_impl.c'
2574 ],
2575 depends=blake2_deps
2576 ))
2577
2578 if "sha3" in configured:
2579 sha3_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002580 os.path.join(escape(self.srcdir), 'Modules/_sha3/kcp/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002581 )
2582 sha3_deps.append('hashlib.h')
2583 self.add(Extension(
2584 '_sha3',
2585 ['_sha3/sha3module.c'],
2586 depends=sha3_deps
2587 ))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002588
2589 def detect_nis(self):
Victor Stinner4cbea512019-02-28 17:48:38 +01002590 if MS_WINDOWS or CYGWIN or HOST_PLATFORM == 'qnx6':
Victor Stinner8058bda2019-03-01 15:31:45 +01002591 self.missing.append('nis')
2592 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002593
2594 libs = []
2595 library_dirs = []
2596 includes_dirs = []
2597
2598 # bpo-32521: glibc has deprecated Sun RPC for some time. Fedora 28
2599 # moved headers and libraries to libtirpc and libnsl. The headers
2600 # are in tircp and nsl sub directories.
2601 rpcsvc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002602 'rpcsvc/yp_prot.h', self.inc_dirs,
2603 [os.path.join(inc_dir, 'nsl') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002604 )
2605 rpc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002606 'rpc/rpc.h', self.inc_dirs,
2607 [os.path.join(inc_dir, 'tirpc') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002608 )
2609 if rpcsvc_inc is None or rpc_inc is None:
2610 # not found
Victor Stinner8058bda2019-03-01 15:31:45 +01002611 self.missing.append('nis')
2612 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002613 includes_dirs.extend(rpcsvc_inc)
2614 includes_dirs.extend(rpc_inc)
2615
Victor Stinner625dbf22019-03-01 15:59:39 +01002616 if self.compiler.find_library_file(self.lib_dirs, 'nsl'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002617 libs.append('nsl')
2618 else:
2619 # libnsl-devel: check for libnsl in nsl/ subdirectory
Victor Stinner625dbf22019-03-01 15:59:39 +01002620 nsl_dirs = [os.path.join(lib_dir, 'nsl') for lib_dir in self.lib_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002621 libnsl = self.compiler.find_library_file(nsl_dirs, 'nsl')
2622 if libnsl is not None:
2623 library_dirs.append(os.path.dirname(libnsl))
2624 libs.append('nsl')
2625
Victor Stinner625dbf22019-03-01 15:59:39 +01002626 if self.compiler.find_library_file(self.lib_dirs, 'tirpc'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002627 libs.append('tirpc')
2628
Victor Stinner8058bda2019-03-01 15:31:45 +01002629 self.add(Extension('nis', ['nismodule.c'],
2630 libraries=libs,
2631 library_dirs=library_dirs,
2632 include_dirs=includes_dirs))
Christian Heimes29a7df72018-01-26 23:28:46 +01002633
Christian Heimesff5be6e2018-01-20 13:19:21 +01002634
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00002635class PyBuildInstall(install):
2636 # Suppress the warning about installation into the lib_dynload
2637 # directory, which is not in sys.path when running Python during
2638 # installation:
2639 def initialize_options (self):
2640 install.initialize_options(self)
2641 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00002642
Éric Araujoe6792c12011-06-09 14:07:02 +02002643 # Customize subcommands to not install an egg-info file for Python
2644 sub_commands = [('install_lib', install.has_lib),
2645 ('install_headers', install.has_headers),
2646 ('install_scripts', install.has_scripts),
2647 ('install_data', install.has_data)]
2648
2649
Michael W. Hudson529a5052002-12-17 16:47:17 +00002650class PyBuildInstallLib(install_lib):
2651 # Do exactly what install_lib does but make sure correct access modes get
2652 # set on installed directories and files. All installed files with get
2653 # mode 644 unless they are a shared library in which case they will get
2654 # mode 755. All installed directories will get mode 755.
2655
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002656 # this is works for EXT_SUFFIX too, which ends with SHLIB_SUFFIX
2657 shlib_suffix = sysconfig.get_config_var("SHLIB_SUFFIX")
Michael W. Hudson529a5052002-12-17 16:47:17 +00002658
2659 def install(self):
2660 outfiles = install_lib.install(self)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002661 self.set_file_modes(outfiles, 0o644, 0o755)
2662 self.set_dir_modes(self.install_dir, 0o755)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002663 return outfiles
2664
2665 def set_file_modes(self, files, defaultMode, sharedLibMode):
Michael W. Hudson529a5052002-12-17 16:47:17 +00002666 if not files: return
2667
2668 for filename in files:
2669 if os.path.islink(filename): continue
2670 mode = defaultMode
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002671 if filename.endswith(self.shlib_suffix): mode = sharedLibMode
Michael W. Hudson529a5052002-12-17 16:47:17 +00002672 log.info("changing mode of %s to %o", filename, mode)
2673 if not self.dry_run: os.chmod(filename, mode)
2674
2675 def set_dir_modes(self, dirname, mode):
Amaury Forgeot d'Arc321e5332009-07-02 23:08:45 +00002676 for dirpath, dirnames, fnames in os.walk(dirname):
2677 if os.path.islink(dirpath):
2678 continue
2679 log.info("changing mode of %s to %o", dirpath, mode)
2680 if not self.dry_run: os.chmod(dirpath, mode)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002681
Victor Stinnerc991f242019-03-01 17:19:04 +01002682
Georg Brandlff52f762010-12-28 09:51:43 +00002683class PyBuildScripts(build_scripts):
2684 def copy_scripts(self):
2685 outfiles, updated_files = build_scripts.copy_scripts(self)
2686 fullversion = '-{0[0]}.{0[1]}'.format(sys.version_info)
2687 minoronly = '.{0[1]}'.format(sys.version_info)
2688 newoutfiles = []
2689 newupdated_files = []
2690 for filename in outfiles:
Brett Cannona8c34242018-04-20 14:15:40 -07002691 if filename.endswith('2to3'):
Georg Brandlff52f762010-12-28 09:51:43 +00002692 newfilename = filename + fullversion
2693 else:
2694 newfilename = filename + minoronly
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -07002695 log.info(f'renaming {filename} to {newfilename}')
Georg Brandlff52f762010-12-28 09:51:43 +00002696 os.rename(filename, newfilename)
2697 newoutfiles.append(newfilename)
2698 if filename in updated_files:
2699 newupdated_files.append(newfilename)
2700 return newoutfiles, newupdated_files
2701
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002702
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002703def main():
Victor Stinnercad80202021-01-19 23:04:49 +01002704 global LIST_MODULE_NAMES
2705
2706 if "--list-module-names" in sys.argv:
2707 LIST_MODULE_NAMES = True
2708 sys.argv.remove("--list-module-names")
2709
Victor Stinnerc991f242019-03-01 17:19:04 +01002710 set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
2711 set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
2712
2713 class DummyProcess:
2714 """Hack for parallel build"""
2715 ProcessPoolExecutor = None
2716
2717 sys.modules['concurrent.futures.process'] = DummyProcess
Paul Ganssle62972d92020-05-16 04:20:06 -04002718 validate_tzpath()
Victor Stinnerc991f242019-03-01 17:19:04 +01002719
Andrew M. Kuchling62686692001-05-21 20:48:09 +00002720 # turn off warnings when deprecated modules are imported
2721 import warnings
2722 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002723 setup(# PyPI Metadata (PEP 301)
2724 name = "Python",
2725 version = sys.version.split()[0],
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002726 url = "https://www.python.org/%d.%d" % sys.version_info[:2],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002727 maintainer = "Guido van Rossum and the Python community",
2728 maintainer_email = "python-dev@python.org",
2729 description = "A high-level object-oriented programming language",
2730 long_description = SUMMARY.strip(),
2731 license = "PSF license",
Guido van Rossumc1f779c2007-07-03 08:25:58 +00002732 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002733 platforms = ["Many"],
2734
2735 # Build info
Georg Brandlff52f762010-12-28 09:51:43 +00002736 cmdclass = {'build_ext': PyBuildExt,
2737 'build_scripts': PyBuildScripts,
2738 'install': PyBuildInstall,
2739 'install_lib': PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002740 # The struct module is defined here, because build_ext won't be
2741 # called unless there's at least one extension module defined.
Victor Stinnercdad2722021-04-22 00:52:52 +02002742 ext_modules=[Extension('_struct', ['_struct.c'],
2743 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00002744
Georg Brandlff52f762010-12-28 09:51:43 +00002745 # If you change the scripts installed here, you also need to
2746 # check the PyBuildScripts command above, and change the links
2747 # created by the bininstall target in Makefile.pre.in
Benjamin Petersondfea1922009-05-23 17:13:14 +00002748 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
Brett Cannona8c34242018-04-20 14:15:40 -07002749 "Tools/scripts/2to3"]
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002750 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00002751
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002752# --install-platlib
2753if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002754 main()