build_ext.py 13 KB

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