build_ext.py 18 KB

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