build_ext.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import os
  2. import sys
  3. import itertools
  4. from importlib.machinery import EXTENSION_SUFFIXES
  5. from importlib.util import cache_from_source as _compiled_file_name
  6. from typing import Dict, Iterator, List, Tuple
  7. from pathlib import Path
  8. from distutils.command.build_ext import build_ext as _du_build_ext
  9. from distutils.ccompiler import new_compiler
  10. from distutils.sysconfig import customize_compiler, get_config_var
  11. from distutils import log
  12. from setuptools.errors import BaseError
  13. from setuptools.extension import Extension, Library
  14. try:
  15. # Attempt to use Cython for building extensions, if available
  16. from Cython.Distutils.build_ext import build_ext as _build_ext
  17. # Additionally, assert that the compiler module will load
  18. # also. Ref #1229.
  19. __import__('Cython.Compiler.Main')
  20. except ImportError:
  21. _build_ext = _du_build_ext
  22. # make sure _config_vars is initialized
  23. get_config_var("LDSHARED")
  24. from distutils.sysconfig import _config_vars as _CONFIG_VARS # noqa
  25. def _customize_compiler_for_shlib(compiler):
  26. if sys.platform == "darwin":
  27. # building .dylib requires additional compiler flags on OSX; here we
  28. # temporarily substitute the pyconfig.h variables so that distutils'
  29. # 'customize_compiler' uses them before we build the shared libraries.
  30. tmp = _CONFIG_VARS.copy()
  31. try:
  32. # XXX Help! I don't have any idea whether these are right...
  33. _CONFIG_VARS[
  34. 'LDSHARED'
  35. ] = "gcc -Wl,-x -dynamiclib -undefined dynamic_lookup"
  36. _CONFIG_VARS['CCSHARED'] = " -dynamiclib"
  37. _CONFIG_VARS['SO'] = ".dylib"
  38. customize_compiler(compiler)
  39. finally:
  40. _CONFIG_VARS.clear()
  41. _CONFIG_VARS.update(tmp)
  42. else:
  43. customize_compiler(compiler)
  44. have_rtld = False
  45. use_stubs = False
  46. libtype = 'shared'
  47. if sys.platform == "darwin":
  48. use_stubs = True
  49. elif os.name != 'nt':
  50. try:
  51. import dl
  52. use_stubs = have_rtld = hasattr(dl, 'RTLD_NOW')
  53. except ImportError:
  54. pass
  55. def if_dl(s):
  56. return s if have_rtld else ''
  57. def get_abi3_suffix():
  58. """Return the file extension for an abi3-compliant Extension()"""
  59. for suffix in EXTENSION_SUFFIXES:
  60. if '.abi3' in suffix: # Unix
  61. return suffix
  62. elif suffix == '.pyd': # Windows
  63. return suffix
  64. class build_ext(_build_ext):
  65. editable_mode: bool = False
  66. inplace: bool = False
  67. def run(self):
  68. """Build extensions in build directory, then copy if --inplace"""
  69. old_inplace, self.inplace = self.inplace, 0
  70. _build_ext.run(self)
  71. self.inplace = old_inplace
  72. if old_inplace:
  73. self.copy_extensions_to_source()
  74. def _get_inplace_equivalent(self, build_py, ext: Extension) -> Tuple[str, str]:
  75. fullname = self.get_ext_fullname(ext.name)
  76. filename = self.get_ext_filename(fullname)
  77. modpath = fullname.split('.')
  78. package = '.'.join(modpath[:-1])
  79. package_dir = build_py.get_package_dir(package)
  80. inplace_file = os.path.join(package_dir, os.path.basename(filename))
  81. regular_file = os.path.join(self.build_lib, filename)
  82. return (inplace_file, regular_file)
  83. def copy_extensions_to_source(self):
  84. build_py = self.get_finalized_command('build_py')
  85. for ext in self.extensions:
  86. inplace_file, regular_file = self._get_inplace_equivalent(build_py, ext)
  87. # Always copy, even if source is older than destination, to ensure
  88. # that the right extensions for the current Python/platform are
  89. # used.
  90. if os.path.exists(regular_file) or not ext.optional:
  91. self.copy_file(regular_file, inplace_file, level=self.verbose)
  92. if ext._needs_stub:
  93. inplace_stub = self._get_equivalent_stub(ext, inplace_file)
  94. self._write_stub_file(inplace_stub, ext, compile=True)
  95. # Always compile stub and remove the original (leave the cache behind)
  96. # (this behaviour was observed in previous iterations of the code)
  97. def _get_equivalent_stub(self, ext: Extension, output_file: str) -> str:
  98. dir_ = os.path.dirname(output_file)
  99. _, _, name = ext.name.rpartition(".")
  100. return f"{os.path.join(dir_, name)}.py"
  101. def _get_output_mapping(self) -> Iterator[Tuple[str, str]]:
  102. if not self.inplace:
  103. return
  104. build_py = self.get_finalized_command('build_py')
  105. opt = self.get_finalized_command('install_lib').optimize or ""
  106. for ext in self.extensions:
  107. inplace_file, regular_file = self._get_inplace_equivalent(build_py, ext)
  108. yield (regular_file, inplace_file)
  109. if ext._needs_stub:
  110. # This version of `build_ext` always builds artifacts in another dir,
  111. # when "inplace=True" is given it just copies them back.
  112. # This is done in the `copy_extensions_to_source` function, which
  113. # always compile stub files via `_compile_and_remove_stub`.
  114. # At the end of the process, a `.pyc` stub file is created without the
  115. # corresponding `.py`.
  116. inplace_stub = self._get_equivalent_stub(ext, inplace_file)
  117. regular_stub = self._get_equivalent_stub(ext, regular_file)
  118. inplace_cache = _compiled_file_name(inplace_stub, optimization=opt)
  119. output_cache = _compiled_file_name(regular_stub, optimization=opt)
  120. yield (output_cache, inplace_cache)
  121. def get_ext_filename(self, fullname):
  122. so_ext = os.getenv('SETUPTOOLS_EXT_SUFFIX')
  123. if so_ext:
  124. filename = os.path.join(*fullname.split('.')) + so_ext
  125. else:
  126. filename = _build_ext.get_ext_filename(self, fullname)
  127. so_ext = get_config_var('EXT_SUFFIX')
  128. if fullname in self.ext_map:
  129. ext = self.ext_map[fullname]
  130. use_abi3 = getattr(ext, 'py_limited_api') and get_abi3_suffix()
  131. if use_abi3:
  132. filename = filename[: -len(so_ext)]
  133. so_ext = get_abi3_suffix()
  134. filename = filename + so_ext
  135. if isinstance(ext, Library):
  136. fn, ext = os.path.splitext(filename)
  137. return self.shlib_compiler.library_filename(fn, libtype)
  138. elif use_stubs and ext._links_to_dynamic:
  139. d, fn = os.path.split(filename)
  140. return os.path.join(d, 'dl-' + fn)
  141. return filename
  142. def initialize_options(self):
  143. _build_ext.initialize_options(self)
  144. self.shlib_compiler = None
  145. self.shlibs = []
  146. self.ext_map = {}
  147. self.editable_mode = False
  148. def finalize_options(self):
  149. _build_ext.finalize_options(self)
  150. self.extensions = self.extensions or []
  151. self.check_extensions_list(self.extensions)
  152. self.shlibs = [ext for ext in self.extensions if isinstance(ext, Library)]
  153. if self.shlibs:
  154. self.setup_shlib_compiler()
  155. for ext in self.extensions:
  156. ext._full_name = self.get_ext_fullname(ext.name)
  157. for ext in self.extensions:
  158. fullname = ext._full_name
  159. self.ext_map[fullname] = ext
  160. # distutils 3.1 will also ask for module names
  161. # XXX what to do with conflicts?
  162. self.ext_map[fullname.split('.')[-1]] = ext
  163. ltd = self.shlibs and self.links_to_dynamic(ext) or False
  164. ns = ltd and use_stubs and not isinstance(ext, Library)
  165. ext._links_to_dynamic = ltd
  166. ext._needs_stub = ns
  167. filename = ext._file_name = self.get_ext_filename(fullname)
  168. libdir = os.path.dirname(os.path.join(self.build_lib, filename))
  169. if ltd and libdir not in ext.library_dirs:
  170. ext.library_dirs.append(libdir)
  171. if ltd and use_stubs and os.curdir not in ext.runtime_library_dirs:
  172. ext.runtime_library_dirs.append(os.curdir)
  173. if self.editable_mode:
  174. self.inplace = True
  175. def setup_shlib_compiler(self):
  176. compiler = self.shlib_compiler = new_compiler(
  177. compiler=self.compiler, dry_run=self.dry_run, force=self.force
  178. )
  179. _customize_compiler_for_shlib(compiler)
  180. if self.include_dirs is not None:
  181. compiler.set_include_dirs(self.include_dirs)
  182. if self.define is not None:
  183. # 'define' option is a list of (name,value) tuples
  184. for name, value in self.define:
  185. compiler.define_macro(name, value)
  186. if self.undef is not None:
  187. for macro in self.undef:
  188. compiler.undefine_macro(macro)
  189. if self.libraries is not None:
  190. compiler.set_libraries(self.libraries)
  191. if self.library_dirs is not None:
  192. compiler.set_library_dirs(self.library_dirs)
  193. if self.rpath is not None:
  194. compiler.set_runtime_library_dirs(self.rpath)
  195. if self.link_objects is not None:
  196. compiler.set_link_objects(self.link_objects)
  197. # hack so distutils' build_extension() builds a library instead
  198. compiler.link_shared_object = link_shared_object.__get__(compiler)
  199. def get_export_symbols(self, ext):
  200. if isinstance(ext, Library):
  201. return ext.export_symbols
  202. return _build_ext.get_export_symbols(self, ext)
  203. def build_extension(self, ext):
  204. ext._convert_pyx_sources_to_lang()
  205. _compiler = self.compiler
  206. try:
  207. if isinstance(ext, Library):
  208. self.compiler = self.shlib_compiler
  209. _build_ext.build_extension(self, ext)
  210. if ext._needs_stub:
  211. build_lib = self.get_finalized_command('build_py').build_lib
  212. self.write_stub(build_lib, ext)
  213. finally:
  214. self.compiler = _compiler
  215. def links_to_dynamic(self, ext):
  216. """Return true if 'ext' links to a dynamic lib in the same package"""
  217. # XXX this should check to ensure the lib is actually being built
  218. # XXX as dynamic, and not just using a locally-found version or a
  219. # XXX static-compiled version
  220. libnames = dict.fromkeys([lib._full_name for lib in self.shlibs])
  221. pkg = '.'.join(ext._full_name.split('.')[:-1] + [''])
  222. return any(pkg + libname in libnames for libname in ext.libraries)
  223. def get_source_files(self) -> List[str]:
  224. return [*_build_ext.get_source_files(self), *self._get_internal_depends()]
  225. def _get_internal_depends(self) -> Iterator[str]:
  226. """Yield ``ext.depends`` that are contained by the project directory"""
  227. project_root = Path(self.distribution.src_root or os.curdir).resolve()
  228. depends = (dep for ext in self.extensions for dep in ext.depends)
  229. def skip(orig_path: str, reason: str) -> None:
  230. log.info(
  231. "dependency %s won't be automatically "
  232. "included in the manifest: the path %s",
  233. orig_path,
  234. reason,
  235. )
  236. for dep in depends:
  237. path = Path(dep)
  238. if path.is_absolute():
  239. skip(dep, "must be relative")
  240. continue
  241. if ".." in path.parts:
  242. skip(dep, "can't have `..` segments")
  243. continue
  244. try:
  245. resolved = (project_root / path).resolve(strict=True)
  246. except OSError:
  247. skip(dep, "doesn't exist")
  248. continue
  249. try:
  250. resolved.relative_to(project_root)
  251. except ValueError:
  252. skip(dep, "must be inside the project root")
  253. continue
  254. yield path.as_posix()
  255. def get_outputs(self) -> List[str]:
  256. if self.inplace:
  257. return list(self.get_output_mapping().keys())
  258. return sorted(_build_ext.get_outputs(self) + self.__get_stubs_outputs())
  259. def get_output_mapping(self) -> Dict[str, str]:
  260. """See :class:`setuptools.commands.build.SubCommand`"""
  261. mapping = self._get_output_mapping()
  262. return dict(sorted(mapping, key=lambda x: x[0]))
  263. def __get_stubs_outputs(self):
  264. # assemble the base name for each extension that needs a stub
  265. ns_ext_bases = (
  266. os.path.join(self.build_lib, *ext._full_name.split('.'))
  267. for ext in self.extensions
  268. if ext._needs_stub
  269. )
  270. # pair each base with the extension
  271. pairs = itertools.product(ns_ext_bases, self.__get_output_extensions())
  272. return list(base + fnext for base, fnext in pairs)
  273. def __get_output_extensions(self):
  274. yield '.py'
  275. yield '.pyc'
  276. if self.get_finalized_command('build_py').optimize:
  277. yield '.pyo'
  278. def write_stub(self, output_dir, ext, compile=False):
  279. stub_file = os.path.join(output_dir, *ext._full_name.split('.')) + '.py'
  280. self._write_stub_file(stub_file, ext, compile)
  281. def _write_stub_file(self, stub_file: str, ext: Extension, compile=False):
  282. log.info("writing stub loader for %s to %s", ext._full_name, stub_file)
  283. if compile and os.path.exists(stub_file):
  284. raise BaseError(stub_file + " already exists! Please delete.")
  285. if not self.dry_run:
  286. f = open(stub_file, 'w')
  287. f.write(
  288. '\n'.join(
  289. [
  290. "def __bootstrap__():",
  291. " global __bootstrap__, __file__, __loader__",
  292. " import sys, os, pkg_resources, importlib.util"
  293. + if_dl(", dl"),
  294. " __file__ = pkg_resources.resource_filename"
  295. "(__name__,%r)" % os.path.basename(ext._file_name),
  296. " del __bootstrap__",
  297. " if '__loader__' in globals():",
  298. " del __loader__",
  299. if_dl(" old_flags = sys.getdlopenflags()"),
  300. " old_dir = os.getcwd()",
  301. " try:",
  302. " os.chdir(os.path.dirname(__file__))",
  303. if_dl(" sys.setdlopenflags(dl.RTLD_NOW)"),
  304. " spec = importlib.util.spec_from_file_location(",
  305. " __name__, __file__)",
  306. " mod = importlib.util.module_from_spec(spec)",
  307. " spec.loader.exec_module(mod)",
  308. " finally:",
  309. if_dl(" sys.setdlopenflags(old_flags)"),
  310. " os.chdir(old_dir)",
  311. "__bootstrap__()",
  312. "", # terminal \n
  313. ]
  314. )
  315. )
  316. f.close()
  317. if compile:
  318. self._compile_and_remove_stub(stub_file)
  319. def _compile_and_remove_stub(self, stub_file: str):
  320. from distutils.util import byte_compile
  321. byte_compile([stub_file], optimize=0, force=True, dry_run=self.dry_run)
  322. optimize = self.get_finalized_command('install_lib').optimize
  323. if optimize > 0:
  324. byte_compile(
  325. [stub_file], optimize=optimize, force=True, dry_run=self.dry_run
  326. )
  327. if os.path.exists(stub_file) and not self.dry_run:
  328. os.unlink(stub_file)
  329. if use_stubs or os.name == 'nt':
  330. # Build shared libraries
  331. #
  332. def link_shared_object(
  333. self,
  334. objects,
  335. output_libname,
  336. output_dir=None,
  337. libraries=None,
  338. library_dirs=None,
  339. runtime_library_dirs=None,
  340. export_symbols=None,
  341. debug=0,
  342. extra_preargs=None,
  343. extra_postargs=None,
  344. build_temp=None,
  345. target_lang=None,
  346. ):
  347. self.link(
  348. self.SHARED_LIBRARY,
  349. objects,
  350. output_libname,
  351. output_dir,
  352. libraries,
  353. library_dirs,
  354. runtime_library_dirs,
  355. export_symbols,
  356. debug,
  357. extra_preargs,
  358. extra_postargs,
  359. build_temp,
  360. target_lang,
  361. )
  362. else:
  363. # Build static libraries everywhere else
  364. libtype = 'static'
  365. def link_shared_object(
  366. self,
  367. objects,
  368. output_libname,
  369. output_dir=None,
  370. libraries=None,
  371. library_dirs=None,
  372. runtime_library_dirs=None,
  373. export_symbols=None,
  374. debug=0,
  375. extra_preargs=None,
  376. extra_postargs=None,
  377. build_temp=None,
  378. target_lang=None,
  379. ):
  380. # XXX we need to either disallow these attrs on Library instances,
  381. # or warn/abort here if set, or something...
  382. # libraries=None, library_dirs=None, runtime_library_dirs=None,
  383. # export_symbols=None, extra_preargs=None, extra_postargs=None,
  384. # build_temp=None
  385. assert output_dir is None # distutils build_ext doesn't pass this
  386. output_dir, filename = os.path.split(output_libname)
  387. basename, ext = os.path.splitext(filename)
  388. if self.library_filename("x").startswith('lib'):
  389. # strip 'lib' prefix; this is kludgy if some platform uses
  390. # a different prefix
  391. basename = basename[3:]
  392. self.create_static_lib(objects, basename, output_dir, debug, target_lang)