build.py 5.3 KB

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