blob: e74a275edbf2d05d39cb32577c690897066fd380 [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)
Miss Islington (bot)171fdf22022-01-26 15:49:53 -0800760 # bpo-38472: With a German locale, GCC returns "gcc-Version 9.1.0
761 # (GCC)", whereas it returns "gcc version 9.1.0" with the C locale.
762 ret = run_command('LC_ALL=C %s -E -v - </dev/null 2>%s 1>/dev/null' % (CC, tmpfile))
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200763 is_gcc = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800764 is_clang = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200765 in_incdirs = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200766 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200767 if ret == 0:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200768 with open(tmpfile) as fp:
769 for line in fp.readlines():
770 if line.startswith("gcc version"):
771 is_gcc = True
pxinwr32f5fdd2019-02-27 19:09:28 +0800772 elif line.startswith("clang version"):
773 is_clang = True
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200774 elif line.startswith("#include <...>"):
775 in_incdirs = True
776 elif line.startswith("End of search list"):
777 in_incdirs = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800778 elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"):
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200779 for d in line.strip().split("=")[1].split(":"):
780 d = os.path.normpath(d)
781 if '/gcc/' not in d:
782 add_dir_to_list(self.compiler.library_dirs,
783 d)
pxinwr32f5fdd2019-02-27 19:09:28 +0800784 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 +0200785 add_dir_to_list(self.compiler.include_dirs,
786 line.strip())
787 finally:
788 os.unlink(tmpfile)
789
pxinwr5e45f1c2021-01-22 08:55:52 +0800790 if VXWORKS:
791 self.add_wrcc_search_dirs()
792
Victor Stinnercfe172d2019-03-01 18:21:49 +0100793 def add_ldflags_cppflags(self):
Brett Cannon516592f2004-12-07 00:42:59 +0000794 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000795 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000796 # We must get the values from the Makefile and not the environment
797 # directly since an inconsistently reproducible issue comes up where
798 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000799 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000800 for env_var, arg_name, dir_list in (
Tarek Ziadé36797272010-07-22 12:50:05 +0000801 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
802 ('LDFLAGS', '-L', self.compiler.library_dirs),
803 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000804 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000805 if env_val:
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800806 parser = argparse.ArgumentParser()
807 parser.add_argument(arg_name, dest="dirs", action="append")
Miss Islington (bot)b1949e02021-10-18 11:49:28 -0700808
809 # To prevent argparse from raising an exception about any
810 # options in env_val that it mistakes for known option, we
811 # strip out all double dashes and any dashes followed by a
812 # character that is not for the option we are dealing with.
813 #
814 # Please note that order of the regex is important! We must
815 # strip out double-dashes first so that we don't end up with
816 # substituting "--Long" to "-Long" and thus lead to "ong" being
817 # used for a library directory.
818 env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
819 ' ', env_val)
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800820 options, _ = parser.parse_known_args(env_val.split())
Brett Cannon44837712005-01-02 21:54:07 +0000821 if options.dirs:
Christian Heimes292d3512008-02-03 16:51:08 +0000822 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000823 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000824
Victor Stinnercfe172d2019-03-01 18:21:49 +0100825 def configure_compiler(self):
826 # Ensure that /usr/local is always used, but the local build
827 # directories (i.e. '.' and 'Include') must be first. See issue
828 # 10520.
829 if not CROSS_COMPILING:
830 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
831 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
832 # only change this for cross builds for 3.3, issues on Mageia
833 if CROSS_COMPILING:
834 self.add_cross_compiling_paths()
835 self.add_multiarch_paths()
836 self.add_ldflags_cppflags()
837
Victor Stinner5ec33a12019-03-01 16:43:28 +0100838 def init_inc_lib_dirs(self):
Victor Stinner4cbea512019-02-28 17:48:38 +0100839 if (not CROSS_COMPILING and
Xavier de Gaye1351c312016-12-14 11:14:33 +0100840 os.path.normpath(sys.base_prefix) != '/usr' and
841 not sysconfig.get_config_var('PYTHONFRAMEWORK')):
Ronald Oussorenf3500e12010-10-20 13:10:12 +0000842 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
843 # (PYTHONFRAMEWORK is set) to avoid # linking problems when
844 # building a framework with different architectures than
845 # the one that is currently installed (issue #7473)
Tarek Ziadé36797272010-07-22 12:50:05 +0000846 add_dir_to_list(self.compiler.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000847 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000848 add_dir_to_list(self.compiler.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000849 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000850
xdegaye77f51392017-11-25 17:25:30 +0100851 system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
852 system_include_dirs = ['/usr/include']
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000853 # lib_dirs and inc_dirs are used to search for files;
854 # if a file is found in one of those directories, it can
855 # be assumed that no additional -I,-L directives are needed.
Victor Stinner4cbea512019-02-28 17:48:38 +0100856 if not CROSS_COMPILING:
Victor Stinner625dbf22019-03-01 15:59:39 +0100857 self.lib_dirs = self.compiler.library_dirs + system_lib_dirs
858 self.inc_dirs = self.compiler.include_dirs + system_include_dirs
Christian Heimesf19529c2012-12-12 12:41:00 +0100859 else:
xdegaye77f51392017-11-25 17:25:30 +0100860 # Add the sysroot paths. 'sysroot' is a compiler option used to
861 # set the logical path of the standard system headers and
862 # libraries.
Victor Stinner625dbf22019-03-01 15:59:39 +0100863 self.lib_dirs = (self.compiler.library_dirs +
864 sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
865 self.inc_dirs = (self.compiler.include_dirs +
866 sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
867 system_include_dirs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000868
Brett Cannon4454a1f2005-04-15 20:32:39 +0000869 config_h = sysconfig.get_config_h_filename()
Brett Cannon9f5db072010-10-29 20:19:27 +0000870 with open(config_h) as file:
Victor Stinner5ec33a12019-03-01 16:43:28 +0100871 self.config_h_vars = sysconfig.parse_config_h(file)
Brett Cannon4454a1f2005-04-15 20:32:39 +0000872
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000873 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
Victor Stinner4cbea512019-02-28 17:48:38 +0100874 if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']:
Victor Stinner625dbf22019-03-01 15:59:39 +0100875 self.lib_dirs += ['/usr/ccs/lib']
Skip Montanaro22e00c42003-05-06 20:43:34 +0000876
Charles-François Natali5739e102012-04-12 19:07:25 +0200877 # HP-UX11iv3 keeps files in lib/hpux folders.
Victor Stinner4cbea512019-02-28 17:48:38 +0100878 if HOST_PLATFORM == 'hp-ux11':
Victor Stinner625dbf22019-03-01 15:59:39 +0100879 self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
Charles-François Natali5739e102012-04-12 19:07:25 +0200880
Victor Stinner4cbea512019-02-28 17:48:38 +0100881 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000882 # This should work on any unixy platform ;-)
883 # If the user has bothered specifying additional -I and -L flags
884 # in OPT and LDFLAGS we might as well use them here.
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000885 #
886 # NOTE: using shlex.split would technically be more correct, but
887 # also gives a bootstrap problem. Let's hope nobody uses
888 # directories with whitespace in the name to store libraries.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889 cflags, ldflags = sysconfig.get_config_vars(
890 'CFLAGS', 'LDFLAGS')
891 for item in cflags.split():
892 if item.startswith('-I'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100893 self.inc_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894
895 for item in ldflags.split():
896 if item.startswith('-L'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100897 self.lib_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000898
Victor Stinner5ec33a12019-03-01 16:43:28 +0100899 def detect_simple_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000900 #
901 # The following modules are all pretty straightforward, and compile
902 # on pretty much any POSIXish platform.
903 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000904
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000905 # array objects
Victor Stinnercdad2722021-04-22 00:52:52 +0200906 self.add(Extension('array', ['arraymodule.c'],
907 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Martin Panterc9deece2016-02-03 05:19:44 +0000908
Yury Selivanovf23746a2018-01-22 19:11:18 -0500909 # Context Variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100910 self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
Yury Selivanovf23746a2018-01-22 19:11:18 -0500911
Martin Panterc9deece2016-02-03 05:19:44 +0000912 shared_math = 'Modules/_math.o'
Victor Stinnercfe172d2019-03-01 18:21:49 +0100913
914 # math library functions, e.g. sin()
915 self.add(Extension('math', ['mathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100916 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100917 extra_objects=[shared_math],
918 depends=['_math.h', shared_math],
919 libraries=['m']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100920
921 # complex math library functions
922 self.add(Extension('cmath', ['cmathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100923 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100924 extra_objects=[shared_math],
925 depends=['_math.h', shared_math],
926 libraries=['m']))
Victor Stinnere0be4232011-10-25 13:06:09 +0200927
928 # time libraries: librt may be needed for clock_gettime()
929 time_libs = []
930 lib = sysconfig.get_config_var('TIMEMODULE_LIB')
931 if lib:
932 time_libs.append(lib)
933
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000934 # time operations and variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100935 self.add(Extension('time', ['timemodule.c'],
936 libraries=time_libs))
Benjamin Peterson8acaa312017-11-12 20:53:39 -0800937 # libm is needed by delta_new() that uses round() and by accum() that
938 # uses modf().
Victor Stinner8058bda2019-03-01 15:31:45 +0100939 self.add(Extension('_datetime', ['_datetimemodule.c'],
Victor Stinner04fc4f22020-06-16 01:28:07 +0200940 libraries=['m'],
941 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Paul Ganssle62972d92020-05-16 04:20:06 -0400942 # zoneinfo module
Victor Stinner37834132020-10-27 17:12:53 +0100943 self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
944 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000945 # random number generator implemented in C
Victor Stinner9f5fe792020-04-17 19:05:35 +0200946 self.add(Extension("_random", ["_randommodule.c"],
947 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Raymond Hettinger0c410272004-01-05 10:13:35 +0000948 # bisect
Victor Stinner8058bda2019-03-01 15:31:45 +0100949 self.add(Extension("_bisect", ["_bisectmodule.c"]))
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000950 # heapq
Victor Stinnerc45dbe932020-06-22 17:39:32 +0200951 self.add(Extension("_heapq", ["_heapqmodule.c"],
952 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000953 # C-optimized pickle replacement
Victor Stinner5c75f372019-04-17 23:02:26 +0200954 self.add(Extension("_pickle", ["_pickle.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200955 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimes90540002008-05-08 14:29:10 +0000956 # _json speedups
Victor Stinner8058bda2019-03-01 15:31:45 +0100957 self.add(Extension("_json", ["_json.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200958 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100959
Fred Drake0e474a82007-10-11 18:01:43 +0000960 # profiler (_lsprof is for cProfile.py)
Victor Stinner8058bda2019-03-01 15:31:45 +0100961 self.add(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000962 # static Unicode character database
Victor Stinner8058bda2019-03-01 15:31:45 +0100963 self.add(Extension('unicodedata', ['unicodedata.c'],
Victor Stinner47e1afd2020-10-26 16:43:47 +0100964 depends=['unicodedata_db.h', 'unicodename_db.h'],
965 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Larry Hastings3a907972013-11-23 14:49:22 -0800966 # _opcode module
Victor Stinner8058bda2019-03-01 15:31:45 +0100967 self.add(Extension('_opcode', ['_opcode.c']))
INADA Naoki9f2ce252016-10-15 15:39:19 +0900968 # asyncio speedups
Chris Jerdonekda742ba2020-05-17 22:47:31 -0700969 self.add(Extension("_asyncio", ["_asynciomodule.c"],
970 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000971 # _abc speedups
Victor Stinnercdad2722021-04-22 00:52:52 +0200972 self.add(Extension("_abc", ["_abc.c"],
973 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Antoine Pitrou94e16962018-01-16 00:27:16 +0100974 # _queue module
Victor Stinnercdad2722021-04-22 00:52:52 +0200975 self.add(Extension("_queue", ["_queuemodule.c"],
976 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Dong-hee Na0a18ee42019-08-24 07:20:30 +0900977 # _statistics module
978 self.add(Extension("_statistics", ["_statisticsmodule.c"]))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000979
980 # Modules with some UNIX dependencies -- on by default:
981 # (If you have a really backward UNIX, select and socket may not be
982 # supported...)
983
984 # fcntl(2) and ioctl(2)
Antoine Pitroua3000072010-09-07 14:52:42 +0000985 libs = []
Victor Stinner5ec33a12019-03-01 16:43:28 +0100986 if (self.config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
Antoine Pitroua3000072010-09-07 14:52:42 +0000987 # May be necessary on AIX for flock function
988 libs = ['bsd']
Victor Stinner8058bda2019-03-01 15:31:45 +0100989 self.add(Extension('fcntl', ['fcntlmodule.c'],
990 libraries=libs))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000991 # pwd(3)
Victor Stinner8058bda2019-03-01 15:31:45 +0100992 self.add(Extension('pwd', ['pwdmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000993 # grp(3)
pxinwr32f5fdd2019-02-27 19:09:28 +0800994 if not VXWORKS:
Victor Stinner8058bda2019-03-01 15:31:45 +0100995 self.add(Extension('grp', ['grpmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000996 # spwd, shadow passwords
Victor Stinner5ec33a12019-03-01 16:43:28 +0100997 if (self.config_h_vars.get('HAVE_GETSPNAM', False) or
998 self.config_h_vars.get('HAVE_GETSPENT', False)):
Victor Stinner8058bda2019-03-01 15:31:45 +0100999 self.add(Extension('spwd', ['spwdmodule.c']))
Michael Felt08970cb2019-06-21 15:58:00 +02001000 # AIX has shadow passwords, but access is not via getspent(), etc.
1001 # module support is not expected so it not 'missing'
1002 elif not AIX:
Victor Stinner8058bda2019-03-01 15:31:45 +01001003 self.missing.append('spwd')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001004
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001005 # select(2); not on ancient System V
Victor Stinner8058bda2019-03-01 15:31:45 +01001006 self.add(Extension('select', ['selectmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001007
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001008 # Memory-mapped files (also works on Win32).
Victor Stinner8058bda2019-03-01 15:31:45 +01001009 self.add(Extension('mmap', ['mmapmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001010
Andrew M. Kuchling57269d02004-08-31 13:37:25 +00001011 # Lance Ellinghaus's syslog module
Ronald Oussoren94f25282010-05-05 19:11:21 +00001012 # syslog daemon interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001013 self.add(Extension('syslog', ['syslogmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001014
Eric Snow7f8bfc92018-01-29 18:23:44 -07001015 # Python interface to subinterpreter C-API.
Eric Snowc11183c2019-03-15 16:35:46 -06001016 self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
Eric Snow7f8bfc92018-01-29 18:23:44 -07001017
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001018 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +00001019 # Here ends the simple stuff. From here on, modules need certain
1020 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001021 #
1022
1023 # Multimedia modules
1024 # These don't work for 64-bit platforms!!!
1025 # These represent audio samples or images as strings:
Victor Stinnerdef80722016-04-19 15:58:11 +02001026 #
Neal Norwitz5e4a3b82004-07-19 16:55:07 +00001027 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +00001028 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001029 # 64-bit platforms.
Victor Stinnerdef80722016-04-19 15:58:11 +02001030 #
Benjamin Peterson8acaa312017-11-12 20:53:39 -08001031 # audioop needs libm for floor() in multiple functions.
Victor Stinner8058bda2019-03-01 15:31:45 +01001032 self.add(Extension('audioop', ['audioop.c'],
1033 libraries=['m']))
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001034
Victor Stinner5ec33a12019-03-01 16:43:28 +01001035 # CSV files
1036 self.add(Extension('_csv', ['_csv.c']))
1037
1038 # POSIX subprocess module helper.
Kyle Evans79925792020-10-13 15:04:44 -05001039 self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'],
1040 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner5ec33a12019-03-01 16:43:28 +01001041
Victor Stinnercfe172d2019-03-01 18:21:49 +01001042 def detect_test_extensions(self):
1043 # Python C API test module
1044 self.add(Extension('_testcapi', ['_testcapimodule.c'],
1045 depends=['testcapi_long.h']))
1046
Victor Stinner23bace22019-04-18 11:37:26 +02001047 # Python Internal C API test module
1048 self.add(Extension('_testinternalcapi', ['_testinternalcapi.c'],
Victor Stinner57491342019-04-23 12:26:33 +02001049 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner23bace22019-04-18 11:37:26 +02001050
Victor Stinnercfe172d2019-03-01 18:21:49 +01001051 # Python PEP-3118 (buffer protocol) test module
1052 self.add(Extension('_testbuffer', ['_testbuffer.c']))
1053
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001054 # Test loading multiple modules from one compiled file (https://bugs.python.org/issue16421)
Victor Stinnercfe172d2019-03-01 18:21:49 +01001055 self.add(Extension('_testimportmultiple', ['_testimportmultiple.c']))
1056
1057 # Test multi-phase extension module init (PEP 489)
1058 self.add(Extension('_testmultiphase', ['_testmultiphase.c']))
1059
1060 # Fuzz tests.
1061 self.add(Extension('_xxtestfuzz',
1062 ['_xxtestfuzz/_xxtestfuzz.c',
1063 '_xxtestfuzz/fuzzer.c']))
1064
Victor Stinner5ec33a12019-03-01 16:43:28 +01001065 def detect_readline_curses(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001066 # readline
Stefan Krah095b2732010-06-08 13:41:44 +00001067 readline_termcap_library = ""
1068 curses_library = ""
doko@ubuntu.com58844492012-06-30 18:25:32 +02001069 # Cannot use os.popen here in py3k.
1070 tmpfile = os.path.join(self.build_temp, 'readline_termcap_lib')
1071 if not os.path.exists(self.build_temp):
1072 os.makedirs(self.build_temp)
Stefan Krah095b2732010-06-08 13:41:44 +00001073 # Determine if readline is already linked against curses or tinfo.
Roland Hiebere1f77692021-02-09 02:05:25 +01001074 if sysconfig.get_config_var('HAVE_LIBREADLINE'):
1075 if sysconfig.get_config_var('WITH_EDITLINE'):
1076 readline_lib = 'edit'
1077 else:
1078 readline_lib = 'readline'
1079 do_readline = self.compiler.find_library_file(self.lib_dirs,
1080 readline_lib)
Victor Stinner4cbea512019-02-28 17:48:38 +01001081 if CROSS_COMPILING:
Victor Stinner6b982c22020-04-01 01:10:07 +02001082 ret = run_command("%s -d %s | grep '(NEEDED)' > %s"
doko@ubuntu.com58844492012-06-30 18:25:32 +02001083 % (sysconfig.get_config_var('READELF'),
1084 do_readline, tmpfile))
1085 elif find_executable('ldd'):
Victor Stinner6b982c22020-04-01 01:10:07 +02001086 ret = run_command("ldd %s > %s" % (do_readline, tmpfile))
doko@ubuntu.com58844492012-06-30 18:25:32 +02001087 else:
Victor Stinner6b982c22020-04-01 01:10:07 +02001088 ret = 1
1089 if ret == 0:
Brett Cannon9f5db072010-10-29 20:19:27 +00001090 with open(tmpfile) as fp:
1091 for ln in fp:
1092 if 'curses' in ln:
1093 readline_termcap_library = re.sub(
1094 r'.*lib(n?cursesw?)\.so.*', r'\1', ln
1095 ).rstrip()
1096 break
1097 # termcap interface split out from ncurses
1098 if 'tinfo' in ln:
1099 readline_termcap_library = 'tinfo'
1100 break
doko@ubuntu.com4c990712012-06-30 23:28:09 +02001101 if os.path.exists(tmpfile):
1102 os.unlink(tmpfile)
Roland Hiebere1f77692021-02-09 02:05:25 +01001103 else:
1104 do_readline = False
Stefan Krah095b2732010-06-08 13:41:44 +00001105 # Issue 7384: If readline is already linked against curses,
1106 # use the same library for the readline and curses modules.
1107 if 'curses' in readline_termcap_library:
1108 curses_library = readline_termcap_library
Victor Stinner625dbf22019-03-01 15:59:39 +01001109 elif self.compiler.find_library_file(self.lib_dirs, 'ncursesw'):
Stefan Krah095b2732010-06-08 13:41:44 +00001110 curses_library = 'ncursesw'
Michael Felt08970cb2019-06-21 15:58:00 +02001111 # Issue 36210: OSS provided ncurses does not link on AIX
1112 # Use IBM supplied 'curses' for successful build of _curses
1113 elif AIX and self.compiler.find_library_file(self.lib_dirs, 'curses'):
1114 curses_library = 'curses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001115 elif self.compiler.find_library_file(self.lib_dirs, 'ncurses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001116 curses_library = 'ncurses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001117 elif self.compiler.find_library_file(self.lib_dirs, 'curses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001118 curses_library = 'curses'
1119
Victor Stinner4cbea512019-02-28 17:48:38 +01001120 if MACOS:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001121 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren961683a2010-03-08 07:09:59 +00001122 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
Ned Deily04cdfa12014-06-25 13:36:14 -07001123 if (dep_target and
Ronald Oussoren49926cf2021-02-01 04:29:44 +01001124 (tuple(int(n) for n in dep_target.split('.')[0:2])
Ned Deily04cdfa12014-06-25 13:36:14 -07001125 < (10, 5) ) ):
Ronald Oussoren961683a2010-03-08 07:09:59 +00001126 os_release = 8
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001127 if os_release < 9:
1128 # MacOSX 10.4 has a broken readline. Don't try to build
1129 # the readline module unless the user has installed a fixed
1130 # readline package
Victor Stinner625dbf22019-03-01 15:59:39 +01001131 if find_file('readline/rlconf.h', self.inc_dirs, []) is None:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001132 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +00001133 if do_readline:
Victor Stinner4cbea512019-02-28 17:48:38 +01001134 if MACOS and os_release < 9:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001135 # In every directory on the search path search for a dynamic
1136 # library and then a static library, instead of first looking
Fred Drake0af17612007-09-04 19:43:19 +00001137 # for dynamic libraries on the entire path.
Martin Pantere26da7c2016-06-02 10:07:09 +00001138 # This way a statically linked custom readline gets picked up
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001139 # before the (possibly broken) dynamic library in /usr/lib.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140 readline_extra_link_args = ('-Wl,-search_paths_first',)
1141 else:
1142 readline_extra_link_args = ()
1143
Roland Hiebere1f77692021-02-09 02:05:25 +01001144 readline_libs = [readline_lib]
Stefan Krah095b2732010-06-08 13:41:44 +00001145 if readline_termcap_library:
1146 pass # Issue 7384: Already linked against curses or tinfo.
1147 elif curses_library:
1148 readline_libs.append(curses_library)
Victor Stinner625dbf22019-03-01 15:59:39 +01001149 elif self.compiler.find_library_file(self.lib_dirs +
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001150 ['/usr/lib/termcap'],
1151 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +00001152 readline_libs.append('termcap')
Victor Stinner8058bda2019-03-01 15:31:45 +01001153 self.add(Extension('readline', ['readline.c'],
1154 library_dirs=['/usr/lib/termcap'],
1155 extra_link_args=readline_extra_link_args,
1156 libraries=readline_libs))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001157 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001158 self.missing.append('readline')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001159
Victor Stinner5ec33a12019-03-01 16:43:28 +01001160 # Curses support, requiring the System V version of curses, often
1161 # provided by the ncurses library.
1162 curses_defines = []
1163 curses_includes = []
1164 panel_library = 'panel'
1165 if curses_library == 'ncursesw':
1166 curses_defines.append(('HAVE_NCURSESW', '1'))
1167 if not CROSS_COMPILING:
1168 curses_includes.append('/usr/include/ncursesw')
1169 # Bug 1464056: If _curses.so links with ncursesw,
1170 # _curses_panel.so must link with panelw.
1171 panel_library = 'panelw'
1172 if MACOS:
1173 # On OS X, there is no separate /usr/lib/libncursesw nor
1174 # libpanelw. If we are here, we found a locally-supplied
1175 # version of libncursesw. There should also be a
1176 # libpanelw. _XOPEN_SOURCE defines are usually excluded
1177 # for OS X but we need _XOPEN_SOURCE_EXTENDED here for
1178 # ncurses wide char support
1179 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
1180 elif MACOS and curses_library == 'ncurses':
1181 # Building with the system-suppied combined libncurses/libpanel
1182 curses_defines.append(('HAVE_NCURSESW', '1'))
1183 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
Tim Peters2c60f7a2003-01-29 03:49:43 +00001184
Victor Stinnercfe172d2019-03-01 18:21:49 +01001185 curses_enabled = True
Victor Stinner5ec33a12019-03-01 16:43:28 +01001186 if curses_library.startswith('ncurses'):
1187 curses_libs = [curses_library]
1188 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001189 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001190 include_dirs=curses_includes,
1191 define_macros=curses_defines,
1192 libraries=curses_libs))
1193 elif curses_library == 'curses' and not MACOS:
1194 # OSX has an old Berkeley curses, not good enough for
1195 # the _curses module.
1196 if (self.compiler.find_library_file(self.lib_dirs, 'terminfo')):
1197 curses_libs = ['curses', 'terminfo']
1198 elif (self.compiler.find_library_file(self.lib_dirs, 'termcap')):
1199 curses_libs = ['curses', 'termcap']
1200 else:
1201 curses_libs = ['curses']
1202
1203 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001204 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001205 define_macros=curses_defines,
1206 libraries=curses_libs))
1207 else:
Victor Stinnercfe172d2019-03-01 18:21:49 +01001208 curses_enabled = False
Victor Stinner5ec33a12019-03-01 16:43:28 +01001209 self.missing.append('_curses')
1210
1211 # If the curses module is enabled, check for the panel module
Michael Felt08970cb2019-06-21 15:58:00 +02001212 # _curses_panel needs some form of ncurses
1213 skip_curses_panel = True if AIX else False
1214 if (curses_enabled and not skip_curses_panel and
1215 self.compiler.find_library_file(self.lib_dirs, panel_library)):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001216 self.add(Extension('_curses_panel', ['_curses_panel.c'],
Michael Felt08970cb2019-06-21 15:58:00 +02001217 include_dirs=curses_includes,
1218 define_macros=curses_defines,
1219 libraries=[panel_library, *curses_libs]))
1220 elif not skip_curses_panel:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001221 self.missing.append('_curses_panel')
1222
1223 def detect_crypt(self):
1224 # crypt module.
pxinwr236d0b72019-04-15 17:02:20 +08001225 if VXWORKS:
1226 # bpo-31904: crypt() function is not provided by VxWorks.
1227 # DES_crypt() OpenSSL provides is too weak to implement
1228 # the encryption.
Victor Stinnercad80202021-01-19 23:04:49 +01001229 self.missing.append('_crypt')
pxinwr236d0b72019-04-15 17:02:20 +08001230 return
1231
Victor Stinner625dbf22019-03-01 15:59:39 +01001232 if self.compiler.find_library_file(self.lib_dirs, 'crypt'):
Ronald Oussoren94f25282010-05-05 19:11:21 +00001233 libs = ['crypt']
Guido van Rossumd8faa362007-04-27 19:54:29 +00001234 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +00001235 libs = []
pxinwr32f5fdd2019-02-27 19:09:28 +08001236
Victor Stinnercad80202021-01-19 23:04:49 +01001237 self.add(Extension('_crypt', ['_cryptmodule.c'], libraries=libs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001238
Victor Stinner5ec33a12019-03-01 16:43:28 +01001239 def detect_socket(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001240 # socket(2)
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001241 kwargs = {'depends': ['socketmodule.h']}
pxinwr00a65682020-11-29 06:14:16 +08001242 if MACOS:
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001243 # Issue #35569: Expose RFC 3542 socket options.
1244 kwargs['extra_compile_args'] = ['-D__APPLE_USE_RFC_3542']
Erlend Egeberg Aasland9a45bfe2020-05-17 08:32:46 +02001245
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001246 self.add(Extension('_socket', ['socketmodule.c'], **kwargs))
pxinwr32f5fdd2019-02-27 19:09:28 +08001247
Victor Stinner5ec33a12019-03-01 16:43:28 +01001248 def detect_dbm_gdbm(self):
Georg Brandl489cb4f2009-07-11 10:08:49 +00001249 # Modules that provide persistent dictionary-like semantics. You will
1250 # probably want to arrange for at least one of them to be available on
1251 # your machine, though none are defined by default because of library
1252 # dependencies. The Python module dbm/__init__.py provides an
1253 # implementation independent wrapper for these; dbm/dumb.py provides
1254 # similar functionality (but slower of course) implemented in Python.
1255
1256 # Sleepycat^WOracle Berkeley DB interface.
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001257 # https://www.oracle.com/database/technologies/related/berkeleydb.html
Georg Brandl489cb4f2009-07-11 10:08:49 +00001258 #
1259 # This requires the Sleepycat^WOracle DB code. The supported versions
1260 # are set below. Visit the URL above to download
1261 # a release. Most open source OSes come with one or more
1262 # versions of BerkeleyDB already installed.
1263
doko@ubuntu.com15bac0f2012-07-01 10:35:54 +02001264 max_db_ver = (5, 3)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001265 min_db_ver = (3, 3)
1266 db_setup_debug = False # verbose debug prints from this script?
1267
1268 def allow_db_ver(db_ver):
1269 """Returns a boolean if the given BerkeleyDB version is acceptable.
1270
1271 Args:
1272 db_ver: A tuple of the version to verify.
1273 """
1274 if not (min_db_ver <= db_ver <= max_db_ver):
1275 return False
1276 return True
1277
1278 def gen_db_minor_ver_nums(major):
1279 if major == 4:
1280 for x in range(max_db_ver[1]+1):
1281 if allow_db_ver((4, x)):
1282 yield x
1283 elif major == 3:
1284 for x in (3,):
1285 if allow_db_ver((3, x)):
1286 yield x
1287 else:
1288 raise ValueError("unknown major BerkeleyDB version", major)
1289
1290 # construct a list of paths to look for the header file in on
1291 # top of the normal inc_dirs.
1292 db_inc_paths = [
1293 '/usr/include/db4',
1294 '/usr/local/include/db4',
1295 '/opt/sfw/include/db4',
1296 '/usr/include/db3',
1297 '/usr/local/include/db3',
1298 '/opt/sfw/include/db3',
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001299 # Fink defaults (https://www.finkproject.org/)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001300 '/sw/include/db4',
1301 '/sw/include/db3',
1302 ]
1303 # 4.x minor number specific paths
1304 for x in gen_db_minor_ver_nums(4):
1305 db_inc_paths.append('/usr/include/db4%d' % x)
1306 db_inc_paths.append('/usr/include/db4.%d' % x)
1307 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
1308 db_inc_paths.append('/usr/local/include/db4%d' % x)
1309 db_inc_paths.append('/pkg/db-4.%d/include' % x)
1310 db_inc_paths.append('/opt/db-4.%d/include' % x)
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001311 # MacPorts default (https://www.macports.org/)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001312 db_inc_paths.append('/opt/local/include/db4%d' % x)
1313 # 3.x minor number specific paths
1314 for x in gen_db_minor_ver_nums(3):
1315 db_inc_paths.append('/usr/include/db3%d' % x)
1316 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
1317 db_inc_paths.append('/usr/local/include/db3%d' % x)
1318 db_inc_paths.append('/pkg/db-3.%d/include' % x)
1319 db_inc_paths.append('/opt/db-3.%d/include' % x)
1320
Victor Stinner4cbea512019-02-28 17:48:38 +01001321 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001322 db_inc_paths = []
1323
Georg Brandl489cb4f2009-07-11 10:08:49 +00001324 # Add some common subdirectories for Sleepycat DB to the list,
1325 # based on the standard include directories. This way DB3/4 gets
1326 # picked up when it is installed in a non-standard prefix and
1327 # the user has added that prefix into inc_dirs.
1328 std_variants = []
Victor Stinner625dbf22019-03-01 15:59:39 +01001329 for dn in self.inc_dirs:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001330 std_variants.append(os.path.join(dn, 'db3'))
1331 std_variants.append(os.path.join(dn, 'db4'))
1332 for x in gen_db_minor_ver_nums(4):
1333 std_variants.append(os.path.join(dn, "db4%d"%x))
1334 std_variants.append(os.path.join(dn, "db4.%d"%x))
1335 for x in gen_db_minor_ver_nums(3):
1336 std_variants.append(os.path.join(dn, "db3%d"%x))
1337 std_variants.append(os.path.join(dn, "db3.%d"%x))
1338
1339 db_inc_paths = std_variants + db_inc_paths
1340 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
1341
1342 db_ver_inc_map = {}
1343
Victor Stinner4cbea512019-02-28 17:48:38 +01001344 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001345 sysroot = macosx_sdk_root()
1346
Georg Brandl489cb4f2009-07-11 10:08:49 +00001347 class db_found(Exception): pass
1348 try:
1349 # See whether there is a Sleepycat header in the standard
1350 # search path.
Victor Stinner625dbf22019-03-01 15:59:39 +01001351 for d in self.inc_dirs + db_inc_paths:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001352 f = os.path.join(d, "db.h")
Victor Stinner4cbea512019-02-28 17:48:38 +01001353 if MACOS and is_macosx_sdk_path(d):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001354 f = os.path.join(sysroot, d[1:], "db.h")
1355
Georg Brandl489cb4f2009-07-11 10:08:49 +00001356 if db_setup_debug: print("db: looking for db.h in", f)
1357 if os.path.exists(f):
Brett Cannon9f5db072010-10-29 20:19:27 +00001358 with open(f, 'rb') as file:
1359 f = file.read()
Benjamin Peterson019f3612009-08-12 18:18:03 +00001360 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001361 if m:
1362 db_major = int(m.group(1))
Benjamin Peterson019f3612009-08-12 18:18:03 +00001363 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001364 db_minor = int(m.group(1))
1365 db_ver = (db_major, db_minor)
1366
1367 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
1368 if db_ver == (4, 6):
Benjamin Peterson019f3612009-08-12 18:18:03 +00001369 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001370 db_patch = int(m.group(1))
1371 if db_patch < 21:
1372 print("db.h:", db_ver, "patch", db_patch,
1373 "being ignored (4.6.x must be >= 4.6.21)")
1374 continue
1375
1376 if ( (db_ver not in db_ver_inc_map) and
1377 allow_db_ver(db_ver) ):
1378 # save the include directory with the db.h version
1379 # (first occurrence only)
1380 db_ver_inc_map[db_ver] = d
1381 if db_setup_debug:
1382 print("db.h: found", db_ver, "in", d)
1383 else:
1384 # we already found a header for this library version
1385 if db_setup_debug: print("db.h: ignoring", d)
1386 else:
1387 # ignore this header, it didn't contain a version number
1388 if db_setup_debug:
1389 print("db.h: no version number version in", d)
1390
1391 db_found_vers = list(db_ver_inc_map.keys())
1392 db_found_vers.sort()
1393
1394 while db_found_vers:
1395 db_ver = db_found_vers.pop()
1396 db_incdir = db_ver_inc_map[db_ver]
1397
1398 # check lib directories parallel to the location of the header
1399 db_dirs_to_check = [
1400 db_incdir.replace("include", 'lib64'),
1401 db_incdir.replace("include", 'lib'),
1402 ]
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001403
Victor Stinner4cbea512019-02-28 17:48:38 +01001404 if not MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001405 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check))
1406
1407 else:
1408 # Same as other branch, but takes OSX SDK into account
1409 tmp = []
1410 for dn in db_dirs_to_check:
1411 if is_macosx_sdk_path(dn):
1412 if os.path.isdir(os.path.join(sysroot, dn[1:])):
1413 tmp.append(dn)
1414 else:
1415 if os.path.isdir(dn):
1416 tmp.append(dn)
Ronald Oussorendc969e52010-06-27 12:37:46 +00001417 db_dirs_to_check = tmp
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001418
1419 db_dirs_to_check = tmp
Georg Brandl489cb4f2009-07-11 10:08:49 +00001420
Ezio Melotti42da6632011-03-15 05:18:48 +02001421 # Look for a version specific db-X.Y before an ambiguous dbX
Georg Brandl489cb4f2009-07-11 10:08:49 +00001422 # XXX should we -ever- look for a dbX name? Do any
1423 # systems really not name their library by version and
1424 # symlink to more general names?
1425 for dblib in (('db-%d.%d' % db_ver),
1426 ('db%d%d' % db_ver),
1427 ('db%d' % db_ver[0])):
1428 dblib_file = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001429 db_dirs_to_check + self.lib_dirs, dblib )
Georg Brandl489cb4f2009-07-11 10:08:49 +00001430 if dblib_file:
1431 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
1432 raise db_found
1433 else:
1434 if db_setup_debug: print("db lib: ", dblib, "not found")
1435
1436 except db_found:
1437 if db_setup_debug:
1438 print("bsddb using BerkeleyDB lib:", db_ver, dblib)
1439 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001440 dblibs = [dblib]
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001441 # Only add the found library and include directories if they aren't
1442 # already being searched. This avoids an explicit runtime library
1443 # dependency.
Victor Stinner625dbf22019-03-01 15:59:39 +01001444 if db_incdir in self.inc_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001445 db_incs = None
1446 else:
1447 db_incs = [db_incdir]
Victor Stinner625dbf22019-03-01 15:59:39 +01001448 if dblib_dir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001449 dblib_dir = None
Georg Brandl489cb4f2009-07-11 10:08:49 +00001450 else:
1451 if db_setup_debug: print("db: no appropriate library found")
1452 db_incs = None
1453 dblibs = []
1454 dblib_dir = None
1455
Victor Stinner5ec33a12019-03-01 16:43:28 +01001456 dbm_setup_debug = False # verbose debug prints from this script?
1457 dbm_order = ['gdbm']
1458 # The standard Unix dbm module:
1459 if not CYGWIN:
1460 config_args = [arg.strip("'")
1461 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
1462 dbm_args = [arg for arg in config_args
1463 if arg.startswith('--with-dbmliborder=')]
1464 if dbm_args:
1465 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
1466 else:
1467 dbm_order = "ndbm:gdbm:bdb".split(":")
1468 dbmext = None
1469 for cand in dbm_order:
1470 if cand == "ndbm":
1471 if find_file("ndbm.h", self.inc_dirs, []) is not None:
1472 # Some systems have -lndbm, others have -lgdbm_compat,
1473 # others don't have either
1474 if self.compiler.find_library_file(self.lib_dirs,
1475 'ndbm'):
1476 ndbm_libs = ['ndbm']
1477 elif self.compiler.find_library_file(self.lib_dirs,
1478 'gdbm_compat'):
1479 ndbm_libs = ['gdbm_compat']
1480 else:
1481 ndbm_libs = []
1482 if dbm_setup_debug: print("building dbm using ndbm")
1483 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1484 define_macros=[
1485 ('HAVE_NDBM_H',None),
1486 ],
1487 libraries=ndbm_libs)
1488 break
1489
1490 elif cand == "gdbm":
1491 if self.compiler.find_library_file(self.lib_dirs, 'gdbm'):
1492 gdbm_libs = ['gdbm']
1493 if self.compiler.find_library_file(self.lib_dirs,
1494 'gdbm_compat'):
1495 gdbm_libs.append('gdbm_compat')
1496 if find_file("gdbm/ndbm.h", self.inc_dirs, []) is not None:
1497 if dbm_setup_debug: print("building dbm using gdbm")
1498 dbmext = Extension(
1499 '_dbm', ['_dbmmodule.c'],
1500 define_macros=[
1501 ('HAVE_GDBM_NDBM_H', None),
1502 ],
1503 libraries = gdbm_libs)
1504 break
1505 if find_file("gdbm-ndbm.h", self.inc_dirs, []) is not None:
1506 if dbm_setup_debug: print("building dbm using gdbm")
1507 dbmext = Extension(
1508 '_dbm', ['_dbmmodule.c'],
1509 define_macros=[
1510 ('HAVE_GDBM_DASH_NDBM_H', None),
1511 ],
1512 libraries = gdbm_libs)
1513 break
1514 elif cand == "bdb":
1515 if dblibs:
1516 if dbm_setup_debug: print("building dbm using bdb")
1517 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1518 library_dirs=dblib_dir,
1519 runtime_library_dirs=dblib_dir,
1520 include_dirs=db_incs,
1521 define_macros=[
1522 ('HAVE_BERKDB_H', None),
1523 ('DB_DBM_HSEARCH', None),
1524 ],
1525 libraries=dblibs)
1526 break
1527 if dbmext is not None:
1528 self.add(dbmext)
1529 else:
1530 self.missing.append('_dbm')
1531
1532 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
1533 if ('gdbm' in dbm_order and
1534 self.compiler.find_library_file(self.lib_dirs, 'gdbm')):
1535 self.add(Extension('_gdbm', ['_gdbmmodule.c'],
1536 libraries=['gdbm']))
1537 else:
1538 self.missing.append('_gdbm')
1539
1540 def detect_sqlite(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541 # The sqlite interface
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 sqlite_setup_debug = False # verbose debug prints from this script?
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543
1544 # We hunt for #define SQLITE_VERSION "n.n.n"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001545 sqlite_incdir = sqlite_libdir = None
1546 sqlite_inc_paths = [ '/usr/include',
1547 '/usr/include/sqlite',
1548 '/usr/include/sqlite3',
1549 '/usr/local/include',
1550 '/usr/local/include/sqlite',
1551 '/usr/local/include/sqlite3',
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001552 ]
Victor Stinner4cbea512019-02-28 17:48:38 +01001553 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001554 sqlite_inc_paths = []
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +01001555 MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556 MIN_SQLITE_VERSION = ".".join([str(x)
1557 for x in MIN_SQLITE_VERSION_NUMBER])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001558
1559 # Scan the default include directories before the SQLite specific
1560 # ones. This allows one to override the copy of sqlite on OSX,
1561 # where /usr/include contains an old version of sqlite.
Victor Stinner4cbea512019-02-28 17:48:38 +01001562 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001563 sysroot = macosx_sdk_root()
1564
Victor Stinner625dbf22019-03-01 15:59:39 +01001565 for d_ in self.inc_dirs + sqlite_inc_paths:
Ned Deily9b635832012-08-05 15:13:33 -07001566 d = d_
Victor Stinner4cbea512019-02-28 17:48:38 +01001567 if MACOS and is_macosx_sdk_path(d):
Ned Deily9b635832012-08-05 15:13:33 -07001568 d = os.path.join(sysroot, d[1:])
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001569
Ned Deily9b635832012-08-05 15:13:33 -07001570 f = os.path.join(d, "sqlite3.h")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001571 if os.path.exists(f):
Guido van Rossum452bf512007-02-09 05:32:43 +00001572 if sqlite_setup_debug: print("sqlite: found %s"%f)
Brett Cannon9f5db072010-10-29 20:19:27 +00001573 with open(f) as file:
1574 incf = file.read()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 m = re.search(
Petri Lehtinened909bc2013-02-23 17:05:28 +01001576 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001577 if m:
1578 sqlite_version = m.group(1)
1579 sqlite_version_tuple = tuple([int(x)
1580 for x in sqlite_version.split(".")])
1581 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
1582 # we win!
Thomas Wouters89f507f2006-12-13 04:49:30 +00001583 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001584 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001585 sqlite_incdir = d
1586 break
1587 else:
1588 if sqlite_setup_debug:
Charles Pigottad0daf52019-04-26 16:38:12 +01001589 print("%s: version %s is too old, need >= %s"%(d,
Guido van Rossum452bf512007-02-09 05:32:43 +00001590 sqlite_version, MIN_SQLITE_VERSION))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001591 elif sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001592 print("sqlite: %s had no SQLITE_VERSION"%(f,))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593
1594 if sqlite_incdir:
1595 sqlite_dirs_to_check = [
1596 os.path.join(sqlite_incdir, '..', 'lib64'),
1597 os.path.join(sqlite_incdir, '..', 'lib'),
1598 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
1599 os.path.join(sqlite_incdir, '..', '..', 'lib'),
1600 ]
Tarek Ziadé36797272010-07-22 12:50:05 +00001601 sqlite_libfile = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001602 sqlite_dirs_to_check + self.lib_dirs, 'sqlite3')
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001603 if sqlite_libfile:
1604 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001605
1606 if sqlite_incdir and sqlite_libdir:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001607 sqlite_srcs = ['_sqlite/cache.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001608 '_sqlite/connection.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609 '_sqlite/cursor.c',
1610 '_sqlite/microprotocols.c',
1611 '_sqlite/module.c',
1612 '_sqlite/prepare_protocol.c',
1613 '_sqlite/row.c',
1614 '_sqlite/statement.c',
1615 '_sqlite/util.c', ]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616 sqlite_defines = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001617
Benjamin Peterson076ed002010-10-31 17:11:02 +00001618 # Enable support for loadable extensions in the sqlite3 module
1619 # if --enable-loadable-sqlite-extensions configure option is used.
1620 if '--enable-loadable-sqlite-extensions' not in sysconfig.get_config_var("CONFIG_ARGS"):
1621 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Miss Islington (bot)baa8d482021-08-27 04:29:24 -07001622 elif MACOS and sqlite_incdir == os.path.join(MACOS_SDK_ROOT, "usr/include"):
1623 raise DistutilsError("System version of SQLite does not support loadable extensions")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624
Victor Stinner4cbea512019-02-28 17:48:38 +01001625 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001626 # In every directory on the search path search for a dynamic
1627 # library and then a static library, instead of first looking
Ezio Melotti13925002011-03-16 11:05:33 +02001628 # for dynamic libraries on the entire path.
1629 # This way a statically linked custom sqlite gets picked up
Thomas Wouters477c8d52006-05-27 19:21:47 +00001630 # before the dynamic library in /usr/lib.
1631 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1632 else:
1633 sqlite_extra_link_args = ()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001634
Brett Cannonc5011fe2011-06-06 20:09:10 -07001635 include_dirs = ["Modules/_sqlite"]
1636 # Only include the directory where sqlite was found if it does
1637 # not already exist in set include directories, otherwise you
1638 # can end up with a bad search path order.
1639 if sqlite_incdir not in self.compiler.include_dirs:
1640 include_dirs.append(sqlite_incdir)
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001641 # avoid a runtime library path for a system library dir
Victor Stinner625dbf22019-03-01 15:59:39 +01001642 if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001643 sqlite_libdir = None
Victor Stinner8058bda2019-03-01 15:31:45 +01001644 self.add(Extension('_sqlite3', sqlite_srcs,
1645 define_macros=sqlite_defines,
1646 include_dirs=include_dirs,
1647 library_dirs=sqlite_libdir,
1648 extra_link_args=sqlite_extra_link_args,
1649 libraries=["sqlite3",]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001650 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001651 self.missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001652
Victor Stinner5ec33a12019-03-01 16:43:28 +01001653 def detect_platform_specific_exts(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001654 # Unix-only modules
Victor Stinner4cbea512019-02-28 17:48:38 +01001655 if not MS_WINDOWS:
pxinwr32f5fdd2019-02-27 19:09:28 +08001656 if not VXWORKS:
1657 # Steen Lumholt's termios module
Victor Stinner8058bda2019-03-01 15:31:45 +01001658 self.add(Extension('termios', ['termios.c']))
pxinwr32f5fdd2019-02-27 19:09:28 +08001659 # Jeremy Hylton's rlimit interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001660 self.add(Extension('resource', ['resource.c']))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001661 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001662 self.missing.extend(['resource', 'termios'])
Christian Heimes29a7df72018-01-26 23:28:46 +01001663
Victor Stinner5ec33a12019-03-01 16:43:28 +01001664 # Platform-specific libraries
1665 if HOST_PLATFORM.startswith(('linux', 'freebsd', 'gnukfreebsd')):
1666 self.add(Extension('ossaudiodev', ['ossaudiodev.c']))
Michael Felt08970cb2019-06-21 15:58:00 +02001667 elif not AIX:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001668 self.missing.append('ossaudiodev')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001669
Victor Stinner5ec33a12019-03-01 16:43:28 +01001670 if MACOS:
Ned Deily951ab582020-05-18 11:31:21 -04001671 self.add(Extension('_scproxy', ['_scproxy.c'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001672 extra_link_args=[
1673 '-framework', 'SystemConfiguration',
Ned Deily951ab582020-05-18 11:31:21 -04001674 '-framework', 'CoreFoundation']))
Fredrik Lundhade711a2001-01-24 08:00:28 +00001675
Victor Stinner5ec33a12019-03-01 16:43:28 +01001676 def detect_compress_exts(self):
Barry Warsaw259b1e12002-08-13 20:09:26 +00001677 # Andrew Kuchling's zlib module. Note that some versions of zlib
1678 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1679 # http://www.cert.org/advisories/CA-2002-07.html
1680 #
1681 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1682 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1683 # now, we still accept 1.1.3, because we think it's difficult to
1684 # exploit this in Python, and we'd rather make it RedHat's problem
1685 # than our problem <wink>.
1686 #
1687 # You can upgrade zlib to version 1.1.4 yourself by going to
1688 # http://www.gzip.org/zlib/
Victor Stinner625dbf22019-03-01 15:59:39 +01001689 zlib_inc = find_file('zlib.h', [], self.inc_dirs)
Christian Heimes1dc54002008-03-24 02:19:29 +00001690 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001691 if zlib_inc is not None:
1692 zlib_h = zlib_inc[0] + '/zlib.h'
1693 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001694 version_req = '"1.1.3"'
Victor Stinner4cbea512019-02-28 17:48:38 +01001695 if MACOS and is_macosx_sdk_path(zlib_h):
Ned Deily507c5912013-10-18 21:32:00 -07001696 zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
Brett Cannon9f5db072010-10-29 20:19:27 +00001697 with open(zlib_h) as fp:
1698 while 1:
1699 line = fp.readline()
1700 if not line:
1701 break
1702 if line.startswith('#define ZLIB_VERSION'):
1703 version = line.split()[2]
1704 break
Guido van Rossume6970912001-04-15 15:16:12 +00001705 if version >= version_req:
Victor Stinner625dbf22019-03-01 15:59:39 +01001706 if (self.compiler.find_library_file(self.lib_dirs, 'z')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001707 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001708 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1709 else:
1710 zlib_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001711 self.add(Extension('zlib', ['zlibmodule.c'],
1712 libraries=['z'],
1713 extra_link_args=zlib_extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001714 have_zlib = True
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')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001719 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001720 self.missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001721
Christian Heimes1dc54002008-03-24 02:19:29 +00001722 # Helper module for various ascii-encoders. Uses zlib for an optimized
1723 # crc32 if we have it. Otherwise binascii uses its own.
1724 if have_zlib:
1725 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1726 libraries = ['z']
1727 extra_link_args = zlib_extra_link_args
1728 else:
1729 extra_compile_args = []
1730 libraries = []
1731 extra_link_args = []
Victor Stinner8058bda2019-03-01 15:31:45 +01001732 self.add(Extension('binascii', ['binascii.c'],
1733 extra_compile_args=extra_compile_args,
1734 libraries=libraries,
1735 extra_link_args=extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001736
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001737 # Gustavo Niemeyer's bz2 module.
Victor Stinner625dbf22019-03-01 15:59:39 +01001738 if (self.compiler.find_library_file(self.lib_dirs, 'bz2')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001739 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001740 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1741 else:
1742 bz2_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001743 self.add(Extension('_bz2', ['_bz2module.c'],
1744 libraries=['bz2'],
1745 extra_link_args=bz2_extra_link_args))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001746 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001747 self.missing.append('_bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001748
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001749 # LZMA compression support.
Victor Stinner625dbf22019-03-01 15:59:39 +01001750 if self.compiler.find_library_file(self.lib_dirs, 'lzma'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001751 self.add(Extension('_lzma', ['_lzmamodule.c'],
1752 libraries=['lzma']))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001753 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001754 self.missing.append('_lzma')
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001755
Victor Stinner5ec33a12019-03-01 16:43:28 +01001756 def detect_expat_elementtree(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001757 # Interface to the Expat XML parser
1758 #
Benjamin Petersona28e7022010-01-09 18:53:06 +00001759 # Expat was written by James Clark and is now maintained by a group of
1760 # developers on SourceForge; see www.libexpat.org for more information.
1761 # The pyexpat module was written by Paul Prescod after a prototype by
1762 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1763 # of a system shared libexpat.so is possible with --with-system-expat
Benjamin Petersonc73206c2010-10-31 16:38:19 +00001764 # configure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001765 #
1766 # More information on Expat can be found at www.libexpat.org.
1767 #
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001768 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1769 expat_inc = []
1770 define_macros = []
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001771 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001772 expat_lib = ['expat']
1773 expat_sources = []
Christian Heimesd489c7a2013-02-09 17:02:06 +01001774 expat_depends = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001775 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01001776 expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')]
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001777 define_macros = [
1778 ('HAVE_EXPAT_CONFIG_H', '1'),
Victor Stinner93d0cb52017-08-18 23:43:54 +02001779 # bpo-30947: Python uses best available entropy sources to
1780 # call XML_SetHashSalt(), expat entropy sources are not needed
1781 ('XML_POOR_ENTROPY', '1'),
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001782 ]
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001783 extra_compile_args = []
Miss Islington (bot)412ae8a2021-09-29 07:13:41 -07001784 # bpo-44394: libexpat uses isnan() of math.h and needs linkage
1785 # against the libm
1786 expat_lib = ['m']
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001787 expat_sources = ['expat/xmlparse.c',
1788 'expat/xmlrole.c',
1789 'expat/xmltok.c']
Christian Heimesd489c7a2013-02-09 17:02:06 +01001790 expat_depends = ['expat/ascii.h',
1791 'expat/asciitab.h',
1792 'expat/expat.h',
1793 'expat/expat_config.h',
1794 'expat/expat_external.h',
1795 'expat/internal.h',
1796 'expat/latin1tab.h',
1797 'expat/utf8tab.h',
1798 'expat/xmlrole.h',
1799 'expat/xmltok.h',
1800 'expat/xmltok_impl.h'
1801 ]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001802
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001803 cc = sysconfig.get_config_var('CC').split()[0]
Victor Stinner6b982c22020-04-01 01:10:07 +02001804 ret = run_command(
Benjamin Peterson95da3102019-06-29 16:00:22 -07001805 '"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc)
Victor Stinner6b982c22020-04-01 01:10:07 +02001806 if ret == 0:
Benjamin Peterson95da3102019-06-29 16:00:22 -07001807 extra_compile_args.append('-Wno-unreachable-code')
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001808
Victor Stinner8058bda2019-03-01 15:31:45 +01001809 self.add(Extension('pyexpat',
1810 define_macros=define_macros,
1811 extra_compile_args=extra_compile_args,
1812 include_dirs=expat_inc,
1813 libraries=expat_lib,
1814 sources=['pyexpat.c'] + expat_sources,
1815 depends=expat_depends))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001816
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001817 # Fredrik Lundh's cElementTree module. Note that this also
1818 # uses expat (via the CAPI hook in pyexpat).
1819
Victor Stinner625dbf22019-03-01 15:59:39 +01001820 if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001821 define_macros.append(('USE_PYEXPAT_CAPI', None))
Victor Stinner8058bda2019-03-01 15:31:45 +01001822 self.add(Extension('_elementtree',
1823 define_macros=define_macros,
1824 include_dirs=expat_inc,
1825 libraries=expat_lib,
1826 sources=['_elementtree.c'],
1827 depends=['pyexpat.c', *expat_sources,
1828 *expat_depends]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001829 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001830 self.missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001831
Victor Stinner5ec33a12019-03-01 16:43:28 +01001832 def detect_multibytecodecs(self):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001833 # Hye-Shik Chang's CJKCodecs modules.
Victor Stinner8058bda2019-03-01 15:31:45 +01001834 self.add(Extension('_multibytecodec',
1835 ['cjkcodecs/multibytecodec.c']))
Walter Dörwalde9eaab42007-05-22 16:02:13 +00001836 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001837 self.add(Extension('_codecs_%s' % loc,
1838 ['cjkcodecs/_codecs_%s.c' % loc]))
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001839
Victor Stinner5ec33a12019-03-01 16:43:28 +01001840 def detect_multiprocessing(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001841 # Richard Oudkerk's multiprocessing module
Victor Stinner4cbea512019-02-28 17:48:38 +01001842 if MS_WINDOWS:
Victor Stinnerc991f242019-03-01 17:19:04 +01001843 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c',
1844 '_multiprocessing/semaphore.c']
Benjamin Petersone711caf2008-06-11 16:44:04 +00001845 else:
Victor Stinnerc991f242019-03-01 17:19:04 +01001846 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c']
Mark Dickinsona614f042009-11-28 12:48:43 +00001847 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
1848 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001849 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
Victor Stinner8058bda2019-03-01 15:31:45 +01001850 self.add(Extension('_multiprocessing', multiprocessing_srcs,
Victor Stinner8058bda2019-03-01 15:31:45 +01001851 include_dirs=["Modules/_multiprocessing"]))
Guido van Rossuma9e20242007-03-08 00:43:48 +00001852
Victor Stinnercad80202021-01-19 23:04:49 +01001853 if (not MS_WINDOWS and
1854 sysconfig.get_config_var('HAVE_SHM_OPEN') and
1855 sysconfig.get_config_var('HAVE_SHM_UNLINK')):
1856 posixshmem_srcs = ['_multiprocessing/posixshmem.c']
1857 libs = []
1858 if sysconfig.get_config_var('SHM_NEEDS_LIBRT'):
1859 # need to link with librt to get shm_open()
1860 libs.append('rt')
1861 self.add(Extension('_posixshmem', posixshmem_srcs,
1862 define_macros={},
1863 libraries=libs,
1864 include_dirs=["Modules/_multiprocessing"]))
1865 else:
1866 self.missing.append('_posixshmem')
1867
Victor Stinner5ec33a12019-03-01 16:43:28 +01001868 def detect_uuid(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001869 # Build the _uuid module if possible
Miss Islington (bot)b71bc052021-11-02 04:49:17 -07001870 uuid_h = sysconfig.get_config_var("HAVE_UUID_H")
1871 uuid_uuid_h = sysconfig.get_config_var("HAVE_UUID_UUID_H")
1872 if uuid_h or uuid_uuid_h:
1873 if sysconfig.get_config_var("HAVE_LIBUUID"):
1874 uuid_libs = ["uuid"]
Antoine Pitroua106aec2017-09-28 23:03:06 +02001875 else:
1876 uuid_libs = []
Victor Stinnercfe172d2019-03-01 18:21:49 +01001877 self.add(Extension('_uuid', ['_uuidmodule.c'],
Miss Islington (bot)b71bc052021-11-02 04:49:17 -07001878 libraries=uuid_libs))
Antoine Pitroua106aec2017-09-28 23:03:06 +02001879 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001880 self.missing.append('_uuid')
Antoine Pitroua106aec2017-09-28 23:03:06 +02001881
Victor Stinner5ec33a12019-03-01 16:43:28 +01001882 def detect_modules(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001883 self.detect_simple_extensions()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001884 if TEST_EXTENSIONS:
1885 self.detect_test_extensions()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001886 self.detect_readline_curses()
1887 self.detect_crypt()
1888 self.detect_socket()
1889 self.detect_openssl_hashlib()
xdegaye2ee077f2019-04-09 17:20:08 +02001890 self.detect_hash_builtins()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001891 self.detect_dbm_gdbm()
1892 self.detect_sqlite()
1893 self.detect_platform_specific_exts()
1894 self.detect_nis()
1895 self.detect_compress_exts()
1896 self.detect_expat_elementtree()
1897 self.detect_multibytecodecs()
1898 self.detect_decimal()
1899 self.detect_ctypes()
1900 self.detect_multiprocessing()
1901 if not self.detect_tkinter():
1902 self.missing.append('_tkinter')
1903 self.detect_uuid()
1904
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001905## # Uncomment these lines if you want to play with xxmodule.c
Victor Stinnercfe172d2019-03-01 18:21:49 +01001906## self.add(Extension('xx', ['xxmodule.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001907
Hai Shi5787ba42021-04-06 20:55:13 +08001908 # The limited C API is not compatible with the Py_TRACE_REFS macro.
1909 if not sysconfig.get_config_var('Py_TRACE_REFS'):
1910 self.add(Extension('xxlimited', ['xxlimited.c']))
1911 self.add(Extension('xxlimited_35', ['xxlimited_35.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001912
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001913 def detect_tkinter_fromenv(self):
1914 # Build _tkinter using the Tcl/Tk locations specified by
1915 # the _TCLTK_INCLUDES and _TCLTK_LIBS environment variables.
1916 # This method is meant to be invoked by detect_tkinter().
Ned Deilyd819b932013-09-06 01:07:05 -07001917 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001918 # The variables can be set via one of the following ways.
Ned Deilyd819b932013-09-06 01:07:05 -07001919 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001920 # - Automatically, at configuration time, by using pkg-config.
1921 # The tool is called by the configure script.
1922 # Additional pkg-config configuration paths can be set via the
1923 # PKG_CONFIG_PATH environment variable.
1924 #
1925 # PKG_CONFIG_PATH=".../lib/pkgconfig" ./configure ...
1926 #
1927 # - Explicitly, at configuration time by setting both
1928 # --with-tcltk-includes and --with-tcltk-libs.
1929 #
1930 # ./configure ... \
Ned Deilyd819b932013-09-06 01:07:05 -07001931 # --with-tcltk-includes="-I/path/to/tclincludes \
1932 # -I/path/to/tkincludes"
1933 # --with-tcltk-libs="-L/path/to/tcllibs -ltclm.n \
1934 # -L/path/to/tklibs -ltkm.n"
1935 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001936 # - Explicitly, at compile time, by passing TCLTK_INCLUDES and
1937 # TCLTK_LIBS to the make target.
1938 # This will override any configuration-time option.
1939 #
1940 # make TCLTK_INCLUDES="..." TCLTK_LIBS="..."
Ned Deilyd819b932013-09-06 01:07:05 -07001941 #
1942 # This can be useful for building and testing tkinter with multiple
1943 # versions of Tcl/Tk. Note that a build of Tk depends on a particular
1944 # build of Tcl so you need to specify both arguments and use care when
1945 # overriding.
1946
1947 # The _TCLTK variables are created in the Makefile sharedmods target.
1948 tcltk_includes = os.environ.get('_TCLTK_INCLUDES')
1949 tcltk_libs = os.environ.get('_TCLTK_LIBS')
1950 if not (tcltk_includes and tcltk_libs):
1951 # Resume default configuration search.
Victor Stinner4cbea512019-02-28 17:48:38 +01001952 return False
Ned Deilyd819b932013-09-06 01:07:05 -07001953
1954 extra_compile_args = tcltk_includes.split()
1955 extra_link_args = tcltk_libs.split()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001956 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1957 define_macros=[('WITH_APPINIT', 1)],
1958 extra_compile_args = extra_compile_args,
1959 extra_link_args = extra_link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01001960 return True
Ned Deilyd819b932013-09-06 01:07:05 -07001961
Victor Stinner625dbf22019-03-01 15:59:39 +01001962 def detect_tkinter_darwin(self):
Ned Deily1731d6d2020-05-18 04:32:38 -04001963 # Build default _tkinter on macOS using Tcl and Tk frameworks.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001964 # This method is meant to be invoked by detect_tkinter().
Ned Deily1731d6d2020-05-18 04:32:38 -04001965 #
1966 # The macOS native Tk (AKA Aqua Tk) and Tcl are most commonly
1967 # built and installed as macOS framework bundles. However,
1968 # for several reasons, we cannot take full advantage of the
1969 # Apple-supplied compiler chain's -framework options here.
1970 # Instead, we need to find and pass to the compiler the
1971 # absolute paths of the Tcl and Tk headers files we want to use
1972 # and the absolute path to the directory containing the Tcl
1973 # and Tk frameworks for linking.
1974 #
1975 # We want to handle here two common use cases on macOS:
1976 # 1. Build and link with system-wide third-party or user-built
1977 # Tcl and Tk frameworks installed in /Library/Frameworks.
1978 # 2. Build and link using a user-specified macOS SDK so that the
1979 # built Python can be exported to other systems. In this case,
1980 # search only the SDK's /Library/Frameworks (normally empty)
1981 # and /System/Library/Frameworks.
1982 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001983 # Any other use cases are handled either by detect_tkinter_fromenv(),
1984 # or detect_tkinter(). The former handles non-standard locations of
1985 # Tcl/Tk, defined via the _TCLTK_INCLUDES and _TCLTK_LIBS environment
1986 # variables. The latter handles any Tcl/Tk versions installed in
1987 # standard Unix directories.
1988 #
1989 # It would be desirable to also handle here the case where
Ned Deily1731d6d2020-05-18 04:32:38 -04001990 # you want to build and link with a framework build of Tcl and Tk
1991 # that is not in /Library/Frameworks, say, in your private
1992 # $HOME/Library/Frameworks directory or elsewhere. It turns
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301993 # out to be difficult to make that work automatically here
Ned Deily1731d6d2020-05-18 04:32:38 -04001994 # without bringing into play more tools and magic. That case
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301995 # can be handled using a recipe with the right arguments
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001996 # to detect_tkinter_fromenv().
Ned Deily1731d6d2020-05-18 04:32:38 -04001997 #
1998 # Note also that the fallback case here is to try to use the
1999 # Apple-supplied Tcl and Tk frameworks in /System/Library but
2000 # be forewarned that they are deprecated by Apple and typically
2001 # out-of-date and buggy; their use should be avoided if at
2002 # all possible by installing a newer version of Tcl and Tk in
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302003 # /Library/Frameworks before building Python without
Ned Deily1731d6d2020-05-18 04:32:38 -04002004 # an explicit SDK or by configuring build arguments explicitly.
2005
Jack Jansen0b06be72002-06-21 14:48:38 +00002006 from os.path import join, exists
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002007
Ned Deily1731d6d2020-05-18 04:32:38 -04002008 sysroot = macosx_sdk_root() # path to the SDK or '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00002009
Ned Deily1731d6d2020-05-18 04:32:38 -04002010 if macosx_sdk_specified():
2011 # Use case #2: an SDK other than '/' was specified.
2012 # Only search there.
2013 framework_dirs = [
2014 join(sysroot, 'Library', 'Frameworks'),
2015 join(sysroot, 'System', 'Library', 'Frameworks'),
2016 ]
2017 else:
2018 # Use case #1: no explicit SDK selected.
2019 # Search the local system-wide /Library/Frameworks,
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302020 # not the one in the default SDK, otherwise fall back to
Ned Deily1731d6d2020-05-18 04:32:38 -04002021 # /System/Library/Frameworks whose header files may be in
2022 # the default SDK or, on older systems, actually installed.
2023 framework_dirs = [
2024 join('/', 'Library', 'Frameworks'),
2025 join(sysroot, 'System', 'Library', 'Frameworks'),
2026 ]
2027
2028 # Find the directory that contains the Tcl.framework and
2029 # Tk.framework bundles.
Jack Jansen0b06be72002-06-21 14:48:38 +00002030 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00002031 # both Tcl.framework and Tk.framework should be present
Jack Jansen0b06be72002-06-21 14:48:38 +00002032 for fw in 'Tcl', 'Tk':
Ned Deily1731d6d2020-05-18 04:32:38 -04002033 if not exists(join(F, fw + '.framework')):
2034 break
Jack Jansen0b06be72002-06-21 14:48:38 +00002035 else:
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302036 # ok, F is now directory with both frameworks. Continue
Jack Jansen0b06be72002-06-21 14:48:38 +00002037 # building
2038 break
2039 else:
2040 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
2041 # will now resume.
Victor Stinner4cbea512019-02-28 17:48:38 +01002042 return False
Tim Peters2c60f7a2003-01-29 03:49:43 +00002043
Jack Jansen0b06be72002-06-21 14:48:38 +00002044 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00002045 join(F, fw + '.framework', H)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002046 for fw in ('Tcl', 'Tk')
Ned Deily1731d6d2020-05-18 04:32:38 -04002047 for H in ('Headers',)
Jack Jansen0b06be72002-06-21 14:48:38 +00002048 ]
2049
Ned Deily1731d6d2020-05-18 04:32:38 -04002050 # Add the base framework directory as well
2051 compile_args = ['-F', F]
Jack Jansen0b06be72002-06-21 14:48:38 +00002052
Ned Deily1731d6d2020-05-18 04:32:38 -04002053 # Do not build tkinter for archs that this Tk was not built with.
Georg Brandlfcaf9102008-07-16 02:17:56 +00002054 cflags = sysconfig.get_config_vars('CFLAGS')[0]
R David Murray44b548d2016-09-08 13:59:53 -04002055 archs = re.findall(r'-arch\s+(\w+)', cflags)
Georg Brandlfcaf9102008-07-16 02:17:56 +00002056
Ronald Oussorend097efe2009-09-15 19:07:58 +00002057 tmpfile = os.path.join(self.build_temp, 'tk.arch')
2058 if not os.path.exists(self.build_temp):
2059 os.makedirs(self.build_temp)
2060
Ned Deily1731d6d2020-05-18 04:32:38 -04002061 run_command(
2062 "file {}/Tk.framework/Tk | grep 'for architecture' > {}".format(F, tmpfile)
2063 )
Brett Cannon9f5db072010-10-29 20:19:27 +00002064 with open(tmpfile) as fp:
2065 detected_archs = []
2066 for ln in fp:
2067 a = ln.split()[-1]
2068 if a in archs:
2069 detected_archs.append(ln.split()[-1])
Ronald Oussorend097efe2009-09-15 19:07:58 +00002070 os.unlink(tmpfile)
2071
Ned Deily1731d6d2020-05-18 04:32:38 -04002072 arch_args = []
Ronald Oussorend097efe2009-09-15 19:07:58 +00002073 for a in detected_archs:
Ned Deily1731d6d2020-05-18 04:32:38 -04002074 arch_args.append('-arch')
2075 arch_args.append(a)
2076
2077 compile_args += arch_args
2078 link_args = [','.join(['-Wl', '-F', F, '-framework', 'Tcl', '-framework', 'Tk']), *arch_args]
2079
2080 # The X11/xlib.h file bundled in the Tk sources can cause function
2081 # prototype warnings from the compiler. Since we cannot easily fix
2082 # that, suppress the warnings here instead.
2083 if '-Wstrict-prototypes' in cflags.split():
2084 compile_args.append('-Wno-strict-prototypes')
Georg Brandlfcaf9102008-07-16 02:17:56 +00002085
Victor Stinnercfe172d2019-03-01 18:21:49 +01002086 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2087 define_macros=[('WITH_APPINIT', 1)],
2088 include_dirs=include_dirs,
2089 libraries=[],
Ned Deily1731d6d2020-05-18 04:32:38 -04002090 extra_compile_args=compile_args,
2091 extra_link_args=link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01002092 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002093
Victor Stinner625dbf22019-03-01 15:59:39 +01002094 def detect_tkinter(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002095 # The _tkinter module.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002096 #
2097 # Detection of Tcl/Tk is attempted in the following order:
2098 # - Through environment variables.
2099 # - Platform specific detection of Tcl/Tk (currently only macOS).
2100 # - Search of various standard Unix header/library paths.
2101 #
2102 # Detection stops at the first successful method.
Michael W. Hudson5b109102002-01-23 15:04:41 +00002103
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002104 # Check for Tcl and Tk at the locations indicated by _TCLTK_INCLUDES
2105 # and _TCLTK_LIBS environment variables.
2106 if self.detect_tkinter_fromenv():
Victor Stinner5ec33a12019-03-01 16:43:28 +01002107 return True
Ned Deilyd819b932013-09-06 01:07:05 -07002108
Jack Jansen0b06be72002-06-21 14:48:38 +00002109 # Rather than complicate the code below, detecting and building
2110 # AquaTk is a separate method. Only one Tkinter will be built on
2111 # Darwin - either AquaTk, if it is found, or X11 based Tk.
Victor Stinner5ec33a12019-03-01 16:43:28 +01002112 if (MACOS and self.detect_tkinter_darwin()):
2113 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002114
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002115 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002116 # The versions with dots are used on Unix, and the versions without
2117 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002118 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polo5d377bd2009-08-16 14:44:14 +00002119 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
2120 '8.2', '82', '8.1', '81', '8.0', '80']:
Victor Stinner625dbf22019-03-01 15:59:39 +01002121 tklib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002122 'tk' + version)
Victor Stinner625dbf22019-03-01 15:59:39 +01002123 tcllib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002124 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00002125 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002126 # Exit the loop when we've found the Tcl/Tk libraries
2127 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002128
Fredrik Lundhade711a2001-01-24 08:00:28 +00002129 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002130 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002131 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002132 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002133 dotversion = version
Victor Stinner4cbea512019-02-28 17:48:38 +01002134 if '.' not in dotversion and "bsd" in HOST_PLATFORM.lower():
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002135 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
2136 # but the include subdirs are named like .../include/tcl8.3.
2137 dotversion = dotversion[:-1] + '.' + dotversion[-1]
2138 tcl_include_sub = []
2139 tk_include_sub = []
Victor Stinner625dbf22019-03-01 15:59:39 +01002140 for dir in self.inc_dirs:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002141 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
2142 tk_include_sub += [dir + os.sep + "tk" + dotversion]
2143 tk_include_sub += tcl_include_sub
Victor Stinner625dbf22019-03-01 15:59:39 +01002144 tcl_includes = find_file('tcl.h', self.inc_dirs, tcl_include_sub)
2145 tk_includes = find_file('tk.h', self.inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002146
Martin v. Löwise86a59a2003-05-03 08:45:51 +00002147 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002148 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002149 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Victor Stinner5ec33a12019-03-01 16:43:28 +01002150 return False
Fredrik Lundhade711a2001-01-24 08:00:28 +00002151
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002152 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002153
Victor Stinnercfe172d2019-03-01 18:21:49 +01002154 include_dirs = []
2155 libs = []
2156 defs = []
2157 added_lib_dirs = []
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002158 for dir in tcl_includes + tk_includes:
2159 if dir not in include_dirs:
2160 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002161
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002162 # Check for various platform-specific directories
Victor Stinner4cbea512019-02-28 17:48:38 +01002163 if HOST_PLATFORM == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002164 include_dirs.append('/usr/openwin/include')
2165 added_lib_dirs.append('/usr/openwin/lib')
2166 elif os.path.exists('/usr/X11R6/include'):
2167 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00002168 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002169 added_lib_dirs.append('/usr/X11R6/lib')
2170 elif os.path.exists('/usr/X11R5/include'):
2171 include_dirs.append('/usr/X11R5/include')
2172 added_lib_dirs.append('/usr/X11R5/lib')
2173 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002174 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002175 include_dirs.append('/usr/X11/include')
2176 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002177
Jason Tishler9181c942003-02-05 15:16:17 +00002178 # If Cygwin, then verify that X is installed before proceeding
Victor Stinner4cbea512019-02-28 17:48:38 +01002179 if CYGWIN:
Jason Tishler9181c942003-02-05 15:16:17 +00002180 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
2181 if x11_inc is None:
Victor Stinner5ec33a12019-03-01 16:43:28 +01002182 return False
Jason Tishler9181c942003-02-05 15:16:17 +00002183
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002184 # Check for BLT extension
Victor Stinner625dbf22019-03-01 15:59:39 +01002185 if self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002186 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002187 defs.append( ('WITH_BLT', 1) )
2188 libs.append('BLT8.0')
Victor Stinner625dbf22019-03-01 15:59:39 +01002189 elif self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002190 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00002191 defs.append( ('WITH_BLT', 1) )
2192 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002193
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002194 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00002195 libs.append('tk'+ version)
2196 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002197
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002198 # Finally, link with the X11 libraries (not appropriate on cygwin)
Victor Stinner4cbea512019-02-28 17:48:38 +01002199 if not CYGWIN:
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002200 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002201
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002202 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002203 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002204 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002205 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002206 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002207 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002208 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002209
Victor Stinnercfe172d2019-03-01 18:21:49 +01002210 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2211 define_macros=[('WITH_APPINIT', 1)] + defs,
2212 include_dirs=include_dirs,
2213 libraries=libs,
2214 library_dirs=added_lib_dirs))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002215 return True
2216
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002217 def configure_ctypes(self, ext):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002218 return True
2219
Victor Stinner625dbf22019-03-01 15:59:39 +01002220 def detect_ctypes(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01002221 # Thomas Heller's _ctypes module
Ronald Oussoren41761932020-11-08 10:05:27 +01002222
2223 if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
2224 self.use_system_libffi = True
2225 else:
2226 self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
2227
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002228 include_dirs = []
Victor Stinner1ae035b2020-04-17 17:47:20 +02002229 extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002230 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00002231 sources = ['_ctypes/_ctypes.c',
2232 '_ctypes/callbacks.c',
2233 '_ctypes/callproc.c',
2234 '_ctypes/stgdict.c',
Thomas Heller864cc672010-08-08 17:58:53 +00002235 '_ctypes/cfield.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00002236 depends = ['_ctypes/ctypes.h']
2237
Victor Stinner4cbea512019-02-28 17:48:38 +01002238 if MACOS:
Ronald Oussoren2decf222010-09-05 18:25:59 +00002239 sources.append('_ctypes/malloc_closure.c')
Ronald Oussoren41761932020-11-08 10:05:27 +01002240 extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
Christian Heimes78644762008-03-04 23:39:23 +00002241 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00002242 include_dirs.append('_ctypes/darwin')
Thomas Hellercf567c12006-03-08 19:51:58 +00002243
Victor Stinner4cbea512019-02-28 17:48:38 +01002244 elif HOST_PLATFORM == 'sunos5':
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002245 # XXX This shouldn't be necessary; it appears that some
2246 # of the assembler code is non-PIC (i.e. it has relocations
2247 # when it shouldn't. The proper fix would be to rewrite
2248 # the assembler code to be PIC.
2249 # This only works with GCC; the Sun compiler likely refuses
2250 # this option. If you want to compile ctypes with the Sun
2251 # compiler, please research a proper solution, instead of
2252 # finding some -z option for the Sun compiler.
2253 extra_link_args.append('-mimpure-text')
2254
Victor Stinner4cbea512019-02-28 17:48:38 +01002255 elif HOST_PLATFORM.startswith('hp-ux'):
Thomas Heller3eaaeb42008-05-23 17:26:46 +00002256 extra_link_args.append('-fPIC')
2257
Thomas Hellercf567c12006-03-08 19:51:58 +00002258 ext = Extension('_ctypes',
2259 include_dirs=include_dirs,
2260 extra_compile_args=extra_compile_args,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002261 extra_link_args=extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002262 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00002263 sources=sources,
2264 depends=depends)
Victor Stinnercfe172d2019-03-01 18:21:49 +01002265 self.add(ext)
2266 if TEST_EXTENSIONS:
2267 # function my_sqrt() needs libm for sqrt()
2268 self.add(Extension('_ctypes_test',
2269 sources=['_ctypes/_ctypes_test.c'],
2270 libraries=['m']))
Thomas Hellercf567c12006-03-08 19:51:58 +00002271
Ronald Oussoren41761932020-11-08 10:05:27 +01002272 ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
2273 ffi_lib = None
2274
Victor Stinner625dbf22019-03-01 15:59:39 +01002275 ffi_inc_dirs = self.inc_dirs.copy()
Victor Stinner4cbea512019-02-28 17:48:38 +01002276 if MACOS:
Ronald Oussoren41761932020-11-08 10:05:27 +01002277 ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
Christian Heimes78644762008-03-04 23:39:23 +00002278
Ronald Oussoren41761932020-11-08 10:05:27 +01002279 if not ffi_inc:
2280 if os.path.exists(ffi_in_sdk):
2281 ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
2282 ffi_inc = ffi_in_sdk
2283 ffi_lib = 'ffi'
2284 else:
2285 # OS X 10.5 comes with libffi.dylib; the include files are
2286 # in /usr/include/ffi
2287 ffi_inc_dirs.append('/usr/include/ffi')
2288
2289 if not ffi_inc:
2290 found = find_file('ffi.h', [], ffi_inc_dirs)
2291 if found:
2292 ffi_inc = found[0]
2293 if ffi_inc:
2294 ffi_h = ffi_inc + '/ffi.h'
Shlomi Fish6d51b872017-09-06 23:19:19 +03002295 if not os.path.exists(ffi_h):
2296 ffi_inc = None
2297 print('Header file {} does not exist'.format(ffi_h))
Ronald Oussoren41761932020-11-08 10:05:27 +01002298 if ffi_lib is None and ffi_inc:
doko@ubuntu.comae683652016-06-05 01:38:29 +02002299 for lib_name in ('ffi', 'ffi_pic'):
Victor Stinner625dbf22019-03-01 15:59:39 +01002300 if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002301 ffi_lib = lib_name
2302 break
2303
2304 if ffi_inc and ffi_lib:
Ronald Oussoren41761932020-11-08 10:05:27 +01002305 ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
2306 if grep_headers_for('ffi_prep_cif_var', ffi_headers):
2307 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
2308 if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
2309 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
2310 if grep_headers_for('ffi_closure_alloc', ffi_headers):
2311 ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
2312
2313 ext.include_dirs.append(ffi_inc)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314 ext.libraries.append(ffi_lib)
2315 self.use_system_libffi = True
2316
Christian Heimes5bb96922018-02-25 10:22:14 +01002317 if sysconfig.get_config_var('HAVE_LIBDL'):
2318 # for dlopen, see bpo-32647
2319 ext.libraries.append('dl')
2320
Victor Stinner5ec33a12019-03-01 16:43:28 +01002321 def detect_decimal(self):
2322 # Stefan Krah's _decimal module
Stefan Krah60187b52012-03-23 19:06:27 +01002323 extra_compile_args = []
Stefan Kraha10e2fb2012-09-01 14:21:22 +02002324 undef_macros = []
Stefan Krah60187b52012-03-23 19:06:27 +01002325 if '--with-system-libmpdec' in sysconfig.get_config_var("CONFIG_ARGS"):
2326 include_dirs = []
Antoine Pitrou73b20ae2021-03-30 18:11:06 +02002327 libraries = ['mpdec']
Stefan Krah60187b52012-03-23 19:06:27 +01002328 sources = ['_decimal/_decimal.c']
2329 depends = ['_decimal/docstrings.h']
2330 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01002331 include_dirs = [os.path.abspath(os.path.join(self.srcdir,
Ned Deily458a6fb2012-04-01 02:30:46 -07002332 'Modules',
2333 '_decimal',
2334 'libmpdec'))]
Stefan Krahbd4ed772017-12-06 18:24:17 +01002335 libraries = ['m']
Stefan Krah60187b52012-03-23 19:06:27 +01002336 sources = [
2337 '_decimal/_decimal.c',
2338 '_decimal/libmpdec/basearith.c',
2339 '_decimal/libmpdec/constants.c',
2340 '_decimal/libmpdec/context.c',
2341 '_decimal/libmpdec/convolute.c',
2342 '_decimal/libmpdec/crt.c',
2343 '_decimal/libmpdec/difradix2.c',
2344 '_decimal/libmpdec/fnt.c',
2345 '_decimal/libmpdec/fourstep.c',
2346 '_decimal/libmpdec/io.c',
Stefan Krahf117d872019-07-10 18:27:38 +02002347 '_decimal/libmpdec/mpalloc.c',
Stefan Krah60187b52012-03-23 19:06:27 +01002348 '_decimal/libmpdec/mpdecimal.c',
2349 '_decimal/libmpdec/numbertheory.c',
2350 '_decimal/libmpdec/sixstep.c',
2351 '_decimal/libmpdec/transpose.c',
2352 ]
2353 depends = [
2354 '_decimal/docstrings.h',
2355 '_decimal/libmpdec/basearith.h',
2356 '_decimal/libmpdec/bits.h',
2357 '_decimal/libmpdec/constants.h',
2358 '_decimal/libmpdec/convolute.h',
2359 '_decimal/libmpdec/crt.h',
2360 '_decimal/libmpdec/difradix2.h',
2361 '_decimal/libmpdec/fnt.h',
2362 '_decimal/libmpdec/fourstep.h',
2363 '_decimal/libmpdec/io.h',
Stefan Krah8d013a82016-04-26 16:34:41 +02002364 '_decimal/libmpdec/mpalloc.h',
Stefan Krah60187b52012-03-23 19:06:27 +01002365 '_decimal/libmpdec/mpdecimal.h',
2366 '_decimal/libmpdec/numbertheory.h',
2367 '_decimal/libmpdec/sixstep.h',
2368 '_decimal/libmpdec/transpose.h',
2369 '_decimal/libmpdec/typearith.h',
2370 '_decimal/libmpdec/umodarith.h',
2371 ]
2372
Stefan Krah1919b7e2012-03-21 18:25:23 +01002373 config = {
2374 'x64': [('CONFIG_64','1'), ('ASM','1')],
2375 'uint128': [('CONFIG_64','1'), ('ANSI','1'), ('HAVE_UINT128_T','1')],
2376 'ansi64': [('CONFIG_64','1'), ('ANSI','1')],
2377 'ppro': [('CONFIG_32','1'), ('PPRO','1'), ('ASM','1')],
2378 'ansi32': [('CONFIG_32','1'), ('ANSI','1')],
2379 'ansi-legacy': [('CONFIG_32','1'), ('ANSI','1'),
2380 ('LEGACY_COMPILER','1')],
2381 'universal': [('UNIVERSAL','1')]
2382 }
2383
Stefan Krah1919b7e2012-03-21 18:25:23 +01002384 cc = sysconfig.get_config_var('CC')
2385 sizeof_size_t = sysconfig.get_config_var('SIZEOF_SIZE_T')
2386 machine = os.environ.get('PYTHON_DECIMAL_WITH_MACHINE')
2387
2388 if machine:
2389 # Override automatic configuration to facilitate testing.
2390 define_macros = config[machine]
Victor Stinner4cbea512019-02-28 17:48:38 +01002391 elif MACOS:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002392 # Universal here means: build with the same options Python
2393 # was built with.
2394 define_macros = config['universal']
2395 elif sizeof_size_t == 8:
2396 if sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X64'):
2397 define_macros = config['x64']
2398 elif sysconfig.get_config_var('HAVE_GCC_UINT128_T'):
2399 define_macros = config['uint128']
2400 else:
2401 define_macros = config['ansi64']
2402 elif sizeof_size_t == 4:
2403 ppro = sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X87')
2404 if ppro and ('gcc' in cc or 'clang' in cc) and \
Victor Stinner4cbea512019-02-28 17:48:38 +01002405 not 'sunos' in HOST_PLATFORM:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002406 # solaris: problems with register allocation.
2407 # icc >= 11.0 works as well.
2408 define_macros = config['ppro']
Stefan Krahce23dbc2012-09-30 21:12:53 +02002409 extra_compile_args.append('-Wno-unknown-pragmas')
Stefan Krah1919b7e2012-03-21 18:25:23 +01002410 else:
2411 define_macros = config['ansi32']
2412 else:
2413 raise DistutilsError("_decimal: unsupported architecture")
2414
2415 # Workarounds for toolchain bugs:
2416 if sysconfig.get_config_var('HAVE_IPA_PURE_CONST_BUG'):
2417 # Some versions of gcc miscompile inline asm:
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002418 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46491
2419 # https://gcc.gnu.org/ml/gcc/2010-11/msg00366.html
Stefan Krah1919b7e2012-03-21 18:25:23 +01002420 extra_compile_args.append('-fno-ipa-pure-const')
2421 if sysconfig.get_config_var('HAVE_GLIBC_MEMMOVE_BUG'):
2422 # _FORTIFY_SOURCE wrappers for memmove and bcopy are incorrect:
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002423 # https://sourceware.org/ml/libc-alpha/2010-12/msg00009.html
Stefan Krah1919b7e2012-03-21 18:25:23 +01002424 undef_macros.append('_FORTIFY_SOURCE')
2425
Stefan Krah1919b7e2012-03-21 18:25:23 +01002426 # Uncomment for extra functionality:
2427 #define_macros.append(('EXTRA_FUNCTIONALITY', 1))
Victor Stinner8058bda2019-03-01 15:31:45 +01002428 self.add(Extension('_decimal',
2429 include_dirs=include_dirs,
2430 libraries=libraries,
2431 define_macros=define_macros,
2432 undef_macros=undef_macros,
2433 extra_compile_args=extra_compile_args,
2434 sources=sources,
2435 depends=depends))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002436
Victor Stinner5ec33a12019-03-01 16:43:28 +01002437 def detect_openssl_hashlib(self):
2438 # Detect SSL support for the socket module (via _ssl)
Christian Heimesff5be6e2018-01-20 13:19:21 +01002439 config_vars = sysconfig.get_config_vars()
2440
2441 def split_var(name, sep):
2442 # poor man's shlex, the re module is not available yet.
2443 value = config_vars.get(name)
2444 if not value:
2445 return ()
2446 # This trick works because ax_check_openssl uses --libs-only-L,
2447 # --libs-only-l, and --cflags-only-I.
2448 value = ' ' + value
2449 sep = ' ' + sep
2450 return [v.strip() for v in value.split(sep) if v.strip()]
2451
2452 openssl_includes = split_var('OPENSSL_INCLUDES', '-I')
2453 openssl_libdirs = split_var('OPENSSL_LDFLAGS', '-L')
2454 openssl_libs = split_var('OPENSSL_LIBS', '-l')
Christian Heimes32eba612021-03-19 10:29:25 +01002455 openssl_rpath = config_vars.get('OPENSSL_RPATH')
Christian Heimesff5be6e2018-01-20 13:19:21 +01002456 if not openssl_libs:
2457 # libssl and libcrypto not found
Christian Heimes8abc3f42019-04-09 18:40:12 +02002458 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002459 return None, None
2460
2461 # Find OpenSSL includes
2462 ssl_incs = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002463 'openssl/ssl.h', self.inc_dirs, openssl_includes
Christian Heimesff5be6e2018-01-20 13:19:21 +01002464 )
2465 if ssl_incs is None:
Christian Heimes8abc3f42019-04-09 18:40:12 +02002466 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002467 return None, None
2468
Christian Heimes32eba612021-03-19 10:29:25 +01002469 if openssl_rpath == 'auto':
2470 runtime_library_dirs = openssl_libdirs[:]
2471 elif not openssl_rpath:
2472 runtime_library_dirs = []
2473 else:
2474 runtime_library_dirs = [openssl_rpath]
2475
Christian Heimesbacefbf2021-03-27 18:03:54 +01002476 openssl_extension_kwargs = dict(
2477 include_dirs=openssl_includes,
2478 library_dirs=openssl_libdirs,
2479 libraries=openssl_libs,
2480 runtime_library_dirs=runtime_library_dirs,
2481 )
2482
2483 # This static linking is NOT OFFICIALLY SUPPORTED.
2484 # Requires static OpenSSL build with position-independent code. Some
2485 # features like DSO engines or external OSSL providers don't work.
2486 # Only tested on GCC and clang on X86_64.
2487 if os.environ.get("PY_UNSUPPORTED_OPENSSL_BUILD") == "static":
2488 extra_linker_args = []
2489 for lib in openssl_extension_kwargs["libraries"]:
2490 # link statically
2491 extra_linker_args.append(f"-l:lib{lib}.a")
2492 # don't export symbols
2493 extra_linker_args.append(f"-Wl,--exclude-libs,lib{lib}.a")
2494 openssl_extension_kwargs["extra_link_args"] = extra_linker_args
2495 # don't link OpenSSL shared libraries.
Christian Heimes5f879152021-04-26 15:13:34 +02002496 # include libz for OpenSSL build flavors with compression support
2497 openssl_extension_kwargs["libraries"] = ["z"]
Christian Heimesbacefbf2021-03-27 18:03:54 +01002498
Christian Heimes39258d32021-04-17 11:36:35 +02002499 self.add(
2500 Extension(
2501 '_ssl',
2502 ['_ssl.c'],
Christian Heimes666991f2021-04-26 15:01:40 +02002503 depends=[
2504 'socketmodule.h',
2505 '_ssl.h',
2506 '_ssl/debughelpers.c',
2507 '_ssl/misc.c',
2508 '_ssl/cert.c',
2509 ],
Christian Heimes39258d32021-04-17 11:36:35 +02002510 **openssl_extension_kwargs
Christian Heimesc7f70692019-05-31 11:44:05 +02002511 )
Christian Heimes39258d32021-04-17 11:36:35 +02002512 )
Christian Heimesbacefbf2021-03-27 18:03:54 +01002513 self.add(
2514 Extension(
2515 '_hashlib',
2516 ['_hashopenssl.c'],
2517 depends=['hashlib.h'],
2518 **openssl_extension_kwargs,
2519 )
2520 )
Christian Heimesff5be6e2018-01-20 13:19:21 +01002521
xdegaye2ee077f2019-04-09 17:20:08 +02002522 def detect_hash_builtins(self):
Christian Heimes9b60e552020-05-15 23:54:53 +02002523 # By default we always compile these even when OpenSSL is available
2524 # (issue #14693). It's harmless and the object code is tiny
2525 # (40-50 KiB per module, only loaded when actually used). Modules can
2526 # be disabled via the --with-builtin-hashlib-hashes configure flag.
2527 supported = {"md5", "sha1", "sha256", "sha512", "sha3", "blake2"}
Victor Stinner5ec33a12019-03-01 16:43:28 +01002528
Christian Heimes9b60e552020-05-15 23:54:53 +02002529 configured = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
2530 configured = configured.strip('"').lower()
2531 configured = {
2532 m.strip() for m in configured.split(",")
2533 }
Victor Stinner5ec33a12019-03-01 16:43:28 +01002534
Christian Heimes9b60e552020-05-15 23:54:53 +02002535 self.disabled_configure.extend(
2536 sorted(supported.difference(configured))
2537 )
Victor Stinner5ec33a12019-03-01 16:43:28 +01002538
Christian Heimes9b60e552020-05-15 23:54:53 +02002539 if "sha256" in configured:
2540 self.add(Extension(
2541 '_sha256', ['sha256module.c'],
2542 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2543 depends=['hashlib.h']
2544 ))
2545
2546 if "sha512" in configured:
2547 self.add(Extension(
2548 '_sha512', ['sha512module.c'],
2549 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2550 depends=['hashlib.h']
2551 ))
2552
2553 if "md5" in configured:
2554 self.add(Extension(
2555 '_md5', ['md5module.c'],
2556 depends=['hashlib.h']
2557 ))
2558
2559 if "sha1" in configured:
2560 self.add(Extension(
2561 '_sha1', ['sha1module.c'],
2562 depends=['hashlib.h']
2563 ))
2564
2565 if "blake2" in configured:
2566 blake2_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002567 os.path.join(escape(self.srcdir), 'Modules/_blake2/impl/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002568 )
2569 blake2_deps.append('hashlib.h')
2570 self.add(Extension(
2571 '_blake2',
2572 [
2573 '_blake2/blake2module.c',
2574 '_blake2/blake2b_impl.c',
2575 '_blake2/blake2s_impl.c'
2576 ],
2577 depends=blake2_deps
2578 ))
2579
2580 if "sha3" in configured:
2581 sha3_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002582 os.path.join(escape(self.srcdir), 'Modules/_sha3/kcp/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002583 )
2584 sha3_deps.append('hashlib.h')
2585 self.add(Extension(
2586 '_sha3',
2587 ['_sha3/sha3module.c'],
2588 depends=sha3_deps
2589 ))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002590
2591 def detect_nis(self):
Victor Stinner4cbea512019-02-28 17:48:38 +01002592 if MS_WINDOWS or CYGWIN or HOST_PLATFORM == 'qnx6':
Victor Stinner8058bda2019-03-01 15:31:45 +01002593 self.missing.append('nis')
2594 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002595
2596 libs = []
2597 library_dirs = []
2598 includes_dirs = []
2599
2600 # bpo-32521: glibc has deprecated Sun RPC for some time. Fedora 28
2601 # moved headers and libraries to libtirpc and libnsl. The headers
2602 # are in tircp and nsl sub directories.
2603 rpcsvc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002604 'rpcsvc/yp_prot.h', self.inc_dirs,
2605 [os.path.join(inc_dir, 'nsl') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002606 )
2607 rpc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002608 'rpc/rpc.h', self.inc_dirs,
2609 [os.path.join(inc_dir, 'tirpc') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002610 )
2611 if rpcsvc_inc is None or rpc_inc is None:
2612 # not found
Victor Stinner8058bda2019-03-01 15:31:45 +01002613 self.missing.append('nis')
2614 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002615 includes_dirs.extend(rpcsvc_inc)
2616 includes_dirs.extend(rpc_inc)
2617
Victor Stinner625dbf22019-03-01 15:59:39 +01002618 if self.compiler.find_library_file(self.lib_dirs, 'nsl'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002619 libs.append('nsl')
2620 else:
2621 # libnsl-devel: check for libnsl in nsl/ subdirectory
Victor Stinner625dbf22019-03-01 15:59:39 +01002622 nsl_dirs = [os.path.join(lib_dir, 'nsl') for lib_dir in self.lib_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002623 libnsl = self.compiler.find_library_file(nsl_dirs, 'nsl')
2624 if libnsl is not None:
2625 library_dirs.append(os.path.dirname(libnsl))
2626 libs.append('nsl')
2627
Victor Stinner625dbf22019-03-01 15:59:39 +01002628 if self.compiler.find_library_file(self.lib_dirs, 'tirpc'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002629 libs.append('tirpc')
2630
Victor Stinner8058bda2019-03-01 15:31:45 +01002631 self.add(Extension('nis', ['nismodule.c'],
2632 libraries=libs,
2633 library_dirs=library_dirs,
2634 include_dirs=includes_dirs))
Christian Heimes29a7df72018-01-26 23:28:46 +01002635
Christian Heimesff5be6e2018-01-20 13:19:21 +01002636
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00002637class PyBuildInstall(install):
2638 # Suppress the warning about installation into the lib_dynload
2639 # directory, which is not in sys.path when running Python during
2640 # installation:
2641 def initialize_options (self):
2642 install.initialize_options(self)
2643 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00002644
Éric Araujoe6792c12011-06-09 14:07:02 +02002645 # Customize subcommands to not install an egg-info file for Python
2646 sub_commands = [('install_lib', install.has_lib),
2647 ('install_headers', install.has_headers),
2648 ('install_scripts', install.has_scripts),
2649 ('install_data', install.has_data)]
2650
2651
Michael W. Hudson529a5052002-12-17 16:47:17 +00002652class PyBuildInstallLib(install_lib):
2653 # Do exactly what install_lib does but make sure correct access modes get
2654 # set on installed directories and files. All installed files with get
2655 # mode 644 unless they are a shared library in which case they will get
2656 # mode 755. All installed directories will get mode 755.
2657
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002658 # this is works for EXT_SUFFIX too, which ends with SHLIB_SUFFIX
2659 shlib_suffix = sysconfig.get_config_var("SHLIB_SUFFIX")
Michael W. Hudson529a5052002-12-17 16:47:17 +00002660
2661 def install(self):
2662 outfiles = install_lib.install(self)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002663 self.set_file_modes(outfiles, 0o644, 0o755)
2664 self.set_dir_modes(self.install_dir, 0o755)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002665 return outfiles
2666
2667 def set_file_modes(self, files, defaultMode, sharedLibMode):
Michael W. Hudson529a5052002-12-17 16:47:17 +00002668 if not files: return
2669
2670 for filename in files:
2671 if os.path.islink(filename): continue
2672 mode = defaultMode
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002673 if filename.endswith(self.shlib_suffix): mode = sharedLibMode
Michael W. Hudson529a5052002-12-17 16:47:17 +00002674 log.info("changing mode of %s to %o", filename, mode)
2675 if not self.dry_run: os.chmod(filename, mode)
2676
2677 def set_dir_modes(self, dirname, mode):
Amaury Forgeot d'Arc321e5332009-07-02 23:08:45 +00002678 for dirpath, dirnames, fnames in os.walk(dirname):
2679 if os.path.islink(dirpath):
2680 continue
2681 log.info("changing mode of %s to %o", dirpath, mode)
2682 if not self.dry_run: os.chmod(dirpath, mode)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002683
Victor Stinnerc991f242019-03-01 17:19:04 +01002684
Georg Brandlff52f762010-12-28 09:51:43 +00002685class PyBuildScripts(build_scripts):
2686 def copy_scripts(self):
2687 outfiles, updated_files = build_scripts.copy_scripts(self)
2688 fullversion = '-{0[0]}.{0[1]}'.format(sys.version_info)
2689 minoronly = '.{0[1]}'.format(sys.version_info)
2690 newoutfiles = []
2691 newupdated_files = []
2692 for filename in outfiles:
Brett Cannona8c34242018-04-20 14:15:40 -07002693 if filename.endswith('2to3'):
Georg Brandlff52f762010-12-28 09:51:43 +00002694 newfilename = filename + fullversion
2695 else:
2696 newfilename = filename + minoronly
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -07002697 log.info(f'renaming {filename} to {newfilename}')
Georg Brandlff52f762010-12-28 09:51:43 +00002698 os.rename(filename, newfilename)
2699 newoutfiles.append(newfilename)
2700 if filename in updated_files:
2701 newupdated_files.append(newfilename)
2702 return newoutfiles, newupdated_files
2703
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002704
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002705def main():
Victor Stinnercad80202021-01-19 23:04:49 +01002706 global LIST_MODULE_NAMES
2707
2708 if "--list-module-names" in sys.argv:
2709 LIST_MODULE_NAMES = True
2710 sys.argv.remove("--list-module-names")
2711
Victor Stinnerc991f242019-03-01 17:19:04 +01002712 set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
2713 set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
2714
2715 class DummyProcess:
2716 """Hack for parallel build"""
2717 ProcessPoolExecutor = None
2718
2719 sys.modules['concurrent.futures.process'] = DummyProcess
Paul Ganssle62972d92020-05-16 04:20:06 -04002720 validate_tzpath()
Victor Stinnerc991f242019-03-01 17:19:04 +01002721
Andrew M. Kuchling62686692001-05-21 20:48:09 +00002722 # turn off warnings when deprecated modules are imported
2723 import warnings
2724 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002725 setup(# PyPI Metadata (PEP 301)
2726 name = "Python",
2727 version = sys.version.split()[0],
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002728 url = "https://www.python.org/%d.%d" % sys.version_info[:2],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002729 maintainer = "Guido van Rossum and the Python community",
2730 maintainer_email = "python-dev@python.org",
2731 description = "A high-level object-oriented programming language",
2732 long_description = SUMMARY.strip(),
2733 license = "PSF license",
Guido van Rossumc1f779c2007-07-03 08:25:58 +00002734 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002735 platforms = ["Many"],
2736
2737 # Build info
Georg Brandlff52f762010-12-28 09:51:43 +00002738 cmdclass = {'build_ext': PyBuildExt,
2739 'build_scripts': PyBuildScripts,
2740 'install': PyBuildInstall,
2741 'install_lib': PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002742 # The struct module is defined here, because build_ext won't be
2743 # called unless there's at least one extension module defined.
Victor Stinnercdad2722021-04-22 00:52:52 +02002744 ext_modules=[Extension('_struct', ['_struct.c'],
2745 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00002746
Georg Brandlff52f762010-12-28 09:51:43 +00002747 # If you change the scripts installed here, you also need to
2748 # check the PyBuildScripts command above, and change the links
2749 # created by the bininstall target in Makefile.pre.in
Benjamin Petersondfea1922009-05-23 17:13:14 +00002750 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
Brett Cannona8c34242018-04-20 14:15:40 -07002751 "Tools/scripts/2to3"]
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002752 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00002753
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002754# --install-platlib
2755if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002756 main()