@@ -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
4349An 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,
6571that the project should be built (e.g., no setup.py/setup.cfg or other
6672build 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