setup.py revision 983258ed7e96897e00c7a4459034d83816018181
1# Autodetecting setup.py script for building the Python extensions
2#
3
4__version__ = "$Revision$"
5
6import sys, os, getopt, imp
7from distutils import sysconfig
8from distutils import text_file
9from distutils.errors import *
10from distutils.core import Extension, setup
11from distutils.command.build_ext import build_ext
12from distutils.command.install import install
13
14# This global variable is used to hold the list of modules to be disabled.
15disabled_module_list = []
16
17def add_dir_to_list(dirlist, dir):
18    """Add the directory 'dir' to the list 'dirlist' (at the front) if
19    1) 'dir' is not already in 'dirlist'
20    2) 'dir' actually exists, and is a directory."""
21    if dir is not None and os.path.isdir(dir) and dir not in dirlist:
22        dirlist.insert(0, dir)
23
24def find_file(filename, std_dirs, paths):
25    """Searches for the directory where a given file is located,
26    and returns a possibly-empty list of additional directories, or None
27    if the file couldn't be found at all.
28
29    'filename' is the name of a file, such as readline.h or libcrypto.a.
30    'std_dirs' is the list of standard system directories; if the
31        file is found in one of them, no additional directives are needed.
32    'paths' is a list of additional locations to check; if the file is
33        found in one of them, the resulting list will contain the directory.
34    """
35
36    # Check the standard locations
37    for dir in std_dirs:
38        f = os.path.join(dir, filename)
39        if os.path.exists(f): return []
40
41    # Check the additional directories
42    for dir in paths:
43        f = os.path.join(dir, filename)
44        if os.path.exists(f):
45            return [dir]
46
47    # Not found anywhere
48    return None
49
50def find_library_file(compiler, libname, std_dirs, paths):
51    filename = compiler.library_filename(libname, lib_type='shared')
52    result = find_file(filename, std_dirs, paths)
53    if result is not None: return result
54
55    filename = compiler.library_filename(libname, lib_type='static')
56    result = find_file(filename, std_dirs, paths)
57    return result
58
59def module_enabled(extlist, modname):
60    """Returns whether the module 'modname' is present in the list
61    of extensions 'extlist'."""
62    extlist = [ext for ext in extlist if ext.name == modname]
63    return len(extlist)
64
65def find_module_file(module, dirlist):
66    """Find a module in a set of possible folders. If it is not found
67    return the unadorned filename"""
68    list = find_file(module, [], dirlist)
69    if not list:
70        return module
71    if len(list) > 1:
72        self.announce("WARNING: multiple copies of %s found"%module)
73    return os.path.join(list[0], module)
74
75class PyBuildExt(build_ext):
76
77    def build_extensions(self):
78
79        # Detect which modules should be compiled
80        self.detect_modules()
81
82        # Remove modules that are present on the disabled list
83        self.extensions = [ext for ext in self.extensions
84                           if ext.name not in disabled_module_list]
85
86        # Fix up the autodetected modules, prefixing all the source files
87        # with Modules/ and adding Python's include directory to the path.
88        (srcdir,) = sysconfig.get_config_vars('srcdir')
89
90        # Figure out the location of the source code for extension modules
91        moddir = os.path.join(os.getcwd(), srcdir, 'Modules')
92        moddir = os.path.normpath(moddir)
93        srcdir, tail = os.path.split(moddir)
94        srcdir = os.path.normpath(srcdir)
95        moddir = os.path.normpath(moddir)
96
97        moddirlist = [moddir]
98        incdirlist = ['./Include']
99
100        # Platform-dependent module source and include directories
101        platform = self.get_platform()
102        if platform in ('darwin', 'mac'):
103            # Mac OS X also includes some mac-specific modules
104            macmoddir = os.path.join(os.getcwd(), srcdir, 'Mac/Modules')
105            moddirlist.append(macmoddir)
106            incdirlist.append('./Mac/Include')
107
108        alldirlist = moddirlist + incdirlist
109
110        # Fix up the paths for scripts, too
111        self.distribution.scripts = [os.path.join(srcdir, filename)
112                                     for filename in self.distribution.scripts]
113
114        for ext in self.extensions[:]:
115            ext.sources = [ find_module_file(filename, moddirlist)
116                            for filename in ext.sources ]
117            if ext.depends is not None:
118                ext.depends = [find_module_file(filename, alldirlist)
119                               for filename in ext.depends]
120            ext.include_dirs.append( '.' ) # to get config.h
121            for incdir in incdirlist:
122                ext.include_dirs.append( os.path.join(srcdir, incdir) )
123
124            # If a module has already been built statically,
125            # don't build it here
126            if ext.name in sys.builtin_module_names:
127                self.extensions.remove(ext)
128
129        if platform != 'mac':
130            # Parse Modules/Setup to figure out which modules are turned
131            # on in the file.
132            input = text_file.TextFile('Modules/Setup', join_lines=1)
133            remove_modules = []
134            while 1:
135                line = input.readline()
136                if not line: break
137                line = line.split()
138                remove_modules.append( line[0] )
139            input.close()
140
141            for ext in self.extensions[:]:
142                if ext.name in remove_modules:
143                    self.extensions.remove(ext)
144
145        # When you run "make CC=altcc" or something similar, you really want
146        # those environment variables passed into the setup.py phase.  Here's
147        # a small set of useful ones.
148        compiler = os.environ.get('CC')
149        linker_so = os.environ.get('LDSHARED')
150        args = {}
151        # unfortunately, distutils doesn't let us provide separate C and C++
152        # compilers
153        if compiler is not None:
154            (ccshared,opt) = sysconfig.get_config_vars('CCSHARED','OPT')
155            args['compiler_so'] = compiler + ' ' + opt + ' ' + ccshared
156        if linker_so is not None:
157            args['linker_so'] = linker_so
158        self.compiler.set_executables(**args)
159
160        build_ext.build_extensions(self)
161
162    def build_extension(self, ext):
163
164        try:
165            build_ext.build_extension(self, ext)
166        except (CCompilerError, DistutilsError), why:
167            self.announce('WARNING: building of extension "%s" failed: %s' %
168                          (ext.name, sys.exc_info()[1]))
169            return
170        # Workaround for Mac OS X: The Carbon-based modules cannot be
171        # reliably imported into a command-line Python
172        if 'Carbon' in ext.extra_link_args:
173            self.announce(
174                'WARNING: skipping import check for Carbon-based "%s"' %
175                ext.name)
176            return
177        # Workaround for Cygwin: Cygwin currently has fork issues when many
178        # modules have been imported
179        if self.get_platform() == 'cygwin':
180            self.announce('WARNING: skipping import check for Cygwin-based "%s"'
181                % ext.name)
182            return
183        ext_filename = os.path.join(
184            self.build_lib,
185            self.get_ext_filename(self.get_ext_fullname(ext.name)))
186        try:
187            imp.load_dynamic(ext.name, ext_filename)
188        except ImportError, why:
189
190            if 1:
191                self.announce('*** WARNING: renaming "%s" since importing it'
192                              ' failed: %s' % (ext.name, why))
193                assert not self.inplace
194                basename, tail = os.path.splitext(ext_filename)
195                newname = basename + "_failed" + tail
196                if os.path.exists(newname): os.remove(newname)
197                os.rename(ext_filename, newname)
198
199                # XXX -- This relies on a Vile HACK in
200                # distutils.command.build_ext.build_extension().  The
201                # _built_objects attribute is stored there strictly for
202                # use here.
203                # If there is a failure, _built_objects may not be there,
204                # so catch the AttributeError and move on.
205                try:
206                    for filename in self._built_objects:
207                        os.remove(filename)
208                except AttributeError:
209                    self.announce('unable to remove files (ignored)')
210            else:
211                self.announce('*** WARNING: importing extension "%s" '
212                              'failed: %s' % (ext.name, why))
213
214    def get_platform (self):
215        # Get value of sys.platform
216        platform = sys.platform
217        if platform[:6] =='cygwin':
218            platform = 'cygwin'
219        elif platform[:4] =='beos':
220            platform = 'beos'
221        elif platform[:6] == 'darwin':
222            platform = 'darwin'
223        elif platform[:6] == 'atheos':
224            platform = 'atheos'
225
226        return platform
227
228    def detect_modules(self):
229        # Ensure that /usr/local is always used
230        add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
231        add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
232
233        if os.path.normpath(sys.prefix) != '/usr':
234            add_dir_to_list(self.compiler.library_dirs,
235                            sysconfig.get_config_var("LIBDIR"))
236            add_dir_to_list(self.compiler.include_dirs,
237                            sysconfig.get_config_var("INCLUDEDIR"))
238
239        try:
240            have_unicode = unicode
241        except NameError:
242            have_unicode = 0
243
244        # lib_dirs and inc_dirs are used to search for files;
245        # if a file is found in one of those directories, it can
246        # be assumed that no additional -I,-L directives are needed.
247        lib_dirs = self.compiler.library_dirs + ['/lib', '/usr/lib']
248        inc_dirs = self.compiler.include_dirs + ['/usr/include']
249        exts = []
250
251        platform = self.get_platform()
252        (srcdir,) = sysconfig.get_config_vars('srcdir')
253
254        # Check for AtheOS which has libraries in non-standard locations
255        if platform == 'atheos':
256            lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
257            lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
258            inc_dirs += ['/system/include', '/atheos/autolnk/include']
259            inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
260
261        # Check for MacOS X, which doesn't need libm.a at all
262        math_libs = ['m']
263        if platform in ['darwin', 'beos', 'mac']:
264            math_libs = []
265
266        # XXX Omitted modules: gl, pure, dl, SGI-specific modules
267
268        #
269        # The following modules are all pretty straightforward, and compile
270        # on pretty much any POSIXish platform.
271        #
272
273        # Some modules that are normally always on:
274        exts.append( Extension('regex', ['regexmodule.c', 'regexpr.c']) )
275        exts.append( Extension('pcre', ['pcremodule.c', 'pypcre.c']) )
276
277        exts.append( Extension('_hotshot', ['_hotshot.c']) )
278        exts.append( Extension('_weakref', ['_weakref.c']) )
279        exts.append( Extension('xreadlines', ['xreadlinesmodule.c']) )
280
281        # array objects
282        exts.append( Extension('array', ['arraymodule.c']) )
283        # complex math library functions
284        exts.append( Extension('cmath', ['cmathmodule.c'],
285                               libraries=math_libs) )
286
287        # math library functions, e.g. sin()
288        exts.append( Extension('math',  ['mathmodule.c'],
289                               libraries=math_libs) )
290        # fast string operations implemented in C
291        exts.append( Extension('strop', ['stropmodule.c']) )
292        # time operations and variables
293        exts.append( Extension('time', ['timemodule.c'],
294                               libraries=math_libs) )
295        # operator.add() and similar goodies
296        exts.append( Extension('operator', ['operator.c']) )
297        # access to the builtin codecs and codec registry
298        exts.append( Extension('_codecs', ['_codecsmodule.c']) )
299        # Python C API test module
300        exts.append( Extension('_testcapi', ['_testcapimodule.c']) )
301        # static Unicode character database
302        if have_unicode:
303            exts.append( Extension('unicodedata', ['unicodedata.c']) )
304        # access to ISO C locale support
305        if platform in ['cygwin']:
306            locale_libs = ['intl']
307        else:
308            locale_libs = []
309        exts.append( Extension('_locale', ['_localemodule.c'],
310                               libraries=locale_libs ) )
311
312        # Modules with some UNIX dependencies -- on by default:
313        # (If you have a really backward UNIX, select and socket may not be
314        # supported...)
315
316        # fcntl(2) and ioctl(2)
317        exts.append( Extension('fcntl', ['fcntlmodule.c']) )
318        if platform not in ['mac']:
319                # pwd(3)
320                exts.append( Extension('pwd', ['pwdmodule.c']) )
321                # grp(3)
322                exts.append( Extension('grp', ['grpmodule.c']) )
323        # select(2); not on ancient System V
324        exts.append( Extension('select', ['selectmodule.c']) )
325
326        # The md5 module implements the RSA Data Security, Inc. MD5
327        # Message-Digest Algorithm, described in RFC 1321.  The
328        # necessary files md5c.c and md5.h are included here.
329        exts.append( Extension('md5', ['md5module.c', 'md5c.c']) )
330
331        # The sha module implements the SHA checksum algorithm.
332        # (NIST's Secure Hash Algorithm.)
333        exts.append( Extension('sha', ['shamodule.c']) )
334
335        # Helper module for various ascii-encoders
336        exts.append( Extension('binascii', ['binascii.c']) )
337
338        # Fred Drake's interface to the Python parser
339        exts.append( Extension('parser', ['parsermodule.c']) )
340
341        # cStringIO and cPickle
342        exts.append( Extension('cStringIO', ['cStringIO.c']) )
343        exts.append( Extension('cPickle', ['cPickle.c']) )
344
345        # Memory-mapped files (also works on Win32).
346        if platform not in ['atheos', 'mac']:
347            exts.append( Extension('mmap', ['mmapmodule.c']) )
348
349        # Lance Ellinghaus's modules:
350        # enigma-inspired encryption
351        exts.append( Extension('rotor', ['rotormodule.c']) )
352        if platform not in ['mac']:
353                # syslog daemon interface
354                exts.append( Extension('syslog', ['syslogmodule.c']) )
355
356        # George Neville-Neil's timing module:
357        exts.append( Extension('timing', ['timingmodule.c']) )
358
359        #
360        # Here ends the simple stuff.  From here on, modules need certain
361        # libraries, are platform-specific, or present other surprises.
362        #
363
364        # Multimedia modules
365        # These don't work for 64-bit platforms!!!
366        # These represent audio samples or images as strings:
367
368        # Disabled on 64-bit platforms
369        if sys.maxint != 9223372036854775807L:
370            # Operations on audio samples
371            exts.append( Extension('audioop', ['audioop.c']) )
372            # Operations on images
373            exts.append( Extension('imageop', ['imageop.c']) )
374            # Read SGI RGB image files (but coded portably)
375            exts.append( Extension('rgbimg', ['rgbimgmodule.c']) )
376
377        # readline
378        if self.compiler.find_library_file(lib_dirs, 'readline'):
379            readline_libs = ['readline']
380            if self.compiler.find_library_file(lib_dirs,
381                                                 'ncurses'):
382                readline_libs.append('ncurses')
383            elif self.compiler.find_library_file(lib_dirs +
384                                               ['/usr/lib/termcap'],
385                                               'termcap'):
386                readline_libs.append('termcap')
387            exts.append( Extension('readline', ['readline.c'],
388                                   library_dirs=['/usr/lib/termcap'],
389                                   libraries=readline_libs) )
390        if platform not in ['mac']:
391                # crypt module.
392
393                if self.compiler.find_library_file(lib_dirs, 'crypt'):
394                    libs = ['crypt']
395                else:
396                    libs = []
397                exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
398
399        # socket(2)
400        exts.append( Extension('_socket', ['socketmodule.c'],
401                               depends = ['socketmodule.h']) )
402        # Detect SSL support for the socket module (via _ssl)
403        ssl_incs = find_file('openssl/ssl.h', inc_dirs,
404                             ['/usr/local/ssl/include',
405                              '/usr/contrib/ssl/include/'
406                             ]
407                             )
408        ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
409                                     ['/usr/local/ssl/lib',
410                                      '/usr/contrib/ssl/lib/'
411                                     ] )
412
413        if (ssl_incs is not None and
414            ssl_libs is not None):
415            exts.append( Extension('_ssl', ['_ssl.c'],
416                                   include_dirs = ssl_incs,
417                                   library_dirs = ssl_libs,
418                                   libraries = ['ssl', 'crypto'],
419                                   depends = ['socketmodule.h']), )
420
421        # Modules that provide persistent dictionary-like semantics.  You will
422        # probably want to arrange for at least one of them to be available on
423        # your machine, though none are defined by default because of library
424        # dependencies.  The Python module anydbm.py provides an
425        # implementation independent wrapper for these; dumbdbm.py provides
426        # similar functionality (but slower of course) implemented in Python.
427
428        # Berkeley DB interface.
429        #
430        # This requires the Berkeley DB code, see
431        # ftp://ftp.cs.berkeley.edu/pub/4bsd/db.1.85.tar.gz
432        #
433        # (See http://pybsddb.sourceforge.net/ for an interface to
434        # Berkeley DB 3.x.)
435
436        # when sorted in reverse order, keys for this dict must appear in the
437        # order you wish to search - e.g., search for db3 before db2, db2
438        # before db1
439        db_try_this = {
440            'db4': {'libs': ('db-4.3', 'db-4.2', 'db-4.1', 'db-4.0'),
441                    'libdirs': ('/usr/local/BerkeleyDB.4.3/lib',
442                                '/usr/local/BerkeleyDB.4.2/lib',
443                                '/usr/local/BerkeleyDB.4.1/lib',
444                                '/usr/local/BerkeleyDB.4.0/lib',
445                                '/usr/local/lib',
446                                '/usr/lib',
447                                '/opt/sfw',
448                                '/sw/lib',
449                                '/lib',
450                                ),
451                    'incdirs': ('/usr/local/BerkeleyDB.4.3/include',
452                                '/usr/local/BerkeleyDB.4.2/include',
453                                '/usr/local/BerkeleyDB.4.1/include',
454                                '/usr/local/BerkeleyDB.4.0/include',
455                                '/usr/local/include/db3',
456                                '/opt/sfw/include/db3',
457                                '/sw/include/db3',
458                                '/usr/include/db3',
459                                ),
460                    'incs': ('db_185.h',)},
461            'db3': {'libs': ('db-3.3', 'db-3.2', 'db-3.1', 'db-3.0'),
462                    'libdirs': ('/usr/local/BerkeleyDB.3.3/lib',
463                                '/usr/local/BerkeleyDB.3.2/lib',
464                                '/usr/local/BerkeleyDB.3.1/lib',
465                                '/usr/local/BerkeleyDB.3.0/lib',
466                                '/usr/local/lib',
467                                '/opt/sfw',
468                                '/sw/lib',
469                                '/usr/lib',
470                                '/lib',
471                                ),
472                    'incdirs': ('/usr/local/BerkeleyDB.3.3/include',
473                                '/usr/local/BerkeleyDB.3.2/include',
474                                '/usr/local/BerkeleyDB.3.1/include',
475                                '/usr/local/BerkeleyDB.3.0/include',
476                                '/usr/local/include/db3',
477                                '/opt/sfw/include/db3',
478                                '/sw/include/db3',
479                                '/usr/include/db3',
480                                ),
481                    'incs': ('db_185.h',)},
482            'db2': {'libs': ('db2',),
483                    'libdirs': ('/usr/local/lib',
484                                '/sw/lib',
485                                '/usr/lib',
486                                '/lib'),
487                    'incdirs': ('/usr/local/include/db2',
488                                '/sw/include/db2',
489                                '/usr/include/db2'),
490                    'incs': ('db_185.h',)},
491            # if you are willing to risk hash db file corruption you can
492            # uncomment the lines below for db1.  Note that this will affect
493            # not only the bsddb module, but the dbhash and anydbm modules
494            # as well.  YOU HAVE BEEN WARNED!!!
495            ##'db1': {'libs': ('db1', 'db'),
496            ##        'libdirs': ('/usr/local/lib',
497            ##                    '/sw/lib',
498            ##                    '/usr/lib',
499            ##                    '/lib'),
500            ##        'incdirs': ('/usr/local/include/db1',
501            ##                    '/usr/local/include',
502            ##                    '/usr/include/db1',
503            ##                    '/usr/include'),
504            ##        'incs': ('db.h',)},
505            }
506
507        # override this list to affect the library version search order
508        # for example, if you want to force version 2 to be used:
509        #   db_search_order = ["db2"]
510        db_search_order = db_try_this.keys()
511        db_search_order.sort()
512        db_search_order.reverse()
513
514        find_lib_file = self.compiler.find_library_file
515        class found(Exception): pass
516        try:
517            for dbkey in db_search_order:
518                dbd = db_try_this[dbkey]
519                for dblib in dbd['libs']:
520                    for dbinc in dbd['incs']:
521                        db_incs = find_file(dbinc, [], dbd['incdirs'])
522                        dblib_dir = find_lib_file(dbd['libdirs'], dblib)
523                        if db_incs and dblib_dir:
524                            dblib_dir = os.path.dirname(dblib_dir)
525                            dblibs = [dblib]
526                            raise found
527        except found:
528            dblibs = [dblib]
529            # A default source build puts Berkeley DB in something like
530            # /usr/local/Berkeley.3.3 and the lib dir under that isn't
531            # normally on ld.so's search path, unless the sysadmin has hacked
532            # /etc/ld.so.conf.  We add the directory to runtime_library_dirs
533            # so the proper -R/--rpath flags get passed to the linker.  This
534            # is usually correct and most trouble free, but may cause problems
535            # in some unusual system configurations (e.g. the directory is on
536            # an NFS server that goes away).
537            if dbinc == 'db_185.h':
538                exts.append(Extension('bsddb', ['bsddbmodule.c'],
539                                      library_dirs=[dblib_dir],
540                                      runtime_library_dirs=[dblib_dir],
541                                      include_dirs=db_incs,
542                                      define_macros=[('HAVE_DB_185_H',1)],
543                                      libraries=dblibs))
544            else:
545                exts.append(Extension('bsddb', ['bsddbmodule.c'],
546                                      library_dirs=[dblib_dir],
547                                      runtime_library_dirs=[dblib_dir],
548                                      include_dirs=db_incs,
549                                      libraries=dblibs))
550        else:
551            db_incs = None
552            dblibs = []
553            dblib_dir = None
554
555        # The standard Unix dbm module:
556        if platform not in ['cygwin']:
557            if (self.compiler.find_library_file(lib_dirs, 'ndbm')
558                and find_file("ndbm.h", inc_dirs, []) is not None):
559                exts.append( Extension('dbm', ['dbmmodule.c'],
560                                       define_macros=[('HAVE_NDBM_H',None)],
561                                       libraries = ['ndbm'] ) )
562            elif (platform in ['darwin']
563                and find_file("ndbm.h", inc_dirs, []) is not None):
564                # Darwin has ndbm in libc
565                exts.append( Extension('dbm', ['dbmmodule.c'],
566                                       define_macros=[('HAVE_NDBM_H',None)]) )
567            elif (self.compiler.find_library_file(lib_dirs, 'gdbm')
568                  and find_file("gdbm/ndbm.h", inc_dirs, []) is not None):
569                exts.append( Extension('dbm', ['dbmmodule.c'],
570                                       define_macros=[('HAVE_GDBM_NDBM_H',None)],
571                                       libraries = ['gdbm'] ) )
572            elif db_incs is not None:
573                exts.append( Extension('dbm', ['dbmmodule.c'],
574                                       library_dirs=[dblib_dir],
575                                       include_dirs=db_incs,
576                                       define_macros=[('HAVE_BERKDB_H',None),
577                                                      ('DB_DBM_HSEARCH',None)],
578                                       libraries=dblibs))
579
580        # Anthony Baxter's gdbm module.  GNU dbm(3) will require -lgdbm:
581        if (self.compiler.find_library_file(lib_dirs, 'gdbm')):
582            exts.append( Extension('gdbm', ['gdbmmodule.c'],
583                                   libraries = ['gdbm'] ) )
584
585        # The mpz module interfaces to the GNU Multiple Precision library.
586        # You need to ftp the GNU MP library.
587        # This was originally written and tested against GMP 1.2 and 1.3.2.
588        # It has been modified by Rob Hooft to work with 2.0.2 as well, but I
589        # haven't tested it recently, and it definitely doesn't work with
590        # GMP 4.0.  For more complete modules, refer to
591        # http://gmpy.sourceforge.net and
592        # http://www.egenix.com/files/python/mxNumber.html
593
594        # A compatible MP library unencumbered by the GPL also exists.  It was
595        # posted to comp.sources.misc in volume 40 and is widely available from
596        # FTP archive sites. One URL for it is:
597        # ftp://gatekeeper.dec.com/.b/usenet/comp.sources.misc/volume40/fgmp/part01.Z
598
599        if (self.compiler.find_library_file(lib_dirs, 'gmp')):
600            exts.append( Extension('mpz', ['mpzmodule.c'],
601                                   libraries = ['gmp'] ) )
602
603
604        # Unix-only modules
605        if platform not in ['mac', 'win32']:
606            # Steen Lumholt's termios module
607            exts.append( Extension('termios', ['termios.c']) )
608            # Jeremy Hylton's rlimit interface
609	    if platform not in ['atheos']:
610                exts.append( Extension('resource', ['resource.c']) )
611
612            # Sun yellow pages. Some systems have the functions in libc.
613            if platform not in ['cygwin', 'atheos']:
614                if (self.compiler.find_library_file(lib_dirs, 'nsl')):
615                    libs = ['nsl']
616                else:
617                    libs = []
618                exts.append( Extension('nis', ['nismodule.c'],
619                                       libraries = libs) )
620
621        # Curses support, requring the System V version of curses, often
622        # provided by the ncurses library.
623        if platform == 'sunos4':
624            inc_dirs += ['/usr/5include']
625            lib_dirs += ['/usr/5lib']
626
627        if (self.compiler.find_library_file(lib_dirs, 'ncurses')):
628            curses_libs = ['ncurses']
629            exts.append( Extension('_curses', ['_cursesmodule.c'],
630                                   libraries = curses_libs) )
631        elif (self.compiler.find_library_file(lib_dirs, 'curses')
632              and platform != 'darwin'):
633                # OSX has an old Berkeley curses, not good enough for
634                # the _curses module.
635            if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
636                curses_libs = ['curses', 'terminfo']
637            else:
638                curses_libs = ['curses', 'termcap']
639
640            exts.append( Extension('_curses', ['_cursesmodule.c'],
641                                   libraries = curses_libs) )
642
643        # If the curses module is enabled, check for the panel module
644        if (module_enabled(exts, '_curses') and
645            self.compiler.find_library_file(lib_dirs, 'panel')):
646            exts.append( Extension('_curses_panel', ['_curses_panel.c'],
647                                   libraries = ['panel'] + curses_libs) )
648
649
650
651        # Lee Busby's SIGFPE modules.
652        # The library to link fpectl with is platform specific.
653        # Choose *one* of the options below for fpectl:
654
655        if platform == 'irix5':
656            # For SGI IRIX (tested on 5.3):
657            exts.append( Extension('fpectl', ['fpectlmodule.c'],
658                                   libraries=['fpe']) )
659        elif 0: # XXX how to detect SunPro?
660            # For Solaris with SunPro compiler (tested on Solaris 2.5
661            # with SunPro C 4.2): (Without the compiler you don't have
662            # -lsunmath.)
663            #fpectl fpectlmodule.c -R/opt/SUNWspro/lib -lsunmath -lm
664            pass
665        else:
666            # For other systems: see instructions in fpectlmodule.c.
667            #fpectl fpectlmodule.c ...
668            exts.append( Extension('fpectl', ['fpectlmodule.c']) )
669
670
671        # Andrew Kuchling's zlib module.  Note that some versions of zlib
672        # 1.1.3 have security problems.  See CERT Advisory CA-2002-07:
673        # http://www.cert.org/advisories/CA-2002-07.html
674        #
675        # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
676        # patch its zlib 1.1.3 package instead of upgrading to 1.1.4.  For
677        # now, we still accept 1.1.3, because we think it's difficult to
678        # exploit this in Python, and we'd rather make it RedHat's problem
679        # than our problem <wink>.
680        #
681        # You can upgrade zlib to version 1.1.4 yourself by going to
682        # http://www.gzip.org/zlib/
683        zlib_inc = find_file('zlib.h', [], inc_dirs)
684        if zlib_inc is not None:
685            zlib_h = zlib_inc[0] + '/zlib.h'
686            version = '"0.0.0"'
687            version_req = '"1.1.3"'
688            fp = open(zlib_h)
689            while 1:
690                line = fp.readline()
691                if not line:
692                    break
693                if line.startswith('#define ZLIB_VERSION'):
694                    version = line.split()[2]
695                    break
696            if version >= version_req:
697                if (self.compiler.find_library_file(lib_dirs, 'z')):
698                    exts.append( Extension('zlib', ['zlibmodule.c'],
699                                           libraries = ['z']) )
700
701        # Interface to the Expat XML parser
702        #
703        # Expat was written by James Clark and is now maintained by a
704        # group of developers on SourceForge; see www.libexpat.org for
705        # more information.  The pyexpat module was written by Paul
706        # Prescod after a prototype by Jack Jansen.  Source of Expat
707        # 1.95.2 is included in Modules/expat/.  Usage of a system
708        # shared libexpat.so/expat.dll is not advised.
709        #
710        # More information on Expat can be found at www.libexpat.org.
711        #
712        if sys.byteorder == "little":
713            xmlbo = "12"
714        else:
715            xmlbo = "21"
716        expatinc = os.path.join(os.getcwd(), srcdir, 'Modules', 'expat')
717        exts.append(Extension('pyexpat',
718                              sources = [
719            'pyexpat.c',
720            'expat/xmlparse.c',
721            'expat/xmlrole.c',
722            'expat/xmltok.c',
723            ],
724                              define_macros = [
725            ('HAVE_EXPAT_H',None),
726            ('XML_NS', '1'),
727            ('XML_DTD', '1'),
728            ('XML_BYTE_ORDER', xmlbo),
729            ('XML_CONTEXT_BYTES','1024'),
730            ],
731                              include_dirs = [expatinc]
732                               ))
733
734        # Dynamic loading module
735        dl_inc = find_file('dlfcn.h', [], inc_dirs)
736        if (dl_inc is not None) and (platform not in ['atheos']):
737            exts.append( Extension('dl', ['dlmodule.c']) )
738
739        # Platform-specific libraries
740        if platform == 'linux2':
741            # Linux-specific modules
742            exts.append( Extension('linuxaudiodev', ['linuxaudiodev.c']) )
743
744        if platform == 'sunos5':
745            # SunOS specific modules
746            exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
747
748        if platform == 'darwin':
749            # Mac OS X specific modules. Modules linked against the Carbon
750            # framework are only built for framework-enabled Pythons. As
751            # of MacOSX 10.1 importing the Carbon framework from a non-windowing
752            # application (MacOSX server, not logged in on the console) may
753            # result in Python crashing.
754            #
755            # I would like to trigger on WITH_NEXT_FRAMEWORK but that isn't
756            # available here. This Makefile variable is also what the install
757            # procedure triggers on.
758            exts.append( Extension('_CF', ['cf/_CFmodule.c', 'cf/pycfbridge.c'],
759                        extra_link_args=['-framework', 'CoreFoundation']) )
760
761            framework = sysconfig.get_config_var('PYTHONFRAMEWORK')
762            if framework:
763                exts.append( Extension('gestalt', ['gestaltmodule.c'],
764                            extra_link_args=['-framework', 'Carbon']) )
765                exts.append( Extension('MacOS', ['macosmodule.c'],
766                            extra_link_args=['-framework', 'Carbon']) )
767                exts.append( Extension('icglue', ['icgluemodule.c'],
768                            extra_link_args=['-framework', 'Carbon']) )
769                exts.append( Extension('macfs',
770                                       ['macfsmodule.c',
771                                        '../Python/getapplbycreator.c'],
772                            extra_link_args=['-framework', 'Carbon']) )
773                exts.append( Extension('_Res', ['res/_Resmodule.c'],
774                            extra_link_args=['-framework', 'Carbon']) )
775                exts.append( Extension('_Snd', ['snd/_Sndmodule.c'],
776                            extra_link_args=['-framework', 'Carbon']) )
777                exts.append( Extension('Nav', ['Nav.c'],
778                        extra_link_args=['-framework', 'Carbon']) )
779                exts.append( Extension('_AE', ['ae/_AEmodule.c'],
780                        extra_link_args=['-framework', 'Carbon']) )
781                exts.append( Extension('_AH', ['ah/_AHmodule.c'],
782                        extra_link_args=['-framework', 'Carbon']) )
783                exts.append( Extension('_App', ['app/_Appmodule.c'],
784                        extra_link_args=['-framework', 'Carbon']) )
785                exts.append( Extension('_CarbonEvt', ['carbonevt/_CarbonEvtmodule.c'],
786                        extra_link_args=['-framework', 'Carbon']) )
787                exts.append( Extension('_CG', ['cg/_CGmodule.c'],
788                        extra_link_args=['-framework', 'ApplicationServices',
789                                         '-framework', 'Carbon']) )
790                exts.append( Extension('_Cm', ['cm/_Cmmodule.c'],
791                        extra_link_args=['-framework', 'Carbon']) )
792                exts.append( Extension('_Ctl', ['ctl/_Ctlmodule.c'],
793                        extra_link_args=['-framework', 'Carbon']) )
794                exts.append( Extension('_Dlg', ['dlg/_Dlgmodule.c'],
795                        extra_link_args=['-framework', 'Carbon']) )
796                exts.append( Extension('_Drag', ['drag/_Dragmodule.c'],
797                        extra_link_args=['-framework', 'Carbon']) )
798                exts.append( Extension('_Evt', ['evt/_Evtmodule.c'],
799                        extra_link_args=['-framework', 'Carbon']) )
800                exts.append( Extension('_Fm', ['fm/_Fmmodule.c'],
801                        extra_link_args=['-framework', 'Carbon']) )
802                exts.append( Extension('_Help', ['help/_Helpmodule.c'],
803                        extra_link_args=['-framework', 'Carbon']) )
804                exts.append( Extension('_Icn', ['icn/_Icnmodule.c'],
805                        extra_link_args=['-framework', 'Carbon']) )
806                exts.append( Extension('_IBCarbon', ['ibcarbon/_IBCarbon.c'],
807                        extra_link_args=['-framework', 'Carbon']) )
808                exts.append( Extension('_List', ['list/_Listmodule.c'],
809                        extra_link_args=['-framework', 'Carbon']) )
810                exts.append( Extension('_Menu', ['menu/_Menumodule.c'],
811                        extra_link_args=['-framework', 'Carbon']) )
812                exts.append( Extension('_Mlte', ['mlte/_Mltemodule.c'],
813                        extra_link_args=['-framework', 'Carbon']) )
814                exts.append( Extension('_Qd', ['qd/_Qdmodule.c'],
815                        extra_link_args=['-framework', 'Carbon']) )
816                exts.append( Extension('_Qdoffs', ['qdoffs/_Qdoffsmodule.c'],
817                        extra_link_args=['-framework', 'Carbon']) )
818                exts.append( Extension('_Qt', ['qt/_Qtmodule.c'],
819                        extra_link_args=['-framework', 'QuickTime',
820                                         '-framework', 'Carbon']) )
821                exts.append( Extension('_Scrap', ['scrap/_Scrapmodule.c'],
822                        extra_link_args=['-framework', 'Carbon']) )
823                exts.append( Extension('_TE', ['te/_TEmodule.c'],
824                        extra_link_args=['-framework', 'Carbon']) )
825                # As there is no standardized place (yet) to put
826                # user-installed Mac libraries on OSX, we search for "waste"
827                # in parent directories of the Python source tree. You
828                # should put a symlink to your Waste installation in the
829                # same folder as your python source tree.  Or modify the
830                # next few lines:-)
831                waste_incs = find_file("WASTE.h", [],
832                        ['../'*n + 'waste/C_C++ Headers' for n in (0,1,2,3,4)])
833                waste_libs = find_library_file(self.compiler, "WASTE", [],
834            [           "../"*n + "waste/Static Libraries" for n in (0,1,2,3,4)])
835                if waste_incs != None and waste_libs != None:
836                    (srcdir,) = sysconfig.get_config_vars('srcdir')
837                    exts.append( Extension('waste',
838                                   ['waste/wastemodule.c'] + [
839                                    os.path.join(srcdir, d) for d in
840                                    'Mac/Wastemods/WEObjectHandlers.c',
841                                    'Mac/Wastemods/WETabHooks.c',
842                                    'Mac/Wastemods/WETabs.c'
843                                   ],
844                                   include_dirs = waste_incs + [os.path.join(srcdir, 'Mac/Wastemods')],
845                                   library_dirs = waste_libs,
846                                   libraries = ['WASTE'],
847                                   extra_link_args = ['-framework', 'Carbon'],
848                    ) )
849                exts.append( Extension('_Win', ['win/_Winmodule.c'],
850                        extra_link_args=['-framework', 'Carbon']) )
851
852        self.extensions.extend(exts)
853
854        # Call the method for detecting whether _tkinter can be compiled
855        self.detect_tkinter(inc_dirs, lib_dirs)
856
857    def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
858        # The _tkinter module, using frameworks. Since frameworks are quite
859        # different the UNIX search logic is not sharable.
860        from os.path import join, exists
861        framework_dirs = [
862            '/System/Library/Frameworks/',
863            '/Library/Frameworks',
864            join(os.getenv('HOME'), '/Library/Frameworks')
865        ]
866
867        # Find the directory that contains the Tcl.framwork and Tk.framework
868        # bundles.
869        # XXX distutils should support -F!
870        for F in framework_dirs:
871            # both Tcl.framework and Tk.framework should be present
872            for fw in 'Tcl', 'Tk':
873            	if not exists(join(F, fw + '.framework')):
874                    break
875            else:
876                # ok, F is now directory with both frameworks. Continure
877                # building
878                break
879        else:
880            # Tk and Tcl frameworks not found. Normal "unix" tkinter search
881            # will now resume.
882            return 0
883
884        # For 8.4a2, we must add -I options that point inside the Tcl and Tk
885        # frameworks. In later release we should hopefully be able to pass
886        # the -F option to gcc, which specifies a framework lookup path.
887        #
888        include_dirs = [
889            join(F, fw + '.framework', H)
890            for fw in 'Tcl', 'Tk'
891            for H in 'Headers', 'Versions/Current/PrivateHeaders'
892        ]
893
894        # For 8.4a2, the X11 headers are not included. Rather than include a
895        # complicated search, this is a hard-coded path. It could bail out
896        # if X11 libs are not found...
897        include_dirs.append('/usr/X11R6/include')
898        frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
899
900        ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
901                        define_macros=[('WITH_APPINIT', 1)],
902                        include_dirs = include_dirs,
903                        libraries = [],
904                        extra_compile_args = frameworks,
905                        extra_link_args = frameworks,
906                        )
907        self.extensions.append(ext)
908        return 1
909
910
911    def detect_tkinter(self, inc_dirs, lib_dirs):
912        # The _tkinter module.
913
914        # Rather than complicate the code below, detecting and building
915        # AquaTk is a separate method. Only one Tkinter will be built on
916        # Darwin - either AquaTk, if it is found, or X11 based Tk.
917        platform = self.get_platform()
918        if platform == 'darwin' and \
919           self.detect_tkinter_darwin(inc_dirs, lib_dirs):
920          return
921
922        # Assume we haven't found any of the libraries or include files
923        # The versions with dots are used on Unix, and the versions without
924        # dots on Windows, for detection by cygwin.
925        tcllib = tklib = tcl_includes = tk_includes = None
926        for version in ['8.4', '84', '8.3', '83', '8.2',
927                        '82', '8.1', '81', '8.0', '80']:
928            tklib = self.compiler.find_library_file(lib_dirs,
929                                                    'tk' + version )
930            tcllib = self.compiler.find_library_file(lib_dirs,
931                                                     'tcl' + version )
932            if tklib and tcllib:
933                # Exit the loop when we've found the Tcl/Tk libraries
934                break
935
936        # Now check for the header files
937        if tklib and tcllib:
938            # Check for the include files on Debian, where
939            # they're put in /usr/include/{tcl,tk}X.Y
940            debian_tcl_include = [ '/usr/include/tcl' + version ]
941            debian_tk_include =  [ '/usr/include/tk'  + version ] + \
942                                 debian_tcl_include
943            tcl_includes = find_file('tcl.h', inc_dirs, debian_tcl_include)
944            tk_includes = find_file('tk.h', inc_dirs, debian_tk_include)
945
946        if (tcllib is None or tklib is None and
947            tcl_includes is None or tk_includes is None):
948            # Something's missing, so give up
949            return
950
951        # OK... everything seems to be present for Tcl/Tk.
952
953        include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
954        for dir in tcl_includes + tk_includes:
955            if dir not in include_dirs:
956                include_dirs.append(dir)
957
958        # Check for various platform-specific directories
959        if platform == 'sunos5':
960            include_dirs.append('/usr/openwin/include')
961            added_lib_dirs.append('/usr/openwin/lib')
962        elif os.path.exists('/usr/X11R6/include'):
963            include_dirs.append('/usr/X11R6/include')
964            added_lib_dirs.append('/usr/X11R6/lib')
965        elif os.path.exists('/usr/X11R5/include'):
966            include_dirs.append('/usr/X11R5/include')
967            added_lib_dirs.append('/usr/X11R5/lib')
968        else:
969            # Assume default location for X11
970            include_dirs.append('/usr/X11/include')
971            added_lib_dirs.append('/usr/X11/lib')
972
973        # If Cygwin, then verify that X is installed before proceeding
974        if platform == 'cygwin':
975            x11_inc = find_file('X11/Xlib.h', [], inc_dirs)
976            if x11_inc is None:
977                # X header files missing, so give up
978                return
979
980        # Check for BLT extension
981        if self.compiler.find_library_file(lib_dirs + added_lib_dirs,
982                                           'BLT8.0'):
983            defs.append( ('WITH_BLT', 1) )
984            libs.append('BLT8.0')
985
986        # Add the Tcl/Tk libraries
987        libs.append('tk'+version)
988        libs.append('tcl'+version)
989
990        if platform in ['aix3', 'aix4']:
991            libs.append('ld')
992
993        # Finally, link with the X11 libraries (not appropriate on cygwin)
994        if platform != "cygwin":
995            libs.append('X11')
996
997        ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
998                        define_macros=[('WITH_APPINIT', 1)] + defs,
999                        include_dirs = include_dirs,
1000                        libraries = libs,
1001                        library_dirs = added_lib_dirs,
1002                        )
1003        self.extensions.append(ext)
1004
1005        # XXX handle these, but how to detect?
1006        # *** Uncomment and edit for PIL (TkImaging) extension only:
1007        #       -DWITH_PIL -I../Extensions/Imaging/libImaging  tkImaging.c \
1008        # *** Uncomment and edit for TOGL extension only:
1009        #       -DWITH_TOGL togl.c \
1010        # *** Uncomment these for TOGL extension only:
1011        #       -lGL -lGLU -lXext -lXmu \
1012
1013class PyBuildInstall(install):
1014    # Suppress the warning about installation into the lib_dynload
1015    # directory, which is not in sys.path when running Python during
1016    # installation:
1017    def initialize_options (self):
1018        install.initialize_options(self)
1019        self.warn_dir=0
1020
1021def main():
1022    # turn off warnings when deprecated modules are imported
1023    import warnings
1024    warnings.filterwarnings("ignore",category=DeprecationWarning)
1025    setup(name = 'Python standard library',
1026          version = '%d.%d' % sys.version_info[:2],
1027          cmdclass = {'build_ext':PyBuildExt, 'install':PyBuildInstall},
1028          # The struct module is defined here, because build_ext won't be
1029          # called unless there's at least one extension module defined.
1030          ext_modules=[Extension('struct', ['structmodule.c'])],
1031
1032          # Scripts to install
1033          scripts = ['Tools/scripts/pydoc']
1034        )
1035
1036# --install-platlib
1037if __name__ == '__main__':
1038    main()
1039