build.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """distutils.command.build
  2. Implements the Distutils 'build' command."""
  3. import sys
  4. import os
  5. from ..core import Command
  6. from ..errors import DistutilsOptionError
  7. from ..util import get_platform
  8. def show_compilers():
  9. from ..ccompiler import show_compilers
  10. show_compilers()
  11. class build(Command):
  12. description = "build everything needed to install"
  13. user_options = [
  14. ('build-base=', 'b', "base directory for build library"),
  15. ('build-purelib=', None, "build directory for platform-neutral distributions"),
  16. ('build-platlib=', None, "build directory for platform-specific distributions"),
  17. (
  18. 'build-lib=',
  19. None,
  20. "build directory for all distribution (defaults to either "
  21. + "build-purelib or build-platlib",
  22. ),
  23. ('build-scripts=', None, "build directory for scripts"),
  24. ('build-temp=', 't', "temporary build directory"),
  25. (
  26. 'plat-name=',
  27. 'p',
  28. "platform name to build for, if supported "
  29. "(default: %s)" % 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 = ['debug', 'force']
  38. help_options = [
  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 = 0
  54. self.executable = None
  55. self.parallel = None
  56. def finalize_options(self): # 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 = ".{}-{}".format(self.plat_name, sys.implementation.cache_tag)
  69. # Make it so Python 2.x and Python 2.x with --with-pydebug don't
  70. # share the same build directories. Doing so confuses the build
  71. # process for C modules
  72. if hasattr(sys, 'gettotalrefcount'):
  73. plat_specifier += '-pydebug'
  74. # 'build_purelib' and 'build_platlib' just default to 'lib' and
  75. # 'lib.<plat>' under the base build directory. We only use one of
  76. # them for a given distribution, though --
  77. if self.build_purelib is None:
  78. self.build_purelib = os.path.join(self.build_base, 'lib')
  79. if self.build_platlib is None:
  80. self.build_platlib = os.path.join(self.build_base, 'lib' + plat_specifier)
  81. # 'build_lib' is the actual directory that we will use for this
  82. # particular module distribution -- if user didn't supply it, pick
  83. # one of 'build_purelib' or 'build_platlib'.
  84. if self.build_lib is None:
  85. if self.distribution.has_ext_modules():
  86. self.build_lib = self.build_platlib
  87. else:
  88. self.build_lib = self.build_purelib
  89. # 'build_temp' -- temporary directory for compiler turds,
  90. # "build/temp.<plat>"
  91. if self.build_temp is None:
  92. self.build_temp = os.path.join(self.build_base, 'temp' + plat_specifier)
  93. if self.build_scripts is None:
  94. self.build_scripts = os.path.join(
  95. self.build_base, 'scripts-%d.%d' % sys.version_info[:2]
  96. )
  97. if self.executable is None and sys.executable:
  98. self.executable = os.path.normpath(sys.executable)
  99. if isinstance(self.parallel, str):
  100. try:
  101. self.parallel = int(self.parallel)
  102. except ValueError:
  103. raise DistutilsOptionError("parallel should be an integer")
  104. def run(self):
  105. # Run all relevant sub-commands. This will be some subset of:
  106. # - build_py - pure Python modules
  107. # - build_clib - standalone C libraries
  108. # - build_ext - Python extensions
  109. # - build_scripts - (Python) scripts
  110. for cmd_name in self.get_sub_commands():
  111. self.run_command(cmd_name)
  112. # -- Predicates for the sub-command list ---------------------------
  113. def has_pure_modules(self):
  114. return self.distribution.has_pure_modules()
  115. def has_c_libraries(self):
  116. return self.distribution.has_c_libraries()
  117. def has_ext_modules(self):
  118. return self.distribution.has_ext_modules()
  119. def has_scripts(self):
  120. return self.distribution.has_scripts()
  121. sub_commands = [
  122. ('build_py', has_pure_modules),
  123. ('build_clib', has_c_libraries),
  124. ('build_ext', has_ext_modules),
  125. ('build_scripts', has_scripts),
  126. ]