build.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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',
  14. "base directory for build library"),
  15. ('build-purelib=', None,
  16. "build directory for platform-neutral distributions"),
  17. ('build-platlib=', None,
  18. "build directory for platform-specific distributions"),
  19. ('build-lib=', None,
  20. "build directory for all distribution (defaults to either " +
  21. "build-purelib or build-platlib"),
  22. ('build-scripts=', None,
  23. "build directory for scripts"),
  24. ('build-temp=', 't',
  25. "temporary build directory"),
  26. ('plat-name=', 'p',
  27. "platform name to build for, if supported "
  28. "(default: %s)" % get_platform()),
  29. ('compiler=', 'c',
  30. "specify the compiler type"),
  31. ('parallel=', 'j',
  32. "number of parallel build jobs"),
  33. ('debug', 'g',
  34. "compile extensions and libraries with debugging information"),
  35. ('force', 'f',
  36. "forcibly build everything (ignore file timestamps)"),
  37. ('executable=', 'e',
  38. "specify final destination interpreter path (build.py)"),
  39. ]
  40. boolean_options = ['debug', 'force']
  41. help_options = [
  42. ('help-compiler', None,
  43. "list available compilers", show_compilers),
  44. ]
  45. def initialize_options(self):
  46. self.build_base = 'build'
  47. # these are decided only after 'build_base' has its final value
  48. # (unless overridden by the user or client)
  49. self.build_purelib = None
  50. self.build_platlib = None
  51. self.build_lib = None
  52. self.build_temp = None
  53. self.build_scripts = None
  54. self.compiler = None
  55. self.plat_name = None
  56. self.debug = None
  57. self.force = 0
  58. self.executable = None
  59. self.parallel = None
  60. def finalize_options(self):
  61. if self.plat_name is None:
  62. self.plat_name = get_platform()
  63. else:
  64. # plat-name only supported for windows (other platforms are
  65. # supported via ./configure flags, if at all). Avoid misleading
  66. # other platforms.
  67. if os.name != 'nt':
  68. raise DistutilsOptionError(
  69. "--plat-name only supported on Windows (try "
  70. "using './configure --help' on your platform)")
  71. plat_specifier = ".%s-%d.%d" % (self.plat_name, *sys.version_info[:2])
  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,
  84. 'lib' + plat_specifier)
  85. # 'build_lib' is the actual directory that we will use for this
  86. # particular module distribution -- if user didn't supply it, pick
  87. # one of 'build_purelib' or 'build_platlib'.
  88. if self.build_lib is None:
  89. if self.distribution.has_ext_modules():
  90. self.build_lib = self.build_platlib
  91. else:
  92. self.build_lib = self.build_purelib
  93. # 'build_temp' -- temporary directory for compiler turds,
  94. # "build/temp.<plat>"
  95. if self.build_temp is None:
  96. self.build_temp = os.path.join(self.build_base,
  97. 'temp' + plat_specifier)
  98. if self.build_scripts is None:
  99. self.build_scripts = os.path.join(self.build_base,
  100. 'scripts-%d.%d' % sys.version_info[:2])
  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):
  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 = [('build_py', has_pure_modules),
  126. ('build_clib', has_c_libraries),
  127. ('build_ext', has_ext_modules),
  128. ('build_scripts', has_scripts),
  129. ]