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

297 lines
12 KiB

  1. from distutils.command.build_ext import build_ext as _du_build_ext
  2. from distutils.file_util import copy_file
  3. from distutils.ccompiler import new_compiler
  4. from distutils.sysconfig import customize_compiler
  5. from distutils.errors import DistutilsError
  6. from distutils import log
  7. import os
  8. import sys
  9. import itertools
  10. from setuptools.extension import Library
  11. try:
  12. # Attempt to use Cython for building extensions, if available
  13. from Cython.Distutils.build_ext import build_ext as _build_ext
  14. except ImportError:
  15. _build_ext = _du_build_ext
  16. try:
  17. # Python 2.7 or >=3.2
  18. from sysconfig import _CONFIG_VARS
  19. except ImportError:
  20. from distutils.sysconfig import get_config_var
  21. get_config_var("LDSHARED") # make sure _config_vars is initialized
  22. del get_config_var
  23. from distutils.sysconfig import _config_vars as _CONFIG_VARS
  24. have_rtld = False
  25. use_stubs = False
  26. libtype = 'shared'
  27. if sys.platform == "darwin":
  28. use_stubs = True
  29. elif os.name != 'nt':
  30. try:
  31. import dl
  32. use_stubs = have_rtld = hasattr(dl, 'RTLD_NOW')
  33. except ImportError:
  34. pass
  35. if_dl = lambda s: s if have_rtld else ''
  36. class build_ext(_build_ext):
  37. def run(self):
  38. """Build extensions in build directory, then copy if --inplace"""
  39. old_inplace, self.inplace = self.inplace, 0
  40. _build_ext.run(self)
  41. self.inplace = old_inplace
  42. if old_inplace:
  43. self.copy_extensions_to_source()
  44. def copy_extensions_to_source(self):
  45. build_py = self.get_finalized_command('build_py')
  46. for ext in self.extensions:
  47. fullname = self.get_ext_fullname(ext.name)
  48. filename = self.get_ext_filename(fullname)
  49. modpath = fullname.split('.')
  50. package = '.'.join(modpath[:-1])
  51. package_dir = build_py.get_package_dir(package)
  52. dest_filename = os.path.join(package_dir,
  53. os.path.basename(filename))
  54. src_filename = os.path.join(self.build_lib, filename)
  55. # Always copy, even if source is older than destination, to ensure
  56. # that the right extensions for the current Python/platform are
  57. # used.
  58. copy_file(
  59. src_filename, dest_filename, verbose=self.verbose,
  60. dry_run=self.dry_run
  61. )
  62. if ext._needs_stub:
  63. self.write_stub(package_dir or os.curdir, ext, True)
  64. def get_ext_filename(self, fullname):
  65. filename = _build_ext.get_ext_filename(self, fullname)
  66. if fullname in self.ext_map:
  67. ext = self.ext_map[fullname]
  68. if isinstance(ext, Library):
  69. fn, ext = os.path.splitext(filename)
  70. return self.shlib_compiler.library_filename(fn, libtype)
  71. elif use_stubs and ext._links_to_dynamic:
  72. d, fn = os.path.split(filename)
  73. return os.path.join(d, 'dl-' + fn)
  74. return filename
  75. def initialize_options(self):
  76. _build_ext.initialize_options(self)
  77. self.shlib_compiler = None
  78. self.shlibs = []
  79. self.ext_map = {}
  80. def finalize_options(self):
  81. _build_ext.finalize_options(self)
  82. self.extensions = self.extensions or []
  83. self.check_extensions_list(self.extensions)
  84. self.shlibs = [ext for ext in self.extensions
  85. if isinstance(ext, Library)]
  86. if self.shlibs:
  87. self.setup_shlib_compiler()
  88. for ext in self.extensions:
  89. ext._full_name = self.get_ext_fullname(ext.name)
  90. for ext in self.extensions:
  91. fullname = ext._full_name
  92. self.ext_map[fullname] = ext
  93. # distutils 3.1 will also ask for module names
  94. # XXX what to do with conflicts?
  95. self.ext_map[fullname.split('.')[-1]] = ext
  96. ltd = self.shlibs and self.links_to_dynamic(ext) or False
  97. ns = ltd and use_stubs and not isinstance(ext, Library)
  98. ext._links_to_dynamic = ltd
  99. ext._needs_stub = ns
  100. filename = ext._file_name = self.get_ext_filename(fullname)
  101. libdir = os.path.dirname(os.path.join(self.build_lib, filename))
  102. if ltd and libdir not in ext.library_dirs:
  103. ext.library_dirs.append(libdir)
  104. if ltd and use_stubs and os.curdir not in ext.runtime_library_dirs:
  105. ext.runtime_library_dirs.append(os.curdir)
  106. def setup_shlib_compiler(self):
  107. compiler = self.shlib_compiler = new_compiler(
  108. compiler=self.compiler, dry_run=self.dry_run, force=self.force
  109. )
  110. if sys.platform == "darwin":
  111. tmp = _CONFIG_VARS.copy()
  112. try:
  113. # XXX Help! I don't have any idea whether these are right...
  114. _CONFIG_VARS['LDSHARED'] = (
  115. "gcc -Wl,-x -dynamiclib -undefined dynamic_lookup")
  116. _CONFIG_VARS['CCSHARED'] = " -dynamiclib"
  117. _CONFIG_VARS['SO'] = ".dylib"
  118. customize_compiler(compiler)
  119. finally:
  120. _CONFIG_VARS.clear()
  121. _CONFIG_VARS.update(tmp)
  122. else:
  123. customize_compiler(compiler)
  124. if self.include_dirs is not None:
  125. compiler.set_include_dirs(self.include_dirs)
  126. if self.define is not None:
  127. # 'define' option is a list of (name,value) tuples
  128. for (name, value) in self.define:
  129. compiler.define_macro(name, value)
  130. if self.undef is not None:
  131. for macro in self.undef:
  132. compiler.undefine_macro(macro)
  133. if self.libraries is not None:
  134. compiler.set_libraries(self.libraries)
  135. if self.library_dirs is not None:
  136. compiler.set_library_dirs(self.library_dirs)
  137. if self.rpath is not None:
  138. compiler.set_runtime_library_dirs(self.rpath)
  139. if self.link_objects is not None:
  140. compiler.set_link_objects(self.link_objects)
  141. # hack so distutils' build_extension() builds a library instead
  142. compiler.link_shared_object = link_shared_object.__get__(compiler)
  143. def get_export_symbols(self, ext):
  144. if isinstance(ext, Library):
  145. return ext.export_symbols
  146. return _build_ext.get_export_symbols(self, ext)
  147. def build_extension(self, ext):
  148. ext._convert_pyx_sources_to_lang()
  149. _compiler = self.compiler
  150. try:
  151. if isinstance(ext, Library):
  152. self.compiler = self.shlib_compiler
  153. _build_ext.build_extension(self, ext)
  154. if ext._needs_stub:
  155. cmd = self.get_finalized_command('build_py').build_lib
  156. self.write_stub(cmd, ext)
  157. finally:
  158. self.compiler = _compiler
  159. def links_to_dynamic(self, ext):
  160. """Return true if 'ext' links to a dynamic lib in the same package"""
  161. # XXX this should check to ensure the lib is actually being built
  162. # XXX as dynamic, and not just using a locally-found version or a
  163. # XXX static-compiled version
  164. libnames = dict.fromkeys([lib._full_name for lib in self.shlibs])
  165. pkg = '.'.join(ext._full_name.split('.')[:-1] + [''])
  166. return any(pkg + libname in libnames for libname in ext.libraries)
  167. def get_outputs(self):
  168. return _build_ext.get_outputs(self) + self.__get_stubs_outputs()
  169. def __get_stubs_outputs(self):
  170. # assemble the base name for each extension that needs a stub
  171. ns_ext_bases = (
  172. os.path.join(self.build_lib, *ext._full_name.split('.'))
  173. for ext in self.extensions
  174. if ext._needs_stub
  175. )
  176. # pair each base with the extension
  177. pairs = itertools.product(ns_ext_bases, self.__get_output_extensions())
  178. return list(base + fnext for base, fnext in pairs)
  179. def __get_output_extensions(self):
  180. yield '.py'
  181. yield '.pyc'
  182. if self.get_finalized_command('build_py').optimize:
  183. yield '.pyo'
  184. def write_stub(self, output_dir, ext, compile=False):
  185. log.info("writing stub loader for %s to %s", ext._full_name,
  186. output_dir)
  187. stub_file = (os.path.join(output_dir, *ext._full_name.split('.')) +
  188. '.py')
  189. if compile and os.path.exists(stub_file):
  190. raise DistutilsError(stub_file + " already exists! Please delete.")
  191. if not self.dry_run:
  192. f = open(stub_file, 'w')
  193. f.write(
  194. '\n'.join([
  195. "def __bootstrap__():",
  196. " global __bootstrap__, __file__, __loader__",
  197. " import sys, os, pkg_resources, imp" + if_dl(", dl"),
  198. " __file__ = pkg_resources.resource_filename"
  199. "(__name__,%r)"
  200. % os.path.basename(ext._file_name),
  201. " del __bootstrap__",
  202. " if '__loader__' in globals():",
  203. " del __loader__",
  204. if_dl(" old_flags = sys.getdlopenflags()"),
  205. " old_dir = os.getcwd()",
  206. " try:",
  207. " os.chdir(os.path.dirname(__file__))",
  208. if_dl(" sys.setdlopenflags(dl.RTLD_NOW)"),
  209. " imp.load_dynamic(__name__,__file__)",
  210. " finally:",
  211. if_dl(" sys.setdlopenflags(old_flags)"),
  212. " os.chdir(old_dir)",
  213. "__bootstrap__()",
  214. "" # terminal \n
  215. ])
  216. )
  217. f.close()
  218. if compile:
  219. from distutils.util import byte_compile
  220. byte_compile([stub_file], optimize=0,
  221. force=True, dry_run=self.dry_run)
  222. optimize = self.get_finalized_command('install_lib').optimize
  223. if optimize > 0:
  224. byte_compile([stub_file], optimize=optimize,
  225. force=True, dry_run=self.dry_run)
  226. if os.path.exists(stub_file) and not self.dry_run:
  227. os.unlink(stub_file)
  228. if use_stubs or os.name == 'nt':
  229. # Build shared libraries
  230. #
  231. def link_shared_object(
  232. self, objects, output_libname, output_dir=None, libraries=None,
  233. library_dirs=None, runtime_library_dirs=None, export_symbols=None,
  234. debug=0, extra_preargs=None, extra_postargs=None, build_temp=None,
  235. target_lang=None):
  236. self.link(
  237. self.SHARED_LIBRARY, objects, output_libname,
  238. output_dir, libraries, library_dirs, runtime_library_dirs,
  239. export_symbols, debug, extra_preargs, extra_postargs,
  240. build_temp, target_lang
  241. )
  242. else:
  243. # Build static libraries everywhere else
  244. libtype = 'static'
  245. def link_shared_object(
  246. self, objects, output_libname, output_dir=None, libraries=None,
  247. library_dirs=None, runtime_library_dirs=None, export_symbols=None,
  248. debug=0, extra_preargs=None, extra_postargs=None, build_temp=None,
  249. target_lang=None):
  250. # XXX we need to either disallow these attrs on Library instances,
  251. # or warn/abort here if set, or something...
  252. # libraries=None, library_dirs=None, runtime_library_dirs=None,
  253. # export_symbols=None, extra_preargs=None, extra_postargs=None,
  254. # build_temp=None
  255. assert output_dir is None # distutils build_ext doesn't pass this
  256. output_dir, filename = os.path.split(output_libname)
  257. basename, ext = os.path.splitext(filename)
  258. if self.library_filename("x").startswith('lib'):
  259. # strip 'lib' prefix; this is kludgy if some platform uses
  260. # a different prefix
  261. basename = basename[3:]
  262. self.create_static_lib(
  263. objects, basename, output_dir, debug, target_lang
  264. )