build.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. """distutils.command.build
  2. Implements the Distutils 'build' command."""
  3. from __future__ import annotations
  4. import os
  5. import sys
  6. import sysconfig
  7. from collections.abc import Callable
  8. from typing import ClassVar
  9. from ..ccompiler import show_compilers
  10. from ..core import Command
  11. from ..errors import DistutilsOptionError
  12. from ..util import get_platform
  13. class build(Command):
  14. description = "build everything needed to install"
  15. user_options = [
  16. ('build-base=', 'b', "base directory for build library"),
  17. ('build-purelib=', None, "build directory for platform-neutral distributions"),
  18. ('build-platlib=', None, "build directory for platform-specific distributions"),
  19. (
  20. 'build-lib=',
  21. None,
  22. "build directory for all distribution (defaults to either build-purelib or build-platlib",
  23. ),
  24. ('build-scripts=', None, "build directory for scripts"),
  25. ('build-temp=', 't', "temporary build directory"),
  26. (
  27. 'plat-name=',
  28. 'p',
  29. f"platform name to build for, if supported [default: {get_platform()}]",
  30. ),
  31. ('compiler=', 'c', "specify the compiler type"),
  32. ('parallel=', 'j', "number of parallel build jobs"),
  33. ('debug', 'g', "compile extensions and libraries with debugging information"),
  34. ('force', 'f', "forcibly build everything (ignore file timestamps)"),
  35. ('executable=', 'e', "specify final destination interpreter path (build.py)"),
  36. ]
  37. boolean_options: ClassVar[list[str]] = ['debug', 'force']
  38. help_options: ClassVar[list[tuple[str, str | None, str, Callable[[], object]]]] = [
  39. ('help-compiler', None, "list available compilers", show_compilers),
  40. ]
  41. def initialize_options(self):
  42. self.build_base = 'build'
  43. # these are decided only after 'build_base' has its final value
  44. # (unless overridden by the user or client)
  45. self.build_purelib = None
  46. self.build_platlib = None
  47. self.build_lib = None
  48. self.build_temp = None
  49. self.build_scripts = None
  50. self.compiler = None
  51. self.plat_name = None
  52. self.debug = None
  53. self.force = False
  54. self.executable = None
  55. self.parallel = None
  56. def finalize_options(self) -> None: # noqa: C901
  57. if self.plat_name is None:
  58. self.plat_name = get_platform()
  59. else:
  60. # plat-name only supported for windows (other platforms are
  61. # supported via ./configure flags, if at all). Avoid misleading
  62. # other platforms.
  63. if os.name != 'nt':
  64. raise DistutilsOptionError(
  65. "--plat-name only supported on Windows (try "
  66. "using './configure --help' on your platform)"
  67. )
  68. plat_specifier = f".{self.plat_name}-{sys.implementation.cache_tag}"
  69. # Python 3.13+ with --disable-gil shouldn't share build directories
  70. if sysconfig.get_config_var('Py_GIL_DISABLED'):
  71. plat_specifier += 't'
  72. # Make it so Python 2.x and Python 2.x with --with-pydebug don't
  73. # share the same build directories. Doing so confuses the build
  74. # process for C modules
  75. if hasattr(sys, 'gettotalrefcount'):
  76. plat_specifier += '-pydebug'
  77. # 'build_purelib' and 'build_platlib' just default to 'lib' and
  78. # 'lib.<plat>' under the base build directory. We only use one of
  79. # them for a given distribution, though --
  80. if self.build_purelib is None:
  81. self.build_purelib = os.path.join(self.build_base, 'lib')
  82. if self.build_platlib is None:
  83. self.build_platlib = os.path.join(self.build_base, 'lib' + plat_specifier)
  84. # 'build_lib' is the actual directory that we will use for this
  85. # particular module distribution -- if user didn't supply it, pick
  86. # one of 'build_purelib' or 'build_platlib'.
  87. if self.build_lib is None:
  88. if self.distribution.has_ext_modules():
  89. self.build_lib = self.build_platlib
  90. else:
  91. self.build_lib = self.build_purelib
  92. # 'build_temp' -- temporary directory for compiler turds,
  93. # "build/temp.<plat>"
  94. if self.build_temp is None:
  95. self.build_temp = os.path.join(self.build_base, 'temp' + plat_specifier)
  96. if self.build_scripts is None:
  97. self.build_scripts = os.path.join(
  98. self.build_base,
  99. f'scripts-{sys.version_info.major}.{sys.version_info.minor}',
  100. )
  101. if self.executable is None and sys.executable:
  102. self.executable = os.path.normpath(sys.executable)
  103. if isinstance(self.parallel, str):
  104. try:
  105. self.parallel = int(self.parallel)
  106. except ValueError:
  107. raise DistutilsOptionError("parallel should be an integer")
  108. def run(self) -> None:
  109. # Run all relevant sub-commands. This will be some subset of:
  110. # - build_py - pure Python modules
  111. # - build_clib - standalone C libraries
  112. # - build_ext - Python extensions
  113. # - build_scripts - (Python) scripts
  114. for cmd_name in self.get_sub_commands():
  115. self.run_command(cmd_name)
  116. # -- Predicates for the sub-command list ---------------------------
  117. def has_pure_modules(self):
  118. return self.distribution.has_pure_modules()
  119. def has_c_libraries(self):
  120. return self.distribution.has_c_libraries()
  121. def has_ext_modules(self):
  122. return self.distribution.has_ext_modules()
  123. def has_scripts(self):
  124. return self.distribution.has_scripts()
  125. sub_commands = [
  126. ('build_py', has_pure_modules),
  127. ('build_clib', has_c_libraries),
  128. ('build_ext', has_ext_modules),
  129. ('build_scripts', has_scripts),
  130. ]