setup.py 88 KB

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