Skip to content

Commit 259d25a

Browse files
committed
Integrate PEP 517 configuration into the pyproject.toml spec
Document requires, build-backend, and backend-path together, replacing the claim that only requires is valid. Move backend lookup, legacy fallback, and the complete in-tree backend restrictions from the imported text; leave dependency graph and hook execution requirements on the interface page with cross-references between the specifications. Keep requires mandatory when the table is present and build-backend optional, preserving the existing missing-file and missing-table defaults. Scope the illustrative JSON schema to the build-system table and include the two PEP 517 keys without changing the other pyproject.toml tables. Validation: pixi run build; illustrative schema checked against four valid and seven invalid configurations.
1 parent f82066d commit 259d25a

2 files changed

Lines changed: 171 additions & 162 deletions

File tree

‎source/specifications/build-system-interface.rst‎

Lines changed: 7 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -50,93 +50,15 @@ combination of wheels and sdists. In a command like ``pip install
5050
lxml==2.4.0``, pip is acting as an integration frontend.
5151

5252

53-
.. _build-system-interface-source-trees:
54-
55-
Source trees
56-
============
57-
58-
There is an existing, legacy source tree format involving
59-
``setup.py``. We don't try to specify it further; its de facto
60-
specification is encoded in the source code and documentation of
61-
``distutils``, ``setuptools``, ``pip``, and other tools. We'll refer
62-
to it as the ``setup.py``\-style.
63-
64-
Here we define a new style of source tree based around the
65-
``pyproject.toml`` file defined in :pep:`518`, extending the
66-
``[build-system]`` table in that file with one additional key,
67-
``build-backend``. Here's an example of how it would look::
68-
69-
[build-system]
70-
# Defined by PEP 518:
71-
requires = ["flit"]
72-
# Defined by this specification:
73-
build-backend = "flit.api:main"
74-
75-
``build-backend`` is a string naming a Python object that will be
76-
used to perform the build (see below for details). This is formatted
77-
following the same ``module:object`` syntax as a ``setuptools`` entry
78-
point. For instance, if the string is ``"flit.api:main"`` as in the
79-
example above, this object would be looked up by executing the
80-
equivalent of::
81-
82-
import flit.api
83-
backend = flit.api.main
84-
85-
It's also legal to leave out the ``:object`` part, e.g. ::
86-
87-
build-backend = "flit.api"
88-
89-
which acts like::
90-
91-
import flit.api
92-
backend = flit.api
93-
94-
Formally, the string should satisfy this grammar::
95-
96-
identifier = (letter | '_') (letter | '_' | digit)*
97-
module_path = identifier ('.' identifier)*
98-
object_path = identifier ('.' identifier)*
99-
entry_point = module_path (':' object_path)?
100-
101-
And we import ``module_path`` and then lookup
102-
``module_path.object_path`` (or just ``module_path`` if
103-
``object_path`` is missing).
104-
105-
When importing the module path, we do *not* look in the directory containing the
106-
source tree, unless that would be on ``sys.path`` anyway (e.g. because it is
107-
specified in PYTHONPATH). Although Python automatically adds the working
108-
directory to ``sys.path`` in some situations, code to resolve the backend should
109-
not be affected by this.
110-
111-
If the ``pyproject.toml`` file is absent, or the ``build-backend``
112-
key is missing, the source tree is not using this specification, and
113-
tools should revert to the legacy behaviour of running ``setup.py`` (either
114-
directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__``
115-
backend).
116-
117-
Where the ``build-backend`` key exists, this takes precedence and the source tree follows the format and
118-
conventions of the specified backend (as such no ``setup.py`` is needed unless the backend requires it).
119-
Projects may still wish to include a ``setup.py`` for compatibility with tools that do not use this spec.
120-
121-
This specification also defines a ``backend-path`` key for use in ``pyproject.toml``, see
122-
the "In-Tree Build Backends" section below. This key would be used as follows::
123-
124-
[build-system]
125-
# Defined by PEP 518:
126-
requires = ["flit"]
127-
# Defined by this specification:
128-
build-backend = "local_backend"
129-
backend-path = ["backend"]
130-
131-
13253
.. _build-system-interface-build-requirements:
13354

