/Lib/distutils/command/build_clib.py

http://unladen-swallow.googlecode.com/ · Python · 238 lines · 182 code · 31 blank · 25 comment · 10 complexity · 4dffb3e46233296af854fcf0b2b124df MD5 · raw file

  1. """distutils.command.build_clib
  2. Implements the Distutils 'build_clib' command, to build a C/C++ library
  3. that is included in the module distribution and needed by an extension
  4. module."""
  5. # This module should be kept compatible with Python 2.1.
  6. __revision__ = "$Id: build_clib.py 37828 2004-11-10 22:23:15Z loewis $"
  7. # XXX this module has *lots* of code ripped-off quite transparently from
  8. # build_ext.py -- not surprisingly really, as the work required to build
  9. # a static library from a collection of C source files is not really all
  10. # that different from what's required to build a shared object file from
  11. # a collection of C source files. Nevertheless, I haven't done the
  12. # necessary refactoring to account for the overlap in code between the
  13. # two modules, mainly because a number of subtle details changed in the
  14. # cut 'n paste. Sigh.
  15. import os, string
  16. from types import *
  17. from distutils.core import Command
  18. from distutils.errors import *
  19. from distutils.sysconfig import customize_compiler
  20. from distutils import log
  21. def show_compilers ():
  22. from distutils.ccompiler import show_compilers
  23. show_compilers()
  24. class build_clib (Command):
  25. description = "build C/C++ libraries used by Python extensions"
  26. user_options = [
  27. ('build-clib', 'b',
  28. "directory to build C/C++ libraries to"),
  29. ('build-temp', 't',
  30. "directory to put temporary build by-products"),
  31. ('debug', 'g',
  32. "compile with debugging information"),
  33. ('force', 'f',
  34. "forcibly build everything (ignore file timestamps)"),
  35. ('compiler=', 'c',
  36. "specify the compiler type"),
  37. ]
  38. boolean_options = ['debug', 'force']
  39. help_options = [
  40. ('help-compiler', None,
  41. "list available compilers", show_compilers),
  42. ]
  43. def initialize_options (self):
  44. self.build_clib = None
  45. self.build_temp = None
  46. # List of libraries to build
  47. self.libraries = None
  48. # Compilation options for all libraries
  49. self.include_dirs = None
  50. self.define = None
  51. self.undef = None
  52. self.debug = None
  53. self.force = 0
  54. self.compiler = None
  55. # initialize_options()
  56. def finalize_options (self):
  57. # This might be confusing: both build-clib and build-temp default
  58. # to build-temp as defined by the "build" command. This is because
  59. # I think that C libraries are really just temporary build
  60. # by-products, at least from the point of view of building Python
  61. # extensions -- but I want to keep my options open.
  62. self.set_undefined_options('build',
  63. ('build_temp', 'build_clib'),
  64. ('build_temp', 'build_temp'),
  65. ('compiler', 'compiler'),
  66. ('debug', 'debug'),
  67. ('force', 'force'))
  68. self.libraries = self.distribution.libraries
  69. if self.libraries:
  70. self.check_library_list(self.libraries)
  71. if self.include_dirs is None:
  72. self.include_dirs = self.distribution.include_dirs or []
  73. if type(self.include_dirs) is StringType:
  74. self.include_dirs = string.split(self.include_dirs,
  75. os.pathsep)
  76. # XXX same as for build_ext -- what about 'self.define' and
  77. # 'self.undef' ?
  78. # finalize_options()
  79. def run (self):
  80. if not self.libraries:
  81. return
  82. # Yech -- this is cut 'n pasted from build_ext.py!
  83. from distutils.ccompiler import new_compiler
  84. self.compiler = new_compiler(compiler=self.compiler,
  85. dry_run=self.dry_run,
  86. force=self.force)
  87. customize_compiler(self.compiler)
  88. if self.include_dirs is not None:
  89. self.compiler.set_include_dirs(self.include_dirs)
  90. if self.define is not None:
  91. # 'define' option is a list of (name,value) tuples
  92. for (name,value) in self.define:
  93. self.compiler.define_macro(name, value)
  94. if self.undef is not None:
  95. for macro in self.undef:
  96. self.compiler.undefine_macro(macro)
  97. self.build_libraries(self.libraries)
  98. # run()
  99. def check_library_list (self, libraries):
  100. """Ensure that the list of libraries (presumably provided as a
  101. command option 'libraries') is valid, i.e. it is a list of
  102. 2-tuples, where the tuples are (library_name, build_info_dict).
  103. Raise DistutilsSetupError if the structure is invalid anywhere;
  104. just returns otherwise."""
  105. # Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
  106. # with only names changed to protect the innocent!
  107. if type(libraries) is not ListType:
  108. raise DistutilsSetupError, \
  109. "'libraries' option must be a list of tuples"
  110. for lib in libraries:
  111. if type(lib) is not TupleType and len(lib) != 2:
  112. raise DistutilsSetupError, \
  113. "each element of 'libraries' must a 2-tuple"
  114. if type(lib[0]) is not StringType:
  115. raise DistutilsSetupError, \
  116. "first element of each tuple in 'libraries' " + \
  117. "must be a string (the library name)"
  118. if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
  119. raise DistutilsSetupError, \
  120. ("bad library name '%s': " +
  121. "may not contain directory separators") % \
  122. lib[0]
  123. if type(lib[1]) is not DictionaryType:
  124. raise DistutilsSetupError, \
  125. "second element of each tuple in 'libraries' " + \
  126. "must be a dictionary (build info)"
  127. # for lib
  128. # check_library_list ()
  129. def get_library_names (self):
  130. # Assume the library list is valid -- 'check_library_list()' is
  131. # called from 'finalize_options()', so it should be!
  132. if not self.libraries:
  133. return None
  134. lib_names = []
  135. for (lib_name, build_info) in self.libraries:
  136. lib_names.append(lib_name)
  137. return lib_names
  138. # get_library_names ()
  139. def get_source_files (self):
  140. self.check_library_list(self.libraries)
  141. filenames = []
  142. for (lib_name, build_info) in self.libraries:
  143. sources = build_info.get('sources')
  144. if (sources is None or
  145. type(sources) not in (ListType, TupleType) ):
  146. raise DistutilsSetupError, \
  147. ("in 'libraries' option (library '%s'), "
  148. "'sources' must be present and must be "
  149. "a list of source filenames") % lib_name
  150. filenames.extend(sources)
  151. return filenames
  152. # get_source_files ()
  153. def build_libraries (self, libraries):
  154. for (lib_name, build_info) in libraries:
  155. sources = build_info.get('sources')
  156. if sources is None or type(sources) not in (ListType, TupleType):
  157. raise DistutilsSetupError, \
  158. ("in 'libraries' option (library '%s'), " +
  159. "'sources' must be present and must be " +
  160. "a list of source filenames") % lib_name
  161. sources = list(sources)
  162. log.info("building '%s' library", lib_name)
  163. # First, compile the source code to object files in the library
  164. # directory. (This should probably change to putting object
  165. # files in a temporary build directory.)
  166. macros = build_info.get('macros')
  167. include_dirs = build_info.get('include_dirs')
  168. objects = self.compiler.compile(sources,
  169. output_dir=self.build_temp,
  170. macros=macros,
  171. include_dirs=include_dirs,
  172. debug=self.debug)
  173. # Now "link" the object files together into a static library.
  174. # (On Unix at least, this isn't really linking -- it just
  175. # builds an archive. Whatever.)
  176. self.compiler.create_static_lib(objects, lib_name,
  177. output_dir=self.build_clib,
  178. debug=self.debug)
  179. # for libraries
  180. # build_libraries ()
  181. # class build_lib