extension.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """distutils.extension
  2. Provides the Extension class, used to describe C/C++ extension
  3. modules in setup scripts."""
  4. from __future__ import annotations
  5. import os
  6. import warnings
  7. from collections.abc import Iterable
  8. # This class is really only used by the "build_ext" command, so it might
  9. # make sense to put it in distutils.command.build_ext. However, that
  10. # module is already big enough, and I want to make this class a bit more
  11. # complex to simplify some common cases ("foo" module in "foo.c") and do
  12. # better error-checking ("foo.c" actually exists).
  13. #
  14. # Also, putting this in build_ext.py means every setup script would have to
  15. # import that large-ish module (indirectly, through distutils.core) in
  16. # order to do anything.
  17. class Extension:
  18. """Just a collection of attributes that describes an extension
  19. module and everything needed to build it (hopefully in a portable
  20. way, but there are hooks that let you be as unportable as you need).
  21. Instance attributes:
  22. name : string
  23. the full name of the extension, including any packages -- ie.
  24. *not* a filename or pathname, but Python dotted name
  25. sources : Iterable[string | os.PathLike]
  26. iterable of source filenames (except strings, which could be misinterpreted
  27. as a single filename), relative to the distribution root (where the setup
  28. script lives), in Unix form (slash-separated) for portability. Can be any
  29. non-string iterable (list, tuple, set, etc.) containing strings or
  30. PathLike objects. Source files may be C, C++, SWIG (.i), platform-specific
  31. resource files, or whatever else is recognized by the "build_ext" command
  32. as source for a Python extension.
  33. include_dirs : [string]
  34. list of directories to search for C/C++ header files (in Unix
  35. form for portability)
  36. define_macros : [(name : string, value : string|None)]
  37. list of macros to define; each macro is defined using a 2-tuple,
  38. where 'value' is either the string to define it to or None to
  39. define it without a particular value (equivalent of "#define
  40. FOO" in source or -DFOO on Unix C compiler command line)
  41. undef_macros : [string]
  42. list of macros to undefine explicitly
  43. library_dirs : [string]
  44. list of directories to search for C/C++ libraries at link time
  45. libraries : [string]
  46. list of library names (not filenames or paths) to link against
  47. runtime_library_dirs : [string]
  48. list of directories to search for C/C++ libraries at run time
  49. (for shared extensions, this is when the extension is loaded)
  50. extra_objects : [string]
  51. list of extra files to link with (eg. object files not implied
  52. by 'sources', static library that must be explicitly specified,
  53. binary resource files, etc.)
  54. extra_compile_args : [string]
  55. any extra platform- and compiler-specific information to use
  56. when compiling the source files in 'sources'. For platforms and
  57. compilers where "command line" makes sense, this is typically a
  58. list of command-line arguments, but for other platforms it could
  59. be anything.
  60. extra_link_args : [string]
  61. any extra platform- and compiler-specific information to use
  62. when linking object files together to create the extension (or
  63. to create a new static Python interpreter). Similar
  64. interpretation as for 'extra_compile_args'.
  65. export_symbols : [string]
  66. list of symbols to be exported from a shared extension. Not
  67. used on all platforms, and not generally necessary for Python
  68. extensions, which typically export exactly one symbol: "init" +
  69. extension_name.
  70. swig_opts : [string]
  71. any extra options to pass to SWIG if a source file has the .i
  72. extension.
  73. depends : [string]
  74. list of files that the extension depends on
  75. language : string
  76. extension language (i.e. "c", "c++", "objc"). Will be detected
  77. from the source extensions if not provided.
  78. optional : boolean
  79. specifies that a build failure in the extension should not abort the
  80. build process, but simply not install the failing extension.
  81. """
  82. # When adding arguments to this constructor, be sure to update
  83. # setup_keywords in core.py.
  84. def __init__(
  85. self,
  86. name: str,
  87. sources: Iterable[str | os.PathLike[str]],
  88. include_dirs: list[str] | None = None,
  89. define_macros: list[tuple[str, str | None]] | None = None,
  90. undef_macros: list[str] | None = None,
  91. library_dirs: list[str] | None = None,
  92. libraries: list[str] | None = None,
  93. runtime_library_dirs: list[str] | None = None,
  94. extra_objects: list[str] | None = None,
  95. extra_compile_args: list[str] | None = None,
  96. extra_link_args: list[str] | None = None,
  97. export_symbols: list[str] | None = None,
  98. swig_opts: list[str] | None = None,
  99. depends: list[str] | None = None,
  100. language: str | None = None,
  101. optional: bool | None = None,
  102. **kw, # To catch unknown keywords
  103. ):
  104. if not isinstance(name, str):
  105. raise TypeError("'name' must be a string")
  106. # handle the string case first; since strings are iterable, disallow them
  107. if isinstance(sources, str):
  108. raise TypeError(
  109. "'sources' must be an iterable of strings or PathLike objects, not a string"
  110. )
  111. # now we check if it's iterable and contains valid types
  112. try:
  113. self.sources = list(map(os.fspath, sources))
  114. except TypeError:
  115. raise TypeError(
  116. "'sources' must be an iterable of strings or PathLike objects"
  117. )
  118. self.name = name
  119. self.include_dirs = include_dirs or []
  120. self.define_macros = define_macros or []
  121. self.undef_macros = undef_macros or []
  122. self.library_dirs = library_dirs or []
  123. self.libraries = libraries or []
  124. self.runtime_library_dirs = runtime_library_dirs or []
  125. self.extra_objects = extra_objects or []
  126. self.extra_compile_args = extra_compile_args or []
  127. self.extra_link_args = extra_link_args or []
  128. self.export_symbols = export_symbols or []
  129. self.swig_opts = swig_opts or []
  130. self.depends = depends or []
  131. self.language = language
  132. self.optional = optional
  133. # If there are unknown keyword options, warn about them
  134. if len(kw) > 0:
  135. options = [repr(option) for option in kw]
  136. options = ', '.join(sorted(options))
  137. msg = f"Unknown Extension options: {options}"
  138. warnings.warn(msg)
  139. def __repr__(self):
  140. return f'<{self.__class__.__module__}.{self.__class__.__qualname__}({self.name!r}) at {id(self):#x}>'
  141. def read_setup_file(filename): # noqa: C901
  142. """Reads a Setup file and returns Extension instances."""
  143. from distutils.sysconfig import _variable_rx, expand_makefile_vars, parse_makefile
  144. from distutils.text_file import TextFile
  145. from distutils.util import split_quoted
  146. # First pass over the file to gather "VAR = VALUE" assignments.
  147. vars = parse_makefile(filename)
  148. # Second pass to gobble up the real content: lines of the form
  149. # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
  150. file = TextFile(
  151. filename,
  152. strip_comments=True,
  153. skip_blanks=True,
  154. join_lines=True,
  155. lstrip_ws=True,
  156. rstrip_ws=True,
  157. )
  158. try:
  159. extensions = []
  160. while True:
  161. line = file.readline()
  162. if line is None: # eof
  163. break
  164. if _variable_rx.match(line): # VAR=VALUE, handled in first pass
  165. continue
  166. if line[0] == line[-1] == "*":
  167. file.warn(f"'{line}' lines not handled yet")
  168. continue
  169. line = expand_makefile_vars(line, vars)
  170. words = split_quoted(line)
  171. # NB. this parses a slightly different syntax than the old
  172. # makesetup script: here, there must be exactly one extension per
  173. # line, and it must be the first word of the line. I have no idea
  174. # why the old syntax supported multiple extensions per line, as
  175. # they all wind up being the same.
  176. module = words[0]
  177. ext = Extension(module, [])
  178. append_next_word = None
  179. for word in words[1:]:
  180. if append_next_word is not None:
  181. append_next_word.append(word)
  182. append_next_word = None
  183. continue
  184. suffix = os.path.splitext(word)[1]
  185. switch = word[0:2]
  186. value = word[2:]
  187. if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
  188. # hmm, should we do something about C vs. C++ sources?
  189. # or leave it up to the CCompiler implementation to
  190. # worry about?
  191. ext.sources.append(word)
  192. elif switch == "-I":
  193. ext.include_dirs.append(value)
  194. elif switch == "-D":
  195. equals = value.find("=")
  196. if equals == -1: # bare "-DFOO" -- no value
  197. ext.define_macros.append((value, None))
  198. else: # "-DFOO=blah"
  199. ext.define_macros.append((value[0:equals], value[equals + 2 :]))
  200. elif switch == "-U":
  201. ext.undef_macros.append(value)
  202. elif switch == "-C": # only here 'cause makesetup has it!
  203. ext.extra_compile_args.append(word)
  204. elif switch == "-l":
  205. ext.libraries.append(value)
  206. elif switch == "-L":
  207. ext.library_dirs.append(value)
  208. elif switch == "-R":
  209. ext.runtime_library_dirs.append(value)
  210. elif word == "-rpath":
  211. append_next_word = ext.runtime_library_dirs
  212. elif word == "-Xlinker":
  213. append_next_word = ext.extra_link_args
  214. elif word == "-Xcompiler":
  215. append_next_word = ext.extra_compile_args
  216. elif switch == "-u":
  217. ext.extra_link_args.append(word)
  218. if not value:
  219. append_next_word = ext.extra_link_args
  220. elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
  221. # NB. a really faithful emulation of makesetup would
  222. # append a .o file to extra_objects only if it
  223. # had a slash in it; otherwise, it would s/.o/.c/
  224. # and append it to sources. Hmmmm.
  225. ext.extra_objects.append(word)
  226. else:
  227. file.warn(f"unrecognized argument '{word}'")
  228. extensions.append(ext)
  229. finally:
  230. file.close()
  231. return extensions