13455
Build requirements
135-
------------------
56+
==================
13657

137-
This specification places a number of additional requirements on the "build requirements"
138-
section of ``pyproject.toml``. These are intended to ensure that projects do
139-
not create impossible to satisfy conditions with their build requirements.
58+
This specification places additional requirements on the
59+
:ref:`build requirements <pyproject-build-system-requires>` declared in
60+
``pyproject.toml``. These are intended to ensure that projects do not create
61+
impossible to satisfy conditions with their build requirements.
14062

14163
- Project build requirements will define a directed graph of requirements
14264
(project A needs B to build, B needs C and D, etc.) This graph MUST NOT
@@ -153,7 +75,8 @@ not create impossible to satisfy conditions with their build requirements.
15375
Note in particular that the requirement for no requirement cycles means that
15476
backends wishing to self-host (i.e., building a wheel for a backend uses that
15577
backend for the build) need to make special provision to avoid causing cycles.
156-
Typically this will involve specifying themselves as an in-tree backend, and
78+
Typically this will involve specifying themselves as an
79+
:ref:`in-tree backend <pyproject-build-system-backend-path>`, and
15780
avoiding external build dependencies (usually by vendoring them).
15881

15982

@@ -505,50 +428,6 @@ package *authors*, while still allowing *end-users* to open up the
505428
hood and apply duct tape when necessary.
506429

507430

508-
.. _build-system-interface-in-tree-build-backends:
509-
510-
In-tree build backends
511-
----------------------
512-
513-
In certain circumstances, projects may wish to include the source code for the
514-
build backend directly in the source tree, rather than referencing the backend
515-
via the ``requires`` key. Two specific situations where this would be expected
516-
are:
517-
518-
- Backends themselves, which want to use their own features for building
519-
themselves ("self-hosting backends")
520-
- Project-specific backends, typically consisting of a custom wrapper around a
521-
standard backend, where the wrapper is too project-specific to be worth
522-
distributing independently ("in-tree backends")
523-
524-
Projects can specify that their backend code is hosted in-tree by including the
525-
``backend-path`` key in ``pyproject.toml``. This key contains a list of
526-
directories, which the frontend will add to the start of ``sys.path`` when
527-
loading the backend, and running the backend hooks.
528-
529-
There are two restrictions on the content of the ``backend-path`` key:
530-
531-
- Directories in ``backend-path`` are interpreted as relative to the project
532-
root, and MUST refer to a location within the source tree (after relative
533-
paths and symbolic links have been resolved).
534-
- The backend code MUST be loaded from one of the directories specified in
535-
``backend-path`` (i.e., it is not permitted to specify ``backend-path`` and
536-
*not* have in-tree backend code).
537-
538-
The first restriction is to ensure that source trees remain self-contained,
539-
and cannot refer to locations outside of the source tree. Frontends SHOULD
540-
check this condition (typically by resolving the location to an absolute path
541-
and resolving symbolic links, and then checking it against the project root),
542-
and fail with an error message if it is violated.
543-
544-
The ``backend-path`` feature is intended to support the implementation of
545-
in-tree backends, and not to allow configuration of existing backends. The
546-
second restriction above is specifically to ensure that this is how the feature
547-
is used. Front ends MAY enforce this check, but are not required to. Doing so
548-
would typically involve checking the backend's ``__file__`` attribute against
549-
the locations in ``backend-path``.
550-
551-
552431
.. _build-system-interface-source-distributions:
553432

554433
Source distributions

‎source/specifications/pyproject-toml.rst‎

Lines changed: 164 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,27 @@ use (tool-specific configuration should use the ``[tool]`` table).
2424

2525
.. _pyproject-build-system-table:
2626

27-
Declaring build system dependencies: the ``[build-system]`` table
28-
=================================================================
27+
Declaring the build system: the ``[build-system]`` table
28+
========================================================
2929

30-
The ``[build-system]`` table declares any Python level dependencies that
31-
must be installed in order to run the project's build system
32-
successfully.
30+
The ``[build-system]`` table stores build-related data. The valid keys are
31+
``requires``, ``build-backend``, and ``backend-path``. The
32+
:ref:`build-system-interface` specification describes how the configured
33+
backend is invoked.
3334

