build.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import sys
  2. from typing import TYPE_CHECKING, List, Dict
  3. from distutils.command.build import build as _build
  4. from ..warnings import SetuptoolsDeprecationWarning
  5. if sys.version_info >= (3, 8):
  6. from typing import Protocol
  7. elif TYPE_CHECKING:
  8. from typing_extensions import Protocol
  9. else:
  10. from abc import ABC as Protocol
  11. _ORIGINAL_SUBCOMMANDS = {"build_py", "build_clib", "build_ext", "build_scripts"}
  12. class build(_build):
  13. # copy to avoid sharing the object with parent class
  14. sub_commands = _build.sub_commands[:]
  15. def get_sub_commands(self):
  16. subcommands = {cmd[0] for cmd in _build.sub_commands}
  17. if subcommands - _ORIGINAL_SUBCOMMANDS:
  18. SetuptoolsDeprecationWarning.emit(
  19. "Direct usage of `distutils` commands",
  20. """
  21. It seems that you are using `distutils.command.build` to add
  22. new subcommands. Using `distutils` directly is considered deprecated,
  23. please use `setuptools.command.build`.
  24. """,
  25. due_date=(2023, 12, 13), # Warning introduced in 13 Jun 2022.
  26. see_url="https://peps.python.org/pep-0632/",
  27. )
  28. self.sub_commands = _build.sub_commands
  29. return super().get_sub_commands()
  30. class SubCommand(Protocol):
  31. """In order to support editable installations (see :pep:`660`) all
  32. build subcommands **SHOULD** implement this protocol. They also **MUST** inherit
  33. from ``setuptools.Command``.
  34. When creating an :pep:`editable wheel <660>`, ``setuptools`` will try to evaluate
  35. custom ``build`` subcommands using the following procedure:
  36. 1. ``setuptools`` will set the ``editable_mode`` attribute to ``True``
  37. 2. ``setuptools`` will execute the ``run()`` command.
  38. .. important::
  39. Subcommands **SHOULD** take advantage of ``editable_mode=True`` to adequate
  40. its behaviour or perform optimisations.
  41. For example, if a subcommand doesn't need to generate an extra file and
  42. all it does is to copy a source file into the build directory,
  43. ``run()`` **SHOULD** simply "early return".
  44. Similarly, if the subcommand creates files that would be placed alongside
  45. Python files in the final distribution, during an editable install
  46. the command **SHOULD** generate these files "in place" (i.e. write them to
  47. the original source directory, instead of using the build directory).
  48. Note that ``get_output_mapping()`` should reflect that and include mappings
  49. for "in place" builds accordingly.
  50. 3. ``setuptools`` use any knowledge it can derive from the return values of
  51. ``get_outputs()`` and ``get_output_mapping()`` to create an editable wheel.
  52. When relevant ``setuptools`` **MAY** attempt to use file links based on the value
  53. of ``get_output_mapping()``. Alternatively, ``setuptools`` **MAY** attempt to use
  54. :doc:`import hooks <python:reference/import>` to redirect any attempt to import
  55. to the directory with the original source code and other files built in place.
  56. Please note that custom sub-commands **SHOULD NOT** rely on ``run()`` being
  57. executed (or not) to provide correct return values for ``get_outputs()``,
  58. ``get_output_mapping()`` or ``get_source_files()``. The ``get_*`` methods should
  59. work independently of ``run()``.
  60. """
  61. editable_mode: bool = False
  62. """Boolean flag that will be set to ``True`` when setuptools is used for an
  63. editable installation (see :pep:`660`).
  64. Implementations **SHOULD** explicitly set the default value of this attribute to
  65. ``False``.
  66. When subcommands run, they can use this flag to perform optimizations or change
  67. their behaviour accordingly.
  68. """
  69. build_lib: str
  70. """String representing the directory where the build artifacts should be stored,
  71. e.g. ``build/lib``.
  72. For example, if a distribution wants to provide a Python module named ``pkg.mod``,
  73. then a corresponding file should be written to ``{build_lib}/package/module.py``.
  74. A way of thinking about this is that the files saved under ``build_lib``
  75. would be eventually copied to one of the directories in :obj:`site.PREFIXES`
  76. upon installation.
  77. A command that produces platform-independent files (e.g. compiling text templates
  78. into Python functions), **CAN** initialize ``build_lib`` by copying its value from
  79. the ``build_py`` command. On the other hand, a command that produces
  80. platform-specific files **CAN** initialize ``build_lib`` by copying its value from
  81. the ``build_ext`` command. In general this is done inside the ``finalize_options``
  82. method with the help of the ``set_undefined_options`` command::
  83. def finalize_options(self):
  84. self.set_undefined_options("build_py", ("build_lib", "build_lib"))
  85. ...
  86. """
  87. def initialize_options(self):
  88. """(Required by the original :class:`setuptools.Command` interface)"""
  89. def finalize_options(self):
  90. """(Required by the original :class:`setuptools.Command` interface)"""
  91. def run(self):
  92. """(Required by the original :class:`setuptools.Command` interface)"""
  93. def get_source_files(self) -> List[str]:
  94. """
  95. Return a list of all files that are used by the command to create the expected
  96. outputs.
  97. For example, if your build command transpiles Java files into Python, you should
  98. list here all the Java files.
  99. The primary purpose of this function is to help populating the ``sdist``
  100. with all the files necessary to build the distribution.
  101. All files should be strings relative to the project root directory.
  102. """
  103. def get_outputs(self) -> List[str]:
  104. """
  105. Return a list of files intended for distribution as they would have been
  106. produced by the build.
  107. These files should be strings in the form of
  108. ``"{build_lib}/destination/file/path"``.
  109. .. note::
  110. The return value of ``get_output()`` should include all files used as keys
  111. in ``get_output_mapping()`` plus files that are generated during the build
  112. and don't correspond to any source file already present in the project.
  113. """
  114. def get_output_mapping(self) -> Dict[str, str]:
  115. """
  116. Return a mapping between destination files as they would be produced by the
  117. build (dict keys) into the respective existing (source) files (dict values).
  118. Existing (source) files should be represented as strings relative to the project
  119. root directory.
  120. Destination files should be strings in the form of
  121. ``"{build_lib}/destination/file/path"``.
  122. """