build_clib.py 7.6 KB

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