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