Merged revisions 65810 via svnmerge from
[python/dscho.git] / setup.py
blobf0fed72b8cb1293ebb3c5fc3420fc17f740d2932
1 # Autodetecting setup.py script for building the Python extensions
4 __version__ = "$Revision$"
6 import sys, os, imp, re, optparse
7 from glob import glob
9 from distutils import log
10 from distutils import sysconfig
11 from distutils import text_file
12 from distutils.errors import *
13 from distutils.core import Extension, setup
14 from distutils.command.build_ext import build_ext
15 from distutils.command.install import install
16 from distutils.command.install_lib import install_lib
18 # This global variable is used to hold the list of modules to be disabled.
19 disabled_module_list = []
21 def add_dir_to_list(dirlist, dir):
22 """Add the directory 'dir' to the list 'dirlist' (at the front) if
23 1) 'dir' is not already in 'dirlist'
24 2) 'dir' actually exists, and is a directory."""
25 if dir is not None and os.path.isdir(dir) and dir not in dirlist:
26 dirlist.insert(0, dir)
28 def find_file(filename, std_dirs, paths):
29 """Searches for the directory where a given file is located,
30 and returns a possibly-empty list of additional directories, or None
31 if the file couldn't be found at all.
33 'filename' is the name of a file, such as readline.h or libcrypto.a.
34 'std_dirs' is the list of standard system directories; if the
35 file is found in one of them, no additional directives are needed.
36 'paths' is a list of additional locations to check; if the file is
37 found in one of them, the resulting list will contain the directory.
38 """
40 # Check the standard locations
41 for dir in std_dirs:
42 f = os.path.join(dir, filename)
43 if os.path.exists(f): return []
45 # Check the additional directories
46 for dir in paths:
47 f = os.path.join(dir, filename)
48 if os.path.exists(f):
49 return [dir]
51 # Not found anywhere
52 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
59 # Check whether the found file is in one of the standard directories
60 dirname = os.path.dirname(result)
61 for p in std_dirs:
62 # Ensure path doesn't end with path separator
63 p = p.rstrip(os.sep)
64 if p == dirname:
65 return [ ]
67 # Otherwise, it must have been in one of the additional directories,
68 # so we have to figure out which one.
69 for p in paths:
70 # Ensure path doesn't end with path separator
71 p = p.rstrip(os.sep)
72 if p == dirname:
73 return [p]
74 else:
75 assert False, "Internal error: Path not found in std_dirs or paths"
77 def module_enabled(extlist, modname):
78 """Returns whether the module 'modname' is present in the list
79 of extensions 'extlist'."""
80 extlist = [ext for ext in extlist if ext.name == modname]
81 return len(extlist)
83 def find_module_file(module, dirlist):
84 """Find a module in a set of possible folders. If it is not found
85 return the unadorned filename"""
86 list = find_file(module, [], dirlist)
87 if not list:
88 return module
89 if len(list) > 1:
90 log.info("WARNING: multiple copies of %s found"%module)
91 return os.path.join(list[0], module)
93 class PyBuildExt(build_ext):
95 def __init__(self, dist):
96 build_ext.__init__(self, dist)
97 self.failed = []
99 def build_extensions(self):
101 # Detect which modules should be compiled
102 missing = self.detect_modules()
104 # Remove modules that are present on the disabled list
105 extensions = [ext for ext in self.extensions
106 if ext.name not in disabled_module_list]
107 # move ctypes to the end, it depends on other modules
108 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
109 if "_ctypes" in ext_map:
110 ctypes = extensions.pop(ext_map["_ctypes"])
111 extensions.append(ctypes)
112 self.extensions = extensions
114 # Fix up the autodetected modules, prefixing all the source files
115 # with Modules/ and adding Python's include directory to the path.
116 (srcdir,) = sysconfig.get_config_vars('srcdir')
117 if not srcdir:
118 # Maybe running on Windows but not using CYGWIN?
119 raise ValueError("No source directory; cannot proceed.")
121 # Figure out the location of the source code for extension modules
122 # (This logic is copied in distutils.test.test_sysconfig,
123 # so building in a separate directory does not break test_distutils.)
124 moddir = os.path.join(os.getcwd(), srcdir, 'Modules')
125 moddir = os.path.normpath(moddir)
126 srcdir, tail = os.path.split(moddir)
127 srcdir = os.path.normpath(srcdir)
128 moddir = os.path.normpath(moddir)
130 moddirlist = [moddir]
131 incdirlist = ['./Include']
133 # Platform-dependent module source and include directories
134 platform = self.get_platform()
136 alldirlist = moddirlist + incdirlist
138 # Fix up the paths for scripts, too
139 self.distribution.scripts = [os.path.join(srcdir, filename)
140 for filename in self.distribution.scripts]
142 # Python header files
143 headers = glob("Include/*.h") + ["pyconfig.h"]
145 for ext in self.extensions[:]:
146 ext.sources = [ find_module_file(filename, moddirlist)
147 for filename in ext.sources ]
148 if ext.depends is not None:
149 ext.depends = [find_module_file(filename, alldirlist)
150 for filename in ext.depends]
151 else:
152 ext.depends = []
153 # re-compile extensions if a header file has been changed
154 ext.depends.extend(headers)
156 ext.include_dirs.append( '.' ) # to get config.h
157 for incdir in incdirlist:
158 ext.include_dirs.append( os.path.join(srcdir, incdir) )
160 # If a module has already been built statically,
161 # don't build it here
162 if ext.name in sys.builtin_module_names:
163 self.extensions.remove(ext)
165 if platform != 'mac':
166 # Parse Modules/Setup and Modules/Setup.local to figure out which
167 # modules are turned on in the file.
168 remove_modules = []
169 for filename in ('Modules/Setup', 'Modules/Setup.local'):
170 input = text_file.TextFile(filename, join_lines=1)
171 while 1:
172 line = input.readline()
173 if not line: break
174 line = line.split()
175 remove_modules.append(line[0])
176 input.close()
178 for ext in self.extensions[:]:
179 if ext.name in remove_modules:
180 self.extensions.remove(ext)
182 # When you run "make CC=altcc" or something similar, you really want
183 # those environment variables passed into the setup.py phase. Here's
184 # a small set of useful ones.
185 compiler = os.environ.get('CC')
186 args = {}
187 # unfortunately, distutils doesn't let us provide separate C and C++
188 # compilers
189 if compiler is not None:
190 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
191 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
192 self.compiler.set_executables(**args)
194 build_ext.build_extensions(self)
196 longest = max([len(e.name) for e in self.extensions])
197 if self.failed:
198 longest = max(longest, max([len(name) for name in self.failed]))
200 def print_three_column(lst):
201 lst.sort(key=str.lower)
202 # guarantee zip() doesn't drop anything
203 while len(lst) % 3:
204 lst.append("")
205 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
206 print("%-*s %-*s %-*s" % (longest, e, longest, f,
207 longest, g))
209 if missing:
210 print()
211 print("Failed to find the necessary bits to build these modules:")
212 print_three_column(missing)
213 print("To find the necessary bits, look in setup.py in"
214 " detect_modules() for the module's name.")
215 print()
217 if self.failed:
218 failed = self.failed[:]
219 print()
220 print("Failed to build these modules:")
221 print_three_column(failed)
222 print()
224 def build_extension(self, ext):
226 if ext.name == '_ctypes':
227 if not self.configure_ctypes(ext):
228 return
230 try:
231 build_ext.build_extension(self, ext)
232 except (CCompilerError, DistutilsError) as why:
233 self.announce('WARNING: building of extension "%s" failed: %s' %
234 (ext.name, sys.exc_info()[1]))
235 self.failed.append(ext.name)
236 return
237 # Workaround for Mac OS X: The Carbon-based modules cannot be
238 # reliably imported into a command-line Python
239 if 'Carbon' in ext.extra_link_args:
240 self.announce(
241 'WARNING: skipping import check for Carbon-based "%s"' %
242 ext.name)
243 return
245 if self.get_platform() == 'darwin' and (
246 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
247 # Don't bother doing an import check when an extension was
248 # build with an explicit '-arch' flag on OSX. That's currently
249 # only used to build 32-bit only extensions in a 4-way
250 # universal build and loading 32-bit code into a 64-bit
251 # process will fail.
252 self.announce(
253 'WARNING: skipping import check for "%s"' %
254 ext.name)
255 return
257 # Workaround for Cygwin: Cygwin currently has fork issues when many
258 # modules have been imported
259 if self.get_platform() == 'cygwin':
260 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
261 % ext.name)
262 return
263 ext_filename = os.path.join(
264 self.build_lib,
265 self.get_ext_filename(self.get_ext_fullname(ext.name)))
267 # If the build directory didn't exist when setup.py was
268 # started, sys.path_importer_cache has a negative result
269 # cached. Clear that cache before trying to import.
270 sys.path_importer_cache.clear()
272 try:
273 imp.load_dynamic(ext.name, ext_filename)
274 except ImportError as why:
275 self.failed.append(ext.name)
276 self.announce('*** WARNING: renaming "%s" since importing it'
277 ' failed: %s' % (ext.name, why), level=3)
278 assert not self.inplace
279 basename, tail = os.path.splitext(ext_filename)
280 newname = basename + "_failed" + tail
281 if os.path.exists(newname):
282 os.remove(newname)
283 os.rename(ext_filename, newname)
285 # XXX -- This relies on a Vile HACK in
286 # distutils.command.build_ext.build_extension(). The
287 # _built_objects attribute is stored there strictly for
288 # use here.
289 # If there is a failure, _built_objects may not be there,
290 # so catch the AttributeError and move on.
291 try:
292 for filename in self._built_objects:
293 os.remove(filename)
294 except AttributeError:
295 self.announce('unable to remove files (ignored)')
296 except:
297 exc_type, why, tb = sys.exc_info()
298 self.announce('*** WARNING: importing extension "%s" '
299 'failed with %s: %s' % (ext.name, exc_type, why),
300 level=3)
301 self.failed.append(ext.name)
303 def get_platform(self):
304 # Get value of sys.platform
305 for platform in ['cygwin', 'darwin', 'atheos', 'osf1']:
306 if sys.platform.startswith(platform):
307 return platform
308 return sys.platform
310 def detect_modules(self):
311 # Ensure that /usr/local is always used
312 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
313 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
315 # Add paths specified in the environment variables LDFLAGS and
316 # CPPFLAGS for header and library files.
317 # We must get the values from the Makefile and not the environment
318 # directly since an inconsistently reproducible issue comes up where
319 # the environment variable is not set even though the value were passed
320 # into configure and stored in the Makefile (issue found on OS X 10.3).
321 for env_var, arg_name, dir_list in (
322 ('LDFLAGS', '-L', self.compiler.library_dirs),
323 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
324 env_val = sysconfig.get_config_var(env_var)
325 if env_val:
326 # To prevent optparse from raising an exception about any
327 # options in env_val that is doesn't know about we strip out
328 # all double dashes and any dashes followed by a character
329 # that is not for the option we are dealing with.
331 # Please note that order of the regex is important! We must
332 # strip out double-dashes first so that we don't end up with
333 # substituting "--Long" to "-Long" and thus lead to "ong" being
334 # used for a library directory.
335 env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
336 ' ', env_val)
337 parser = optparse.OptionParser()
338 # Make sure that allowing args interspersed with options is
339 # allowed
340 parser.allow_interspersed_args = True
341 parser.error = lambda msg: None
342 parser.add_option(arg_name, dest="dirs", action="append")
343 options = parser.parse_args(env_val.split())[0]
344 if options.dirs:
345 for directory in reversed(options.dirs):
346 add_dir_to_list(dir_list, directory)
348 if os.path.normpath(sys.prefix) != '/usr':
349 add_dir_to_list(self.compiler.library_dirs,
350 sysconfig.get_config_var("LIBDIR"))
351 add_dir_to_list(self.compiler.include_dirs,
352 sysconfig.get_config_var("INCLUDEDIR"))
354 # lib_dirs and inc_dirs are used to search for files;
355 # if a file is found in one of those directories, it can
356 # be assumed that no additional -I,-L directives are needed.
357 lib_dirs = self.compiler.library_dirs + [
358 '/lib64', '/usr/lib64',
359 '/lib', '/usr/lib',
361 inc_dirs = self.compiler.include_dirs + ['/usr/include']
362 exts = []
363 missing = []
365 config_h = sysconfig.get_config_h_filename()
366 config_h_vars = sysconfig.parse_config_h(open(config_h))
368 platform = self.get_platform()
369 (srcdir,) = sysconfig.get_config_vars('srcdir')
371 # Check for AtheOS which has libraries in non-standard locations
372 if platform == 'atheos':
373 lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
374 lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
375 inc_dirs += ['/system/include', '/atheos/autolnk/include']
376 inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
378 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
379 if platform in ['osf1', 'unixware7', 'openunix8']:
380 lib_dirs += ['/usr/ccs/lib']
382 if platform == 'darwin':
383 # This should work on any unixy platform ;-)
384 # If the user has bothered specifying additional -I and -L flags
385 # in OPT and LDFLAGS we might as well use them here.
386 # NOTE: using shlex.split would technically be more correct, but
387 # also gives a bootstrap problem. Let's hope nobody uses directories
388 # with whitespace in the name to store libraries.
389 cflags, ldflags = sysconfig.get_config_vars(
390 'CFLAGS', 'LDFLAGS')
391 for item in cflags.split():
392 if item.startswith('-I'):
393 inc_dirs.append(item[2:])
395 for item in ldflags.split():
396 if item.startswith('-L'):
397 lib_dirs.append(item[2:])
399 # Check for MacOS X, which doesn't need libm.a at all
400 math_libs = ['m']
401 if platform in ['darwin', 'mac']:
402 math_libs = []
404 # XXX Omitted modules: gl, pure, dl, SGI-specific modules
407 # The following modules are all pretty straightforward, and compile
408 # on pretty much any POSIXish platform.
411 # Some modules that are normally always on:
412 exts.append( Extension('_weakref', ['_weakref.c']) )
414 # array objects
415 exts.append( Extension('array', ['arraymodule.c']) )
416 # complex math library functions
417 exts.append( Extension('cmath', ['cmathmodule.c'],
418 libraries=math_libs) )
420 # math library functions, e.g. sin()
421 exts.append( Extension('math', ['mathmodule.c'],
422 libraries=math_libs) )
423 # time operations and variables
424 exts.append( Extension('time', ['timemodule.c'],
425 libraries=math_libs) )
426 exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'],
427 libraries=math_libs) )
428 # fast iterator tools implemented in C
429 exts.append( Extension("itertools", ["itertoolsmodule.c"]) )
430 # random number generator implemented in C
431 exts.append( Extension("_random", ["_randommodule.c"]) )
432 # high-performance collections
433 exts.append( Extension("_collections", ["_collectionsmodule.c"]) )
434 # bisect
435 exts.append( Extension("_bisect", ["_bisectmodule.c"]) )
436 # heapq
437 exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
438 # operator.add() and similar goodies
439 exts.append( Extension('operator', ['operator.c']) )
440 # _functools
441 exts.append( Extension("_functools", ["_functoolsmodule.c"]) )
442 # Memory-based IO accelerator modules
443 exts.append( Extension("_bytesio", ["_bytesio.c"]) )
444 exts.append( Extension("_stringio", ["_stringio.c"]) )
445 # C-optimized pickle replacement
446 exts.append( Extension("_pickle", ["_pickle.c"]) )
447 # atexit
448 exts.append( Extension("atexit", ["atexitmodule.c"]) )
449 # _json speedups
450 exts.append( Extension("_json", ["_json.c"]) )
451 # Python C API test module
452 exts.append( Extension('_testcapi', ['_testcapimodule.c']) )
453 # profiler (_lsprof is for cProfile.py)
454 exts.append( Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']) )
455 # static Unicode character database
456 exts.append( Extension('unicodedata', ['unicodedata.c']) )
457 # access to ISO C locale support
458 data = open('pyconfig.h').read()
459 m = re.search(r"#s*define\s+WITH_LIBINTL\s+1\s*", data)
460 if m is not None:
461 locale_libs = ['intl']
462 else:
463 locale_libs = []
464 if platform == 'darwin':
465 locale_extra_link_args = ['-framework', 'CoreFoundation']
466 else:
467 locale_extra_link_args = []
470 exts.append( Extension('_locale', ['_localemodule.c'],
471 libraries=locale_libs,
472 extra_link_args=locale_extra_link_args) )
474 # Modules with some UNIX dependencies -- on by default:
475 # (If you have a really backward UNIX, select and socket may not be
476 # supported...)
478 # fcntl(2) and ioctl(2)
479 exts.append( Extension('fcntl', ['fcntlmodule.c']) )
480 if platform not in ['mac']:
481 # pwd(3)
482 exts.append( Extension('pwd', ['pwdmodule.c']) )
483 # grp(3)
484 exts.append( Extension('grp', ['grpmodule.c']) )
485 # spwd, shadow passwords
486 if (config_h_vars.get('HAVE_GETSPNAM', False) or
487 config_h_vars.get('HAVE_GETSPENT', False)):
488 exts.append( Extension('spwd', ['spwdmodule.c']) )
489 else:
490 missing.append('spwd')
491 else:
492 missing.extend(['pwd', 'grp', 'spwd'])
494 # select(2); not on ancient System V
495 exts.append( Extension('select', ['selectmodule.c']) )
497 # Fred Drake's interface to the Python parser
498 exts.append( Extension('parser', ['parsermodule.c']) )
500 # Memory-mapped files (also works on Win32).
501 if platform not in ['atheos', 'mac']:
502 exts.append( Extension('mmap', ['mmapmodule.c']) )
503 else:
504 missing.append('mmap')
506 # Lance Ellinghaus's syslog module
507 if platform not in ['mac']:
508 # syslog daemon interface
509 exts.append( Extension('syslog', ['syslogmodule.c']) )
510 else:
511 missing.append('syslog')
514 # Here ends the simple stuff. From here on, modules need certain
515 # libraries, are platform-specific, or present other surprises.
518 # Multimedia modules
519 # These don't work for 64-bit platforms!!!
520 # These represent audio samples or images as strings:
522 # Operations on audio samples
523 # According to #993173, this one should actually work fine on
524 # 64-bit platforms.
525 exts.append( Extension('audioop', ['audioop.c']) )
527 # readline
528 do_readline = self.compiler.find_library_file(lib_dirs, 'readline')
529 if platform == 'darwin': # and os.uname()[2] < '9.':
530 # MacOSX 10.4 has a broken readline. Don't try to build
531 # the readline module unless the user has installed a fixed
532 # readline package
533 # FIXME: The readline emulation on 10.5 is better, but the
534 # readline module doesn't compile out of the box.
535 if find_file('readline/rlconf.h', inc_dirs, []) is None:
536 do_readline = False
537 if do_readline:
538 if sys.platform == 'darwin':
539 # In every directory on the search path search for a dynamic
540 # library and then a static library, instead of first looking
541 # for dynamic libraries on the entire path.
542 # This way a staticly linked custom readline gets picked up
543 # before the (broken) dynamic library in /usr/lib.
544 readline_extra_link_args = ('-Wl,-search_paths_first',)
545 else:
546 readline_extra_link_args = ()
548 readline_libs = ['readline']
549 if self.compiler.find_library_file(lib_dirs,
550 'ncursesw'):
551 readline_libs.append('ncursesw')
552 elif self.compiler.find_library_file(lib_dirs,
553 'ncurses'):
554 readline_libs.append('ncurses')
555 elif self.compiler.find_library_file(lib_dirs, 'curses'):
556 readline_libs.append('curses')
557 elif self.compiler.find_library_file(lib_dirs +
558 ['/usr/lib/termcap'],
559 'termcap'):
560 readline_libs.append('termcap')
561 exts.append( Extension('readline', ['readline.c'],
562 library_dirs=['/usr/lib/termcap'],
563 extra_link_args=readline_extra_link_args,
564 libraries=readline_libs) )
565 else:
566 missing.append('readline')
568 if platform not in ['mac']:
569 # crypt module.
571 if self.compiler.find_library_file(lib_dirs, 'crypt'):
572 libs = ['crypt']
573 else:
574 libs = []
575 exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
576 else:
577 missing.append('crypt')
579 # CSV files
580 exts.append( Extension('_csv', ['_csv.c']) )
582 # socket(2)
583 exts.append( Extension('_socket', ['socketmodule.c'],
584 depends = ['socketmodule.h']) )
585 # Detect SSL support for the socket module (via _ssl)
586 search_for_ssl_incs_in = [
587 '/usr/local/ssl/include',
588 '/usr/contrib/ssl/include/'
590 ssl_incs = find_file('openssl/ssl.h', inc_dirs,
591 search_for_ssl_incs_in
593 if ssl_incs is not None:
594 krb5_h = find_file('krb5.h', inc_dirs,
595 ['/usr/kerberos/include'])
596 if krb5_h:
597 ssl_incs += krb5_h
598 ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
599 ['/usr/local/ssl/lib',
600 '/usr/contrib/ssl/lib/'
603 if (ssl_incs is not None and
604 ssl_libs is not None):
605 exts.append( Extension('_ssl', ['_ssl.c'],
606 include_dirs = ssl_incs,
607 library_dirs = ssl_libs,
608 libraries = ['ssl', 'crypto'],
609 depends = ['socketmodule.h']), )
610 else:
611 missing.append('_ssl')
613 # find out which version of OpenSSL we have
614 openssl_ver = 0
615 openssl_ver_re = re.compile(
616 '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
617 for ssl_inc_dir in inc_dirs + search_for_ssl_incs_in:
618 name = os.path.join(ssl_inc_dir, 'openssl', 'opensslv.h')
619 if os.path.isfile(name):
620 try:
621 incfile = open(name, 'r')
622 for line in incfile:
623 m = openssl_ver_re.match(line)
624 if m:
625 openssl_ver = eval(m.group(1))
626 break
627 except IOError:
628 pass
630 # first version found is what we'll use (as the compiler should)
631 if openssl_ver:
632 break
634 #print('openssl_ver = 0x%08x' % openssl_ver)
636 if ssl_incs is not None and ssl_libs is not None:
637 if openssl_ver >= 0x00907000:
638 # The _hashlib module wraps optimized implementations
639 # of hash functions from the OpenSSL library.
640 exts.append( Extension('_hashlib', ['_hashopenssl.c'],
641 include_dirs = ssl_incs,
642 library_dirs = ssl_libs,
643 libraries = ['ssl', 'crypto']) )
644 else:
645 print("warning: openssl 0x%08x is too old for _hashlib" %
646 openssl_ver)
647 missing.append('_hashlib')
648 else:
649 missing.append('_hashlib')
651 if openssl_ver < 0x00908000:
652 # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
653 exts.append( Extension('_sha256', ['sha256module.c']) )
654 exts.append( Extension('_sha512', ['sha512module.c']) )
656 if openssl_ver < 0x00907000:
657 # no openssl at all, use our own md5 and sha1
658 exts.append( Extension('_md5', ['md5module.c']) )
659 exts.append( Extension('_sha1', ['sha1module.c']) )
661 # Modules that provide persistent dictionary-like semantics. You will
662 # probably want to arrange for at least one of them to be available on
663 # your machine, though none are defined by default because of library
664 # dependencies. The Python module dbm/__init__.py provides an
665 # implementation independent wrapper for these; dbm/dumb.py provides
666 # similar functionality (but slower of course) implemented in Python.
668 # Sleepycat^WOracle Berkeley DB interface.
669 # http://www.oracle.com/database/berkeley-db/db/index.html
671 # This requires the Sleepycat^WOracle DB code. The supported versions
672 # are set below. Visit the URL above to download
673 # a release. Most open source OSes come with one or more
674 # versions of BerkeleyDB already installed.
676 max_db_ver = (4, 5) # XXX(gregory.p.smith): 4.6 "works" but seems to
677 # have issues on many platforms. I've temporarily
678 # disabled 4.6 to see what the odd platform
679 # buildbots say.
680 max_db_ver = (4, 7) # XXX(matthias.klose): test with 4.7 on some buildds
681 min_db_ver = (3, 3)
682 db_setup_debug = False # verbose debug prints from this script?
684 # construct a list of paths to look for the header file in on
685 # top of the normal inc_dirs.
686 db_inc_paths = [
687 '/usr/include/db4',
688 '/usr/local/include/db4',
689 '/opt/sfw/include/db4',
690 '/usr/include/db3',
691 '/usr/local/include/db3',
692 '/opt/sfw/include/db3',
693 # Fink defaults (http://fink.sourceforge.net/)
694 '/sw/include/db4',
695 '/sw/include/db3',
697 # 4.x minor number specific paths
698 for x in range(max_db_ver[1]+1):
699 db_inc_paths.append('/usr/include/db4%d' % x)
700 db_inc_paths.append('/usr/include/db4.%d' % x)
701 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
702 db_inc_paths.append('/usr/local/include/db4%d' % x)
703 db_inc_paths.append('/pkg/db-4.%d/include' % x)
704 db_inc_paths.append('/opt/db-4.%d/include' % x)
705 # MacPorts default (http://www.macports.org/)
706 db_inc_paths.append('/opt/local/include/db4%d' % x)
707 # 3.x minor number specific paths
708 for x in (3,):
709 db_inc_paths.append('/usr/include/db3%d' % x)
710 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
711 db_inc_paths.append('/usr/local/include/db3%d' % x)
712 db_inc_paths.append('/pkg/db-3.%d/include' % x)
713 db_inc_paths.append('/opt/db-3.%d/include' % x)
715 # Add some common subdirectories for Sleepycat DB to the list,
716 # based on the standard include directories. This way DB3/4 gets
717 # picked up when it is installed in a non-standard prefix and
718 # the user has added that prefix into inc_dirs.
719 std_variants = []
720 for dn in inc_dirs:
721 std_variants.append(os.path.join(dn, 'db3'))
722 std_variants.append(os.path.join(dn, 'db4'))
723 for x in range(max_db_ver[1]+1):
724 std_variants.append(os.path.join(dn, "db4%d"%x))
725 std_variants.append(os.path.join(dn, "db4.%d"%x))
726 for x in (3,):
727 std_variants.append(os.path.join(dn, "db3%d"%x))
728 std_variants.append(os.path.join(dn, "db3.%d"%x))
730 db_inc_paths = std_variants + db_inc_paths
731 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
733 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)
751 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
752 if db_ver == (4, 6):
753 m = re.search(r"#define\WDB_VERSION_PATCH\W(\d+)", f)
754 db_patch = int(m.group(1))
755 if db_patch < 21:
756 print("db.h:", db_ver, "patch", db_patch,
757 "being ignored (4.6.x must be >= 4.6.21)")
758 continue
760 if ( (db_ver not in db_ver_inc_map) and
761 (db_ver <= max_db_ver and db_ver >= min_db_ver) ):
762 # save the include directory with the db.h version
763 # (first occurrence only)
764 db_ver_inc_map[db_ver] = d
765 if db_setup_debug:
766 print("db.h: found", db_ver, "in", d)
767 else:
768 # we already found a header for this library version
769 if db_setup_debug: print("db.h: ignoring", d)
770 else:
771 # ignore this header, it didn't contain a version number
772 if db_setup_debug:
773 print("db.h: no version number version in", d)
775 db_found_vers = sorted(db_ver_inc_map.keys())
777 while db_found_vers:
778 db_ver = db_found_vers.pop()
779 db_incdir = db_ver_inc_map[db_ver]
781 # check lib directories parallel to the location of the header
782 db_dirs_to_check = [
783 db_incdir.replace("include", 'lib64'),
784 db_incdir.replace("include", 'lib'),
786 db_dirs_to_check = [x for x in db_dirs_to_check if os.path.isdir(x)]
788 # Look for a version specific db-X.Y before an ambiguoius dbX
789 # XXX should we -ever- look for a dbX name? Do any
790 # systems really not name their library by version and
791 # symlink to more general names?
792 for dblib in (('db-%d.%d' % db_ver),
793 ('db%d%d' % db_ver),
794 ('db%d' % db_ver[0])):
795 dblib_file = self.compiler.find_library_file(
796 db_dirs_to_check + lib_dirs, dblib )
797 if dblib_file:
798 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
799 raise db_found
800 else:
801 if db_setup_debug: print("db lib: ", dblib, "not found")
803 except db_found:
804 if db_setup_debug:
805 print("db lib: using", db_ver, dblib)
806 print("db: lib dir", dblib_dir, "inc dir", db_incdir)
807 db_incs = [db_incdir]
808 dblibs = [dblib]
809 # We add the runtime_library_dirs argument because the
810 # BerkeleyDB lib we're linking against often isn't in the
811 # system dynamic library search path. This is usually
812 # correct and most trouble free, but may cause problems in
813 # some unusual system configurations (e.g. the directory
814 # is on an NFS server that goes away).
815 exts.append(Extension('_bsddb', ['_bsddb.c'],
816 depends = ['bsddb.h'],
817 library_dirs=dblib_dir,
818 runtime_library_dirs=dblib_dir,
819 include_dirs=db_incs,
820 libraries=dblibs))
821 else:
822 if db_setup_debug: print("db: no appropriate library found")
823 db_incs = None
824 dblibs = []
825 dblib_dir = None
826 missing.append('_bsddb')
828 # The sqlite interface
829 sqlite_setup_debug = False # verbose debug prints from this script?
831 # We hunt for #define SQLITE_VERSION "n.n.n"
832 # We need to find >= sqlite version 3.0.8
833 sqlite_incdir = sqlite_libdir = None
834 sqlite_inc_paths = [ '/usr/include',
835 '/usr/include/sqlite',
836 '/usr/include/sqlite3',
837 '/usr/local/include',
838 '/usr/local/include/sqlite',
839 '/usr/local/include/sqlite3',
841 MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
842 MIN_SQLITE_VERSION = ".".join([str(x)
843 for x in MIN_SQLITE_VERSION_NUMBER])
845 # Scan the default include directories before the SQLite specific
846 # ones. This allows one to override the copy of sqlite on OSX,
847 # where /usr/include contains an old version of sqlite.
848 for d in inc_dirs + sqlite_inc_paths:
849 f = os.path.join(d, "sqlite3.h")
850 if os.path.exists(f):
851 if sqlite_setup_debug: print("sqlite: found %s"%f)
852 incf = open(f).read()
853 m = re.search(
854 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
855 if m:
856 sqlite_version = m.group(1)
857 sqlite_version_tuple = tuple([int(x)
858 for x in sqlite_version.split(".")])
859 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
860 # we win!
861 if sqlite_setup_debug:
862 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
863 sqlite_incdir = d
864 break
865 else:
866 if sqlite_setup_debug:
867 print("%s: version %d is too old, need >= %s"%(d,
868 sqlite_version, MIN_SQLITE_VERSION))
869 elif sqlite_setup_debug:
870 print("sqlite: %s had no SQLITE_VERSION"%(f,))
872 if sqlite_incdir:
873 sqlite_dirs_to_check = [
874 os.path.join(sqlite_incdir, '..', 'lib64'),
875 os.path.join(sqlite_incdir, '..', 'lib'),
876 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
877 os.path.join(sqlite_incdir, '..', '..', 'lib'),
879 sqlite_libfile = self.compiler.find_library_file(
880 sqlite_dirs_to_check + lib_dirs, 'sqlite3')
881 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
883 if sqlite_incdir and sqlite_libdir:
884 sqlite_srcs = ['_sqlite/cache.c',
885 '_sqlite/connection.c',
886 '_sqlite/cursor.c',
887 '_sqlite/microprotocols.c',
888 '_sqlite/module.c',
889 '_sqlite/prepare_protocol.c',
890 '_sqlite/row.c',
891 '_sqlite/statement.c',
892 '_sqlite/util.c', ]
894 sqlite_defines = []
895 if sys.platform != "win32":
896 sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
897 else:
898 sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
901 if sys.platform == 'darwin':
902 # In every directory on the search path search for a dynamic
903 # library and then a static library, instead of first looking
904 # for dynamic libraries on the entiry path.
905 # This way a staticly linked custom sqlite gets picked up
906 # before the dynamic library in /usr/lib.
907 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
908 else:
909 sqlite_extra_link_args = ()
911 exts.append(Extension('_sqlite3', sqlite_srcs,
912 define_macros=sqlite_defines,
913 include_dirs=["Modules/_sqlite",
914 sqlite_incdir],
915 library_dirs=sqlite_libdir,
916 runtime_library_dirs=sqlite_libdir,
917 extra_link_args=sqlite_extra_link_args,
918 libraries=["sqlite3",]))
919 else:
920 missing.append('_sqlite3')
922 # The standard Unix dbm module:
923 if platform not in ['cygwin']:
924 if find_file("ndbm.h", inc_dirs, []) is not None:
925 # Some systems have -lndbm, others don't
926 if self.compiler.find_library_file(lib_dirs, 'ndbm'):
927 ndbm_libs = ['ndbm']
928 else:
929 ndbm_libs = []
930 exts.append( Extension('_dbm', ['_dbmmodule.c'],
931 define_macros=[('HAVE_NDBM_H',None)],
932 libraries = ndbm_libs ) )
933 elif (self.compiler.find_library_file(lib_dirs, 'gdbm')
934 and find_file("gdbm/ndbm.h", inc_dirs, []) is not None):
935 exts.append( Extension('_dbm', ['_dbmmodule.c'],
936 define_macros=[('HAVE_GDBM_NDBM_H',None)],
937 libraries = ['gdbm'] ) )
938 elif db_incs is not None:
939 exts.append( Extension('_dbm', ['_dbmmodule.c'],
940 library_dirs=dblib_dir,
941 runtime_library_dirs=dblib_dir,
942 include_dirs=db_incs,
943 define_macros=[('HAVE_BERKDB_H',None),
944 ('DB_DBM_HSEARCH',None)],
945 libraries=dblibs))
946 else:
947 missing.append('_dbm')
949 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
950 if (self.compiler.find_library_file(lib_dirs, 'gdbm')):
951 exts.append( Extension('_gdbm', ['_gdbmmodule.c'],
952 libraries = ['gdbm'] ) )
953 else:
954 missing.append('_gdbm')
956 # Unix-only modules
957 if platform not in ['mac', 'win32']:
958 # Steen Lumholt's termios module
959 exts.append( Extension('termios', ['termios.c']) )
960 # Jeremy Hylton's rlimit interface
961 if platform not in ['atheos']:
962 exts.append( Extension('resource', ['resource.c']) )
963 else:
964 missing.append('resource')
966 # Sun yellow pages. Some systems have the functions in libc.
967 if platform not in ['cygwin', 'atheos', 'qnx6']:
968 if (self.compiler.find_library_file(lib_dirs, 'nsl')):
969 libs = ['nsl']
970 else:
971 libs = []
972 exts.append( Extension('nis', ['nismodule.c'],
973 libraries = libs) )
974 else:
975 missing.append('nis')
976 else:
977 missing.extend(['nis', 'resource', 'termios'])
979 # Curses support, requiring the System V version of curses, often
980 # provided by the ncurses library.
981 panel_library = 'panel'
982 if (self.compiler.find_library_file(lib_dirs, 'ncursesw')):
983 curses_libs = ['ncursesw']
984 # Bug 1464056: If _curses.so links with ncursesw,
985 # _curses_panel.so must link with panelw.
986 panel_library = 'panelw'
987 exts.append( Extension('_curses', ['_cursesmodule.c'],
988 libraries = curses_libs) )
989 elif (self.compiler.find_library_file(lib_dirs, 'ncurses')):
990 curses_libs = ['ncurses']
991 exts.append( Extension('_curses', ['_cursesmodule.c'],
992 libraries = curses_libs) )
993 elif (self.compiler.find_library_file(lib_dirs, 'curses')
994 and platform != 'darwin'):
995 # OSX has an old Berkeley curses, not good enough for
996 # the _curses module.
997 if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
998 curses_libs = ['curses', 'terminfo']
999 elif (self.compiler.find_library_file(lib_dirs, 'termcap')):
1000 curses_libs = ['curses', 'termcap']
1001 else:
1002 curses_libs = ['curses']
1004 exts.append( Extension('_curses', ['_cursesmodule.c'],
1005 libraries = curses_libs) )
1006 else:
1007 missing.append('_curses')
1009 # If the curses module is enabled, check for the panel module
1010 if (module_enabled(exts, '_curses') and
1011 self.compiler.find_library_file(lib_dirs, panel_library)):
1012 exts.append( Extension('_curses_panel', ['_curses_panel.c'],
1013 libraries = [panel_library] + curses_libs) )
1014 else:
1015 missing.append('_curses_panel')
1017 # Andrew Kuchling's zlib module. Note that some versions of zlib
1018 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1019 # http://www.cert.org/advisories/CA-2002-07.html
1021 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1022 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1023 # now, we still accept 1.1.3, because we think it's difficult to
1024 # exploit this in Python, and we'd rather make it RedHat's problem
1025 # than our problem <wink>.
1027 # You can upgrade zlib to version 1.1.4 yourself by going to
1028 # http://www.gzip.org/zlib/
1029 zlib_inc = find_file('zlib.h', [], inc_dirs)
1030 have_zlib = False
1031 if zlib_inc is not None:
1032 zlib_h = zlib_inc[0] + '/zlib.h'
1033 version = '"0.0.0"'
1034 version_req = '"1.1.3"'
1035 fp = open(zlib_h)
1036 while 1:
1037 line = fp.readline()
1038 if not line:
1039 break
1040 if line.startswith('#define ZLIB_VERSION'):
1041 version = line.split()[2]
1042 break
1043 if version >= version_req:
1044 if (self.compiler.find_library_file(lib_dirs, 'z')):
1045 if sys.platform == "darwin":
1046 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1047 else:
1048 zlib_extra_link_args = ()
1049 exts.append( Extension('zlib', ['zlibmodule.c'],
1050 libraries = ['z'],
1051 extra_link_args = zlib_extra_link_args))
1052 have_zlib = True
1053 else:
1054 missing.append('zlib')
1055 else:
1056 missing.append('zlib')
1057 else:
1058 missing.append('zlib')
1060 # Helper module for various ascii-encoders. Uses zlib for an optimized
1061 # crc32 if we have it. Otherwise binascii uses its own.
1062 if have_zlib:
1063 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1064 libraries = ['z']
1065 extra_link_args = zlib_extra_link_args
1066 else:
1067 extra_compile_args = []
1068 libraries = []
1069 extra_link_args = []
1070 exts.append( Extension('binascii', ['binascii.c'],
1071 extra_compile_args = extra_compile_args,
1072 libraries = libraries,
1073 extra_link_args = extra_link_args) )
1075 # Gustavo Niemeyer's bz2 module.
1076 if (self.compiler.find_library_file(lib_dirs, 'bz2')):
1077 if sys.platform == "darwin":
1078 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1079 else:
1080 bz2_extra_link_args = ()
1081 exts.append( Extension('bz2', ['bz2module.c'],
1082 libraries = ['bz2'],
1083 extra_link_args = bz2_extra_link_args) )
1084 else:
1085 missing.append('bz2')
1087 # Interface to the Expat XML parser
1089 # Expat was written by James Clark and is now maintained by a
1090 # group of developers on SourceForge; see www.libexpat.org for
1091 # more information. The pyexpat module was written by Paul
1092 # Prescod after a prototype by Jack Jansen. The Expat source
1093 # is included in Modules/expat/. Usage of a system
1094 # shared libexpat.so/expat.dll is not advised.
1096 # More information on Expat can be found at www.libexpat.org.
1098 expatinc = os.path.join(os.getcwd(), srcdir, 'Modules', 'expat')
1099 define_macros = [
1100 ('HAVE_EXPAT_CONFIG_H', '1'),
1103 exts.append(Extension('pyexpat',
1104 define_macros = define_macros,
1105 include_dirs = [expatinc],
1106 sources = ['pyexpat.c',
1107 'expat/xmlparse.c',
1108 'expat/xmlrole.c',
1109 'expat/xmltok.c',
1113 # Fredrik Lundh's cElementTree module. Note that this also
1114 # uses expat (via the CAPI hook in pyexpat).
1116 if os.path.isfile(os.path.join(srcdir, 'Modules', '_elementtree.c')):
1117 define_macros.append(('USE_PYEXPAT_CAPI', None))
1118 exts.append(Extension('_elementtree',
1119 define_macros = define_macros,
1120 include_dirs = [expatinc],
1121 sources = ['_elementtree.c'],
1123 else:
1124 missing.append('_elementtree')
1126 # Hye-Shik Chang's CJKCodecs modules.
1127 exts.append(Extension('_multibytecodec',
1128 ['cjkcodecs/multibytecodec.c']))
1129 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
1130 exts.append(Extension('_codecs_%s' % loc,
1131 ['cjkcodecs/_codecs_%s.c' % loc]))
1133 # Thomas Heller's _ctypes module
1134 self.detect_ctypes(inc_dirs, lib_dirs)
1136 # _fileio -- supposedly cross platform
1137 exts.append(Extension('_fileio', ['_fileio.c']))
1138 # Richard Oudkerk's multiprocessing module
1139 if platform == 'win32': # Windows
1140 macros = dict()
1141 libraries = ['ws2_32']
1143 elif platform == 'darwin': # Mac OSX
1144 macros = dict(
1145 HAVE_SEM_OPEN=1,
1146 HAVE_SEM_TIMEDWAIT=0,
1147 HAVE_FD_TRANSFER=1,
1148 HAVE_BROKEN_SEM_GETVALUE=1
1150 libraries = []
1152 elif platform == 'cygwin': # Cygwin
1153 macros = dict(
1154 HAVE_SEM_OPEN=1,
1155 HAVE_SEM_TIMEDWAIT=1,
1156 HAVE_FD_TRANSFER=0,
1157 HAVE_BROKEN_SEM_UNLINK=1
1159 libraries = []
1161 elif platform in ('freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
1162 # FreeBSD's P1003.1b semaphore support is very experimental
1163 # and has many known problems. (as of June 2008)
1164 macros = dict( # FreeBSD
1165 HAVE_SEM_OPEN=0,
1166 HAVE_SEM_TIMEDWAIT=0,
1167 HAVE_FD_TRANSFER=1,
1169 libraries = []
1171 else: # Linux and other unices
1172 macros = dict(
1173 HAVE_SEM_OPEN=1,
1174 HAVE_SEM_TIMEDWAIT=1,
1175 HAVE_FD_TRANSFER=1
1177 libraries = ['rt']
1179 if platform == 'win32':
1180 multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1181 '_multiprocessing/semaphore.c',
1182 '_multiprocessing/pipe_connection.c',
1183 '_multiprocessing/socket_connection.c',
1184 '_multiprocessing/win32_functions.c'
1187 else:
1188 multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1189 '_multiprocessing/socket_connection.c'
1192 if macros.get('HAVE_SEM_OPEN', False):
1193 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
1195 exts.append ( Extension('_multiprocessing', multiprocessing_srcs,
1196 define_macros=list(macros.items()),
1197 include_dirs=["Modules/_multiprocessing"]))
1198 # End multiprocessing
1200 # Platform-specific libraries
1201 if platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
1202 'freebsd7', 'freebsd8'):
1203 exts.append( Extension('ossaudiodev', ['ossaudiodev.c']) )
1204 else:
1205 missing.append('ossaudiodev')
1207 if sys.platform == 'darwin':
1208 exts.append(
1209 Extension('_gestalt', ['_gestalt.c'],
1210 extra_link_args=['-framework', 'Carbon'])
1213 self.extensions.extend(exts)
1215 # Call the method for detecting whether _tkinter can be compiled
1216 self.detect_tkinter(inc_dirs, lib_dirs)
1218 if '_tkinter' not in [e.name for e in self.extensions]:
1219 missing.append('_tkinter')
1221 return missing
1223 def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
1224 # The _tkinter module, using frameworks. Since frameworks are quite
1225 # different the UNIX search logic is not sharable.
1226 from os.path import join, exists
1227 framework_dirs = [
1228 '/System/Library/Frameworks/',
1229 '/Library/Frameworks',
1230 join(os.getenv('HOME'), '/Library/Frameworks')
1233 # Find the directory that contains the Tcl.framework and Tk.framework
1234 # bundles.
1235 # XXX distutils should support -F!
1236 for F in framework_dirs:
1237 # both Tcl.framework and Tk.framework should be present
1238 for fw in 'Tcl', 'Tk':
1239 if not exists(join(F, fw + '.framework')):
1240 break
1241 else:
1242 # ok, F is now directory with both frameworks. Continure
1243 # building
1244 break
1245 else:
1246 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
1247 # will now resume.
1248 return 0
1250 # For 8.4a2, we must add -I options that point inside the Tcl and Tk
1251 # frameworks. In later release we should hopefully be able to pass
1252 # the -F option to gcc, which specifies a framework lookup path.
1254 include_dirs = [
1255 join(F, fw + '.framework', H)
1256 for fw in ('Tcl', 'Tk')
1257 for H in ('Headers', 'Versions/Current/PrivateHeaders')
1260 # For 8.4a2, the X11 headers are not included. Rather than include a
1261 # complicated search, this is a hard-coded path. It could bail out
1262 # if X11 libs are not found...
1263 include_dirs.append('/usr/X11R6/include')
1264 frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
1266 # All existing framework builds of Tcl/Tk don't support 64-bit
1267 # architectures.
1268 cflags = sysconfig.get_config_vars('CFLAGS')[0]
1269 archs = re.findall('-arch\s+(\w+)', cflags)
1270 if 'x86_64' in archs or 'ppc64' in archs:
1271 try:
1272 archs.remove('x86_64')
1273 except ValueError:
1274 pass
1275 try:
1276 archs.remove('ppc64')
1277 except ValueError:
1278 pass
1280 for a in archs:
1281 frameworks.append('-arch')
1282 frameworks.append(a)
1284 ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1285 define_macros=[('WITH_APPINIT', 1)],
1286 include_dirs = include_dirs,
1287 libraries = [],
1288 extra_compile_args = frameworks[2:],
1289 extra_link_args = frameworks,
1291 self.extensions.append(ext)
1292 return 1
1295 def detect_tkinter(self, inc_dirs, lib_dirs):
1296 # The _tkinter module.
1298 # Rather than complicate the code below, detecting and building
1299 # AquaTk is a separate method. Only one Tkinter will be built on
1300 # Darwin - either AquaTk, if it is found, or X11 based Tk.
1301 platform = self.get_platform()
1302 if (platform == 'darwin' and
1303 self.detect_tkinter_darwin(inc_dirs, lib_dirs)):
1304 return
1306 # Assume we haven't found any of the libraries or include files
1307 # The versions with dots are used on Unix, and the versions without
1308 # dots on Windows, for detection by cygwin.
1309 tcllib = tklib = tcl_includes = tk_includes = None
1310 for version in ['8.5', '85', '8.4', '84', '8.3', '83', '8.2',
1311 '82', '8.1', '81', '8.0', '80']:
1312 tklib = self.compiler.find_library_file(lib_dirs, 'tk' + version)
1313 tcllib = self.compiler.find_library_file(lib_dirs, 'tcl' + version)
1314 if tklib and tcllib:
1315 # Exit the loop when we've found the Tcl/Tk libraries
1316 break
1318 # Now check for the header files
1319 if tklib and tcllib:
1320 # Check for the include files on Debian and {Free,Open}BSD, where
1321 # they're put in /usr/include/{tcl,tk}X.Y
1322 dotversion = version
1323 if '.' not in dotversion and "bsd" in sys.platform.lower():
1324 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
1325 # but the include subdirs are named like .../include/tcl8.3.
1326 dotversion = dotversion[:-1] + '.' + dotversion[-1]
1327 tcl_include_sub = []
1328 tk_include_sub = []
1329 for dir in inc_dirs:
1330 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
1331 tk_include_sub += [dir + os.sep + "tk" + dotversion]
1332 tk_include_sub += tcl_include_sub
1333 tcl_includes = find_file('tcl.h', inc_dirs, tcl_include_sub)
1334 tk_includes = find_file('tk.h', inc_dirs, tk_include_sub)
1336 if (tcllib is None or tklib is None or
1337 tcl_includes is None or tk_includes is None):
1338 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
1339 return
1341 # OK... everything seems to be present for Tcl/Tk.
1343 include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
1344 for dir in tcl_includes + tk_includes:
1345 if dir not in include_dirs:
1346 include_dirs.append(dir)
1348 # Check for various platform-specific directories
1349 if platform == 'sunos5':
1350 include_dirs.append('/usr/openwin/include')
1351 added_lib_dirs.append('/usr/openwin/lib')
1352 elif os.path.exists('/usr/X11R6/include'):
1353 include_dirs.append('/usr/X11R6/include')
1354 added_lib_dirs.append('/usr/X11R6/lib64')
1355 added_lib_dirs.append('/usr/X11R6/lib')
1356 elif os.path.exists('/usr/X11R5/include'):
1357 include_dirs.append('/usr/X11R5/include')
1358 added_lib_dirs.append('/usr/X11R5/lib')
1359 else:
1360 # Assume default location for X11
1361 include_dirs.append('/usr/X11/include')
1362 added_lib_dirs.append('/usr/X11/lib')
1364 # If Cygwin, then verify that X is installed before proceeding
1365 if platform == 'cygwin':
1366 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
1367 if x11_inc is None:
1368 return
1370 # Check for BLT extension
1371 if self.compiler.find_library_file(lib_dirs + added_lib_dirs,
1372 'BLT8.0'):
1373 defs.append( ('WITH_BLT', 1) )
1374 libs.append('BLT8.0')
1375 elif self.compiler.find_library_file(lib_dirs + added_lib_dirs,
1376 'BLT'):
1377 defs.append( ('WITH_BLT', 1) )
1378 libs.append('BLT')
1380 # Add the Tcl/Tk libraries
1381 libs.append('tk'+ version)
1382 libs.append('tcl'+ version)
1384 if platform in ['aix3', 'aix4']:
1385 libs.append('ld')
1387 # Finally, link with the X11 libraries (not appropriate on cygwin)
1388 if platform != "cygwin":
1389 libs.append('X11')
1391 ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1392 define_macros=[('WITH_APPINIT', 1)] + defs,
1393 include_dirs = include_dirs,
1394 libraries = libs,
1395 library_dirs = added_lib_dirs,
1397 self.extensions.append(ext)
1399 ## # Uncomment these lines if you want to play with xxmodule.c
1400 ## ext = Extension('xx', ['xxmodule.c'])
1401 ## self.extensions.append(ext)
1403 # XXX handle these, but how to detect?
1404 # *** Uncomment and edit for PIL (TkImaging) extension only:
1405 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
1406 # *** Uncomment and edit for TOGL extension only:
1407 # -DWITH_TOGL togl.c \
1408 # *** Uncomment these for TOGL extension only:
1409 # -lGL -lGLU -lXext -lXmu \
1411 def configure_ctypes_darwin(self, ext):
1412 # Darwin (OS X) uses preconfigured files, in
1413 # the Modules/_ctypes/libffi_osx directory.
1414 (srcdir,) = sysconfig.get_config_vars('srcdir')
1415 ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
1416 '_ctypes', 'libffi_osx'))
1417 sources = [os.path.join(ffi_srcdir, p)
1418 for p in ['ffi.c',
1419 'x86/darwin64.S',
1420 'x86/x86-darwin.S',
1421 'x86/x86-ffi_darwin.c',
1422 'x86/x86-ffi64.c',
1423 'powerpc/ppc-darwin.S',
1424 'powerpc/ppc-darwin_closure.S',
1425 'powerpc/ppc-ffi_darwin.c',
1426 'powerpc/ppc64-darwin_closure.S',
1429 # Add .S (preprocessed assembly) to C compiler source extensions.
1430 self.compiler.src_extensions.append('.S')
1432 include_dirs = [os.path.join(ffi_srcdir, 'include'),
1433 os.path.join(ffi_srcdir, 'powerpc')]
1434 ext.include_dirs.extend(include_dirs)
1435 ext.sources.extend(sources)
1436 return True
1438 def configure_ctypes(self, ext):
1439 if not self.use_system_libffi:
1440 if sys.platform == 'darwin':
1441 return self.configure_ctypes_darwin(ext)
1443 (srcdir,) = sysconfig.get_config_vars('srcdir')
1444 ffi_builddir = os.path.join(self.build_temp, 'libffi')
1445 ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
1446 '_ctypes', 'libffi'))
1447 ffi_configfile = os.path.join(ffi_builddir, 'fficonfig.py')
1449 from distutils.dep_util import newer_group
1451 config_sources = [os.path.join(ffi_srcdir, fname)
1452 for fname in os.listdir(ffi_srcdir)
1453 if os.path.isfile(os.path.join(ffi_srcdir, fname))]
1454 if self.force or newer_group(config_sources,
1455 ffi_configfile):
1456 from distutils.dir_util import mkpath
1457 mkpath(ffi_builddir)
1458 config_args = []
1460 # Pass empty CFLAGS because we'll just append the resulting
1461 # CFLAGS to Python's; -g or -O2 is to be avoided.
1462 cmd = "cd %s && env CFLAGS='' '%s/configure' %s" \
1463 % (ffi_builddir, ffi_srcdir, " ".join(config_args))
1465 res = os.system(cmd)
1466 if res or not os.path.exists(ffi_configfile):
1467 print("Failed to configure _ctypes module")
1468 return False
1470 fficonfig = {}
1471 fp = open(ffi_configfile)
1472 try:
1473 script = fp.read()
1474 finally:
1475 fp.close()
1476 exec(script, globals(), fficonfig)
1477 ffi_srcdir = os.path.join(fficonfig['ffi_srcdir'], 'src')
1479 # Add .S (preprocessed assembly) to C compiler source extensions.
1480 self.compiler.src_extensions.append('.S')
1482 include_dirs = [os.path.join(ffi_builddir, 'include'),
1483 ffi_builddir, ffi_srcdir]
1484 extra_compile_args = fficonfig['ffi_cflags'].split()
1486 ext.sources.extend(fficonfig['ffi_sources'])
1487 ext.include_dirs.extend(include_dirs)
1488 ext.extra_compile_args.extend(extra_compile_args)
1489 return True
1491 def detect_ctypes(self, inc_dirs, lib_dirs):
1492 self.use_system_libffi = False
1493 include_dirs = []
1494 extra_compile_args = []
1495 extra_link_args = []
1496 sources = ['_ctypes/_ctypes.c',
1497 '_ctypes/callbacks.c',
1498 '_ctypes/callproc.c',
1499 '_ctypes/stgdict.c',
1500 '_ctypes/cfield.c',
1501 '_ctypes/malloc_closure.c']
1502 depends = ['_ctypes/ctypes.h']
1504 if sys.platform == 'darwin':
1505 sources.append('_ctypes/darwin/dlfcn_simple.c')
1506 extra_compile_args.append('-DMACOSX')
1507 include_dirs.append('_ctypes/darwin')
1508 # XXX Is this still needed?
1509 ## extra_link_args.extend(['-read_only_relocs', 'warning'])
1511 elif sys.platform == 'sunos5':
1512 # XXX This shouldn't be necessary; it appears that some
1513 # of the assembler code is non-PIC (i.e. it has relocations
1514 # when it shouldn't. The proper fix would be to rewrite
1515 # the assembler code to be PIC.
1516 # This only works with GCC; the Sun compiler likely refuses
1517 # this option. If you want to compile ctypes with the Sun
1518 # compiler, please research a proper solution, instead of
1519 # finding some -z option for the Sun compiler.
1520 extra_link_args.append('-mimpure-text')
1522 elif sys.platform.startswith('hp-ux'):
1523 extra_link_args.append('-fPIC')
1525 ext = Extension('_ctypes',
1526 include_dirs=include_dirs,
1527 extra_compile_args=extra_compile_args,
1528 extra_link_args=extra_link_args,
1529 libraries=[],
1530 sources=sources,
1531 depends=depends)
1532 ext_test = Extension('_ctypes_test',
1533 sources=['_ctypes/_ctypes_test.c'])
1534 self.extensions.extend([ext, ext_test])
1536 if not '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS"):
1537 return
1539 if sys.platform == 'darwin':
1540 # OS X 10.5 comes with libffi.dylib; the include files are
1541 # in /usr/include/ffi
1542 inc_dirs.append('/usr/include/ffi')
1544 ffi_inc = find_file('ffi.h', [], inc_dirs)
1545 if ffi_inc is not None:
1546 ffi_h = ffi_inc[0] + '/ffi.h'
1547 fp = open(ffi_h)
1548 while 1:
1549 line = fp.readline()
1550 if not line:
1551 ffi_inc = None
1552 break
1553 if line.startswith('#define LIBFFI_H'):
1554 break
1555 ffi_lib = None
1556 if ffi_inc is not None:
1557 for lib_name in ('ffi_convenience', 'ffi_pic', 'ffi'):
1558 if (self.compiler.find_library_file(lib_dirs, lib_name)):
1559 ffi_lib = lib_name
1560 break
1562 if ffi_inc and ffi_lib:
1563 ext.include_dirs.extend(ffi_inc)
1564 ext.libraries.append(ffi_lib)
1565 self.use_system_libffi = True
1568 class PyBuildInstall(install):
1569 # Suppress the warning about installation into the lib_dynload
1570 # directory, which is not in sys.path when running Python during
1571 # installation:
1572 def initialize_options (self):
1573 install.initialize_options(self)
1574 self.warn_dir=0
1576 class PyBuildInstallLib(install_lib):
1577 # Do exactly what install_lib does but make sure correct access modes get
1578 # set on installed directories and files. All installed files with get
1579 # mode 644 unless they are a shared library in which case they will get
1580 # mode 755. All installed directories will get mode 755.
1582 so_ext = sysconfig.get_config_var("SO")
1584 def install(self):
1585 outfiles = install_lib.install(self)
1586 self.set_file_modes(outfiles, 0o644, 0o755)
1587 self.set_dir_modes(self.install_dir, 0o755)
1588 return outfiles
1590 def set_file_modes(self, files, defaultMode, sharedLibMode):
1591 if not self.is_chmod_supported(): return
1592 if not files: return
1594 for filename in files:
1595 if os.path.islink(filename): continue
1596 mode = defaultMode
1597 if filename.endswith(self.so_ext): mode = sharedLibMode
1598 log.info("changing mode of %s to %o", filename, mode)
1599 if not self.dry_run: os.chmod(filename, mode)
1601 def set_dir_modes(self, dirname, mode):
1602 if not self.is_chmod_supported(): return
1603 os.walk(dirname, self.set_dir_modes_visitor, mode)
1605 def set_dir_modes_visitor(self, mode, dirname, names):
1606 if os.path.islink(dirname): return
1607 log.info("changing mode of %s to %o", dirname, mode)
1608 if not self.dry_run: os.chmod(dirname, mode)
1610 def is_chmod_supported(self):
1611 return hasattr(os, 'chmod')
1613 SUMMARY = """
1614 Python is an interpreted, interactive, object-oriented programming
1615 language. It is often compared to Tcl, Perl, Scheme or Java.
1617 Python combines remarkable power with very clear syntax. It has
1618 modules, classes, exceptions, very high level dynamic data types, and
1619 dynamic typing. There are interfaces to many system calls and
1620 libraries, as well as to various windowing systems (X11, Motif, Tk,
1621 Mac, MFC). New built-in modules are easily written in C or C++. Python
1622 is also usable as an extension language for applications that need a
1623 programmable interface.
1625 The Python implementation is portable: it runs on many brands of UNIX,
1626 on Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't
1627 listed here, it may still be supported, if there's a C compiler for
1628 it. Ask around on comp.lang.python -- or just try compiling Python
1629 yourself.
1632 CLASSIFIERS = """
1633 Development Status :: 3 - Alpha
1634 Development Status :: 6 - Mature
1635 License :: OSI Approved :: Python Software Foundation License
1636 Natural Language :: English
1637 Programming Language :: C
1638 Programming Language :: Python
1639 Topic :: Software Development
1642 def main():
1643 # turn off warnings when deprecated modules are imported
1644 import warnings
1645 warnings.filterwarnings("ignore",category=DeprecationWarning)
1646 setup(# PyPI Metadata (PEP 301)
1647 name = "Python",
1648 version = sys.version.split()[0],
1649 url = "http://www.python.org/%s" % sys.version[:3],
1650 maintainer = "Guido van Rossum and the Python community",
1651 maintainer_email = "python-dev@python.org",
1652 description = "A high-level object-oriented programming language",
1653 long_description = SUMMARY.strip(),
1654 license = "PSF license",
1655 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
1656 platforms = ["Many"],
1658 # Build info
1659 cmdclass = {'build_ext':PyBuildExt, 'install':PyBuildInstall,
1660 'install_lib':PyBuildInstallLib},
1661 # The struct module is defined here, because build_ext won't be
1662 # called unless there's at least one extension module defined.
1663 ext_modules=[Extension('_struct', ['_struct.c'])],
1665 # Scripts to install
1666 scripts = ['Tools/scripts/pydoc', 'Tools/scripts/idle',
1667 'Tools/scripts/2to3',
1668 'Lib/smtpd.py']
1671 # --install-platlib
1672 if __name__ == '__main__':
1673 main()