blob: df434d48768e989e82b11a7d6ba103f08d09c887 [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
Victor Stinner625dbf22019-03-01 15:59:39 +01007import os
8import re
9import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +000010import sysconfig
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020011import warnings
Serhiy Storchaka93558682020-06-20 11:10:31 +030012from glob import glob, escape
Ronald Oussoren404a7192020-11-22 06:14:25 +010013import _osx_support
Michael W. Hudson529a5052002-12-17 16:47:17 +000014
Victor Stinner1ec63b62020-03-04 14:50:19 +010015
16try:
17 import subprocess
18 del subprocess
19 SUBPROCESS_BOOTSTRAP = False
20except ImportError:
Victor Stinner1ec63b62020-03-04 14:50:19 +010021 # Bootstrap Python: distutils.spawn uses subprocess to build C extensions,
22 # subprocess requires C extensions built by setup.py like _posixsubprocess.
23 #
Victor Stinneraddaaaa2020-03-09 23:45:59 +010024 # Use _bootsubprocess which only uses the os module.
Victor Stinner1ec63b62020-03-04 14:50:19 +010025 #
26 # It is dropped from sys.modules as soon as all C extension modules
27 # are built.
Victor Stinneraddaaaa2020-03-09 23:45:59 +010028 import _bootsubprocess
29 sys.modules['subprocess'] = _bootsubprocess
30 del _bootsubprocess
31 SUBPROCESS_BOOTSTRAP = True
Victor Stinner1ec63b62020-03-04 14:50:19 +010032
33
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020034with warnings.catch_warnings():
35 # bpo-41282 (PEP 632) deprecated distutils but setup.py still uses it
36 warnings.filterwarnings("ignore", "The distutils package is deprecated",
37 DeprecationWarning)
38
39 from distutils import log
40 from distutils.command.build_ext import build_ext
41 from distutils.command.build_scripts import build_scripts
42 from distutils.command.install import install
43 from distutils.command.install_lib import install_lib
44 from distutils.core import Extension, setup
45 from distutils.errors import CCompilerError, DistutilsError
46 from distutils.spawn import find_executable
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000047
Antoine Pitrou2c0a9162014-09-26 23:31:59 +020048
Victor Stinnercfe172d2019-03-01 18:21:49 +010049# Compile extensions used to test Python?
pxinwr277ce302020-12-30 20:50:39 +080050TEST_EXTENSIONS = (sysconfig.get_config_var('TEST_MODULES') == 'yes')
Victor Stinnercfe172d2019-03-01 18:21:49 +010051
52# This global variable is used to hold the list of modules to be disabled.
53DISABLED_MODULE_LIST = []
54
Victor Stinnercad80202021-01-19 23:04:49 +010055# --list-module-names option used by Tools/scripts/generate_module_names.py
56LIST_MODULE_NAMES = False
57
Victor Stinnercfe172d2019-03-01 18:21:49 +010058
doko@ubuntu.com93df16b2012-06-30 14:32:08 +020059def get_platform():
Victor Stinnerc991f242019-03-01 17:19:04 +010060 # Cross compiling
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +020061 if "_PYTHON_HOST_PLATFORM" in os.environ:
62 return os.environ["_PYTHON_HOST_PLATFORM"]
Victor Stinnerc991f242019-03-01 17:19:04 +010063
doko@ubuntu.com93df16b2012-06-30 14:32:08 +020064 # Get value of sys.platform
65 if sys.platform.startswith('osf1'):
66 return 'osf1'
67 return sys.platform
Victor Stinnerc991f242019-03-01 17:19:04 +010068
69
70CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
Victor Stinner4cbea512019-02-28 17:48:38 +010071HOST_PLATFORM = get_platform()
72MS_WINDOWS = (HOST_PLATFORM == 'win32')
73CYGWIN = (HOST_PLATFORM == 'cygwin')
74MACOS = (HOST_PLATFORM == 'darwin')
Michael Felt08970cb2019-06-21 15:58:00 +020075AIX = (HOST_PLATFORM.startswith('aix'))
Victor Stinner4cbea512019-02-28 17:48:38 +010076VXWORKS = ('vxworks' in HOST_PLATFORM)
pxinwr32f5fdd2019-02-27 19:09:28 +080077
Victor Stinnerc991f242019-03-01 17:19:04 +010078
79SUMMARY = """
80Python is an interpreted, interactive, object-oriented programming
81language. It is often compared to Tcl, Perl, Scheme or Java.
82
83Python combines remarkable power with very clear syntax. It has
84modules, classes, exceptions, very high level dynamic data types, and
85dynamic typing. There are interfaces to many system calls and
86libraries, as well as to various windowing systems (X11, Motif, Tk,
87Mac, MFC). New built-in modules are easily written in C or C++. Python
88is also usable as an extension language for applications that need a
89programmable interface.
90
91The Python implementation is portable: it runs on many brands of UNIX,
92on Windows, DOS, Mac, Amiga... If your favorite system isn't
93listed here, it may still be supported, if there's a C compiler for
94it. Ask around on comp.lang.python -- or just try compiling Python
95yourself.
96"""
97
98CLASSIFIERS = """
99Development Status :: 6 - Mature
100License :: OSI Approved :: Python Software Foundation License
101Natural Language :: English
102Programming Language :: C
103Programming Language :: Python
104Topic :: Software Development
105"""
106
107
Victor Stinner6b982c22020-04-01 01:10:07 +0200108def run_command(cmd):
109 status = os.system(cmd)
Victor Stinner65a796e2020-04-01 18:49:29 +0200110 return os.waitstatus_to_exitcode(status)
Victor Stinner6b982c22020-04-01 01:10:07 +0200111
112
Victor Stinnerc991f242019-03-01 17:19:04 +0100113# Set common compiler and linker flags derived from the Makefile,
114# reserved for building the interpreter and the stdlib modules.
115# See bpo-21121 and bpo-35257
116def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
117 flags = sysconfig.get_config_var(compiler_flags)
118 py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
119 sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
120
121
Michael W. Hudson39230b32002-01-16 15:26:48 +0000122def add_dir_to_list(dirlist, dir):
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000123 """Add the directory 'dir' to the list 'dirlist' (after any relative
124 directories) if:
125
Michael W. Hudson39230b32002-01-16 15:26:48 +0000126 1) 'dir' is not already in 'dirlist'
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000127 2) 'dir' actually exists, and is a directory.
128 """
129 if dir is None or not os.path.isdir(dir) or dir in dirlist:
130 return
131 for i, path in enumerate(dirlist):
132 if not os.path.isabs(path):
133 dirlist.insert(i + 1, dir)
Barry Warsaw34520cd2010-11-27 20:03:03 +0000134 return
135 dirlist.insert(0, dir)
Michael W. Hudson39230b32002-01-16 15:26:48 +0000136
Victor Stinnerc991f242019-03-01 17:19:04 +0100137
xdegaye77f51392017-11-25 17:25:30 +0100138def sysroot_paths(make_vars, subdirs):
139 """Get the paths of sysroot sub-directories.
140
141 * make_vars: a sequence of names of variables of the Makefile where
142 sysroot may be set.
143 * subdirs: a sequence of names of subdirectories used as the location for
144 headers or libraries.
145 """
146
147 dirs = []
148 for var_name in make_vars:
149 var = sysconfig.get_config_var(var_name)
150 if var is not None:
151 m = re.search(r'--sysroot=([^"]\S*|"[^"]+")', var)
152 if m is not None:
153 sysroot = m.group(1).strip('"')
154 for subdir in subdirs:
155 if os.path.isabs(subdir):
156 subdir = subdir[1:]
157 path = os.path.join(sysroot, subdir)
158 if os.path.isdir(path):
159 dirs.append(path)
160 break
161 return dirs
162
Ned Deily1731d6d2020-05-18 04:32:38 -0400163
Ned Deily0288dd62019-06-03 06:34:48 -0400164MACOS_SDK_ROOT = None
Ned Deily1731d6d2020-05-18 04:32:38 -0400165MACOS_SDK_SPECIFIED = None
Victor Stinnerc991f242019-03-01 17:19:04 +0100166
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000167def macosx_sdk_root():
Ned Deily0288dd62019-06-03 06:34:48 -0400168 """Return the directory of the current macOS SDK.
169
170 If no SDK was explicitly configured, call the compiler to find which
171 include files paths are being searched by default. Use '/' if the
172 compiler is searching /usr/include (meaning system header files are
173 installed) or use the root of an SDK if that is being searched.
174 (The SDK may be supplied via Xcode or via the Command Line Tools).
175 The SDK paths used by Apple-supplied tool chains depend on the
176 setting of various variables; see the xcrun man page for more info.
Ned Deily1731d6d2020-05-18 04:32:38 -0400177 Also sets MACOS_SDK_SPECIFIED for use by macosx_sdk_specified().
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000178 """
Ned Deily1731d6d2020-05-18 04:32:38 -0400179 global MACOS_SDK_ROOT, MACOS_SDK_SPECIFIED
Ned Deily0288dd62019-06-03 06:34:48 -0400180
181 # If already called, return cached result.
182 if MACOS_SDK_ROOT:
183 return MACOS_SDK_ROOT
184
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000185 cflags = sysconfig.get_config_var('CFLAGS')
Joshua Rootb3107002020-04-22 17:44:10 +1000186 m = re.search(r'-isysroot\s*(\S+)', cflags)
Ned Deily0288dd62019-06-03 06:34:48 -0400187 if m is not None:
188 MACOS_SDK_ROOT = m.group(1)
Ned Deily29afab62020-12-04 23:02:09 -0500189 MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000190 else:
Ronald Oussoren404a7192020-11-22 06:14:25 +0100191 MACOS_SDK_ROOT = _osx_support._default_sysroot(
192 sysconfig.get_config_var('CC'))
Ned Deily29afab62020-12-04 23:02:09 -0500193 MACOS_SDK_SPECIFIED = False
Ned Deily0288dd62019-06-03 06:34:48 -0400194
195 return MACOS_SDK_ROOT
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000196
Victor Stinnerc991f242019-03-01 17:19:04 +0100197
Ned Deily1731d6d2020-05-18 04:32:38 -0400198def macosx_sdk_specified():
199 """Returns true if an SDK was explicitly configured.
200
201 True if an SDK was selected at configure time, either by specifying
202 --enable-universalsdk=(something other than no or /) or by adding a
203 -isysroot option to CFLAGS. In some cases, like when making
204 decisions about macOS Tk framework paths, we need to be able to
205 know whether the user explicitly asked to build with an SDK versus
206 the implicit use of an SDK when header files are no longer
207 installed on a running system by the Command Line Tools.
208 """
209 global MACOS_SDK_SPECIFIED
210
211 # If already called, return cached result.
212 if MACOS_SDK_SPECIFIED:
213 return MACOS_SDK_SPECIFIED
214
215 # Find the sdk root and set MACOS_SDK_SPECIFIED
216 macosx_sdk_root()
217 return MACOS_SDK_SPECIFIED
218
219
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000220def is_macosx_sdk_path(path):
221 """
222 Returns True if 'path' can be located in an OSX SDK
223 """
Ned Deily2910a7b2012-07-30 02:35:58 -0700224 return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
225 or path.startswith('/System/')
226 or path.startswith('/Library/') )
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000227
Victor Stinnerc991f242019-03-01 17:19:04 +0100228
Ronald Oussoren41761932020-11-08 10:05:27 +0100229def grep_headers_for(function, headers):
230 for header in headers:
Ronald Oussoren7a27c7e2020-11-14 16:07:47 +0100231 with open(header, 'r', errors='surrogateescape') as f:
Ronald Oussoren41761932020-11-08 10:05:27 +0100232 if function in f.read():
233 return True
234 return False
235
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000236def find_file(filename, std_dirs, paths):
237 """Searches for the directory where a given file is located,
238 and returns a possibly-empty list of additional directories, or None
239 if the file couldn't be found at all.
Fredrik Lundhade711a2001-01-24 08:00:28 +0000240
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000241 'filename' is the name of a file, such as readline.h or libcrypto.a.
242 'std_dirs' is the list of standard system directories; if the
243 file is found in one of them, no additional directives are needed.
244 'paths' is a list of additional locations to check; if the file is
245 found in one of them, the resulting list will contain the directory.
246 """
Victor Stinner4cbea512019-02-28 17:48:38 +0100247 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000248 # Honor the MacOSX SDK setting when one was specified.
249 # An SDK is a directory with the same structure as a real
250 # system, but with only header files and libraries.
251 sysroot = macosx_sdk_root()
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000252
253 # Check the standard locations
254 for dir in std_dirs:
255 f = os.path.join(dir, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000256
Victor Stinner4cbea512019-02-28 17:48:38 +0100257 if MACOS and is_macosx_sdk_path(dir):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000258 f = os.path.join(sysroot, dir[1:], filename)
259
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000260 if os.path.exists(f): return []
261
262 # Check the additional directories
263 for dir in paths:
264 f = os.path.join(dir, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000265
Victor Stinner4cbea512019-02-28 17:48:38 +0100266 if MACOS and is_macosx_sdk_path(dir):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000267 f = os.path.join(sysroot, dir[1:], filename)
268
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000269 if os.path.exists(f):
270 return [dir]
271
272 # Not found anywhere
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000273 return None
274
Victor Stinnerc991f242019-03-01 17:19:04 +0100275
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000276def find_library_file(compiler, libname, std_dirs, paths):
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000277 result = compiler.find_library_file(std_dirs + paths, libname)
278 if result is None:
279 return None
Fredrik Lundhade711a2001-01-24 08:00:28 +0000280
Victor Stinner4cbea512019-02-28 17:48:38 +0100281 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000282 sysroot = macosx_sdk_root()
283
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000284 # Check whether the found file is in one of the standard directories
285 dirname = os.path.dirname(result)
286 for p in std_dirs:
287 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000288 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000289
Victor Stinner4cbea512019-02-28 17:48:38 +0100290 if MACOS and is_macosx_sdk_path(p):
Ned Deily020250f2016-02-25 00:56:38 +1100291 # Note that, as of Xcode 7, Apple SDKs may contain textual stub
292 # libraries with .tbd extensions rather than the normal .dylib
293 # shared libraries installed in /. The Apple compiler tool
294 # chain handles this transparently but it can cause problems
295 # for programs that are being built with an SDK and searching
296 # for specific libraries. Distutils find_library_file() now
297 # knows to also search for and return .tbd files. But callers
298 # of find_library_file need to keep in mind that the base filename
299 # of the returned SDK library file might have a different extension
300 # from that of the library file installed on the running system,
301 # for example:
302 # /Applications/Xcode.app/Contents/Developer/Platforms/
303 # MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/
304 # usr/lib/libedit.tbd
305 # vs
306 # /usr/lib/libedit.dylib
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000307 if os.path.join(sysroot, p[1:]) == dirname:
308 return [ ]
309
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000310 if p == dirname:
311 return [ ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000312
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000313 # Otherwise, it must have been in one of the additional directories,
314 # so we have to figure out which one.
315 for p in paths:
316 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000317 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000318
Victor Stinner4cbea512019-02-28 17:48:38 +0100319 if MACOS and is_macosx_sdk_path(p):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000320 if os.path.join(sysroot, p[1:]) == dirname:
321 return [ p ]
322
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000323 if p == dirname:
324 return [p]
325 else:
326 assert False, "Internal error: Path not found in std_dirs or paths"
Tim Peters2c60f7a2003-01-29 03:49:43 +0000327
Paul Ganssle62972d92020-05-16 04:20:06 -0400328def validate_tzpath():
329 base_tzpath = sysconfig.get_config_var('TZPATH')
330 if not base_tzpath:
331 return
332
333 tzpaths = base_tzpath.split(os.pathsep)
334 bad_paths = [tzpath for tzpath in tzpaths if not os.path.isabs(tzpath)]
335 if bad_paths:
336 raise ValueError('TZPATH must contain only absolute paths, '
337 + f'found:\n{tzpaths!r}\nwith invalid paths:\n'
338 + f'{bad_paths!r}')
Victor Stinnerc991f242019-03-01 17:19:04 +0100339
Jack Jansen144ebcc2001-08-05 22:31:19 +0000340def find_module_file(module, dirlist):
341 """Find a module in a set of possible folders. If it is not found
342 return the unadorned filename"""
343 list = find_file(module, [], dirlist)
344 if not list:
345 return module
346 if len(list) > 1:
Vinay Sajipdd917f82016-08-31 08:22:29 +0100347 log.info("WARNING: multiple copies of %s found", module)
Jack Jansen144ebcc2001-08-05 22:31:19 +0000348 return os.path.join(list[0], module)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000349
Victor Stinnerc991f242019-03-01 17:19:04 +0100350
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000351class PyBuildExt(build_ext):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000352
Guido van Rossumd8faa362007-04-27 19:54:29 +0000353 def __init__(self, dist):
354 build_ext.__init__(self, dist)
Victor Stinner625dbf22019-03-01 15:59:39 +0100355 self.srcdir = None
356 self.lib_dirs = None
357 self.inc_dirs = None
Victor Stinner5ec33a12019-03-01 16:43:28 +0100358 self.config_h_vars = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000359 self.failed = []
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400360 self.failed_on_import = []
Victor Stinner8058bda2019-03-01 15:31:45 +0100361 self.missing = []
Christian Heimes9b60e552020-05-15 23:54:53 +0200362 self.disabled_configure = []
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200363 if '-j' in os.environ.get('MAKEFLAGS', ''):
364 self.parallel = True
Guido van Rossumd8faa362007-04-27 19:54:29 +0000365
Victor Stinner8058bda2019-03-01 15:31:45 +0100366 def add(self, ext):
367 self.extensions.append(ext)
368
Victor Stinner00c77ae2020-03-04 18:44:49 +0100369 def set_srcdir(self):
Victor Stinner625dbf22019-03-01 15:59:39 +0100370 self.srcdir = sysconfig.get_config_var('srcdir')
371 if not self.srcdir:
372 # Maybe running on Windows but not using CYGWIN?
373 raise ValueError("No source directory; cannot proceed.")
374 self.srcdir = os.path.abspath(self.srcdir)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000375
Victor Stinner00c77ae2020-03-04 18:44:49 +0100376 def remove_disabled(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000377 # Remove modules that are present on the disabled list
Christian Heimes679db4a2008-01-18 09:56:22 +0000378 extensions = [ext for ext in self.extensions
Victor Stinner4cbea512019-02-28 17:48:38 +0100379 if ext.name not in DISABLED_MODULE_LIST]
Christian Heimes679db4a2008-01-18 09:56:22 +0000380 # move ctypes to the end, it depends on other modules
381 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
382 if "_ctypes" in ext_map:
383 ctypes = extensions.pop(ext_map["_ctypes"])
384 extensions.append(ctypes)
385 self.extensions = extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +0000386
Victor Stinner00c77ae2020-03-04 18:44:49 +0100387 def update_sources_depends(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000388 # Fix up the autodetected modules, prefixing all the source files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000389 # with Modules/.
Victor Stinner625dbf22019-03-01 15:59:39 +0100390 moddirlist = [os.path.join(self.srcdir, 'Modules')]
Michael W. Hudson5b109102002-01-23 15:04:41 +0000391
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000392 # Fix up the paths for scripts, too
Victor Stinner625dbf22019-03-01 15:59:39 +0100393 self.distribution.scripts = [os.path.join(self.srcdir, filename)
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000394 for filename in self.distribution.scripts]
395
Christian Heimesaf98da12008-01-27 15:18:18 +0000396 # Python header files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000397 headers = [sysconfig.get_config_h_filename()]
Serhiy Storchaka93558682020-06-20 11:10:31 +0300398 headers += glob(os.path.join(escape(sysconfig.get_path('include')), "*.h"))
Christian Heimesaf98da12008-01-27 15:18:18 +0000399
Xavier de Gaye84968b72016-10-29 16:57:20 +0200400 for ext in self.extensions:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000401 ext.sources = [ find_module_file(filename, moddirlist)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000402 for filename in ext.sources ]
Jeremy Hylton340043e2002-06-13 17:38:11 +0000403 if ext.depends is not None:
Neil Schemenauer014bf282009-02-05 16:35:45 +0000404 ext.depends = [find_module_file(filename, moddirlist)
Jeremy Hylton340043e2002-06-13 17:38:11 +0000405 for filename in ext.depends]
Christian Heimesaf98da12008-01-27 15:18:18 +0000406 else:
407 ext.depends = []
408 # re-compile extensions if a header file has been changed
409 ext.depends.extend(headers)
410
Victor Stinner00c77ae2020-03-04 18:44:49 +0100411 def remove_configured_extensions(self):
412 # The sysconfig variables built by makesetup that list the already
413 # built modules and the disabled modules as configured by the Setup
414 # files.
415 sysconf_built = sysconfig.get_config_var('MODBUILT_NAMES').split()
416 sysconf_dis = sysconfig.get_config_var('MODDISABLED_NAMES').split()
417
418 mods_built = []
419 mods_disabled = []
420 for ext in self.extensions:
xdegayec0364fc2017-05-27 18:25:03 +0200421 # If a module has already been built or has been disabled in the
422 # Setup files, don't build it here.
423 if ext.name in sysconf_built:
424 mods_built.append(ext)
425 if ext.name in sysconf_dis:
426 mods_disabled.append(ext)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000427
xdegayec0364fc2017-05-27 18:25:03 +0200428 mods_configured = mods_built + mods_disabled
429 if mods_configured:
Xavier de Gaye84968b72016-10-29 16:57:20 +0200430 self.extensions = [x for x in self.extensions if x not in
xdegayec0364fc2017-05-27 18:25:03 +0200431 mods_configured]
432 # Remove the shared libraries built by a previous build.
433 for ext in mods_configured:
434 fullpath = self.get_ext_fullpath(ext.name)
435 if os.path.exists(fullpath):
436 os.unlink(fullpath)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000437
Victor Stinner00c77ae2020-03-04 18:44:49 +0100438 return (mods_built, mods_disabled)
439
440 def set_compiler_executables(self):
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000441 # When you run "make CC=altcc" or something similar, you really want
442 # those environment variables passed into the setup.py phase. Here's
443 # a small set of useful ones.
444 compiler = os.environ.get('CC')
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000445 args = {}
446 # unfortunately, distutils doesn't let us provide separate C and C++
447 # compilers
448 if compiler is not None:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000449 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
450 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
Tarek Ziadé36797272010-07-22 12:50:05 +0000451 self.compiler.set_executables(**args)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000452
Victor Stinner00c77ae2020-03-04 18:44:49 +0100453 def build_extensions(self):
454 self.set_srcdir()
455
456 # Detect which modules should be compiled
457 self.detect_modules()
458
Victor Stinnercad80202021-01-19 23:04:49 +0100459 if not LIST_MODULE_NAMES:
460 self.remove_disabled()
Victor Stinner00c77ae2020-03-04 18:44:49 +0100461
462 self.update_sources_depends()
463 mods_built, mods_disabled = self.remove_configured_extensions()
464 self.set_compiler_executables()
465
Victor Stinnercad80202021-01-19 23:04:49 +0100466 if LIST_MODULE_NAMES:
467 for ext in self.extensions:
468 print(ext.name)
469 for name in self.missing:
470 print(name)
471 return
472
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000473 build_ext.build_extensions(self)
474
Victor Stinner1ec63b62020-03-04 14:50:19 +0100475 if SUBPROCESS_BOOTSTRAP:
476 # Drop our custom subprocess module:
477 # use the newly built subprocess module
478 del sys.modules['subprocess']
479
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200480 for ext in self.extensions:
481 self.check_extension_import(ext)
482
Victor Stinner00c77ae2020-03-04 18:44:49 +0100483 self.summary(mods_built, mods_disabled)
484
485 def summary(self, mods_built, mods_disabled):
Berker Peksag1d82a9c2014-10-01 05:11:13 +0300486 longest = max([len(e.name) for e in self.extensions], default=0)
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400487 if self.failed or self.failed_on_import:
488 all_failed = self.failed + self.failed_on_import
489 longest = max(longest, max([len(name) for name in all_failed]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000490
491 def print_three_column(lst):
492 lst.sort(key=str.lower)
493 # guarantee zip() doesn't drop anything
494 while len(lst) % 3:
495 lst.append("")
496 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
497 print("%-*s %-*s %-*s" % (longest, e, longest, f,
498 longest, g))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000499
Victor Stinner8058bda2019-03-01 15:31:45 +0100500 if self.missing:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000501 print()
Brett Cannonae95b4f2013-07-12 11:30:32 -0400502 print("Python build finished successfully!")
503 print("The necessary bits to build these optional modules were not "
504 "found:")
Victor Stinner8058bda2019-03-01 15:31:45 +0100505 print_three_column(self.missing)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000506 print("To find the necessary bits, look in setup.py in"
507 " detect_modules() for the module's name.")
508 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000509
xdegayec0364fc2017-05-27 18:25:03 +0200510 if mods_built:
511 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200512 print("The following modules found by detect_modules() in"
513 " setup.py, have been")
514 print("built by the Makefile instead, as configured by the"
515 " Setup files:")
xdegayec0364fc2017-05-27 18:25:03 +0200516 print_three_column([ext.name for ext in mods_built])
517 print()
518
519 if mods_disabled:
520 print()
521 print("The following modules found by detect_modules() in"
522 " setup.py have not")
523 print("been built, they are *disabled* in the Setup files:")
524 print_three_column([ext.name for ext in mods_disabled])
525 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200526
Christian Heimes9b60e552020-05-15 23:54:53 +0200527 if self.disabled_configure:
528 print()
529 print("The following modules found by detect_modules() in"
530 " setup.py have not")
531 print("been built, they are *disabled* by configure:")
532 print_three_column(self.disabled_configure)
533 print()
534
Guido van Rossumd8faa362007-04-27 19:54:29 +0000535 if self.failed:
536 failed = self.failed[:]
537 print()
538 print("Failed to build these modules:")
539 print_three_column(failed)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000540 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000541
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400542 if self.failed_on_import:
543 failed = self.failed_on_import[:]
544 print()
545 print("Following modules built successfully"
546 " but were removed because they could not be imported:")
547 print_three_column(failed)
548 print()
549
Christian Heimes61d478c2018-01-27 15:51:38 +0100550 if any('_ssl' in l
Victor Stinner8058bda2019-03-01 15:31:45 +0100551 for l in (self.missing, self.failed, self.failed_on_import)):
Christian Heimes61d478c2018-01-27 15:51:38 +0100552 print()
553 print("Could not build the ssl module!")
Christian Heimes39258d32021-04-17 11:36:35 +0200554 print("Python requires a OpenSSL 1.1.1 or newer")
Christian Heimes32eba612021-03-19 10:29:25 +0100555 if sysconfig.get_config_var("OPENSSL_LDFLAGS"):
556 print("Custom linker flags may require --with-openssl-rpath=auto")
Christian Heimes61d478c2018-01-27 15:51:38 +0100557 print()
558
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000559 def build_extension(self, ext):
560
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561 if ext.name == '_ctypes':
562 if not self.configure_ctypes(ext):
Zachary Waref40d4dd2016-09-17 01:25:24 -0500563 self.failed.append(ext.name)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000564 return
565
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000566 try:
567 build_ext.build_extension(self, ext)
Guido van Rossumb940e112007-01-10 16:19:56 +0000568 except (CCompilerError, DistutilsError) as why:
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000569 self.announce('WARNING: building of extension "%s" failed: %s' %
Victor Stinner625dbf22019-03-01 15:59:39 +0100570 (ext.name, why))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000571 self.failed.append(ext.name)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000572 return
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200573
574 def check_extension_import(self, ext):
575 # Don't try to import an extension that has failed to compile
576 if ext.name in self.failed:
577 self.announce(
578 'WARNING: skipping import check for failed build "%s"' %
579 ext.name, level=1)
580 return
581
Jack Jansenf49c6f92001-11-01 14:44:15 +0000582 # Workaround for Mac OS X: The Carbon-based modules cannot be
583 # reliably imported into a command-line Python
584 if 'Carbon' in ext.extra_link_args:
Michael W. Hudson5b109102002-01-23 15:04:41 +0000585 self.announce(
586 'WARNING: skipping import check for Carbon-based "%s"' %
587 ext.name)
588 return
Georg Brandlfcaf9102008-07-16 02:17:56 +0000589
Victor Stinner4cbea512019-02-28 17:48:38 +0100590 if MACOS and (
Benjamin Petersonfc576352008-07-16 02:39:02 +0000591 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
Georg Brandlfcaf9102008-07-16 02:17:56 +0000592 # Don't bother doing an import check when an extension was
593 # build with an explicit '-arch' flag on OSX. That's currently
594 # only used to build 32-bit only extensions in a 4-way
595 # universal build and loading 32-bit code into a 64-bit
596 # process will fail.
597 self.announce(
598 'WARNING: skipping import check for "%s"' %
599 ext.name)
600 return
601
Jason Tishler24cf7762002-05-22 16:46:15 +0000602 # Workaround for Cygwin: Cygwin currently has fork issues when many
603 # modules have been imported
Victor Stinner4cbea512019-02-28 17:48:38 +0100604 if CYGWIN:
Jason Tishler24cf7762002-05-22 16:46:15 +0000605 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
606 % ext.name)
607 return
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000608 ext_filename = os.path.join(
609 self.build_lib,
610 self.get_ext_filename(self.get_ext_fullname(ext.name)))
Guido van Rossumc3fee692008-07-17 16:23:53 +0000611
612 # If the build directory didn't exist when setup.py was
613 # started, sys.path_importer_cache has a negative result
614 # cached. Clear that cache before trying to import.
615 sys.path_importer_cache.clear()
616
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200617 # Don't try to load extensions for cross builds
Victor Stinner4cbea512019-02-28 17:48:38 +0100618 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200619 return
620
Brett Cannonca5ff3a2013-06-15 17:52:59 -0400621 loader = importlib.machinery.ExtensionFileLoader(ext.name, ext_filename)
Eric Snow335e14d2014-01-04 15:09:28 -0700622 spec = importlib.util.spec_from_file_location(ext.name, ext_filename,
623 loader=loader)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000624 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400625 importlib._bootstrap._load(spec)
Guido van Rossumb940e112007-01-10 16:19:56 +0000626 except ImportError as why:
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400627 self.failed_on_import.append(ext.name)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000628 self.announce('*** WARNING: renaming "%s" since importing it'
629 ' failed: %s' % (ext.name, why), level=3)
630 assert not self.inplace
631 basename, tail = os.path.splitext(ext_filename)
632 newname = basename + "_failed" + tail
633 if os.path.exists(newname):
634 os.remove(newname)
635 os.rename(ext_filename, newname)
636
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000637 except:
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000638 exc_type, why, tb = sys.exc_info()
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000639 self.announce('*** WARNING: importing extension "%s" '
640 'failed with %s: %s' % (ext.name, exc_type, why),
641 level=3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000642 self.failed.append(ext.name)
Fred Drake9028d0a2001-12-06 22:59:54 +0000643
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400644 def add_multiarch_paths(self):
645 # Debian/Ubuntu multiarch support.
646 # https://wiki.ubuntu.com/MultiarchSpec
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200647 cc = sysconfig.get_config_var('CC')
648 tmpfile = os.path.join(self.build_temp, 'multiarch')
649 if not os.path.exists(self.build_temp):
650 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200651 ret = run_command(
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200652 '%s -print-multiarch > %s 2> /dev/null' % (cc, tmpfile))
653 multiarch_path_component = ''
654 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200655 if ret == 0:
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200656 with open(tmpfile) as fp:
657 multiarch_path_component = fp.readline().strip()
658 finally:
659 os.unlink(tmpfile)
660
661 if multiarch_path_component != '':
662 add_dir_to_list(self.compiler.library_dirs,
663 '/usr/lib/' + multiarch_path_component)
664 add_dir_to_list(self.compiler.include_dirs,
665 '/usr/include/' + multiarch_path_component)
666 return
667
Barry Warsaw88e19452011-04-07 10:40:36 -0400668 if not find_executable('dpkg-architecture'):
669 return
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200670 opt = ''
Victor Stinner4cbea512019-02-28 17:48:38 +0100671 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200672 opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400673 tmpfile = os.path.join(self.build_temp, 'multiarch')
674 if not os.path.exists(self.build_temp):
675 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200676 ret = run_command(
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200677 'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
678 (opt, tmpfile))
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400679 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200680 if ret == 0:
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400681 with open(tmpfile) as fp:
682 multiarch_path_component = fp.readline().strip()
683 add_dir_to_list(self.compiler.library_dirs,
684 '/usr/lib/' + multiarch_path_component)
685 add_dir_to_list(self.compiler.include_dirs,
686 '/usr/include/' + multiarch_path_component)
687 finally:
688 os.unlink(tmpfile)
689
pxinwr5e45f1c2021-01-22 08:55:52 +0800690 def add_wrcc_search_dirs(self):
691 # add library search path by wr-cc, the compiler wrapper
692
693 def convert_mixed_path(path):
694 # convert path like C:\folder1\folder2/folder3/folder4
695 # to msys style /c/folder1/folder2/folder3/folder4
696 drive = path[0].lower()
697 left = path[2:].replace("\\", "/")
698 return "/" + drive + left
699
700 def add_search_path(line):
701 # On Windows building machine, VxWorks does
702 # cross builds under msys2 environment.
703 pathsep = (";" if sys.platform == "msys" else ":")
704 for d in line.strip().split("=")[1].split(pathsep):
705 d = d.strip()
706 if sys.platform == "msys":
707 # On Windows building machine, compiler
708 # returns mixed style path like:
709 # C:\folder1\folder2/folder3/folder4
710 d = convert_mixed_path(d)
711 d = os.path.normpath(d)
712 add_dir_to_list(self.compiler.library_dirs, d)
713
714 cc = sysconfig.get_config_var('CC')
715 tmpfile = os.path.join(self.build_temp, 'wrccpaths')
716 os.makedirs(self.build_temp, exist_ok=True)
717 try:
718 ret = run_command('%s --print-search-dirs >%s' % (cc, tmpfile))
719 if ret:
720 return
721 with open(tmpfile) as fp:
722 # Parse paths in libraries line. The line is like:
723 # On Linux, "libraries: = path1:path2:path3"
724 # On Windows, "libraries: = path1;path2;path3"
725 for line in fp:
726 if not line.startswith("libraries"):
727 continue
728 add_search_path(line)
729 finally:
730 try:
731 os.unlink(tmpfile)
732 except OSError:
733 pass
734
pxinwr32f5fdd2019-02-27 19:09:28 +0800735 def add_cross_compiling_paths(self):
736 cc = sysconfig.get_config_var('CC')
737 tmpfile = os.path.join(self.build_temp, 'ccpaths')
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200738 if not os.path.exists(self.build_temp):
739 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200740 ret = run_command('%s -E -v - </dev/null 2>%s 1>/dev/null' % (cc, tmpfile))
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200741 is_gcc = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800742 is_clang = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200743 in_incdirs = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200744 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200745 if ret == 0:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200746 with open(tmpfile) as fp:
747 for line in fp.readlines():
748 if line.startswith("gcc version"):
749 is_gcc = True
pxinwr32f5fdd2019-02-27 19:09:28 +0800750 elif line.startswith("clang version"):
751 is_clang = True
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200752 elif line.startswith("#include <...>"):
753 in_incdirs = True
754 elif line.startswith("End of search list"):
755 in_incdirs = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800756 elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"):
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200757 for d in line.strip().split("=")[1].split(":"):
758 d = os.path.normpath(d)
759 if '/gcc/' not in d:
760 add_dir_to_list(self.compiler.library_dirs,
761 d)
pxinwr32f5fdd2019-02-27 19:09:28 +0800762 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 +0200763 add_dir_to_list(self.compiler.include_dirs,
764 line.strip())
765 finally:
766 os.unlink(tmpfile)
767
pxinwr5e45f1c2021-01-22 08:55:52 +0800768 if VXWORKS:
769 self.add_wrcc_search_dirs()
770
Victor Stinnercfe172d2019-03-01 18:21:49 +0100771 def add_ldflags_cppflags(self):
Brett Cannon516592f2004-12-07 00:42:59 +0000772 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000773 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000774 # We must get the values from the Makefile and not the environment
775 # directly since an inconsistently reproducible issue comes up where
776 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000777 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000778 for env_var, arg_name, dir_list in (
Tarek Ziadé36797272010-07-22 12:50:05 +0000779 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
780 ('LDFLAGS', '-L', self.compiler.library_dirs),
781 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000782 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000783 if env_val:
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800784 parser = argparse.ArgumentParser()
785 parser.add_argument(arg_name, dest="dirs", action="append")
786 options, _ = parser.parse_known_args(env_val.split())
Brett Cannon44837712005-01-02 21:54:07 +0000787 if options.dirs:
Christian Heimes292d3512008-02-03 16:51:08 +0000788 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000789 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000790
Victor Stinnercfe172d2019-03-01 18:21:49 +0100791 def configure_compiler(self):
792 # Ensure that /usr/local is always used, but the local build
793 # directories (i.e. '.' and 'Include') must be first. See issue
794 # 10520.
795 if not CROSS_COMPILING:
796 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
797 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
798 # only change this for cross builds for 3.3, issues on Mageia
799 if CROSS_COMPILING:
800 self.add_cross_compiling_paths()
801 self.add_multiarch_paths()
802 self.add_ldflags_cppflags()
803
Victor Stinner5ec33a12019-03-01 16:43:28 +0100804 def init_inc_lib_dirs(self):
Victor Stinner4cbea512019-02-28 17:48:38 +0100805 if (not CROSS_COMPILING and
Xavier de Gaye1351c312016-12-14 11:14:33 +0100806 os.path.normpath(sys.base_prefix) != '/usr' and
807 not sysconfig.get_config_var('PYTHONFRAMEWORK')):
Ronald Oussorenf3500e12010-10-20 13:10:12 +0000808 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
809 # (PYTHONFRAMEWORK is set) to avoid # linking problems when
810 # building a framework with different architectures than
811 # the one that is currently installed (issue #7473)
Tarek Ziadé36797272010-07-22 12:50:05 +0000812 add_dir_to_list(self.compiler.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000813 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000814 add_dir_to_list(self.compiler.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000815 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000816
xdegaye77f51392017-11-25 17:25:30 +0100817 system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
818 system_include_dirs = ['/usr/include']
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000819 # lib_dirs and inc_dirs are used to search for files;
820 # if a file is found in one of those directories, it can
821 # be assumed that no additional -I,-L directives are needed.
Victor Stinner4cbea512019-02-28 17:48:38 +0100822 if not CROSS_COMPILING:
Victor Stinner625dbf22019-03-01 15:59:39 +0100823 self.lib_dirs = self.compiler.library_dirs + system_lib_dirs
824 self.inc_dirs = self.compiler.include_dirs + system_include_dirs
Christian Heimesf19529c2012-12-12 12:41:00 +0100825 else:
xdegaye77f51392017-11-25 17:25:30 +0100826 # Add the sysroot paths. 'sysroot' is a compiler option used to
827 # set the logical path of the standard system headers and
828 # libraries.
Victor Stinner625dbf22019-03-01 15:59:39 +0100829 self.lib_dirs = (self.compiler.library_dirs +
830 sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
831 self.inc_dirs = (self.compiler.include_dirs +
832 sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
833 system_include_dirs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000834
Brett Cannon4454a1f2005-04-15 20:32:39 +0000835 config_h = sysconfig.get_config_h_filename()
Brett Cannon9f5db072010-10-29 20:19:27 +0000836 with open(config_h) as file:
Victor Stinner5ec33a12019-03-01 16:43:28 +0100837 self.config_h_vars = sysconfig.parse_config_h(file)
Brett Cannon4454a1f2005-04-15 20:32:39 +0000838
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000839 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
Victor Stinner4cbea512019-02-28 17:48:38 +0100840 if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']:
Victor Stinner625dbf22019-03-01 15:59:39 +0100841 self.lib_dirs += ['/usr/ccs/lib']
Skip Montanaro22e00c42003-05-06 20:43:34 +0000842
Charles-François Natali5739e102012-04-12 19:07:25 +0200843 # HP-UX11iv3 keeps files in lib/hpux folders.
Victor Stinner4cbea512019-02-28 17:48:38 +0100844 if HOST_PLATFORM == 'hp-ux11':
Victor Stinner625dbf22019-03-01 15:59:39 +0100845 self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
Charles-François Natali5739e102012-04-12 19:07:25 +0200846
Victor Stinner4cbea512019-02-28 17:48:38 +0100847 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848 # This should work on any unixy platform ;-)
849 # If the user has bothered specifying additional -I and -L flags
850 # in OPT and LDFLAGS we might as well use them here.
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000851 #
852 # NOTE: using shlex.split would technically be more correct, but
853 # also gives a bootstrap problem. Let's hope nobody uses
854 # directories with whitespace in the name to store libraries.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855 cflags, ldflags = sysconfig.get_config_vars(
856 'CFLAGS', 'LDFLAGS')
857 for item in cflags.split():
858 if item.startswith('-I'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100859 self.inc_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860
861 for item in ldflags.split():
862 if item.startswith('-L'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100863 self.lib_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864
Victor Stinner5ec33a12019-03-01 16:43:28 +0100865 def detect_simple_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000866 #
867 # The following modules are all pretty straightforward, and compile
868 # on pretty much any POSIXish platform.
869 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000870
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000871 # array objects
Victor Stinnercdad2722021-04-22 00:52:52 +0200872 self.add(Extension('array', ['arraymodule.c'],
873 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Martin Panterc9deece2016-02-03 05:19:44 +0000874
Yury Selivanovf23746a2018-01-22 19:11:18 -0500875 # Context Variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100876 self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
Yury Selivanovf23746a2018-01-22 19:11:18 -0500877
Martin Panterc9deece2016-02-03 05:19:44 +0000878 shared_math = 'Modules/_math.o'
Victor Stinnercfe172d2019-03-01 18:21:49 +0100879
880 # math library functions, e.g. sin()
881 self.add(Extension('math', ['mathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100882 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100883 extra_objects=[shared_math],
884 depends=['_math.h', shared_math],
885 libraries=['m']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100886
887 # complex math library functions
888 self.add(Extension('cmath', ['cmathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100889 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100890 extra_objects=[shared_math],
891 depends=['_math.h', shared_math],
892 libraries=['m']))
Victor Stinnere0be4232011-10-25 13:06:09 +0200893
894 # time libraries: librt may be needed for clock_gettime()
895 time_libs = []
896 lib = sysconfig.get_config_var('TIMEMODULE_LIB')
897 if lib:
898 time_libs.append(lib)
899
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000900 # time operations and variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100901 self.add(Extension('time', ['timemodule.c'],
902 libraries=time_libs))
Benjamin Peterson8acaa312017-11-12 20:53:39 -0800903 # libm is needed by delta_new() that uses round() and by accum() that
904 # uses modf().
Victor Stinner8058bda2019-03-01 15:31:45 +0100905 self.add(Extension('_datetime', ['_datetimemodule.c'],
Victor Stinner04fc4f22020-06-16 01:28:07 +0200906 libraries=['m'],
907 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Paul Ganssle62972d92020-05-16 04:20:06 -0400908 # zoneinfo module
Victor Stinner37834132020-10-27 17:12:53 +0100909 self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
910 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000911 # random number generator implemented in C
Victor Stinner9f5fe792020-04-17 19:05:35 +0200912 self.add(Extension("_random", ["_randommodule.c"],
913 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Raymond Hettinger0c410272004-01-05 10:13:35 +0000914 # bisect
Victor Stinner8058bda2019-03-01 15:31:45 +0100915 self.add(Extension("_bisect", ["_bisectmodule.c"]))
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000916 # heapq
Victor Stinnerc45dbe932020-06-22 17:39:32 +0200917 self.add(Extension("_heapq", ["_heapqmodule.c"],
918 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000919 # C-optimized pickle replacement
Victor Stinner5c75f372019-04-17 23:02:26 +0200920 self.add(Extension("_pickle", ["_pickle.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200921 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimes90540002008-05-08 14:29:10 +0000922 # _json speedups
Victor Stinner8058bda2019-03-01 15:31:45 +0100923 self.add(Extension("_json", ["_json.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200924 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100925
Fred Drake0e474a82007-10-11 18:01:43 +0000926 # profiler (_lsprof is for cProfile.py)
Victor Stinner8058bda2019-03-01 15:31:45 +0100927 self.add(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000928 # static Unicode character database
Victor Stinner8058bda2019-03-01 15:31:45 +0100929 self.add(Extension('unicodedata', ['unicodedata.c'],
Victor Stinner47e1afd2020-10-26 16:43:47 +0100930 depends=['unicodedata_db.h', 'unicodename_db.h'],
931 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Larry Hastings3a907972013-11-23 14:49:22 -0800932 # _opcode module
Victor Stinner8058bda2019-03-01 15:31:45 +0100933 self.add(Extension('_opcode', ['_opcode.c']))
INADA Naoki9f2ce252016-10-15 15:39:19 +0900934 # asyncio speedups
Chris Jerdonekda742ba2020-05-17 22:47:31 -0700935 self.add(Extension("_asyncio", ["_asynciomodule.c"],
936 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000937 # _abc speedups
Victor Stinnercdad2722021-04-22 00:52:52 +0200938 self.add(Extension("_abc", ["_abc.c"],
939 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Antoine Pitrou94e16962018-01-16 00:27:16 +0100940 # _queue module
Victor Stinnercdad2722021-04-22 00:52:52 +0200941 self.add(Extension("_queue", ["_queuemodule.c"],
942 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Dong-hee Na0a18ee42019-08-24 07:20:30 +0900943 # _statistics module
944 self.add(Extension("_statistics", ["_statisticsmodule.c"]))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000945
946 # Modules with some UNIX dependencies -- on by default:
947 # (If you have a really backward UNIX, select and socket may not be
948 # supported...)
949
950 # fcntl(2) and ioctl(2)
Antoine Pitroua3000072010-09-07 14:52:42 +0000951 libs = []
Victor Stinner5ec33a12019-03-01 16:43:28 +0100952 if (self.config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
Antoine Pitroua3000072010-09-07 14:52:42 +0000953 # May be necessary on AIX for flock function
954 libs = ['bsd']
Victor Stinner8058bda2019-03-01 15:31:45 +0100955 self.add(Extension('fcntl', ['fcntlmodule.c'],
956 libraries=libs))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000957 # pwd(3)
Victor Stinner8058bda2019-03-01 15:31:45 +0100958 self.add(Extension('pwd', ['pwdmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000959 # grp(3)
pxinwr32f5fdd2019-02-27 19:09:28 +0800960 if not VXWORKS:
Victor Stinner8058bda2019-03-01 15:31:45 +0100961 self.add(Extension('grp', ['grpmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000962 # spwd, shadow passwords
Victor Stinner5ec33a12019-03-01 16:43:28 +0100963 if (self.config_h_vars.get('HAVE_GETSPNAM', False) or
964 self.config_h_vars.get('HAVE_GETSPENT', False)):
Victor Stinner8058bda2019-03-01 15:31:45 +0100965 self.add(Extension('spwd', ['spwdmodule.c']))
Michael Felt08970cb2019-06-21 15:58:00 +0200966 # AIX has shadow passwords, but access is not via getspent(), etc.
967 # module support is not expected so it not 'missing'
968 elif not AIX:
Victor Stinner8058bda2019-03-01 15:31:45 +0100969 self.missing.append('spwd')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000970
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000971 # select(2); not on ancient System V
Victor Stinner8058bda2019-03-01 15:31:45 +0100972 self.add(Extension('select', ['selectmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000973
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000974 # Memory-mapped files (also works on Win32).
Victor Stinner8058bda2019-03-01 15:31:45 +0100975 self.add(Extension('mmap', ['mmapmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000976
Andrew M. Kuchling57269d02004-08-31 13:37:25 +0000977 # Lance Ellinghaus's syslog module
Ronald Oussoren94f25282010-05-05 19:11:21 +0000978 # syslog daemon interface
Victor Stinner8058bda2019-03-01 15:31:45 +0100979 self.add(Extension('syslog', ['syslogmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000980
Eric Snow7f8bfc92018-01-29 18:23:44 -0700981 # Python interface to subinterpreter C-API.
Eric Snowc11183c2019-03-15 16:35:46 -0600982 self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
Eric Snow7f8bfc92018-01-29 18:23:44 -0700983
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000984 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000985 # Here ends the simple stuff. From here on, modules need certain
986 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000987 #
988
989 # Multimedia modules
990 # These don't work for 64-bit platforms!!!
991 # These represent audio samples or images as strings:
Victor Stinnerdef80722016-04-19 15:58:11 +0200992 #
Neal Norwitz5e4a3b82004-07-19 16:55:07 +0000993 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +0000994 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +0000995 # 64-bit platforms.
Victor Stinnerdef80722016-04-19 15:58:11 +0200996 #
Benjamin Peterson8acaa312017-11-12 20:53:39 -0800997 # audioop needs libm for floor() in multiple functions.
Victor Stinner8058bda2019-03-01 15:31:45 +0100998 self.add(Extension('audioop', ['audioop.c'],
999 libraries=['m']))
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001000
Victor Stinner5ec33a12019-03-01 16:43:28 +01001001 # CSV files
1002 self.add(Extension('_csv', ['_csv.c']))
1003
1004 # POSIX subprocess module helper.
Kyle Evans79925792020-10-13 15:04:44 -05001005 self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'],
1006 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner5ec33a12019-03-01 16:43:28 +01001007
Victor Stinnercfe172d2019-03-01 18:21:49 +01001008 def detect_test_extensions(self):
1009 # Python C API test module
1010 self.add(Extension('_testcapi', ['_testcapimodule.c'],
1011 depends=['testcapi_long.h']))
1012
Victor Stinner23bace22019-04-18 11:37:26 +02001013 # Python Internal C API test module
1014 self.add(Extension('_testinternalcapi', ['_testinternalcapi.c'],
Victor Stinner57491342019-04-23 12:26:33 +02001015 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner23bace22019-04-18 11:37:26 +02001016
Victor Stinnercfe172d2019-03-01 18:21:49 +01001017 # Python PEP-3118 (buffer protocol) test module
1018 self.add(Extension('_testbuffer', ['_testbuffer.c']))
1019
1020 # Test loading multiple modules from one compiled file (http://bugs.python.org/issue16421)
1021 self.add(Extension('_testimportmultiple', ['_testimportmultiple.c']))
1022
1023 # Test multi-phase extension module init (PEP 489)
1024 self.add(Extension('_testmultiphase', ['_testmultiphase.c']))
1025
1026 # Fuzz tests.
1027 self.add(Extension('_xxtestfuzz',
1028 ['_xxtestfuzz/_xxtestfuzz.c',
1029 '_xxtestfuzz/fuzzer.c']))
1030
Victor Stinner5ec33a12019-03-01 16:43:28 +01001031 def detect_readline_curses(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001032 # readline
Stefan Krah095b2732010-06-08 13:41:44 +00001033 readline_termcap_library = ""
1034 curses_library = ""
doko@ubuntu.com58844492012-06-30 18:25:32 +02001035 # Cannot use os.popen here in py3k.
1036 tmpfile = os.path.join(self.build_temp, 'readline_termcap_lib')
1037 if not os.path.exists(self.build_temp):
1038 os.makedirs(self.build_temp)
Stefan Krah095b2732010-06-08 13:41:44 +00001039 # Determine if readline is already linked against curses or tinfo.
Roland Hiebere1f77692021-02-09 02:05:25 +01001040 if sysconfig.get_config_var('HAVE_LIBREADLINE'):
1041 if sysconfig.get_config_var('WITH_EDITLINE'):
1042 readline_lib = 'edit'
1043 else:
1044 readline_lib = 'readline'
1045 do_readline = self.compiler.find_library_file(self.lib_dirs,
1046 readline_lib)
Victor Stinner4cbea512019-02-28 17:48:38 +01001047 if CROSS_COMPILING:
Victor Stinner6b982c22020-04-01 01:10:07 +02001048 ret = run_command("%s -d %s | grep '(NEEDED)' > %s"
doko@ubuntu.com58844492012-06-30 18:25:32 +02001049 % (sysconfig.get_config_var('READELF'),
1050 do_readline, tmpfile))
1051 elif find_executable('ldd'):
Victor Stinner6b982c22020-04-01 01:10:07 +02001052 ret = run_command("ldd %s > %s" % (do_readline, tmpfile))
doko@ubuntu.com58844492012-06-30 18:25:32 +02001053 else:
Victor Stinner6b982c22020-04-01 01:10:07 +02001054 ret = 1
1055 if ret == 0:
Brett Cannon9f5db072010-10-29 20:19:27 +00001056 with open(tmpfile) as fp:
1057 for ln in fp:
1058 if 'curses' in ln:
1059 readline_termcap_library = re.sub(
1060 r'.*lib(n?cursesw?)\.so.*', r'\1', ln
1061 ).rstrip()
1062 break
1063 # termcap interface split out from ncurses
1064 if 'tinfo' in ln:
1065 readline_termcap_library = 'tinfo'
1066 break
doko@ubuntu.com4c990712012-06-30 23:28:09 +02001067 if os.path.exists(tmpfile):
1068 os.unlink(tmpfile)
Roland Hiebere1f77692021-02-09 02:05:25 +01001069 else:
1070 do_readline = False
Stefan Krah095b2732010-06-08 13:41:44 +00001071 # Issue 7384: If readline is already linked against curses,
1072 # use the same library for the readline and curses modules.
1073 if 'curses' in readline_termcap_library:
1074 curses_library = readline_termcap_library
Victor Stinner625dbf22019-03-01 15:59:39 +01001075 elif self.compiler.find_library_file(self.lib_dirs, 'ncursesw'):
Stefan Krah095b2732010-06-08 13:41:44 +00001076 curses_library = 'ncursesw'
Michael Felt08970cb2019-06-21 15:58:00 +02001077 # Issue 36210: OSS provided ncurses does not link on AIX
1078 # Use IBM supplied 'curses' for successful build of _curses
1079 elif AIX and self.compiler.find_library_file(self.lib_dirs, 'curses'):
1080 curses_library = 'curses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001081 elif self.compiler.find_library_file(self.lib_dirs, 'ncurses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001082 curses_library = 'ncurses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001083 elif self.compiler.find_library_file(self.lib_dirs, 'curses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001084 curses_library = 'curses'
1085
Victor Stinner4cbea512019-02-28 17:48:38 +01001086 if MACOS:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001087 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren961683a2010-03-08 07:09:59 +00001088 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
Ned Deily04cdfa12014-06-25 13:36:14 -07001089 if (dep_target and
Ronald Oussoren49926cf2021-02-01 04:29:44 +01001090 (tuple(int(n) for n in dep_target.split('.')[0:2])
Ned Deily04cdfa12014-06-25 13:36:14 -07001091 < (10, 5) ) ):
Ronald Oussoren961683a2010-03-08 07:09:59 +00001092 os_release = 8
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001093 if os_release < 9:
1094 # MacOSX 10.4 has a broken readline. Don't try to build
1095 # the readline module unless the user has installed a fixed
1096 # readline package
Victor Stinner625dbf22019-03-01 15:59:39 +01001097 if find_file('readline/rlconf.h', self.inc_dirs, []) is None:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001098 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +00001099 if do_readline:
Victor Stinner4cbea512019-02-28 17:48:38 +01001100 if MACOS and os_release < 9:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001101 # In every directory on the search path search for a dynamic
1102 # library and then a static library, instead of first looking
Fred Drake0af17612007-09-04 19:43:19 +00001103 # for dynamic libraries on the entire path.
Martin Pantere26da7c2016-06-02 10:07:09 +00001104 # This way a statically linked custom readline gets picked up
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001105 # before the (possibly broken) dynamic library in /usr/lib.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106 readline_extra_link_args = ('-Wl,-search_paths_first',)
1107 else:
1108 readline_extra_link_args = ()
1109
Roland Hiebere1f77692021-02-09 02:05:25 +01001110 readline_libs = [readline_lib]
Stefan Krah095b2732010-06-08 13:41:44 +00001111 if readline_termcap_library:
1112 pass # Issue 7384: Already linked against curses or tinfo.
1113 elif curses_library:
1114 readline_libs.append(curses_library)
Victor Stinner625dbf22019-03-01 15:59:39 +01001115 elif self.compiler.find_library_file(self.lib_dirs +
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001116 ['/usr/lib/termcap'],
1117 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +00001118 readline_libs.append('termcap')
Victor Stinner8058bda2019-03-01 15:31:45 +01001119 self.add(Extension('readline', ['readline.c'],
1120 library_dirs=['/usr/lib/termcap'],
1121 extra_link_args=readline_extra_link_args,
1122 libraries=readline_libs))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001123 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001124 self.missing.append('readline')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125
Victor Stinner5ec33a12019-03-01 16:43:28 +01001126 # Curses support, requiring the System V version of curses, often
1127 # provided by the ncurses library.
1128 curses_defines = []
1129 curses_includes = []
1130 panel_library = 'panel'
1131 if curses_library == 'ncursesw':
1132 curses_defines.append(('HAVE_NCURSESW', '1'))
1133 if not CROSS_COMPILING:
1134 curses_includes.append('/usr/include/ncursesw')
1135 # Bug 1464056: If _curses.so links with ncursesw,
1136 # _curses_panel.so must link with panelw.
1137 panel_library = 'panelw'
1138 if MACOS:
1139 # On OS X, there is no separate /usr/lib/libncursesw nor
1140 # libpanelw. If we are here, we found a locally-supplied
1141 # version of libncursesw. There should also be a
1142 # libpanelw. _XOPEN_SOURCE defines are usually excluded
1143 # for OS X but we need _XOPEN_SOURCE_EXTENDED here for
1144 # ncurses wide char support
1145 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
1146 elif MACOS and curses_library == 'ncurses':
1147 # Building with the system-suppied combined libncurses/libpanel
1148 curses_defines.append(('HAVE_NCURSESW', '1'))
1149 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
Tim Peters2c60f7a2003-01-29 03:49:43 +00001150
Victor Stinnercfe172d2019-03-01 18:21:49 +01001151 curses_enabled = True
Victor Stinner5ec33a12019-03-01 16:43:28 +01001152 if curses_library.startswith('ncurses'):
1153 curses_libs = [curses_library]
1154 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001155 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001156 include_dirs=curses_includes,
1157 define_macros=curses_defines,
1158 libraries=curses_libs))
1159 elif curses_library == 'curses' and not MACOS:
1160 # OSX has an old Berkeley curses, not good enough for
1161 # the _curses module.
1162 if (self.compiler.find_library_file(self.lib_dirs, 'terminfo')):
1163 curses_libs = ['curses', 'terminfo']
1164 elif (self.compiler.find_library_file(self.lib_dirs, 'termcap')):
1165 curses_libs = ['curses', 'termcap']
1166 else:
1167 curses_libs = ['curses']
1168
1169 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001170 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001171 define_macros=curses_defines,
1172 libraries=curses_libs))
1173 else:
Victor Stinnercfe172d2019-03-01 18:21:49 +01001174 curses_enabled = False
Victor Stinner5ec33a12019-03-01 16:43:28 +01001175 self.missing.append('_curses')
1176
1177 # If the curses module is enabled, check for the panel module
Michael Felt08970cb2019-06-21 15:58:00 +02001178 # _curses_panel needs some form of ncurses
1179 skip_curses_panel = True if AIX else False
1180 if (curses_enabled and not skip_curses_panel and
1181 self.compiler.find_library_file(self.lib_dirs, panel_library)):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001182 self.add(Extension('_curses_panel', ['_curses_panel.c'],
Michael Felt08970cb2019-06-21 15:58:00 +02001183 include_dirs=curses_includes,
1184 define_macros=curses_defines,
1185 libraries=[panel_library, *curses_libs]))
1186 elif not skip_curses_panel:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001187 self.missing.append('_curses_panel')
1188
1189 def detect_crypt(self):
1190 # crypt module.
pxinwr236d0b72019-04-15 17:02:20 +08001191 if VXWORKS:
1192 # bpo-31904: crypt() function is not provided by VxWorks.
1193 # DES_crypt() OpenSSL provides is too weak to implement
1194 # the encryption.
Victor Stinnercad80202021-01-19 23:04:49 +01001195 self.missing.append('_crypt')
pxinwr236d0b72019-04-15 17:02:20 +08001196 return
1197
Victor Stinner625dbf22019-03-01 15:59:39 +01001198 if self.compiler.find_library_file(self.lib_dirs, 'crypt'):
Ronald Oussoren94f25282010-05-05 19:11:21 +00001199 libs = ['crypt']
Guido van Rossumd8faa362007-04-27 19:54:29 +00001200 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +00001201 libs = []
pxinwr32f5fdd2019-02-27 19:09:28 +08001202
Victor Stinnercad80202021-01-19 23:04:49 +01001203 self.add(Extension('_crypt', ['_cryptmodule.c'], libraries=libs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001204
Victor Stinner5ec33a12019-03-01 16:43:28 +01001205 def detect_socket(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001206 # socket(2)
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001207 kwargs = {'depends': ['socketmodule.h']}
pxinwr00a65682020-11-29 06:14:16 +08001208 if MACOS:
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001209 # Issue #35569: Expose RFC 3542 socket options.
1210 kwargs['extra_compile_args'] = ['-D__APPLE_USE_RFC_3542']
Erlend Egeberg Aasland9a45bfe2020-05-17 08:32:46 +02001211
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001212 self.add(Extension('_socket', ['socketmodule.c'], **kwargs))
pxinwr32f5fdd2019-02-27 19:09:28 +08001213
Victor Stinner5ec33a12019-03-01 16:43:28 +01001214 def detect_dbm_gdbm(self):
Georg Brandl489cb4f2009-07-11 10:08:49 +00001215 # Modules that provide persistent dictionary-like semantics. You will
1216 # probably want to arrange for at least one of them to be available on
1217 # your machine, though none are defined by default because of library
1218 # dependencies. The Python module dbm/__init__.py provides an
1219 # implementation independent wrapper for these; dbm/dumb.py provides
1220 # similar functionality (but slower of course) implemented in Python.
1221
1222 # Sleepycat^WOracle Berkeley DB interface.
1223 # http://www.oracle.com/database/berkeley-db/db/index.html
1224 #
1225 # This requires the Sleepycat^WOracle DB code. The supported versions
1226 # are set below. Visit the URL above to download
1227 # a release. Most open source OSes come with one or more
1228 # versions of BerkeleyDB already installed.
1229
doko@ubuntu.com15bac0f2012-07-01 10:35:54 +02001230 max_db_ver = (5, 3)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001231 min_db_ver = (3, 3)
1232 db_setup_debug = False # verbose debug prints from this script?
1233
1234 def allow_db_ver(db_ver):
1235 """Returns a boolean if the given BerkeleyDB version is acceptable.
1236
1237 Args:
1238 db_ver: A tuple of the version to verify.
1239 """
1240 if not (min_db_ver <= db_ver <= max_db_ver):
1241 return False
1242 return True
1243
1244 def gen_db_minor_ver_nums(major):
1245 if major == 4:
1246 for x in range(max_db_ver[1]+1):
1247 if allow_db_ver((4, x)):
1248 yield x
1249 elif major == 3:
1250 for x in (3,):
1251 if allow_db_ver((3, x)):
1252 yield x
1253 else:
1254 raise ValueError("unknown major BerkeleyDB version", major)
1255
1256 # construct a list of paths to look for the header file in on
1257 # top of the normal inc_dirs.
1258 db_inc_paths = [
1259 '/usr/include/db4',
1260 '/usr/local/include/db4',
1261 '/opt/sfw/include/db4',
1262 '/usr/include/db3',
1263 '/usr/local/include/db3',
1264 '/opt/sfw/include/db3',
1265 # Fink defaults (http://fink.sourceforge.net/)
1266 '/sw/include/db4',
1267 '/sw/include/db3',
1268 ]
1269 # 4.x minor number specific paths
1270 for x in gen_db_minor_ver_nums(4):
1271 db_inc_paths.append('/usr/include/db4%d' % x)
1272 db_inc_paths.append('/usr/include/db4.%d' % x)
1273 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
1274 db_inc_paths.append('/usr/local/include/db4%d' % x)
1275 db_inc_paths.append('/pkg/db-4.%d/include' % x)
1276 db_inc_paths.append('/opt/db-4.%d/include' % x)
1277 # MacPorts default (http://www.macports.org/)
1278 db_inc_paths.append('/opt/local/include/db4%d' % x)
1279 # 3.x minor number specific paths
1280 for x in gen_db_minor_ver_nums(3):
1281 db_inc_paths.append('/usr/include/db3%d' % x)
1282 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
1283 db_inc_paths.append('/usr/local/include/db3%d' % x)
1284 db_inc_paths.append('/pkg/db-3.%d/include' % x)
1285 db_inc_paths.append('/opt/db-3.%d/include' % x)
1286
Victor Stinner4cbea512019-02-28 17:48:38 +01001287 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001288 db_inc_paths = []
1289
Georg Brandl489cb4f2009-07-11 10:08:49 +00001290 # Add some common subdirectories for Sleepycat DB to the list,
1291 # based on the standard include directories. This way DB3/4 gets
1292 # picked up when it is installed in a non-standard prefix and
1293 # the user has added that prefix into inc_dirs.
1294 std_variants = []
Victor Stinner625dbf22019-03-01 15:59:39 +01001295 for dn in self.inc_dirs:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001296 std_variants.append(os.path.join(dn, 'db3'))
1297 std_variants.append(os.path.join(dn, 'db4'))
1298 for x in gen_db_minor_ver_nums(4):
1299 std_variants.append(os.path.join(dn, "db4%d"%x))
1300 std_variants.append(os.path.join(dn, "db4.%d"%x))
1301 for x in gen_db_minor_ver_nums(3):
1302 std_variants.append(os.path.join(dn, "db3%d"%x))
1303 std_variants.append(os.path.join(dn, "db3.%d"%x))
1304
1305 db_inc_paths = std_variants + db_inc_paths
1306 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
1307
1308 db_ver_inc_map = {}
1309
Victor Stinner4cbea512019-02-28 17:48:38 +01001310 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001311 sysroot = macosx_sdk_root()
1312
Georg Brandl489cb4f2009-07-11 10:08:49 +00001313 class db_found(Exception): pass
1314 try:
1315 # See whether there is a Sleepycat header in the standard
1316 # search path.
Victor Stinner625dbf22019-03-01 15:59:39 +01001317 for d in self.inc_dirs + db_inc_paths:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001318 f = os.path.join(d, "db.h")
Victor Stinner4cbea512019-02-28 17:48:38 +01001319 if MACOS and is_macosx_sdk_path(d):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001320 f = os.path.join(sysroot, d[1:], "db.h")
1321
Georg Brandl489cb4f2009-07-11 10:08:49 +00001322 if db_setup_debug: print("db: looking for db.h in", f)
1323 if os.path.exists(f):
Brett Cannon9f5db072010-10-29 20:19:27 +00001324 with open(f, 'rb') as file:
1325 f = file.read()
Benjamin Peterson019f3612009-08-12 18:18:03 +00001326 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001327 if m:
1328 db_major = int(m.group(1))
Benjamin Peterson019f3612009-08-12 18:18:03 +00001329 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001330 db_minor = int(m.group(1))
1331 db_ver = (db_major, db_minor)
1332
1333 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
1334 if db_ver == (4, 6):
Benjamin Peterson019f3612009-08-12 18:18:03 +00001335 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001336 db_patch = int(m.group(1))
1337 if db_patch < 21:
1338 print("db.h:", db_ver, "patch", db_patch,
1339 "being ignored (4.6.x must be >= 4.6.21)")
1340 continue
1341
1342 if ( (db_ver not in db_ver_inc_map) and
1343 allow_db_ver(db_ver) ):
1344 # save the include directory with the db.h version
1345 # (first occurrence only)
1346 db_ver_inc_map[db_ver] = d
1347 if db_setup_debug:
1348 print("db.h: found", db_ver, "in", d)
1349 else:
1350 # we already found a header for this library version
1351 if db_setup_debug: print("db.h: ignoring", d)
1352 else:
1353 # ignore this header, it didn't contain a version number
1354 if db_setup_debug:
1355 print("db.h: no version number version in", d)
1356
1357 db_found_vers = list(db_ver_inc_map.keys())
1358 db_found_vers.sort()
1359
1360 while db_found_vers:
1361 db_ver = db_found_vers.pop()
1362 db_incdir = db_ver_inc_map[db_ver]
1363
1364 # check lib directories parallel to the location of the header
1365 db_dirs_to_check = [
1366 db_incdir.replace("include", 'lib64'),
1367 db_incdir.replace("include", 'lib'),
1368 ]
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001369
Victor Stinner4cbea512019-02-28 17:48:38 +01001370 if not MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001371 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check))
1372
1373 else:
1374 # Same as other branch, but takes OSX SDK into account
1375 tmp = []
1376 for dn in db_dirs_to_check:
1377 if is_macosx_sdk_path(dn):
1378 if os.path.isdir(os.path.join(sysroot, dn[1:])):
1379 tmp.append(dn)
1380 else:
1381 if os.path.isdir(dn):
1382 tmp.append(dn)
Ronald Oussorendc969e52010-06-27 12:37:46 +00001383 db_dirs_to_check = tmp
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001384
1385 db_dirs_to_check = tmp
Georg Brandl489cb4f2009-07-11 10:08:49 +00001386
Ezio Melotti42da6632011-03-15 05:18:48 +02001387 # Look for a version specific db-X.Y before an ambiguous dbX
Georg Brandl489cb4f2009-07-11 10:08:49 +00001388 # XXX should we -ever- look for a dbX name? Do any
1389 # systems really not name their library by version and
1390 # symlink to more general names?
1391 for dblib in (('db-%d.%d' % db_ver),
1392 ('db%d%d' % db_ver),
1393 ('db%d' % db_ver[0])):
1394 dblib_file = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001395 db_dirs_to_check + self.lib_dirs, dblib )
Georg Brandl489cb4f2009-07-11 10:08:49 +00001396 if dblib_file:
1397 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
1398 raise db_found
1399 else:
1400 if db_setup_debug: print("db lib: ", dblib, "not found")
1401
1402 except db_found:
1403 if db_setup_debug:
1404 print("bsddb using BerkeleyDB lib:", db_ver, dblib)
1405 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001406 dblibs = [dblib]
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001407 # Only add the found library and include directories if they aren't
1408 # already being searched. This avoids an explicit runtime library
1409 # dependency.
Victor Stinner625dbf22019-03-01 15:59:39 +01001410 if db_incdir in self.inc_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001411 db_incs = None
1412 else:
1413 db_incs = [db_incdir]
Victor Stinner625dbf22019-03-01 15:59:39 +01001414 if dblib_dir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001415 dblib_dir = None
Georg Brandl489cb4f2009-07-11 10:08:49 +00001416 else:
1417 if db_setup_debug: print("db: no appropriate library found")
1418 db_incs = None
1419 dblibs = []
1420 dblib_dir = None
1421
Victor Stinner5ec33a12019-03-01 16:43:28 +01001422 dbm_setup_debug = False # verbose debug prints from this script?
1423 dbm_order = ['gdbm']
1424 # The standard Unix dbm module:
1425 if not CYGWIN:
1426 config_args = [arg.strip("'")
1427 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
1428 dbm_args = [arg for arg in config_args
1429 if arg.startswith('--with-dbmliborder=')]
1430 if dbm_args:
1431 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
1432 else:
1433 dbm_order = "ndbm:gdbm:bdb".split(":")
1434 dbmext = None
1435 for cand in dbm_order:
1436 if cand == "ndbm":
1437 if find_file("ndbm.h", self.inc_dirs, []) is not None:
1438 # Some systems have -lndbm, others have -lgdbm_compat,
1439 # others don't have either
1440 if self.compiler.find_library_file(self.lib_dirs,
1441 'ndbm'):
1442 ndbm_libs = ['ndbm']
1443 elif self.compiler.find_library_file(self.lib_dirs,
1444 'gdbm_compat'):
1445 ndbm_libs = ['gdbm_compat']
1446 else:
1447 ndbm_libs = []
1448 if dbm_setup_debug: print("building dbm using ndbm")
1449 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1450 define_macros=[
1451 ('HAVE_NDBM_H',None),
1452 ],
1453 libraries=ndbm_libs)
1454 break
1455
1456 elif cand == "gdbm":
1457 if self.compiler.find_library_file(self.lib_dirs, 'gdbm'):
1458 gdbm_libs = ['gdbm']
1459 if self.compiler.find_library_file(self.lib_dirs,
1460 'gdbm_compat'):
1461 gdbm_libs.append('gdbm_compat')
1462 if find_file("gdbm/ndbm.h", self.inc_dirs, []) is not None:
1463 if dbm_setup_debug: print("building dbm using gdbm")
1464 dbmext = Extension(
1465 '_dbm', ['_dbmmodule.c'],
1466 define_macros=[
1467 ('HAVE_GDBM_NDBM_H', None),
1468 ],
1469 libraries = gdbm_libs)
1470 break
1471 if find_file("gdbm-ndbm.h", self.inc_dirs, []) is not None:
1472 if dbm_setup_debug: print("building dbm using gdbm")
1473 dbmext = Extension(
1474 '_dbm', ['_dbmmodule.c'],
1475 define_macros=[
1476 ('HAVE_GDBM_DASH_NDBM_H', None),
1477 ],
1478 libraries = gdbm_libs)
1479 break
1480 elif cand == "bdb":
1481 if dblibs:
1482 if dbm_setup_debug: print("building dbm using bdb")
1483 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1484 library_dirs=dblib_dir,
1485 runtime_library_dirs=dblib_dir,
1486 include_dirs=db_incs,
1487 define_macros=[
1488 ('HAVE_BERKDB_H', None),
1489 ('DB_DBM_HSEARCH', None),
1490 ],
1491 libraries=dblibs)
1492 break
1493 if dbmext is not None:
1494 self.add(dbmext)
1495 else:
1496 self.missing.append('_dbm')
1497
1498 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
1499 if ('gdbm' in dbm_order and
1500 self.compiler.find_library_file(self.lib_dirs, 'gdbm')):
1501 self.add(Extension('_gdbm', ['_gdbmmodule.c'],
1502 libraries=['gdbm']))
1503 else:
1504 self.missing.append('_gdbm')
1505
1506 def detect_sqlite(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507 # The sqlite interface
Thomas Wouters89f507f2006-12-13 04:49:30 +00001508 sqlite_setup_debug = False # verbose debug prints from this script?
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509
1510 # We hunt for #define SQLITE_VERSION "n.n.n"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511 sqlite_incdir = sqlite_libdir = None
1512 sqlite_inc_paths = [ '/usr/include',
1513 '/usr/include/sqlite',
1514 '/usr/include/sqlite3',
1515 '/usr/local/include',
1516 '/usr/local/include/sqlite',
1517 '/usr/local/include/sqlite3',
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001518 ]
Victor Stinner4cbea512019-02-28 17:48:38 +01001519 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001520 sqlite_inc_paths = []
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +01001521 MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001522 MIN_SQLITE_VERSION = ".".join([str(x)
1523 for x in MIN_SQLITE_VERSION_NUMBER])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001524
1525 # Scan the default include directories before the SQLite specific
1526 # ones. This allows one to override the copy of sqlite on OSX,
1527 # where /usr/include contains an old version of sqlite.
Victor Stinner4cbea512019-02-28 17:48:38 +01001528 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001529 sysroot = macosx_sdk_root()
1530
Victor Stinner625dbf22019-03-01 15:59:39 +01001531 for d_ in self.inc_dirs + sqlite_inc_paths:
Ned Deily9b635832012-08-05 15:13:33 -07001532 d = d_
Victor Stinner4cbea512019-02-28 17:48:38 +01001533 if MACOS and is_macosx_sdk_path(d):
Ned Deily9b635832012-08-05 15:13:33 -07001534 d = os.path.join(sysroot, d[1:])
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001535
Ned Deily9b635832012-08-05 15:13:33 -07001536 f = os.path.join(d, "sqlite3.h")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 if os.path.exists(f):
Guido van Rossum452bf512007-02-09 05:32:43 +00001538 if sqlite_setup_debug: print("sqlite: found %s"%f)
Brett Cannon9f5db072010-10-29 20:19:27 +00001539 with open(f) as file:
1540 incf = file.read()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541 m = re.search(
Petri Lehtinened909bc2013-02-23 17:05:28 +01001542 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543 if m:
1544 sqlite_version = m.group(1)
1545 sqlite_version_tuple = tuple([int(x)
1546 for x in sqlite_version.split(".")])
1547 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
1548 # we win!
Thomas Wouters89f507f2006-12-13 04:49:30 +00001549 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001550 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001551 sqlite_incdir = d
1552 break
1553 else:
1554 if sqlite_setup_debug:
Charles Pigottad0daf52019-04-26 16:38:12 +01001555 print("%s: version %s is too old, need >= %s"%(d,
Guido van Rossum452bf512007-02-09 05:32:43 +00001556 sqlite_version, MIN_SQLITE_VERSION))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001557 elif sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001558 print("sqlite: %s had no SQLITE_VERSION"%(f,))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559
1560 if sqlite_incdir:
1561 sqlite_dirs_to_check = [
1562 os.path.join(sqlite_incdir, '..', 'lib64'),
1563 os.path.join(sqlite_incdir, '..', 'lib'),
1564 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
1565 os.path.join(sqlite_incdir, '..', '..', 'lib'),
1566 ]
Tarek Ziadé36797272010-07-22 12:50:05 +00001567 sqlite_libfile = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001568 sqlite_dirs_to_check + self.lib_dirs, 'sqlite3')
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001569 if sqlite_libfile:
1570 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001571
1572 if sqlite_incdir and sqlite_libdir:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001573 sqlite_srcs = ['_sqlite/cache.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001574 '_sqlite/connection.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 '_sqlite/cursor.c',
1576 '_sqlite/microprotocols.c',
1577 '_sqlite/module.c',
1578 '_sqlite/prepare_protocol.c',
1579 '_sqlite/row.c',
1580 '_sqlite/statement.c',
1581 '_sqlite/util.c', ]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001582 sqlite_defines = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001583
Benjamin Peterson076ed002010-10-31 17:11:02 +00001584 # Enable support for loadable extensions in the sqlite3 module
1585 # if --enable-loadable-sqlite-extensions configure option is used.
1586 if '--enable-loadable-sqlite-extensions' not in sysconfig.get_config_var("CONFIG_ARGS"):
1587 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001588
Victor Stinner4cbea512019-02-28 17:48:38 +01001589 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001590 # In every directory on the search path search for a dynamic
1591 # library and then a static library, instead of first looking
Ezio Melotti13925002011-03-16 11:05:33 +02001592 # for dynamic libraries on the entire path.
1593 # This way a statically linked custom sqlite gets picked up
Thomas Wouters477c8d52006-05-27 19:21:47 +00001594 # before the dynamic library in /usr/lib.
1595 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1596 else:
1597 sqlite_extra_link_args = ()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001598
Brett Cannonc5011fe2011-06-06 20:09:10 -07001599 include_dirs = ["Modules/_sqlite"]
1600 # Only include the directory where sqlite was found if it does
1601 # not already exist in set include directories, otherwise you
1602 # can end up with a bad search path order.
1603 if sqlite_incdir not in self.compiler.include_dirs:
1604 include_dirs.append(sqlite_incdir)
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001605 # avoid a runtime library path for a system library dir
Victor Stinner625dbf22019-03-01 15:59:39 +01001606 if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001607 sqlite_libdir = None
Victor Stinner8058bda2019-03-01 15:31:45 +01001608 self.add(Extension('_sqlite3', sqlite_srcs,
1609 define_macros=sqlite_defines,
1610 include_dirs=include_dirs,
1611 library_dirs=sqlite_libdir,
1612 extra_link_args=sqlite_extra_link_args,
1613 libraries=["sqlite3",]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001614 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001615 self.missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001616
Victor Stinner5ec33a12019-03-01 16:43:28 +01001617 def detect_platform_specific_exts(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001618 # Unix-only modules
Victor Stinner4cbea512019-02-28 17:48:38 +01001619 if not MS_WINDOWS:
pxinwr32f5fdd2019-02-27 19:09:28 +08001620 if not VXWORKS:
1621 # Steen Lumholt's termios module
Victor Stinner8058bda2019-03-01 15:31:45 +01001622 self.add(Extension('termios', ['termios.c']))
pxinwr32f5fdd2019-02-27 19:09:28 +08001623 # Jeremy Hylton's rlimit interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001624 self.add(Extension('resource', ['resource.c']))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001625 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001626 self.missing.extend(['resource', 'termios'])
Christian Heimes29a7df72018-01-26 23:28:46 +01001627
Victor Stinner5ec33a12019-03-01 16:43:28 +01001628 # Platform-specific libraries
1629 if HOST_PLATFORM.startswith(('linux', 'freebsd', 'gnukfreebsd')):
1630 self.add(Extension('ossaudiodev', ['ossaudiodev.c']))
Michael Felt08970cb2019-06-21 15:58:00 +02001631 elif not AIX:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001632 self.missing.append('ossaudiodev')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001633
Victor Stinner5ec33a12019-03-01 16:43:28 +01001634 if MACOS:
Ned Deily951ab582020-05-18 11:31:21 -04001635 self.add(Extension('_scproxy', ['_scproxy.c'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001636 extra_link_args=[
1637 '-framework', 'SystemConfiguration',
Ned Deily951ab582020-05-18 11:31:21 -04001638 '-framework', 'CoreFoundation']))
Fredrik Lundhade711a2001-01-24 08:00:28 +00001639
Victor Stinner5ec33a12019-03-01 16:43:28 +01001640 def detect_compress_exts(self):
Barry Warsaw259b1e12002-08-13 20:09:26 +00001641 # Andrew Kuchling's zlib module. Note that some versions of zlib
1642 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1643 # http://www.cert.org/advisories/CA-2002-07.html
1644 #
1645 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1646 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1647 # now, we still accept 1.1.3, because we think it's difficult to
1648 # exploit this in Python, and we'd rather make it RedHat's problem
1649 # than our problem <wink>.
1650 #
1651 # You can upgrade zlib to version 1.1.4 yourself by going to
1652 # http://www.gzip.org/zlib/
Victor Stinner625dbf22019-03-01 15:59:39 +01001653 zlib_inc = find_file('zlib.h', [], self.inc_dirs)
Christian Heimes1dc54002008-03-24 02:19:29 +00001654 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001655 if zlib_inc is not None:
1656 zlib_h = zlib_inc[0] + '/zlib.h'
1657 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001658 version_req = '"1.1.3"'
Victor Stinner4cbea512019-02-28 17:48:38 +01001659 if MACOS and is_macosx_sdk_path(zlib_h):
Ned Deily507c5912013-10-18 21:32:00 -07001660 zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
Brett Cannon9f5db072010-10-29 20:19:27 +00001661 with open(zlib_h) as fp:
1662 while 1:
1663 line = fp.readline()
1664 if not line:
1665 break
1666 if line.startswith('#define ZLIB_VERSION'):
1667 version = line.split()[2]
1668 break
Guido van Rossume6970912001-04-15 15:16:12 +00001669 if version >= version_req:
Victor Stinner625dbf22019-03-01 15:59:39 +01001670 if (self.compiler.find_library_file(self.lib_dirs, 'z')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001671 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001672 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1673 else:
1674 zlib_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001675 self.add(Extension('zlib', ['zlibmodule.c'],
1676 libraries=['z'],
1677 extra_link_args=zlib_extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001678 have_zlib = True
Guido van Rossumd8faa362007-04-27 19:54:29 +00001679 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001680 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001681 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001682 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001683 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001684 self.missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001685
Christian Heimes1dc54002008-03-24 02:19:29 +00001686 # Helper module for various ascii-encoders. Uses zlib for an optimized
1687 # crc32 if we have it. Otherwise binascii uses its own.
1688 if have_zlib:
1689 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1690 libraries = ['z']
1691 extra_link_args = zlib_extra_link_args
1692 else:
1693 extra_compile_args = []
1694 libraries = []
1695 extra_link_args = []
Victor Stinner8058bda2019-03-01 15:31:45 +01001696 self.add(Extension('binascii', ['binascii.c'],
1697 extra_compile_args=extra_compile_args,
1698 libraries=libraries,
1699 extra_link_args=extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001700
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001701 # Gustavo Niemeyer's bz2 module.
Victor Stinner625dbf22019-03-01 15:59:39 +01001702 if (self.compiler.find_library_file(self.lib_dirs, 'bz2')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001703 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001704 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1705 else:
1706 bz2_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001707 self.add(Extension('_bz2', ['_bz2module.c'],
1708 libraries=['bz2'],
1709 extra_link_args=bz2_extra_link_args))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001710 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001711 self.missing.append('_bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001712
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001713 # LZMA compression support.
Victor Stinner625dbf22019-03-01 15:59:39 +01001714 if self.compiler.find_library_file(self.lib_dirs, 'lzma'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001715 self.add(Extension('_lzma', ['_lzmamodule.c'],
1716 libraries=['lzma']))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001717 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001718 self.missing.append('_lzma')
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001719
Victor Stinner5ec33a12019-03-01 16:43:28 +01001720 def detect_expat_elementtree(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001721 # Interface to the Expat XML parser
1722 #
Benjamin Petersona28e7022010-01-09 18:53:06 +00001723 # Expat was written by James Clark and is now maintained by a group of
1724 # developers on SourceForge; see www.libexpat.org for more information.
1725 # The pyexpat module was written by Paul Prescod after a prototype by
1726 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1727 # of a system shared libexpat.so is possible with --with-system-expat
Benjamin Petersonc73206c2010-10-31 16:38:19 +00001728 # configure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001729 #
1730 # More information on Expat can be found at www.libexpat.org.
1731 #
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001732 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1733 expat_inc = []
1734 define_macros = []
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001735 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001736 expat_lib = ['expat']
1737 expat_sources = []
Christian Heimesd489c7a2013-02-09 17:02:06 +01001738 expat_depends = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001739 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01001740 expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')]
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001741 define_macros = [
1742 ('HAVE_EXPAT_CONFIG_H', '1'),
Victor Stinner93d0cb52017-08-18 23:43:54 +02001743 # bpo-30947: Python uses best available entropy sources to
1744 # call XML_SetHashSalt(), expat entropy sources are not needed
1745 ('XML_POOR_ENTROPY', '1'),
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001746 ]
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001747 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001748 expat_lib = []
1749 expat_sources = ['expat/xmlparse.c',
1750 'expat/xmlrole.c',
1751 'expat/xmltok.c']
Christian Heimesd489c7a2013-02-09 17:02:06 +01001752 expat_depends = ['expat/ascii.h',
1753 'expat/asciitab.h',
1754 'expat/expat.h',
1755 'expat/expat_config.h',
1756 'expat/expat_external.h',
1757 'expat/internal.h',
1758 'expat/latin1tab.h',
1759 'expat/utf8tab.h',
1760 'expat/xmlrole.h',
1761 'expat/xmltok.h',
1762 'expat/xmltok_impl.h'
1763 ]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001764
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001765 cc = sysconfig.get_config_var('CC').split()[0]
Victor Stinner6b982c22020-04-01 01:10:07 +02001766 ret = run_command(
Benjamin Peterson95da3102019-06-29 16:00:22 -07001767 '"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc)
Victor Stinner6b982c22020-04-01 01:10:07 +02001768 if ret == 0:
Benjamin Peterson95da3102019-06-29 16:00:22 -07001769 extra_compile_args.append('-Wno-unreachable-code')
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001770
Victor Stinner8058bda2019-03-01 15:31:45 +01001771 self.add(Extension('pyexpat',
1772 define_macros=define_macros,
1773 extra_compile_args=extra_compile_args,
1774 include_dirs=expat_inc,
1775 libraries=expat_lib,
1776 sources=['pyexpat.c'] + expat_sources,
1777 depends=expat_depends))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001778
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001779 # Fredrik Lundh's cElementTree module. Note that this also
1780 # uses expat (via the CAPI hook in pyexpat).
1781
Victor Stinner625dbf22019-03-01 15:59:39 +01001782 if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001783 define_macros.append(('USE_PYEXPAT_CAPI', None))
Victor Stinner8058bda2019-03-01 15:31:45 +01001784 self.add(Extension('_elementtree',
1785 define_macros=define_macros,
1786 include_dirs=expat_inc,
1787 libraries=expat_lib,
1788 sources=['_elementtree.c'],
1789 depends=['pyexpat.c', *expat_sources,
1790 *expat_depends]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001791 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001792 self.missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001793
Victor Stinner5ec33a12019-03-01 16:43:28 +01001794 def detect_multibytecodecs(self):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001795 # Hye-Shik Chang's CJKCodecs modules.
Victor Stinner8058bda2019-03-01 15:31:45 +01001796 self.add(Extension('_multibytecodec',
1797 ['cjkcodecs/multibytecodec.c']))
Walter Dörwalde9eaab42007-05-22 16:02:13 +00001798 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001799 self.add(Extension('_codecs_%s' % loc,
1800 ['cjkcodecs/_codecs_%s.c' % loc]))
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001801
Victor Stinner5ec33a12019-03-01 16:43:28 +01001802 def detect_multiprocessing(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001803 # Richard Oudkerk's multiprocessing module
Victor Stinner4cbea512019-02-28 17:48:38 +01001804 if MS_WINDOWS:
Victor Stinnerc991f242019-03-01 17:19:04 +01001805 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c',
1806 '_multiprocessing/semaphore.c']
Benjamin Petersone711caf2008-06-11 16:44:04 +00001807 else:
Victor Stinnerc991f242019-03-01 17:19:04 +01001808 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c']
Mark Dickinsona614f042009-11-28 12:48:43 +00001809 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
1810 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001811 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
Victor Stinner8058bda2019-03-01 15:31:45 +01001812 self.add(Extension('_multiprocessing', multiprocessing_srcs,
Victor Stinner8058bda2019-03-01 15:31:45 +01001813 include_dirs=["Modules/_multiprocessing"]))
Guido van Rossuma9e20242007-03-08 00:43:48 +00001814
Victor Stinnercad80202021-01-19 23:04:49 +01001815 if (not MS_WINDOWS and
1816 sysconfig.get_config_var('HAVE_SHM_OPEN') and
1817 sysconfig.get_config_var('HAVE_SHM_UNLINK')):
1818 posixshmem_srcs = ['_multiprocessing/posixshmem.c']
1819 libs = []
1820 if sysconfig.get_config_var('SHM_NEEDS_LIBRT'):
1821 # need to link with librt to get shm_open()
1822 libs.append('rt')
1823 self.add(Extension('_posixshmem', posixshmem_srcs,
1824 define_macros={},
1825 libraries=libs,
1826 include_dirs=["Modules/_multiprocessing"]))
1827 else:
1828 self.missing.append('_posixshmem')
1829
Victor Stinner5ec33a12019-03-01 16:43:28 +01001830 def detect_uuid(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001831 # Build the _uuid module if possible
Victor Stinner625dbf22019-03-01 15:59:39 +01001832 uuid_incs = find_file("uuid.h", self.inc_dirs, ["/usr/include/uuid"])
Nick Coghlan53efbf32017-11-26 13:04:46 +10001833 if uuid_incs is not None:
Victor Stinner625dbf22019-03-01 15:59:39 +01001834 if self.compiler.find_library_file(self.lib_dirs, 'uuid'):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001835 uuid_libs = ['uuid']
1836 else:
1837 uuid_libs = []
Victor Stinnercfe172d2019-03-01 18:21:49 +01001838 self.add(Extension('_uuid', ['_uuidmodule.c'],
1839 libraries=uuid_libs,
1840 include_dirs=uuid_incs))
Antoine Pitroua106aec2017-09-28 23:03:06 +02001841 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001842 self.missing.append('_uuid')
Antoine Pitroua106aec2017-09-28 23:03:06 +02001843
Victor Stinner5ec33a12019-03-01 16:43:28 +01001844 def detect_modules(self):
Victor Stinnercfe172d2019-03-01 18:21:49 +01001845 self.configure_compiler()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001846 self.init_inc_lib_dirs()
1847
1848 self.detect_simple_extensions()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001849 if TEST_EXTENSIONS:
1850 self.detect_test_extensions()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001851 self.detect_readline_curses()
1852 self.detect_crypt()
1853 self.detect_socket()
1854 self.detect_openssl_hashlib()
xdegaye2ee077f2019-04-09 17:20:08 +02001855 self.detect_hash_builtins()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001856 self.detect_dbm_gdbm()
1857 self.detect_sqlite()
1858 self.detect_platform_specific_exts()
1859 self.detect_nis()
1860 self.detect_compress_exts()
1861 self.detect_expat_elementtree()
1862 self.detect_multibytecodecs()
1863 self.detect_decimal()
1864 self.detect_ctypes()
1865 self.detect_multiprocessing()
1866 if not self.detect_tkinter():
1867 self.missing.append('_tkinter')
1868 self.detect_uuid()
1869
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001870## # Uncomment these lines if you want to play with xxmodule.c
Victor Stinnercfe172d2019-03-01 18:21:49 +01001871## self.add(Extension('xx', ['xxmodule.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001872
Hai Shi5787ba42021-04-06 20:55:13 +08001873 # The limited C API is not compatible with the Py_TRACE_REFS macro.
1874 if not sysconfig.get_config_var('Py_TRACE_REFS'):
1875 self.add(Extension('xxlimited', ['xxlimited.c']))
1876 self.add(Extension('xxlimited_35', ['xxlimited_35.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001877
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001878 def detect_tkinter_fromenv(self):
1879 # Build _tkinter using the Tcl/Tk locations specified by
1880 # the _TCLTK_INCLUDES and _TCLTK_LIBS environment variables.
1881 # This method is meant to be invoked by detect_tkinter().
Ned Deilyd819b932013-09-06 01:07:05 -07001882 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001883 # The variables can be set via one of the following ways.
Ned Deilyd819b932013-09-06 01:07:05 -07001884 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001885 # - Automatically, at configuration time, by using pkg-config.
1886 # The tool is called by the configure script.
1887 # Additional pkg-config configuration paths can be set via the
1888 # PKG_CONFIG_PATH environment variable.
1889 #
1890 # PKG_CONFIG_PATH=".../lib/pkgconfig" ./configure ...
1891 #
1892 # - Explicitly, at configuration time by setting both
1893 # --with-tcltk-includes and --with-tcltk-libs.
1894 #
1895 # ./configure ... \
Ned Deilyd819b932013-09-06 01:07:05 -07001896 # --with-tcltk-includes="-I/path/to/tclincludes \
1897 # -I/path/to/tkincludes"
1898 # --with-tcltk-libs="-L/path/to/tcllibs -ltclm.n \
1899 # -L/path/to/tklibs -ltkm.n"
1900 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001901 # - Explicitly, at compile time, by passing TCLTK_INCLUDES and
1902 # TCLTK_LIBS to the make target.
1903 # This will override any configuration-time option.
1904 #
1905 # make TCLTK_INCLUDES="..." TCLTK_LIBS="..."
Ned Deilyd819b932013-09-06 01:07:05 -07001906 #
1907 # This can be useful for building and testing tkinter with multiple
1908 # versions of Tcl/Tk. Note that a build of Tk depends on a particular
1909 # build of Tcl so you need to specify both arguments and use care when
1910 # overriding.
1911
1912 # The _TCLTK variables are created in the Makefile sharedmods target.
1913 tcltk_includes = os.environ.get('_TCLTK_INCLUDES')
1914 tcltk_libs = os.environ.get('_TCLTK_LIBS')
1915 if not (tcltk_includes and tcltk_libs):
1916 # Resume default configuration search.
Victor Stinner4cbea512019-02-28 17:48:38 +01001917 return False
Ned Deilyd819b932013-09-06 01:07:05 -07001918
1919 extra_compile_args = tcltk_includes.split()
1920 extra_link_args = tcltk_libs.split()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001921 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1922 define_macros=[('WITH_APPINIT', 1)],
1923 extra_compile_args = extra_compile_args,
1924 extra_link_args = extra_link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01001925 return True
Ned Deilyd819b932013-09-06 01:07:05 -07001926
Victor Stinner625dbf22019-03-01 15:59:39 +01001927 def detect_tkinter_darwin(self):
Ned Deily1731d6d2020-05-18 04:32:38 -04001928 # Build default _tkinter on macOS using Tcl and Tk frameworks.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001929 # This method is meant to be invoked by detect_tkinter().
Ned Deily1731d6d2020-05-18 04:32:38 -04001930 #
1931 # The macOS native Tk (AKA Aqua Tk) and Tcl are most commonly
1932 # built and installed as macOS framework bundles. However,
1933 # for several reasons, we cannot take full advantage of the
1934 # Apple-supplied compiler chain's -framework options here.
1935 # Instead, we need to find and pass to the compiler the
1936 # absolute paths of the Tcl and Tk headers files we want to use
1937 # and the absolute path to the directory containing the Tcl
1938 # and Tk frameworks for linking.
1939 #
1940 # We want to handle here two common use cases on macOS:
1941 # 1. Build and link with system-wide third-party or user-built
1942 # Tcl and Tk frameworks installed in /Library/Frameworks.
1943 # 2. Build and link using a user-specified macOS SDK so that the
1944 # built Python can be exported to other systems. In this case,
1945 # search only the SDK's /Library/Frameworks (normally empty)
1946 # and /System/Library/Frameworks.
1947 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001948 # Any other use cases are handled either by detect_tkinter_fromenv(),
1949 # or detect_tkinter(). The former handles non-standard locations of
1950 # Tcl/Tk, defined via the _TCLTK_INCLUDES and _TCLTK_LIBS environment
1951 # variables. The latter handles any Tcl/Tk versions installed in
1952 # standard Unix directories.
1953 #
1954 # It would be desirable to also handle here the case where
Ned Deily1731d6d2020-05-18 04:32:38 -04001955 # you want to build and link with a framework build of Tcl and Tk
1956 # that is not in /Library/Frameworks, say, in your private
1957 # $HOME/Library/Frameworks directory or elsewhere. It turns
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301958 # out to be difficult to make that work automatically here
Ned Deily1731d6d2020-05-18 04:32:38 -04001959 # without bringing into play more tools and magic. That case
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301960 # can be handled using a recipe with the right arguments
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001961 # to detect_tkinter_fromenv().
Ned Deily1731d6d2020-05-18 04:32:38 -04001962 #
1963 # Note also that the fallback case here is to try to use the
1964 # Apple-supplied Tcl and Tk frameworks in /System/Library but
1965 # be forewarned that they are deprecated by Apple and typically
1966 # out-of-date and buggy; their use should be avoided if at
1967 # all possible by installing a newer version of Tcl and Tk in
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301968 # /Library/Frameworks before building Python without
Ned Deily1731d6d2020-05-18 04:32:38 -04001969 # an explicit SDK or by configuring build arguments explicitly.
1970
Jack Jansen0b06be72002-06-21 14:48:38 +00001971 from os.path import join, exists
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001972
Ned Deily1731d6d2020-05-18 04:32:38 -04001973 sysroot = macosx_sdk_root() # path to the SDK or '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001974
Ned Deily1731d6d2020-05-18 04:32:38 -04001975 if macosx_sdk_specified():
1976 # Use case #2: an SDK other than '/' was specified.
1977 # Only search there.
1978 framework_dirs = [
1979 join(sysroot, 'Library', 'Frameworks'),
1980 join(sysroot, 'System', 'Library', 'Frameworks'),
1981 ]
1982 else:
1983 # Use case #1: no explicit SDK selected.
1984 # Search the local system-wide /Library/Frameworks,
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301985 # not the one in the default SDK, otherwise fall back to
Ned Deily1731d6d2020-05-18 04:32:38 -04001986 # /System/Library/Frameworks whose header files may be in
1987 # the default SDK or, on older systems, actually installed.
1988 framework_dirs = [
1989 join('/', 'Library', 'Frameworks'),
1990 join(sysroot, 'System', 'Library', 'Frameworks'),
1991 ]
1992
1993 # Find the directory that contains the Tcl.framework and
1994 # Tk.framework bundles.
Jack Jansen0b06be72002-06-21 14:48:38 +00001995 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00001996 # both Tcl.framework and Tk.framework should be present
Jack Jansen0b06be72002-06-21 14:48:38 +00001997 for fw in 'Tcl', 'Tk':
Ned Deily1731d6d2020-05-18 04:32:38 -04001998 if not exists(join(F, fw + '.framework')):
1999 break
Jack Jansen0b06be72002-06-21 14:48:38 +00002000 else:
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302001 # ok, F is now directory with both frameworks. Continue
Jack Jansen0b06be72002-06-21 14:48:38 +00002002 # building
2003 break
2004 else:
2005 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
2006 # will now resume.
Victor Stinner4cbea512019-02-28 17:48:38 +01002007 return False
Tim Peters2c60f7a2003-01-29 03:49:43 +00002008
Jack Jansen0b06be72002-06-21 14:48:38 +00002009 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00002010 join(F, fw + '.framework', H)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002011 for fw in ('Tcl', 'Tk')
Ned Deily1731d6d2020-05-18 04:32:38 -04002012 for H in ('Headers',)
Jack Jansen0b06be72002-06-21 14:48:38 +00002013 ]
2014
Ned Deily1731d6d2020-05-18 04:32:38 -04002015 # Add the base framework directory as well
2016 compile_args = ['-F', F]
Jack Jansen0b06be72002-06-21 14:48:38 +00002017
Ned Deily1731d6d2020-05-18 04:32:38 -04002018 # Do not build tkinter for archs that this Tk was not built with.
Georg Brandlfcaf9102008-07-16 02:17:56 +00002019 cflags = sysconfig.get_config_vars('CFLAGS')[0]
R David Murray44b548d2016-09-08 13:59:53 -04002020 archs = re.findall(r'-arch\s+(\w+)', cflags)
Georg Brandlfcaf9102008-07-16 02:17:56 +00002021
Ronald Oussorend097efe2009-09-15 19:07:58 +00002022 tmpfile = os.path.join(self.build_temp, 'tk.arch')
2023 if not os.path.exists(self.build_temp):
2024 os.makedirs(self.build_temp)
2025
Ned Deily1731d6d2020-05-18 04:32:38 -04002026 run_command(
2027 "file {}/Tk.framework/Tk | grep 'for architecture' > {}".format(F, tmpfile)
2028 )
Brett Cannon9f5db072010-10-29 20:19:27 +00002029 with open(tmpfile) as fp:
2030 detected_archs = []
2031 for ln in fp:
2032 a = ln.split()[-1]
2033 if a in archs:
2034 detected_archs.append(ln.split()[-1])
Ronald Oussorend097efe2009-09-15 19:07:58 +00002035 os.unlink(tmpfile)
2036
Ned Deily1731d6d2020-05-18 04:32:38 -04002037 arch_args = []
Ronald Oussorend097efe2009-09-15 19:07:58 +00002038 for a in detected_archs:
Ned Deily1731d6d2020-05-18 04:32:38 -04002039 arch_args.append('-arch')
2040 arch_args.append(a)
2041
2042 compile_args += arch_args
2043 link_args = [','.join(['-Wl', '-F', F, '-framework', 'Tcl', '-framework', 'Tk']), *arch_args]
2044
2045 # The X11/xlib.h file bundled in the Tk sources can cause function
2046 # prototype warnings from the compiler. Since we cannot easily fix
2047 # that, suppress the warnings here instead.
2048 if '-Wstrict-prototypes' in cflags.split():
2049 compile_args.append('-Wno-strict-prototypes')
Georg Brandlfcaf9102008-07-16 02:17:56 +00002050
Victor Stinnercfe172d2019-03-01 18:21:49 +01002051 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2052 define_macros=[('WITH_APPINIT', 1)],
2053 include_dirs=include_dirs,
2054 libraries=[],
Ned Deily1731d6d2020-05-18 04:32:38 -04002055 extra_compile_args=compile_args,
2056 extra_link_args=link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01002057 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002058
Victor Stinner625dbf22019-03-01 15:59:39 +01002059 def detect_tkinter(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002060 # The _tkinter module.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002061 #
2062 # Detection of Tcl/Tk is attempted in the following order:
2063 # - Through environment variables.
2064 # - Platform specific detection of Tcl/Tk (currently only macOS).
2065 # - Search of various standard Unix header/library paths.
2066 #
2067 # Detection stops at the first successful method.
Michael W. Hudson5b109102002-01-23 15:04:41 +00002068
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002069 # Check for Tcl and Tk at the locations indicated by _TCLTK_INCLUDES
2070 # and _TCLTK_LIBS environment variables.
2071 if self.detect_tkinter_fromenv():
Victor Stinner5ec33a12019-03-01 16:43:28 +01002072 return True
Ned Deilyd819b932013-09-06 01:07:05 -07002073
Jack Jansen0b06be72002-06-21 14:48:38 +00002074 # Rather than complicate the code below, detecting and building
2075 # AquaTk is a separate method. Only one Tkinter will be built on
2076 # Darwin - either AquaTk, if it is found, or X11 based Tk.
Victor Stinner5ec33a12019-03-01 16:43:28 +01002077 if (MACOS and self.detect_tkinter_darwin()):
2078 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002079
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002080 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002081 # The versions with dots are used on Unix, and the versions without
2082 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002083 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polo5d377bd2009-08-16 14:44:14 +00002084 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
2085 '8.2', '82', '8.1', '81', '8.0', '80']:
Victor Stinner625dbf22019-03-01 15:59:39 +01002086 tklib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002087 'tk' + version)
Victor Stinner625dbf22019-03-01 15:59:39 +01002088 tcllib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002089 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00002090 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002091 # Exit the loop when we've found the Tcl/Tk libraries
2092 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002093
Fredrik Lundhade711a2001-01-24 08:00:28 +00002094 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002095 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002096 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002097 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002098 dotversion = version
Victor Stinner4cbea512019-02-28 17:48:38 +01002099 if '.' not in dotversion and "bsd" in HOST_PLATFORM.lower():
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002100 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
2101 # but the include subdirs are named like .../include/tcl8.3.
2102 dotversion = dotversion[:-1] + '.' + dotversion[-1]
2103 tcl_include_sub = []
2104 tk_include_sub = []
Victor Stinner625dbf22019-03-01 15:59:39 +01002105 for dir in self.inc_dirs:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002106 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
2107 tk_include_sub += [dir + os.sep + "tk" + dotversion]
2108 tk_include_sub += tcl_include_sub
Victor Stinner625dbf22019-03-01 15:59:39 +01002109 tcl_includes = find_file('tcl.h', self.inc_dirs, tcl_include_sub)
2110 tk_includes = find_file('tk.h', self.inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002111
Martin v. Löwise86a59a2003-05-03 08:45:51 +00002112 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002113 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002114 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Victor Stinner5ec33a12019-03-01 16:43:28 +01002115 return False
Fredrik Lundhade711a2001-01-24 08:00:28 +00002116
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002117 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002118
Victor Stinnercfe172d2019-03-01 18:21:49 +01002119 include_dirs = []
2120 libs = []
2121 defs = []
2122 added_lib_dirs = []
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002123 for dir in tcl_includes + tk_includes:
2124 if dir not in include_dirs:
2125 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002126
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002127 # Check for various platform-specific directories
Victor Stinner4cbea512019-02-28 17:48:38 +01002128 if HOST_PLATFORM == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002129 include_dirs.append('/usr/openwin/include')
2130 added_lib_dirs.append('/usr/openwin/lib')
2131 elif os.path.exists('/usr/X11R6/include'):
2132 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00002133 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002134 added_lib_dirs.append('/usr/X11R6/lib')
2135 elif os.path.exists('/usr/X11R5/include'):
2136 include_dirs.append('/usr/X11R5/include')
2137 added_lib_dirs.append('/usr/X11R5/lib')
2138 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002139 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002140 include_dirs.append('/usr/X11/include')
2141 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002142
Jason Tishler9181c942003-02-05 15:16:17 +00002143 # If Cygwin, then verify that X is installed before proceeding
Victor Stinner4cbea512019-02-28 17:48:38 +01002144 if CYGWIN:
Jason Tishler9181c942003-02-05 15:16:17 +00002145 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
2146 if x11_inc is None:
Victor Stinner5ec33a12019-03-01 16:43:28 +01002147 return False
Jason Tishler9181c942003-02-05 15:16:17 +00002148
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002149 # Check for BLT extension
Victor Stinner625dbf22019-03-01 15:59:39 +01002150 if self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002151 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002152 defs.append( ('WITH_BLT', 1) )
2153 libs.append('BLT8.0')
Victor Stinner625dbf22019-03-01 15:59:39 +01002154 elif self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002155 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00002156 defs.append( ('WITH_BLT', 1) )
2157 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002158
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002159 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00002160 libs.append('tk'+ version)
2161 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002162
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002163 # Finally, link with the X11 libraries (not appropriate on cygwin)
Victor Stinner4cbea512019-02-28 17:48:38 +01002164 if not CYGWIN:
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002165 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002166
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002167 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002168 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002169 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002170 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002171 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002172 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002173 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002174
Victor Stinnercfe172d2019-03-01 18:21:49 +01002175 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2176 define_macros=[('WITH_APPINIT', 1)] + defs,
2177 include_dirs=include_dirs,
2178 libraries=libs,
2179 library_dirs=added_lib_dirs))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002180 return True
2181
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002182 def configure_ctypes(self, ext):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002183 return True
2184
Victor Stinner625dbf22019-03-01 15:59:39 +01002185 def detect_ctypes(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01002186 # Thomas Heller's _ctypes module
Ronald Oussoren41761932020-11-08 10:05:27 +01002187
2188 if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
2189 self.use_system_libffi = True
2190 else:
2191 self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
2192
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002193 include_dirs = []
Victor Stinner1ae035b2020-04-17 17:47:20 +02002194 extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002195 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00002196 sources = ['_ctypes/_ctypes.c',
2197 '_ctypes/callbacks.c',
2198 '_ctypes/callproc.c',
2199 '_ctypes/stgdict.c',
Thomas Heller864cc672010-08-08 17:58:53 +00002200 '_ctypes/cfield.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00002201 depends = ['_ctypes/ctypes.h']
2202
Victor Stinner4cbea512019-02-28 17:48:38 +01002203 if MACOS:
Ronald Oussoren2decf222010-09-05 18:25:59 +00002204 sources.append('_ctypes/malloc_closure.c')
Ronald Oussoren41761932020-11-08 10:05:27 +01002205 extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
Christian Heimes78644762008-03-04 23:39:23 +00002206 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00002207 include_dirs.append('_ctypes/darwin')
Thomas Hellercf567c12006-03-08 19:51:58 +00002208
Victor Stinner4cbea512019-02-28 17:48:38 +01002209 elif HOST_PLATFORM == 'sunos5':
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002210 # XXX This shouldn't be necessary; it appears that some
2211 # of the assembler code is non-PIC (i.e. it has relocations
2212 # when it shouldn't. The proper fix would be to rewrite
2213 # the assembler code to be PIC.
2214 # This only works with GCC; the Sun compiler likely refuses
2215 # this option. If you want to compile ctypes with the Sun
2216 # compiler, please research a proper solution, instead of
2217 # finding some -z option for the Sun compiler.
2218 extra_link_args.append('-mimpure-text')
2219
Victor Stinner4cbea512019-02-28 17:48:38 +01002220 elif HOST_PLATFORM.startswith('hp-ux'):
Thomas Heller3eaaeb42008-05-23 17:26:46 +00002221 extra_link_args.append('-fPIC')
2222
Thomas Hellercf567c12006-03-08 19:51:58 +00002223 ext = Extension('_ctypes',
2224 include_dirs=include_dirs,
2225 extra_compile_args=extra_compile_args,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002226 extra_link_args=extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002227 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00002228 sources=sources,
2229 depends=depends)
Victor Stinnercfe172d2019-03-01 18:21:49 +01002230 self.add(ext)
2231 if TEST_EXTENSIONS:
2232 # function my_sqrt() needs libm for sqrt()
2233 self.add(Extension('_ctypes_test',
2234 sources=['_ctypes/_ctypes_test.c'],
2235 libraries=['m']))
Thomas Hellercf567c12006-03-08 19:51:58 +00002236
Ronald Oussoren41761932020-11-08 10:05:27 +01002237 ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
2238 ffi_lib = None
2239
Victor Stinner625dbf22019-03-01 15:59:39 +01002240 ffi_inc_dirs = self.inc_dirs.copy()
Victor Stinner4cbea512019-02-28 17:48:38 +01002241 if MACOS:
Ronald Oussoren41761932020-11-08 10:05:27 +01002242 ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
Christian Heimes78644762008-03-04 23:39:23 +00002243
Ronald Oussoren41761932020-11-08 10:05:27 +01002244 if not ffi_inc:
2245 if os.path.exists(ffi_in_sdk):
2246 ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
2247 ffi_inc = ffi_in_sdk
2248 ffi_lib = 'ffi'
2249 else:
2250 # OS X 10.5 comes with libffi.dylib; the include files are
2251 # in /usr/include/ffi
2252 ffi_inc_dirs.append('/usr/include/ffi')
2253
2254 if not ffi_inc:
2255 found = find_file('ffi.h', [], ffi_inc_dirs)
2256 if found:
2257 ffi_inc = found[0]
2258 if ffi_inc:
2259 ffi_h = ffi_inc + '/ffi.h'
Shlomi Fish6d51b872017-09-06 23:19:19 +03002260 if not os.path.exists(ffi_h):
2261 ffi_inc = None
2262 print('Header file {} does not exist'.format(ffi_h))
Ronald Oussoren41761932020-11-08 10:05:27 +01002263 if ffi_lib is None and ffi_inc:
doko@ubuntu.comae683652016-06-05 01:38:29 +02002264 for lib_name in ('ffi', 'ffi_pic'):
Victor Stinner625dbf22019-03-01 15:59:39 +01002265 if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266 ffi_lib = lib_name
2267 break
2268
2269 if ffi_inc and ffi_lib:
Ronald Oussoren41761932020-11-08 10:05:27 +01002270 ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
2271 if grep_headers_for('ffi_prep_cif_var', ffi_headers):
2272 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
2273 if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
2274 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
2275 if grep_headers_for('ffi_closure_alloc', ffi_headers):
2276 ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
2277
2278 ext.include_dirs.append(ffi_inc)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002279 ext.libraries.append(ffi_lib)
2280 self.use_system_libffi = True
2281
Christian Heimes5bb96922018-02-25 10:22:14 +01002282 if sysconfig.get_config_var('HAVE_LIBDL'):
2283 # for dlopen, see bpo-32647
2284 ext.libraries.append('dl')
2285
Victor Stinner5ec33a12019-03-01 16:43:28 +01002286 def detect_decimal(self):
2287 # Stefan Krah's _decimal module
Stefan Krah60187b52012-03-23 19:06:27 +01002288 extra_compile_args = []
Stefan Kraha10e2fb2012-09-01 14:21:22 +02002289 undef_macros = []
Stefan Krah60187b52012-03-23 19:06:27 +01002290 if '--with-system-libmpdec' in sysconfig.get_config_var("CONFIG_ARGS"):
2291 include_dirs = []
Antoine Pitrou73b20ae2021-03-30 18:11:06 +02002292 libraries = ['mpdec']
Stefan Krah60187b52012-03-23 19:06:27 +01002293 sources = ['_decimal/_decimal.c']
2294 depends = ['_decimal/docstrings.h']
2295 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01002296 include_dirs = [os.path.abspath(os.path.join(self.srcdir,
Ned Deily458a6fb2012-04-01 02:30:46 -07002297 'Modules',
2298 '_decimal',
2299 'libmpdec'))]
Stefan Krahbd4ed772017-12-06 18:24:17 +01002300 libraries = ['m']
Stefan Krah60187b52012-03-23 19:06:27 +01002301 sources = [
2302 '_decimal/_decimal.c',
2303 '_decimal/libmpdec/basearith.c',
2304 '_decimal/libmpdec/constants.c',
2305 '_decimal/libmpdec/context.c',
2306 '_decimal/libmpdec/convolute.c',
2307 '_decimal/libmpdec/crt.c',
2308 '_decimal/libmpdec/difradix2.c',
2309 '_decimal/libmpdec/fnt.c',
2310 '_decimal/libmpdec/fourstep.c',
2311 '_decimal/libmpdec/io.c',
Stefan Krahf117d872019-07-10 18:27:38 +02002312 '_decimal/libmpdec/mpalloc.c',
Stefan Krah60187b52012-03-23 19:06:27 +01002313 '_decimal/libmpdec/mpdecimal.c',
2314 '_decimal/libmpdec/numbertheory.c',
2315 '_decimal/libmpdec/sixstep.c',
2316 '_decimal/libmpdec/transpose.c',
2317 ]
2318 depends = [
2319 '_decimal/docstrings.h',
2320 '_decimal/libmpdec/basearith.h',
2321 '_decimal/libmpdec/bits.h',
2322 '_decimal/libmpdec/constants.h',
2323 '_decimal/libmpdec/convolute.h',
2324 '_decimal/libmpdec/crt.h',
2325 '_decimal/libmpdec/difradix2.h',
2326 '_decimal/libmpdec/fnt.h',
2327 '_decimal/libmpdec/fourstep.h',
2328 '_decimal/libmpdec/io.h',
Stefan Krah8d013a82016-04-26 16:34:41 +02002329 '_decimal/libmpdec/mpalloc.h',
Stefan Krah60187b52012-03-23 19:06:27 +01002330 '_decimal/libmpdec/mpdecimal.h',
2331 '_decimal/libmpdec/numbertheory.h',
2332 '_decimal/libmpdec/sixstep.h',
2333 '_decimal/libmpdec/transpose.h',
2334 '_decimal/libmpdec/typearith.h',
2335 '_decimal/libmpdec/umodarith.h',
2336 ]
2337
Stefan Krah1919b7e2012-03-21 18:25:23 +01002338 config = {
2339 'x64': [('CONFIG_64','1'), ('ASM','1')],
2340 'uint128': [('CONFIG_64','1'), ('ANSI','1'), ('HAVE_UINT128_T','1')],
2341 'ansi64': [('CONFIG_64','1'), ('ANSI','1')],
2342 'ppro': [('CONFIG_32','1'), ('PPRO','1'), ('ASM','1')],
2343 'ansi32': [('CONFIG_32','1'), ('ANSI','1')],
2344 'ansi-legacy': [('CONFIG_32','1'), ('ANSI','1'),
2345 ('LEGACY_COMPILER','1')],
2346 'universal': [('UNIVERSAL','1')]
2347 }
2348
Stefan Krah1919b7e2012-03-21 18:25:23 +01002349 cc = sysconfig.get_config_var('CC')
2350 sizeof_size_t = sysconfig.get_config_var('SIZEOF_SIZE_T')
2351 machine = os.environ.get('PYTHON_DECIMAL_WITH_MACHINE')
2352
2353 if machine:
2354 # Override automatic configuration to facilitate testing.
2355 define_macros = config[machine]
Victor Stinner4cbea512019-02-28 17:48:38 +01002356 elif MACOS:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002357 # Universal here means: build with the same options Python
2358 # was built with.
2359 define_macros = config['universal']
2360 elif sizeof_size_t == 8:
2361 if sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X64'):
2362 define_macros = config['x64']
2363 elif sysconfig.get_config_var('HAVE_GCC_UINT128_T'):
2364 define_macros = config['uint128']
2365 else:
2366 define_macros = config['ansi64']
2367 elif sizeof_size_t == 4:
2368 ppro = sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X87')
2369 if ppro and ('gcc' in cc or 'clang' in cc) and \
Victor Stinner4cbea512019-02-28 17:48:38 +01002370 not 'sunos' in HOST_PLATFORM:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002371 # solaris: problems with register allocation.
2372 # icc >= 11.0 works as well.
2373 define_macros = config['ppro']
Stefan Krahce23dbc2012-09-30 21:12:53 +02002374 extra_compile_args.append('-Wno-unknown-pragmas')
Stefan Krah1919b7e2012-03-21 18:25:23 +01002375 else:
2376 define_macros = config['ansi32']
2377 else:
2378 raise DistutilsError("_decimal: unsupported architecture")
2379
2380 # Workarounds for toolchain bugs:
2381 if sysconfig.get_config_var('HAVE_IPA_PURE_CONST_BUG'):
2382 # Some versions of gcc miscompile inline asm:
2383 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46491
2384 # http://gcc.gnu.org/ml/gcc/2010-11/msg00366.html
2385 extra_compile_args.append('-fno-ipa-pure-const')
2386 if sysconfig.get_config_var('HAVE_GLIBC_MEMMOVE_BUG'):
2387 # _FORTIFY_SOURCE wrappers for memmove and bcopy are incorrect:
2388 # http://sourceware.org/ml/libc-alpha/2010-12/msg00009.html
2389 undef_macros.append('_FORTIFY_SOURCE')
2390
Stefan Krah1919b7e2012-03-21 18:25:23 +01002391 # Uncomment for extra functionality:
2392 #define_macros.append(('EXTRA_FUNCTIONALITY', 1))
Victor Stinner8058bda2019-03-01 15:31:45 +01002393 self.add(Extension('_decimal',
2394 include_dirs=include_dirs,
2395 libraries=libraries,
2396 define_macros=define_macros,
2397 undef_macros=undef_macros,
2398 extra_compile_args=extra_compile_args,
2399 sources=sources,
2400 depends=depends))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002401
Victor Stinner5ec33a12019-03-01 16:43:28 +01002402 def detect_openssl_hashlib(self):
2403 # Detect SSL support for the socket module (via _ssl)
Christian Heimesff5be6e2018-01-20 13:19:21 +01002404 config_vars = sysconfig.get_config_vars()
2405
2406 def split_var(name, sep):
2407 # poor man's shlex, the re module is not available yet.
2408 value = config_vars.get(name)
2409 if not value:
2410 return ()
2411 # This trick works because ax_check_openssl uses --libs-only-L,
2412 # --libs-only-l, and --cflags-only-I.
2413 value = ' ' + value
2414 sep = ' ' + sep
2415 return [v.strip() for v in value.split(sep) if v.strip()]
2416
2417 openssl_includes = split_var('OPENSSL_INCLUDES', '-I')
2418 openssl_libdirs = split_var('OPENSSL_LDFLAGS', '-L')
2419 openssl_libs = split_var('OPENSSL_LIBS', '-l')
Christian Heimes32eba612021-03-19 10:29:25 +01002420 openssl_rpath = config_vars.get('OPENSSL_RPATH')
Christian Heimesff5be6e2018-01-20 13:19:21 +01002421 if not openssl_libs:
2422 # libssl and libcrypto not found
Christian Heimes8abc3f42019-04-09 18:40:12 +02002423 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002424 return None, None
2425
2426 # Find OpenSSL includes
2427 ssl_incs = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002428 'openssl/ssl.h', self.inc_dirs, openssl_includes
Christian Heimesff5be6e2018-01-20 13:19:21 +01002429 )
2430 if ssl_incs is None:
Christian Heimes8abc3f42019-04-09 18:40:12 +02002431 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002432 return None, None
2433
Christian Heimes32eba612021-03-19 10:29:25 +01002434 if openssl_rpath == 'auto':
2435 runtime_library_dirs = openssl_libdirs[:]
2436 elif not openssl_rpath:
2437 runtime_library_dirs = []
2438 else:
2439 runtime_library_dirs = [openssl_rpath]
2440
Christian Heimesbacefbf2021-03-27 18:03:54 +01002441 openssl_extension_kwargs = dict(
2442 include_dirs=openssl_includes,
2443 library_dirs=openssl_libdirs,
2444 libraries=openssl_libs,
2445 runtime_library_dirs=runtime_library_dirs,
2446 )
2447
2448 # This static linking is NOT OFFICIALLY SUPPORTED.
2449 # Requires static OpenSSL build with position-independent code. Some
2450 # features like DSO engines or external OSSL providers don't work.
2451 # Only tested on GCC and clang on X86_64.
2452 if os.environ.get("PY_UNSUPPORTED_OPENSSL_BUILD") == "static":
2453 extra_linker_args = []
2454 for lib in openssl_extension_kwargs["libraries"]:
2455 # link statically
2456 extra_linker_args.append(f"-l:lib{lib}.a")
2457 # don't export symbols
2458 extra_linker_args.append(f"-Wl,--exclude-libs,lib{lib}.a")
2459 openssl_extension_kwargs["extra_link_args"] = extra_linker_args
2460 # don't link OpenSSL shared libraries.
2461 openssl_extension_kwargs["libraries"] = []
2462
Christian Heimes39258d32021-04-17 11:36:35 +02002463 self.add(
2464 Extension(
2465 '_ssl',
2466 ['_ssl.c'],
Christian Heimes7f1305e2021-04-17 20:06:38 +02002467 depends=['socketmodule.h', '_ssl/debughelpers.c', '_ssl.h'],
Christian Heimes39258d32021-04-17 11:36:35 +02002468 **openssl_extension_kwargs
Christian Heimesc7f70692019-05-31 11:44:05 +02002469 )
Christian Heimes39258d32021-04-17 11:36:35 +02002470 )
Christian Heimesbacefbf2021-03-27 18:03:54 +01002471 self.add(
2472 Extension(
2473 '_hashlib',
2474 ['_hashopenssl.c'],
2475 depends=['hashlib.h'],
2476 **openssl_extension_kwargs,
2477 )
2478 )
Christian Heimesff5be6e2018-01-20 13:19:21 +01002479
xdegaye2ee077f2019-04-09 17:20:08 +02002480 def detect_hash_builtins(self):
Christian Heimes9b60e552020-05-15 23:54:53 +02002481 # By default we always compile these even when OpenSSL is available
2482 # (issue #14693). It's harmless and the object code is tiny
2483 # (40-50 KiB per module, only loaded when actually used). Modules can
2484 # be disabled via the --with-builtin-hashlib-hashes configure flag.
2485 supported = {"md5", "sha1", "sha256", "sha512", "sha3", "blake2"}
Victor Stinner5ec33a12019-03-01 16:43:28 +01002486
Christian Heimes9b60e552020-05-15 23:54:53 +02002487 configured = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
2488 configured = configured.strip('"').lower()
2489 configured = {
2490 m.strip() for m in configured.split(",")
2491 }
Victor Stinner5ec33a12019-03-01 16:43:28 +01002492
Christian Heimes9b60e552020-05-15 23:54:53 +02002493 self.disabled_configure.extend(
2494 sorted(supported.difference(configured))
2495 )
Victor Stinner5ec33a12019-03-01 16:43:28 +01002496
Christian Heimes9b60e552020-05-15 23:54:53 +02002497 if "sha256" in configured:
2498 self.add(Extension(
2499 '_sha256', ['sha256module.c'],
2500 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2501 depends=['hashlib.h']
2502 ))
2503
2504 if "sha512" in configured:
2505 self.add(Extension(
2506 '_sha512', ['sha512module.c'],
2507 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2508 depends=['hashlib.h']
2509 ))
2510
2511 if "md5" in configured:
2512 self.add(Extension(
2513 '_md5', ['md5module.c'],
2514 depends=['hashlib.h']
2515 ))
2516
2517 if "sha1" in configured:
2518 self.add(Extension(
2519 '_sha1', ['sha1module.c'],
2520 depends=['hashlib.h']
2521 ))
2522
2523 if "blake2" in configured:
2524 blake2_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002525 os.path.join(escape(self.srcdir), 'Modules/_blake2/impl/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002526 )
2527 blake2_deps.append('hashlib.h')
2528 self.add(Extension(
2529 '_blake2',
2530 [
2531 '_blake2/blake2module.c',
2532 '_blake2/blake2b_impl.c',
2533 '_blake2/blake2s_impl.c'
2534 ],
2535 depends=blake2_deps
2536 ))
2537
2538 if "sha3" in configured:
2539 sha3_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002540 os.path.join(escape(self.srcdir), 'Modules/_sha3/kcp/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002541 )
2542 sha3_deps.append('hashlib.h')
2543 self.add(Extension(
2544 '_sha3',
2545 ['_sha3/sha3module.c'],
2546 depends=sha3_deps
2547 ))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002548
2549 def detect_nis(self):
Victor Stinner4cbea512019-02-28 17:48:38 +01002550 if MS_WINDOWS or CYGWIN or HOST_PLATFORM == 'qnx6':
Victor Stinner8058bda2019-03-01 15:31:45 +01002551 self.missing.append('nis')
2552 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002553
2554 libs = []
2555 library_dirs = []
2556 includes_dirs = []
2557
2558 # bpo-32521: glibc has deprecated Sun RPC for some time. Fedora 28
2559 # moved headers and libraries to libtirpc and libnsl. The headers
2560 # are in tircp and nsl sub directories.
2561 rpcsvc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002562 'rpcsvc/yp_prot.h', self.inc_dirs,
2563 [os.path.join(inc_dir, 'nsl') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002564 )
2565 rpc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002566 'rpc/rpc.h', self.inc_dirs,
2567 [os.path.join(inc_dir, 'tirpc') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002568 )
2569 if rpcsvc_inc is None or rpc_inc is None:
2570 # not found
Victor Stinner8058bda2019-03-01 15:31:45 +01002571 self.missing.append('nis')
2572 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002573 includes_dirs.extend(rpcsvc_inc)
2574 includes_dirs.extend(rpc_inc)
2575
Victor Stinner625dbf22019-03-01 15:59:39 +01002576 if self.compiler.find_library_file(self.lib_dirs, 'nsl'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002577 libs.append('nsl')
2578 else:
2579 # libnsl-devel: check for libnsl in nsl/ subdirectory
Victor Stinner625dbf22019-03-01 15:59:39 +01002580 nsl_dirs = [os.path.join(lib_dir, 'nsl') for lib_dir in self.lib_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002581 libnsl = self.compiler.find_library_file(nsl_dirs, 'nsl')
2582 if libnsl is not None:
2583 library_dirs.append(os.path.dirname(libnsl))
2584 libs.append('nsl')
2585
Victor Stinner625dbf22019-03-01 15:59:39 +01002586 if self.compiler.find_library_file(self.lib_dirs, 'tirpc'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002587 libs.append('tirpc')
2588
Victor Stinner8058bda2019-03-01 15:31:45 +01002589 self.add(Extension('nis', ['nismodule.c'],
2590 libraries=libs,
2591 library_dirs=library_dirs,
2592 include_dirs=includes_dirs))
Christian Heimes29a7df72018-01-26 23:28:46 +01002593
Christian Heimesff5be6e2018-01-20 13:19:21 +01002594
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00002595class PyBuildInstall(install):
2596 # Suppress the warning about installation into the lib_dynload
2597 # directory, which is not in sys.path when running Python during
2598 # installation:
2599 def initialize_options (self):
2600 install.initialize_options(self)
2601 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00002602
Éric Araujoe6792c12011-06-09 14:07:02 +02002603 # Customize subcommands to not install an egg-info file for Python
2604 sub_commands = [('install_lib', install.has_lib),
2605 ('install_headers', install.has_headers),
2606 ('install_scripts', install.has_scripts),
2607 ('install_data', install.has_data)]
2608
2609
Michael W. Hudson529a5052002-12-17 16:47:17 +00002610class PyBuildInstallLib(install_lib):
2611 # Do exactly what install_lib does but make sure correct access modes get
2612 # set on installed directories and files. All installed files with get
2613 # mode 644 unless they are a shared library in which case they will get
2614 # mode 755. All installed directories will get mode 755.
2615
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002616 # this is works for EXT_SUFFIX too, which ends with SHLIB_SUFFIX
2617 shlib_suffix = sysconfig.get_config_var("SHLIB_SUFFIX")
Michael W. Hudson529a5052002-12-17 16:47:17 +00002618
2619 def install(self):
2620 outfiles = install_lib.install(self)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002621 self.set_file_modes(outfiles, 0o644, 0o755)
2622 self.set_dir_modes(self.install_dir, 0o755)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002623 return outfiles
2624
2625 def set_file_modes(self, files, defaultMode, sharedLibMode):
Michael W. Hudson529a5052002-12-17 16:47:17 +00002626 if not files: return
2627
2628 for filename in files:
2629 if os.path.islink(filename): continue
2630 mode = defaultMode
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002631 if filename.endswith(self.shlib_suffix): mode = sharedLibMode
Michael W. Hudson529a5052002-12-17 16:47:17 +00002632 log.info("changing mode of %s to %o", filename, mode)
2633 if not self.dry_run: os.chmod(filename, mode)
2634
2635 def set_dir_modes(self, dirname, mode):
Amaury Forgeot d'Arc321e5332009-07-02 23:08:45 +00002636 for dirpath, dirnames, fnames in os.walk(dirname):
2637 if os.path.islink(dirpath):
2638 continue
2639 log.info("changing mode of %s to %o", dirpath, mode)
2640 if not self.dry_run: os.chmod(dirpath, mode)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002641
Victor Stinnerc991f242019-03-01 17:19:04 +01002642
Georg Brandlff52f762010-12-28 09:51:43 +00002643class PyBuildScripts(build_scripts):
2644 def copy_scripts(self):
2645 outfiles, updated_files = build_scripts.copy_scripts(self)
2646 fullversion = '-{0[0]}.{0[1]}'.format(sys.version_info)
2647 minoronly = '.{0[1]}'.format(sys.version_info)
2648 newoutfiles = []
2649 newupdated_files = []
2650 for filename in outfiles:
Brett Cannona8c34242018-04-20 14:15:40 -07002651 if filename.endswith('2to3'):
Georg Brandlff52f762010-12-28 09:51:43 +00002652 newfilename = filename + fullversion
2653 else:
2654 newfilename = filename + minoronly
Vinay Sajipdd917f82016-08-31 08:22:29 +01002655 log.info('renaming %s to %s', filename, newfilename)
Georg Brandlff52f762010-12-28 09:51:43 +00002656 os.rename(filename, newfilename)
2657 newoutfiles.append(newfilename)
2658 if filename in updated_files:
2659 newupdated_files.append(newfilename)
2660 return newoutfiles, newupdated_files
2661
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002662
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002663def main():
Victor Stinnercad80202021-01-19 23:04:49 +01002664 global LIST_MODULE_NAMES
2665
2666 if "--list-module-names" in sys.argv:
2667 LIST_MODULE_NAMES = True
2668 sys.argv.remove("--list-module-names")
2669
Victor Stinnerc991f242019-03-01 17:19:04 +01002670 set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
2671 set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
2672
2673 class DummyProcess:
2674 """Hack for parallel build"""
2675 ProcessPoolExecutor = None
2676
2677 sys.modules['concurrent.futures.process'] = DummyProcess
Paul Ganssle62972d92020-05-16 04:20:06 -04002678 validate_tzpath()
Victor Stinnerc991f242019-03-01 17:19:04 +01002679
Andrew M. Kuchling62686692001-05-21 20:48:09 +00002680 # turn off warnings when deprecated modules are imported
2681 import warnings
2682 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002683 setup(# PyPI Metadata (PEP 301)
2684 name = "Python",
2685 version = sys.version.split()[0],
Serhiy Storchaka885bdc42016-02-11 13:10:36 +02002686 url = "http://www.python.org/%d.%d" % sys.version_info[:2],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002687 maintainer = "Guido van Rossum and the Python community",
2688 maintainer_email = "python-dev@python.org",
2689 description = "A high-level object-oriented programming language",
2690 long_description = SUMMARY.strip(),
2691 license = "PSF license",
Guido van Rossumc1f779c2007-07-03 08:25:58 +00002692 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002693 platforms = ["Many"],
2694
2695 # Build info
Georg Brandlff52f762010-12-28 09:51:43 +00002696 cmdclass = {'build_ext': PyBuildExt,
2697 'build_scripts': PyBuildScripts,
2698 'install': PyBuildInstall,
2699 'install_lib': PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002700 # The struct module is defined here, because build_ext won't be
2701 # called unless there's at least one extension module defined.
Victor Stinnercdad2722021-04-22 00:52:52 +02002702 ext_modules=[Extension('_struct', ['_struct.c'],
2703 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00002704
Georg Brandlff52f762010-12-28 09:51:43 +00002705 # If you change the scripts installed here, you also need to
2706 # check the PyBuildScripts command above, and change the links
2707 # created by the bininstall target in Makefile.pre.in
Benjamin Petersondfea1922009-05-23 17:13:14 +00002708 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
Brett Cannona8c34242018-04-20 14:15:40 -07002709 "Tools/scripts/2to3"]
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002710 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00002711
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002712# --install-platlib
2713if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002714 main()