/setup.py

http://unladen-swallow.googlecode.com/ · Python · 1966 lines · 1338 code · 220 blank · 408 comment · 359 complexity · 876f60a7a1997b71b1e8eb54e199eaf4 MD5 · raw file

Large files are truncated click here to view the full file

  1. # Autodetecting setup.py script for building the Python extensions
  2. #
  3. __version__ = "$Revision: 75282 $"
  4. import sys, os, imp, re, optparse
  5. from glob import glob
  6. from platform import machine as platform_machine
  7. from distutils import log
  8. from distutils import sysconfig
  9. from distutils import text_file
  10. from distutils.errors import *
  11. from distutils.core import Extension, setup
  12. from distutils.command.build_ext import build_ext
  13. from distutils.command.install import install
  14. from distutils.command.install_lib import install_lib
  15. from distutils.util import get_platform
  16. # This global variable is used to hold the list of modules to be disabled.
  17. disabled_module_list = []
  18. def record_build_dir():
  19. """Record the relative path to the build diretory for site.py."""
  20. build_dir = "build/lib.%s-%.3s" % (get_platform(), sys.version)
  21. build_dir_file = os.path.join(os.path.dirname(sys.executable), "build_dir")
  22. f = open(build_dir_file, "w")
  23. try:
  24. f.write(build_dir)
  25. finally:
  26. f.close()
  27. def add_dir_to_list(dirlist, dir):
  28. """Add the directory 'dir' to the list 'dirlist' (at the front) if
  29. 1) 'dir' is not already in 'dirlist'
  30. 2) 'dir' actually exists, and is a directory."""
  31. if dir is not None and os.path.isdir(dir) and dir not in dirlist:
  32. dirlist.insert(0, dir)
  33. def find_file(filename, std_dirs, paths):
  34. """Searches for the directory where a given file is located,
  35. and returns a possibly-empty list of additional directories, or None
  36. if the file couldn't be found at all.
  37. 'filename' is the name of a file, such as readline.h or libcrypto.a.
  38. 'std_dirs' is the list of standard system directories; if the
  39. file is found in one of them, no additional directives are needed.
  40. 'paths' is a list of additional locations to check; if the file is
  41. found in one of them, the resulting list will contain the directory.
  42. """
  43. # Check the standard locations
  44. for dir in std_dirs:
  45. f = os.path.join(dir, filename)
  46. if os.path.exists(f): return []
  47. # Check the additional directories
  48. for dir in paths:
  49. f = os.path.join(dir, filename)
  50. if os.path.exists(f):
  51. return [dir]
  52. # Not found anywhere
  53. return None
  54. def find_library_file(compiler, libname, std_dirs, paths):
  55. result = compiler.find_library_file(std_dirs + paths, libname)
  56. if result is None:
  57. return None
  58. # Check whether the found file is in one of the standard directories
  59. dirname = os.path.dirname(result)
  60. for p in std_dirs:
  61. # Ensure path doesn't end with path separator
  62. p = p.rstrip(os.sep)
  63. if p == dirname:
  64. return [ ]
  65. # Otherwise, it must have been in one of the additional directories,
  66. # so we have to figure out which one.
  67. for p in paths:
  68. # Ensure path doesn't end with path separator
  69. p = p.rstrip(os.sep)
  70. if p == dirname:
  71. return [p]
  72. else:
  73. assert False, "Internal error: Path not found in std_dirs or paths"
  74. def module_enabled(extlist, modname):
  75. """Returns whether the module 'modname' is present in the list
  76. of extensions 'extlist'."""
  77. extlist = [ext for ext in extlist if ext.name == modname]
  78. return len(extlist)
  79. def find_module_file(module, dirlist):
  80. """Find a module in a set of possible folders. If it is not found
  81. return the unadorned filename"""
  82. list = find_file(module, [], dirlist)
  83. if not list:
  84. return module
  85. if len(list) > 1:
  86. log.info("WARNING: multiple copies of %s found"%module)
  87. return os.path.join(list[0], module)
  88. def combine_dirs_to_check(extra_dirs, orig_lib_dirs):
  89. extra_dirs = [d for d in extra_dirs if d and os.path.isdir(d)]
  90. # First search extra directories that already appear in the
  91. # original list.
  92. extra_dirs.sort(key=lambda d:0 if d in orig_lib_dirs else 1)
  93. return extra_dirs + orig_lib_dirs
  94. class PyBuildExt(build_ext):
  95. def __init__(self, dist):
  96. build_ext.__init__(self, dist)
  97. self.failed = []
  98. def build_extensions(self):
  99. # Detect which modules should be compiled
  100. missing = self.detect_modules()
  101. # Remove modules that are present on the disabled list
  102. extensions = [ext for ext in self.extensions
  103. if ext.name not in disabled_module_list]
  104. # move ctypes to the end, it depends on other modules
  105. ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
  106. if "_ctypes" in ext_map:
  107. ctypes = extensions.pop(ext_map["_ctypes"])
  108. extensions.append(ctypes)
  109. self.extensions = extensions
  110. # Fix up the autodetected modules, prefixing all the source files
  111. # with Modules/ and adding Python's include directory to the path.
  112. (srcdir,) = sysconfig.get_config_vars('srcdir')
  113. if not srcdir:
  114. # Maybe running on Windows but not using CYGWIN?
  115. raise ValueError("No source directory; cannot proceed.")
  116. # Figure out the location of the source code for extension modules
  117. # (This logic is copied in distutils.test.test_sysconfig,
  118. # so building in a separate directory does not break test_distutils.)
  119. moddir = os.path.join(os.getcwd(), srcdir, 'Modules')
  120. moddir = os.path.normpath(moddir)
  121. srcdir, tail = os.path.split(moddir)
  122. srcdir = os.path.normpath(srcdir)
  123. moddir = os.path.normpath(moddir)
  124. moddirlist = [moddir]
  125. incdirlist = ['./Include']
  126. # Platform-dependent module source and include directories
  127. platform = self.get_platform()
  128. if platform in ('darwin', 'mac') and ("--disable-toolbox-glue" not in
  129. sysconfig.get_config_var("CONFIG_ARGS")):
  130. # Mac OS X also includes some mac-specific modules
  131. macmoddir = os.path.join(os.getcwd(), srcdir, 'Mac/Modules')
  132. moddirlist.append(macmoddir)
  133. incdirlist.append('./Mac/Include')
  134. alldirlist = moddirlist + incdirlist
  135. # Fix up the paths for scripts, too
  136. self.distribution.scripts = [os.path.join(srcdir, filename)
  137. for filename in self.distribution.scripts]
  138. # Python header files
  139. headers = glob("Include/*.h") + ["pyconfig.h"]
  140. for ext in self.extensions[:]:
  141. ext.sources = [ find_module_file(filename, moddirlist)
  142. for filename in ext.sources ]
  143. if ext.depends is not None:
  144. ext.depends = [find_module_file(filename, alldirlist)
  145. for filename in ext.depends]
  146. else:
  147. ext.depends = []
  148. # re-compile extensions if a header file has been changed
  149. ext.depends.extend(headers)
  150. ext.include_dirs.append( '.' ) # to get config.h
  151. for incdir in incdirlist:
  152. ext.include_dirs.append( os.path.join(srcdir, incdir) )
  153. # If a module has already been built statically,
  154. # don't build it here
  155. if ext.name in sys.builtin_module_names:
  156. self.extensions.remove(ext)
  157. if platform != 'mac':
  158. # Parse Modules/Setup and Modules/Setup.local to figure out which
  159. # modules are turned on in the file.
  160. remove_modules = []
  161. for filename in ('Modules/Setup', 'Modules/Setup.local'):
  162. input = text_file.TextFile(filename, join_lines=1)
  163. while 1:
  164. line = input.readline()
  165. if not line: break
  166. line = line.split()
  167. remove_modules.append(line[0])
  168. input.close()
  169. for ext in self.extensions[:]:
  170. if ext.name in remove_modules:
  171. self.extensions.remove(ext)
  172. # When you run "make CC=altcc" or something similar, you really want
  173. # those environment variables passed into the setup.py phase. Here's
  174. # a small set of useful ones.
  175. compiler = os.environ.get('CC')
  176. args = {}
  177. # unfortunately, distutils doesn't let us provide separate C and C++
  178. # compilers
  179. if compiler is not None:
  180. (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
  181. args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
  182. self.compiler.set_executables(**args)
  183. build_ext.build_extensions(self)
  184. longest = max([len(e.name) for e in self.extensions])
  185. if self.failed:
  186. longest = max(longest, max([len(name) for name in self.failed]))
  187. def print_three_column(lst):
  188. lst.sort(key=str.lower)
  189. # guarantee zip() doesn't drop anything
  190. while len(lst) % 3:
  191. lst.append("")
  192. for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
  193. print "%-*s %-*s %-*s" % (longest, e, longest, f,
  194. longest, g)
  195. if missing:
  196. print
  197. print "Failed to find the necessary bits to build these modules:"
  198. print_three_column(missing)
  199. print ("To find the necessary bits, look in setup.py in"
  200. " detect_modules() for the module's name.")
  201. print
  202. if self.failed:
  203. failed = self.failed[:]
  204. print
  205. print "Failed to build these modules:"
  206. print_three_column(failed)
  207. print
  208. def build_extension(self, ext):
  209. if ext.name == '_ctypes':
  210. if not self.configure_ctypes(ext):
  211. return
  212. try:
  213. build_ext.build_extension(self, ext)
  214. except (CCompilerError, DistutilsError), why:
  215. self.announce('WARNING: building of extension "%s" failed: %s' %
  216. (ext.name, sys.exc_info()[1]))
  217. self.failed.append(ext.name)
  218. return
  219. # Workaround for Mac OS X: The Carbon-based modules cannot be
  220. # reliably imported into a command-line Python
  221. if 'Carbon' in ext.extra_link_args:
  222. self.announce(
  223. 'WARNING: skipping import check for Carbon-based "%s"' %
  224. ext.name)
  225. return
  226. if self.get_platform() == 'darwin' and (
  227. sys.maxint > 2**32 and '-arch' in ext.extra_link_args):
  228. # Don't bother doing an import check when an extension was
  229. # build with an explicit '-arch' flag on OSX. That's currently
  230. # only used to build 32-bit only extensions in a 4-way
  231. # universal build and loading 32-bit code into a 64-bit
  232. # process will fail.
  233. self.announce(
  234. 'WARNING: skipping import check for "%s"' %
  235. ext.name)
  236. return
  237. # Workaround for Cygwin: Cygwin currently has fork issues when many
  238. # modules have been imported
  239. if self.get_platform() == 'cygwin':
  240. self.announce('WARNING: skipping import check for Cygwin-based "%s"'
  241. % ext.name)
  242. return
  243. ext_filename = os.path.join(
  244. self.build_lib,
  245. self.get_ext_filename(self.get_ext_fullname(ext.name)))
  246. try:
  247. imp.load_dynamic(ext.name, ext_filename)
  248. except ImportError, why:
  249. self.failed.append(ext.name)
  250. self.announce('*** WARNING: renaming "%s" since importing it'
  251. ' failed: %s' % (ext.name, why), level=3)
  252. assert not self.inplace
  253. basename, tail = os.path.splitext(ext_filename)
  254. newname = basename + "_failed" + tail
  255. if os.path.exists(newname):
  256. os.remove(newname)
  257. os.rename(ext_filename, newname)
  258. # XXX -- This relies on a Vile HACK in
  259. # distutils.command.build_ext.build_extension(). The
  260. # _built_objects attribute is stored there strictly for
  261. # use here.
  262. # If there is a failure, _built_objects may not be there,
  263. # so catch the AttributeError and move on.
  264. try:
  265. for filename in self._built_objects:
  266. os.remove(filename)
  267. except AttributeError:
  268. self.announce('unable to remove files (ignored)')
  269. except:
  270. exc_type, why, tb = sys.exc_info()
  271. self.announce('*** WARNING: importing extension "%s" '
  272. 'failed with %s: %s' % (ext.name, exc_type, why),
  273. level=3)
  274. self.failed.append(ext.name)
  275. def get_platform(self):
  276. # Get value of sys.platform
  277. for platform in ['cygwin', 'beos', 'darwin', 'atheos', 'osf1']:
  278. if sys.platform.startswith(platform):
  279. return platform
  280. return sys.platform
  281. def detect_modules(self):
  282. use_system_paths = not os.getenv('IGNORE_SYSTEM_PATHS')
  283. if use_system_paths:
  284. # Ensure that /usr/local is always used
  285. add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
  286. add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
  287. # Add paths specified in the environment variables LDFLAGS and
  288. # CPPFLAGS for header and library files.
  289. # We must get the values from the Makefile and not the environment
  290. # directly since an inconsistently reproducible issue comes up where
  291. # the environment variable is not set even though the value were passed
  292. # into configure and stored in the Makefile (issue found on OS X 10.3).
  293. for env_var, arg_name, dir_list in (
  294. ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
  295. ('LDFLAGS', '-L', self.compiler.library_dirs),
  296. ('CPPFLAGS', '-I', self.compiler.include_dirs)):
  297. env_val = sysconfig.get_config_var(env_var)
  298. if env_val:
  299. # To prevent optparse from raising an exception about any
  300. # options in env_val that it doesn't know about we strip out
  301. # all double dashes and any dashes followed by a character
  302. # that is not for the option we are dealing with.
  303. #
  304. # Please note that order of the regex is important! We must
  305. # strip out double-dashes first so that we don't end up with
  306. # substituting "--Long" to "-Long" and thus lead to "ong" being
  307. # used for a library directory.
  308. env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
  309. ' ', env_val)
  310. parser = optparse.OptionParser()
  311. # Make sure that allowing args interspersed with options is
  312. # allowed
  313. parser.allow_interspersed_args = True
  314. parser.error = lambda msg: None
  315. parser.add_option(arg_name, dest="dirs", action="append")
  316. options = parser.parse_args(env_val.split())[0]
  317. if options.dirs:
  318. for directory in reversed(options.dirs):
  319. add_dir_to_list(dir_list, directory)
  320. if os.path.normpath(sys.prefix) != '/usr':
  321. add_dir_to_list(self.compiler.library_dirs,
  322. sysconfig.get_config_var("LIBDIR"))
  323. add_dir_to_list(self.compiler.include_dirs,
  324. sysconfig.get_config_var("INCLUDEDIR"))
  325. try:
  326. have_unicode = unicode
  327. except NameError:
  328. have_unicode = 0
  329. # lib_dirs and inc_dirs are used to search for files;
  330. # if a file is found in one of those directories, it can
  331. # be assumed that no additional -I,-L directives are needed.
  332. lib_dirs = list(self.compiler.library_dirs)
  333. inc_dirs = list(self.compiler.include_dirs)
  334. if use_system_paths:
  335. lib_dirs += [
  336. '/lib64', '/usr/lib64',
  337. '/lib', '/usr/lib',
  338. ]
  339. inc_dirs += ['/usr/include']
  340. exts = []
  341. missing = []
  342. config_h = sysconfig.get_config_h_filename()
  343. config_h_vars = sysconfig.parse_config_h(open(config_h))
  344. platform = self.get_platform()
  345. (srcdir,) = sysconfig.get_config_vars('srcdir')
  346. # Check for AtheOS which has libraries in non-standard locations
  347. if platform == 'atheos':
  348. if use_system_paths:
  349. lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
  350. lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
  351. inc_dirs += ['/system/include', '/atheos/autolnk/include']
  352. inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
  353. # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
  354. if (platform in ['osf1', 'unixware7', 'openunix8'] and
  355. use_system_paths):
  356. lib_dirs += ['/usr/ccs/lib']
  357. if platform == 'darwin' or platform == 'linux2':
  358. # This should work on any unixy platform ;-)
  359. # If the user has bothered specifying additional -I and -L flags
  360. # in OPT and LDFLAGS we might as well use them here.
  361. # NOTE: using shlex.split would technically be more correct, but
  362. # also gives a bootstrap problem. Let's hope nobody uses directories
  363. # with whitespace in the name to store libraries.
  364. cflags, ldflags = sysconfig.get_config_vars(
  365. 'CFLAGS', 'LDFLAGS')
  366. for item in cflags.split():
  367. if item.startswith('-I'):
  368. inc_dirs.append(item[2:])
  369. for item in ldflags.split():
  370. if item.startswith('-L'):
  371. lib_dirs.append(item[2:])
  372. # Check for MacOS X, which doesn't need libm.a at all
  373. math_libs = ['m']
  374. if platform in ['darwin', 'beos', 'mac']:
  375. math_libs = []
  376. # XXX Omitted modules: gl, pure, dl, SGI-specific modules
  377. #
  378. # The following modules are all pretty straightforward, and compile
  379. # on pretty much any POSIXish platform.
  380. #
  381. # Some modules that are normally always on:
  382. exts.append( Extension('_weakref', ['_weakref.c']) )
  383. # array objects
  384. exts.append( Extension('array', ['arraymodule.c']) )
  385. # complex math library functions
  386. exts.append( Extension('cmath', ['cmathmodule.c'],
  387. libraries=math_libs) )
  388. # math library functions, e.g. sin()
  389. exts.append( Extension('math', ['mathmodule.c'],
  390. libraries=math_libs) )
  391. # fast string operations implemented in C
  392. exts.append( Extension('strop', ['stropmodule.c']) )
  393. # time operations and variables
  394. exts.append( Extension('time', ['timemodule.c'],
  395. libraries=math_libs) )
  396. exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'],
  397. libraries=math_libs) )
  398. # fast iterator tools implemented in C
  399. exts.append( Extension("itertools", ["itertoolsmodule.c"]) )
  400. # code that will be builtins in the future, but conflict with the
  401. # current builtins
  402. exts.append( Extension('future_builtins', ['future_builtins.c']) )
  403. # random number generator implemented in C
  404. exts.append( Extension("_random", ["_randommodule.c"]) )
  405. # high-performance collections
  406. exts.append( Extension("_collections", ["_collectionsmodule.c"]) )
  407. # bisect
  408. exts.append( Extension("_bisect", ["_bisectmodule.c"]) )
  409. # heapq
  410. exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
  411. # operator.add() and similar goodies
  412. exts.append( Extension('operator', ['operator.c']) )
  413. # Python 3.0 _fileio module
  414. exts.append( Extension("_fileio", ["_fileio.c"]) )
  415. # Python 3.0 _bytesio module
  416. exts.append( Extension("_bytesio", ["_bytesio.c"]) )
  417. # _functools
  418. exts.append( Extension("_functools", ["_functoolsmodule.c"]) )
  419. # _json speedups
  420. exts.append( Extension("_json", ["_json.c"]) )
  421. # Python C API test module
  422. exts.append( Extension('_testcapi', ['_testcapimodule.c', 'cPickle.c'],
  423. define_macros=[('NO_STATIC_MEMOTABLE', 1)],
  424. depends=['testcapi_long.h']) )
  425. # profilers (_lsprof is for cProfile.py)
  426. exts.append( Extension('_hotshot', ['_hotshot.c']) )
  427. exts.append( Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']) )
  428. # static Unicode character database
  429. if have_unicode:
  430. exts.append( Extension('unicodedata', ['unicodedata.c']) )
  431. else:
  432. missing.append('unicodedata')
  433. # access to ISO C locale support
  434. data = open('pyconfig.h').read()
  435. m = re.search(r"#s*define\s+WITH_LIBINTL\s+1\s*", data)
  436. if m is not None:
  437. locale_libs = ['intl']
  438. else:
  439. locale_libs = []
  440. if platform == 'darwin':
  441. locale_extra_link_args = ['-framework', 'CoreFoundation']
  442. else:
  443. locale_extra_link_args = []
  444. exts.append( Extension('_locale', ['_localemodule.c'],
  445. libraries=locale_libs,
  446. extra_link_args=locale_extra_link_args) )
  447. # Modules with some UNIX dependencies -- on by default:
  448. # (If you have a really backward UNIX, select and socket may not be
  449. # supported...)
  450. # fcntl(2) and ioctl(2)
  451. exts.append( Extension('fcntl', ['fcntlmodule.c']) )
  452. if platform not in ['mac']:
  453. # pwd(3)
  454. exts.append( Extension('pwd', ['pwdmodule.c']) )
  455. # grp(3)
  456. exts.append( Extension('grp', ['grpmodule.c']) )
  457. # spwd, shadow passwords
  458. if (config_h_vars.get('HAVE_GETSPNAM', False) or
  459. config_h_vars.get('HAVE_GETSPENT', False)):
  460. exts.append( Extension('spwd', ['spwdmodule.c']) )
  461. else:
  462. missing.append('spwd')
  463. else:
  464. missing.extend(['pwd', 'grp', 'spwd'])
  465. # select(2); not on ancient System V
  466. exts.append( Extension('select', ['selectmodule.c']) )
  467. # Fred Drake's interface to the Python parser
  468. exts.append( Extension('parser', ['parsermodule.c']) )
  469. # cStringIO and cPickle
  470. exts.append( Extension('cStringIO', ['cStringIO.c']) )
  471. exts.append( Extension('cPickle', ['cPickle.c']) )
  472. # Memory-mapped files (also works on Win32).
  473. if platform not in ['atheos', 'mac']:
  474. exts.append( Extension('mmap', ['mmapmodule.c']) )
  475. else:
  476. missing.append('mmap')
  477. # Lance Ellinghaus's syslog module
  478. if platform not in ['mac']:
  479. # syslog daemon interface
  480. exts.append( Extension('syslog', ['syslogmodule.c']) )
  481. else:
  482. missing.append('syslog')
  483. # George Neville-Neil's timing module:
  484. # Deprecated in PEP 4 http://www.python.org/peps/pep-0004.html
  485. # http://mail.python.org/pipermail/python-dev/2006-January/060023.html
  486. #exts.append( Extension('timing', ['timingmodule.c']) )
  487. #
  488. # Here ends the simple stuff. From here on, modules need certain
  489. # libraries, are platform-specific, or present other surprises.
  490. #
  491. # Multimedia modules
  492. # These don't work for 64-bit platforms!!!
  493. # These represent audio samples or images as strings:
  494. # Operations on audio samples
  495. # According to #993173, this one should actually work fine on
  496. # 64-bit platforms.
  497. exts.append( Extension('audioop', ['audioop.c']) )
  498. # Disabled on 64-bit platforms
  499. if sys.maxint != 9223372036854775807L:
  500. # Operations on images
  501. exts.append( Extension('imageop', ['imageop.c']) )
  502. else:
  503. missing.extend(['imageop'])
  504. # readline
  505. do_readline = self.compiler.find_library_file(lib_dirs, 'readline')
  506. if platform == 'darwin': # and os.uname()[2] < '9.':
  507. # MacOSX 10.4 has a broken readline. Don't try to build
  508. # the readline module unless the user has installed a fixed
  509. # readline package
  510. # FIXME: The readline emulation on 10.5 is better, but the
  511. # readline module doesn't compile out of the box.
  512. if find_file('readline/rlconf.h', inc_dirs, []) is None:
  513. do_readline = False
  514. if do_readline:
  515. if sys.platform == 'darwin':
  516. # In every directory on the search path search for a dynamic
  517. # library and then a static library, instead of first looking
  518. # for dynamic libraries on the entiry path.
  519. # This way a staticly linked custom readline gets picked up
  520. # before the (broken) dynamic library in /usr/lib.
  521. readline_extra_link_args = ('-Wl,-search_paths_first',)
  522. else:
  523. readline_extra_link_args = ()
  524. readline_libs = ['readline']
  525. if self.compiler.find_library_file(lib_dirs,
  526. 'ncursesw'):
  527. readline_libs.append('ncursesw')
  528. elif self.compiler.find_library_file(lib_dirs,
  529. 'ncurses'):
  530. readline_libs.append('ncurses')
  531. elif self.compiler.find_library_file(lib_dirs, 'curses'):
  532. readline_libs.append('curses')
  533. elif self.compiler.find_library_file(lib_dirs +
  534. ['/usr/lib/termcap'],
  535. 'termcap'):
  536. readline_libs.append('termcap')
  537. exts.append( Extension('readline', ['readline.c'],
  538. library_dirs=['/usr/lib/termcap'],
  539. extra_link_args=readline_extra_link_args,
  540. libraries=readline_libs) )
  541. else:
  542. missing.append('readline')
  543. if platform not in ['mac']:
  544. # crypt module.
  545. if self.compiler.find_library_file(lib_dirs, 'crypt'):
  546. libs = ['crypt']
  547. else:
  548. libs = []
  549. exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
  550. else:
  551. missing.append('crypt')
  552. # CSV files
  553. exts.append( Extension('_csv', ['_csv.c']) )
  554. # LLVM wrappers
  555. if sysconfig.get_config_var("WITH_LLVM"):
  556. exts.append( Extension('_llvm', ['_llvm.c']))
  557. else:
  558. missing.append('_llvm')
  559. # socket(2)
  560. exts.append( Extension('_socket', ['socketmodule.c'],
  561. depends = ['socketmodule.h']) )
  562. # Detect SSL support for the socket module (via _ssl)
  563. search_for_ssl_incs_in = []
  564. if use_system_paths:
  565. search_for_ssl_incs_in = [
  566. '/usr/local/ssl/include',
  567. '/usr/contrib/ssl/include/'
  568. ]
  569. if "SSL_ROOT" in os.environ:
  570. search_for_ssl_incs_in.append(os.path.join(os.environ["SSL_ROOT"],
  571. "include"))
  572. ssl_incs = find_file('openssl/ssl.h', inc_dirs,
  573. search_for_ssl_incs_in
  574. )
  575. if ssl_incs is not None:
  576. krb5_h = find_file(
  577. 'krb5.h', inc_dirs,
  578. ['/usr/kerberos/include'] if use_system_paths else [])
  579. if krb5_h:
  580. ssl_incs += krb5_h
  581. search_for_ssl_libs_in = []
  582. if use_system_paths:
  583. search_for_ssl_libs_in = ['/usr/local/ssl/lib',
  584. '/usr/contrib/ssl/lib/']
  585. if "SSL_ROOT" in os.environ:
  586. search_for_ssl_libs_in.append(os.path.join(os.environ["SSL_ROOT"],
  587. "lib"))
  588. ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
  589. search_for_ssl_libs_in)
  590. if (ssl_incs is not None and
  591. ssl_libs is not None):
  592. exts.append( Extension('_ssl', ['_ssl.c'],
  593. include_dirs = ssl_incs,
  594. library_dirs = ssl_libs,
  595. libraries = ['ssl', 'crypto'],
  596. depends = ['socketmodule.h']), )
  597. else:
  598. missing.append('_ssl')
  599. # find out which version of OpenSSL we have
  600. openssl_ver = 0
  601. openssl_ver_re = re.compile(
  602. '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
  603. for ssl_inc_dir in inc_dirs + search_for_ssl_incs_in:
  604. name = os.path.join(ssl_inc_dir, 'openssl', 'opensslv.h')
  605. if os.path.isfile(name):
  606. try:
  607. incfile = open(name, 'r')
  608. for line in incfile:
  609. m = openssl_ver_re.match(line)
  610. if m:
  611. openssl_ver = eval(m.group(1))
  612. break
  613. except IOError:
  614. pass
  615. # first version found is what we'll use (as the compiler should)
  616. if openssl_ver:
  617. break
  618. #print 'openssl_ver = 0x%08x' % openssl_ver
  619. if (ssl_incs is not None and
  620. ssl_libs is not None and
  621. openssl_ver >= 0x00907000):
  622. # The _hashlib module wraps optimized implementations
  623. # of hash functions from the OpenSSL library.
  624. exts.append( Extension('_hashlib', ['_hashopenssl.c'],
  625. include_dirs = ssl_incs,
  626. library_dirs = ssl_libs,
  627. libraries = ['ssl', 'crypto']) )
  628. # these aren't strictly missing since they are unneeded.
  629. #missing.extend(['_sha', '_md5'])
  630. else:
  631. # The _sha module implements the SHA1 hash algorithm.
  632. exts.append( Extension('_sha', ['shamodule.c']) )
  633. # The _md5 module implements the RSA Data Security, Inc. MD5
  634. # Message-Digest Algorithm, described in RFC 1321. The
  635. # necessary files md5.c and md5.h are included here.
  636. exts.append( Extension('_md5',
  637. sources = ['md5module.c', 'md5.c'],
  638. depends = ['md5.h']) )
  639. missing.append('_hashlib')
  640. if (openssl_ver < 0x00908000):
  641. # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
  642. exts.append( Extension('_sha256', ['sha256module.c']) )
  643. exts.append( Extension('_sha512', ['sha512module.c']) )
  644. # Modules that provide persistent dictionary-like semantics. You will
  645. # probably want to arrange for at least one of them to be available on
  646. # your machine, though none are defined by default because of library
  647. # dependencies. The Python module anydbm.py provides an
  648. # implementation independent wrapper for these; dumbdbm.py provides
  649. # similar functionality (but slower of course) implemented in Python.
  650. # Sleepycat^WOracle Berkeley DB interface.
  651. # http://www.oracle.com/database/berkeley-db/db/index.html
  652. #
  653. # This requires the Sleepycat^WOracle DB code. The supported versions
  654. # are set below. Visit the URL above to download
  655. # a release. Most open source OSes come with one or more
  656. # versions of BerkeleyDB already installed.
  657. max_db_ver = (4, 7)
  658. min_db_ver = (3, 3)
  659. db_setup_debug = False # verbose debug prints from this script?
  660. def allow_db_ver(db_ver):
  661. """Returns a boolean if the given BerkeleyDB version is acceptable.
  662. Args:
  663. db_ver: A tuple of the version to verify.
  664. """
  665. if not (min_db_ver <= db_ver <= max_db_ver):
  666. return False
  667. # Use this function to filter out known bad configurations.
  668. if (4, 6) == db_ver[:2]:
  669. # BerkeleyDB 4.6.x is not stable on many architectures.
  670. arch = platform_machine()
  671. if arch not in ('i386', 'i486', 'i586', 'i686',
  672. 'x86_64', 'ia64'):
  673. return False
  674. return True
  675. def gen_db_minor_ver_nums(major):
  676. if major == 4:
  677. for x in range(max_db_ver[1]+1):
  678. if allow_db_ver((4, x)):
  679. yield x
  680. elif major == 3:
  681. for x in (3,):
  682. if allow_db_ver((3, x)):
  683. yield x
  684. else:
  685. raise ValueError("unknown major BerkeleyDB version", major)
  686. # construct a list of paths to look for the header file in on
  687. # top of the normal inc_dirs.
  688. db_inc_paths = []
  689. if use_system_paths:
  690. db_inc_paths.extend([
  691. '/usr/include/db4',
  692. '/usr/local/include/db4',
  693. '/opt/sfw/include/db4',
  694. '/usr/include/db3',
  695. '/usr/local/include/db3',
  696. '/opt/sfw/include/db3',
  697. # Fink defaults (http://fink.sourceforge.net/)
  698. '/sw/include/db4',
  699. '/sw/include/db3',
  700. ])
  701. # 4.x minor number specific paths
  702. for x in gen_db_minor_ver_nums(4):
  703. db_inc_paths.append('/usr/include/db4%d' % x)
  704. db_inc_paths.append('/usr/include/db4.%d' % x)
  705. db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
  706. db_inc_paths.append('/usr/local/include/db4%d' % x)
  707. db_inc_paths.append('/pkg/db-4.%d/include' % x)
  708. db_inc_paths.append('/opt/db-4.%d/include' % x)
  709. # MacPorts default (http://www.macports.org/)
  710. db_inc_paths.append('/opt/local/include/db4%d' % x)
  711. # 3.x minor number specific paths
  712. for x in gen_db_minor_ver_nums(3):
  713. db_inc_paths.append('/usr/include/db3%d' % x)
  714. db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
  715. db_inc_paths.append('/usr/local/include/db3%d' % x)
  716. db_inc_paths.append('/pkg/db-3.%d/include' % x)
  717. db_inc_paths.append('/opt/db-3.%d/include' % x)
  718. # Add some common subdirectories for Sleepycat DB to the list,
  719. # based on the standard include directories. This way DB3/4 gets
  720. # picked up when it is installed in a non-standard prefix and
  721. # the user has added that prefix into inc_dirs.
  722. std_variants = []
  723. for dn in inc_dirs:
  724. std_variants.append(os.path.join(dn, 'db3'))
  725. std_variants.append(os.path.join(dn, 'db4'))
  726. for x in gen_db_minor_ver_nums(4):
  727. std_variants.append(os.path.join(dn, "db4%d"%x))
  728. std_variants.append(os.path.join(dn, "db4.%d"%x))
  729. for x in gen_db_minor_ver_nums(3):
  730. std_variants.append(os.path.join(dn, "db3%d"%x))
  731. std_variants.append(os.path.join(dn, "db3.%d"%x))
  732. db_inc_paths = std_variants + db_inc_paths
  733. db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
  734. db_ver_inc_map = {}
  735. class db_found(Exception): pass
  736. try:
  737. # See whether there is a Sleepycat header in the standard
  738. # search path.
  739. for d in inc_dirs + db_inc_paths:
  740. f = os.path.join(d, "db.h")
  741. if db_setup_debug: print "db: looking for db.h in", f
  742. if os.path.exists(f):
  743. f = open(f).read()
  744. m = re.search(r"#define\WDB_VERSION_MAJOR\W(\d+)", f)
  745. if m:
  746. db_major = int(m.group(1))
  747. m = re.search(r"#define\WDB_VERSION_MINOR\W(\d+)", f)
  748. db_minor = int(m.group(1))
  749. db_ver = (db_major, db_minor)
  750. # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
  751. if db_ver == (4, 6):
  752. m = re.search(r"#define\WDB_VERSION_PATCH\W(\d+)", f)
  753. db_patch = int(m.group(1))
  754. if db_patch < 21:
  755. print "db.h:", db_ver, "patch", db_patch,
  756. print "being ignored (4.6.x must be >= 4.6.21)"
  757. continue
  758. if ( (not db_ver_inc_map.has_key(db_ver)) and
  759. allow_db_ver(db_ver) ):
  760. # save the include directory with the db.h version
  761. # (first occurrence only)
  762. db_ver_inc_map[db_ver] = d
  763. if db_setup_debug:
  764. print "db.h: found", db_ver, "in", d
  765. else:
  766. # we already found a header for this library version
  767. if db_setup_debug: print "db.h: ignoring", d
  768. else:
  769. # ignore this header, it didn't contain a version number
  770. if db_setup_debug:
  771. print "db.h: no version number version in", d
  772. db_found_vers = db_ver_inc_map.keys()
  773. db_found_vers.sort()
  774. while db_found_vers:
  775. db_ver = db_found_vers.pop()
  776. db_incdir = db_ver_inc_map[db_ver]
  777. # check lib directories parallel to the location of the header
  778. db_dirs_to_check = combine_dirs_to_check([
  779. db_incdir.replace("include", 'lib64'),
  780. db_incdir.replace("include", 'lib'),
  781. ], lib_dirs)
  782. # Look for a version specific db-X.Y before an ambiguoius dbX
  783. # XXX should we -ever- look for a dbX name? Do any
  784. # systems really not name their library by version and
  785. # symlink to more general names?
  786. for dblib in (('db-%d.%d' % db_ver),
  787. ('db%d%d' % db_ver),
  788. ('db%d' % db_ver[0])):
  789. dblib_file = self.compiler.find_library_file(
  790. db_dirs_to_check, dblib )
  791. if dblib_file:
  792. dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
  793. raise db_found
  794. else:
  795. if db_setup_debug: print "db lib: ", dblib, "not found"
  796. except db_found:
  797. if db_setup_debug:
  798. print "bsddb using BerkeleyDB lib:", db_ver, dblib
  799. print "bsddb lib dir:", dblib_dir, " inc dir:", db_incdir
  800. db_incs = [db_incdir]
  801. dblibs = [dblib]
  802. # We add the runtime_library_dirs argument because the
  803. # BerkeleyDB lib we're linking against often isn't in the
  804. # system dynamic library search path. This is usually
  805. # correct and most trouble free, but may cause problems in
  806. # some unusual system configurations (e.g. the directory
  807. # is on an NFS server that goes away).
  808. exts.append(Extension('_bsddb', ['_bsddb.c'],
  809. depends = ['bsddb.h'],
  810. library_dirs=dblib_dir,
  811. runtime_library_dirs=dblib_dir,
  812. include_dirs=db_incs,
  813. libraries=dblibs))
  814. else:
  815. if db_setup_debug: print "db: no appropriate library found"
  816. db_incs = None
  817. dblibs = []
  818. dblib_dir = None
  819. missing.append('_bsddb')
  820. # The sqlite interface
  821. sqlite_setup_debug = False # verbose debug prints from this script?
  822. # We hunt for #define SQLITE_VERSION "n.n.n"
  823. # We need to find >= sqlite version 3.0.8
  824. sqlite_incdir = sqlite_libdir = None
  825. sqlite_inc_paths = []
  826. if use_system_paths:
  827. sqlite_inc_paths = [ '/usr/include',
  828. '/usr/include/sqlite',
  829. '/usr/include/sqlite3',
  830. '/usr/local/include',
  831. '/usr/local/include/sqlite',
  832. '/usr/local/include/sqlite3',
  833. ]
  834. if "SQLITE_INC" in os.environ:
  835. sqlite_inc_paths.append(os.environ["SQLITE_INC"])
  836. MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
  837. MIN_SQLITE_VERSION = ".".join([str(x)
  838. for x in MIN_SQLITE_VERSION_NUMBER])
  839. # Scan the default include directories before the SQLite specific
  840. # ones. This allows one to override the copy of sqlite on OSX,
  841. # where /usr/include contains an old version of sqlite.
  842. for d in inc_dirs + sqlite_inc_paths:
  843. f = os.path.join(d, "sqlite3.h")
  844. if sqlite_setup_debug: print "sqlite: looking for sqlite3.h in", f
  845. if os.path.exists(f):
  846. if sqlite_setup_debug: print "sqlite: found %s"%f
  847. incf = open(f).read()
  848. m = re.search(
  849. r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
  850. if m:
  851. sqlite_version = m.group(1)
  852. sqlite_version_tuple = tuple([int(x)
  853. for x in sqlite_version.split(".")])
  854. if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
  855. # we win!
  856. if sqlite_setup_debug:
  857. print "%s/sqlite3.h: version %s"%(d, sqlite_version)
  858. sqlite_incdir = d
  859. break
  860. else:
  861. if sqlite_setup_debug:
  862. print "%s: version %d is too old, need >= %s"%(d,
  863. sqlite_version, MIN_SQLITE_VERSION)
  864. elif sqlite_setup_debug:
  865. print "sqlite: %s had no SQLITE_VERSION"%(f,)
  866. if sqlite_incdir:
  867. sqlite_dirs_to_check = combine_dirs_to_check([
  868. os.path.join(sqlite_incdir, '..', 'lib64'),
  869. os.path.join(sqlite_incdir, '..', 'lib'),
  870. os.path.join(sqlite_incdir, '..', '..', 'lib64'),
  871. os.path.join(sqlite_incdir, '..', '..', 'lib'),
  872. os.environ.get("SQLITE_LIB", ""),
  873. ], lib_dirs)
  874. sqlite_libfile = self.compiler.find_library_file(
  875. sqlite_dirs_to_check, 'sqlite3')
  876. if sqlite_libfile:
  877. sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
  878. elif sqlite_setup_debug:
  879. print ("sqlite: no sqlite3 library in %s" %
  880. (sqlite_dirs_to_check))
  881. if sqlite_incdir and sqlite_libdir:
  882. sqlite_srcs = ['_sqlite/cache.c',
  883. '_sqlite/connection.c',
  884. '_sqlite/cursor.c',
  885. '_sqlite/microprotocols.c',
  886. '_sqlite/module.c',
  887. '_sqlite/prepare_protocol.c',
  888. '_sqlite/row.c',
  889. '_sqlite/statement.c',
  890. '_sqlite/util.c', ]
  891. sqlite_defines = []
  892. if sys.platform != "win32":
  893. sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
  894. else:
  895. sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
  896. if sys.platform == 'darwin':
  897. # In every directory on the search path search for a dynamic
  898. # library and then a static library, instead of first looking
  899. # for dynamic libraries on the entiry path.
  900. # This way a staticly linked custom sqlite gets picked up
  901. # before the dynamic library in /usr/lib.
  902. sqlite_extra_link_args = ('-Wl,-search_paths_first',)
  903. else:
  904. sqlite_extra_link_args = ()
  905. exts.append(Extension('_sqlite3', sqlite_srcs,
  906. define_macros=sqlite_defines,
  907. include_dirs=["Modules/_sqlite",
  908. sqlite_incdir],
  909. library_dirs=sqlite_libdir,
  910. runtime_library_dirs=sqlite_libdir,
  911. extra_link_args=sqlite_extra_link_args,
  912. libraries=["sqlite3",]))
  913. else:
  914. missing.append('_sqlite3')
  915. # Look for Berkeley db 1.85. Note that it is built as a different
  916. # module name so it can be included even when later versions are
  917. # available. A very restrictive search is performed to avoid
  918. # accidentally building this module with a later version of the
  919. # underlying db library. May BSD-ish Unixes incorporate db 1.85
  920. # symbols into libc and place the include file in /usr/include.
  921. #
  922. # If the better bsddb library can be built (db_incs is defined)
  923. # we do not build this one. Otherwise this build will pick up
  924. # the more recent berkeleydb's db.h file first in the include path
  925. # when attempting to compile and it will fail.
  926. f = "/usr/include/db.h"
  927. if use_system_paths and os.path.exists(f) and not db_incs:
  928. data = open(f).read()
  929. m = re.search(r"#s*define\s+HASHVERSION\s+2\s*", data)
  930. if m is not None:
  931. # bingo - old version used hash file format version 2
  932. ### XXX this should be fixed to not be platform-dependent
  933. ### but I don't have direct access to an osf1 platform and
  934. ### seemed to be muffing the search somehow
  935. libraries = platform == "osf1" and ['db'] or None
  936. if libraries is not None:
  937. exts.append(Extension('bsddb185', ['bsddbmodule.c'],
  938. libraries=libraries))
  939. else:
  940. exts.append(Extension('bsddb185', ['bsddbmodule.c']))
  941. else:
  942. missing.append('bsddb185')
  943. else:
  944. missing.append('bsddb185')
  945. # The standard Unix dbm module:
  946. if platform not in ['cygwin']:
  947. if find_file("ndbm.h", inc_dirs, []) is not None:
  948. # Some systems have -lndbm, others don't
  949. if self.compiler.find_library_file(lib_dirs, 'ndbm'):
  950. ndbm_libs = ['ndbm']
  951. else:
  952. ndbm_libs = []
  953. exts.append( Extension('dbm', ['dbmmodule.c'],
  954. define_macros=[('HAVE_NDBM_H',None)],
  955. libraries = ndbm_libs ) )
  956. elif self.compiler.find_library_file(lib_dirs, 'gdbm'):
  957. gdbm_libs = ['gdbm']
  958. if self.compiler.find_library_file(lib_dirs, 'gdbm_compat'):
  959. gdbm_libs.append('gdbm_compat')
  960. if find_file("gdbm/ndbm.h", inc_dirs, []) is not None:
  961. exts.append( Extension(
  962. 'dbm', ['dbmmodule.c'],
  963. define_macros=[('HAVE_GDBM_NDBM_H',None)],
  964. libraries = gdbm_libs ) )
  965. elif find_file("gdbm-ndbm.h", inc_dirs, []) is not None:
  966. exts.append( Extension(
  967. 'dbm', ['dbmmodule.c'],
  968. define_macros=[('HAVE_GDBM_DASH_NDBM_H',None)],
  969. libraries = gdbm_libs ) )
  970. else:
  971. missing.append('dbm')
  972. elif db_incs is not None:
  973. exts.append( Extension('dbm', ['dbmmodule.c'],
  974. library_dirs=dblib_dir,
  975. runtime_library_dirs=dblib_dir,
  976. include_dirs=db_incs,
  977. define_macros=[('HAVE_BERKDB_H',None),
  978. ('DB_DBM_HSEARCH',None)],
  979. libraries=dblibs))
  980. else:
  981. missing.append('dbm')
  982. # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
  983. if (self.compiler.find_library_file(lib_dirs, 'gdbm')):
  984. exts.append( Extension('gdbm', ['gdbmmodule.c'],
  985. libraries = ['gdbm'] ) )
  986. else:
  987. missing.append('gdbm')
  988. # Unix-only modules
  989. if platform not in ['mac', 'win32']:
  990. # Steen Lumholt's termios module
  991. exts.append( Extension('termios', ['termios.c']) )
  992. # Jeremy Hylton's rlimit interface
  993. if platform not in ['atheos']:
  994. exts.append( Extension('resource', ['resource.c']) )
  995. else:
  996. missing.append('resource')
  997. # Sun yellow pages. Some systems have the functions in libc.
  998. if platform not in ['cygwin', 'atheos', 'qnx6']:
  999. if (self.compiler.find_libra