build_ext.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. so_ext = os.getenv('SETUPTOOLS_EXT_SUFFIX')
  91. if so_ext:
  92. filename = os.path.join(*fullname.split('.')) + so_ext
  93. else:
  94. filename = _build_ext.get_ext_filename(self, fullname)
  95. so_ext = get_config_var('EXT_SUFFIX')
  96. if fullname in self.ext_map:
  97. ext = self.ext_map[fullname]
  98. use_abi3 = getattr(ext, 'py_limited_api') and get_abi3_suffix()
  99. if use_abi3:
  100. filename = filename[:-len(so_ext)]
  101. so_ext = get_abi3_suffix()
  102. filename = filename + so_ext
  103. if isinstance(ext, Library):
  104. fn, ext = os.path.splitext(filename)
  105. return self.shlib_compiler.library_filename(fn, libtype)
  106. elif use_stubs and ext._links_to_dynamic:
  107. d, fn = os.path.split(filename)
  108. return os.path.join(d, 'dl-' + fn)
  109. return filename
  110. def initialize_options(self):
  111. _build_ext.initialize_options(self)
  112. self.shlib_compiler = None
  113. self.shlibs = []
  114. self.ext_map = {}
  115. def finalize_options(self):
  116. _build_ext.finalize_options(self)
  117. self.extensions = self.extensions or []
  118. self.check_extensions_list(self.extensions)
  119. self.shlibs = [ext for ext in self.extensions
  120. if isinstance(ext, Library)]
  121. if self.shlibs:
  122. self.setup_shlib_compiler()
  123. for ext in self.extensions:
  124. ext._full_name = self.get_ext_fullname(ext.name)
  125. for ext in self.extensions:
  126. fullname = ext._full_name
  127. self.ext_map[fullname] = ext
  128. # distutils 3.1 will also ask for module names
  129. # XXX what to do with conflicts?
  130. self.ext_map[fullname.split('.')[-1]] = ext
  131. ltd = self.shlibs and self.links_to_dynamic(ext) or False
  132. ns = ltd and use_stubs and not isinstance(ext, Library)
  133. ext._links_to_dynamic = ltd
  134. ext._needs_stub = ns
  135. filename = ext._file_name = self.get_ext_filename(fullname)
  136. libdir = os.path.dirname(os.path.join(self.build_lib, filename))
  137. if ltd and libdir not in ext.library_dirs:
  138. ext.library_dirs.append(libdir)
  139. if ltd and use_stubs and os.curdir not in ext.runtime_library_dirs:
  140. ext.runtime_library_dirs.append(os.curdir)
  141. def setup_shlib_compiler(self):
  142. compiler = self.shlib_compiler = new_compiler(
  143. compiler=self.compiler, dry_run=self.dry_run, force=self.force
  144. )
  145. _customize_compiler_for_shlib(compiler)
  146. if self.include_dirs is not None:
  147. compiler.set_include_dirs(self.include_dirs)
  148. if self.define is not None:
  149. # 'define' option is a list of (name,value) tuples
  150. for (name, value) in self.define:
  151. compiler.define_macro(name, value)
  152. if self.undef is not None:
  153. for macro in self.undef:
  154. compiler.undefine_macro(macro)
  155. if self.libraries is not None:
  156. compiler.set_libraries(self.libraries)
  157. if self.library_dirs is not None:
  158. compiler.set_library_dirs(self.library_dirs)
  159. if self.rpath is not None:
  160. compiler.set_runtime_library_dirs(self.rpath)
  161. if self.link_objects is not None:
  162. compiler.set_link_objects(self.link_objects)
  163. # hack so distutils' build_extension() builds a library instead
  164. compiler.link_shared_object = link_shared_object.__get__(compiler)
  165. def get_export_symbols(self, ext):
  166. if isinstance(ext, Library):
  167. return ext.export_symbols
  168. return _build_ext.get_export_symbols(self, ext)
  169. def build_extension(self, ext):
  170. ext._convert_pyx_sources_to_lang()
  171. _compiler = self.compiler
  172. try:
  173. if isinstance(ext, Library):
  174. self.compiler = self.shlib_compiler
  175. _build_ext.build_extension(self, ext)
  176. if ext._needs_stub:
  177. cmd = self.get_finalized_command('build_py').build_lib
  178. self.write_stub(cmd, ext)
  179. finally:
  180. self.compiler = _compiler
  181. def links_to_dynamic(self, ext):
  182. """Return true if 'ext' links to a dynamic lib in the same package"""
  183. # XXX this should check to ensure the lib is actually being built
  184. # XXX as dynamic, and not just using a locally-found version or a
  185. # XXX static-compiled version
  186. libnames = dict.fromkeys([lib._full_name for lib in self.shlibs])
  187. pkg = '.'.join(ext._full_name.split('.')[:-1] + [''])
  188. return any(pkg + libname in libnames for libname in ext.libraries)
  189. def get_outputs(self):
  190. return _build_ext.get_outputs(self) + self.__get_stubs_outputs()
  191. def __get_stubs_outputs(self):
  192. # assemble the base name for each extension that needs a stub
  193. ns_ext_bases = (
  194. os.path.join(self.build_lib, *ext._full_name.split('.'))
  195. for ext in self.extensions
  196. if ext._needs_stub
  197. )
  198. # pair each base with the extension
  199. pairs = itertools.product(ns_ext_bases, self.__get_output_extensions())
  200. return list(base + fnext for base, fnext in pairs)
  201. def __get_output_extensions(self):
  202. yield '.py'
  203. yield '.pyc'
  204. if self.get_finalized_command('build_py').optimize:
  205. yield '.pyo'
  206. def write_stub(self, output_dir, ext, compile=False):
  207. log.info("writing stub loader for %s to %s", ext._full_name,
  208. output_dir)
  209. stub_file = (os.path.join(output_dir, *ext._full_name.split('.')) +
  210. '.py')
  211. if compile and os.path.exists(stub_file):
  212. raise DistutilsError(stub_file + " already exists! Please delete.")
  213. if not self.dry_run:
  214. f = open(stub_file, 'w')
  215. f.write(
  216. '\n'.join([
  217. "def __bootstrap__():",
  218. " global __bootstrap__, __file__, __loader__",
  219. " import sys, os, pkg_resources, importlib.util" +
  220. if_dl(", dl"),
  221. " __file__ = pkg_resources.resource_filename"
  222. "(__name__,%r)"
  223. % os.path.basename(ext._file_name),
  224. " del __bootstrap__",
  225. " if '__loader__' in globals():",
  226. " del __loader__",
  227. if_dl(" old_flags = sys.getdlopenflags()"),
  228. " old_dir = os.getcwd()",
  229. " try:",
  230. " os.chdir(os.path.dirname(__file__))",
  231. if_dl(" sys.setdlopenflags(dl.RTLD_NOW)"),
  232. " spec = importlib.util.spec_from_file_location(",
  233. " __name__, __file__)",
  234. " mod = importlib.util.module_from_spec(spec)",
  235. " spec.loader.exec_module(mod)",
  236. " finally:",
  237. if_dl(" sys.setdlopenflags(old_flags)"),
  238. " os.chdir(old_dir)",
  239. "__bootstrap__()",
  240. "" # terminal \n
  241. ])
  242. )
  243. f.close()
  244. if compile:
  245. from distutils.util import byte_compile
  246. byte_compile([stub_file], optimize=0,
  247. force=True, dry_run=self.dry_run)
  248. optimize = self.get_finalized_command('install_lib').optimize
  249. if optimize > 0:
  250. byte_compile([stub_file], optimize=optimize,
  251. force=True, dry_run=self.dry_run)
  252. if os.path.exists(stub_file) and not self.dry_run:
  253. os.unlink(stub_file)
  254. if use_stubs or os.name == 'nt':
  255. # Build shared libraries
  256. #
  257. def link_shared_object(
  258. self, objects, output_libname, output_dir=None, libraries=None,
  259. library_dirs=None, runtime_library_dirs=None, export_symbols=None,
  260. debug=0, extra_preargs=None, extra_postargs=None, build_temp=None,
  261. target_lang=None):
  262. self.link(
  263. self.SHARED_LIBRARY, objects, output_libname,
  264. output_dir, libraries, library_dirs, runtime_library_dirs,
  265. export_symbols, debug, extra_preargs, extra_postargs,
  266. build_temp, target_lang
  267. )
  268. else:
  269. # Build static libraries everywhere else
  270. libtype = 'static'
  271. def link_shared_object(
  272. self, objects, output_libname, output_dir=None, libraries=None,
  273. library_dirs=None, runtime_library_dirs=None, export_symbols=None,
  274. debug=0, extra_preargs=None, extra_postargs=None, build_temp=None,
  275. target_lang=None):
  276. # XXX we need to either disallow these attrs on Library instances,
  277. # or warn/abort here if set, or something...
  278. # libraries=None, library_dirs=None, runtime_library_dirs=None,
  279. # export_symbols=None, extra_preargs=None, extra_postargs=None,
  280. # build_temp=None
  281. assert output_dir is None # distutils build_ext doesn't pass this
  282. output_dir, filename = os.path.split(output_libname)
  283. basename, ext = os.path.splitext(filename)
  284. if self.library_filename("x").startswith('lib'):
  285. # strip 'lib' prefix; this is kludgy if some platform uses
  286. # a different prefix
  287. basename = basename[3:]
  288. self.create_static_lib(
  289. objects, basename, output_dir, debug, target_lang
  290. )