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