blob: 9343d490c14330547805a314747af509b70180af [file] [log] [blame]
Andrew M. Kuchling66012fe2001-01-26 21:56:58 +00001# Autodetecting setup.py script for building the Python extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +00002
Victor Stinner625dbf22019-03-01 15:59:39 +01003import argparse
Eric Snow335e14d2014-01-04 15:09:28 -07004import importlib._bootstrap
Victor Stinner625dbf22019-03-01 15:59:39 +01005import importlib.machinery
Eric Snow335e14d2014-01-04 15:09:28 -07006import importlib.util
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -07007import logging
Victor Stinner625dbf22019-03-01 15:59:39 +01008import os
9import re
10import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +000011import sysconfig
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020012import warnings
Serhiy Storchaka93558682020-06-20 11:10:31 +030013from glob import glob, escape
Ronald Oussoren404a7192020-11-22 06:14:25 +010014import _osx_support
Michael W. Hudson529a5052002-12-17 16:47:17 +000015
Victor Stinner1ec63b62020-03-04 14:50:19 +010016
17try:
18 import subprocess
19 del subprocess
20 SUBPROCESS_BOOTSTRAP = False
21except ImportError:
Victor Stinner1ec63b62020-03-04 14:50:19 +010022 # Bootstrap Python: distutils.spawn uses subprocess to build C extensions,
23 # subprocess requires C extensions built by setup.py like _posixsubprocess.
24 #
Victor Stinneraddaaaa2020-03-09 23:45:59 +010025 # Use _bootsubprocess which only uses the os module.
Victor Stinner1ec63b62020-03-04 14:50:19 +010026 #
27 # It is dropped from sys.modules as soon as all C extension modules
28 # are built.
Victor Stinneraddaaaa2020-03-09 23:45:59 +010029 import _bootsubprocess
30 sys.modules['subprocess'] = _bootsubprocess
31 del _bootsubprocess
32 SUBPROCESS_BOOTSTRAP = True
Victor Stinner1ec63b62020-03-04 14:50:19 +010033
34
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020035with warnings.catch_warnings():
36 # bpo-41282 (PEP 632) deprecated distutils but setup.py still uses it
Christian Heimesa460ab32021-04-24 09:55:15 +020037 warnings.filterwarnings(
38 "ignore",
39 "The distutils package is deprecated",
40 DeprecationWarning
41 )
42 warnings.filterwarnings(
43 "ignore",
44 "The distutils.sysconfig module is deprecated, use sysconfig instead",
45 DeprecationWarning
46 )
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020047
Victor Stinnerd9ba9de2021-04-14 16:38:58 +020048 from distutils.command.build_ext import build_ext
49 from distutils.command.build_scripts import build_scripts
50 from distutils.command.install import install
51 from distutils.command.install_lib import install_lib
52 from distutils.core import Extension, setup
53 from distutils.errors import CCompilerError, DistutilsError
54 from distutils.spawn import find_executable
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000055
Antoine Pitrou2c0a9162014-09-26 23:31:59 +020056
Victor Stinnercfe172d2019-03-01 18:21:49 +010057# Compile extensions used to test Python?
pxinwr277ce302020-12-30 20:50:39 +080058TEST_EXTENSIONS = (sysconfig.get_config_var('TEST_MODULES') == 'yes')
Victor Stinnercfe172d2019-03-01 18:21:49 +010059
60# This global variable is used to hold the list of modules to be disabled.
61DISABLED_MODULE_LIST = []
62
Victor Stinnercad80202021-01-19 23:04:49 +010063# --list-module-names option used by Tools/scripts/generate_module_names.py
64LIST_MODULE_NAMES = False
65
Victor Stinnercfe172d2019-03-01 18:21:49 +010066
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -070067logging.basicConfig(format='%(message)s', level=logging.INFO)
68log = logging.getLogger('setup')
69
70
doko@ubuntu.com93df16b2012-06-30 14:32:08 +020071def get_platform():
Victor Stinnerc991f242019-03-01 17:19:04 +010072 # Cross compiling
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +020073 if "_PYTHON_HOST_PLATFORM" in os.environ:
74 return os.environ["_PYTHON_HOST_PLATFORM"]
Victor Stinnerc991f242019-03-01 17:19:04 +010075
doko@ubuntu.com93df16b2012-06-30 14:32:08 +020076 # Get value of sys.platform
77 if sys.platform.startswith('osf1'):
78 return 'osf1'
79 return sys.platform
Victor Stinnerc991f242019-03-01 17:19:04 +010080
81
82CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
Victor Stinner4cbea512019-02-28 17:48:38 +010083HOST_PLATFORM = get_platform()
84MS_WINDOWS = (HOST_PLATFORM == 'win32')
85CYGWIN = (HOST_PLATFORM == 'cygwin')
86MACOS = (HOST_PLATFORM == 'darwin')
Michael Felt08970cb2019-06-21 15:58:00 +020087AIX = (HOST_PLATFORM.startswith('aix'))
Victor Stinner4cbea512019-02-28 17:48:38 +010088VXWORKS = ('vxworks' in HOST_PLATFORM)
pxinwr32f5fdd2019-02-27 19:09:28 +080089
Victor Stinnerc991f242019-03-01 17:19:04 +010090
91SUMMARY = """
92Python is an interpreted, interactive, object-oriented programming
93language. It is often compared to Tcl, Perl, Scheme or Java.
94
95Python combines remarkable power with very clear syntax. It has
96modules, classes, exceptions, very high level dynamic data types, and
97dynamic typing. There are interfaces to many system calls and
98libraries, as well as to various windowing systems (X11, Motif, Tk,
99Mac, MFC). New built-in modules are easily written in C or C++. Python
100is also usable as an extension language for applications that need a
101programmable interface.
102
103The Python implementation is portable: it runs on many brands of UNIX,
104on Windows, DOS, Mac, Amiga... If your favorite system isn't
105listed here, it may still be supported, if there's a C compiler for
106it. Ask around on comp.lang.python -- or just try compiling Python
107yourself.
108"""
109
110CLASSIFIERS = """
111Development Status :: 6 - Mature
112License :: OSI Approved :: Python Software Foundation License
113Natural Language :: English
114Programming Language :: C
115Programming Language :: Python
116Topic :: Software Development
117"""
118
119
Victor Stinner6b982c22020-04-01 01:10:07 +0200120def run_command(cmd):
121 status = os.system(cmd)
Victor Stinner65a796e2020-04-01 18:49:29 +0200122 return os.waitstatus_to_exitcode(status)
Victor Stinner6b982c22020-04-01 01:10:07 +0200123
124
Victor Stinnerc991f242019-03-01 17:19:04 +0100125# Set common compiler and linker flags derived from the Makefile,
126# reserved for building the interpreter and the stdlib modules.
127# See bpo-21121 and bpo-35257
128def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
129 flags = sysconfig.get_config_var(compiler_flags)
130 py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
131 sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
132
133
Michael W. Hudson39230b32002-01-16 15:26:48 +0000134def add_dir_to_list(dirlist, dir):
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000135 """Add the directory 'dir' to the list 'dirlist' (after any relative
136 directories) if:
137
Michael W. Hudson39230b32002-01-16 15:26:48 +0000138 1) 'dir' is not already in 'dirlist'
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000139 2) 'dir' actually exists, and is a directory.
140 """
141 if dir is None or not os.path.isdir(dir) or dir in dirlist:
142 return
143 for i, path in enumerate(dirlist):
144 if not os.path.isabs(path):
145 dirlist.insert(i + 1, dir)
Barry Warsaw34520cd2010-11-27 20:03:03 +0000146 return
147 dirlist.insert(0, dir)
Michael W. Hudson39230b32002-01-16 15:26:48 +0000148
Victor Stinnerc991f242019-03-01 17:19:04 +0100149
xdegaye77f51392017-11-25 17:25:30 +0100150def sysroot_paths(make_vars, subdirs):
151 """Get the paths of sysroot sub-directories.
152
153 * make_vars: a sequence of names of variables of the Makefile where
154 sysroot may be set.
155 * subdirs: a sequence of names of subdirectories used as the location for
156 headers or libraries.
157 """
158
159 dirs = []
160 for var_name in make_vars:
161 var = sysconfig.get_config_var(var_name)
162 if var is not None:
163 m = re.search(r'--sysroot=([^"]\S*|"[^"]+")', var)
164 if m is not None:
165 sysroot = m.group(1).strip('"')
166 for subdir in subdirs:
167 if os.path.isabs(subdir):
168 subdir = subdir[1:]
169 path = os.path.join(sysroot, subdir)
170 if os.path.isdir(path):
171 dirs.append(path)
172 break
173 return dirs
174
Ned Deily1731d6d2020-05-18 04:32:38 -0400175
Ned Deily0288dd62019-06-03 06:34:48 -0400176MACOS_SDK_ROOT = None
Ned Deily1731d6d2020-05-18 04:32:38 -0400177MACOS_SDK_SPECIFIED = None
Victor Stinnerc991f242019-03-01 17:19:04 +0100178
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000179def macosx_sdk_root():
Ned Deily0288dd62019-06-03 06:34:48 -0400180 """Return the directory of the current macOS SDK.
181
182 If no SDK was explicitly configured, call the compiler to find which
183 include files paths are being searched by default. Use '/' if the
184 compiler is searching /usr/include (meaning system header files are
185 installed) or use the root of an SDK if that is being searched.
186 (The SDK may be supplied via Xcode or via the Command Line Tools).
187 The SDK paths used by Apple-supplied tool chains depend on the
188 setting of various variables; see the xcrun man page for more info.
Ned Deily1731d6d2020-05-18 04:32:38 -0400189 Also sets MACOS_SDK_SPECIFIED for use by macosx_sdk_specified().
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000190 """
Ned Deily1731d6d2020-05-18 04:32:38 -0400191 global MACOS_SDK_ROOT, MACOS_SDK_SPECIFIED
Ned Deily0288dd62019-06-03 06:34:48 -0400192
193 # If already called, return cached result.
194 if MACOS_SDK_ROOT:
195 return MACOS_SDK_ROOT
196
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000197 cflags = sysconfig.get_config_var('CFLAGS')
Joshua Rootb3107002020-04-22 17:44:10 +1000198 m = re.search(r'-isysroot\s*(\S+)', cflags)
Ned Deily0288dd62019-06-03 06:34:48 -0400199 if m is not None:
200 MACOS_SDK_ROOT = m.group(1)
Ned Deily29afab62020-12-04 23:02:09 -0500201 MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000202 else:
Ronald Oussoren404a7192020-11-22 06:14:25 +0100203 MACOS_SDK_ROOT = _osx_support._default_sysroot(
204 sysconfig.get_config_var('CC'))
Ned Deily29afab62020-12-04 23:02:09 -0500205 MACOS_SDK_SPECIFIED = False
Ned Deily0288dd62019-06-03 06:34:48 -0400206
207 return MACOS_SDK_ROOT
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000208
Victor Stinnerc991f242019-03-01 17:19:04 +0100209
Ned Deily1731d6d2020-05-18 04:32:38 -0400210def macosx_sdk_specified():
211 """Returns true if an SDK was explicitly configured.
212
213 True if an SDK was selected at configure time, either by specifying
214 --enable-universalsdk=(something other than no or /) or by adding a
215 -isysroot option to CFLAGS. In some cases, like when making
216 decisions about macOS Tk framework paths, we need to be able to
217 know whether the user explicitly asked to build with an SDK versus
218 the implicit use of an SDK when header files are no longer
219 installed on a running system by the Command Line Tools.
220 """
221 global MACOS_SDK_SPECIFIED
222
223 # If already called, return cached result.
224 if MACOS_SDK_SPECIFIED:
225 return MACOS_SDK_SPECIFIED
226
227 # Find the sdk root and set MACOS_SDK_SPECIFIED
228 macosx_sdk_root()
229 return MACOS_SDK_SPECIFIED
230
231
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000232def is_macosx_sdk_path(path):
233 """
Ned Batchelderd52bbde2021-05-02 19:58:57 -0700234 Returns True if 'path' can be located in a macOS SDK
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000235 """
Ned Deily2910a7b2012-07-30 02:35:58 -0700236 return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
Ned Batchelderd52bbde2021-05-02 19:58:57 -0700237 or path.startswith('/System/Library')
238 or path.startswith('/System/iOSSupport') )
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000239
Victor Stinnerc991f242019-03-01 17:19:04 +0100240
Ronald Oussoren41761932020-11-08 10:05:27 +0100241def grep_headers_for(function, headers):
242 for header in headers:
Ronald Oussoren7a27c7e2020-11-14 16:07:47 +0100243 with open(header, 'r', errors='surrogateescape') as f:
Ronald Oussoren41761932020-11-08 10:05:27 +0100244 if function in f.read():
245 return True
246 return False
247
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700248
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000249def find_file(filename, std_dirs, paths):
250 """Searches for the directory where a given file is located,
251 and returns a possibly-empty list of additional directories, or None
252 if the file couldn't be found at all.
Fredrik Lundhade711a2001-01-24 08:00:28 +0000253
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000254 'filename' is the name of a file, such as readline.h or libcrypto.a.
255 'std_dirs' is the list of standard system directories; if the
256 file is found in one of them, no additional directives are needed.
257 'paths' is a list of additional locations to check; if the file is
258 found in one of them, the resulting list will contain the directory.
259 """
Victor Stinner4cbea512019-02-28 17:48:38 +0100260 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000261 # Honor the MacOSX SDK setting when one was specified.
262 # An SDK is a directory with the same structure as a real
263 # system, but with only header files and libraries.
264 sysroot = macosx_sdk_root()
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000265
266 # Check the standard locations
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700267 for dir_ in std_dirs:
268 f = os.path.join(dir_, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000269
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700270 if MACOS and is_macosx_sdk_path(dir_):
271 f = os.path.join(sysroot, dir_[1:], filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000272
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000273 if os.path.exists(f): return []
274
275 # Check the additional directories
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700276 for dir_ in paths:
277 f = os.path.join(dir_, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000278
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700279 if MACOS and is_macosx_sdk_path(dir_):
280 f = os.path.join(sysroot, dir_[1:], filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000281
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000282 if os.path.exists(f):
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700283 return [dir_]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000284
285 # Not found anywhere
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000286 return None
287
Victor Stinnerc991f242019-03-01 17:19:04 +0100288
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000289def find_library_file(compiler, libname, std_dirs, paths):
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000290 result = compiler.find_library_file(std_dirs + paths, libname)
291 if result is None:
292 return None
Fredrik Lundhade711a2001-01-24 08:00:28 +0000293
Victor Stinner4cbea512019-02-28 17:48:38 +0100294 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000295 sysroot = macosx_sdk_root()
296
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000297 # Check whether the found file is in one of the standard directories
298 dirname = os.path.dirname(result)
299 for p in std_dirs:
300 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000301 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000302
Victor Stinner4cbea512019-02-28 17:48:38 +0100303 if MACOS and is_macosx_sdk_path(p):
Ned Deily020250f2016-02-25 00:56:38 +1100304 # Note that, as of Xcode 7, Apple SDKs may contain textual stub
305 # libraries with .tbd extensions rather than the normal .dylib
306 # shared libraries installed in /. The Apple compiler tool
307 # chain handles this transparently but it can cause problems
308 # for programs that are being built with an SDK and searching
309 # for specific libraries. Distutils find_library_file() now
310 # knows to also search for and return .tbd files. But callers
311 # of find_library_file need to keep in mind that the base filename
312 # of the returned SDK library file might have a different extension
313 # from that of the library file installed on the running system,
314 # for example:
315 # /Applications/Xcode.app/Contents/Developer/Platforms/
316 # MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/
317 # usr/lib/libedit.tbd
318 # vs
319 # /usr/lib/libedit.dylib
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000320 if os.path.join(sysroot, p[1:]) == dirname:
321 return [ ]
322
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000323 if p == dirname:
324 return [ ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000325
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000326 # Otherwise, it must have been in one of the additional directories,
327 # so we have to figure out which one.
328 for p in paths:
329 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000330 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000331
Victor Stinner4cbea512019-02-28 17:48:38 +0100332 if MACOS and is_macosx_sdk_path(p):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000333 if os.path.join(sysroot, p[1:]) == dirname:
334 return [ p ]
335
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000336 if p == dirname:
337 return [p]
338 else:
339 assert False, "Internal error: Path not found in std_dirs or paths"
Tim Peters2c60f7a2003-01-29 03:49:43 +0000340
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700341
Paul Ganssle62972d92020-05-16 04:20:06 -0400342def validate_tzpath():
343 base_tzpath = sysconfig.get_config_var('TZPATH')
344 if not base_tzpath:
345 return
346
347 tzpaths = base_tzpath.split(os.pathsep)
348 bad_paths = [tzpath for tzpath in tzpaths if not os.path.isabs(tzpath)]
349 if bad_paths:
350 raise ValueError('TZPATH must contain only absolute paths, '
351 + f'found:\n{tzpaths!r}\nwith invalid paths:\n'
352 + f'{bad_paths!r}')
Victor Stinnerc991f242019-03-01 17:19:04 +0100353
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700354
Jack Jansen144ebcc2001-08-05 22:31:19 +0000355def find_module_file(module, dirlist):
356 """Find a module in a set of possible folders. If it is not found
357 return the unadorned filename"""
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700358 dirs = find_file(module, [], dirlist)
359 if not dirs:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000360 return module
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -0700361 if len(dirs) > 1:
362 log.info(f"WARNING: multiple copies of {module} found")
363 return os.path.join(dirs[0], module)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000364
Victor Stinnerc991f242019-03-01 17:19:04 +0100365
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000366class PyBuildExt(build_ext):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000367
Guido van Rossumd8faa362007-04-27 19:54:29 +0000368 def __init__(self, dist):
369 build_ext.__init__(self, dist)
Victor Stinner625dbf22019-03-01 15:59:39 +0100370 self.srcdir = None
371 self.lib_dirs = None
372 self.inc_dirs = None
Victor Stinner5ec33a12019-03-01 16:43:28 +0100373 self.config_h_vars = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000374 self.failed = []
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400375 self.failed_on_import = []
Victor Stinner8058bda2019-03-01 15:31:45 +0100376 self.missing = []
Christian Heimes9b60e552020-05-15 23:54:53 +0200377 self.disabled_configure = []
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200378 if '-j' in os.environ.get('MAKEFLAGS', ''):
379 self.parallel = True
Guido van Rossumd8faa362007-04-27 19:54:29 +0000380
Victor Stinner8058bda2019-03-01 15:31:45 +0100381 def add(self, ext):
382 self.extensions.append(ext)
383
Victor Stinner00c77ae2020-03-04 18:44:49 +0100384 def set_srcdir(self):
Victor Stinner625dbf22019-03-01 15:59:39 +0100385 self.srcdir = sysconfig.get_config_var('srcdir')
386 if not self.srcdir:
387 # Maybe running on Windows but not using CYGWIN?
388 raise ValueError("No source directory; cannot proceed.")
389 self.srcdir = os.path.abspath(self.srcdir)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000390
Victor Stinner00c77ae2020-03-04 18:44:49 +0100391 def remove_disabled(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000392 # Remove modules that are present on the disabled list
Christian Heimes679db4a2008-01-18 09:56:22 +0000393 extensions = [ext for ext in self.extensions
Victor Stinner4cbea512019-02-28 17:48:38 +0100394 if ext.name not in DISABLED_MODULE_LIST]
Christian Heimes679db4a2008-01-18 09:56:22 +0000395 # move ctypes to the end, it depends on other modules
396 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
397 if "_ctypes" in ext_map:
398 ctypes = extensions.pop(ext_map["_ctypes"])
399 extensions.append(ctypes)
400 self.extensions = extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +0000401
Victor Stinner00c77ae2020-03-04 18:44:49 +0100402 def update_sources_depends(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000403 # Fix up the autodetected modules, prefixing all the source files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000404 # with Modules/.
Victor Stinner625dbf22019-03-01 15:59:39 +0100405 moddirlist = [os.path.join(self.srcdir, 'Modules')]
Michael W. Hudson5b109102002-01-23 15:04:41 +0000406
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000407 # Fix up the paths for scripts, too
Victor Stinner625dbf22019-03-01 15:59:39 +0100408 self.distribution.scripts = [os.path.join(self.srcdir, filename)
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000409 for filename in self.distribution.scripts]
410
Christian Heimesaf98da12008-01-27 15:18:18 +0000411 # Python header files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000412 headers = [sysconfig.get_config_h_filename()]
Serhiy Storchaka93558682020-06-20 11:10:31 +0300413 headers += glob(os.path.join(escape(sysconfig.get_path('include')), "*.h"))
Christian Heimesaf98da12008-01-27 15:18:18 +0000414
Xavier de Gaye84968b72016-10-29 16:57:20 +0200415 for ext in self.extensions:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000416 ext.sources = [ find_module_file(filename, moddirlist)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000417 for filename in ext.sources ]
Jeremy Hylton340043e2002-06-13 17:38:11 +0000418 if ext.depends is not None:
Neil Schemenauer014bf282009-02-05 16:35:45 +0000419 ext.depends = [find_module_file(filename, moddirlist)
Jeremy Hylton340043e2002-06-13 17:38:11 +0000420 for filename in ext.depends]
Christian Heimesaf98da12008-01-27 15:18:18 +0000421 else:
422 ext.depends = []
423 # re-compile extensions if a header file has been changed
424 ext.depends.extend(headers)
425
Victor Stinner00c77ae2020-03-04 18:44:49 +0100426 def remove_configured_extensions(self):
427 # The sysconfig variables built by makesetup that list the already
428 # built modules and the disabled modules as configured by the Setup
429 # files.
430 sysconf_built = sysconfig.get_config_var('MODBUILT_NAMES').split()
431 sysconf_dis = sysconfig.get_config_var('MODDISABLED_NAMES').split()
432
433 mods_built = []
434 mods_disabled = []
435 for ext in self.extensions:
xdegayec0364fc2017-05-27 18:25:03 +0200436 # If a module has already been built or has been disabled in the
437 # Setup files, don't build it here.
438 if ext.name in sysconf_built:
439 mods_built.append(ext)
440 if ext.name in sysconf_dis:
441 mods_disabled.append(ext)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000442
xdegayec0364fc2017-05-27 18:25:03 +0200443 mods_configured = mods_built + mods_disabled
444 if mods_configured:
Xavier de Gaye84968b72016-10-29 16:57:20 +0200445 self.extensions = [x for x in self.extensions if x not in
xdegayec0364fc2017-05-27 18:25:03 +0200446 mods_configured]
447 # Remove the shared libraries built by a previous build.
448 for ext in mods_configured:
449 fullpath = self.get_ext_fullpath(ext.name)
450 if os.path.exists(fullpath):
451 os.unlink(fullpath)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000452
Victor Stinner00c77ae2020-03-04 18:44:49 +0100453 return (mods_built, mods_disabled)
454
455 def set_compiler_executables(self):
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000456 # When you run "make CC=altcc" or something similar, you really want
457 # those environment variables passed into the setup.py phase. Here's
458 # a small set of useful ones.
459 compiler = os.environ.get('CC')
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000460 args = {}
461 # unfortunately, distutils doesn't let us provide separate C and C++
462 # compilers
463 if compiler is not None:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000464 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
465 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
Tarek Ziadé36797272010-07-22 12:50:05 +0000466 self.compiler.set_executables(**args)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000467
Victor Stinner00c77ae2020-03-04 18:44:49 +0100468 def build_extensions(self):
469 self.set_srcdir()
470
471 # Detect which modules should be compiled
472 self.detect_modules()
473
Victor Stinnercad80202021-01-19 23:04:49 +0100474 if not LIST_MODULE_NAMES:
475 self.remove_disabled()
Victor Stinner00c77ae2020-03-04 18:44:49 +0100476
477 self.update_sources_depends()
478 mods_built, mods_disabled = self.remove_configured_extensions()
479 self.set_compiler_executables()
480
Victor Stinnercad80202021-01-19 23:04:49 +0100481 if LIST_MODULE_NAMES:
482 for ext in self.extensions:
483 print(ext.name)
484 for name in self.missing:
485 print(name)
486 return
487
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000488 build_ext.build_extensions(self)
489
Victor Stinner1ec63b62020-03-04 14:50:19 +0100490 if SUBPROCESS_BOOTSTRAP:
491 # Drop our custom subprocess module:
492 # use the newly built subprocess module
493 del sys.modules['subprocess']
494
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200495 for ext in self.extensions:
496 self.check_extension_import(ext)
497
Victor Stinner00c77ae2020-03-04 18:44:49 +0100498 self.summary(mods_built, mods_disabled)
499
500 def summary(self, mods_built, mods_disabled):
Berker Peksag1d82a9c2014-10-01 05:11:13 +0300501 longest = max([len(e.name) for e in self.extensions], default=0)
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400502 if self.failed or self.failed_on_import:
503 all_failed = self.failed + self.failed_on_import
504 longest = max(longest, max([len(name) for name in all_failed]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000505
506 def print_three_column(lst):
507 lst.sort(key=str.lower)
508 # guarantee zip() doesn't drop anything
509 while len(lst) % 3:
510 lst.append("")
511 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
512 print("%-*s %-*s %-*s" % (longest, e, longest, f,
513 longest, g))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000514
Victor Stinner8058bda2019-03-01 15:31:45 +0100515 if self.missing:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000516 print()
Brett Cannonae95b4f2013-07-12 11:30:32 -0400517 print("Python build finished successfully!")
518 print("The necessary bits to build these optional modules were not "
519 "found:")
Victor Stinner8058bda2019-03-01 15:31:45 +0100520 print_three_column(self.missing)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000521 print("To find the necessary bits, look in setup.py in"
522 " detect_modules() for the module's name.")
523 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524
xdegayec0364fc2017-05-27 18:25:03 +0200525 if mods_built:
526 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200527 print("The following modules found by detect_modules() in"
528 " setup.py, have been")
529 print("built by the Makefile instead, as configured by the"
530 " Setup files:")
xdegayec0364fc2017-05-27 18:25:03 +0200531 print_three_column([ext.name for ext in mods_built])
532 print()
533
534 if mods_disabled:
535 print()
536 print("The following modules found by detect_modules() in"
537 " setup.py have not")
538 print("been built, they are *disabled* in the Setup files:")
539 print_three_column([ext.name for ext in mods_disabled])
540 print()
Xavier de Gaye84968b72016-10-29 16:57:20 +0200541
Christian Heimes9b60e552020-05-15 23:54:53 +0200542 if self.disabled_configure:
543 print()
544 print("The following modules found by detect_modules() in"
545 " setup.py have not")
546 print("been built, they are *disabled* by configure:")
547 print_three_column(self.disabled_configure)
548 print()
549
Guido van Rossumd8faa362007-04-27 19:54:29 +0000550 if self.failed:
551 failed = self.failed[:]
552 print()
553 print("Failed to build these modules:")
554 print_three_column(failed)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000555 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000556
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400557 if self.failed_on_import:
558 failed = self.failed_on_import[:]
559 print()
560 print("Following modules built successfully"
561 " but were removed because they could not be imported:")
562 print_three_column(failed)
563 print()
564
Christian Heimes61d478c2018-01-27 15:51:38 +0100565 if any('_ssl' in l
Victor Stinner8058bda2019-03-01 15:31:45 +0100566 for l in (self.missing, self.failed, self.failed_on_import)):
Christian Heimes61d478c2018-01-27 15:51:38 +0100567 print()
568 print("Could not build the ssl module!")
Christian Heimes39258d32021-04-17 11:36:35 +0200569 print("Python requires a OpenSSL 1.1.1 or newer")
Christian Heimes32eba612021-03-19 10:29:25 +0100570 if sysconfig.get_config_var("OPENSSL_LDFLAGS"):
571 print("Custom linker flags may require --with-openssl-rpath=auto")
Christian Heimes61d478c2018-01-27 15:51:38 +0100572 print()
573
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000574 def build_extension(self, ext):
575
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576 if ext.name == '_ctypes':
577 if not self.configure_ctypes(ext):
Zachary Waref40d4dd2016-09-17 01:25:24 -0500578 self.failed.append(ext.name)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 return
580
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000581 try:
582 build_ext.build_extension(self, ext)
Guido van Rossumb940e112007-01-10 16:19:56 +0000583 except (CCompilerError, DistutilsError) as why:
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000584 self.announce('WARNING: building of extension "%s" failed: %s' %
Victor Stinner625dbf22019-03-01 15:59:39 +0100585 (ext.name, why))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000586 self.failed.append(ext.name)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000587 return
Antoine Pitrou2c0a9162014-09-26 23:31:59 +0200588
589 def check_extension_import(self, ext):
590 # Don't try to import an extension that has failed to compile
591 if ext.name in self.failed:
592 self.announce(
593 'WARNING: skipping import check for failed build "%s"' %
594 ext.name, level=1)
595 return
596
Jack Jansenf49c6f92001-11-01 14:44:15 +0000597 # Workaround for Mac OS X: The Carbon-based modules cannot be
598 # reliably imported into a command-line Python
599 if 'Carbon' in ext.extra_link_args:
Michael W. Hudson5b109102002-01-23 15:04:41 +0000600 self.announce(
601 'WARNING: skipping import check for Carbon-based "%s"' %
602 ext.name)
603 return
Georg Brandlfcaf9102008-07-16 02:17:56 +0000604
Victor Stinner4cbea512019-02-28 17:48:38 +0100605 if MACOS and (
Benjamin Petersonfc576352008-07-16 02:39:02 +0000606 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
Georg Brandlfcaf9102008-07-16 02:17:56 +0000607 # Don't bother doing an import check when an extension was
608 # build with an explicit '-arch' flag on OSX. That's currently
609 # only used to build 32-bit only extensions in a 4-way
610 # universal build and loading 32-bit code into a 64-bit
611 # process will fail.
612 self.announce(
613 'WARNING: skipping import check for "%s"' %
614 ext.name)
615 return
616
Jason Tishler24cf7762002-05-22 16:46:15 +0000617 # Workaround for Cygwin: Cygwin currently has fork issues when many
618 # modules have been imported
Victor Stinner4cbea512019-02-28 17:48:38 +0100619 if CYGWIN:
Jason Tishler24cf7762002-05-22 16:46:15 +0000620 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
621 % ext.name)
622 return
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000623 ext_filename = os.path.join(
624 self.build_lib,
625 self.get_ext_filename(self.get_ext_fullname(ext.name)))
Guido van Rossumc3fee692008-07-17 16:23:53 +0000626
627 # If the build directory didn't exist when setup.py was
628 # started, sys.path_importer_cache has a negative result
629 # cached. Clear that cache before trying to import.
630 sys.path_importer_cache.clear()
631
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200632 # Don't try to load extensions for cross builds
Victor Stinner4cbea512019-02-28 17:48:38 +0100633 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200634 return
635
Brett Cannonca5ff3a2013-06-15 17:52:59 -0400636 loader = importlib.machinery.ExtensionFileLoader(ext.name, ext_filename)
Eric Snow335e14d2014-01-04 15:09:28 -0700637 spec = importlib.util.spec_from_file_location(ext.name, ext_filename,
638 loader=loader)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000639 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400640 importlib._bootstrap._load(spec)
Guido van Rossumb940e112007-01-10 16:19:56 +0000641 except ImportError as why:
Benjamin Peterson5c2ac8c2014-04-30 11:06:16 -0400642 self.failed_on_import.append(ext.name)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000643 self.announce('*** WARNING: renaming "%s" since importing it'
644 ' failed: %s' % (ext.name, why), level=3)
645 assert not self.inplace
646 basename, tail = os.path.splitext(ext_filename)
647 newname = basename + "_failed" + tail
648 if os.path.exists(newname):
649 os.remove(newname)
650 os.rename(ext_filename, newname)
651
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000652 except:
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000653 exc_type, why, tb = sys.exc_info()
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000654 self.announce('*** WARNING: importing extension "%s" '
655 'failed with %s: %s' % (ext.name, exc_type, why),
656 level=3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000657 self.failed.append(ext.name)
Fred Drake9028d0a2001-12-06 22:59:54 +0000658
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400659 def add_multiarch_paths(self):
660 # Debian/Ubuntu multiarch support.
661 # https://wiki.ubuntu.com/MultiarchSpec
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200662 cc = sysconfig.get_config_var('CC')
663 tmpfile = os.path.join(self.build_temp, 'multiarch')
664 if not os.path.exists(self.build_temp):
665 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200666 ret = run_command(
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200667 '%s -print-multiarch > %s 2> /dev/null' % (cc, tmpfile))
668 multiarch_path_component = ''
669 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200670 if ret == 0:
doko@ubuntu.com3277b352012-08-08 12:15:55 +0200671 with open(tmpfile) as fp:
672 multiarch_path_component = fp.readline().strip()
673 finally:
674 os.unlink(tmpfile)
675
676 if multiarch_path_component != '':
677 add_dir_to_list(self.compiler.library_dirs,
678 '/usr/lib/' + multiarch_path_component)
679 add_dir_to_list(self.compiler.include_dirs,
680 '/usr/include/' + multiarch_path_component)
681 return
682
Barry Warsaw88e19452011-04-07 10:40:36 -0400683 if not find_executable('dpkg-architecture'):
684 return
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200685 opt = ''
Victor Stinner4cbea512019-02-28 17:48:38 +0100686 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200687 opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400688 tmpfile = os.path.join(self.build_temp, 'multiarch')
689 if not os.path.exists(self.build_temp):
690 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200691 ret = run_command(
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200692 'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
693 (opt, tmpfile))
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400694 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200695 if ret == 0:
Barry Warsaw5ca305a2011-04-06 15:18:12 -0400696 with open(tmpfile) as fp:
697 multiarch_path_component = fp.readline().strip()
698 add_dir_to_list(self.compiler.library_dirs,
699 '/usr/lib/' + multiarch_path_component)
700 add_dir_to_list(self.compiler.include_dirs,
701 '/usr/include/' + multiarch_path_component)
702 finally:
703 os.unlink(tmpfile)
704
pxinwr5e45f1c2021-01-22 08:55:52 +0800705 def add_wrcc_search_dirs(self):
706 # add library search path by wr-cc, the compiler wrapper
707
708 def convert_mixed_path(path):
709 # convert path like C:\folder1\folder2/folder3/folder4
710 # to msys style /c/folder1/folder2/folder3/folder4
711 drive = path[0].lower()
712 left = path[2:].replace("\\", "/")
713 return "/" + drive + left
714
715 def add_search_path(line):
716 # On Windows building machine, VxWorks does
717 # cross builds under msys2 environment.
718 pathsep = (";" if sys.platform == "msys" else ":")
719 for d in line.strip().split("=")[1].split(pathsep):
720 d = d.strip()
721 if sys.platform == "msys":
722 # On Windows building machine, compiler
723 # returns mixed style path like:
724 # C:\folder1\folder2/folder3/folder4
725 d = convert_mixed_path(d)
726 d = os.path.normpath(d)
727 add_dir_to_list(self.compiler.library_dirs, d)
728
729 cc = sysconfig.get_config_var('CC')
730 tmpfile = os.path.join(self.build_temp, 'wrccpaths')
731 os.makedirs(self.build_temp, exist_ok=True)
732 try:
733 ret = run_command('%s --print-search-dirs >%s' % (cc, tmpfile))
734 if ret:
735 return
736 with open(tmpfile) as fp:
737 # Parse paths in libraries line. The line is like:
738 # On Linux, "libraries: = path1:path2:path3"
739 # On Windows, "libraries: = path1;path2;path3"
740 for line in fp:
741 if not line.startswith("libraries"):
742 continue
743 add_search_path(line)
744 finally:
745 try:
746 os.unlink(tmpfile)
747 except OSError:
748 pass
749
pxinwr32f5fdd2019-02-27 19:09:28 +0800750 def add_cross_compiling_paths(self):
751 cc = sysconfig.get_config_var('CC')
752 tmpfile = os.path.join(self.build_temp, 'ccpaths')
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200753 if not os.path.exists(self.build_temp):
754 os.makedirs(self.build_temp)
Victor Stinner6b982c22020-04-01 01:10:07 +0200755 ret = run_command('%s -E -v - </dev/null 2>%s 1>/dev/null' % (cc, tmpfile))
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200756 is_gcc = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800757 is_clang = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200758 in_incdirs = False
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200759 try:
Victor Stinner6b982c22020-04-01 01:10:07 +0200760 if ret == 0:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200761 with open(tmpfile) as fp:
762 for line in fp.readlines():
763 if line.startswith("gcc version"):
764 is_gcc = True
pxinwr32f5fdd2019-02-27 19:09:28 +0800765 elif line.startswith("clang version"):
766 is_clang = True
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200767 elif line.startswith("#include <...>"):
768 in_incdirs = True
769 elif line.startswith("End of search list"):
770 in_incdirs = False
pxinwr32f5fdd2019-02-27 19:09:28 +0800771 elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"):
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +0200772 for d in line.strip().split("=")[1].split(":"):
773 d = os.path.normpath(d)
774 if '/gcc/' not in d:
775 add_dir_to_list(self.compiler.library_dirs,
776 d)
pxinwr32f5fdd2019-02-27 19:09:28 +0800777 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 +0200778 add_dir_to_list(self.compiler.include_dirs,
779 line.strip())
780 finally:
781 os.unlink(tmpfile)
782
pxinwr5e45f1c2021-01-22 08:55:52 +0800783 if VXWORKS:
784 self.add_wrcc_search_dirs()
785
Victor Stinnercfe172d2019-03-01 18:21:49 +0100786 def add_ldflags_cppflags(self):
Brett Cannon516592f2004-12-07 00:42:59 +0000787 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000788 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000789 # We must get the values from the Makefile and not the environment
790 # directly since an inconsistently reproducible issue comes up where
791 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000792 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000793 for env_var, arg_name, dir_list in (
Tarek Ziadé36797272010-07-22 12:50:05 +0000794 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
795 ('LDFLAGS', '-L', self.compiler.library_dirs),
796 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000797 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000798 if env_val:
Chih-Hsuan Yen09b2bec2018-07-11 16:48:43 +0800799 parser = argparse.ArgumentParser()
800 parser.add_argument(arg_name, dest="dirs", action="append")
801 options, _ = parser.parse_known_args(env_val.split())
Brett Cannon44837712005-01-02 21:54:07 +0000802 if options.dirs:
Christian Heimes292d3512008-02-03 16:51:08 +0000803 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000804 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000805
Victor Stinnercfe172d2019-03-01 18:21:49 +0100806 def configure_compiler(self):
807 # Ensure that /usr/local is always used, but the local build
808 # directories (i.e. '.' and 'Include') must be first. See issue
809 # 10520.
810 if not CROSS_COMPILING:
811 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
812 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
813 # only change this for cross builds for 3.3, issues on Mageia
814 if CROSS_COMPILING:
815 self.add_cross_compiling_paths()
816 self.add_multiarch_paths()
817 self.add_ldflags_cppflags()
818
Victor Stinner5ec33a12019-03-01 16:43:28 +0100819 def init_inc_lib_dirs(self):
Victor Stinner4cbea512019-02-28 17:48:38 +0100820 if (not CROSS_COMPILING and
Xavier de Gaye1351c312016-12-14 11:14:33 +0100821 os.path.normpath(sys.base_prefix) != '/usr' and
822 not sysconfig.get_config_var('PYTHONFRAMEWORK')):
Ronald Oussorenf3500e12010-10-20 13:10:12 +0000823 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
824 # (PYTHONFRAMEWORK is set) to avoid # linking problems when
825 # building a framework with different architectures than
826 # the one that is currently installed (issue #7473)
Tarek Ziadé36797272010-07-22 12:50:05 +0000827 add_dir_to_list(self.compiler.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000828 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadé36797272010-07-22 12:50:05 +0000829 add_dir_to_list(self.compiler.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000830 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000831
xdegaye77f51392017-11-25 17:25:30 +0100832 system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
833 system_include_dirs = ['/usr/include']
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000834 # lib_dirs and inc_dirs are used to search for files;
835 # if a file is found in one of those directories, it can
836 # be assumed that no additional -I,-L directives are needed.
Victor Stinner4cbea512019-02-28 17:48:38 +0100837 if not CROSS_COMPILING:
Victor Stinner625dbf22019-03-01 15:59:39 +0100838 self.lib_dirs = self.compiler.library_dirs + system_lib_dirs
839 self.inc_dirs = self.compiler.include_dirs + system_include_dirs
Christian Heimesf19529c2012-12-12 12:41:00 +0100840 else:
xdegaye77f51392017-11-25 17:25:30 +0100841 # Add the sysroot paths. 'sysroot' is a compiler option used to
842 # set the logical path of the standard system headers and
843 # libraries.
Victor Stinner625dbf22019-03-01 15:59:39 +0100844 self.lib_dirs = (self.compiler.library_dirs +
845 sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
846 self.inc_dirs = (self.compiler.include_dirs +
847 sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
848 system_include_dirs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000849
Brett Cannon4454a1f2005-04-15 20:32:39 +0000850 config_h = sysconfig.get_config_h_filename()
Brett Cannon9f5db072010-10-29 20:19:27 +0000851 with open(config_h) as file:
Victor Stinner5ec33a12019-03-01 16:43:28 +0100852 self.config_h_vars = sysconfig.parse_config_h(file)
Brett Cannon4454a1f2005-04-15 20:32:39 +0000853
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000854 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
Victor Stinner4cbea512019-02-28 17:48:38 +0100855 if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']:
Victor Stinner625dbf22019-03-01 15:59:39 +0100856 self.lib_dirs += ['/usr/ccs/lib']
Skip Montanaro22e00c42003-05-06 20:43:34 +0000857
Charles-François Natali5739e102012-04-12 19:07:25 +0200858 # HP-UX11iv3 keeps files in lib/hpux folders.
Victor Stinner4cbea512019-02-28 17:48:38 +0100859 if HOST_PLATFORM == 'hp-ux11':
Victor Stinner625dbf22019-03-01 15:59:39 +0100860 self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
Charles-François Natali5739e102012-04-12 19:07:25 +0200861
Victor Stinner4cbea512019-02-28 17:48:38 +0100862 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 # This should work on any unixy platform ;-)
864 # If the user has bothered specifying additional -I and -L flags
865 # in OPT and LDFLAGS we might as well use them here.
Barry Warsaw807bd0a2010-11-24 20:30:00 +0000866 #
867 # NOTE: using shlex.split would technically be more correct, but
868 # also gives a bootstrap problem. Let's hope nobody uses
869 # directories with whitespace in the name to store libraries.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870 cflags, ldflags = sysconfig.get_config_vars(
871 'CFLAGS', 'LDFLAGS')
872 for item in cflags.split():
873 if item.startswith('-I'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100874 self.inc_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875
876 for item in ldflags.split():
877 if item.startswith('-L'):
Victor Stinner625dbf22019-03-01 15:59:39 +0100878 self.lib_dirs.append(item[2:])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879
Victor Stinner5ec33a12019-03-01 16:43:28 +0100880 def detect_simple_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000881 #
882 # The following modules are all pretty straightforward, and compile
883 # on pretty much any POSIXish platform.
884 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000885
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000886 # array objects
Victor Stinnercdad2722021-04-22 00:52:52 +0200887 self.add(Extension('array', ['arraymodule.c'],
888 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Martin Panterc9deece2016-02-03 05:19:44 +0000889
Yury Selivanovf23746a2018-01-22 19:11:18 -0500890 # Context Variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100891 self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
Yury Selivanovf23746a2018-01-22 19:11:18 -0500892
Martin Panterc9deece2016-02-03 05:19:44 +0000893 shared_math = 'Modules/_math.o'
Victor Stinnercfe172d2019-03-01 18:21:49 +0100894
895 # math library functions, e.g. sin()
896 self.add(Extension('math', ['mathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100897 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100898 extra_objects=[shared_math],
899 depends=['_math.h', shared_math],
900 libraries=['m']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100901
902 # complex math library functions
903 self.add(Extension('cmath', ['cmathmodule.c'],
Victor Stinnere9e7d282020-02-12 22:54:42 +0100904 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner8058bda2019-03-01 15:31:45 +0100905 extra_objects=[shared_math],
906 depends=['_math.h', shared_math],
907 libraries=['m']))
Victor Stinnere0be4232011-10-25 13:06:09 +0200908
909 # time libraries: librt may be needed for clock_gettime()
910 time_libs = []
911 lib = sysconfig.get_config_var('TIMEMODULE_LIB')
912 if lib:
913 time_libs.append(lib)
914
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000915 # time operations and variables
Victor Stinner8058bda2019-03-01 15:31:45 +0100916 self.add(Extension('time', ['timemodule.c'],
917 libraries=time_libs))
Benjamin Peterson8acaa312017-11-12 20:53:39 -0800918 # libm is needed by delta_new() that uses round() and by accum() that
919 # uses modf().
Victor Stinner8058bda2019-03-01 15:31:45 +0100920 self.add(Extension('_datetime', ['_datetimemodule.c'],
Victor Stinner04fc4f22020-06-16 01:28:07 +0200921 libraries=['m'],
922 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Paul Ganssle62972d92020-05-16 04:20:06 -0400923 # zoneinfo module
Victor Stinner37834132020-10-27 17:12:53 +0100924 self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
925 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000926 # random number generator implemented in C
Victor Stinner9f5fe792020-04-17 19:05:35 +0200927 self.add(Extension("_random", ["_randommodule.c"],
928 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Raymond Hettinger0c410272004-01-05 10:13:35 +0000929 # bisect
Victor Stinner8058bda2019-03-01 15:31:45 +0100930 self.add(Extension("_bisect", ["_bisectmodule.c"]))
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000931 # heapq
Victor Stinnerc45dbe932020-06-22 17:39:32 +0200932 self.add(Extension("_heapq", ["_heapqmodule.c"],
933 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000934 # C-optimized pickle replacement
Victor Stinner5c75f372019-04-17 23:02:26 +0200935 self.add(Extension("_pickle", ["_pickle.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200936 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Christian Heimes90540002008-05-08 14:29:10 +0000937 # _json speedups
Victor Stinner8058bda2019-03-01 15:31:45 +0100938 self.add(Extension("_json", ["_json.c"],
Victor Stinner57491342019-04-23 12:26:33 +0200939 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinnercfe172d2019-03-01 18:21:49 +0100940
Fred Drake0e474a82007-10-11 18:01:43 +0000941 # profiler (_lsprof is for cProfile.py)
Victor Stinner8058bda2019-03-01 15:31:45 +0100942 self.add(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000943 # static Unicode character database
Victor Stinner8058bda2019-03-01 15:31:45 +0100944 self.add(Extension('unicodedata', ['unicodedata.c'],
Victor Stinner47e1afd2020-10-26 16:43:47 +0100945 depends=['unicodedata_db.h', 'unicodename_db.h'],
946 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Larry Hastings3a907972013-11-23 14:49:22 -0800947 # _opcode module
Victor Stinner8058bda2019-03-01 15:31:45 +0100948 self.add(Extension('_opcode', ['_opcode.c']))
INADA Naoki9f2ce252016-10-15 15:39:19 +0900949 # asyncio speedups
Chris Jerdonekda742ba2020-05-17 22:47:31 -0700950 self.add(Extension("_asyncio", ["_asynciomodule.c"],
951 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000952 # _abc speedups
Victor Stinnercdad2722021-04-22 00:52:52 +0200953 self.add(Extension("_abc", ["_abc.c"],
954 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Antoine Pitrou94e16962018-01-16 00:27:16 +0100955 # _queue module
Victor Stinnercdad2722021-04-22 00:52:52 +0200956 self.add(Extension("_queue", ["_queuemodule.c"],
957 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Dong-hee Na0a18ee42019-08-24 07:20:30 +0900958 # _statistics module
959 self.add(Extension("_statistics", ["_statisticsmodule.c"]))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000960
961 # Modules with some UNIX dependencies -- on by default:
962 # (If you have a really backward UNIX, select and socket may not be
963 # supported...)
964
965 # fcntl(2) and ioctl(2)
Antoine Pitroua3000072010-09-07 14:52:42 +0000966 libs = []
Victor Stinner5ec33a12019-03-01 16:43:28 +0100967 if (self.config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
Antoine Pitroua3000072010-09-07 14:52:42 +0000968 # May be necessary on AIX for flock function
969 libs = ['bsd']
Victor Stinner8058bda2019-03-01 15:31:45 +0100970 self.add(Extension('fcntl', ['fcntlmodule.c'],
971 libraries=libs))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000972 # pwd(3)
Victor Stinner8058bda2019-03-01 15:31:45 +0100973 self.add(Extension('pwd', ['pwdmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000974 # grp(3)
pxinwr32f5fdd2019-02-27 19:09:28 +0800975 if not VXWORKS:
Victor Stinner8058bda2019-03-01 15:31:45 +0100976 self.add(Extension('grp', ['grpmodule.c']))
Ronald Oussoren94f25282010-05-05 19:11:21 +0000977 # spwd, shadow passwords
Victor Stinner5ec33a12019-03-01 16:43:28 +0100978 if (self.config_h_vars.get('HAVE_GETSPNAM', False) or
979 self.config_h_vars.get('HAVE_GETSPENT', False)):
Victor Stinner8058bda2019-03-01 15:31:45 +0100980 self.add(Extension('spwd', ['spwdmodule.c']))
Michael Felt08970cb2019-06-21 15:58:00 +0200981 # AIX has shadow passwords, but access is not via getspent(), etc.
982 # module support is not expected so it not 'missing'
983 elif not AIX:
Victor Stinner8058bda2019-03-01 15:31:45 +0100984 self.missing.append('spwd')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000986 # select(2); not on ancient System V
Victor Stinner8058bda2019-03-01 15:31:45 +0100987 self.add(Extension('select', ['selectmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000988
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000989 # Memory-mapped files (also works on Win32).
Victor Stinner8058bda2019-03-01 15:31:45 +0100990 self.add(Extension('mmap', ['mmapmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000991
Andrew M. Kuchling57269d02004-08-31 13:37:25 +0000992 # Lance Ellinghaus's syslog module
Ronald Oussoren94f25282010-05-05 19:11:21 +0000993 # syslog daemon interface
Victor Stinner8058bda2019-03-01 15:31:45 +0100994 self.add(Extension('syslog', ['syslogmodule.c']))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000995
Eric Snow7f8bfc92018-01-29 18:23:44 -0700996 # Python interface to subinterpreter C-API.
Eric Snowc11183c2019-03-15 16:35:46 -0600997 self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
Eric Snow7f8bfc92018-01-29 18:23:44 -0700998
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000999 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +00001000 # Here ends the simple stuff. From here on, modules need certain
1001 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001002 #
1003
1004 # Multimedia modules
1005 # These don't work for 64-bit platforms!!!
1006 # These represent audio samples or images as strings:
Victor Stinnerdef80722016-04-19 15:58:11 +02001007 #
Neal Norwitz5e4a3b82004-07-19 16:55:07 +00001008 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +00001009 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001010 # 64-bit platforms.
Victor Stinnerdef80722016-04-19 15:58:11 +02001011 #
Benjamin Peterson8acaa312017-11-12 20:53:39 -08001012 # audioop needs libm for floor() in multiple functions.
Victor Stinner8058bda2019-03-01 15:31:45 +01001013 self.add(Extension('audioop', ['audioop.c'],
1014 libraries=['m']))
Martin v. Löwis8fbefe22004-07-19 16:42:20 +00001015
Victor Stinner5ec33a12019-03-01 16:43:28 +01001016 # CSV files
1017 self.add(Extension('_csv', ['_csv.c']))
1018
1019 # POSIX subprocess module helper.
Kyle Evans79925792020-10-13 15:04:44 -05001020 self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'],
1021 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner5ec33a12019-03-01 16:43:28 +01001022
Victor Stinnercfe172d2019-03-01 18:21:49 +01001023 def detect_test_extensions(self):
1024 # Python C API test module
1025 self.add(Extension('_testcapi', ['_testcapimodule.c'],
1026 depends=['testcapi_long.h']))
1027
Victor Stinner23bace22019-04-18 11:37:26 +02001028 # Python Internal C API test module
1029 self.add(Extension('_testinternalcapi', ['_testinternalcapi.c'],
Victor Stinner57491342019-04-23 12:26:33 +02001030 extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Victor Stinner23bace22019-04-18 11:37:26 +02001031
Victor Stinnercfe172d2019-03-01 18:21:49 +01001032 # Python PEP-3118 (buffer protocol) test module
1033 self.add(Extension('_testbuffer', ['_testbuffer.c']))
1034
1035 # Test loading multiple modules from one compiled file (http://bugs.python.org/issue16421)
1036 self.add(Extension('_testimportmultiple', ['_testimportmultiple.c']))
1037
1038 # Test multi-phase extension module init (PEP 489)
1039 self.add(Extension('_testmultiphase', ['_testmultiphase.c']))
1040
1041 # Fuzz tests.
1042 self.add(Extension('_xxtestfuzz',
1043 ['_xxtestfuzz/_xxtestfuzz.c',
1044 '_xxtestfuzz/fuzzer.c']))
1045
Victor Stinner5ec33a12019-03-01 16:43:28 +01001046 def detect_readline_curses(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001047 # readline
Stefan Krah095b2732010-06-08 13:41:44 +00001048 readline_termcap_library = ""
1049 curses_library = ""
doko@ubuntu.com58844492012-06-30 18:25:32 +02001050 # Cannot use os.popen here in py3k.
1051 tmpfile = os.path.join(self.build_temp, 'readline_termcap_lib')
1052 if not os.path.exists(self.build_temp):
1053 os.makedirs(self.build_temp)
Stefan Krah095b2732010-06-08 13:41:44 +00001054 # Determine if readline is already linked against curses or tinfo.
Roland Hiebere1f77692021-02-09 02:05:25 +01001055 if sysconfig.get_config_var('HAVE_LIBREADLINE'):
1056 if sysconfig.get_config_var('WITH_EDITLINE'):
1057 readline_lib = 'edit'
1058 else:
1059 readline_lib = 'readline'
1060 do_readline = self.compiler.find_library_file(self.lib_dirs,
1061 readline_lib)
Victor Stinner4cbea512019-02-28 17:48:38 +01001062 if CROSS_COMPILING:
Victor Stinner6b982c22020-04-01 01:10:07 +02001063 ret = run_command("%s -d %s | grep '(NEEDED)' > %s"
doko@ubuntu.com58844492012-06-30 18:25:32 +02001064 % (sysconfig.get_config_var('READELF'),
1065 do_readline, tmpfile))
1066 elif find_executable('ldd'):
Victor Stinner6b982c22020-04-01 01:10:07 +02001067 ret = run_command("ldd %s > %s" % (do_readline, tmpfile))
doko@ubuntu.com58844492012-06-30 18:25:32 +02001068 else:
Victor Stinner6b982c22020-04-01 01:10:07 +02001069 ret = 1
1070 if ret == 0:
Brett Cannon9f5db072010-10-29 20:19:27 +00001071 with open(tmpfile) as fp:
1072 for ln in fp:
1073 if 'curses' in ln:
1074 readline_termcap_library = re.sub(
1075 r'.*lib(n?cursesw?)\.so.*', r'\1', ln
1076 ).rstrip()
1077 break
1078 # termcap interface split out from ncurses
1079 if 'tinfo' in ln:
1080 readline_termcap_library = 'tinfo'
1081 break
doko@ubuntu.com4c990712012-06-30 23:28:09 +02001082 if os.path.exists(tmpfile):
1083 os.unlink(tmpfile)
Roland Hiebere1f77692021-02-09 02:05:25 +01001084 else:
1085 do_readline = False
Stefan Krah095b2732010-06-08 13:41:44 +00001086 # Issue 7384: If readline is already linked against curses,
1087 # use the same library for the readline and curses modules.
1088 if 'curses' in readline_termcap_library:
1089 curses_library = readline_termcap_library
Victor Stinner625dbf22019-03-01 15:59:39 +01001090 elif self.compiler.find_library_file(self.lib_dirs, 'ncursesw'):
Stefan Krah095b2732010-06-08 13:41:44 +00001091 curses_library = 'ncursesw'
Michael Felt08970cb2019-06-21 15:58:00 +02001092 # Issue 36210: OSS provided ncurses does not link on AIX
1093 # Use IBM supplied 'curses' for successful build of _curses
1094 elif AIX and self.compiler.find_library_file(self.lib_dirs, 'curses'):
1095 curses_library = 'curses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001096 elif self.compiler.find_library_file(self.lib_dirs, 'ncurses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001097 curses_library = 'ncurses'
Victor Stinner625dbf22019-03-01 15:59:39 +01001098 elif self.compiler.find_library_file(self.lib_dirs, 'curses'):
Stefan Krah095b2732010-06-08 13:41:44 +00001099 curses_library = 'curses'
1100
Victor Stinner4cbea512019-02-28 17:48:38 +01001101 if MACOS:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001102 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren961683a2010-03-08 07:09:59 +00001103 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
Ned Deily04cdfa12014-06-25 13:36:14 -07001104 if (dep_target and
Ronald Oussoren49926cf2021-02-01 04:29:44 +01001105 (tuple(int(n) for n in dep_target.split('.')[0:2])
Ned Deily04cdfa12014-06-25 13:36:14 -07001106 < (10, 5) ) ):
Ronald Oussoren961683a2010-03-08 07:09:59 +00001107 os_release = 8
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001108 if os_release < 9:
1109 # MacOSX 10.4 has a broken readline. Don't try to build
1110 # the readline module unless the user has installed a fixed
1111 # readline package
Victor Stinner625dbf22019-03-01 15:59:39 +01001112 if find_file('readline/rlconf.h', self.inc_dirs, []) is None:
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001113 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +00001114 if do_readline:
Victor Stinner4cbea512019-02-28 17:48:38 +01001115 if MACOS and os_release < 9:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001116 # In every directory on the search path search for a dynamic
1117 # library and then a static library, instead of first looking
Fred Drake0af17612007-09-04 19:43:19 +00001118 # for dynamic libraries on the entire path.
Martin Pantere26da7c2016-06-02 10:07:09 +00001119 # This way a statically linked custom readline gets picked up
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001120 # before the (possibly broken) dynamic library in /usr/lib.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121 readline_extra_link_args = ('-Wl,-search_paths_first',)
1122 else:
1123 readline_extra_link_args = ()
1124
Roland Hiebere1f77692021-02-09 02:05:25 +01001125 readline_libs = [readline_lib]
Stefan Krah095b2732010-06-08 13:41:44 +00001126 if readline_termcap_library:
1127 pass # Issue 7384: Already linked against curses or tinfo.
1128 elif curses_library:
1129 readline_libs.append(curses_library)
Victor Stinner625dbf22019-03-01 15:59:39 +01001130 elif self.compiler.find_library_file(self.lib_dirs +
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001131 ['/usr/lib/termcap'],
1132 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +00001133 readline_libs.append('termcap')
Victor Stinner8058bda2019-03-01 15:31:45 +01001134 self.add(Extension('readline', ['readline.c'],
1135 library_dirs=['/usr/lib/termcap'],
1136 extra_link_args=readline_extra_link_args,
1137 libraries=readline_libs))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001138 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001139 self.missing.append('readline')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001140
Victor Stinner5ec33a12019-03-01 16:43:28 +01001141 # Curses support, requiring the System V version of curses, often
1142 # provided by the ncurses library.
1143 curses_defines = []
1144 curses_includes = []
1145 panel_library = 'panel'
1146 if curses_library == 'ncursesw':
1147 curses_defines.append(('HAVE_NCURSESW', '1'))
1148 if not CROSS_COMPILING:
1149 curses_includes.append('/usr/include/ncursesw')
1150 # Bug 1464056: If _curses.so links with ncursesw,
1151 # _curses_panel.so must link with panelw.
1152 panel_library = 'panelw'
1153 if MACOS:
1154 # On OS X, there is no separate /usr/lib/libncursesw nor
1155 # libpanelw. If we are here, we found a locally-supplied
1156 # version of libncursesw. There should also be a
1157 # libpanelw. _XOPEN_SOURCE defines are usually excluded
1158 # for OS X but we need _XOPEN_SOURCE_EXTENDED here for
1159 # ncurses wide char support
1160 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
1161 elif MACOS and curses_library == 'ncurses':
1162 # Building with the system-suppied combined libncurses/libpanel
1163 curses_defines.append(('HAVE_NCURSESW', '1'))
1164 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1'))
Tim Peters2c60f7a2003-01-29 03:49:43 +00001165
Victor Stinnercfe172d2019-03-01 18:21:49 +01001166 curses_enabled = True
Victor Stinner5ec33a12019-03-01 16:43:28 +01001167 if curses_library.startswith('ncurses'):
1168 curses_libs = [curses_library]
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 include_dirs=curses_includes,
1172 define_macros=curses_defines,
1173 libraries=curses_libs))
1174 elif curses_library == 'curses' and not MACOS:
1175 # OSX has an old Berkeley curses, not good enough for
1176 # the _curses module.
1177 if (self.compiler.find_library_file(self.lib_dirs, 'terminfo')):
1178 curses_libs = ['curses', 'terminfo']
1179 elif (self.compiler.find_library_file(self.lib_dirs, 'termcap')):
1180 curses_libs = ['curses', 'termcap']
1181 else:
1182 curses_libs = ['curses']
1183
1184 self.add(Extension('_curses', ['_cursesmodule.c'],
Victor Stinner37834132020-10-27 17:12:53 +01001185 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001186 define_macros=curses_defines,
1187 libraries=curses_libs))
1188 else:
Victor Stinnercfe172d2019-03-01 18:21:49 +01001189 curses_enabled = False
Victor Stinner5ec33a12019-03-01 16:43:28 +01001190 self.missing.append('_curses')
1191
1192 # If the curses module is enabled, check for the panel module
Michael Felt08970cb2019-06-21 15:58:00 +02001193 # _curses_panel needs some form of ncurses
1194 skip_curses_panel = True if AIX else False
1195 if (curses_enabled and not skip_curses_panel and
1196 self.compiler.find_library_file(self.lib_dirs, panel_library)):
Victor Stinner5ec33a12019-03-01 16:43:28 +01001197 self.add(Extension('_curses_panel', ['_curses_panel.c'],
Michael Felt08970cb2019-06-21 15:58:00 +02001198 include_dirs=curses_includes,
1199 define_macros=curses_defines,
1200 libraries=[panel_library, *curses_libs]))
1201 elif not skip_curses_panel:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001202 self.missing.append('_curses_panel')
1203
1204 def detect_crypt(self):
1205 # crypt module.
pxinwr236d0b72019-04-15 17:02:20 +08001206 if VXWORKS:
1207 # bpo-31904: crypt() function is not provided by VxWorks.
1208 # DES_crypt() OpenSSL provides is too weak to implement
1209 # the encryption.
Victor Stinnercad80202021-01-19 23:04:49 +01001210 self.missing.append('_crypt')
pxinwr236d0b72019-04-15 17:02:20 +08001211 return
1212
Victor Stinner625dbf22019-03-01 15:59:39 +01001213 if self.compiler.find_library_file(self.lib_dirs, 'crypt'):
Ronald Oussoren94f25282010-05-05 19:11:21 +00001214 libs = ['crypt']
Guido van Rossumd8faa362007-04-27 19:54:29 +00001215 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +00001216 libs = []
pxinwr32f5fdd2019-02-27 19:09:28 +08001217
Victor Stinnercad80202021-01-19 23:04:49 +01001218 self.add(Extension('_crypt', ['_cryptmodule.c'], libraries=libs))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001219
Victor Stinner5ec33a12019-03-01 16:43:28 +01001220 def detect_socket(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001221 # socket(2)
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001222 kwargs = {'depends': ['socketmodule.h']}
pxinwr00a65682020-11-29 06:14:16 +08001223 if MACOS:
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001224 # Issue #35569: Expose RFC 3542 socket options.
1225 kwargs['extra_compile_args'] = ['-D__APPLE_USE_RFC_3542']
Erlend Egeberg Aasland9a45bfe2020-05-17 08:32:46 +02001226
Erlend Egeberg Aaslandccdcb202020-11-18 01:08:58 +01001227 self.add(Extension('_socket', ['socketmodule.c'], **kwargs))
pxinwr32f5fdd2019-02-27 19:09:28 +08001228
Victor Stinner5ec33a12019-03-01 16:43:28 +01001229 def detect_dbm_gdbm(self):
Georg Brandl489cb4f2009-07-11 10:08:49 +00001230 # Modules that provide persistent dictionary-like semantics. You will
1231 # probably want to arrange for at least one of them to be available on
1232 # your machine, though none are defined by default because of library
1233 # dependencies. The Python module dbm/__init__.py provides an
1234 # implementation independent wrapper for these; dbm/dumb.py provides
1235 # similar functionality (but slower of course) implemented in Python.
1236
1237 # Sleepycat^WOracle Berkeley DB interface.
1238 # http://www.oracle.com/database/berkeley-db/db/index.html
1239 #
1240 # This requires the Sleepycat^WOracle DB code. The supported versions
1241 # are set below. Visit the URL above to download
1242 # a release. Most open source OSes come with one or more
1243 # versions of BerkeleyDB already installed.
1244
doko@ubuntu.com15bac0f2012-07-01 10:35:54 +02001245 max_db_ver = (5, 3)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001246 min_db_ver = (3, 3)
1247 db_setup_debug = False # verbose debug prints from this script?
1248
1249 def allow_db_ver(db_ver):
1250 """Returns a boolean if the given BerkeleyDB version is acceptable.
1251
1252 Args:
1253 db_ver: A tuple of the version to verify.
1254 """
1255 if not (min_db_ver <= db_ver <= max_db_ver):
1256 return False
1257 return True
1258
1259 def gen_db_minor_ver_nums(major):
1260 if major == 4:
1261 for x in range(max_db_ver[1]+1):
1262 if allow_db_ver((4, x)):
1263 yield x
1264 elif major == 3:
1265 for x in (3,):
1266 if allow_db_ver((3, x)):
1267 yield x
1268 else:
1269 raise ValueError("unknown major BerkeleyDB version", major)
1270
1271 # construct a list of paths to look for the header file in on
1272 # top of the normal inc_dirs.
1273 db_inc_paths = [
1274 '/usr/include/db4',
1275 '/usr/local/include/db4',
1276 '/opt/sfw/include/db4',
1277 '/usr/include/db3',
1278 '/usr/local/include/db3',
1279 '/opt/sfw/include/db3',
1280 # Fink defaults (http://fink.sourceforge.net/)
1281 '/sw/include/db4',
1282 '/sw/include/db3',
1283 ]
1284 # 4.x minor number specific paths
1285 for x in gen_db_minor_ver_nums(4):
1286 db_inc_paths.append('/usr/include/db4%d' % x)
1287 db_inc_paths.append('/usr/include/db4.%d' % x)
1288 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
1289 db_inc_paths.append('/usr/local/include/db4%d' % x)
1290 db_inc_paths.append('/pkg/db-4.%d/include' % x)
1291 db_inc_paths.append('/opt/db-4.%d/include' % x)
1292 # MacPorts default (http://www.macports.org/)
1293 db_inc_paths.append('/opt/local/include/db4%d' % x)
1294 # 3.x minor number specific paths
1295 for x in gen_db_minor_ver_nums(3):
1296 db_inc_paths.append('/usr/include/db3%d' % x)
1297 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
1298 db_inc_paths.append('/usr/local/include/db3%d' % x)
1299 db_inc_paths.append('/pkg/db-3.%d/include' % x)
1300 db_inc_paths.append('/opt/db-3.%d/include' % x)
1301
Victor Stinner4cbea512019-02-28 17:48:38 +01001302 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001303 db_inc_paths = []
1304
Georg Brandl489cb4f2009-07-11 10:08:49 +00001305 # Add some common subdirectories for Sleepycat DB to the list,
1306 # based on the standard include directories. This way DB3/4 gets
1307 # picked up when it is installed in a non-standard prefix and
1308 # the user has added that prefix into inc_dirs.
1309 std_variants = []
Victor Stinner625dbf22019-03-01 15:59:39 +01001310 for dn in self.inc_dirs:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001311 std_variants.append(os.path.join(dn, 'db3'))
1312 std_variants.append(os.path.join(dn, 'db4'))
1313 for x in gen_db_minor_ver_nums(4):
1314 std_variants.append(os.path.join(dn, "db4%d"%x))
1315 std_variants.append(os.path.join(dn, "db4.%d"%x))
1316 for x in gen_db_minor_ver_nums(3):
1317 std_variants.append(os.path.join(dn, "db3%d"%x))
1318 std_variants.append(os.path.join(dn, "db3.%d"%x))
1319
1320 db_inc_paths = std_variants + db_inc_paths
1321 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
1322
1323 db_ver_inc_map = {}
1324
Victor Stinner4cbea512019-02-28 17:48:38 +01001325 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001326 sysroot = macosx_sdk_root()
1327
Georg Brandl489cb4f2009-07-11 10:08:49 +00001328 class db_found(Exception): pass
1329 try:
1330 # See whether there is a Sleepycat header in the standard
1331 # search path.
Victor Stinner625dbf22019-03-01 15:59:39 +01001332 for d in self.inc_dirs + db_inc_paths:
Georg Brandl489cb4f2009-07-11 10:08:49 +00001333 f = os.path.join(d, "db.h")
Victor Stinner4cbea512019-02-28 17:48:38 +01001334 if MACOS and is_macosx_sdk_path(d):
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001335 f = os.path.join(sysroot, d[1:], "db.h")
1336
Georg Brandl489cb4f2009-07-11 10:08:49 +00001337 if db_setup_debug: print("db: looking for db.h in", f)
1338 if os.path.exists(f):
Brett Cannon9f5db072010-10-29 20:19:27 +00001339 with open(f, 'rb') as file:
1340 f = file.read()
Benjamin Peterson019f3612009-08-12 18:18:03 +00001341 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001342 if m:
1343 db_major = int(m.group(1))
Benjamin Peterson019f3612009-08-12 18:18:03 +00001344 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001345 db_minor = int(m.group(1))
1346 db_ver = (db_major, db_minor)
1347
1348 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
1349 if db_ver == (4, 6):
Benjamin Peterson019f3612009-08-12 18:18:03 +00001350 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001351 db_patch = int(m.group(1))
1352 if db_patch < 21:
1353 print("db.h:", db_ver, "patch", db_patch,
1354 "being ignored (4.6.x must be >= 4.6.21)")
1355 continue
1356
1357 if ( (db_ver not in db_ver_inc_map) and
1358 allow_db_ver(db_ver) ):
1359 # save the include directory with the db.h version
1360 # (first occurrence only)
1361 db_ver_inc_map[db_ver] = d
1362 if db_setup_debug:
1363 print("db.h: found", db_ver, "in", d)
1364 else:
1365 # we already found a header for this library version
1366 if db_setup_debug: print("db.h: ignoring", d)
1367 else:
1368 # ignore this header, it didn't contain a version number
1369 if db_setup_debug:
1370 print("db.h: no version number version in", d)
1371
1372 db_found_vers = list(db_ver_inc_map.keys())
1373 db_found_vers.sort()
1374
1375 while db_found_vers:
1376 db_ver = db_found_vers.pop()
1377 db_incdir = db_ver_inc_map[db_ver]
1378
1379 # check lib directories parallel to the location of the header
1380 db_dirs_to_check = [
1381 db_incdir.replace("include", 'lib64'),
1382 db_incdir.replace("include", 'lib'),
1383 ]
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001384
Victor Stinner4cbea512019-02-28 17:48:38 +01001385 if not MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001386 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check))
1387
1388 else:
1389 # Same as other branch, but takes OSX SDK into account
1390 tmp = []
1391 for dn in db_dirs_to_check:
1392 if is_macosx_sdk_path(dn):
1393 if os.path.isdir(os.path.join(sysroot, dn[1:])):
1394 tmp.append(dn)
1395 else:
1396 if os.path.isdir(dn):
1397 tmp.append(dn)
Ronald Oussorendc969e52010-06-27 12:37:46 +00001398 db_dirs_to_check = tmp
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001399
1400 db_dirs_to_check = tmp
Georg Brandl489cb4f2009-07-11 10:08:49 +00001401
Ezio Melotti42da6632011-03-15 05:18:48 +02001402 # Look for a version specific db-X.Y before an ambiguous dbX
Georg Brandl489cb4f2009-07-11 10:08:49 +00001403 # XXX should we -ever- look for a dbX name? Do any
1404 # systems really not name their library by version and
1405 # symlink to more general names?
1406 for dblib in (('db-%d.%d' % db_ver),
1407 ('db%d%d' % db_ver),
1408 ('db%d' % db_ver[0])):
1409 dblib_file = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001410 db_dirs_to_check + self.lib_dirs, dblib )
Georg Brandl489cb4f2009-07-11 10:08:49 +00001411 if dblib_file:
1412 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
1413 raise db_found
1414 else:
1415 if db_setup_debug: print("db lib: ", dblib, "not found")
1416
1417 except db_found:
1418 if db_setup_debug:
1419 print("bsddb using BerkeleyDB lib:", db_ver, dblib)
1420 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir)
Georg Brandl489cb4f2009-07-11 10:08:49 +00001421 dblibs = [dblib]
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001422 # Only add the found library and include directories if they aren't
1423 # already being searched. This avoids an explicit runtime library
1424 # dependency.
Victor Stinner625dbf22019-03-01 15:59:39 +01001425 if db_incdir in self.inc_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001426 db_incs = None
1427 else:
1428 db_incs = [db_incdir]
Victor Stinner625dbf22019-03-01 15:59:39 +01001429 if dblib_dir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001430 dblib_dir = None
Georg Brandl489cb4f2009-07-11 10:08:49 +00001431 else:
1432 if db_setup_debug: print("db: no appropriate library found")
1433 db_incs = None
1434 dblibs = []
1435 dblib_dir = None
1436
Victor Stinner5ec33a12019-03-01 16:43:28 +01001437 dbm_setup_debug = False # verbose debug prints from this script?
1438 dbm_order = ['gdbm']
1439 # The standard Unix dbm module:
1440 if not CYGWIN:
1441 config_args = [arg.strip("'")
1442 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
1443 dbm_args = [arg for arg in config_args
1444 if arg.startswith('--with-dbmliborder=')]
1445 if dbm_args:
1446 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
1447 else:
1448 dbm_order = "ndbm:gdbm:bdb".split(":")
1449 dbmext = None
1450 for cand in dbm_order:
1451 if cand == "ndbm":
1452 if find_file("ndbm.h", self.inc_dirs, []) is not None:
1453 # Some systems have -lndbm, others have -lgdbm_compat,
1454 # others don't have either
1455 if self.compiler.find_library_file(self.lib_dirs,
1456 'ndbm'):
1457 ndbm_libs = ['ndbm']
1458 elif self.compiler.find_library_file(self.lib_dirs,
1459 'gdbm_compat'):
1460 ndbm_libs = ['gdbm_compat']
1461 else:
1462 ndbm_libs = []
1463 if dbm_setup_debug: print("building dbm using ndbm")
1464 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1465 define_macros=[
1466 ('HAVE_NDBM_H',None),
1467 ],
1468 libraries=ndbm_libs)
1469 break
1470
1471 elif cand == "gdbm":
1472 if self.compiler.find_library_file(self.lib_dirs, 'gdbm'):
1473 gdbm_libs = ['gdbm']
1474 if self.compiler.find_library_file(self.lib_dirs,
1475 'gdbm_compat'):
1476 gdbm_libs.append('gdbm_compat')
1477 if find_file("gdbm/ndbm.h", self.inc_dirs, []) is not None:
1478 if dbm_setup_debug: print("building dbm using gdbm")
1479 dbmext = Extension(
1480 '_dbm', ['_dbmmodule.c'],
1481 define_macros=[
1482 ('HAVE_GDBM_NDBM_H', None),
1483 ],
1484 libraries = gdbm_libs)
1485 break
1486 if find_file("gdbm-ndbm.h", self.inc_dirs, []) is not None:
1487 if dbm_setup_debug: print("building dbm using gdbm")
1488 dbmext = Extension(
1489 '_dbm', ['_dbmmodule.c'],
1490 define_macros=[
1491 ('HAVE_GDBM_DASH_NDBM_H', None),
1492 ],
1493 libraries = gdbm_libs)
1494 break
1495 elif cand == "bdb":
1496 if dblibs:
1497 if dbm_setup_debug: print("building dbm using bdb")
1498 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1499 library_dirs=dblib_dir,
1500 runtime_library_dirs=dblib_dir,
1501 include_dirs=db_incs,
1502 define_macros=[
1503 ('HAVE_BERKDB_H', None),
1504 ('DB_DBM_HSEARCH', None),
1505 ],
1506 libraries=dblibs)
1507 break
1508 if dbmext is not None:
1509 self.add(dbmext)
1510 else:
1511 self.missing.append('_dbm')
1512
1513 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
1514 if ('gdbm' in dbm_order and
1515 self.compiler.find_library_file(self.lib_dirs, 'gdbm')):
1516 self.add(Extension('_gdbm', ['_gdbmmodule.c'],
1517 libraries=['gdbm']))
1518 else:
1519 self.missing.append('_gdbm')
1520
1521 def detect_sqlite(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001522 # The sqlite interface
Thomas Wouters89f507f2006-12-13 04:49:30 +00001523 sqlite_setup_debug = False # verbose debug prints from this script?
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524
1525 # We hunt for #define SQLITE_VERSION "n.n.n"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001526 sqlite_incdir = sqlite_libdir = None
1527 sqlite_inc_paths = [ '/usr/include',
1528 '/usr/include/sqlite',
1529 '/usr/include/sqlite3',
1530 '/usr/local/include',
1531 '/usr/local/include/sqlite',
1532 '/usr/local/include/sqlite3',
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001533 ]
Victor Stinner4cbea512019-02-28 17:48:38 +01001534 if CROSS_COMPILING:
doko@ubuntu.com1abe1c52012-06-30 20:42:45 +02001535 sqlite_inc_paths = []
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +01001536 MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 MIN_SQLITE_VERSION = ".".join([str(x)
1538 for x in MIN_SQLITE_VERSION_NUMBER])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539
1540 # Scan the default include directories before the SQLite specific
1541 # ones. This allows one to override the copy of sqlite on OSX,
1542 # where /usr/include contains an old version of sqlite.
Victor Stinner4cbea512019-02-28 17:48:38 +01001543 if MACOS:
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001544 sysroot = macosx_sdk_root()
1545
Victor Stinner625dbf22019-03-01 15:59:39 +01001546 for d_ in self.inc_dirs + sqlite_inc_paths:
Ned Deily9b635832012-08-05 15:13:33 -07001547 d = d_
Victor Stinner4cbea512019-02-28 17:48:38 +01001548 if MACOS and is_macosx_sdk_path(d):
Ned Deily9b635832012-08-05 15:13:33 -07001549 d = os.path.join(sysroot, d[1:])
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001550
Ned Deily9b635832012-08-05 15:13:33 -07001551 f = os.path.join(d, "sqlite3.h")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001552 if os.path.exists(f):
Guido van Rossum452bf512007-02-09 05:32:43 +00001553 if sqlite_setup_debug: print("sqlite: found %s"%f)
Brett Cannon9f5db072010-10-29 20:19:27 +00001554 with open(f) as file:
1555 incf = file.read()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556 m = re.search(
Petri Lehtinened909bc2013-02-23 17:05:28 +01001557 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001558 if m:
1559 sqlite_version = m.group(1)
1560 sqlite_version_tuple = tuple([int(x)
1561 for x in sqlite_version.split(".")])
1562 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
1563 # we win!
Thomas Wouters89f507f2006-12-13 04:49:30 +00001564 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001565 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001566 sqlite_incdir = d
1567 break
1568 else:
1569 if sqlite_setup_debug:
Charles Pigottad0daf52019-04-26 16:38:12 +01001570 print("%s: version %s is too old, need >= %s"%(d,
Guido van Rossum452bf512007-02-09 05:32:43 +00001571 sqlite_version, MIN_SQLITE_VERSION))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001572 elif sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +00001573 print("sqlite: %s had no SQLITE_VERSION"%(f,))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001574
1575 if sqlite_incdir:
1576 sqlite_dirs_to_check = [
1577 os.path.join(sqlite_incdir, '..', 'lib64'),
1578 os.path.join(sqlite_incdir, '..', 'lib'),
1579 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
1580 os.path.join(sqlite_incdir, '..', '..', 'lib'),
1581 ]
Tarek Ziadé36797272010-07-22 12:50:05 +00001582 sqlite_libfile = self.compiler.find_library_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01001583 sqlite_dirs_to_check + self.lib_dirs, 'sqlite3')
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001584 if sqlite_libfile:
1585 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001586
1587 if sqlite_incdir and sqlite_libdir:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001588 sqlite_srcs = ['_sqlite/cache.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001589 '_sqlite/connection.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001590 '_sqlite/cursor.c',
1591 '_sqlite/microprotocols.c',
1592 '_sqlite/module.c',
1593 '_sqlite/prepare_protocol.c',
1594 '_sqlite/row.c',
1595 '_sqlite/statement.c',
1596 '_sqlite/util.c', ]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001597 sqlite_defines = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001598
Benjamin Peterson076ed002010-10-31 17:11:02 +00001599 # Enable support for loadable extensions in the sqlite3 module
1600 # if --enable-loadable-sqlite-extensions configure option is used.
1601 if '--enable-loadable-sqlite-extensions' not in sysconfig.get_config_var("CONFIG_ARGS"):
1602 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001603
Victor Stinner4cbea512019-02-28 17:48:38 +01001604 if MACOS:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001605 # In every directory on the search path search for a dynamic
1606 # library and then a static library, instead of first looking
Ezio Melotti13925002011-03-16 11:05:33 +02001607 # for dynamic libraries on the entire path.
1608 # This way a statically linked custom sqlite gets picked up
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609 # before the dynamic library in /usr/lib.
1610 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1611 else:
1612 sqlite_extra_link_args = ()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001613
Brett Cannonc5011fe2011-06-06 20:09:10 -07001614 include_dirs = ["Modules/_sqlite"]
1615 # Only include the directory where sqlite was found if it does
1616 # not already exist in set include directories, otherwise you
1617 # can end up with a bad search path order.
1618 if sqlite_incdir not in self.compiler.include_dirs:
1619 include_dirs.append(sqlite_incdir)
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001620 # avoid a runtime library path for a system library dir
Victor Stinner625dbf22019-03-01 15:59:39 +01001621 if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs:
doko@ubuntu.coma3818a32014-04-17 17:52:48 +02001622 sqlite_libdir = None
Victor Stinner8058bda2019-03-01 15:31:45 +01001623 self.add(Extension('_sqlite3', sqlite_srcs,
1624 define_macros=sqlite_defines,
1625 include_dirs=include_dirs,
1626 library_dirs=sqlite_libdir,
1627 extra_link_args=sqlite_extra_link_args,
1628 libraries=["sqlite3",]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001629 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001630 self.missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001631
Victor Stinner5ec33a12019-03-01 16:43:28 +01001632 def detect_platform_specific_exts(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001633 # Unix-only modules
Victor Stinner4cbea512019-02-28 17:48:38 +01001634 if not MS_WINDOWS:
pxinwr32f5fdd2019-02-27 19:09:28 +08001635 if not VXWORKS:
1636 # Steen Lumholt's termios module
Victor Stinner8058bda2019-03-01 15:31:45 +01001637 self.add(Extension('termios', ['termios.c']))
pxinwr32f5fdd2019-02-27 19:09:28 +08001638 # Jeremy Hylton's rlimit interface
Victor Stinner8058bda2019-03-01 15:31:45 +01001639 self.add(Extension('resource', ['resource.c']))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001640 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001641 self.missing.extend(['resource', 'termios'])
Christian Heimes29a7df72018-01-26 23:28:46 +01001642
Victor Stinner5ec33a12019-03-01 16:43:28 +01001643 # Platform-specific libraries
1644 if HOST_PLATFORM.startswith(('linux', 'freebsd', 'gnukfreebsd')):
1645 self.add(Extension('ossaudiodev', ['ossaudiodev.c']))
Michael Felt08970cb2019-06-21 15:58:00 +02001646 elif not AIX:
Victor Stinner5ec33a12019-03-01 16:43:28 +01001647 self.missing.append('ossaudiodev')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001648
Victor Stinner5ec33a12019-03-01 16:43:28 +01001649 if MACOS:
Ned Deily951ab582020-05-18 11:31:21 -04001650 self.add(Extension('_scproxy', ['_scproxy.c'],
Victor Stinner5ec33a12019-03-01 16:43:28 +01001651 extra_link_args=[
1652 '-framework', 'SystemConfiguration',
Ned Deily951ab582020-05-18 11:31:21 -04001653 '-framework', 'CoreFoundation']))
Fredrik Lundhade711a2001-01-24 08:00:28 +00001654
Victor Stinner5ec33a12019-03-01 16:43:28 +01001655 def detect_compress_exts(self):
Barry Warsaw259b1e12002-08-13 20:09:26 +00001656 # Andrew Kuchling's zlib module. Note that some versions of zlib
1657 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1658 # http://www.cert.org/advisories/CA-2002-07.html
1659 #
1660 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1661 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1662 # now, we still accept 1.1.3, because we think it's difficult to
1663 # exploit this in Python, and we'd rather make it RedHat's problem
1664 # than our problem <wink>.
1665 #
1666 # You can upgrade zlib to version 1.1.4 yourself by going to
1667 # http://www.gzip.org/zlib/
Victor Stinner625dbf22019-03-01 15:59:39 +01001668 zlib_inc = find_file('zlib.h', [], self.inc_dirs)
Christian Heimes1dc54002008-03-24 02:19:29 +00001669 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001670 if zlib_inc is not None:
1671 zlib_h = zlib_inc[0] + '/zlib.h'
1672 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001673 version_req = '"1.1.3"'
Victor Stinner4cbea512019-02-28 17:48:38 +01001674 if MACOS and is_macosx_sdk_path(zlib_h):
Ned Deily507c5912013-10-18 21:32:00 -07001675 zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
Brett Cannon9f5db072010-10-29 20:19:27 +00001676 with open(zlib_h) as fp:
1677 while 1:
1678 line = fp.readline()
1679 if not line:
1680 break
1681 if line.startswith('#define ZLIB_VERSION'):
1682 version = line.split()[2]
1683 break
Guido van Rossume6970912001-04-15 15:16:12 +00001684 if version >= version_req:
Victor Stinner625dbf22019-03-01 15:59:39 +01001685 if (self.compiler.find_library_file(self.lib_dirs, 'z')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001686 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001687 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1688 else:
1689 zlib_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001690 self.add(Extension('zlib', ['zlibmodule.c'],
1691 libraries=['z'],
1692 extra_link_args=zlib_extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001693 have_zlib = True
Guido van Rossumd8faa362007-04-27 19:54:29 +00001694 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001695 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001696 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001697 self.missing.append('zlib')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001698 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001699 self.missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001700
Christian Heimes1dc54002008-03-24 02:19:29 +00001701 # Helper module for various ascii-encoders. Uses zlib for an optimized
1702 # crc32 if we have it. Otherwise binascii uses its own.
1703 if have_zlib:
1704 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1705 libraries = ['z']
1706 extra_link_args = zlib_extra_link_args
1707 else:
1708 extra_compile_args = []
1709 libraries = []
1710 extra_link_args = []
Victor Stinner8058bda2019-03-01 15:31:45 +01001711 self.add(Extension('binascii', ['binascii.c'],
1712 extra_compile_args=extra_compile_args,
1713 libraries=libraries,
1714 extra_link_args=extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001715
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001716 # Gustavo Niemeyer's bz2 module.
Victor Stinner625dbf22019-03-01 15:59:39 +01001717 if (self.compiler.find_library_file(self.lib_dirs, 'bz2')):
Victor Stinner4cbea512019-02-28 17:48:38 +01001718 if MACOS:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001719 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1720 else:
1721 bz2_extra_link_args = ()
Victor Stinner8058bda2019-03-01 15:31:45 +01001722 self.add(Extension('_bz2', ['_bz2module.c'],
1723 libraries=['bz2'],
1724 extra_link_args=bz2_extra_link_args))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001725 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001726 self.missing.append('_bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001727
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001728 # LZMA compression support.
Victor Stinner625dbf22019-03-01 15:59:39 +01001729 if self.compiler.find_library_file(self.lib_dirs, 'lzma'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001730 self.add(Extension('_lzma', ['_lzmamodule.c'],
1731 libraries=['lzma']))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001732 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001733 self.missing.append('_lzma')
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001734
Victor Stinner5ec33a12019-03-01 16:43:28 +01001735 def detect_expat_elementtree(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001736 # Interface to the Expat XML parser
1737 #
Benjamin Petersona28e7022010-01-09 18:53:06 +00001738 # Expat was written by James Clark and is now maintained by a group of
1739 # developers on SourceForge; see www.libexpat.org for more information.
1740 # The pyexpat module was written by Paul Prescod after a prototype by
1741 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1742 # of a system shared libexpat.so is possible with --with-system-expat
Benjamin Petersonc73206c2010-10-31 16:38:19 +00001743 # configure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001744 #
1745 # More information on Expat can be found at www.libexpat.org.
1746 #
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001747 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1748 expat_inc = []
1749 define_macros = []
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001750 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001751 expat_lib = ['expat']
1752 expat_sources = []
Christian Heimesd489c7a2013-02-09 17:02:06 +01001753 expat_depends = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001754 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01001755 expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')]
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001756 define_macros = [
1757 ('HAVE_EXPAT_CONFIG_H', '1'),
Victor Stinner93d0cb52017-08-18 23:43:54 +02001758 # bpo-30947: Python uses best available entropy sources to
1759 # call XML_SetHashSalt(), expat entropy sources are not needed
1760 ('XML_POOR_ENTROPY', '1'),
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001761 ]
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001762 extra_compile_args = []
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001763 expat_lib = []
1764 expat_sources = ['expat/xmlparse.c',
1765 'expat/xmlrole.c',
1766 'expat/xmltok.c']
Christian Heimesd489c7a2013-02-09 17:02:06 +01001767 expat_depends = ['expat/ascii.h',
1768 'expat/asciitab.h',
1769 'expat/expat.h',
1770 'expat/expat_config.h',
1771 'expat/expat_external.h',
1772 'expat/internal.h',
1773 'expat/latin1tab.h',
1774 'expat/utf8tab.h',
1775 'expat/xmlrole.h',
1776 'expat/xmltok.h',
1777 'expat/xmltok_impl.h'
1778 ]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001779
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001780 cc = sysconfig.get_config_var('CC').split()[0]
Victor Stinner6b982c22020-04-01 01:10:07 +02001781 ret = run_command(
Benjamin Peterson95da3102019-06-29 16:00:22 -07001782 '"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc)
Victor Stinner6b982c22020-04-01 01:10:07 +02001783 if ret == 0:
Benjamin Peterson95da3102019-06-29 16:00:22 -07001784 extra_compile_args.append('-Wno-unreachable-code')
Stefan Krah9e1e6f52017-08-25 14:07:50 +02001785
Victor Stinner8058bda2019-03-01 15:31:45 +01001786 self.add(Extension('pyexpat',
1787 define_macros=define_macros,
1788 extra_compile_args=extra_compile_args,
1789 include_dirs=expat_inc,
1790 libraries=expat_lib,
1791 sources=['pyexpat.c'] + expat_sources,
1792 depends=expat_depends))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001793
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001794 # Fredrik Lundh's cElementTree module. Note that this also
1795 # uses expat (via the CAPI hook in pyexpat).
1796
Victor Stinner625dbf22019-03-01 15:59:39 +01001797 if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001798 define_macros.append(('USE_PYEXPAT_CAPI', None))
Victor Stinner8058bda2019-03-01 15:31:45 +01001799 self.add(Extension('_elementtree',
1800 define_macros=define_macros,
1801 include_dirs=expat_inc,
1802 libraries=expat_lib,
1803 sources=['_elementtree.c'],
1804 depends=['pyexpat.c', *expat_sources,
1805 *expat_depends]))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001806 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001807 self.missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001808
Victor Stinner5ec33a12019-03-01 16:43:28 +01001809 def detect_multibytecodecs(self):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001810 # Hye-Shik Chang's CJKCodecs modules.
Victor Stinner8058bda2019-03-01 15:31:45 +01001811 self.add(Extension('_multibytecodec',
1812 ['cjkcodecs/multibytecodec.c']))
Walter Dörwalde9eaab42007-05-22 16:02:13 +00001813 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
Victor Stinner8058bda2019-03-01 15:31:45 +01001814 self.add(Extension('_codecs_%s' % loc,
1815 ['cjkcodecs/_codecs_%s.c' % loc]))
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001816
Victor Stinner5ec33a12019-03-01 16:43:28 +01001817 def detect_multiprocessing(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001818 # Richard Oudkerk's multiprocessing module
Victor Stinner4cbea512019-02-28 17:48:38 +01001819 if MS_WINDOWS:
Victor Stinnerc991f242019-03-01 17:19:04 +01001820 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c',
1821 '_multiprocessing/semaphore.c']
Benjamin Petersone711caf2008-06-11 16:44:04 +00001822 else:
Victor Stinnerc991f242019-03-01 17:19:04 +01001823 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c']
Mark Dickinsona614f042009-11-28 12:48:43 +00001824 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
1825 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001826 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
Victor Stinner8058bda2019-03-01 15:31:45 +01001827 self.add(Extension('_multiprocessing', multiprocessing_srcs,
Victor Stinner8058bda2019-03-01 15:31:45 +01001828 include_dirs=["Modules/_multiprocessing"]))
Guido van Rossuma9e20242007-03-08 00:43:48 +00001829
Victor Stinnercad80202021-01-19 23:04:49 +01001830 if (not MS_WINDOWS and
1831 sysconfig.get_config_var('HAVE_SHM_OPEN') and
1832 sysconfig.get_config_var('HAVE_SHM_UNLINK')):
1833 posixshmem_srcs = ['_multiprocessing/posixshmem.c']
1834 libs = []
1835 if sysconfig.get_config_var('SHM_NEEDS_LIBRT'):
1836 # need to link with librt to get shm_open()
1837 libs.append('rt')
1838 self.add(Extension('_posixshmem', posixshmem_srcs,
1839 define_macros={},
1840 libraries=libs,
1841 include_dirs=["Modules/_multiprocessing"]))
1842 else:
1843 self.missing.append('_posixshmem')
1844
Victor Stinner5ec33a12019-03-01 16:43:28 +01001845 def detect_uuid(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001846 # Build the _uuid module if possible
Victor Stinner625dbf22019-03-01 15:59:39 +01001847 uuid_incs = find_file("uuid.h", self.inc_dirs, ["/usr/include/uuid"])
Nick Coghlan53efbf32017-11-26 13:04:46 +10001848 if uuid_incs is not None:
Victor Stinner625dbf22019-03-01 15:59:39 +01001849 if self.compiler.find_library_file(self.lib_dirs, 'uuid'):
Antoine Pitroua106aec2017-09-28 23:03:06 +02001850 uuid_libs = ['uuid']
1851 else:
1852 uuid_libs = []
Victor Stinnercfe172d2019-03-01 18:21:49 +01001853 self.add(Extension('_uuid', ['_uuidmodule.c'],
1854 libraries=uuid_libs,
1855 include_dirs=uuid_incs))
Antoine Pitroua106aec2017-09-28 23:03:06 +02001856 else:
Victor Stinner8058bda2019-03-01 15:31:45 +01001857 self.missing.append('_uuid')
Antoine Pitroua106aec2017-09-28 23:03:06 +02001858
Victor Stinner5ec33a12019-03-01 16:43:28 +01001859 def detect_modules(self):
Victor Stinnercfe172d2019-03-01 18:21:49 +01001860 self.configure_compiler()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001861 self.init_inc_lib_dirs()
1862
1863 self.detect_simple_extensions()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001864 if TEST_EXTENSIONS:
1865 self.detect_test_extensions()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001866 self.detect_readline_curses()
1867 self.detect_crypt()
1868 self.detect_socket()
1869 self.detect_openssl_hashlib()
xdegaye2ee077f2019-04-09 17:20:08 +02001870 self.detect_hash_builtins()
Victor Stinner5ec33a12019-03-01 16:43:28 +01001871 self.detect_dbm_gdbm()
1872 self.detect_sqlite()
1873 self.detect_platform_specific_exts()
1874 self.detect_nis()
1875 self.detect_compress_exts()
1876 self.detect_expat_elementtree()
1877 self.detect_multibytecodecs()
1878 self.detect_decimal()
1879 self.detect_ctypes()
1880 self.detect_multiprocessing()
1881 if not self.detect_tkinter():
1882 self.missing.append('_tkinter')
1883 self.detect_uuid()
1884
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001885## # Uncomment these lines if you want to play with xxmodule.c
Victor Stinnercfe172d2019-03-01 18:21:49 +01001886## self.add(Extension('xx', ['xxmodule.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001887
Hai Shi5787ba42021-04-06 20:55:13 +08001888 # The limited C API is not compatible with the Py_TRACE_REFS macro.
1889 if not sysconfig.get_config_var('Py_TRACE_REFS'):
1890 self.add(Extension('xxlimited', ['xxlimited.c']))
1891 self.add(Extension('xxlimited_35', ['xxlimited_35.c']))
Ned Deilycd3d8fb2013-08-01 23:51:27 -07001892
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001893 def detect_tkinter_fromenv(self):
1894 # Build _tkinter using the Tcl/Tk locations specified by
1895 # the _TCLTK_INCLUDES and _TCLTK_LIBS environment variables.
1896 # This method is meant to be invoked by detect_tkinter().
Ned Deilyd819b932013-09-06 01:07:05 -07001897 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001898 # The variables can be set via one of the following ways.
Ned Deilyd819b932013-09-06 01:07:05 -07001899 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001900 # - Automatically, at configuration time, by using pkg-config.
1901 # The tool is called by the configure script.
1902 # Additional pkg-config configuration paths can be set via the
1903 # PKG_CONFIG_PATH environment variable.
1904 #
1905 # PKG_CONFIG_PATH=".../lib/pkgconfig" ./configure ...
1906 #
1907 # - Explicitly, at configuration time by setting both
1908 # --with-tcltk-includes and --with-tcltk-libs.
1909 #
1910 # ./configure ... \
Ned Deilyd819b932013-09-06 01:07:05 -07001911 # --with-tcltk-includes="-I/path/to/tclincludes \
1912 # -I/path/to/tkincludes"
1913 # --with-tcltk-libs="-L/path/to/tcllibs -ltclm.n \
1914 # -L/path/to/tklibs -ltkm.n"
1915 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001916 # - Explicitly, at compile time, by passing TCLTK_INCLUDES and
1917 # TCLTK_LIBS to the make target.
1918 # This will override any configuration-time option.
1919 #
1920 # make TCLTK_INCLUDES="..." TCLTK_LIBS="..."
Ned Deilyd819b932013-09-06 01:07:05 -07001921 #
1922 # This can be useful for building and testing tkinter with multiple
1923 # versions of Tcl/Tk. Note that a build of Tk depends on a particular
1924 # build of Tcl so you need to specify both arguments and use care when
1925 # overriding.
1926
1927 # The _TCLTK variables are created in the Makefile sharedmods target.
1928 tcltk_includes = os.environ.get('_TCLTK_INCLUDES')
1929 tcltk_libs = os.environ.get('_TCLTK_LIBS')
1930 if not (tcltk_includes and tcltk_libs):
1931 # Resume default configuration search.
Victor Stinner4cbea512019-02-28 17:48:38 +01001932 return False
Ned Deilyd819b932013-09-06 01:07:05 -07001933
1934 extra_compile_args = tcltk_includes.split()
1935 extra_link_args = tcltk_libs.split()
Victor Stinnercfe172d2019-03-01 18:21:49 +01001936 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1937 define_macros=[('WITH_APPINIT', 1)],
1938 extra_compile_args = extra_compile_args,
1939 extra_link_args = extra_link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01001940 return True
Ned Deilyd819b932013-09-06 01:07:05 -07001941
Victor Stinner625dbf22019-03-01 15:59:39 +01001942 def detect_tkinter_darwin(self):
Ned Deily1731d6d2020-05-18 04:32:38 -04001943 # Build default _tkinter on macOS using Tcl and Tk frameworks.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001944 # This method is meant to be invoked by detect_tkinter().
Ned Deily1731d6d2020-05-18 04:32:38 -04001945 #
1946 # The macOS native Tk (AKA Aqua Tk) and Tcl are most commonly
1947 # built and installed as macOS framework bundles. However,
1948 # for several reasons, we cannot take full advantage of the
1949 # Apple-supplied compiler chain's -framework options here.
1950 # Instead, we need to find and pass to the compiler the
1951 # absolute paths of the Tcl and Tk headers files we want to use
1952 # and the absolute path to the directory containing the Tcl
1953 # and Tk frameworks for linking.
1954 #
1955 # We want to handle here two common use cases on macOS:
1956 # 1. Build and link with system-wide third-party or user-built
1957 # Tcl and Tk frameworks installed in /Library/Frameworks.
1958 # 2. Build and link using a user-specified macOS SDK so that the
1959 # built Python can be exported to other systems. In this case,
1960 # search only the SDK's /Library/Frameworks (normally empty)
1961 # and /System/Library/Frameworks.
1962 #
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001963 # Any other use cases are handled either by detect_tkinter_fromenv(),
1964 # or detect_tkinter(). The former handles non-standard locations of
1965 # Tcl/Tk, defined via the _TCLTK_INCLUDES and _TCLTK_LIBS environment
1966 # variables. The latter handles any Tcl/Tk versions installed in
1967 # standard Unix directories.
1968 #
1969 # It would be desirable to also handle here the case where
Ned Deily1731d6d2020-05-18 04:32:38 -04001970 # you want to build and link with a framework build of Tcl and Tk
1971 # that is not in /Library/Frameworks, say, in your private
1972 # $HOME/Library/Frameworks directory or elsewhere. It turns
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301973 # out to be difficult to make that work automatically here
Ned Deily1731d6d2020-05-18 04:32:38 -04001974 # without bringing into play more tools and magic. That case
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301975 # can be handled using a recipe with the right arguments
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01001976 # to detect_tkinter_fromenv().
Ned Deily1731d6d2020-05-18 04:32:38 -04001977 #
1978 # Note also that the fallback case here is to try to use the
1979 # Apple-supplied Tcl and Tk frameworks in /System/Library but
1980 # be forewarned that they are deprecated by Apple and typically
1981 # out-of-date and buggy; their use should be avoided if at
1982 # all possible by installing a newer version of Tcl and Tk in
Manan Kumar Garg619f9802020-10-05 02:58:43 +05301983 # /Library/Frameworks before building Python without
Ned Deily1731d6d2020-05-18 04:32:38 -04001984 # an explicit SDK or by configuring build arguments explicitly.
1985
Jack Jansen0b06be72002-06-21 14:48:38 +00001986 from os.path import join, exists
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001987
Ned Deily1731d6d2020-05-18 04:32:38 -04001988 sysroot = macosx_sdk_root() # path to the SDK or '/'
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001989
Ned Deily1731d6d2020-05-18 04:32:38 -04001990 if macosx_sdk_specified():
1991 # Use case #2: an SDK other than '/' was specified.
1992 # Only search there.
1993 framework_dirs = [
1994 join(sysroot, 'Library', 'Frameworks'),
1995 join(sysroot, 'System', 'Library', 'Frameworks'),
1996 ]
1997 else:
1998 # Use case #1: no explicit SDK selected.
1999 # Search the local system-wide /Library/Frameworks,
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302000 # not the one in the default SDK, otherwise fall back to
Ned Deily1731d6d2020-05-18 04:32:38 -04002001 # /System/Library/Frameworks whose header files may be in
2002 # the default SDK or, on older systems, actually installed.
2003 framework_dirs = [
2004 join('/', 'Library', 'Frameworks'),
2005 join(sysroot, 'System', 'Library', 'Frameworks'),
2006 ]
2007
2008 # Find the directory that contains the Tcl.framework and
2009 # Tk.framework bundles.
Jack Jansen0b06be72002-06-21 14:48:38 +00002010 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00002011 # both Tcl.framework and Tk.framework should be present
Jack Jansen0b06be72002-06-21 14:48:38 +00002012 for fw in 'Tcl', 'Tk':
Ned Deily1731d6d2020-05-18 04:32:38 -04002013 if not exists(join(F, fw + '.framework')):
2014 break
Jack Jansen0b06be72002-06-21 14:48:38 +00002015 else:
Manan Kumar Garg619f9802020-10-05 02:58:43 +05302016 # ok, F is now directory with both frameworks. Continue
Jack Jansen0b06be72002-06-21 14:48:38 +00002017 # building
2018 break
2019 else:
2020 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
2021 # will now resume.
Victor Stinner4cbea512019-02-28 17:48:38 +01002022 return False
Tim Peters2c60f7a2003-01-29 03:49:43 +00002023
Jack Jansen0b06be72002-06-21 14:48:38 +00002024 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00002025 join(F, fw + '.framework', H)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002026 for fw in ('Tcl', 'Tk')
Ned Deily1731d6d2020-05-18 04:32:38 -04002027 for H in ('Headers',)
Jack Jansen0b06be72002-06-21 14:48:38 +00002028 ]
2029
Ned Deily1731d6d2020-05-18 04:32:38 -04002030 # Add the base framework directory as well
2031 compile_args = ['-F', F]
Jack Jansen0b06be72002-06-21 14:48:38 +00002032
Ned Deily1731d6d2020-05-18 04:32:38 -04002033 # Do not build tkinter for archs that this Tk was not built with.
Georg Brandlfcaf9102008-07-16 02:17:56 +00002034 cflags = sysconfig.get_config_vars('CFLAGS')[0]
R David Murray44b548d2016-09-08 13:59:53 -04002035 archs = re.findall(r'-arch\s+(\w+)', cflags)
Georg Brandlfcaf9102008-07-16 02:17:56 +00002036
Ronald Oussorend097efe2009-09-15 19:07:58 +00002037 tmpfile = os.path.join(self.build_temp, 'tk.arch')
2038 if not os.path.exists(self.build_temp):
2039 os.makedirs(self.build_temp)
2040
Ned Deily1731d6d2020-05-18 04:32:38 -04002041 run_command(
2042 "file {}/Tk.framework/Tk | grep 'for architecture' > {}".format(F, tmpfile)
2043 )
Brett Cannon9f5db072010-10-29 20:19:27 +00002044 with open(tmpfile) as fp:
2045 detected_archs = []
2046 for ln in fp:
2047 a = ln.split()[-1]
2048 if a in archs:
2049 detected_archs.append(ln.split()[-1])
Ronald Oussorend097efe2009-09-15 19:07:58 +00002050 os.unlink(tmpfile)
2051
Ned Deily1731d6d2020-05-18 04:32:38 -04002052 arch_args = []
Ronald Oussorend097efe2009-09-15 19:07:58 +00002053 for a in detected_archs:
Ned Deily1731d6d2020-05-18 04:32:38 -04002054 arch_args.append('-arch')
2055 arch_args.append(a)
2056
2057 compile_args += arch_args
2058 link_args = [','.join(['-Wl', '-F', F, '-framework', 'Tcl', '-framework', 'Tk']), *arch_args]
2059
2060 # The X11/xlib.h file bundled in the Tk sources can cause function
2061 # prototype warnings from the compiler. Since we cannot easily fix
2062 # that, suppress the warnings here instead.
2063 if '-Wstrict-prototypes' in cflags.split():
2064 compile_args.append('-Wno-strict-prototypes')
Georg Brandlfcaf9102008-07-16 02:17:56 +00002065
Victor Stinnercfe172d2019-03-01 18:21:49 +01002066 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2067 define_macros=[('WITH_APPINIT', 1)],
2068 include_dirs=include_dirs,
2069 libraries=[],
Ned Deily1731d6d2020-05-18 04:32:38 -04002070 extra_compile_args=compile_args,
2071 extra_link_args=link_args))
Victor Stinner4cbea512019-02-28 17:48:38 +01002072 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002073
Victor Stinner625dbf22019-03-01 15:59:39 +01002074 def detect_tkinter(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002075 # The _tkinter module.
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002076 #
2077 # Detection of Tcl/Tk is attempted in the following order:
2078 # - Through environment variables.
2079 # - Platform specific detection of Tcl/Tk (currently only macOS).
2080 # - Search of various standard Unix header/library paths.
2081 #
2082 # Detection stops at the first successful method.
Michael W. Hudson5b109102002-01-23 15:04:41 +00002083
Manolis Stamatogiannakisd2027942021-03-01 04:29:57 +01002084 # Check for Tcl and Tk at the locations indicated by _TCLTK_INCLUDES
2085 # and _TCLTK_LIBS environment variables.
2086 if self.detect_tkinter_fromenv():
Victor Stinner5ec33a12019-03-01 16:43:28 +01002087 return True
Ned Deilyd819b932013-09-06 01:07:05 -07002088
Jack Jansen0b06be72002-06-21 14:48:38 +00002089 # Rather than complicate the code below, detecting and building
2090 # AquaTk is a separate method. Only one Tkinter will be built on
2091 # Darwin - either AquaTk, if it is found, or X11 based Tk.
Victor Stinner5ec33a12019-03-01 16:43:28 +01002092 if (MACOS and self.detect_tkinter_darwin()):
2093 return True
Jack Jansen0b06be72002-06-21 14:48:38 +00002094
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002095 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002096 # The versions with dots are used on Unix, and the versions without
2097 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002098 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polo5d377bd2009-08-16 14:44:14 +00002099 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
2100 '8.2', '82', '8.1', '81', '8.0', '80']:
Victor Stinner625dbf22019-03-01 15:59:39 +01002101 tklib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002102 'tk' + version)
Victor Stinner625dbf22019-03-01 15:59:39 +01002103 tcllib = self.compiler.find_library_file(self.lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002104 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00002105 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002106 # Exit the loop when we've found the Tcl/Tk libraries
2107 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002108
Fredrik Lundhade711a2001-01-24 08:00:28 +00002109 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002110 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002111 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002112 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002113 dotversion = version
Victor Stinner4cbea512019-02-28 17:48:38 +01002114 if '.' not in dotversion and "bsd" in HOST_PLATFORM.lower():
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002115 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
2116 # but the include subdirs are named like .../include/tcl8.3.
2117 dotversion = dotversion[:-1] + '.' + dotversion[-1]
2118 tcl_include_sub = []
2119 tk_include_sub = []
Victor Stinner625dbf22019-03-01 15:59:39 +01002120 for dir in self.inc_dirs:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002121 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
2122 tk_include_sub += [dir + os.sep + "tk" + dotversion]
2123 tk_include_sub += tcl_include_sub
Victor Stinner625dbf22019-03-01 15:59:39 +01002124 tcl_includes = find_file('tcl.h', self.inc_dirs, tcl_include_sub)
2125 tk_includes = find_file('tk.h', self.inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002126
Martin v. Löwise86a59a2003-05-03 08:45:51 +00002127 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002128 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00002129 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Victor Stinner5ec33a12019-03-01 16:43:28 +01002130 return False
Fredrik Lundhade711a2001-01-24 08:00:28 +00002131
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002132 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002133
Victor Stinnercfe172d2019-03-01 18:21:49 +01002134 include_dirs = []
2135 libs = []
2136 defs = []
2137 added_lib_dirs = []
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002138 for dir in tcl_includes + tk_includes:
2139 if dir not in include_dirs:
2140 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002141
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002142 # Check for various platform-specific directories
Victor Stinner4cbea512019-02-28 17:48:38 +01002143 if HOST_PLATFORM == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002144 include_dirs.append('/usr/openwin/include')
2145 added_lib_dirs.append('/usr/openwin/lib')
2146 elif os.path.exists('/usr/X11R6/include'):
2147 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00002148 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002149 added_lib_dirs.append('/usr/X11R6/lib')
2150 elif os.path.exists('/usr/X11R5/include'):
2151 include_dirs.append('/usr/X11R5/include')
2152 added_lib_dirs.append('/usr/X11R5/lib')
2153 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002154 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002155 include_dirs.append('/usr/X11/include')
2156 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002157
Jason Tishler9181c942003-02-05 15:16:17 +00002158 # If Cygwin, then verify that X is installed before proceeding
Victor Stinner4cbea512019-02-28 17:48:38 +01002159 if CYGWIN:
Jason Tishler9181c942003-02-05 15:16:17 +00002160 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
2161 if x11_inc is None:
Victor Stinner5ec33a12019-03-01 16:43:28 +01002162 return False
Jason Tishler9181c942003-02-05 15:16:17 +00002163
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002164 # Check for BLT extension
Victor Stinner625dbf22019-03-01 15:59:39 +01002165 if self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002166 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002167 defs.append( ('WITH_BLT', 1) )
2168 libs.append('BLT8.0')
Victor Stinner625dbf22019-03-01 15:59:39 +01002169 elif self.compiler.find_library_file(self.lib_dirs + added_lib_dirs,
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00002170 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00002171 defs.append( ('WITH_BLT', 1) )
2172 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002173
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002174 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00002175 libs.append('tk'+ version)
2176 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00002177
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002178 # Finally, link with the X11 libraries (not appropriate on cygwin)
Victor Stinner4cbea512019-02-28 17:48:38 +01002179 if not CYGWIN:
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00002180 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002181
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00002182 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002183 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002184 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002185 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002186 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002187 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00002188 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002189
Victor Stinnercfe172d2019-03-01 18:21:49 +01002190 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
2191 define_macros=[('WITH_APPINIT', 1)] + defs,
2192 include_dirs=include_dirs,
2193 libraries=libs,
2194 library_dirs=added_lib_dirs))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002195 return True
2196
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002197 def configure_ctypes(self, ext):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002198 return True
2199
Victor Stinner625dbf22019-03-01 15:59:39 +01002200 def detect_ctypes(self):
Victor Stinner5ec33a12019-03-01 16:43:28 +01002201 # Thomas Heller's _ctypes module
Ronald Oussoren41761932020-11-08 10:05:27 +01002202
2203 if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
2204 self.use_system_libffi = True
2205 else:
2206 self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
2207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208 include_dirs = []
Victor Stinner1ae035b2020-04-17 17:47:20 +02002209 extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002210 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00002211 sources = ['_ctypes/_ctypes.c',
2212 '_ctypes/callbacks.c',
2213 '_ctypes/callproc.c',
2214 '_ctypes/stgdict.c',
Thomas Heller864cc672010-08-08 17:58:53 +00002215 '_ctypes/cfield.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00002216 depends = ['_ctypes/ctypes.h']
2217
Victor Stinner4cbea512019-02-28 17:48:38 +01002218 if MACOS:
Ronald Oussoren2decf222010-09-05 18:25:59 +00002219 sources.append('_ctypes/malloc_closure.c')
Ronald Oussoren41761932020-11-08 10:05:27 +01002220 extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
Christian Heimes78644762008-03-04 23:39:23 +00002221 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00002222 include_dirs.append('_ctypes/darwin')
Thomas Hellercf567c12006-03-08 19:51:58 +00002223
Victor Stinner4cbea512019-02-28 17:48:38 +01002224 elif HOST_PLATFORM == 'sunos5':
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002225 # XXX This shouldn't be necessary; it appears that some
2226 # of the assembler code is non-PIC (i.e. it has relocations
2227 # when it shouldn't. The proper fix would be to rewrite
2228 # the assembler code to be PIC.
2229 # This only works with GCC; the Sun compiler likely refuses
2230 # this option. If you want to compile ctypes with the Sun
2231 # compiler, please research a proper solution, instead of
2232 # finding some -z option for the Sun compiler.
2233 extra_link_args.append('-mimpure-text')
2234
Victor Stinner4cbea512019-02-28 17:48:38 +01002235 elif HOST_PLATFORM.startswith('hp-ux'):
Thomas Heller3eaaeb42008-05-23 17:26:46 +00002236 extra_link_args.append('-fPIC')
2237
Thomas Hellercf567c12006-03-08 19:51:58 +00002238 ext = Extension('_ctypes',
2239 include_dirs=include_dirs,
2240 extra_compile_args=extra_compile_args,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002241 extra_link_args=extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00002243 sources=sources,
2244 depends=depends)
Victor Stinnercfe172d2019-03-01 18:21:49 +01002245 self.add(ext)
2246 if TEST_EXTENSIONS:
2247 # function my_sqrt() needs libm for sqrt()
2248 self.add(Extension('_ctypes_test',
2249 sources=['_ctypes/_ctypes_test.c'],
2250 libraries=['m']))
Thomas Hellercf567c12006-03-08 19:51:58 +00002251
Ronald Oussoren41761932020-11-08 10:05:27 +01002252 ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
2253 ffi_lib = None
2254
Victor Stinner625dbf22019-03-01 15:59:39 +01002255 ffi_inc_dirs = self.inc_dirs.copy()
Victor Stinner4cbea512019-02-28 17:48:38 +01002256 if MACOS:
Ronald Oussoren41761932020-11-08 10:05:27 +01002257 ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
Christian Heimes78644762008-03-04 23:39:23 +00002258
Ronald Oussoren41761932020-11-08 10:05:27 +01002259 if not ffi_inc:
2260 if os.path.exists(ffi_in_sdk):
2261 ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
2262 ffi_inc = ffi_in_sdk
2263 ffi_lib = 'ffi'
2264 else:
2265 # OS X 10.5 comes with libffi.dylib; the include files are
2266 # in /usr/include/ffi
2267 ffi_inc_dirs.append('/usr/include/ffi')
2268
2269 if not ffi_inc:
2270 found = find_file('ffi.h', [], ffi_inc_dirs)
2271 if found:
2272 ffi_inc = found[0]
2273 if ffi_inc:
2274 ffi_h = ffi_inc + '/ffi.h'
Shlomi Fish6d51b872017-09-06 23:19:19 +03002275 if not os.path.exists(ffi_h):
2276 ffi_inc = None
2277 print('Header file {} does not exist'.format(ffi_h))
Ronald Oussoren41761932020-11-08 10:05:27 +01002278 if ffi_lib is None and ffi_inc:
doko@ubuntu.comae683652016-06-05 01:38:29 +02002279 for lib_name in ('ffi', 'ffi_pic'):
Victor Stinner625dbf22019-03-01 15:59:39 +01002280 if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002281 ffi_lib = lib_name
2282 break
2283
2284 if ffi_inc and ffi_lib:
Ronald Oussoren41761932020-11-08 10:05:27 +01002285 ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
2286 if grep_headers_for('ffi_prep_cif_var', ffi_headers):
2287 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
2288 if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
2289 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
2290 if grep_headers_for('ffi_closure_alloc', ffi_headers):
2291 ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
2292
2293 ext.include_dirs.append(ffi_inc)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 ext.libraries.append(ffi_lib)
2295 self.use_system_libffi = True
2296
Christian Heimes5bb96922018-02-25 10:22:14 +01002297 if sysconfig.get_config_var('HAVE_LIBDL'):
2298 # for dlopen, see bpo-32647
2299 ext.libraries.append('dl')
2300
Victor Stinner5ec33a12019-03-01 16:43:28 +01002301 def detect_decimal(self):
2302 # Stefan Krah's _decimal module
Stefan Krah60187b52012-03-23 19:06:27 +01002303 extra_compile_args = []
Stefan Kraha10e2fb2012-09-01 14:21:22 +02002304 undef_macros = []
Stefan Krah60187b52012-03-23 19:06:27 +01002305 if '--with-system-libmpdec' in sysconfig.get_config_var("CONFIG_ARGS"):
2306 include_dirs = []
Antoine Pitrou73b20ae2021-03-30 18:11:06 +02002307 libraries = ['mpdec']
Stefan Krah60187b52012-03-23 19:06:27 +01002308 sources = ['_decimal/_decimal.c']
2309 depends = ['_decimal/docstrings.h']
2310 else:
Victor Stinner625dbf22019-03-01 15:59:39 +01002311 include_dirs = [os.path.abspath(os.path.join(self.srcdir,
Ned Deily458a6fb2012-04-01 02:30:46 -07002312 'Modules',
2313 '_decimal',
2314 'libmpdec'))]
Stefan Krahbd4ed772017-12-06 18:24:17 +01002315 libraries = ['m']
Stefan Krah60187b52012-03-23 19:06:27 +01002316 sources = [
2317 '_decimal/_decimal.c',
2318 '_decimal/libmpdec/basearith.c',
2319 '_decimal/libmpdec/constants.c',
2320 '_decimal/libmpdec/context.c',
2321 '_decimal/libmpdec/convolute.c',
2322 '_decimal/libmpdec/crt.c',
2323 '_decimal/libmpdec/difradix2.c',
2324 '_decimal/libmpdec/fnt.c',
2325 '_decimal/libmpdec/fourstep.c',
2326 '_decimal/libmpdec/io.c',
Stefan Krahf117d872019-07-10 18:27:38 +02002327 '_decimal/libmpdec/mpalloc.c',
Stefan Krah60187b52012-03-23 19:06:27 +01002328 '_decimal/libmpdec/mpdecimal.c',
2329 '_decimal/libmpdec/numbertheory.c',
2330 '_decimal/libmpdec/sixstep.c',
2331 '_decimal/libmpdec/transpose.c',
2332 ]
2333 depends = [
2334 '_decimal/docstrings.h',
2335 '_decimal/libmpdec/basearith.h',
2336 '_decimal/libmpdec/bits.h',
2337 '_decimal/libmpdec/constants.h',
2338 '_decimal/libmpdec/convolute.h',
2339 '_decimal/libmpdec/crt.h',
2340 '_decimal/libmpdec/difradix2.h',
2341 '_decimal/libmpdec/fnt.h',
2342 '_decimal/libmpdec/fourstep.h',
2343 '_decimal/libmpdec/io.h',
Stefan Krah8d013a82016-04-26 16:34:41 +02002344 '_decimal/libmpdec/mpalloc.h',
Stefan Krah60187b52012-03-23 19:06:27 +01002345 '_decimal/libmpdec/mpdecimal.h',
2346 '_decimal/libmpdec/numbertheory.h',
2347 '_decimal/libmpdec/sixstep.h',
2348 '_decimal/libmpdec/transpose.h',
2349 '_decimal/libmpdec/typearith.h',
2350 '_decimal/libmpdec/umodarith.h',
2351 ]
2352
Stefan Krah1919b7e2012-03-21 18:25:23 +01002353 config = {
2354 'x64': [('CONFIG_64','1'), ('ASM','1')],
2355 'uint128': [('CONFIG_64','1'), ('ANSI','1'), ('HAVE_UINT128_T','1')],
2356 'ansi64': [('CONFIG_64','1'), ('ANSI','1')],
2357 'ppro': [('CONFIG_32','1'), ('PPRO','1'), ('ASM','1')],
2358 'ansi32': [('CONFIG_32','1'), ('ANSI','1')],
2359 'ansi-legacy': [('CONFIG_32','1'), ('ANSI','1'),
2360 ('LEGACY_COMPILER','1')],
2361 'universal': [('UNIVERSAL','1')]
2362 }
2363
Stefan Krah1919b7e2012-03-21 18:25:23 +01002364 cc = sysconfig.get_config_var('CC')
2365 sizeof_size_t = sysconfig.get_config_var('SIZEOF_SIZE_T')
2366 machine = os.environ.get('PYTHON_DECIMAL_WITH_MACHINE')
2367
2368 if machine:
2369 # Override automatic configuration to facilitate testing.
2370 define_macros = config[machine]
Victor Stinner4cbea512019-02-28 17:48:38 +01002371 elif MACOS:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002372 # Universal here means: build with the same options Python
2373 # was built with.
2374 define_macros = config['universal']
2375 elif sizeof_size_t == 8:
2376 if sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X64'):
2377 define_macros = config['x64']
2378 elif sysconfig.get_config_var('HAVE_GCC_UINT128_T'):
2379 define_macros = config['uint128']
2380 else:
2381 define_macros = config['ansi64']
2382 elif sizeof_size_t == 4:
2383 ppro = sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X87')
2384 if ppro and ('gcc' in cc or 'clang' in cc) and \
Victor Stinner4cbea512019-02-28 17:48:38 +01002385 not 'sunos' in HOST_PLATFORM:
Stefan Krah1919b7e2012-03-21 18:25:23 +01002386 # solaris: problems with register allocation.
2387 # icc >= 11.0 works as well.
2388 define_macros = config['ppro']
Stefan Krahce23dbc2012-09-30 21:12:53 +02002389 extra_compile_args.append('-Wno-unknown-pragmas')
Stefan Krah1919b7e2012-03-21 18:25:23 +01002390 else:
2391 define_macros = config['ansi32']
2392 else:
2393 raise DistutilsError("_decimal: unsupported architecture")
2394
2395 # Workarounds for toolchain bugs:
2396 if sysconfig.get_config_var('HAVE_IPA_PURE_CONST_BUG'):
2397 # Some versions of gcc miscompile inline asm:
2398 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46491
2399 # http://gcc.gnu.org/ml/gcc/2010-11/msg00366.html
2400 extra_compile_args.append('-fno-ipa-pure-const')
2401 if sysconfig.get_config_var('HAVE_GLIBC_MEMMOVE_BUG'):
2402 # _FORTIFY_SOURCE wrappers for memmove and bcopy are incorrect:
2403 # http://sourceware.org/ml/libc-alpha/2010-12/msg00009.html
2404 undef_macros.append('_FORTIFY_SOURCE')
2405
Stefan Krah1919b7e2012-03-21 18:25:23 +01002406 # Uncomment for extra functionality:
2407 #define_macros.append(('EXTRA_FUNCTIONALITY', 1))
Victor Stinner8058bda2019-03-01 15:31:45 +01002408 self.add(Extension('_decimal',
2409 include_dirs=include_dirs,
2410 libraries=libraries,
2411 define_macros=define_macros,
2412 undef_macros=undef_macros,
2413 extra_compile_args=extra_compile_args,
2414 sources=sources,
2415 depends=depends))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002416
Victor Stinner5ec33a12019-03-01 16:43:28 +01002417 def detect_openssl_hashlib(self):
2418 # Detect SSL support for the socket module (via _ssl)
Christian Heimesff5be6e2018-01-20 13:19:21 +01002419 config_vars = sysconfig.get_config_vars()
2420
2421 def split_var(name, sep):
2422 # poor man's shlex, the re module is not available yet.
2423 value = config_vars.get(name)
2424 if not value:
2425 return ()
2426 # This trick works because ax_check_openssl uses --libs-only-L,
2427 # --libs-only-l, and --cflags-only-I.
2428 value = ' ' + value
2429 sep = ' ' + sep
2430 return [v.strip() for v in value.split(sep) if v.strip()]
2431
2432 openssl_includes = split_var('OPENSSL_INCLUDES', '-I')
2433 openssl_libdirs = split_var('OPENSSL_LDFLAGS', '-L')
2434 openssl_libs = split_var('OPENSSL_LIBS', '-l')
Christian Heimes32eba612021-03-19 10:29:25 +01002435 openssl_rpath = config_vars.get('OPENSSL_RPATH')
Christian Heimesff5be6e2018-01-20 13:19:21 +01002436 if not openssl_libs:
2437 # libssl and libcrypto not found
Christian Heimes8abc3f42019-04-09 18:40:12 +02002438 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002439 return None, None
2440
2441 # Find OpenSSL includes
2442 ssl_incs = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002443 'openssl/ssl.h', self.inc_dirs, openssl_includes
Christian Heimesff5be6e2018-01-20 13:19:21 +01002444 )
2445 if ssl_incs is None:
Christian Heimes8abc3f42019-04-09 18:40:12 +02002446 self.missing.extend(['_ssl', '_hashlib'])
Christian Heimesff5be6e2018-01-20 13:19:21 +01002447 return None, None
2448
Christian Heimes32eba612021-03-19 10:29:25 +01002449 if openssl_rpath == 'auto':
2450 runtime_library_dirs = openssl_libdirs[:]
2451 elif not openssl_rpath:
2452 runtime_library_dirs = []
2453 else:
2454 runtime_library_dirs = [openssl_rpath]
2455
Christian Heimesbacefbf2021-03-27 18:03:54 +01002456 openssl_extension_kwargs = dict(
2457 include_dirs=openssl_includes,
2458 library_dirs=openssl_libdirs,
2459 libraries=openssl_libs,
2460 runtime_library_dirs=runtime_library_dirs,
2461 )
2462
2463 # This static linking is NOT OFFICIALLY SUPPORTED.
2464 # Requires static OpenSSL build with position-independent code. Some
2465 # features like DSO engines or external OSSL providers don't work.
2466 # Only tested on GCC and clang on X86_64.
2467 if os.environ.get("PY_UNSUPPORTED_OPENSSL_BUILD") == "static":
2468 extra_linker_args = []
2469 for lib in openssl_extension_kwargs["libraries"]:
2470 # link statically
2471 extra_linker_args.append(f"-l:lib{lib}.a")
2472 # don't export symbols
2473 extra_linker_args.append(f"-Wl,--exclude-libs,lib{lib}.a")
2474 openssl_extension_kwargs["extra_link_args"] = extra_linker_args
2475 # don't link OpenSSL shared libraries.
Christian Heimes5f879152021-04-26 15:13:34 +02002476 # include libz for OpenSSL build flavors with compression support
2477 openssl_extension_kwargs["libraries"] = ["z"]
Christian Heimesbacefbf2021-03-27 18:03:54 +01002478
Christian Heimes39258d32021-04-17 11:36:35 +02002479 self.add(
2480 Extension(
2481 '_ssl',
2482 ['_ssl.c'],
Christian Heimes666991f2021-04-26 15:01:40 +02002483 depends=[
2484 'socketmodule.h',
2485 '_ssl.h',
2486 '_ssl/debughelpers.c',
2487 '_ssl/misc.c',
2488 '_ssl/cert.c',
2489 ],
Christian Heimes39258d32021-04-17 11:36:35 +02002490 **openssl_extension_kwargs
Christian Heimesc7f70692019-05-31 11:44:05 +02002491 )
Christian Heimes39258d32021-04-17 11:36:35 +02002492 )
Christian Heimesbacefbf2021-03-27 18:03:54 +01002493 self.add(
2494 Extension(
2495 '_hashlib',
2496 ['_hashopenssl.c'],
2497 depends=['hashlib.h'],
2498 **openssl_extension_kwargs,
2499 )
2500 )
Christian Heimesff5be6e2018-01-20 13:19:21 +01002501
xdegaye2ee077f2019-04-09 17:20:08 +02002502 def detect_hash_builtins(self):
Christian Heimes9b60e552020-05-15 23:54:53 +02002503 # By default we always compile these even when OpenSSL is available
2504 # (issue #14693). It's harmless and the object code is tiny
2505 # (40-50 KiB per module, only loaded when actually used). Modules can
2506 # be disabled via the --with-builtin-hashlib-hashes configure flag.
2507 supported = {"md5", "sha1", "sha256", "sha512", "sha3", "blake2"}
Victor Stinner5ec33a12019-03-01 16:43:28 +01002508
Christian Heimes9b60e552020-05-15 23:54:53 +02002509 configured = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
2510 configured = configured.strip('"').lower()
2511 configured = {
2512 m.strip() for m in configured.split(",")
2513 }
Victor Stinner5ec33a12019-03-01 16:43:28 +01002514
Christian Heimes9b60e552020-05-15 23:54:53 +02002515 self.disabled_configure.extend(
2516 sorted(supported.difference(configured))
2517 )
Victor Stinner5ec33a12019-03-01 16:43:28 +01002518
Christian Heimes9b60e552020-05-15 23:54:53 +02002519 if "sha256" in configured:
2520 self.add(Extension(
2521 '_sha256', ['sha256module.c'],
2522 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2523 depends=['hashlib.h']
2524 ))
2525
2526 if "sha512" in configured:
2527 self.add(Extension(
2528 '_sha512', ['sha512module.c'],
2529 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
2530 depends=['hashlib.h']
2531 ))
2532
2533 if "md5" in configured:
2534 self.add(Extension(
2535 '_md5', ['md5module.c'],
2536 depends=['hashlib.h']
2537 ))
2538
2539 if "sha1" in configured:
2540 self.add(Extension(
2541 '_sha1', ['sha1module.c'],
2542 depends=['hashlib.h']
2543 ))
2544
2545 if "blake2" in configured:
2546 blake2_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002547 os.path.join(escape(self.srcdir), 'Modules/_blake2/impl/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002548 )
2549 blake2_deps.append('hashlib.h')
2550 self.add(Extension(
2551 '_blake2',
2552 [
2553 '_blake2/blake2module.c',
2554 '_blake2/blake2b_impl.c',
2555 '_blake2/blake2s_impl.c'
2556 ],
2557 depends=blake2_deps
2558 ))
2559
2560 if "sha3" in configured:
2561 sha3_deps = glob(
Serhiy Storchaka93558682020-06-20 11:10:31 +03002562 os.path.join(escape(self.srcdir), 'Modules/_sha3/kcp/*')
Christian Heimes9b60e552020-05-15 23:54:53 +02002563 )
2564 sha3_deps.append('hashlib.h')
2565 self.add(Extension(
2566 '_sha3',
2567 ['_sha3/sha3module.c'],
2568 depends=sha3_deps
2569 ))
Victor Stinner5ec33a12019-03-01 16:43:28 +01002570
2571 def detect_nis(self):
Victor Stinner4cbea512019-02-28 17:48:38 +01002572 if MS_WINDOWS or CYGWIN or HOST_PLATFORM == 'qnx6':
Victor Stinner8058bda2019-03-01 15:31:45 +01002573 self.missing.append('nis')
2574 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002575
2576 libs = []
2577 library_dirs = []
2578 includes_dirs = []
2579
2580 # bpo-32521: glibc has deprecated Sun RPC for some time. Fedora 28
2581 # moved headers and libraries to libtirpc and libnsl. The headers
2582 # are in tircp and nsl sub directories.
2583 rpcsvc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002584 'rpcsvc/yp_prot.h', self.inc_dirs,
2585 [os.path.join(inc_dir, 'nsl') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002586 )
2587 rpc_inc = find_file(
Victor Stinner625dbf22019-03-01 15:59:39 +01002588 'rpc/rpc.h', self.inc_dirs,
2589 [os.path.join(inc_dir, 'tirpc') for inc_dir in self.inc_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002590 )
2591 if rpcsvc_inc is None or rpc_inc is None:
2592 # not found
Victor Stinner8058bda2019-03-01 15:31:45 +01002593 self.missing.append('nis')
2594 return
Christian Heimes29a7df72018-01-26 23:28:46 +01002595 includes_dirs.extend(rpcsvc_inc)
2596 includes_dirs.extend(rpc_inc)
2597
Victor Stinner625dbf22019-03-01 15:59:39 +01002598 if self.compiler.find_library_file(self.lib_dirs, 'nsl'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002599 libs.append('nsl')
2600 else:
2601 # libnsl-devel: check for libnsl in nsl/ subdirectory
Victor Stinner625dbf22019-03-01 15:59:39 +01002602 nsl_dirs = [os.path.join(lib_dir, 'nsl') for lib_dir in self.lib_dirs]
Christian Heimes29a7df72018-01-26 23:28:46 +01002603 libnsl = self.compiler.find_library_file(nsl_dirs, 'nsl')
2604 if libnsl is not None:
2605 library_dirs.append(os.path.dirname(libnsl))
2606 libs.append('nsl')
2607
Victor Stinner625dbf22019-03-01 15:59:39 +01002608 if self.compiler.find_library_file(self.lib_dirs, 'tirpc'):
Christian Heimes29a7df72018-01-26 23:28:46 +01002609 libs.append('tirpc')
2610
Victor Stinner8058bda2019-03-01 15:31:45 +01002611 self.add(Extension('nis', ['nismodule.c'],
2612 libraries=libs,
2613 library_dirs=library_dirs,
2614 include_dirs=includes_dirs))
Christian Heimes29a7df72018-01-26 23:28:46 +01002615
Christian Heimesff5be6e2018-01-20 13:19:21 +01002616
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00002617class PyBuildInstall(install):
2618 # Suppress the warning about installation into the lib_dynload
2619 # directory, which is not in sys.path when running Python during
2620 # installation:
2621 def initialize_options (self):
2622 install.initialize_options(self)
2623 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00002624
Éric Araujoe6792c12011-06-09 14:07:02 +02002625 # Customize subcommands to not install an egg-info file for Python
2626 sub_commands = [('install_lib', install.has_lib),
2627 ('install_headers', install.has_headers),
2628 ('install_scripts', install.has_scripts),
2629 ('install_data', install.has_data)]
2630
2631
Michael W. Hudson529a5052002-12-17 16:47:17 +00002632class PyBuildInstallLib(install_lib):
2633 # Do exactly what install_lib does but make sure correct access modes get
2634 # set on installed directories and files. All installed files with get
2635 # mode 644 unless they are a shared library in which case they will get
2636 # mode 755. All installed directories will get mode 755.
2637
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002638 # this is works for EXT_SUFFIX too, which ends with SHLIB_SUFFIX
2639 shlib_suffix = sysconfig.get_config_var("SHLIB_SUFFIX")
Michael W. Hudson529a5052002-12-17 16:47:17 +00002640
2641 def install(self):
2642 outfiles = install_lib.install(self)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002643 self.set_file_modes(outfiles, 0o644, 0o755)
2644 self.set_dir_modes(self.install_dir, 0o755)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002645 return outfiles
2646
2647 def set_file_modes(self, files, defaultMode, sharedLibMode):
Michael W. Hudson529a5052002-12-17 16:47:17 +00002648 if not files: return
2649
2650 for filename in files:
2651 if os.path.islink(filename): continue
2652 mode = defaultMode
doko@ubuntu.comd5537d02013-03-21 13:21:49 -07002653 if filename.endswith(self.shlib_suffix): mode = sharedLibMode
Michael W. Hudson529a5052002-12-17 16:47:17 +00002654 log.info("changing mode of %s to %o", filename, mode)
2655 if not self.dry_run: os.chmod(filename, mode)
2656
2657 def set_dir_modes(self, dirname, mode):
Amaury Forgeot d'Arc321e5332009-07-02 23:08:45 +00002658 for dirpath, dirnames, fnames in os.walk(dirname):
2659 if os.path.islink(dirpath):
2660 continue
2661 log.info("changing mode of %s to %o", dirpath, mode)
2662 if not self.dry_run: os.chmod(dirpath, mode)
Michael W. Hudson529a5052002-12-17 16:47:17 +00002663
Victor Stinnerc991f242019-03-01 17:19:04 +01002664
Georg Brandlff52f762010-12-28 09:51:43 +00002665class PyBuildScripts(build_scripts):
2666 def copy_scripts(self):
2667 outfiles, updated_files = build_scripts.copy_scripts(self)
2668 fullversion = '-{0[0]}.{0[1]}'.format(sys.version_info)
2669 minoronly = '.{0[1]}'.format(sys.version_info)
2670 newoutfiles = []
2671 newupdated_files = []
2672 for filename in outfiles:
Brett Cannona8c34242018-04-20 14:15:40 -07002673 if filename.endswith('2to3'):
Georg Brandlff52f762010-12-28 09:51:43 +00002674 newfilename = filename + fullversion
2675 else:
2676 newfilename = filename + minoronly
Miss Islington (bot)956f1fc2021-07-01 19:05:11 -07002677 log.info(f'renaming {filename} to {newfilename}')
Georg Brandlff52f762010-12-28 09:51:43 +00002678 os.rename(filename, newfilename)
2679 newoutfiles.append(newfilename)
2680 if filename in updated_files:
2681 newupdated_files.append(newfilename)
2682 return newoutfiles, newupdated_files
2683
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002684
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002685def main():
Victor Stinnercad80202021-01-19 23:04:49 +01002686 global LIST_MODULE_NAMES
2687
2688 if "--list-module-names" in sys.argv:
2689 LIST_MODULE_NAMES = True
2690 sys.argv.remove("--list-module-names")
2691
Victor Stinnerc991f242019-03-01 17:19:04 +01002692 set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
2693 set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
2694
2695 class DummyProcess:
2696 """Hack for parallel build"""
2697 ProcessPoolExecutor = None
2698
2699 sys.modules['concurrent.futures.process'] = DummyProcess
Paul Ganssle62972d92020-05-16 04:20:06 -04002700 validate_tzpath()
Victor Stinnerc991f242019-03-01 17:19:04 +01002701
Andrew M. Kuchling62686692001-05-21 20:48:09 +00002702 # turn off warnings when deprecated modules are imported
2703 import warnings
2704 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002705 setup(# PyPI Metadata (PEP 301)
2706 name = "Python",
2707 version = sys.version.split()[0],
Serhiy Storchaka885bdc42016-02-11 13:10:36 +02002708 url = "http://www.python.org/%d.%d" % sys.version_info[:2],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002709 maintainer = "Guido van Rossum and the Python community",
2710 maintainer_email = "python-dev@python.org",
2711 description = "A high-level object-oriented programming language",
2712 long_description = SUMMARY.strip(),
2713 license = "PSF license",
Guido van Rossumc1f779c2007-07-03 08:25:58 +00002714 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002715 platforms = ["Many"],
2716
2717 # Build info
Georg Brandlff52f762010-12-28 09:51:43 +00002718 cmdclass = {'build_ext': PyBuildExt,
2719 'build_scripts': PyBuildScripts,
2720 'install': PyBuildInstall,
2721 'install_lib': PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002722 # The struct module is defined here, because build_ext won't be
2723 # called unless there's at least one extension module defined.
Victor Stinnercdad2722021-04-22 00:52:52 +02002724 ext_modules=[Extension('_struct', ['_struct.c'],
2725 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00002726
Georg Brandlff52f762010-12-28 09:51:43 +00002727 # If you change the scripts installed here, you also need to
2728 # check the PyBuildScripts command above, and change the links
2729 # created by the bininstall target in Makefile.pre.in
Benjamin Petersondfea1922009-05-23 17:13:14 +00002730 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
Brett Cannona8c34242018-04-20 14:15:40 -07002731 "Tools/scripts/2to3"]
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002732 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00002733
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002734# --install-platlib
2735if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002736 main()