blob: 1791fbe2821a05d844a3e9792e6a5b005a5b29ee [file] [log] [blame]
Andrew M. Kuchling66012fe2001-01-26 21:56:58 +00001# Autodetecting setup.py script for building the Python extensions
2#
Fredrik Lundhade711a2001-01-24 08:00:28 +00003
Andrew M. Kuchling66012fe2001-01-26 21:56:58 +00004__version__ = "$Revision$"
5
Brett Cannon84667c02004-12-07 03:25:18 +00006import sys, os, imp, re, optparse
Christian Heimes8608d912008-01-25 15:52:11 +00007from glob import glob
Gregory P. Smith0902cac2008-05-27 08:40:09 +00008from platform import machine as platform_machine
Tarek Ziadé5633a802010-01-23 09:23:15 +00009import sysconfig
Michael W. Hudson529a5052002-12-17 16:47:17 +000010
11from distutils import log
Andrew M. Kuchling8d7f0862001-02-23 16:32:32 +000012from distutils import text_file
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +000013from distutils.errors import *
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000014from distutils.core import Extension, setup
15from distutils.command.build_ext import build_ext
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +000016from distutils.command.install import install
Michael W. Hudson529a5052002-12-17 16:47:17 +000017from distutils.command.install_lib import install_lib
Stefan Krah4d32c9c2010-06-04 09:49:20 +000018from distutils.spawn import find_executable
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000019
Gregory P. Smithc2fa18c2010-01-02 22:25:29 +000020# Were we compiled --with-pydebug or with #define Py_DEBUG?
21COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
22
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000023# This global variable is used to hold the list of modules to be disabled.
24disabled_module_list = []
25
Michael W. Hudson39230b32002-01-16 15:26:48 +000026def add_dir_to_list(dirlist, dir):
27 """Add the directory 'dir' to the list 'dirlist' (at the front) if
28 1) 'dir' is not already in 'dirlist'
29 2) 'dir' actually exists, and is a directory."""
Jack Jansen4439b7c2002-06-26 15:44:30 +000030 if dir is not None and os.path.isdir(dir) and dir not in dirlist:
Michael W. Hudson39230b32002-01-16 15:26:48 +000031 dirlist.insert(0, dir)
32
Ronald Oussoren593e4ca2010-06-03 09:47:21 +000033def macosx_sdk_root():
34 """
35 Return the directory of the current OSX SDK,
36 or '/' if no SDK was specified.
37 """
38 cflags = sysconfig.get_config_var('CFLAGS')
39 m = re.search(r'-isysroot\s+(\S+)', cflags)
40 if m is None:
41 sysroot = '/'
42 else:
43 sysroot = m.group(1)
44 return sysroot
45
46def is_macosx_sdk_path(path):
47 """
48 Returns True if 'path' can be located in an OSX SDK
49 """
Ronald Oussorencd172132010-06-27 12:36:16 +000050 return (path.startswith('/usr/') and not path.startswith('/usr/local')) or path.startswith('/System/')
Ned Deilyd8ec4642012-07-30 04:07:49 -070051 return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
52 or path.startswith('/System/')
53 or path.startswith('/Library/') )
Ronald Oussoren593e4ca2010-06-03 09:47:21 +000054
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000055def find_file(filename, std_dirs, paths):
56 """Searches for the directory where a given file is located,
57 and returns a possibly-empty list of additional directories, or None
58 if the file couldn't be found at all.
Fredrik Lundhade711a2001-01-24 08:00:28 +000059
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000060 'filename' is the name of a file, such as readline.h or libcrypto.a.
61 'std_dirs' is the list of standard system directories; if the
62 file is found in one of them, no additional directives are needed.
63 'paths' is a list of additional locations to check; if the file is
64 found in one of them, the resulting list will contain the directory.
65 """
Ronald Oussoren593e4ca2010-06-03 09:47:21 +000066 if sys.platform == 'darwin':
67 # Honor the MacOSX SDK setting when one was specified.
68 # An SDK is a directory with the same structure as a real
69 # system, but with only header files and libraries.
70 sysroot = macosx_sdk_root()
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000071
72 # Check the standard locations
73 for dir in std_dirs:
74 f = os.path.join(dir, filename)
Ronald Oussoren593e4ca2010-06-03 09:47:21 +000075
76 if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
77 f = os.path.join(sysroot, dir[1:], filename)
78
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000079 if os.path.exists(f): return []
80
81 # Check the additional directories
82 for dir in paths:
83 f = os.path.join(dir, filename)
Ronald Oussoren593e4ca2010-06-03 09:47:21 +000084
85 if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
86 f = os.path.join(sysroot, dir[1:], filename)
87
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000088 if os.path.exists(f):
89 return [dir]
90
91 # Not found anywhere
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000092 return None
93
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000094def find_library_file(compiler, libname, std_dirs, paths):
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +000095 result = compiler.find_library_file(std_dirs + paths, libname)
96 if result is None:
97 return None
Fredrik Lundhade711a2001-01-24 08:00:28 +000098
Ronald Oussoren593e4ca2010-06-03 09:47:21 +000099 if sys.platform == 'darwin':
100 sysroot = macosx_sdk_root()
101
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000102 # Check whether the found file is in one of the standard directories
103 dirname = os.path.dirname(result)
104 for p in std_dirs:
105 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000106 p = p.rstrip(os.sep)
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000107
108 if sys.platform == 'darwin' and is_macosx_sdk_path(p):
109 if os.path.join(sysroot, p[1:]) == dirname:
110 return [ ]
111
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000112 if p == dirname:
113 return [ ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000114
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000115 # Otherwise, it must have been in one of the additional directories,
116 # so we have to figure out which one.
117 for p in paths:
118 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000119 p = p.rstrip(os.sep)
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000120
121 if sys.platform == 'darwin' and is_macosx_sdk_path(p):
122 if os.path.join(sysroot, p[1:]) == dirname:
123 return [ p ]
124
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000125 if p == dirname:
126 return [p]
127 else:
128 assert False, "Internal error: Path not found in std_dirs or paths"
Tim Peters2c60f7a2003-01-29 03:49:43 +0000129
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000130def module_enabled(extlist, modname):
131 """Returns whether the module 'modname' is present in the list
132 of extensions 'extlist'."""
133 extlist = [ext for ext in extlist if ext.name == modname]
134 return len(extlist)
Fredrik Lundhade711a2001-01-24 08:00:28 +0000135
Jack Jansen144ebcc2001-08-05 22:31:19 +0000136def find_module_file(module, dirlist):
137 """Find a module in a set of possible folders. If it is not found
138 return the unadorned filename"""
139 list = find_file(module, [], dirlist)
140 if not list:
141 return module
142 if len(list) > 1:
Guido van Rossum12471d62003-02-20 02:11:43 +0000143 log.info("WARNING: multiple copies of %s found"%module)
Jack Jansen144ebcc2001-08-05 22:31:19 +0000144 return os.path.join(list[0], module)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000145
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000146class PyBuildExt(build_ext):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000147
Skip Montanarod1287322007-03-06 15:41:38 +0000148 def __init__(self, dist):
149 build_ext.__init__(self, dist)
150 self.failed = []
151
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000152 def build_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000153
154 # Detect which modules should be compiled
Skip Montanarod1287322007-03-06 15:41:38 +0000155 missing = self.detect_modules()
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000156
157 # Remove modules that are present on the disabled list
Christian Heimesb222bbc2008-01-18 09:51:43 +0000158 extensions = [ext for ext in self.extensions
159 if ext.name not in disabled_module_list]
160 # move ctypes to the end, it depends on other modules
161 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
162 if "_ctypes" in ext_map:
163 ctypes = extensions.pop(ext_map["_ctypes"])
164 extensions.append(ctypes)
165 self.extensions = extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +0000166
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000167 # Fix up the autodetected modules, prefixing all the source files
168 # with Modules/ and adding Python's include directory to the path.
169 (srcdir,) = sysconfig.get_config_vars('srcdir')
Guido van Rossume0fea6c2002-10-14 20:48:09 +0000170 if not srcdir:
171 # Maybe running on Windows but not using CYGWIN?
172 raise ValueError("No source directory; cannot proceed.")
Neil Schemenauer0189ddc2009-02-06 00:21:55 +0000173 srcdir = os.path.abspath(srcdir)
Neil Schemenauerc59c5f32009-02-05 16:32:29 +0000174 moddirlist = [os.path.join(srcdir, 'Modules')]
Michael W. Hudson5b109102002-01-23 15:04:41 +0000175
Jack Jansen144ebcc2001-08-05 22:31:19 +0000176 # Platform-dependent module source and include directories
Neil Schemenauer38870cb2009-02-05 22:14:04 +0000177 incdirlist = []
Jack Jansen144ebcc2001-08-05 22:31:19 +0000178 platform = self.get_platform()
Ronald Oussoren9545a232010-05-05 19:09:31 +0000179 if platform == 'darwin' and ("--disable-toolbox-glue" not in
Brett Cannoncc8a4f62004-08-26 01:44:07 +0000180 sysconfig.get_config_var("CONFIG_ARGS")):
Jack Jansen144ebcc2001-08-05 22:31:19 +0000181 # Mac OS X also includes some mac-specific modules
Neil Schemenauerc59c5f32009-02-05 16:32:29 +0000182 macmoddir = os.path.join(srcdir, 'Mac/Modules')
Jack Jansen144ebcc2001-08-05 22:31:19 +0000183 moddirlist.append(macmoddir)
Neil Schemenauer38870cb2009-02-05 22:14:04 +0000184 incdirlist.append(os.path.join(srcdir, 'Mac/Include'))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000185
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000186 # Fix up the paths for scripts, too
187 self.distribution.scripts = [os.path.join(srcdir, filename)
188 for filename in self.distribution.scripts]
189
Christian Heimes8608d912008-01-25 15:52:11 +0000190 # Python header files
Neil Schemenauerc59c5f32009-02-05 16:32:29 +0000191 headers = [sysconfig.get_config_h_filename()]
Stefan Krah4666ebd2012-02-29 14:17:18 +0100192 headers += glob(os.path.join(sysconfig.get_path('include'), "*.h"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000193 for ext in self.extensions[:]:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000194 ext.sources = [ find_module_file(filename, moddirlist)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000195 for filename in ext.sources ]
Jeremy Hylton340043e2002-06-13 17:38:11 +0000196 if ext.depends is not None:
Neil Schemenauerc59c5f32009-02-05 16:32:29 +0000197 ext.depends = [find_module_file(filename, moddirlist)
Jeremy Hylton340043e2002-06-13 17:38:11 +0000198 for filename in ext.depends]
Christian Heimes8608d912008-01-25 15:52:11 +0000199 else:
200 ext.depends = []
201 # re-compile extensions if a header file has been changed
202 ext.depends.extend(headers)
203
Neil Schemenauer38870cb2009-02-05 22:14:04 +0000204 # platform specific include directories
205 ext.include_dirs.extend(incdirlist)
206
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000207 # If a module has already been built statically,
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000208 # don't build it here
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000209 if ext.name in sys.builtin_module_names:
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000210 self.extensions.remove(ext)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000211
Ronald Oussoren9545a232010-05-05 19:09:31 +0000212 # Parse Modules/Setup and Modules/Setup.local to figure out which
213 # modules are turned on in the file.
214 remove_modules = []
215 for filename in ('Modules/Setup', 'Modules/Setup.local'):
216 input = text_file.TextFile(filename, join_lines=1)
217 while 1:
218 line = input.readline()
219 if not line: break
220 line = line.split()
221 remove_modules.append(line[0])
222 input.close()
Tim Peters1b27f862005-12-30 18:42:42 +0000223
Ronald Oussoren9545a232010-05-05 19:09:31 +0000224 for ext in self.extensions[:]:
225 if ext.name in remove_modules:
226 self.extensions.remove(ext)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000227
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000228 # When you run "make CC=altcc" or something similar, you really want
229 # those environment variables passed into the setup.py phase. Here's
230 # a small set of useful ones.
231 compiler = os.environ.get('CC')
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000232 args = {}
233 # unfortunately, distutils doesn't let us provide separate C and C++
234 # compilers
235 if compiler is not None:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000236 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
237 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000238 self.compiler.set_executables(**args)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000239
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000240 build_ext.build_extensions(self)
241
Skip Montanarod1287322007-03-06 15:41:38 +0000242 longest = max([len(e.name) for e in self.extensions])
243 if self.failed:
244 longest = max(longest, max([len(name) for name in self.failed]))
245
246 def print_three_column(lst):
Georg Brandle95cf1c2007-03-06 17:49:14 +0000247 lst.sort(key=str.lower)
Skip Montanarod1287322007-03-06 15:41:38 +0000248 # guarantee zip() doesn't drop anything
249 while len(lst) % 3:
250 lst.append("")
251 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
252 print "%-*s %-*s %-*s" % (longest, e, longest, f,
253 longest, g)
Skip Montanarod1287322007-03-06 15:41:38 +0000254
255 if missing:
256 print
Georg Brandl40f982f2008-12-28 11:58:49 +0000257 print ("Python build finished, but the necessary bits to build "
258 "these modules were not found:")
Skip Montanarod1287322007-03-06 15:41:38 +0000259 print_three_column(missing)
Jeffrey Yasskin87997562007-08-22 23:14:27 +0000260 print ("To find the necessary bits, look in setup.py in"
261 " detect_modules() for the module's name.")
262 print
Skip Montanarod1287322007-03-06 15:41:38 +0000263
264 if self.failed:
265 failed = self.failed[:]
266 print
267 print "Failed to build these modules:"
268 print_three_column(failed)
Jeffrey Yasskin87997562007-08-22 23:14:27 +0000269 print
Skip Montanarod1287322007-03-06 15:41:38 +0000270
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000271 def build_extension(self, ext):
272
Thomas Hellereba43c12006-04-07 19:04:09 +0000273 if ext.name == '_ctypes':
Thomas Heller795246c2006-04-07 19:27:56 +0000274 if not self.configure_ctypes(ext):
275 return
Thomas Hellereba43c12006-04-07 19:04:09 +0000276
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000277 try:
278 build_ext.build_extension(self, ext)
279 except (CCompilerError, DistutilsError), why:
280 self.announce('WARNING: building of extension "%s" failed: %s' %
281 (ext.name, sys.exc_info()[1]))
Skip Montanarod1287322007-03-06 15:41:38 +0000282 self.failed.append(ext.name)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000283 return
Jack Jansenf49c6f92001-11-01 14:44:15 +0000284 # Workaround for Mac OS X: The Carbon-based modules cannot be
285 # reliably imported into a command-line Python
286 if 'Carbon' in ext.extra_link_args:
Michael W. Hudson5b109102002-01-23 15:04:41 +0000287 self.announce(
288 'WARNING: skipping import check for Carbon-based "%s"' %
289 ext.name)
290 return
Ronald Oussoren5640ce22008-06-05 12:58:24 +0000291
292 if self.get_platform() == 'darwin' and (
293 sys.maxint > 2**32 and '-arch' in ext.extra_link_args):
294 # Don't bother doing an import check when an extension was
295 # build with an explicit '-arch' flag on OSX. That's currently
296 # only used to build 32-bit only extensions in a 4-way
297 # universal build and loading 32-bit code into a 64-bit
298 # process will fail.
299 self.announce(
300 'WARNING: skipping import check for "%s"' %
301 ext.name)
302 return
303
Jason Tishler24cf7762002-05-22 16:46:15 +0000304 # Workaround for Cygwin: Cygwin currently has fork issues when many
305 # modules have been imported
306 if self.get_platform() == 'cygwin':
307 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
308 % ext.name)
309 return
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000310 ext_filename = os.path.join(
311 self.build_lib,
312 self.get_ext_filename(self.get_ext_fullname(ext.name)))
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000313 try:
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000314 imp.load_dynamic(ext.name, ext_filename)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000315 except ImportError, why:
Skip Montanarod1287322007-03-06 15:41:38 +0000316 self.failed.append(ext.name)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000317 self.announce('*** WARNING: renaming "%s" since importing it'
318 ' failed: %s' % (ext.name, why), level=3)
319 assert not self.inplace
320 basename, tail = os.path.splitext(ext_filename)
321 newname = basename + "_failed" + tail
322 if os.path.exists(newname):
323 os.remove(newname)
324 os.rename(ext_filename, newname)
325
326 # XXX -- This relies on a Vile HACK in
327 # distutils.command.build_ext.build_extension(). The
328 # _built_objects attribute is stored there strictly for
329 # use here.
330 # If there is a failure, _built_objects may not be there,
331 # so catch the AttributeError and move on.
332 try:
333 for filename in self._built_objects:
334 os.remove(filename)
335 except AttributeError:
336 self.announce('unable to remove files (ignored)')
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000337 except:
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000338 exc_type, why, tb = sys.exc_info()
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000339 self.announce('*** WARNING: importing extension "%s" '
340 'failed with %s: %s' % (ext.name, exc_type, why),
341 level=3)
Skip Montanarod1287322007-03-06 15:41:38 +0000342 self.failed.append(ext.name)
Fred Drake9028d0a2001-12-06 22:59:54 +0000343
Neal Norwitz51dead72003-06-17 02:51:28 +0000344 def get_platform(self):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000345 # Get value of sys.platform
Neal Norwitz51dead72003-06-17 02:51:28 +0000346 for platform in ['cygwin', 'beos', 'darwin', 'atheos', 'osf1']:
347 if sys.platform.startswith(platform):
348 return platform
349 return sys.platform
Andrew M. Kuchling34febf52001-01-24 03:31:07 +0000350
Barry Warsawffc9caf2011-04-07 11:28:11 -0400351 def add_multiarch_paths(self):
352 # Debian/Ubuntu multiarch support.
353 # https://wiki.ubuntu.com/MultiarchSpec
doko@ubuntu.com3d2fc152012-09-21 13:51:40 +0200354 cc = sysconfig.get_config_var('CC')
355 tmpfile = os.path.join(self.build_temp, 'multiarch')
356 if not os.path.exists(self.build_temp):
357 os.makedirs(self.build_temp)
358 ret = os.system(
359 '%s -print-multiarch > %s 2> /dev/null' % (cc, tmpfile))
360 multiarch_path_component = ''
361 try:
362 if ret >> 8 == 0:
363 with open(tmpfile) as fp:
364 multiarch_path_component = fp.readline().strip()
365 finally:
366 os.unlink(tmpfile)
367
368 if multiarch_path_component != '':
369 add_dir_to_list(self.compiler.library_dirs,
370 '/usr/lib/' + multiarch_path_component)
371 add_dir_to_list(self.compiler.include_dirs,
372 '/usr/include/' + multiarch_path_component)
373 return
374
Barry Warsawffc9caf2011-04-07 11:28:11 -0400375 if not find_executable('dpkg-architecture'):
376 return
377 tmpfile = os.path.join(self.build_temp, 'multiarch')
378 if not os.path.exists(self.build_temp):
379 os.makedirs(self.build_temp)
380 ret = os.system(
381 'dpkg-architecture -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
382 tmpfile)
383 try:
384 if ret >> 8 == 0:
385 with open(tmpfile) as fp:
386 multiarch_path_component = fp.readline().strip()
387 add_dir_to_list(self.compiler.library_dirs,
388 '/usr/lib/' + multiarch_path_component)
389 add_dir_to_list(self.compiler.include_dirs,
390 '/usr/include/' + multiarch_path_component)
391 finally:
392 os.unlink(tmpfile)
393
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000394 def detect_modules(self):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000395 # Ensure that /usr/local is always used
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000396 add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
397 add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
Barry Warsawffc9caf2011-04-07 11:28:11 -0400398 self.add_multiarch_paths()
Michael W. Hudson39230b32002-01-16 15:26:48 +0000399
Brett Cannon516592f2004-12-07 00:42:59 +0000400 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000401 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000402 # We must get the values from the Makefile and not the environment
403 # directly since an inconsistently reproducible issue comes up where
404 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000405 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000406 for env_var, arg_name, dir_list in (
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000407 ('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
408 ('LDFLAGS', '-L', self.compiler.library_dirs),
409 ('CPPFLAGS', '-I', self.compiler.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000410 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000411 if env_val:
Brett Cannon4810eb92004-12-31 08:11:21 +0000412 # To prevent optparse from raising an exception about any
Skip Montanaroa46ed912008-10-07 02:02:00 +0000413 # options in env_val that it doesn't know about we strip out
Brett Cannon4810eb92004-12-31 08:11:21 +0000414 # all double dashes and any dashes followed by a character
415 # that is not for the option we are dealing with.
416 #
417 # Please note that order of the regex is important! We must
418 # strip out double-dashes first so that we don't end up with
419 # substituting "--Long" to "-Long" and thus lead to "ong" being
420 # used for a library directory.
Georg Brandl915c87d2007-08-24 11:47:37 +0000421 env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
422 ' ', env_val)
Brett Cannon84667c02004-12-07 03:25:18 +0000423 parser = optparse.OptionParser()
Brett Cannon4810eb92004-12-31 08:11:21 +0000424 # Make sure that allowing args interspersed with options is
425 # allowed
426 parser.allow_interspersed_args = True
427 parser.error = lambda msg: None
Brett Cannon84667c02004-12-07 03:25:18 +0000428 parser.add_option(arg_name, dest="dirs", action="append")
429 options = parser.parse_args(env_val.split())[0]
Brett Cannon44837712005-01-02 21:54:07 +0000430 if options.dirs:
Brett Cannon861e3962008-02-03 02:08:45 +0000431 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000432 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000433
Ronald Oussoren24215252010-10-20 13:11:46 +0000434 if os.path.normpath(sys.prefix) != '/usr' \
435 and not sysconfig.get_config_var('PYTHONFRAMEWORK'):
436 # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
437 # (PYTHONFRAMEWORK is set) to avoid # linking problems when
438 # building a framework with different architectures than
439 # the one that is currently installed (issue #7473)
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000440 add_dir_to_list(self.compiler.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000441 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000442 add_dir_to_list(self.compiler.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000443 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000444
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000445 try:
446 have_unicode = unicode
447 except NameError:
448 have_unicode = 0
449
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000450 # lib_dirs and inc_dirs are used to search for files;
451 # if a file is found in one of those directories, it can
452 # be assumed that no additional -I,-L directives are needed.
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000453 lib_dirs = self.compiler.library_dirs + [
Martin v. Löwisfba73692004-11-13 11:13:35 +0000454 '/lib64', '/usr/lib64',
455 '/lib', '/usr/lib',
456 ]
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000457 inc_dirs = self.compiler.include_dirs + ['/usr/include']
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000458 exts = []
Skip Montanarod1287322007-03-06 15:41:38 +0000459 missing = []
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000460
Brett Cannon4454a1f2005-04-15 20:32:39 +0000461 config_h = sysconfig.get_config_h_filename()
462 config_h_vars = sysconfig.parse_config_h(open(config_h))
463
Fredrik Lundhade711a2001-01-24 08:00:28 +0000464 platform = self.get_platform()
Neil Schemenauerc59c5f32009-02-05 16:32:29 +0000465 srcdir = sysconfig.get_config_var('srcdir')
Michael W. Hudson5b109102002-01-23 15:04:41 +0000466
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000467 # Check for AtheOS which has libraries in non-standard locations
468 if platform == 'atheos':
469 lib_dirs += ['/system/libs', '/atheos/autolnk/lib']
470 lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
471 inc_dirs += ['/system/include', '/atheos/autolnk/include']
472 inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
473
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000474 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
475 if platform in ['osf1', 'unixware7', 'openunix8']:
Skip Montanaro22e00c42003-05-06 20:43:34 +0000476 lib_dirs += ['/usr/ccs/lib']
477
Charles-François Natali0d3db3a2012-04-12 19:11:54 +0200478 # HP-UX11iv3 keeps files in lib/hpux folders.
479 if platform == 'hp-ux11':
480 lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
481
Ronald Oussoren39be38c2006-05-26 11:38:39 +0000482 if platform == 'darwin':
483 # This should work on any unixy platform ;-)
484 # If the user has bothered specifying additional -I and -L flags
485 # in OPT and LDFLAGS we might as well use them here.
486 # NOTE: using shlex.split would technically be more correct, but
487 # also gives a bootstrap problem. Let's hope nobody uses directories
488 # with whitespace in the name to store libraries.
489 cflags, ldflags = sysconfig.get_config_vars(
490 'CFLAGS', 'LDFLAGS')
491 for item in cflags.split():
492 if item.startswith('-I'):
493 inc_dirs.append(item[2:])
494
495 for item in ldflags.split():
496 if item.startswith('-L'):
497 lib_dirs.append(item[2:])
498
Fredrik Lundhade711a2001-01-24 08:00:28 +0000499 # Check for MacOS X, which doesn't need libm.a at all
500 math_libs = ['m']
Ronald Oussoren9545a232010-05-05 19:09:31 +0000501 if platform in ['darwin', 'beos']:
Fredrik Lundhade711a2001-01-24 08:00:28 +0000502 math_libs = []
Michael W. Hudson5b109102002-01-23 15:04:41 +0000503
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000504 # XXX Omitted modules: gl, pure, dl, SGI-specific modules
505
506 #
507 # The following modules are all pretty straightforward, and compile
508 # on pretty much any POSIXish platform.
509 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000510
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000511 # Some modules that are normally always on:
Georg Brandlfa8fa0c2010-08-21 13:05:38 +0000512 #exts.append( Extension('_weakref', ['_weakref.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000513
514 # array objects
515 exts.append( Extension('array', ['arraymodule.c']) )
516 # complex math library functions
Mark Dickinson12748b02009-12-21 15:22:00 +0000517 exts.append( Extension('cmath', ['cmathmodule.c', '_math.c'],
518 depends=['_math.h'],
Andrew M. Kuchling5ddb25f2001-01-23 22:21:11 +0000519 libraries=math_libs) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000520 # math library functions, e.g. sin()
Mark Dickinson9cae1782009-12-16 20:13:40 +0000521 exts.append( Extension('math', ['mathmodule.c', '_math.c'],
Mark Dickinson1c498282009-12-17 08:33:56 +0000522 depends=['_math.h'],
Andrew M. Kuchling5ddb25f2001-01-23 22:21:11 +0000523 libraries=math_libs) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000524 # fast string operations implemented in C
525 exts.append( Extension('strop', ['stropmodule.c']) )
526 # time operations and variables
Andrew M. Kuchling5ddb25f2001-01-23 22:21:11 +0000527 exts.append( Extension('time', ['timemodule.c'],
528 libraries=math_libs) )
Brett Cannon057e7202004-06-24 01:38:47 +0000529 exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'],
Guido van Rossuma29d5082002-12-16 20:31:57 +0000530 libraries=math_libs) )
Neal Norwitz0d2192b2008-03-23 06:13:25 +0000531 # fast iterator tools implemented in C
532 exts.append( Extension("itertools", ["itertoolsmodule.c"]) )
Eric Smitha73fbe72008-02-23 03:09:44 +0000533 # code that will be builtins in the future, but conflict with the
534 # current builtins
535 exts.append( Extension('future_builtins', ['future_builtins.c']) )
Raymond Hettinger40f62172002-12-29 23:03:38 +0000536 # random number generator implemented in C
Tim Peters2c60f7a2003-01-29 03:49:43 +0000537 exts.append( Extension("_random", ["_randommodule.c"]) )
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000538 # high-performance collections
Raymond Hettingereb979882007-02-28 18:37:52 +0000539 exts.append( Extension("_collections", ["_collectionsmodule.c"]) )
Raymond Hettinger0c410272004-01-05 10:13:35 +0000540 # bisect
541 exts.append( Extension("_bisect", ["_bisectmodule.c"]) )
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000542 # heapq
Raymond Hettingerc46cb2a2004-04-19 19:06:21 +0000543 exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000544 # operator.add() and similar goodies
545 exts.append( Extension('operator', ['operator.c']) )
Antoine Pitrou19690592009-06-12 20:14:08 +0000546 # Python 3.1 _io library
547 exts.append( Extension("_io",
548 ["_io/bufferedio.c", "_io/bytesio.c", "_io/fileio.c",
549 "_io/iobase.c", "_io/_iomodule.c", "_io/stringio.c", "_io/textio.c"],
550 depends=["_io/_iomodule.h"], include_dirs=["Modules/_io"]))
Nick Coghlanc649ec52006-05-29 12:43:05 +0000551 # _functools
552 exts.append( Extension("_functools", ["_functoolsmodule.c"]) )
Brett Cannon4b964f92008-05-05 20:21:38 +0000553 # _json speedups
554 exts.append( Extension("_json", ["_json.c"]) )
Marc-André Lemburg261b8e22001-02-02 12:12:44 +0000555 # Python C API test module
Mark Dickinsond155bbf2009-02-10 16:17:16 +0000556 exts.append( Extension('_testcapi', ['_testcapimodule.c'],
557 depends=['testcapi_long.h']) )
Armin Rigoa871ef22006-02-08 12:53:56 +0000558 # profilers (_lsprof is for cProfile.py)
559 exts.append( Extension('_hotshot', ['_hotshot.c']) )
560 exts.append( Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000561 # static Unicode character database
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000562 if have_unicode:
563 exts.append( Extension('unicodedata', ['unicodedata.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +0000564 else:
565 missing.append('unicodedata')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000566 # access to ISO C locale support
Martin v. Löwis19d17342003-06-14 21:03:05 +0000567 data = open('pyconfig.h').read()
568 m = re.search(r"#s*define\s+WITH_LIBINTL\s+1\s*", data)
569 if m is not None:
Jason Tishlerd28216b2002-08-14 11:13:52 +0000570 locale_libs = ['intl']
571 else:
572 locale_libs = []
Jack Jansen84b74472004-07-15 19:56:25 +0000573 if platform == 'darwin':
574 locale_extra_link_args = ['-framework', 'CoreFoundation']
575 else:
576 locale_extra_link_args = []
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000577
Jack Jansen84b74472004-07-15 19:56:25 +0000578
Jason Tishlerd28216b2002-08-14 11:13:52 +0000579 exts.append( Extension('_locale', ['_localemodule.c'],
Jack Jansen84b74472004-07-15 19:56:25 +0000580 libraries=locale_libs,
581 extra_link_args=locale_extra_link_args) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000582
583 # Modules with some UNIX dependencies -- on by default:
584 # (If you have a really backward UNIX, select and socket may not be
585 # supported...)
586
587 # fcntl(2) and ioctl(2)
Antoine Pitrou85729812010-09-07 14:55:24 +0000588 libs = []
589 if (config_h_vars.get('FLOCK_NEEDS_LIBBSD', False)):
590 # May be necessary on AIX for flock function
591 libs = ['bsd']
592 exts.append( Extension('fcntl', ['fcntlmodule.c'], libraries=libs) )
Ronald Oussoren9545a232010-05-05 19:09:31 +0000593 # pwd(3)
594 exts.append( Extension('pwd', ['pwdmodule.c']) )
595 # grp(3)
596 exts.append( Extension('grp', ['grpmodule.c']) )
597 # spwd, shadow passwords
598 if (config_h_vars.get('HAVE_GETSPNAM', False) or
599 config_h_vars.get('HAVE_GETSPENT', False)):
600 exts.append( Extension('spwd', ['spwdmodule.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +0000601 else:
Ronald Oussoren9545a232010-05-05 19:09:31 +0000602 missing.append('spwd')
Skip Montanarod1287322007-03-06 15:41:38 +0000603
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000604 # select(2); not on ancient System V
605 exts.append( Extension('select', ['selectmodule.c']) )
606
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000607 # Fred Drake's interface to the Python parser
608 exts.append( Extension('parser', ['parsermodule.c']) )
609
Guido van Rossum2e1c09c2002-04-04 17:52:50 +0000610 # cStringIO and cPickle
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000611 exts.append( Extension('cStringIO', ['cStringIO.c']) )
612 exts.append( Extension('cPickle', ['cPickle.c']) )
613
614 # Memory-mapped files (also works on Win32).
Ronald Oussoren9545a232010-05-05 19:09:31 +0000615 if platform not in ['atheos']:
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000616 exts.append( Extension('mmap', ['mmapmodule.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +0000617 else:
618 missing.append('mmap')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000619
Andrew M. Kuchling57269d02004-08-31 13:37:25 +0000620 # Lance Ellinghaus's syslog module
Ronald Oussoren9545a232010-05-05 19:09:31 +0000621 # syslog daemon interface
622 exts.append( Extension('syslog', ['syslogmodule.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000623
624 # George Neville-Neil's timing module:
Neal Norwitz6143c542006-03-03 00:48:46 +0000625 # Deprecated in PEP 4 http://www.python.org/peps/pep-0004.html
626 # http://mail.python.org/pipermail/python-dev/2006-January/060023.html
627 #exts.append( Extension('timing', ['timingmodule.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000628
629 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000630 # Here ends the simple stuff. From here on, modules need certain
631 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000632 #
633
634 # Multimedia modules
635 # These don't work for 64-bit platforms!!!
636 # These represent audio samples or images as strings:
637
Neal Norwitz5e4a3b82004-07-19 16:55:07 +0000638 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +0000639 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +0000640 # 64-bit platforms.
641 exts.append( Extension('audioop', ['audioop.c']) )
642
Fredrik Lundhade711a2001-01-24 08:00:28 +0000643 # Disabled on 64-bit platforms
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000644 if sys.maxint != 9223372036854775807L:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000645 # Operations on images
646 exts.append( Extension('imageop', ['imageop.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +0000647 else:
Brett Cannondc48b742007-05-20 07:09:50 +0000648 missing.extend(['imageop'])
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000649
650 # readline
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000651 do_readline = self.compiler.find_library_file(lib_dirs, 'readline')
Stefan Krah449aa862010-06-03 12:39:50 +0000652 readline_termcap_library = ""
653 curses_library = ""
654 # Determine if readline is already linked against curses or tinfo.
Stefan Krah4d32c9c2010-06-04 09:49:20 +0000655 if do_readline and find_executable('ldd'):
Stefan Krah449aa862010-06-03 12:39:50 +0000656 fp = os.popen("ldd %s" % do_readline)
Stefan Krah2e26e232010-07-17 12:21:08 +0000657 ldd_output = fp.readlines()
658 ret = fp.close()
659 if ret is None or ret >> 8 == 0:
660 for ln in ldd_output:
661 if 'curses' in ln:
662 readline_termcap_library = re.sub(
663 r'.*lib(n?cursesw?)\.so.*', r'\1', ln
664 ).rstrip()
665 break
666 if 'tinfo' in ln: # termcap interface split out from ncurses
667 readline_termcap_library = 'tinfo'
668 break
Stefan Krah449aa862010-06-03 12:39:50 +0000669 # Issue 7384: If readline is already linked against curses,
670 # use the same library for the readline and curses modules.
671 if 'curses' in readline_termcap_library:
672 curses_library = readline_termcap_library
Stefan Krah23152ea2010-06-03 14:25:16 +0000673 elif self.compiler.find_library_file(lib_dirs, 'ncursesw'):
Stefan Krah449aa862010-06-03 12:39:50 +0000674 curses_library = 'ncursesw'
Stefan Krah23152ea2010-06-03 14:25:16 +0000675 elif self.compiler.find_library_file(lib_dirs, 'ncurses'):
Stefan Krah449aa862010-06-03 12:39:50 +0000676 curses_library = 'ncurses'
Stefan Krah23152ea2010-06-03 14:25:16 +0000677 elif self.compiler.find_library_file(lib_dirs, 'curses'):
Stefan Krah449aa862010-06-03 12:39:50 +0000678 curses_library = 'curses'
679
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000680 if platform == 'darwin':
681 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren05b0d1d2010-03-08 07:06:47 +0000682 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
683 if dep_target and dep_target.split('.') < ['10', '5']:
684 os_release = 8
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000685 if os_release < 9:
686 # MacOSX 10.4 has a broken readline. Don't try to build
687 # the readline module unless the user has installed a fixed
688 # readline package
689 if find_file('readline/rlconf.h', inc_dirs, []) is None:
690 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +0000691 if do_readline:
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000692 if platform == 'darwin' and os_release < 9:
Ronald Oussoren39be38c2006-05-26 11:38:39 +0000693 # In every directory on the search path search for a dynamic
694 # library and then a static library, instead of first looking
695 # for dynamic libraries on the entiry path.
696 # This way a staticly linked custom readline gets picked up
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000697 # before the (possibly broken) dynamic library in /usr/lib.
Ronald Oussoren39be38c2006-05-26 11:38:39 +0000698 readline_extra_link_args = ('-Wl,-search_paths_first',)
699 else:
700 readline_extra_link_args = ()
701
Marc-André Lemburg2efc3232001-01-26 18:23:02 +0000702 readline_libs = ['readline']
Stefan Krah449aa862010-06-03 12:39:50 +0000703 if readline_termcap_library:
704 pass # Issue 7384: Already linked against curses or tinfo.
705 elif curses_library:
706 readline_libs.append(curses_library)
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000707 elif self.compiler.find_library_file(lib_dirs +
Tarek Ziadée670e5a2009-07-06 12:50:46 +0000708 ['/usr/lib/termcap'],
709 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +0000710 readline_libs.append('termcap')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000711 exts.append( Extension('readline', ['readline.c'],
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000712 library_dirs=['/usr/lib/termcap'],
Ronald Oussoren39be38c2006-05-26 11:38:39 +0000713 extra_link_args=readline_extra_link_args,
Marc-André Lemburg2efc3232001-01-26 18:23:02 +0000714 libraries=readline_libs) )
Skip Montanarod1287322007-03-06 15:41:38 +0000715 else:
716 missing.append('readline')
717
Ronald Oussoren9545a232010-05-05 19:09:31 +0000718 # crypt module.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000719
Ronald Oussoren9545a232010-05-05 19:09:31 +0000720 if self.compiler.find_library_file(lib_dirs, 'crypt'):
721 libs = ['crypt']
Skip Montanarod1287322007-03-06 15:41:38 +0000722 else:
Ronald Oussoren9545a232010-05-05 19:09:31 +0000723 libs = []
724 exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000725
Skip Montanaroba9e9782003-03-20 23:34:22 +0000726 # CSV files
727 exts.append( Extension('_csv', ['_csv.c']) )
728
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000729 # socket(2)
Guido van Rossum47d3a7a2002-06-13 14:41:32 +0000730 exts.append( Extension('_socket', ['socketmodule.c'],
Jeremy Hylton340043e2002-06-13 17:38:11 +0000731 depends = ['socketmodule.h']) )
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000732 # Detect SSL support for the socket module (via _ssl)
Gregory P. Smithade97332005-08-23 21:19:40 +0000733 search_for_ssl_incs_in = [
734 '/usr/local/ssl/include',
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000735 '/usr/contrib/ssl/include/'
736 ]
Gregory P. Smithade97332005-08-23 21:19:40 +0000737 ssl_incs = find_file('openssl/ssl.h', inc_dirs,
738 search_for_ssl_incs_in
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000739 )
Martin v. Löwisa950f7f2003-05-09 09:05:19 +0000740 if ssl_incs is not None:
741 krb5_h = find_file('krb5.h', inc_dirs,
742 ['/usr/kerberos/include'])
743 if krb5_h:
744 ssl_incs += krb5_h
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000745 ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000746 ['/usr/local/ssl/lib',
747 '/usr/contrib/ssl/lib/'
748 ] )
Fredrik Lundhade711a2001-01-24 08:00:28 +0000749
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000750 if (ssl_incs is not None and
751 ssl_libs is not None):
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000752 exts.append( Extension('_ssl', ['_ssl.c'],
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000753 include_dirs = ssl_incs,
Fredrik Lundhade711a2001-01-24 08:00:28 +0000754 library_dirs = ssl_libs,
Guido van Rossum47d3a7a2002-06-13 14:41:32 +0000755 libraries = ['ssl', 'crypto'],
Jeremy Hylton340043e2002-06-13 17:38:11 +0000756 depends = ['socketmodule.h']), )
Skip Montanarod1287322007-03-06 15:41:38 +0000757 else:
758 missing.append('_ssl')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000759
Gregory P. Smithade97332005-08-23 21:19:40 +0000760 # find out which version of OpenSSL we have
761 openssl_ver = 0
762 openssl_ver_re = re.compile(
763 '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
Gregory P. Smithade97332005-08-23 21:19:40 +0000764
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000765 # look for the openssl version header on the compiler search path.
766 opensslv_h = find_file('openssl/opensslv.h', [],
767 inc_dirs + search_for_ssl_incs_in)
768 if opensslv_h:
769 name = os.path.join(opensslv_h[0], 'openssl/opensslv.h')
770 if sys.platform == 'darwin' and is_macosx_sdk_path(name):
771 name = os.path.join(macosx_sdk_root(), name[1:])
772 try:
773 incfile = open(name, 'r')
774 for line in incfile:
775 m = openssl_ver_re.match(line)
776 if m:
777 openssl_ver = eval(m.group(1))
778 except IOError, msg:
779 print "IOError while reading opensshv.h:", msg
780 pass
Gregory P. Smithade97332005-08-23 21:19:40 +0000781
Gregory P. Smithc2fa18c2010-01-02 22:25:29 +0000782 min_openssl_ver = 0x00907000
Gregory P. Smithffd5d882010-01-03 00:43:02 +0000783 have_any_openssl = ssl_incs is not None and ssl_libs is not None
784 have_usable_openssl = (have_any_openssl and
Gregory P. Smithc2fa18c2010-01-02 22:25:29 +0000785 openssl_ver >= min_openssl_ver)
Gregory P. Smithade97332005-08-23 21:19:40 +0000786
Gregory P. Smithffd5d882010-01-03 00:43:02 +0000787 if have_any_openssl:
788 if have_usable_openssl:
789 # The _hashlib module wraps optimized implementations
790 # of hash functions from the OpenSSL library.
791 exts.append( Extension('_hashlib', ['_hashopenssl.c'],
792 include_dirs = ssl_incs,
793 library_dirs = ssl_libs,
794 libraries = ['ssl', 'crypto']) )
795 else:
796 print ("warning: openssl 0x%08x is too old for _hashlib" %
797 openssl_ver)
798 missing.append('_hashlib')
Gregory P. Smithc2fa18c2010-01-02 22:25:29 +0000799 if COMPILED_WITH_PYDEBUG or not have_usable_openssl:
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000800 # The _sha module implements the SHA1 hash algorithm.
801 exts.append( Extension('_sha', ['shamodule.c']) )
802 # The _md5 module implements the RSA Data Security, Inc. MD5
803 # Message-Digest Algorithm, described in RFC 1321. The
Matthias Klose8e39ec72006-04-03 16:27:50 +0000804 # necessary files md5.c and md5.h are included here.
Gregory P. Smithd7923922006-06-05 23:38:06 +0000805 exts.append( Extension('_md5',
806 sources = ['md5module.c', 'md5.c'],
807 depends = ['md5.h']) )
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000808
Gregory P. Smithc2fa18c2010-01-02 22:25:29 +0000809 min_sha2_openssl_ver = 0x00908000
810 if COMPILED_WITH_PYDEBUG or openssl_ver < min_sha2_openssl_ver:
Gregory P. Smithade97332005-08-23 21:19:40 +0000811 # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
812 exts.append( Extension('_sha256', ['sha256module.c']) )
813 exts.append( Extension('_sha512', ['sha512module.c']) )
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000814
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000815 # Modules that provide persistent dictionary-like semantics. You will
816 # probably want to arrange for at least one of them to be available on
817 # your machine, though none are defined by default because of library
818 # dependencies. The Python module anydbm.py provides an
819 # implementation independent wrapper for these; dumbdbm.py provides
820 # similar functionality (but slower of course) implemented in Python.
821
Gregory P. Smith1475cd82007-10-06 07:51:59 +0000822 # Sleepycat^WOracle Berkeley DB interface.
823 # http://www.oracle.com/database/berkeley-db/db/index.html
Skip Montanaro57454e52002-06-14 20:30:31 +0000824 #
Gregory P. Smith4eb60e52007-08-26 00:26:00 +0000825 # This requires the Sleepycat^WOracle DB code. The supported versions
Gregory P. Smithe7f4d842007-10-09 18:26:02 +0000826 # are set below. Visit the URL above to download
Gregory P. Smith3adc4aa2006-04-13 19:19:01 +0000827 # a release. Most open source OSes come with one or more
828 # versions of BerkeleyDB already installed.
Skip Montanaro57454e52002-06-14 20:30:31 +0000829
Matthias Klose54cc5392010-03-15 12:46:18 +0000830 max_db_ver = (4, 8)
Jesus Cea6557aac2010-03-22 14:22:26 +0000831 min_db_ver = (4, 1)
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000832 db_setup_debug = False # verbose debug prints from this script?
Skip Montanaro57454e52002-06-14 20:30:31 +0000833
Gregory P. Smith0902cac2008-05-27 08:40:09 +0000834 def allow_db_ver(db_ver):
835 """Returns a boolean if the given BerkeleyDB version is acceptable.
836
837 Args:
838 db_ver: A tuple of the version to verify.
839 """
840 if not (min_db_ver <= db_ver <= max_db_ver):
841 return False
842 # Use this function to filter out known bad configurations.
843 if (4, 6) == db_ver[:2]:
844 # BerkeleyDB 4.6.x is not stable on many architectures.
845 arch = platform_machine()
846 if arch not in ('i386', 'i486', 'i586', 'i686',
847 'x86_64', 'ia64'):
848 return False
849 return True
850
851 def gen_db_minor_ver_nums(major):
852 if major == 4:
853 for x in range(max_db_ver[1]+1):
854 if allow_db_ver((4, x)):
855 yield x
856 elif major == 3:
857 for x in (3,):
858 if allow_db_ver((3, x)):
859 yield x
860 else:
861 raise ValueError("unknown major BerkeleyDB version", major)
862
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000863 # construct a list of paths to look for the header file in on
864 # top of the normal inc_dirs.
865 db_inc_paths = [
866 '/usr/include/db4',
867 '/usr/local/include/db4',
868 '/opt/sfw/include/db4',
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000869 '/usr/include/db3',
870 '/usr/local/include/db3',
871 '/opt/sfw/include/db3',
Skip Montanaro00c5a012007-03-04 20:52:28 +0000872 # Fink defaults (http://fink.sourceforge.net/)
873 '/sw/include/db4',
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000874 '/sw/include/db3',
875 ]
876 # 4.x minor number specific paths
Gregory P. Smith0902cac2008-05-27 08:40:09 +0000877 for x in gen_db_minor_ver_nums(4):
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000878 db_inc_paths.append('/usr/include/db4%d' % x)
Neal Norwitz8f401712005-10-20 05:28:29 +0000879 db_inc_paths.append('/usr/include/db4.%d' % x)
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000880 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
881 db_inc_paths.append('/usr/local/include/db4%d' % x)
882 db_inc_paths.append('/pkg/db-4.%d/include' % x)
Gregory P. Smith29602d22006-01-24 09:46:48 +0000883 db_inc_paths.append('/opt/db-4.%d/include' % x)
Skip Montanaro00c5a012007-03-04 20:52:28 +0000884 # MacPorts default (http://www.macports.org/)
885 db_inc_paths.append('/opt/local/include/db4%d' % x)
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000886 # 3.x minor number specific paths
Gregory P. Smith0902cac2008-05-27 08:40:09 +0000887 for x in gen_db_minor_ver_nums(3):
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000888 db_inc_paths.append('/usr/include/db3%d' % x)
889 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
890 db_inc_paths.append('/usr/local/include/db3%d' % x)
891 db_inc_paths.append('/pkg/db-3.%d/include' % x)
Gregory P. Smith29602d22006-01-24 09:46:48 +0000892 db_inc_paths.append('/opt/db-3.%d/include' % x)
Tim Peters2c60f7a2003-01-29 03:49:43 +0000893
Ronald Oussoren9b8b6192006-06-27 12:53:52 +0000894 # Add some common subdirectories for Sleepycat DB to the list,
895 # based on the standard include directories. This way DB3/4 gets
896 # picked up when it is installed in a non-standard prefix and
897 # the user has added that prefix into inc_dirs.
898 std_variants = []
899 for dn in inc_dirs:
900 std_variants.append(os.path.join(dn, 'db3'))
901 std_variants.append(os.path.join(dn, 'db4'))
Gregory P. Smith0902cac2008-05-27 08:40:09 +0000902 for x in gen_db_minor_ver_nums(4):
Ronald Oussoren9b8b6192006-06-27 12:53:52 +0000903 std_variants.append(os.path.join(dn, "db4%d"%x))
904 std_variants.append(os.path.join(dn, "db4.%d"%x))
Gregory P. Smith0902cac2008-05-27 08:40:09 +0000905 for x in gen_db_minor_ver_nums(3):
Ronald Oussoren9b8b6192006-06-27 12:53:52 +0000906 std_variants.append(os.path.join(dn, "db3%d"%x))
907 std_variants.append(os.path.join(dn, "db3.%d"%x))
908
Tim Peters38ff36c2006-06-30 06:18:39 +0000909 db_inc_paths = std_variants + db_inc_paths
Skip Montanaro00c5a012007-03-04 20:52:28 +0000910 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
Ronald Oussoren9b8b6192006-06-27 12:53:52 +0000911
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000912 db_ver_inc_map = {}
913
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000914 if sys.platform == 'darwin':
915 sysroot = macosx_sdk_root()
916
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000917 class db_found(Exception): pass
Skip Montanaro57454e52002-06-14 20:30:31 +0000918 try:
Martin v. Löwis05d4d562002-12-06 10:25:02 +0000919 # See whether there is a Sleepycat header in the standard
920 # search path.
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000921 for d in inc_dirs + db_inc_paths:
Martin v. Löwis05d4d562002-12-06 10:25:02 +0000922 f = os.path.join(d, "db.h")
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000923
924 if sys.platform == 'darwin' and is_macosx_sdk_path(d):
925 f = os.path.join(sysroot, d[1:], "db.h")
926
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000927 if db_setup_debug: print "db: looking for db.h in", f
Martin v. Löwis05d4d562002-12-06 10:25:02 +0000928 if os.path.exists(f):
929 f = open(f).read()
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000930 m = re.search(r"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Martin v. Löwis05d4d562002-12-06 10:25:02 +0000931 if m:
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000932 db_major = int(m.group(1))
933 m = re.search(r"#define\WDB_VERSION_MINOR\W(\d+)", f)
934 db_minor = int(m.group(1))
935 db_ver = (db_major, db_minor)
936
Gregory P. Smith1475cd82007-10-06 07:51:59 +0000937 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
938 if db_ver == (4, 6):
939 m = re.search(r"#define\WDB_VERSION_PATCH\W(\d+)", f)
940 db_patch = int(m.group(1))
941 if db_patch < 21:
942 print "db.h:", db_ver, "patch", db_patch,
943 print "being ignored (4.6.x must be >= 4.6.21)"
944 continue
945
Antoine Pitrou8c510e72010-01-13 11:47:49 +0000946 if ( (db_ver not in db_ver_inc_map) and
Gregory P. Smith0902cac2008-05-27 08:40:09 +0000947 allow_db_ver(db_ver) ):
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000948 # save the include directory with the db.h version
Skip Montanaro00c5a012007-03-04 20:52:28 +0000949 # (first occurrence only)
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000950 db_ver_inc_map[db_ver] = d
Andrew M. Kuchling738446f42006-10-27 18:13:46 +0000951 if db_setup_debug:
952 print "db.h: found", db_ver, "in", d
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000953 else:
954 # we already found a header for this library version
955 if db_setup_debug: print "db.h: ignoring", d
956 else:
957 # ignore this header, it didn't contain a version number
Skip Montanaro00c5a012007-03-04 20:52:28 +0000958 if db_setup_debug:
959 print "db.h: no version number version in", d
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000960
961 db_found_vers = db_ver_inc_map.keys()
962 db_found_vers.sort()
963
964 while db_found_vers:
965 db_ver = db_found_vers.pop()
966 db_incdir = db_ver_inc_map[db_ver]
967
968 # check lib directories parallel to the location of the header
969 db_dirs_to_check = [
Skip Montanaro00c5a012007-03-04 20:52:28 +0000970 db_incdir.replace("include", 'lib64'),
971 db_incdir.replace("include", 'lib'),
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000972 ]
Ronald Oussoren593e4ca2010-06-03 09:47:21 +0000973
974 if sys.platform != 'darwin':
975 db_dirs_to_check = filter(os.path.isdir, db_dirs_to_check)
976
977 else:
978 # Same as other branch, but takes OSX SDK into account
979 tmp = []
980 for dn in db_dirs_to_check:
981 if is_macosx_sdk_path(dn):
982 if os.path.isdir(os.path.join(sysroot, dn[1:])):
983 tmp.append(dn)
984 else:
985 if os.path.isdir(dn):
986 tmp.append(dn)
Ronald Oussorencd172132010-06-27 12:36:16 +0000987 db_dirs_to_check = tmp
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000988
Ezio Melotti24b07bc2011-03-15 18:55:01 +0200989 # Look for a version specific db-X.Y before an ambiguous dbX
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000990 # XXX should we -ever- look for a dbX name? Do any
991 # systems really not name their library by version and
992 # symlink to more general names?
Andrew MacIntyre953f98d2005-03-09 22:21:08 +0000993 for dblib in (('db-%d.%d' % db_ver),
994 ('db%d%d' % db_ver),
995 ('db%d' % db_ver[0])):
Tarek Ziadé35a3f572010-03-05 00:29:38 +0000996 dblib_file = self.compiler.find_library_file(
Gregory P. Smithe76c8c02004-12-13 12:01:24 +0000997 db_dirs_to_check + lib_dirs, dblib )
998 if dblib_file:
999 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
1000 raise db_found
1001 else:
1002 if db_setup_debug: print "db lib: ", dblib, "not found"
1003
1004 except db_found:
Brett Cannonef3dab22008-05-29 21:23:33 +00001005 if db_setup_debug:
1006 print "bsddb using BerkeleyDB lib:", db_ver, dblib
1007 print "bsddb lib dir:", dblib_dir, " inc dir:", db_incdir
Gregory P. Smithe76c8c02004-12-13 12:01:24 +00001008 db_incs = [db_incdir]
Jack Jansend1b20452002-07-08 21:39:36 +00001009 dblibs = [dblib]
Gregory P. Smithe76c8c02004-12-13 12:01:24 +00001010 # We add the runtime_library_dirs argument because the
1011 # BerkeleyDB lib we're linking against often isn't in the
1012 # system dynamic library search path. This is usually
1013 # correct and most trouble free, but may cause problems in
1014 # some unusual system configurations (e.g. the directory
1015 # is on an NFS server that goes away).
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +00001016 exts.append(Extension('_bsddb', ['_bsddb.c'],
Gregory P. Smith39250532007-10-09 06:02:21 +00001017 depends = ['bsddb.h'],
Martin v. Löwis05d4d562002-12-06 10:25:02 +00001018 library_dirs=dblib_dir,
1019 runtime_library_dirs=dblib_dir,
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +00001020 include_dirs=db_incs,
1021 libraries=dblibs))
Skip Montanaro57454e52002-06-14 20:30:31 +00001022 else:
Gregory P. Smithe76c8c02004-12-13 12:01:24 +00001023 if db_setup_debug: print "db: no appropriate library found"
Skip Montanaro57454e52002-06-14 20:30:31 +00001024 db_incs = None
1025 dblibs = []
1026 dblib_dir = None
Skip Montanarod1287322007-03-06 15:41:38 +00001027 missing.append('_bsddb')
Skip Montanaro57454e52002-06-14 20:30:31 +00001028
Anthony Baxterc51ee692006-04-01 00:57:31 +00001029 # The sqlite interface
Andrew M. Kuchling738446f42006-10-27 18:13:46 +00001030 sqlite_setup_debug = False # verbose debug prints from this script?
Anthony Baxterc51ee692006-04-01 00:57:31 +00001031
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001032 # We hunt for #define SQLITE_VERSION "n.n.n"
1033 # We need to find >= sqlite version 3.0.8
Anthony Baxterc51ee692006-04-01 00:57:31 +00001034 sqlite_incdir = sqlite_libdir = None
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001035 sqlite_inc_paths = [ '/usr/include',
Anthony Baxterc51ee692006-04-01 00:57:31 +00001036 '/usr/include/sqlite',
1037 '/usr/include/sqlite3',
1038 '/usr/local/include',
1039 '/usr/local/include/sqlite',
1040 '/usr/local/include/sqlite3',
1041 ]
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001042 MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
1043 MIN_SQLITE_VERSION = ".".join([str(x)
1044 for x in MIN_SQLITE_VERSION_NUMBER])
Ronald Oussoren39be38c2006-05-26 11:38:39 +00001045
1046 # Scan the default include directories before the SQLite specific
1047 # ones. This allows one to override the copy of sqlite on OSX,
1048 # where /usr/include contains an old version of sqlite.
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001049 if sys.platform == 'darwin':
1050 sysroot = macosx_sdk_root()
1051
Ned Deily67028042012-08-05 14:42:45 -07001052 for d_ in inc_dirs + sqlite_inc_paths:
1053 d = d_
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001054 if sys.platform == 'darwin' and is_macosx_sdk_path(d):
Ned Deily67028042012-08-05 14:42:45 -07001055 d = os.path.join(sysroot, d[1:])
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001056
Ned Deily67028042012-08-05 14:42:45 -07001057 f = os.path.join(d, "sqlite3.h")
Anthony Baxterc51ee692006-04-01 00:57:31 +00001058 if os.path.exists(f):
Anthony Baxter07f5b352006-04-01 08:36:27 +00001059 if sqlite_setup_debug: print "sqlite: found %s"%f
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001060 incf = open(f).read()
1061 m = re.search(
1062 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001063 if m:
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001064 sqlite_version = m.group(1)
1065 sqlite_version_tuple = tuple([int(x)
1066 for x in sqlite_version.split(".")])
1067 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001068 # we win!
Andrew M. Kuchling738446f42006-10-27 18:13:46 +00001069 if sqlite_setup_debug:
1070 print "%s/sqlite3.h: version %s"%(d, sqlite_version)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001071 sqlite_incdir = d
1072 break
1073 else:
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001074 if sqlite_setup_debug:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001075 print "%s: version %d is too old, need >= %s"%(d,
1076 sqlite_version, MIN_SQLITE_VERSION)
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001077 elif sqlite_setup_debug:
1078 print "sqlite: %s had no SQLITE_VERSION"%(f,)
1079
Anthony Baxterc51ee692006-04-01 00:57:31 +00001080 if sqlite_incdir:
1081 sqlite_dirs_to_check = [
1082 os.path.join(sqlite_incdir, '..', 'lib64'),
1083 os.path.join(sqlite_incdir, '..', 'lib'),
1084 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
1085 os.path.join(sqlite_incdir, '..', '..', 'lib'),
1086 ]
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001087 sqlite_libfile = self.compiler.find_library_file(
Anthony Baxterc51ee692006-04-01 00:57:31 +00001088 sqlite_dirs_to_check + lib_dirs, 'sqlite3')
Hirokazu Yamamoto1ae415c2008-10-03 17:34:49 +00001089 if sqlite_libfile:
1090 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001091
1092 if sqlite_incdir and sqlite_libdir:
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001093 sqlite_srcs = ['_sqlite/cache.c',
Anthony Baxterc51ee692006-04-01 00:57:31 +00001094 '_sqlite/connection.c',
Anthony Baxterc51ee692006-04-01 00:57:31 +00001095 '_sqlite/cursor.c',
1096 '_sqlite/microprotocols.c',
1097 '_sqlite/module.c',
1098 '_sqlite/prepare_protocol.c',
1099 '_sqlite/row.c',
1100 '_sqlite/statement.c',
1101 '_sqlite/util.c', ]
1102
Anthony Baxterc51ee692006-04-01 00:57:31 +00001103 sqlite_defines = []
1104 if sys.platform != "win32":
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001105 sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
Anthony Baxterc51ee692006-04-01 00:57:31 +00001106 else:
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001107 sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
1108
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001109 # Comment this out if you want the sqlite3 module to be able to load extensions.
1110 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Ronald Oussoren39be38c2006-05-26 11:38:39 +00001111
1112 if sys.platform == 'darwin':
1113 # In every directory on the search path search for a dynamic
1114 # library and then a static library, instead of first looking
Ezio Melottic2077b02011-03-16 12:34:31 +02001115 # for dynamic libraries on the entire path.
1116 # This way a statically linked custom sqlite gets picked up
Ronald Oussoren39be38c2006-05-26 11:38:39 +00001117 # before the dynamic library in /usr/lib.
1118 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
1119 else:
1120 sqlite_extra_link_args = ()
1121
Anthony Baxterc51ee692006-04-01 00:57:31 +00001122 exts.append(Extension('_sqlite3', sqlite_srcs,
1123 define_macros=sqlite_defines,
Anthony Baxter3dc6bb32006-04-03 02:20:49 +00001124 include_dirs=["Modules/_sqlite",
Anthony Baxterc51ee692006-04-01 00:57:31 +00001125 sqlite_incdir],
1126 library_dirs=sqlite_libdir,
1127 runtime_library_dirs=sqlite_libdir,
Ronald Oussoren39be38c2006-05-26 11:38:39 +00001128 extra_link_args=sqlite_extra_link_args,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001129 libraries=["sqlite3",]))
Skip Montanarod1287322007-03-06 15:41:38 +00001130 else:
1131 missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001132
1133 # Look for Berkeley db 1.85. Note that it is built as a different
1134 # module name so it can be included even when later versions are
1135 # available. A very restrictive search is performed to avoid
1136 # accidentally building this module with a later version of the
1137 # underlying db library. May BSD-ish Unixes incorporate db 1.85
1138 # symbols into libc and place the include file in /usr/include.
Gregory P. Smith4eb60e52007-08-26 00:26:00 +00001139 #
1140 # If the better bsddb library can be built (db_incs is defined)
1141 # we do not build this one. Otherwise this build will pick up
1142 # the more recent berkeleydb's db.h file first in the include path
1143 # when attempting to compile and it will fail.
Skip Montanaro22e00c42003-05-06 20:43:34 +00001144 f = "/usr/include/db.h"
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001145
1146 if sys.platform == 'darwin':
1147 if is_macosx_sdk_path(f):
1148 sysroot = macosx_sdk_root()
1149 f = os.path.join(sysroot, f[1:])
1150
Gregory P. Smith4eb60e52007-08-26 00:26:00 +00001151 if os.path.exists(f) and not db_incs:
Skip Montanaro22e00c42003-05-06 20:43:34 +00001152 data = open(f).read()
1153 m = re.search(r"#s*define\s+HASHVERSION\s+2\s*", data)
1154 if m is not None:
1155 # bingo - old version used hash file format version 2
1156 ### XXX this should be fixed to not be platform-dependent
1157 ### but I don't have direct access to an osf1 platform and
1158 ### seemed to be muffing the search somehow
1159 libraries = platform == "osf1" and ['db'] or None
1160 if libraries is not None:
1161 exts.append(Extension('bsddb185', ['bsddbmodule.c'],
1162 libraries=libraries))
1163 else:
1164 exts.append(Extension('bsddb185', ['bsddbmodule.c']))
Skip Montanarod1287322007-03-06 15:41:38 +00001165 else:
1166 missing.append('bsddb185')
1167 else:
1168 missing.append('bsddb185')
Skip Montanaro22e00c42003-05-06 20:43:34 +00001169
Benjamin Petersonedfe72f2010-01-01 15:21:13 +00001170 dbm_order = ['gdbm']
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001171 # The standard Unix dbm module:
Jack Jansend1b20452002-07-08 21:39:36 +00001172 if platform not in ['cygwin']:
Matthias Klose51c614e2009-04-29 19:52:49 +00001173 config_args = [arg.strip("'")
1174 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
Benjamin Petersonedfe72f2010-01-01 15:21:13 +00001175 dbm_args = [arg for arg in config_args
Matthias Klose10cbe482009-04-29 17:18:19 +00001176 if arg.startswith('--with-dbmliborder=')]
1177 if dbm_args:
Benjamin Petersonedfe72f2010-01-01 15:21:13 +00001178 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
Matthias Klose10cbe482009-04-29 17:18:19 +00001179 else:
Matthias Klose51c614e2009-04-29 19:52:49 +00001180 dbm_order = "ndbm:gdbm:bdb".split(":")
Matthias Klose10cbe482009-04-29 17:18:19 +00001181 dbmext = None
1182 for cand in dbm_order:
1183 if cand == "ndbm":
1184 if find_file("ndbm.h", inc_dirs, []) is not None:
Nick Coghlan970fcef2012-06-17 18:35:39 +10001185 # Some systems have -lndbm, others have -lgdbm_compat,
1186 # others don't have either
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001187 if self.compiler.find_library_file(lib_dirs,
Tarek Ziadée670e5a2009-07-06 12:50:46 +00001188 'ndbm'):
Matthias Klose10cbe482009-04-29 17:18:19 +00001189 ndbm_libs = ['ndbm']
Nick Coghlan970fcef2012-06-17 18:35:39 +10001190 elif self.compiler.find_library_file(lib_dirs,
1191 'gdbm_compat'):
1192 ndbm_libs = ['gdbm_compat']
Matthias Klose10cbe482009-04-29 17:18:19 +00001193 else:
1194 ndbm_libs = []
1195 print "building dbm using ndbm"
1196 dbmext = Extension('dbm', ['dbmmodule.c'],
1197 define_macros=[
1198 ('HAVE_NDBM_H',None),
1199 ],
1200 libraries=ndbm_libs)
1201 break
1202
1203 elif cand == "gdbm":
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001204 if self.compiler.find_library_file(lib_dirs, 'gdbm'):
Matthias Klose10cbe482009-04-29 17:18:19 +00001205 gdbm_libs = ['gdbm']
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001206 if self.compiler.find_library_file(lib_dirs,
Tarek Ziadée670e5a2009-07-06 12:50:46 +00001207 'gdbm_compat'):
Matthias Klose10cbe482009-04-29 17:18:19 +00001208 gdbm_libs.append('gdbm_compat')
1209 if find_file("gdbm/ndbm.h", inc_dirs, []) is not None:
1210 print "building dbm using gdbm"
1211 dbmext = Extension(
1212 'dbm', ['dbmmodule.c'],
1213 define_macros=[
1214 ('HAVE_GDBM_NDBM_H', None),
1215 ],
1216 libraries = gdbm_libs)
1217 break
1218 if find_file("gdbm-ndbm.h", inc_dirs, []) is not None:
1219 print "building dbm using gdbm"
1220 dbmext = Extension(
1221 'dbm', ['dbmmodule.c'],
1222 define_macros=[
1223 ('HAVE_GDBM_DASH_NDBM_H', None),
1224 ],
1225 libraries = gdbm_libs)
1226 break
1227 elif cand == "bdb":
1228 if db_incs is not None:
1229 print "building dbm using bdb"
1230 dbmext = Extension('dbm', ['dbmmodule.c'],
1231 library_dirs=dblib_dir,
1232 runtime_library_dirs=dblib_dir,
1233 include_dirs=db_incs,
1234 define_macros=[
1235 ('HAVE_BERKDB_H', None),
1236 ('DB_DBM_HSEARCH', None),
1237 ],
1238 libraries=dblibs)
1239 break
1240 if dbmext is not None:
1241 exts.append(dbmext)
Skip Montanarod1287322007-03-06 15:41:38 +00001242 else:
1243 missing.append('dbm')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001244
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001245 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
Benjamin Petersonedfe72f2010-01-01 15:21:13 +00001246 if ('gdbm' in dbm_order and
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001247 self.compiler.find_library_file(lib_dirs, 'gdbm')):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001248 exts.append( Extension('gdbm', ['gdbmmodule.c'],
1249 libraries = ['gdbm'] ) )
Skip Montanarod1287322007-03-06 15:41:38 +00001250 else:
1251 missing.append('gdbm')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001252
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001253 # Unix-only modules
Ronald Oussoren9545a232010-05-05 19:09:31 +00001254 if platform not in ['win32']:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001255 # Steen Lumholt's termios module
1256 exts.append( Extension('termios', ['termios.c']) )
1257 # Jeremy Hylton's rlimit interface
Tim Peters2c60f7a2003-01-29 03:49:43 +00001258 if platform not in ['atheos']:
Martin v. Löwisf90ae202002-06-11 06:22:31 +00001259 exts.append( Extension('resource', ['resource.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +00001260 else:
1261 missing.append('resource')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001262
Andrew M. Kuchlingcf393f32001-02-21 02:38:24 +00001263 # Sun yellow pages. Some systems have the functions in libc.
Benjamin Petersoneb74da82009-12-30 03:02:34 +00001264 if (platform not in ['cygwin', 'atheos', 'qnx6'] and
1265 find_file('rpcsvc/yp_prot.h', inc_dirs, []) is not None):
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001266 if (self.compiler.find_library_file(lib_dirs, 'nsl')):
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +00001267 libs = ['nsl']
1268 else:
1269 libs = []
1270 exts.append( Extension('nis', ['nismodule.c'],
1271 libraries = libs) )
Skip Montanarod1287322007-03-06 15:41:38 +00001272 else:
1273 missing.append('nis')
1274 else:
1275 missing.extend(['nis', 'resource', 'termios'])
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001276
Skip Montanaro72092942004-02-07 12:50:19 +00001277 # Curses support, requiring the System V version of curses, often
Fredrik Lundhade711a2001-01-24 08:00:28 +00001278 # provided by the ncurses library.
Andrew M. Kuchling86070422006-08-06 22:07:04 +00001279 panel_library = 'panel'
Stefan Krah449aa862010-06-03 12:39:50 +00001280 if curses_library.startswith('ncurses'):
1281 if curses_library == 'ncursesw':
1282 # Bug 1464056: If _curses.so links with ncursesw,
1283 # _curses_panel.so must link with panelw.
1284 panel_library = 'panelw'
1285 curses_libs = [curses_library]
Martin v. Löwisa55e55e2006-02-11 15:55:14 +00001286 exts.append( Extension('_curses', ['_cursesmodule.c'],
1287 libraries = curses_libs) )
Stefan Krah449aa862010-06-03 12:39:50 +00001288 elif curses_library == 'curses' and platform != 'darwin':
Michael W. Hudson5b109102002-01-23 15:04:41 +00001289 # OSX has an old Berkeley curses, not good enough for
1290 # the _curses module.
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001291 if (self.compiler.find_library_file(lib_dirs, 'terminfo')):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001292 curses_libs = ['curses', 'terminfo']
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001293 elif (self.compiler.find_library_file(lib_dirs, 'termcap')):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001294 curses_libs = ['curses', 'termcap']
Neal Norwitz0b27ff92003-03-31 15:53:49 +00001295 else:
1296 curses_libs = ['curses']
Fredrik Lundhade711a2001-01-24 08:00:28 +00001297
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001298 exts.append( Extension('_curses', ['_cursesmodule.c'],
1299 libraries = curses_libs) )
Skip Montanarod1287322007-03-06 15:41:38 +00001300 else:
1301 missing.append('_curses')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001302
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001303 # If the curses module is enabled, check for the panel module
Andrew M. Kuchlinge7ffbb22001-12-06 15:57:16 +00001304 if (module_enabled(exts, '_curses') and
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001305 self.compiler.find_library_file(lib_dirs, panel_library)):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001306 exts.append( Extension('_curses_panel', ['_curses_panel.c'],
Andrew M. Kuchling86070422006-08-06 22:07:04 +00001307 libraries = [panel_library] + curses_libs) )
Skip Montanarod1287322007-03-06 15:41:38 +00001308 else:
1309 missing.append('_curses_panel')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001310
Barry Warsaw259b1e12002-08-13 20:09:26 +00001311 # Andrew Kuchling's zlib module. Note that some versions of zlib
1312 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1313 # http://www.cert.org/advisories/CA-2002-07.html
1314 #
1315 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1316 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1317 # now, we still accept 1.1.3, because we think it's difficult to
1318 # exploit this in Python, and we'd rather make it RedHat's problem
1319 # than our problem <wink>.
1320 #
1321 # You can upgrade zlib to version 1.1.4 yourself by going to
1322 # http://www.gzip.org/zlib/
Guido van Rossume6970912001-04-15 15:16:12 +00001323 zlib_inc = find_file('zlib.h', [], inc_dirs)
Gregory P. Smith440ca772008-03-24 00:08:01 +00001324 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001325 if zlib_inc is not None:
1326 zlib_h = zlib_inc[0] + '/zlib.h'
1327 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001328 version_req = '"1.1.3"'
Guido van Rossume6970912001-04-15 15:16:12 +00001329 fp = open(zlib_h)
1330 while 1:
1331 line = fp.readline()
1332 if not line:
1333 break
Guido van Rossum8cdc03d2002-08-06 17:28:30 +00001334 if line.startswith('#define ZLIB_VERSION'):
Guido van Rossume6970912001-04-15 15:16:12 +00001335 version = line.split()[2]
1336 break
1337 if version >= version_req:
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001338 if (self.compiler.find_library_file(lib_dirs, 'z')):
Ronald Oussoren9b8b6192006-06-27 12:53:52 +00001339 if sys.platform == "darwin":
1340 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1341 else:
1342 zlib_extra_link_args = ()
Guido van Rossume6970912001-04-15 15:16:12 +00001343 exts.append( Extension('zlib', ['zlibmodule.c'],
Ronald Oussoren9b8b6192006-06-27 12:53:52 +00001344 libraries = ['z'],
1345 extra_link_args = zlib_extra_link_args))
Gregory P. Smith440ca772008-03-24 00:08:01 +00001346 have_zlib = True
Skip Montanarod1287322007-03-06 15:41:38 +00001347 else:
1348 missing.append('zlib')
1349 else:
1350 missing.append('zlib')
1351 else:
1352 missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001353
Gregory P. Smith440ca772008-03-24 00:08:01 +00001354 # Helper module for various ascii-encoders. Uses zlib for an optimized
1355 # crc32 if we have it. Otherwise binascii uses its own.
1356 if have_zlib:
1357 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1358 libraries = ['z']
1359 extra_link_args = zlib_extra_link_args
1360 else:
1361 extra_compile_args = []
1362 libraries = []
1363 extra_link_args = []
1364 exts.append( Extension('binascii', ['binascii.c'],
1365 extra_compile_args = extra_compile_args,
1366 libraries = libraries,
1367 extra_link_args = extra_link_args) )
1368
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001369 # Gustavo Niemeyer's bz2 module.
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001370 if (self.compiler.find_library_file(lib_dirs, 'bz2')):
Ronald Oussoren9b8b6192006-06-27 12:53:52 +00001371 if sys.platform == "darwin":
1372 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1373 else:
1374 bz2_extra_link_args = ()
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001375 exts.append( Extension('bz2', ['bz2module.c'],
Ronald Oussoren9b8b6192006-06-27 12:53:52 +00001376 libraries = ['bz2'],
1377 extra_link_args = bz2_extra_link_args) )
Skip Montanarod1287322007-03-06 15:41:38 +00001378 else:
1379 missing.append('bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001380
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001381 # Interface to the Expat XML parser
1382 #
Benjamin Peterson2fd2e862009-12-31 16:28:24 +00001383 # Expat was written by James Clark and is now maintained by a group of
1384 # developers on SourceForge; see www.libexpat.org for more information.
1385 # The pyexpat module was written by Paul Prescod after a prototype by
1386 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1387 # of a system shared libexpat.so is possible with --with-system-expat
Benjamin Peterson206e10c2010-10-31 16:40:21 +00001388 # configure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001389 #
1390 # More information on Expat can be found at www.libexpat.org.
1391 #
Benjamin Peterson2c196742009-12-31 03:17:18 +00001392 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1393 expat_inc = []
1394 define_macros = []
1395 expat_lib = ['expat']
1396 expat_sources = []
1397 else:
1398 expat_inc = [os.path.join(os.getcwd(), srcdir, 'Modules', 'expat')]
1399 define_macros = [
1400 ('HAVE_EXPAT_CONFIG_H', '1'),
1401 ]
1402 expat_lib = []
1403 expat_sources = ['expat/xmlparse.c',
1404 'expat/xmlrole.c',
1405 'expat/xmltok.c']
Ronald Oussoren988117f2006-04-29 11:31:35 +00001406
Fred Drake2d59a492003-10-21 15:41:15 +00001407 exts.append(Extension('pyexpat',
1408 define_macros = define_macros,
Benjamin Peterson2c196742009-12-31 03:17:18 +00001409 include_dirs = expat_inc,
1410 libraries = expat_lib,
1411 sources = ['pyexpat.c'] + expat_sources
Fred Drake2d59a492003-10-21 15:41:15 +00001412 ))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001413
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001414 # Fredrik Lundh's cElementTree module. Note that this also
1415 # uses expat (via the CAPI hook in pyexpat).
1416
Hye-Shik Chang6c403592006-03-27 08:43:11 +00001417 if os.path.isfile(os.path.join(srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001418 define_macros.append(('USE_PYEXPAT_CAPI', None))
1419 exts.append(Extension('_elementtree',
1420 define_macros = define_macros,
Benjamin Peterson2c196742009-12-31 03:17:18 +00001421 include_dirs = expat_inc,
1422 libraries = expat_lib,
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001423 sources = ['_elementtree.c'],
1424 ))
Skip Montanarod1287322007-03-06 15:41:38 +00001425 else:
1426 missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001427
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001428 # Hye-Shik Chang's CJKCodecs modules.
Martin v. Löwise2713be2005-03-08 15:03:08 +00001429 if have_unicode:
1430 exts.append(Extension('_multibytecodec',
1431 ['cjkcodecs/multibytecodec.c']))
1432 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
Skip Montanarod1287322007-03-06 15:41:38 +00001433 exts.append(Extension('_codecs_%s' % loc,
Martin v. Löwise2713be2005-03-08 15:03:08 +00001434 ['cjkcodecs/_codecs_%s.c' % loc]))
Skip Montanarod1287322007-03-06 15:41:38 +00001435 else:
1436 missing.append('_multibytecodec')
1437 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
1438 missing.append('_codecs_%s' % loc)
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001439
Michael W. Hudson5b109102002-01-23 15:04:41 +00001440 # Dynamic loading module
Guido van Rossum770acd32002-09-12 14:41:20 +00001441 if sys.maxint == 0x7fffffff:
1442 # This requires sizeof(int) == sizeof(long) == sizeof(char*)
1443 dl_inc = find_file('dlfcn.h', [], inc_dirs)
Anthony Baxter82201742006-04-09 15:07:40 +00001444 if (dl_inc is not None) and (platform not in ['atheos']):
Guido van Rossum770acd32002-09-12 14:41:20 +00001445 exts.append( Extension('dl', ['dlmodule.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +00001446 else:
1447 missing.append('dl')
1448 else:
1449 missing.append('dl')
Michael W. Hudson5b109102002-01-23 15:04:41 +00001450
Thomas Hellercf567c12006-03-08 19:51:58 +00001451 # Thomas Heller's _ctypes module
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001452 self.detect_ctypes(inc_dirs, lib_dirs)
Thomas Hellercf567c12006-03-08 19:51:58 +00001453
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001454 # Richard Oudkerk's multiprocessing module
1455 if platform == 'win32': # Windows
1456 macros = dict()
1457 libraries = ['ws2_32']
1458
1459 elif platform == 'darwin': # Mac OSX
Jesse Noller355b1262009-04-02 00:03:28 +00001460 macros = dict()
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001461 libraries = []
1462
1463 elif platform == 'cygwin': # Cygwin
Jesse Noller355b1262009-04-02 00:03:28 +00001464 macros = dict()
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001465 libraries = []
Hye-Shik Chang99c48a82008-06-28 01:04:31 +00001466
Martin v. Löwisbb86d832008-11-04 20:40:09 +00001467 elif platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
Hye-Shik Chang99c48a82008-06-28 01:04:31 +00001468 # FreeBSD's P1003.1b semaphore support is very experimental
1469 # and has many known problems. (as of June 2008)
Jesse Noller355b1262009-04-02 00:03:28 +00001470 macros = dict()
Hye-Shik Chang99c48a82008-06-28 01:04:31 +00001471 libraries = []
1472
Jesse Noller37040cd2008-09-30 00:15:45 +00001473 elif platform.startswith('openbsd'):
Jesse Noller355b1262009-04-02 00:03:28 +00001474 macros = dict()
Jesse Noller37040cd2008-09-30 00:15:45 +00001475 libraries = []
1476
Jesse Noller40a61642009-03-31 18:12:35 +00001477 elif platform.startswith('netbsd'):
Jesse Noller355b1262009-04-02 00:03:28 +00001478 macros = dict()
Jesse Noller40a61642009-03-31 18:12:35 +00001479 libraries = []
1480
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001481 else: # Linux and other unices
Jesse Noller355b1262009-04-02 00:03:28 +00001482 macros = dict()
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001483 libraries = ['rt']
1484
1485 if platform == 'win32':
1486 multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1487 '_multiprocessing/semaphore.c',
1488 '_multiprocessing/pipe_connection.c',
1489 '_multiprocessing/socket_connection.c',
1490 '_multiprocessing/win32_functions.c'
1491 ]
1492
1493 else:
1494 multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1495 '_multiprocessing/socket_connection.c'
1496 ]
Mark Dickinsonc4920e82009-11-20 19:30:22 +00001497 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
Mark Dickinson5afa6d42009-11-28 10:44:20 +00001498 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001499 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
1500
Jesse Nollerf6da8d12009-01-23 14:04:41 +00001501 if sysconfig.get_config_var('WITH_THREAD'):
1502 exts.append ( Extension('_multiprocessing', multiprocessing_srcs,
1503 define_macros=macros.items(),
1504 include_dirs=["Modules/_multiprocessing"]))
1505 else:
1506 missing.append('_multiprocessing')
1507
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001508 # End multiprocessing
1509
1510
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001511 # Platform-specific libraries
Andrew M. Kuchling34febf52001-01-24 03:31:07 +00001512 if platform == 'linux2':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001513 # Linux-specific modules
1514 exts.append( Extension('linuxaudiodev', ['linuxaudiodev.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +00001515 else:
1516 missing.append('linuxaudiodev')
Greg Ward0a6355e2003-01-08 01:37:41 +00001517
Matthias Klose8a96d202010-04-21 22:18:52 +00001518 if (platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
1519 'freebsd7', 'freebsd8')
1520 or platform.startswith("gnukfreebsd")):
Guido van Rossum0c016a92003-02-13 16:12:21 +00001521 exts.append( Extension('ossaudiodev', ['ossaudiodev.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +00001522 else:
1523 missing.append('ossaudiodev')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001524
Andrew M. Kuchling34febf52001-01-24 03:31:07 +00001525 if platform == 'sunos5':
Fredrik Lundhade711a2001-01-24 08:00:28 +00001526 # SunOS specific modules
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001527 exts.append( Extension('sunaudiodev', ['sunaudiodev.c']) )
Skip Montanarod1287322007-03-06 15:41:38 +00001528 else:
1529 missing.append('sunaudiodev')
Michael W. Hudson5b109102002-01-23 15:04:41 +00001530
Ronald Oussorena5b642c2009-10-08 08:04:15 +00001531 if platform == 'darwin':
1532 # _scproxy
1533 exts.append(Extension("_scproxy", [os.path.join(srcdir, "Mac/Modules/_scproxy.c")],
1534 extra_link_args= [
1535 '-framework', 'SystemConfiguration',
1536 '-framework', 'CoreFoundation'
1537 ]))
1538
1539
Tim Peters66cb0182004-08-26 05:23:19 +00001540 if platform == 'darwin' and ("--disable-toolbox-glue" not in
Ronald Oussoren090f8152006-03-30 20:18:33 +00001541 sysconfig.get_config_var("CONFIG_ARGS")):
1542
Ronald Oussoren05b0d1d2010-03-08 07:06:47 +00001543 if int(os.uname()[2].split('.')[0]) >= 8:
Ronald Oussoren090f8152006-03-30 20:18:33 +00001544 # We're on Mac OS X 10.4 or later, the compiler should
1545 # support '-Wno-deprecated-declarations'. This will
1546 # surpress deprecation warnings for the Carbon extensions,
1547 # these extensions wrap the Carbon APIs and even those
1548 # parts that are deprecated.
1549 carbon_extra_compile_args = ['-Wno-deprecated-declarations']
1550 else:
1551 carbon_extra_compile_args = []
1552
Just van Rossum05ced6a2002-11-24 23:15:57 +00001553 # Mac OS X specific modules.
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001554 def macSrcExists(name1, name2=''):
1555 if not name1:
1556 return None
1557 names = (name1,)
1558 if name2:
1559 names = (name1, name2)
1560 path = os.path.join(srcdir, 'Mac', 'Modules', *names)
1561 return os.path.exists(path)
1562
1563 def addMacExtension(name, kwds, extra_srcs=[]):
1564 dirname = ''
1565 if name[0] == '_':
1566 dirname = name[1:].lower()
1567 cname = name + '.c'
1568 cmodulename = name + 'module.c'
1569 # Check for NNN.c, NNNmodule.c, _nnn/NNN.c, _nnn/NNNmodule.c
1570 if macSrcExists(cname):
1571 srcs = [cname]
1572 elif macSrcExists(cmodulename):
1573 srcs = [cmodulename]
1574 elif macSrcExists(dirname, cname):
1575 # XXX(nnorwitz): If all the names ended with module, we
1576 # wouldn't need this condition. ibcarbon is the only one.
1577 srcs = [os.path.join(dirname, cname)]
1578 elif macSrcExists(dirname, cmodulename):
1579 srcs = [os.path.join(dirname, cmodulename)]
1580 else:
1581 raise RuntimeError("%s not found" % name)
1582
1583 # Here's the whole point: add the extension with sources
1584 exts.append(Extension(name, srcs + extra_srcs, **kwds))
1585
1586 # Core Foundation
1587 core_kwds = {'extra_compile_args': carbon_extra_compile_args,
1588 'extra_link_args': ['-framework', 'CoreFoundation'],
1589 }
1590 addMacExtension('_CF', core_kwds, ['cf/pycfbridge.c'])
1591 addMacExtension('autoGIL', core_kwds)
1592
Ronald Oussoren51f06332009-09-20 10:31:22 +00001593
1594
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001595 # Carbon
1596 carbon_kwds = {'extra_compile_args': carbon_extra_compile_args,
1597 'extra_link_args': ['-framework', 'Carbon'],
1598 }
Anthony Baxtera2a26b92006-04-05 17:30:38 +00001599 CARBON_EXTS = ['ColorPicker', 'gestalt', 'MacOS', 'Nav',
1600 'OSATerminology', 'icglue',
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001601 # All these are in subdirs
Anthony Baxtera2a26b92006-04-05 17:30:38 +00001602 '_AE', '_AH', '_App', '_CarbonEvt', '_Cm', '_Ctl',
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001603 '_Dlg', '_Drag', '_Evt', '_File', '_Folder', '_Fm',
Anthony Baxtera2a26b92006-04-05 17:30:38 +00001604 '_Help', '_Icn', '_IBCarbon', '_List',
1605 '_Menu', '_Mlte', '_OSA', '_Res', '_Qd', '_Qdoffs',
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001606 '_Scrap', '_Snd', '_TE',
Anthony Baxtera2a26b92006-04-05 17:30:38 +00001607 ]
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001608 for name in CARBON_EXTS:
1609 addMacExtension(name, carbon_kwds)
1610
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001611 # Workaround for a bug in the version of gcc shipped with Xcode 3.
1612 # The _Win extension should build just like the other Carbon extensions, but
1613 # this actually results in a hard crash of the linker.
1614 #
1615 if '-arch ppc64' in cflags and '-arch ppc' in cflags:
1616 win_kwds = {'extra_compile_args': carbon_extra_compile_args + ['-arch', 'i386', '-arch', 'ppc'],
1617 'extra_link_args': ['-framework', 'Carbon', '-arch', 'i386', '-arch', 'ppc'],
1618 }
1619 addMacExtension('_Win', win_kwds)
1620 else:
1621 addMacExtension('_Win', carbon_kwds)
1622
1623
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001624 # Application Services & QuickTime
1625 app_kwds = {'extra_compile_args': carbon_extra_compile_args,
1626 'extra_link_args': ['-framework','ApplicationServices'],
1627 }
1628 addMacExtension('_Launch', app_kwds)
1629 addMacExtension('_CG', app_kwds)
1630
Just van Rossum05ced6a2002-11-24 23:15:57 +00001631 exts.append( Extension('_Qt', ['qt/_Qtmodule.c'],
Ronald Oussoren090f8152006-03-30 20:18:33 +00001632 extra_compile_args=carbon_extra_compile_args,
1633 extra_link_args=['-framework', 'QuickTime',
Just van Rossum05ced6a2002-11-24 23:15:57 +00001634 '-framework', 'Carbon']) )
Neal Norwitz3e1ec3a2006-04-03 04:52:05 +00001635
Michael W. Hudson5b109102002-01-23 15:04:41 +00001636
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001637 self.extensions.extend(exts)
1638
1639 # Call the method for detecting whether _tkinter can be compiled
1640 self.detect_tkinter(inc_dirs, lib_dirs)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001641
Skip Montanarod1287322007-03-06 15:41:38 +00001642 if '_tkinter' not in [e.name for e in self.extensions]:
1643 missing.append('_tkinter')
1644
1645 return missing
1646
Jack Jansen0b06be72002-06-21 14:48:38 +00001647 def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
1648 # The _tkinter module, using frameworks. Since frameworks are quite
1649 # different the UNIX search logic is not sharable.
1650 from os.path import join, exists
1651 framework_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00001652 '/Library/Frameworks',
Ronald Oussorencea1ddb2009-03-04 21:30:12 +00001653 '/System/Library/Frameworks/',
Jack Jansen0b06be72002-06-21 14:48:38 +00001654 join(os.getenv('HOME'), '/Library/Frameworks')
1655 ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001656
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001657 sysroot = macosx_sdk_root()
1658
Skip Montanaro0174ddd2005-12-30 05:01:26 +00001659 # Find the directory that contains the Tcl.framework and Tk.framework
Jack Jansen0b06be72002-06-21 14:48:38 +00001660 # bundles.
1661 # XXX distutils should support -F!
1662 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00001663 # both Tcl.framework and Tk.framework should be present
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001664
1665
Jack Jansen0b06be72002-06-21 14:48:38 +00001666 for fw in 'Tcl', 'Tk':
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001667 if is_macosx_sdk_path(F):
1668 if not exists(join(sysroot, F[1:], fw + '.framework')):
1669 break
1670 else:
1671 if not exists(join(F, fw + '.framework')):
1672 break
Jack Jansen0b06be72002-06-21 14:48:38 +00001673 else:
1674 # ok, F is now directory with both frameworks. Continure
1675 # building
1676 break
1677 else:
1678 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
1679 # will now resume.
1680 return 0
Tim Peters2c60f7a2003-01-29 03:49:43 +00001681
Jack Jansen0b06be72002-06-21 14:48:38 +00001682 # For 8.4a2, we must add -I options that point inside the Tcl and Tk
1683 # frameworks. In later release we should hopefully be able to pass
Tim Peters2c60f7a2003-01-29 03:49:43 +00001684 # the -F option to gcc, which specifies a framework lookup path.
Jack Jansen0b06be72002-06-21 14:48:38 +00001685 #
1686 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00001687 join(F, fw + '.framework', H)
Jack Jansen0b06be72002-06-21 14:48:38 +00001688 for fw in 'Tcl', 'Tk'
1689 for H in 'Headers', 'Versions/Current/PrivateHeaders'
1690 ]
1691
Tim Peters2c60f7a2003-01-29 03:49:43 +00001692 # For 8.4a2, the X11 headers are not included. Rather than include a
Jack Jansen0b06be72002-06-21 14:48:38 +00001693 # complicated search, this is a hard-coded path. It could bail out
1694 # if X11 libs are not found...
1695 include_dirs.append('/usr/X11R6/include')
1696 frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
1697
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001698 # All existing framework builds of Tcl/Tk don't support 64-bit
1699 # architectures.
1700 cflags = sysconfig.get_config_vars('CFLAGS')[0]
1701 archs = re.findall('-arch\s+(\w+)', cflags)
Ronald Oussoren593e4ca2010-06-03 09:47:21 +00001702
1703 if is_macosx_sdk_path(F):
1704 fp = os.popen("file %s/Tk.framework/Tk | grep 'for architecture'"%(os.path.join(sysroot, F[1:]),))
1705 else:
1706 fp = os.popen("file %s/Tk.framework/Tk | grep 'for architecture'"%(F,))
1707
Ronald Oussoren91a11a42009-09-15 18:33:33 +00001708 detected_archs = []
1709 for ln in fp:
1710 a = ln.split()[-1]
1711 if a in archs:
1712 detected_archs.append(ln.split()[-1])
1713 fp.close()
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001714
Ronald Oussoren91a11a42009-09-15 18:33:33 +00001715 for a in detected_archs:
1716 frameworks.append('-arch')
1717 frameworks.append(a)
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001718
Jack Jansen0b06be72002-06-21 14:48:38 +00001719 ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1720 define_macros=[('WITH_APPINIT', 1)],
1721 include_dirs = include_dirs,
1722 libraries = [],
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001723 extra_compile_args = frameworks[2:],
Jack Jansen0b06be72002-06-21 14:48:38 +00001724 extra_link_args = frameworks,
1725 )
1726 self.extensions.append(ext)
1727 return 1
1728
Tim Peters2c60f7a2003-01-29 03:49:43 +00001729
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001730 def detect_tkinter(self, inc_dirs, lib_dirs):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001731 # The _tkinter module.
Michael W. Hudson5b109102002-01-23 15:04:41 +00001732
Jack Jansen0b06be72002-06-21 14:48:38 +00001733 # Rather than complicate the code below, detecting and building
1734 # AquaTk is a separate method. Only one Tkinter will be built on
1735 # Darwin - either AquaTk, if it is found, or X11 based Tk.
1736 platform = self.get_platform()
Skip Montanaro0174ddd2005-12-30 05:01:26 +00001737 if (platform == 'darwin' and
1738 self.detect_tkinter_darwin(inc_dirs, lib_dirs)):
Tim Peters2c60f7a2003-01-29 03:49:43 +00001739 return
Jack Jansen0b06be72002-06-21 14:48:38 +00001740
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001741 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00001742 # The versions with dots are used on Unix, and the versions without
1743 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001744 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polofb118352009-08-16 14:34:26 +00001745 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
1746 '8.2', '82', '8.1', '81', '8.0', '80']:
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001747 tklib = self.compiler.find_library_file(lib_dirs,
Tarek Ziadée670e5a2009-07-06 12:50:46 +00001748 'tk' + version)
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001749 tcllib = self.compiler.find_library_file(lib_dirs,
Tarek Ziadée670e5a2009-07-06 12:50:46 +00001750 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00001751 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001752 # Exit the loop when we've found the Tcl/Tk libraries
1753 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001754
Fredrik Lundhade711a2001-01-24 08:00:28 +00001755 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001756 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00001757 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001758 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00001759 dotversion = version
1760 if '.' not in dotversion and "bsd" in sys.platform.lower():
1761 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
1762 # but the include subdirs are named like .../include/tcl8.3.
1763 dotversion = dotversion[:-1] + '.' + dotversion[-1]
1764 tcl_include_sub = []
1765 tk_include_sub = []
1766 for dir in inc_dirs:
1767 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
1768 tk_include_sub += [dir + os.sep + "tk" + dotversion]
1769 tk_include_sub += tcl_include_sub
1770 tcl_includes = find_file('tcl.h', inc_dirs, tcl_include_sub)
1771 tk_includes = find_file('tk.h', inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001772
Martin v. Löwise86a59a2003-05-03 08:45:51 +00001773 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001774 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00001775 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001776 return
Fredrik Lundhade711a2001-01-24 08:00:28 +00001777
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001778 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001779
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001780 include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
1781 for dir in tcl_includes + tk_includes:
1782 if dir not in include_dirs:
1783 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001784
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001785 # Check for various platform-specific directories
Andrew M. Kuchling34febf52001-01-24 03:31:07 +00001786 if platform == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001787 include_dirs.append('/usr/openwin/include')
1788 added_lib_dirs.append('/usr/openwin/lib')
1789 elif os.path.exists('/usr/X11R6/include'):
1790 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00001791 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001792 added_lib_dirs.append('/usr/X11R6/lib')
1793 elif os.path.exists('/usr/X11R5/include'):
1794 include_dirs.append('/usr/X11R5/include')
1795 added_lib_dirs.append('/usr/X11R5/lib')
1796 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001797 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001798 include_dirs.append('/usr/X11/include')
1799 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001800
Jason Tishler9181c942003-02-05 15:16:17 +00001801 # If Cygwin, then verify that X is installed before proceeding
1802 if platform == 'cygwin':
1803 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
1804 if x11_inc is None:
1805 return
1806
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001807 # Check for BLT extension
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001808 if self.compiler.find_library_file(lib_dirs + added_lib_dirs,
Tarek Ziadée670e5a2009-07-06 12:50:46 +00001809 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001810 defs.append( ('WITH_BLT', 1) )
1811 libs.append('BLT8.0')
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001812 elif self.compiler.find_library_file(lib_dirs + added_lib_dirs,
Tarek Ziadée670e5a2009-07-06 12:50:46 +00001813 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00001814 defs.append( ('WITH_BLT', 1) )
1815 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001816
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001817 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00001818 libs.append('tk'+ version)
1819 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001820
Andrew M. Kuchling34febf52001-01-24 03:31:07 +00001821 if platform in ['aix3', 'aix4']:
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001822 libs.append('ld')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001823
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00001824 # Finally, link with the X11 libraries (not appropriate on cygwin)
1825 if platform != "cygwin":
1826 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001827
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001828 ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1829 define_macros=[('WITH_APPINIT', 1)] + defs,
1830 include_dirs = include_dirs,
1831 libraries = libs,
1832 library_dirs = added_lib_dirs,
1833 )
1834 self.extensions.append(ext)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001835
Guido van Rossum6c7438e2003-02-11 20:05:50 +00001836## # Uncomment these lines if you want to play with xxmodule.c
1837## ext = Extension('xx', ['xxmodule.c'])
1838## self.extensions.append(ext)
1839
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001840 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001841 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001842 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001843 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001844 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001845 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001846 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001847
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001848 def configure_ctypes_darwin(self, ext):
1849 # Darwin (OS X) uses preconfigured files, in
1850 # the Modules/_ctypes/libffi_osx directory.
Neil Schemenauerc59c5f32009-02-05 16:32:29 +00001851 srcdir = sysconfig.get_config_var('srcdir')
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001852 ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
1853 '_ctypes', 'libffi_osx'))
1854 sources = [os.path.join(ffi_srcdir, p)
1855 for p in ['ffi.c',
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001856 'x86/darwin64.S',
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001857 'x86/x86-darwin.S',
1858 'x86/x86-ffi_darwin.c',
1859 'x86/x86-ffi64.c',
1860 'powerpc/ppc-darwin.S',
1861 'powerpc/ppc-darwin_closure.S',
1862 'powerpc/ppc-ffi_darwin.c',
1863 'powerpc/ppc64-darwin_closure.S',
1864 ]]
1865
1866 # Add .S (preprocessed assembly) to C compiler source extensions.
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001867 self.compiler.src_extensions.append('.S')
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001868
1869 include_dirs = [os.path.join(ffi_srcdir, 'include'),
1870 os.path.join(ffi_srcdir, 'powerpc')]
1871 ext.include_dirs.extend(include_dirs)
1872 ext.sources.extend(sources)
1873 return True
1874
Thomas Hellereba43c12006-04-07 19:04:09 +00001875 def configure_ctypes(self, ext):
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001876 if not self.use_system_libffi:
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001877 if sys.platform == 'darwin':
1878 return self.configure_ctypes_darwin(ext)
1879
Neil Schemenauerc59c5f32009-02-05 16:32:29 +00001880 srcdir = sysconfig.get_config_var('srcdir')
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001881 ffi_builddir = os.path.join(self.build_temp, 'libffi')
1882 ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
1883 '_ctypes', 'libffi'))
1884 ffi_configfile = os.path.join(ffi_builddir, 'fficonfig.py')
Thomas Hellercf567c12006-03-08 19:51:58 +00001885
Thomas Heller5e218b42006-04-27 15:50:42 +00001886 from distutils.dep_util import newer_group
1887
1888 config_sources = [os.path.join(ffi_srcdir, fname)
Martin v. Löwisf1a4aa32007-02-14 11:30:56 +00001889 for fname in os.listdir(ffi_srcdir)
1890 if os.path.isfile(os.path.join(ffi_srcdir, fname))]
Thomas Heller5e218b42006-04-27 15:50:42 +00001891 if self.force or newer_group(config_sources,
1892 ffi_configfile):
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001893 from distutils.dir_util import mkpath
1894 mkpath(ffi_builddir)
1895 config_args = []
Christian Heimes6fd32482012-09-06 18:02:49 +02001896 if not self.verbose:
1897 config_args.append("-q")
Thomas Hellercf567c12006-03-08 19:51:58 +00001898
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001899 # Pass empty CFLAGS because we'll just append the resulting
1900 # CFLAGS to Python's; -g or -O2 is to be avoided.
1901 cmd = "cd %s && env CFLAGS='' '%s/configure' %s" \
1902 % (ffi_builddir, ffi_srcdir, " ".join(config_args))
Thomas Hellercf567c12006-03-08 19:51:58 +00001903
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001904 res = os.system(cmd)
1905 if res or not os.path.exists(ffi_configfile):
1906 print "Failed to configure _ctypes module"
1907 return False
Thomas Hellercf567c12006-03-08 19:51:58 +00001908
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001909 fficonfig = {}
Antoine Pitrou1379b842010-01-13 11:57:42 +00001910 with open(ffi_configfile) as f:
1911 exec f in fficonfig
Thomas Hellercf567c12006-03-08 19:51:58 +00001912
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001913 # Add .S (preprocessed assembly) to C compiler source extensions.
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001914 self.compiler.src_extensions.append('.S')
Thomas Hellercf567c12006-03-08 19:51:58 +00001915
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001916 include_dirs = [os.path.join(ffi_builddir, 'include'),
Antoine Pitrou8c510e72010-01-13 11:47:49 +00001917 ffi_builddir,
1918 os.path.join(ffi_srcdir, 'src')]
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001919 extra_compile_args = fficonfig['ffi_cflags'].split()
Thomas Hellereba43c12006-04-07 19:04:09 +00001920
Antoine Pitrou8c510e72010-01-13 11:47:49 +00001921 ext.sources.extend(os.path.join(ffi_srcdir, f) for f in
1922 fficonfig['ffi_sources'])
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001923 ext.include_dirs.extend(include_dirs)
1924 ext.extra_compile_args.extend(extra_compile_args)
Thomas Heller795246c2006-04-07 19:27:56 +00001925 return True
Thomas Hellereba43c12006-04-07 19:04:09 +00001926
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001927 def detect_ctypes(self, inc_dirs, lib_dirs):
1928 self.use_system_libffi = False
Thomas Hellereba43c12006-04-07 19:04:09 +00001929 include_dirs = []
1930 extra_compile_args = []
Thomas Heller17984892006-08-04 18:57:34 +00001931 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00001932 sources = ['_ctypes/_ctypes.c',
1933 '_ctypes/callbacks.c',
1934 '_ctypes/callproc.c',
1935 '_ctypes/stgdict.c',
Thomas Heller001d3a12010-08-08 17:56:41 +00001936 '_ctypes/cfield.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00001937 depends = ['_ctypes/ctypes.h']
1938
1939 if sys.platform == 'darwin':
Ronald Oussoren30a171f2010-09-16 11:35:07 +00001940 sources.append('_ctypes/malloc_closure.c')
Thomas Hellercf567c12006-03-08 19:51:58 +00001941 sources.append('_ctypes/darwin/dlfcn_simple.c')
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001942 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00001943 include_dirs.append('_ctypes/darwin')
1944# XXX Is this still needed?
1945## extra_link_args.extend(['-read_only_relocs', 'warning'])
1946
Thomas Heller17984892006-08-04 18:57:34 +00001947 elif sys.platform == 'sunos5':
Martin v. Löwis73f12a32006-08-09 23:42:18 +00001948 # XXX This shouldn't be necessary; it appears that some
1949 # of the assembler code is non-PIC (i.e. it has relocations
1950 # when it shouldn't. The proper fix would be to rewrite
1951 # the assembler code to be PIC.
1952 # This only works with GCC; the Sun compiler likely refuses
1953 # this option. If you want to compile ctypes with the Sun
1954 # compiler, please research a proper solution, instead of
1955 # finding some -z option for the Sun compiler.
Thomas Heller17984892006-08-04 18:57:34 +00001956 extra_link_args.append('-mimpure-text')
1957
Thomas Hellerde2d78a2008-06-02 18:41:30 +00001958 elif sys.platform.startswith('hp-ux'):
Thomas Heller03b75dd2008-05-20 19:53:47 +00001959 extra_link_args.append('-fPIC')
1960
Thomas Hellercf567c12006-03-08 19:51:58 +00001961 ext = Extension('_ctypes',
1962 include_dirs=include_dirs,
1963 extra_compile_args=extra_compile_args,
Thomas Heller17984892006-08-04 18:57:34 +00001964 extra_link_args=extra_link_args,
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001965 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00001966 sources=sources,
1967 depends=depends)
1968 ext_test = Extension('_ctypes_test',
1969 sources=['_ctypes/_ctypes_test.c'])
1970 self.extensions.extend([ext, ext_test])
1971
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001972 if not '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS"):
1973 return
1974
Thomas Heller8bdf81d2008-03-04 20:09:11 +00001975 if sys.platform == 'darwin':
1976 # OS X 10.5 comes with libffi.dylib; the include files are
1977 # in /usr/include/ffi
1978 inc_dirs.append('/usr/include/ffi')
1979
Benjamin Peterson1c335e62010-01-01 15:16:29 +00001980 ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
Matthias Kloseca6d9e92010-04-21 21:45:30 +00001981 if not ffi_inc or ffi_inc[0] == '':
Benjamin Peterson1c335e62010-01-01 15:16:29 +00001982 ffi_inc = find_file('ffi.h', [], inc_dirs)
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001983 if ffi_inc is not None:
1984 ffi_h = ffi_inc[0] + '/ffi.h'
1985 fp = open(ffi_h)
1986 while 1:
1987 line = fp.readline()
1988 if not line:
1989 ffi_inc = None
1990 break
1991 if line.startswith('#define LIBFFI_H'):
1992 break
1993 ffi_lib = None
1994 if ffi_inc is not None:
1995 for lib_name in ('ffi_convenience', 'ffi_pic', 'ffi'):
Tarek Ziadé35a3f572010-03-05 00:29:38 +00001996 if (self.compiler.find_library_file(lib_dirs, lib_name)):
Martin v. Löwis9176fc12006-04-11 11:12:43 +00001997 ffi_lib = lib_name
1998 break
1999
2000 if ffi_inc and ffi_lib:
2001 ext.include_dirs.extend(ffi_inc)
2002 ext.libraries.append(ffi_lib)
2003 self.use_system_libffi = True
2004
2005
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00002006class PyBuildInstall(install):
2007 # Suppress the warning about installation into the lib_dynload
2008 # directory, which is not in sys.path when running Python during
2009 # installation:
2010 def initialize_options (self):
2011 install.initialize_options(self)
2012 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00002013
Michael W. Hudson529a5052002-12-17 16:47:17 +00002014class PyBuildInstallLib(install_lib):
2015 # Do exactly what install_lib does but make sure correct access modes get
2016 # set on installed directories and files. All installed files with get
2017 # mode 644 unless they are a shared library in which case they will get
2018 # mode 755. All installed directories will get mode 755.
2019
2020 so_ext = sysconfig.get_config_var("SO")
2021
2022 def install(self):
2023 outfiles = install_lib.install(self)
2024 self.set_file_modes(outfiles, 0644, 0755)
2025 self.set_dir_modes(self.install_dir, 0755)
2026 return outfiles
2027
2028 def set_file_modes(self, files, defaultMode, sharedLibMode):
2029 if not self.is_chmod_supported(): return
2030 if not files: return
2031
2032 for filename in files:
2033 if os.path.islink(filename): continue
2034 mode = defaultMode
2035 if filename.endswith(self.so_ext): mode = sharedLibMode
2036 log.info("changing mode of %s to %o", filename, mode)
2037 if not self.dry_run: os.chmod(filename, mode)
2038
2039 def set_dir_modes(self, dirname, mode):
2040 if not self.is_chmod_supported(): return
2041 os.path.walk(dirname, self.set_dir_modes_visitor, mode)
2042
2043 def set_dir_modes_visitor(self, mode, dirname, names):
2044 if os.path.islink(dirname): return
2045 log.info("changing mode of %s to %o", dirname, mode)
2046 if not self.dry_run: os.chmod(dirname, mode)
2047
2048 def is_chmod_supported(self):
2049 return hasattr(os, 'chmod')
2050
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002051SUMMARY = """
2052Python is an interpreted, interactive, object-oriented programming
2053language. It is often compared to Tcl, Perl, Scheme or Java.
2054
2055Python combines remarkable power with very clear syntax. It has
2056modules, classes, exceptions, very high level dynamic data types, and
2057dynamic typing. There are interfaces to many system calls and
2058libraries, as well as to various windowing systems (X11, Motif, Tk,
2059Mac, MFC). New built-in modules are easily written in C or C++. Python
2060is also usable as an extension language for applications that need a
2061programmable interface.
2062
2063The Python implementation is portable: it runs on many brands of UNIX,
2064on Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't
2065listed here, it may still be supported, if there's a C compiler for
2066it. Ask around on comp.lang.python -- or just try compiling Python
2067yourself.
2068"""
2069
2070CLASSIFIERS = """
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002071Development Status :: 6 - Mature
2072License :: OSI Approved :: Python Software Foundation License
2073Natural Language :: English
2074Programming Language :: C
2075Programming Language :: Python
2076Topic :: Software Development
2077"""
2078
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002079def main():
Andrew M. Kuchling62686692001-05-21 20:48:09 +00002080 # turn off warnings when deprecated modules are imported
2081 import warnings
2082 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00002083 setup(# PyPI Metadata (PEP 301)
2084 name = "Python",
2085 version = sys.version.split()[0],
2086 url = "http://www.python.org/%s" % sys.version[:3],
2087 maintainer = "Guido van Rossum and the Python community",
2088 maintainer_email = "python-dev@python.org",
2089 description = "A high-level object-oriented programming language",
2090 long_description = SUMMARY.strip(),
2091 license = "PSF license",
2092 classifiers = filter(None, CLASSIFIERS.split("\n")),
2093 platforms = ["Many"],
2094
2095 # Build info
Michael W. Hudson529a5052002-12-17 16:47:17 +00002096 cmdclass = {'build_ext':PyBuildExt, 'install':PyBuildInstall,
2097 'install_lib':PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002098 # The struct module is defined here, because build_ext won't be
2099 # called unless there's at least one extension module defined.
Bob Ippolito7ccc95a2006-05-23 19:11:34 +00002100 ext_modules=[Extension('_struct', ['_struct.c'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00002101
2102 # Scripts to install
Skip Montanaro852f7992004-06-26 22:29:42 +00002103 scripts = ['Tools/scripts/pydoc', 'Tools/scripts/idle',
Martin v. Löwiscdbc9772008-03-24 12:57:53 +00002104 'Tools/scripts/2to3',
Skip Montanaro852f7992004-06-26 22:29:42 +00002105 'Lib/smtpd.py']
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002106 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00002107
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002108# --install-platlib
2109if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00002110 main()