build_ext.py 12 KB

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