You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2089 lines
89 KiB

16 years ago
16 years ago
  1. # Autodetecting setup.py script for building the Python extensions
  2. #
  3. __version__ = "$Revision$"
  4. import sys, os, imp, re, optparse
  5. from glob import glob
  6. from platform import machine as platform_machine
  7. import sysconfig
  8. from distutils import log
  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.spawn import find_executable
  16. # Were we compiled --with-pydebug or with #define Py_DEBUG?
  17. COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
  18. # This global variable is used to hold the list of modules to be disabled.
  19. disabled_module_list = []
  20. def add_dir_to_list(dirlist, dir):
  21. """Add the directory 'dir' to the list 'dirlist' (at the front) if
  22. 1) 'dir' is not already in 'dirlist'
  23. 2) 'dir' actually exists, and is a directory."""
  24. if dir is not None and os.path.isdir(dir) and dir not in dirlist:
  25. dirlist.insert(0, dir)
  26. def macosx_sdk_root():
  27. """
  28. Return the directory of the current OSX SDK,
  29. or '/' if no SDK was specified.
  30. """
  31. cflags = sysconfig.get_config_var('CFLAGS')
  32. m = re.search(r'-isysroot\s+(\S+)', cflags)
  33. if m is None:
  34. sysroot = '/'
  35. else:
  36. sysroot = m.group(1)
  37. return sysroot
  38. def is_macosx_sdk_path(path):
  39. """
  40. Returns True if 'path' can be located in an OSX SDK
  41. """
  42. return (path.startswith('/usr/') and not path.startswith('/usr/local')) or path.startswith('/System/')
  43. return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
  44. or path.startswith('/System/')
  45. or path.startswith('/Library/') )
  46. def find_file(filename, std_dirs, paths):
  47. """Searches for the directory where a given file is located,
  48. and returns a possibly-empty list of additional directories, or None
  49. if the file couldn't be found at all.
  50. 'filename' is the name of a file, such as readline.h or libcrypto.a.
  51. 'std_dirs' is the list of standard system directories; if the
  52. file is found in one of them, no additional directives are needed.
  53. 'paths' is a list of additional locations to check; if the file is
  54. found in one of them, the resulting list will contain the directory.
  55. """
  56. if sys.platform == 'darwin':
  57. # Honor the MacOSX SDK setting when one was specified.
  58. # An SDK is a directory with the same structure as a real
  59. # system, but with only header files and libraries.
  60. sysroot = macosx_sdk_root()
  61. # Check the standard locations
  62. for dir in std_dirs:
  63. f = os.path.join(dir, filename)
  64. if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
  65. f = os.path.join(sysroot, dir[1:], filename)
  66. if os.path.exists(f): return []
  67. # Check the additional directories
  68. for dir in paths:
  69. f = os.path.join(dir, filename)
  70. if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
  71. f = os.path.join(sysroot, dir[1:], filename)
  72. if os.path.exists(f):
  73. return [dir]
  74. # Not found anywhere
  75. return None
  76. def find_library_file(compiler, libname, std_dirs, paths):
  77. result = compiler.find_library_file(std_dirs + paths, libname)
  78. if result is None:
  79. return None
  80. if sys.platform == 'darwin':
  81. sysroot = macosx_sdk_root()
  82. # Check whether the found file is in one of the standard directories
  83. dirname = os.path.dirname(result)
  84. for p in std_dirs:
  85. # Ensure path doesn't end with path separator
  86. p = p.rstrip(os.sep)
  87. if sys.platform == 'darwin' and is_macosx_sdk_path(p):
  88. if os.path.join(sysroot, p[1:]) == dirname:
  89. return [ ]
  90. if p == dirname:
  91. return [ ]
  92. # Otherwise, it must have been in one of the additional directories,
  93. # so we have to figure out which one.
  94. for p in paths:
  95. # Ensure path doesn't end with path separator
  96. p = p.rstrip(os.sep)
  97. if sys.platform == 'darwin' and is_macosx_sdk_path(p):
  98. if os.path.join(sysroot, p[1:]) == dirname:
  99. return [ p ]
  100. if p == dirname:
  101. return [p]
  102. else:
  103. assert False, "Internal error: Path not found in std_dirs or paths"
  104. def module_enabled(extlist, modname):
  105. """Returns whether the module 'modname' is present in the list
  106. of extensions 'extlist'."""
  107. extlist = [ext for ext in extlist if ext.name == modname]
  108. return len(extlist)
  109. def find_module_file(module, dirlist):
  110. """Find a module in a set of possible folders. If it is not found
  111. return the unadorned filename"""
  112. list = find_file(module, [], dirlist)
  113. if not list:
  114. return module
  115. if len(list) > 1:
  116. log.info("WARNING: multiple copies of %s found"%module)
  117. return os.path.join(list[0], module)
  118. class PyBuildExt(build_ext):
  119. def __init__(self, dist):
  120. build_ext.__init__(self, dist)
  121. self.failed = []
  122. def build_extensions(self):
  123. # Detect which modules should be compiled
  124. missing = self.detect_modules()
  125. # Remove modules that are present on the disabled list
  126. extensions = [ext for ext in self.extensions
  127. if ext.name not in disabled_module_list]
  128. # move ctypes to the end, it depends on other modules
  129. ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
  130. if "_ctypes" in ext_map:
  131. ctypes = extensions.pop(ext_map["_ctypes"])
  132. extensions.append(ctypes)
  133. self.extensions = extensions
  134. # Fix up the autodetected modules, prefixing all the source files
  135. # with Modules/ and adding Python's include directory to the path.
  136. (srcdir,) = sysconfig.get_config_vars('srcdir')
  137. if not srcdir:
  138. # Maybe running on Windows but not using CYGWIN?
  139. raise ValueError("No source directory; cannot proceed.")
  140. srcdir = os.path.abspath(srcdir)
  141. moddirlist = [os.path.join(srcdir, 'Modules')]
  142. # Platform-dependent module source and include directories
  143. incdirlist = []
  144. platform = self.get_platform()
  145. if platform == 'darwin' and ("--disable-toolbox-glue" not in
  146. sysconfig.get_config_var("CONFIG_ARGS")):
  147. # Mac OS X also includes some mac-specific modules
  148. macmoddir = os.path.join(srcdir, 'Mac/Modules')
  149. moddirlist.append(macmoddir)
  150. incdirlist.append(os.path.join(srcdir, 'Mac/Include'))
  151. # Fix up the paths for scripts, too
  152. self.distribution.scripts = [os.path.join(srcdir, filename)
  153. for filename in self.distribution.scripts]
  154. # Python header files
  155. headers = [sysconfig.get_config_h_filename()]
  156. headers += glob(os.path.join(sysconfig.get_path('include'), "*.h"))
  157. for ext in self.extensions[:]:
  158. ext.sources = [ find_module_file(filename, moddirlist)
  159. for filename in ext.sources ]
  160. if ext.depends is not None:
  161. ext.depends = [find_module_file(filename, moddirlist)
  162. for filename in ext.depends]
  163. else:
  164. ext.depends = []
  165. # re-compile extensions if a header file has been changed
  166. ext.depends.extend(headers)
  167. # platform specific include directories
  168. ext.include_dirs.extend(incdirlist)
  169. # If a module has already been built statically,
  170. # don't build it here
  171. if ext.name in sys.builtin_module_names:
  172. self.extensions.remove(ext)
  173. # Parse Modules/Setup and Modules/Setup.local to figure out which
  174. # modules are turned on in the file.
  175. remove_modules = []
  176. for filename in ('Modules/Setup', 'Modules/Setup.local'):
  177. input = text_file.TextFile(filename, join_lines=1)
  178. while 1:
  179. line = input.readline()
  180. if not line: break
  181. line = line.split()
  182. remove_modules.append(line[0])
  183. input.close()
  184. for ext in self.extensions[:]:
  185. if ext.name in remove_modules:
  186. self.extensions.remove(ext)
  187. # When you run "make CC=altcc" or something similar, you really want
  188. # those environment variables passed into the setup.py phase. Here's
  189. # a small set of useful ones.
  190. compiler = os.environ.get('CC')
  191. args = {}
  192. # unfortunately, distutils doesn't let us provide separate C and C++
  193. # compilers
  194. if compiler is not None:
  195. (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
  196. args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
  197. self.compiler.set_executables(**args)
  198. build_ext.build_extensions(self)
  199. longest = max([len(e.name) for e in self.extensions])
  200. if self.failed:
  201. longest = max(longest, max([len(name) for name in self.failed]))
  202. def print_three_column(lst):
  203. lst.sort(key=str.lower)
  204. # guarantee zip() doesn't drop anything
  205. while len(lst) % 3:
  206. lst.append("")
  207. for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
  208. print "%-*s %-*s %-*s" % (longest, e, longest, f,
  209. longest, g)
  210. if missing:
  211. print
  212. print ("Python build finished, but the necessary bits to build "
  213. "these modules were not found:")
  214. print_three_column(missing)
  215. print ("To find the necessary bits, look in setup.py in"
  216. " detect_modules() for the module's name.")
  217. print
  218. if self.failed:
  219. failed = self.failed[:]
  220. print
  221. print "Failed to build these modules:"
  222. print_three_column(failed)
  223. print
  224. def build_extension(self, ext):
  225. if ext.name == '_ctypes':
  226. if not self.configure_ctypes(ext):
  227. return
  228. try:
  229. build_ext.build_extension(self, ext)
  230. except (CCompilerError, DistutilsError), why:
  231. self.announce('WARNING: building of extension "%s" failed: %s' %
  232. (ext.name, sys.exc_info()[1]))
  233. self.failed.append(ext.name)
  234. return
  235. # Workaround for Mac OS X: The Carbon-based modules cannot be
  236. # reliably imported into a command-line Python
  237. if 'Carbon' in ext.extra_link_args:
  238. self.announce(
  239. 'WARNING: skipping import check for Carbon-based "%s"' %
  240. ext.name)
  241. return
  242. if self.get_platform() == 'darwin' and (
  243. sys.maxint > 2**32 and '-arch' in ext.extra_link_args):
  244. # Don't bother doing an import check when an extension was
  245. # build with an explicit '-arch' flag on OSX. That's currently
  246. # only used to build 32-bit only extensions in a 4-way
  247. # universal build and loading 32-bit code into a 64-bit
  248. # process will fail.
  249. self.announce(
  250. 'WARNING: skipping import check for "%s"' %
  251. ext.name)
  252. return
  253. # Workaround for Cygwin: Cygwin currently has fork issues when many
  254. # modules have been imported
  255. if self.get_platform() == 'cygwin':
  256. self.announce('WARNING: skipping import check for Cygwin-based "%s"'
  257. % ext.name)
  258. return
  259. ext_filename = os.path.join(
  260. self.build_lib,
  261. self.get_ext_filename(self.get_ext_fullname(ext.name)))
  262. try:
  263. imp.load_dynamic(ext.name, ext_filename)
  264. except ImportError, why:
  265. self.failed.append(ext.name)
  266. self.announce('*** WARNING: renaming "%s" since importing it'
  267. ' failed: %s' % (ext.name, why), level=3)
  268. assert not self.inplace
  269. basename, tail = os.path.splitext(ext_filename)
  270. newname = basename + "_failed" + tail
  271. if os.path.exists(newname):
  272. os.remove(newname)
  273. os.rename(ext_filename, newname)
  274. # XXX -- This relies on a Vile HACK in
  275. # distutils.command.build_ext.build_extension(). The
  276. # _built_objects attribute is stored there strictly for
  277. # use here.
  278. # If there is a failure, _built_objects may not be there,
  279. # so catch the AttributeError and move on.
  280. try:
  281. for filename in self._built_objects:
  282. os.remove(filename)
  283. except AttributeError:
  284. self.announce('unable to remove files (ignored)')
  285. except:
  286. exc_type, why, tb = sys.exc_info()
  287. self.announce('*** WARNING: importing extension "%s" '
  288. 'failed with %s: %s' % (ext.name, exc_type, why),
  289. level=3)
  290. self.failed.append(ext.name)
  291. def get_platform(self):
  292. # Get value of sys.platform
  293. for platform in ['cygwin', 'beos', 'darwin', 'atheos', 'osf1']:
  294. if sys.platform.startswith(platform):
  295. return platform
  296. return sys.platform
  297. def add_multiarch_paths(self):
  298. # Debian/Ubuntu multiarch support.
  299. # https://wiki.ubuntu.com/MultiarchSpec
  300. if not find_executable('dpkg-architecture'):
  301. return
  302. tmpfile = os.path.join(self.build_temp, 'multiarch')
  303. if not os.path.exists(self.build_temp):
  304. os.makedirs(self.build_temp)
  305. ret = os.system(
  306. 'dpkg-architecture -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
  307. tmpfile)
  308. try:
  309. if ret >> 8 == 0:
  310. with open(tmpfile) as fp:
  311. multiarch_path_component = fp.readline().strip()
  312. add_dir_to_list(self.compiler.library_dirs,
  313. '/usr/lib/' + multiarch_path_component)
  314. add_dir_to_list(self.compiler.include_dirs,
  315. '/usr/include/' + multiarch_path_component)
  316. finally:
  317. os.unlink(tmpfile)
  318. def detect_modules(self):
  319. # Ensure that /usr/local is always used
  320. add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
  321. add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
  322. self.add_multiarch_paths()
  323. # Add paths specified in the environment variables LDFLAGS and
  324. # CPPFLAGS for header and library files.
  325. # We must get the values from the Makefile and not the environment
  326. # directly since an inconsistently reproducible issue comes up where
  327. # the environment variable is not set even though the value were passed
  328. # into configure and stored in the Makefile (issue found on OS X 10.3).
  329. for env_var, arg_name, dir_list in (
  330. ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
  331. ('LDFLAGS', '-L', self.compiler.library_dirs),
  332. ('CPPFLAGS', '-I', self.compiler.include_dirs)):
  333. env_val = sysconfig.get_config_var(env_var)
  334. if env_val:
  335. # To prevent optparse from raising an exception about any
  336. # options in env_val that it doesn't know about we strip out
  337. # all double dashes and any dashes followed by a character
  338. # that is not for the option we are dealing with.
  339. #
  340. # Please note that order of the regex is important! We must
  341. # strip out double-dashes first so that we don't end up with
  342. # substituting "--Long" to "-Long" and thus lead to "ong" being
  343. # used for a library directory.
  344. env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
  345. ' ', env_val)
  346. parser = optparse.OptionParser()
  347. # Make sure that allowing args interspersed with options is
  348. # allowed
  349. parser.allow_interspersed_args = True
  350. parser.error = lambda msg: None
  351. parser.add_option(arg_name, dest="dirs", action="append")
  352. options = parser.parse_args(env_val.split())[0]
  353. if options.dirs:
  354. for directory in reversed(options.dirs):
  355. add_dir_to_list(dir_list, directory)
  356. if os.path.normpath(sys.prefix) != '/usr' \
  357. and not sysconfig.get_config_var('PYTHONFRAMEWORK'):
  358. # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
  359. # (PYTHONFRAMEWORK is set) to avoid # linking problems when
  360. # building a framework with different architectures than
  361. # the one that is currently installed (issue #7473)
  362. add_dir_to_list(self.compiler.library_dirs,
  363. sysconfig.get_config_var("LIBDIR"))
  364. add_dir_to_list(self.compiler.include_dirs,
  365. sysconfig.get_config_var("INCLUDEDIR"))
  366. try:
  367. have_unicode = unicode
  368. except NameError:
  369. have_unicode = 0
  370. # lib_dirs and inc_dirs are used to search for files;
  371. # if a file is found in one of those directories, it can
  372. # be assumed that no additional -I,-L directives are needed.
  373. lib_dirs = self.compiler.library_dirs + [
  374. '/lib64', '/usr/lib64',
  375. '/lib', '/usr/lib',
  376. ]
  377. inc_dirs = self.compiler.include_dirs + ['/usr/include']
  378. exts = []
  379. missing = []
  380. config_h = sysconfig.get_config_h_filename()
  381. config_h_vars = sysconfig.parse_config_h(open(config_h))
  382. platform = self.get_platform()
  383. srcdir = sysconfig.get_config_var('srcdir')
  384. # Check for AtheOS which has libraries in non-standard locations
  385. if platform == 'atheos':
  386. lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
  387. lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
  388. inc_dirs += ['/system/include', '/atheos/autolnk/include']
  389. inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
  390. # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
  391. if platform in ['osf1', 'unixware7', 'openunix8']:
  392. lib_dirs += ['/usr/ccs/lib']
  393. # HP-UX11iv3 keeps files in lib/hpux folders.
  394. if platform == 'hp-ux11':
  395. lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
  396. if platform == 'darwin':
  397. # This should work on any unixy platform ;-)
  398. # If the user has bothered specifying additional -I and -L flags
  399. # in OPT and LDFLAGS we might as well use them here.
  400. # NOTE: using shlex.split would technically be more correct, but
  401. # also gives a bootstrap problem. Let's hope nobody uses directories
  402. # with whitespace in the name to store libraries.
  403. cflags, ldflags = sysconfig.get_config_vars(
  404. 'CFLAGS', 'LDFLAGS')
  405. for item in cflags.split():
  406. if item.startswith('-I'):
  407. inc_dirs.append(item[2:])
  408. for item in ldflags.split():
  409. if item.startswith('-L'):
  410. lib_dirs.append(item[2:])
  411. # Check for MacOS X, which doesn't need libm.a at all
  412. math_libs = ['m']
  413. if platform in ['darwin', 'beos']:
  414. math_libs = []
  415. # XXX Omitted modules: gl, pure, dl, SGI-specific modules
  416. #
  417. # The following modules are all pretty straightforward, and compile
  418. # on pretty much any POSIXish platform.
  419. #
  420. # Some modules that are normally always on:
  421. #exts.append( Extension('_weakref', ['_weakref.c']) )
  422. # array objects
  423. exts.append( Extension('array', ['arraymodule.c']) )
  424. # complex math library functions
  425. exts.append( Extension('cmath', ['cmathmodule.c', '_math.c'],
  426. depends=['_math.h'],
  427. libraries=math_libs) )
  428. # math library functions, e.g. sin()
  429. exts.append( Extension('math', ['mathmodule.c', '_math.c'],
  430. depends=['_math.h'],
  431. libraries=math_libs) )
  432. # fast string operations implemented in C
  433. exts.append( Extension('strop', ['stropmodule.c']) )
  434. # time operations and variables
  435. exts.append( Extension('time', ['timemodule.c'],
  436. libraries=math_libs) )
  437. exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'],
  438. libraries=math_libs) )
  439. # fast iterator tools implemented in C
  440. exts.append( Extension("itertools", ["itertoolsmodule.c"]) )
  441. # code that will be builtins in the future, but conflict with the
  442. # current builtins
  443. exts.append( Extension('future_builtins', ['future_builtins.c']) )
  444. # random number generator implemented in C
  445. exts.append( Extension("_random", ["_randommodule.c"]) )
  446. # high-performance collections
  447. exts.append( Extension("_collections", ["_collectionsmodule.c"]) )
  448. # bisect
  449. exts.append( Extension("_bisect", ["_bisectmodule.c"]) )
  450. # heapq
  451. exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
  452. # operator.add() and similar goodies
  453. exts.append( Extension('operator', ['operator.c']) )
  454. # Python 3.1 _io library
  455. exts.append( Extension("_io",
  456. ["_io/bufferedio.c", "_io/bytesio.c", "_io/fileio.c",
  457. "_io/iobase.c", "_io/_iomodule.c", "_io/stringio.c", "_io/textio.c"],
  458. depends=["_io/_iomodule.h"], include_dirs=["Modules/_io"]))
  459. # _functools
  460. exts.append( Extension("_functools", ["_functoolsmodule.c"]) )
  461. # _json speedups
  462. exts.append( Extension("_json", ["_json.c"]) )
  463. # Python C API test module
  464. exts.append( Extension('_testcapi', ['_testcapimodule.c'],
  465. depends=['testcapi_long.h']) )
  466. # profilers (_lsprof is for cProfile.py)
  467. exts.append( Extension('_hotshot', ['_hotshot.c']) )
  468. exts.append( Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']) )
  469. # static Unicode character database
  470. if have_unicode:
  471. exts.append( Extension('unicodedata', ['unicodedata.c']) )
  472. else:
  473. missing.append('unicodedata')
  474. # access to ISO C locale support
  475. data = open('pyconfig.h').read()
  476. m = re.search(r"#s*define\s+WITH_LIBINTL\s+1\s*", data)
  477. if m is not None:
  478. locale_libs = ['intl']
  479. else:
  480. locale_libs = []
  481. if platform == 'darwin':
  482. locale_extra_link_args = ['-framework', 'CoreFoundation']
  483. else:
  484. locale_extra_link_args = []
  485. exts.append( Extension('_locale', ['_localemodule.c'],
  486. libraries=locale_libs,
  487. extra_link_args=locale_extra_link_args) )
  488. # Modules with some UNIX dependencies -- on by default:
  489. # (If you have a really backward UNIX, select and socket may not be
  490. # supported...)
  491. # fcntl(2) and ioctl(2)
  492. libs = []
  493. if (config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
  494. # May be necessary on AIX for flock function
  495. libs = ['bsd']
  496. exts.append( Extension('fcntl', ['fcntlmodule.c'], libraries=libs) )
  497. # pwd(3)
  498. exts.append( Extension('pwd', ['pwdmodule.c']) )
  499. # grp(3)
  500. exts.append( Extension('grp', ['grpmodule.c']) )
  501. # spwd, shadow passwords
  502. if (config_h_vars.get('HAVE_GETSPNAM', False) or
  503. config_h_vars.get('HAVE_GETSPENT', False)):
  504. exts.append( Extension('spwd', ['spwdmodule.c']) )
  505. else:
  506. missing.append('spwd')
  507. # select(2); not on ancient System V
  508. exts.append( Extension('select', ['selectmodule.c']) )
  509. # Fred Drake's interface to the Python parser
  510. exts.append( Extension('parser', ['parsermodule.c']) )
  511. # cStringIO and cPickle
  512. exts.append( Extension('cStringIO', ['cStringIO.c']) )
  513. exts.append( Extension('cPickle', ['cPickle.c']) )
  514. # Memory-mapped files (also works on Win32).
  515. if platform not in ['atheos']:
  516. exts.append( Extension('mmap', ['mmapmodule.c']) )
  517. else:
  518. missing.append('mmap')
  519. # Lance Ellinghaus's syslog module
  520. # syslog daemon interface
  521. exts.append( Extension('syslog', ['syslogmodule.c']) )
  522. # George Neville-Neil's timing module:
  523. # Deprecated in PEP 4 http://www.python.org/peps/pep-0004.html
  524. # http://mail.python.org/pipermail/python-dev/2006-January/060023.html
  525. #exts.append( Extension('timing', ['timingmodule.c']) )
  526. #
  527. # Here ends the simple stuff. From here on, modules need certain
  528. # libraries, are platform-specific, or present other surprises.
  529. #
  530. # Multimedia modules
  531. # These don't work for 64-bit platforms!!!
  532. # These represent audio samples or images as strings:
  533. # Operations on audio samples
  534. # According to #993173, this one should actually work fine on
  535. # 64-bit platforms.
  536. exts.append( Extension('audioop', ['audioop.c']) )
  537. # Disabled on 64-bit platforms
  538. if sys.maxint != 9223372036854775807L:
  539. # Operations on images
  540. exts.append( Extension('imageop', ['imageop.c']) )
  541. else:
  542. missing.extend(['imageop'])
  543. # readline
  544. do_readline = self.compiler.find_library_file(lib_dirs, 'readline')
  545. readline_termcap_library = ""
  546. curses_library = ""
  547. # Determine if readline is already linked against curses or tinfo.
  548. if do_readline and find_executable('ldd'):
  549. fp = os.popen("ldd %s" % do_readline)
  550. ldd_output = fp.readlines()
  551. ret = fp.close()
  552. if ret is None or ret >> 8 == 0:
  553. for ln in ldd_output:
  554. if 'curses' in ln:
  555. readline_termcap_library = re.sub(
  556. r'.*lib(n?cursesw?)\.so.*', r'\1', ln
  557. ).rstrip()
  558. break
  559. if 'tinfo' in ln: # termcap interface split out from ncurses
  560. readline_termcap_library = 'tinfo'
  561. break
  562. # Issue 7384: If readline is already linked against curses,
  563. # use the same library for the readline and curses modules.
  564. if 'curses' in readline_termcap_library:
  565. curses_library = readline_termcap_library
  566. elif self.compiler.find_library_file(lib_dirs, 'ncursesw'):
  567. curses_library = 'ncursesw'
  568. elif self.compiler.find_library_file(lib_dirs, 'ncurses'):
  569. curses_library = 'ncurses'
  570. elif self.compiler.find_library_file(lib_dirs, 'curses'):
  571. curses_library = 'curses'
  572. if platform == 'darwin':
  573. os_release = int(os.uname()[2].split('.')[0])
  574. dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
  575. if dep_target and dep_target.split('.') < ['10', '5']:
  576. os_release = 8
  577. if os_release < 9:
  578. # MacOSX 10.4 has a broken readline. Don't try to build
  579. # the readline module unless the user has installed a fixed
  580. # readline package
  581. if find_file('readline/rlconf.h', inc_dirs, []) is None:
  582. do_readline = False
  583. if do_readline:
  584. if platform == 'darwin' and os_release < 9:
  585. # In every directory on the search path search for a dynamic
  586. # library and then a static library, instead of first looking
  587. # for dynamic libraries on the entiry path.
  588. # This way a staticly linked custom readline gets picked up
  589. # before the (possibly broken) dynamic library in /usr/lib.
  590. readline_extra_link_args = ('-Wl,-search_paths_first',)
  591. else:
  592. readline_extra_link_args = ()
  593. readline_libs = ['readline']
  594. if readline_termcap_library:
  595. pass # Issue 7384: Already linked against curses or tinfo.
  596. elif curses_library:
  597. readline_libs.append(curses_library)
  598. elif self.compiler.find_library_file(lib_dirs +
  599. ['/usr/lib/termcap'],
  600. 'termcap'):
  601. readline_libs.append('termcap')
  602. exts.append( Extension('readline', ['readline.c'],
  603. library_dirs=['/usr/lib/termcap'],
  604. extra_link_args=readline_extra_link_args,
  605. libraries=readline_libs) )
  606. else:
  607. missing.append('readline')
  608. # crypt module.
  609. if self.compiler.find_library_file(lib_dirs, 'crypt'):
  610. libs = ['crypt']
  611. else:
  612. libs = []
  613. exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
  614. # CSV files
  615. exts.append( Extension('_csv', ['_csv.c']) )
  616. # socket(2)
  617. exts.append( Extension('_socket', ['socketmodule.c'],
  618. depends = ['socketmodule.h']) )
  619. # Detect SSL support for the socket module (via _ssl)
  620. search_for_ssl_incs_in = [
  621. '/usr/local/ssl/include',
  622. '/usr/contrib/ssl/include/'
  623. ]
  624. ssl_incs = find_file('openssl/ssl.h', inc_dirs,
  625. search_for_ssl_incs_in
  626. )
  627. if ssl_incs is not None:
  628. krb5_h = find_file('krb5.h', inc_dirs,
  629. ['/usr/kerberos/include'])
  630. if krb5_h:
  631. ssl_incs += krb5_h
  632. ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
  633. ['/usr/local/ssl/lib',
  634. '/usr/contrib/ssl/lib/'
  635. ] )
  636. if (ssl_incs is not None and
  637. ssl_libs is not None):
  638. exts.append( Extension('_ssl', ['_ssl.c'],
  639. include_dirs = ssl_incs,
  640. library_dirs = ssl_libs,
  641. libraries = ['ssl', 'crypto'],
  642. depends = ['socketmodule.h']), )
  643. else:
  644. missing.append('_ssl')
  645. # find out which version of OpenSSL we have
  646. openssl_ver = 0
  647. openssl_ver_re = re.compile(
  648. '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
  649. # look for the openssl version header on the compiler search path.
  650. opensslv_h = find_file('openssl/opensslv.h', [],
  651. inc_dirs + search_for_ssl_incs_in)
  652. if opensslv_h:
  653. name = os.path.join(opensslv_h[0], 'openssl/opensslv.h')
  654. if sys.platform == 'darwin' and is_macosx_sdk_path(name):
  655. name = os.path.join(macosx_sdk_root(), name[1:])
  656. try:
  657. incfile = open(name, 'r')
  658. for line in incfile:
  659. m = openssl_ver_re.match(line)
  660. if m:
  661. openssl_ver = eval(m.group(1))
  662. except IOError, msg:
  663. print "IOError while reading opensshv.h:", msg
  664. pass
  665. min_openssl_ver = 0x00907000
  666. have_any_openssl = ssl_incs is not None and ssl_libs is not None
  667. have_usable_openssl = (have_any_openssl and
  668. openssl_ver >= min_openssl_ver)
  669. if have_any_openssl:
  670. if have_usable_openssl:
  671. # The _hashlib module wraps optimized implementations
  672. # of hash functions from the OpenSSL library.
  673. exts.append( Extension('_hashlib', ['_hashopenssl.c'],
  674. include_dirs = ssl_incs,
  675. library_dirs = ssl_libs,
  676. libraries = ['ssl', 'crypto']) )
  677. else:
  678. print ("warning: openssl 0x%08x is too old for _hashlib" %
  679. openssl_ver)
  680. missing.append('_hashlib')
  681. if COMPILED_WITH_PYDEBUG or not have_usable_openssl:
  682. # The _sha module implements the SHA1 hash algorithm.
  683. exts.append( Extension('_sha', ['shamodule.c']) )
  684. # The _md5 module implements the RSA Data Security, Inc. MD5
  685. # Message-Digest Algorithm, described in RFC 1321. The
  686. # necessary files md5.c and md5.h are included here.
  687. exts.append( Extension('_md5',
  688. sources = ['md5module.c', 'md5.c'],
  689. depends = ['md5.h']) )
  690. min_sha2_openssl_ver = 0x00908000
  691. if COMPILED_WITH_PYDEBUG or openssl_ver < min_sha2_openssl_ver:
  692. # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
  693. exts.append( Extension('_sha256', ['sha256module.c']) )
  694. exts.append( Extension('_sha512', ['sha512module.c']) )
  695. # Modules that provide persistent dictionary-like semantics. You will
  696. # probably want to arrange for at least one of them to be available on
  697. # your machine, though none are defined by default because of library
  698. # dependencies. The Python module anydbm.py provides an
  699. # implementation independent wrapper for these; dumbdbm.py provides
  700. # similar functionality (but slower of course) implemented in Python.
  701. # Sleepycat^WOracle Berkeley DB interface.
  702. # http://www.oracle.com/database/berkeley-db/db/index.html
  703. #
  704. # This requires the Sleepycat^WOracle DB code. The supported versions
  705. # are set below. Visit the URL above to download
  706. # a release. Most open source OSes come with one or more
  707. # versions of BerkeleyDB already installed.
  708. max_db_ver = (4, 8)
  709. min_db_ver = (4, 1)
  710. db_setup_debug = False # verbose debug prints from this script?
  711. def allow_db_ver(db_ver):
  712. """Returns a boolean if the given BerkeleyDB version is acceptable.
  713. Args:
  714. db_ver: A tuple of the version to verify.
  715. """
  716. if not (min_db_ver <= db_ver <= max_db_ver):
  717. return False
  718. # Use this function to filter out known bad configurations.
  719. if (4, 6) == db_ver[:2]:
  720. # BerkeleyDB 4.6.x is not stable on many architectures.
  721. arch = platform_machine()
  722. if arch not in ('i386', 'i486', 'i586', 'i686',
  723. 'x86_64', 'ia64'):
  724. return False
  725. return True
  726. def gen_db_minor_ver_nums(major):
  727. if major == 4:
  728. for x in range(max_db_ver[1]+1):
  729. if allow_db_ver((4, x)):
  730. yield x
  731. elif major == 3:
  732. for x in (3,):
  733. if allow_db_ver((3, x)):
  734. yield x
  735. else:
  736. raise ValueError("unknown major BerkeleyDB version", major)
  737. # construct a list of paths to look for the header file in on
  738. # top of the normal inc_dirs.
  739. db_inc_paths = [
  740. '/usr/include/db4',
  741. '/usr/local/include/db4',
  742. '/opt/sfw/include/db4',
  743. '/usr/include/db3',
  744. '/usr/local/include/db3',
  745. '/opt/sfw/include/db3',
  746. # Fink defaults (http://fink.sourceforge.net/)
  747. '/sw/include/db4',
  748. '/sw/include/db3',
  749. ]
  750. # 4.x minor number specific paths
  751. for x in gen_db_minor_ver_nums(4):
  752. db_inc_paths.append('/usr/include/db4%d' % x)
  753. db_inc_paths.append('/usr/include/db4.%d' % x)
  754. db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
  755. db_inc_paths.append('/usr/local/include/db4%d' % x)
  756. db_inc_paths.append('/pkg/db-4.%d/include' % x)
  757. db_inc_paths.append('/opt/db-4.%d/include' % x)
  758. # MacPorts default (http://www.macports.org/)
  759. db_inc_paths.append('/opt/local/include/db4%d' % x)
  760. # 3.x minor number specific paths
  761. for x in gen_db_minor_ver_nums(3):
  762. db_inc_paths.append('/usr/include/db3%d' % x)
  763. db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
  764. db_inc_paths.append('/usr/local/include/db3%d' % x)
  765. db_inc_paths.append('/pkg/db-3.%d/include' % x)
  766. db_inc_paths.append('/opt/db-3.%d/include' % x)
  767. # Add some common subdirectories for Sleepycat DB to the list,
  768. # based on the standard include directories. This way DB3/4 gets
  769. # picked up when it is installed in a non-standard prefix and
  770. # the user has added that prefix into inc_dirs.
  771. std_variants = []
  772. for dn in inc_dirs:
  773. std_variants.append(os.path.join(dn, 'db3'))
  774. std_variants.append(os.path.join(dn, 'db4'))
  775. for x in gen_db_minor_ver_nums(4):
  776. std_variants.append(os.path.join(dn, "db4%d"%x))
  777. std_variants.append(os.path.join(dn, "db4.%d"%x))
  778. for x in gen_db_minor_ver_nums(3):
  779. std_variants.append(os.path.join(dn, "db3%d"%x))
  780. std_variants.append(os.path.join(dn, "db3.%d"%x))
  781. db_inc_paths = std_variants + db_inc_paths
  782. db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
  783. db_ver_inc_map = {}
  784. if sys.platform == 'darwin':
  785. sysroot = macosx_sdk_root()
  786. class db_found(Exception): pass
  787. try:
  788. # See whether there is a Sleepycat header in the standard
  789. # search path.
  790. for d in inc_dirs + db_inc_paths:
  791. f = os.path.join(d, "db.h")
  792. if sys.platform == 'darwin' and is_macosx_sdk_path(d):
  793. f = os.path.join(sysroot, d[1:], "db.h")
  794. if db_setup_debug: print "db: looking for db.h in", f
  795. if os.path.exists(f):
  796. f = open(f).read()
  797. m = re.search(r"#define\WDB_VERSION_MAJOR\W(\d+)", f)
  798. if m:
  799. db_major = int(m.group(1))
  800. m = re.search(r"#define\WDB_VERSION_MINOR\W(\d+)", f)
  801. db_minor = int(m.group(1))
  802. db_ver = (db_major, db_minor)
  803. # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
  804. if db_ver == (4, 6):
  805. m = re.search(r"#define\WDB_VERSION_PATCH\W(\d+)", f)
  806. db_patch = int(m.group(1))
  807. if db_patch < 21:
  808. print "db.h:", db_ver, "patch", db_patch,
  809. print "being ignored (4.6.x must be >= 4.6.21)"
  810. continue
  811. if ( (db_ver not in db_ver_inc_map) and
  812. allow_db_ver(db_ver) ):
  813. # save the include directory with the db.h version
  814. # (first occurrence only)
  815. db_ver_inc_map[db_ver] = d
  816. if db_setup_debug:
  817. print "db.h: found", db_ver, "in", d
  818. else:
  819. # we already found a header for this library version
  820. if db_setup_debug: print "db.h: ignoring", d
  821. else:
  822. # ignore this header, it didn't contain a version number
  823. if db_setup_debug:
  824. print "db.h: no version number version in", d
  825. db_found_vers = db_ver_inc_map.keys()
  826. db_found_vers.sort()
  827. while db_found_vers:
  828. db_ver = db_found_vers.pop()
  829. db_incdir = db_ver_inc_map[db_ver]
  830. # check lib directories parallel to the location of the header
  831. db_dirs_to_check = [
  832. db_incdir.replace("include", 'lib64'),
  833. db_incdir.replace("include", 'lib'),
  834. ]
  835. if sys.platform != 'darwin':
  836. db_dirs_to_check = filter(os.path.isdir, db_dirs_to_check)
  837. else:
  838. # Same as other branch, but takes OSX SDK into account
  839. tmp = []
  840. for dn in db_dirs_to_check:
  841. if is_macosx_sdk_path(dn):
  842. if os.path.isdir(os.path.join(sysroot, dn[1:])):
  843. tmp.append(dn)
  844. else:
  845. if os.path.isdir(dn):
  846. tmp.append(dn)
  847. db_dirs_to_check = tmp
  848. # Look for a version specific db-X.Y before an ambiguous dbX
  849. # XXX should we -ever- look for a dbX name? Do any
  850. # systems really not name their library by version and
  851. # symlink to more general names?
  852. for dblib in (('db-%d.%d' % db_ver),
  853. ('db%d%d' % db_ver),
  854. ('db%d' % db_ver[0])):
  855. dblib_file = self.compiler.find_library_file(
  856. db_dirs_to_check + lib_dirs, dblib )
  857. if dblib_file:
  858. dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
  859. raise db_found
  860. else:
  861. if db_setup_debug: print "db lib: ", dblib, "not found"
  862. except db_found:
  863. if db_setup_debug:
  864. print "bsddb using BerkeleyDB lib:", db_ver, dblib
  865. print "bsddb lib dir:", dblib_dir, " inc dir:", db_incdir
  866. db_incs = [db_incdir]
  867. dblibs = [dblib]
  868. # We add the runtime_library_dirs argument because the
  869. # BerkeleyDB lib we're linking against often isn't in the
  870. # system dynamic library search path. This is usually
  871. # correct and most trouble free, but may cause problems in
  872. # some unusual system configurations (e.g. the directory
  873. # is on an NFS server that goes away).
  874. exts.append(Extension('_bsddb', ['_bsddb.c'],
  875. depends = ['bsddb.h'],
  876. library_dirs=dblib_dir,
  877. runtime_library_dirs=dblib_dir,
  878. include_dirs=db_incs,
  879. libraries=dblibs))
  880. else:
  881. if db_setup_debug: print "db: no appropriate library found"
  882. db_incs = None
  883. dblibs = []
  884. dblib_dir = None
  885. missing.append('_bsddb')
  886. # The sqlite interface
  887. sqlite_setup_debug = False # verbose debug prints from this script?
  888. # We hunt for #define SQLITE_VERSION "n.n.n"
  889. # We need to find >= sqlite version 3.0.8
  890. sqlite_incdir = sqlite_libdir = None
  891. sqlite_inc_paths = [ '/usr/include',
  892. '/usr/include/sqlite',
  893. '/usr/include/sqlite3',
  894. '/usr/local/include',
  895. '/usr/local/include/sqlite',
  896. '/usr/local/include/sqlite3',
  897. ]
  898. MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
  899. MIN_SQLITE_VERSION = ".".join([str(x)
  900. for x in MIN_SQLITE_VERSION_NUMBER])
  901. # Scan the default include directories before the SQLite specific
  902. # ones. This allows one to override the copy of sqlite on OSX,
  903. # where /usr/include contains an old version of sqlite.
  904. if sys.platform == 'darwin':
  905. sysroot = macosx_sdk_root()
  906. for d_ in inc_dirs + sqlite_inc_paths:
  907. d = d_
  908. if sys.platform == 'darwin' and is_macosx_sdk_path(d):
  909. d = os.path.join(sysroot, d[1:])
  910. f = os.path.join(d, "sqlite3.h")
  911. if os.path.exists(f):
  912. if sqlite_setup_debug: print "sqlite: found %s"%f
  913. incf = open(f).read()
  914. m = re.search(
  915. r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
  916. if m:
  917. sqlite_version = m.group(1)
  918. sqlite_version_tuple = tuple([int(x)
  919. for x in sqlite_version.split(".")])
  920. if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
  921. # we win!
  922. if sqlite_setup_debug:
  923. print "%s/sqlite3.h: version %s"%(d, sqlite_version)
  924. sqlite_incdir = d
  925. break
  926. else:
  927. if sqlite_setup_debug:
  928. print "%s: version %d is too old, need >= %s"%(d,
  929. sqlite_version, MIN_SQLITE_VERSION)
  930. elif sqlite_setup_debug:
  931. print "sqlite: %s had no SQLITE_VERSION"%(f,)
  932. if sqlite_incdir:
  933. sqlite_dirs_to_check = [
  934. os.path.join(sqlite_incdir, '..', 'lib64'),
  935. os.path.join(sqlite_incdir, '..', 'lib'),
  936. os.path.join(sqlite_incdir, '..', '..', 'lib64'),
  937. os.path.join(sqlite_incdir, '..', '..', 'lib'),
  938. ]
  939. sqlite_libfile = self.compiler.find_library_file(
  940. sqlite_dirs_to_check + lib_dirs, 'sqlite3')
  941. if sqlite_libfile:
  942. sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
  943. if sqlite_incdir and sqlite_libdir:
  944. sqlite_srcs = ['_sqlite/cache.c',
  945. '_sqlite/connection.c',
  946. '_sqlite/cursor.c',
  947. '_sqlite/microprotocols.c',
  948. '_sqlite/module.c',
  949. '_sqlite/prepare_protocol.c',
  950. '_sqlite/row.c',
  951. '_sqlite/statement.c',
  952. '_sqlite/util.c', ]
  953. sqlite_defines = []
  954. if sys.platform != "win32":
  955. sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
  956. else:
  957. sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
  958. # Comment this out if you want the sqlite3 module to be able to load extensions.
  959. sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
  960. if sys.platform == 'darwin':
  961. # In every directory on the search path search for a dynamic
  962. # library and then a static library, instead of first looking
  963. # for dynamic libraries on the entire path.
  964. # This way a statically linked custom sqlite gets picked up
  965. # before the dynamic library in /usr/lib.
  966. sqlite_extra_link_args = ('-Wl,-search_paths_first',)
  967. else:
  968. sqlite_extra_link_args = ()
  969. exts.append(Extension('_sqlite3', sqlite_srcs,
  970. define_macros=sqlite_defines,
  971. include_dirs=["Modules/_sqlite",
  972. sqlite_incdir],
  973. library_dirs=sqlite_libdir,
  974. runtime_library_dirs=sqlite_libdir,
  975. extra_link_args=sqlite_extra_link_args,
  976. libraries=["sqlite3",]))
  977. else:
  978. missing.append('_sqlite3')
  979. # Look for Berkeley db 1.85. Note that it is built as a different
  980. # module name so it can be included even when later versions are
  981. # available. A very restrictive search is performed to avoid
  982. # accidentally building this module with a later version of the
  983. # underlying db library. May BSD-ish Unixes incorporate db 1.85
  984. # symbols into libc and place the include file in /usr/include.
  985. #
  986. # If the better bsddb library can be built (db_incs is defined)
  987. # we do not build this one. Otherwise this build will pick up
  988. # the more recent berkeleydb's db.h file first in the include path
  989. # when attempting to compile and it will fail.
  990. f = "/usr/include/db.h"
  991. if sys.platform == 'darwin':
  992. if is_macosx_sdk_path(f):
  993. sysroot = macosx_sdk_root()
  994. f = os.path.join(sysroot, f[1:])
  995. if os.path.exists(f) and not db_incs:
  996. data = open(f).read()
  997. m = re.search(r"#s*define\s+HASHVERSION\s+2\s*", data)
  998. if m is not None:
  999. # bingo - old version used hash file format version 2
  1000. ### XXX this should be fixed to not be platform-dependent
  1001. ### but I don't have direct access to an osf1 platform and
  1002. ### seemed to be muffing the search somehow
  1003. libraries = platform == "osf1" and ['db'] or None
  1004. if libraries is not None:
  1005. exts.append(Extension('bsddb185', ['bsddbmodule.c'],
  1006. libraries=libraries))
  1007. else:
  1008. exts.append(Extension('bsddb185', ['bsddbmodule.c']))
  1009. else:
  1010. missing.append('bsddb185')
  1011. else:
  1012. missing.append('bsddb185')
  1013. dbm_order = ['gdbm']
  1014. # The standard Unix dbm module:
  1015. if platform not in ['cygwin']:
  1016. config_args = [arg.strip("'")
  1017. for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
  1018. dbm_args = [arg for arg in config_args
  1019. if arg.startswith('--with-dbmliborder=')]
  1020. if dbm_args:
  1021. dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
  1022. else:
  1023. dbm_order = "ndbm:gdbm:bdb".split(":")
  1024. dbmext = None
  1025. for cand in dbm_order:
  1026. if cand == "ndbm":
  1027. if find_file("ndbm.h", inc_dirs, []) is not None:
  1028. # Some systems have -lndbm, others have -lgdbm_compat,
  1029. # others don't have either
  1030. if self.compiler.find_library_file(lib_dirs,
  1031. 'ndbm'):
  1032. ndbm_libs = ['ndbm']
  1033. elif self.compiler.find_library_file(lib_dirs,
  1034. 'gdbm_compat'):
  1035. ndbm_libs = ['gdbm_compat']
  1036. else:
  1037. ndbm_libs = []
  1038. print "building dbm using ndbm"
  1039. dbmext = Extension('dbm', ['dbmmodule.c'],
  1040. define_macros=[
  1041. ('HAVE_NDBM_H',None),
  1042. ],
  1043. libraries=ndbm_libs)
  1044. break
  1045. elif cand == "gdbm":
  1046. if self.compiler.find_library_file(lib_dirs, 'gdbm'):
  1047. gdbm_libs = ['gdbm']
  1048. if self.compiler.find_library_file(lib_dirs,
  1049. 'gdbm_compat'):
  1050. gdbm_libs.append('gdbm_compat')
  1051. if find_file("gdbm/ndbm.h", inc_dirs, []) is not None:
  1052. print "building dbm using gdbm"
  1053. dbmext = Extension(
  1054. 'dbm', ['dbmmodule.c'],
  1055. define_macros=[
  1056. ('HAVE_GDBM_NDBM_H', None),
  1057. ],
  1058. libraries = gdbm_libs)
  1059. break
  1060. if find_file("gdbm-ndbm.h", inc_dirs, []) is not None:
  1061. print "building dbm using gdbm"
  1062. dbmext = Extension(
  1063. 'dbm', ['dbmmodule.c'],
  1064. define_macros=[
  1065. ('HAVE_GDBM_DASH_NDBM_H', None),
  1066. ],
  1067. libraries = gdbm_libs)
  1068. break
  1069. elif cand == "bdb":
  1070. if db_incs is not None:
  1071. print "building dbm using bdb"
  1072. dbmext = Extension('dbm', ['dbmmodule.c'],
  1073. library_dirs=dblib_dir,
  1074. runtime_library_dirs=dblib_dir,
  1075. include_dirs=db_incs,
  1076. define_macros=[
  1077. ('HAVE_BERKDB_H', None),
  1078. ('DB_DBM_HSEARCH', None),
  1079. ],
  1080. libraries=dblibs)
  1081. break
  1082. if dbmext is not None:
  1083. exts.append(dbmext)
  1084. else:
  1085. missing.append('dbm')
  1086. # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
  1087. if ('gdbm' in dbm_order and
  1088. self.compiler.find_library_file(lib_dirs, 'gdbm')):
  1089. exts.append( Extension('gdbm', ['gdbmmodule.c'],
  1090. libraries = ['gdbm'] ) )
  1091. else:
  1092. missing.append('gdbm')
  1093. # Unix-only modules
  1094. if platform not in ['win32']:
  1095. # Steen Lumholt's termios module
  1096. exts.append( Extension('termios', ['termios.c']) )
  1097. # Jeremy Hylton's rlimit interface
  1098. if platform not in ['atheos']:
  1099. exts.append( Extension('resource', ['resource.c']) )
  1100. else:
  1101. missing.append('resource')
  1102. # Sun yellow pages. Some systems have the functions in libc.
  1103. if (platform not in ['cygwin', 'atheos', 'qnx6'] and
  1104. find_file('rpcsvc/yp_prot.h', inc_dirs, []) is not None):
  1105. if (self.compiler.find_library_file(lib_dirs, 'nsl')):
  1106. libs = ['nsl']
  1107. else:
  1108. libs = []
  1109. exts.append( Extension('nis', ['nismodule.c'],
  1110. libraries = libs) )
  1111. else:
  1112. missing.append('nis')
  1113. else:
  1114. missing.extend(['nis', 'resource', 'termios'])
  1115. # Curses support, requiring the System V version of curses, often
  1116. # provided by the ncurses library.
  1117. panel_library = 'panel'
  1118. if curses_library.startswith('ncurses'):
  1119. if curses_library == 'ncursesw':
  1120. # Bug 1464056: If _curses.so links with ncursesw,
  1121. # _curses_panel.so must link with panelw.
  1122. panel_library = 'panelw'
  1123. curses_libs = [curses_library]
  1124. exts.append( Extension('_curses', ['_cursesmodule.c'],
  1125. libraries = curses_libs) )
  1126. elif curses_library == 'curses' and platform != 'darwin':
  1127. # OSX has an old Berkeley curses, not good enough for
  1128. # the _curses module.
  1129. if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
  1130. curses_libs = ['curses', 'terminfo']
  1131. elif (self.compiler.find_library_file(lib_dirs, 'termcap')):
  1132. curses_libs = ['curses', 'termcap']
  1133. else:
  1134. curses_libs = ['curses']
  1135. exts.append( Extension('_curses', ['_cursesmodule.c'],
  1136. libraries = curses_libs) )
  1137. else:
  1138. missing.append('_curses')
  1139. # If the curses module is enabled, check for the panel module
  1140. if (module_enabled(exts, '_curses') and
  1141. self.compiler.find_library_file(lib_dirs, panel_library)):
  1142. exts.append( Extension('_curses_panel', ['_curses_panel.c'],
  1143. libraries = [panel_library] + curses_libs) )
  1144. else:
  1145. missing.append('_curses_panel')
  1146. # Andrew Kuchling's zlib module. Note that some versions of zlib
  1147. # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
  1148. # http://www.cert.org/advisories/CA-2002-07.html
  1149. #
  1150. # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
  1151. # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
  1152. # now, we still accept 1.1.3, because we think it's difficult to
  1153. # exploit this in Python, and we'd rather make it RedHat's problem
  1154. # than our problem <wink>.
  1155. #
  1156. # You can upgrade zlib to version 1.1.4 yourself by going to
  1157. # http://www.gzip.org/zlib/
  1158. zlib_inc = find_file('zlib.h', [], inc_dirs)
  1159. have_zlib = False
  1160. if zlib_inc is not None:
  1161. zlib_h = zlib_inc[0] + '/zlib.h'
  1162. version = '"0.0.0"'
  1163. version_req = '"1.1.3"'
  1164. fp = open(zlib_h)
  1165. while 1:
  1166. line = fp.readline()
  1167. if not line:
  1168. break
  1169. if line.startswith('#define ZLIB_VERSION'):
  1170. version = line.split()[2]
  1171. break
  1172. if version >= version_req:
  1173. if (self.compiler.find_library_file(lib_dirs, 'z')):
  1174. if sys.platform == "darwin":
  1175. zlib_extra_link_args = ('-Wl,-search_paths_first',)
  1176. else:
  1177. zlib_extra_link_args = ()
  1178. exts.append( Extension('zlib', ['zlibmodule.c'],
  1179. libraries = ['z'],
  1180. extra_link_args = zlib_extra_link_args))
  1181. have_zlib = True
  1182. else:
  1183. missing.append('zlib')
  1184. else:
  1185. missing.append('zlib')
  1186. else:
  1187. missing.append('zlib')
  1188. # Helper module for various ascii-encoders. Uses zlib for an optimized
  1189. # crc32 if we have it. Otherwise binascii uses its own.
  1190. if have_zlib:
  1191. extra_compile_args = ['-DUSE_ZLIB_CRC32']
  1192. libraries = ['z']
  1193. extra_link_args = zlib_extra_link_args
  1194. else:
  1195. extra_compile_args = []
  1196. libraries = []
  1197. extra_link_args = []
  1198. exts.append( Extension('binascii', ['binascii.c'],
  1199. extra_compile_args = extra_compile_args,
  1200. libraries = libraries,
  1201. extra_link_args = extra_link_args) )
  1202. # Gustavo Niemeyer's bz2 module.
  1203. if (self.compiler.find_library_file(lib_dirs, 'bz2')):
  1204. if sys.platform == "darwin":
  1205. bz2_extra_link_args = ('-Wl,-search_paths_first',)
  1206. else:
  1207. bz2_extra_link_args = ()
  1208. exts.append( Extension('bz2', ['bz2module.c'],
  1209. libraries = ['bz2'],
  1210. extra_link_args = bz2_extra_link_args) )
  1211. else:
  1212. missing.append('bz2')
  1213. # Interface to the Expat XML parser
  1214. #
  1215. # Expat was written by James Clark and is now maintained by a group of
  1216. # developers on SourceForge; see www.libexpat.org for more information.
  1217. # The pyexpat module was written by Paul Prescod after a prototype by
  1218. # Jack Jansen. The Expat source is included in Modules/expat/. Usage
  1219. # of a system shared libexpat.so is possible with --with-system-expat
  1220. # configure option.
  1221. #
  1222. # More information on Expat can be found at www.libexpat.org.
  1223. #
  1224. if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
  1225. expat_inc = []
  1226. define_macros = []
  1227. expat_lib = ['expat']
  1228. expat_sources = []
  1229. else:
  1230. expat_inc = [os.path.join(os.getcwd(), srcdir, 'Modules', 'expat')]
  1231. define_macros = [
  1232. ('HAVE_EXPAT_CONFIG_H', '1'),
  1233. ]
  1234. expat_lib = []
  1235. expat_sources = ['expat/xmlparse.c',
  1236. 'expat/xmlrole.c',
  1237. 'expat/xmltok.c']
  1238. exts.append(Extension('pyexpat',
  1239. define_macros = define_macros,
  1240. include_dirs = expat_inc,
  1241. libraries = expat_lib,
  1242. sources = ['pyexpat.c'] + expat_sources
  1243. ))
  1244. # Fredrik Lundh's cElementTree module. Note that this also
  1245. # uses expat (via the CAPI hook in pyexpat).
  1246. if os.path.isfile(os.path.join(srcdir, 'Modules', '_elementtree.c')):
  1247. define_macros.append(('USE_PYEXPAT_CAPI', None))
  1248. exts.append(Extension('_elementtree',
  1249. define_macros = define_macros,
  1250. include_dirs = expat_inc,
  1251. libraries = expat_lib,
  1252. sources = ['_elementtree.c'],
  1253. ))
  1254. else:
  1255. missing.append('_elementtree')
  1256. # Hye-Shik Chang's CJKCodecs modules.
  1257. if have_unicode:
  1258. exts.append(Extension('_multibytecodec',
  1259. ['cjkcodecs/multibytecodec.c']))
  1260. for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
  1261. exts.append(Extension('_codecs_%s' % loc,
  1262. ['cjkcodecs/_codecs_%s.c' % loc]))
  1263. else:
  1264. missing.append('_multibytecodec')
  1265. for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
  1266. missing.append('_codecs_%s' % loc)
  1267. # Dynamic loading module
  1268. if sys.maxint == 0x7fffffff:
  1269. # This requires sizeof(int) == sizeof(long) == sizeof(char*)
  1270. dl_inc = find_file('dlfcn.h', [], inc_dirs)
  1271. if (dl_inc is not None) and (platform not in ['atheos']):
  1272. exts.append( Extension('dl', ['dlmodule.c']) )
  1273. else:
  1274. missing.append('dl')
  1275. else:
  1276. missing.append('dl')
  1277. # Thomas Heller's _ctypes module
  1278. self.detect_ctypes(inc_dirs, lib_dirs)
  1279. # Richard Oudkerk's multiprocessing module
  1280. if platform == 'win32': # Windows
  1281. macros = dict()
  1282. libraries = ['ws2_32']
  1283. elif platform == 'darwin': # Mac OSX
  1284. macros = dict()
  1285. libraries = []
  1286. elif platform == 'cygwin': # Cygwin
  1287. macros = dict()
  1288. libraries = []
  1289. elif platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
  1290. # FreeBSD's P1003.1b semaphore support is very experimental
  1291. # and has many known problems. (as of June 2008)
  1292. macros = dict()
  1293. libraries = []
  1294. elif platform.startswith('openbsd'):
  1295. macros = dict()
  1296. libraries = []
  1297. elif platform.startswith('netbsd'):
  1298. macros = dict()
  1299. libraries = []
  1300. else: # Linux and other unices
  1301. macros = dict()
  1302. libraries = ['rt']
  1303. if platform == 'win32':
  1304. multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
  1305. '_multiprocessing/semaphore.c',
  1306. '_multiprocessing/pipe_connection.c',
  1307. '_multiprocessing/socket_connection.c',
  1308. '_multiprocessing/win32_functions.c'
  1309. ]
  1310. else:
  1311. multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
  1312. '_multiprocessing/socket_connection.c'
  1313. ]
  1314. if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
  1315. sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
  1316. multiprocessing_srcs.append('_multiprocessing/semaphore.c')
  1317. if sysconfig.get_config_var('WITH_THREAD'):
  1318. exts.append ( Extension('_multiprocessing', multiprocessing_srcs,
  1319. define_macros=macros.items(),
  1320. include_dirs=["Modules/_multiprocessing"]))
  1321. else:
  1322. missing.append('_multiprocessing')
  1323. # End multiprocessing
  1324. # Platform-specific libraries
  1325. if platform == 'linux2':
  1326. # Linux-specific modules
  1327. exts.append( Extension('linuxaudiodev', ['linuxaudiodev.c']) )
  1328. else:
  1329. missing.append('linuxaudiodev')
  1330. if (platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
  1331. 'freebsd7', 'freebsd8')
  1332. or platform.startswith("gnukfreebsd")):
  1333. exts.append( Extension('ossaudiodev', ['ossaudiodev.c']) )
  1334. else:
  1335. missing.append('ossaudiodev')
  1336. if platform == 'sunos5':
  1337. # SunOS specific modules
  1338. exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
  1339. else:
  1340. missing.append('sunaudiodev')
  1341. if platform == 'darwin':
  1342. # _scproxy
  1343. exts.append(Extension("_scproxy", [os.path.join(srcdir, "Mac/Modules/_scproxy.c")],
  1344. extra_link_args= [
  1345. '-framework', 'SystemConfiguration',
  1346. '-framework', 'CoreFoundation'
  1347. ]))
  1348. if platform == 'darwin' and ("--disable-toolbox-glue" not in
  1349. sysconfig.get_config_var("CONFIG_ARGS")):
  1350. if int(os.uname()[2].split('.')[0]) >= 8:
  1351. # We're on Mac OS X 10.4 or later, the compiler should
  1352. # support '-Wno-deprecated-declarations'. This will
  1353. # surpress deprecation warnings for the Carbon extensions,
  1354. # these extensions wrap the Carbon APIs and even those
  1355. # parts that are deprecated.
  1356. carbon_extra_compile_args = ['-Wno-deprecated-declarations']
  1357. else:
  1358. carbon_extra_compile_args = []
  1359. # Mac OS X specific modules.
  1360. def macSrcExists(name1, name2=''):
  1361. if not name1:
  1362. return None
  1363. names = (name1,)
  1364. if name2:
  1365. names = (name1, name2)
  1366. path = os.path.join(srcdir, 'Mac', 'Modules', *names)
  1367. return os.path.exists(path)
  1368. def addMacExtension(name, kwds, extra_srcs=[]):
  1369. dirname = ''
  1370. if name[0] == '_':
  1371. dirname = name[1:].lower()
  1372. cname = name + '.c'
  1373. cmodulename = name + 'module.c'
  1374. # Check for NNN.c, NNNmodule.c, _nnn/NNN.c, _nnn/NNNmodule.c
  1375. if macSrcExists(cname):
  1376. srcs = [cname]
  1377. elif macSrcExists(cmodulename):
  1378. srcs = [cmodulename]
  1379. elif macSrcExists(dirname, cname):
  1380. # XXX(nnorwitz): If all the names ended with module, we
  1381. # wouldn't need this condition. ibcarbon is the only one.
  1382. srcs = [os.path.join(dirname, cname)]
  1383. elif macSrcExists(dirname, cmodulename):
  1384. srcs = [os.path.join(dirname, cmodulename)]
  1385. else:
  1386. raise RuntimeError("%s not found" % name)
  1387. # Here's the whole point: add the extension with sources
  1388. exts.append(Extension(name, srcs + extra_srcs, **kwds))
  1389. # Core Foundation
  1390. core_kwds = {'extra_compile_args': carbon_extra_compile_args,
  1391. 'extra_link_args': ['-framework', 'CoreFoundation'],
  1392. }
  1393. addMacExtension('_CF', core_kwds, ['cf/pycfbridge.c'])
  1394. addMacExtension('autoGIL', core_kwds)
  1395. # Carbon
  1396. carbon_kwds = {'extra_compile_args': carbon_extra_compile_args,
  1397. 'extra_link_args': ['-framework', 'Carbon'],
  1398. }
  1399. CARBON_EXTS = ['ColorPicker', 'gestalt', 'MacOS', 'Nav',
  1400. 'OSATerminology', 'icglue',
  1401. # All these are in subdirs
  1402. '_AE', '_AH', '_App', '_CarbonEvt', '_Cm', '_Ctl',
  1403. '_Dlg', '_Drag', '_Evt', '_File', '_Folder', '_Fm',
  1404. '_Help', '_Icn', '_IBCarbon', '_List',
  1405. '_Menu', '_Mlte', '_OSA', '_Res', '_Qd', '_Qdoffs',
  1406. '_Scrap', '_Snd', '_TE',
  1407. ]
  1408. for name in CARBON_EXTS:
  1409. addMacExtension(name, carbon_kwds)
  1410. # Workaround for a bug in the version of gcc shipped with Xcode 3.
  1411. # The _Win extension should build just like the other Carbon extensions, but
  1412. # this actually results in a hard crash of the linker.
  1413. #
  1414. if '-arch ppc64' in cflags and '-arch ppc' in cflags:
  1415. win_kwds = {'extra_compile_args': carbon_extra_compile_args + ['-arch', 'i386', '-arch', 'ppc'],
  1416. 'extra_link_args': ['-framework', 'Carbon', '-arch', 'i386', '-arch', 'ppc'],
  1417. }
  1418. addMacExtension('_Win', win_kwds)
  1419. else:
  1420. addMacExtension('_Win', carbon_kwds)
  1421. # Application Services & QuickTime
  1422. app_kwds = {'extra_compile_args': carbon_extra_compile_args,
  1423. 'extra_link_args': ['-framework','ApplicationServices'],
  1424. }
  1425. addMacExtension('_Launch', app_kwds)
  1426. addMacExtension('_CG', app_kwds)
  1427. exts.append( Extension('_Qt', ['qt/_Qtmodule.c'],
  1428. extra_compile_args=carbon_extra_compile_args,
  1429. extra_link_args=['-framework', 'QuickTime',
  1430. '-framework', 'Carbon']) )
  1431. self.extensions.extend(exts)
  1432. # Call the method for detecting whether _tkinter can be compiled
  1433. self.detect_tkinter(inc_dirs, lib_dirs)
  1434. if '_tkinter' not in [e.name for e in self.extensions]:
  1435. missing.append('_tkinter')
  1436. return missing
  1437. def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
  1438. # The _tkinter module, using frameworks. Since frameworks are quite
  1439. # different the UNIX search logic is not sharable.
  1440. from os.path import join, exists
  1441. framework_dirs = [
  1442. '/Library/Frameworks',
  1443. '/System/Library/Frameworks/',
  1444. join(os.getenv('HOME'), '/Library/Frameworks')
  1445. ]
  1446. sysroot = macosx_sdk_root()
  1447. # Find the directory that contains the Tcl.framework and Tk.framework
  1448. # bundles.
  1449. # XXX distutils should support -F!
  1450. for F in framework_dirs:
  1451. # both Tcl.framework and Tk.framework should be present
  1452. for fw in 'Tcl', 'Tk':
  1453. if is_macosx_sdk_path(F):
  1454. if not exists(join(sysroot, F[1:], fw + '.framework')):
  1455. break
  1456. else:
  1457. if not exists(join(F, fw + '.framework')):
  1458. break
  1459. else:
  1460. # ok, F is now directory with both frameworks. Continure
  1461. # building
  1462. break
  1463. else:
  1464. # Tk and Tcl frameworks not found. Normal "unix" tkinter search
  1465. # will now resume.
  1466. return 0
  1467. # For 8.4a2, we must add -I options that point inside the Tcl and Tk
  1468. # frameworks. In later release we should hopefully be able to pass
  1469. # the -F option to gcc, which specifies a framework lookup path.
  1470. #
  1471. include_dirs = [
  1472. join(F, fw + '.framework', H)
  1473. for fw in 'Tcl', 'Tk'
  1474. for H in 'Headers', 'Versions/Current/PrivateHeaders'
  1475. ]
  1476. # For 8.4a2, the X11 headers are not included. Rather than include a
  1477. # complicated search, this is a hard-coded path. It could bail out
  1478. # if X11 libs are not found...
  1479. include_dirs.append('/usr/X11R6/include')
  1480. frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
  1481. # All existing framework builds of Tcl/Tk don't support 64-bit
  1482. # architectures.
  1483. cflags = sysconfig.get_config_vars('CFLAGS')[0]
  1484. archs = re.findall('-arch\s+(\w+)', cflags)
  1485. if is_macosx_sdk_path(F):
  1486. fp = os.popen("file %s/Tk.framework/Tk | grep 'for architecture'"%(os.path.join(sysroot, F[1:]),))
  1487. else:
  1488. fp = os.popen("file %s/Tk.framework/Tk | grep 'for architecture'"%(F,))
  1489. detected_archs = []
  1490. for ln in fp:
  1491. a = ln.split()[-1]
  1492. if a in archs:
  1493. detected_archs.append(ln.split()[-1])
  1494. fp.close()
  1495. for a in detected_archs:
  1496. frameworks.append('-arch')
  1497. frameworks.append(a)
  1498. ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
  1499. define_macros=[('WITH_APPINIT', 1)],
  1500. include_dirs = include_dirs,
  1501. libraries = [],
  1502. extra_compile_args = frameworks[2:],
  1503. extra_link_args = frameworks,
  1504. )
  1505. self.extensions.append(ext)
  1506. return 1
  1507. def detect_tkinter(self, inc_dirs, lib_dirs):
  1508. # The _tkinter module.
  1509. # Rather than complicate the code below, detecting and building
  1510. # AquaTk is a separate method. Only one Tkinter will be built on
  1511. # Darwin - either AquaTk, if it is found, or X11 based Tk.
  1512. platform = self.get_platform()
  1513. if (platform == 'darwin' and
  1514. self.detect_tkinter_darwin(inc_dirs, lib_dirs)):
  1515. return
  1516. # Assume we haven't found any of the libraries or include files
  1517. # The versions with dots are used on Unix, and the versions without
  1518. # dots on Windows, for detection by cygwin.
  1519. tcllib = tklib = tcl_includes = tk_includes = None
  1520. for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
  1521. '8.2', '82', '8.1', '81', '8.0', '80']:
  1522. tklib = self.compiler.find_library_file(lib_dirs,
  1523. 'tk' + version)
  1524. tcllib = self.compiler.find_library_file(lib_dirs,
  1525. 'tcl' + version)
  1526. if tklib and tcllib:
  1527. # Exit the loop when we've found the Tcl/Tk libraries
  1528. break
  1529. # Now check for the header files
  1530. if tklib and tcllib:
  1531. # Check for the include files on Debian and {Free,Open}BSD, where
  1532. # they're put in /usr/include/{tcl,tk}X.Y
  1533. dotversion = version
  1534. if '.' not in dotversion and "bsd" in sys.platform.lower():
  1535. # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
  1536. # but the include subdirs are named like .../include/tcl8.3.
  1537. dotversion = dotversion[:-1] + '.' + dotversion[-1]
  1538. tcl_include_sub = []
  1539. tk_include_sub = []
  1540. for dir in inc_dirs:
  1541. tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
  1542. tk_include_sub += [dir + os.sep + "tk" + dotversion]
  1543. tk_include_sub += tcl_include_sub
  1544. tcl_includes = find_file('tcl.h', inc_dirs, tcl_include_sub)
  1545. tk_includes = find_file('tk.h', inc_dirs, tk_include_sub)
  1546. if (tcllib is None or tklib is None or
  1547. tcl_includes is None or tk_includes is None):
  1548. self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
  1549. return
  1550. # OK... everything seems to be present for Tcl/Tk.
  1551. include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
  1552. for dir in tcl_includes + tk_includes:
  1553. if dir not in include_dirs:
  1554. include_dirs.append(dir)
  1555. # Check for various platform-specific directories
  1556. if platform == 'sunos5':
  1557. include_dirs.append('/usr/openwin/include')
  1558. added_lib_dirs.append('/usr/openwin/lib')
  1559. elif os.path.exists('/usr/X11R6/include'):
  1560. include_dirs.append('/usr/X11R6/include')
  1561. added_lib_dirs.append('/usr/X11R6/lib64')
  1562. added_lib_dirs.append('/usr/X11R6/lib')
  1563. elif os.path.exists('/usr/X11R5/include'):
  1564. include_dirs.append('/usr/X11R5/include')
  1565. added_lib_dirs.append('/usr/X11R5/lib')
  1566. else:
  1567. # Assume default location for X11
  1568. include_dirs.append('/usr/X11/include')
  1569. added_lib_dirs.append('/usr/X11/lib')
  1570. # If Cygwin, then verify that X is installed before proceeding
  1571. if platform == 'cygwin':
  1572. x11_inc = find_file('X11/Xlib.h', [], include_dirs)
  1573. if x11_inc is None:
  1574. return
  1575. # Check for BLT extension
  1576. if self.compiler.find_library_file(lib_dirs + added_lib_dirs,
  1577. 'BLT8.0'):
  1578. defs.append( ('WITH_BLT', 1) )
  1579. libs.append('BLT8.0')
  1580. elif self.compiler.find_library_file(lib_dirs + added_lib_dirs,
  1581. 'BLT'):
  1582. defs.append( ('WITH_BLT', 1) )
  1583. libs.append('BLT')
  1584. # Add the Tcl/Tk libraries
  1585. libs.append('tk'+ version)
  1586. libs.append('tcl'+ version)
  1587. if platform in ['aix3', 'aix4']:
  1588. libs.append('ld')
  1589. # Finally, link with the X11 libraries (not appropriate on cygwin)
  1590. if platform != "cygwin":
  1591. libs.append('X11')
  1592. ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
  1593. define_macros=[('WITH_APPINIT', 1)] + defs,
  1594. include_dirs = include_dirs,
  1595. libraries = libs,
  1596. library_dirs = added_lib_dirs,
  1597. )
  1598. self.extensions.append(ext)
  1599. ## # Uncomment these lines if you want to play with xxmodule.c
  1600. ## ext = Extension('xx', ['xxmodule.c'])
  1601. ## self.extensions.append(ext)
  1602. # XXX handle these, but how to detect?
  1603. # *** Uncomment and edit for PIL (TkImaging) extension only:
  1604. # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
  1605. # *** Uncomment and edit for TOGL extension only:
  1606. # -DWITH_TOGL togl.c \
  1607. # *** Uncomment these for TOGL extension only:
  1608. # -lGL -lGLU -lXext -lXmu \
  1609. def configure_ctypes_darwin(self, ext):
  1610. # Darwin (OS X) uses preconfigured files, in
  1611. # the Modules/_ctypes/libffi_osx directory.
  1612. srcdir = sysconfig.get_config_var('srcdir')
  1613. ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
  1614. '_ctypes', 'libffi_osx'))
  1615. sources = [os.path.join(ffi_srcdir, p)
  1616. for p in ['ffi.c',
  1617. 'x86/darwin64.S',
  1618. 'x86/x86-darwin.S',
  1619. 'x86/x86-ffi_darwin.c',
  1620. 'x86/x86-ffi64.c',
  1621. 'powerpc/ppc-darwin.S',
  1622. 'powerpc/ppc-darwin_closure.S',
  1623. 'powerpc/ppc-ffi_darwin.c',
  1624. 'powerpc/ppc64-darwin_closure.S',
  1625. ]]
  1626. # Add .S (preprocessed assembly) to C compiler source extensions.
  1627. self.compiler.src_extensions.append('.S')
  1628. include_dirs = [os.path.join(ffi_srcdir, 'include'),
  1629. os.path.join(ffi_srcdir, 'powerpc')]
  1630. ext.include_dirs.extend(include_dirs)
  1631. ext.sources.extend(sources)
  1632. return True
  1633. def configure_ctypes(self, ext):
  1634. if not self.use_system_libffi:
  1635. if sys.platform == 'darwin':
  1636. return self.configure_ctypes_darwin(ext)
  1637. srcdir = sysconfig.get_config_var('srcdir')
  1638. ffi_builddir = os.path.join(self.build_temp, 'libffi')
  1639. ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
  1640. '_ctypes', 'libffi'))
  1641. ffi_configfile = os.path.join(ffi_builddir, 'fficonfig.py')
  1642. from distutils.dep_util import newer_group
  1643. config_sources = [os.path.join(ffi_srcdir, fname)
  1644. for fname in os.listdir(ffi_srcdir)
  1645. if os.path.isfile(os.path.join(ffi_srcdir, fname))]
  1646. if self.force or newer_group(config_sources,
  1647. ffi_configfile):
  1648. from distutils.dir_util import mkpath
  1649. mkpath(ffi_builddir)
  1650. config_args = []
  1651. if not self.verbose:
  1652. config_args.append("-q")
  1653. # Pass empty CFLAGS because we'll just append the resulting
  1654. # CFLAGS to Python's; -g or -O2 is to be avoided.
  1655. cmd = "cd %s && env CFLAGS='' '%s/configure' %s" \
  1656. % (ffi_builddir, ffi_srcdir, " ".join(config_args))
  1657. res = os.system(cmd)
  1658. if res or not os.path.exists(ffi_configfile):
  1659. print "Failed to configure _ctypes module"
  1660. return False
  1661. fficonfig = {}
  1662. with open(ffi_configfile) as f:
  1663. exec f in fficonfig
  1664. # Add .S (preprocessed assembly) to C compiler source extensions.
  1665. self.compiler.src_extensions.append('.S')
  1666. include_dirs = [os.path.join(ffi_builddir, 'include'),
  1667. ffi_builddir,
  1668. os.path.join(ffi_srcdir, 'src')]
  1669. extra_compile_args = fficonfig['ffi_cflags'].split()
  1670. ext.sources.extend(os.path.join(ffi_srcdir, f) for f in
  1671. fficonfig['ffi_sources'])
  1672. ext.include_dirs.extend(include_dirs)
  1673. ext.extra_compile_args.extend(extra_compile_args)
  1674. return True
  1675. def detect_ctypes(self, inc_dirs, lib_dirs):
  1676. self.use_system_libffi = False
  1677. include_dirs = []
  1678. extra_compile_args = []
  1679. extra_link_args = []
  1680. sources = ['_ctypes/_ctypes.c',
  1681. '_ctypes/callbacks.c',
  1682. '_ctypes/callproc.c',
  1683. '_ctypes/stgdict.c',
  1684. '_ctypes/cfield.c']
  1685. depends = ['_ctypes/ctypes.h']
  1686. if sys.platform == 'darwin':
  1687. sources.append('_ctypes/malloc_closure.c')
  1688. sources.append('_ctypes/darwin/dlfcn_simple.c')
  1689. extra_compile_args.append('-DMACOSX')
  1690. include_dirs.append('_ctypes/darwin')
  1691. # XXX Is this still needed?
  1692. ## extra_link_args.extend(['-read_only_relocs', 'warning'])
  1693. elif sys.platform == 'sunos5':
  1694. # XXX This shouldn't be necessary; it appears that some
  1695. # of the assembler code is non-PIC (i.e. it has relocations
  1696. # when it shouldn't. The proper fix would be to rewrite
  1697. # the assembler code to be PIC.
  1698. # This only works with GCC; the Sun compiler likely refuses
  1699. # this option. If you want to compile ctypes with the Sun
  1700. # compiler, please research a proper solution, instead of
  1701. # finding some -z option for the Sun compiler.
  1702. extra_link_args.append('-mimpure-text')
  1703. elif sys.platform.startswith('hp-ux'):
  1704. extra_link_args.append('-fPIC')
  1705. ext = Extension('_ctypes',
  1706. include_dirs=include_dirs,
  1707. extra_compile_args=extra_compile_args,
  1708. extra_link_args=extra_link_args,
  1709. libraries=[],
  1710. sources=sources,
  1711. depends=depends)
  1712. ext_test = Extension('_ctypes_test',
  1713. sources=['_ctypes/_ctypes_test.c'])
  1714. self.extensions.extend([ext, ext_test])
  1715. if not '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS"):
  1716. return
  1717. if sys.platform == 'darwin':
  1718. # OS X 10.5 comes with libffi.dylib; the include files are
  1719. # in /usr/include/ffi
  1720. inc_dirs.append('/usr/include/ffi')
  1721. ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
  1722. if not ffi_inc or ffi_inc[0] == '':
  1723. ffi_inc = find_file('ffi.h', [], inc_dirs)
  1724. if ffi_inc is not None:
  1725. ffi_h = ffi_inc[0] + '/ffi.h'
  1726. fp = open(ffi_h)
  1727. while 1:
  1728. line = fp.readline()
  1729. if not line:
  1730. ffi_inc = None
  1731. break
  1732. if line.startswith('#define LIBFFI_H'):
  1733. break
  1734. ffi_lib = None
  1735. if ffi_inc is not None:
  1736. for lib_name in ('ffi_convenience', 'ffi_pic', 'ffi'):
  1737. if (self.compiler.find_library_file(lib_dirs, lib_name)):
  1738. ffi_lib = lib_name
  1739. break
  1740. if ffi_inc and ffi_lib:
  1741. ext.include_dirs.extend(ffi_inc)
  1742. ext.libraries.append(ffi_lib)
  1743. self.use_system_libffi = True
  1744. class PyBuildInstall(install):
  1745. # Suppress the warning about installation into the lib_dynload
  1746. # directory, which is not in sys.path when running Python during
  1747. # installation:
  1748. def initialize_options (self):
  1749. install.initialize_options(self)
  1750. self.warn_dir=0
  1751. class PyBuildInstallLib(install_lib):
  1752. # Do exactly what install_lib does but make sure correct access modes get
  1753. # set on installed directories and files. All installed files with get
  1754. # mode 644 unless they are a shared library in which case they will get
  1755. # mode 755. All installed directories will get mode 755.
  1756. so_ext = sysconfig.get_config_var("SO")
  1757. def install(self):
  1758. outfiles = install_lib.install(self)
  1759. self.set_file_modes(outfiles, 0644, 0755)
  1760. self.set_dir_modes(self.install_dir, 0755)
  1761. return outfiles
  1762. def set_file_modes(self, files, defaultMode, sharedLibMode):
  1763. if not self.is_chmod_supported(): return
  1764. if not files: return
  1765. for filename in files:
  1766. if os.path.islink(filename): continue
  1767. mode = defaultMode
  1768. if filename.endswith(self.so_ext): mode = sharedLibMode
  1769. log.info("changing mode of %s to %o", filename, mode)
  1770. if not self.dry_run: os.chmod(filename, mode)
  1771. def set_dir_modes(self, dirname, mode):
  1772. if not self.is_chmod_supported(): return
  1773. os.path.walk(dirname, self.set_dir_modes_visitor, mode)
  1774. def set_dir_modes_visitor(self, mode, dirname, names):
  1775. if os.path.islink(dirname): return
  1776. log.info("changing mode of %s to %o", dirname, mode)
  1777. if not self.dry_run: os.chmod(dirname, mode)
  1778. def is_chmod_supported(self):
  1779. return hasattr(os, 'chmod')
  1780. SUMMARY = """
  1781. Python is an interpreted, interactive, object-oriented programming
  1782. language. It is often compared to Tcl, Perl, Scheme or Java.
  1783. Python combines remarkable power with very clear syntax. It has
  1784. modules, classes, exceptions, very high level dynamic data types, and
  1785. dynamic typing. There are interfaces to many system calls and
  1786. libraries, as well as to various windowing systems (X11, Motif, Tk,
  1787. Mac, MFC). New built-in modules are easily written in C or C++. Python
  1788. is also usable as an extension language for applications that need a
  1789. programmable interface.
  1790. The Python implementation is portable: it runs on many brands of UNIX,
  1791. on Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't
  1792. listed here, it may still be supported, if there's a C compiler for
  1793. it. Ask around on comp.lang.python -- or just try compiling Python
  1794. yourself.
  1795. """
  1796. CLASSIFIERS = """
  1797. Development Status :: 6 - Mature
  1798. License :: OSI Approved :: Python Software Foundation License
  1799. Natural Language :: English
  1800. Programming Language :: C
  1801. Programming Language :: Python
  1802. Topic :: Software Development
  1803. """
  1804. def main():
  1805. # turn off warnings when deprecated modules are imported
  1806. import warnings
  1807. warnings.filterwarnings("ignore",category=DeprecationWarning)
  1808. setup(# PyPI Metadata (PEP 301)
  1809. name = "Python",
  1810. version = sys.version.split()[0],
  1811. url = "http://www.python.org/%s" % sys.version[:3],
  1812. maintainer = "Guido van Rossum and the Python community",
  1813. maintainer_email = "python-dev@python.org",
  1814. description = "A high-level object-oriented programming language",
  1815. long_description = SUMMARY.strip(),
  1816. license = "PSF license",
  1817. classifiers = filter(None, CLASSIFIERS.split("\n")),
  1818. platforms = ["Many"],
  1819. # Build info
  1820. cmdclass = {'build_ext':PyBuildExt, 'install':PyBuildInstall,
  1821. 'install_lib':PyBuildInstallLib},
  1822. # The struct module is defined here, because build_ext won't be
  1823. # called unless there's at least one extension module defined.
  1824. ext_modules=[Extension('_struct', ['_struct.c'])],
  1825. # Scripts to install
  1826. scripts = ['Tools/scripts/pydoc', 'Tools/scripts/idle',
  1827. 'Tools/scripts/2to3',
  1828. 'Lib/smtpd.py']
  1829. )
  1830. # --install-platlib
  1831. if __name__ == '__main__':
  1832. main()