Lines Matching +full:build +full:- +full:python
1 ---
2 title: Design for a Python Toolchain
4 created: 2019-02-12
5 updated: 2019-02-21
7 - [brandjon@](https://github.com/brandjon)
9 …- [katre@](https://github.com/katre), [mrovner@](https://github.com/mrovner), [nlopezgi@](https://…
11 ---
13 # Design for a Python Toolchain
17 …Python toolchain rule and its associated machinery. Essentially a new `py_runtime_pair` toolchain …
19 …-related definitions are implemented in Starlark. A byproduct of this is that the provider type fo…
23 The goal is to make the native Python rules use the toolchain framework to resolve the Python runti…
27 … allowing Python 2 and Python 3 targets to run in the same build without [hacks](https://github.co…
29 * making it easier to run Python-related builds under remote execution
31 * adding support for autodetection of available system Python runtimes, without requiring ad hoc ru…
33 * removing `--python_top` and `--python_path`
35 * bringing Python in line with other rule sets and Bazel's best practices
37 **Non-goal:** This work does not allow individual `py_binary`s to directly name a Python runtime to…
43 …build/versions/master/toolchains.html#writing-rules-that-use-toolchains) is created at `@bazel_too…
45 Toolchain rules of this type are expected to return a [`ToolchainInfo`](https://docs.bazel.build/ve…
47 ```python
55 …Python 2 or Python 3 is not provided by the toolchain, the corresponding field may be set to `None…
57 …-exposed Starlark name of the native provider returned by the [`py_runtime`](https://docs.bazel.bu…
59 …-build runtime*. A platform runtime accesses a system-installed interpreter at a known path, where…
61 …build/versions/master/be/platform.html#constraint_setting)s to act as a standardized namespace for…
67 * `interpreter`: If this is an in-build runtime, this field is a `File` representing the interprete…
69 * `files`: If this is an in-build runtime, this field is a depset of `File`s that need to be added …
71 * `python_version`: Either the string `"PY2"` or `"PY3"`, indicating which version of Python the in…
75 It is not possible to directly specify a system command (e.g. `"python"`) in `interpreter_path`. Ho…
77 …Python toolchain rule implementing the new toolchain type. The rule's name is `py_runtime_pair` an…
79 ### Changes to the native Python rules
81 …Python rules [`py_binary`](https://docs.bazel.build/versions/master/be/python.html#py_binary) and …
83 Since `--python_top` is no longer read, it is deprecated. Since `--python_path` was only read when …
85 Implementation wise, the native `PyRuntimeProvider` is turned into the user-visible `PyRuntimeInfo`…
89 …-by cleanup (and non-breaking change), the `files` attribute of `py_runtime` is made optional. For…
93 …build/versions/master/be/platform.html#toolchain) of last resort, `@bazel_tools//tools/python:auto…
97 …that defines a platform whose Python interpreters are located under a non-standard path. The examp…
99 ```python
100 # //platform_defs:BUILD
102 load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
109 constraint_setting = "@bazel_tools//tools/python:py2_interpreter_path",
114 constraint_setting = "@bazel_tools//tools/python:py3_interpreter_path",
117 # A definition of a platform whose Python interpreters are under these paths.
127 # Python runtime definitions that reify these system paths as BUILD targets.
149 # Since the Python interpreter is invoked at runtime on the target
158 toolchain_type = "@bazel_tools//tools/python:toolchain_type",
162 ```python
163 # //pkg:BUILD
165 # An ordinary Python target to build.
173 ```python
176 # Register the custom Python toolchain so it can be chosen for my_platform.
182 We can then build with
185 bazel build //pkg:my_pybin --platforms=//platform_defs:my_platform
194 …--incompatible_use_python_toolchains`, is created to assist migration. When the flag is enabled, `…
196 …python:toolchain_type` immediately and unconditionally. This may impact how toolchain resolution d…
198 …attribute for `py_binary`, i.e. `PY3` if `--incompatible_py3_is_default` is true and `PY2` otherwi…
202 …How can I force a `py_binary` to use a given runtime, say for a particular minor version of Python?
204 …e between different Python implementations (CPython vs PyPy), compilation modes (optimized, debug)…
208 … `PyRuntimeInfo` to return. We'd use Starlark Build Configurations to define a flag to represent t…
212 …Python major version to use this approach. Instead of having two different `ToolchainInfo` fields,…
214 …ution platforms, and this is not a platform-related constraint. What would be needed is a per-targ…
216 … a massive `select()`. But the first approach is much more feasible to implement in the short-term.
218 #### Why `py_runtime_pair` as opposed to some other way of organizing multiple Python runtimes?
222 …Python rule set and indeed the Python ecosystem at large. Keeping this concept in the toolchain ru…
224 … `ToolchainInfo` accepted by the Python rules, and then by defining new user-facing toolchain rule…
226 #### Why not split Python 2 and Python 3 into two separate toolchain types?
230 If the way of categorizing Python runtimes changes in the future, it will probably be easier to mig…
232 …of new symbols to `@bazel_tools` affect the eventual plan to migrate the Python rules to `bazelbui…
238 …'s workspace by Bazel. This follows precedent for other languages with built-in support in Bazel. …
243 ------------ | ------
244 2019-02-12 | Initial version
245 2019-02-14 | Make `PyRuntimeInfo` natively defined
246 2019-02-15 | Clarify platform runtime vs in-build runtime
247 2019-02-21 | Formal approval