34-
.. TODO: merge with PEP 517
35+
.. _pyproject-build-system-requires:
3536

36-
The ``[build-system]`` table is used to store build-related data.
37-
Initially, only one key of the table is valid and is mandatory
38-
for the table: ``requires``. This key must have a value of a list
39-
of strings representing dependencies required to execute the
40-
build system. The strings in this list follow the :ref:`dependency specifier
41-
specification <dependency-specifiers>`.
37+
``requires``
38+
------------
39+
40+
The ``requires`` key declares the Python level dependencies that must be
41+
installed in order to run the project's build system successfully.
42+
This key is mandatory if the table is present. Its value must be a list
43+
of strings representing dependencies required to execute the build system.
44+
The strings in this list follow the :ref:`dependency specifier specification
45+
<dependency-specifiers>`. See also the
46+
:ref:`requirements on build dependency graphs
47+
<build-system-interface-build-requirements>`.
4248

4349
An example ``[build-system]`` table for a project built with
4450
``setuptools`` is:
@@ -65,38 +71,158 @@ Tools may choose to present an error to the user if the file exists,
6571
that the project should be built (e.g., no setup.py/setup.cfg or other
6672
build configuration files, and no ``[project]`` table).
6773

68-
To provide a type-specific representation of the resulting data from
69-
the TOML file for illustrative purposes only, the following
70-
`JSON Schema <https://json-schema.org>`_ would match the data format:
74+
.. _pyproject-build-system-build-backend:
75+
.. _build-system-interface-source-trees:
76+
77+
``build-backend``
78+
-----------------
79+
80+
There is an existing, legacy source tree format involving
81+
``setup.py``. We don't try to specify it further; its de facto
82+
specification is encoded in the source code and documentation of
83+
``distutils``, ``setuptools``, ``pip``, and other tools. We'll refer
84+
to it as the ``setup.py``\-style.
85+
86+
Projects can select their build backend using the optional ``build-backend``
87+
key. Here's an example of how it would look::
88+
89+
[build-system]
90+
# Defined by PEP 518:
91+
requires = ["flit"]
92+
# Defined by this specification:
93+
build-backend = "flit.api:main"
94+
95+
``build-backend`` is a string naming a Python object that will be
96+
used to perform the build (see :ref:`build-system-interface` for details).
97+
This is formatted following the same ``module:object`` syntax as a ``setuptools`` entry
98+
point. For instance, if the string is ``"flit.api:main"`` as in the
99+
example above, this object would be looked up by executing the
100+
equivalent of::
101+
102+
import flit.api
103+
backend = flit.api.main
104+
105+
It's also legal to leave out the ``:object`` part, e.g. ::
106+
107+
build-backend = "flit.api"
108+
109+
which acts like::
110+
111+
import flit.api
112+
backend = flit.api
113+
114+
Formally, the string should satisfy this grammar::
115+
116+
identifier = (letter | '_') (letter | '_' | digit)*
117+
module_path = identifier ('.' identifier)*
118+
object_path = identifier ('.' identifier)*
119+
entry_point = module_path (':' object_path)?
120+
121+
And we import ``module_path`` and then lookup
122+
``module_path.object_path`` (or just ``module_path`` if
123+
``object_path`` is missing).
124+
125+
When importing the module path, we do *not* look in the directory containing the
126+
source tree, unless that would be on ``sys.path`` anyway (e.g. because it is
127+
specified in PYTHONPATH). Although Python automatically adds the working
128+
directory to ``sys.path`` in some situations, code to resolve the backend should
129+
not be affected by this.
130+
131+
If the ``pyproject.toml`` file is absent, or the ``build-backend``
132+
key is missing, the source tree is not using the :ref:`build-system-interface`
133+
specification, and tools should revert to the legacy behaviour of running ``setup.py`` (either
134+
directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__``
135+
backend).
136+
137+
Where the ``build-backend`` key exists, this takes precedence and the source tree follows the format and
138+
conventions of the specified backend (as such no ``setup.py`` is needed unless the backend requires it).
139+
Projects may still wish to include a ``setup.py`` for compatibility with tools
140+
that do not use the build system interface specification.
141+
142+
.. _pyproject-build-system-backend-path:
143+
.. _build-system-interface-in-tree-build-backends:
144+
145+
``backend-path``
146+
----------------
147+
148+
The optional ``backend-path`` key can be used as follows::
149+
150+
[build-system]
151+
# Defined by PEP 518:
152+
requires = ["flit"]
153+
# Defined by this specification:
154+
build-backend = "local_backend"
155+
backend-path = ["backend"]
156+
157+
In certain circumstances, projects may wish to include the source code for the
158+
build backend directly in the source tree, rather than referencing the backend
159+
via the ``requires`` key. Two specific situations where this would be expected
160+
are:
161+
162+
- Backends themselves, which want to use their own features for building
163+
themselves ("self-hosting backends")
164+
- Project-specific backends, typically consisting of a custom wrapper around a
165+
standard backend, where the wrapper is too project-specific to be worth
166+
distributing independently ("in-tree backends")
167+
168+
Projects can specify that their backend code is hosted in-tree by including the
169+
``backend-path`` key in ``pyproject.toml``. This key contains a list of
170+
directories, which the frontend will add to the start of ``sys.path`` when
171+
loading the backend, and running the backend hooks.
172+
173+
There are two restrictions on the content of the ``backend-path`` key:
174+
175+
- Directories in ``backend-path`` are interpreted as relative to the project
176+
root, and MUST refer to a location within the source tree (after relative
177+
paths and symbolic links have been resolved).
178+
- The backend code MUST be loaded from one of the directories specified in
179+
``backend-path`` (i.e., it is not permitted to specify ``backend-path`` and
180+
*not* have in-tree backend code).
181+
182+
The first restriction is to ensure that source trees remain self-contained,
183+
and cannot refer to locations outside of the source tree. Frontends SHOULD
184+
check this condition (typically by resolving the location to an absolute path
185+
and resolving symbolic links, and then checking it against the project root),
186+
and fail with an error message if it is violated.
187+
188+
The ``backend-path`` feature is intended to support the implementation of
189+
in-tree backends, and not to allow configuration of existing backends. The
190+
second restriction above is specifically to ensure that this is how the feature
191+
is used. Front ends MAY enforce this check, but are not required to. Doing so
192+
would typically involve checking the backend's ``__file__`` attribute against
193+
the locations in ``backend-path``.
194+
195+
Illustrative JSON schema
196+
------------------------
197+
198+
To provide a type-specific representation of the ``[build-system]`` table
199+
for illustrative purposes only, the following
200+
`JSON Schema <https://json-schema.org>`_ would match its data format:
71201

