/Misc/BeOS-setup.py

http://unladen-swallow.googlecode.com/ · Python · 574 lines · 312 code · 84 blank · 178 comment · 72 complexity · f710d7e4c964cdb8837bf4e649067665 MD5 · raw file

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