extension.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import re
  2. import functools
  3. import distutils.core
  4. import distutils.errors
  5. import distutils.extension
  6. from .monkey import get_unpatched
  7. def _have_cython():
  8. """
  9. Return True if Cython can be imported.
  10. """
  11. cython_impl = 'Cython.Distutils.build_ext'
  12. try:
  13. # from (cython_impl) import build_ext
  14. __import__(cython_impl, fromlist=['build_ext']).build_ext
  15. return True
  16. except Exception:
  17. pass
  18. return False
  19. # for compatibility
  20. have_pyrex = _have_cython
  21. _Extension = get_unpatched(distutils.core.Extension)
  22. class Extension(_Extension):
  23. """
  24. Describes a single extension module.
  25. This means that all source files will be compiled into a single binary file
  26. ``<module path>.<suffix>`` (with ``<module path>`` derived from ``name`` and
  27. ``<suffix>`` defined by one of the values in
  28. ``importlib.machinery.EXTENSION_SUFFIXES``).
  29. In the case ``.pyx`` files are passed as ``sources and`` ``Cython`` is **not**
  30. installed in the build environment, ``setuptools`` may also try to look for the
  31. equivalent ``.cpp`` or ``.c`` files.
  32. :arg str name:
  33. the full name of the extension, including any packages -- ie.
  34. *not* a filename or pathname, but Python dotted name
  35. :arg list[str] sources:
  36. list of source filenames, relative to the distribution root
  37. (where the setup script lives), in Unix form (slash-separated)
  38. for portability. Source files may be C, C++, SWIG (.i),
  39. platform-specific resource files, or whatever else is recognized
  40. by the "build_ext" command as source for a Python extension.
  41. :keyword list[str] include_dirs:
  42. list of directories to search for C/C++ header files (in Unix
  43. form for portability)
  44. :keyword list[tuple[str, str|None]] define_macros:
  45. list of macros to define; each macro is defined using a 2-tuple:
  46. the first item corresponding to the name of the macro and the second
  47. item either a string with its value or None to
  48. define it without a particular value (equivalent of "#define
  49. FOO" in source or -DFOO on Unix C compiler command line)
  50. :keyword list[str] undef_macros:
  51. list of macros to undefine explicitly
  52. :keyword list[str] library_dirs:
  53. list of directories to search for C/C++ libraries at link time
  54. :keyword list[str] libraries:
  55. list of library names (not filenames or paths) to link against
  56. :keyword list[str] runtime_library_dirs:
  57. list of directories to search for C/C++ libraries at run time
  58. (for shared extensions, this is when the extension is loaded).
  59. Setting this will cause an exception during build on Windows
  60. platforms.
  61. :keyword list[str] extra_objects:
  62. list of extra files to link with (eg. object files not implied
  63. by 'sources', static library that must be explicitly specified,
  64. binary resource files, etc.)
  65. :keyword list[str] extra_compile_args:
  66. any extra platform- and compiler-specific information to use
  67. when compiling the source files in 'sources'. For platforms and
  68. compilers where "command line" makes sense, this is typically a
  69. list of command-line arguments, but for other platforms it could
  70. be anything.
  71. :keyword list[str] extra_link_args:
  72. any extra platform- and compiler-specific information to use
  73. when linking object files together to create the extension (or
  74. to create a new static Python interpreter). Similar
  75. interpretation as for 'extra_compile_args'.
  76. :keyword list[str] export_symbols:
  77. list of symbols to be exported from a shared extension. Not
  78. used on all platforms, and not generally necessary for Python
  79. extensions, which typically export exactly one symbol: "init" +
  80. extension_name.
  81. :keyword list[str] swig_opts:
  82. any extra options to pass to SWIG if a source file has the .i
  83. extension.
  84. :keyword list[str] depends:
  85. list of files that the extension depends on
  86. :keyword str language:
  87. extension language (i.e. "c", "c++", "objc"). Will be detected
  88. from the source extensions if not provided.
  89. :keyword bool optional:
  90. specifies that a build failure in the extension should not abort the
  91. build process, but simply not install the failing extension.
  92. :keyword bool py_limited_api:
  93. opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`.
  94. :raises setuptools.errors.PlatformError: if 'runtime_library_dirs' is
  95. specified on Windows. (since v63)
  96. """
  97. def __init__(self, name, sources, *args, **kw):
  98. # The *args is needed for compatibility as calls may use positional
  99. # arguments. py_limited_api may be set only via keyword.
  100. self.py_limited_api = kw.pop("py_limited_api", False)
  101. super().__init__(name, sources, *args, **kw)
  102. def _convert_pyx_sources_to_lang(self):
  103. """
  104. Replace sources with .pyx extensions to sources with the target
  105. language extension. This mechanism allows language authors to supply
  106. pre-converted sources but to prefer the .pyx sources.
  107. """
  108. if _have_cython():
  109. # the build has Cython, so allow it to compile the .pyx files
  110. return
  111. lang = self.language or ''
  112. target_ext = '.cpp' if lang.lower() == 'c++' else '.c'
  113. sub = functools.partial(re.sub, '.pyx$', target_ext)
  114. self.sources = list(map(sub, self.sources))
  115. class Library(Extension):
  116. """Just like a regular Extension, but built as a library instead"""