72202
.. code-block:: json
73203
74204
{
75205
"$schema": "http://json-schema.org/schema#",
76-
77206
"type": "object",
78207
"additionalProperties": false,
79-
80208
"properties": {
81-
"build-system": {
82-
"type": "object",
83-
"additionalProperties": false,
84-
85-
"properties": {
86-
"requires": {
87-
"type": "array",
88-
"items": {
89-
"type": "string"
90-
}
91-
}
92-
},
93-
"required": ["requires"]
209+
"requires": {
210+
"type": "array",
211+
"items": {
212+
"type": "string"
213+
}
94214
},
95-
96-
"tool": {
97-
"type": "object"
215+
"build-backend": {
216+
"type": "string"
217+
},
218+
"backend-path": {
219+
"type": "array",
220+
"items": {
221+
"type": "string"
222+
}
98223
}
99-
}
224+
},
225+
"required": ["requires"]
100226
}
101227
102228
@@ -703,6 +829,10 @@ History
703829
a ``[build-system]`` containing a ``requires`` key and a ``[tool]`` table, was
704830
approved through :pep:`518`.
705831

832+
- September 2017: The ``build-backend`` key was approved through :pep:`517`.
833+
834+
- March 2019: The ``backend-path`` key was added through an amendment to :pep:`517`.
835+
706836
- November 2020: The specification of the ``[project]`` table was approved
707837
through :pep:`621`.
708838

0 commit comments

Comments
 (0)