build_clib.py 7.8 KB

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