PageRenderTime 598ms CodeModel.GetById 121ms app.highlight 343ms RepoModel.GetById 73ms app.codeStats 2ms

/setup.py

http://unladen-swallow.googlecode.com/
Python | 1966 lines | 1392 code | 200 blank | 374 comment | 364 complexity | 876f60a7a1997b71b1e8eb54e199eaf4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1# Autodetecting setup.py script for building the Python extensions
   2#
   3
   4__version__ = "$Revision: 75282 $"
   5
   6import sys, os, imp, re, optparse
   7from glob import glob
   8from platform import machine as platform_machine
   9
  10from distutils import log
  11from distutils import sysconfig
  12from distutils import text_file
  13from distutils.errors import *
  14from distutils.core import Extension, setup
  15from distutils.command.build_ext import build_ext
  16from distutils.command.install import install
  17from distutils.command.install_lib import install_lib
  18from distutils.util import get_platform
  19
  20# This global variable is used to hold the list of modules to be disabled.
  21disabled_module_list = []
  22
  23def record_build_dir():
  24    """Record the relative path to the build diretory for site.py."""
  25    build_dir = "build/lib.%s-%.3s" % (get_platform(), sys.version)
  26
  27    build_dir_file = os.path.join(os.path.dirname(sys.executable), "build_dir")
  28    f = open(build_dir_file, "w")
  29    try:
  30        f.write(build_dir)
  31    finally:
  32        f.close()
  33
  34def add_dir_to_list(dirlist, dir):
  35    """Add the directory 'dir' to the list 'dirlist' (at the front) if
  36    1) 'dir' is not already in 'dirlist'
  37    2) 'dir' actually exists, and is a directory."""
  38    if dir is not None and os.path.isdir(dir) and dir not in dirlist:
  39        dirlist.insert(0, dir)
  40
  41def find_file(filename, std_dirs, paths):
  42    """Searches for the directory where a given file is located,
  43    and returns a possibly-empty list of additional directories, or None
  44    if the file couldn't be found at all.
  45
  46    'filename' is the name of a file, such as readline.h or libcrypto.a.
  47    'std_dirs' is the list of standard system directories; if the
  48        file is found in one of them, no additional directives are needed.
  49    'paths' is a list of additional locations to check; if the file is
  50        found in one of them, the resulting list will contain the directory.
  51    """
  52
  53    # Check the standard locations
  54    for dir in std_dirs:
  55        f = os.path.join(dir, filename)
  56        if os.path.exists(f): return []
  57
  58    # Check the additional directories
  59    for dir in paths:
  60        f = os.path.join(dir, filename)
  61        if os.path.exists(f):
  62            return [dir]
  63
  64    # Not found anywhere
  65    return None
  66
  67def find_library_file(compiler, libname, std_dirs, paths):
  68    result = compiler.find_library_file(std_dirs + paths, libname)
  69    if result is None:
  70        return None
  71
  72    # Check whether the found file is in one of the standard directories
  73    dirname = os.path.dirname(result)
  74    for p in std_dirs:
  75        # Ensure path doesn't end with path separator
  76        p = p.rstrip(os.sep)
  77        if p == dirname:
  78            return [ ]
  79
  80    # Otherwise, it must have been in one of the additional directories,
  81    # so we have to figure out which one.
  82    for p in paths:
  83        # Ensure path doesn't end with path separator
  84        p = p.rstrip(os.sep)
  85        if p == dirname:
  86            return [p]
  87    else:
  88        assert False, "Internal error: Path not found in std_dirs or paths"
  89
  90def module_enabled(extlist, modname):
  91    """Returns whether the module 'modname' is present in the list
  92    of extensions 'extlist'."""
  93    extlist = [ext for ext in extlist if ext.name == modname]
  94    return len(extlist)
  95
  96def find_module_file(module, dirlist):
  97    """Find a module in a set of possible folders. If it is not found
  98    return the unadorned filename"""
  99    list = find_file(module, [], dirlist)
 100    if not list:
 101        return module
 102    if len(list) > 1:
 103        log.info("WARNING: multiple copies of %s found"%module)
 104    return os.path.join(list[0], module)
 105
 106def combine_dirs_to_check(extra_dirs, orig_lib_dirs):
 107    extra_dirs = [d for d in extra_dirs if d and os.path.isdir(d)]
 108    # First search extra directories that already appear in the
 109    # original list.
 110    extra_dirs.sort(key=lambda d:0 if d in orig_lib_dirs else 1)
 111    return extra_dirs + orig_lib_dirs
 112
 113class PyBuildExt(build_ext):
 114
 115    def __init__(self, dist):
 116        build_ext.__init__(self, dist)
 117        self.failed = []
 118
 119    def build_extensions(self):
 120
 121        # Detect which modules should be compiled
 122        missing = self.detect_modules()
 123
 124        # Remove modules that are present on the disabled list
 125        extensions = [ext for ext in self.extensions
 126                      if ext.name not in disabled_module_list]
 127        # move ctypes to the end, it depends on other modules
 128        ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
 129        if "_ctypes" in ext_map:
 130            ctypes = extensions.pop(ext_map["_ctypes"])
 131            extensions.append(ctypes)
 132        self.extensions = extensions
 133
 134        # Fix up the autodetected modules, prefixing all the source files
 135        # with Modules/ and adding Python's include directory to the path.
 136        (srcdir,) = sysconfig.get_config_vars('srcdir')
 137        if not srcdir:
 138            # Maybe running on Windows but not using CYGWIN?
 139            raise ValueError("No source directory; cannot proceed.")
 140
 141        # Figure out the location of the source code for extension modules
 142        # (This logic is copied in distutils.test.test_sysconfig,
 143        # so building in a separate directory does not break test_distutils.)
 144        moddir = os.path.join(os.getcwd(), srcdir, 'Modules')
 145        moddir = os.path.normpath(moddir)
 146        srcdir, tail = os.path.split(moddir)
 147        srcdir = os.path.normpath(srcdir)
 148        moddir = os.path.normpath(moddir)
 149
 150        moddirlist = [moddir]
 151        incdirlist = ['./Include']
 152
 153        # Platform-dependent module source and include directories
 154        platform = self.get_platform()
 155        if platform in ('darwin', 'mac') and ("--disable-toolbox-glue" not in
 156            sysconfig.get_config_var("CONFIG_ARGS")):
 157            # Mac OS X also includes some mac-specific modules
 158            macmoddir = os.path.join(os.getcwd(), srcdir, 'Mac/Modules')
 159            moddirlist.append(macmoddir)
 160            incdirlist.append('./Mac/Include')
 161
 162        alldirlist = moddirlist + incdirlist
 163
 164        # Fix up the paths for scripts, too
 165        self.distribution.scripts = [os.path.join(srcdir, filename)
 166                                     for filename in self.distribution.scripts]
 167
 168        # Python header files
 169        headers = glob("Include/*.h") + ["pyconfig.h"]
 170
 171        for ext in self.extensions[:]:
 172            ext.sources = [ find_module_file(filename, moddirlist)
 173                            for filename in ext.sources ]
 174            if ext.depends is not None:
 175                ext.depends = [find_module_file(filename, alldirlist)
 176                               for filename in ext.depends]
 177            else:
 178                ext.depends = []
 179            # re-compile extensions if a header file has been changed
 180            ext.depends.extend(headers)
 181
 182            ext.include_dirs.append( '.' ) # to get config.h
 183            for incdir in incdirlist:
 184                ext.include_dirs.append( os.path.join(srcdir, incdir) )
 185
 186            # If a module has already been built statically,
 187            # don't build it here
 188            if ext.name in sys.builtin_module_names:
 189                self.extensions.remove(ext)
 190
 191        if platform != 'mac':
 192            # Parse Modules/Setup and Modules/Setup.local to figure out which
 193            # modules are turned on in the file.
 194            remove_modules = []
 195            for filename in ('Modules/Setup', 'Modules/Setup.local'):
 196                input = text_file.TextFile(filename, join_lines=1)
 197                while 1:
 198                    line = input.readline()
 199                    if not line: break
 200                    line = line.split()
 201                    remove_modules.append(line[0])
 202                input.close()
 203
 204            for ext in self.extensions[:]:
 205                if ext.name in remove_modules:
 206                    self.extensions.remove(ext)
 207
 208        # When you run "make CC=altcc" or something similar, you really want
 209        # those environment variables passed into the setup.py phase.  Here's
 210        # a small set of useful ones.
 211        compiler = os.environ.get('CC')
 212        args = {}
 213        # unfortunately, distutils doesn't let us provide separate C and C++
 214        # compilers
 215        if compiler is not None:
 216            (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
 217            args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
 218        self.compiler.set_executables(**args)
 219
 220        build_ext.build_extensions(self)
 221
 222        longest = max([len(e.name) for e in self.extensions])
 223        if self.failed:
 224            longest = max(longest, max([len(name) for name in self.failed]))
 225
 226        def print_three_column(lst):
 227            lst.sort(key=str.lower)
 228            # guarantee zip() doesn't drop anything
 229            while len(lst) % 3:
 230                lst.append("")
 231            for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
 232                print "%-*s   %-*s   %-*s" % (longest, e, longest, f,
 233                                              longest, g)
 234
 235        if missing:
 236            print
 237            print "Failed to find the necessary bits to build these modules:"
 238            print_three_column(missing)
 239            print ("To find the necessary bits, look in setup.py in"
 240                   " detect_modules() for the module's name.")
 241            print
 242
 243        if self.failed:
 244            failed = self.failed[:]
 245            print
 246            print "Failed to build these modules:"
 247            print_three_column(failed)
 248            print
 249
 250    def build_extension(self, ext):
 251
 252        if ext.name == '_ctypes':
 253            if not self.configure_ctypes(ext):
 254                return
 255
 256        try:
 257            build_ext.build_extension(self, ext)
 258        except (CCompilerError, DistutilsError), why:
 259            self.announce('WARNING: building of extension "%s" failed: %s' %
 260                          (ext.name, sys.exc_info()[1]))
 261            self.failed.append(ext.name)
 262            return
 263        # Workaround for Mac OS X: The Carbon-based modules cannot be
 264        # reliably imported into a command-line Python
 265        if 'Carbon' in ext.extra_link_args:
 266            self.announce(
 267                'WARNING: skipping import check for Carbon-based "%s"' %
 268                ext.name)
 269            return
 270
 271        if self.get_platform() == 'darwin' and (
 272                sys.maxint > 2**32 and '-arch' in ext.extra_link_args):
 273            # Don't bother doing an import check when an extension was
 274            # build with an explicit '-arch' flag on OSX. That's currently
 275            # only used to build 32-bit only extensions in a 4-way
 276            # universal build and loading 32-bit code into a 64-bit
 277            # process will fail.
 278            self.announce(
 279                'WARNING: skipping import check for "%s"' %
 280                ext.name)
 281            return
 282
 283        # Workaround for Cygwin: Cygwin currently has fork issues when many
 284        # modules have been imported
 285        if self.get_platform() == 'cygwin':
 286            self.announce('WARNING: skipping import check for Cygwin-based "%s"'
 287                % ext.name)
 288            return
 289        ext_filename = os.path.join(
 290            self.build_lib,
 291            self.get_ext_filename(self.get_ext_fullname(ext.name)))
 292        try:
 293            imp.load_dynamic(ext.name, ext_filename)
 294        except ImportError, why:
 295            self.failed.append(ext.name)
 296            self.announce('*** WARNING: renaming "%s" since importing it'
 297                          ' failed: %s' % (ext.name, why), level=3)
 298            assert not self.inplace
 299            basename, tail = os.path.splitext(ext_filename)
 300            newname = basename + "_failed" + tail
 301            if os.path.exists(newname):
 302                os.remove(newname)
 303            os.rename(ext_filename, newname)
 304
 305            # XXX -- This relies on a Vile HACK in
 306            # distutils.command.build_ext.build_extension().  The
 307            # _built_objects attribute is stored there strictly for
 308            # use here.
 309            # If there is a failure, _built_objects may not be there,
 310            # so catch the AttributeError and move on.
 311            try:
 312                for filename in self._built_objects:
 313                    os.remove(filename)
 314            except AttributeError:
 315                self.announce('unable to remove files (ignored)')
 316        except:
 317            exc_type, why, tb = sys.exc_info()
 318            self.announce('*** WARNING: importing extension "%s" '
 319                          'failed with %s: %s' % (ext.name, exc_type, why),
 320                          level=3)
 321            self.failed.append(ext.name)
 322
 323    def get_platform(self):
 324        # Get value of sys.platform
 325        for platform in ['cygwin', 'beos', 'darwin', 'atheos', 'osf1']:
 326            if sys.platform.startswith(platform):
 327                return platform
 328        return sys.platform
 329
 330    def detect_modules(self):
 331        use_system_paths = not os.getenv('IGNORE_SYSTEM_PATHS')
 332
 333        if use_system_paths:
 334            # Ensure that /usr/local is always used
 335            add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
 336            add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
 337
 338        # Add paths specified in the environment variables LDFLAGS and
 339        # CPPFLAGS for header and library files.
 340        # We must get the values from the Makefile and not the environment
 341        # directly since an inconsistently reproducible issue comes up where
 342        # the environment variable is not set even though the value were passed
 343        # into configure and stored in the Makefile (issue found on OS X 10.3).
 344        for env_var, arg_name, dir_list in (
 345                ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
 346                ('LDFLAGS', '-L', self.compiler.library_dirs),
 347                ('CPPFLAGS', '-I', self.compiler.include_dirs)):
 348            env_val = sysconfig.get_config_var(env_var)
 349            if env_val:
 350                # To prevent optparse from raising an exception about any
 351                # options in env_val that it doesn't know about we strip out
 352                # all double dashes and any dashes followed by a character
 353                # that is not for the option we are dealing with.
 354                #
 355                # Please note that order of the regex is important!  We must
 356                # strip out double-dashes first so that we don't end up with
 357                # substituting "--Long" to "-Long" and thus lead to "ong" being
 358                # used for a library directory.
 359                env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
 360                                 ' ', env_val)
 361                parser = optparse.OptionParser()
 362                # Make sure that allowing args interspersed with options is
 363                # allowed
 364                parser.allow_interspersed_args = True
 365                parser.error = lambda msg: None
 366                parser.add_option(arg_name, dest="dirs", action="append")
 367                options = parser.parse_args(env_val.split())[0]
 368                if options.dirs:
 369                    for directory in reversed(options.dirs):
 370                        add_dir_to_list(dir_list, directory)
 371
 372        if os.path.normpath(sys.prefix) != '/usr':
 373            add_dir_to_list(self.compiler.library_dirs,
 374                            sysconfig.get_config_var("LIBDIR"))
 375            add_dir_to_list(self.compiler.include_dirs,
 376                            sysconfig.get_config_var("INCLUDEDIR"))
 377
 378        try:
 379            have_unicode = unicode
 380        except NameError:
 381            have_unicode = 0
 382
 383        # lib_dirs and inc_dirs are used to search for files;
 384        # if a file is found in one of those directories, it can
 385        # be assumed that no additional -I,-L directives are needed.
 386        lib_dirs = list(self.compiler.library_dirs)
 387        inc_dirs = list(self.compiler.include_dirs)
 388        if use_system_paths:
 389            lib_dirs += [
 390                '/lib64', '/usr/lib64',
 391                '/lib', '/usr/lib',
 392                ]
 393            inc_dirs += ['/usr/include']
 394        exts = []
 395        missing = []
 396
 397        config_h = sysconfig.get_config_h_filename()
 398        config_h_vars = sysconfig.parse_config_h(open(config_h))
 399
 400        platform = self.get_platform()
 401        (srcdir,) = sysconfig.get_config_vars('srcdir')
 402
 403        # Check for AtheOS which has libraries in non-standard locations
 404        if platform == 'atheos':
 405            if use_system_paths:
 406                lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
 407                lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
 408                inc_dirs += ['/system/include', '/atheos/autolnk/include']
 409                inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
 410
 411        # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
 412        if (platform in ['osf1', 'unixware7', 'openunix8'] and
 413            use_system_paths):
 414            lib_dirs += ['/usr/ccs/lib']
 415
 416        if platform == 'darwin' or platform == 'linux2':
 417            # This should work on any unixy platform ;-)
 418            # If the user has bothered specifying additional -I and -L flags
 419            # in OPT and LDFLAGS we might as well use them here.
 420            #   NOTE: using shlex.split would technically be more correct, but
 421            # also gives a bootstrap problem. Let's hope nobody uses directories
 422            # with whitespace in the name to store libraries.
 423            cflags, ldflags = sysconfig.get_config_vars(
 424                    'CFLAGS', 'LDFLAGS')
 425            for item in cflags.split():
 426                if item.startswith('-I'):
 427                    inc_dirs.append(item[2:])
 428
 429            for item in ldflags.split():
 430                if item.startswith('-L'):
 431                    lib_dirs.append(item[2:])
 432
 433        # Check for MacOS X, which doesn't need libm.a at all
 434        math_libs = ['m']
 435        if platform in ['darwin', 'beos', 'mac']:
 436            math_libs = []
 437
 438        # XXX Omitted modules: gl, pure, dl, SGI-specific modules
 439
 440        #
 441        # The following modules are all pretty straightforward, and compile
 442        # on pretty much any POSIXish platform.
 443        #
 444
 445        # Some modules that are normally always on:
 446        exts.append( Extension('_weakref', ['_weakref.c']) )
 447
 448        # array objects
 449        exts.append( Extension('array', ['arraymodule.c']) )
 450        # complex math library functions
 451        exts.append( Extension('cmath', ['cmathmodule.c'],
 452                               libraries=math_libs) )
 453
 454        # math library functions, e.g. sin()
 455        exts.append( Extension('math',  ['mathmodule.c'],
 456                               libraries=math_libs) )
 457        # fast string operations implemented in C
 458        exts.append( Extension('strop', ['stropmodule.c']) )
 459        # time operations and variables
 460        exts.append( Extension('time', ['timemodule.c'],
 461                               libraries=math_libs) )
 462        exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'],
 463                               libraries=math_libs) )
 464        # fast iterator tools implemented in C
 465        exts.append( Extension("itertools", ["itertoolsmodule.c"]) )
 466        # code that will be builtins in the future, but conflict with the
 467        #  current builtins
 468        exts.append( Extension('future_builtins', ['future_builtins.c']) )
 469        # random number generator implemented in C
 470        exts.append( Extension("_random", ["_randommodule.c"]) )
 471        # high-performance collections
 472        exts.append( Extension("_collections", ["_collectionsmodule.c"]) )
 473        # bisect
 474        exts.append( Extension("_bisect", ["_bisectmodule.c"]) )
 475        # heapq
 476        exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
 477        # operator.add() and similar goodies
 478        exts.append( Extension('operator', ['operator.c']) )
 479        # Python 3.0 _fileio module
 480        exts.append( Extension("_fileio", ["_fileio.c"]) )
 481        # Python 3.0 _bytesio module
 482        exts.append( Extension("_bytesio", ["_bytesio.c"]) )
 483        # _functools
 484        exts.append( Extension("_functools", ["_functoolsmodule.c"]) )
 485        # _json speedups
 486        exts.append( Extension("_json", ["_json.c"]) )
 487        # Python C API test module
 488        exts.append( Extension('_testcapi', ['_testcapimodule.c', 'cPickle.c'],
 489                               define_macros=[('NO_STATIC_MEMOTABLE', 1)],
 490                               depends=['testcapi_long.h']) )
 491        # profilers (_lsprof is for cProfile.py)
 492        exts.append( Extension('_hotshot', ['_hotshot.c']) )
 493        exts.append( Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']) )
 494        # static Unicode character database
 495        if have_unicode:
 496            exts.append( Extension('unicodedata', ['unicodedata.c']) )
 497        else:
 498            missing.append('unicodedata')
 499        # access to ISO C locale support
 500        data = open('pyconfig.h').read()
 501        m = re.search(r"#s*define\s+WITH_LIBINTL\s+1\s*", data)
 502        if m is not None:
 503            locale_libs = ['intl']
 504        else:
 505            locale_libs = []
 506        if platform == 'darwin':
 507            locale_extra_link_args = ['-framework', 'CoreFoundation']
 508        else:
 509            locale_extra_link_args = []
 510
 511
 512        exts.append( Extension('_locale', ['_localemodule.c'],
 513                               libraries=locale_libs,
 514                               extra_link_args=locale_extra_link_args) )
 515
 516        # Modules with some UNIX dependencies -- on by default:
 517        # (If you have a really backward UNIX, select and socket may not be
 518        # supported...)
 519
 520        # fcntl(2) and ioctl(2)
 521        exts.append( Extension('fcntl', ['fcntlmodule.c']) )
 522        if platform not in ['mac']:
 523            # pwd(3)
 524            exts.append( Extension('pwd', ['pwdmodule.c']) )
 525            # grp(3)
 526            exts.append( Extension('grp', ['grpmodule.c']) )
 527            # spwd, shadow passwords
 528            if (config_h_vars.get('HAVE_GETSPNAM', False) or
 529                    config_h_vars.get('HAVE_GETSPENT', False)):
 530                exts.append( Extension('spwd', ['spwdmodule.c']) )
 531            else:
 532                missing.append('spwd')
 533        else:
 534            missing.extend(['pwd', 'grp', 'spwd'])
 535
 536        # select(2); not on ancient System V
 537        exts.append( Extension('select', ['selectmodule.c']) )
 538
 539        # Fred Drake's interface to the Python parser
 540        exts.append( Extension('parser', ['parsermodule.c']) )
 541
 542        # cStringIO and cPickle
 543        exts.append( Extension('cStringIO', ['cStringIO.c']) )
 544        exts.append( Extension('cPickle', ['cPickle.c']) )
 545
 546        # Memory-mapped files (also works on Win32).
 547        if platform not in ['atheos', 'mac']:
 548            exts.append( Extension('mmap', ['mmapmodule.c']) )
 549        else:
 550            missing.append('mmap')
 551
 552        # Lance Ellinghaus's syslog module
 553        if platform not in ['mac']:
 554            # syslog daemon interface
 555            exts.append( Extension('syslog', ['syslogmodule.c']) )
 556        else:
 557            missing.append('syslog')
 558
 559        # George Neville-Neil's timing module:
 560        # Deprecated in PEP 4 http://www.python.org/peps/pep-0004.html
 561        # http://mail.python.org/pipermail/python-dev/2006-January/060023.html
 562        #exts.append( Extension('timing', ['timingmodule.c']) )
 563
 564        #
 565        # Here ends the simple stuff.  From here on, modules need certain
 566        # libraries, are platform-specific, or present other surprises.
 567        #
 568
 569        # Multimedia modules
 570        # These don't work for 64-bit platforms!!!
 571        # These represent audio samples or images as strings:
 572
 573        # Operations on audio samples
 574        # According to #993173, this one should actually work fine on
 575        # 64-bit platforms.
 576        exts.append( Extension('audioop', ['audioop.c']) )
 577
 578        # Disabled on 64-bit platforms
 579        if sys.maxint != 9223372036854775807L:
 580            # Operations on images
 581            exts.append( Extension('imageop', ['imageop.c']) )
 582        else:
 583            missing.extend(['imageop'])
 584
 585        # readline
 586        do_readline = self.compiler.find_library_file(lib_dirs, 'readline')
 587        if platform == 'darwin': # and os.uname()[2] < '9.':
 588            # MacOSX 10.4 has a broken readline. Don't try to build
 589            # the readline module unless the user has installed a fixed
 590            # readline package
 591            # FIXME: The readline emulation on 10.5 is better, but the
 592            # readline module doesn't compile out of the box.
 593            if find_file('readline/rlconf.h', inc_dirs, []) is None:
 594                do_readline = False
 595        if do_readline:
 596            if sys.platform == 'darwin':
 597                # In every directory on the search path search for a dynamic
 598                # library and then a static library, instead of first looking
 599                # for dynamic libraries on the entiry path.
 600                # This way a staticly linked custom readline gets picked up
 601                # before the (broken) dynamic library in /usr/lib.
 602                readline_extra_link_args = ('-Wl,-search_paths_first',)
 603            else:
 604                readline_extra_link_args = ()
 605
 606            readline_libs = ['readline']
 607            if self.compiler.find_library_file(lib_dirs,
 608                                                 'ncursesw'):
 609                readline_libs.append('ncursesw')
 610            elif self.compiler.find_library_file(lib_dirs,
 611                                                 'ncurses'):
 612                readline_libs.append('ncurses')
 613            elif self.compiler.find_library_file(lib_dirs, 'curses'):
 614                readline_libs.append('curses')
 615            elif self.compiler.find_library_file(lib_dirs +
 616                                               ['/usr/lib/termcap'],
 617                                               'termcap'):
 618                readline_libs.append('termcap')
 619            exts.append( Extension('readline', ['readline.c'],
 620                                   library_dirs=['/usr/lib/termcap'],
 621                                   extra_link_args=readline_extra_link_args,
 622                                   libraries=readline_libs) )
 623        else:
 624            missing.append('readline')
 625
 626        if platform not in ['mac']:
 627            # crypt module.
 628
 629            if self.compiler.find_library_file(lib_dirs, 'crypt'):
 630                libs = ['crypt']
 631            else:
 632                libs = []
 633            exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
 634        else:
 635            missing.append('crypt')
 636
 637        # CSV files
 638        exts.append( Extension('_csv', ['_csv.c']) )
 639
 640        # LLVM wrappers
 641        if sysconfig.get_config_var("WITH_LLVM"):
 642            exts.append( Extension('_llvm', ['_llvm.c']))
 643        else:
 644            missing.append('_llvm')
 645
 646        # socket(2)
 647        exts.append( Extension('_socket', ['socketmodule.c'],
 648                               depends = ['socketmodule.h']) )
 649        # Detect SSL support for the socket module (via _ssl)
 650        search_for_ssl_incs_in = []
 651        if use_system_paths:
 652            search_for_ssl_incs_in = [
 653                              '/usr/local/ssl/include',
 654                              '/usr/contrib/ssl/include/'
 655                             ]
 656        if "SSL_ROOT" in os.environ:
 657            search_for_ssl_incs_in.append(os.path.join(os.environ["SSL_ROOT"],
 658                                                       "include"))
 659        ssl_incs = find_file('openssl/ssl.h', inc_dirs,
 660                             search_for_ssl_incs_in
 661                             )
 662        if ssl_incs is not None:
 663            krb5_h = find_file(
 664                'krb5.h', inc_dirs,
 665                ['/usr/kerberos/include'] if use_system_paths else [])
 666            if krb5_h:
 667                ssl_incs += krb5_h
 668        search_for_ssl_libs_in = []
 669        if use_system_paths:
 670            search_for_ssl_libs_in = ['/usr/local/ssl/lib',
 671                                      '/usr/contrib/ssl/lib/']
 672        if "SSL_ROOT" in os.environ:
 673            search_for_ssl_libs_in.append(os.path.join(os.environ["SSL_ROOT"],
 674                                                       "lib"))
 675        ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
 676                                     search_for_ssl_libs_in)
 677
 678        if (ssl_incs is not None and
 679            ssl_libs is not None):
 680            exts.append( Extension('_ssl', ['_ssl.c'],
 681                                   include_dirs = ssl_incs,
 682                                   library_dirs = ssl_libs,
 683                                   libraries = ['ssl', 'crypto'],
 684                                   depends = ['socketmodule.h']), )
 685        else:
 686            missing.append('_ssl')
 687
 688        # find out which version of OpenSSL we have
 689        openssl_ver = 0
 690        openssl_ver_re = re.compile(
 691            '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
 692        for ssl_inc_dir in inc_dirs + search_for_ssl_incs_in:
 693            name = os.path.join(ssl_inc_dir, 'openssl', 'opensslv.h')
 694            if os.path.isfile(name):
 695                try:
 696                    incfile = open(name, 'r')
 697                    for line in incfile:
 698                        m = openssl_ver_re.match(line)
 699                        if m:
 700                            openssl_ver = eval(m.group(1))
 701                            break
 702                except IOError:
 703                    pass
 704
 705            # first version found is what we'll use (as the compiler should)
 706            if openssl_ver:
 707                break
 708
 709        #print 'openssl_ver = 0x%08x' % openssl_ver
 710
 711        if (ssl_incs is not None and
 712            ssl_libs is not None and
 713            openssl_ver >= 0x00907000):
 714            # The _hashlib module wraps optimized implementations
 715            # of hash functions from the OpenSSL library.
 716            exts.append( Extension('_hashlib', ['_hashopenssl.c'],
 717                                   include_dirs = ssl_incs,
 718                                   library_dirs = ssl_libs,
 719                                   libraries = ['ssl', 'crypto']) )
 720            # these aren't strictly missing since they are unneeded.
 721            #missing.extend(['_sha', '_md5'])
 722        else:
 723            # The _sha module implements the SHA1 hash algorithm.
 724            exts.append( Extension('_sha', ['shamodule.c']) )
 725            # The _md5 module implements the RSA Data Security, Inc. MD5
 726            # Message-Digest Algorithm, described in RFC 1321.  The
 727            # necessary files md5.c and md5.h are included here.
 728            exts.append( Extension('_md5',
 729                            sources = ['md5module.c', 'md5.c'],
 730                            depends = ['md5.h']) )
 731            missing.append('_hashlib')
 732
 733        if (openssl_ver < 0x00908000):
 734            # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
 735            exts.append( Extension('_sha256', ['sha256module.c']) )
 736            exts.append( Extension('_sha512', ['sha512module.c']) )
 737
 738        # Modules that provide persistent dictionary-like semantics.  You will
 739        # probably want to arrange for at least one of them to be available on
 740        # your machine, though none are defined by default because of library
 741        # dependencies.  The Python module anydbm.py provides an
 742        # implementation independent wrapper for these; dumbdbm.py provides
 743        # similar functionality (but slower of course) implemented in Python.
 744
 745        # Sleepycat^WOracle Berkeley DB interface.
 746        #  http://www.oracle.com/database/berkeley-db/db/index.html
 747        #
 748        # This requires the Sleepycat^WOracle DB code. The supported versions
 749        # are set below.  Visit the URL above to download
 750        # a release.  Most open source OSes come with one or more
 751        # versions of BerkeleyDB already installed.
 752
 753        max_db_ver = (4, 7)
 754        min_db_ver = (3, 3)
 755        db_setup_debug = False   # verbose debug prints from this script?
 756
 757        def allow_db_ver(db_ver):
 758            """Returns a boolean if the given BerkeleyDB version is acceptable.
 759
 760            Args:
 761              db_ver: A tuple of the version to verify.
 762            """
 763            if not (min_db_ver <= db_ver <= max_db_ver):
 764                return False
 765            # Use this function to filter out known bad configurations.
 766            if (4, 6) == db_ver[:2]:
 767                # BerkeleyDB 4.6.x is not stable on many architectures.
 768                arch = platform_machine()
 769                if arch not in ('i386', 'i486', 'i586', 'i686',
 770                                'x86_64', 'ia64'):
 771                    return False
 772            return True
 773
 774        def gen_db_minor_ver_nums(major):
 775            if major == 4:
 776                for x in range(max_db_ver[1]+1):
 777                    if allow_db_ver((4, x)):
 778                        yield x
 779            elif major == 3:
 780                for x in (3,):
 781                    if allow_db_ver((3, x)):
 782                        yield x
 783            else:
 784                raise ValueError("unknown major BerkeleyDB version", major)
 785
 786        # construct a list of paths to look for the header file in on
 787        # top of the normal inc_dirs.
 788        db_inc_paths = []
 789        if use_system_paths:
 790            db_inc_paths.extend([
 791                '/usr/include/db4',
 792                '/usr/local/include/db4',
 793                '/opt/sfw/include/db4',
 794                '/usr/include/db3',
 795                '/usr/local/include/db3',
 796                '/opt/sfw/include/db3',
 797                # Fink defaults (http://fink.sourceforge.net/)
 798                '/sw/include/db4',
 799                '/sw/include/db3',
 800                ])
 801            # 4.x minor number specific paths
 802            for x in gen_db_minor_ver_nums(4):
 803                db_inc_paths.append('/usr/include/db4%d' % x)
 804                db_inc_paths.append('/usr/include/db4.%d' % x)
 805                db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
 806                db_inc_paths.append('/usr/local/include/db4%d' % x)
 807                db_inc_paths.append('/pkg/db-4.%d/include' % x)
 808                db_inc_paths.append('/opt/db-4.%d/include' % x)
 809                # MacPorts default (http://www.macports.org/)
 810                db_inc_paths.append('/opt/local/include/db4%d' % x)
 811            # 3.x minor number specific paths
 812            for x in gen_db_minor_ver_nums(3):
 813                db_inc_paths.append('/usr/include/db3%d' % x)
 814                db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
 815                db_inc_paths.append('/usr/local/include/db3%d' % x)
 816                db_inc_paths.append('/pkg/db-3.%d/include' % x)
 817                db_inc_paths.append('/opt/db-3.%d/include' % x)
 818
 819        # Add some common subdirectories for Sleepycat DB to the list,
 820        # based on the standard include directories. This way DB3/4 gets
 821        # picked up when it is installed in a non-standard prefix and
 822        # the user has added that prefix into inc_dirs.
 823        std_variants = []
 824        for dn in inc_dirs:
 825            std_variants.append(os.path.join(dn, 'db3'))
 826            std_variants.append(os.path.join(dn, 'db4'))
 827            for x in gen_db_minor_ver_nums(4):
 828                std_variants.append(os.path.join(dn, "db4%d"%x))
 829                std_variants.append(os.path.join(dn, "db4.%d"%x))
 830            for x in gen_db_minor_ver_nums(3):
 831                std_variants.append(os.path.join(dn, "db3%d"%x))
 832                std_variants.append(os.path.join(dn, "db3.%d"%x))
 833
 834        db_inc_paths = std_variants + db_inc_paths
 835        db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
 836
 837        db_ver_inc_map = {}
 838
 839        class db_found(Exception): pass
 840        try:
 841            # See whether there is a Sleepycat header in the standard
 842            # search path.
 843            for d in inc_dirs + db_inc_paths:
 844                f = os.path.join(d, "db.h")
 845                if db_setup_debug: print "db: looking for db.h in", f
 846                if os.path.exists(f):
 847                    f = open(f).read()
 848                    m = re.search(r"#define\WDB_VERSION_MAJOR\W(\d+)", f)
 849                    if m:
 850                        db_major = int(m.group(1))
 851                        m = re.search(r"#define\WDB_VERSION_MINOR\W(\d+)", f)
 852                        db_minor = int(m.group(1))
 853                        db_ver = (db_major, db_minor)
 854
 855                        # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
 856                        if db_ver == (4, 6):
 857                            m = re.search(r"#define\WDB_VERSION_PATCH\W(\d+)", f)
 858                            db_patch = int(m.group(1))
 859                            if db_patch < 21:
 860                                print "db.h:", db_ver, "patch", db_patch,
 861                                print "being ignored (4.6.x must be >= 4.6.21)"
 862                                continue
 863
 864                        if ( (not db_ver_inc_map.has_key(db_ver)) and
 865                            allow_db_ver(db_ver) ):
 866                            # save the include directory with the db.h version
 867                            # (first occurrence only)
 868                            db_ver_inc_map[db_ver] = d
 869                            if db_setup_debug:
 870                                print "db.h: found", db_ver, "in", d
 871                        else:
 872                            # we already found a header for this library version
 873                            if db_setup_debug: print "db.h: ignoring", d
 874                    else:
 875                        # ignore this header, it didn't contain a version number
 876                        if db_setup_debug:
 877                            print "db.h: no version number version in", d
 878
 879            db_found_vers = db_ver_inc_map.keys()
 880            db_found_vers.sort()
 881
 882            while db_found_vers:
 883                db_ver = db_found_vers.pop()
 884                db_incdir = db_ver_inc_map[db_ver]
 885
 886                # check lib directories parallel to the location of the header
 887                db_dirs_to_check = combine_dirs_to_check([
 888                    db_incdir.replace("include", 'lib64'),
 889                    db_incdir.replace("include", 'lib'),
 890                    ], lib_dirs)
 891
 892                # Look for a version specific db-X.Y before an ambiguoius dbX
 893                # XXX should we -ever- look for a dbX name?  Do any
 894                # systems really not name their library by version and
 895                # symlink to more general names?
 896                for dblib in (('db-%d.%d' % db_ver),
 897                              ('db%d%d' % db_ver),
 898                              ('db%d' % db_ver[0])):
 899                    dblib_file = self.compiler.find_library_file(
 900                                    db_dirs_to_check, dblib )
 901                    if dblib_file:
 902                        dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
 903                        raise db_found
 904                    else:
 905                        if db_setup_debug: print "db lib: ", dblib, "not found"
 906
 907        except db_found:
 908            if db_setup_debug:
 909                print "bsddb using BerkeleyDB lib:", db_ver, dblib
 910                print "bsddb lib dir:", dblib_dir, " inc dir:", db_incdir
 911            db_incs = [db_incdir]
 912            dblibs = [dblib]
 913            # We add the runtime_library_dirs argument because the
 914            # BerkeleyDB lib we're linking against often isn't in the
 915            # system dynamic library search path.  This is usually
 916            # correct and most trouble free, but may cause problems in
 917            # some unusual system configurations (e.g. the directory
 918            # is on an NFS server that goes away).
 919            exts.append(Extension('_bsddb', ['_bsddb.c'],
 920                                  depends = ['bsddb.h'],
 921                                  library_dirs=dblib_dir,
 922                                  runtime_library_dirs=dblib_dir,
 923                                  include_dirs=db_incs,
 924                                  libraries=dblibs))
 925        else:
 926            if db_setup_debug: print "db: no appropriate library found"
 927            db_incs = None
 928            dblibs = []
 929            dblib_dir = None
 930            missing.append('_bsddb')
 931
 932        # The sqlite interface
 933        sqlite_setup_debug = False   # verbose debug prints from this script?
 934
 935        # We hunt for #define SQLITE_VERSION "n.n.n"
 936        # We need to find >= sqlite version 3.0.8
 937        sqlite_incdir = sqlite_libdir = None
 938        sqlite_inc_paths = []
 939        if use_system_paths:
 940            sqlite_inc_paths = [ '/usr/include',
 941                                 '/usr/include/sqlite',
 942                                 '/usr/include/sqlite3',
 943                                 '/usr/local/include',
 944                                 '/usr/local/include/sqlite',
 945                                 '/usr/local/include/sqlite3',
 946                                 ]
 947        if "SQLITE_INC" in os.environ:
 948            sqlite_inc_paths.append(os.environ["SQLITE_INC"])
 949        MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
 950        MIN_SQLITE_VERSION = ".".join([str(x)
 951                                    for x in MIN_SQLITE_VERSION_NUMBER])
 952
 953        # Scan the default include directories before the SQLite specific
 954        # ones. This allows one to override the copy of sqlite on OSX,
 955        # where /usr/include contains an old version of sqlite.
 956        for d in inc_dirs + sqlite_inc_paths:
 957            f = os.path.join(d, "sqlite3.h")
 958            if sqlite_setup_debug: print "sqlite: looking for sqlite3.h in", f
 959            if os.path.exists(f):
 960                if sqlite_setup_debug: print "sqlite: found %s"%f
 961                incf = open(f).read()
 962                m = re.search(
 963                    r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
 964                if m:
 965                    sqlite_version = m.group(1)
 966                    sqlite_version_tuple = tuple([int(x)
 967                                        for x in sqlite_version.split(".")])
 968                    if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
 969                        # we win!
 970                        if sqlite_setup_debug:
 971                            print "%s/sqlite3.h: version %s"%(d, sqlite_version)
 972                        sqlite_incdir = d
 973                        break
 974                    else:
 975                        if sqlite_setup_debug:
 976                            print "%s: version %d is too old, need >= %s"%(d,
 977                                        sqlite_version, MIN_SQLITE_VERSION)
 978                elif sqlite_setup_debug:
 979                    print "sqlite: %s had no SQLITE_VERSION"%(f,)
 980
 981        if sqlite_incdir:
 982            sqlite_dirs_to_check = combine_dirs_to_check([
 983                os.path.join(sqlite_incdir, '..', 'lib64'),
 984                os.path.join(sqlite_incdir, '..', 'lib'),
 985                os.path.join(sqlite_incdir, '..', '..', 'lib64'),
 986                os.path.join(sqlite_incdir, '..', '..', 'lib'),
 987                os.environ.get("SQLITE_LIB", ""),
 988                ], lib_dirs)
 989            sqlite_libfile = self.compiler.find_library_file(
 990                                sqlite_dirs_to_check, 'sqlite3')
 991            if sqlite_libfile:
 992                sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
 993            elif sqlite_setup_debug:
 994                print ("sqlite: no sqlite3 library in %s" %
 995                       (sqlite_dirs_to_check))
 996
 997        if sqlite_incdir and sqlite_libdir:
 998            sqlite_srcs = ['_sqlite/cache.c',
 999                '_sqlite/connection.c',
1000                '_sqlite/cursor.c',
1001                '_sqlite/microprotocols.c',
1002                '_sqlite/module.c',
1003                '_sqlite/prepare_protocol.c',
1004                '_sqlite/row.c',
1005                '_sqlite/statement.c',
1006                '_sqlite/util.c', ]
1007
1008            sqlite_defines = []
1009            if sys.platform != "win32":
1010                sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
1011            else:
1012                sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
1013
1014
1015            if sys.platform == 'darwin':
1016                # In every directory on the search path search for a dynamic
1017                # library and then a static library, instead of first looking
1018                # for dynamic libraries on the entiry path.
1019                # This way a staticly linked custom sqlite gets picked up
1020                # before the dynamic library in /usr/lib.
1021                sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1022            else:
1023                sqlite_extra_link_args = ()
1024
1025            exts.append(Extension('_sqlite3', sqlite_srcs,
1026                                  define_macros=sqlite_defines,
1027                                  include_dirs=["Modules/_sqlite",
1028                                                sqlite_incdir],
1029                                  library_dirs=sqlite_libdir,
1030                                  runtime_library_dirs=sqlite_libdir,
1031                                  extra_link_args=sqlite_extra_link_args,
1032                                  libraries=["sqlite3",]))
1033        else:
1034            missing.append('_sqlite3')
1035
1036        # Look for Berkeley db 1.85.   Note that it is built as a different
1037        # module name so it can be included even when later versions are
1038        # available.  A very restrictive search is performed to avoid
1039        # accidentally building this module with a later version of the
1040        # underlying db library.  May BSD-ish Unixes incorporate db 1.85
1041        # symbols into libc and place the include file in /usr/include.
1042        #
1043        # If the better bsddb library can be built (db_incs is defined)
1044        # we do not build this one.  Otherwise this build will pick up
1045        # the more recent berkeleydb's db.h file first in the include path
1046        # when attempting to compile and it will fail.
1047        f = "/usr/include/db.h"
1048        if use_system_paths and os.path.exists(f) and not db_incs:
1049            data = open(f).read()
1050            m = re.search(r"#s*define\s+HASHVERSION\s+2\s*", data)
1051            if m is not None:
1052                # bingo - old version used hash file format version 2
1053                ### XXX this should be fixed to not be platform-dependent
1054                ### but I don't have direct access to an osf1 platform and
1055                ### seemed to be muffing the search somehow
1056                libraries = platform == "osf1" and ['db'] or None
1057                if libraries is not None:
1058                    exts.append(Extension('bsddb185', ['bsddbmodule.c'],
1059                                          libraries=libraries))
1060                else:
1061                    exts.append(Extension('bsddb185', ['bsddbmodule.c']))
1062            else:
1063                missing.append('bsddb185')
1064        else:
1065            missing.append('bsddb185')
1066
1067        # The standard Unix dbm module:
1068        if platform not in ['cygwin']:
1069            if find_file("ndbm.h", inc_dirs, []) is not None:
1070                # Some systems have -lndbm, others don't
1071                if self.compiler.find_library_file(lib_dirs, 'ndbm'):
1072                    ndbm_libs = ['ndbm']
1073                else:
1074                    ndbm_libs = []
1075                exts.append( Extension('dbm', ['dbmmodule.c'],
1076                                       define_macros=[('HAVE_NDBM_H',None)],
1077                                       libraries = ndbm_libs ) )
1078            elif self.compiler.find_library_file(lib_dirs, 'gdbm'):
1079                gdbm_libs = ['gdbm']
1080                if self.compiler.find_library_file(lib_dirs, 'gdbm_compat'):
1081                    gdbm_libs.append('gdbm_compat')
1082                if find_file("gdbm/ndbm.h", inc_dirs, []) is not None:
1083                    exts.append( Extension(
1084                        'dbm', ['dbmmodule.c'],
1085                        define_macros=[('HAVE_GDBM_NDBM_H',None)],
1086                        libraries = gdbm_libs ) )
1087                elif find_file("gdbm-ndbm.h", inc_dirs, []) is not None:
1088                    exts.append( Extension(
1089                        'dbm', ['dbmmodule.c'],
1090                        define_macros=[('HAVE_GDBM_DASH_NDBM_H',None)],
1091                        libraries = gdbm_libs ) )
1092                else:
1093                    missing.append('dbm')
1094            elif db_incs is not None:
1095                exts.append( Extension('dbm', ['dbmmodule.c'],
1096                                       library_dirs=dblib_dir,
1097                                       runtime_library_dirs=dblib_dir,
1098                                       include_dirs=db_incs,
1099                                       define_macros=[('HAVE_BERKDB_H',None),
1100                                                      ('DB_DBM_HSEARCH',None)],
1101                                       libraries=dblibs))
1102            else:
1103                missing.append('dbm')
1104
1105        # Anthony Baxter's gdbm module.  GNU dbm(3) will require -lgdbm:
1106        if (self.compiler.find_library_file(lib_dirs, 'gdbm')):
1107            exts.append( Extension('gdbm', ['gdbmmodule.c'],
1108                                   libraries = ['gdbm'] ) )
1109        else:
1110            missing.append('gdbm')
1111
1112        # Unix-only modules
1113        if platform not in ['mac', 'win32']:
1114            # Steen Lumholt's termios module
1115            exts.append( Extension('termios', ['termios.c']) )
1116            # Jeremy Hylton's rlimit interface
1117            if platform not in ['atheos']:
1118                exts.append( Extension('resource', ['resource.c']) )
1119            else:
1120                missing.append('resource')
1121
1122            # Sun yellow pages. Some systems have the functions in libc.
1123            if platform not in ['cygwin', 'atheos', 'qnx6']:
1124                if (self.compiler.find_libra

Large files files are truncated, but you can click here to view the full file