Skip to content

Commit ea38135

Browse files
authored
Added documentation page about extension modules (#3368)
2 parents 78cb747 + 0540600 commit ea38135

File tree

4 files changed

+204
-1
lines changed

4 files changed

+204
-1
lines changed

changelog.d/3368.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added documentation page about extension modules -- by :user:`mkoeppe`

docs/userguide/ext_modules.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
==========================
2+
Building Extension Modules
3+
==========================
4+
5+
Setuptools can build C/C++ extension modules. The keyword argument
6+
``ext_modules`` of ``setup()`` should be a list of instances of the
7+
:class:`setuptools.Extension` class.
8+
9+
10+
For example, let's consider a simple project with only one extension module::
11+
12+
<project_folder>
13+
├── pyproject.toml
14+
└── foo.c
15+
16+
and all project metadata configuration in the ``pyproject.toml`` file:
17+
18+
.. code-block:: toml
19+
20+
# pyproject.toml
21+
[build-system]
22+
requires = ["setuptools"]
23+
build-backend = "setuptools.build_meta"
24+
25+
[project]
26+
name = "mylib-foo" # as it would appear on PyPI
27+
version = "0.42"
28+
29+
To instruct setuptools to compile the ``foo.c`` file into the extension module
30+
``mylib.foo``, we need to add a ``setup.py`` file similar to the following:
31+
32+
.. code-block:: python
33+
34+
from setuptools import Extension, setup
35+
36+
setup(
37+
ext_modules=[
38+
Extension(
39+
name="mylib.foo", # as it would be imported
40+
# may include packages/namespaces separated by `.`
41+
42+
sources=["foo.c"], # all sources are compiled into a single binary file
43+
),
44+
]
45+
)
46+
47+
.. seealso::
48+
You can find more information on the `Python docs about C/C++ extensions`_.
49+
Alternatively, you might also be interested in learn about `Cython`_.
50+
51+
If you plan to distribute a package that uses extensions across multiple
52+
platforms, :pypi:`cibuildwheel` can also be helpful.
53+
54+
55+
Compiler and linker options
56+
===========================
57+
58+
The command ``build_ext`` builds C/C++ extension modules. It creates
59+
a command line for running the compiler and linker by combining
60+
compiler and linker options from various sources:
61+
62+
.. Reference: `test_customize_compiler` in distutils/tests/test_sysconfig.py
63+
64+
* the ``sysconfig`` variables ``CC``, ``CXX``, ``CCSHARED``,
65+
``LDSHARED``, and ``CFLAGS``,
66+
* the environment variables ``CC``, ``CPP``,
67+
``CXX``, ``LDSHARED`` and ``LDFLAGS``,
68+
``CFLAGS``, ``CPPFLAGS``, ``LDFLAGS``,
69+
* the ``Extension`` attributes ``include_dirs``,
70+
``library_dirs``, ``extra_compile_args``, ``extra_link_args``,
71+
``runtime_library_dirs``.
72+
73+
.. Ignoring AR, ARFLAGS, RANLIB here because they are used by the (obsolete?) build_clib, not build_ext.
74+
75+
The resulting command line is then processed by the compiler and linker.
76+
According to the GCC manual sections on `directory options`_ and
77+
`environment variables`_, the C/C++ compiler searches for files named in
78+
``#include <file>`` directives in the following order:
79+
80+
* first, in directories given by ``-I`` options (in left-to-right order),
81+
* then, in directories given by the environment variable ``CPATH`` (in left-to-right order),
82+
* then, in directories given by ``-isystem`` options (in left-to-right order),
83+
* then, in directories given by the environment variable ``C_INCLUDE_PATH`` (for C) and ``CPLUS_INCLUDE_PATH`` (for C++),
84+
* then, in standard system directories,
85+
* finally, in directories given by ``-idirafter`` options (in left-to-right order).
86+
87+
The linker searches for libraries in the following order:
88+
89+
* first, in directories given by ``-L`` options (in left-to-right order),
90+
* then, in directories given by the environment variable ``LIBRARY_PATH`` (in left-to-right order).
91+
92+
.. important::
93+
All files used to compile your extension need to be available on the system
94+
when building the package, so please make sure to include some documentation
95+
on how developers interested in building your package from source
96+
can obtain operating system level dependencies
97+
(e.g. compilers and external binary libraries/artifacts).
98+
99+
You will also need to make sure that all auxiliary files that are contained
100+
inside your :term:`project` (e.g. C headers authored by you or your team)
101+
are configured to be included in your :term:`sdist <Source Distribution (or "sdist")>`.
102+
Please have a look on our section on :ref:`Controlling files in the distribution`.
103+
104+
105+
----
106+
107+
API Reference
108+
-------------
109+
110+
.. autoclass:: setuptools.Extension
111+
112+
113+
.. _Python docs about C/C++ extensions: https://docs.python.org/3/extending/extending.html
114+
.. _Cython: https://cython.readthedocs.io/en/stable/index.html
115+
.. _directory options: https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html
116+
.. _environment variables: https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html>

docs/userguide/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Contents
2828
package_discovery
2929
entry_point
3030
dependency_management
31+
ext_modules
3132
datafiles
3233
development_mode
3334
distribution

setuptools/extension.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,92 @@ def _have_cython():
2828

2929

3030
class Extension(_Extension):
31-
"""Extension that uses '.c' files in place of '.pyx' files"""
31+
"""
32+
Describes a single extension module.
33+
34+
This means that all source files will be compiled into a single binary file
35+
``<module path>.<suffix>`` (with ``<module path>`` derived from ``name`` and
36+
``<suffix>`` defined by one of the values in
37+
``importlib.machinery.EXTENSION_SUFFIXES``).
38+
39+
In the case ``.pyx`` files are passed as ``sources and`` ``Cython`` is **not**
40+
installed in the build environment, ``setuptools`` may also try to look for the
41+
equivalent ``.cpp`` or ``.c`` files.
42+
43+
:arg str name:
44+
the full name of the extension, including any packages -- ie.
45+
*not* a filename or pathname, but Python dotted name
46+
47+
:arg list[str] sources:
48+
list of source filenames, relative to the distribution root
49+
(where the setup script lives), in Unix form (slash-separated)
50+
for portability. Source files may be C, C++, SWIG (.i),
51+
platform-specific resource files, or whatever else is recognized
52+
by the "build_ext" command as source for a Python extension.
53+
54+
:keyword list[str] include_dirs:
55+
list of directories to search for C/C++ header files (in Unix
56+
form for portability)
57+
58+
:keyword list[tuple[str, str|None]] define_macros:
59+
list of macros to define; each macro is defined using a 2-tuple:
60+
the first item corresponding to the name of the macro and the second
61+
item either a string with its value or None to
62+
define it without a particular value (equivalent of "#define
63+
FOO" in source or -DFOO on Unix C compiler command line)
64+
65+
:keyword list[str] undef_macros:
66+
list of macros to undefine explicitly
67+
68+
:keyword list[str] library_dirs:
69+
list of directories to search for C/C++ libraries at link time
70+
71+
:keyword list[str] libraries:
72+
list of library names (not filenames or paths) to link against
73+
74+
:keyword list[str] runtime_library_dirs:
75+
list of directories to search for C/C++ libraries at run time
76+
(for shared extensions, this is when the extension is loaded)
77+
78+
:keyword list[str] extra_objects:
79+
list of extra files to link with (eg. object files not implied
80+
by 'sources', static library that must be explicitly specified,
81+
binary resource files, etc.)
82+
83+
:keyword list[str] extra_compile_args:
84+
any extra platform- and compiler-specific information to use
85+
when compiling the source files in 'sources'. For platforms and
86+
compilers where "command line" makes sense, this is typically a
87+
list of command-line arguments, but for other platforms it could
88+
be anything.
89+
90+
:keyword list[str] extra_link_args:
91+
any extra platform- and compiler-specific information to use
92+
when linking object files together to create the extension (or
93+
to create a new static Python interpreter). Similar
94+
interpretation as for 'extra_compile_args'.
95+
96+
:keyword list[str] export_symbols:
97+
list of symbols to be exported from a shared extension. Not
98+
used on all platforms, and not generally necessary for Python
99+
extensions, which typically export exactly one symbol: "init" +
100+
extension_name.
101+
102+
:keyword list[str] swig_opts:
103+
any extra options to pass to SWIG if a source file has the .i
104+
extension.
105+
106+
:keyword list[str] depends:
107+
list of files that the extension depends on
108+
109+
:keyword str language:
110+
extension language (i.e. "c", "c++", "objc"). Will be detected
111+
from the source extensions if not provided.
112+
113+
:keyword bool optional:
114+
specifies that a build failure in the extension should not abort the
115+
build process, but simply not install the failing extension.
116+
"""
32117

33118
def __init__(self, name, sources, *args, **kw):
34119
# The *args is needed for compatibility as calls may use positional

0 commit comments

Comments
 (0)