setup.py revision 66012fe889db4ad88326f739f2e7cd7cb693f52a
1# Autodetecting setup.py script for building the Python extensions
2#
3# To be fixed:
4#   Implement --disable-modules setting
5#
6
7__version__ = "$Revision$"
8
9import sys, os, getopt
10from distutils import sysconfig
11from distutils.errors import *
12from distutils.core import Extension, setup
13from distutils.command.build_ext import build_ext
14
15# This global variable is used to hold the list of modules to be disabled.
16disabled_module_list = []
17
18def find_file(filename, std_dirs, paths):
19    """Searches for the directory where a given file is located,
20    and returns a possibly-empty list of additional directories, or None
21    if the file couldn't be found at all.
22
23    'filename' is the name of a file, such as readline.h or libcrypto.a.
24    'std_dirs' is the list of standard system directories; if the
25        file is found in one of them, no additional directives are needed.
26    'paths' is a list of additional locations to check; if the file is
27        found in one of them, the resulting list will contain the directory.
28    """
29
30    # Check the standard locations
31    for dir in std_dirs:
32        f = os.path.join(dir, filename)
33        if os.path.exists(f): return []
34
35    # Check the additional directories
36    for dir in paths:
37        f = os.path.join(dir, filename)
38        if os.path.exists(f):
39            return [dir]
40
41    # Not found anywhere
42    return None
43
44def find_library_file(compiler, libname, std_dirs, paths):
45    filename = compiler.library_filename(libname, lib_type='shared')
46    result = find_file(filename, std_dirs, paths)
47    if result is not None: return result
48
49    filename = compiler.library_filename(libname, lib_type='static')
50    result = find_file(filename, std_dirs, paths)
51    return result
52
53def module_enabled(extlist, modname):
54    """Returns whether the module 'modname' is present in the list
55    of extensions 'extlist'."""
56    extlist = [ext for ext in extlist if ext.name == modname]
57    return len(extlist)
58
59class PyBuildExt(build_ext):
60
61    def build_extensions(self):
62
63        # Detect which modules should be compiled
64        self.detect_modules()
65
66        # Remove modules that are present on the disabled list
67        self.extensions = [ext for ext in self.extensions
68                           if ext.name not in disabled_module_list]
69
70        # Fix up the autodetected modules, prefixing all the source files
71        # with Modules/ and adding Python's include directory to the path.
72        (srcdir,) = sysconfig.get_config_vars('srcdir')
73
74        # Figure out the location of the source code for extension modules
75        moddir = os.path.join(os.getcwd(), srcdir, 'Modules')
76        moddir = os.path.normpath(moddir)
77        srcdir, tail = os.path.split(moddir)
78        srcdir = os.path.normpath(srcdir)
79        moddir = os.path.normpath(moddir)
80
81        for ext in self.extensions[:]:
82            ext.sources = [ os.path.join(moddir, filename)
83                            for filename in ext.sources ]
84            ext.include_dirs.append( '.' ) # to get config.h
85            ext.include_dirs.append( os.path.join(srcdir, './Include') )
86
87            # If a module has already been built statically,
88            # don't build it here
89            if ext.name in sys.builtin_module_names:
90                self.extensions.remove(ext)
91
92        # When you run "make CC=altcc" or something similar, you really want
93        # those environment variables passed into the setup.py phase.  Here's
94        # a small set of useful ones.
95        compiler = os.environ.get('CC')
96        linker_so = os.environ.get('LDSHARED')
97        args = {}
98        # unfortunately, distutils doesn't let us provide separate C and C++
99        # compilers
100        if compiler is not None:
101            args['compiler_so'] = compiler
102        if linker_so is not None:
103            args['linker_so'] = linker_so + ' -shared'
104        self.compiler.set_executables(**args)
105
106        build_ext.build_extensions(self)
107
108    def build_extension(self, ext):
109
110        try:
111            build_ext.build_extension(self, ext)
112        except (CCompilerError, DistutilsError), why:
113            self.announce('WARNING: building of extension "%s" failed: %s' %
114                          (ext.name, sys.exc_info()[1]))
115
116    def get_platform (self):
117        # Get value of sys.platform
118        platform = sys.platform
119        if platform[:6] =='cygwin':
120            platform = 'cygwin'
121
122        return platform
123
124    def detect_modules(self):
125        # Ensure that /usr/local is always used
126        if '/usr/local/lib' not in self.compiler.library_dirs:
127            self.compiler.library_dirs.append('/usr/local/lib')
128        if '/usr/local/include' not in self.compiler.include_dirs:
129            self.compiler.include_dirs.append( '/usr/local/include' )
130
131        # lib_dirs and inc_dirs are used to search for files;
132        # if a file is found in one of those directories, it can
133        # be assumed that no additional -I,-L directives are needed.
134        lib_dirs = self.compiler.library_dirs + ['/lib', '/usr/lib']
135        inc_dirs = ['/usr/include'] + self.compiler.include_dirs
136        exts = []
137
138        platform = self.get_platform()
139
140        # Check for MacOS X, which doesn't need libm.a at all
141        math_libs = ['m']
142        if platform == 'Darwin1.2':
143            math_libs = []
144
145        # XXX Omitted modules: gl, pure, dl, SGI-specific modules
146
147        #
148        # The following modules are all pretty straightforward, and compile
149        # on pretty much any POSIXish platform.
150        #
151
152        # Some modules that are normally always on:
153        exts.append( Extension('regex', ['regexmodule.c', 'regexpr.c']) )
154        exts.append( Extension('pcre', ['pcremodule.c', 'pypcre.c']) )
155
156        exts.append( Extension('xreadlines', ['xreadlinesmodule.c']) )
157
158        # array objects
159        exts.append( Extension('array', ['arraymodule.c']) )
160        # complex math library functions
161        exts.append( Extension('cmath', ['cmathmodule.c'],
162                               libraries=math_libs) )
163
164        # math library functions, e.g. sin()
165        exts.append( Extension('math',  ['mathmodule.c'],
166                               libraries=math_libs) )
167        # fast string operations implemented in C
168        exts.append( Extension('strop', ['stropmodule.c']) )
169        # time operations and variables
170        exts.append( Extension('time', ['timemodule.c'],
171                               libraries=math_libs) )
172        # operator.add() and similar goodies
173        exts.append( Extension('operator', ['operator.c']) )
174        # access to the builtin codecs and codec registry
175        exts.append( Extension('_codecs', ['_codecsmodule.c']) )
176        # static Unicode character database
177        exts.append( Extension('unicodedata', ['unicodedata.c']) )
178        # access to ISO C locale support
179        exts.append( Extension('_locale', ['_localemodule.c']) )
180
181        # Modules with some UNIX dependencies -- on by default:
182        # (If you have a really backward UNIX, select and socket may not be
183        # supported...)
184
185        # fcntl(2) and ioctl(2)
186        exts.append( Extension('fcntl', ['fcntlmodule.c']) )
187        # pwd(3)
188        exts.append( Extension('pwd', ['pwdmodule.c']) )
189        # grp(3)
190        exts.append( Extension('grp', ['grpmodule.c']) )
191        # posix (UNIX) errno values
192        exts.append( Extension('errno', ['errnomodule.c']) )
193        # select(2); not on ancient System V
194        exts.append( Extension('select', ['selectmodule.c']) )
195
196        # The md5 module implements the RSA Data Security, Inc. MD5
197        # Message-Digest Algorithm, described in RFC 1321.  The necessary files
198        # md5c.c and md5.h are included here.
199        exts.append( Extension('md5', ['md5module.c', 'md5c.c']) )
200
201        # The sha module implements the SHA checksum algorithm.
202        # (NIST's Secure Hash Algorithm.)
203        exts.append( Extension('sha', ['shamodule.c']) )
204
205        # Tommy Burnette's 'new' module (creates new empty objects of certain
206        # kinds):
207        exts.append( Extension('new', ['newmodule.c']) )
208
209        # Helper module for various ascii-encoders
210        exts.append( Extension('binascii', ['binascii.c']) )
211
212        # Fred Drake's interface to the Python parser
213        exts.append( Extension('parser', ['parsermodule.c']) )
214
215        # Digital Creations' cStringIO and cPickle
216        exts.append( Extension('cStringIO', ['cStringIO.c']) )
217        exts.append( Extension('cPickle', ['cPickle.c']) )
218
219        # Memory-mapped files (also works on Win32).
220        exts.append( Extension('mmap', ['mmapmodule.c']) )
221
222        # Lance Ellinghaus's modules:
223        # enigma-inspired encryption
224        exts.append( Extension('rotor', ['rotormodule.c']) )
225        # syslog daemon interface
226        exts.append( Extension('syslog', ['syslogmodule.c']) )
227
228        # George Neville-Neil's timing module:
229        exts.append( Extension('timing', ['timingmodule.c']) )
230
231        #
232        # Here ends the simple stuff.  From here on, modules need certain
233        # libraries, are platform-specific, or present other surprises.
234        #
235
236        # Multimedia modules
237        # These don't work for 64-bit platforms!!!
238        # These represent audio samples or images as strings:
239
240        # Disabled on 64-bit platforms
241        if sys.maxint != 9223372036854775807L:
242            # Operations on audio samples
243            exts.append( Extension('audioop', ['audioop.c']) )
244            # Operations on images
245            exts.append( Extension('imageop', ['imageop.c']) )
246            # Read SGI RGB image files (but coded portably)
247            exts.append( Extension('rgbimg', ['rgbimgmodule.c']) )
248
249        # readline
250        if self.compiler.find_library_file(lib_dirs, 'readline'):
251            readline_libs = ['readline']
252            if self.compiler.find_library_file(lib_dirs +
253                                               ['/usr/lib/termcap'],
254                                               'termcap'):
255                readline_libs.append('termcap')
256            exts.append( Extension('readline', ['readline.c'],
257                                   library_dirs=['/usr/lib/termcap'],
258                                   libraries=readline_libs) )
259
260        # The crypt module is now disabled by default because it breaks builds
261        # on many systems (where -lcrypt is needed), e.g. Linux (I believe).
262
263        if self.compiler.find_library_file(lib_dirs, 'crypt'):
264            libs = ['crypt']
265        else:
266            libs = []
267        exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
268
269        # socket(2)
270        # Detect SSL support for the socket module
271        ssl_incs = find_file('openssl/ssl.h', inc_dirs,
272                             ['/usr/local/ssl/include',
273                              '/usr/contrib/ssl/include/'
274                             ]
275                             )
276        ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
277                                     ['/usr/local/ssl/lib',
278                                      '/usr/contrib/ssl/lib/'
279                                     ] )
280
281        if (ssl_incs is not None and
282            ssl_libs is not None):
283            exts.append( Extension('_socket', ['socketmodule.c'],
284                                   include_dirs = ssl_incs,
285                                   library_dirs = ssl_libs,
286                                   libraries = ['ssl', 'crypto'],
287                                   define_macros = [('USE_SSL',1)] ) )
288        else:
289            exts.append( Extension('_socket', ['socketmodule.c']) )
290
291        # Modules that provide persistent dictionary-like semantics.  You will
292        # probably want to arrange for at least one of them to be available on
293        # your machine, though none are defined by default because of library
294        # dependencies.  The Python module anydbm.py provides an
295        # implementation independent wrapper for these; dumbdbm.py provides
296        # similar functionality (but slower of course) implemented in Python.
297
298        # The standard Unix dbm module:
299        if platform not in ['cygwin']:
300            if (self.compiler.find_library_file(lib_dirs, 'ndbm')):
301                exts.append( Extension('dbm', ['dbmmodule.c'],
302                                       libraries = ['ndbm'] ) )
303            else:
304                exts.append( Extension('dbm', ['dbmmodule.c']) )
305
306        # Anthony Baxter's gdbm module.  GNU dbm(3) will require -lgdbm:
307        if (self.compiler.find_library_file(lib_dirs, 'gdbm')):
308            exts.append( Extension('gdbm', ['gdbmmodule.c'],
309                                   libraries = ['gdbm'] ) )
310
311        # Berkeley DB interface.
312        #
313        # This requires the Berkeley DB code, see
314        # ftp://ftp.cs.berkeley.edu/pub/4bsd/db.1.85.tar.gz
315        #
316        # Edit the variables DB and DBPORT to point to the db top directory
317        # and the subdirectory of PORT where you built it.
318        #
319        # (See http://electricrain.com/greg/python/bsddb3/ for an interface to
320        # BSD DB 3.x.)
321
322        # Note: If a db.h file is found by configure, bsddb will be enabled
323        # automatically via Setup.config.in.  It only needs to be enabled here
324        # if it is not automatically enabled there; check the generated
325        # Setup.config before enabling it here.
326
327        db_incs = find_file('db_185.h', inc_dirs, [])
328        if (db_incs is not None and
329            self.compiler.find_library_file(lib_dirs, 'db') ):
330            exts.append( Extension('bsddb', ['bsddbmodule.c'],
331                                   include_dirs = db_incs,
332                                   libraries = ['db'] ) )
333
334        # The mpz module interfaces to the GNU Multiple Precision library.
335        # You need to ftp the GNU MP library.
336        # This was originally written and tested against GMP 1.2 and 1.3.2.
337        # It has been modified by Rob Hooft to work with 2.0.2 as well, but I
338        # haven't tested it recently.   For a more complete module,
339        # refer to pympz.sourceforge.net.
340
341        # A compatible MP library unencombered by the GPL also exists.  It was
342        # posted to comp.sources.misc in volume 40 and is widely available from
343        # FTP archive sites. One URL for it is:
344        # ftp://gatekeeper.dec.com/.b/usenet/comp.sources.misc/volume40/fgmp/part01.Z
345
346        # Anthony Baxter's gdbm module.  GNU dbm(3) will require -lgdbm:
347        if (self.compiler.find_library_file(lib_dirs, 'gmp')):
348            exts.append( Extension('mpz', ['mpzmodule.c'],
349                                   libraries = ['gmp'] ) )
350
351
352        # Unix-only modules
353        if platform not in ['mac', 'win32']:
354            # Steen Lumholt's termios module
355            exts.append( Extension('termios', ['termios.c']) )
356            # Jeremy Hylton's rlimit interface
357            if platform not in ['cygwin']:
358                exts.append( Extension('resource', ['resource.c']) )
359
360            if (self.compiler.find_library_file(lib_dirs, 'nsl')):
361                exts.append( Extension('nis', ['nismodule.c'],
362                                       libraries = ['nsl']) )
363
364        # Curses support, requring the System V version of curses, often
365        # provided by the ncurses library.
366        if platform == 'sunos4':
367            include_dirs += ['/usr/5include']
368            lib_dirs += ['/usr/5lib']
369
370        if (self.compiler.find_library_file(lib_dirs, 'ncurses')):
371            curses_libs = ['ncurses']
372            exts.append( Extension('_curses', ['_cursesmodule.c'],
373                                   libraries = curses_libs) )
374        elif (self.compiler.find_library_file(lib_dirs, 'curses')):
375            if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
376                curses_libs = ['curses', 'terminfo']
377            else:
378                curses_libs = ['curses', 'termcap']
379
380            exts.append( Extension('_curses', ['_cursesmodule.c'],
381                                   libraries = curses_libs) )
382
383        # If the curses module is enabled, check for the panel module
384        if (os.path.exists('Modules/_curses_panel.c') and
385            module_enabled(exts, '_curses') and
386            self.compiler.find_library_file(lib_dirs, 'panel')):
387            exts.append( Extension('_curses_panel', ['_curses_panel.c'],
388                                   libraries = ['panel'] + curses_libs) )
389
390
391
392        # Lee Busby's SIGFPE modules.
393        # The library to link fpectl with is platform specific.
394        # Choose *one* of the options below for fpectl:
395
396        if platform == 'irix5':
397            # For SGI IRIX (tested on 5.3):
398            exts.append( Extension('fpectl', ['fpectlmodule.c'],
399                                   libraries=['fpe']) )
400        elif 0: # XXX how to detect SunPro?
401            # For Solaris with SunPro compiler (tested on Solaris 2.5 with SunPro C 4.2):
402            # (Without the compiler you don't have -lsunmath.)
403            #fpectl fpectlmodule.c -R/opt/SUNWspro/lib -lsunmath -lm
404            pass
405        else:
406            # For other systems: see instructions in fpectlmodule.c.
407            #fpectl fpectlmodule.c ...
408            exts.append( Extension('fpectl', ['fpectlmodule.c']) )
409
410
411        # Andrew Kuchling's zlib module.
412        # This require zlib 1.1.3 (or later).
413        # See http://www.cdrom.com/pub/infozip/zlib/
414        if (self.compiler.find_library_file(lib_dirs, 'z')):
415            exts.append( Extension('zlib', ['zlibmodule.c'],
416                                   libraries = ['z']) )
417
418        # Interface to the Expat XML parser
419        #
420        # Expat is written by James Clark and must be downloaded separately
421        # (see below).  The pyexpat module was written by Paul Prescod after a
422        # prototype by Jack Jansen.
423        #
424        # The Expat dist includes Windows .lib and .dll files.  Home page is
425        # at http://www.jclark.com/xml/expat.html, the current production
426        # release is always ftp://ftp.jclark.com/pub/xml/expat.zip.
427        #
428        # EXPAT_DIR, below, should point to the expat/ directory created by
429        # unpacking the Expat source distribution.
430        #
431        # Note: the expat build process doesn't yet build a libexpat.a; you
432        # can do this manually while we try convince the author to add it.  To
433        # do so, cd to EXPAT_DIR, run "make" if you have not done so, then
434        # run:
435        #
436        #    ar cr libexpat.a xmltok/*.o xmlparse/*.o
437        #
438        expat_defs = []
439        expat_incs = find_file('expat.h', inc_dirs, [])
440        if expat_incs is not None:
441            # expat.h was found
442            expat_defs = [('HAVE_EXPAT_H', 1)]
443        else:
444            expat_incs = find_file('xmlparse.h', inc_dirs, [])
445
446        if (expat_incs is not None and
447            self.compiler.find_library_file(lib_dirs, 'expat')):
448            exts.append( Extension('pyexpat', ['pyexpat.c'],
449                                   define_macros = expat_defs,
450                                   libraries = ['expat']) )
451
452        # Platform-specific libraries
453        if platform == 'linux2':
454            # Linux-specific modules
455            exts.append( Extension('linuxaudiodev', ['linuxaudiodev.c']) )
456
457        if platform == 'sunos5':
458            # SunOS specific modules
459            exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
460
461        self.extensions.extend(exts)
462
463        # Call the method for detecting whether _tkinter can be compiled
464        self.detect_tkinter(inc_dirs, lib_dirs)
465
466
467    def detect_tkinter(self, inc_dirs, lib_dirs):
468        # The _tkinter module.
469        #
470        # The command for _tkinter is long and site specific.  Please
471        # uncomment and/or edit those parts as indicated.  If you don't have a
472        # specific extension (e.g. Tix or BLT), leave the corresponding line
473        # commented out.  (Leave the trailing backslashes in!  If you
474        # experience strange errors, you may want to join all uncommented
475        # lines and remove the backslashes -- the backslash interpretation is
476        # done by the shell's "read" command and it may not be implemented on
477        # every system.
478
479        # Assume we haven't found any of the libraries or include files
480        tcllib = tklib = tcl_includes = tk_includes = None
481        for version in ['8.4', '8.3', '8.2', '8.1', '8.0']:
482             tklib = self.compiler.find_library_file(lib_dirs,
483                                                     'tk' + version )
484             tcllib = self.compiler.find_library_file(lib_dirs,
485                                                      'tcl' + version )
486             if tklib and tcllib:
487                # Exit the loop when we've found the Tcl/Tk libraries
488                break
489
490        # Now check for the header files
491        if tklib and tcllib:
492            # Check for the include files on Debian, where
493            # they're put in /usr/include/{tcl,tk}X.Y
494            debian_tcl_include = ( '/usr/include/tcl' + version )
495            debian_tk_include =  ( '/usr/include/tk'  + version )
496            tcl_includes = find_file('tcl.h', inc_dirs,
497                                     [debian_tcl_include]
498                                     )
499            tk_includes = find_file('tk.h', inc_dirs,
500                                     [debian_tk_include]
501                                     )
502
503        if (tcllib is None or tklib is None and
504            tcl_includes is None or tk_includes is None):
505            # Something's missing, so give up
506            return
507
508        # OK... everything seems to be present for Tcl/Tk.
509
510        include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
511        for dir in tcl_includes + tk_includes:
512            if dir not in include_dirs:
513                include_dirs.append(dir)
514
515        # Check for various platform-specific directories
516        platform = self.get_platform()
517        if platform == 'sunos5':
518            include_dirs.append('/usr/openwin/include')
519            added_lib_dirs.append('/usr/openwin/lib')
520        elif os.path.exists('/usr/X11R6/include'):
521            include_dirs.append('/usr/X11R6/include')
522            added_lib_dirs.append('/usr/X11R6/lib')
523        elif os.path.exists('/usr/X11R5/include'):
524            include_dirs.append('/usr/X11R5/include')
525            added_lib_dirs.append('/usr/X11R5/lib')
526        else:
527            # Assume default location for X11
528            include_dirs.append('/usr/X11/include')
529            added_lib_dirs.append('/usr/X11/lib')
530
531        # Check for Tix extension
532        if self.compiler.find_library_file(lib_dirs + added_lib_dirs, 'tix4.1.8.0'):
533            defs.append( ('WITH_TIX', 1) )
534            libs.append('tix4.1.8.0')
535
536        # Check for BLT extension
537        if self.compiler.find_library_file(lib_dirs + added_lib_dirs, 'BLT8.0'):
538            defs.append( ('WITH_BLT', 1) )
539            libs.append('BLT8.0')
540
541        # Add the Tcl/Tk libraries
542        libs.append('tk'+version)
543        libs.append('tcl'+version)
544
545        if platform in ['aix3', 'aix4']:
546            libs.append('ld')
547
548        # Finally, link with the X11 libraries
549        libs.append('X11')
550
551        ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
552                        define_macros=[('WITH_APPINIT', 1)] + defs,
553                        include_dirs = include_dirs,
554                        libraries = libs,
555                        library_dirs = added_lib_dirs,
556                        )
557        self.extensions.append(ext)
558
559        # XXX handle these, but how to detect?
560        # *** Uncomment and edit for PIL (TkImaging) extension only:
561        #       -DWITH_PIL -I../Extensions/Imaging/libImaging  tkImaging.c \
562        # *** Uncomment and edit for TOGL extension only:
563        #       -DWITH_TOGL togl.c \
564        # *** Uncomment these for TOGL extension only:
565        #       -lGL -lGLU -lXext -lXmu \
566
567def main():
568    setup(name = 'Python standard library',
569          version = '%d.%d' % sys.version_info[:2],
570          cmdclass = {'build_ext':PyBuildExt},
571          # The struct module is defined here, because build_ext won't be
572          # called unless there's at least one extension module defined.
573          ext_modules=[Extension('struct', ['structmodule.c'])]
574        )
575
576# --install-platlib
577if __name__ == '__main__':
578    sysconfig.set_python_build()
579    main()
580