blob: 3b064eb2b9335c5d8c6514636579651c7a916a19 [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 Heimesaf98da12008-01-27 15:18:18 +00007from glob import glob
Tarek Ziadéedacea32010-01-29 11:41:03 +00008import sysconfig
Michael W. Hudson529a5052002-12-17 16:47:17 +00009
10from distutils import log
Andrew M. Kuchling8d7f0862001-02-23 16:32:32 +000011from distutils import text_file
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +000012from distutils.errors import *
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000013from distutils.core import Extension, setup
14from distutils.command.build_ext import build_ext
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +000015from distutils.command.install import install
Michael W. Hudson529a5052002-12-17 16:47:17 +000016from distutils.command.install_lib import install_lib
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000017
Gregory P. Smithb04ded42010-01-03 00:38:10 +000018# Were we compiled --with-pydebug or with #define Py_DEBUG?
19COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
20
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000021# This global variable is used to hold the list of modules to be disabled.
22disabled_module_list = []
23
Michael W. Hudson39230b32002-01-16 15:26:48 +000024def add_dir_to_list(dirlist, dir):
25 """Add the directory 'dir' to the list 'dirlist' (at the front) if
26 1) 'dir' is not already in 'dirlist'
27 2) 'dir' actually exists, and is a directory."""
Jack Jansen4439b7c2002-06-26 15:44:30 +000028 if dir is not None and os.path.isdir(dir) and dir not in dirlist:
Michael W. Hudson39230b32002-01-16 15:26:48 +000029 dirlist.insert(0, dir)
30
Ronald Oussoren2c12ab12010-06-03 14:42:25 +000031def macosx_sdk_root():
32 """
33 Return the directory of the current OSX SDK,
34 or '/' if no SDK was specified.
35 """
36 cflags = sysconfig.get_config_var('CFLAGS')
37 m = re.search(r'-isysroot\s+(\S+)', cflags)
38 if m is None:
39 sysroot = '/'
40 else:
41 sysroot = m.group(1)
42 return sysroot
43
44def is_macosx_sdk_path(path):
45 """
46 Returns True if 'path' can be located in an OSX SDK
47 """
48 return path.startswith('/usr/') or path.startswith('/System/')
49
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000050def find_file(filename, std_dirs, paths):
51 """Searches for the directory where a given file is located,
52 and returns a possibly-empty list of additional directories, or None
53 if the file couldn't be found at all.
Fredrik Lundhade711a2001-01-24 08:00:28 +000054
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000055 'filename' is the name of a file, such as readline.h or libcrypto.a.
56 'std_dirs' is the list of standard system directories; if the
57 file is found in one of them, no additional directives are needed.
58 'paths' is a list of additional locations to check; if the file is
59 found in one of them, the resulting list will contain the directory.
60 """
Ronald Oussoren2c12ab12010-06-03 14:42:25 +000061 if sys.platform == 'darwin':
62 # Honor the MacOSX SDK setting when one was specified.
63 # An SDK is a directory with the same structure as a real
64 # system, but with only header files and libraries.
65 sysroot = macosx_sdk_root()
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000066
67 # Check the standard locations
68 for dir in std_dirs:
69 f = os.path.join(dir, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +000070
71 if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
72 f = os.path.join(sysroot, dir[1:], filename)
73
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000074 if os.path.exists(f): return []
75
76 # Check the additional directories
77 for dir in paths:
78 f = os.path.join(dir, filename)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +000079
80 if sys.platform == 'darwin' and is_macosx_sdk_path(dir):
81 f = os.path.join(sysroot, dir[1:], filename)
82
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000083 if os.path.exists(f):
84 return [dir]
85
86 # Not found anywhere
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +000087 return None
88
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +000089def find_library_file(compiler, libname, std_dirs, paths):
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +000090 result = compiler.find_library_file(std_dirs + paths, libname)
91 if result is None:
92 return None
Fredrik Lundhade711a2001-01-24 08:00:28 +000093
Ronald Oussoren2c12ab12010-06-03 14:42:25 +000094 if sys.platform == 'darwin':
95 sysroot = macosx_sdk_root()
96
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +000097 # Check whether the found file is in one of the standard directories
98 dirname = os.path.dirname(result)
99 for p in std_dirs:
100 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000101 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000102
103 if sys.platform == 'darwin' and is_macosx_sdk_path(p):
104 if os.path.join(sysroot, p[1:]) == dirname:
105 return [ ]
106
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000107 if p == dirname:
108 return [ ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000109
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000110 # Otherwise, it must have been in one of the additional directories,
111 # so we have to figure out which one.
112 for p in paths:
113 # Ensure path doesn't end with path separator
Skip Montanaro9f5178a2003-05-06 20:59:57 +0000114 p = p.rstrip(os.sep)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000115
116 if sys.platform == 'darwin' and is_macosx_sdk_path(p):
117 if os.path.join(sysroot, p[1:]) == dirname:
118 return [ p ]
119
Andrew M. Kuchlinga246d9f2002-11-27 13:43:46 +0000120 if p == dirname:
121 return [p]
122 else:
123 assert False, "Internal error: Path not found in std_dirs or paths"
Tim Peters2c60f7a2003-01-29 03:49:43 +0000124
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000125def module_enabled(extlist, modname):
126 """Returns whether the module 'modname' is present in the list
127 of extensions 'extlist'."""
128 extlist = [ext for ext in extlist if ext.name == modname]
129 return len(extlist)
Fredrik Lundhade711a2001-01-24 08:00:28 +0000130
Jack Jansen144ebcc2001-08-05 22:31:19 +0000131def find_module_file(module, dirlist):
132 """Find a module in a set of possible folders. If it is not found
133 return the unadorned filename"""
134 list = find_file(module, [], dirlist)
135 if not list:
136 return module
137 if len(list) > 1:
Guido van Rossum12471d62003-02-20 02:11:43 +0000138 log.info("WARNING: multiple copies of %s found"%module)
Jack Jansen144ebcc2001-08-05 22:31:19 +0000139 return os.path.join(list[0], module)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000140
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000141class PyBuildExt(build_ext):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000142
Guido van Rossumd8faa362007-04-27 19:54:29 +0000143 def __init__(self, dist):
144 build_ext.__init__(self, dist)
145 self.failed = []
146
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000147 def build_extensions(self):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000148
149 # Detect which modules should be compiled
Guido van Rossumd8faa362007-04-27 19:54:29 +0000150 missing = self.detect_modules()
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000151
152 # Remove modules that are present on the disabled list
Christian Heimes679db4a2008-01-18 09:56:22 +0000153 extensions = [ext for ext in self.extensions
154 if ext.name not in disabled_module_list]
155 # move ctypes to the end, it depends on other modules
156 ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
157 if "_ctypes" in ext_map:
158 ctypes = extensions.pop(ext_map["_ctypes"])
159 extensions.append(ctypes)
160 self.extensions = extensions
Fredrik Lundhade711a2001-01-24 08:00:28 +0000161
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000162 # Fix up the autodetected modules, prefixing all the source files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000163 # with Modules/.
164 srcdir = sysconfig.get_config_var('srcdir')
Guido van Rossume0fea6c2002-10-14 20:48:09 +0000165 if not srcdir:
166 # Maybe running on Windows but not using CYGWIN?
167 raise ValueError("No source directory; cannot proceed.")
Neil Schemenauer4d491a52009-02-06 00:27:50 +0000168 srcdir = os.path.abspath(srcdir)
Neil Schemenauer014bf282009-02-05 16:35:45 +0000169 moddirlist = [os.path.join(srcdir, 'Modules')]
Michael W. Hudson5b109102002-01-23 15:04:41 +0000170
Jack Jansen144ebcc2001-08-05 22:31:19 +0000171 # Platform-dependent module source and include directories
172 platform = self.get_platform()
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000173
Andrew M. Kuchling3da989c2001-02-28 22:49:26 +0000174 # Fix up the paths for scripts, too
175 self.distribution.scripts = [os.path.join(srcdir, filename)
176 for filename in self.distribution.scripts]
177
Christian Heimesaf98da12008-01-27 15:18:18 +0000178 # Python header files
Neil Schemenauer014bf282009-02-05 16:35:45 +0000179 headers = [sysconfig.get_config_h_filename()]
Tarek Ziadéedacea32010-01-29 11:41:03 +0000180 headers += glob(os.path.join(sysconfig.get_path('platinclude'), "*.h"))
Christian Heimesaf98da12008-01-27 15:18:18 +0000181
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000182 for ext in self.extensions[:]:
Jack Jansen144ebcc2001-08-05 22:31:19 +0000183 ext.sources = [ find_module_file(filename, moddirlist)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000184 for filename in ext.sources ]
Jeremy Hylton340043e2002-06-13 17:38:11 +0000185 if ext.depends is not None:
Neil Schemenauer014bf282009-02-05 16:35:45 +0000186 ext.depends = [find_module_file(filename, moddirlist)
Jeremy Hylton340043e2002-06-13 17:38:11 +0000187 for filename in ext.depends]
Christian Heimesaf98da12008-01-27 15:18:18 +0000188 else:
189 ext.depends = []
190 # re-compile extensions if a header file has been changed
191 ext.depends.extend(headers)
192
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000193 # If a module has already been built statically,
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000194 # don't build it here
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000195 if ext.name in sys.builtin_module_names:
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000196 self.extensions.remove(ext)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000197
Ronald Oussoren94f25282010-05-05 19:11:21 +0000198 # Parse Modules/Setup and Modules/Setup.local to figure out which
199 # modules are turned on in the file.
200 remove_modules = []
201 for filename in ('Modules/Setup', 'Modules/Setup.local'):
202 input = text_file.TextFile(filename, join_lines=1)
203 while 1:
204 line = input.readline()
205 if not line: break
206 line = line.split()
207 remove_modules.append(line[0])
208 input.close()
Tim Peters1b27f862005-12-30 18:42:42 +0000209
Ronald Oussoren94f25282010-05-05 19:11:21 +0000210 for ext in self.extensions[:]:
211 if ext.name in remove_modules:
212 self.extensions.remove(ext)
Michael W. Hudson5b109102002-01-23 15:04:41 +0000213
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000214 # When you run "make CC=altcc" or something similar, you really want
215 # those environment variables passed into the setup.py phase. Here's
216 # a small set of useful ones.
217 compiler = os.environ.get('CC')
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000218 args = {}
219 # unfortunately, distutils doesn't let us provide separate C and C++
220 # compilers
221 if compiler is not None:
Martin v. Löwisd7c795e2005-04-25 07:14:03 +0000222 (ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
223 args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000224 self.compiler_obj.set_executables(**args)
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000225
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000226 build_ext.build_extensions(self)
227
Guido van Rossumd8faa362007-04-27 19:54:29 +0000228 longest = max([len(e.name) for e in self.extensions])
229 if self.failed:
230 longest = max(longest, max([len(name) for name in self.failed]))
231
232 def print_three_column(lst):
233 lst.sort(key=str.lower)
234 # guarantee zip() doesn't drop anything
235 while len(lst) % 3:
236 lst.append("")
237 for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
238 print("%-*s %-*s %-*s" % (longest, e, longest, f,
239 longest, g))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000240
241 if missing:
242 print()
Benjamin Petersonda10d3b2009-01-01 00:23:30 +0000243 print("Python build finished, but the necessary bits to build "
244 "these modules were not found:")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245 print_three_column(missing)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000246 print("To find the necessary bits, look in setup.py in"
247 " detect_modules() for the module's name.")
248 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000249
250 if self.failed:
251 failed = self.failed[:]
252 print()
253 print("Failed to build these modules:")
254 print_three_column(failed)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000255 print()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000256
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000257 def build_extension(self, ext):
258
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259 if ext.name == '_ctypes':
260 if not self.configure_ctypes(ext):
261 return
262
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000263 try:
264 build_ext.build_extension(self, ext)
Guido van Rossumb940e112007-01-10 16:19:56 +0000265 except (CCompilerError, DistutilsError) as why:
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000266 self.announce('WARNING: building of extension "%s" failed: %s' %
267 (ext.name, sys.exc_info()[1]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000268 self.failed.append(ext.name)
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000269 return
Jack Jansenf49c6f92001-11-01 14:44:15 +0000270 # Workaround for Mac OS X: The Carbon-based modules cannot be
271 # reliably imported into a command-line Python
272 if 'Carbon' in ext.extra_link_args:
Michael W. Hudson5b109102002-01-23 15:04:41 +0000273 self.announce(
274 'WARNING: skipping import check for Carbon-based "%s"' %
275 ext.name)
276 return
Georg Brandlfcaf9102008-07-16 02:17:56 +0000277
278 if self.get_platform() == 'darwin' and (
Benjamin Petersonfc576352008-07-16 02:39:02 +0000279 sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
Georg Brandlfcaf9102008-07-16 02:17:56 +0000280 # Don't bother doing an import check when an extension was
281 # build with an explicit '-arch' flag on OSX. That's currently
282 # only used to build 32-bit only extensions in a 4-way
283 # universal build and loading 32-bit code into a 64-bit
284 # process will fail.
285 self.announce(
286 'WARNING: skipping import check for "%s"' %
287 ext.name)
288 return
289
Jason Tishler24cf7762002-05-22 16:46:15 +0000290 # Workaround for Cygwin: Cygwin currently has fork issues when many
291 # modules have been imported
292 if self.get_platform() == 'cygwin':
293 self.announce('WARNING: skipping import check for Cygwin-based "%s"'
294 % ext.name)
295 return
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000296 ext_filename = os.path.join(
297 self.build_lib,
298 self.get_ext_filename(self.get_ext_fullname(ext.name)))
Guido van Rossumc3fee692008-07-17 16:23:53 +0000299
300 # If the build directory didn't exist when setup.py was
301 # started, sys.path_importer_cache has a negative result
302 # cached. Clear that cache before trying to import.
303 sys.path_importer_cache.clear()
304
Andrew M. Kuchling62686692001-05-21 20:48:09 +0000305 try:
Michael W. Hudsonaf142892002-01-23 15:07:46 +0000306 imp.load_dynamic(ext.name, ext_filename)
Guido van Rossumb940e112007-01-10 16:19:56 +0000307 except ImportError as why:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000308 self.failed.append(ext.name)
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000309 self.announce('*** WARNING: renaming "%s" since importing it'
310 ' failed: %s' % (ext.name, why), level=3)
311 assert not self.inplace
312 basename, tail = os.path.splitext(ext_filename)
313 newname = basename + "_failed" + tail
314 if os.path.exists(newname):
315 os.remove(newname)
316 os.rename(ext_filename, newname)
317
318 # XXX -- This relies on a Vile HACK in
319 # distutils.command.build_ext.build_extension(). The
320 # _built_objects attribute is stored there strictly for
321 # use here.
322 # If there is a failure, _built_objects may not be there,
323 # so catch the AttributeError and move on.
324 try:
325 for filename in self._built_objects:
326 os.remove(filename)
327 except AttributeError:
328 self.announce('unable to remove files (ignored)')
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000329 except:
Neal Norwitz3f5fcc82003-02-28 17:21:39 +0000330 exc_type, why, tb = sys.exc_info()
Neal Norwitz6e2d1c72003-02-28 17:39:42 +0000331 self.announce('*** WARNING: importing extension "%s" '
332 'failed with %s: %s' % (ext.name, exc_type, why),
333 level=3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000334 self.failed.append(ext.name)
Fred Drake9028d0a2001-12-06 22:59:54 +0000335
Neal Norwitz51dead72003-06-17 02:51:28 +0000336 def get_platform(self):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000337 # Get value of sys.platform
Antoine Pitrou6103ab12009-10-24 20:11:21 +0000338 for platform in ['cygwin', 'darwin', 'osf1']:
Neal Norwitz51dead72003-06-17 02:51:28 +0000339 if sys.platform.startswith(platform):
340 return platform
341 return sys.platform
Andrew M. Kuchling34febf52001-01-24 03:31:07 +0000342
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000343 def detect_modules(self):
Fredrik Lundhade711a2001-01-24 08:00:28 +0000344 # Ensure that /usr/local is always used
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000345 add_dir_to_list(self.compiler_obj.library_dirs, '/usr/local/lib')
346 add_dir_to_list(self.compiler_obj.include_dirs, '/usr/local/include')
Michael W. Hudson39230b32002-01-16 15:26:48 +0000347
Brett Cannon516592f2004-12-07 00:42:59 +0000348 # Add paths specified in the environment variables LDFLAGS and
Brett Cannon4810eb92004-12-31 08:11:21 +0000349 # CPPFLAGS for header and library files.
Brett Cannon5399c6d2004-12-18 20:48:09 +0000350 # We must get the values from the Makefile and not the environment
351 # directly since an inconsistently reproducible issue comes up where
352 # the environment variable is not set even though the value were passed
Brett Cannon4810eb92004-12-31 08:11:21 +0000353 # into configure and stored in the Makefile (issue found on OS X 10.3).
Brett Cannon516592f2004-12-07 00:42:59 +0000354 for env_var, arg_name, dir_list in (
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000355 ('LDFLAGS', '-R', self.compiler_obj.runtime_library_dirs),
356 ('LDFLAGS', '-L', self.compiler_obj.library_dirs),
357 ('CPPFLAGS', '-I', self.compiler_obj.include_dirs)):
Brett Cannon5399c6d2004-12-18 20:48:09 +0000358 env_val = sysconfig.get_config_var(env_var)
Brett Cannon516592f2004-12-07 00:42:59 +0000359 if env_val:
Brett Cannon4810eb92004-12-31 08:11:21 +0000360 # To prevent optparse from raising an exception about any
Skip Montanaroa5c2a512008-10-07 02:51:48 +0000361 # options in env_val that it doesn't know about we strip out
Brett Cannon4810eb92004-12-31 08:11:21 +0000362 # all double dashes and any dashes followed by a character
363 # that is not for the option we are dealing with.
364 #
365 # Please note that order of the regex is important! We must
366 # strip out double-dashes first so that we don't end up with
367 # substituting "--Long" to "-Long" and thus lead to "ong" being
368 # used for a library directory.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000369 env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
370 ' ', env_val)
Brett Cannon84667c02004-12-07 03:25:18 +0000371 parser = optparse.OptionParser()
Brett Cannon4810eb92004-12-31 08:11:21 +0000372 # Make sure that allowing args interspersed with options is
373 # allowed
374 parser.allow_interspersed_args = True
375 parser.error = lambda msg: None
Brett Cannon84667c02004-12-07 03:25:18 +0000376 parser.add_option(arg_name, dest="dirs", action="append")
377 options = parser.parse_args(env_val.split())[0]
Brett Cannon44837712005-01-02 21:54:07 +0000378 if options.dirs:
Christian Heimes292d3512008-02-03 16:51:08 +0000379 for directory in reversed(options.dirs):
Brett Cannon44837712005-01-02 21:54:07 +0000380 add_dir_to_list(dir_list, directory)
Skip Montanarodecc6a42003-01-01 20:07:49 +0000381
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000382 if os.path.normpath(sys.prefix) != '/usr':
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000383 add_dir_to_list(self.compiler_obj.library_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000384 sysconfig.get_config_var("LIBDIR"))
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000385 add_dir_to_list(self.compiler_obj.include_dirs,
Michael W. Hudson90b8e4d2002-08-02 13:55:50 +0000386 sysconfig.get_config_var("INCLUDEDIR"))
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000387
388 # lib_dirs and inc_dirs are used to search for files;
389 # if a file is found in one of those directories, it can
390 # be assumed that no additional -I,-L directives are needed.
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000391 lib_dirs = self.compiler_obj.library_dirs + [
Martin v. Löwisfba73692004-11-13 11:13:35 +0000392 '/lib64', '/usr/lib64',
393 '/lib', '/usr/lib',
394 ]
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000395 inc_dirs = self.compiler_obj.include_dirs + ['/usr/include']
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000396 exts = []
Guido van Rossumd8faa362007-04-27 19:54:29 +0000397 missing = []
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000398
Brett Cannon4454a1f2005-04-15 20:32:39 +0000399 config_h = sysconfig.get_config_h_filename()
400 config_h_vars = sysconfig.parse_config_h(open(config_h))
401
Fredrik Lundhade711a2001-01-24 08:00:28 +0000402 platform = self.get_platform()
Neil Schemenauer014bf282009-02-05 16:35:45 +0000403 srcdir = sysconfig.get_config_var('srcdir')
Michael W. Hudson5b109102002-01-23 15:04:41 +0000404
Andrew M. Kuchling7883dc82003-10-24 18:26:26 +0000405 # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
406 if platform in ['osf1', 'unixware7', 'openunix8']:
Skip Montanaro22e00c42003-05-06 20:43:34 +0000407 lib_dirs += ['/usr/ccs/lib']
408
Thomas Wouters477c8d52006-05-27 19:21:47 +0000409 if platform == 'darwin':
410 # This should work on any unixy platform ;-)
411 # If the user has bothered specifying additional -I and -L flags
412 # in OPT and LDFLAGS we might as well use them here.
413 # NOTE: using shlex.split would technically be more correct, but
414 # also gives a bootstrap problem. Let's hope nobody uses directories
415 # with whitespace in the name to store libraries.
416 cflags, ldflags = sysconfig.get_config_vars(
417 'CFLAGS', 'LDFLAGS')
418 for item in cflags.split():
419 if item.startswith('-I'):
420 inc_dirs.append(item[2:])
421
422 for item in ldflags.split():
423 if item.startswith('-L'):
424 lib_dirs.append(item[2:])
425
Fredrik Lundhade711a2001-01-24 08:00:28 +0000426 # Check for MacOS X, which doesn't need libm.a at all
427 math_libs = ['m']
Ronald Oussoren94f25282010-05-05 19:11:21 +0000428 if platform == 'darwin':
Fredrik Lundhade711a2001-01-24 08:00:28 +0000429 math_libs = []
Michael W. Hudson5b109102002-01-23 15:04:41 +0000430
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000431 # XXX Omitted modules: gl, pure, dl, SGI-specific modules
432
433 #
434 # The following modules are all pretty straightforward, and compile
435 # on pretty much any POSIXish platform.
436 #
Fredrik Lundhade711a2001-01-24 08:00:28 +0000437
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000438 # Some modules that are normally always on:
Fred Drake2de74712001-02-01 05:26:54 +0000439 exts.append( Extension('_weakref', ['_weakref.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000440
441 # array objects
442 exts.append( Extension('array', ['arraymodule.c']) )
443 # complex math library functions
Mark Dickinsonf3718592009-12-21 15:27:41 +0000444 exts.append( Extension('cmath', ['cmathmodule.c', '_math.c'],
445 depends=['_math.h'],
Andrew M. Kuchling5ddb25f2001-01-23 22:21:11 +0000446 libraries=math_libs) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000447 # math library functions, e.g. sin()
Mark Dickinson664b5112009-12-16 20:23:42 +0000448 exts.append( Extension('math', ['mathmodule.c', '_math.c'],
Mark Dickinson8a591132009-12-17 08:35:56 +0000449 depends=['_math.h'],
Andrew M. Kuchling5ddb25f2001-01-23 22:21:11 +0000450 libraries=math_libs) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000451 # time operations and variables
Andrew M. Kuchling5ddb25f2001-01-23 22:21:11 +0000452 exts.append( Extension('time', ['timemodule.c'],
453 libraries=math_libs) )
Brett Cannon057e7202004-06-24 01:38:47 +0000454 exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'],
Guido van Rossuma29d5082002-12-16 20:31:57 +0000455 libraries=math_libs) )
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000456 # fast iterator tools implemented in C
457 exts.append( Extension("itertools", ["itertoolsmodule.c"]) )
Christian Heimesfe337bf2008-03-23 21:54:12 +0000458 # random number generator implemented in C
459 exts.append( Extension("_random", ["_randommodule.c"]) )
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000460 # high-performance collections
Guido van Rossumd8faa362007-04-27 19:54:29 +0000461 exts.append( Extension("_collections", ["_collectionsmodule.c"]) )
Raymond Hettinger0c410272004-01-05 10:13:35 +0000462 # bisect
463 exts.append( Extension("_bisect", ["_bisectmodule.c"]) )
Raymond Hettingerb3af1812003-11-08 10:24:38 +0000464 # heapq
Raymond Hettingerc46cb2a2004-04-19 19:06:21 +0000465 exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000466 # operator.add() and similar goodies
467 exts.append( Extension('operator', ['operator.c']) )
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000468 # C-optimized pickle replacement
469 exts.append( Extension("_pickle", ["_pickle.c"]) )
Collin Winter670e6922007-03-21 02:57:17 +0000470 # atexit
471 exts.append( Extension("atexit", ["atexitmodule.c"]) )
Christian Heimes90540002008-05-08 14:29:10 +0000472 # _json speedups
473 exts.append( Extension("_json", ["_json.c"]) )
Marc-André Lemburg261b8e22001-02-02 12:12:44 +0000474 # Python C API test module
Mark Dickinsona06f44b2009-02-10 16:18:22 +0000475 exts.append( Extension('_testcapi', ['_testcapimodule.c'],
476 depends=['testcapi_long.h']) )
Fred Drake0e474a82007-10-11 18:01:43 +0000477 # profiler (_lsprof is for cProfile.py)
Armin Rigoa871ef22006-02-08 12:53:56 +0000478 exts.append( Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000479 # static Unicode character database
Walter Dörwalde9eaab42007-05-22 16:02:13 +0000480 exts.append( Extension('unicodedata', ['unicodedata.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000481
482 # Modules with some UNIX dependencies -- on by default:
483 # (If you have a really backward UNIX, select and socket may not be
484 # supported...)
485
486 # fcntl(2) and ioctl(2)
487 exts.append( Extension('fcntl', ['fcntlmodule.c']) )
Ronald Oussoren94f25282010-05-05 19:11:21 +0000488 # pwd(3)
489 exts.append( Extension('pwd', ['pwdmodule.c']) )
490 # grp(3)
491 exts.append( Extension('grp', ['grpmodule.c']) )
492 # spwd, shadow passwords
493 if (config_h_vars.get('HAVE_GETSPNAM', False) or
494 config_h_vars.get('HAVE_GETSPENT', False)):
495 exts.append( Extension('spwd', ['spwdmodule.c']) )
Guido van Rossumd8faa362007-04-27 19:54:29 +0000496 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +0000497 missing.append('spwd')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000498
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000499 # select(2); not on ancient System V
500 exts.append( Extension('select', ['selectmodule.c']) )
501
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000502 # Fred Drake's interface to the Python parser
503 exts.append( Extension('parser', ['parsermodule.c']) )
504
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000505 # Memory-mapped files (also works on Win32).
Ronald Oussoren94f25282010-05-05 19:11:21 +0000506 exts.append( Extension('mmap', ['mmapmodule.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000507
Andrew M. Kuchling57269d02004-08-31 13:37:25 +0000508 # Lance Ellinghaus's syslog module
Ronald Oussoren94f25282010-05-05 19:11:21 +0000509 # syslog daemon interface
510 exts.append( Extension('syslog', ['syslogmodule.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000511
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000512 #
Andrew M. Kuchling5bbc7b92001-01-18 20:39:34 +0000513 # Here ends the simple stuff. From here on, modules need certain
514 # libraries, are platform-specific, or present other surprises.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000515 #
516
517 # Multimedia modules
518 # These don't work for 64-bit platforms!!!
519 # These represent audio samples or images as strings:
520
Neal Norwitz5e4a3b82004-07-19 16:55:07 +0000521 # Operations on audio samples
Tim Petersf9cbf212004-07-23 02:50:10 +0000522 # According to #993173, this one should actually work fine on
Martin v. Löwis8fbefe22004-07-19 16:42:20 +0000523 # 64-bit platforms.
524 exts.append( Extension('audioop', ['audioop.c']) )
525
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000526 # readline
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000527 do_readline = self.compiler_obj.find_library_file(lib_dirs, 'readline')
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000528 if platform == 'darwin':
529 os_release = int(os.uname()[2].split('.')[0])
Ronald Oussoren961683a2010-03-08 07:09:59 +0000530 dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
531 if dep_target and dep_target.split('.') < ['10', '5']:
532 os_release = 8
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000533 if os_release < 9:
534 # MacOSX 10.4 has a broken readline. Don't try to build
535 # the readline module unless the user has installed a fixed
536 # readline package
537 if find_file('readline/rlconf.h', inc_dirs, []) is None:
538 do_readline = False
Jack Jansen81ae2352006-02-23 15:02:23 +0000539 if do_readline:
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000540 if platform == 'darwin' and os_release < 9:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000541 # In every directory on the search path search for a dynamic
542 # library and then a static library, instead of first looking
Fred Drake0af17612007-09-04 19:43:19 +0000543 # for dynamic libraries on the entire path.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000544 # This way a staticly linked custom readline gets picked up
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000545 # before the (possibly broken) dynamic library in /usr/lib.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000546 readline_extra_link_args = ('-Wl,-search_paths_first',)
547 else:
548 readline_extra_link_args = ()
549
Marc-André Lemburg2efc3232001-01-26 18:23:02 +0000550 readline_libs = ['readline']
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000551 if self.compiler_obj.find_library_file(lib_dirs,
552 'ncursesw'):
Martin v. Löwisa55e55e2006-02-11 15:55:14 +0000553 readline_libs.append('ncursesw')
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000554 elif self.compiler_obj.find_library_file(lib_dirs,
555 'ncurses'):
Andrew M. Kuchling5aa3c4a2001-08-16 20:30:18 +0000556 readline_libs.append('ncurses')
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000557 elif self.compiler_obj.find_library_file(lib_dirs, 'curses'):
Neal Norwitz0b27ff92003-03-31 15:53:49 +0000558 readline_libs.append('curses')
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000559 elif self.compiler_obj.find_library_file(lib_dirs +
560 ['/usr/lib/termcap'],
561 'termcap'):
Marc-André Lemburg2efc3232001-01-26 18:23:02 +0000562 readline_libs.append('termcap')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000563 exts.append( Extension('readline', ['readline.c'],
Marc-André Lemburg7c6fcda2001-01-26 18:03:24 +0000564 library_dirs=['/usr/lib/termcap'],
Thomas Wouters477c8d52006-05-27 19:21:47 +0000565 extra_link_args=readline_extra_link_args,
Marc-André Lemburg2efc3232001-01-26 18:23:02 +0000566 libraries=readline_libs) )
Guido van Rossumd8faa362007-04-27 19:54:29 +0000567 else:
568 missing.append('readline')
569
Ronald Oussoren94f25282010-05-05 19:11:21 +0000570 # crypt module.
Tim Peters2c60f7a2003-01-29 03:49:43 +0000571
Ronald Oussoren94f25282010-05-05 19:11:21 +0000572 if self.compiler_obj.find_library_file(lib_dirs, 'crypt'):
573 libs = ['crypt']
Guido van Rossumd8faa362007-04-27 19:54:29 +0000574 else:
Ronald Oussoren94f25282010-05-05 19:11:21 +0000575 libs = []
576 exts.append( Extension('crypt', ['cryptmodule.c'], libraries=libs) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000577
Skip Montanaroba9e9782003-03-20 23:34:22 +0000578 # CSV files
579 exts.append( Extension('_csv', ['_csv.c']) )
580
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000581 # POSIX subprocess module helper.
582 exts.append( Extension('_posixsubprocess', ['_posixsubprocess.c']) )
583
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000584 # socket(2)
Guido van Rossum47d3a7a2002-06-13 14:41:32 +0000585 exts.append( Extension('_socket', ['socketmodule.c'],
Jeremy Hylton340043e2002-06-13 17:38:11 +0000586 depends = ['socketmodule.h']) )
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000587 # Detect SSL support for the socket module (via _ssl)
Gregory P. Smithade97332005-08-23 21:19:40 +0000588 search_for_ssl_incs_in = [
589 '/usr/local/ssl/include',
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000590 '/usr/contrib/ssl/include/'
591 ]
Gregory P. Smithade97332005-08-23 21:19:40 +0000592 ssl_incs = find_file('openssl/ssl.h', inc_dirs,
593 search_for_ssl_incs_in
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000594 )
Martin v. Löwisa950f7f2003-05-09 09:05:19 +0000595 if ssl_incs is not None:
596 krb5_h = find_file('krb5.h', inc_dirs,
597 ['/usr/kerberos/include'])
598 if krb5_h:
599 ssl_incs += krb5_h
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000600 ssl_libs = find_library_file(self.compiler_obj, 'ssl',lib_dirs,
Andrew M. Kuchlinge7c87322001-01-19 16:58:21 +0000601 ['/usr/local/ssl/lib',
602 '/usr/contrib/ssl/lib/'
603 ] )
Fredrik Lundhade711a2001-01-24 08:00:28 +0000604
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000605 if (ssl_incs is not None and
606 ssl_libs is not None):
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000607 exts.append( Extension('_ssl', ['_ssl.c'],
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +0000608 include_dirs = ssl_incs,
Fredrik Lundhade711a2001-01-24 08:00:28 +0000609 library_dirs = ssl_libs,
Guido van Rossum47d3a7a2002-06-13 14:41:32 +0000610 libraries = ['ssl', 'crypto'],
Jeremy Hylton340043e2002-06-13 17:38:11 +0000611 depends = ['socketmodule.h']), )
Guido van Rossumd8faa362007-04-27 19:54:29 +0000612 else:
613 missing.append('_ssl')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000614
Gregory P. Smithade97332005-08-23 21:19:40 +0000615 # find out which version of OpenSSL we have
616 openssl_ver = 0
617 openssl_ver_re = re.compile(
618 '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
Gregory P. Smithade97332005-08-23 21:19:40 +0000619
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000620 # look for the openssl version header on the compiler search path.
621 opensslv_h = find_file('openssl/opensslv.h', [],
622 inc_dirs + search_for_ssl_incs_in)
623 if opensslv_h:
624 name = os.path.join(opensslv_h[0], 'openssl/opensslv.h')
625 if sys.platform == 'darwin' and is_macosx_sdk_path(name):
626 name = os.path.join(macosx_sdk_root(), name[1:])
627 try:
628 incfile = open(name, 'r')
629 for line in incfile:
630 m = openssl_ver_re.match(line)
631 if m:
632 openssl_ver = eval(m.group(1))
633 except IOError as msg:
634 print("IOError while reading opensshv.h:", msg)
635 pass
Gregory P. Smithade97332005-08-23 21:19:40 +0000636
Guido van Rossumeb1cf4e2007-08-17 17:14:17 +0000637 #print('openssl_ver = 0x%08x' % openssl_ver)
Gregory P. Smithb04ded42010-01-03 00:38:10 +0000638 min_openssl_ver = 0x00907000
639 have_any_openssl = ssl_incs is not None and ssl_libs is not None
640 have_usable_openssl = (have_any_openssl and
641 openssl_ver >= min_openssl_ver)
Gregory P. Smithade97332005-08-23 21:19:40 +0000642
Gregory P. Smithb04ded42010-01-03 00:38:10 +0000643 if have_any_openssl:
644 if have_usable_openssl:
Guido van Rossumeb1cf4e2007-08-17 17:14:17 +0000645 # The _hashlib module wraps optimized implementations
646 # of hash functions from the OpenSSL library.
647 exts.append( Extension('_hashlib', ['_hashopenssl.c'],
Gregory P. Smith5af7fba2010-01-03 14:51:13 +0000648 depends = ['hashlib.h'],
Guido van Rossumeb1cf4e2007-08-17 17:14:17 +0000649 include_dirs = ssl_incs,
650 library_dirs = ssl_libs,
651 libraries = ['ssl', 'crypto']) )
652 else:
653 print("warning: openssl 0x%08x is too old for _hashlib" %
654 openssl_ver)
655 missing.append('_hashlib')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000656
Gregory P. Smithb04ded42010-01-03 00:38:10 +0000657 min_sha2_openssl_ver = 0x00908000
658 if COMPILED_WITH_PYDEBUG or openssl_ver < min_sha2_openssl_ver:
Gregory P. Smithade97332005-08-23 21:19:40 +0000659 # OpenSSL doesn't do these until 0.9.8 so we'll bring our own hash
Gregory P. Smith5af7fba2010-01-03 14:51:13 +0000660 exts.append( Extension('_sha256', ['sha256module.c'],
661 depends=['hashlib.h']) )
662 exts.append( Extension('_sha512', ['sha512module.c'],
663 depends=['hashlib.h']) )
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000664
Gregory P. Smith91ae4a12010-01-03 00:44:10 +0000665 if COMPILED_WITH_PYDEBUG or not have_usable_openssl:
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000666 # no openssl at all, use our own md5 and sha1
Gregory P. Smith5af7fba2010-01-03 14:51:13 +0000667 exts.append( Extension('_md5', ['md5module.c'],
668 depends=['hashlib.h']) )
669 exts.append( Extension('_sha1', ['sha1module.c'],
670 depends=['hashlib.h']) )
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000671
Georg Brandl489cb4f2009-07-11 10:08:49 +0000672 # Modules that provide persistent dictionary-like semantics. You will
673 # probably want to arrange for at least one of them to be available on
674 # your machine, though none are defined by default because of library
675 # dependencies. The Python module dbm/__init__.py provides an
676 # implementation independent wrapper for these; dbm/dumb.py provides
677 # similar functionality (but slower of course) implemented in Python.
678
679 # Sleepycat^WOracle Berkeley DB interface.
680 # http://www.oracle.com/database/berkeley-db/db/index.html
681 #
682 # This requires the Sleepycat^WOracle DB code. The supported versions
683 # are set below. Visit the URL above to download
684 # a release. Most open source OSes come with one or more
685 # versions of BerkeleyDB already installed.
686
Matthias Klose9550aa12010-03-15 12:49:46 +0000687 max_db_ver = (4, 8)
Georg Brandl489cb4f2009-07-11 10:08:49 +0000688 min_db_ver = (3, 3)
689 db_setup_debug = False # verbose debug prints from this script?
690
691 def allow_db_ver(db_ver):
692 """Returns a boolean if the given BerkeleyDB version is acceptable.
693
694 Args:
695 db_ver: A tuple of the version to verify.
696 """
697 if not (min_db_ver <= db_ver <= max_db_ver):
698 return False
699 return True
700
701 def gen_db_minor_ver_nums(major):
702 if major == 4:
703 for x in range(max_db_ver[1]+1):
704 if allow_db_ver((4, x)):
705 yield x
706 elif major == 3:
707 for x in (3,):
708 if allow_db_ver((3, x)):
709 yield x
710 else:
711 raise ValueError("unknown major BerkeleyDB version", major)
712
713 # construct a list of paths to look for the header file in on
714 # top of the normal inc_dirs.
715 db_inc_paths = [
716 '/usr/include/db4',
717 '/usr/local/include/db4',
718 '/opt/sfw/include/db4',
719 '/usr/include/db3',
720 '/usr/local/include/db3',
721 '/opt/sfw/include/db3',
722 # Fink defaults (http://fink.sourceforge.net/)
723 '/sw/include/db4',
724 '/sw/include/db3',
725 ]
726 # 4.x minor number specific paths
727 for x in gen_db_minor_ver_nums(4):
728 db_inc_paths.append('/usr/include/db4%d' % x)
729 db_inc_paths.append('/usr/include/db4.%d' % x)
730 db_inc_paths.append('/usr/local/BerkeleyDB.4.%d/include' % x)
731 db_inc_paths.append('/usr/local/include/db4%d' % x)
732 db_inc_paths.append('/pkg/db-4.%d/include' % x)
733 db_inc_paths.append('/opt/db-4.%d/include' % x)
734 # MacPorts default (http://www.macports.org/)
735 db_inc_paths.append('/opt/local/include/db4%d' % x)
736 # 3.x minor number specific paths
737 for x in gen_db_minor_ver_nums(3):
738 db_inc_paths.append('/usr/include/db3%d' % x)
739 db_inc_paths.append('/usr/local/BerkeleyDB.3.%d/include' % x)
740 db_inc_paths.append('/usr/local/include/db3%d' % x)
741 db_inc_paths.append('/pkg/db-3.%d/include' % x)
742 db_inc_paths.append('/opt/db-3.%d/include' % x)
743
744 # Add some common subdirectories for Sleepycat DB to the list,
745 # based on the standard include directories. This way DB3/4 gets
746 # picked up when it is installed in a non-standard prefix and
747 # the user has added that prefix into inc_dirs.
748 std_variants = []
749 for dn in inc_dirs:
750 std_variants.append(os.path.join(dn, 'db3'))
751 std_variants.append(os.path.join(dn, 'db4'))
752 for x in gen_db_minor_ver_nums(4):
753 std_variants.append(os.path.join(dn, "db4%d"%x))
754 std_variants.append(os.path.join(dn, "db4.%d"%x))
755 for x in gen_db_minor_ver_nums(3):
756 std_variants.append(os.path.join(dn, "db3%d"%x))
757 std_variants.append(os.path.join(dn, "db3.%d"%x))
758
759 db_inc_paths = std_variants + db_inc_paths
760 db_inc_paths = [p for p in db_inc_paths if os.path.exists(p)]
761
762 db_ver_inc_map = {}
763
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000764 if sys.platform == 'darwin':
765 sysroot = macosx_sdk_root()
766
Georg Brandl489cb4f2009-07-11 10:08:49 +0000767 class db_found(Exception): pass
768 try:
769 # See whether there is a Sleepycat header in the standard
770 # search path.
771 for d in inc_dirs + db_inc_paths:
772 f = os.path.join(d, "db.h")
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000773 if sys.platform == 'darwin' and is_macosx_sdk_path(d):
774 f = os.path.join(sysroot, d[1:], "db.h")
775
Georg Brandl489cb4f2009-07-11 10:08:49 +0000776 if db_setup_debug: print("db: looking for db.h in", f)
777 if os.path.exists(f):
Benjamin Peterson019f3612009-08-12 18:18:03 +0000778 f = open(f, "rb").read()
779 m = re.search(br"#define\WDB_VERSION_MAJOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +0000780 if m:
781 db_major = int(m.group(1))
Benjamin Peterson019f3612009-08-12 18:18:03 +0000782 m = re.search(br"#define\WDB_VERSION_MINOR\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +0000783 db_minor = int(m.group(1))
784 db_ver = (db_major, db_minor)
785
786 # Avoid 4.6 prior to 4.6.21 due to a BerkeleyDB bug
787 if db_ver == (4, 6):
Benjamin Peterson019f3612009-08-12 18:18:03 +0000788 m = re.search(br"#define\WDB_VERSION_PATCH\W(\d+)", f)
Georg Brandl489cb4f2009-07-11 10:08:49 +0000789 db_patch = int(m.group(1))
790 if db_patch < 21:
791 print("db.h:", db_ver, "patch", db_patch,
792 "being ignored (4.6.x must be >= 4.6.21)")
793 continue
794
795 if ( (db_ver not in db_ver_inc_map) and
796 allow_db_ver(db_ver) ):
797 # save the include directory with the db.h version
798 # (first occurrence only)
799 db_ver_inc_map[db_ver] = d
800 if db_setup_debug:
801 print("db.h: found", db_ver, "in", d)
802 else:
803 # we already found a header for this library version
804 if db_setup_debug: print("db.h: ignoring", d)
805 else:
806 # ignore this header, it didn't contain a version number
807 if db_setup_debug:
808 print("db.h: no version number version in", d)
809
810 db_found_vers = list(db_ver_inc_map.keys())
811 db_found_vers.sort()
812
813 while db_found_vers:
814 db_ver = db_found_vers.pop()
815 db_incdir = db_ver_inc_map[db_ver]
816
817 # check lib directories parallel to the location of the header
818 db_dirs_to_check = [
819 db_incdir.replace("include", 'lib64'),
820 db_incdir.replace("include", 'lib'),
821 ]
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000822
823 if sys.platform != 'darwin':
824 db_dirs_to_check = list(filter(os.path.isdir, db_dirs_to_check))
825
826 else:
827 # Same as other branch, but takes OSX SDK into account
828 tmp = []
829 for dn in db_dirs_to_check:
830 if is_macosx_sdk_path(dn):
831 if os.path.isdir(os.path.join(sysroot, dn[1:])):
832 tmp.append(dn)
833 else:
834 if os.path.isdir(dn):
835 tmp.append(dn)
836
837 db_dirs_to_check = tmp
Georg Brandl489cb4f2009-07-11 10:08:49 +0000838
839 # Look for a version specific db-X.Y before an ambiguoius dbX
840 # XXX should we -ever- look for a dbX name? Do any
841 # systems really not name their library by version and
842 # symlink to more general names?
843 for dblib in (('db-%d.%d' % db_ver),
844 ('db%d%d' % db_ver),
845 ('db%d' % db_ver[0])):
846 dblib_file = self.compiler.find_library_file(
847 db_dirs_to_check + lib_dirs, dblib )
848 if dblib_file:
849 dblib_dir = [ os.path.abspath(os.path.dirname(dblib_file)) ]
850 raise db_found
851 else:
852 if db_setup_debug: print("db lib: ", dblib, "not found")
853
854 except db_found:
855 if db_setup_debug:
856 print("bsddb using BerkeleyDB lib:", db_ver, dblib)
857 print("bsddb lib dir:", dblib_dir, " inc dir:", db_incdir)
858 db_incs = [db_incdir]
859 dblibs = [dblib]
860 else:
861 if db_setup_debug: print("db: no appropriate library found")
862 db_incs = None
863 dblibs = []
864 dblib_dir = None
865
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866 # The sqlite interface
Thomas Wouters89f507f2006-12-13 04:49:30 +0000867 sqlite_setup_debug = False # verbose debug prints from this script?
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868
869 # We hunt for #define SQLITE_VERSION "n.n.n"
870 # We need to find >= sqlite version 3.0.8
871 sqlite_incdir = sqlite_libdir = None
872 sqlite_inc_paths = [ '/usr/include',
873 '/usr/include/sqlite',
874 '/usr/include/sqlite3',
875 '/usr/local/include',
876 '/usr/local/include/sqlite',
877 '/usr/local/include/sqlite3',
878 ]
879 MIN_SQLITE_VERSION_NUMBER = (3, 0, 8)
880 MIN_SQLITE_VERSION = ".".join([str(x)
881 for x in MIN_SQLITE_VERSION_NUMBER])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000882
883 # Scan the default include directories before the SQLite specific
884 # ones. This allows one to override the copy of sqlite on OSX,
885 # where /usr/include contains an old version of sqlite.
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000886 if sys.platform == 'darwin':
887 sysroot = macosx_sdk_root()
888
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889 for d in inc_dirs + sqlite_inc_paths:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000890 f = os.path.join(d, "sqlite3.h")
Ronald Oussoren2c12ab12010-06-03 14:42:25 +0000891
892 if sys.platform == 'darwin' and is_macosx_sdk_path(d):
893 f = os.path.join(sysroot, d[1:], "sqlite3.h")
894
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895 if os.path.exists(f):
Guido van Rossum452bf512007-02-09 05:32:43 +0000896 if sqlite_setup_debug: print("sqlite: found %s"%f)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897 incf = open(f).read()
898 m = re.search(
899 r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"(.*)"', incf)
900 if m:
901 sqlite_version = m.group(1)
902 sqlite_version_tuple = tuple([int(x)
903 for x in sqlite_version.split(".")])
904 if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
905 # we win!
Thomas Wouters89f507f2006-12-13 04:49:30 +0000906 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +0000907 print("%s/sqlite3.h: version %s"%(d, sqlite_version))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000908 sqlite_incdir = d
909 break
910 else:
911 if sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +0000912 print("%s: version %d is too old, need >= %s"%(d,
913 sqlite_version, MIN_SQLITE_VERSION))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000914 elif sqlite_setup_debug:
Guido van Rossum452bf512007-02-09 05:32:43 +0000915 print("sqlite: %s had no SQLITE_VERSION"%(f,))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916
917 if sqlite_incdir:
918 sqlite_dirs_to_check = [
919 os.path.join(sqlite_incdir, '..', 'lib64'),
920 os.path.join(sqlite_incdir, '..', 'lib'),
921 os.path.join(sqlite_incdir, '..', '..', 'lib64'),
922 os.path.join(sqlite_incdir, '..', '..', 'lib'),
923 ]
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000924 sqlite_libfile = self.compiler_obj.find_library_file(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925 sqlite_dirs_to_check + lib_dirs, 'sqlite3')
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000926 if sqlite_libfile:
927 sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928
929 if sqlite_incdir and sqlite_libdir:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930 sqlite_srcs = ['_sqlite/cache.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000931 '_sqlite/connection.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 '_sqlite/cursor.c',
933 '_sqlite/microprotocols.c',
934 '_sqlite/module.c',
935 '_sqlite/prepare_protocol.c',
936 '_sqlite/row.c',
937 '_sqlite/statement.c',
938 '_sqlite/util.c', ]
939
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000940 sqlite_defines = []
941 if sys.platform != "win32":
942 sqlite_defines.append(('MODULE_NAME', '"sqlite3"'))
943 else:
944 sqlite_defines.append(('MODULE_NAME', '\\"sqlite3\\"'))
945
Gerhard Häringf9cee222010-03-05 15:20:03 +0000946 # Comment this out if you want the sqlite3 module to be able to load extensions.
947 sqlite_defines.append(("SQLITE_OMIT_LOAD_EXTENSION", "1"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948
949 if sys.platform == 'darwin':
950 # In every directory on the search path search for a dynamic
951 # library and then a static library, instead of first looking
952 # for dynamic libraries on the entiry path.
953 # This way a staticly linked custom sqlite gets picked up
954 # before the dynamic library in /usr/lib.
955 sqlite_extra_link_args = ('-Wl,-search_paths_first',)
956 else:
957 sqlite_extra_link_args = ()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958
959 exts.append(Extension('_sqlite3', sqlite_srcs,
960 define_macros=sqlite_defines,
961 include_dirs=["Modules/_sqlite",
962 sqlite_incdir],
963 library_dirs=sqlite_libdir,
964 runtime_library_dirs=sqlite_libdir,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965 extra_link_args=sqlite_extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000966 libraries=["sqlite3",]))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000967 else:
968 missing.append('_sqlite3')
Skip Montanaro22e00c42003-05-06 20:43:34 +0000969
Benjamin Petersond78735d2010-01-01 16:04:23 +0000970 dbm_order = ['gdbm']
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +0000971 # The standard Unix dbm module:
Jack Jansend1b20452002-07-08 21:39:36 +0000972 if platform not in ['cygwin']:
Matthias Klose55708cc2009-04-30 08:06:49 +0000973 config_args = [arg.strip("'")
974 for arg in sysconfig.get_config_var("CONFIG_ARGS").split()]
Benjamin Petersond78735d2010-01-01 16:04:23 +0000975 dbm_args = [arg for arg in config_args
Matthias Klose55708cc2009-04-30 08:06:49 +0000976 if arg.startswith('--with-dbmliborder=')]
977 if dbm_args:
Benjamin Petersond78735d2010-01-01 16:04:23 +0000978 dbm_order = [arg.split('=')[-1] for arg in dbm_args][-1].split(":")
Matthias Klose55708cc2009-04-30 08:06:49 +0000979 else:
Georg Brandl489cb4f2009-07-11 10:08:49 +0000980 dbm_order = "ndbm:gdbm:bdb".split(":")
Matthias Klose55708cc2009-04-30 08:06:49 +0000981 dbmext = None
982 for cand in dbm_order:
983 if cand == "ndbm":
984 if find_file("ndbm.h", inc_dirs, []) is not None:
985 # Some systems have -lndbm, others don't
Tarek Ziadédd07ebb2009-07-06 13:52:17 +0000986 if self.compiler_obj.find_library_file(lib_dirs,
987 'ndbm'):
Matthias Klose55708cc2009-04-30 08:06:49 +0000988 ndbm_libs = ['ndbm']
989 else:
990 ndbm_libs = []
991 print("building dbm using ndbm")
992 dbmext = Extension('_dbm', ['_dbmmodule.c'],
993 define_macros=[
994 ('HAVE_NDBM_H',None),
995 ],
996 libraries=ndbm_libs)
997 break
998
999 elif cand == "gdbm":
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001000 if self.compiler_obj.find_library_file(lib_dirs, 'gdbm'):
Matthias Klose55708cc2009-04-30 08:06:49 +00001001 gdbm_libs = ['gdbm']
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001002 if self.compiler_obj.find_library_file(lib_dirs,
1003 'gdbm_compat'):
Matthias Klose55708cc2009-04-30 08:06:49 +00001004 gdbm_libs.append('gdbm_compat')
1005 if find_file("gdbm/ndbm.h", inc_dirs, []) is not None:
1006 print("building dbm using gdbm")
1007 dbmext = Extension(
1008 '_dbm', ['_dbmmodule.c'],
1009 define_macros=[
1010 ('HAVE_GDBM_NDBM_H', None),
1011 ],
1012 libraries = gdbm_libs)
1013 break
1014 if find_file("gdbm-ndbm.h", inc_dirs, []) is not None:
1015 print("building dbm using gdbm")
1016 dbmext = Extension(
1017 '_dbm', ['_dbmmodule.c'],
1018 define_macros=[
1019 ('HAVE_GDBM_DASH_NDBM_H', None),
1020 ],
1021 libraries = gdbm_libs)
1022 break
Georg Brandl489cb4f2009-07-11 10:08:49 +00001023 elif cand == "bdb":
1024 if db_incs is not None:
1025 print("building dbm using bdb")
1026 dbmext = Extension('_dbm', ['_dbmmodule.c'],
1027 library_dirs=dblib_dir,
1028 runtime_library_dirs=dblib_dir,
1029 include_dirs=db_incs,
1030 define_macros=[
1031 ('HAVE_BERKDB_H', None),
1032 ('DB_DBM_HSEARCH', None),
1033 ],
1034 libraries=dblibs)
Matthias Klose55708cc2009-04-30 08:06:49 +00001035 break
1036 if dbmext is not None:
1037 exts.append(dbmext)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001038 else:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00001039 missing.append('_dbm')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001040
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001041 # Anthony Baxter's gdbm module. GNU dbm(3) will require -lgdbm:
Benjamin Petersond78735d2010-01-01 16:04:23 +00001042 if ('gdbm' in dbm_order and
1043 self.compiler_obj.find_library_file(lib_dirs, 'gdbm')):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00001044 exts.append( Extension('_gdbm', ['_gdbmmodule.c'],
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001045 libraries = ['gdbm'] ) )
Guido van Rossumd8faa362007-04-27 19:54:29 +00001046 else:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00001047 missing.append('_gdbm')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001048
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001049 # Unix-only modules
Ronald Oussoren94f25282010-05-05 19:11:21 +00001050 if platform != 'win32':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001051 # Steen Lumholt's termios module
1052 exts.append( Extension('termios', ['termios.c']) )
1053 # Jeremy Hylton's rlimit interface
Antoine Pitrou6103ab12009-10-24 20:11:21 +00001054 exts.append( Extension('resource', ['resource.c']) )
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001055
Andrew M. Kuchlingcf393f32001-02-21 02:38:24 +00001056 # Sun yellow pages. Some systems have the functions in libc.
Benjamin Peterson588009e2009-12-30 03:03:54 +00001057 if (platform not in ['cygwin', 'qnx6'] and
1058 find_file('rpcsvc/yp_prot.h', inc_dirs, []) is not None):
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001059 if (self.compiler_obj.find_library_file(lib_dirs, 'nsl')):
Andrew M. Kuchling6efc6e72001-02-27 20:54:23 +00001060 libs = ['nsl']
1061 else:
1062 libs = []
1063 exts.append( Extension('nis', ['nismodule.c'],
1064 libraries = libs) )
Guido van Rossumd8faa362007-04-27 19:54:29 +00001065 else:
1066 missing.append('nis')
1067 else:
1068 missing.extend(['nis', 'resource', 'termios'])
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001069
Skip Montanaro72092942004-02-07 12:50:19 +00001070 # Curses support, requiring the System V version of curses, often
Fredrik Lundhade711a2001-01-24 08:00:28 +00001071 # provided by the ncurses library.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001072 panel_library = 'panel'
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001073 if (self.compiler_obj.find_library_file(lib_dirs, 'ncursesw')):
Martin v. Löwisa55e55e2006-02-11 15:55:14 +00001074 curses_libs = ['ncursesw']
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001075 # Bug 1464056: If _curses.so links with ncursesw,
1076 # _curses_panel.so must link with panelw.
1077 panel_library = 'panelw'
Martin v. Löwisa55e55e2006-02-11 15:55:14 +00001078 exts.append( Extension('_curses', ['_cursesmodule.c'],
1079 libraries = curses_libs) )
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001080 elif (self.compiler_obj.find_library_file(lib_dirs, 'ncurses')):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001081 curses_libs = ['ncurses']
1082 exts.append( Extension('_curses', ['_cursesmodule.c'],
1083 libraries = curses_libs) )
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001084 elif (self.compiler_obj.find_library_file(lib_dirs, 'curses')
Fred Drake38419c02001-12-06 22:24:47 +00001085 and platform != 'darwin'):
Michael W. Hudson5b109102002-01-23 15:04:41 +00001086 # OSX has an old Berkeley curses, not good enough for
1087 # the _curses module.
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001088 if (self.compiler_obj.find_library_file(lib_dirs, 'terminfo')):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001089 curses_libs = ['curses', 'terminfo']
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001090 elif (self.compiler_obj.find_library_file(lib_dirs, 'termcap')):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001091 curses_libs = ['curses', 'termcap']
Neal Norwitz0b27ff92003-03-31 15:53:49 +00001092 else:
1093 curses_libs = ['curses']
Fredrik Lundhade711a2001-01-24 08:00:28 +00001094
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001095 exts.append( Extension('_curses', ['_cursesmodule.c'],
1096 libraries = curses_libs) )
Guido van Rossumd8faa362007-04-27 19:54:29 +00001097 else:
1098 missing.append('_curses')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001099
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001100 # If the curses module is enabled, check for the panel module
Andrew M. Kuchlinge7ffbb22001-12-06 15:57:16 +00001101 if (module_enabled(exts, '_curses') and
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001102 self.compiler_obj.find_library_file(lib_dirs, panel_library)):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001103 exts.append( Extension('_curses_panel', ['_curses_panel.c'],
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001104 libraries = [panel_library] + curses_libs) )
Guido van Rossumd8faa362007-04-27 19:54:29 +00001105 else:
1106 missing.append('_curses_panel')
Fredrik Lundhade711a2001-01-24 08:00:28 +00001107
Barry Warsaw259b1e12002-08-13 20:09:26 +00001108 # Andrew Kuchling's zlib module. Note that some versions of zlib
1109 # 1.1.3 have security problems. See CERT Advisory CA-2002-07:
1110 # http://www.cert.org/advisories/CA-2002-07.html
1111 #
1112 # zlib 1.1.4 is fixed, but at least one vendor (RedHat) has decided to
1113 # patch its zlib 1.1.3 package instead of upgrading to 1.1.4. For
1114 # now, we still accept 1.1.3, because we think it's difficult to
1115 # exploit this in Python, and we'd rather make it RedHat's problem
1116 # than our problem <wink>.
1117 #
1118 # You can upgrade zlib to version 1.1.4 yourself by going to
1119 # http://www.gzip.org/zlib/
Guido van Rossume6970912001-04-15 15:16:12 +00001120 zlib_inc = find_file('zlib.h', [], inc_dirs)
Christian Heimes1dc54002008-03-24 02:19:29 +00001121 have_zlib = False
Guido van Rossume6970912001-04-15 15:16:12 +00001122 if zlib_inc is not None:
1123 zlib_h = zlib_inc[0] + '/zlib.h'
1124 version = '"0.0.0"'
Barry Warsaw259b1e12002-08-13 20:09:26 +00001125 version_req = '"1.1.3"'
Guido van Rossume6970912001-04-15 15:16:12 +00001126 fp = open(zlib_h)
1127 while 1:
1128 line = fp.readline()
1129 if not line:
1130 break
Guido van Rossum8cdc03d2002-08-06 17:28:30 +00001131 if line.startswith('#define ZLIB_VERSION'):
Guido van Rossume6970912001-04-15 15:16:12 +00001132 version = line.split()[2]
1133 break
1134 if version >= version_req:
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001135 if (self.compiler_obj.find_library_file(lib_dirs, 'z')):
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001136 if sys.platform == "darwin":
1137 zlib_extra_link_args = ('-Wl,-search_paths_first',)
1138 else:
1139 zlib_extra_link_args = ()
Guido van Rossume6970912001-04-15 15:16:12 +00001140 exts.append( Extension('zlib', ['zlibmodule.c'],
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001141 libraries = ['z'],
1142 extra_link_args = zlib_extra_link_args))
Christian Heimes1dc54002008-03-24 02:19:29 +00001143 have_zlib = True
Guido van Rossumd8faa362007-04-27 19:54:29 +00001144 else:
1145 missing.append('zlib')
1146 else:
1147 missing.append('zlib')
1148 else:
1149 missing.append('zlib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001150
Christian Heimes1dc54002008-03-24 02:19:29 +00001151 # Helper module for various ascii-encoders. Uses zlib for an optimized
1152 # crc32 if we have it. Otherwise binascii uses its own.
1153 if have_zlib:
1154 extra_compile_args = ['-DUSE_ZLIB_CRC32']
1155 libraries = ['z']
1156 extra_link_args = zlib_extra_link_args
1157 else:
1158 extra_compile_args = []
1159 libraries = []
1160 extra_link_args = []
1161 exts.append( Extension('binascii', ['binascii.c'],
1162 extra_compile_args = extra_compile_args,
1163 libraries = libraries,
1164 extra_link_args = extra_link_args) )
1165
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001166 # Gustavo Niemeyer's bz2 module.
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001167 if (self.compiler_obj.find_library_file(lib_dirs, 'bz2')):
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001168 if sys.platform == "darwin":
1169 bz2_extra_link_args = ('-Wl,-search_paths_first',)
1170 else:
1171 bz2_extra_link_args = ()
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001172 exts.append( Extension('bz2', ['bz2module.c'],
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001173 libraries = ['bz2'],
1174 extra_link_args = bz2_extra_link_args) )
Guido van Rossumd8faa362007-04-27 19:54:29 +00001175 else:
1176 missing.append('bz2')
Gustavo Niemeyerf8ca8362002-11-05 16:50:05 +00001177
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001178 # Interface to the Expat XML parser
1179 #
Benjamin Petersona28e7022010-01-09 18:53:06 +00001180 # Expat was written by James Clark and is now maintained by a group of
1181 # developers on SourceForge; see www.libexpat.org for more information.
1182 # The pyexpat module was written by Paul Prescod after a prototype by
1183 # Jack Jansen. The Expat source is included in Modules/expat/. Usage
1184 # of a system shared libexpat.so is possible with --with-system-expat
1185 # cofigure option.
Fred Drakefc8341d2002-06-17 17:55:30 +00001186 #
1187 # More information on Expat can be found at www.libexpat.org.
1188 #
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001189 if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
1190 expat_inc = []
1191 define_macros = []
1192 expat_lib = ['expat']
1193 expat_sources = []
1194 else:
1195 expat_inc = [os.path.join(os.getcwd(), srcdir, 'Modules', 'expat')]
1196 define_macros = [
1197 ('HAVE_EXPAT_CONFIG_H', '1'),
1198 ]
1199 expat_lib = []
1200 expat_sources = ['expat/xmlparse.c',
1201 'expat/xmlrole.c',
1202 'expat/xmltok.c']
Thomas Wouters477c8d52006-05-27 19:21:47 +00001203
Fred Drake2d59a492003-10-21 15:41:15 +00001204 exts.append(Extension('pyexpat',
1205 define_macros = define_macros,
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001206 include_dirs = expat_inc,
1207 libraries = expat_lib,
1208 sources = ['pyexpat.c'] + expat_sources
Fred Drake2d59a492003-10-21 15:41:15 +00001209 ))
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001210
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001211 # Fredrik Lundh's cElementTree module. Note that this also
1212 # uses expat (via the CAPI hook in pyexpat).
1213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214 if os.path.isfile(os.path.join(srcdir, 'Modules', '_elementtree.c')):
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001215 define_macros.append(('USE_PYEXPAT_CAPI', None))
1216 exts.append(Extension('_elementtree',
1217 define_macros = define_macros,
Benjamin Petersonb2d90462009-12-31 03:23:10 +00001218 include_dirs = expat_inc,
1219 libraries = expat_lib,
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001220 sources = ['_elementtree.c'],
1221 ))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001222 else:
1223 missing.append('_elementtree')
Fredrik Lundh4c86ec62005-12-14 18:46:16 +00001224
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001225 # Hye-Shik Chang's CJKCodecs modules.
Walter Dörwalde9eaab42007-05-22 16:02:13 +00001226 exts.append(Extension('_multibytecodec',
1227 ['cjkcodecs/multibytecodec.c']))
1228 for loc in ('kr', 'jp', 'cn', 'tw', 'hk', 'iso2022'):
1229 exts.append(Extension('_codecs_%s' % loc,
1230 ['cjkcodecs/_codecs_%s.c' % loc]))
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001231
Thomas Hellercf567c12006-03-08 19:51:58 +00001232 # Thomas Heller's _ctypes module
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233 self.detect_ctypes(inc_dirs, lib_dirs)
Thomas Hellercf567c12006-03-08 19:51:58 +00001234
Benjamin Petersone711caf2008-06-11 16:44:04 +00001235 # Richard Oudkerk's multiprocessing module
1236 if platform == 'win32': # Windows
1237 macros = dict()
1238 libraries = ['ws2_32']
1239
1240 elif platform == 'darwin': # Mac OSX
Benjamin Peterson965ce872009-04-05 21:24:58 +00001241 macros = dict()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001242 libraries = []
1243
1244 elif platform == 'cygwin': # Cygwin
Benjamin Peterson965ce872009-04-05 21:24:58 +00001245 macros = dict()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001246 libraries = []
Benjamin Peterson41181742008-07-02 20:22:54 +00001247
Martin v. Löwisb37509b2008-11-04 20:45:29 +00001248 elif platform in ('freebsd4', 'freebsd5', 'freebsd6', 'freebsd7', 'freebsd8'):
Benjamin Peterson41181742008-07-02 20:22:54 +00001249 # FreeBSD's P1003.1b semaphore support is very experimental
1250 # and has many known problems. (as of June 2008)
Benjamin Peterson965ce872009-04-05 21:24:58 +00001251 macros = dict()
Benjamin Peterson41181742008-07-02 20:22:54 +00001252 libraries = []
1253
Benjamin Petersone5384b02008-10-04 22:00:42 +00001254 elif platform.startswith('openbsd'):
Benjamin Peterson965ce872009-04-05 21:24:58 +00001255 macros = dict()
Benjamin Petersone5384b02008-10-04 22:00:42 +00001256 libraries = []
1257
Jesse Noller32d68c22009-03-31 18:48:42 +00001258 elif platform.startswith('netbsd'):
Benjamin Peterson965ce872009-04-05 21:24:58 +00001259 macros = dict()
Jesse Noller32d68c22009-03-31 18:48:42 +00001260 libraries = []
1261
Benjamin Petersone711caf2008-06-11 16:44:04 +00001262 else: # Linux and other unices
Benjamin Peterson965ce872009-04-05 21:24:58 +00001263 macros = dict()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001264 libraries = ['rt']
1265
1266 if platform == 'win32':
1267 multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1268 '_multiprocessing/semaphore.c',
1269 '_multiprocessing/pipe_connection.c',
1270 '_multiprocessing/socket_connection.c',
1271 '_multiprocessing/win32_functions.c'
1272 ]
1273
1274 else:
1275 multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1276 '_multiprocessing/socket_connection.c'
1277 ]
Mark Dickinsona614f042009-11-28 12:48:43 +00001278 if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
1279 sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
Benjamin Petersone711caf2008-06-11 16:44:04 +00001280 multiprocessing_srcs.append('_multiprocessing/semaphore.c')
1281
Jesse Noller6fd47e22009-01-23 14:09:08 +00001282 if sysconfig.get_config_var('WITH_THREAD'):
1283 exts.append ( Extension('_multiprocessing', multiprocessing_srcs,
1284 define_macros=list(macros.items()),
1285 include_dirs=["Modules/_multiprocessing"]))
1286 else:
1287 missing.append('_multiprocessing')
Benjamin Petersone711caf2008-06-11 16:44:04 +00001288 # End multiprocessing
Guido van Rossuma9e20242007-03-08 00:43:48 +00001289
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001290 # Platform-specific libraries
Matthias Klose4c4b0782010-04-21 22:21:03 +00001291 if (platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
1292 'freebsd7', 'freebsd8')
1293 or platform.startswith("gnukfreebsd")):
Guido van Rossum0c016a92003-02-13 16:12:21 +00001294 exts.append( Extension('ossaudiodev', ['ossaudiodev.c']) )
Guido van Rossumd8faa362007-04-27 19:54:29 +00001295 else:
1296 missing.append('ossaudiodev')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001297
Benjamin Petersonebacd262008-05-29 21:09:51 +00001298 if sys.platform == 'darwin':
1299 exts.append(
1300 Extension('_gestalt', ['_gestalt.c'],
1301 extra_link_args=['-framework', 'Carbon'])
1302 )
Ronald Oussoren84151202010-04-18 20:46:11 +00001303 exts.append(
1304 Extension('_scproxy', ['_scproxy.c'],
1305 extra_link_args=[
1306 '-framework', 'SystemConfiguration',
1307 '-framework', 'CoreFoundation',
1308 ]))
Benjamin Petersonebacd262008-05-29 21:09:51 +00001309
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001310 self.extensions.extend(exts)
1311
1312 # Call the method for detecting whether _tkinter can be compiled
1313 self.detect_tkinter(inc_dirs, lib_dirs)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001314
Guido van Rossumd8faa362007-04-27 19:54:29 +00001315 if '_tkinter' not in [e.name for e in self.extensions]:
1316 missing.append('_tkinter')
1317
1318 return missing
1319
Jack Jansen0b06be72002-06-21 14:48:38 +00001320 def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
1321 # The _tkinter module, using frameworks. Since frameworks are quite
1322 # different the UNIX search logic is not sharable.
1323 from os.path import join, exists
1324 framework_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00001325 '/Library/Frameworks',
Ronald Oussoren5f734f12009-03-04 21:32:48 +00001326 '/System/Library/Frameworks/',
Jack Jansen0b06be72002-06-21 14:48:38 +00001327 join(os.getenv('HOME'), '/Library/Frameworks')
1328 ]
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001329
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001330 sysroot = macosx_sdk_root()
1331
Skip Montanaro0174ddd2005-12-30 05:01:26 +00001332 # Find the directory that contains the Tcl.framework and Tk.framework
Jack Jansen0b06be72002-06-21 14:48:38 +00001333 # bundles.
1334 # XXX distutils should support -F!
1335 for F in framework_dirs:
Tim Peters2c60f7a2003-01-29 03:49:43 +00001336 # both Tcl.framework and Tk.framework should be present
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001337
1338
Jack Jansen0b06be72002-06-21 14:48:38 +00001339 for fw in 'Tcl', 'Tk':
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001340 if is_macosx_sdk_path(F):
1341 if not exists(join(sysroot, F[1:], fw + '.framework')):
1342 break
1343 else:
1344 if not exists(join(F, fw + '.framework')):
1345 break
Jack Jansen0b06be72002-06-21 14:48:38 +00001346 else:
1347 # ok, F is now directory with both frameworks. Continure
1348 # building
1349 break
1350 else:
1351 # Tk and Tcl frameworks not found. Normal "unix" tkinter search
1352 # will now resume.
1353 return 0
Tim Peters2c60f7a2003-01-29 03:49:43 +00001354
Jack Jansen0b06be72002-06-21 14:48:38 +00001355 # For 8.4a2, we must add -I options that point inside the Tcl and Tk
1356 # frameworks. In later release we should hopefully be able to pass
Tim Peters2c60f7a2003-01-29 03:49:43 +00001357 # the -F option to gcc, which specifies a framework lookup path.
Jack Jansen0b06be72002-06-21 14:48:38 +00001358 #
1359 include_dirs = [
Tim Peters2c60f7a2003-01-29 03:49:43 +00001360 join(F, fw + '.framework', H)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001361 for fw in ('Tcl', 'Tk')
1362 for H in ('Headers', 'Versions/Current/PrivateHeaders')
Jack Jansen0b06be72002-06-21 14:48:38 +00001363 ]
1364
Tim Peters2c60f7a2003-01-29 03:49:43 +00001365 # For 8.4a2, the X11 headers are not included. Rather than include a
Jack Jansen0b06be72002-06-21 14:48:38 +00001366 # complicated search, this is a hard-coded path. It could bail out
1367 # if X11 libs are not found...
1368 include_dirs.append('/usr/X11R6/include')
1369 frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
1370
Georg Brandlfcaf9102008-07-16 02:17:56 +00001371 # All existing framework builds of Tcl/Tk don't support 64-bit
1372 # architectures.
1373 cflags = sysconfig.get_config_vars('CFLAGS')[0]
1374 archs = re.findall('-arch\s+(\w+)', cflags)
Georg Brandlfcaf9102008-07-16 02:17:56 +00001375
Ronald Oussorend097efe2009-09-15 19:07:58 +00001376 tmpfile = os.path.join(self.build_temp, 'tk.arch')
1377 if not os.path.exists(self.build_temp):
1378 os.makedirs(self.build_temp)
1379
1380 # Note: cannot use os.popen or subprocess here, that
1381 # requires extensions that are not available here.
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001382 if is_macosx_sdk_path(F):
1383 os.system("file %s/Tk.framework/Tk | grep 'for architecture' > %s"%(os.path.join(sysroot, F[1:]), tmpfile))
1384 else:
1385 os.system("file %s/Tk.framework/Tk | grep 'for architecture' > %s"%(F, tmpfile))
Ronald Oussorend097efe2009-09-15 19:07:58 +00001386 fp = open(tmpfile)
Ronald Oussoren2c12ab12010-06-03 14:42:25 +00001387
Ronald Oussorend097efe2009-09-15 19:07:58 +00001388 detected_archs = []
1389 for ln in fp:
1390 a = ln.split()[-1]
1391 if a in archs:
1392 detected_archs.append(ln.split()[-1])
1393 fp.close()
1394 os.unlink(tmpfile)
1395
1396 for a in detected_archs:
1397 frameworks.append('-arch')
1398 frameworks.append(a)
Georg Brandlfcaf9102008-07-16 02:17:56 +00001399
Jack Jansen0b06be72002-06-21 14:48:38 +00001400 ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1401 define_macros=[('WITH_APPINIT', 1)],
1402 include_dirs = include_dirs,
1403 libraries = [],
Georg Brandlfcaf9102008-07-16 02:17:56 +00001404 extra_compile_args = frameworks[2:],
Jack Jansen0b06be72002-06-21 14:48:38 +00001405 extra_link_args = frameworks,
1406 )
1407 self.extensions.append(ext)
1408 return 1
1409
Tim Peters2c60f7a2003-01-29 03:49:43 +00001410
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001411 def detect_tkinter(self, inc_dirs, lib_dirs):
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001412 # The _tkinter module.
Michael W. Hudson5b109102002-01-23 15:04:41 +00001413
Jack Jansen0b06be72002-06-21 14:48:38 +00001414 # Rather than complicate the code below, detecting and building
1415 # AquaTk is a separate method. Only one Tkinter will be built on
1416 # Darwin - either AquaTk, if it is found, or X11 based Tk.
1417 platform = self.get_platform()
Skip Montanaro0174ddd2005-12-30 05:01:26 +00001418 if (platform == 'darwin' and
1419 self.detect_tkinter_darwin(inc_dirs, lib_dirs)):
Tim Peters2c60f7a2003-01-29 03:49:43 +00001420 return
Jack Jansen0b06be72002-06-21 14:48:38 +00001421
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001422 # Assume we haven't found any of the libraries or include files
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00001423 # The versions with dots are used on Unix, and the versions without
1424 # dots on Windows, for detection by cygwin.
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001425 tcllib = tklib = tcl_includes = tk_includes = None
Guilherme Polo5d377bd2009-08-16 14:44:14 +00001426 for version in ['8.6', '86', '8.5', '85', '8.4', '84', '8.3', '83',
1427 '8.2', '82', '8.1', '81', '8.0', '80']:
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001428 tklib = self.compiler_obj.find_library_file(lib_dirs,
1429 'tk' + version)
1430 tcllib = self.compiler_obj.find_library_file(lib_dirs,
1431 'tcl' + version)
Michael W. Hudson5b109102002-01-23 15:04:41 +00001432 if tklib and tcllib:
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001433 # Exit the loop when we've found the Tcl/Tk libraries
1434 break
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001435
Fredrik Lundhade711a2001-01-24 08:00:28 +00001436 # Now check for the header files
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001437 if tklib and tcllib:
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00001438 # Check for the include files on Debian and {Free,Open}BSD, where
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001439 # they're put in /usr/include/{tcl,tk}X.Y
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00001440 dotversion = version
1441 if '.' not in dotversion and "bsd" in sys.platform.lower():
1442 # OpenBSD and FreeBSD use Tcl/Tk library names like libtcl83.a,
1443 # but the include subdirs are named like .../include/tcl8.3.
1444 dotversion = dotversion[:-1] + '.' + dotversion[-1]
1445 tcl_include_sub = []
1446 tk_include_sub = []
1447 for dir in inc_dirs:
1448 tcl_include_sub += [dir + os.sep + "tcl" + dotversion]
1449 tk_include_sub += [dir + os.sep + "tk" + dotversion]
1450 tk_include_sub += tcl_include_sub
1451 tcl_includes = find_file('tcl.h', inc_dirs, tcl_include_sub)
1452 tk_includes = find_file('tk.h', inc_dirs, tk_include_sub)
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001453
Martin v. Löwise86a59a2003-05-03 08:45:51 +00001454 if (tcllib is None or tklib is None or
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001455 tcl_includes is None or tk_includes is None):
Andrew M. Kuchling3c0aa7e2004-03-21 18:57:35 +00001456 self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001457 return
Fredrik Lundhade711a2001-01-24 08:00:28 +00001458
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001459 # OK... everything seems to be present for Tcl/Tk.
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001460
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001461 include_dirs = [] ; libs = [] ; defs = [] ; added_lib_dirs = []
1462 for dir in tcl_includes + tk_includes:
1463 if dir not in include_dirs:
1464 include_dirs.append(dir)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001465
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001466 # Check for various platform-specific directories
Andrew M. Kuchling34febf52001-01-24 03:31:07 +00001467 if platform == 'sunos5':
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001468 include_dirs.append('/usr/openwin/include')
1469 added_lib_dirs.append('/usr/openwin/lib')
1470 elif os.path.exists('/usr/X11R6/include'):
1471 include_dirs.append('/usr/X11R6/include')
Martin v. Löwisfba73692004-11-13 11:13:35 +00001472 added_lib_dirs.append('/usr/X11R6/lib64')
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001473 added_lib_dirs.append('/usr/X11R6/lib')
1474 elif os.path.exists('/usr/X11R5/include'):
1475 include_dirs.append('/usr/X11R5/include')
1476 added_lib_dirs.append('/usr/X11R5/lib')
1477 else:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001478 # Assume default location for X11
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001479 include_dirs.append('/usr/X11/include')
1480 added_lib_dirs.append('/usr/X11/lib')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001481
Jason Tishler9181c942003-02-05 15:16:17 +00001482 # If Cygwin, then verify that X is installed before proceeding
1483 if platform == 'cygwin':
1484 x11_inc = find_file('X11/Xlib.h', [], include_dirs)
1485 if x11_inc is None:
1486 return
1487
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001488 # Check for BLT extension
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001489 if self.compiler_obj.find_library_file(lib_dirs + added_lib_dirs,
1490 'BLT8.0'):
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001491 defs.append( ('WITH_BLT', 1) )
1492 libs.append('BLT8.0')
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001493 elif self.compiler_obj.find_library_file(lib_dirs + added_lib_dirs,
1494 'BLT'):
Martin v. Löwis427a2902002-12-12 20:23:38 +00001495 defs.append( ('WITH_BLT', 1) )
1496 libs.append('BLT')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001497
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001498 # Add the Tcl/Tk libraries
Jason Tishlercccac1a2003-02-05 15:06:46 +00001499 libs.append('tk'+ version)
1500 libs.append('tcl'+ version)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001501
Andrew M. Kuchling34febf52001-01-24 03:31:07 +00001502 if platform in ['aix3', 'aix4']:
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001503 libs.append('ld')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001504
Martin v. Löwis3db5b8c2001-07-24 06:54:01 +00001505 # Finally, link with the X11 libraries (not appropriate on cygwin)
1506 if platform != "cygwin":
1507 libs.append('X11')
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001508
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001509 ext = Extension('_tkinter', ['_tkinter.c', 'tkappinit.c'],
1510 define_macros=[('WITH_APPINIT', 1)] + defs,
1511 include_dirs = include_dirs,
1512 libraries = libs,
1513 library_dirs = added_lib_dirs,
1514 )
1515 self.extensions.append(ext)
Fredrik Lundhade711a2001-01-24 08:00:28 +00001516
Guido van Rossum6c7438e2003-02-11 20:05:50 +00001517## # Uncomment these lines if you want to play with xxmodule.c
1518## ext = Extension('xx', ['xxmodule.c'])
1519## self.extensions.append(ext)
1520
Andrew M. Kuchlingfbe73762001-01-18 18:44:20 +00001521 # XXX handle these, but how to detect?
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001522 # *** Uncomment and edit for PIL (TkImaging) extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001523 # -DWITH_PIL -I../Extensions/Imaging/libImaging tkImaging.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001524 # *** Uncomment and edit for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001525 # -DWITH_TOGL togl.c \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001526 # *** Uncomment these for TOGL extension only:
Fredrik Lundhade711a2001-01-24 08:00:28 +00001527 # -lGL -lGLU -lXext -lXmu \
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001528
Christian Heimes78644762008-03-04 23:39:23 +00001529 def configure_ctypes_darwin(self, ext):
1530 # Darwin (OS X) uses preconfigured files, in
1531 # the Modules/_ctypes/libffi_osx directory.
Neil Schemenauer014bf282009-02-05 16:35:45 +00001532 srcdir = sysconfig.get_config_var('srcdir')
Christian Heimes78644762008-03-04 23:39:23 +00001533 ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
1534 '_ctypes', 'libffi_osx'))
1535 sources = [os.path.join(ffi_srcdir, p)
1536 for p in ['ffi.c',
Georg Brandlfcaf9102008-07-16 02:17:56 +00001537 'x86/darwin64.S',
Christian Heimes78644762008-03-04 23:39:23 +00001538 'x86/x86-darwin.S',
1539 'x86/x86-ffi_darwin.c',
1540 'x86/x86-ffi64.c',
1541 'powerpc/ppc-darwin.S',
1542 'powerpc/ppc-darwin_closure.S',
1543 'powerpc/ppc-ffi_darwin.c',
1544 'powerpc/ppc64-darwin_closure.S',
1545 ]]
1546
1547 # Add .S (preprocessed assembly) to C compiler source extensions.
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001548 self.compiler_obj.src_extensions.append('.S')
Christian Heimes78644762008-03-04 23:39:23 +00001549
1550 include_dirs = [os.path.join(ffi_srcdir, 'include'),
1551 os.path.join(ffi_srcdir, 'powerpc')]
1552 ext.include_dirs.extend(include_dirs)
1553 ext.sources.extend(sources)
1554 return True
1555
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556 def configure_ctypes(self, ext):
1557 if not self.use_system_libffi:
Christian Heimes78644762008-03-04 23:39:23 +00001558 if sys.platform == 'darwin':
1559 return self.configure_ctypes_darwin(ext)
1560
Neil Schemenauer014bf282009-02-05 16:35:45 +00001561 srcdir = sysconfig.get_config_var('srcdir')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001562 ffi_builddir = os.path.join(self.build_temp, 'libffi')
1563 ffi_srcdir = os.path.abspath(os.path.join(srcdir, 'Modules',
1564 '_ctypes', 'libffi'))
1565 ffi_configfile = os.path.join(ffi_builddir, 'fficonfig.py')
Thomas Hellercf567c12006-03-08 19:51:58 +00001566
Thomas Wouters477c8d52006-05-27 19:21:47 +00001567 from distutils.dep_util import newer_group
1568
1569 config_sources = [os.path.join(ffi_srcdir, fname)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001570 for fname in os.listdir(ffi_srcdir)
1571 if os.path.isfile(os.path.join(ffi_srcdir, fname))]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001572 if self.force or newer_group(config_sources,
1573 ffi_configfile):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001574 from distutils.dir_util import mkpath
1575 mkpath(ffi_builddir)
1576 config_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00001577
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001578 # Pass empty CFLAGS because we'll just append the resulting
1579 # CFLAGS to Python's; -g or -O2 is to be avoided.
1580 cmd = "cd %s && env CFLAGS='' '%s/configure' %s" \
1581 % (ffi_builddir, ffi_srcdir, " ".join(config_args))
Thomas Hellercf567c12006-03-08 19:51:58 +00001582
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001583 res = os.system(cmd)
1584 if res or not os.path.exists(ffi_configfile):
Guido van Rossum452bf512007-02-09 05:32:43 +00001585 print("Failed to configure _ctypes module")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001586 return False
Thomas Hellercf567c12006-03-08 19:51:58 +00001587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001588 fficonfig = {}
Antoine Pitrou72f4d642010-01-13 12:04:20 +00001589 with open(ffi_configfile) as f:
1590 exec(f.read(), globals(), fficonfig)
Thomas Hellercf567c12006-03-08 19:51:58 +00001591
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001592 # Add .S (preprocessed assembly) to C compiler source extensions.
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001593 self.compiler_obj.src_extensions.append('.S')
Thomas Hellercf567c12006-03-08 19:51:58 +00001594
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001595 include_dirs = [os.path.join(ffi_builddir, 'include'),
Antoine Pitrou72f4d642010-01-13 12:04:20 +00001596 ffi_builddir,
1597 os.path.join(ffi_srcdir, 'src')]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001598 extra_compile_args = fficonfig['ffi_cflags'].split()
1599
Antoine Pitrou72f4d642010-01-13 12:04:20 +00001600 ext.sources.extend(os.path.join(ffi_srcdir, f) for f in
1601 fficonfig['ffi_sources'])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001602 ext.include_dirs.extend(include_dirs)
1603 ext.extra_compile_args.extend(extra_compile_args)
1604 return True
1605
1606 def detect_ctypes(self, inc_dirs, lib_dirs):
1607 self.use_system_libffi = False
1608 include_dirs = []
1609 extra_compile_args = []
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001610 extra_link_args = []
Thomas Hellercf567c12006-03-08 19:51:58 +00001611 sources = ['_ctypes/_ctypes.c',
1612 '_ctypes/callbacks.c',
1613 '_ctypes/callproc.c',
1614 '_ctypes/stgdict.c',
1615 '_ctypes/cfield.c',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616 '_ctypes/malloc_closure.c']
Thomas Hellercf567c12006-03-08 19:51:58 +00001617 depends = ['_ctypes/ctypes.h']
1618
1619 if sys.platform == 'darwin':
1620 sources.append('_ctypes/darwin/dlfcn_simple.c')
Christian Heimes78644762008-03-04 23:39:23 +00001621 extra_compile_args.append('-DMACOSX')
Thomas Hellercf567c12006-03-08 19:51:58 +00001622 include_dirs.append('_ctypes/darwin')
1623# XXX Is this still needed?
1624## extra_link_args.extend(['-read_only_relocs', 'warning'])
1625
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001626 elif sys.platform == 'sunos5':
1627 # XXX This shouldn't be necessary; it appears that some
1628 # of the assembler code is non-PIC (i.e. it has relocations
1629 # when it shouldn't. The proper fix would be to rewrite
1630 # the assembler code to be PIC.
1631 # This only works with GCC; the Sun compiler likely refuses
1632 # this option. If you want to compile ctypes with the Sun
1633 # compiler, please research a proper solution, instead of
1634 # finding some -z option for the Sun compiler.
1635 extra_link_args.append('-mimpure-text')
1636
Thomas Heller9af0cba2008-06-06 09:11:46 +00001637 elif sys.platform.startswith('hp-ux'):
Thomas Heller3eaaeb42008-05-23 17:26:46 +00001638 extra_link_args.append('-fPIC')
1639
Thomas Hellercf567c12006-03-08 19:51:58 +00001640 ext = Extension('_ctypes',
1641 include_dirs=include_dirs,
1642 extra_compile_args=extra_compile_args,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001643 extra_link_args=extra_link_args,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001644 libraries=[],
Thomas Hellercf567c12006-03-08 19:51:58 +00001645 sources=sources,
1646 depends=depends)
1647 ext_test = Extension('_ctypes_test',
1648 sources=['_ctypes/_ctypes_test.c'])
1649 self.extensions.extend([ext, ext_test])
1650
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001651 if not '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS"):
1652 return
1653
Christian Heimes78644762008-03-04 23:39:23 +00001654 if sys.platform == 'darwin':
1655 # OS X 10.5 comes with libffi.dylib; the include files are
1656 # in /usr/include/ffi
1657 inc_dirs.append('/usr/include/ffi')
1658
Benjamin Petersond78735d2010-01-01 16:04:23 +00001659 ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
Matthias Klose5a204fe2010-04-21 21:47:45 +00001660 if not ffi_inc or ffi_inc[0] == '':
Benjamin Petersond78735d2010-01-01 16:04:23 +00001661 ffi_inc = find_file('ffi.h', [], inc_dirs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001662 if ffi_inc is not None:
1663 ffi_h = ffi_inc[0] + '/ffi.h'
1664 fp = open(ffi_h)
1665 while 1:
1666 line = fp.readline()
1667 if not line:
1668 ffi_inc = None
1669 break
1670 if line.startswith('#define LIBFFI_H'):
1671 break
1672 ffi_lib = None
1673 if ffi_inc is not None:
1674 for lib_name in ('ffi_convenience', 'ffi_pic', 'ffi'):
Tarek Ziadédd07ebb2009-07-06 13:52:17 +00001675 if (self.compiler_obj.find_library_file(lib_dirs, lib_name)):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676 ffi_lib = lib_name
1677 break
1678
1679 if ffi_inc and ffi_lib:
1680 ext.include_dirs.extend(ffi_inc)
1681 ext.libraries.append(ffi_lib)
1682 self.use_system_libffi = True
1683
1684
Andrew M. Kuchlingf52d27e2001-05-21 20:29:27 +00001685class PyBuildInstall(install):
1686 # Suppress the warning about installation into the lib_dynload
1687 # directory, which is not in sys.path when running Python during
1688 # installation:
1689 def initialize_options (self):
1690 install.initialize_options(self)
1691 self.warn_dir=0
Michael W. Hudson5b109102002-01-23 15:04:41 +00001692
Michael W. Hudson529a5052002-12-17 16:47:17 +00001693class PyBuildInstallLib(install_lib):
1694 # Do exactly what install_lib does but make sure correct access modes get
1695 # set on installed directories and files. All installed files with get
1696 # mode 644 unless they are a shared library in which case they will get
1697 # mode 755. All installed directories will get mode 755.
1698
1699 so_ext = sysconfig.get_config_var("SO")
1700
1701 def install(self):
1702 outfiles = install_lib.install(self)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001703 self.set_file_modes(outfiles, 0o644, 0o755)
1704 self.set_dir_modes(self.install_dir, 0o755)
Michael W. Hudson529a5052002-12-17 16:47:17 +00001705 return outfiles
1706
1707 def set_file_modes(self, files, defaultMode, sharedLibMode):
1708 if not self.is_chmod_supported(): return
1709 if not files: return
1710
1711 for filename in files:
1712 if os.path.islink(filename): continue
1713 mode = defaultMode
1714 if filename.endswith(self.so_ext): mode = sharedLibMode
1715 log.info("changing mode of %s to %o", filename, mode)
1716 if not self.dry_run: os.chmod(filename, mode)
1717
1718 def set_dir_modes(self, dirname, mode):
1719 if not self.is_chmod_supported(): return
Amaury Forgeot d'Arc321e5332009-07-02 23:08:45 +00001720 for dirpath, dirnames, fnames in os.walk(dirname):
1721 if os.path.islink(dirpath):
1722 continue
1723 log.info("changing mode of %s to %o", dirpath, mode)
1724 if not self.dry_run: os.chmod(dirpath, mode)
Michael W. Hudson529a5052002-12-17 16:47:17 +00001725
1726 def is_chmod_supported(self):
1727 return hasattr(os, 'chmod')
1728
Guido van Rossum14ee89c2003-02-20 02:52:04 +00001729SUMMARY = """
1730Python is an interpreted, interactive, object-oriented programming
1731language. It is often compared to Tcl, Perl, Scheme or Java.
1732
1733Python combines remarkable power with very clear syntax. It has
1734modules, classes, exceptions, very high level dynamic data types, and
1735dynamic typing. There are interfaces to many system calls and
1736libraries, as well as to various windowing systems (X11, Motif, Tk,
1737Mac, MFC). New built-in modules are easily written in C or C++. Python
1738is also usable as an extension language for applications that need a
1739programmable interface.
1740
1741The Python implementation is portable: it runs on many brands of UNIX,
1742on Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't
1743listed here, it may still be supported, if there's a C compiler for
1744it. Ask around on comp.lang.python -- or just try compiling Python
1745yourself.
1746"""
1747
1748CLASSIFIERS = """
Guido van Rossum14ee89c2003-02-20 02:52:04 +00001749Development Status :: 6 - Mature
1750License :: OSI Approved :: Python Software Foundation License
1751Natural Language :: English
1752Programming Language :: C
1753Programming Language :: Python
1754Topic :: Software Development
1755"""
1756
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001757def main():
Andrew M. Kuchling62686692001-05-21 20:48:09 +00001758 # turn off warnings when deprecated modules are imported
1759 import warnings
1760 warnings.filterwarnings("ignore",category=DeprecationWarning)
Guido van Rossum14ee89c2003-02-20 02:52:04 +00001761 setup(# PyPI Metadata (PEP 301)
1762 name = "Python",
1763 version = sys.version.split()[0],
1764 url = "http://www.python.org/%s" % sys.version[:3],
1765 maintainer = "Guido van Rossum and the Python community",
1766 maintainer_email = "python-dev@python.org",
1767 description = "A high-level object-oriented programming language",
1768 long_description = SUMMARY.strip(),
1769 license = "PSF license",
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001770 classifiers = [x for x in CLASSIFIERS.split("\n") if x],
Guido van Rossum14ee89c2003-02-20 02:52:04 +00001771 platforms = ["Many"],
1772
1773 # Build info
Michael W. Hudson529a5052002-12-17 16:47:17 +00001774 cmdclass = {'build_ext':PyBuildExt, 'install':PyBuildInstall,
1775 'install_lib':PyBuildInstallLib},
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001776 # The struct module is defined here, because build_ext won't be
1777 # called unless there's at least one extension module defined.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001778 ext_modules=[Extension('_struct', ['_struct.c'])],
Andrew M. Kuchlingaece4272001-02-28 20:56:49 +00001779
Benjamin Petersondfea1922009-05-23 17:13:14 +00001780 scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
1781 "Tools/scripts/2to3"]
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001782 )
Fredrik Lundhade711a2001-01-24 08:00:28 +00001783
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001784# --install-platlib
1785if __name__ == '__main__':
Andrew M. Kuchling00e0f212001-01-17 15:23:23 +00001786 main()