annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/setuptools/extension.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 from __future__ import annotations
jpayne@68 2
jpayne@68 3 import functools
jpayne@68 4 import re
jpayne@68 5 from typing import TYPE_CHECKING
jpayne@68 6
jpayne@68 7 from setuptools._path import StrPath
jpayne@68 8
jpayne@68 9 from .monkey import get_unpatched
jpayne@68 10
jpayne@68 11 import distutils.core
jpayne@68 12 import distutils.errors
jpayne@68 13 import distutils.extension
jpayne@68 14
jpayne@68 15
jpayne@68 16 def _have_cython():
jpayne@68 17 """
jpayne@68 18 Return True if Cython can be imported.
jpayne@68 19 """
jpayne@68 20 cython_impl = 'Cython.Distutils.build_ext'
jpayne@68 21 try:
jpayne@68 22 # from (cython_impl) import build_ext
jpayne@68 23 __import__(cython_impl, fromlist=['build_ext']).build_ext
jpayne@68 24 except Exception:
jpayne@68 25 return False
jpayne@68 26 return True
jpayne@68 27
jpayne@68 28
jpayne@68 29 # for compatibility
jpayne@68 30 have_pyrex = _have_cython
jpayne@68 31 if TYPE_CHECKING:
jpayne@68 32 # Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
jpayne@68 33 from distutils.core import Extension as _Extension
jpayne@68 34 else:
jpayne@68 35 _Extension = get_unpatched(distutils.core.Extension)
jpayne@68 36
jpayne@68 37
jpayne@68 38 class Extension(_Extension):
jpayne@68 39 """
jpayne@68 40 Describes a single extension module.
jpayne@68 41
jpayne@68 42 This means that all source files will be compiled into a single binary file
jpayne@68 43 ``<module path>.<suffix>`` (with ``<module path>`` derived from ``name`` and
jpayne@68 44 ``<suffix>`` defined by one of the values in
jpayne@68 45 ``importlib.machinery.EXTENSION_SUFFIXES``).
jpayne@68 46
jpayne@68 47 In the case ``.pyx`` files are passed as ``sources and`` ``Cython`` is **not**
jpayne@68 48 installed in the build environment, ``setuptools`` may also try to look for the
jpayne@68 49 equivalent ``.cpp`` or ``.c`` files.
jpayne@68 50
jpayne@68 51 :arg str name:
jpayne@68 52 the full name of the extension, including any packages -- ie.
jpayne@68 53 *not* a filename or pathname, but Python dotted name
jpayne@68 54
jpayne@68 55 :arg list[str|os.PathLike[str]] sources:
jpayne@68 56 list of source filenames, relative to the distribution root
jpayne@68 57 (where the setup script lives), in Unix form (slash-separated)
jpayne@68 58 for portability. Source files may be C, C++, SWIG (.i),
jpayne@68 59 platform-specific resource files, or whatever else is recognized
jpayne@68 60 by the "build_ext" command as source for a Python extension.
jpayne@68 61
jpayne@68 62 :keyword list[str] include_dirs:
jpayne@68 63 list of directories to search for C/C++ header files (in Unix
jpayne@68 64 form for portability)
jpayne@68 65
jpayne@68 66 :keyword list[tuple[str, str|None]] define_macros:
jpayne@68 67 list of macros to define; each macro is defined using a 2-tuple:
jpayne@68 68 the first item corresponding to the name of the macro and the second
jpayne@68 69 item either a string with its value or None to
jpayne@68 70 define it without a particular value (equivalent of "#define
jpayne@68 71 FOO" in source or -DFOO on Unix C compiler command line)
jpayne@68 72
jpayne@68 73 :keyword list[str] undef_macros:
jpayne@68 74 list of macros to undefine explicitly
jpayne@68 75
jpayne@68 76 :keyword list[str] library_dirs:
jpayne@68 77 list of directories to search for C/C++ libraries at link time
jpayne@68 78
jpayne@68 79 :keyword list[str] libraries:
jpayne@68 80 list of library names (not filenames or paths) to link against
jpayne@68 81
jpayne@68 82 :keyword list[str] runtime_library_dirs:
jpayne@68 83 list of directories to search for C/C++ libraries at run time
jpayne@68 84 (for shared extensions, this is when the extension is loaded).
jpayne@68 85 Setting this will cause an exception during build on Windows
jpayne@68 86 platforms.
jpayne@68 87
jpayne@68 88 :keyword list[str] extra_objects:
jpayne@68 89 list of extra files to link with (eg. object files not implied
jpayne@68 90 by 'sources', static library that must be explicitly specified,
jpayne@68 91 binary resource files, etc.)
jpayne@68 92
jpayne@68 93 :keyword list[str] extra_compile_args:
jpayne@68 94 any extra platform- and compiler-specific information to use
jpayne@68 95 when compiling the source files in 'sources'. For platforms and
jpayne@68 96 compilers where "command line" makes sense, this is typically a
jpayne@68 97 list of command-line arguments, but for other platforms it could
jpayne@68 98 be anything.
jpayne@68 99
jpayne@68 100 :keyword list[str] extra_link_args:
jpayne@68 101 any extra platform- and compiler-specific information to use
jpayne@68 102 when linking object files together to create the extension (or
jpayne@68 103 to create a new static Python interpreter). Similar
jpayne@68 104 interpretation as for 'extra_compile_args'.
jpayne@68 105
jpayne@68 106 :keyword list[str] export_symbols:
jpayne@68 107 list of symbols to be exported from a shared extension. Not
jpayne@68 108 used on all platforms, and not generally necessary for Python
jpayne@68 109 extensions, which typically export exactly one symbol: "init" +
jpayne@68 110 extension_name.
jpayne@68 111
jpayne@68 112 :keyword list[str] swig_opts:
jpayne@68 113 any extra options to pass to SWIG if a source file has the .i
jpayne@68 114 extension.
jpayne@68 115
jpayne@68 116 :keyword list[str] depends:
jpayne@68 117 list of files that the extension depends on
jpayne@68 118
jpayne@68 119 :keyword str language:
jpayne@68 120 extension language (i.e. "c", "c++", "objc"). Will be detected
jpayne@68 121 from the source extensions if not provided.
jpayne@68 122
jpayne@68 123 :keyword bool optional:
jpayne@68 124 specifies that a build failure in the extension should not abort the
jpayne@68 125 build process, but simply not install the failing extension.
jpayne@68 126
jpayne@68 127 :keyword bool py_limited_api:
jpayne@68 128 opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`.
jpayne@68 129
jpayne@68 130 :raises setuptools.errors.PlatformError: if ``runtime_library_dirs`` is
jpayne@68 131 specified on Windows. (since v63)
jpayne@68 132 """
jpayne@68 133
jpayne@68 134 # These 4 are set and used in setuptools/command/build_ext.py
jpayne@68 135 # The lack of a default value and risk of `AttributeError` is purposeful
jpayne@68 136 # to avoid people forgetting to call finalize_options if they modify the extension list.
jpayne@68 137 # See example/rationale in https://github.com/pypa/setuptools/issues/4529.
jpayne@68 138 _full_name: str #: Private API, internal use only.
jpayne@68 139 _links_to_dynamic: bool #: Private API, internal use only.
jpayne@68 140 _needs_stub: bool #: Private API, internal use only.
jpayne@68 141 _file_name: str #: Private API, internal use only.
jpayne@68 142
jpayne@68 143 def __init__(
jpayne@68 144 self,
jpayne@68 145 name: str,
jpayne@68 146 sources: list[StrPath],
jpayne@68 147 *args,
jpayne@68 148 py_limited_api: bool = False,
jpayne@68 149 **kw,
jpayne@68 150 ):
jpayne@68 151 # The *args is needed for compatibility as calls may use positional
jpayne@68 152 # arguments. py_limited_api may be set only via keyword.
jpayne@68 153 self.py_limited_api = py_limited_api
jpayne@68 154 super().__init__(
jpayne@68 155 name,
jpayne@68 156 sources, # type: ignore[arg-type] # Vendored version of setuptools supports PathLike
jpayne@68 157 *args,
jpayne@68 158 **kw,
jpayne@68 159 )
jpayne@68 160
jpayne@68 161 def _convert_pyx_sources_to_lang(self):
jpayne@68 162 """
jpayne@68 163 Replace sources with .pyx extensions to sources with the target
jpayne@68 164 language extension. This mechanism allows language authors to supply
jpayne@68 165 pre-converted sources but to prefer the .pyx sources.
jpayne@68 166 """
jpayne@68 167 if _have_cython():
jpayne@68 168 # the build has Cython, so allow it to compile the .pyx files
jpayne@68 169 return
jpayne@68 170 lang = self.language or ''
jpayne@68 171 target_ext = '.cpp' if lang.lower() == 'c++' else '.c'
jpayne@68 172 sub = functools.partial(re.sub, '.pyx$', target_ext)
jpayne@68 173 self.sources = list(map(sub, self.sources))
jpayne@68 174
jpayne@68 175
jpayne@68 176 class Library(Extension):
jpayne@68 177 """Just like a regular Extension, but built as a library instead"""