build.py 5.5 KB

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