1# Autodetecting setup.py script for building the Python extensions 2 3import argparse 4import importlib._bootstrap 5import importlib.machinery 6import importlib.util 7import logging 8import os 9import re 10import sys 11import sysconfig 12import warnings 13from glob import glob, escape 14import _osx_support 15 16 17try: 18 import subprocess 19 del subprocess 20 SUBPROCESS_BOOTSTRAP = False 21except ImportError: 22 # Bootstrap Python: distutils.spawn uses subprocess to build C extensions, 23 # subprocess requires C extensions built by setup.py like _posixsubprocess. 24 # 25 # Use _bootsubprocess which only uses the os module. 26 # 27 # It is dropped from sys.modules as soon as all C extension modules 28 # are built. 29 import _bootsubprocess 30 sys.modules['subprocess'] = _bootsubprocess 31 del _bootsubprocess 32 SUBPROCESS_BOOTSTRAP = True 33 34 35with warnings.catch_warnings(): 36 # bpo-41282 (PEP 632) deprecated distutils but setup.py still uses it 37 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 ) 47 48 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 55 56 57# Compile extensions used to test Python? 58TEST_EXTENSIONS = (sysconfig.get_config_var('TEST_MODULES') == 'yes') 59 60# This global variable is used to hold the list of modules to be disabled. 61DISABLED_MODULE_LIST = [] 62 63# --list-module-names option used by Tools/scripts/generate_module_names.py 64LIST_MODULE_NAMES = False 65 66 67logging.basicConfig(format='%(message)s', level=logging.INFO) 68log = logging.getLogger('setup') 69 70 71def get_platform(): 72 # Cross compiling 73 if "_PYTHON_HOST_PLATFORM" in os.environ: 74 return os.environ["_PYTHON_HOST_PLATFORM"] 75 76 # Get value of sys.platform 77 if sys.platform.startswith('osf1'): 78 return 'osf1' 79 return sys.platform 80 81 82CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ) 83HOST_PLATFORM = get_platform() 84MS_WINDOWS = (HOST_PLATFORM == 'win32') 85CYGWIN = (HOST_PLATFORM == 'cygwin') 86MACOS = (HOST_PLATFORM == 'darwin') 87AIX = (HOST_PLATFORM.startswith('aix')) 88VXWORKS = ('vxworks' in HOST_PLATFORM) 89CC = os.environ.get("CC") 90if not CC: 91 CC = sysconfig.get_config_var("CC") 92 93 94SUMMARY = """ 95Python is an interpreted, interactive, object-oriented programming 96language. It is often compared to Tcl, Perl, Scheme or Java. 97 98Python combines remarkable power with very clear syntax. It has 99modules, classes, exceptions, very high level dynamic data types, and 100dynamic typing. There are interfaces to many system calls and 101libraries, as well as to various windowing systems (X11, Motif, Tk, 102Mac, MFC). New built-in modules are easily written in C or C++. Python 103is also usable as an extension language for applications that need a 104programmable interface. 105 106The Python implementation is portable: it runs on many brands of UNIX, 107on Windows, DOS, Mac, Amiga... If your favorite system isn't 108listed here, it may still be supported, if there's a C compiler for 109it. Ask around on comp.lang.python -- or just try compiling Python 110yourself. 111""" 112 113CLASSIFIERS = """ 114Development Status :: 6 - Mature 115License :: OSI Approved :: Python Software Foundation License 116Natural Language :: English 117Programming Language :: C 118Programming Language :: Python 119Topic :: Software Development 120""" 121 122 123def run_command(cmd): 124 status = os.system(cmd) 125 return os.waitstatus_to_exitcode(status) 126 127 128# Set common compiler and linker flags derived from the Makefile, 129# reserved for building the interpreter and the stdlib modules. 130# See bpo-21121 and bpo-35257 131def set_compiler_flags(compiler_flags, compiler_py_flags_nodist): 132 flags = sysconfig.get_config_var(compiler_flags) 133 py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist) 134 sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist 135 136 137def add_dir_to_list(dirlist, dir): 138 """Add the directory 'dir' to the list 'dirlist' (after any relative 139 directories) if: 140 141 1) 'dir' is not already in 'dirlist' 142 2) 'dir' actually exists, and is a directory. 143 """ 144 if dir is None or not os.path.isdir(dir) or dir in dirlist: 145 return 146 for i, path in enumerate(dirlist): 147 if not os.path.isabs(path): 148 dirlist.insert(i + 1, dir) 149 return 150 dirlist.insert(0, dir) 151 152 153def sysroot_paths(make_vars, subdirs): 154 """Get the paths of sysroot sub-directories. 155 156 * make_vars: a sequence of names of variables of the Makefile where 157 sysroot may be set. 158 * subdirs: a sequence of names of subdirectories used as the location for 159 headers or libraries. 160 """ 161 162 dirs = [] 163 for var_name in make_vars: 164 var = sysconfig.get_config_var(var_name) 165 if var is not None: 166 m = re.search(r'--sysroot=([^"]\S*|"[^"]+")', var) 167 if m is not None: 168 sysroot = m.group(1).strip('"') 169 for subdir in subdirs: 170 if os.path.isabs(subdir): 171 subdir = subdir[1:] 172 path = os.path.join(sysroot, subdir) 173 if os.path.isdir(path): 174 dirs.append(path) 175 break 176 return dirs 177 178 179MACOS_SDK_ROOT = None 180MACOS_SDK_SPECIFIED = None 181 182def macosx_sdk_root(): 183 """Return the directory of the current macOS SDK. 184 185 If no SDK was explicitly configured, call the compiler to find which 186 include files paths are being searched by default. Use '/' if the 187 compiler is searching /usr/include (meaning system header files are 188 installed) or use the root of an SDK if that is being searched. 189 (The SDK may be supplied via Xcode or via the Command Line Tools). 190 The SDK paths used by Apple-supplied tool chains depend on the 191 setting of various variables; see the xcrun man page for more info. 192 Also sets MACOS_SDK_SPECIFIED for use by macosx_sdk_specified(). 193 """ 194 global MACOS_SDK_ROOT, MACOS_SDK_SPECIFIED 195 196 # If already called, return cached result. 197 if MACOS_SDK_ROOT: 198 return MACOS_SDK_ROOT 199 200 cflags = sysconfig.get_config_var('CFLAGS') 201 m = re.search(r'-isysroot\s*(\S+)', cflags) 202 if m is not None: 203 MACOS_SDK_ROOT = m.group(1) 204 MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/' 205 else: 206 MACOS_SDK_ROOT = _osx_support._default_sysroot( 207 sysconfig.get_config_var('CC')) 208 MACOS_SDK_SPECIFIED = False 209 210 return MACOS_SDK_ROOT 211 212 213def macosx_sdk_specified(): 214 """Returns true if an SDK was explicitly configured. 215 216 True if an SDK was selected at configure time, either by specifying 217 --enable-universalsdk=(something other than no or /) or by adding a 218 -isysroot option to CFLAGS. In some cases, like when making 219 decisions about macOS Tk framework paths, we need to be able to 220 know whether the user explicitly asked to build with an SDK versus 221 the implicit use of an SDK when header files are no longer 222 installed on a running system by the Command Line Tools. 223 """ 224 global MACOS_SDK_SPECIFIED 225 226 # If already called, return cached result. 227 if MACOS_SDK_SPECIFIED: 228 return MACOS_SDK_SPECIFIED 229 230 # Find the sdk root and set MACOS_SDK_SPECIFIED 231 macosx_sdk_root() 232 return MACOS_SDK_SPECIFIED 233 234 235def is_macosx_sdk_path(path): 236 """ 237 Returns True if 'path' can be located in a macOS SDK 238 """ 239 return ( (path.startswith('/usr/') and not path.startswith('/usr/local')) 240 or path.startswith('/System/Library') 241 or path.startswith('/System/iOSSupport') ) 242 243 244def grep_headers_for(function, headers): 245 for header in headers: 246 with open(header, 'r', errors='surrogateescape') as f: 247 if function in f.read(): 248 return True 249 return False 250 251 252def find_file(filename, std_dirs, paths): 253 """Searches for the directory where a given file is located, 254 and returns a possibly-empty list of additional directories, or None 255 if the file couldn't be found at all. 256 257 'filename' is the name of a file, such as readline.h or libcrypto.a. 258 'std_dirs' is the list of standard system directories; if the 259 file is found in one of them, no additional directives are needed. 260 'paths' is a list of additional locations to check; if the file is 261 found in one of them, the resulting list will contain the directory. 262 """ 263 if MACOS: 264 # Honor the MacOSX SDK setting when one was specified. 265 # An SDK is a directory with the same structure as a real 266 # system, but with only header files and libraries. 267 sysroot = macosx_sdk_root() 268 269 # Check the standard locations 270 for dir_ in std_dirs: 271 f = os.path.join(dir_, filename) 272 273 if MACOS and is_macosx_sdk_path(dir_): 274 f = os.path.join(sysroot, dir_[1:], filename) 275 276 if os.path.exists(f): return [] 277 278 # Check the additional directories 279 for dir_ in paths: 280 f = os.path.join(dir_, filename) 281 282 if MACOS and is_macosx_sdk_path(dir_): 283 f = os.path.join(sysroot, dir_[1:], filename) 284 285 if os.path.exists(f): 286 return [dir_] 287 288 # Not found anywhere 289 return None 290 291 292def find_library_file(compiler, libname, std_dirs, paths): 293 result = compiler.find_library_file(std_dirs + paths, libname) 294 if result is None: 295 return None 296 297 if MACOS: 298 sysroot = macosx_sdk_root() 299 300 # Check whether the found file is in one of the standard directories 301 dirname = os.path.dirname(result) 302 for p in std_dirs: 303 # Ensure path doesn't end with path separator 304 p = p.rstrip(os.sep) 305 306 if MACOS and is_macosx_sdk_path(p): 307 # Note that, as of Xcode 7, Apple SDKs may contain textual stub 308 # libraries with .tbd extensions rather than the normal .dylib 309 # shared libraries installed in /. The Apple compiler tool 310 # chain handles this transparently but it can cause problems 311 # for programs that are being built with an SDK and searching 312 # for specific libraries. Distutils find_library_file() now 313 # knows to also search for and return .tbd files. But callers 314 # of find_library_file need to keep in mind that the base filename 315 # of the returned SDK library file might have a different extension 316 # from that of the library file installed on the running system, 317 # for example: 318 # /Applications/Xcode.app/Contents/Developer/Platforms/ 319 # MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/ 320 # usr/lib/libedit.tbd 321 # vs 322 # /usr/lib/libedit.dylib 323 if os.path.join(sysroot, p[1:]) == dirname: 324 return [ ] 325 326 if p == dirname: 327 return [ ] 328 329 # Otherwise, it must have been in one of the additional directories, 330 # so we have to figure out which one. 331 for p in paths: 332 # Ensure path doesn't end with path separator 333 p = p.rstrip(os.sep) 334 335 if MACOS and is_macosx_sdk_path(p): 336 if os.path.join(sysroot, p[1:]) == dirname: 337 return [ p ] 338 339 if p == dirname: 340 return [p] 341 else: 342 assert False, "Internal error: Path not found in std_dirs or paths" 343 344 345def validate_tzpath(): 346 base_tzpath = sysconfig.get_config_var('TZPATH') 347 if not base_tzpath: 348 return 349 350 tzpaths = base_tzpath.split(os.pathsep) 351 bad_paths = [tzpath for tzpath in tzpaths if not os.path.isabs(tzpath)] 352 if bad_paths: 353 raise ValueError('TZPATH must contain only absolute paths, ' 354 + f'found:\n{tzpaths!r}\nwith invalid paths:\n' 355 + f'{bad_paths!r}') 356 357 358def find_module_file(module, dirlist): 359 """Find a module in a set of possible folders. If it is not found 360 return the unadorned filename""" 361 dirs = find_file(module, [], dirlist) 362 if not dirs: 363 return module 364 if len(dirs) > 1: 365 log.info(f"WARNING: multiple copies of {module} found") 366 return os.path.join(dirs[0], module) 367 368 369class PyBuildExt(build_ext): 370 371 def __init__(self, dist): 372 build_ext.__init__(self, dist) 373 self.srcdir = None 374 self.lib_dirs = None 375 self.inc_dirs = None 376 self.config_h_vars = None 377 self.failed = [] 378 self.failed_on_import = [] 379 self.missing = [] 380 self.disabled_configure = [] 381 if '-j' in os.environ.get('MAKEFLAGS', ''): 382 self.parallel = True 383 384 def add(self, ext): 385 self.extensions.append(ext) 386 387 def set_srcdir(self): 388 self.srcdir = sysconfig.get_config_var('srcdir') 389 if not self.srcdir: 390 # Maybe running on Windows but not using CYGWIN? 391 raise ValueError("No source directory; cannot proceed.") 392 self.srcdir = os.path.abspath(self.srcdir) 393 394 def remove_disabled(self): 395 # Remove modules that are present on the disabled list 396 extensions = [ext for ext in self.extensions 397 if ext.name not in DISABLED_MODULE_LIST] 398 # move ctypes to the end, it depends on other modules 399 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions)) 400 if "_ctypes" in ext_map: 401 ctypes = extensions.pop(ext_map["_ctypes"]) 402 extensions.append(ctypes) 403 self.extensions = extensions 404 405 def update_sources_depends(self): 406 # Fix up the autodetected modules, prefixing all the source files 407 # with Modules/. 408 moddirlist = [os.path.join(self.srcdir, 'Modules')] 409 410 # Fix up the paths for scripts, too 411 self.distribution.scripts = [os.path.join(self.srcdir, filename) 412 for filename in self.distribution.scripts] 413 414 # Python header files 415 headers = [sysconfig.get_config_h_filename()] 416 headers += glob(os.path.join(escape(sysconfig.get_path('include')), "*.h")) 417 418 for ext in self.extensions: 419 ext.sources = [ find_module_file(filename, moddirlist) 420 for filename in ext.sources ] 421 if ext.depends is not None: 422 ext.depends = [find_module_file(filename, moddirlist) 423 for filename in ext.depends] 424 else: 425 ext.depends = [] 426 # re-compile extensions if a header file has been changed 427 ext.depends.extend(headers) 428 429 def remove_configured_extensions(self): 430 # The sysconfig variables built by makesetup that list the already 431 # built modules and the disabled modules as configured by the Setup 432 # files. 433 sysconf_built = sysconfig.get_config_var('MODBUILT_NAMES').split() 434 sysconf_dis = sysconfig.get_config_var('MODDISABLED_NAMES').split() 435 436 mods_built = [] 437 mods_disabled = [] 438 for ext in self.extensions: 439 # If a module has already been built or has been disabled in the 440 # Setup files, don't build it here. 441 if ext.name in sysconf_built: 442 mods_built.append(ext) 443 if ext.name in sysconf_dis: 444 mods_disabled.append(ext) 445 446 mods_configured = mods_built + mods_disabled 447 if mods_configured: 448 self.extensions = [x for x in self.extensions if x not in 449 mods_configured] 450 # Remove the shared libraries built by a previous build. 451 for ext in mods_configured: 452 fullpath = self.get_ext_fullpath(ext.name) 453 if os.path.exists(fullpath): 454 os.unlink(fullpath) 455 456 return (mods_built, mods_disabled) 457 458 def set_compiler_executables(self): 459 # When you run "make CC=altcc" or something similar, you really want 460 # those environment variables passed into the setup.py phase. Here's 461 # a small set of useful ones. 462 compiler = os.environ.get('CC') 463 args = {} 464 # unfortunately, distutils doesn't let us provide separate C and C++ 465 # compilers 466 if compiler is not None: 467 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS') 468 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags 469 self.compiler.set_executables(**args) 470 471 def build_extensions(self): 472 self.set_srcdir() 473 self.set_compiler_executables() 474 self.configure_compiler() 475 self.init_inc_lib_dirs() 476 477 # Detect which modules should be compiled 478 self.detect_modules() 479 480 if not LIST_MODULE_NAMES: 481 self.remove_disabled() 482 483 self.update_sources_depends() 484 mods_built, mods_disabled = self.remove_configured_extensions() 485 486 if LIST_MODULE_NAMES: 487 for ext in self.extensions: 488 print(ext.name) 489 for name in self.missing: 490 print(name) 491 return 492 493 build_ext.build_extensions(self) 494 495 if SUBPROCESS_BOOTSTRAP: 496 # Drop our custom subprocess module: 497 # use the newly built subprocess module 498 del sys.modules['subprocess'] 499 500 for ext in self.extensions: 501 self.check_extension_import(ext) 502 503 self.summary(mods_built, mods_disabled) 504 505 def summary(self, mods_built, mods_disabled): 506 longest = max([len(e.name) for e in self.extensions], default=0) 507 if self.failed or self.failed_on_import: 508 all_failed = self.failed + self.failed_on_import 509 longest = max(longest, max([len(name) for name in all_failed])) 510 511 def print_three_column(lst): 512 lst.sort(key=str.lower) 513 # guarantee zip() doesn't drop anything 514 while len(lst) % 3: 515 lst.append("") 516 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]): 517 print("%-*s %-*s %-*s" % (longest, e, longest, f, 518 longest, g)) 519 520 if self.missing: 521 print() 522 print("Python build finished successfully!") 523 print("The necessary bits to build these optional modules were not " 524 "found:") 525 print_three_column(self.missing) 526 print("To find the necessary bits, look in setup.py in" 527 " detect_modules() for the module's name.") 528 print() 529 530 if mods_built: 531 print() 532 print("The following modules found by detect_modules() in" 533 " setup.py, have been") 534 print("built by the Makefile instead, as configured by the" 535 " Setup files:") 536 print_three_column([ext.name for ext in mods_built]) 537 print() 538 539 if mods_disabled: 540 print() 541 print("The following modules found by detect_modules() in" 542 " setup.py have not") 543 print("been built, they are *disabled* in the Setup files:") 544 print_three_column([ext.name for ext in mods_disabled]) 545 print() 546 547 if self.disabled_configure: 548 print() 549 print("The following modules found by detect_modules() in" 550 " setup.py have not") 551 print("been built, they are *disabled* by configure:") 552 print_three_column(self.disabled_configure) 553 print() 554 555 if self.failed: 556 failed = self.failed[:] 557 print() 558 print("Failed to build these modules:") 559 print_three_column(failed) 560 print() 561 562 if self.failed_on_import: 563 failed = self.failed_on_import[:] 564 print() 565 print("Following modules built successfully" 566 " but were removed because they could not be imported:") 567 print_three_column(failed) 568 print() 569 570 if any('_ssl' in l 571 for l in (self.missing, self.failed, self.failed_on_import)): 572 print() 573 print("Could not build the ssl module!") 574 print("Python requires a OpenSSL 1.1.1 or newer") 575 if sysconfig.get_config_var("OPENSSL_LDFLAGS"): 576 print("Custom linker flags may require --with-openssl-rpath=auto") 577 print() 578 579 if os.environ.get("PYTHONSTRICTEXTENSIONBUILD") and (self.failed or self.failed_on_import): 580 raise RuntimeError("Failed to build some stdlib modules") 581 582 def build_extension(self, ext): 583 584 if ext.name == '_ctypes': 585 if not self.configure_ctypes(ext): 586 self.failed.append(ext.name) 587 return 588 589 try: 590 build_ext.build_extension(self, ext) 591 except (CCompilerError, DistutilsError) as why: 592 self.announce('WARNING: building of extension "%s" failed: %s' % 593 (ext.name, why)) 594 self.failed.append(ext.name) 595 return 596 597 def check_extension_import(self, ext): 598 # Don't try to import an extension that has failed to compile 599 if ext.name in self.failed: 600 self.announce( 601 'WARNING: skipping import check for failed build "%s"' % 602 ext.name, level=1) 603 return 604 605 # Workaround for Mac OS X: The Carbon-based modules cannot be 606 # reliably imported into a command-line Python 607 if 'Carbon' in ext.extra_link_args: 608 self.announce( 609 'WARNING: skipping import check for Carbon-based "%s"' % 610 ext.name) 611 return 612 613 if MACOS and ( 614 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args): 615 # Don't bother doing an import check when an extension was 616 # build with an explicit '-arch' flag on OSX. That's currently 617 # only used to build 32-bit only extensions in a 4-way 618 # universal build and loading 32-bit code into a 64-bit 619 # process will fail. 620 self.announce( 621 'WARNING: skipping import check for "%s"' % 622 ext.name) 623 return 624 625 # Workaround for Cygwin: Cygwin currently has fork issues when many 626 # modules have been imported 627 if CYGWIN: 628 self.announce('WARNING: skipping import check for Cygwin-based "%s"' 629 % ext.name) 630 return 631 ext_filename = os.path.join( 632 self.build_lib, 633 self.get_ext_filename(self.get_ext_fullname(ext.name))) 634 635 # If the build directory didn't exist when setup.py was 636 # started, sys.path_importer_cache has a negative result 637 # cached. Clear that cache before trying to import. 638 sys.path_importer_cache.clear() 639 640 # Don't try to load extensions for cross builds 641 if CROSS_COMPILING: 642 return 643 644 loader = importlib.machinery.ExtensionFileLoader(ext.name, ext_filename) 645 spec = importlib.util.spec_from_file_location(ext.name, ext_filename, 646 loader=loader) 647 try: 648 importlib._bootstrap._load(spec) 649 except ImportError as why: 650 self.failed_on_import.append(ext.name) 651 self.announce('*** WARNING: renaming "%s" since importing it' 652 ' failed: %s' % (ext.name, why), level=3) 653 assert not self.inplace 654 basename, tail = os.path.splitext(ext_filename) 655 newname = basename + "_failed" + tail 656 if os.path.exists(newname): 657 os.remove(newname) 658 os.rename(ext_filename, newname) 659 660 except: 661 exc_type, why, tb = sys.exc_info() 662 self.announce('*** WARNING: importing extension "%s" ' 663 'failed with %s: %s' % (ext.name, exc_type, why), 664 level=3) 665 self.failed.append(ext.name) 666 667 def add_multiarch_paths(self): 668 # Debian/Ubuntu multiarch support. 669 # https://wiki.ubuntu.com/MultiarchSpec 670 tmpfile = os.path.join(self.build_temp, 'multiarch') 671 if not os.path.exists(self.build_temp): 672 os.makedirs(self.build_temp) 673 ret = run_command( 674 '%s -print-multiarch > %s 2> /dev/null' % (CC, tmpfile)) 675 multiarch_path_component = '' 676 try: 677 if ret == 0: 678 with open(tmpfile) as fp: 679 multiarch_path_component = fp.readline().strip() 680 finally: 681 os.unlink(tmpfile) 682 683 if multiarch_path_component != '': 684 add_dir_to_list(self.compiler.library_dirs, 685 '/usr/lib/' + multiarch_path_component) 686 add_dir_to_list(self.compiler.include_dirs, 687 '/usr/include/' + multiarch_path_component) 688 return 689 690 if not find_executable('dpkg-architecture'): 691 return 692 opt = '' 693 if CROSS_COMPILING: 694 opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE') 695 tmpfile = os.path.join(self.build_temp, 'multiarch') 696 if not os.path.exists(self.build_temp): 697 os.makedirs(self.build_temp) 698 ret = run_command( 699 'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' % 700 (opt, tmpfile)) 701 try: 702 if ret == 0: 703 with open(tmpfile) as fp: 704 multiarch_path_component = fp.readline().strip() 705 add_dir_to_list(self.compiler.library_dirs, 706 '/usr/lib/' + multiarch_path_component) 707 add_dir_to_list(self.compiler.include_dirs, 708 '/usr/include/' + multiarch_path_component) 709 finally: 710 os.unlink(tmpfile) 711 712 def add_wrcc_search_dirs(self): 713 # add library search path by wr-cc, the compiler wrapper 714 715 def convert_mixed_path(path): 716 # convert path like C:\folder1\folder2/folder3/folder4 717 # to msys style /c/folder1/folder2/folder3/folder4 718 drive = path[0].lower() 719 left = path[2:].replace("\\", "/") 720 return "/" + drive + left 721 722 def add_search_path(line): 723 # On Windows building machine, VxWorks does 724 # cross builds under msys2 environment. 725 pathsep = (";" if sys.platform == "msys" else ":") 726 for d in line.strip().split("=")[1].split(pathsep): 727 d = d.strip() 728 if sys.platform == "msys": 729 # On Windows building machine, compiler 730 # returns mixed style path like: 731 # C:\folder1\folder2/folder3/folder4 732 d = convert_mixed_path(d) 733 d = os.path.normpath(d) 734 add_dir_to_list(self.compiler.library_dirs, d) 735 736 tmpfile = os.path.join(self.build_temp, 'wrccpaths') 737 os.makedirs(self.build_temp, exist_ok=True) 738 try: 739 ret = run_command('%s --print-search-dirs >%s' % (CC, tmpfile)) 740 if ret: 741 return 742 with open(tmpfile) as fp: 743 # Parse paths in libraries line. The line is like: 744 # On Linux, "libraries: = path1:path2:path3" 745 # On Windows, "libraries: = path1;path2;path3" 746 for line in fp: 747 if not line.startswith("libraries"): 748 continue 749 add_search_path(line) 750 finally: 751 try: 752 os.unlink(tmpfile) 753 except OSError: 754 pass 755 756 def add_cross_compiling_paths(self): 757 tmpfile = os.path.join(self.build_temp, 'ccpaths') 758 if not os.path.exists(self.build_temp): 759 os.makedirs(self.build_temp) 760 ret = run_command('%s -E -v - </dev/null 2>%s 1>/dev/null' % (CC, tmpfile)) 761 is_gcc = False 762 is_clang = False 763 in_incdirs = False 764 try: 765 if ret == 0: 766 with open(tmpfile) as fp: 767 for line in fp.readlines(): 768 if line.startswith("gcc version"): 769 is_gcc = True 770 elif line.startswith("clang version"): 771 is_clang = True 772 elif line.startswith("#include <...>"): 773 in_incdirs = True 774 elif line.startswith("End of search list"): 775 in_incdirs = False 776 elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"): 777 for d in line.strip().split("=")[1].split(":"): 778 d = os.path.normpath(d) 779 if '/gcc/' not in d: 780 add_dir_to_list(self.compiler.library_dirs, 781 d) 782 elif (is_gcc or is_clang) and in_incdirs and '/gcc/' not in line and '/clang/' not in line: 783 add_dir_to_list(self.compiler.include_dirs, 784 line.strip()) 785 finally: 786 os.unlink(tmpfile) 787 788 if VXWORKS: 789 self.add_wrcc_search_dirs() 790 791 def add_ldflags_cppflags(self): 792 # Add paths specified in the environment variables LDFLAGS and 793 # CPPFLAGS for header and library files. 794 # We must get the values from the Makefile and not the environment 795 # directly since an inconsistently reproducible issue comes up where 796 # the environment variable is not set even though the value were passed 797 # into configure and stored in the Makefile (issue found on OS X 10.3). 798 for env_var, arg_name, dir_list in ( 799 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs), 800 ('LDFLAGS', '-L', self.compiler.library_dirs), 801 ('CPPFLAGS', '-I', self.compiler.include_dirs)): 802 env_val = sysconfig.get_config_var(env_var) 803 if env_val: 804 parser = argparse.ArgumentParser() 805 parser.add_argument(arg_name, dest="dirs", action="append") 806 807 # To prevent argparse from raising an exception about any 808 # options in env_val that it mistakes for known option, we 809 # strip out all double dashes and any dashes followed by a 810 # character that is not for the option we are dealing with. 811 # 812 # Please note that order of the regex is important! We must 813 # strip out double-dashes first so that we don't end up with 814 # substituting "--Long" to "-Long" and thus lead to "ong" being 815 # used for a library directory. 816 env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1], 817 ' ', env_val) 818 options, _ = parser.parse_known_args(env_val.split()) 819 if options.dirs: 820 for directory in reversed(options.dirs): 821 add_dir_to_list(dir_list, directory) 822 823 def configure_compiler(self): 824 # Ensure that /usr/local is always used, but the local build 825 # directories (i.e. '.' and 'Include') must be first. See issue 826 # 10520. 827 if not CROSS_COMPILING: 828 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib') 829 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include') 830 # only change this for cross builds for 3.3, issues on Mageia 831 if CROSS_COMPILING: 832 self.add_cross_compiling_paths() 833 self.add_multiarch_paths() 834 self.add_ldflags_cppflags() 835 836 def init_inc_lib_dirs(self): 837 if (not CROSS_COMPILING and 838 os.path.normpath(sys.base_prefix) != '/usr' and 839 not sysconfig.get_config_var('PYTHONFRAMEWORK')): 840 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework 841 # (PYTHONFRAMEWORK is set) to avoid # linking problems when 842 # building a framework with different architectures than 843 # the one that is currently installed (issue #7473) 844 add_dir_to_list(self.compiler.library_dirs, 845 sysconfig.get_config_var("LIBDIR")) 846 add_dir_to_list(self.compiler.include_dirs, 847 sysconfig.get_config_var("INCLUDEDIR")) 848 849 system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib'] 850 system_include_dirs = ['/usr/include'] 851 # lib_dirs and inc_dirs are used to search for files; 852 # if a file is found in one of those directories, it can 853 # be assumed that no additional -I,-L directives are needed. 854 if not CROSS_COMPILING: 855 self.lib_dirs = self.compiler.library_dirs + system_lib_dirs 856 self.inc_dirs = self.compiler.include_dirs + system_include_dirs 857 else: 858 # Add the sysroot paths. 'sysroot' is a compiler option used to 859 # set the logical path of the standard system headers and 860 # libraries. 861 self.lib_dirs = (self.compiler.library_dirs + 862 sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs)) 863 self.inc_dirs = (self.compiler.include_dirs + 864 sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'), 865 system_include_dirs)) 866 867 config_h = sysconfig.get_config_h_filename() 868 with open(config_h) as file: 869 self.config_h_vars = sysconfig.parse_config_h(file) 870 871 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb) 872 if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']: 873 self.lib_dirs += ['/usr/ccs/lib'] 874 875 # HP-UX11iv3 keeps files in lib/hpux folders. 876 if HOST_PLATFORM == 'hp-ux11': 877 self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32'] 878 879 if MACOS: 880 # This should work on any unixy platform ;-) 881 # If the user has bothered specifying additional -I and -L flags 882 # in OPT and LDFLAGS we might as well use them here. 883 # 884 # NOTE: using shlex.split would technically be more correct, but 885 # also gives a bootstrap problem. Let's hope nobody uses 886 # directories with whitespace in the name to store libraries. 887 cflags, ldflags = sysconfig.get_config_vars( 888 'CFLAGS', 'LDFLAGS') 889 for item in cflags.split(): 890 if item.startswith('-I'): 891 self.inc_dirs.append(item[2:]) 892 893 for item in ldflags.split(): 894 if item.startswith('-L'): 895 self.lib_dirs.append(item[2:]) 896 897 def detect_simple_extensions(self): 898 # 899 # The following modules are all pretty straightforward, and compile 900 # on pretty much any POSIXish platform. 901 # 902 903 # array objects 904 self.add(Extension('array', ['arraymodule.c'], 905 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 906 907 # Context Variables 908 self.add(Extension('_contextvars', ['_contextvarsmodule.c'])) 909 910 shared_math = 'Modules/_math.o' 911 912 # math library functions, e.g. sin() 913 self.add(Extension('math', ['mathmodule.c'], 914 extra_compile_args=['-DPy_BUILD_CORE_MODULE'], 915 extra_objects=[shared_math], 916 depends=['_math.h', shared_math], 917 libraries=['m'])) 918 919 # complex math library functions 920 self.add(Extension('cmath', ['cmathmodule.c'], 921 extra_compile_args=['-DPy_BUILD_CORE_MODULE'], 922 extra_objects=[shared_math], 923 depends=['_math.h', shared_math], 924 libraries=['m'])) 925 926 # time libraries: librt may be needed for clock_gettime() 927 time_libs = [] 928 lib = sysconfig.get_config_var('TIMEMODULE_LIB') 929 if lib: 930 time_libs.append(lib) 931 932 # time operations and variables 933 self.add(Extension('time', ['timemodule.c'], 934 libraries=time_libs)) 935 # libm is needed by delta_new() that uses round() and by accum() that 936 # uses modf(). 937 self.add(Extension('_datetime', ['_datetimemodule.c'], 938 libraries=['m'], 939 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 940 # zoneinfo module 941 self.add(Extension('_zoneinfo', ['_zoneinfo.c'], 942 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 943 # random number generator implemented in C 944 self.add(Extension("_random", ["_randommodule.c"], 945 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 946 # bisect 947 self.add(Extension("_bisect", ["_bisectmodule.c"])) 948 # heapq 949 self.add(Extension("_heapq", ["_heapqmodule.c"], 950 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 951 # C-optimized pickle replacement 952 self.add(Extension("_pickle", ["_pickle.c"], 953 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 954 # _json speedups 955 self.add(Extension("_json", ["_json.c"], 956 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 957 958 # profiler (_lsprof is for cProfile.py) 959 self.add(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c'])) 960 # static Unicode character database 961 self.add(Extension('unicodedata', ['unicodedata.c'], 962 depends=['unicodedata_db.h', 'unicodename_db.h'], 963 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 964 # _opcode module 965 self.add(Extension('_opcode', ['_opcode.c'])) 966 # asyncio speedups 967 self.add(Extension("_asyncio", ["_asynciomodule.c"], 968 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 969 # _abc speedups 970 self.add(Extension("_abc", ["_abc.c"], 971 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 972 # _queue module 973 self.add(Extension("_queue", ["_queuemodule.c"], 974 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 975 # _statistics module 976 self.add(Extension("_statistics", ["_statisticsmodule.c"])) 977 978 # Modules with some UNIX dependencies -- on by default: 979 # (If you have a really backward UNIX, select and socket may not be 980 # supported...) 981 982 # fcntl(2) and ioctl(2) 983 libs = [] 984 if (self.config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)): 985 # May be necessary on AIX for flock function 986 libs = ['bsd'] 987 self.add(Extension('fcntl', ['fcntlmodule.c'], 988 libraries=libs)) 989 # pwd(3) 990 self.add(Extension('pwd', ['pwdmodule.c'])) 991 # grp(3) 992 if not VXWORKS: 993 self.add(Extension('grp', ['grpmodule.c'])) 994 # spwd, shadow passwords 995 if (self.config_h_vars.get('HAVE_GETSPNAM', False) or 996 self.config_h_vars.get('HAVE_GETSPENT', False)): 997 self.add(Extension('spwd', ['spwdmodule.c'])) 998 # AIX has shadow passwords, but access is not via getspent(), etc. 999 # module support is not expected so it not 'missing' 1000 elif not AIX: 1001 self.missing.append('spwd') 1002 1003 # select(2); not on ancient System V 1004 self.add(Extension('select', ['selectmodule.c'])) 1005 1006 # Memory-mapped files (also works on Win32). 1007 self.add(Extension('mmap', ['mmapmodule.c'])) 1008 1009 # Lance Ellinghaus's syslog module 1010 # syslog daemon interface 1011 self.add(Extension('syslog', ['syslogmodule.c'])) 1012 1013 # Python interface to subinterpreter C-API. 1014 self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c'])) 1015 1016 # 1017 # Here ends the simple stuff. From here on, modules need certain 1018 # libraries, are platform-specific, or present other surprises. 1019 # 1020 1021 # Multimedia modules 1022 # These don't work for 64-bit platforms!!! 1023 # These represent audio samples or images as strings: 1024 # 1025 # Operations on audio samples 1026 # According to #993173, this one should actually work fine on 1027 # 64-bit platforms. 1028 # 1029 # audioop needs libm for floor() in multiple functions. 1030 self.add(Extension('audioop', ['audioop.c'], 1031 libraries=['m'])) 1032 1033 # CSV files 1034 self.add(Extension('_csv', ['_csv.c'])) 1035 1036 # POSIX subprocess module helper. 1037 self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'], 1038 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 1039 1040 def detect_test_extensions(self): 1041 # Python C API test module 1042 self.add(Extension('_testcapi', ['_testcapimodule.c'], 1043 depends=['testcapi_long.h'])) 1044 1045 # Python Internal C API test module 1046 self.add(Extension('_testinternalcapi', ['_testinternalcapi.c'], 1047 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])) 1048 1049 # Python PEP-3118 (buffer protocol) test module 1050 self.add(Extension('_testbuffer', ['_testbuffer.c'])) 1051 1052 # Test loading multiple modules from one compiled file (https://bugs.python.org/issue16421) 1053 self.add(Extension('_testimportmultiple', ['_testimportmultiple.c'])) 1054 1055 # Test multi-phase extension module init (PEP 489) 1056 self.add(Extension('_testmultiphase', ['_testmultiphase.c'])) 1057 1058 # Fuzz tests. 1059 self.add(Extension('_xxtestfuzz', 1060 ['_xxtestfuzz/_xxtestfuzz.c', 1061 '_xxtestfuzz/fuzzer.c'])) 1062 1063 def detect_readline_curses(self): 1064 # readline 1065 readline_termcap_library = "" 1066 curses_library = "" 1067 # Cannot use os.popen here in py3k. 1068 tmpfile = os.path.join(self.build_temp, 'readline_termcap_lib') 1069 if not os.path.exists(self.build_temp): 1070 os.makedirs(self.build_temp) 1071 # Determine if readline is already linked against curses or tinfo. 1072 if sysconfig.get_config_var('HAVE_LIBREADLINE'): 1073 if sysconfig.get_config_var('WITH_EDITLINE'): 1074 readline_lib = 'edit' 1075 else: 1076 readline_lib = 'readline' 1077 do_readline = self.compiler.find_library_file(self.lib_dirs, 1078 readline_lib) 1079 if CROSS_COMPILING: 1080 ret = run_command("%s -d %s | grep '(NEEDED)' > %s" 1081 % (sysconfig.get_config_var('READELF'), 1082 do_readline, tmpfile)) 1083 elif find_executable('ldd'): 1084 ret = run_command("ldd %s > %s" % (do_readline, tmpfile)) 1085 else: 1086 ret = 1 1087 if ret == 0: 1088 with open(tmpfile) as fp: 1089 for ln in fp: 1090 if 'curses' in ln: 1091 readline_termcap_library = re.sub( 1092 r'.*lib(n?cursesw?)\.so.*', r'\1', ln 1093 ).rstrip() 1094 break 1095 # termcap interface split out from ncurses 1096 if 'tinfo' in ln: 1097 readline_termcap_library = 'tinfo' 1098 break 1099 if os.path.exists(tmpfile): 1100 os.unlink(tmpfile) 1101 else: 1102 do_readline = False 1103 # Issue 7384: If readline is already linked against curses, 1104 # use the same library for the readline and curses modules. 1105 if 'curses' in readline_termcap_library: 1106 curses_library = readline_termcap_library 1107 elif self.compiler.find_library_file(self.lib_dirs, 'ncursesw'): 1108 curses_library = 'ncursesw' 1109 # Issue 36210: OSS provided ncurses does not link on AIX 1110 # Use IBM supplied 'curses' for successful build of _curses 1111 elif AIX and self.compiler.find_library_file(self.lib_dirs, 'curses'): 1112 curses_library = 'curses' 1113 elif self.compiler.find_library_file(self.lib_dirs, 'ncurses'): 1114 curses_library = 'ncurses' 1115 elif self.compiler.find_library_file(self.lib_dirs, 'curses'): 1116 curses_library = 'curses' 1117 1118 if MACOS: 1119 os_release = int(os.uname()[2].split('.')[0]) 1120 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') 1121 if (dep_target and 1122 (tuple(int(n) for n in dep_target.split('.')[0:2]) 1123 < (10, 5) ) ): 1124 os_release = 8 1125 if os_release < 9: 1126 # MacOSX 10.4 has a broken readline. Don't try to build 1127 # the readline module unless the user has installed a fixed 1128 # readline package 1129 if find_file('readline/rlconf.h', self.inc_dirs, []) is None: 1130 do_readline = False 1131 if do_readline: 1132 if MACOS and os_release < 9: 1133 # In every directory on the search path search for a dynamic 1134 # library and then a static library, instead of first looking 1135 # for dynamic libraries on the entire path. 1136 # This way a statically linked custom readline gets picked up 1137 # before the (possibly broken) dynamic library in /usr/lib. 1138 readline_extra_link_args = ('-Wl,-search_paths_first',) 1139 else: 1140 readline_extra_link_args = () 1141 1142 readline_libs = [readline_lib] 1143 if readline_termcap_library: 1144 pass # Issue 7384: Already linked against curses or tinfo. 1145 elif curses_library: 1146 readline_libs.append(curses_library) 1147 elif self.compiler.find_library_file(self.lib_dirs + 1148 ['/usr/lib/termcap'], 1149 'termcap'): 1150 readline_libs.append('termcap') 1151 self.add(Extension('readline', ['readline.c'], 1152 library_dirs=['/usr/lib/termcap'], 1153 extra_link_args=readline_extra_link_args, 1154 libraries=readline_libs)) 1155 else: 1156 self.missing.append('readline') 1157 1158 # Curses support, requiring the System V version of curses, often 1159 # provided by the ncurses library. 1160 curses_defines = [] 1161 curses_includes = [] 1162 panel_library = 'panel' 1163 if curses_library == 'ncursesw': 1164 curses_defines.append(('HAVE_NCURSESW', '1')) 1165 if not CROSS_COMPILING: 1166 curses_includes.append('/usr/include/ncursesw') 1167 # Bug 1464056: If _curses.so links with ncursesw, 1168 # _curses_panel.so must link with panelw. 1169 panel_library = 'panelw' 1170 if MACOS: 1171 # On OS X, there is no separate /usr/lib/libncursesw nor 1172 # libpanelw. If we are here, we found a locally-supplied 1173 # version of libncursesw. There should also be a 1174 # libpanelw. _XOPEN_SOURCE defines are usually excluded 1175 # for OS X but we need _XOPEN_SOURCE_EXTENDED here for 1176 # ncurses wide char support 1177 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1')) 1178 elif MACOS and curses_library == 'ncurses': 1179 # Building with the system-suppied combined libncurses/libpanel 1180 curses_defines.append(('HAVE_NCURSESW', '1')) 1181 curses_defines.append(('_XOPEN_SOURCE_EXTENDED', '1')) 1182 1183 curses_enabled = True 1184 if curses_library.startswith('ncurses'): 1185 curses_libs = [curses_library] 1186 self.add(Extension('_curses', ['_cursesmodule.c'], 1187 extra_compile_args=['-DPy_BUILD_CORE_MODULE'], 1188 include_dirs=curses_includes, 1189 define_macros=curses_defines, 1190 libraries=curses_libs)) 1191 elif curses_library == 'curses' and not MACOS: 1192 # OSX has an old Berkeley curses, not good enough for 1193 # the _curses module. 1194 if (self.compiler.find_library_file(self.lib_dirs, 'terminfo')): 1195 curses_libs = ['curses', 'terminfo'] 1196 elif (self.compiler.find_library_file(self.lib_dirs, 'termcap')): 1197 curses_libs = ['curses', 'termcap'] 1198 else: 1199 curses_libs = ['curses'] 1200 1201 self.add(Extension('_curses', ['_cursesmodule.c'], 1202 extra_compile_args=['-DPy_BUILD_CORE_MODULE'], 1203 define_macros=curses_defines, 1204 libraries=curses_libs)) 1205 else: 1206 curses_enabled = False 1207 self.missing.append('_curses') 1208 1209 # If the curses module is enabled, check for the panel module 1210 # _curses_panel needs some form of ncurses 1211 skip_curses_panel = True if AIX else False 1212 if (curses_enabled and not skip_curses_panel and 1213 self.compiler.find_library_file(self.lib_dirs, panel_library)): 1214 self.add(Extension('_curses_panel', ['_curses_panel.c'], 1215 include_dirs=curses_includes, 1216 define_macros=curses_defines, 1217 libraries=[panel_library, *curses_libs])) 1218 elif not skip_curses_panel: 1219 self.missing.append('_curses_panel') 1220 1221 def detect_crypt(self): 1222 # crypt module. 1223 if VXWORKS: 1224 # bpo-31904: crypt() function is not provided by VxWorks. 1225 # DES_crypt() OpenSSL provides is too weak to implement 1226 # the encryption. 1227 self.missing.append('_crypt') 1228 return 1229 1230 if self.compiler.find_library_file(self.lib_dirs, 'crypt'): 1231 libs = ['crypt'] 1232 else: 1233 libs = [] 1234 1235 self.add(Extension('_crypt', ['_cryptmodule.c'], libraries=libs)) 1236 1237 def detect_socket(self): 1238 # socket(2) 1239 kwargs = {'depends': ['socketmodule.h']} 1240 if MACOS: 1241 # Issue #35569: Expose RFC 3542 socket options. 1242 kwargs['extra_compile_args'] = ['-D__APPLE_USE_RFC_3542'] 1243 1244 self.add(Extension('_socket', ['socketmodule.c'], **kwargs)) 1245 1246 def detect_dbm_gdbm(self): 1247 # Modules that provide persistent dictionary-like semantics. You will 1248 # probably want to arrange for at least one of them to be available on 1249 # your machine, though none are defined by default because of library 1250 # dependencies. The Python module dbm/__init__.py provides an 1251 # implementation independent wrapper for these; dbm/dumb.py provides 1252 # similar functionality (but slower of course) implemented in Python. 1253 1254 # Sleepycat^WOracle Berkeley DB interface. 1255 # https://www.oracle.com/database/technologies/related/berkeleydb.html 1256 # 1257 # This requires the Sleepycat^WOracle DB code. The supported versions 1258 # are set below. Visit the URL above to download 1259 # a release. Most open source OSes come with one or more 1260 # versions of BerkeleyDB already installed. 1261 1262 max_db_ver = (5, 3) 1263 min_db_ver = (3, 3) 1264 db_setup_debug = False # verbose debug prints from this script? 1265 1266 def allow_db_ver(db_ver): 1267 """Returns a boolean if the given BerkeleyDB version is acceptable. 1268 1269 Args: 1270 db_ver: A tuple of the version to verify. 1271 """ 1272 if not (min_db_ver <= db_ver <= max_db_ver): 1273 return False 1274 return True 1275 1276 def gen_db_minor_ver_nums(major): 1277 if major == 4: 1278 for x in range(max_db_ver[1]+1): 1279 if allow_db_ver((4, x)): 1280 yield x 1281 elif major == 3: 1282 for x in (3,): 1283 if allow_db_ver((3, x)): 1284 yield x 1285 else: 1286 raise ValueError("unknown major BerkeleyDB version", major) 1287 1288 # construct a list of paths to look for the header file in on 1289 # top of the normal inc_dirs. 1290 db_inc_paths = [ 1291 '/usr/include/db4', 1292 '/usr/local/include/db4', 1293 '/opt/sfw/include/db4', 1294 '/usr/include/db3', 1295 '/usr/local/include/db3', 1296 '/opt/sfw/include/db3', 1297 # Fink defaults (https://www.finkproject.org/) 1298 '/sw/include/db4', 1299 '/sw/include/db3', 1300 ] 1301 # 4.x minor number specific paths 1302 for x in gen_db_minor_ver_nums(4): 1303 db_inc_paths.append('/usr/include/db4%d' % x) 1304 db_inc_paths.append('/usr/include/db4.%d' % x) 1305 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x) 1306 db_inc_paths.append('/usr/local/include/db4%d' % x) 1307 db_inc_paths.append('/pkg/db-4.%d/include' % x) 1308 db_inc_paths.append('/opt/db-4.%d/include' % x) 1309 # MacPorts default (https://www.macports.org/) 1310 db_inc_paths.append('/opt/local/include/db4%d' % x) 1311 # 3.x minor number specific paths 1312 for x in gen_db_minor_ver_nums(3): 1313 db_inc_paths.append('/usr/include/db3%d' % x) 1314 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x) 1315 db_inc_paths.append('/usr/local/include/db3%d' % x) 1316 db_inc_paths.append('/pkg/db-3.%d/include' % x) 1317 db_inc_paths.append('/opt/db-3.%d/include' % x) 1318 1319 if CROSS_COMPILING: 1320 db_inc_paths = [] 1321 1322 # Add some common subdirectories for Sleepycat DB to the list, 1323 # based on the standard include directories. This way DB3/4 gets 1324 # picked up when it is installed in a non-standard prefix and 1325 # the user has added that prefix into inc_dirs. 1326 std_variants = [] 1327 for dn in self.inc_dirs: 1328 std_variants.append(os.path.join(dn, 'db3')) 1329 std_variants.append(os.path.join(dn, 'db4')) 1330 for x in gen_db_minor_ver_nums(4): 1331 std_variants.append(os.path.join(dn, "db4%d"%x)) 1332 std_variants.append(os.path.join(dn, "db4.%d"%x)) 1333 for x in gen_db_minor_ver_nums(3): 1334 std_variants.append(os.path.join(dn, "db3%d"%x)) 1335 std_variants.append(os.path.join(dn, "db3.%d"%x)) 1336 1337 db_inc_paths = std_variants + db_inc_paths 1338 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)] 1339 1340 db_ver_inc_map = {} 1341 1342 if MACOS: 1343 sysroot = macosx_sdk_root() 1344 1345 class db_found(Exception): pass 1346 try: 1347 # See whether there is a Sleepycat header in the standard 1348 # search path. 1349 for d in self.inc_dirs + db_inc_paths: 1350 f = os.path.join(d, "db.h") 1351 if MACOS and is_macosx_sdk_path(d): 1352 f = os.path.join(sysroot, d[1:], "db.h") 1353 1354 if db_setup_debug: print("db: looking for db.h in", f) 1355 if os.path.exists(f): 1356 with open(f, 'rb') as file: 1357 f = file.read() 1358 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f) 1359 if m: 1360 db_major = int(m.group(1)) 1361 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f) 1362 db_minor = int(m.group(1)) 1363 db_ver = (db_major, db_minor) 1364 1365 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug 1366 if db_ver == (4, 6): 1367 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f) 1368 db_patch = int(m.group(1)) 1369 if db_patch < 21: 1370 print("db.h:", db_ver, "patch", db_patch, 1371 "being ignored (4.6.x must be >= 4.6.21)") 1372 continue 1373 1374 if ( (db_ver not in db_ver_inc_map) and 1375 allow_db_ver(db_ver) ): 1376 # save the include directory with the db.h version 1377 # (first occurrence only) 1378 db_ver_inc_map[db_ver] = d 1379 if db_setup_debug: 1380 print("db.h: found", db_ver, "in", d) 1381 else: 1382 # we already found a header for this library version 1383 if db_setup_debug: print("db.h: ignoring", d) 1384 else: 1385 # ignore this header, it didn't contain a version number 1386 if db_setup_debug: 1387 print("db.h: no version number version in", d) 1388 1389 db_found_vers = list(db_ver_inc_map.keys()) 1390 db_found_vers.sort() 1391 1392 while db_found_vers: 1393 db_ver = db_found_vers.pop() 1394 db_incdir = db_ver_inc_map[db_ver] 1395 1396 # check lib directories parallel to the location of the header 1397 db_dirs_to_check = [ 1398 db_incdir.replace("include", 'lib64'), 1399 db_incdir.replace("include", 'lib'), 1400 ] 1401 1402 if not MACOS: 1403 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check)) 1404 1405 else: 1406 # Same as other branch, but takes OSX SDK into account 1407 tmp = [] 1408 for dn in db_dirs_to_check: 1409 if is_macosx_sdk_path(dn): 1410 if os.path.isdir(os.path.join(sysroot, dn[1:])): 1411 tmp.append(dn) 1412 else: 1413 if os.path.isdir(dn): 1414 tmp.append(dn) 1415 db_dirs_to_check = tmp 1416 1417 db_dirs_to_check = tmp 1418 1419 # Look for a version specific db-X.Y before an ambiguous dbX 1420 # XXX should we -ever- look for a dbX name? Do any 1421 # systems really not name their library by version and 1422 # symlink to more general names? 1423 for dblib in (('db-%d.%d' % db_ver), 1424 ('db%d%d' % db_ver), 1425 ('db%d' % db_ver[0])): 1426 dblib_file = self.compiler.find_library_file( 1427 db_dirs_to_check + self.lib_dirs, dblib ) 1428 if dblib_file: 1429 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ] 1430 raise db_found 1431 else: 1432 if db_setup_debug: print("db lib: ", dblib, "not found") 1433 1434 except db_found: 1435 if db_setup_debug: 1436 print("bsddb using BerkeleyDB lib:", db_ver, dblib) 1437 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir) 1438 dblibs = [dblib] 1439 # Only add the found library and include directories if they aren't 1440 # already being searched. This avoids an explicit runtime library 1441 # dependency. 1442 if db_incdir in self.inc_dirs: 1443 db_incs = None 1444 else: 1445 db_incs = [db_incdir] 1446 if dblib_dir[0] in self.lib_dirs: 1447 dblib_dir = None 1448 else: 1449 if db_setup_debug: print("db: no appropriate library found") 1450 db_incs = None 1451 dblibs = [] 1452 dblib_dir = None 1453 1454 dbm_setup_debug = False # verbose debug prints from this script? 1455 dbm_order = ['gdbm'] 1456 # The standard Unix dbm module: 1457 if not CYGWIN: 1458 config_args = [arg.strip("'") 1459 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()] 1460 dbm_args = [arg for arg in config_args 1461 if arg.startswith('--with-dbmliborder=')] 1462 if dbm_args: 1463 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":") 1464 else: 1465 dbm_order = "ndbm:gdbm:bdb".split(":") 1466 dbmext = None 1467 for cand in dbm_order: 1468 if cand == "ndbm": 1469 if find_file("ndbm.h", self.inc_dirs, []) is not None: 1470 # Some systems have -lndbm, others have -lgdbm_compat, 1471 # others don't have either 1472 if self.compiler.find_library_file(self.lib_dirs, 1473 'ndbm'): 1474 ndbm_libs = ['ndbm'] 1475 elif self.compiler.find_library_file(self.lib_dirs, 1476 'gdbm_compat'): 1477 ndbm_libs = ['gdbm_compat'] 1478 else: 1479 ndbm_libs = [] 1480 if dbm_setup_debug: print("building dbm using ndbm") 1481 dbmext = Extension('_dbm', ['_dbmmodule.c'], 1482 define_macros=[ 1483 ('HAVE_NDBM_H',None), 1484 ], 1485 libraries=ndbm_libs) 1486 break 1487 1488 elif cand == "gdbm": 1489 if self.compiler.find_library_file(self.lib_dirs, 'gdbm'): 1490 gdbm_libs = ['gdbm'] 1491 if self.compiler.find_library_file(self.lib_dirs, 1492 'gdbm_compat'): 1493 gdbm_libs.append('gdbm_compat') 1494 if find_file("gdbm/ndbm.h", self.inc_dirs, []) is not None: 1495 if dbm_setup_debug: print("building dbm using gdbm") 1496 dbmext = Extension( 1497 '_dbm', ['_dbmmodule.c'], 1498 define_macros=[ 1499 ('HAVE_GDBM_NDBM_H', None), 1500 ], 1501 libraries = gdbm_libs) 1502 break 1503 if find_file("gdbm-ndbm.h", self.inc_dirs, []) is not None: 1504 if dbm_setup_debug: print("building dbm using gdbm") 1505 dbmext = Extension( 1506 '_dbm', ['_dbmmodule.c'], 1507 define_macros=[ 1508 ('HAVE_GDBM_DASH_NDBM_H', None), 1509 ], 1510 libraries = gdbm_libs) 1511 break 1512 elif cand == "bdb": 1513 if dblibs: 1514 if dbm_setup_debug: print("building dbm using bdb") 1515 dbmext = Extension('_dbm', ['_dbmmodule.c'], 1516 library_dirs=dblib_dir, 1517 runtime_library_dirs=dblib_dir, 1518 include_dirs=db_incs, 1519 define_macros=[ 1520 ('HAVE_BERKDB_H', None), 1521 ('DB_DBM_HSEARCH', None), 1522 ], 1523 libraries=dblibs) 1524 break 1525 if dbmext is not None: 1526 self.add(dbmext) 1527 else: 1528 self.missing.append('_dbm') 1529 1530 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm: 1531 if ('gdbm' in dbm_order and 1532 self.compiler.find_library_file(self.lib_dirs, 'gdbm')): 1533 self.add(Extension('_gdbm', ['_gdbmmodule.c'], 1534 libraries=['gdbm'])) 1535 else: 1536 self.missing.append('_gdbm') 1537 1538 def detect_sqlite(self): 1539 # The sqlite interface 1540 sqlite_setup_debug = False # verbose debug prints from this script? 1541 1542 # We hunt for #define SQLITE_VERSION "n.n.n" 1543 sqlite_incdir = sqlite_libdir = None 1544 sqlite_inc_paths = [ '/usr/include', 1545 '/usr/include/sqlite', 1546 '/usr/include/sqlite3', 1547 '/usr/local/include', 1548 '/usr/local/include/sqlite', 1549 '/usr/local/include/sqlite3', 1550 ] 1551 if CROSS_COMPILING: 1552 sqlite_inc_paths = [] 1553 MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810 1554 MIN_SQLITE_VERSION = ".".join([str(x) 1555 for x in MIN_SQLITE_VERSION_NUMBER]) 1556 1557 # Scan the default include directories before the SQLite specific 1558 # ones. This allows one to override the copy of sqlite on OSX, 1559 # where /usr/include contains an old version of sqlite. 1560 if MACOS: 1561 sysroot = macosx_sdk_root() 1562 1563 for d_ in self.inc_dirs + sqlite_inc_paths: 1564 d = d_ 1565 if MACOS and is_macosx_sdk_path(d): 1566 d = os.path.join(sysroot, d[1:]) 1567 1568 f = os.path.join(d, "sqlite3.h") 1569 if os.path.exists(f): 1570 if sqlite_setup_debug: print("sqlite: found %s"%f) 1571 with open(f) as file: 1572 incf = file.read() 1573 m = re.search( 1574 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf) 1575 if m: 1576 sqlite_version = m.group(1) 1577 sqlite_version_tuple = tuple([int(x) 1578 for x in sqlite_version.split(".")]) 1579 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER: 1580 # we win! 1581 if sqlite_setup_debug: 1582 print("%s/sqlite3.h: version %s"%(d, sqlite_version)) 1583 sqlite_incdir = d 1584 break 1585 else: 1586 if sqlite_setup_debug: 1587 print("%s: version %s is too old, need >= %s"%(d, 1588 sqlite_version, MIN_SQLITE_VERSION)) 1589 elif sqlite_setup_debug: 1590 print("sqlite: %s had no SQLITE_VERSION"%(f,)) 1591 1592 if sqlite_incdir: 1593 sqlite_dirs_to_check = [ 1594 os.path.join(sqlite_incdir, '..', 'lib64'), 1595 os.path.join(sqlite_incdir, '..', 'lib'), 1596 os.path.join(sqlite_incdir, '..', '..', 'lib64'), 1597 os.path.join(sqlite_incdir, '..', '..', 'lib'), 1598 ] 1599 sqlite_libfile = self.compiler.find_library_file( 1600 sqlite_dirs_to_check + self.lib_dirs, 'sqlite3') 1601 if sqlite_libfile: 1602 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))] 1603 1604 if sqlite_incdir and sqlite_libdir: 1605 sqlite_srcs = ['_sqlite/cache.c', 1606 '_sqlite/connection.c', 1607 '_sqlite/cursor.c', 1608 '_sqlite/microprotocols.c', 1609 '_sqlite/module.c', 1610 '_sqlite/prepare_protocol.c', 1611 '_sqlite/row.c', 1612 '_sqlite/statement.c', 1613 '_sqlite/util.c', ] 1614 sqlite_defines = [] 1615 1616 # Enable support for loadable extensions in the sqlite3 module 1617 # if --enable-loadable-sqlite-extensions configure option is used. 1618 if '--enable-loadable-sqlite-extensions' not in sysconfig.get_config_var("CONFIG_ARGS"): 1619 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1")) 1620 elif MACOS and sqlite_incdir == os.path.join(MACOS_SDK_ROOT, "usr/include"): 1621 raise DistutilsError("System version of SQLite does not support loadable extensions") 1622 1623 if MACOS: 1624 # In every directory on the search path search for a dynamic 1625 # library and then a static library, instead of first looking 1626 # for dynamic libraries on the entire path. 1627 # This way a statically linked custom sqlite gets picked up 1628 # before the dynamic library in /usr/lib. 1629 sqlite_extra_link_args = ('-Wl,-search_paths_first',) 1630 else: 1631 sqlite_extra_link_args = () 1632 1633 include_dirs = ["Modules/_sqlite"] 1634 # Only include the directory where sqlite was found if it does 1635 # not already exist in set include directories, otherwise you 1636 # can end up with a bad search path order. 1637 if sqlite_incdir not in self.compiler.include_dirs: 1638 include_dirs.append(sqlite_incdir) 1639 # avoid a runtime library path for a system library dir 1640 if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs: 1641 sqlite_libdir = None 1642 self.add(Extension('_sqlite3', sqlite_srcs, 1643 define_macros=sqlite_defines, 1644 include_dirs=include_dirs, 1645 library_dirs=sqlite_libdir, 1646 extra_link_args=sqlite_extra_link_args, 1647 libraries=["sqlite3",])) 1648 else: 1649 self.missing.append('_sqlite3') 1650 1651 def detect_platform_specific_exts(self): 1652 # Unix-only modules 1653 if not MS_WINDOWS: 1654 if not VXWORKS: 1655 # Steen Lumholt's termios module 1656 self.add(Extension('termios', ['termios.c'])) 1657 # Jeremy Hylton's rlimit interface 1658 self.add(Extension('resource', ['resource.c'])) 1659 else: 1660 self.missing.extend(['resource', 'termios']) 1661 1662 # Platform-specific libraries 1663 if HOST_PLATFORM.startswith(('linux', 'freebsd', 'gnukfreebsd')): 1664 self.add(Extension('ossaudiodev', ['ossaudiodev.c'])) 1665 elif not AIX: 1666 self.missing.append('ossaudiodev') 1667 1668 if MACOS: 1669 self.add(Extension('_scproxy', ['_scproxy.c'], 1670 extra_link_args=[ 1671 '-framework', 'SystemConfiguration', 1672 '-framework', 'CoreFoundation'])) 1673 1674 def detect_compress_exts(self): 1675 # Andrew Kuchling's zlib module. Note that some versions of zlib 1676 # 1.1.3 have security problems. See CERT Advisory CA-2002-07: 1677 # http://www.cert.org/advisories/CA-2002-07.html 1678 # 1679 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to 1680 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For 1681 # now, we still accept 1.1.3, because we think it's difficult to 1682 # exploit this in Python, and we'd rather make it RedHat's problem 1683 # than our problem <wink>. 1684 # 1685 # You can upgrade zlib to version 1.1.4 yourself by going to 1686 # http://www.gzip.org/zlib/ 1687 zlib_inc = find_file('zlib.h', [], self.inc_dirs) 1688 have_zlib = False 1689 if zlib_inc is not None: 1690 zlib_h = zlib_inc[0] + '/zlib.h' 1691 version = '"0.0.0"' 1692 version_req = '"1.1.3"' 1693 if MACOS and is_macosx_sdk_path(zlib_h): 1694 zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:]) 1695 with open(zlib_h) as fp: 1696 while 1: 1697 line = fp.readline() 1698 if not line: 1699 break 1700 if line.startswith('#define ZLIB_VERSION'): 1701 version = line.split()[2] 1702 break 1703 if version >= version_req: 1704 if (self.compiler.find_library_file(self.lib_dirs, 'z')): 1705 if MACOS: 1706 zlib_extra_link_args = ('-Wl,-search_paths_first',) 1707 else: 1708 zlib_extra_link_args = () 1709 self.add(Extension('zlib', ['zlibmodule.c'], 1710 libraries=['z'], 1711 extra_link_args=zlib_extra_link_args)) 1712 have_zlib = True 1713 else: 1714 self.missing.append('zlib') 1715 else: 1716 self.missing.append('zlib') 1717 else: 1718 self.missing.append('zlib') 1719 1720 # Helper module for various ascii-encoders. Uses zlib for an optimized 1721 # crc32 if we have it. Otherwise binascii uses its own. 1722 if have_zlib: 1723 extra_compile_args = ['-DUSE_ZLIB_CRC32'] 1724 libraries = ['z'] 1725 extra_link_args = zlib_extra_link_args 1726 else: 1727 extra_compile_args = [] 1728 libraries = [] 1729 extra_link_args = [] 1730 self.add(Extension('binascii', ['binascii.c'], 1731 extra_compile_args=extra_compile_args, 1732 libraries=libraries, 1733 extra_link_args=extra_link_args)) 1734 1735 # Gustavo Niemeyer's bz2 module. 1736 if (self.compiler.find_library_file(self.lib_dirs, 'bz2')): 1737 if MACOS: 1738 bz2_extra_link_args = ('-Wl,-search_paths_first',) 1739 else: 1740 bz2_extra_link_args = () 1741 self.add(Extension('_bz2', ['_bz2module.c'], 1742 libraries=['bz2'], 1743 extra_link_args=bz2_extra_link_args)) 1744 else: 1745 self.missing.append('_bz2') 1746 1747 # LZMA compression support. 1748 if self.compiler.find_library_file(self.lib_dirs, 'lzma'): 1749 self.add(Extension('_lzma', ['_lzmamodule.c'], 1750 libraries=['lzma'])) 1751 else: 1752 self.missing.append('_lzma') 1753 1754 def detect_expat_elementtree(self): 1755 # Interface to the Expat XML parser 1756 # 1757 # Expat was written by James Clark and is now maintained by a group of 1758 # developers on SourceForge; see www.libexpat.org for more information. 1759 # The pyexpat module was written by Paul Prescod after a prototype by 1760 # Jack Jansen. The Expat source is included in Modules/expat/. Usage 1761 # of a system shared libexpat.so is possible with --with-system-expat 1762 # configure option. 1763 # 1764 # More information on Expat can be found at www.libexpat.org. 1765 # 1766 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"): 1767 expat_inc = [] 1768 define_macros = [] 1769 extra_compile_args = [] 1770 expat_lib = ['expat'] 1771 expat_sources = [] 1772 expat_depends = [] 1773 else: 1774 expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')] 1775 define_macros = [ 1776 ('HAVE_EXPAT_CONFIG_H', '1'), 1777 # bpo-30947: Python uses best available entropy sources to 1778 # call XML_SetHashSalt(), expat entropy sources are not needed 1779 ('XML_POOR_ENTROPY', '1'), 1780 ] 1781 extra_compile_args = [] 1782 # bpo-44394: libexpat uses isnan() of math.h and needs linkage 1783 # against the libm 1784 expat_lib = ['m'] 1785 expat_sources = ['expat/xmlparse.c', 1786 'expat/xmlrole.c', 1787 'expat/xmltok.c'] 1788 expat_depends = ['expat/ascii.h', 1789 'expat/asciitab.h', 1790 'expat/expat.h', 1791 'expat/expat_config.h', 1792 'expat/expat_external.h', 1793 'expat/internal.h', 1794 'expat/latin1tab.h', 1795 'expat/utf8tab.h', 1796 'expat/xmlrole.h', 1797 'expat/xmltok.h', 1798 'expat/xmltok_impl.h' 1799 ] 1800 1801 cc = sysconfig.get_config_var('CC').split()[0] 1802 ret = run_command( 1803 '"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc) 1804 if ret == 0: 1805 extra_compile_args.append('-Wno-unreachable-code') 1806 1807 self.add(Extension('pyexpat', 1808 define_macros=define_macros, 1809 extra_compile_args=extra_compile_args, 1810 include_dirs=expat_inc, 1811 libraries=expat_lib, 1812 sources=['pyexpat.c'] + expat_sources, 1813 depends=expat_depends)) 1814 1815 # Fredrik Lundh's cElementTree module. Note that this also 1816 # uses expat (via the CAPI hook in pyexpat). 1817 1818 if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')): 1819 define_macros.append(('USE_PYEXPAT_CAPI', None)) 1820 self.add(Extension('_elementtree', 1821 define_macros=define_macros, 1822 include_dirs=expat_inc, 1823 libraries=expat_lib, 1824 sources=['_elementtree.c'], 1825 depends=['pyexpat.c', *expat_sources, 1826 *expat_depends])) 1827 else: 1828 self.missing.append('_elementtree') 1829 1830 def detect_multibytecodecs(self): 1831 # Hye-Shik Chang's CJKCodecs modules. 1832 self.add(Extension('_multibytecodec', 1833 ['cjkcodecs/multibytecodec.c'])) 1834 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'): 1835 self.add(Extension('_codecs_%s' % loc, 1836 ['cjkcodecs/_codecs_%s.c' % loc])) 1837 1838 def detect_multiprocessing(self): 1839 # Richard Oudkerk's multiprocessing module 1840 if MS_WINDOWS: 1841 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c', 1842 '_multiprocessing/semaphore.c'] 1843 else: 1844 multiprocessing_srcs = ['_multiprocessing/multiprocessing.c'] 1845 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not 1846 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')): 1847 multiprocessing_srcs.append('_multiprocessing/semaphore.c') 1848 self.add(Extension('_multiprocessing', multiprocessing_srcs, 1849 include_dirs=["Modules/_multiprocessing"])) 1850 1851 if (not MS_WINDOWS and 1852 sysconfig.get_config_var('HAVE_SHM_OPEN') and 1853 sysconfig.get_config_var('HAVE_SHM_UNLINK')): 1854 posixshmem_srcs = ['_multiprocessing/posixshmem.c'] 1855 libs = [] 1856 if sysconfig.get_config_var('SHM_NEEDS_LIBRT'): 1857 # need to link with librt to get shm_open() 1858 libs.append('rt') 1859 self.add(Extension('_posixshmem', posixshmem_srcs, 1860 define_macros={}, 1861 libraries=libs, 1862 include_dirs=["Modules/_multiprocessing"])) 1863 else: 1864 self.missing.append('_posixshmem') 1865 1866 def detect_uuid(self): 1867 # Build the _uuid module if possible 1868 uuid_h = sysconfig.get_config_var("HAVE_UUID_H") 1869 uuid_uuid_h = sysconfig.get_config_var("HAVE_UUID_UUID_H") 1870 if uuid_h or uuid_uuid_h: 1871 if sysconfig.get_config_var("HAVE_LIBUUID"): 1872 uuid_libs = ["uuid"] 1873 else: 1874 uuid_libs = [] 1875 self.add(Extension('_uuid', ['_uuidmodule.c'], 1876 libraries=uuid_libs)) 1877 else: 1878 self.missing.append('_uuid') 1879 1880 def detect_modules(self): 1881 self.detect_simple_extensions() 1882 if TEST_EXTENSIONS: 1883 self.detect_test_extensions() 1884 self.detect_readline_curses() 1885 self.detect_crypt() 1886 self.detect_socket() 1887 self.detect_openssl_hashlib() 1888 self.detect_hash_builtins() 1889 self.detect_dbm_gdbm() 1890 self.detect_sqlite() 1891 self.detect_platform_specific_exts() 1892 self.detect_nis() 1893 self.detect_compress_exts() 1894 self.detect_expat_elementtree() 1895 self.detect_multibytecodecs() 1896 self.detect_decimal() 1897 self.detect_ctypes() 1898 self.detect_multiprocessing() 1899 if not self.detect_tkinter(): 1900 self.missing.append('_tkinter') 1901 self.detect_uuid() 1902 1903## # Uncomment these lines if you want to play with xxmodule.c 1904## self.add(Extension('xx', ['xxmodule.c'])) 1905 1906 # The limited C API is not compatible with the Py_TRACE_REFS macro. 1907 if not sysconfig.get_config_var('Py_TRACE_REFS'): 1908 self.add(Extension('xxlimited', ['xxlimited.c'])) 1909 self.add(Extension('xxlimited_35', ['xxlimited_35.c'])) 1910 1911 def detect_tkinter_fromenv(self): 1912 # Build _tkinter using the Tcl/Tk locations specified by 1913 # the _TCLTK_INCLUDES and _TCLTK_LIBS environment variables. 1914 # This method is meant to be invoked by detect_tkinter(). 1915 # 1916 # The variables can be set via one of the following ways. 1917 # 1918 # - Automatically, at configuration time, by using pkg-config. 1919 # The tool is called by the configure script. 1920 # Additional pkg-config configuration paths can be set via the 1921 # PKG_CONFIG_PATH environment variable. 1922 # 1923 # PKG_CONFIG_PATH=".../lib/pkgconfig" ./configure ... 1924 # 1925 # - Explicitly, at configuration time by setting both 1926 # --with-tcltk-includes and --with-tcltk-libs. 1927 # 1928 # ./configure ... \ 1929 # --with-tcltk-includes="-I/path/to/tclincludes \ 1930 # -I/path/to/tkincludes" 1931 # --with-tcltk-libs="-L/path/to/tcllibs -ltclm.n \ 1932 # -L/path/to/tklibs -ltkm.n" 1933 # 1934 # - Explicitly, at compile time, by passing TCLTK_INCLUDES and 1935 # TCLTK_LIBS to the make target. 1936 # This will override any configuration-time option. 1937 # 1938 # make TCLTK_INCLUDES="..." TCLTK_LIBS="..." 1939 # 1940 # This can be useful for building and testing tkinter with multiple 1941 # versions of Tcl/Tk. Note that a build of Tk depends on a particular 1942 # build of Tcl so you need to specify both arguments and use care when 1943 # overriding. 1944 1945 # The _TCLTK variables are created in the Makefile sharedmods target. 1946 tcltk_includes = os.environ.get('_TCLTK_INCLUDES') 1947 tcltk_libs = os.environ.get('_TCLTK_LIBS') 1948 if not (tcltk_includes and tcltk_libs): 1949 # Resume default configuration search. 1950 return False 1951 1952 extra_compile_args = tcltk_includes.split() 1953 extra_link_args = tcltk_libs.split() 1954 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'], 1955 define_macros=[('WITH_APPINIT', 1)], 1956 extra_compile_args = extra_compile_args, 1957 extra_link_args = extra_link_args)) 1958 return True 1959 1960 def detect_tkinter_darwin(self): 1961 # Build default _tkinter on macOS using Tcl and Tk frameworks. 1962 # This method is meant to be invoked by detect_tkinter(). 1963 # 1964 # The macOS native Tk (AKA Aqua Tk) and Tcl are most commonly 1965 # built and installed as macOS framework bundles. However, 1966 # for several reasons, we cannot take full advantage of the 1967 # Apple-supplied compiler chain's -framework options here. 1968 # Instead, we need to find and pass to the compiler the 1969 # absolute paths of the Tcl and Tk headers files we want to use 1970 # and the absolute path to the directory containing the Tcl 1971 # and Tk frameworks for linking. 1972 # 1973 # We want to handle here two common use cases on macOS: 1974 # 1. Build and link with system-wide third-party or user-built 1975 # Tcl and Tk frameworks installed in /Library/Frameworks. 1976 # 2. Build and link using a user-specified macOS SDK so that the 1977 # built Python can be exported to other systems. In this case, 1978 # search only the SDK's /Library/Frameworks (normally empty) 1979 # and /System/Library/Frameworks. 1980 # 1981 # Any other use cases are handled either by detect_tkinter_fromenv(), 1982 # or detect_tkinter(). The former handles non-standard locations of 1983 # Tcl/Tk, defined via the _TCLTK_INCLUDES and _TCLTK_LIBS environment 1984 # variables. The latter handles any Tcl/Tk versions installed in 1985 # standard Unix directories. 1986 # 1987 # It would be desirable to also handle here the case where 1988 # you want to build and link with a framework build of Tcl and Tk 1989 # that is not in /Library/Frameworks, say, in your private 1990 # $HOME/Library/Frameworks directory or elsewhere. It turns 1991 # out to be difficult to make that work automatically here 1992 # without bringing into play more tools and magic. That case 1993 # can be handled using a recipe with the right arguments 1994 # to detect_tkinter_fromenv(). 1995 # 1996 # Note also that the fallback case here is to try to use the 1997 # Apple-supplied Tcl and Tk frameworks in /System/Library but 1998 # be forewarned that they are deprecated by Apple and typically 1999 # out-of-date and buggy; their use should be avoided if at 2000 # all possible by installing a newer version of Tcl and Tk in 2001 # /Library/Frameworks before building Python without 2002 # an explicit SDK or by configuring build arguments explicitly. 2003 2004 from os.path import join, exists 2005 2006 sysroot = macosx_sdk_root() # path to the SDK or '/' 2007 2008 if macosx_sdk_specified(): 2009 # Use case #2: an SDK other than '/' was specified. 2010 # Only search there. 2011 framework_dirs = [ 2012 join(sysroot, 'Library', 'Frameworks'), 2013 join(sysroot, 'System', 'Library', 'Frameworks'), 2014 ] 2015 else: 2016 # Use case #1: no explicit SDK selected. 2017 # Search the local system-wide /Library/Frameworks, 2018 # not the one in the default SDK, otherwise fall back to 2019 # /System/Library/Frameworks whose header files may be in 2020 # the default SDK or, on older systems, actually installed. 2021 framework_dirs = [ 2022 join('/', 'Library', 'Frameworks'), 2023 join(sysroot, 'System', 'Library', 'Frameworks'), 2024 ] 2025 2026 # Find the directory that contains the Tcl.framework and 2027 # Tk.framework bundles. 2028 for F in framework_dirs: 2029 # both Tcl.framework and Tk.framework should be present 2030 for fw in 'Tcl', 'Tk': 2031 if not exists(join(F, fw + '.framework')): 2032 break 2033 else: 2034 # ok, F is now directory with both frameworks. Continue 2035 # building 2036 break 2037 else: 2038 # Tk and Tcl frameworks not found. Normal "unix" tkinter search 2039 # will now resume. 2040 return False 2041 2042 include_dirs = [ 2043 join(F, fw + '.framework', H) 2044 for fw in ('Tcl', 'Tk') 2045 for H in ('Headers',) 2046 ] 2047 2048 # Add the base framework directory as well 2049 compile_args = ['-F', F] 2050 2051 # Do not build tkinter for archs that this Tk was not built with. 2052 cflags = sysconfig.get_config_vars('CFLAGS')[0] 2053 archs = re.findall(r'-arch\s+(\w+)', cflags) 2054 2055 tmpfile = os.path.join(self.build_temp, 'tk.arch') 2056 if not os.path.exists(self.build_temp): 2057 os.makedirs(self.build_temp) 2058 2059 run_command( 2060 "file {}/Tk.framework/Tk | grep 'for architecture' > {}".format(F, tmpfile) 2061 ) 2062 with open(tmpfile) as fp: 2063 detected_archs = [] 2064 for ln in fp: 2065 a = ln.split()[-1] 2066 if a in archs: 2067 detected_archs.append(ln.split()[-1]) 2068 os.unlink(tmpfile) 2069 2070 arch_args = [] 2071 for a in detected_archs: 2072 arch_args.append('-arch') 2073 arch_args.append(a) 2074 2075 compile_args += arch_args 2076 link_args = [','.join(['-Wl', '-F', F, '-framework', 'Tcl', '-framework', 'Tk']), *arch_args] 2077 2078 # The X11/xlib.h file bundled in the Tk sources can cause function 2079 # prototype warnings from the compiler. Since we cannot easily fix 2080 # that, suppress the warnings here instead. 2081 if '-Wstrict-prototypes' in cflags.split(): 2082 compile_args.append('-Wno-strict-prototypes') 2083 2084 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'], 2085 define_macros=[('WITH_APPINIT', 1)], 2086 include_dirs=include_dirs, 2087 libraries=[], 2088 extra_compile_args=compile_args, 2089 extra_link_args=link_args)) 2090 return True 2091 2092 def detect_tkinter(self): 2093 # The _tkinter module. 2094 # 2095 # Detection of Tcl/Tk is attempted in the following order: 2096 # - Through environment variables. 2097 # - Platform specific detection of Tcl/Tk (currently only macOS). 2098 # - Search of various standard Unix header/library paths. 2099 # 2100 # Detection stops at the first successful method. 2101 2102 # Check for Tcl and Tk at the locations indicated by _TCLTK_INCLUDES 2103 # and _TCLTK_LIBS environment variables. 2104 if self.detect_tkinter_fromenv(): 2105 return True 2106 2107 # Rather than complicate the code below, detecting and building 2108 # AquaTk is a separate method. Only one Tkinter will be built on 2109 # Darwin - either AquaTk, if it is found, or X11 based Tk. 2110 if (MACOS and self.detect_tkinter_darwin()): 2111 return True 2112 2113 # Assume we haven't found any of the libraries or include files 2114 # The versions with dots are used on Unix, and the versions without 2115 # dots on Windows, for detection by cygwin. 2116 tcllib = tklib = tcl_includes = tk_includes = None 2117 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83', 2118 '8.2', '82', '8.1', '81', '8.0', '80']: 2119 tklib = self.compiler.find_library_file(self.lib_dirs, 2120 'tk' + version) 2121 tcllib = self.compiler.find_library_file(self.lib_dirs, 2122 'tcl' + version) 2123 if tklib and tcllib: 2124 # Exit the loop when we've found the Tcl/Tk libraries 2125 break 2126 2127 # Now check for the header files 2128 if tklib and tcllib: 2129 # Check for the include files on Debian and {Free,Open}BSD, where 2130 # they're put in /usr/include/{tcl,tk}X.Y 2131 dotversion = version 2132 if '.' not in dotversion and "bsd" in HOST_PLATFORM.lower(): 2133 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a, 2134 # but the include subdirs are named like .../include/tcl8.3. 2135 dotversion = dotversion[:-1] + '.' + dotversion[-1] 2136 tcl_include_sub = [] 2137 tk_include_sub = [] 2138 for dir in self.inc_dirs: 2139 tcl_include_sub += [dir + os.sep + "tcl" + dotversion] 2140 tk_include_sub += [dir + os.sep + "tk" + dotversion] 2141 tk_include_sub += tcl_include_sub 2142 tcl_includes = find_file('tcl.h', self.inc_dirs, tcl_include_sub) 2143 tk_includes = find_file('tk.h', self.inc_dirs, tk_include_sub) 2144 2145 if (tcllib is None or tklib is None or 2146 tcl_includes is None or tk_includes is None): 2147 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2) 2148 return False 2149 2150 # OK... everything seems to be present for Tcl/Tk. 2151 2152 include_dirs = [] 2153 libs = [] 2154 defs = [] 2155 added_lib_dirs = [] 2156 for dir in tcl_includes + tk_includes: 2157 if dir not in include_dirs: 2158 include_dirs.append(dir) 2159 2160 # Check for various platform-specific directories 2161 if HOST_PLATFORM == 'sunos5': 2162 include_dirs.append('/usr/openwin/include') 2163 added_lib_dirs.append('/usr/openwin/lib') 2164 elif os.path.exists('/usr/X11R6/include'): 2165 include_dirs.append('/usr/X11R6/include') 2166 added_lib_dirs.append('/usr/X11R6/lib64') 2167 added_lib_dirs.append('/usr/X11R6/lib') 2168 elif os.path.exists('/usr/X11R5/include'): 2169 include_dirs.append('/usr/X11R5/include') 2170 added_lib_dirs.append('/usr/X11R5/lib') 2171 else: 2172 # Assume default location for X11 2173 include_dirs.append('/usr/X11/include') 2174 added_lib_dirs.append('/usr/X11/lib') 2175 2176 # If Cygwin, then verify that X is installed before proceeding 2177 if CYGWIN: 2178 x11_inc = find_file('X11/Xlib.h', [], include_dirs) 2179 if x11_inc is None: 2180 return False 2181 2182 # Check for BLT extension 2183 if self.compiler.find_library_file(self.lib_dirs + added_lib_dirs, 2184 'BLT8.0'): 2185 defs.append( ('WITH_BLT', 1) ) 2186 libs.append('BLT8.0') 2187 elif self.compiler.find_library_file(self.lib_dirs + added_lib_dirs, 2188 'BLT'): 2189 defs.append( ('WITH_BLT', 1) ) 2190 libs.append('BLT') 2191 2192 # Add the Tcl/Tk libraries 2193 libs.append('tk'+ version) 2194 libs.append('tcl'+ version) 2195 2196 # Finally, link with the X11 libraries (not appropriate on cygwin) 2197 if not CYGWIN: 2198 libs.append('X11') 2199 2200 # XXX handle these, but how to detect? 2201 # *** Uncomment and edit for PIL (TkImaging) extension only: 2202 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \ 2203 # *** Uncomment and edit for TOGL extension only: 2204 # -DWITH_TOGL togl.c \ 2205 # *** Uncomment these for TOGL extension only: 2206 # -lGL -lGLU -lXext -lXmu \ 2207 2208 self.add(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'], 2209 define_macros=[('WITH_APPINIT', 1)] + defs, 2210 include_dirs=include_dirs, 2211 libraries=libs, 2212 library_dirs=added_lib_dirs)) 2213 return True 2214 2215 def configure_ctypes(self, ext): 2216 return True 2217 2218 def detect_ctypes(self): 2219 # Thomas Heller's _ctypes module 2220 2221 if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS): 2222 self.use_system_libffi = True 2223 else: 2224 self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS") 2225 2226 include_dirs = [] 2227 extra_compile_args = ['-DPy_BUILD_CORE_MODULE'] 2228 extra_link_args = [] 2229 sources = ['_ctypes/_ctypes.c', 2230 '_ctypes/callbacks.c', 2231 '_ctypes/callproc.c', 2232 '_ctypes/stgdict.c', 2233 '_ctypes/cfield.c'] 2234 depends = ['_ctypes/ctypes.h'] 2235 2236 if MACOS: 2237 sources.append('_ctypes/malloc_closure.c') 2238 extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1') 2239 extra_compile_args.append('-DMACOSX') 2240 include_dirs.append('_ctypes/darwin') 2241 2242 elif HOST_PLATFORM == 'sunos5': 2243 # XXX This shouldn't be necessary; it appears that some 2244 # of the assembler code is non-PIC (i.e. it has relocations 2245 # when it shouldn't. The proper fix would be to rewrite 2246 # the assembler code to be PIC. 2247 # This only works with GCC; the Sun compiler likely refuses 2248 # this option. If you want to compile ctypes with the Sun 2249 # compiler, please research a proper solution, instead of 2250 # finding some -z option for the Sun compiler. 2251 extra_link_args.append('-mimpure-text') 2252 2253 elif HOST_PLATFORM.startswith('hp-ux'): 2254 extra_link_args.append('-fPIC') 2255 2256 ext = Extension('_ctypes', 2257 include_dirs=include_dirs, 2258 extra_compile_args=extra_compile_args, 2259 extra_link_args=extra_link_args, 2260 libraries=[], 2261 sources=sources, 2262 depends=depends) 2263 self.add(ext) 2264 if TEST_EXTENSIONS: 2265 # function my_sqrt() needs libm for sqrt() 2266 self.add(Extension('_ctypes_test', 2267 sources=['_ctypes/_ctypes_test.c'], 2268 libraries=['m'])) 2269 2270 ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR") 2271 ffi_lib = None 2272 2273 ffi_inc_dirs = self.inc_dirs.copy() 2274 if MACOS: 2275 ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi") 2276 2277 if not ffi_inc: 2278 if os.path.exists(ffi_in_sdk): 2279 ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1") 2280 ffi_inc = ffi_in_sdk 2281 ffi_lib = 'ffi' 2282 else: 2283 # OS X 10.5 comes with libffi.dylib; the include files are 2284 # in /usr/include/ffi 2285 ffi_inc_dirs.append('/usr/include/ffi') 2286 2287 if not ffi_inc: 2288 found = find_file('ffi.h', [], ffi_inc_dirs) 2289 if found: 2290 ffi_inc = found[0] 2291 if ffi_inc: 2292 ffi_h = ffi_inc + '/ffi.h' 2293 if not os.path.exists(ffi_h): 2294 ffi_inc = None 2295 print('Header file {} does not exist'.format(ffi_h)) 2296 if ffi_lib is None and ffi_inc: 2297 for lib_name in ('ffi', 'ffi_pic'): 2298 if (self.compiler.find_library_file(self.lib_dirs, lib_name)): 2299 ffi_lib = lib_name 2300 break 2301 2302 if ffi_inc and ffi_lib: 2303 ffi_headers = glob(os.path.join(ffi_inc, '*.h')) 2304 if grep_headers_for('ffi_prep_cif_var', ffi_headers): 2305 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1") 2306 if grep_headers_for('ffi_prep_closure_loc', ffi_headers): 2307 ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1") 2308 if grep_headers_for('ffi_closure_alloc', ffi_headers): 2309 ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1") 2310 2311 ext.include_dirs.append(ffi_inc) 2312 ext.libraries.append(ffi_lib) 2313 self.use_system_libffi = True 2314 2315 if sysconfig.get_config_var('HAVE_LIBDL'): 2316 # for dlopen, see bpo-32647 2317 ext.libraries.append('dl') 2318 2319 def detect_decimal(self): 2320 # Stefan Krah's _decimal module 2321 extra_compile_args = [] 2322 undef_macros = [] 2323 if '--with-system-libmpdec' in sysconfig.get_config_var("CONFIG_ARGS"): 2324 include_dirs = [] 2325 libraries = ['mpdec'] 2326 sources = ['_decimal/_decimal.c'] 2327 depends = ['_decimal/docstrings.h'] 2328 else: 2329 include_dirs = [os.path.abspath(os.path.join(self.srcdir, 2330 'Modules', 2331 '_decimal', 2332 'libmpdec'))] 2333 libraries = ['m'] 2334 sources = [ 2335 '_decimal/_decimal.c', 2336 '_decimal/libmpdec/basearith.c', 2337 '_decimal/libmpdec/constants.c', 2338 '_decimal/libmpdec/context.c', 2339 '_decimal/libmpdec/convolute.c', 2340 '_decimal/libmpdec/crt.c', 2341 '_decimal/libmpdec/difradix2.c', 2342 '_decimal/libmpdec/fnt.c', 2343 '_decimal/libmpdec/fourstep.c', 2344 '_decimal/libmpdec/io.c', 2345 '_decimal/libmpdec/mpalloc.c', 2346 '_decimal/libmpdec/mpdecimal.c', 2347 '_decimal/libmpdec/numbertheory.c', 2348 '_decimal/libmpdec/sixstep.c', 2349 '_decimal/libmpdec/transpose.c', 2350 ] 2351 depends = [ 2352 '_decimal/docstrings.h', 2353 '_decimal/libmpdec/basearith.h', 2354 '_decimal/libmpdec/bits.h', 2355 '_decimal/libmpdec/constants.h', 2356 '_decimal/libmpdec/convolute.h', 2357 '_decimal/libmpdec/crt.h', 2358 '_decimal/libmpdec/difradix2.h', 2359 '_decimal/libmpdec/fnt.h', 2360 '_decimal/libmpdec/fourstep.h', 2361 '_decimal/libmpdec/io.h', 2362 '_decimal/libmpdec/mpalloc.h', 2363 '_decimal/libmpdec/mpdecimal.h', 2364 '_decimal/libmpdec/numbertheory.h', 2365 '_decimal/libmpdec/sixstep.h', 2366 '_decimal/libmpdec/transpose.h', 2367 '_decimal/libmpdec/typearith.h', 2368 '_decimal/libmpdec/umodarith.h', 2369 ] 2370 2371 config = { 2372 'x64': [('CONFIG_64','1'), ('ASM','1')], 2373 'uint128': [('CONFIG_64','1'), ('ANSI','1'), ('HAVE_UINT128_T','1')], 2374 'ansi64': [('CONFIG_64','1'), ('ANSI','1')], 2375 'ppro': [('CONFIG_32','1'), ('PPRO','1'), ('ASM','1')], 2376 'ansi32': [('CONFIG_32','1'), ('ANSI','1')], 2377 'ansi-legacy': [('CONFIG_32','1'), ('ANSI','1'), 2378 ('LEGACY_COMPILER','1')], 2379 'universal': [('UNIVERSAL','1')] 2380 } 2381 2382 cc = sysconfig.get_config_var('CC') 2383 sizeof_size_t = sysconfig.get_config_var('SIZEOF_SIZE_T') 2384 machine = os.environ.get('PYTHON_DECIMAL_WITH_MACHINE') 2385 2386 if machine: 2387 # Override automatic configuration to facilitate testing. 2388 define_macros = config[machine] 2389 elif MACOS: 2390 # Universal here means: build with the same options Python 2391 # was built with. 2392 define_macros = config['universal'] 2393 elif sizeof_size_t == 8: 2394 if sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X64'): 2395 define_macros = config['x64'] 2396 elif sysconfig.get_config_var('HAVE_GCC_UINT128_T'): 2397 define_macros = config['uint128'] 2398 else: 2399 define_macros = config['ansi64'] 2400 elif sizeof_size_t == 4: 2401 ppro = sysconfig.get_config_var('HAVE_GCC_ASM_FOR_X87') 2402 if ppro and ('gcc' in cc or 'clang' in cc) and \ 2403 not 'sunos' in HOST_PLATFORM: 2404 # solaris: problems with register allocation. 2405 # icc >= 11.0 works as well. 2406 define_macros = config['ppro'] 2407 extra_compile_args.append('-Wno-unknown-pragmas') 2408 else: 2409 define_macros = config['ansi32'] 2410 else: 2411 raise DistutilsError("_decimal: unsupported architecture") 2412 2413 # Workarounds for toolchain bugs: 2414 if sysconfig.get_config_var('HAVE_IPA_PURE_CONST_BUG'): 2415 # Some versions of gcc miscompile inline asm: 2416 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46491 2417 # https://gcc.gnu.org/ml/gcc/2010-11/msg00366.html 2418 extra_compile_args.append('-fno-ipa-pure-const') 2419 if sysconfig.get_config_var('HAVE_GLIBC_MEMMOVE_BUG'): 2420 # _FORTIFY_SOURCE wrappers for memmove and bcopy are incorrect: 2421 # https://sourceware.org/ml/libc-alpha/2010-12/msg00009.html 2422 undef_macros.append('_FORTIFY_SOURCE') 2423 2424 # Uncomment for extra functionality: 2425 #define_macros.append(('EXTRA_FUNCTIONALITY', 1)) 2426 self.add(Extension('_decimal', 2427 include_dirs=include_dirs, 2428 libraries=libraries, 2429 define_macros=define_macros, 2430 undef_macros=undef_macros, 2431 extra_compile_args=extra_compile_args, 2432 sources=sources, 2433 depends=depends)) 2434 2435 def detect_openssl_hashlib(self): 2436 # Detect SSL support for the socket module (via _ssl) 2437 config_vars = sysconfig.get_config_vars() 2438 2439 def split_var(name, sep): 2440 # poor man's shlex, the re module is not available yet. 2441 value = config_vars.get(name) 2442 if not value: 2443 return () 2444 # This trick works because ax_check_openssl uses --libs-only-L, 2445 # --libs-only-l, and --cflags-only-I. 2446 value = ' ' + value 2447 sep = ' ' + sep 2448 return [v.strip() for v in value.split(sep) if v.strip()] 2449 2450 openssl_includes = split_var('OPENSSL_INCLUDES', '-I') 2451 openssl_libdirs = split_var('OPENSSL_LDFLAGS', '-L') 2452 openssl_libs = split_var('OPENSSL_LIBS', '-l') 2453 openssl_rpath = config_vars.get('OPENSSL_RPATH') 2454 if not openssl_libs: 2455 # libssl and libcrypto not found 2456 self.missing.extend(['_ssl', '_hashlib']) 2457 return None, None 2458 2459 # Find OpenSSL includes 2460 ssl_incs = find_file( 2461 'openssl/ssl.h', self.inc_dirs, openssl_includes 2462 ) 2463 if ssl_incs is None: 2464 self.missing.extend(['_ssl', '_hashlib']) 2465 return None, None 2466 2467 if openssl_rpath == 'auto': 2468 runtime_library_dirs = openssl_libdirs[:] 2469 elif not openssl_rpath: 2470 runtime_library_dirs = [] 2471 else: 2472 runtime_library_dirs = [openssl_rpath] 2473 2474 openssl_extension_kwargs = dict( 2475 include_dirs=openssl_includes, 2476 library_dirs=openssl_libdirs, 2477 libraries=openssl_libs, 2478 runtime_library_dirs=runtime_library_dirs, 2479 ) 2480 2481 # This static linking is NOT OFFICIALLY SUPPORTED. 2482 # Requires static OpenSSL build with position-independent code. Some 2483 # features like DSO engines or external OSSL providers don't work. 2484 # Only tested on GCC and clang on X86_64. 2485 if os.environ.get("PY_UNSUPPORTED_OPENSSL_BUILD") == "static": 2486 extra_linker_args = [] 2487 for lib in openssl_extension_kwargs["libraries"]: 2488 # link statically 2489 extra_linker_args.append(f"-l:lib{lib}.a") 2490 # don't export symbols 2491 extra_linker_args.append(f"-Wl,--exclude-libs,lib{lib}.a") 2492 openssl_extension_kwargs["extra_link_args"] = extra_linker_args 2493 # don't link OpenSSL shared libraries. 2494 # include libz for OpenSSL build flavors with compression support 2495 openssl_extension_kwargs["libraries"] = ["z"] 2496 2497 self.add( 2498 Extension( 2499 '_ssl', 2500 ['_ssl.c'], 2501 depends=[ 2502 'socketmodule.h', 2503 '_ssl.h', 2504 '_ssl/debughelpers.c', 2505 '_ssl/misc.c', 2506 '_ssl/cert.c', 2507 ], 2508 **openssl_extension_kwargs 2509 ) 2510 ) 2511 self.add( 2512 Extension( 2513 '_hashlib', 2514 ['_hashopenssl.c'], 2515 depends=['hashlib.h'], 2516 **openssl_extension_kwargs, 2517 ) 2518 ) 2519 2520 def detect_hash_builtins(self): 2521 # By default we always compile these even when OpenSSL is available 2522 # (issue #14693). It's harmless and the object code is tiny 2523 # (40-50 KiB per module, only loaded when actually used). Modules can 2524 # be disabled via the --with-builtin-hashlib-hashes configure flag. 2525 supported = {"md5", "sha1", "sha256", "sha512", "sha3", "blake2"} 2526 2527 configured = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES") 2528 configured = configured.strip('"').lower() 2529 configured = { 2530 m.strip() for m in configured.split(",") 2531 } 2532 2533 self.disabled_configure.extend( 2534 sorted(supported.difference(configured)) 2535 ) 2536 2537 if "sha256" in configured: 2538 self.add(Extension( 2539 '_sha256', ['sha256module.c'], 2540 extra_compile_args=['-DPy_BUILD_CORE_MODULE'], 2541 depends=['hashlib.h'] 2542 )) 2543 2544 if "sha512" in configured: 2545 self.add(Extension( 2546 '_sha512', ['sha512module.c'], 2547 extra_compile_args=['-DPy_BUILD_CORE_MODULE'], 2548 depends=['hashlib.h'] 2549 )) 2550 2551 if "md5" in configured: 2552 self.add(Extension( 2553 '_md5', ['md5module.c'], 2554 depends=['hashlib.h'] 2555 )) 2556 2557 if "sha1" in configured: 2558 self.add(Extension( 2559 '_sha1', ['sha1module.c'], 2560 depends=['hashlib.h'] 2561 )) 2562 2563 if "blake2" in configured: 2564 blake2_deps = glob( 2565 os.path.join(escape(self.srcdir), 'Modules/_blake2/impl/*') 2566 ) 2567 blake2_deps.append('hashlib.h') 2568 self.add(Extension( 2569 '_blake2', 2570 [ 2571 '_blake2/blake2module.c', 2572 '_blake2/blake2b_impl.c', 2573 '_blake2/blake2s_impl.c' 2574 ], 2575 depends=blake2_deps 2576 )) 2577 2578 if "sha3" in configured: 2579 sha3_deps = glob( 2580 os.path.join(escape(self.srcdir), 'Modules/_sha3/kcp/*') 2581 ) 2582 sha3_deps.append('hashlib.h') 2583 self.add(Extension( 2584 '_sha3', 2585 ['_sha3/sha3module.c'], 2586 depends=sha3_deps 2587 )) 2588 2589 def detect_nis(self): 2590 if MS_WINDOWS or CYGWIN or HOST_PLATFORM == 'qnx6': 2591 self.missing.append('nis') 2592 return 2593 2594 libs = [] 2595 library_dirs = [] 2596 includes_dirs = [] 2597 2598 # bpo-32521: glibc has deprecated Sun RPC for some time. Fedora 28 2599 # moved headers and libraries to libtirpc and libnsl. The headers 2600 # are in tircp and nsl sub directories. 2601 rpcsvc_inc = find_file( 2602 'rpcsvc/yp_prot.h', self.inc_dirs, 2603 [os.path.join(inc_dir, 'nsl') for inc_dir in self.inc_dirs] 2604 ) 2605 rpc_inc = find_file( 2606 'rpc/rpc.h', self.inc_dirs, 2607 [os.path.join(inc_dir, 'tirpc') for inc_dir in self.inc_dirs] 2608 ) 2609 if rpcsvc_inc is None or rpc_inc is None: 2610 # not found 2611 self.missing.append('nis') 2612 return 2613 includes_dirs.extend(rpcsvc_inc) 2614 includes_dirs.extend(rpc_inc) 2615 2616 if self.compiler.find_library_file(self.lib_dirs, 'nsl'): 2617 libs.append('nsl') 2618 else: 2619 # libnsl-devel: check for libnsl in nsl/ subdirectory 2620 nsl_dirs = [os.path.join(lib_dir, 'nsl') for lib_dir in self.lib_dirs] 2621 libnsl = self.compiler.find_library_file(nsl_dirs, 'nsl') 2622 if libnsl is not None: 2623 library_dirs.append(os.path.dirname(libnsl)) 2624 libs.append('nsl') 2625 2626 if self.compiler.find_library_file(self.lib_dirs, 'tirpc'): 2627 libs.append('tirpc') 2628 2629 self.add(Extension('nis', ['nismodule.c'], 2630 libraries=libs, 2631 library_dirs=library_dirs, 2632 include_dirs=includes_dirs)) 2633 2634 2635class PyBuildInstall(install): 2636 # Suppress the warning about installation into the lib_dynload 2637 # directory, which is not in sys.path when running Python during 2638 # installation: 2639 def initialize_options (self): 2640 install.initialize_options(self) 2641 self.warn_dir=0 2642 2643 # Customize subcommands to not install an egg-info file for Python 2644 sub_commands = [('install_lib', install.has_lib), 2645 ('install_headers', install.has_headers), 2646 ('install_scripts', install.has_scripts), 2647 ('install_data', install.has_data)] 2648 2649 2650class PyBuildInstallLib(install_lib): 2651 # Do exactly what install_lib does but make sure correct access modes get 2652 # set on installed directories and files. All installed files with get 2653 # mode 644 unless they are a shared library in which case they will get 2654 # mode 755. All installed directories will get mode 755. 2655 2656 # this is works for EXT_SUFFIX too, which ends with SHLIB_SUFFIX 2657 shlib_suffix = sysconfig.get_config_var("SHLIB_SUFFIX") 2658 2659 def install(self): 2660 outfiles = install_lib.install(self) 2661 self.set_file_modes(outfiles, 0o644, 0o755) 2662 self.set_dir_modes(self.install_dir, 0o755) 2663 return outfiles 2664 2665 def set_file_modes(self, files, defaultMode, sharedLibMode): 2666 if not files: return 2667 2668 for filename in files: 2669 if os.path.islink(filename): continue 2670 mode = defaultMode 2671 if filename.endswith(self.shlib_suffix): mode = sharedLibMode 2672 log.info("changing mode of %s to %o", filename, mode) 2673 if not self.dry_run: os.chmod(filename, mode) 2674 2675 def set_dir_modes(self, dirname, mode): 2676 for dirpath, dirnames, fnames in os.walk(dirname): 2677 if os.path.islink(dirpath): 2678 continue 2679 log.info("changing mode of %s to %o", dirpath, mode) 2680 if not self.dry_run: os.chmod(dirpath, mode) 2681 2682 2683class PyBuildScripts(build_scripts): 2684 def copy_scripts(self): 2685 outfiles, updated_files = build_scripts.copy_scripts(self) 2686 fullversion = '-{0[0]}.{0[1]}'.format(sys.version_info) 2687 minoronly = '.{0[1]}'.format(sys.version_info) 2688 newoutfiles = [] 2689 newupdated_files = [] 2690 for filename in outfiles: 2691 if filename.endswith('2to3'): 2692 newfilename = filename + fullversion 2693 else: 2694 newfilename = filename + minoronly 2695 log.info(f'renaming {filename} to {newfilename}') 2696 os.rename(filename, newfilename) 2697 newoutfiles.append(newfilename) 2698 if filename in updated_files: 2699 newupdated_files.append(newfilename) 2700 return newoutfiles, newupdated_files 2701 2702 2703def main(): 2704 global LIST_MODULE_NAMES 2705 2706 if "--list-module-names" in sys.argv: 2707 LIST_MODULE_NAMES = True 2708 sys.argv.remove("--list-module-names") 2709 2710 set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST') 2711 set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST') 2712 2713 class DummyProcess: 2714 """Hack for parallel build""" 2715 ProcessPoolExecutor = None 2716 2717 sys.modules['concurrent.futures.process'] = DummyProcess 2718 validate_tzpath() 2719 2720 # turn off warnings when deprecated modules are imported 2721 import warnings 2722 warnings.filterwarnings("ignore",category=DeprecationWarning) 2723 setup(# PyPI Metadata (PEP 301) 2724 name = "Python", 2725 version = sys.version.split()[0], 2726 url = "https://www.python.org/%d.%d" % sys.version_info[:2], 2727 maintainer = "Guido van Rossum and the Python community", 2728 maintainer_email = "python-dev@python.org", 2729 description = "A high-level object-oriented programming language", 2730 long_description = SUMMARY.strip(), 2731 license = "PSF license", 2732 classifiers = [x for x in CLASSIFIERS.split("\n") if x], 2733 platforms = ["Many"], 2734 2735 # Build info 2736 cmdclass = {'build_ext': PyBuildExt, 2737 'build_scripts': PyBuildScripts, 2738 'install': PyBuildInstall, 2739 'install_lib': PyBuildInstallLib}, 2740 # The struct module is defined here, because build_ext won't be 2741 # called unless there's at least one extension module defined. 2742 ext_modules=[Extension('_struct', ['_struct.c'], 2743 extra_compile_args=['-DPy_BUILD_CORE_MODULE'])], 2744 2745 # If you change the scripts installed here, you also need to 2746 # check the PyBuildScripts command above, and change the links 2747 # created by the bininstall target in Makefile.pre.in 2748 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3", 2749 "Tools/scripts/2to3"] 2750 ) 2751 2752# --install-platlib 2753if __name__ == '__main__': 2754 main() 2755