From ee4cf5cc51d4e1552c5001b2f5fc1226e2f8a82f Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 11 Nov 2026 15:46:43 +0000 Subject: [PATCH 1/6] Import current PEP 517 specification verbatim Copy the sections from Terminology and goals through Source distributions without changing their text. Use python/peps revision b39aefe6614b4e8a925d07c4b3ed47e147236dbe, which includes the amendments for in-tree backends, build requirement cycles, legacy fallback, and duplicate config_settings keys. Place the new interface specification beside the distribution formats. Keep the import separate from subsequent editorial changes so reviewers can verify it directly against the PEP. --- .../specifications/build-system-interface.rst | 556 ++++++++++++++++++ .../section-distribution-formats.rst | 7 +- 2 files changed, 560 insertions(+), 3 deletions(-) create mode 100644 source/specifications/build-system-interface.rst diff --git a/source/specifications/build-system-interface.rst b/source/specifications/build-system-interface.rst new file mode 100644 index 0000000000..113b707f1c --- /dev/null +++ b/source/specifications/build-system-interface.rst @@ -0,0 +1,556 @@ +.. _build-system-interface: + +###################### +Build system interface +###################### + +======================= + Terminology and goals +======================= + +A *source tree* is something like a VCS checkout. We need a standard +interface for installing from this format, to support usages like +``pip install some-directory/``. + +A *source distribution* is a static snapshot representing a particular +release of some source code, like ``lxml-3.4.4.tar.gz``. Source +distributions serve many purposes: they form an archival record of +releases, they provide a stupid-simple de facto standard for tools +that want to ingest and process large corpora of code, possibly +written in many languages (e.g. code search), they act as the input to +downstream packaging systems like Debian/Fedora/Conda/..., and so +forth. In the Python ecosystem they additionally have a particularly +important role to play, because packaging tools like ``pip`` are able +to use source distributions to fulfill binary dependencies, e.g. if +there is a distribution ``foo.whl`` which declares a dependency on +``bar``, then we need to support the case where ``pip install bar`` or +``pip install foo`` automatically locates the sdist for ``bar``, +downloads it, builds it, and installs the resulting package. + +Source distributions are also known as *sdists* for short. + +A *build frontend* is a tool that users might run that takes arbitrary +source trees or source distributions and builds wheels from them. The +actual building is done by each source tree's *build backend*. In a +command like ``pip wheel some-directory/``, pip is acting as a build +frontend. + +An *integration frontend* is a tool that users might run that takes a +set of package requirements (e.g. a requirements.txt file) and +attempts to update a working environment to satisfy those +requirements. This may require locating, building, and installing a +combination of wheels and sdists. In a command like ``pip install +lxml==2.4.0``, pip is acting as an integration frontend. + + +============== + Source trees +============== + +There is an existing, legacy source tree format involving +``setup.py``. We don't try to specify it further; its de facto +specification is encoded in the source code and documentation of +``distutils``, ``setuptools``, ``pip``, and other tools. We'll refer +to it as the ``setup.py``\-style. + +Here we define a new style of source tree based around the +``pyproject.toml`` file defined in :pep:`518`, extending the +``[build-system]`` table in that file with one additional key, +``build-backend``. Here's an example of how it would look:: + + [build-system] + # Defined by PEP 518: + requires = ["flit"] + # Defined by this PEP: + build-backend = "flit.api:main" + +``build-backend`` is a string naming a Python object that will be +used to perform the build (see below for details). This is formatted +following the same ``module:object`` syntax as a ``setuptools`` entry +point. For instance, if the string is ``"flit.api:main"`` as in the +example above, this object would be looked up by executing the +equivalent of:: + + import flit.api + backend = flit.api.main + +It's also legal to leave out the ``:object`` part, e.g. :: + + build-backend = "flit.api" + +which acts like:: + + import flit.api + backend = flit.api + +Formally, the string should satisfy this grammar:: + + identifier = (letter | '_') (letter | '_' | digit)* + module_path = identifier ('.' identifier)* + object_path = identifier ('.' identifier)* + entry_point = module_path (':' object_path)? + +And we import ``module_path`` and then lookup +``module_path.object_path`` (or just ``module_path`` if +``object_path`` is missing). + +When importing the module path, we do *not* look in the directory containing the +source tree, unless that would be on ``sys.path`` anyway (e.g. because it is +specified in PYTHONPATH). Although Python automatically adds the working +directory to ``sys.path`` in some situations, code to resolve the backend should +not be affected by this. + +If the ``pyproject.toml`` file is absent, or the ``build-backend`` +key is missing, the source tree is not using this specification, and +tools should revert to the legacy behaviour of running ``setup.py`` (either +directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__`` +backend). + +Where the ``build-backend`` key exists, this takes precedence and the source tree follows the format and +conventions of the specified backend (as such no ``setup.py`` is needed unless the backend requires it). +Projects may still wish to include a ``setup.py`` for compatibility with tools that do not use this spec. + +This PEP also defines a ``backend-path`` key for use in ``pyproject.toml``, see +the "In-Tree Build Backends" section below. This key would be used as follows:: + + [build-system] + # Defined by PEP 518: + requires = ["flit"] + # Defined by this PEP: + build-backend = "local_backend" + backend-path = ["backend"] + + +Build requirements +================== + +This PEP places a number of additional requirements on the "build requirements" +section of ``pyproject.toml``. These are intended to ensure that projects do +not create impossible to satisfy conditions with their build requirements. + +- Project build requirements will define a directed graph of requirements + (project A needs B to build, B needs C and D, etc.) This graph MUST NOT + contain cycles. If (due to lack of co-ordination between projects, for + example) a cycle is present, front ends MAY refuse to build the project. +- Where build requirements are available as wheels, front ends SHOULD use these + where practical, to avoid deeply nested builds. However front ends MAY have + modes where they do not consider wheels when locating build requirements, and + so projects MUST NOT assume that publishing wheels is sufficient to break a + requirement cycle. +- Front ends SHOULD check explicitly for requirement cycles, and terminate + the build with an informative message if one is found. + +Note in particular that the requirement for no requirement cycles means that +backends wishing to self-host (i.e., building a wheel for a backend uses that +backend for the build) need to make special provision to avoid causing cycles. +Typically this will involve specifying themselves as an in-tree backend, and +avoiding external build dependencies (usually by vendoring them). + + +========================= + Build backend interface +========================= + +The build backend object is expected to have attributes which provide +some or all of the following hooks. The common ``config_settings`` +argument is described after the individual hooks. + +Mandatory hooks +=============== + +build_wheel +----------- + +:: + + def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): + ... + +Must build a .whl file, and place it in the specified ``wheel_directory``. It +must return the basename (not the full path) of the ``.whl`` file it creates, +as a unicode string. + +If the build frontend has previously called ``prepare_metadata_for_build_wheel`` +and depends on the wheel resulting from this call to have metadata +matching this earlier call, then it should provide the path to the created +``.dist-info`` directory as the ``metadata_directory`` argument. If this +argument is provided, then ``build_wheel`` MUST produce a wheel with identical +metadata. The directory passed in by the build frontend MUST be +identical to the directory created by ``prepare_metadata_for_build_wheel``, +including any unrecognized files it created. + +Backends which do not provide the ``prepare_metadata_for_build_wheel`` hook may +either silently ignore the ``metadata_directory`` parameter to ``build_wheel``, +or else raise an exception when it is set to anything other than ``None``. + +To ensure that wheels from different sources are built the same way, frontends +may call ``build_sdist`` first, and then call ``build_wheel`` in the unpacked +sdist. But if the backend indicates that it is missing some requirements for +creating an sdist (see below), the frontend will fall back to calling +``build_wheel`` in the source directory. + +The source directory may be read-only. Backends should therefore be +prepared to build without creating or modifying any files in the source +directory, but they may opt not to handle this case, in which case +failures will be visible to the user. Frontends are not responsible for +any special handling of read-only source directories. + +The backend may store intermediate artifacts in cache locations or +temporary directories. The presence or absence of any caches should not +make a material difference to the final result of the build. + +build_sdist +----------- + +:: + + def build_sdist(sdist_directory, config_settings=None): + ... + +Must build a .tar.gz source distribution and place it in the specified +``sdist_directory``. It must return the basename (not the full path) of the +``.tar.gz`` file it creates, as a unicode string. + +A .tar.gz source distribution (sdist) contains a single top-level directory called +``{name}-{version}`` (e.g. ``foo-1.0``), containing the source files of the +package. This directory must also contain the +``pyproject.toml`` from the build directory, and a PKG-INFO file containing +metadata in the format described in +:pep:`345`. Although historically +zip files have also been used as sdists, this hook should produce a gzipped +tarball. This is already the more common format for sdists, and having a +consistent format makes for simpler tooling. + +The generated tarball should use the modern POSIX.1-2001 pax tar format, which +specifies UTF-8 based file names. This is not yet the default for the tarfile +module shipped with Python 3.6, so backends using the tarfile module need to +explicitly pass ``format=tarfile.PAX_FORMAT``. + +Some backends may have extra requirements for creating sdists, such as version +control tools. However, some frontends may prefer to make intermediate sdists +when producing wheels, to ensure consistency. +If the backend cannot produce an sdist because a dependency is missing, or +for another well understood reason, it should raise an exception of a specific +type which it makes available as ``UnsupportedOperation`` on the backend object. +If the frontend gets this exception while building an sdist as an intermediate +for a wheel, it should fall back to building a wheel directly. +The backend does not need to define this exception type if it would never raise +it. + +Optional hooks +============== + +get_requires_for_build_wheel +---------------------------- + +:: + + def get_requires_for_build_wheel(config_settings=None): + ... + +This hook MUST return an additional list of strings containing :pep:`508` +dependency specifications, above and beyond those specified in the +``pyproject.toml`` file, to be installed when calling the ``build_wheel`` or +``prepare_metadata_for_build_wheel`` hooks. + +Example:: + + def get_requires_for_build_wheel(config_settings): + return ["wheel >= 0.25", "setuptools"] + +If not defined, the default implementation is equivalent to ``return []``. + +prepare_metadata_for_build_wheel +-------------------------------- + +:: + + def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None): + ... + +Must create a ``.dist-info`` directory containing wheel metadata +inside the specified ``metadata_directory`` (i.e., creates a directory +like ``{metadata_directory}/{package}-{version}.dist-info/``). This +directory MUST be a valid ``.dist-info`` directory as defined in the +wheel specification, except that it need not contain ``RECORD`` or +signatures. The hook MAY also create other files inside this +directory, and a build frontend MUST preserve, but otherwise ignore, such files; +the intention +here is that in cases where the metadata depends on build-time +decisions, the build backend may need to record these decisions in +some convenient format for re-use by the actual wheel-building step. + +This must return the basename (not the full path) of the ``.dist-info`` +directory it creates, as a unicode string. + +If a build frontend needs this information and the method is +not defined, it should call ``build_wheel`` and look at the resulting +metadata directly. + +get_requires_for_build_sdist +---------------------------- + +:: + + def get_requires_for_build_sdist(config_settings=None): + ... + +This hook MUST return an additional list of strings containing :pep:`508` +dependency specifications, above and beyond those specified in the +``pyproject.toml`` file. These dependencies will be installed when calling the +``build_sdist`` hook. + +If not defined, the default implementation is equivalent to ``return []``. + + +.. note:: Editable installs + + This PEP originally specified another hook, ``install_editable``, to do an + editable install (as with ``pip install -e``). It was removed due to the + complexity of the topic, but may be specified in a later PEP. + + Briefly, the questions to be answered include: what reasonable ways existing + of implementing an 'editable install'? Should the backend or the frontend + pick how to make an editable install? And if the frontend does, what does it + need from the backend to do so. + +Config settings +=============== + +:: + + config_settings + +This argument, which is passed to all hooks, is an arbitrary +dictionary provided as an "escape hatch" for users to pass ad-hoc +configuration into individual package builds. Build backends MAY +assign any semantics they like to this dictionary. Build frontends +SHOULD provide some mechanism for users to specify arbitrary +string-key/string-value pairs to be placed in this dictionary. +For example, they might support some syntax like ``--package-config CC=gcc``. +In case a user provides duplicate string-keys, build frontends SHOULD +combine the corresponding string-values into a list of strings. +Build frontends MAY also provide arbitrary other mechanisms +for users to place entries in this dictionary. For example, ``pip`` +might choose to map a mix of modern and legacy command line arguments +like:: + + pip install \ + --package-config CC=gcc \ + --global-option="--some-global-option" \ + --build-option="--build-option1" \ + --build-option="--build-option2" + +into a ``config_settings`` dictionary like:: + + { + "CC": "gcc", + "--global-option": ["--some-global-option"], + "--build-option": ["--build-option1", "--build-option2"], + } + +Of course, it's up to users to make sure that they pass options which +make sense for the particular build backend and package that they are +building. + +The hooks may be called with positional or keyword arguments, so backends +implementing them should be careful to make sure that their signatures match +both the order and the names of the arguments above. + +All hooks are run with working directory set to the root of the source +tree, and MAY print arbitrary informational text on stdout and +stderr. They MUST NOT read from stdin, and the build frontend MAY +close stdin before invoking the hooks. + +The build frontend may capture stdout and/or stderr from the backend. If the +backend detects that an output stream is not a terminal/console (e.g. +``not sys.stdout.isatty()``), it SHOULD ensure that any output it writes to that +stream is UTF-8 encoded. The build frontend MUST NOT fail if captured output is +not valid UTF-8, but it MAY not preserve all the information in that case (e.g. +it may decode using the *replace* error handler in Python). If the output stream +is a terminal, the build backend is responsible for presenting its output +accurately, as for any program running in a terminal. + +If a hook raises an exception, or causes the process to terminate, +then this indicates an error. + + +Build environment +================= + +One of the responsibilities of a build frontend is to set up the +Python environment in which the build backend will run. + +We do not require that any particular "virtual environment" mechanism +be used; a build frontend might use virtualenv, or venv, or no special +mechanism at all. But whatever mechanism is used MUST meet the +following criteria: + +- All requirements specified by the project's build-requirements must + be available for import from Python. In particular: + + - The ``get_requires_for_build_wheel`` and ``get_requires_for_build_sdist`` hooks are + executed in an environment which contains the bootstrap requirements + specified in the ``pyproject.toml`` file. + + - The ``prepare_metadata_for_build_wheel`` and ``build_wheel`` hooks are + executed in an environment which contains the + bootstrap requirements from ``pyproject.toml`` and those specified by the + ``get_requires_for_build_wheel`` hook. + + - The ``build_sdist`` hook is executed in an environment which contains the + bootstrap requirements from ``pyproject.toml`` and those specified by the + ``get_requires_for_build_sdist`` hook. + +- This must remain true even for new Python subprocesses spawned by + the build environment, e.g. code like:: + + import sys, subprocess + subprocess.check_call([sys.executable, ...]) + + must spawn a Python process which has access to all the project's + build-requirements. This is necessary e.g. for build backends that + want to run legacy ``setup.py`` scripts in a subprocess. + +- All command-line scripts provided by the build-required packages + must be present in the build environment's PATH. For example, if a + project declares a build-requirement on `flit + `__, then the following must + work as a mechanism for running the flit command-line tool:: + + import subprocess + import shutil + subprocess.check_call([shutil.which("flit"), ...]) + +A build backend MUST be prepared to function in any environment which +meets the above criteria. In particular, it MUST NOT assume that it +has access to any packages except those that are present in the +stdlib, or that are explicitly declared as build-requirements. + +Frontends should call each hook in a fresh subprocess, so that backends are +free to change process global state (such as environment variables or the +working directory). A Python library will be provided which frontends can use +to easily call hooks this way. + +Recommendations for build frontends (non-normative) +--------------------------------------------------- + +A build frontend MAY use any mechanism for setting up a build +environment that meets the above criteria. For example, simply +installing all build-requirements into the global environment would be +sufficient to build any compliant package -- but this would be +sub-optimal for a number of reasons. This section contains +non-normative advice to frontend implementors. + +A build frontend SHOULD, by default, create an isolated environment +for each build, containing only the standard library and any +explicitly requested build-dependencies. This has two benefits: + +- It allows for a single installation run to build multiple packages + that have contradictory build-requirements. E.g. if package1 + build-requires pbr==1.8.1, and package2 build-requires pbr==1.7.2, + then these cannot both be installed simultaneously into the global + environment -- which is a problem when the user requests ``pip + install package1 package2``. Or if the user already has pbr==1.8.1 + installed in their global environment, and a package build-requires + pbr==1.7.2, then downgrading the user's version would be rather + rude. + +- It acts as a kind of public health measure to maximize the number of + packages that actually do declare accurate build-dependencies. We + can write all the strongly worded admonitions to package authors we + want, but if build frontends don't enforce isolation by default, + then we'll inevitably end up with lots of packages on PyPI that + build fine on the original author's machine and nowhere else, which + is a headache that no-one needs. + +However, there will also be situations where build-requirements are +problematic in various ways. For example, a package author might +accidentally leave off some crucial requirement despite our best +efforts; or, a package might declare a build-requirement on ``foo >= +1.0`` which worked great when 1.0 was the latest version, but now 1.1 +is out and it has a showstopper bug; or, the user might decide to +build a package against numpy==1.7 -- overriding the package's +preferred numpy==1.8 -- to guarantee that the resulting build will be +compatible at the C ABI level with an older version of numpy (even if +this means the resulting build is unsupported upstream). Therefore, +build frontends SHOULD provide some mechanism for users to override +the above defaults. For example, a build frontend could have a +``--build-with-system-site-packages`` option that causes the +``--system-site-packages`` option to be passed to +virtualenv-or-equivalent when creating build environments, or a +``--build-requirements-override=my-requirements.txt`` option that +overrides the project's normal build-requirements. + +The general principle here is that we want to enforce hygiene on +package *authors*, while still allowing *end-users* to open up the +hood and apply duct tape when necessary. + + +In-tree build backends +====================== + +In certain circumstances, projects may wish to include the source code for the +build backend directly in the source tree, rather than referencing the backend +via the ``requires`` key. Two specific situations where this would be expected +are: + +- Backends themselves, which want to use their own features for building + themselves ("self-hosting backends") +- Project-specific backends, typically consisting of a custom wrapper around a + standard backend, where the wrapper is too project-specific to be worth + distributing independently ("in-tree backends") + +Projects can specify that their backend code is hosted in-tree by including the +``backend-path`` key in ``pyproject.toml``. This key contains a list of +directories, which the frontend will add to the start of ``sys.path`` when +loading the backend, and running the backend hooks. + +There are two restrictions on the content of the ``backend-path`` key: + +- Directories in ``backend-path`` are interpreted as relative to the project + root, and MUST refer to a location within the source tree (after relative + paths and symbolic links have been resolved). +- The backend code MUST be loaded from one of the directories specified in + ``backend-path`` (i.e., it is not permitted to specify ``backend-path`` and + *not* have in-tree backend code). + +The first restriction is to ensure that source trees remain self-contained, +and cannot refer to locations outside of the source tree. Frontends SHOULD +check this condition (typically by resolving the location to an absolute path +and resolving symbolic links, and then checking it against the project root), +and fail with an error message if it is violated. + +The ``backend-path`` feature is intended to support the implementation of +in-tree backends, and not to allow configuration of existing backends. The +second restriction above is specifically to ensure that this is how the feature +is used. Front ends MAY enforce this check, but are not required to. Doing so +would typically involve checking the backend's ``__file__`` attribute against +the locations in ``backend-path``. + + +====================== + Source distributions +====================== + +We continue with the legacy sdist format, adding some new restrictions. +This format is mostly +undefined, but basically comes down to: a file named +``{NAME}-{VERSION}.{EXT}``, which unpacks into a buildable source tree +called ``{NAME}-{VERSION}/``. Traditionally these have always +contained ``setup.py``\-style source trees; we now allow them to also +contain ``pyproject.toml``\-style source trees. + +Integration frontends require that an sdist named +``{NAME}-{VERSION}.{EXT}`` will generate a wheel named +``{NAME}-{VERSION}-{COMPAT-INFO}.whl``. + +The new restrictions for sdists built by :pep:`517` backends are: + +- They will be gzipped tar archives, with the ``.tar.gz`` extension. Zip + archives, or other compression formats for tarballs, are not allowed at + present. +- Tar archives must be created in the modern POSIX.1-2001 pax tar format, which + uses UTF-8 for file names. +- The source tree contained in an sdist is expected to include the + ``pyproject.toml`` file. diff --git a/source/specifications/section-distribution-formats.rst b/source/specifications/section-distribution-formats.rst index b2c09f8db5..fc1954114b 100644 --- a/source/specifications/section-distribution-formats.rst +++ b/source/specifications/section-distribution-formats.rst @@ -1,9 +1,10 @@ -================================= -Package Distribution File Formats -================================= +======================================= +Package Distribution Formats and Builds +======================================= .. toctree:: :titlesonly: source-distribution-format binary-distribution-format + build-system-interface From f82066dd58045c791d10046af562b1fbd3d56d35 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 11 Nov 2026 15:47:45 +0000 Subject: [PATCH 2/6] Adapt the imported build interface text to the Guide Normalize the PEP heading hierarchy and add stable cross-reference labels without changing hook signatures or requirement strength. Retain the frontend recommendations and examples, including the current amendment about duplicate config_settings keys. Replace the superseded editable-install discussion with a reference to PEP 660, remove the historical promise of a future hook-calling library, and record the specification's approval and amendments in History. --- .../specifications/build-system-interface.rst | 115 ++++++++++++------ 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/source/specifications/build-system-interface.rst b/source/specifications/build-system-interface.rst index 113b707f1c..d6ea8ced57 100644 --- a/source/specifications/build-system-interface.rst +++ b/source/specifications/build-system-interface.rst @@ -1,12 +1,19 @@ .. _build-system-interface: -###################### +====================== Build system interface -###################### +====================== -======================= - Terminology and goals -======================= +This specification defines a standard interface between build frontends and +build backends to build wheels and source distributions from source trees. +The project's build system is configured in the +:ref:`[build-system] table ` of +:file:`pyproject.toml`. + +.. _build-system-interface-terminology: + +Terminology +=========== A *source tree* is something like a VCS checkout. We need a standard interface for installing from this format, to support usages like @@ -43,9 +50,10 @@ combination of wheels and sdists. In a command like ``pip install lxml==2.4.0``, pip is acting as an integration frontend. -============== - Source trees -============== +.. _build-system-interface-source-trees: + +Source trees +============ There is an existing, legacy source tree format involving ``setup.py``. We don't try to specify it further; its de facto @@ -61,7 +69,7 @@ Here we define a new style of source tree based around the [build-system] # Defined by PEP 518: requires = ["flit"] - # Defined by this PEP: + # Defined by this specification: build-backend = "flit.api:main" ``build-backend`` is a string naming a Python object that will be @@ -110,21 +118,23 @@ Where the ``build-backend`` key exists, this takes precedence and the source tre conventions of the specified backend (as such no ``setup.py`` is needed unless the backend requires it). Projects may still wish to include a ``setup.py`` for compatibility with tools that do not use this spec. -This PEP also defines a ``backend-path`` key for use in ``pyproject.toml``, see +This specification also defines a ``backend-path`` key for use in ``pyproject.toml``, see the "In-Tree Build Backends" section below. This key would be used as follows:: [build-system] # Defined by PEP 518: requires = ["flit"] - # Defined by this PEP: + # Defined by this specification: build-backend = "local_backend" backend-path = ["backend"] +.. _build-system-interface-build-requirements: + Build requirements -================== +------------------ -This PEP places a number of additional requirements on the "build requirements" +This specification places a number of additional requirements on the "build requirements" section of ``pyproject.toml``. These are intended to ensure that projects do not create impossible to satisfy conditions with their build requirements. @@ -147,19 +157,22 @@ Typically this will involve specifying themselves as an in-tree backend, and avoiding external build dependencies (usually by vendoring them). -========================= - Build backend interface -========================= +.. _build-system-interface-backend-interface: + +Build backend interface +======================= The build backend object is expected to have attributes which provide some or all of the following hooks. The common ``config_settings`` argument is described after the individual hooks. Mandatory hooks -=============== +--------------- + +.. _build-system-interface-build-wheel: build_wheel ------------ +~~~~~~~~~~~ :: @@ -199,8 +212,10 @@ The backend may store intermediate artifacts in cache locations or temporary directories. The presence or absence of any caches should not make a material difference to the final result of the build. +.. _build-system-interface-build-sdist: + build_sdist ------------ +~~~~~~~~~~~ :: @@ -238,10 +253,12 @@ The backend does not need to define this exception type if it would never raise it. Optional hooks -============== +-------------- + +.. _build-system-interface-get-requires-for-build-wheel: get_requires_for_build_wheel ----------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: @@ -260,8 +277,10 @@ Example:: If not defined, the default implementation is equivalent to ``return []``. +.. _build-system-interface-prepare-metadata-for-build-wheel: + prepare_metadata_for_build_wheel --------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: @@ -287,8 +306,10 @@ If a build frontend needs this information and the method is not defined, it should call ``build_wheel`` and look at the resulting metadata directly. +.. _build-system-interface-get-requires-for-build-sdist: + get_requires_for_build_sdist ----------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: @@ -303,19 +324,15 @@ dependency specifications, above and beyond those specified in the If not defined, the default implementation is equivalent to ``return []``. -.. note:: Editable installs +.. note:: + + Editable installs are specified separately in :pep:`660`. - This PEP originally specified another hook, ``install_editable``, to do an - editable install (as with ``pip install -e``). It was removed due to the - complexity of the topic, but may be specified in a later PEP. - Briefly, the questions to be answered include: what reasonable ways existing - of implementing an 'editable install'? Should the backend or the frontend - pick how to make an editable install? And if the frontend does, what does it - need from the backend to do so. +.. _build-system-interface-config-settings: Config settings -=============== +--------------- :: @@ -375,8 +392,10 @@ If a hook raises an exception, or causes the process to terminate, then this indicates an error. +.. _build-system-interface-build-environment: + Build environment -================= +----------------- One of the responsibilities of a build frontend is to set up the Python environment in which the build backend will run. @@ -429,11 +448,10 @@ stdlib, or that are explicitly declared as build-requirements. Frontends should call each hook in a fresh subprocess, so that backends are free to change process global state (such as environment variables or the -working directory). A Python library will be provided which frontends can use -to easily call hooks this way. +working directory). Recommendations for build frontends (non-normative) ---------------------------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A build frontend MAY use any mechanism for setting up a build environment that meets the above criteria. For example, simply @@ -487,8 +505,10 @@ package *authors*, while still allowing *end-users* to open up the hood and apply duct tape when necessary. +.. _build-system-interface-in-tree-build-backends: + In-tree build backends -====================== +---------------------- In certain circumstances, projects may wish to include the source code for the build backend directly in the source tree, rather than referencing the backend @@ -529,9 +549,10 @@ would typically involve checking the backend's ``__file__`` attribute against the locations in ``backend-path``. -====================== - Source distributions -====================== +.. _build-system-interface-source-distributions: + +Source distributions +==================== We continue with the legacy sdist format, adding some new restrictions. This format is mostly @@ -554,3 +575,17 @@ The new restrictions for sdists built by :pep:`517` backends are: uses UTF-8 for file names. - The source tree contained in an sdist is expected to include the ``pyproject.toml`` file. + + +History +======== + +- September 2017: This specification was provisionally approved through :pep:`517`. +- March 2019: Cycles in build requirements were explicitly prohibited, and + ``backend-path`` was added to support in-tree and self-hosting backends. +- May 2019: Clarified that invoking ``setuptools.build_meta:__legacy__`` is an + acceptable alternative to directly invoking ``setup.py`` when ``build-backend`` + is not specified. +- June 2021: :pep:`517` was marked Final. +- February 2023: Clarified that frontends should combine values for duplicate + ``config_settings`` keys into a list of strings. From 259d25ae7d10c685813d27bdb19852e4d639e803 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 11 Nov 2026 15:49:17 +0000 Subject: [PATCH 3/6] 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. --- .../specifications/build-system-interface.rst | 135 +----------- source/specifications/pyproject-toml.rst | 198 +++++++++++++++--- 2 files changed, 171 insertions(+), 162 deletions(-) diff --git a/source/specifications/build-system-interface.rst b/source/specifications/build-system-interface.rst index d6ea8ced57..ed94004a38 100644 --- a/source/specifications/build-system-interface.rst +++ b/source/specifications/build-system-interface.rst @@ -50,93 +50,15 @@ combination of wheels and sdists. In a command like ``pip install lxml==2.4.0``, pip is acting as an integration frontend. -.. _build-system-interface-source-trees: - -Source trees -============ - -There is an existing, legacy source tree format involving -``setup.py``. We don't try to specify it further; its de facto -specification is encoded in the source code and documentation of -``distutils``, ``setuptools``, ``pip``, and other tools. We'll refer -to it as the ``setup.py``\-style. - -Here we define a new style of source tree based around the -``pyproject.toml`` file defined in :pep:`518`, extending the -``[build-system]`` table in that file with one additional key, -``build-backend``. Here's an example of how it would look:: - - [build-system] - # Defined by PEP 518: - requires = ["flit"] - # Defined by this specification: - build-backend = "flit.api:main" - -``build-backend`` is a string naming a Python object that will be -used to perform the build (see below for details). This is formatted -following the same ``module:object`` syntax as a ``setuptools`` entry -point. For instance, if the string is ``"flit.api:main"`` as in the -example above, this object would be looked up by executing the -equivalent of:: - - import flit.api - backend = flit.api.main - -It's also legal to leave out the ``:object`` part, e.g. :: - - build-backend = "flit.api" - -which acts like:: - - import flit.api - backend = flit.api - -Formally, the string should satisfy this grammar:: - - identifier = (letter | '_') (letter | '_' | digit)* - module_path = identifier ('.' identifier)* - object_path = identifier ('.' identifier)* - entry_point = module_path (':' object_path)? - -And we import ``module_path`` and then lookup -``module_path.object_path`` (or just ``module_path`` if -``object_path`` is missing). - -When importing the module path, we do *not* look in the directory containing the -source tree, unless that would be on ``sys.path`` anyway (e.g. because it is -specified in PYTHONPATH). Although Python automatically adds the working -directory to ``sys.path`` in some situations, code to resolve the backend should -not be affected by this. - -If the ``pyproject.toml`` file is absent, or the ``build-backend`` -key is missing, the source tree is not using this specification, and -tools should revert to the legacy behaviour of running ``setup.py`` (either -directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__`` -backend). - -Where the ``build-backend`` key exists, this takes precedence and the source tree follows the format and -conventions of the specified backend (as such no ``setup.py`` is needed unless the backend requires it). -Projects may still wish to include a ``setup.py`` for compatibility with tools that do not use this spec. - -This specification also defines a ``backend-path`` key for use in ``pyproject.toml``, see -the "In-Tree Build Backends" section below. This key would be used as follows:: - - [build-system] - # Defined by PEP 518: - requires = ["flit"] - # Defined by this specification: - build-backend = "local_backend" - backend-path = ["backend"] - - .. _build-system-interface-build-requirements: Build requirements ------------------- +================== -This specification places a number of additional requirements on the "build requirements" -section of ``pyproject.toml``. These are intended to ensure that projects do -not create impossible to satisfy conditions with their build requirements. +This specification places additional requirements on the +:ref:`build requirements ` declared in +``pyproject.toml``. These are intended to ensure that projects do not create +impossible to satisfy conditions with their build requirements. - Project build requirements will define a directed graph of requirements (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. Note in particular that the requirement for no requirement cycles means that backends wishing to self-host (i.e., building a wheel for a backend uses that backend for the build) need to make special provision to avoid causing cycles. -Typically this will involve specifying themselves as an in-tree backend, and +Typically this will involve specifying themselves as an +:ref:`in-tree backend `, and avoiding external build dependencies (usually by vendoring them). @@ -505,50 +428,6 @@ package *authors*, while still allowing *end-users* to open up the hood and apply duct tape when necessary. -.. _build-system-interface-in-tree-build-backends: - -In-tree build backends ----------------------- - -In certain circumstances, projects may wish to include the source code for the -build backend directly in the source tree, rather than referencing the backend -via the ``requires`` key. Two specific situations where this would be expected -are: - -- Backends themselves, which want to use their own features for building - themselves ("self-hosting backends") -- Project-specific backends, typically consisting of a custom wrapper around a - standard backend, where the wrapper is too project-specific to be worth - distributing independently ("in-tree backends") - -Projects can specify that their backend code is hosted in-tree by including the -``backend-path`` key in ``pyproject.toml``. This key contains a list of -directories, which the frontend will add to the start of ``sys.path`` when -loading the backend, and running the backend hooks. - -There are two restrictions on the content of the ``backend-path`` key: - -- Directories in ``backend-path`` are interpreted as relative to the project - root, and MUST refer to a location within the source tree (after relative - paths and symbolic links have been resolved). -- The backend code MUST be loaded from one of the directories specified in - ``backend-path`` (i.e., it is not permitted to specify ``backend-path`` and - *not* have in-tree backend code). - -The first restriction is to ensure that source trees remain self-contained, -and cannot refer to locations outside of the source tree. Frontends SHOULD -check this condition (typically by resolving the location to an absolute path -and resolving symbolic links, and then checking it against the project root), -and fail with an error message if it is violated. - -The ``backend-path`` feature is intended to support the implementation of -in-tree backends, and not to allow configuration of existing backends. The -second restriction above is specifically to ensure that this is how the feature -is used. Front ends MAY enforce this check, but are not required to. Doing so -would typically involve checking the backend's ``__file__`` attribute against -the locations in ``backend-path``. - - .. _build-system-interface-source-distributions: Source distributions diff --git a/source/specifications/pyproject-toml.rst b/source/specifications/pyproject-toml.rst index 43fdb4b844..7cd0a79887 100644 --- a/source/specifications/pyproject-toml.rst +++ b/source/specifications/pyproject-toml.rst @@ -24,21 +24,27 @@ use (tool-specific configuration should use the ``[tool]`` table). .. _pyproject-build-system-table: -Declaring build system dependencies: the ``[build-system]`` table -================================================================= +Declaring the build system: the ``[build-system]`` table +======================================================== -The ``[build-system]`` table declares any Python level dependencies that -must be installed in order to run the project's build system -successfully. +The ``[build-system]`` table stores build-related data. The valid keys are +``requires``, ``build-backend``, and ``backend-path``. The +:ref:`build-system-interface` specification describes how the configured +backend is invoked. -.. TODO: merge with PEP 517 +.. _pyproject-build-system-requires: -The ``[build-system]`` table is used to store build-related data. -Initially, only one key of the table is valid and is mandatory -for the table: ``requires``. This key must have a value of a list -of strings representing dependencies required to execute the -build system. The strings in this list follow the :ref:`dependency specifier -specification `. +``requires`` +------------ + +The ``requires`` key declares the Python level dependencies that must be +installed in order to run the project's build system successfully. +This key is mandatory if the table is present. Its value must be a list +of strings representing dependencies required to execute the build system. +The strings in this list follow the :ref:`dependency specifier specification +`. See also the +:ref:`requirements on build dependency graphs +`. An example ``[build-system]`` table for a project built with ``setuptools`` is: @@ -65,38 +71,158 @@ Tools may choose to present an error to the user if the file exists, that the project should be built (e.g., no setup.py/setup.cfg or other build configuration files, and no ``[project]`` table). -To provide a type-specific representation of the resulting data from -the TOML file for illustrative purposes only, the following -`JSON Schema `_ would match the data format: +.. _pyproject-build-system-build-backend: +.. _build-system-interface-source-trees: + +``build-backend`` +----------------- + +There is an existing, legacy source tree format involving +``setup.py``. We don't try to specify it further; its de facto +specification is encoded in the source code and documentation of +``distutils``, ``setuptools``, ``pip``, and other tools. We'll refer +to it as the ``setup.py``\-style. + +Projects can select their build backend using the optional ``build-backend`` +key. Here's an example of how it would look:: + + [build-system] + # Defined by PEP 518: + requires = ["flit"] + # Defined by this specification: + build-backend = "flit.api:main" + +``build-backend`` is a string naming a Python object that will be +used to perform the build (see :ref:`build-system-interface` for details). +This is formatted following the same ``module:object`` syntax as a ``setuptools`` entry +point. For instance, if the string is ``"flit.api:main"`` as in the +example above, this object would be looked up by executing the +equivalent of:: + + import flit.api + backend = flit.api.main + +It's also legal to leave out the ``:object`` part, e.g. :: + + build-backend = "flit.api" + +which acts like:: + + import flit.api + backend = flit.api + +Formally, the string should satisfy this grammar:: + + identifier = (letter | '_') (letter | '_' | digit)* + module_path = identifier ('.' identifier)* + object_path = identifier ('.' identifier)* + entry_point = module_path (':' object_path)? + +And we import ``module_path`` and then lookup +``module_path.object_path`` (or just ``module_path`` if +``object_path`` is missing). + +When importing the module path, we do *not* look in the directory containing the +source tree, unless that would be on ``sys.path`` anyway (e.g. because it is +specified in PYTHONPATH). Although Python automatically adds the working +directory to ``sys.path`` in some situations, code to resolve the backend should +not be affected by this. + +If the ``pyproject.toml`` file is absent, or the ``build-backend`` +key is missing, the source tree is not using the :ref:`build-system-interface` +specification, and tools should revert to the legacy behaviour of running ``setup.py`` (either +directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__`` +backend). + +Where the ``build-backend`` key exists, this takes precedence and the source tree follows the format and +conventions of the specified backend (as such no ``setup.py`` is needed unless the backend requires it). +Projects may still wish to include a ``setup.py`` for compatibility with tools +that do not use the build system interface specification. + +.. _pyproject-build-system-backend-path: +.. _build-system-interface-in-tree-build-backends: + +``backend-path`` +---------------- + +The optional ``backend-path`` key can be used as follows:: + + [build-system] + # Defined by PEP 518: + requires = ["flit"] + # Defined by this specification: + build-backend = "local_backend" + backend-path = ["backend"] + +In certain circumstances, projects may wish to include the source code for the +build backend directly in the source tree, rather than referencing the backend +via the ``requires`` key. Two specific situations where this would be expected +are: + +- Backends themselves, which want to use their own features for building + themselves ("self-hosting backends") +- Project-specific backends, typically consisting of a custom wrapper around a + standard backend, where the wrapper is too project-specific to be worth + distributing independently ("in-tree backends") + +Projects can specify that their backend code is hosted in-tree by including the +``backend-path`` key in ``pyproject.toml``. This key contains a list of +directories, which the frontend will add to the start of ``sys.path`` when +loading the backend, and running the backend hooks. + +There are two restrictions on the content of the ``backend-path`` key: + +- Directories in ``backend-path`` are interpreted as relative to the project + root, and MUST refer to a location within the source tree (after relative + paths and symbolic links have been resolved). +- The backend code MUST be loaded from one of the directories specified in + ``backend-path`` (i.e., it is not permitted to specify ``backend-path`` and + *not* have in-tree backend code). + +The first restriction is to ensure that source trees remain self-contained, +and cannot refer to locations outside of the source tree. Frontends SHOULD +check this condition (typically by resolving the location to an absolute path +and resolving symbolic links, and then checking it against the project root), +and fail with an error message if it is violated. + +The ``backend-path`` feature is intended to support the implementation of +in-tree backends, and not to allow configuration of existing backends. The +second restriction above is specifically to ensure that this is how the feature +is used. Front ends MAY enforce this check, but are not required to. Doing so +would typically involve checking the backend's ``__file__`` attribute against +the locations in ``backend-path``. + +Illustrative JSON schema +------------------------ + +To provide a type-specific representation of the ``[build-system]`` table +for illustrative purposes only, the following +`JSON Schema `_ would match its data format: .. code-block:: json { "$schema": "http://json-schema.org/schema#", - "type": "object", "additionalProperties": false, - "properties": { - "build-system": { - "type": "object", - "additionalProperties": false, - - "properties": { - "requires": { - "type": "array", - "items": { - "type": "string" - } - } - }, - "required": ["requires"] + "requires": { + "type": "array", + "items": { + "type": "string" + } }, - - "tool": { - "type": "object" + "build-backend": { + "type": "string" + }, + "backend-path": { + "type": "array", + "items": { + "type": "string" + } } - } + }, + "required": ["requires"] } @@ -703,6 +829,10 @@ History a ``[build-system]`` containing a ``requires`` key and a ``[tool]`` table, was approved through :pep:`518`. +- September 2017: The ``build-backend`` key was approved through :pep:`517`. + +- March 2019: The ``backend-path`` key was added through an amendment to :pep:`517`. + - November 2020: The specification of the ``[project]`` table was approved through :pep:`621`. From 297d78fbcca782bf6e8446cbd0d5921533536e91 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 11 Nov 2026 15:50:09 +0000 Subject: [PATCH 4/6] Reference the existing sdist format from the build interface Replace duplicate PEP 517 archive descriptions with links to the current source distribution specification, which already incorporates later metadata and archive requirements. Retain the hook's requirement to include pyproject.toml from the build directory and the requirement that sdists produce wheels with the corresponding name and version. Point the sdist source-tree definition at the local configuration and interface specifications instead of sending readers to PEPs 517 and 518. Preserve all UnsupportedOperation and intermediate-sdist fallback text. Closes #388; addresses part of #955 and #1093. --- .../specifications/build-system-interface.rst | 35 ++----------------- .../source-distribution-format.rst | 6 ++-- 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/source/specifications/build-system-interface.rst b/source/specifications/build-system-interface.rst index ed94004a38..3a441253be 100644 --- a/source/specifications/build-system-interface.rst +++ b/source/specifications/build-system-interface.rst @@ -149,20 +149,8 @@ Must build a .tar.gz source distribution and place it in the specified ``sdist_directory``. It must return the basename (not the full path) of the ``.tar.gz`` file it creates, as a unicode string. -A .tar.gz source distribution (sdist) contains a single top-level directory called -``{name}-{version}`` (e.g. ``foo-1.0``), containing the source files of the -package. This directory must also contain the -``pyproject.toml`` from the build directory, and a PKG-INFO file containing -metadata in the format described in -:pep:`345`. Although historically -zip files have also been used as sdists, this hook should produce a gzipped -tarball. This is already the more common format for sdists, and having a -consistent format makes for simpler tooling. - -The generated tarball should use the modern POSIX.1-2001 pax tar format, which -specifies UTF-8 based file names. This is not yet the default for the tarfile -module shipped with Python 3.6, so backends using the tarfile module need to -explicitly pass ``format=tarfile.PAX_FORMAT``. +The source distribution must conform to :ref:`source-distribution-format` +and contain the ``pyproject.toml`` from the build directory. Some backends may have extra requirements for creating sdists, such as version control tools. However, some frontends may prefer to make intermediate sdists @@ -433,29 +421,12 @@ hood and apply duct tape when necessary. Source distributions ==================== -We continue with the legacy sdist format, adding some new restrictions. -This format is mostly -undefined, but basically comes down to: a file named -``{NAME}-{VERSION}.{EXT}``, which unpacks into a buildable source tree -called ``{NAME}-{VERSION}/``. Traditionally these have always -contained ``setup.py``\-style source trees; we now allow them to also -contain ``pyproject.toml``\-style source trees. +The :ref:`source-distribution-format` specification defines the archive format. Integration frontends require that an sdist named ``{NAME}-{VERSION}.{EXT}`` will generate a wheel named ``{NAME}-{VERSION}-{COMPAT-INFO}.whl``. -The new restrictions for sdists built by :pep:`517` backends are: - -- They will be gzipped tar archives, with the ``.tar.gz`` extension. Zip - archives, or other compression formats for tarballs, are not allowed at - present. -- Tar archives must be created in the modern POSIX.1-2001 pax tar format, which - uses UTF-8 for file names. -- The source tree contained in an sdist is expected to include the - ``pyproject.toml`` file. - - History ======== diff --git a/source/specifications/source-distribution-format.rst b/source/specifications/source-distribution-format.rst index 569173f9d1..9ec149d8a0 100644 --- a/source/specifications/source-distribution-format.rst +++ b/source/specifications/source-distribution-format.rst @@ -28,9 +28,9 @@ Source trees A *source tree* is a collection of files and directories -- like a version control system checkout -- which contains a :file:`pyproject.toml` file that can be used to build a source distribution from the contained files and -directories. :pep:`517` and :pep:`518` specify what is required to meet the -definition of what :file:`pyproject.toml` must contain for something to be -deemed a source tree. +directories. The :ref:`[build-system] table ` +specifies the build system configuration, and :ref:`build-system-interface` +defines how tools use it to build source distributions and wheels. .. _source-distribution-format-sdist: From 71d702b96e311aa6252f719ed5a449236ca4ced8 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 11 Nov 2026 15:52:41 +0000 Subject: [PATCH 5/6] Cross-link the migrated build specifications and glossary Link the existing frontend and backend definitions to the interface, and replace normative PEP references with local specification links. Connect backend lookup, dependency specifiers, wheel metadata, and sdist creation without changing the glossary terminology or historical PEP citations. Correct two inherited spellings flagged by codespell. Validation: pixi run build; scoped pre-commit checks; rendered heading hierarchy and 387 links between changed pages; illustrative schema cases. Also compare the unchanged hook signatures, config_settings and invocation text, in-tree restrictions, and build environment recommendations with the imported PEP, accounting for the documented editorial edits. --- source/glossary.rst | 8 +++-- .../binary-distribution-format.rst | 2 ++ .../specifications/build-system-interface.rst | 35 ++++++++++--------- source/specifications/pyproject-toml.rst | 10 +++--- .../source-distribution-format.rst | 3 +- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/source/glossary.rst b/source/glossary.rst index ab32f077b2..e5660b2829 100644 --- a/source/glossary.rst +++ b/source/glossary.rst @@ -19,7 +19,7 @@ Glossary :term:`built distribution ` from it. The build is delegated to the backend by a :term:`frontend `. - All backends offer a standardized interface. + All backends offer a :ref:`standardized interface `. Examples of build backends are :ref:`flit's flit-core `, @@ -40,6 +40,7 @@ Glossary :term:`build backend `. Examples of build frontends are :ref:`pip` and :ref:`build`. + See :ref:`build-system-interface` for the standardized interface they use. Built Distribution @@ -207,7 +208,8 @@ Glossary packaged into a :term:`Distribution `. Since most projects create :term:`Distributions ` - using either :pep:`518` ``build-system``, :ref:`distutils` or + using either the :ref:`[build-system] table `, + :ref:`distutils` or :ref:`setuptools`, another practical way to define projects currently is something that contains a :term:`pyproject.toml`, :term:`setup.py`, or :term:`setup.cfg` file at the root of the project source directory. @@ -309,7 +311,7 @@ Glossary pyproject.toml The tool-agnostic :term:`Project` specification file. - Defined in :pep:`518`. + Defined in :ref:`pyproject-toml-spec`. Release diff --git a/source/specifications/binary-distribution-format.rst b/source/specifications/binary-distribution-format.rst index b2814f50c7..1704df9295 100644 --- a/source/specifications/binary-distribution-format.rst +++ b/source/specifications/binary-distribution-format.rst @@ -8,6 +8,8 @@ Wheel distribution format ========================= This page specifies the wheel format for Python packages. +The :ref:`build-system-interface` specification describes how wheels are built +from source trees. A wheel is a ZIP-format archive with a specially formatted file name and the ``.whl`` extension. It contains a single distribution nearly as it diff --git a/source/specifications/build-system-interface.rst b/source/specifications/build-system-interface.rst index 3a441253be..4289ebebcb 100644 --- a/source/specifications/build-system-interface.rst +++ b/source/specifications/build-system-interface.rst @@ -4,8 +4,9 @@ Build system interface ====================== -This specification defines a standard interface between build frontends and -build backends to build wheels and source distributions from source trees. +This specification defines a standard interface between +:term:`build frontends ` and :term:`build backends ` +to build wheels and source distributions from source trees. The project's build system is configured in the :ref:`[build-system] table ` of :file:`pyproject.toml`. @@ -85,9 +86,10 @@ avoiding external build dependencies (usually by vendoring them). Build backend interface ======================= -The build backend object is expected to have attributes which provide -some or all of the following hooks. The common ``config_settings`` -argument is described after the individual hooks. +The :ref:`build backend object ` is expected +to have attributes which provide some or all of the following hooks. The common +``config_settings`` argument is described in +:ref:`build-system-interface-config-settings`. Mandatory hooks --------------- @@ -122,7 +124,8 @@ or else raise an exception when it is set to anything other than ``None``. To ensure that wheels from different sources are built the same way, frontends may call ``build_sdist`` first, and then call ``build_wheel`` in the unpacked sdist. But if the backend indicates that it is missing some requirements for -creating an sdist (see below), the frontend will fall back to calling +creating an sdist (see :ref:`build-system-interface-build-sdist`), +the frontend will fall back to calling ``build_wheel`` in the source directory. The source directory may be read-only. Backends should therefore be @@ -176,9 +179,9 @@ get_requires_for_build_wheel def get_requires_for_build_wheel(config_settings=None): ... -This hook MUST return an additional list of strings containing :pep:`508` -dependency specifications, above and beyond those specified in the -``pyproject.toml`` file, to be installed when calling the ``build_wheel`` or +This hook MUST return an additional list of strings containing +:ref:`dependency specifiers `, above and beyond those +specified in the ``pyproject.toml`` file, to be installed when calling the ``build_wheel`` or ``prepare_metadata_for_build_wheel`` hooks. Example:: @@ -202,13 +205,13 @@ Must create a ``.dist-info`` directory containing wheel metadata inside the specified ``metadata_directory`` (i.e., creates a directory like ``{metadata_directory}/{package}-{version}.dist-info/``). This directory MUST be a valid ``.dist-info`` directory as defined in the -wheel specification, except that it need not contain ``RECORD`` or -signatures. The hook MAY also create other files inside this +:ref:`wheel specification `, except that it need not +contain ``RECORD`` or signatures. The hook MAY also create other files inside this directory, and a build frontend MUST preserve, but otherwise ignore, such files; the intention here is that in cases where the metadata depends on build-time decisions, the build backend may need to record these decisions in -some convenient format for re-use by the actual wheel-building step. +some convenient format for reuse by the actual wheel-building step. This must return the basename (not the full path) of the ``.dist-info`` directory it creates, as a unicode string. @@ -227,9 +230,9 @@ get_requires_for_build_sdist def get_requires_for_build_sdist(config_settings=None): ... -This hook MUST return an additional list of strings containing :pep:`508` -dependency specifications, above and beyond those specified in the -``pyproject.toml`` file. These dependencies will be installed when calling the +This hook MUST return an additional list of strings containing +:ref:`dependency specifiers `, above and beyond those +specified in the ``pyproject.toml`` file. These dependencies will be installed when calling the ``build_sdist`` hook. If not defined, the default implementation is equivalent to ``return []``. @@ -369,7 +372,7 @@ environment that meets the above criteria. For example, simply installing all build-requirements into the global environment would be sufficient to build any compliant package -- but this would be sub-optimal for a number of reasons. This section contains -non-normative advice to frontend implementors. +non-normative advice to frontend implementers. A build frontend SHOULD, by default, create an isolated environment for each build, containing only the standard library and any diff --git a/source/specifications/pyproject-toml.rst b/source/specifications/pyproject-toml.rst index 7cd0a79887..5b2bdef2f6 100644 --- a/source/specifications/pyproject-toml.rst +++ b/source/specifications/pyproject-toml.rst @@ -94,9 +94,9 @@ key. Here's an example of how it would look:: ``build-backend`` is a string naming a Python object that will be used to perform the build (see :ref:`build-system-interface` for details). -This is formatted following the same ``module:object`` syntax as a ``setuptools`` entry -point. For instance, if the string is ``"flit.api:main"`` as in the -example above, this object would be looked up by executing the +This is formatted following the same ``module:object`` syntax as an +:ref:`entry point `. For instance, if the string is +``"flit.api:main"`` as in the example above, this object would be looked up by executing the equivalent of:: import flit.api @@ -130,8 +130,8 @@ not be affected by this. If the ``pyproject.toml`` file is absent, or the ``build-backend`` key is missing, the source tree is not using the :ref:`build-system-interface` -specification, and tools should revert to the legacy behaviour of running ``setup.py`` (either -directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__`` +specification, and tools should revert to the legacy behaviour of running +``setup.py`` (either directly, or by implicitly invoking the ``setuptools.build_meta:__legacy__`` backend). Where the ``build-backend`` key exists, this takes precedence and the source tree follows the format and diff --git a/source/specifications/source-distribution-format.rst b/source/specifications/source-distribution-format.rst index 9ec149d8a0..b5e96c69e4 100644 --- a/source/specifications/source-distribution-format.rst +++ b/source/specifications/source-distribution-format.rst @@ -47,7 +47,8 @@ The name and version components of the filename MUST match the values stored in the metadata contained in the file. Code that produces a source distribution file MUST give the file a name that matches -this specification. This includes the ``build_sdist`` hook of a +this specification. This includes the +:ref:`build_sdist ` hook of a :term:`build backend `. Code that processes source distribution files MAY recognise source distribution files From e1e2b3f4ef686232a35d578d0d9b4766f21d27b9 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Thu, 12 Nov 2026 18:35:12 +0000 Subject: [PATCH 6/6] Remove PEP migration leftovers and refresh examples --- .../specifications/build-system-interface.rst | 11 +++----- source/specifications/pyproject-toml.rst | 27 +++++++------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/source/specifications/build-system-interface.rst b/source/specifications/build-system-interface.rst index 4289ebebcb..0685fc6224 100644 --- a/source/specifications/build-system-interface.rst +++ b/source/specifications/build-system-interface.rst @@ -16,9 +16,7 @@ The project's build system is configured in the Terminology =========== -A *source tree* is something like a VCS checkout. We need a standard -interface for installing from this format, to support usages like -``pip install some-directory/``. +A *source tree* is something like a VCS checkout. A *source distribution* is a static snapshot representing a particular release of some source code, like ``lxml-3.4.4.tar.gz``. Source @@ -262,11 +260,10 @@ For example, they might support some syntax like ``--package-config CC=gcc``. In case a user provides duplicate string-keys, build frontends SHOULD combine the corresponding string-values into a list of strings. Build frontends MAY also provide arbitrary other mechanisms -for users to place entries in this dictionary. For example, ``pip`` -might choose to map a mix of modern and legacy command line arguments -like:: +for users to place entries in this dictionary. For example, a frontend +might map command line arguments like:: - pip install \ + build-tool build \ --package-config CC=gcc \ --global-option="--some-global-option" \ --build-option="--build-option1" \ diff --git a/source/specifications/pyproject-toml.rst b/source/specifications/pyproject-toml.rst index 5b2bdef2f6..682f48c009 100644 --- a/source/specifications/pyproject-toml.rst +++ b/source/specifications/pyproject-toml.rst @@ -72,7 +72,6 @@ that the project should be built (e.g., no setup.py/setup.cfg or other build configuration files, and no ``[project]`` table). .. _pyproject-build-system-build-backend: -.. _build-system-interface-source-trees: ``build-backend`` ----------------- @@ -80,36 +79,33 @@ build configuration files, and no ``[project]`` table). There is an existing, legacy source tree format involving ``setup.py``. We don't try to specify it further; its de facto specification is encoded in the source code and documentation of -``distutils``, ``setuptools``, ``pip``, and other tools. We'll refer -to it as the ``setup.py``\-style. +``setuptools``, ``pip``, and other tools. Projects can select their build backend using the optional ``build-backend`` key. Here's an example of how it would look:: [build-system] - # Defined by PEP 518: requires = ["flit"] - # Defined by this specification: - build-backend = "flit.api:main" + build-backend = "flit.buildapi" ``build-backend`` is a string naming a Python object that will be used to perform the build (see :ref:`build-system-interface` for details). This is formatted following the same ``module:object`` syntax as an :ref:`entry point `. For instance, if the string is -``"flit.api:main"`` as in the example above, this object would be looked up by executing the -equivalent of:: +``"flit.buildapi"`` as in the example above, this object would +be looked up by executing the equivalent of:: - import flit.api - backend = flit.api.main + import flit.buildapi + backend = flit.buildapi -It's also legal to leave out the ``:object`` part, e.g. :: +It's also legal to specify an object within a module, e.g. :: - build-backend = "flit.api" + build-backend = "example_backend.build:backend" which acts like:: - import flit.api - backend = flit.api + import example_backend.build + backend = example_backend.build.backend Formally, the string should satisfy this grammar:: @@ -140,7 +136,6 @@ Projects may still wish to include a ``setup.py`` for compatibility with tools that do not use the build system interface specification. .. _pyproject-build-system-backend-path: -.. _build-system-interface-in-tree-build-backends: ``backend-path`` ---------------- @@ -148,9 +143,7 @@ that do not use the build system interface specification. The optional ``backend-path`` key can be used as follows:: [build-system] - # Defined by PEP 518: requires = ["flit"] - # Defined by this specification: build-backend = "local_backend" backend-path = ["backend"]