build_clib.py 7.5 KB

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