blob: 16081a069697b2ef802c57886e509efe91da8707 [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)
pxinwr32f5fdd2019-02-27 19:09:28 +080089
Victor Stinnerc991f242019-03-01 17:19:04 +010090
91SUMMARY = """
92Python is an interpreted, interactive, object-oriented programming
93language. It is often compared to Tcl, Perl, Scheme or Java.
94
95Python combines remarkable power with very clear syntax. It has
96modules, classes, exceptions, very high level dynamic data types, and
97dynamic typing. There are interfaces to many system calls and
98libraries, as well as to various windowing systems (X11, Motif, Tk,
99Mac, MFC). New built-in modules are easily written in C or C++. Python
100is also usable as an extension language for applications that need a
101programmable interface.
102
103The Python implementation is portable: it runs on many brands of UNIX,
104on Windows, DOS, Mac, Amiga... If your favorite system isn't
105listed here, it may still be supported, if there's a C compiler for
106it. Ask around on comp.lang.python -- or just try compiling Python
107yourself.
108"""
109
110CLASSIFIERS = """
111Development Status :: 6 - Mature
112License :: OSI Approved :: Python Software Foundation License
113Natural Language :: English
114Programming Language :: C
115Programming Language :: Python
116Topic :: Software Development
117"""
118
119
Victor Stinner6b982c22020-04-01 01:10:07 +0200120def run_command(cmd):
121 status = os.system(cmd)
Victor Stinner65a796e2020-04-01 18:49:29 +0200122 return os.waitstatus_to_exitcode(status)
Victor Stinner6b982c22020-04-01 01:10:07 +0200123
124
Victor Stinnerc991f242019-03-01 17:19:04 +0100125# Set common compiler and linker flags derived from the Makefile,
126# reserved for building the interpreter and the stdlib modules.
127# See bpo-21121 and bpo-35257
128def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
129 flags = sysconfig.get_config_var(compiler_flags)
130 py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
131 sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
132
133
Michael W. Hudson39230b32002-01-16 15:26:48 +0000134def add_dir_to_list(dirlist, dir):
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000135 """Add the directory 'dir' to the list 'dirlist' (after any relative
136 directories) if:
137
Michael W. Hudson39230b32002-01-16 15:26:48 +0000138 1) 'dir' is not already in 'dirlist'
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000139 2) 'dir' actually exists, and is a directory.
140 """
141 if dir is None or not os.path.isdir(dir) or dir in dirlist:
142 return
143 for i, path in enumerate(dirlist):
144 if not os.path.isabs(path):
145 dirlist.insert(i + 1, dir)
Barry Warsaw34520cd2010-11-27 20:03:03 +0000146 return
147 dirlist.insert(0, dir)
Michael W. Hudson39230b32002-01-16 15:26:48 +0000148
Victor Stinnerc991f242019-03-01 17:19:04 +0100149
xdegaye77f51392017-11-25 17:25:30 +0100150def sysroot_paths(make_vars, subdirs):
151 """Get the paths of sysroot sub-directories.
152
153 * make_vars: a sequence of names of variables of the Makefile where
154 sysroot may be set.
155 * subdirs: a sequence of names of subdirectories used as the location for
156 headers or libraries.
157 """
158
159 dirs = []
160 for var_name in make_vars:
161 var = sysconfig.get_config_var(var_name)
162 if var is not None:
163 m = re.search(r'--sysroot=([^"]\S*|"[^"]+")', var)
164 if m is not None:
165 sysroot = m.group(1).strip('"')
166 for subdir in subdirs:
167 if os.path.isabs(subdir):
168 subdir = subdir[1:]
169 path = os.path.join(sysroot, subdir)
170 if os.path.isdir(path):
171 dirs.append(path)
172 break
173 return dirs
174
Ned Deily1731d6d2020-05-18 04:32:38 -0400175
Ned Deily0288dd62019-06-03 06:34:48 -0400176MACOS_SDK_ROOT = None
Ned Deily1731d6d2020-05-18 04:32:38 -0400177MACOS_SDK_SPECIFIED = None
Victor Stinnerc991f242019-03-01 17:19:04 +0100178
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000179def macosx_sdk_root():
Ned Deily0288dd62019-06-03 06:34:48 -0400180 """Return the directory of the current macOS SDK.
181
182 If no SDK was explicitly configured, call the compiler to find which
183 include files paths are being searched by default. Use '/' if the
184 compiler is searching /usr/include (meaning system header files are
185 installed) or use the root of an SDK if that is being searched.
186 (The SDK may be supplied via Xcode or via the Command Line Tools).
187 The SDK paths used by Apple-supplied tool chains depend on the
188 setting of various variables; see the xcrun man page for more info.
Ned Deily1731d6d2020-05-18 04:32:38 -0400189 Also sets MACOS_SDK_SPECIFIED for use by macosx_sdk_specified().
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000190 """
Ned Deily1731d6d2020-05-18 04:32:38 -0400191 global MACOS_SDK_ROOT, MACOS_SDK_SPECIFIED
Ned Deily0288dd62019-06-03 06:34:48 -0400192
193 # If already called, return cached result.
194 if MACOS_SDK_ROOT:
195 return MACOS_SDK_ROOT
196
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000197 cflags = sysconfig.get_config_var('CFLAGS')
Joshua Rootb3107002020-04-22 17:44:10 +1000198 m = re.search(r'-isysroot\s*(\S+)', cflags)
Ned Deily0288dd62019-06-03 06:34:48 -0400199 if m is not None:
200 MACOS_SDK_ROOT = m.group(1)
Ned Deily29afab62020-12-04 23:02:09 -0500201 MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000202 else:
Ronald Oussoren404a7192020-11-22 06:14:25 +0100203 MACOS_SDK_ROOT = _osx_support._default_sysroot(
204 sysconfig.get_config_var('CC'))
Ned Deily29afab62020-12-04 23:02:09 -0500205 MACOS_SDK_SPECIFIED = False
Ned Deily0288dd62019-06-03 06:34:48 -0400206
207 return MACOS_SDK_ROOT
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000208
Victor Stinnerc991f242019-03-01 17:19:04 +0100209
Ned Deily1731d6d2020-05-18 04:32:38 -0400210def macosx_sdk_specified():
211 """Returns true if an SDK was explicitly configured.
212
213 True if an SDK was selected at configure time, either by specifying
214 --enable-universalsdk=(something other than no or /) or by adding a
215 -isysroot option to CFLAGS. In some cases, like when making
216 decisions about macOS Tk framework paths, we need to be able to
217 know whether the user explicitly asked to build with an SDK versus
218 the implicit use of an SDK when header files are no longer
219 installed on a running system by the Command Line Tools.
220 """
221 global MACOS_SDK_SPECIFIED
222
223 # If already called, return cached result.
224 if MACOS_SDK_SPECIFIED:
225 return MACOS_SDK_SPECIFIED
226
227 # Find the sdk root and set MACOS_SDK_SPECIFIED
228 macosx_sdk_root()
229 return MACOS_SDK_SPECIFIED
230
231
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000232def is_macosx_sdk_path(path):
233 """
Ned Batchelderd52bbde2021-05-02 19:58:57 -0700234 Returns True if 'path' can be located in a macOS SDK
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000235 """
Ned Deily2910a7b2012-07-30 02:35:58 -0700236 return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
Ned Batchelderd52bbde2021-05-02 19:58:57 -0700237 or path.startswith('/System/Library')
238 or path.startswith('/System/iOSSupport') )
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000239
Victor Stinnerc991f242019-03-01 17:19:04 +0100240
Ronald Oussoren41761932020-11-08 10:05:27 +0100241def grep_headers_for(function, headers):
242 for header in headers:
Ronald Oussoren7a27c7e2020-11-14 16:07:47 +0100243 with open(header, 'r', errors='surrogateescape') as f:
Ronald Oussoren41761932020-11-08 10:05:27 +0100244 if function in f.read():
245 return True
246 return False
247
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700248
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000249def find_file(filename, std_dirs, paths):
250 """Searches for the directory where a given file is located,
251 and returns a possibly-empty list of additional directories, or None
252 if the file couldn't be found at all.
Fredrik Lundhade711a2001-01-24 08:00:28 +0000253
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000254 'filename' is the name of a file, such as readline.h or libcrypto.a.
255 'std_dirs' is the list of standard system directories; if the
256 file is found in one of them, no additional directives are needed.
257 'paths' is a list of additional locations to check; if the file is
258 found in one of them, the resulting list will contain the directory.
259 """
Victor Stinner4cbea512019-02-28 17:48:38 +0100260 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000261 # Honor the MacOSX SDK setting when one was specified.
262 # An SDK is a directory with the same structure as a real
263 # system, but with only header files and libraries.
264 sysroot = macosx_sdk_root()
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000265
266 # Check the standard locations
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700267 for dir_ in std_dirs:
268 f = os.path.join(dir_, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000269
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700270 if MACOS and is_macosx_sdk_path(dir_):
271 f = os.path.join(sysroot, dir_[1:], filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000272
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000273 if os.path.exists(f): return []
274
275 # Check the additional directories
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700276 for dir_ in paths:
277 f = os.path.join(dir_, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000278
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700279 if MACOS and is_macosx_sdk_path(dir_):
280 f = os.path.join(sysroot, dir_[1:], filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000281
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000282 if os.path.exists(f):
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700283 return [dir_]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000284
285 # Not found anywhere
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000286 return None
287
Victor Stinnerc991f242019-03-01 17:19:04 +0100288
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000289def find_library_file(compiler, libname, std_dirs, paths):
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000290 result = compiler.find_library_file(std_dirs + paths, libname)
291 if result is None:
292 return None
Fredrik Lundhade711a2001-01-24 08:00:28 +0000293
Victor Stinner4cbea512019-02-28 17:48:38 +0100294 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000295 sysroot = macosx_sdk_root()
296
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000297 # Check whether the found file is in one of the standard directories
298 dirname = os.path.dirname(result)
299 for p in std_dirs:
300 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000301 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000302
Victor Stinner4cbea512019-02-28 17:48:38 +0100303 if MACOS and is_macosx_sdk_path(p):
Ned Deily020250f2016-02-25 00:56:38 +1100304 # Note that, as of Xcode 7, Apple SDKs may contain textual stub
305 # libraries with .tbd extensions rather than the normal .dylib
306 # shared libraries installed in /. The Apple compiler tool
307 # chain handles this transparently but it can cause problems
308 # for programs that are being built with an SDK and searching
309 # for specific libraries. Distutils find_library_file() now
310 # knows to also search for and return .tbd files. But callers
311 # of find_library_file need to keep in mind that the base filename
312 # of the returned SDK library file might have a different extension
313 # from that of the library file installed on the running system,
314 # for example:
315 # /Applications/Xcode.app/Contents/Developer/Platforms/
316 # MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/
317 # usr/lib/libedit.tbd
318 # vs
319 # /usr/lib/libedit.dylib
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000320 if os.path.join(sysroot, p[1:]) == dirname:
321 return [ ]
322
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000323 if p == dirname:
324 return [ ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000325
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000326 # Otherwise, it must have been in one of the additional directories,
327 # so we have to figure out which one.
328 for p in paths:
329 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000330 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000331
Victor Stinner4cbea512019-02-28 17:48:38 +0100332 if MACOS and is_macosx_sdk_path(p):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000333 if os.path.join(sysroot, p[1:]) == dirname:
334 return [ p ]
335
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000336 if p == dirname:
337 return [p]
338 else:
339 assert False, "Internal error: Path not found in std_dirs or paths"
Tim Peters2c60f7a2003-01-29 03:49:43 +0000340
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700341
Paul Ganssle62972d92020-05-16 04:20:06 -0400342def validate_tzpath():
343 base_tzpath = sysconfig.get_config_var('TZPATH')
344 if not base_tzpath:
345 return
346
347 tzpaths = base_tzpath.split(os.pathsep)
348 bad_paths = [tzpath for tzpath in tzpaths if not os.path.isabs(tzpath)]
349 if bad_paths:
350 raise ValueError('TZPATH must contain only absolute paths, '
351 + f'found:\n{tzpaths!r}\nwith invalid paths:\n'
352 + f'{bad_paths!r}')
Victor Stinnerc991f242019-03-01 17:19:04 +0100353
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700354
Jack Jansen144ebcc2001-08-05 22:31:19 +0000355def find_module_file(module, dirlist):
356 """Find a module in a set of possible folders. If it is not found
357 return the unadorned filename"""
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700358 dirs = find_file(module, [], dirlist)
359 if not dirs:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000360 return module
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700361 if len(dirs) > 1:
362 log.info(f"WARNING: multiple copies of {module} found")
363 return os.path.join(dirs[0], module)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000364
Victor Stinnerc991f242019-03-01 17:19:04 +0100365
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000366class PyBuildExt(build_ext):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000367
Guido van Rossumd8faa362007-04-27 19:54:29 +0000368 def __init__(self, dist):
369 build_ext.__init__(self, dist)
Victor Stinner625dbf22019-03-01 15:59:39 +0100370 self.srcdir = None
371 self.lib_dirs = None
372 self.inc_dirs = None
Victor Stinner5ec33a12019-03-01 16:43:28 +0100373 self.config_h_vars = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000374 self.failed = []
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400375 self.failed_on_import = []
Victor Stinner8058bda2019-03-01 15:31:45 +0100376 self.missing = []
Christian Heimes9b60e552020-05-15 23:54:53 +0200377 self.disabled_configure = []
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200378 if '-j' in os.environ.get('MAKEFLAGS', ''):
379 self.parallel = True
Guido van Rossumd8faa362007-04-27 19:54:29 +0000380
Victor Stinner8058bda2019-03-01 15:31:45 +0100381 def add(self, ext):
382 self.extensions.append(ext)
383
Victor Stinner00c77ae2020-03-04 18:44:49 +0100384 def set_srcdir(self):
Victor Stinner625dbf22019-03-01 15:59:39 +0100385 self.srcdir = sysconfig.get_config_var('srcdir')
386 if not self.srcdir:
387 # Maybe running on Windows but not using CYGWIN?
388 raise ValueError("No source directory; cannot proceed.")
389 self.srcdir = os.path.abspath(self.srcdir)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000390
Victor Stinner00c77ae2020-03-04 18:44:49 +0100391 def remove_disabled(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000392 # Remove modules that are present on the disabled list
Christian Heimes679db4a2008-01-18 09:56:22 +0000393 extensions = [ext for ext in self.extensions
Victor Stinner4cbea512019-02-28 17:48:38 +0100394 if ext.name not in DISABLED_MODULE_LIST]
Christian Heimes679db4a2008-01-18 09:56:22 +0000395 # move ctypes to the end, it depends on other modules
396 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
397 if "_ctypes" in ext_map:
398 ctypes = extensions.pop(ext_map["_ctypes"])
399 extensions.append(ctypes)
400 self.extensions = extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +0000401
Victor Stinner00c77ae2020-03-04 18:44:49 +0100402 def update_sources_depends(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000403 # Fix up the autodetected modules, prefixing all the source files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000404 # with Modules/.
Victor Stinner625dbf22019-03-01 15:59:39 +0100405 moddirlist = [os.path.join(self.srcdir, 'Modules')]
Michael W. Hudson5b109102002-01-23 15:04:41 +0000406
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000407 # Fix up the paths for scripts, too
Victor Stinner625dbf22019-03-01 15:59:39 +0100408 self.distribution.scripts = [os.path.join(self.srcdir, filename)
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000409 for filename in self.distribution.scripts]
410
Christian Heimesaf98da12008-01-27 15:18:18 +0000411 # Python header files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000412 headers = [sysconfig.get_config_h_filename()]
Serhiy Storchaka93558682020-06-20 11:10:31 +0300413 headers += glob(os.path.join(escape(sysconfig.get_path('include')), "*.h"))
Christian Heimesaf98da12008-01-27 15:18:18 +0000414
Xavier de Gaye84968b72016-10-29 16:57:20 +0200415 for ext in self.extensions:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000416 ext.sources = [ find_module_file(filename, moddirlist)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000417 for filename in ext.sources ]
Jeremy Hylton340043e2002-06-13 17:38:11 +0000418 if ext.depends is not None:
Neil Schemenauer014bf282009-02-05 16:35:45 +0000419 ext.depends = [find_module_file(filename, moddirlist)
Jeremy Hylton340043e2002-06-13 17:38:11 +0000420 for filename in ext.depends]
Christian Heimesaf98da12008-01-27 15:18:18 +0000421 else:
422 ext.depends = []
423 # re-compile extensions if a header file has been changed
424 ext.depends.extend(headers)
425
Victor Stinner00c77ae2020-03-04 18:44:49 +0100426 def remove_configured_extensions(self):
427 # The sysconfig variables built by makesetup that list the already
428 # built modules and the disabled modules as configured by the Setup
429 # files.
430 sysconf_built = sysconfig.get_config_var('MODBUILT_NAMES').split()
431 sysconf_dis = sysconfig.get_config_var('MODDISABLED_NAMES').split()
432
433 mods_built = []
434 mods_disabled = []
435 for ext in self.extensions:
xdegayec0364fc2017-05-27 18:25:03 +0200436 # If a module has already been built or has been disabled in the
437 # Setup files, don't build it here.
438 if ext.name in sysconf_built:
439 mods_built.append(ext)
440 if ext.name in sysconf_dis:
441 mods_disabled.append(ext)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000442
xdegayec0364fc2017-05-27 18:25:03 +0200443 mods_configured = mods_built + mods_disabled
444 if mods_configured:
Xavier de Gaye84968b72016-10-29 16:57:20 +0200445 self.extensions = [x for x in self.extensions if x not in
xdegayec0364fc2017-05-27 18:25:03 +0200446 mods_configured]
447 # Remove the shared libraries built by a previous build.
448 for ext in mods_configured:
449 fullpath = self.get_ext_fullpath(ext.name)
450 if os.path.exists(fullpath):
451 os.unlink(fullpath)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000452
Victor Stinner00c77ae2020-03-04 18:44:49 +0100453 return (mods_built, mods_disabled)
454
455 def set_compiler_executables(self):
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000456 # When you run "make CC=altcc" or something similar, you really want
457 # those environment variables passed into the setup.py phase. Here's
458 # a small set of useful ones.
459 compiler = os.environ.get('CC')
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000460 args = {}
461 # unfortunately, distutils doesn't let us provide separate C and C++
462 # compilers
463 if compiler is not None:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000464 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
465 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
Tarek Ziadé36797272010-07-22 12:50:05 +0000466 self.compiler.set_executables(**args)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000467
Victor Stinner00c77ae2020-03-04 18:44:49 +0100468 def build_extensions(self):
469 self.set_srcdir()
470
471 # Detect which modules should be compiled
472 self.detect_modules()
473
Victor Stinnercad80202021-01-19 23:04:49 +0100474 if not LIST_MODULE_NAMES:
475 self.remove_disabled()
Victor Stinner00c77ae2020-03-04 18:44:49 +0100476
477 self.update_sources_depends()
478 mods_built, mods_disabled = self.remove_configured_extensions()
479 self.set_compiler_executables()
480
Victor Stinnercad80202021-01-19 23:04:49 +0100481 if LIST_MODULE_NAMES:
482 for ext in self.extensions:
483 print(ext.name)
484 for name in self.missing:
485 print(name)
486 return
487
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000488 build_ext.build_extensions(self)
489
Victor Stinner1ec63b62020-03-04 14:50:19 +0100490 if SUBPROCESS_BOOTSTRAP:
491 # Drop our custom subprocess module:
492 # use the newly built subprocess module
493 del sys.modules['subprocess']
494
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200495 for ext in self.extensions:
496 self.check_extension_import(ext)
497
Victor Stinner00c77ae2020-03-04 18:44:49 +0100498 self.summary(mods_built, mods_disabled)
499
500 def summary(self, mods_built, mods_disabled):
Berker Peksag1d82a9c2014-10-01 05:11:13 +0300501 longest = max([len(e.name) for e in self.extensions], default=0)
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400502 if self.failed or self.failed_on_import:
503 all_failed = self.failed + self.failed_on_import
504 longest = max(longest, max([len(name) for name in all_failed]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000505
506 def print_three_column(lst):
507 lst.sort(key=str.lower)
508 # guarantee zip() doesn't drop anything
509 while len(lst) % 3:
510 lst.append("")
511 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
512 print("%-*s %-*s %-*s" % (longest, e, longest, f,
513 longest, g))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000514
Victor Stinner8058bda2019-03-01 15:31:45 +0100515 if self.missing:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000516 print()
Brett Cannonae95b4f2013-07-12 11:30:32 -0400517 print("Python build finished successfully!")
518 print("The necessary bits to build these optional modules were not "
519 "found:")
Victor Stinner8058bda2019-03-01 15:31:45 +0100520 print_three_column(self.missing)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000521 print("To find the necessary bits, look in setup.py in"
522 " detect_modules() for the module's name.")
523 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524
xdegayec0364fc2017-05-27 18:25:03 +0200525 if mods_built:
526 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200527 print("The following modules found by detect_modules() in"
528 " setup.py, have been")
529 print("built by the Makefile instead, as configured by the"
530 " Setup files:")
xdegayec0364fc2017-05-27 18:25:03 +0200531 print_three_column([ext.name for ext in mods_built])
532 print()
533
534 if mods_disabled:
535 print()
536 print("The following modules found by detect_modules() in"
537 " setup.py have not")
538 print("been built, they are *disabled* in the Setup files:")
539 print_three_column([ext.name for ext in mods_disabled])
540 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200541
Christian Heimes9b60e552020-05-15 23:54:53 +0200542 if self.disabled_configure:
543 print()
544 print("The following modules found by detect_modules() in"
545 " setup.py have not")
546 print("been built, they are *disabled* by configure:")
547 print_three_column(self.disabled_configure)
548 print()
549
Guido van Rossumd8faa362007-04-27 19:54:29 +0000550 if self.failed:
551 failed = self.failed[:]
552 print()
553 print("Failed to build these modules:")
554 print_three_column(failed)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000555 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000556
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400557 if self.failed_on_import:
558 failed = self.failed_on_import[:]
559 print()
560 print("Following modules built successfully"
561 " but were removed because they could not be imported:")
562 print_three_column(failed)
563 print()
564
Christian Heimes61d478c2018-01-27 15:51:38 +0100565 if any('_ssl' in l
Victor Stinner8058bda2019-03-01 15:31:45 +0100566 for l in (self.missing, self.failed, self.failed_on_import)):
Christian Heimes61d478c2018-01-27 15:51:38 +0100567 print()
568 print("Could not build the ssl module!")
Christian Heimes39258d32021-04-17 11:36:35 +0200569 print("Python requires a OpenSSL 1.1.1 or newer")
Christian Heimes32eba612021-03-19 10:29:25 +0100570 if sysconfig.get_config_var("OPENSSL_LDFLAGS"):
571 print("Custom linker flags may require --with-openssl-rpath=auto")
Christian Heimes61d478c2018-01-27 15:51:38 +0100572 print()
573
Pablo Galindo Salgadoc2e0b132021-07-30 16:14:28 +0100574 if os.environ.get("PYTHONSTRICTEXTENSIONBUILD") and (self.failed or self.failed_on_import):
575 raise RuntimeError("Failed to build some stdlib modules")
576
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000577 def build_extension(self, ext):
578
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 if ext.name == '_ctypes':
580 if not self.configure_ctypes(ext):
Zachary Waref40d4dd2016-09-17 01:25:24 -0500581 self.failed.append(ext.name)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000582 return
583
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000584 try:
585 build_ext.build_extension(self, ext)
Guido van Rossumb940e112007-01-10 16:19:56 +0000586 except (CCompilerError, DistutilsError) as why:
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000587 self.announce('WARNING: building of extension "%s" failed: %s' %
Victor Stinner625dbf22019-03-01 15:59:39 +0100588 (ext.name, why))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000589 self.failed.append(ext.name)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000590 return
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200591
592 def check_extension_import(self, ext):
593 # Don't try to import an extension that has failed to compile
594 if ext.name in self.failed:
595 self.announce(
596 'WARNING: skipping import check for failed build "%s"' %
597 ext.name, level=1)
598 return
599
Jack Jansenf49c6f92001-11-01 14:44:15 +0000600 # Workaround for Mac OS X: The Carbon-based modules cannot be
601 # reliably imported into a command-line Python
602 if 'Carbon' in ext.extra_link_args:
Michael W. Hudson5b109102002-01-23 15:04:41 +0000603 self.announce(
604 'WARNING: skipping import check for Carbon-based "%s"' %
605 ext.name)
606 return
Georg Brandlfcaf9102008-07-16 02:17:56 +0000607
Victor Stinner4cbea512019-02-28 17:48:38 +0100608 if MACOS and (
Benjamin Petersonfc576352008-07-16 02:39:02 +0000609 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
Georg Brandlfcaf9102008-07-16 02:17:56 +0000610 # Don't bother doing an import check when an extension was
611 # build with an explicit '-arch' flag on OSX. That's currently
612 # only used to build 32-bit only extensions in a 4-way
613 # universal build and loading 32-bit code into a 64-bit
614 # process will fail.
615 self.announce(
616 'WARNING: skipping import check for "%s"' %
617 ext.name)
618 return
619
Jason Tishler24cf7762002-05-22 16:46:15 +0000620 # Workaround for Cygwin: Cygwin currently has fork issues when many
621 # modules have been imported
Victor Stinner4cbea512019-02-28 17:48:38 +0100622 if CYGWIN:
Jason Tishler24cf7762002-05-22 16:46:15 +0000623 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
624 % ext.name)
625 return
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000626 ext_filename = os.path.join(
627 self.build_lib,
628 self.get_ext_filename(self.get_ext_fullname(ext.name)))
Guido van Rossumc3fee692008-07-17 16:23:53 +0000629
630 # If the build directory didn't exist when setup.py was
631 # started, sys.path_importer_cache has a negative result
632 # cached. Clear that cache before trying to import.
633 sys.path_importer_cache.clear()
634
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200635 # Don't try to load extensions for cross builds
Victor Stinner4cbea512019-02-28 17:48:38 +0100636 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200637 return
638
Brett Cannonca5ff3a2013-06-15 17:52:59 -0400639 loader = importlib.machinery.ExtensionFileLoader(ext.name, ext_filename)
Eric Snow335e14d2014-01-04 15:09:28 -0700640 spec = importlib.util.spec_from_file_location(ext.name, ext_filename,
641 loader=loader)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000642 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400643 importlib._bootstrap._load(spec)
Guido van Rossumb940e112007-01-10 16:19:56 +0000644 except ImportError as why:
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400645 self.failed_on_import.append(ext.name)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000646 self.announce('*** WARNING: renaming "%s" since importing it'
647 ' failed: %s' % (ext.name, why), level=3)
648 assert not self.inplace
649 basename, tail = os.path.splitext(ext_filename)
650 newname = basename + "_failed" + tail
651 if os.path.exists(newname):
652 os.remove(newname)
653 os.rename(ext_filename, newname)
654
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000655 except:
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000656 exc_type, why, tb = sys.exc_info()
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000657 self.announce('*** WARNING: importing extension "%s" '
658 'failed with %s: %s' % (ext.name, exc_type, why),
659 level=3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000660 self.failed.append(ext.name)
Fred Drake9028d0a2001-12-06 22:59:54 +0000661
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400662 def add_multiarch_paths(self):
663 # Debian/Ubuntu multiarch support.
664 # https://wiki.ubuntu.com/MultiarchSpec
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200665 cc = sysconfig.get_config_var('CC')
666 tmpfile = os.path.join(self.build_temp, 'multiarch')
667 if not os.path.exists(self.build_temp):
668 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200669 ret = run_command(
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200670 '%s -print-multiarch > %s 2> /dev/null' % (cc, tmpfile))
671 multiarch_path_component = ''
672 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200673 if ret == 0:
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200674 with open(tmpfile) as fp:
675 multiarch_path_component = fp.readline().strip()
676 finally:
677 os.unlink(tmpfile)
678
679 if multiarch_path_component != '':
680 add_dir_to_list(self.compiler.library_dirs,
681 '/usr/lib/' + multiarch_path_component)
682 add_dir_to_list(self.compiler.include_dirs,
683 '/usr/include/' + multiarch_path_component)
684 return
685
Barry Warsaw88e19452011-04-07 10:40:36 -0400686 if not find_executable('dpkg-architecture'):
687 return
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200688 opt = ''
Victor Stinner4cbea512019-02-28 17:48:38 +0100689 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200690 opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400691 tmpfile = os.path.join(self.build_temp, 'multiarch')
692 if not os.path.exists(self.build_temp):
693 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200694 ret = run_command(
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200695 'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
696 (opt, tmpfile))
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400697 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200698 if ret == 0:
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400699 with open(tmpfile) as fp:
700 multiarch_path_component = fp.readline().strip()
701 add_dir_to_list(self.compiler.library_dirs,
702 '/usr/lib/' + multiarch_path_component)
703 add_dir_to_list(self.compiler.include_dirs,
704 '/usr/include/' + multiarch_path_component)
705 finally:
706 os.unlink(tmpfile)
707
pxinwr5e45f1c2021-01-22 08:55:52 +0800708 def add_wrcc_search_dirs(self):
709 # add library search path by wr-cc, the compiler wrapper
710
711 def convert_mixed_path(path):
712 # convert path like C:\folder1\folder2/folder3/folder4
713 # to msys style /c/folder1/folder2/folder3/folder4
714 drive = path[0].lower()
715 left = path[2:].replace("\\", "/")
716 return "/" + drive + left
717
718 def add_search_path(line):
719 # On Windows building machine, VxWorks does
720 # cross builds under msys2 environment.
721 pathsep = (";" if sys.platform == "msys" else ":")
722 for d in line.strip().split("=")[1].split(pathsep):
723 d = d.strip()
724 if sys.platform == "msys":
725 # On Windows building machine, compiler
726 # returns mixed style path like:
727 # C:\folder1\folder2/folder3/folder4
728 d = convert_mixed_path(d)
729 d = os.path.normpath(d)
730 add_dir_to_list(self.compiler.library_dirs, d)
731
732 cc = sysconfig.get_config_var('CC')
733 tmpfile = os.path.join(self.build_temp, 'wrccpaths')
734 os.makedirs(self.build_temp, exist_ok=True)
735 try:
736 ret = run_command('%s --print-search-dirs >%s' % (cc, tmpfile))
737 if ret:
738 return
739 with open(tmpfile) as fp:
740 # Parse paths in libraries line. The line is like:
741 # On Linux, "libraries: = path1:path2:path3"
742 # On Windows, "libraries: = path1;path2;path3"
743 for line in fp:
744 if not line.startswith("libraries"):
745 continue
746 add_search_path(line)
747 finally:
748 try:
749 os.unlink(tmpfile)
750 except OSError:
751 pass
752
pxinwr32f5fdd2019-02-27 19:09:28 +0800753 def add_cross_compiling_paths(self):
754 cc = sysconfig.get_config_var('CC')
755 tmpfile = os.path.join(self.build_temp, 'ccpaths')
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200756 if not os.path.exists(self.build_temp):
757 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200758 ret = run_command('%s -E -v - </dev/null 2>%s 1>/dev/null' % (cc, tmpfile))
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200759 is_gcc = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800760 is_clang = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200761 in_incdirs = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200762 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200763 if ret == 0:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200764 with open(tmpfile) as fp:
765 for line in fp.readlines():
766 if line.startswith("gcc version"):
767 is_gcc = True
pxinwr32f5fdd2019-02-27 19:09:28 +0800768 elif line.startswith("clang version"):
769 is_clang = True
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200770 elif line.startswith("#include <...>"):
771 in_incdirs = True
772 elif line.startswith("End of search list"):
773 in_incdirs = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800774 elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"):
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200775 for d in line.strip().split("=")[1].split(":"):
776 d = os.path.normpath(d)
777 if '/gcc/' not in d:
778 add_dir_to_list(self.compiler.library_dirs,
779 d)
pxinwr32f5fdd2019-02-27 19:09:28 +0800780 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 +0200781 add_dir_to_list(self.compiler.include_dirs,
782 line.strip())
783 finally:
784 os.unlink(tmpfile)
785
pxinwr5e45f1c2021-01-22 08:55:52 +0800786 if VXWORKS:
787 self.add_wrcc_search_dirs()
788
Victor Stinnercfe172d2019-03-01 18:21:49 +0100789 def add_ldflags_cppflags(self):
Brett Cannon516592f2004-12-07 00:42:59 +0000790 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000791 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000792 # We must get the values from the Makefile and not the environment
793 # directly since an inconsistently reproducible issue comes up where
794 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000795 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000796 for env_var, arg_name, dir_list in (
Tarek Ziadé36797272010-07-22 12:50:05 +0000797 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
798 ('LDFLAGS', '-L', self.compiler.library_dirs),
799 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000800 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000801 if env_val:
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800802 parser = argparse.ArgumentParser()
803 parser.add_argument(arg_name, dest="dirs", action="append")
804 options, _ = parser.parse_known_args(env_val.split())
Brett Cannon44837712005-01-02 21:54:07 +0000805 if options.dirs:
Christian Heimes292d3512008-02-03 16:51:08 +0000806 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000807 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000808
Victor Stinnercfe172d2019-03-01 18:21:49 +0100809 def configure_compiler(self):
810 # Ensure that /usr/local is always used, but the local build
811 # directories (i.e. '.' and 'Include') must be first. See issue
812 # 10520.
813 if not CROSS_COMPILING:
814 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
815 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
816 # only change this for cross builds for 3.3, issues on Mageia
817 if CROSS_COMPILING:
818 self.add_cross_compiling_paths()
819 self.add_multiarch_paths()
820 self.add_ldflags_cppflags()
821
Victor Stinner5ec33a12019-03-01 16:43:28 +0100822 def init_inc_lib_dirs(self):
Victor Stinner4cbea512019-02-28 17:48:38 +0100823 if (not CROSS_COMPILING and
Xavier de Gaye1351c312016-12-14 11:14:33 +0100824 os.path.normpath(sys.base_prefix) != '/usr' and
825 not sysconfig.get_config_var('PYTHONFRAMEWORK')):
Ronald Oussorenf3500e12010-10-20 13:10:12 +0000826 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
827 # (PYTHONFRAMEWORK is set) to avoid # linking problems when
828 # building a framework with different architectures than
829 # the one that is currently installed (issue #7473)
Tarek Ziadé36797272010-07-22 12:50:05 +0000830 add_dir_to_list(self.compiler.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000831 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000832 add_dir_to_list(self.compiler.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000833 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000834
xdegaye77f51392017-11-25 17:25:30 +0100835 system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
836 system_include_dirs = ['/usr/include']
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000837 # lib_dirs and inc_dirs are used to search for files;
838 # if a file is found in one of those directories, it can
839 # be assumed that no additional -I,-L directives are needed.
Victor Stinner4cbea512019-02-28 17:48:38 +0100840 if not CROSS_COMPILING:
Victor Stinner625dbf22019-03-01 15:59:39 +0100841 self.lib_dirs = self.compiler.library_dirs + system_lib_dirs
842 self.inc_dirs = self.compiler.include_dirs + system_include_dirs
Christian Heimesf19529c2012-12-12 12:41:00 +0100843 else:
xdegaye77f51392017-11-25 17:25:30 +0100844 # Add the sysroot paths. 'sysroot' is a compiler option used to
845 # set the logical path of the standard system headers and
846 # libraries.
Victor Stinner625dbf22019-03-01 15:59:39 +0100847 self.lib_dirs = (self.compiler.library_dirs +
848 sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
849 self.inc_dirs = (self.compiler.include_dirs +
850 sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
851 system_include_dirs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000852
Brett Cannon4454a1f2005-04-15 20:32:39 +0000853 config_h = sysconfig.get_config_h_filename()
Brett Cannon9f5db072010-10-29 20:19:27 +0000854 with open(config_h) as file:
Victor Stinner5ec33a12019-03-01 16:43:28 +0100855 self.config_h_vars = sysconfig.parse_config_h(file)
Brett Cannon4454a1f2005-04-15 20:32:39 +0000856
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000857 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
Victor Stinner4cbea512019-02-28 17:48:38 +0100858 if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']:
Victor Stinner625dbf22019-03-01 15:59:39 +0100859 self.lib_dirs += ['/usr/ccs/lib']
Skip Montanaro22e00c42003-05-06 20:43:34 +0000860
Charles-François Natali5739e102012-04-12 19:07:25 +0200861 # HP-UX11iv3 keeps files in lib/hpux folders.
Victor Stinner4cbea512019-02-28 17:48:38 +0100862 if HOST_PLATFORM == 'hp-ux11':
Victor Stinner625dbf22019-03-01 15:59:39 +0100863 self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
Charles-François Natali5739e102012-04-12 19:07:25 +0200864
Victor Stinner4cbea512019-02-28 17:48:38 +0100865 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866 # This should work on any unixy platform ;-)
867 # If the user has bothered specifying additional -I and -L flags
868 # in OPT and LDFLAGS we might as well use them here.
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000869 #
870 # NOTE: using shlex.split would technically be more correct, but
871 # also gives a bootstrap problem. Let's hope nobody uses
872 # directories with whitespace in the name to store libraries.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000873 cflags, ldflags = sysconfig.get_config_vars(
874 'CFLAGS', 'LDFLAGS')
875 for item in cflags.split():
876 if item.startswith('-I'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100877 self.inc_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878
879 for item in ldflags.split():
880 if item.startswith('-L'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100881 self.lib_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000882
Victor Stinner5ec33a12019-03-01 16:43:28 +0100883 def detect_simple_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000884 #
885 # The following modules are all pretty straightforward, and compile
886 # on pretty much any POSIXish platform.
887 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000888
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000889 # array objects
Victor Stinnercdad2722021-04-22 00:52:52 +0200890 self.add(Extension('array', ['arraymodule.c'],
891 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Martin Panterc9deece2016-02-03 05:19:44 +0000892
Yury Selivanovf23746a2018-01-22 19:11:18 -0500893 # Context Variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100894 self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
Yury Selivanovf23746a2018-01-22 19:11:18 -0500895
Martin Panterc9deece2016-02-03 05:19:44 +0000896 shared_math = 'Modules/_math.o'
Victor Stinnercfe172d2019-03-01 18:21:49 +0100897
898 # math library functions, e.g. sin()
899 self.add(Extension('math', ['mathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100900 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100901 extra_objects=[shared_math],
902 depends=['_math.h', shared_math],
903 libraries=['m']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100904
905 # complex math library functions
906 self.add(Extension('cmath', ['cmathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100907 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100908 extra_objects=[shared_math],
909 depends=['_math.h', shared_math],
910 libraries=['m']))
Victor Stinnere0be4232011-10-25 13:06:09 +0200911
912 # time libraries: librt may be needed for clock_gettime()
913 time_libs = []
914 lib = sysconfig.get_config_var('TIMEMODULE_LIB')
915 if lib:
916 time_libs.append(lib)
917
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000918 # time operations and variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100919 self.add(Extension('time', ['timemodule.c'],
920 libraries=time_libs))
Benjamin Peterson8acaa312017-11-12 20:53:39 -0800921 # libm is needed by delta_new() that uses round() and by accum() that
922 # uses modf().
Victor Stinner8058bda2019-03-01 15:31:45 +0100923 self.add(Extension('_datetime', ['_datetimemodule.c'],
Victor Stinner04fc4f22020-06-16 01:28:07 +0200924 libraries=['m'],
925 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Paul Ganssle62972d92020-05-16 04:20:06 -0400926 # zoneinfo module
Victor Stinner37834132020-10-27 17:12:53 +0100927 self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
928 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000929 # random number generator implemented in C
Victor Stinner9f5fe792020-04-17 19:05:35 +0200930 self.add(Extension("_random", ["_randommodule.c"],
931 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Raymond Hettinger0c410272004-01-05 10:13:35 +0000932 # bisect
Victor Stinner8058bda2019-03-01 15:31:45 +0100933 self.add(Extension("_bisect", ["_bisectmodule.c"]))
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000934 # heapq
Victor Stinnerc45dbe932020-06-22 17:39:32 +0200935 self.add(Extension("_heapq", ["_heapqmodule.c"],
936 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000937 # C-optimized pickle replacement
Victor Stinner5c75f372019-04-17 23:02:26 +0200938 self.add(Extension("_pickle", ["_pickle.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200939 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimes90540002008-05-08 14:29:10 +0000940 # _json speedups
Victor Stinner8058bda2019-03-01 15:31:45 +0100941 self.add(Extension("_json", ["_json.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200942 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100943
Fred Drake0e474a82007-10-11 18:01:43 +0000944 # profiler (_lsprof is for cProfile.py)
Victor Stinner8058bda2019-03-01 15:31:45 +0100945 self.add(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000946 # static Unicode character database
Victor Stinner8058bda2019-03-01 15:31:45 +0100947 self.add(Extension('unicodedata', ['unicodedata.c'],
Victor Stinner47e1afd2020-10-26 16:43:47 +0100948 depends=['unicodedata_db.h', 'unicodename_db.h'],
949 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Larry Hastings3a907972013-11-23 14:49:22 -0800950 # _opcode module
Victor Stinner8058bda2019-03-01 15:31:45 +0100951 self.add(Extension('_opcode', ['_opcode.c']))
INADA Naoki9f2ce252016-10-15 15:39:19 +0900952 # asyncio speedups
Chris Jerdonekda742ba2020-05-17 22:47:31 -0700953 self.add(Extension("_asyncio", ["_asynciomodule.c"],
954 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000955 # _abc speedups
Victor Stinnercdad2722021-04-22 00:52:52 +0200956 self.add(Extension("_abc", ["_abc.c"],
957 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Antoine Pitrou94e16962018-01-16 00:27:16 +0100958 # _queue module
Victor Stinnercdad2722021-04-22 00:52:52 +0200959 self.add(Extension("_queue", ["_queuemodule.c"],
960 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Dong-hee Na0a18ee42019-08-24 07:20:30 +0900961 # _statistics module
962 self.add(Extension("_statistics", ["_statisticsmodule.c"]))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000963
964 # Modules with some UNIX dependencies -- on by default:
965 # (If you have a really backward UNIX, select and socket may not be
966 # supported...)
967
968 # fcntl(2) and ioctl(2)
Antoine Pitroua3000072010-09-07 14:52:42 +0000969 libs = []
Victor Stinner5ec33a12019-03-01 16:43:28 +0100970 if (self.config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
Antoine Pitroua3000072010-09-07 14:52:42 +0000971 # May be necessary on AIX for flock function
972 libs = ['bsd']
Victor Stinner8058bda2019-03-01 15:31:45 +0100973 self.add(Extension('fcntl', ['fcntlmodule.c'],
974 libraries=libs))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000975 # pwd(3)
Victor Stinner8058bda2019-03-01 15:31:45 +0100976 self.add(Extension('pwd', ['pwdmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000977 # grp(3)
pxinwr32f5fdd2019-02-27 19:09:28 +0800978 if not VXWORKS:
Victor Stinner8058bda2019-03-01 15:31:45 +0100979 self.add(Extension('grp', ['grpmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000980 # spwd, shadow passwords
Victor Stinner5ec33a12019-03-01 16:43:28 +0100981 if (self.config_h_vars.get('HAVE_GETSPNAM', False) or
982 self.config_h_vars.get('HAVE_GETSPENT', False)):
Victor Stinner8058bda2019-03-01 15:31:45 +0100983 self.add(Extension('spwd', ['spwdmodule.c']))
Michael Felt08970cb2019-06-21 15:58:00 +0200984 # AIX has shadow passwords, but access is not via getspent(), etc.
985 # module support is not expected so it not 'missing'
986 elif not AIX:
Victor Stinner8058bda2019-03-01 15:31:45 +0100987 self.missing.append('spwd')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000989 # select(2); not on ancient System V
Victor Stinner8058bda2019-03-01 15:31:45 +0100990 self.add(Extension('select', ['selectmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000991
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000992 # Memory-mapped files (also works on Win32).
Victor Stinner8058bda2019-03-01 15:31:45 +0100993 self.add(Extension('mmap', ['mmapmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000994
Andrew M. Kuchling57269d02004-08-31 13:37:25 +0000995 # Lance Ellinghaus's syslog module
Ronald Oussoren94f25282010-05-05 19:11:21 +0000996 # syslog daemon interface
Victor Stinner8058bda2019-03-01 15:31:45 +0100997 self.add(Extension('syslog', ['syslogmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000998
Eric Snow7f8bfc92018-01-29 18:23:44 -0700999 # Python interface to subinterpreter C-API.
Eric Snowc11183c2019-03-15 16:35:46 -06001000 self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
Eric Snow7f8bfc92018-01-29 18:23:44 -07001001
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001002 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +00001003 # Here ends the simple stuff. From here on, modules need certain
1004 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001005 #
1006
1007 # Multimedia modules
1008 # These don't work for 64-bit platforms!!!
1009 # These represent audio samples or images as strings:
Victor Stinnerdef80722016-04-19 15:58:11 +02001010 #
Neal Norwitz5e4a3b82004-07-19 16:55:07 +00001011 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +00001012 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001013 # 64-bit platforms.
Victor Stinnerdef80722016-04-19 15:58:11 +02001014 #
Benjamin Peterson8acaa312017-11-12 20:53:39 -08001015 # audioop needs libm for floor() in multiple functions.
Victor Stinner8058bda2019-03-01 15:31:45 +01001016 self.add(Extension('audioop', ['audioop.c'],
1017 libraries=['m']))
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001018
Victor Stinner5ec33a12019-03-01 16:43:28 +01001019 # CSV files
1020 self.add(Extension('_csv', ['_csv.c']))
1021
1022 # POSIX subprocess module helper.
Kyle Evans79925792020-10-13 15:04:44 -05001023 self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'],
1024 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner5ec33a12019-03-01 16:43:28 +01001025
Victor Stinnercfe172d2019-03-01 18:21:49 +01001026 def detect_test_extensions(self):
1027 # Python C API test module
1028 self.add(Extension('_testcapi', ['_testcapimodule.c'],
1029 depends=['testcapi_long.h']))
1030
Victor Stinner23bace22019-04-18 11:37:26 +02001031 # Python Internal C API test module
1032 self.add(Extension('_testinternalcapi', ['_testinternalcapi.c'],
Victor Stinner57491342019-04-23 12:26:33 +02001033 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner23bace22019-04-18 11:37:26 +02001034
Victor Stinnercfe172d2019-03-01 18:21:49 +01001035 # Python PEP-3118 (buffer protocol) test module
1036 self.add(Extension('_testbuffer', ['_testbuffer.c']))
1037
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001038 # Test loading multiple modules from one compiled file (https://bugs.python.org/issue16421)
Victor Stinnercfe172d2019-03-01 18:21:49 +01001039 self.add(Extension('_testimportmultiple', ['_testimportmultiple.c']))
1040
1041 # Test multi-phase extension module init (PEP 489)
1042 self.add(Extension('_testmultiphase', ['_testmultiphase.c']))
1043
1044 # Fuzz tests.
1045 self.add(Extension('_xxtestfuzz',
1046 ['_xxtestfuzz/_xxtestfuzz.c',
1047 '_xxtestfuzz/fuzzer.c']))
1048
Victor Stinner5ec33a12019-03-01 16:43:28 +01001049 def detect_readline_curses(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001050 # readline
Stefan Krah095b2732010-06-08 13:41:44 +00001051 readline_termcap_library = ""
1052 curses_library = ""
doko@ubuntu.com58844492012-06-30 18:25:32 +02001053 # Cannot use os.popen here in py3k.
1054 tmpfile = os.path.join(self.build_temp, 'readline_termcap_lib')
1055 if not os.path.exists(self.build_temp):
1056 os.makedirs(self.build_temp)
Stefan Krah095b2732010-06-08 13:41:44 +00001057 # Determine if readline is already linked against curses or tinfo.
Roland Hiebere1f77692021-02-09 02:05:25 +01001058 if sysconfig.get_config_var('HAVE_LIBREADLINE'):
1059 if sysconfig.get_config_var('WITH_EDITLINE'):
1060 readline_lib = 'edit'
1061 else:
1062 readline_lib = 'readline'
1063 do_readline = self.compiler.find_library_file(self.lib_dirs,
1064 readline_lib)
Victor Stinner4cbea512019-02-28 17:48:38 +01001065 if CROSS_COMPILING:
Victor Stinner6b982c22020-04-01 01:10:07 +02001066 ret = run_command("%s -d %s | grep '(NEEDED)' > %s"
doko@ubuntu.com58844492012-06-30 18:25:32 +02001067 % (sysconfig.get_config_var('READELF'),
1068 do_readline, tmpfile))
1069 elif find_executable('ldd'):
Victor Stinner6b982c22020-04-01 01:10:07 +02001070 ret = run_command("ldd %s > %s" % (do_readline, tmpfile))
doko@ubuntu.com58844492012-06-30 18:25:32 +02001071 else:
Victor Stinner6b982c22020-04-01 01:10:07 +02001072 ret = 1
1073 if ret == 0:
Brett Cannon9f5db072010-10-29 20:19:27 +00001074 with open(tmpfile) as fp:
1075 for ln in fp:
1076 if 'curses' in ln:
1077 readline_termcap_library = re.sub(
1078 r'.*lib(n?cursesw?)\.so.*', r'\1', ln
1079 ).rstrip()
1080 break
1081 # termcap interface split out from ncurses
1082 if 'tinfo' in ln:
1083 readline_termcap_library = 'tinfo'
1084 break
doko@ubuntu.com4c990712012-06-30 23:28:09 +02001085 if os.path.exists(tmpfile):
1086 os.unlink(tmpfile)
Roland Hiebere1f77692021-02-09 02:05:25 +01001087 else:
1088 do_readline = False
Stefan Krah095b2732010-06-08 13:41:44 +00001089 # Issue 7384: If readline is already linked against curses,
1090 # use the same library for the readline and curses modules.
1091 if 'curses' in readline_termcap_library:
1092 curses_library = readline_termcap_library
Victor Stinner625dbf22019-03-01 15:59:39 +01001093 elif self.compiler.find_library_file(self.lib_dirs, 'ncursesw'):
Stefan Krah095b2732010-06-08 13:41:44 +00001094 curses_library = 'ncursesw'
Michael Felt08970cb2019-06-21 15:58:00 +02001095 # Issue 36210: OSS provided ncurses does not link on AIX
1096 # Use IBM supplied 'curses' for successful build of _curses
1097 elif AIX and self.compiler.find_library_file(self.lib_dirs, 'curses'):
1098 curses_library = 'curses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001099 elif self.compiler.find_library_file(self.lib_dirs, 'ncurses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001100 curses_library = 'ncurses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001101 elif self.compiler.find_library_file(self.lib_dirs, 'curses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001102 curses_library = 'curses'
1103
Victor Stinner4cbea512019-02-28 17:48:38 +01001104 if MACOS:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001105 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren961683a2010-03-08 07:09:59 +00001106 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
Ned Deily04cdfa12014-06-25 13:36:14 -07001107 if (dep_target and
Ronald Oussoren49926cf2021-02-01 04:29:44 +01001108 (tuple(int(n) for n in dep_target.split('.')[0:2])
Ned Deily04cdfa12014-06-25 13:36:14 -07001109 < (10, 5) ) ):
Ronald Oussoren961683a2010-03-08 07:09:59 +00001110 os_release = 8
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001111 if os_release < 9:
1112 # MacOSX 10.4 has a broken readline. Don't try to build
1113 # the readline module unless the user has installed a fixed
1114 # readline package
Victor Stinner625dbf22019-03-01 15:59:39 +01001115 if find_file('readline/rlconf.h', self.inc_dirs, []) is None:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001116 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +00001117 if do_readline:
Victor Stinner4cbea512019-02-28 17:48:38 +01001118 if MACOS and os_release < 9:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001119 # In every directory on the search path search for a dynamic
1120 # library and then a static library, instead of first looking
Fred Drake0af17612007-09-04 19:43:19 +00001121 # for dynamic libraries on the entire path.
Martin Pantere26da7c2016-06-02 10:07:09 +00001122 # This way a statically linked custom readline gets picked up
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001123 # before the (possibly broken) dynamic library in /usr/lib.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001124 readline_extra_link_args = ('-Wl,-search_paths_first',)
1125 else:
1126 readline_extra_link_args = ()
1127
Roland Hiebere1f77692021-02-09 02:05:25 +01001128 readline_libs = [readline_lib]
Stefan Krah095b2732010-06-08 13:41:44 +00001129 if readline_termcap_library:
1130 pass # Issue 7384: Already linked against curses or tinfo.
1131 elif curses_library:
1132 readline_libs.append(curses_library)
Victor Stinner625dbf22019-03-01 15:59:39 +01001133 elif self.compiler.find_library_file(self.lib_dirs +
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001134 ['/usr/lib/termcap'],
1135 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +00001136 readline_libs.append('termcap')
Victor Stinner8058bda2019-03-01 15:31:45 +01001137 self.add(Extension('readline', ['readline.c'],
1138 library_dirs=['/usr/lib/termcap'],
1139 extra_link_args=readline_extra_link_args,
1140 libraries=readline_libs))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001141 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001142 self.missing.append('readline')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001143
Victor Stinner5ec33a12019-03-01 16:43:28 +01001144 # Curses support, requiring the System V version of curses, often
1145 # provided by the ncurses library.
1146 curses_defines = []
1147 curses_includes = []
1148 panel_library = 'panel'
1149 if curses_library == 'ncursesw':
1150 curses_defines.append(('HAVE_NCURSESW', '1'))
1151 if not CROSS_COMPILING:
1152 curses_includes.append('/usr/include/ncursesw')
1153 # Bug 1464056: If _curses.so links with ncursesw,
1154 # _curses_panel.so must link with panelw.
1155 panel_library = 'panelw'
1156 if MACOS:
1157 # On OS X, there is no separate /usr/lib/libncursesw nor
1158 # libpanelw. If we are here, we found a locally-supplied
1159 # version of libncursesw. There should also be a
1160 # libpanelw. _XOPEN_SOURCE defines are usually excluded
1161 # for OS X but we need _XOPEN_SOURCE_EXTENDED here for
1162 # ncurses wide char support
1163 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
1164 elif MACOS and curses_library == 'ncurses':
1165 # Building with the system-suppied combined libncurses/libpanel
1166 curses_defines.append(('HAVE_NCURSESW', '1'))
1167 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
Tim Peters2c60f7a2003-01-29 03:49:43 +00001168
Victor Stinnercfe172d2019-03-01 18:21:49 +01001169 curses_enabled = True
Victor Stinner5ec33a12019-03-01 16:43:28 +01001170 if curses_library.startswith('ncurses'):
1171 curses_libs = [curses_library]
1172 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001173 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001174 include_dirs=curses_includes,
1175 define_macros=curses_defines,
1176 libraries=curses_libs))
1177 elif curses_library == 'curses' and not MACOS:
1178 # OSX has an old Berkeley curses, not good enough for
1179 # the _curses module.
1180 if (self.compiler.find_library_file(self.lib_dirs, 'terminfo')):
1181 curses_libs = ['curses', 'terminfo']
1182 elif (self.compiler.find_library_file(self.lib_dirs, 'termcap')):
1183 curses_libs = ['curses', 'termcap']
1184 else:
1185 curses_libs = ['curses']
1186
1187 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001188 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001189 define_macros=curses_defines,
1190 libraries=curses_libs))
1191 else:
Victor Stinnercfe172d2019-03-01 18:21:49 +01001192 curses_enabled = False
Victor Stinner5ec33a12019-03-01 16:43:28 +01001193 self.missing.append('_curses')
1194
1195 # If the curses module is enabled, check for the panel module
Michael Felt08970cb2019-06-21 15:58:00 +02001196 # _curses_panel needs some form of ncurses
1197 skip_curses_panel = True if AIX else False
1198 if (curses_enabled and not skip_curses_panel and
1199 self.compiler.find_library_file(self.lib_dirs, panel_library)):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001200 self.add(Extension('_curses_panel', ['_curses_panel.c'],
Michael Felt08970cb2019-06-21 15:58:00 +02001201 include_dirs=curses_includes,
1202 define_macros=curses_defines,
1203 libraries=[panel_library, *curses_libs]))
1204 elif not skip_curses_panel:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001205 self.missing.append('_curses_panel')
1206
1207 def detect_crypt(self):
1208 # crypt module.
pxinwr236d0b72019-04-15 17:02:20 +08001209 if VXWORKS:
1210 # bpo-31904: crypt() function is not provided by VxWorks.
1211 # DES_crypt() OpenSSL provides is too weak to implement
1212 # the encryption.
Victor Stinnercad80202021-01-19 23:04:49 +01001213 self.missing.append('_crypt')
pxinwr236d0b72019-04-15 17:02:20 +08001214 return
1215
Victor Stinner625dbf22019-03-01 15:59:39 +01001216 if self.compiler.find_library_file(self.lib_dirs, 'crypt'):
Ronald Oussoren94f25282010-05-05 19:11:21 +00001217 libs = ['crypt']
Guido van Rossumd8faa362007-04-27 19:54:29 +00001218 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +00001219 libs = []
pxinwr32f5fdd2019-02-27 19:09:28 +08001220
Victor Stinnercad80202021-01-19 23:04:49 +01001221 self.add(Extension('_crypt', ['_cryptmodule.c'], libraries=libs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001222
Victor Stinner5ec33a12019-03-01 16:43:28 +01001223 def detect_socket(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001224 # socket(2)
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001225 kwargs = {'depends': ['socketmodule.h']}
pxinwr00a65682020-11-29 06:14:16 +08001226 if MACOS:
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001227 # Issue #35569: Expose RFC 3542 socket options.
1228 kwargs['extra_compile_args'] = ['-D__APPLE_USE_RFC_3542']
Erlend Egeberg Aasland9a45bfe2020-05-17 08:32:46 +02001229
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001230 self.add(Extension('_socket', ['socketmodule.c'], **kwargs))
pxinwr32f5fdd2019-02-27 19:09:28 +08001231
Victor Stinner5ec33a12019-03-01 16:43:28 +01001232 def detect_dbm_gdbm(self):
Georg Brandl489cb4f2009-07-11 10:08:49 +00001233 # Modules that provide persistent dictionary-like semantics. You will
1234 # probably want to arrange for at least one of them to be available on
1235 # your machine, though none are defined by default because of library
1236 # dependencies. The Python module dbm/__init__.py provides an
1237 # implementation independent wrapper for these; dbm/dumb.py provides
1238 # similar functionality (but slower of course) implemented in Python.
1239
1240 # Sleepycat^WOracle Berkeley DB interface.
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001241 # https://www.oracle.com/database/technologies/related/berkeleydb.html
Georg Brandl489cb4f2009-07-11 10:08:49 +00001242 #
1243 # This requires the Sleepycat^WOracle DB code. The supported versions
1244 # are set below. Visit the URL above to download
1245 # a release. Most open source OSes come with one or more
1246 # versions of BerkeleyDB already installed.
1247
doko@ubuntu.com15bac0f2012-07-01 10:35:54 +02001248 max_db_ver = (5, 3)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001249 min_db_ver = (3, 3)
1250 db_setup_debug = False # verbose debug prints from this script?
1251
1252 def allow_db_ver(db_ver):
1253 """Returns a boolean if the given BerkeleyDB version is acceptable.
1254
1255 Args:
1256 db_ver: A tuple of the version to verify.
1257 """
1258 if not (min_db_ver <= db_ver <= max_db_ver):
1259 return False
1260 return True
1261
1262 def gen_db_minor_ver_nums(major):
1263 if major == 4:
1264 for x in range(max_db_ver[1]+1):
1265 if allow_db_ver((4, x)):
1266 yield x
1267 elif major == 3:
1268 for x in (3,):
1269 if allow_db_ver((3, x)):
1270 yield x
1271 else:
1272 raise ValueError("unknown major BerkeleyDB version", major)
1273
1274 # construct a list of paths to look for the header file in on
1275 # top of the normal inc_dirs.
1276 db_inc_paths = [
1277 '/usr/include/db4',
1278 '/usr/local/include/db4',
1279 '/opt/sfw/include/db4',
1280 '/usr/include/db3',
1281 '/usr/local/include/db3',
1282 '/opt/sfw/include/db3',
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001283 # Fink defaults (https://www.finkproject.org/)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001284 '/sw/include/db4',
1285 '/sw/include/db3',
1286 ]
1287 # 4.x minor number specific paths
1288 for x in gen_db_minor_ver_nums(4):
1289 db_inc_paths.append('/usr/include/db4%d' % x)
1290 db_inc_paths.append('/usr/include/db4.%d' % x)
1291 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
1292 db_inc_paths.append('/usr/local/include/db4%d' % x)
1293 db_inc_paths.append('/pkg/db-4.%d/include' % x)
1294 db_inc_paths.append('/opt/db-4.%d/include' % x)
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07001295 # MacPorts default (https://www.macports.org/)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001296 db_inc_paths.append('/opt/local/include/db4%d' % x)
1297 # 3.x minor number specific paths
1298 for x in gen_db_minor_ver_nums(3):
1299 db_inc_paths.append('/usr/include/db3%d' % x)
1300 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
1301 db_inc_paths.append('/usr/local/include/db3%d' % x)
1302 db_inc_paths.append('/pkg/db-3.%d/include' % x)
1303 db_inc_paths.append('/opt/db-3.%d/include' % x)
1304
Victor Stinner4cbea512019-02-28 17:48:38 +01001305 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001306 db_inc_paths = []
1307
Georg Brandl489cb4f2009-07-11 10:08:49 +00001308 # Add some common subdirectories for Sleepycat DB to the list,
1309 # based on the standard include directories. This way DB3/4 gets
1310 # picked up when it is installed in a non-standard prefix and
1311 # the user has added that prefix into inc_dirs.
1312 std_variants = []
Victor Stinner625dbf22019-03-01 15:59:39 +01001313 for dn in self.inc_dirs:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001314 std_variants.append(os.path.join(dn, 'db3'))
1315 std_variants.append(os.path.join(dn, 'db4'))
1316 for x in gen_db_minor_ver_nums(4):
1317 std_variants.append(os.path.join(dn, "db4%d"%x))
1318 std_variants.append(os.path.join(dn, "db4.%d"%x))
1319 for x in gen_db_minor_ver_nums(3):
1320 std_variants.append(os.path.join(dn, "db3%d"%x))
1321 std_variants.append(os.path.join(dn, "db3.%d"%x))
1322
1323 db_inc_paths = std_variants + db_inc_paths
1324 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
1325
1326 db_ver_inc_map = {}
1327
Victor Stinner4cbea512019-02-28 17:48:38 +01001328 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001329 sysroot = macosx_sdk_root()
1330
Georg Brandl489cb4f2009-07-11 10:08:49 +00001331 class db_found(Exception): pass
1332 try:
1333 # See whether there is a Sleepycat header in the standard
1334 # search path.
Victor Stinner625dbf22019-03-01 15:59:39 +01001335 for d in self.inc_dirs + db_inc_paths:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001336 f = os.path.join(d, "db.h")
Victor Stinner4cbea512019-02-28 17:48:38 +01001337 if MACOS and is_macosx_sdk_path(d):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001338 f = os.path.join(sysroot, d[1:], "db.h")
1339
Georg Brandl489cb4f2009-07-11 10:08:49 +00001340 if db_setup_debug: print("db: looking for db.h in", f)
1341 if os.path.exists(f):
Brett Cannon9f5db072010-10-29 20:19:27 +00001342 with open(f, 'rb') as file:
1343 f = file.read()
Benjamin Peterson019f3612009-08-12 18:18:03 +00001344 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001345 if m:
1346 db_major = int(m.group(1))
Benjamin Peterson019f3612009-08-12 18:18:03 +00001347 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001348 db_minor = int(m.group(1))
1349 db_ver = (db_major, db_minor)
1350
1351 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
1352 if db_ver == (4, 6):
Benjamin Peterson019f3612009-08-12 18:18:03 +00001353 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001354 db_patch = int(m.group(1))
1355 if db_patch < 21:
1356 print("db.h:", db_ver, "patch", db_patch,
1357 "being ignored (4.6.x must be >= 4.6.21)")
1358 continue
1359
1360 if ( (db_ver not in db_ver_inc_map) and
1361 allow_db_ver(db_ver) ):
1362 # save the include directory with the db.h version
1363 # (first occurrence only)
1364 db_ver_inc_map[db_ver] = d
1365 if db_setup_debug:
1366 print("db.h: found", db_ver, "in", d)
1367 else:
1368 # we already found a header for this library version
1369 if db_setup_debug: print("db.h: ignoring", d)
1370 else:
1371 # ignore this header, it didn't contain a version number
1372 if db_setup_debug:
1373 print("db.h: no version number version in", d)
1374
1375 db_found_vers = list(db_ver_inc_map.keys())
1376 db_found_vers.sort()
1377
1378 while db_found_vers:
1379 db_ver = db_found_vers.pop()
1380 db_incdir = db_ver_inc_map[db_ver]
1381
1382 # check lib directories parallel to the location of the header
1383 db_dirs_to_check = [
1384 db_incdir.replace("include", 'lib64'),
1385 db_incdir.replace("include", 'lib'),
1386 ]
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001387
Victor Stinner4cbea512019-02-28 17:48:38 +01001388 if not MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001389 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check))
1390
1391 else:
1392 # Same as other branch, but takes OSX SDK into account
1393 tmp = []
1394 for dn in db_dirs_to_check:
1395 if is_macosx_sdk_path(dn):
1396 if os.path.isdir(os.path.join(sysroot, dn[1:])):
1397 tmp.append(dn)
1398 else:
1399 if os.path.isdir(dn):
1400 tmp.append(dn)
Ronald Oussorendc969e52010-06-27 12:37:46 +00001401 db_dirs_to_check = tmp
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001402
1403 db_dirs_to_check = tmp
Georg Brandl489cb4f2009-07-11 10:08:49 +00001404
Ezio Melotti42da6632011-03-15 05:18:48 +02001405 # Look for a version specific db-X.Y before an ambiguous dbX
Georg Brandl489cb4f2009-07-11 10:08:49 +00001406 # XXX should we -ever- look for a dbX name? Do any
1407 # systems really not name their library by version and
1408 # symlink to more general names?
1409 for dblib in (('db-%d.%d' % db_ver),
1410 ('db%d%d' % db_ver),
1411 ('db%d' % db_ver[0])):
1412 dblib_file = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001413 db_dirs_to_check + self.lib_dirs, dblib )
Georg Brandl489cb4f2009-07-11 10:08:49 +00001414 if dblib_file:
1415 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
1416 raise db_found
1417 else:
1418 if db_setup_debug: print("db lib: ", dblib, "not found")
1419
1420 except db_found:
1421 if db_setup_debug:
1422 print("bsddb using BerkeleyDB lib:", db_ver, dblib)
1423 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001424 dblibs = [dblib]
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001425 # Only add the found library and include directories if they aren't
1426 # already being searched. This avoids an explicit runtime library
1427 # dependency.
Victor Stinner625dbf22019-03-01 15:59:39 +01001428 if db_incdir in self.inc_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001429 db_incs = None
1430 else:
1431 db_incs = [db_incdir]
Victor Stinner625dbf22019-03-01 15:59:39 +01001432 if dblib_dir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001433 dblib_dir = None
Georg Brandl489cb4f2009-07-11 10:08:49 +00001434 else:
1435 if db_setup_debug: print("db: no appropriate library found")
1436 db_incs = None
1437 dblibs = []
1438 dblib_dir = None
1439
Victor Stinner5ec33a12019-03-01 16:43:28 +01001440 dbm_setup_debug = False # verbose debug prints from this script?
1441 dbm_order = ['gdbm']
1442 # The standard Unix dbm module:
1443 if not CYGWIN:
1444 config_args = [arg.strip("'")
1445 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
1446 dbm_args = [arg for arg in config_args
1447 if arg.startswith('--with-dbmliborder=')]
1448 if dbm_args:
1449 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
1450 else:
1451 dbm_order = "ndbm:gdbm:bdb".split(":")
1452 dbmext = None
1453 for cand in dbm_order:
1454 if cand == "ndbm":
1455 if find_file("ndbm.h", self.inc_dirs, []) is not None:
1456 # Some systems have -lndbm, others have -lgdbm_compat,
1457 # others don't have either
1458 if self.compiler.find_library_file(self.lib_dirs,
1459 'ndbm'):
1460 ndbm_libs = ['ndbm']
1461 elif self.compiler.find_library_file(self.lib_dirs,
1462 'gdbm_compat'):
1463 ndbm_libs = ['gdbm_compat']
1464 else:
1465 ndbm_libs = []
1466 if dbm_setup_debug: print("building dbm using ndbm")
1467 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1468 define_macros=[
1469 ('HAVE_NDBM_H',None),
1470 ],
1471 libraries=ndbm_libs)
1472 break
1473
1474 elif cand == "gdbm":
1475 if self.compiler.find_library_file(self.lib_dirs, 'gdbm'):
1476 gdbm_libs = ['gdbm']
1477 if self.compiler.find_library_file(self.lib_dirs,
1478 'gdbm_compat'):
1479 gdbm_libs.append('gdbm_compat')
1480 if find_file("gdbm/ndbm.h", self.inc_dirs, []) is not None:
1481 if dbm_setup_debug: print("building dbm using gdbm")
1482 dbmext = Extension(
1483 '_dbm', ['_dbmmodule.c'],
1484 define_macros=[
1485 ('HAVE_GDBM_NDBM_H', None),
1486 ],
1487 libraries = gdbm_libs)
1488 break
1489 if find_file("gdbm-ndbm.h", self.inc_dirs, []) is not None:
1490 if dbm_setup_debug: print("building dbm using gdbm")
1491 dbmext = Extension(
1492 '_dbm', ['_dbmmodule.c'],
1493 define_macros=[
1494 ('HAVE_GDBM_DASH_NDBM_H', None),
1495 ],
1496 libraries = gdbm_libs)
1497 break
1498 elif cand == "bdb":
1499 if dblibs:
1500 if dbm_setup_debug: print("building dbm using bdb")
1501 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1502 library_dirs=dblib_dir,
1503 runtime_library_dirs=dblib_dir,
1504 include_dirs=db_incs,
1505 define_macros=[
1506 ('HAVE_BERKDB_H', None),
1507 ('DB_DBM_HSEARCH', None),
1508 ],
1509 libraries=dblibs)
1510 break
1511 if dbmext is not None:
1512 self.add(dbmext)
1513 else:
1514 self.missing.append('_dbm')
1515
1516 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
1517 if ('gdbm' in dbm_order and
1518 self.compiler.find_library_file(self.lib_dirs, 'gdbm')):
1519 self.add(Extension('_gdbm', ['_gdbmmodule.c'],
1520 libraries=['gdbm']))
1521 else:
1522 self.missing.append('_gdbm')
1523
1524 def detect_sqlite(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001525 # The sqlite interface
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 sqlite_setup_debug = False # verbose debug prints from this script?
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001527
1528 # We hunt for #define SQLITE_VERSION "n.n.n"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 sqlite_incdir = sqlite_libdir = None
1530 sqlite_inc_paths = [ '/usr/include',
1531 '/usr/include/sqlite',
1532 '/usr/include/sqlite3',
1533 '/usr/local/include',
1534 '/usr/local/include/sqlite',
1535 '/usr/local/include/sqlite3',
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001536 ]
Victor Stinner4cbea512019-02-28 17:48:38 +01001537 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001538 sqlite_inc_paths = []
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +01001539 MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001540 MIN_SQLITE_VERSION = ".".join([str(x)
1541 for x in MIN_SQLITE_VERSION_NUMBER])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001542
1543 # Scan the default include directories before the SQLite specific
1544 # ones. This allows one to override the copy of sqlite on OSX,
1545 # where /usr/include contains an old version of sqlite.
Victor Stinner4cbea512019-02-28 17:48:38 +01001546 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001547 sysroot = macosx_sdk_root()
1548
Victor Stinner625dbf22019-03-01 15:59:39 +01001549 for d_ in self.inc_dirs + sqlite_inc_paths:
Ned Deily9b635832012-08-05 15:13:33 -07001550 d = d_
Victor Stinner4cbea512019-02-28 17:48:38 +01001551 if MACOS and is_macosx_sdk_path(d):
Ned Deily9b635832012-08-05 15:13:33 -07001552 d = os.path.join(sysroot, d[1:])
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001553
Ned Deily9b635832012-08-05 15:13:33 -07001554 f = os.path.join(d, "sqlite3.h")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001555 if os.path.exists(f):
Guido van Rossum452bf512007-02-09 05:32:43 +00001556 if sqlite_setup_debug: print("sqlite: found %s"%f)
Brett Cannon9f5db072010-10-29 20:19:27 +00001557 with open(f) as file:
1558 incf = file.read()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559 m = re.search(
Petri Lehtinened909bc2013-02-23 17:05:28 +01001560 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001561 if m:
1562 sqlite_version = m.group(1)
1563 sqlite_version_tuple = tuple([int(x)
1564 for x in sqlite_version.split(".")])
1565 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
1566 # we win!
Thomas Wouters89f507f2006-12-13 04:49:30 +00001567 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001568 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001569 sqlite_incdir = d
1570 break
1571 else:
1572 if sqlite_setup_debug:
Charles Pigottad0daf52019-04-26 16:38:12 +01001573 print("%s: version %s is too old, need >= %s"%(d,
Guido van Rossum452bf512007-02-09 05:32:43 +00001574 sqlite_version, MIN_SQLITE_VERSION))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 elif sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001576 print("sqlite: %s had no SQLITE_VERSION"%(f,))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001577
1578 if sqlite_incdir:
1579 sqlite_dirs_to_check = [
1580 os.path.join(sqlite_incdir, '..', 'lib64'),
1581 os.path.join(sqlite_incdir, '..', 'lib'),
1582 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
1583 os.path.join(sqlite_incdir, '..', '..', 'lib'),
1584 ]
Tarek Ziadé36797272010-07-22 12:50:05 +00001585 sqlite_libfile = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001586 sqlite_dirs_to_check + self.lib_dirs, 'sqlite3')
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001587 if sqlite_libfile:
1588 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001589
1590 if sqlite_incdir and sqlite_libdir:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001591 sqlite_srcs = ['_sqlite/cache.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001592 '_sqlite/connection.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593 '_sqlite/cursor.c',
1594 '_sqlite/microprotocols.c',
1595 '_sqlite/module.c',
1596 '_sqlite/prepare_protocol.c',
1597 '_sqlite/row.c',
1598 '_sqlite/statement.c',
1599 '_sqlite/util.c', ]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001600 sqlite_defines = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001601
Benjamin Peterson076ed002010-10-31 17:11:02 +00001602 # Enable support for loadable extensions in the sqlite3 module
1603 # if --enable-loadable-sqlite-extensions configure option is used.
1604 if '--enable-loadable-sqlite-extensions' not in sysconfig.get_config_var("CONFIG_ARGS"):
1605 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001606
Victor Stinner4cbea512019-02-28 17:48:38 +01001607 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001608 # In every directory on the search path search for a dynamic
1609 # library and then a static library, instead of first looking
Ezio Melotti13925002011-03-16 11:05:33 +02001610 # for dynamic libraries on the entire path.
1611 # This way a statically linked custom sqlite gets picked up
Thomas Wouters477c8d52006-05-27 19:21:47 +00001612 # before the dynamic library in /usr/lib.
1613 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1614 else:
1615 sqlite_extra_link_args = ()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616
Brett Cannonc5011fe2011-06-06 20:09:10 -07001617 include_dirs = ["Modules/_sqlite"]
1618 # Only include the directory where sqlite was found if it does
1619 # not already exist in set include directories, otherwise you
1620 # can end up with a bad search path order.
1621 if sqlite_incdir not in self.compiler.include_dirs:
1622 include_dirs.append(sqlite_incdir)
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001623 # avoid a runtime library path for a system library dir
Victor Stinner625dbf22019-03-01 15:59:39 +01001624 if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001625 sqlite_libdir = None
Victor Stinner8058bda2019-03-01 15:31:45 +01001626 self.add(Extension('_sqlite3', sqlite_srcs,
1627 define_macros=sqlite_defines,
1628 include_dirs=include_dirs,
1629 library_dirs=sqlite_libdir,
1630 extra_link_args=sqlite_extra_link_args,
1631 libraries=["sqlite3",]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001632 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001633 self.missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001634
Victor Stinner5ec33a12019-03-01 16:43:28 +01001635 def detect_platform_specific_exts(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001636 # Unix-only modules
Victor Stinner4cbea512019-02-28 17:48:38 +01001637 if not MS_WINDOWS:
pxinwr32f5fdd2019-02-27 19:09:28 +08001638 if not VXWORKS:
1639 # Steen Lumholt's termios module
Victor Stinner8058bda2019-03-01 15:31:45 +01001640 self.add(Extension('termios', ['termios.c']))
pxinwr32f5fdd2019-02-27 19:09:28 +08001641 # Jeremy Hylton's rlimit interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001642 self.add(Extension('resource', ['resource.c']))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001643 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001644 self.missing.extend(['resource', 'termios'])
Christian Heimes29a7df72018-01-26 23:28:46 +01001645
Victor Stinner5ec33a12019-03-01 16:43:28 +01001646 # Platform-specific libraries
1647 if HOST_PLATFORM.startswith(('linux', 'freebsd', 'gnukfreebsd')):
1648 self.add(Extension('ossaudiodev', ['ossaudiodev.c']))
Michael Felt08970cb2019-06-21 15:58:00 +02001649 elif not AIX:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001650 self.missing.append('ossaudiodev')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001651
Victor Stinner5ec33a12019-03-01 16:43:28 +01001652 if MACOS:
Ned Deily951ab582020-05-18 11:31:21 -04001653 self.add(Extension('_scproxy', ['_scproxy.c'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001654 extra_link_args=[
1655 '-framework', 'SystemConfiguration',
Ned Deily951ab582020-05-18 11:31:21 -04001656 '-framework', 'CoreFoundation']))
Fredrik Lundhade711a2001-01-24 08:00:28 +00001657
Victor Stinner5ec33a12019-03-01 16:43:28 +01001658 def detect_compress_exts(self):
Barry Warsaw259b1e12002-08-13 20:09:26 +00001659 # Andrew Kuchling's zlib module. Note that some versions of zlib
1660 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1661 # http://www.cert.org/advisories/CA-2002-07.html
1662 #
1663 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1664 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1665 # now, we still accept 1.1.3, because we think it's difficult to
1666 # exploit this in Python, and we'd rather make it RedHat's problem
1667 # than our problem <wink>.
1668 #
1669 # You can upgrade zlib to version 1.1.4 yourself by going to
1670 # http://www.gzip.org/zlib/
Victor Stinner625dbf22019-03-01 15:59:39 +01001671 zlib_inc = find_file('zlib.h', [], self.inc_dirs)
Christian Heimes1dc54002008-03-24 02:19:29 +00001672 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001673 if zlib_inc is not None:
1674 zlib_h = zlib_inc[0] + '/zlib.h'
1675 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001676 version_req = '"1.1.3"'
Victor Stinner4cbea512019-02-28 17:48:38 +01001677 if MACOS and is_macosx_sdk_path(zlib_h):
Ned Deily507c5912013-10-18 21:32:00 -07001678 zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
Brett Cannon9f5db072010-10-29 20:19:27 +00001679 with open(zlib_h) as fp:
1680 while 1:
1681 line = fp.readline()
1682 if not line:
1683 break
1684 if line.startswith('#define ZLIB_VERSION'):
1685 version = line.split()[2]
1686 break
Guido van Rossume6970912001-04-15 15:16:12 +00001687 if version >= version_req:
Victor Stinner625dbf22019-03-01 15:59:39 +01001688 if (self.compiler.find_library_file(self.lib_dirs, 'z')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001689 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001690 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1691 else:
1692 zlib_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001693 self.add(Extension('zlib', ['zlibmodule.c'],
1694 libraries=['z'],
1695 extra_link_args=zlib_extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001696 have_zlib = True
Guido van Rossumd8faa362007-04-27 19:54:29 +00001697 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001698 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001699 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001700 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001701 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001702 self.missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001703
Christian Heimes1dc54002008-03-24 02:19:29 +00001704 # Helper module for various ascii-encoders. Uses zlib for an optimized
1705 # crc32 if we have it. Otherwise binascii uses its own.
1706 if have_zlib:
1707 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1708 libraries = ['z']
1709 extra_link_args = zlib_extra_link_args
1710 else:
1711 extra_compile_args = []
1712 libraries = []
1713 extra_link_args = []
Victor Stinner8058bda2019-03-01 15:31:45 +01001714 self.add(Extension('binascii', ['binascii.c'],
1715 extra_compile_args=extra_compile_args,
1716 libraries=libraries,
1717 extra_link_args=extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001718
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001719 # Gustavo Niemeyer's bz2 module.
Victor Stinner625dbf22019-03-01 15:59:39 +01001720 if (self.compiler.find_library_file(self.lib_dirs, 'bz2')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001721 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001722 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1723 else:
1724 bz2_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001725 self.add(Extension('_bz2', ['_bz2module.c'],
1726 libraries=['bz2'],
1727 extra_link_args=bz2_extra_link_args))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001728 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001729 self.missing.append('_bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001730
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001731 # LZMA compression support.
Victor Stinner625dbf22019-03-01 15:59:39 +01001732 if self.compiler.find_library_file(self.lib_dirs, 'lzma'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001733 self.add(Extension('_lzma', ['_lzmamodule.c'],
1734 libraries=['lzma']))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001735 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001736 self.missing.append('_lzma')
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001737
Victor Stinner5ec33a12019-03-01 16:43:28 +01001738 def detect_expat_elementtree(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001739 # Interface to the Expat XML parser
1740 #
Benjamin Petersona28e7022010-01-09 18:53:06 +00001741 # Expat was written by James Clark and is now maintained by a group of
1742 # developers on SourceForge; see www.libexpat.org for more information.
1743 # The pyexpat module was written by Paul Prescod after a prototype by
1744 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1745 # of a system shared libexpat.so is possible with --with-system-expat
Benjamin Petersonc73206c2010-10-31 16:38:19 +00001746 # configure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001747 #
1748 # More information on Expat can be found at www.libexpat.org.
1749 #
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001750 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1751 expat_inc = []
1752 define_macros = []
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001753 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001754 expat_lib = ['expat']
1755 expat_sources = []
Christian Heimesd489c7a2013-02-09 17:02:06 +01001756 expat_depends = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001757 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01001758 expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')]
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001759 define_macros = [
1760 ('HAVE_EXPAT_CONFIG_H', '1'),
Victor Stinner93d0cb52017-08-18 23:43:54 +02001761 # bpo-30947: Python uses best available entropy sources to
1762 # call XML_SetHashSalt(), expat entropy sources are not needed
1763 ('XML_POOR_ENTROPY', '1'),
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001764 ]
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001765 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001766 expat_lib = []
1767 expat_sources = ['expat/xmlparse.c',
1768 'expat/xmlrole.c',
1769 'expat/xmltok.c']
Christian Heimesd489c7a2013-02-09 17:02:06 +01001770 expat_depends = ['expat/ascii.h',
1771 'expat/asciitab.h',
1772 'expat/expat.h',
1773 'expat/expat_config.h',
1774 'expat/expat_external.h',
1775 'expat/internal.h',
1776 'expat/latin1tab.h',
1777 'expat/utf8tab.h',
1778 'expat/xmlrole.h',
1779 'expat/xmltok.h',
1780 'expat/xmltok_impl.h'
1781 ]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001782
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001783 cc = sysconfig.get_config_var('CC').split()[0]
Victor Stinner6b982c22020-04-01 01:10:07 +02001784 ret = run_command(
Benjamin Peterson95da3102019-06-29 16:00:22 -07001785 '"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc)
Victor Stinner6b982c22020-04-01 01:10:07 +02001786 if ret == 0:
Benjamin Peterson95da3102019-06-29 16:00:22 -07001787 extra_compile_args.append('-Wno-unreachable-code')
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001788
Victor Stinner8058bda2019-03-01 15:31:45 +01001789 self.add(Extension('pyexpat',
1790 define_macros=define_macros,
1791 extra_compile_args=extra_compile_args,
1792 include_dirs=expat_inc,
1793 libraries=expat_lib,
1794 sources=['pyexpat.c'] + expat_sources,
1795 depends=expat_depends))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001796
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001797 # Fredrik Lundh's cElementTree module. Note that this also
1798 # uses expat (via the CAPI hook in pyexpat).
1799
Victor Stinner625dbf22019-03-01 15:59:39 +01001800 if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001801 define_macros.append(('USE_PYEXPAT_CAPI', None))
Victor Stinner8058bda2019-03-01 15:31:45 +01001802 self.add(Extension('_elementtree',
1803 define_macros=define_macros,
1804 include_dirs=expat_inc,
1805 libraries=expat_lib,
1806 sources=['_elementtree.c'],
1807 depends=['pyexpat.c', *expat_sources,
1808 *expat_depends]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001809 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001810 self.missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001811
Victor Stinner5ec33a12019-03-01 16:43:28 +01001812 def detect_multibytecodecs(self):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001813 # Hye-Shik Chang's CJKCodecs modules.
Victor Stinner8058bda2019-03-01 15:31:45 +01001814 self.add(Extension('_multibytecodec',
1815 ['cjkcodecs/multibytecodec.c']))
Walter Dörwalde9eaab42007-05-22 16:02:13 +00001816 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001817 self.add(Extension('_codecs_%s' % loc,
1818 ['cjkcodecs/_codecs_%s.c' % loc]))
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001819
Victor Stinner5ec33a12019-03-01 16:43:28 +01001820 def detect_multiprocessing(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001821 # Richard Oudkerk's multiprocessing module
Victor Stinner4cbea512019-02-28 17:48:38 +01001822 if MS_WINDOWS:
Victor Stinnerc991f242019-03-01 17:19:04 +01001823 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c',
1824 '_multiprocessing/semaphore.c']
Benjamin Petersone711caf2008-06-11 16:44:04 +00001825 else:
Victor Stinnerc991f242019-03-01 17:19:04 +01001826 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c']
Mark Dickinsona614f042009-11-28 12:48:43 +00001827 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
1828 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001829 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
Victor Stinner8058bda2019-03-01 15:31:45 +01001830 self.add(Extension('_multiprocessing', multiprocessing_srcs,
Victor Stinner8058bda2019-03-01 15:31:45 +01001831 include_dirs=["Modules/_multiprocessing"]))
Guido van Rossuma9e20242007-03-08 00:43:48 +00001832
Victor Stinnercad80202021-01-19 23:04:49 +01001833 if (not MS_WINDOWS and
1834 sysconfig.get_config_var('HAVE_SHM_OPEN') and
1835 sysconfig.get_config_var('HAVE_SHM_UNLINK')):
1836 posixshmem_srcs = ['_multiprocessing/posixshmem.c']
1837 libs = []
1838 if sysconfig.get_config_var('SHM_NEEDS_LIBRT'):
1839 # need to link with librt to get shm_open()
1840 libs.append('rt')
1841 self.add(Extension('_posixshmem', posixshmem_srcs,
1842 define_macros={},
1843 libraries=libs,
1844 include_dirs=["Modules/_multiprocessing"]))
1845 else:
1846 self.missing.append('_posixshmem')
1847
Victor Stinner5ec33a12019-03-01 16:43:28 +01001848 def detect_uuid(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001849 # Build the _uuid module if possible
Victor Stinner625dbf22019-03-01 15:59:39 +01001850 uuid_incs = find_file("uuid.h", self.inc_dirs, ["/usr/include/uuid"])
Nick Coghlan53efbf32017-11-26 13:04:46 +10001851 if uuid_incs is not None:
Victor Stinner625dbf22019-03-01 15:59:39 +01001852 if self.compiler.find_library_file(self.lib_dirs, 'uuid'):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001853 uuid_libs = ['uuid']
1854 else:
1855 uuid_libs = []
Victor Stinnercfe172d2019-03-01 18:21:49 +01001856 self.add(Extension('_uuid', ['_uuidmodule.c'],
1857 libraries=uuid_libs,
1858 include_dirs=uuid_incs))
Antoine Pitroua106aec2017-09-28 23:03:06 +02001859 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001860 self.missing.append('_uuid')
Antoine Pitroua106aec2017-09-28 23:03:06 +02001861
Victor Stinner5ec33a12019-03-01 16:43:28 +01001862 def detect_modules(self):
Victor Stinnercfe172d2019-03-01 18:21:49 +01001863 self.configure_compiler()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001864 self.init_inc_lib_dirs()
1865
1866 self.detect_simple_extensions()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001867 if TEST_EXTENSIONS:
1868 self.detect_test_extensions()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001869 self.detect_readline_curses()
1870 self.detect_crypt()
1871 self.detect_socket()
1872 self.detect_openssl_hashlib()
xdegaye2ee077f2019-04-09 17:20:08 +02001873 self.detect_hash_builtins()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001874 self.detect_dbm_gdbm()
1875 self.detect_sqlite()
1876 self.detect_platform_specific_exts()
1877 self.detect_nis()
1878 self.detect_compress_exts()
1879 self.detect_expat_elementtree()
1880 self.detect_multibytecodecs()
1881 self.detect_decimal()
1882 self.detect_ctypes()
1883 self.detect_multiprocessing()
1884 if not self.detect_tkinter():
1885 self.missing.append('_tkinter')
1886 self.detect_uuid()
1887
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001888## # Uncomment these lines if you want to play with xxmodule.c
Victor Stinnercfe172d2019-03-01 18:21:49 +01001889## self.add(Extension('xx', ['xxmodule.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001890
Hai Shi5787ba42021-04-06 20:55:13 +08001891 # The limited C API is not compatible with the Py_TRACE_REFS macro.
1892 if not sysconfig.get_config_var('Py_TRACE_REFS'):
1893 self.add(Extension('xxlimited', ['xxlimited.c']))
1894 self.add(Extension('xxlimited_35', ['xxlimited_35.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001895
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001896 def detect_tkinter_fromenv(self):
1897 # Build _tkinter using the Tcl/Tk locations specified by
1898 # the _TCLTK_INCLUDES and _TCLTK_LIBS environment variables.
1899 # This method is meant to be invoked by detect_tkinter().
Ned Deilyd819b932013-09-06 01:07:05 -07001900 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001901 # The variables can be set via one of the following ways.
Ned Deilyd819b932013-09-06 01:07:05 -07001902 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001903 # - Automatically, at configuration time, by using pkg-config.
1904 # The tool is called by the configure script.
1905 # Additional pkg-config configuration paths can be set via the
1906 # PKG_CONFIG_PATH environment variable.
1907 #
1908 # PKG_CONFIG_PATH=".../lib/pkgconfig" ./configure ...
1909 #
1910 # - Explicitly, at configuration time by setting both
1911 # --with-tcltk-includes and --with-tcltk-libs.
1912 #
1913 # ./configure ... \
Ned Deilyd819b932013-09-06 01:07:05 -07001914 # --with-tcltk-includes="-I/path/to/tclincludes \
1915 # -I/path/to/tkincludes"
1916 # --with-tcltk-libs="-L/path/to/tcllibs -ltclm.n \
1917 # -L/path/to/tklibs -ltkm.n"
1918 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001919 # - Explicitly, at compile time, by passing TCLTK_INCLUDES and
1920 # TCLTK_LIBS to the make target.
1921 # This will override any configuration-time option.
1922 #
1923 # make TCLTK_INCLUDES="..." TCLTK_LIBS="..."
Ned Deilyd819b932013-09-06 01:07:05 -07001924 #
1925 # This can be useful for building and testing tkinter with multiple
1926 # versions of Tcl/Tk. Note that a build of Tk depends on a particular
1927 # build of Tcl so you need to specify both arguments and use care when
1928 # overriding.
1929
1930 # The _TCLTK variables are created in the Makefile sharedmods target.
1931 tcltk_includes = os.environ.get('_TCLTK_INCLUDES')
1932 tcltk_libs = os.environ.get('_TCLTK_LIBS')
1933 if not (tcltk_includes and tcltk_libs):
1934 # Resume default configuration search.
Victor Stinner4cbea512019-02-28 17:48:38 +01001935 return False
Ned Deilyd819b932013-09-06 01:07:05 -07001936
1937 extra_compile_args = tcltk_includes.split()
1938 extra_link_args = tcltk_libs.split()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001939 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1940 define_macros=[('WITH_APPINIT', 1)],
1941 extra_compile_args = extra_compile_args,
1942 extra_link_args = extra_link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01001943 return True
Ned Deilyd819b932013-09-06 01:07:05 -07001944
Victor Stinner625dbf22019-03-01 15:59:39 +01001945 def detect_tkinter_darwin(self):
Ned Deily1731d6d2020-05-18 04:32:38 -04001946 # Build default _tkinter on macOS using Tcl and Tk frameworks.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001947 # This method is meant to be invoked by detect_tkinter().
Ned Deily1731d6d2020-05-18 04:32:38 -04001948 #
1949 # The macOS native Tk (AKA Aqua Tk) and Tcl are most commonly
1950 # built and installed as macOS framework bundles. However,
1951 # for several reasons, we cannot take full advantage of the
1952 # Apple-supplied compiler chain's -framework options here.
1953 # Instead, we need to find and pass to the compiler the
1954 # absolute paths of the Tcl and Tk headers files we want to use
1955 # and the absolute path to the directory containing the Tcl
1956 # and Tk frameworks for linking.
1957 #
1958 # We want to handle here two common use cases on macOS:
1959 # 1. Build and link with system-wide third-party or user-built
1960 # Tcl and Tk frameworks installed in /Library/Frameworks.
1961 # 2. Build and link using a user-specified macOS SDK so that the
1962 # built Python can be exported to other systems. In this case,
1963 # search only the SDK's /Library/Frameworks (normally empty)
1964 # and /System/Library/Frameworks.
1965 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001966 # Any other use cases are handled either by detect_tkinter_fromenv(),
1967 # or detect_tkinter(). The former handles non-standard locations of
1968 # Tcl/Tk, defined via the _TCLTK_INCLUDES and _TCLTK_LIBS environment
1969 # variables. The latter handles any Tcl/Tk versions installed in
1970 # standard Unix directories.
1971 #
1972 # It would be desirable to also handle here the case where
Ned Deily1731d6d2020-05-18 04:32:38 -04001973 # you want to build and link with a framework build of Tcl and Tk
1974 # that is not in /Library/Frameworks, say, in your private
1975 # $HOME/Library/Frameworks directory or elsewhere. It turns
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301976 # out to be difficult to make that work automatically here
Ned Deily1731d6d2020-05-18 04:32:38 -04001977 # without bringing into play more tools and magic. That case
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301978 # can be handled using a recipe with the right arguments
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001979 # to detect_tkinter_fromenv().
Ned Deily1731d6d2020-05-18 04:32:38 -04001980 #
1981 # Note also that the fallback case here is to try to use the
1982 # Apple-supplied Tcl and Tk frameworks in /System/Library but
1983 # be forewarned that they are deprecated by Apple and typically
1984 # out-of-date and buggy; their use should be avoided if at
1985 # all possible by installing a newer version of Tcl and Tk in
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301986 # /Library/Frameworks before building Python without
Ned Deily1731d6d2020-05-18 04:32:38 -04001987 # an explicit SDK or by configuring build arguments explicitly.
1988
Jack Jansen0b06be72002-06-21 14:48:38 +00001989 from os.path import join, exists
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001990
Ned Deily1731d6d2020-05-18 04:32:38 -04001991 sysroot = macosx_sdk_root() # path to the SDK or '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001992
Ned Deily1731d6d2020-05-18 04:32:38 -04001993 if macosx_sdk_specified():
1994 # Use case #2: an SDK other than '/' was specified.
1995 # Only search there.
1996 framework_dirs = [
1997 join(sysroot, 'Library', 'Frameworks'),
1998 join(sysroot, 'System', 'Library', 'Frameworks'),
1999 ]
2000 else:
2001 # Use case #1: no explicit SDK selected.
2002 # Search the local system-wide /Library/Frameworks,
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302003 # not the one in the default SDK, otherwise fall back to
Ned Deily1731d6d2020-05-18 04:32:38 -04002004 # /System/Library/Frameworks whose header files may be in
2005 # the default SDK or, on older systems, actually installed.
2006 framework_dirs = [
2007 join('/', 'Library', 'Frameworks'),
2008 join(sysroot, 'System', 'Library', 'Frameworks'),
2009 ]
2010
2011 # Find the directory that contains the Tcl.framework and
2012 # Tk.framework bundles.
Jack Jansen0b06be72002-06-21 14:48:38 +00002013 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00002014 # both Tcl.framework and Tk.framework should be present
Jack Jansen0b06be72002-06-21 14:48:38 +00002015 for fw in 'Tcl', 'Tk':
Ned Deily1731d6d2020-05-18 04:32:38 -04002016 if not exists(join(F, fw + '.framework')):
2017 break
Jack Jansen0b06be72002-06-21 14:48:38 +00002018 else:
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302019 # ok, F is now directory with both frameworks. Continue
Jack Jansen0b06be72002-06-21 14:48:38 +00002020 # building
2021 break
2022 else:
2023 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
2024 # will now resume.
Victor Stinner4cbea512019-02-28 17:48:38 +01002025 return False
Tim Peters2c60f7a2003-01-29 03:49:43 +00002026
Jack Jansen0b06be72002-06-21 14:48:38 +00002027 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00002028 join(F, fw + '.framework', H)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002029 for fw in ('Tcl', 'Tk')
Ned Deily1731d6d2020-05-18 04:32:38 -04002030 for H in ('Headers',)
Jack Jansen0b06be72002-06-21 14:48:38 +00002031 ]
2032
Ned Deily1731d6d2020-05-18 04:32:38 -04002033 # Add the base framework directory as well
2034 compile_args = ['-F', F]
Jack Jansen0b06be72002-06-21 14:48:38 +00002035
Ned Deily1731d6d2020-05-18 04:32:38 -04002036 # Do not build tkinter for archs that this Tk was not built with.
Georg Brandlfcaf9102008-07-16 02:17:56 +00002037 cflags = sysconfig.get_config_vars('CFLAGS')[0]
R David Murray44b548d2016-09-08 13:59:53 -04002038 archs = re.findall(r'-arch\s+(\w+)', cflags)
Georg Brandlfcaf9102008-07-16 02:17:56 +00002039
Ronald Oussorend097efe2009-09-15 19:07:58 +00002040 tmpfile = os.path.join(self.build_temp, 'tk.arch')
2041 if not os.path.exists(self.build_temp):
2042 os.makedirs(self.build_temp)
2043
Ned Deily1731d6d2020-05-18 04:32:38 -04002044 run_command(
2045 "file {}/Tk.framework/Tk | grep 'for architecture' > {}".format(F, tmpfile)
2046 )
Brett Cannon9f5db072010-10-29 20:19:27 +00002047 with open(tmpfile) as fp:
2048 detected_archs = []
2049 for ln in fp:
2050 a = ln.split()[-1]
2051 if a in archs:
2052 detected_archs.append(ln.split()[-1])
Ronald Oussorend097efe2009-09-15 19:07:58 +00002053 os.unlink(tmpfile)
2054
Ned Deily1731d6d2020-05-18 04:32:38 -04002055 arch_args = []
Ronald Oussorend097efe2009-09-15 19:07:58 +00002056 for a in detected_archs:
Ned Deily1731d6d2020-05-18 04:32:38 -04002057 arch_args.append('-arch')
2058 arch_args.append(a)
2059
2060 compile_args += arch_args
2061 link_args = [','.join(['-Wl', '-F', F, '-framework', 'Tcl', '-framework', 'Tk']), *arch_args]
2062
2063 # The X11/xlib.h file bundled in the Tk sources can cause function
2064 # prototype warnings from the compiler. Since we cannot easily fix
2065 # that, suppress the warnings here instead.
2066 if '-Wstrict-prototypes' in cflags.split():
2067 compile_args.append('-Wno-strict-prototypes')
Georg Brandlfcaf9102008-07-16 02:17:56 +00002068
Victor Stinnercfe172d2019-03-01 18:21:49 +01002069 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2070 define_macros=[('WITH_APPINIT', 1)],
2071 include_dirs=include_dirs,
2072 libraries=[],
Ned Deily1731d6d2020-05-18 04:32:38 -04002073 extra_compile_args=compile_args,
2074 extra_link_args=link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01002075 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002076
Victor Stinner625dbf22019-03-01 15:59:39 +01002077 def detect_tkinter(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002078 # The _tkinter module.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002079 #
2080 # Detection of Tcl/Tk is attempted in the following order:
2081 # - Through environment variables.
2082 # - Platform specific detection of Tcl/Tk (currently only macOS).
2083 # - Search of various standard Unix header/library paths.
2084 #
2085 # Detection stops at the first successful method.
Michael W. Hudson5b109102002-01-23 15:04:41 +00002086
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002087 # Check for Tcl and Tk at the locations indicated by _TCLTK_INCLUDES
2088 # and _TCLTK_LIBS environment variables.
2089 if self.detect_tkinter_fromenv():
Victor Stinner5ec33a12019-03-01 16:43:28 +01002090 return True
Ned Deilyd819b932013-09-06 01:07:05 -07002091
Jack Jansen0b06be72002-06-21 14:48:38 +00002092 # Rather than complicate the code below, detecting and building
2093 # AquaTk is a separate method. Only one Tkinter will be built on
2094 # Darwin - either AquaTk, if it is found, or X11 based Tk.
Victor Stinner5ec33a12019-03-01 16:43:28 +01002095 if (MACOS and self.detect_tkinter_darwin()):
2096 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002097
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002098 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002099 # The versions with dots are used on Unix, and the versions without
2100 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002101 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polo5d377bd2009-08-16 14:44:14 +00002102 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
2103 '8.2', '82', '8.1', '81', '8.0', '80']:
Victor Stinner625dbf22019-03-01 15:59:39 +01002104 tklib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002105 'tk' + version)
Victor Stinner625dbf22019-03-01 15:59:39 +01002106 tcllib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002107 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00002108 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002109 # Exit the loop when we've found the Tcl/Tk libraries
2110 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002111
Fredrik Lundhade711a2001-01-24 08:00:28 +00002112 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002113 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002114 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002115 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002116 dotversion = version
Victor Stinner4cbea512019-02-28 17:48:38 +01002117 if '.' not in dotversion and "bsd" in HOST_PLATFORM.lower():
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002118 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
2119 # but the include subdirs are named like .../include/tcl8.3.
2120 dotversion = dotversion[:-1] + '.' + dotversion[-1]
2121 tcl_include_sub = []
2122 tk_include_sub = []
Victor Stinner625dbf22019-03-01 15:59:39 +01002123 for dir in self.inc_dirs:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002124 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
2125 tk_include_sub += [dir + os.sep + "tk" + dotversion]
2126 tk_include_sub += tcl_include_sub
Victor Stinner625dbf22019-03-01 15:59:39 +01002127 tcl_includes = find_file('tcl.h', self.inc_dirs, tcl_include_sub)
2128 tk_includes = find_file('tk.h', self.inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002129
Martin v. Löwise86a59a2003-05-03 08:45:51 +00002130 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002131 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002132 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Victor Stinner5ec33a12019-03-01 16:43:28 +01002133 return False
Fredrik Lundhade711a2001-01-24 08:00:28 +00002134
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002135 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002136
Victor Stinnercfe172d2019-03-01 18:21:49 +01002137 include_dirs = []
2138 libs = []
2139 defs = []
2140 added_lib_dirs = []
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002141 for dir in tcl_includes + tk_includes:
2142 if dir not in include_dirs:
2143 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002144
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002145 # Check for various platform-specific directories
Victor Stinner4cbea512019-02-28 17:48:38 +01002146 if HOST_PLATFORM == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002147 include_dirs.append('/usr/openwin/include')
2148 added_lib_dirs.append('/usr/openwin/lib')
2149 elif os.path.exists('/usr/X11R6/include'):
2150 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00002151 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002152 added_lib_dirs.append('/usr/X11R6/lib')
2153 elif os.path.exists('/usr/X11R5/include'):
2154 include_dirs.append('/usr/X11R5/include')
2155 added_lib_dirs.append('/usr/X11R5/lib')
2156 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002157 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002158 include_dirs.append('/usr/X11/include')
2159 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002160
Jason Tishler9181c942003-02-05 15:16:17 +00002161 # If Cygwin, then verify that X is installed before proceeding
Victor Stinner4cbea512019-02-28 17:48:38 +01002162 if CYGWIN:
Jason Tishler9181c942003-02-05 15:16:17 +00002163 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
2164 if x11_inc is None:
Victor Stinner5ec33a12019-03-01 16:43:28 +01002165 return False
Jason Tishler9181c942003-02-05 15:16:17 +00002166
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002167 # Check for BLT extension
Victor Stinner625dbf22019-03-01 15:59:39 +01002168 if self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002169 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002170 defs.append( ('WITH_BLT', 1) )
2171 libs.append('BLT8.0')
Victor Stinner625dbf22019-03-01 15:59:39 +01002172 elif self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002173 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00002174 defs.append( ('WITH_BLT', 1) )
2175 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002176
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002177 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00002178 libs.append('tk'+ version)
2179 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002180
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002181 # Finally, link with the X11 libraries (not appropriate on cygwin)
Victor Stinner4cbea512019-02-28 17:48:38 +01002182 if not CYGWIN:
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002183 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002184
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002185 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002186 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002187 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002188 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002189 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002190 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002191 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002192
Victor Stinnercfe172d2019-03-01 18:21:49 +01002193 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2194 define_macros=[('WITH_APPINIT', 1)] + defs,
2195 include_dirs=include_dirs,
2196 libraries=libs,
2197 library_dirs=added_lib_dirs))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002198 return True
2199
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002200 def configure_ctypes(self, ext):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002201 return True
2202
Victor Stinner625dbf22019-03-01 15:59:39 +01002203 def detect_ctypes(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01002204 # Thomas Heller's _ctypes module
Ronald Oussoren41761932020-11-08 10:05:27 +01002205
2206 if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
2207 self.use_system_libffi = True
2208 else:
2209 self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
2210
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002211 include_dirs = []
Victor Stinner1ae035b2020-04-17 17:47:20 +02002212 extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002213 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00002214 sources = ['_ctypes/_ctypes.c',
2215 '_ctypes/callbacks.c',
2216 '_ctypes/callproc.c',
2217 '_ctypes/stgdict.c',
Thomas Heller864cc672010-08-08 17:58:53 +00002218 '_ctypes/cfield.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00002219 depends = ['_ctypes/ctypes.h']
2220
Victor Stinner4cbea512019-02-28 17:48:38 +01002221 if MACOS:
Ronald Oussoren2decf222010-09-05 18:25:59 +00002222 sources.append('_ctypes/malloc_closure.c')
Ronald Oussoren41761932020-11-08 10:05:27 +01002223 extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
Christian Heimes78644762008-03-04 23:39:23 +00002224 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00002225 include_dirs.append('_ctypes/darwin')
Thomas Hellercf567c12006-03-08 19:51:58 +00002226
Victor Stinner4cbea512019-02-28 17:48:38 +01002227 elif HOST_PLATFORM == 'sunos5':
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002228 # XXX This shouldn't be necessary; it appears that some
2229 # of the assembler code is non-PIC (i.e. it has relocations
2230 # when it shouldn't. The proper fix would be to rewrite
2231 # the assembler code to be PIC.
2232 # This only works with GCC; the Sun compiler likely refuses
2233 # this option. If you want to compile ctypes with the Sun
2234 # compiler, please research a proper solution, instead of
2235 # finding some -z option for the Sun compiler.
2236 extra_link_args.append('-mimpure-text')
2237
Victor Stinner4cbea512019-02-28 17:48:38 +01002238 elif HOST_PLATFORM.startswith('hp-ux'):
Thomas Heller3eaaeb42008-05-23 17:26:46 +00002239 extra_link_args.append('-fPIC')
2240
Thomas Hellercf567c12006-03-08 19:51:58 +00002241 ext = Extension('_ctypes',
2242 include_dirs=include_dirs,
2243 extra_compile_args=extra_compile_args,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002244 extra_link_args=extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002245 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00002246 sources=sources,
2247 depends=depends)
Victor Stinnercfe172d2019-03-01 18:21:49 +01002248 self.add(ext)
2249 if TEST_EXTENSIONS:
2250 # function my_sqrt() needs libm for sqrt()
2251 self.add(Extension('_ctypes_test',
2252 sources=['_ctypes/_ctypes_test.c'],
2253 libraries=['m']))
Thomas Hellercf567c12006-03-08 19:51:58 +00002254
Ronald Oussoren41761932020-11-08 10:05:27 +01002255 ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
2256 ffi_lib = None
2257
Victor Stinner625dbf22019-03-01 15:59:39 +01002258 ffi_inc_dirs = self.inc_dirs.copy()
Victor Stinner4cbea512019-02-28 17:48:38 +01002259 if MACOS:
Ronald Oussoren41761932020-11-08 10:05:27 +01002260 ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
Christian Heimes78644762008-03-04 23:39:23 +00002261
Ronald Oussoren41761932020-11-08 10:05:27 +01002262 if not ffi_inc:
2263 if os.path.exists(ffi_in_sdk):
2264 ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
2265 ffi_inc = ffi_in_sdk
2266 ffi_lib = 'ffi'
2267 else:
2268 # OS X 10.5 comes with libffi.dylib; the include files are
2269 # in /usr/include/ffi
2270 ffi_inc_dirs.append('/usr/include/ffi')
2271
2272 if not ffi_inc:
2273 found = find_file('ffi.h', [], ffi_inc_dirs)
2274 if found:
2275 ffi_inc = found[0]
2276 if ffi_inc:
2277 ffi_h = ffi_inc + '/ffi.h'
Shlomi Fish6d51b872017-09-06 23:19:19 +03002278 if not os.path.exists(ffi_h):
2279 ffi_inc = None
2280 print('Header file {} does not exist'.format(ffi_h))
Ronald Oussoren41761932020-11-08 10:05:27 +01002281 if ffi_lib is None and ffi_inc:
doko@ubuntu.comae683652016-06-05 01:38:29 +02002282 for lib_name in ('ffi', 'ffi_pic'):
Victor Stinner625dbf22019-03-01 15:59:39 +01002283 if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002284 ffi_lib = lib_name
2285 break
2286
2287 if ffi_inc and ffi_lib:
Ronald Oussoren41761932020-11-08 10:05:27 +01002288 ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
2289 if grep_headers_for('ffi_prep_cif_var', ffi_headers):
2290 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
2291 if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
2292 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
2293 if grep_headers_for('ffi_closure_alloc', ffi_headers):
2294 ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
2295
2296 ext.include_dirs.append(ffi_inc)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002297 ext.libraries.append(ffi_lib)
2298 self.use_system_libffi = True
2299
Christian Heimes5bb96922018-02-25 10:22:14 +01002300 if sysconfig.get_config_var('HAVE_LIBDL'):
2301 # for dlopen, see bpo-32647
2302 ext.libraries.append('dl')
2303
Victor Stinner5ec33a12019-03-01 16:43:28 +01002304 def detect_decimal(self):
2305 # Stefan Krah's _decimal module
Stefan Krah60187b52012-03-23 19:06:27 +01002306 extra_compile_args = []
Stefan Kraha10e2fb2012-09-01 14:21:22 +02002307 undef_macros = []
Stefan Krah60187b52012-03-23 19:06:27 +01002308 if '--with-system-libmpdec' in sysconfig.get_config_var("CONFIG_ARGS"):
2309 include_dirs = []
Antoine Pitrou73b20ae2021-03-30 18:11:06 +02002310 libraries = ['mpdec']
Stefan Krah60187b52012-03-23 19:06:27 +01002311 sources = ['_decimal/_decimal.c']
2312 depends = ['_decimal/docstrings.h']
2313 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01002314 include_dirs = [os.path.abspath(os.path.join(self.srcdir,
Ned Deily458a6fb2012-04-01 02:30:46 -07002315 'Modules',
2316 '_decimal',
2317 'libmpdec'))]
Stefan Krahbd4ed772017-12-06 18:24:17 +01002318 libraries = ['m']
Stefan Krah60187b52012-03-23 19:06:27 +01002319 sources = [
2320 '_decimal/_decimal.c',
2321 '_decimal/libmpdec/basearith.c',
2322 '_decimal/libmpdec/constants.c',
2323 '_decimal/libmpdec/context.c',
2324 '_decimal/libmpdec/convolute.c',
2325 '_decimal/libmpdec/crt.c',
2326 '_decimal/libmpdec/difradix2.c',
2327 '_decimal/libmpdec/fnt.c',
2328 '_decimal/libmpdec/fourstep.c',
2329 '_decimal/libmpdec/io.c',
Stefan Krahf117d872019-07-10 18:27:38 +02002330 '_decimal/libmpdec/mpalloc.c',
Stefan Krah60187b52012-03-23 19:06:27 +01002331 '_decimal/libmpdec/mpdecimal.c',
2332 '_decimal/libmpdec/numbertheory.c',
2333 '_decimal/libmpdec/sixstep.c',
2334 '_decimal/libmpdec/transpose.c',
2335 ]
2336 depends = [
2337 '_decimal/docstrings.h',
2338 '_decimal/libmpdec/basearith.h',
2339 '_decimal/libmpdec/bits.h',
2340 '_decimal/libmpdec/constants.h',
2341 '_decimal/libmpdec/convolute.h',
2342 '_decimal/libmpdec/crt.h',
2343 '_decimal/libmpdec/difradix2.h',
2344 '_decimal/libmpdec/fnt.h',
2345 '_decimal/libmpdec/fourstep.h',
2346 '_decimal/libmpdec/io.h',
Stefan Krah8d013a82016-04-26 16:34:41 +02002347 '_decimal/libmpdec/mpalloc.h',
Stefan Krah60187b52012-03-23 19:06:27 +01002348 '_decimal/libmpdec/mpdecimal.h',
2349 '_decimal/libmpdec/numbertheory.h',
2350 '_decimal/libmpdec/sixstep.h',
2351 '_decimal/libmpdec/transpose.h',
2352 '_decimal/libmpdec/typearith.h',
2353 '_decimal/libmpdec/umodarith.h',
2354 ]
2355
Stefan Krah1919b7e2012-03-21 18:25:23 +01002356 config = {
2357 'x64': [('CONFIG_64','1'), ('ASM','1')],
2358 'uint128': [('CONFIG_64','1'), ('ANSI','1'), ('HAVE_UINT128_T','1')],
2359 'ansi64': [('CONFIG_64','1'), ('ANSI','1')],
2360 'ppro': [('CONFIG_32','1'), ('PPRO','1'), ('ASM','1')],
2361 'ansi32': [('CONFIG_32','1'), ('ANSI','1')],
2362 'ansi-legacy': [('CONFIG_32','1'), ('ANSI','1'),
2363 ('LEGACY_COMPILER','1')],
2364 'universal': [('UNIVERSAL','1')]
2365 }
2366
Stefan Krah1919b7e2012-03-21 18:25:23 +01002367 cc = sysconfig.get_config_var('CC')
2368 sizeof_size_t = sysconfig.get_config_var('SIZEOF_SIZE_T')
2369 machine = os.environ.get('PYTHON_DECIMAL_WITH_MACHINE')
2370
2371 if machine:
2372 # Override automatic configuration to facilitate testing.
2373 define_macros = config[machine]
Victor Stinner4cbea512019-02-28 17:48:38 +01002374 elif MACOS:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002375 # Universal here means: build with the same options Python
2376 # was built with.
2377 define_macros = config['universal']
2378 elif sizeof_size_t == 8:
2379 if sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X64'):
2380 define_macros = config['x64']
2381 elif sysconfig.get_config_var('HAVE_GCC_UINT128_T'):
2382 define_macros = config['uint128']
2383 else:
2384 define_macros = config['ansi64']
2385 elif sizeof_size_t == 4:
2386 ppro = sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X87')
2387 if ppro and ('gcc' in cc or 'clang' in cc) and \
Victor Stinner4cbea512019-02-28 17:48:38 +01002388 not 'sunos' in HOST_PLATFORM:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002389 # solaris: problems with register allocation.
2390 # icc >= 11.0 works as well.
2391 define_macros = config['ppro']
Stefan Krahce23dbc2012-09-30 21:12:53 +02002392 extra_compile_args.append('-Wno-unknown-pragmas')
Stefan Krah1919b7e2012-03-21 18:25:23 +01002393 else:
2394 define_macros = config['ansi32']
2395 else:
2396 raise DistutilsError("_decimal: unsupported architecture")
2397
2398 # Workarounds for toolchain bugs:
2399 if sysconfig.get_config_var('HAVE_IPA_PURE_CONST_BUG'):
2400 # Some versions of gcc miscompile inline asm:
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002401 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46491
2402 # https://gcc.gnu.org/ml/gcc/2010-11/msg00366.html
Stefan Krah1919b7e2012-03-21 18:25:23 +01002403 extra_compile_args.append('-fno-ipa-pure-const')
2404 if sysconfig.get_config_var('HAVE_GLIBC_MEMMOVE_BUG'):
2405 # _FORTIFY_SOURCE wrappers for memmove and bcopy are incorrect:
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002406 # https://sourceware.org/ml/libc-alpha/2010-12/msg00009.html
Stefan Krah1919b7e2012-03-21 18:25:23 +01002407 undef_macros.append('_FORTIFY_SOURCE')
2408
Stefan Krah1919b7e2012-03-21 18:25:23 +01002409 # Uncomment for extra functionality:
2410 #define_macros.append(('EXTRA_FUNCTIONALITY', 1))
Victor Stinner8058bda2019-03-01 15:31:45 +01002411 self.add(Extension('_decimal',
2412 include_dirs=include_dirs,
2413 libraries=libraries,
2414 define_macros=define_macros,
2415 undef_macros=undef_macros,
2416 extra_compile_args=extra_compile_args,
2417 sources=sources,
2418 depends=depends))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002419
Victor Stinner5ec33a12019-03-01 16:43:28 +01002420 def detect_openssl_hashlib(self):
2421 # Detect SSL support for the socket module (via _ssl)
Christian Heimesff5be6e2018-01-20 13:19:21 +01002422 config_vars = sysconfig.get_config_vars()
2423
2424 def split_var(name, sep):
2425 # poor man's shlex, the re module is not available yet.
2426 value = config_vars.get(name)
2427 if not value:
2428 return ()
2429 # This trick works because ax_check_openssl uses --libs-only-L,
2430 # --libs-only-l, and --cflags-only-I.
2431 value = ' ' + value
2432 sep = ' ' + sep
2433 return [v.strip() for v in value.split(sep) if v.strip()]
2434
2435 openssl_includes = split_var('OPENSSL_INCLUDES', '-I')
2436 openssl_libdirs = split_var('OPENSSL_LDFLAGS', '-L')
2437 openssl_libs = split_var('OPENSSL_LIBS', '-l')
Christian Heimes32eba612021-03-19 10:29:25 +01002438 openssl_rpath = config_vars.get('OPENSSL_RPATH')
Christian Heimesff5be6e2018-01-20 13:19:21 +01002439 if not openssl_libs:
2440 # libssl and libcrypto not found
Christian Heimes8abc3f42019-04-09 18:40:12 +02002441 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002442 return None, None
2443
2444 # Find OpenSSL includes
2445 ssl_incs = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002446 'openssl/ssl.h', self.inc_dirs, openssl_includes
Christian Heimesff5be6e2018-01-20 13:19:21 +01002447 )
2448 if ssl_incs is None:
Christian Heimes8abc3f42019-04-09 18:40:12 +02002449 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002450 return None, None
2451
Christian Heimes32eba612021-03-19 10:29:25 +01002452 if openssl_rpath == 'auto':
2453 runtime_library_dirs = openssl_libdirs[:]
2454 elif not openssl_rpath:
2455 runtime_library_dirs = []
2456 else:
2457 runtime_library_dirs = [openssl_rpath]
2458
Christian Heimesbacefbf2021-03-27 18:03:54 +01002459 openssl_extension_kwargs = dict(
2460 include_dirs=openssl_includes,
2461 library_dirs=openssl_libdirs,
2462 libraries=openssl_libs,
2463 runtime_library_dirs=runtime_library_dirs,
2464 )
2465
2466 # This static linking is NOT OFFICIALLY SUPPORTED.
2467 # Requires static OpenSSL build with position-independent code. Some
2468 # features like DSO engines or external OSSL providers don't work.
2469 # Only tested on GCC and clang on X86_64.
2470 if os.environ.get("PY_UNSUPPORTED_OPENSSL_BUILD") == "static":
2471 extra_linker_args = []
2472 for lib in openssl_extension_kwargs["libraries"]:
2473 # link statically
2474 extra_linker_args.append(f"-l:lib{lib}.a")
2475 # don't export symbols
2476 extra_linker_args.append(f"-Wl,--exclude-libs,lib{lib}.a")
2477 openssl_extension_kwargs["extra_link_args"] = extra_linker_args
2478 # don't link OpenSSL shared libraries.
Christian Heimes5f879152021-04-26 15:13:34 +02002479 # include libz for OpenSSL build flavors with compression support
2480 openssl_extension_kwargs["libraries"] = ["z"]
Christian Heimesbacefbf2021-03-27 18:03:54 +01002481
Christian Heimes39258d32021-04-17 11:36:35 +02002482 self.add(
2483 Extension(
2484 '_ssl',
2485 ['_ssl.c'],
Christian Heimes666991f2021-04-26 15:01:40 +02002486 depends=[
2487 'socketmodule.h',
2488 '_ssl.h',
2489 '_ssl/debughelpers.c',
2490 '_ssl/misc.c',
2491 '_ssl/cert.c',
2492 ],
Christian Heimes39258d32021-04-17 11:36:35 +02002493 **openssl_extension_kwargs
Christian Heimesc7f70692019-05-31 11:44:05 +02002494 )
Christian Heimes39258d32021-04-17 11:36:35 +02002495 )
Christian Heimesbacefbf2021-03-27 18:03:54 +01002496 self.add(
2497 Extension(
2498 '_hashlib',
2499 ['_hashopenssl.c'],
2500 depends=['hashlib.h'],
2501 **openssl_extension_kwargs,
2502 )
2503 )
Christian Heimesff5be6e2018-01-20 13:19:21 +01002504
xdegaye2ee077f2019-04-09 17:20:08 +02002505 def detect_hash_builtins(self):
Christian Heimes9b60e552020-05-15 23:54:53 +02002506 # By default we always compile these even when OpenSSL is available
2507 # (issue #14693). It's harmless and the object code is tiny
2508 # (40-50 KiB per module, only loaded when actually used). Modules can
2509 # be disabled via the --with-builtin-hashlib-hashes configure flag.
2510 supported = {"md5", "sha1", "sha256", "sha512", "sha3", "blake2"}
Victor Stinner5ec33a12019-03-01 16:43:28 +01002511
Christian Heimes9b60e552020-05-15 23:54:53 +02002512 configured = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
2513 configured = configured.strip('"').lower()
2514 configured = {
2515 m.strip() for m in configured.split(",")
2516 }
Victor Stinner5ec33a12019-03-01 16:43:28 +01002517
Christian Heimes9b60e552020-05-15 23:54:53 +02002518 self.disabled_configure.extend(
2519 sorted(supported.difference(configured))
2520 )
Victor Stinner5ec33a12019-03-01 16:43:28 +01002521
Christian Heimes9b60e552020-05-15 23:54:53 +02002522 if "sha256" in configured:
2523 self.add(Extension(
2524 '_sha256', ['sha256module.c'],
2525 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2526 depends=['hashlib.h']
2527 ))
2528
2529 if "sha512" in configured:
2530 self.add(Extension(
2531 '_sha512', ['sha512module.c'],
2532 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2533 depends=['hashlib.h']
2534 ))
2535
2536 if "md5" in configured:
2537 self.add(Extension(
2538 '_md5', ['md5module.c'],
2539 depends=['hashlib.h']
2540 ))
2541
2542 if "sha1" in configured:
2543 self.add(Extension(
2544 '_sha1', ['sha1module.c'],
2545 depends=['hashlib.h']
2546 ))
2547
2548 if "blake2" in configured:
2549 blake2_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002550 os.path.join(escape(self.srcdir), 'Modules/_blake2/impl/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002551 )
2552 blake2_deps.append('hashlib.h')
2553 self.add(Extension(
2554 '_blake2',
2555 [
2556 '_blake2/blake2module.c',
2557 '_blake2/blake2b_impl.c',
2558 '_blake2/blake2s_impl.c'
2559 ],
2560 depends=blake2_deps
2561 ))
2562
2563 if "sha3" in configured:
2564 sha3_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002565 os.path.join(escape(self.srcdir), 'Modules/_sha3/kcp/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002566 )
2567 sha3_deps.append('hashlib.h')
2568 self.add(Extension(
2569 '_sha3',
2570 ['_sha3/sha3module.c'],
2571 depends=sha3_deps
2572 ))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002573
2574 def detect_nis(self):
Victor Stinner4cbea512019-02-28 17:48:38 +01002575 if MS_WINDOWS or CYGWIN or HOST_PLATFORM == 'qnx6':
Victor Stinner8058bda2019-03-01 15:31:45 +01002576 self.missing.append('nis')
2577 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002578
2579 libs = []
2580 library_dirs = []
2581 includes_dirs = []
2582
2583 # bpo-32521: glibc has deprecated Sun RPC for some time. Fedora 28
2584 # moved headers and libraries to libtirpc and libnsl. The headers
2585 # are in tircp and nsl sub directories.
2586 rpcsvc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002587 'rpcsvc/yp_prot.h', self.inc_dirs,
2588 [os.path.join(inc_dir, 'nsl') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002589 )
2590 rpc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002591 'rpc/rpc.h', self.inc_dirs,
2592 [os.path.join(inc_dir, 'tirpc') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002593 )
2594 if rpcsvc_inc is None or rpc_inc is None:
2595 # not found
Victor Stinner8058bda2019-03-01 15:31:45 +01002596 self.missing.append('nis')
2597 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002598 includes_dirs.extend(rpcsvc_inc)
2599 includes_dirs.extend(rpc_inc)
2600
Victor Stinner625dbf22019-03-01 15:59:39 +01002601 if self.compiler.find_library_file(self.lib_dirs, 'nsl'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002602 libs.append('nsl')
2603 else:
2604 # libnsl-devel: check for libnsl in nsl/ subdirectory
Victor Stinner625dbf22019-03-01 15:59:39 +01002605 nsl_dirs = [os.path.join(lib_dir, 'nsl') for lib_dir in self.lib_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002606 libnsl = self.compiler.find_library_file(nsl_dirs, 'nsl')
2607 if libnsl is not None:
2608 library_dirs.append(os.path.dirname(libnsl))
2609 libs.append('nsl')
2610
Victor Stinner625dbf22019-03-01 15:59:39 +01002611 if self.compiler.find_library_file(self.lib_dirs, 'tirpc'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002612 libs.append('tirpc')
2613
Victor Stinner8058bda2019-03-01 15:31:45 +01002614 self.add(Extension('nis', ['nismodule.c'],
2615 libraries=libs,
2616 library_dirs=library_dirs,
2617 include_dirs=includes_dirs))
Christian Heimes29a7df72018-01-26 23:28:46 +01002618
Christian Heimesff5be6e2018-01-20 13:19:21 +01002619
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00002620class PyBuildInstall(install):
2621 # Suppress the warning about installation into the lib_dynload
2622 # directory, which is not in sys.path when running Python during
2623 # installation:
2624 def initialize_options (self):
2625 install.initialize_options(self)
2626 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00002627
Éric Araujoe6792c12011-06-09 14:07:02 +02002628 # Customize subcommands to not install an egg-info file for Python
2629 sub_commands = [('install_lib', install.has_lib),
2630 ('install_headers', install.has_headers),
2631 ('install_scripts', install.has_scripts),
2632 ('install_data', install.has_data)]
2633
2634
Michael W. Hudson529a5052002-12-17 16:47:17 +00002635class PyBuildInstallLib(install_lib):
2636 # Do exactly what install_lib does but make sure correct access modes get
2637 # set on installed directories and files. All installed files with get
2638 # mode 644 unless they are a shared library in which case they will get
2639 # mode 755. All installed directories will get mode 755.
2640
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002641 # this is works for EXT_SUFFIX too, which ends with SHLIB_SUFFIX
2642 shlib_suffix = sysconfig.get_config_var("SHLIB_SUFFIX")
Michael W. Hudson529a5052002-12-17 16:47:17 +00002643
2644 def install(self):
2645 outfiles = install_lib.install(self)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002646 self.set_file_modes(outfiles, 0o644, 0o755)
2647 self.set_dir_modes(self.install_dir, 0o755)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002648 return outfiles
2649
2650 def set_file_modes(self, files, defaultMode, sharedLibMode):
Michael W. Hudson529a5052002-12-17 16:47:17 +00002651 if not files: return
2652
2653 for filename in files:
2654 if os.path.islink(filename): continue
2655 mode = defaultMode
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002656 if filename.endswith(self.shlib_suffix): mode = sharedLibMode
Michael W. Hudson529a5052002-12-17 16:47:17 +00002657 log.info("changing mode of %s to %o", filename, mode)
2658 if not self.dry_run: os.chmod(filename, mode)
2659
2660 def set_dir_modes(self, dirname, mode):
Amaury Forgeot d'Arc321e5332009-07-02 23:08:45 +00002661 for dirpath, dirnames, fnames in os.walk(dirname):
2662 if os.path.islink(dirpath):
2663 continue
2664 log.info("changing mode of %s to %o", dirpath, mode)
2665 if not self.dry_run: os.chmod(dirpath, mode)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002666
Victor Stinnerc991f242019-03-01 17:19:04 +01002667
Georg Brandlff52f762010-12-28 09:51:43 +00002668class PyBuildScripts(build_scripts):
2669 def copy_scripts(self):
2670 outfiles, updated_files = build_scripts.copy_scripts(self)
2671 fullversion = '-{0[0]}.{0[1]}'.format(sys.version_info)
2672 minoronly = '.{0[1]}'.format(sys.version_info)
2673 newoutfiles = []
2674 newupdated_files = []
2675 for filename in outfiles:
Brett Cannona8c34242018-04-20 14:15:40 -07002676 if filename.endswith('2to3'):
Georg Brandlff52f762010-12-28 09:51:43 +00002677 newfilename = filename + fullversion
2678 else:
2679 newfilename = filename + minoronly
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -07002680 log.info(f'renaming {filename} to {newfilename}')
Georg Brandlff52f762010-12-28 09:51:43 +00002681 os.rename(filename, newfilename)
2682 newoutfiles.append(newfilename)
2683 if filename in updated_files:
2684 newupdated_files.append(newfilename)
2685 return newoutfiles, newupdated_files
2686
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002687
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002688def main():
Victor Stinnercad80202021-01-19 23:04:49 +01002689 global LIST_MODULE_NAMES
2690
2691 if "--list-module-names" in sys.argv:
2692 LIST_MODULE_NAMES = True
2693 sys.argv.remove("--list-module-names")
2694
Victor Stinnerc991f242019-03-01 17:19:04 +01002695 set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
2696 set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
2697
2698 class DummyProcess:
2699 """Hack for parallel build"""
2700 ProcessPoolExecutor = None
2701
2702 sys.modules['concurrent.futures.process'] = DummyProcess
Paul Ganssle62972d92020-05-16 04:20:06 -04002703 validate_tzpath()
Victor Stinnerc991f242019-03-01 17:19:04 +01002704
Andrew M. Kuchling62686692001-05-21 20:48:09 +00002705 # turn off warnings when deprecated modules are imported
2706 import warnings
2707 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002708 setup(# PyPI Metadata (PEP 301)
2709 name = "Python",
2710 version = sys.version.split()[0],
Miss Islington (bot)f7f1c262021-07-30 07:25:28 -07002711 url = "https://www.python.org/%d.%d" % sys.version_info[:2],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002712 maintainer = "Guido van Rossum and the Python community",
2713 maintainer_email = "python-dev@python.org",
2714 description = "A high-level object-oriented programming language",
2715 long_description = SUMMARY.strip(),
2716 license = "PSF license",
Guido van Rossumc1f779c2007-07-03 08:25:58 +00002717 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002718 platforms = ["Many"],
2719
2720 # Build info
Georg Brandlff52f762010-12-28 09:51:43 +00002721 cmdclass = {'build_ext': PyBuildExt,
2722 'build_scripts': PyBuildScripts,
2723 'install': PyBuildInstall,
2724 'install_lib': PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002725 # The struct module is defined here, because build_ext won't be
2726 # called unless there's at least one extension module defined.
Victor Stinnercdad2722021-04-22 00:52:52 +02002727 ext_modules=[Extension('_struct', ['_struct.c'],
2728 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00002729
Georg Brandlff52f762010-12-28 09:51:43 +00002730 # If you change the scripts installed here, you also need to
2731 # check the PyBuildScripts command above, and change the links
2732 # created by the bininstall target in Makefile.pre.in
Benjamin Petersondfea1922009-05-23 17:13:14 +00002733 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
Brett Cannona8c34242018-04-20 14:15:40 -07002734 "Tools/scripts/2to3"]
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002735 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00002736
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002737# --install-platlib
2738if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002739 main()