Debian Codegraph @

docs/using.rst

raw
1 Using extension-helpers
2 =======================
3
4 To use extension-helpers in your package, you will need to make sure your
5 package uses a ``pyproject.toml`` file as described in `PEP 518
6 <https://www.python.org/dev/peps/pep-0518/>`_.
7
8 You can then add extension-helpers to the build-time dependencies in your
9 ``pyproject.toml`` file::
10
11 [build-system]
12 requires = ["setuptools",
13 "wheel",
14 "extension-helpers==1.*"]
15
16 If you have Cython extensions, you will need to make sure ``cython`` is included
17 in the above list too.
18
19 .. note:: It is highly recommended to pin the version of extension-helpers
20 to a major version, such as ``1.*``, since extension-helpers uses
21 `semantic versioning <https://semver.org>`_
22 and there will therefore likely be breaking changes when the major version is bumped.
23 If you do not specify any pinning, then old versions of your package that are already
24 on PyPI may no longer be installable on source without disabling the build isolation
25 and installing build dependencies manually.
26
27 The main functionality in extension-helpers is the
28 :func:`~extension_helpers.get_extensions` function which can be
29 used to collect package extensions. Defining functions is then done in two ways:
30
31 * For simple Cython extensions, :func:`~extension_helpers.get_extensions`
32 will automatically generate extension modules with no further work.
33
34 * For other extensions, you can create ``setup_package.py`` files anywhere
35 in your package, and these files can then include a ``get_extensions``
36 function that returns a list of :class:`setuptools.Extension` objects.
37
38 In the second case, the idea is that for large packages, extensions can be defined
39 in the relevant sub-packages rather than having to all be listed in the main
40 ``setup.py`` file.
41
42 To use this, you should modify your ``setup.py`` file to use
43 :func:`~extension_helpers.get_extensions` as follows::
44
45 from extension_helpers import get_extensions
46 ...
47 setup(..., ext_modules=get_extensions())
48
49 Note that if you use this, extension-helpers will also we create a
50 ``packagename.compiler_version`` submodule that contain information about the
51 compilers used.
52
53 It is also possible to enable extension-helpers in ``setup.cfg`` instead of
54 ``setup.py`` by adding the following configuration to the ``setup.cfg`` file::
55
56 [extension-helpers]
57 use_extension_helpers = true
58
59 Moreover, one can also enable extension-helpers in ``pyproject.toml`` by adding
60 the following configuration to the ``pyproject.toml`` file::
61
62 [tool.extension-helpers]
63 use_extension_helpers = true
64
65 .. note::
66 For backwards compatibility, the setting of ``use_extension_helpers`` in
67 ``setup.cfg`` will override any setting of it in ``pyproject.toml``.
68
69 Python limited API
70 ------------------
71
72 Your package may opt in to the :pep:`384` Python Limited API so that a single
73 binary wheel works with many different versions of Python on the same platform.
74 For this to work, any C extensions you write needs to make use only of
75 `certain C functions <https://docs.python.org/3/c-api/stable.html#limited-api-list>`__.
76
77 To opt in to the Python Limited API, add the following standard setuptools
78 option to your project's ``setup.cfg`` file::
79
80 [bdist_wheel]
81 py_limited_api = cp311
82
83 Here, ``311`` denotes API compatibility with Python >= 3.11. Replace with the
84 lowest major and minor version number that you wish to support.
85
86 You can also set this option in ``pyproject.toml``, using::
87
88 [tool.distutils.bdist_wheel]
89 py-limited-api = "cp312"
90
91 although note that this option is not formally documented/supported by the Python
92 packaging infrastructure and may change in future.
93
94 Alternatively, if you use setuptools 65.4 or later, you can dynamically opt in
95 to limited API builds by setting the ``EXTENSION_HELPERS_PY_LIMITED_API``
96 environment variable, e.g.::
97
98 EXTENSION_HELPERS_PY_LIMITED_API='cp311' python -m build
99
100 If you define ``py_limited_api`` in ``setup.cfg``, you can use
101 ``EXTENSION_HELPERS_PY_LIMITED_API`` to opt **out** of the limited API builds
102 by setting ``EXTENSION_HELPERS_PY_LIMITED_API`` to an empty string. There is however
103 no way to opt out if you use ``py-limited-api`` in ``pyproject.toml``.
104
105 The ``get_extensions()`` functions will automatically detect these options and
106 add the necessary compiler flags to build your extension modules.