/setup.py

http://unladen-swallow.googlecode.com/ · Python · 1966 lines · 1338 code · 220 blank · 408 comment · 359 complexity · 876f60a7a1997b71b1e8eb54e199eaf4 MD5 · raw 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_library_file(lib_dirs, 'nsl')):
  1000. libs = ['nsl']
  1001. else:
  1002. libs = []
  1003. exts.append( Extension('nis', ['nismodule.c'],
  1004. libraries = libs) )
  1005. else:
  1006. missing.append('nis')
  1007. else:
  1008. missing.extend(['nis', 'resource', 'termios'])
  1009. # Curses support, requiring the System V version of curses, often
  1010. # provided by the ncurses library.
  1011. panel_library = 'panel'
  1012. if (self.compiler.find_library_file(lib_dirs, 'ncursesw')):
  1013. curses_libs = ['ncursesw']
  1014. # Bug 1464056: If _curses.so links with ncursesw,
  1015. # _curses_panel.so must link with panelw.
  1016. panel_library = 'panelw'
  1017. exts.append( Extension('_curses', ['_cursesmodule.c'],
  1018. libraries = curses_libs) )
  1019. elif (self.compiler.find_library_file(lib_dirs, 'ncurses')):
  1020. curses_libs = ['ncurses']
  1021. exts.append( Extension('_curses', ['_cursesmodule.c'],
  1022. libraries = curses_libs) )
  1023. elif (self.compiler.find_library_file(lib_dirs, 'curses')
  1024. and platform != 'darwin'):
  1025. # OSX has an old Berkeley curses, not good enough for
  1026. # the _curses module.
  1027. if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
  1028. curses_libs = ['curses', 'terminfo']
  1029. elif (self.compiler.find_library_file(lib_dirs, 'termcap')):
  1030. curses_libs = ['curses', 'termcap']
  1031. else:
  1032. curses_libs = ['curses']
  1033. exts.append( Extension('_curses', ['_cursesmodule.c'],
  1034. libraries = curses_libs) )
  1035. else:
  1036. missing.append('_curses')
  1037. # If the curses module is enabled, check for the panel module
  1038. if (module_enabled(exts, '_curses') and
  1039. self.compiler.find_library_file(lib_dirs, panel_library)):
  1040. exts.append( Extension('_curses_panel', ['_curses_panel.c'],
  1041. libraries = [panel_library] + curses_libs) )
  1042. else:
  1043. missing.append('_curses_panel')
  1044. # Andrew Kuchling's zlib module. Note that some versions of zlib
  1045. # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
  1046. # http://www.cert.org/advisories/CA-2002-07.html
  1047. #
  1048. # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
  1049. # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
  1050. # now, we still accept 1.1.3, because we think it's difficult to
  1051. # exploit this in Python, and we'd rather make it RedHat's problem
  1052. # than our problem <wink>.
  1053. #
  1054. # You can upgrade zlib to version 1.1.4 yourself by going to
  1055. # http://www.gzip.org/zlib/
  1056. zlib_inc = find_file('zlib.h', [], inc_dirs)
  1057. have_zlib = False
  1058. if zlib_inc is not None:
  1059. zlib_h = zlib_inc[0] + '/zlib.h'
  1060. version = '"0.0.0"'
  1061. version_req = '"1.1.3"'
  1062. fp = open(zlib_h)
  1063. while 1:
  1064. line = fp.readline()
  1065. if not line:
  1066. break
  1067. if line.startswith('#define ZLIB_VERSION'):
  1068. version = line.split()[2]
  1069. break
  1070. if version >= version_req:
  1071. if (self.compiler.find_library_file(lib_dirs, 'z')):
  1072. if sys.platform == "darwin":
  1073. zlib_extra_link_args = ('-Wl,-search_paths_first',)
  1074. else:
  1075. zlib_extra_link_args = ()
  1076. exts.append( Extension('zlib', ['zlibmodule.c'],
  1077. libraries = ['z'],
  1078. extra_link_args = zlib_extra_link_args))
  1079. have_zlib = True
  1080. else:
  1081. missing.append('zlib')
  1082. else:
  1083. missing.append('zlib')
  1084. else:
  1085. missing.append('zlib')
  1086. # Helper module for various ascii-encoders. Uses zlib for an optimized
  1087. # crc32 if we have it. Otherwise binascii uses its own.
  1088. if have_zlib:
  1089. extra_compile_args = ['-DUSE_ZLIB_CRC32']
  1090. libraries = ['z']
  1091. extra_link_args = zlib_extra_link_args
  1092. else:
  1093. extra_compile_args = []
  1094. libraries = []
  1095. extra_link_args = []
  1096. exts.append( Extension('binascii', ['binascii.c'],
  1097. extra_compile_args = extra_compile_args,
  1098. libraries = libraries,
  1099. extra_link_args = extra_link_args) )
  1100. # Gustavo Niemeyer's bz2 module.
  1101. if (self.compiler.find_library_file(lib_dirs, 'bz2')):
  1102. if sys.platform == "darwin":
  1103. bz2_extra_link_args = ('-Wl,-search_paths_first',)
  1104. else:
  1105. bz2_extra_link_args = ()
  1106. exts.append( Extension('bz2', ['bz2module.c'],
  1107. libraries = ['bz2'],
  1108. extra_link_args = bz2_extra_link_args) )
  1109. else:
  1110. missing.append('bz2')
  1111. # Interface to the Expat XML parser
  1112. #
  1113. # Expat was written by James Clark and is now maintained by a
  1114. # group of developers on SourceForge; see www.libexpat.org for
  1115. # more information. The pyexpat module was written by Paul
  1116. # Prescod after a prototype by Jack Jansen. The Expat source
  1117. # is included in Modules/expat/. Usage of a system
  1118. # shared libexpat.so/expat.dll is not advised.
  1119. #
  1120. # More information on Expat can be found at www.libexpat.org.
  1121. #
  1122. expatinc = os.path.join(os.getcwd(), srcdir, 'Modules', 'expat')
  1123. define_macros = [
  1124. ('HAVE_EXPAT_CONFIG_H', '1'),
  1125. ]
  1126. exts.append(Extension('pyexpat',
  1127. define_macros = define_macros,
  1128. include_dirs = [expatinc],
  1129. sources = ['pyexpat.c',
  1130. 'expat/xmlparse.c',
  1131. 'expat/xmlrole.c',
  1132. 'expat/xmltok.c',
  1133. ],
  1134. ))
  1135. # Fredrik Lundh's cElementTree module. Note that this also
  1136. # uses expat (via the CAPI hook in pyexpat).
  1137. if os.path.isfile(os.path.join(srcdir, 'Modules', '_elementtree.c')):
  1138. define_macros.append(('USE_PYEXPAT_CAPI', None))
  1139. exts.append(Extension('_elementtree',
  1140. define_macros = define_macros,
  1141. include_dirs = [expatinc],
  1142. sources = ['_elementtree.c'],
  1143. ))
  1144. else:
  1145. missing.append('_elementtree')
  1146. # Hye-Shik Chang's CJKCodecs modules.
  1147. if have_unicode:
  1148. exts.append(Extension('_multibytecodec',
  1149. ['cjkcodecs/multibytecodec.c']))
  1150. for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
  1151. exts.append(Extension('_codecs_%s' % loc,
  1152. ['cjkcodecs/_codecs_%s.c' % loc]))
  1153. else:
  1154. missing.append('_multibytecodec')
  1155. for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
  1156. missing.append('_codecs_%s' % loc)
  1157. # Dynamic loading module
  1158. if sys.maxint == 0x7fffffff:
  1159. # This requires sizeof(int) == sizeof(long) == sizeof(char*)
  1160. dl_inc = find_file('dlfcn.h', [], inc_dirs)
  1161. if (dl_inc is not None) and (platform not in ['atheos']):
  1162. exts.append( Extension('dl', ['dlmodule.c']) )
  1163. else:
  1164. missing.append('dl')
  1165. else:
  1166. missing.append('dl')
  1167. # Thomas Heller's _ctypes module
  1168. self.detect_ctypes(inc_dirs, lib_dirs)
  1169. # Richard Oudkerk's multiprocessing module
  1170. if platform == 'win32': # Windows
  1171. macros = dict()
  1172. libraries = ['ws2_32']
  1173. elif platform == 'darwin': # Mac OSX
  1174. macros = dict(
  1175. HAVE_SEM_OPEN=1,
  1176. HAVE_SEM_TIMEDWAIT=0,
  1177. HAVE_FD_TRANSFER=1,
  1178. HAVE_BROKEN_SEM_GETVALUE=1
  1179. )
  1180. libraries = []
  1181. elif platform == 'cygwin': # Cygwin
  1182. macros = dict(
  1183. HAVE_SEM_OPEN=1,
  1184. HAVE_SEM_TIMEDWAIT=1,
  1185. HAVE_FD_TRANSFER=0,
  1186. HAVE_BROKEN_SEM_UNLINK=1
  1187. )
  1188. libraries = []
  1189. elif platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
  1190. # FreeBSD's P1003.1b semaphore support is very experimental
  1191. # and has many known problems. (as of June 2008)
  1192. macros = dict( # FreeBSD
  1193. HAVE_SEM_OPEN=0,
  1194. HAVE_SEM_TIMEDWAIT=0,
  1195. HAVE_FD_TRANSFER=1,
  1196. )
  1197. libraries = []
  1198. elif platform.startswith('openbsd'):
  1199. macros = dict( # OpenBSD
  1200. HAVE_SEM_OPEN=0, # Not implemented
  1201. HAVE_SEM_TIMEDWAIT=0,
  1202. HAVE_FD_TRANSFER=1,
  1203. )
  1204. libraries = []
  1205. elif platform.startswith('netbsd'):
  1206. macros = dict( # at least NetBSD 5
  1207. HAVE_SEM_OPEN=1,
  1208. HAVE_SEM_TIMEDWAIT=0,
  1209. HAVE_FD_TRANSFER=1,
  1210. HAVE_BROKEN_SEM_GETVALUE=1
  1211. )
  1212. libraries = []
  1213. else: # Linux and other unices
  1214. macros = dict(
  1215. HAVE_SEM_OPEN=1,
  1216. HAVE_SEM_TIMEDWAIT=1,
  1217. HAVE_FD_TRANSFER=1
  1218. )
  1219. libraries = ['rt']
  1220. if platform == 'win32':
  1221. multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
  1222. '_multiprocessing/semaphore.c',
  1223. '_multiprocessing/pipe_connection.c',
  1224. '_multiprocessing/socket_connection.c',
  1225. '_multiprocessing/win32_functions.c'
  1226. ]
  1227. else:
  1228. multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
  1229. '_multiprocessing/socket_connection.c'
  1230. ]
  1231. if macros.get('HAVE_SEM_OPEN', False):
  1232. multiprocessing_srcs.append('_multiprocessing/semaphore.c')
  1233. exts.append ( Extension('_multiprocessing', multiprocessing_srcs,
  1234. define_macros=macros.items(),
  1235. include_dirs=["Modules/_multiprocessing"]))
  1236. # End multiprocessing
  1237. # Platform-specific libraries
  1238. if platform == 'linux2':
  1239. # Linux-specific modules
  1240. exts.append( Extension('linuxaudiodev', ['linuxaudiodev.c']) )
  1241. else:
  1242. missing.append('linuxaudiodev')
  1243. if platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
  1244. 'freebsd7', 'freebsd8'):
  1245. exts.append( Extension('ossaudiodev', ['ossaudiodev.c']) )
  1246. else:
  1247. missing.append('ossaudiodev')
  1248. if platform == 'sunos5':
  1249. # SunOS specific modules
  1250. exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
  1251. else:
  1252. missing.append('sunaudiodev')
  1253. if platform == 'darwin':
  1254. # _scproxy
  1255. exts.append(Extension("_scproxy", [os.path.join(srcdir, "Mac/Modules/_scproxy.c")],
  1256. extra_link_args= [
  1257. '-framework', 'SystemConfiguration',
  1258. '-framework', 'CoreFoundation'
  1259. ]))
  1260. if platform == 'darwin' and ("--disable-toolbox-glue" not in
  1261. sysconfig.get_config_var("CONFIG_ARGS")):
  1262. if os.uname()[2] > '8.':
  1263. # We're on Mac OS X 10.4 or later, the compiler should
  1264. # support '-Wno-deprecated-declarations'. This will
  1265. # surpress deprecation warnings for the Carbon extensions,
  1266. # these extensions wrap the Carbon APIs and even those
  1267. # parts that are deprecated.
  1268. carbon_extra_compile_args = ['-Wno-deprecated-declarations']
  1269. else:
  1270. carbon_extra_compile_args = []
  1271. # Mac OS X specific modules.
  1272. def macSrcExists(name1, name2=''):
  1273. if not name1:
  1274. return None
  1275. names = (name1,)
  1276. if name2:
  1277. names = (name1, name2)
  1278. path = os.path.join(srcdir, 'Mac', 'Modules', *names)
  1279. return os.path.exists(path)
  1280. def addMacExtension(name, kwds, extra_srcs=[]):
  1281. dirname = ''
  1282. if name[0] == '_':
  1283. dirname = name[1:].lower()
  1284. cname = name + '.c'
  1285. cmodulename = name + 'module.c'
  1286. # Check for NNN.c, NNNmodule.c, _nnn/NNN.c, _nnn/NNNmodule.c
  1287. if macSrcExists(cname):
  1288. srcs = [cname]
  1289. elif macSrcExists(cmodulename):
  1290. srcs = [cmodulename]
  1291. elif macSrcExists(dirname, cname):
  1292. # XXX(nnorwitz): If all the names ended with module, we
  1293. # wouldn't need this condition. ibcarbon is the only one.
  1294. srcs = [os.path.join(dirname, cname)]
  1295. elif macSrcExists(dirname, cmodulename):
  1296. srcs = [os.path.join(dirname, cmodulename)]
  1297. else:
  1298. raise RuntimeError("%s not found" % name)
  1299. # Here's the whole point: add the extension with sources
  1300. exts.append(Extension(name, srcs + extra_srcs, **kwds))
  1301. # Core Foundation
  1302. core_kwds = {'extra_compile_args': carbon_extra_compile_args,
  1303. 'extra_link_args': ['-framework', 'CoreFoundation'],
  1304. }
  1305. addMacExtension('_CF', core_kwds, ['cf/pycfbridge.c'])
  1306. addMacExtension('autoGIL', core_kwds)
  1307. # Carbon
  1308. carbon_kwds = {'extra_compile_args': carbon_extra_compile_args,
  1309. 'extra_link_args': ['-framework', 'Carbon'],
  1310. }
  1311. CARBON_EXTS = ['ColorPicker', 'gestalt', 'MacOS', 'Nav',
  1312. 'OSATerminology', 'icglue',
  1313. # All these are in subdirs
  1314. '_AE', '_AH', '_App', '_CarbonEvt', '_Cm', '_Ctl',
  1315. '_Dlg', '_Drag', '_Evt', '_File', '_Folder', '_Fm',
  1316. '_Help', '_Icn', '_IBCarbon', '_List',
  1317. '_Menu', '_Mlte', '_OSA', '_Res', '_Qd', '_Qdoffs',
  1318. '_Scrap', '_Snd', '_TE',
  1319. ]
  1320. for name in CARBON_EXTS:
  1321. addMacExtension(name, carbon_kwds)
  1322. # Workaround for a bug in the version of gcc shipped with Xcode 3.
  1323. # The _Win extension should build just like the other Carbon extensions, but
  1324. # this actually results in a hard crash of the linker.
  1325. #
  1326. if '-arch ppc64' in cflags and '-arch ppc' in cflags:
  1327. win_kwds = {'extra_compile_args': carbon_extra_compile_args + ['-arch', 'i386', '-arch', 'ppc'],
  1328. 'extra_link_args': ['-framework', 'Carbon', '-arch', 'i386', '-arch', 'ppc'],
  1329. }
  1330. addMacExtension('_Win', win_kwds)
  1331. else:
  1332. addMacExtension('_Win', carbon_kwds)
  1333. # Application Services & QuickTime
  1334. app_kwds = {'extra_compile_args': carbon_extra_compile_args,
  1335. 'extra_link_args': ['-framework','ApplicationServices'],
  1336. }
  1337. addMacExtension('_Launch', app_kwds)
  1338. addMacExtension('_CG', app_kwds)
  1339. exts.append( Extension('_Qt', ['qt/_Qtmodule.c'],
  1340. extra_compile_args=carbon_extra_compile_args,
  1341. extra_link_args=['-framework', 'QuickTime',
  1342. '-framework', 'Carbon']) )
  1343. self.extensions.extend(exts)
  1344. # Call the method for detecting whether _tkinter can be compiled
  1345. self.detect_tkinter(inc_dirs, lib_dirs)
  1346. if '_tkinter' not in [e.name for e in self.extensions]:
  1347. missing.append('_tkinter')
  1348. return missing
  1349. def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
  1350. # The _tkinter module, using frameworks. Since frameworks are quite
  1351. # different the UNIX search logic is not sharable.
  1352. from os.path import join, exists
  1353. framework_dirs = [
  1354. '/Library/Frameworks',
  1355. '/System/Library/Frameworks/',
  1356. join(os.getenv('HOME'), '/Library/Frameworks')
  1357. ]
  1358. # Find the directory that contains the Tcl.framework and Tk.framework
  1359. # bundles.
  1360. # XXX distutils should support -F!
  1361. for F in framework_dirs:
  1362. # both Tcl.framework and Tk.framework should be present
  1363. for fw in 'Tcl', 'Tk':
  1364. if not exists(join(F, fw + '.framework')):
  1365. break
  1366. else:
  1367. # ok, F is now directory with both frameworks. Continure
  1368. # building
  1369. break
  1370. else:
  1371. # Tk and Tcl frameworks not found. Normal "unix" tkinter search
  1372. # will now resume.
  1373. return 0
  1374. # For 8.4a2, we must add -I options that point inside the Tcl and Tk
  1375. # frameworks. In later release we should hopefully be able to pass
  1376. # the -F option to gcc, which specifies a framework lookup path.
  1377. #
  1378. include_dirs = [
  1379. join(F, fw + '.framework', H)
  1380. for fw in 'Tcl', 'Tk'
  1381. for H in 'Headers', 'Versions/Current/PrivateHeaders'
  1382. ]
  1383. # For 8.4a2, the X11 headers are not included. Rather than include a
  1384. # complicated search, this is a hard-coded path. It could bail out
  1385. # if X11 libs are not found...
  1386. include_dirs.append('/usr/X11R6/include')
  1387. frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
  1388. # All existing framework builds of Tcl/Tk don't support 64-bit
  1389. # architectures.
  1390. cflags = sysconfig.get_config_vars('CFLAGS')[0]
  1391. archs = re.findall('-arch\s+(\w+)', cflags)
  1392. fp = os.popen("file %s/Tk.framework/Tk | grep 'for architecture'"%(F,))
  1393. detected_archs = []
  1394. for ln in fp:
  1395. a = ln.split()[-1]
  1396. if a in archs:
  1397. detected_archs.append(ln.split()[-1])
  1398. fp.close()
  1399. for a in detected_archs:
  1400. frameworks.append('-arch')
  1401. frameworks.append(a)
  1402. ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
  1403. define_macros=[('WITH_APPINIT', 1)],
  1404. include_dirs = include_dirs,
  1405. libraries = [],
  1406. extra_compile_args = frameworks[2:],
  1407. extra_link_args = frameworks,
  1408. )
  1409. self.extensions.append(ext)
  1410. return 1
  1411. def detect_tkinter(self, inc_dirs, lib_dirs):
  1412. # The _tkinter module.
  1413. # Rather than complicate the code below, detecting and building
  1414. # AquaTk is a separate method. Only one Tkinter will be built on
  1415. # Darwin - either AquaTk, if it is found, or X11 based Tk.
  1416. platform = self.get_platform()
  1417. if (platform == 'darwin' and
  1418. self.detect_tkinter_darwin(inc_dirs, lib_dirs)):
  1419. return
  1420. # Assume we haven't found any of the libraries or include files
  1421. # The versions with dots are used on Unix, and the versions without
  1422. # dots on Windows, for detection by cygwin.
  1423. tcllib = tklib = tcl_includes = tk_includes = None
  1424. for version in ['8.5', '85', '8.4', '84', '8.3', '83', '8.2',
  1425. '82', '8.1', '81', '8.0', '80']:
  1426. tklib = self.compiler.find_library_file(lib_dirs, 'tk' + version)
  1427. tcllib = self.compiler.find_library_file(lib_dirs, 'tcl' + version)
  1428. if tklib and tcllib:
  1429. # Exit the loop when we've found the Tcl/Tk libraries
  1430. break
  1431. # Now check for the header files
  1432. if tklib and tcllib:
  1433. # Check for the include files on Debian and {Free,Open}BSD, where
  1434. # they're put in /usr/include/{tcl,tk}X.Y
  1435. dotversion = version
  1436. if '.' not in dotversion and "bsd" in sys.platform.lower():
  1437. # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
  1438. # but the include subdirs are named like .../include/tcl8.3.
  1439. dotversion = dotversion[:-1] + '.' + dotversion[-1]
  1440. tcl_include_sub = []
  1441. tk_include_sub = []
  1442. for dir in inc_dirs:
  1443. tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
  1444. tk_include_sub += [dir + os.sep + "tk" + dotversion]
  1445. tk_include_sub += tcl_include_sub
  1446. tcl_includes = find_file('tcl.h', inc_dirs, tcl_include_sub)
  1447. tk_includes = find_file('tk.h', inc_dirs, tk_include_sub)
  1448. if (tcllib is None or tklib is None or
  1449. tcl_includes is None or tk_includes is None):
  1450. self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
  1451. return
  1452. # OK... everything seems to be present for Tcl/Tk.
  1453. include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
  1454. for dir in tcl_includes + tk_includes:
  1455. if dir not in include_dirs:
  1456. include_dirs.append(dir)
  1457. # Check for various platform-specific directories
  1458. if platform == 'sunos5':
  1459. include_dirs.append('/usr/openwin/include')
  1460. added_lib_dirs.append('/usr/openwin/lib')
  1461. elif os.path.exists('/usr/X11R6/include'):
  1462. include_dirs.append('/usr/X11R6/include')
  1463. added_lib_dirs.append('/usr/X11R6/lib64')
  1464. added_lib_dirs.append('/usr/X11R6/lib')
  1465. elif os.path.exists('/usr/X11R5/include'):
  1466. include_dirs.append('/usr/X11R5/include')
  1467. added_lib_dirs.append('/usr/X11R5/lib')
  1468. else:
  1469. # Assume default location for X11
  1470. include_dirs.append('/usr/X11/include')
  1471. added_lib_dirs.append('/usr/X11/lib')
  1472. # If Cygwin, then verify that X is installed before proceeding
  1473. if platform == 'cygwin':
  1474. x11_inc = find_file('X11/Xlib.h', [], include_dirs)
  1475. if x11_inc is None:
  1476. return
  1477. # Check for BLT extension
  1478. if self.compiler.find_library_file(lib_dirs + added_lib_dirs,
  1479. 'BLT8.0'):
  1480. defs.append( ('WITH_BLT', 1) )
  1481. libs.append('BLT8.0')
  1482. elif self.compiler.find_library_file(lib_dirs + added_lib_dirs,
  1483. 'BLT'):
  1484. defs.append( ('WITH_BLT', 1) )
  1485. libs.append('BLT')
  1486. # Add the Tcl/Tk libraries
  1487. libs.append('tk'+ version)
  1488. libs.append('tcl'+ version)
  1489. if platform in ['aix3', 'aix4']:
  1490. libs.append('ld')
  1491. # Finally, link with the X11 libraries (not appropriate on cygwin)
  1492. if platform != "cygwin":
  1493. libs.append('X11')
  1494. ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
  1495. define_macros=[('WITH_APPINIT', 1)] + defs,
  1496. include_dirs = include_dirs,
  1497. libraries = libs,
  1498. library_dirs = added_lib_dirs,
  1499. )
  1500. self.extensions.append(ext)
  1501. ## # Uncomment these lines if you want to play with xxmodule.c
  1502. ## ext = Extension('xx', ['xxmodule.c'])
  1503. ## self.extensions.append(ext)
  1504. # XXX handle these, but how to detect?
  1505. # *** Uncomment and edit for PIL (TkImaging) extension only:
  1506. # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
  1507. # *** Uncomment and edit for TOGL extension only:
  1508. # -DWITH_TOGL togl.c \
  1509. # *** Uncomment these for TOGL extension only:
  1510. # -lGL -lGLU -lXext -lXmu \
  1511. def configure_ctypes_darwin(self, ext):
  1512. # Darwin (OS X) uses preconfigured files, in
  1513. # the Modules/_ctypes/libffi_osx directory.
  1514. (srcdir,) = sysconfig.get_config_vars('srcdir')
  1515. ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
  1516. '_ctypes', 'libffi_osx'))
  1517. sources = [os.path.join(ffi_srcdir, p)
  1518. for p in ['ffi.c',
  1519. 'x86/darwin64.S',
  1520. 'x86/x86-darwin.S',
  1521. 'x86/x86-ffi_darwin.c',
  1522. 'x86/x86-ffi64.c',
  1523. 'powerpc/ppc-darwin.S',
  1524. 'powerpc/ppc-darwin_closure.S',
  1525. 'powerpc/ppc-ffi_darwin.c',
  1526. 'powerpc/ppc64-darwin_closure.S',
  1527. ]]
  1528. # Add .S (preprocessed assembly) to C compiler source extensions.
  1529. self.compiler.src_extensions.append('.S')
  1530. include_dirs = [os.path.join(ffi_srcdir, 'include'),
  1531. os.path.join(ffi_srcdir, 'powerpc')]
  1532. ext.include_dirs.extend(include_dirs)
  1533. ext.sources.extend(sources)
  1534. return True
  1535. def configure_ctypes(self, ext):
  1536. if not self.use_system_libffi:
  1537. if sys.platform == 'darwin':
  1538. return self.configure_ctypes_darwin(ext)
  1539. (srcdir,) = sysconfig.get_config_vars('srcdir')
  1540. ffi_builddir = os.path.join(self.build_temp, 'libffi')
  1541. ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
  1542. '_ctypes', 'libffi'))
  1543. ffi_configfile = os.path.join(ffi_builddir, 'fficonfig.py')
  1544. from distutils.dep_util import newer_group
  1545. config_sources = [os.path.join(ffi_srcdir, fname)
  1546. for fname in os.listdir(ffi_srcdir)
  1547. if os.path.isfile(os.path.join(ffi_srcdir, fname))]
  1548. if self.force or newer_group(config_sources,
  1549. ffi_configfile):
  1550. from distutils.dir_util import mkpath
  1551. mkpath(ffi_builddir)
  1552. config_args = []
  1553. # Pass empty CFLAGS because we'll just append the resulting
  1554. # CFLAGS to Python's; -g or -O2 is to be avoided.
  1555. cmd = "cd %s && env CFLAGS='' '%s/configure' %s" \
  1556. % (ffi_builddir, ffi_srcdir, " ".join(config_args))
  1557. res = os.system(cmd)
  1558. if res or not os.path.exists(ffi_configfile):
  1559. print "Failed to configure _ctypes module"
  1560. return False
  1561. fficonfig = {}
  1562. execfile(ffi_configfile, globals(), fficonfig)
  1563. ffi_srcdir = os.path.join(fficonfig['ffi_srcdir'], 'src')
  1564. # Add .S (preprocessed assembly) to C compiler source extensions.
  1565. self.compiler.src_extensions.append('.S')
  1566. include_dirs = [os.path.join(ffi_builddir, 'include'),
  1567. ffi_builddir, ffi_srcdir]
  1568. extra_compile_args = fficonfig['ffi_cflags'].split()
  1569. ext.sources.extend(fficonfig['ffi_sources'])
  1570. ext.include_dirs.extend(include_dirs)
  1571. ext.extra_compile_args.extend(extra_compile_args)
  1572. return True
  1573. def detect_ctypes(self, inc_dirs, lib_dirs):
  1574. self.use_system_libffi = False
  1575. include_dirs = []
  1576. extra_compile_args = []
  1577. extra_link_args = []
  1578. sources = ['_ctypes/_ctypes.c',
  1579. '_ctypes/callbacks.c',
  1580. '_ctypes/callproc.c',
  1581. '_ctypes/stgdict.c',
  1582. '_ctypes/cfield.c',
  1583. '_ctypes/malloc_closure.c']
  1584. depends = ['_ctypes/ctypes.h']
  1585. if sys.platform == 'darwin':
  1586. sources.append('_ctypes/darwin/dlfcn_simple.c')
  1587. extra_compile_args.append('-DMACOSX')
  1588. include_dirs.append('_ctypes/darwin')
  1589. # XXX Is this still needed?
  1590. ## extra_link_args.extend(['-read_only_relocs', 'warning'])
  1591. elif sys.platform == 'sunos5':
  1592. # XXX This shouldn't be necessary; it appears that some
  1593. # of the assembler code is non-PIC (i.e. it has relocations
  1594. # when it shouldn't. The proper fix would be to rewrite
  1595. # the assembler code to be PIC.
  1596. # This only works with GCC; the Sun compiler likely refuses
  1597. # this option. If you want to compile ctypes with the Sun
  1598. # compiler, please research a proper solution, instead of
  1599. # finding some -z option for the Sun compiler.
  1600. extra_link_args.append('-mimpure-text')
  1601. elif sys.platform.startswith('hp-ux'):
  1602. extra_link_args.append('-fPIC')
  1603. ext = Extension('_ctypes',
  1604. include_dirs=include_dirs,
  1605. extra_compile_args=extra_compile_args,
  1606. extra_link_args=extra_link_args,
  1607. libraries=[],
  1608. sources=sources,
  1609. depends=depends)
  1610. ext_test = Extension('_ctypes_test',
  1611. sources=['_ctypes/_ctypes_test.c'])
  1612. self.extensions.extend([ext, ext_test])
  1613. if not '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS"):
  1614. return
  1615. if sys.platform == 'darwin' and use_system_paths:
  1616. # OS X 10.5 comes with libffi.dylib; the include files are
  1617. # in /usr/include/ffi
  1618. inc_dirs.append('/usr/include/ffi')
  1619. ffi_inc = find_file('ffi.h', [], inc_dirs)
  1620. if ffi_inc is not None:
  1621. ffi_h = ffi_inc[0] + '/ffi.h'
  1622. fp = open(ffi_h)
  1623. while 1:
  1624. line = fp.readline()
  1625. if not line:
  1626. ffi_inc = None
  1627. break
  1628. if line.startswith('#define LIBFFI_H'):
  1629. break
  1630. ffi_lib = None
  1631. if ffi_inc is not None:
  1632. for lib_name in ('ffi_convenience', 'ffi_pic', 'ffi'):
  1633. if (self.compiler.find_library_file(lib_dirs, lib_name)):
  1634. ffi_lib = lib_name
  1635. break
  1636. if ffi_inc and ffi_lib:
  1637. ext.include_dirs.extend(ffi_inc)
  1638. ext.libraries.append(ffi_lib)
  1639. self.use_system_libffi = True
  1640. class PyBuildInstall(install):
  1641. # Suppress the warning about installation into the lib_dynload
  1642. # directory, which is not in sys.path when running Python during
  1643. # installation:
  1644. def initialize_options (self):
  1645. install.initialize_options(self)
  1646. self.warn_dir=0
  1647. class PyBuildInstallLib(install_lib):
  1648. # Do exactly what install_lib does but make sure correct access modes get
  1649. # set on installed directories and files. All installed files with get
  1650. # mode 644 unless they are a shared library in which case they will get
  1651. # mode 755. All installed directories will get mode 755.
  1652. so_ext = sysconfig.get_config_var("SO")
  1653. def install(self):
  1654. outfiles = install_lib.install(self)
  1655. self.set_file_modes(outfiles, 0644, 0755)
  1656. self.set_dir_modes(self.install_dir, 0755)
  1657. return outfiles
  1658. def set_file_modes(self, files, defaultMode, sharedLibMode):
  1659. if not self.is_chmod_supported(): return
  1660. if not files: return
  1661. for filename in files:
  1662. if os.path.islink(filename): continue
  1663. mode = defaultMode
  1664. if filename.endswith(self.so_ext): mode = sharedLibMode
  1665. log.info("changing mode of %s to %o", filename, mode)
  1666. if not self.dry_run: os.chmod(filename, mode)
  1667. def set_dir_modes(self, dirname, mode):
  1668. if not self.is_chmod_supported(): return
  1669. os.path.walk(dirname, self.set_dir_modes_visitor, mode)
  1670. def set_dir_modes_visitor(self, mode, dirname, names):
  1671. if os.path.islink(dirname): return
  1672. log.info("changing mode of %s to %o", dirname, mode)
  1673. if not self.dry_run: os.chmod(dirname, mode)
  1674. def is_chmod_supported(self):
  1675. return hasattr(os, 'chmod')
  1676. SUMMARY = """
  1677. Python is an interpreted, interactive, object-oriented programming
  1678. language. It is often compared to Tcl, Perl, Scheme or Java.
  1679. Python combines remarkable power with very clear syntax. It has
  1680. modules, classes, exceptions, very high level dynamic data types, and
  1681. dynamic typing. There are interfaces to many system calls and
  1682. libraries, as well as to various windowing systems (X11, Motif, Tk,
  1683. Mac, MFC). New built-in modules are easily written in C or C++. Python
  1684. is also usable as an extension language for applications that need a
  1685. programmable interface.
  1686. The Python implementation is portable: it runs on many brands of UNIX,
  1687. on Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't
  1688. listed here, it may still be supported, if there's a C compiler for
  1689. it. Ask around on comp.lang.python -- or just try compiling Python
  1690. yourself.
  1691. """
  1692. CLASSIFIERS = """
  1693. Development Status :: 6 - Mature
  1694. License :: OSI Approved :: Python Software Foundation License
  1695. Natural Language :: English
  1696. Programming Language :: C
  1697. Programming Language :: Python
  1698. Topic :: Software Development
  1699. """
  1700. def main():
  1701. # turn off warnings when deprecated modules are imported
  1702. import warnings
  1703. warnings.filterwarnings("ignore",category=DeprecationWarning)
  1704. record_build_dir()
  1705. setup(# PyPI Metadata (PEP 301)
  1706. name = "Python",
  1707. version = sys.version.split()[0],
  1708. url = "http://www.python.org/%s" % sys.version[:3],
  1709. maintainer = "Guido van Rossum and the Python community",
  1710. maintainer_email = "python-dev@python.org",
  1711. description = "A high-level object-oriented programming language",
  1712. long_description = SUMMARY.strip(),
  1713. license = "PSF license",
  1714. classifiers = filter(None, CLASSIFIERS.split("\n")),
  1715. platforms = ["Many"],
  1716. # Build info
  1717. cmdclass = {'build_ext':PyBuildExt, 'install':PyBuildInstall,
  1718. 'install_lib':PyBuildInstallLib},
  1719. # The struct module is defined here, because build_ext won't be
  1720. # called unless there's at least one extension module defined.
  1721. ext_modules=[Extension('_struct', ['_struct.c'])],
  1722. # Scripts to install
  1723. scripts = ['Tools/scripts/pydoc', 'Tools/scripts/idle',
  1724. 'Tools/scripts/2to3',
  1725. 'Lib/smtpd.py']
  1726. )
  1727. # --install-platlib
  1728. if __name__ == '__main__':
  1729. main()