@@ -236,6 +236,79 @@ existing attributes:
236236* Adding additional Python versions via {bzl: obj }` python.single_version_override ` or
237237 {bzl: obj }` python.single_version_platform_override ` .
238238
239+ ### Registering custom runtimes
240+
241+ Because the python-build-standalone project has _ thousands_ of prebuilt runtimes
242+ available, rules_python only includes popular runtimes in its built in
243+ configurations. If you want to use a runtime that isn't already known to
244+ rules_python then {obj}` single_version_platform_override() ` can be used to do
245+ so. In short, it allows specifying an arbitrary URL and using custom flags
246+ to control when a runtime is used.
247+
248+ In the example below, we register a particular python-build-standalone runtime
249+ that is activated for Linux x86 builds when the custom flag
250+ ` --//:runtime=my-custom-runtime ` is set.
251+
252+ ```
253+ # File: MODULE.bazel
254+ bazel_dep(name = "bazel_skylib", version = "1.7.1.")
255+ bazel_dep(name = "rules_python", version = "1.5.0")
256+ python = use_extension("@rules_python//python/extensions:python.bzl", "python")
257+ python.toolchain(python_version="3.13.3")
258+ python.single_version_platform_override(
259+ platform = "my-platform",
260+ python_version = "3.13.3",
261+ sha256 = "01d08b9bc8a96698b9d64c2fc26da4ecc4fa9e708ce0a34fb88f11ab7e552cbd",
262+ target_compatible_with = [
263+ "@platforms//os:linux",
264+ "@platforms//cpu:x86_64",
265+ ],
266+ target_settings = [
267+ "@@//:runtime=my-custom-runtime",
268+ ],
269+ urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250409/cpython-3.13.3+20250409-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"],
270+ )
271+
272+ # File: //:BUILD.bazel
273+
274+ load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
275+ string_flag(
276+ name = "custom_runtime",
277+ build_setting_default = "",
278+ )
279+
280+ config_setting(
281+ name = "is_custom_runtime_linux-x86-install-only-stripped",
282+ flag_values = {
283+ ":custom_runtime": "linux-x86-install-only-stripped",
284+ },
285+ )
286+ ```
287+
288+ Notes:
289+ - While any URL and archive can be used, it's assumed their content looks how
290+ a python-build-standalone archive looks.
291+ - ` python.toolchain() ` is required if the version is unknown; if the version
292+ is already known, it can be omitted.
293+ - A "version aware" toolchain is registered, which means the Python version flag
294+ must also match (e.g. ` --@rules_python//python/config_settings:python_version=3.13.3 `
295+ must be set -- see ` minor_mapping ` and ` is_default ` for controls and docs
296+ about version matching and selection).
297+ - The labels in ` target_settings ` must be absolute; ` @@ ` refers to the main repo.
298+ - The ` target_settings ` are ` config_setting ` targets, which means you can
299+ customize how matching occurs.
300+
301+ :::{seealso}
302+ See {obj}` //python/config_settings ` for flags rules_python already defines
303+ that can be used with ` target_settings ` . Some particular ones of note are:
304+ {flag}` --py_linux_libc ` and {flag}` --py_freethreaded ` , among others.
305+ :::
306+
307+ :::{versionadded} VERSION_NEXT_FEATURE
308+ Added support for custom platform names, ` target_compatible_with ` , and
309+ ` target_settings ` with ` single_version_platform_override ` .
310+ :::
311+
239312### Using defined toolchains from WORKSPACE
240313
241314It is possible to use toolchains defined in ` MODULE.bazel ` in ` WORKSPACE ` . For example
0 commit comments