You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

178 line
6.5 KiB

  1. from __future__ import annotations
  2. import functools
  3. import re
  4. from typing import TYPE_CHECKING
  5. from setuptools._path import StrPath
  6. from .monkey import get_unpatched
  7. import distutils.core
  8. import distutils.errors
  9. import distutils.extension
  10. def _have_cython():
  11. """
  12. Return True if Cython can be imported.
  13. """
  14. cython_impl = 'Cython.Distutils.build_ext'
  15. try:
  16. # from (cython_impl) import build_ext
  17. __import__(cython_impl, fromlist=['build_ext']).build_ext
  18. except Exception:
  19. return False
  20. return True
  21. # for compatibility
  22. have_pyrex = _have_cython
  23. if TYPE_CHECKING:
  24. # Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
  25. from distutils.core import Extension as _Extension
  26. else:
  27. _Extension = get_unpatched(distutils.core.Extension)
  28. class Extension(_Extension):
  29. """
  30. Describes a single extension module.
  31. This means that all source files will be compiled into a single binary file
  32. ``<module path>.<suffix>`` (with ``<module path>`` derived from ``name`` and
  33. ``<suffix>`` defined by one of the values in
  34. ``importlib.machinery.EXTENSION_SUFFIXES``).
  35. In the case ``.pyx`` files are passed as ``sources and`` ``Cython`` is **not**
  36. installed in the build environment, ``setuptools`` may also try to look for the
  37. equivalent ``.cpp`` or ``.c`` files.
  38. :arg str name:
  39. the full name of the extension, including any packages -- ie.
  40. *not* a filename or pathname, but Python dotted name
  41. :arg list[str|os.PathLike[str]] sources:
  42. list of source filenames, relative to the distribution root
  43. (where the setup script lives), in Unix form (slash-separated)
  44. for portability. Source files may be C, C++, SWIG (.i),
  45. platform-specific resource files, or whatever else is recognized
  46. by the "build_ext" command as source for a Python extension.
  47. :keyword list[str] include_dirs:
  48. list of directories to search for C/C++ header files (in Unix
  49. form for portability)
  50. :keyword list[tuple[str, str|None]] define_macros:
  51. list of macros to define; each macro is defined using a 2-tuple:
  52. the first item corresponding to the name of the macro and the second
  53. item either a string with its value or None to
  54. define it without a particular value (equivalent of "#define
  55. FOO" in source or -DFOO on Unix C compiler command line)
  56. :keyword list[str] undef_macros:
  57. list of macros to undefine explicitly
  58. :keyword list[str] library_dirs:
  59. list of directories to search for C/C++ libraries at link time
  60. :keyword list[str] libraries:
  61. list of library names (not filenames or paths) to link against
  62. :keyword list[str] runtime_library_dirs:
  63. list of directories to search for C/C++ libraries at run time
  64. (for shared extensions, this is when the extension is loaded).
  65. Setting this will cause an exception during build on Windows
  66. platforms.
  67. :keyword list[str] extra_objects:
  68. list of extra files to link with (eg. object files not implied
  69. by 'sources', static library that must be explicitly specified,
  70. binary resource files, etc.)
  71. :keyword list[str] extra_compile_args:
  72. any extra platform- and compiler-specific information to use
  73. when compiling the source files in 'sources'. For platforms and
  74. compilers where "command line" makes sense, this is typically a
  75. list of command-line arguments, but for other platforms it could
  76. be anything.
  77. :keyword list[str] extra_link_args:
  78. any extra platform- and compiler-specific information to use
  79. when linking object files together to create the extension (or
  80. to create a new static Python interpreter). Similar
  81. interpretation as for 'extra_compile_args'.
  82. :keyword list[str] export_symbols:
  83. list of symbols to be exported from a shared extension. Not
  84. used on all platforms, and not generally necessary for Python
  85. extensions, which typically export exactly one symbol: "init" +
  86. extension_name.
  87. :keyword list[str] swig_opts:
  88. any extra options to pass to SWIG if a source file has the .i
  89. extension.
  90. :keyword list[str] depends:
  91. list of files that the extension depends on
  92. :keyword str language:
  93. extension language (i.e. "c", "c++", "objc"). Will be detected
  94. from the source extensions if not provided.
  95. :keyword bool optional:
  96. specifies that a build failure in the extension should not abort the
  97. build process, but simply not install the failing extension.
  98. :keyword bool py_limited_api:
  99. opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`.
  100. :raises setuptools.errors.PlatformError: if ``runtime_library_dirs`` is
  101. specified on Windows. (since v63)
  102. """
  103. # These 4 are set and used in setuptools/command/build_ext.py
  104. # The lack of a default value and risk of `AttributeError` is purposeful
  105. # to avoid people forgetting to call finalize_options if they modify the extension list.
  106. # See example/rationale in https://github.com/pypa/setuptools/issues/4529.
  107. _full_name: str #: Private API, internal use only.
  108. _links_to_dynamic: bool #: Private API, internal use only.
  109. _needs_stub: bool #: Private API, internal use only.
  110. _file_name: str #: Private API, internal use only.
  111. def __init__(
  112. self,
  113. name: str,
  114. sources: list[StrPath],
  115. *args,
  116. py_limited_api: bool = False,
  117. **kw,
  118. ) -> None:
  119. # The *args is needed for compatibility as calls may use positional
  120. # arguments. py_limited_api may be set only via keyword.
  121. self.py_limited_api = py_limited_api
  122. super().__init__(
  123. name,
  124. sources, # type: ignore[arg-type] # Vendored version of setuptools supports PathLike
  125. *args,
  126. **kw,
  127. )
  128. def _convert_pyx_sources_to_lang(self):
  129. """
  130. Replace sources with .pyx extensions to sources with the target
  131. language extension. This mechanism allows language authors to supply
  132. pre-converted sources but to prefer the .pyx sources.
  133. """
  134. if _have_cython():
  135. # the build has Cython, so allow it to compile the .pyx files
  136. return
  137. lang = self.language or ''
  138. target_ext = '.cpp' if lang.lower() == 'c++' else '.c'
  139. sub = functools.partial(re.sub, '.pyx$', target_ext)
  140. self.sources = list(map(sub, self.sources))
  141. class Library(Extension):
  142. """Just like a regular Extension, but built as a library instead"""