From c53ada90b969b1d00f0295d601b6a196c77db66b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Mon, 14 Oct 2024 10:49:24 +0900 Subject: [PATCH 01/24] chore: add freethreaded interpreters to 3.13 def --- python/config_settings/BUILD.bazel | 10 + python/private/toolchains_repo.bzl | 3 + python/versions.bzl | 286 +++++++++++++++++------------ 3 files changed, 180 insertions(+), 119 deletions(-) diff --git a/python/config_settings/BUILD.bazel b/python/config_settings/BUILD.bazel index c530afe98b..f0196d7fe6 100644 --- a/python/config_settings/BUILD.bazel +++ b/python/config_settings/BUILD.bazel @@ -92,6 +92,16 @@ string_flag( visibility = ["//visibility:public"], ) +string_flag( + name = "py_freethreaded", + build_setting_default = "no", + values = [ + "yes", + "no", + ], + visibility = ["//visibility:public"], +) + # pip.parse related flags string_flag( diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl index d21e46ac48..97a3124b6d 100644 --- a/python/private/toolchains_repo.bzl +++ b/python/private/toolchains_repo.bzl @@ -390,6 +390,9 @@ def _get_host_platform(os_name, arch): """ host_platform = None for platform, meta in PLATFORMS.items(): + if "freethreaded" in platform: + continue + if meta.os_name == os_name and meta.arch == arch: host_platform = platform if not host_platform: diff --git a/python/versions.bzl b/python/versions.bzl index ae017e3d12..52155eb023 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -561,7 +561,27 @@ TOOL_VERSIONS = { "strip_prefix": "python", }, "3.13.0": { - "url": "20241008/cpython-{python_version}+20241008-{platform}-{build}.tar.gz", + "url": { + platform + suffix: "20241008/cpython-{python_version}+20241008-" + build + for platform, opt in { + "aarch64-apple-darwin": "pgo+lto", + "aarch64-unknown-linux-gnu": "lto", + "ppc64le-unknown-linux-gnu": "lto", + "s390x-unknown-linux-gnu": "lto", + "x86_64-apple-darwin": "pgo+lto", + "x86_64-pc-windows-msvc": "pgo", + "x86_64-unknown-linux-gnu": "pgo+lto", + }.items() + for suffix, build in { + "": platform + "-{build}.tar.gz", + "-freethreaded": "".join([ + platform, + "-shared" if "windows" in platform else "", + "-freethreaded+" + opt, + "-full.tar.zst", + ]), + }.items() + }, "sha256": { "aarch64-apple-darwin": "5d3cb8d7ca4cfbbe7ae1f118f26be112ee417d982fab8c6d85cfd8ccccf70718", "aarch64-unknown-linux-gnu": "c1142af8f2c85923d2ba8201a35b913bb903a5d15f052c38bbecf2f49e2342dc", @@ -570,6 +590,14 @@ TOOL_VERSIONS = { "x86_64-apple-darwin": "b58ca12d9ae14bbd79f9e5cf4b748211ff1953e59abeac63b0f4e8e49845669f", "x86_64-pc-windows-msvc": "c7651a7a575104f47c808902b020168057f3ad80f277e54cecfaf79a9ff50e22", "x86_64-unknown-linux-gnu": "455200e1a202e9d9ef4b630c04af701c0a91dcaa6462022efc76893fc762ec95", + # Add freethreaded variants + "aarch64-apple-darwin-freethreaded": "8cc1586c4ee730bb33b7e6d39f1b6388f895075fadb1771e3c27b0561abb9242", + "aarch64-unknown-linux-gnu-freethreaded": "56b11e29095e7c183ae191bf9f5ec4e7a71ac41e9c759786faf16c707b83b6b0", + "ppc64le-unknown-linux-gnu-freethreaded": "abac77abeb3c39c355fbc2cd74216254c46bcb28dda10b525daf821bf1d364dc", + "s390x-unknown-linux-gnu-freethreaded": "9adab574543ab8c5fc0ad9e313050030dbdae85160629b1dcbbc3e9d9515a0da", + "x86_64-apple-darwin-freethreaded": "117528b68096379b1303faee1f4f9e32ef3d255207ec92fb063f1bd0b942549d", + "x86_64-pc-windows-msvc-freethreaded": "fc665561556f4dc843cd3eeba4d482f716aec65d5b89a657316829cfbdc9462a", + "x86_64-unknown-linux-gnu-freethreaded": "00a159a64640ce614bdac064b270a9854d86d40d1d8387a822daf1fe0881e64b", }, "strip_prefix": "python", }, @@ -586,121 +614,135 @@ MINOR_MAPPING = { } PLATFORMS = { - "aarch64-apple-darwin": struct( - compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:aarch64", - ], - flag_values = {}, - os_name = MACOS_NAME, - # Matches the value returned from: - # repository_ctx.execute(["uname", "-m"]).stdout.strip() - arch = "arm64", - ), - "aarch64-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - # Note: this string differs between OSX and Linux - # Matches the value returned from: - # repository_ctx.execute(["uname", "-m"]).stdout.strip() - arch = "aarch64", - ), - "armv7-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:armv7", - ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - arch = "armv7", - ), - "i386-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:i386", - ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - arch = "i386", - ), - "ppc64le-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:ppc", - ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - # Note: this string differs between OSX and Linux - # Matches the value returned from: - # repository_ctx.execute(["uname", "-m"]).stdout.strip() - arch = "ppc64le", - ), - "riscv64-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:riscv64", - ], + p + suffix: struct( + compatible_with = v.compatible_with, flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - arch = "riscv64", - ), - "s390x-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:s390x", - ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - # Note: this string differs between OSX and Linux - # Matches the value returned from: - # repository_ctx.execute(["uname", "-m"]).stdout.strip() - arch = "s390x", - ), - "x86_64-apple-darwin": struct( - compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - flag_values = {}, - os_name = MACOS_NAME, - arch = "x86_64", - ), - "x86_64-pc-windows-msvc": struct( - compatible_with = [ - "@platforms//os:windows", - "@platforms//cpu:x86_64", - ], - flag_values = {}, - os_name = WINDOWS_NAME, - arch = "x86_64", - ), - "x86_64-unknown-linux-gnu": struct( - compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, - os_name = LINUX_NAME, - arch = "x86_64", - ), + Label("//python/config_settings:py_freethreaded"): freethreaded, + } | v.flag_values, + os_name = v.os_name, + arch = v.arch, + ) + for p, v in { + "aarch64-apple-darwin": struct( + compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], + flag_values = {}, + os_name = MACOS_NAME, + # Matches the value returned from: + # repository_ctx.execute(["uname", "-m"]).stdout.strip() + arch = "arm64", + ), + "aarch64-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + # Note: this string differs between OSX and Linux + # Matches the value returned from: + # repository_ctx.execute(["uname", "-m"]).stdout.strip() + arch = "aarch64", + ), + "armv7-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:armv7", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + arch = "armv7", + ), + "i386-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:i386", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + arch = "i386", + ), + "ppc64le-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:ppc", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + # Note: this string differs between OSX and Linux + # Matches the value returned from: + # repository_ctx.execute(["uname", "-m"]).stdout.strip() + arch = "ppc64le", + ), + "riscv64-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:riscv64", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + arch = "riscv64", + ), + "s390x-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:s390x", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + # Note: this string differs between OSX and Linux + # Matches the value returned from: + # repository_ctx.execute(["uname", "-m"]).stdout.strip() + arch = "s390x", + ), + "x86_64-apple-darwin": struct( + compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + flag_values = {}, + os_name = MACOS_NAME, + arch = "x86_64", + ), + "x86_64-pc-windows-msvc": struct( + compatible_with = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], + flag_values = {}, + os_name = WINDOWS_NAME, + arch = "x86_64", + ), + "x86_64-unknown-linux-gnu": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + flag_values = { + Label("//python/config_settings:py_linux_libc"): "glibc", + }, + os_name = LINUX_NAME, + arch = "x86_64", + ), + }.items() + for suffix, freethreaded in { + "": "no", + "-freethreaded": "yes", + }.items() } def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS): @@ -760,6 +802,15 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U return (release_filename, rendered_urls, strip_prefix, patches, patch_strip) def print_toolchains_checksums(name): + """A macro to print checksums for a particular Python interpreter version. + + Args: + name: {type}`str`: the name of the runnable target. + """ + commands = [] + for python_version in TOOL_VERSIONS.keys(): + commands.append(_commands_for_version(python_version)) + native.genrule( name = name, srcs = [], @@ -775,10 +826,7 @@ echo "Fetching hashes..." {commands} EOF """.format( - commands = "\n".join([ - _commands_for_version(python_version) - for python_version in TOOL_VERSIONS.keys() - ]), + commands = "\n".join(commands), ), executable = True, ) From b182b52ad9d5f71ad80ca57bd1187ed347df75cf Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 08:55:33 +0900 Subject: [PATCH 02/24] fixup! chore: add freethreaded interpreters to 3.13 def --- MODULE.bazel | 5 ++++- examples/bzlmod/MODULE.bazel.lock | 14 ++++++++++++-- python/private/python.bzl | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index de14b86f1b..5199eea14c 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -46,7 +46,10 @@ python.toolchain( is_default = True, python_version = "3.11", ) -use_repo(python, "python_3_11", "python_versions", "pythons_hub") +python.toolchain( + python_version = "3.13", +) +use_repo(python, "python_3_11", "python_3_13", "python_versions", "pythons_hub") # This call registers the Python toolchains. register_toolchains("@pythons_hub//:all") diff --git a/examples/bzlmod/MODULE.bazel.lock b/examples/bzlmod/MODULE.bazel.lock index d34f4ecb73..782188c7a3 100644 --- a/examples/bzlmod/MODULE.bazel.lock +++ b/examples/bzlmod/MODULE.bazel.lock @@ -1392,7 +1392,7 @@ }, "@@rules_python~//python/extensions:pip.bzl%pip": { "general": { - "bzlTransitiveDigest": "qxyKk6sb6G2WeW3iUlRmVO5jafUab5qPwz66Y2anPp8=", + "bzlTransitiveDigest": "mhSG+MAzH9HCQAh9xtg6TFeulsSLlkq20aS5R9UT1q0=", "usagesDigest": "MChlcSw99EuW3K7OOoMcXQIdcJnEh6YmfyjJm+9mxIg=", "recordedFileInputs": { "@@other_module~//requirements_lock_3_11.txt": "a7d0061366569043d5efcf80e34a32c732679367cb3c831c4cdc606adc36d314", @@ -6571,6 +6571,11 @@ "python_3_11_host", "rules_python~~python~python_3_11_host" ], + [ + "rules_python~~python~pythons_hub", + "python_3_13_host", + "rules_python~~python~python_3_13_host" + ], [ "rules_python~~python~pythons_hub", "python_3_9_host", @@ -6581,7 +6586,7 @@ }, "@@rules_python~//python/private/pypi:pip.bzl%pip_internal": { "general": { - "bzlTransitiveDigest": "6NoEDGeQugmtzNzf4Emcb8Sb/cW3RTxSSA6DTHLB1/A=", + "bzlTransitiveDigest": "n5wKuto2x2bg+GUrAMuRorPeAkOm682mG12a9P0tSNk=", "usagesDigest": "LYtSAPzhPjmfD9vF39mCED1UQSvHEo2Hv+aK5Z4ZWWc=", "recordedFileInputs": { "@@rules_python~//tools/publish/requirements_linux.txt": "8175b4c8df50ae2f22d1706961884beeb54e7da27bd2447018314a175981997d", @@ -9148,6 +9153,11 @@ "python_3_11_host", "rules_python~~python~python_3_11_host" ], + [ + "rules_python~~python~pythons_hub", + "python_3_13_host", + "rules_python~~python~python_3_13_host" + ], [ "rules_python~~python~pythons_hub", "python_3_9_host", diff --git a/python/private/python.bzl b/python/private/python.bzl index d2b1007231..74e7e8abe6 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -464,7 +464,7 @@ def _get_toolchain_config(*, modules, _fail = fail): "url": { platform: [item["url"]] for platform in item["sha256"] - }, + } if type(item["url"]) == type("") else item["url"], } for version, item in TOOL_VERSIONS.items() } From 02e84f7266e3e7fc071e87e1456310aba10523ea Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:12:44 +0900 Subject: [PATCH 03/24] update the hermetic_runtime_repo_setup to support free-threaded interpreters --- python/config_settings/BUILD.bazel | 8 ++ .../private/hermetic_runtime_repo_setup.bzl | 81 ++++++++++++++++--- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/python/config_settings/BUILD.bazel b/python/config_settings/BUILD.bazel index f0196d7fe6..6070635c98 100644 --- a/python/config_settings/BUILD.bazel +++ b/python/config_settings/BUILD.bazel @@ -102,6 +102,14 @@ string_flag( visibility = ["//visibility:public"], ) +config_setting( + name = "is_py_freethreaded", + flag_values = { + ":py_freethreaded": "yes", + }, + visibility = ["//visibility:public"], +) + # pip.parse related flags string_flag( diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index cf9a5a6b1d..2388fc900e 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -50,6 +50,7 @@ def define_hermetic_runtime_toolchain_impl( use. """ _ = name # @unused + is_freethreaded = Label("//python/config_settings:is_py_freethreaded") version_info = semver(python_version) version_dict = version_info.to_dict() native.filegroup( @@ -65,21 +66,27 @@ def define_hermetic_runtime_toolchain_impl( # Platform-agnostic filegroup can't match on all patterns. allow_empty = True, exclude = [ - # Unused shared libraries. `python` executable and the `:libpython` target - # depend on `libpython{python_version}.so.1.0`. - "lib/libpython{major}.{minor}.so".format(**version_dict), # static libraries "lib/**/*.a", + # During pyc creation, temp files named *.pyc.NNN are created + "**/__pycache__/*.pyc.*", + # Do exclusions for all known ABIs + # Unused shared libraries. `python` executable and the `:libpython` target + # depend on `libpython{python_version}.so.1.0`. + "lib/libpython{major}.{minor}*.so".format(**version_dict), + "lib/libpython{major}.{minor}*.so".format(**version_dict), # tests for the standard libraries. - "lib/python{major}.{minor}/**/test/**".format(**version_dict), - "lib/python{major}.{minor}/**/tests/**".format(**version_dict), - "**/__pycache__/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created + "lib/python{major}.{minor}*/**/test/**".format(**version_dict), + "lib/python{major}.{minor}*/**/tests/**".format(**version_dict), ] + glob_excludes.version_dependent_exclusions() + extra_files_glob_exclude, ), ) cc_import( name = "interface", - interface_library = "libs/python{major}{minor}.lib".format(**version_dict), + interface_library = select({ + is_freethreaded: "libs/python{major}{minor}t.lib".format(**version_dict), + "//conditions:default": "libs/python{major}{minor}.lib".format(**version_dict), + }), system_provided = True, ) @@ -96,14 +103,65 @@ def define_hermetic_runtime_toolchain_impl( hdrs = [":includes"], includes = [ "include", - "include/python{major}.{minor}".format(**version_dict), - "include/python{major}.{minor}m".format(**version_dict), + ] + select({ + is_freethreaded: [ + "include/python{major}.{minor}t".format(**version_dict), + # FIXME @aignas 2024-11-05: the following looks fishy - should + # we have a config setting for `m` (i.e. DEBUG builds)? + "include/python{major}.{minor}tm".format(**version_dict), + ], + "//conditions:default": [ + "include/python{major}.{minor}".format(**version_dict), + "include/python{major}.{minor}m".format(**version_dict), + ], + }), + ) + native.config_setting( + name = "is_freethreaded_linux", + flag_values = { + Label("//python/config_settings:py_freethreaded"): "yes", + }, + constraint_values = [ + "@platforms//os:linux", + ], + visibility = ["//visibility:private"], + ) + native.config_setting( + name = "is_freethreaded_osx", + flag_values = { + Label("//python/config_settings:py_freethreaded"): "yes", + }, + constraint_values = [ + "@platforms//os:osx", + ], + visibility = ["//visibility:private"], + ) + native.config_setting( + name = "is_freethreaded_windows", + flag_values = { + Label("//python/config_settings:py_freethreaded"): "yes", + }, + constraint_values = [ + "@platforms//os:windows", ], + visibility = ["//visibility:private"], ) + cc_library( name = "libpython", hdrs = [":includes"], srcs = select({ + ":is_freethreaded_linux": [ + "lib/libpython{major}.{minor}t.so".format(**version_dict), + "lib/libpython{major}.{minor}t.so.1.0".format(**version_dict), + ], + ":is_freethreaded_osx": [ + "lib/libpython{major}.{minor}t.dylib".format(**version_dict), + ], + ":is_freethreaded_windows": [ + "python3.dll", + "libs/python{major}{minor}t.lib".format(**version_dict), + ], "@platforms//os:linux": [ "lib/libpython{major}.{minor}.so".format(**version_dict), "lib/libpython{major}.{minor}.so.1.0".format(**version_dict), @@ -137,7 +195,10 @@ def define_hermetic_runtime_toolchain_impl( python_version = "PY3", implementation_name = "cpython", # See https://peps.python.org/pep-3147/ for pyc tag infix format - pyc_tag = "cpython-{major}{minor}".format(**version_dict), + pyc_tag = select({ + is_freethreaded: "cpython-{major}{minor}t".format(**version_dict), + "//conditions:default": "cpython-{major}{minor}".format(**version_dict), + }), ) py_runtime_pair( From 0b57e4dd2fae453623ca40fc52af2e6f2c496a24 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:30:56 +0900 Subject: [PATCH 04/24] finish adding the freethreaded toolchains --- CHANGELOG.md | 4 ++ .../private/hermetic_runtime_repo_setup.bzl | 2 +- python/private/python_repository.bzl | 3 +- python/versions.bzl | 62 +++++++++++-------- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f21f9bb3cc..1a2c4437b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,9 @@ A brief description of the categories of changes: by default. Users wishing to keep this argument and to enforce more hermetic builds can do so by passing the argument in [`pip.parse#extra_pip_args`](https://rules-python.readthedocs.io/en/latest/api/rules_python/python/extensions/pip.html#pip.parse.extra_pip_args) +* (toolchains) Use the latest indygreg toolchain release [20241016]. + +[20241016]: https://github.com/indygreg/python-build-standalone/releases/tag/20241016 {#v0-0-0-fixed} ### Fixed @@ -58,6 +61,7 @@ A brief description of the categories of changes: and one extra file `requirements_universal.txt` if you prefer a single file. The `requirements.txt` file may be removed in the future. * The rules_python version is now reported in `//python/features.bzl#features.version` +* (toolchain) The support for freethreaded Python toolchains is now available. {#v0-0-0-removed} ### Removed diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index 2388fc900e..8307df68ab 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -45,7 +45,7 @@ def define_hermetic_runtime_toolchain_impl( python_version: {type}`str` The Python version, in `major.minor.micro` format. python_bin: {type}`str` The path to the Python binary within the - repositoroy. + repository. coverage_tool: {type}`str` optional target to the coverage tool to use. """ diff --git a/python/private/python_repository.bzl b/python/private/python_repository.bzl index e44bdd151d..d7fdb6e6af 100644 --- a/python/private/python_repository.bzl +++ b/python/private/python_repository.bzl @@ -63,8 +63,9 @@ def _python_repository_impl(rctx): platform = rctx.attr.platform python_version = rctx.attr.python_version python_version_info = python_version.split(".") - python_short_version = "{0}.{1}".format(*python_version_info) release_filename = rctx.attr.release_filename + is_freethreaded = "freethreaded" in release_filename + python_short_version = "{0}.{1}".format(*python_version_info) + "t" if is_freethreaded else "" urls = rctx.attr.urls or [rctx.attr.url] auth = get_auth(rctx, urls) diff --git a/python/versions.bzl b/python/versions.bzl index 52155eb023..2e4c0079a7 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -24,7 +24,7 @@ DEFAULT_RELEASE_BASE_URL = "https://github.com/indygreg/python-build-standalone/ # When updating the versions and releases, run the following command to get # the hashes: -# bazel run //python/private:print_toolchains_checksums +# bazel run //python/private:print_toolchains_checksums --//python/config_settings:python_version={major}.{minor}.{patch} # # Note, to users looking at how to specify their tool versions, coverage_tool version for each # interpreter can be specified by: @@ -562,7 +562,7 @@ TOOL_VERSIONS = { }, "3.13.0": { "url": { - platform + suffix: "20241008/cpython-{python_version}+20241008-" + build + platform + suffix: "20241016/cpython-{python_version}+20241016-" + build for platform, opt in { "aarch64-apple-darwin": "pgo+lto", "aarch64-unknown-linux-gnu": "lto", @@ -583,21 +583,20 @@ TOOL_VERSIONS = { }.items() }, "sha256": { - "aarch64-apple-darwin": "5d3cb8d7ca4cfbbe7ae1f118f26be112ee417d982fab8c6d85cfd8ccccf70718", - "aarch64-unknown-linux-gnu": "c1142af8f2c85923d2ba8201a35b913bb903a5d15f052c38bbecf2f49e2342dc", - "ppc64le-unknown-linux-gnu": "1be64a330499fed4e1f864b97eef5445b0e4abc0559ae45df3108981800cf998", - "s390x-unknown-linux-gnu": "c0b1cc51426feadaa932fdd9afd9a9af789916e128e48ac8909f9a269bbbd749", - "x86_64-apple-darwin": "b58ca12d9ae14bbd79f9e5cf4b748211ff1953e59abeac63b0f4e8e49845669f", - "x86_64-pc-windows-msvc": "c7651a7a575104f47c808902b020168057f3ad80f277e54cecfaf79a9ff50e22", - "x86_64-unknown-linux-gnu": "455200e1a202e9d9ef4b630c04af701c0a91dcaa6462022efc76893fc762ec95", - # Add freethreaded variants - "aarch64-apple-darwin-freethreaded": "8cc1586c4ee730bb33b7e6d39f1b6388f895075fadb1771e3c27b0561abb9242", - "aarch64-unknown-linux-gnu-freethreaded": "56b11e29095e7c183ae191bf9f5ec4e7a71ac41e9c759786faf16c707b83b6b0", - "ppc64le-unknown-linux-gnu-freethreaded": "abac77abeb3c39c355fbc2cd74216254c46bcb28dda10b525daf821bf1d364dc", - "s390x-unknown-linux-gnu-freethreaded": "9adab574543ab8c5fc0ad9e313050030dbdae85160629b1dcbbc3e9d9515a0da", - "x86_64-apple-darwin-freethreaded": "117528b68096379b1303faee1f4f9e32ef3d255207ec92fb063f1bd0b942549d", - "x86_64-pc-windows-msvc-freethreaded": "fc665561556f4dc843cd3eeba4d482f716aec65d5b89a657316829cfbdc9462a", - "x86_64-unknown-linux-gnu-freethreaded": "00a159a64640ce614bdac064b270a9854d86d40d1d8387a822daf1fe0881e64b", + "aarch64-apple-darwin": "31397953849d275aa2506580f3fa1cb5a85b6a3d392e495f8030e8b6412f5556", + "aarch64-unknown-linux-gnu": "e8378c0162b2e0e4cc1f62b29443a3305d116d09583304dbb0149fecaff6347b", + "ppc64le-unknown-linux-gnu": "fc4b7f27c4e84c78f3c8e6c7f8e4023e4638d11f1b36b6b5ce457b1926cebb53", + "s390x-unknown-linux-gnu": "66b19e6a07717f6cfcd3a8ca953f0a2eaa232291142f3d26a8d17c979ec0f467", + "x86_64-apple-darwin": "cff1b7e7cd26f2d47acac1ad6590e27d29829776f77e8afa067e9419f2f6ce77", + "x86_64-pc-windows-msvc": "b25926e8ce4164cf103bacc4f4d154894ea53e07dd3fdd5ebb16fb1a82a7b1a0", + "x86_64-unknown-linux-gnu": "2c8cb15c6a2caadaa98af51df6fe78a8155b8471cb3dd7b9836038e0d3657fb4", + "aarch64-apple-darwin-freethreaded": "efc2e71c0e05bc5bedb7a846e05f28dd26491b1744ded35ed82f8b49ccfa684b", + "aarch64-unknown-linux-gnu-freethreaded": "59b50df9826475d24bb7eff781fa3949112b5e9c92adb29e96a09cdf1216d5bd", + "ppc64le-unknown-linux-gnu-freethreaded": "1217efa5f4ce67fcc9f7eb64165b1bd0912b2a21bc25c1a7e2cb174a21a5df7e", + "s390x-unknown-linux-gnu-freethreaded": "6c3e1e4f19d2b018b65a7e3ef4cd4225c5b9adfbc490218628466e636d5c4b8c", + "x86_64-apple-darwin-freethreaded": "2e07dfea62fe2215738551a179c87dbed1cc79d1b3654f4d7559889a6d5ce4eb", + "x86_64-pc-windows-msvc-freethreaded": "bfd89f9acf866463bc4baf01733da5e767d13f5d0112175a4f57ba91f1541310", + "x86_64-unknown-linux-gnu-freethreaded": "a73adeda301ad843cce05f31a2d3e76222b656984535a7b87696a24a098b216c", }, "strip_prefix": "python", }, @@ -807,15 +806,13 @@ def print_toolchains_checksums(name): Args: name: {type}`str`: the name of the runnable target. """ - commands = [] + all_commands = [] + by_version = {} for python_version in TOOL_VERSIONS.keys(): - commands.append(_commands_for_version(python_version)) + by_version[python_version] = _commands_for_version(python_version) + all_commands.append(_commands_for_version(python_version)) - native.genrule( - name = name, - srcs = [], - outs = ["print_toolchains_checksums.sh"], - cmd = """\ + template = """\ cat > "$@" <<'EOF' #!/bin/bash @@ -825,9 +822,20 @@ echo "Fetching hashes..." {commands} EOF - """.format( - commands = "\n".join(commands), - ), + """ + + native.genrule( + name = name, + srcs = [], + outs = ["print_toolchains_checksums.sh"], + cmd = select({ + "//python/config_settings:is_python_{}".format(version): template.format( + commands = commands, + ) + for version, commands in by_version.items() + } | { + "//conditions:default": template.format(commands = "\n".join(all_commands)), + }), executable = True, ) From 330b9f26f0170b2647fd5201b2814377aaa96a4b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:56:51 +0900 Subject: [PATCH 05/24] fixup --- python/private/python_repository.bzl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/private/python_repository.bzl b/python/private/python_repository.bzl index d7fdb6e6af..ba772ede1b 100644 --- a/python/private/python_repository.bzl +++ b/python/private/python_repository.bzl @@ -64,8 +64,11 @@ def _python_repository_impl(rctx): python_version = rctx.attr.python_version python_version_info = python_version.split(".") release_filename = rctx.attr.release_filename - is_freethreaded = "freethreaded" in release_filename - python_short_version = "{0}.{1}".format(*python_version_info) + "t" if is_freethreaded else "" + version_suffix = "t" if "freethreaded" in release_filename else "" + python_short_version = "{0}.{1}{suffix}".format( + suffix = version_suffix, + *python_version_info, + ) urls = rctx.attr.urls or [rctx.attr.url] auth = get_auth(rctx, urls) From 503acab3607a9d99e32fa1afaefc54682c681e49 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:49:50 +0900 Subject: [PATCH 06/24] revert the inclusion of 3.13 toolchain --- MODULE.bazel | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 5199eea14c..de14b86f1b 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -46,10 +46,7 @@ python.toolchain( is_default = True, python_version = "3.11", ) -python.toolchain( - python_version = "3.13", -) -use_repo(python, "python_3_11", "python_3_13", "python_versions", "pythons_hub") +use_repo(python, "python_3_11", "python_versions", "pythons_hub") # This call registers the Python toolchains. register_toolchains("@pythons_hub//:all") From fe35320dd0515c4125a552d45a4bde5ded536143 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 22:21:11 +0900 Subject: [PATCH 07/24] Pass around the list of loaded platforms for each toolchain --- examples/bzlmod/MODULE.bazel.lock | 14 ++------------ python/private/python.bzl | 4 +++- python/private/python_register_toolchains.bzl | 7 ++++++- python/private/pythons_hub.bzl | 8 +++++++- python/private/toolchains_repo.bzl | 8 +++++++- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/bzlmod/MODULE.bazel.lock b/examples/bzlmod/MODULE.bazel.lock index 782188c7a3..d34f4ecb73 100644 --- a/examples/bzlmod/MODULE.bazel.lock +++ b/examples/bzlmod/MODULE.bazel.lock @@ -1392,7 +1392,7 @@ }, "@@rules_python~//python/extensions:pip.bzl%pip": { "general": { - "bzlTransitiveDigest": "mhSG+MAzH9HCQAh9xtg6TFeulsSLlkq20aS5R9UT1q0=", + "bzlTransitiveDigest": "qxyKk6sb6G2WeW3iUlRmVO5jafUab5qPwz66Y2anPp8=", "usagesDigest": "MChlcSw99EuW3K7OOoMcXQIdcJnEh6YmfyjJm+9mxIg=", "recordedFileInputs": { "@@other_module~//requirements_lock_3_11.txt": "a7d0061366569043d5efcf80e34a32c732679367cb3c831c4cdc606adc36d314", @@ -6571,11 +6571,6 @@ "python_3_11_host", "rules_python~~python~python_3_11_host" ], - [ - "rules_python~~python~pythons_hub", - "python_3_13_host", - "rules_python~~python~python_3_13_host" - ], [ "rules_python~~python~pythons_hub", "python_3_9_host", @@ -6586,7 +6581,7 @@ }, "@@rules_python~//python/private/pypi:pip.bzl%pip_internal": { "general": { - "bzlTransitiveDigest": "n5wKuto2x2bg+GUrAMuRorPeAkOm682mG12a9P0tSNk=", + "bzlTransitiveDigest": "6NoEDGeQugmtzNzf4Emcb8Sb/cW3RTxSSA6DTHLB1/A=", "usagesDigest": "LYtSAPzhPjmfD9vF39mCED1UQSvHEo2Hv+aK5Z4ZWWc=", "recordedFileInputs": { "@@rules_python~//tools/publish/requirements_linux.txt": "8175b4c8df50ae2f22d1706961884beeb54e7da27bd2447018314a175981997d", @@ -9153,11 +9148,6 @@ "python_3_11_host", "rules_python~~python~python_3_11_host" ], - [ - "rules_python~~python~pythons_hub", - "python_3_13_host", - "rules_python~~python~python_3_13_host" - ], [ "rules_python~~python~pythons_hub", "python_3_9_host", diff --git a/python/private/python.bzl b/python/private/python.bzl index 74e7e8abe6..8632554c51 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -213,6 +213,7 @@ def parse_modules(*, module_ctx, _fail = fail): def _python_impl(module_ctx): py = parse_modules(module_ctx = module_ctx) + loaded_platforms = {} for toolchain_info in py.toolchains: # Ensure that we pass the full version here. full_python_version = full_version( @@ -228,7 +229,7 @@ def _python_impl(module_ctx): kwargs.update(py.config.kwargs.get(toolchain_info.python_version, {})) kwargs.update(py.config.kwargs.get(full_python_version, {})) kwargs.update(py.config.default) - python_register_toolchains( + loaded_platforms[full_python_version] = python_register_toolchains( name = toolchain_info.name, _internal_bzlmod_toolchain_call = True, **kwargs @@ -257,6 +258,7 @@ def _python_impl(module_ctx): for i in range(len(py.toolchains)) ], toolchain_user_repository_names = [t.name for t in py.toolchains], + loaded_platforms = loaded_platforms, ) # This is require in order to support multiple version py_test diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl index 64b66d5a6f..637b457d81 100644 --- a/python/private/python_register_toolchains.bzl +++ b/python/private/python_register_toolchains.bzl @@ -73,6 +73,9 @@ def python_register_toolchains( minor_mapping: {type}`dict[str, str]` contains a mapping from `X.Y` to `X.Y.Z` version. **kwargs: passed to each {obj}`python_repository` call. + + Returns: + on bzlmod this returns the loaded platform labels. """ bzlmod_toolchain_call = kwargs.pop("_internal_bzlmod_toolchain_call", False) if bzlmod_toolchain_call: @@ -168,11 +171,13 @@ def python_register_toolchains( # in bzlmod we write out our own toolchain repos if bzlmod_toolchain_call: - return + return loaded_platforms toolchains_repo( name = toolchain_repo_name, python_version = python_version, set_python_version_constraint = set_python_version_constraint, user_repository_name = name, + platforms = loaded_platforms, ) + return None diff --git a/python/private/pythons_hub.bzl b/python/private/pythons_hub.bzl index fdaad60e22..706ce9b98a 100644 --- a/python/private/pythons_hub.bzl +++ b/python/private/pythons_hub.bzl @@ -46,7 +46,8 @@ def _hub_build_file_content( python_versions, set_python_version_constraints, user_repository_names, - workspace_location): + workspace_location, + loaded_platforms): """This macro iterates over each of the lists and returns the toolchain content. python_toolchain_build_file_content is called to generate each of the toolchain @@ -65,6 +66,7 @@ def _hub_build_file_content( python_version = python_versions[i], set_python_version_constraint = set_python_version_constraints[i], user_repository_name = user_repository_names[i], + loaded_platforms = loaded_platforms[python_versions[i]], ) for i in range(len(python_versions)) ], @@ -103,6 +105,7 @@ def _hub_repo_impl(rctx): rctx.attr.toolchain_set_python_version_constraints, rctx.attr.toolchain_user_repository_names, rctx.attr._rules_python_workspace, + rctx.attr.loaded_platforms, ), executable = False, ) @@ -149,6 +152,9 @@ This rule also writes out the various toolchains for the different Python versio doc = "Default Python version for the build in `X.Y` or `X.Y.Z` format.", mandatory = True, ), + "loaded_platforms": attr.string_list_dict( + doc = "The mapping from platform to version", + ), "minor_mapping": attr.string_dict( doc = "The minor mapping of the `X.Y` to `X.Y.Z` format that is used in config settings.", mandatory = True, diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl index 97a3124b6d..78d8fb3c47 100644 --- a/python/private/toolchains_repo.bzl +++ b/python/private/toolchains_repo.bzl @@ -41,7 +41,8 @@ def python_toolchain_build_file_content( prefix, python_version, set_python_version_constraint, - user_repository_name): + user_repository_name, + loaded_platforms): """Creates the content for toolchain definitions for a build file. Args: @@ -51,6 +52,8 @@ def python_toolchain_build_file_content( have the Python version constraint added as a requirement for matching the toolchain, "False" if not. user_repository_name: names for the user repos + loaded_platforms: the list of platform identifiers for which to generate + the toolchain targets. Returns: build_content: Text containing toolchain definitions @@ -78,6 +81,7 @@ py_toolchain_suite( python_version = python_version, ) for platform, meta in PLATFORMS.items() + if platform in loaded_platforms ]) def _toolchains_repo_impl(rctx): @@ -100,6 +104,7 @@ load("@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_sui python_version = rctx.attr.python_version, set_python_version_constraint = str(rctx.attr.set_python_version_constraint), user_repository_name = rctx.attr.user_repository_name, + loaded_platforms = rctx.attr.platforms, ) rctx.file("BUILD.bazel", build_content + toolchains) @@ -109,6 +114,7 @@ toolchains_repo = repository_rule( doc = "Creates a repository with toolchain definitions for all known platforms " + "which can be registered or selected.", attrs = { + "platforms": attr.string_list(doc = "List of platforms for which the toolchain definitions shall be created"), "python_version": attr.string(doc = "The Python version."), "set_python_version_constraint": attr.bool(doc = "if target_compatible_with for the toolchain should set the version constraint"), "user_repository_name": attr.string(doc = "what the user chose for the base name"), From aef8f47953789d006e6f20fe4ca448de0711706b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 22:30:44 +0900 Subject: [PATCH 08/24] finish bumping the toolchains --- CHANGELOG.md | 7 ++++- python/versions.bzl | 66 ++++++++++++++++++++++----------------------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16501aa3bc..fc1393a18c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,12 @@ A brief description of the categories of changes: [`pip.parse#extra_pip_args`](https://rules-python.readthedocs.io/en/latest/api/rules_python/python/extensions/pip.html#pip.parse.extra_pip_args) * (pip.parse) {attr}`pip.parse.whl_modifications` now normalizes the given whl names and now `pyyaml` and `PyYAML` will both work. -* (toolchains) Use the latest indygreg toolchain release [20241016]. +* (toolchains) Use the latest indygreg toolchain release [20241016] for Python versions: + * 3.9.20 + * 3.10.15 + * 3.11.10 + * 3.12.7 + * 3.13.0 [20241016]: https://github.com/indygreg/python-build-standalone/releases/tag/20241016 diff --git a/python/versions.bzl b/python/versions.bzl index 2e4c0079a7..1fc7939e4a 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -237,15 +237,15 @@ TOOL_VERSIONS = { "strip_prefix": "python", }, "3.9.20": { - "url": "20241008/cpython-{python_version}+20241008-{platform}-{build}.tar.gz", + "url": "20241016/cpython-{python_version}+20241016-{platform}-{build}.tar.gz", "sha256": { - "aarch64-apple-darwin": "dde4c3662e8b4ea336af12b94e7963d4c9b4b847e6f4a5a2921d801fbc75d55c", - "aarch64-unknown-linux-gnu": "adb22acc4f5417ecb6113e4beb98f1a1492bcf631b3d3094135f60d1c6794e07", - "ppc64le-unknown-linux-gnu": "abc12738616d3d87e878cd022c4d6a3d7cb6c130a6f3859996ce758a90c8abae", - "s390x-unknown-linux-gnu": "bb037b3b266524df5a27f384755b2eab397837b3c955041145434261248a731d", - "x86_64-apple-darwin": "980fd160c8a3e7839d808055b9497e653bd7be94dcc9cae6db0ddcb343bc5ad6", - "x86_64-pc-windows-msvc": "dc12754f52b7cfcdded91c10953a96ed7d9b08eff54623ee5b819cec13f4715a", - "x86_64-unknown-linux-gnu": "ddae7e904f5ecdff4c8993eb5256fbcec1e477923b40ec0515ffc77706dc2951", + "aarch64-apple-darwin": "34ab2bc4c51502145e1a624b4e4ea06877e3d1934a88cc73ac2e0fd5fd439b75", + "aarch64-unknown-linux-gnu": "1e486c054a4e86666cf24e04f5e29456324ba9c2b95bf1cae1805be90d3da154", + "ppc64le-unknown-linux-gnu": "9a24ccdbfc7f67545d859128f02a3150a160ea6c2fc134b0773bf56f2d90b397", + "s390x-unknown-linux-gnu": "2cee381069bf344fb20eba609af92dfe7ba67eb75bea08eeccf11048a2c380c0", + "x86_64-apple-darwin": "193dc7f0284e4917d52b17a077924474882ee172872f2257cfe3375d6d468ed9", + "x86_64-pc-windows-msvc": "5069008a237b90f6f7a86956903f2a0221b90d471daa6e4a94831eaa399e3993", + "x86_64-unknown-linux-gnu": "c20ee831f7f46c58fa57919b75a40eb2b6a31e03fd29aaa4e8dab4b9c4b60d5d", }, "strip_prefix": "python", }, @@ -356,15 +356,15 @@ TOOL_VERSIONS = { "strip_prefix": "python", }, "3.10.15": { - "url": "20241008/cpython-{python_version}+20241008-{platform}-{build}.tar.gz", + "url": "20241016/cpython-{python_version}+20241016-{platform}-{build}.tar.gz", "sha256": { - "aarch64-apple-darwin": "6bfed646145b9f1f512bbf3c37de8a29fae3544559c501185f552c3b92dc270b", - "aarch64-unknown-linux-gnu": "51f08e2132dca177ac90175536118b3c01c106ec253b93db04e3ca7484525d00", - "ppc64le-unknown-linux-gnu": "44b05f1f831fbef00b36f5d6ef82f308e32d3dee58e1272d1fac26004ce7c76f", - "s390x-unknown-linux-gnu": "793bd6c565bd24b6db8e573d599492c6fddbaee43e4b4aeef240ada1105287d7", - "x86_64-apple-darwin": "df1324c960b9023cfebfd2716f69af57156d823a4d286d8e67ffc4f876309611", - "x86_64-pc-windows-msvc": "c519cb6bbb8caf508e3f3b91a3dd633b4bebdf84217ab34033a10c902b8a8519", - "x86_64-unknown-linux-gnu": "5e07b34c66fbd99f1e2f06d3d42aed04c0f2991e66c1d171fb43e04b7ae71ad5", + "aarch64-apple-darwin": "f64776f455a44c24d50f947c813738cfb7b9ac43732c44891bc831fa7940a33c", + "aarch64-unknown-linux-gnu": "eb58581f85fde83d1f3e8e1f8c6f5a15c7ae4fdbe3b1d1083931f9167fdd8dbc", + "ppc64le-unknown-linux-gnu": "0c45af4e7525e2db59901606db32b2896ac1e9830c6f95551402207f537c2ce4", + "s390x-unknown-linux-gnu": "de205896b070e6f5259ac0f2b3379eead875ea84e6a6ef533b89886fcbb46a4c", + "x86_64-apple-darwin": "90b46dfb1abd98d45663c7a2a8c45d3047a59391d8586d71b459cec7b75f662b", + "x86_64-pc-windows-msvc": "e48952619796c66ec9719867b87be97edca791c2ef7fbf87d42c417c3331609e", + "x86_64-unknown-linux-gnu": "3db2171e03c1a7acdc599fba583c1b92306d3788b375c9323077367af1e9d9de", }, "strip_prefix": "python", }, @@ -470,15 +470,15 @@ TOOL_VERSIONS = { "strip_prefix": "python", }, "3.11.10": { - "url": "20241008/cpython-{python_version}+20241008-{platform}-{build}.tar.gz", + "url": "20241016/cpython-{python_version}+20241016-{platform}-{build}.tar.gz", "sha256": { - "aarch64-apple-darwin": "ecdc9c042b8f97bff211fcf9425bc51c96acd4037df1565964e89816f2c9564d", - "aarch64-unknown-linux-gnu": "320635e957e13d2e10d70a3031563d032fae9e40e60e5ec32bc353643fae1335", - "ppc64le-unknown-linux-gnu": "7eed40dc5751046e2164b1a3f08f177c2c965064f1e3b0f84c00f3f715d099ca", - "s390x-unknown-linux-gnu": "eb86c655159d6f7b5fb245d9017f23aa388b5423f21caefeaee54469446ef9f2", - "x86_64-apple-darwin": "a618c086e0514f681523947e2b66a4dc0c6560f91c36faa072fa6787455df9ea", - "x86_64-pc-windows-msvc": "2cab4d2ee0c9313923c9b11297e23b1876ecb79ce6ad6de0b8b48baf8519ab67", - "x86_64-unknown-linux-gnu": "ff121f14ed113c9da83a45f76c3cf41976fb4419fe406d5cc7066765761c6a4e", + "aarch64-apple-darwin": "5a69382da99c4620690643517ca1f1f53772331b347e75f536088c42a4cf6620", + "aarch64-unknown-linux-gnu": "803e49259280af0f5466d32829cd9d65a302b0226e424b3f0b261f9daf6aee8f", + "ppc64le-unknown-linux-gnu": "92b666d103902001322f42badbd68da92adc5cebb826af9c1c906c33166e2f34", + "s390x-unknown-linux-gnu": "6d584317651c1ad4a857cb32d1999707e8bb3046fcb2f156d80381814fa19fde", + "x86_64-apple-darwin": "1e23ffe5bc473e1323ab8f51464da62d77399afb423babf67f8e13c82b69c674", + "x86_64-pc-windows-msvc": "647b66ff4552e70aec3bf634dd470891b4a2b291e8e8715b3bdb162f577d4c55", + "x86_64-unknown-linux-gnu": "8b50a442b04724a24c1eebb65a36a0c0e833d35374dbdf9c9470d8a97b164cd9", }, "strip_prefix": "python", }, @@ -548,15 +548,15 @@ TOOL_VERSIONS = { "strip_prefix": "python", }, "3.12.7": { - "url": "20241008/cpython-{python_version}+20241008-{platform}-{build}.tar.gz", - "sha256": { - "aarch64-apple-darwin": "dd07d467f1d533b93d06e4d2ff88b91f491329510c6434297b88b584641bff5d", - "aarch64-unknown-linux-gnu": "ce3230da53aacb17ff77e912170786f47db4a446d4acb6cde7c397953a032bca", - "ppc64le-unknown-linux-gnu": "27d3cba42e94593c49f8610dcadd74f5b731c78f04ebabc2b0e1ba031ec09441", - "s390x-unknown-linux-gnu": "1e28e0fc9cd1fa0365a149c715c44d3030b2c989ca397fc074809b943449df41", - "x86_64-apple-darwin": "2347bf53ed3623645bed35adfca950b2c5291e3a759ec6c7765aa707b5dc866b", - "x86_64-pc-windows-msvc": "4ed1a146c66c7dbd85b87df69b17afc166ea7d70056aaf59a49c3d987a030d3b", - "x86_64-unknown-linux-gnu": "adbda1f3b77d7b65a551206e34a225375f408f9823e2e11df4c332aaecb8714b", + "url": "20241016/cpython-{python_version}+20241016-{platform}-{build}.tar.gz", + "sha256": { + "aarch64-apple-darwin": "4c18852bf9c1a11b56f21bcf0df1946f7e98ee43e9e4c0c5374b2b3765cf9508", + "aarch64-unknown-linux-gnu": "bba3c6be6153f715f2941da34f3a6a69c2d0035c9c5396bc5bb68c6d2bd1065a", + "ppc64le-unknown-linux-gnu": "0a1d1d92e33a969bd2f40a80af53c97b6c0cc1060d384ceff50ff801593bf9d6", + "s390x-unknown-linux-gnu": "935676a0c960b552f95e9ac2e1e385de5de4b34038ff65ffdc688838f1189c17", + "x86_64-apple-darwin": "60c5271e7edc3c2ab47440b7abf4ed50fbc693880b474f74f05768f5b657045a", + "x86_64-pc-windows-msvc": "f05531bff16fa77b53be0776587b97b466070e768e6d5920894de988bdcd547a", + "x86_64-unknown-linux-gnu": "43576f7db1033dd57b900307f09c2e86f371152ac8a2607133afa51cbfc36064", }, "strip_prefix": "python", }, From e58413c00f5c978a4ddb01ce88b594b7880bf567 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 22:33:39 +0900 Subject: [PATCH 09/24] buildifier --- python/private/python_repository.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/private/python_repository.bzl b/python/private/python_repository.bzl index ba772ede1b..92d17025a9 100644 --- a/python/private/python_repository.bzl +++ b/python/private/python_repository.bzl @@ -67,7 +67,7 @@ def _python_repository_impl(rctx): version_suffix = "t" if "freethreaded" in release_filename else "" python_short_version = "{0}.{1}{suffix}".format( suffix = version_suffix, - *python_version_info, + *python_version_info ) urls = rctx.attr.urls or [rctx.attr.url] auth = get_auth(rctx, urls) From 0d6366a1019c7262d5648f2c9e407c86941db512 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 22:35:51 +0900 Subject: [PATCH 10/24] fixup changelog --- CHANGELOG.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc1393a18c..baa4221122 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,13 +68,10 @@ A brief description of the categories of changes: and one extra file `requirements_universal.txt` if you prefer a single file. The `requirements.txt` file may be removed in the future. * The rules_python version is now reported in `//python/features.bzl#features.version` -<<<<<<< HEAD -* (toolchain) The support for freethreaded Python toolchains is now available. -======= * (pip.parse) {attr}`pip.parse.extra_hub_aliases` can now be used to expose extra targets created by annotations in whl repositories. Fixes [#2187](https://github.com/bazelbuild/rules_python/issues/2187). ->>>>>>> main +* (toolchain) The support for freethreaded Python toolchains is now available. {#v0-0-0-removed} ### Removed From 1c541168a6afe5bbebd0149d6a13408ab68a4d1d Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 5 Nov 2024 22:39:44 +0900 Subject: [PATCH 11/24] add a merge-conflict pre-commit-hook --- .pre-commit-config.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 38b9161e24..65389db797 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,6 +16,10 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 # Use the ref you want to point at + hooks: + - id: check-merge-conflict - repo: https://github.com/keith/pre-commit-buildifier rev: 6.1.0 hooks: From 472cc40099f8ca721f9c9209befe583fefa1f858 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 08:48:24 +0900 Subject: [PATCH 12/24] comment: use enum --- python/config_settings/BUILD.bazel | 12 ++++-------- python/private/flags.bzl | 10 ++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/config_settings/BUILD.bazel b/python/config_settings/BUILD.bazel index 6070635c98..6d34ee95c7 100644 --- a/python/config_settings/BUILD.bazel +++ b/python/config_settings/BUILD.bazel @@ -5,6 +5,7 @@ load( "AddSrcsToRunfilesFlag", "BootstrapImplFlag", "ExecToolsToolchainFlag", + "FreeThreadedFlag", "PrecompileFlag", "PrecompileSourceRetentionFlag", ) @@ -94,19 +95,14 @@ string_flag( string_flag( name = "py_freethreaded", - build_setting_default = "no", - values = [ - "yes", - "no", - ], + build_setting_default = FreeThreadedFlag.NO, + values = sorted(FreeThreadedFlag.__members__.values()), visibility = ["//visibility:public"], ) config_setting( name = "is_py_freethreaded", - flag_values = { - ":py_freethreaded": "yes", - }, + flag_values = {":py_freethreaded": FreeThreadedFlag.YES}, visibility = ["//visibility:public"], ) diff --git a/python/private/flags.bzl b/python/private/flags.bzl index c190cf682b..5239771d7e 100644 --- a/python/private/flags.bzl +++ b/python/private/flags.bzl @@ -122,3 +122,13 @@ PrecompileSourceRetentionFlag = enum( OMIT_SOURCE = "omit_source", get_effective_value = _precompile_source_retention_flag_get_effective_value, ) + +# Used for matching freethreaded toolchains and would have to be used in wheels +# as well. +# buildifier: disable=name-conventions +FreeThreadedFlag = enum( + # Use freethreaded python toolchain and wheels. + YES = "yes", + # Do not use freethreaded python toolchain and wheels. + NO = "no", +) From c319b2b0c36f6905bbfda53671d152cc636aa2ed Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 08:49:41 +0900 Subject: [PATCH 13/24] comment: use a constant --- python/private/hermetic_runtime_repo_setup.bzl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index 8307df68ab..074f1b3e81 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -21,6 +21,8 @@ load(":glob_excludes.bzl", "glob_excludes") load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain") load(":semver.bzl", "semver") +_IS_FREETHREADED = Label("//python/config_settings:is_py_freethreaded") + def define_hermetic_runtime_toolchain_impl( *, name, @@ -50,7 +52,6 @@ def define_hermetic_runtime_toolchain_impl( use. """ _ = name # @unused - is_freethreaded = Label("//python/config_settings:is_py_freethreaded") version_info = semver(python_version) version_dict = version_info.to_dict() native.filegroup( @@ -84,7 +85,7 @@ def define_hermetic_runtime_toolchain_impl( cc_import( name = "interface", interface_library = select({ - is_freethreaded: "libs/python{major}{minor}t.lib".format(**version_dict), + _IS_FREETHREADED: "libs/python{major}{minor}t.lib".format(**version_dict), "//conditions:default": "libs/python{major}{minor}.lib".format(**version_dict), }), system_provided = True, @@ -104,7 +105,7 @@ def define_hermetic_runtime_toolchain_impl( includes = [ "include", ] + select({ - is_freethreaded: [ + _IS_FREETHREADED: [ "include/python{major}.{minor}t".format(**version_dict), # FIXME @aignas 2024-11-05: the following looks fishy - should # we have a config setting for `m` (i.e. DEBUG builds)? @@ -196,7 +197,7 @@ def define_hermetic_runtime_toolchain_impl( implementation_name = "cpython", # See https://peps.python.org/pep-3147/ for pyc tag infix format pyc_tag = select({ - is_freethreaded: "cpython-{major}{minor}t".format(**version_dict), + _IS_FREETHREADED: "cpython-{major}{minor}t".format(**version_dict), "//conditions:default": "cpython-{major}{minor}".format(**version_dict), }), ) From 5fbbf9e02df3dfa527d70932c67cce52656706ba Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 08:52:50 +0900 Subject: [PATCH 14/24] comment: remove sorting --- python/private/hermetic_runtime_repo_setup.bzl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index 074f1b3e81..6f4cc12639 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -67,18 +67,16 @@ def define_hermetic_runtime_toolchain_impl( # Platform-agnostic filegroup can't match on all patterns. allow_empty = True, exclude = [ - # static libraries - "lib/**/*.a", - # During pyc creation, temp files named *.pyc.NNN are created - "**/__pycache__/*.pyc.*", - # Do exclusions for all known ABIs # Unused shared libraries. `python` executable and the `:libpython` target # depend on `libpython{python_version}.so.1.0`. "lib/libpython{major}.{minor}*.so".format(**version_dict), - "lib/libpython{major}.{minor}*.so".format(**version_dict), + # static libraries + "lib/**/*.a", # tests for the standard libraries. "lib/python{major}.{minor}*/**/test/**".format(**version_dict), "lib/python{major}.{minor}*/**/tests/**".format(**version_dict), + # During pyc creation, temp files named *.pyc.NNN are created + "**/__pycache__/*.pyc.*", ] + glob_excludes.version_dependent_exclusions() + extra_files_glob_exclude, ), ) From 2d6e11a9afca59833defb26a748df572036b7374 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 08:59:46 +0900 Subject: [PATCH 15/24] fix: use the coverage enabled config setting --- python/private/hermetic_runtime_repo_setup.bzl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index 6f4cc12639..35308cfb06 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -189,8 +189,11 @@ def define_hermetic_runtime_toolchain_impl( "micro": str(version_info.patch), "minor": str(version_info.minor), }, - # Convert empty string to None - coverage_tool = coverage_tool or None, + coverage_tool = select({ + # Convert empty string to None + ":coverage_enabled": coverage_tool or None, + "//conditions:default": None, + }), python_version = "PY3", implementation_name = "cpython", # See https://peps.python.org/pep-3147/ for pyc tag infix format From 465111e4fef089e58b6f2936566a4933a1e9784d Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:00:25 +0900 Subject: [PATCH 16/24] comment: loaded_platform strings --- python/private/pythons_hub.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/private/pythons_hub.bzl b/python/private/pythons_hub.bzl index 706ce9b98a..6854e19a16 100644 --- a/python/private/pythons_hub.bzl +++ b/python/private/pythons_hub.bzl @@ -153,7 +153,7 @@ This rule also writes out the various toolchains for the different Python versio mandatory = True, ), "loaded_platforms": attr.string_list_dict( - doc = "The mapping from platform to version", + doc = "The list of loaded platforms keyed by the toolchain full python version", ), "minor_mapping": attr.string_dict( doc = "The minor mapping of the `X.Y` to `X.Y.Z` format that is used in config settings.", From 0f9954efe9f4c39763ce2b6b2df138e768632468 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:07:00 +0900 Subject: [PATCH 17/24] comment: pass loaded_platform structs --- python/private/BUILD.bazel | 1 + python/private/pythons_hub.bzl | 7 ++++++- python/private/toolchains_repo.bzl | 13 ++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 7399b104e0..ffcebb587c 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -256,6 +256,7 @@ bzl_library( deps = [ ":py_toolchain_suite_bzl", ":text_util_bzl", + "//python:versions_bzl", ], ) diff --git a/python/private/pythons_hub.bzl b/python/private/pythons_hub.bzl index 6854e19a16..8afee5af17 100644 --- a/python/private/pythons_hub.bzl +++ b/python/private/pythons_hub.bzl @@ -14,6 +14,7 @@ "Repo rule used by bzlmod extension to create a repo that has a map of Python interpreters and their labels" +load("//python:versions.bzl", "PLATFORMS") load(":text_util.bzl", "render") load(":toolchains_repo.bzl", "python_toolchain_build_file_content") @@ -66,7 +67,11 @@ def _hub_build_file_content( python_version = python_versions[i], set_python_version_constraint = set_python_version_constraints[i], user_repository_name = user_repository_names[i], - loaded_platforms = loaded_platforms[python_versions[i]], + loaded_platforms = { + k: v + for k, v in PLATFORMS.items() + if k in loaded_platforms[python_versions[i]] + }, ) for i in range(len(python_versions)) ], diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl index 78d8fb3c47..d21fb53a41 100644 --- a/python/private/toolchains_repo.bzl +++ b/python/private/toolchains_repo.bzl @@ -52,8 +52,8 @@ def python_toolchain_build_file_content( have the Python version constraint added as a requirement for matching the toolchain, "False" if not. user_repository_name: names for the user repos - loaded_platforms: the list of platform identifiers for which to generate - the toolchain targets. + loaded_platforms: {type}`struct` the list of platform structs defining the + loaded platforms. It is as they are defined in `//python:versions.bzl`. Returns: build_content: Text containing toolchain definitions @@ -80,8 +80,7 @@ py_toolchain_suite( prefix = prefix, python_version = python_version, ) - for platform, meta in PLATFORMS.items() - if platform in loaded_platforms + for platform, meta in loaded_platforms.items() ]) def _toolchains_repo_impl(rctx): @@ -104,7 +103,11 @@ load("@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_sui python_version = rctx.attr.python_version, set_python_version_constraint = str(rctx.attr.set_python_version_constraint), user_repository_name = rctx.attr.user_repository_name, - loaded_platforms = rctx.attr.platforms, + loaded_platforms = { + k: v + for k, v in PLATFORMS.items() + if k in rctx.attr.platforms + }, ) rctx.file("BUILD.bazel", build_content + toolchains) From daaa4de1fda97705c5fe74b51a0844a0bb3fb663 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:15:48 +0900 Subject: [PATCH 18/24] use a function to define platforms --- python/versions.bzl | 52 +++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/python/versions.bzl b/python/versions.bzl index 1fc7939e4a..8e7a3d8ac6 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -19,6 +19,7 @@ MACOS_NAME = "mac os" LINUX_NAME = "linux" WINDOWS_NAME = "windows" +FREETHREADED = "freethreaded" DEFAULT_RELEASE_BASE_URL = "https://github.com/indygreg/python-build-standalone/releases/download" @@ -612,16 +613,10 @@ MINOR_MAPPING = { "3.13": "3.13.0", } -PLATFORMS = { - p + suffix: struct( - compatible_with = v.compatible_with, - flag_values = { - Label("//python/config_settings:py_freethreaded"): freethreaded, - } | v.flag_values, - os_name = v.os_name, - arch = v.arch, - ) - for p, v in { +def _generate_platforms(): + libc = Label("//python/config_settings:py_linux_libc") + + platforms = { "aarch64-apple-darwin": struct( compatible_with = [ "@platforms//os:macos", @@ -639,7 +634,7 @@ PLATFORMS = { "@platforms//cpu:aarch64", ], flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", + libc: "glibc", }, os_name = LINUX_NAME, # Note: this string differs between OSX and Linux @@ -653,7 +648,7 @@ PLATFORMS = { "@platforms//cpu:armv7", ], flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", + libc: "glibc", }, os_name = LINUX_NAME, arch = "armv7", @@ -664,7 +659,7 @@ PLATFORMS = { "@platforms//cpu:i386", ], flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", + libc: "glibc", }, os_name = LINUX_NAME, arch = "i386", @@ -675,7 +670,7 @@ PLATFORMS = { "@platforms//cpu:ppc", ], flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", + libc: "glibc", }, os_name = LINUX_NAME, # Note: this string differs between OSX and Linux @@ -732,17 +727,32 @@ PLATFORMS = { "@platforms//cpu:x86_64", ], flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", + libc: "glibc", }, os_name = LINUX_NAME, arch = "x86_64", ), - }.items() - for suffix, freethreaded in { - "": "no", - "-freethreaded": "yes", - }.items() -} + } + + freethreaded = Label("//python/config_settings:py_freethreaded") + return { + p + suffix: struct( + compatible_with = v.compatible_with, + flag_values = { + freethreaded: freethreaded_value, + } | v.flag_values, + os_name = v.os_name, + arch = v.arch, + ) + for suffix, freethreaded_value in { + "": "no", + "-freethreaded": "yes", + }.items() + for p, v in platforms.items() + } + +PLATFORMS = _generate_platforms( +) def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS): """Resolve the release URL for the requested interpreter version From 66a102a7a9e574d174445936dd834bf856ed67da Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:49:14 +0900 Subject: [PATCH 19/24] move the URL generation to a function instead of list comprehension --- python/versions.bzl | 46 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/python/versions.bzl b/python/versions.bzl index 8e7a3d8ac6..edf4337228 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -562,27 +562,7 @@ TOOL_VERSIONS = { "strip_prefix": "python", }, "3.13.0": { - "url": { - platform + suffix: "20241016/cpython-{python_version}+20241016-" + build - for platform, opt in { - "aarch64-apple-darwin": "pgo+lto", - "aarch64-unknown-linux-gnu": "lto", - "ppc64le-unknown-linux-gnu": "lto", - "s390x-unknown-linux-gnu": "lto", - "x86_64-apple-darwin": "pgo+lto", - "x86_64-pc-windows-msvc": "pgo", - "x86_64-unknown-linux-gnu": "pgo+lto", - }.items() - for suffix, build in { - "": platform + "-{build}.tar.gz", - "-freethreaded": "".join([ - platform, - "-shared" if "windows" in platform else "", - "-freethreaded+" + opt, - "-full.tar.zst", - ]), - }.items() - }, + "url": "20241016/cpython-{python_version}+20241016-{platform}-{build}.{ext}", "sha256": { "aarch64-apple-darwin": "31397953849d275aa2506580f3fa1cb5a85b6a3d392e495f8030e8b6412f5556", "aarch64-unknown-linux-gnu": "e8378c0162b2e0e4cc1f62b29443a3305d116d09583304dbb0149fecaff6347b", @@ -751,8 +731,7 @@ def _generate_platforms(): for p, v in platforms.items() } -PLATFORMS = _generate_platforms( -) +PLATFORMS = _generate_platforms() def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS): """Resolve the release URL for the requested interpreter version @@ -782,10 +761,27 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U release_filename = None rendered_urls = [] for u in url: + p, _, _ = platform.partition("-freethreaded") + if "freethreaded" in platform: + build = { + "aarch64-apple-darwin": "freethreaded+pgo+lto-full", + "aarch64-unknown-linux-gnu": "freethreaded+lto-full", + "ppc64le-unknown-linux-gnu": "freethreaded+lto-full", + "s390x-unknown-linux-gnu": "freethreaded+lto-full", + "x86_64-apple-darwin": "freethreaded+pgo+lto-full", + "x86_64-pc-windows-msvc": "shared-freethreaded+pgo-full", + "x86_64-unknown-linux-gnu": "freethreaded+pgo+lto-full", + }[p] + ext = "tar.zst" + else: + build = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only" + ext = "tar.gz" + release_filename = u.format( - platform = platform, + platform = p, python_version = python_version, - build = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only", + build = build, + ext = ext, ) if "://" in release_filename: # is absolute url? rendered_urls.append(release_filename) From b53073af55aefc0f7395f0e60dd849daa601cc89 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:56:02 +0900 Subject: [PATCH 20/24] simplify --- python/versions.bzl | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/python/versions.bzl b/python/versions.bzl index edf4337228..1b61d2dd18 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -762,26 +762,28 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U rendered_urls = [] for u in url: p, _, _ = platform.partition("-freethreaded") + if "freethreaded" in platform: - build = { - "aarch64-apple-darwin": "freethreaded+pgo+lto-full", - "aarch64-unknown-linux-gnu": "freethreaded+lto-full", - "ppc64le-unknown-linux-gnu": "freethreaded+lto-full", - "s390x-unknown-linux-gnu": "freethreaded+lto-full", - "x86_64-apple-darwin": "freethreaded+pgo+lto-full", - "x86_64-pc-windows-msvc": "shared-freethreaded+pgo-full", - "x86_64-unknown-linux-gnu": "freethreaded+pgo+lto-full", - }[p] - ext = "tar.zst" + build = "freethreaded+{}-full".format({ + "aarch64-apple-darwin": "pgo+lto", + "aarch64-unknown-linux-gnu": "lto", + "ppc64le-unknown-linux-gnu": "lto", + "s390x-unknown-linux-gnu": "lto", + "x86_64-apple-darwin": "pgo+lto", + "x86_64-pc-windows-msvc": "pgo", + "x86_64-unknown-linux-gnu": "pgo+lto", + }[p]) else: - build = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only" - ext = "tar.gz" + build = "install_only" + + if WINDOWS_NAME in platform: + build = "shared-" + build release_filename = u.format( platform = p, python_version = python_version, build = build, - ext = ext, + ext = "tar.zst" if build.endswith("full") else "tar.gz", ) if "://" in release_filename: # is absolute url? rendered_urls.append(release_filename) From ffe70cbd05d1c201087c6638c400f3ccd30768b5 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:58:46 +0900 Subject: [PATCH 21/24] comment: use a constant --- python/private/python_repository.bzl | 4 ++-- python/versions.bzl | 31 +++++++++++++++------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/python/private/python_repository.bzl b/python/private/python_repository.bzl index 92d17025a9..9ffa196a20 100644 --- a/python/private/python_repository.bzl +++ b/python/private/python_repository.bzl @@ -15,7 +15,7 @@ """This file contains repository rules and macros to support toolchain registration. """ -load("//python:versions.bzl", "PLATFORMS") +load("//python:versions.bzl", "FREETHREADED", "PLATFORMS") load(":auth.bzl", "get_auth") load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils") load(":text_util.bzl", "render") @@ -64,7 +64,7 @@ def _python_repository_impl(rctx): python_version = rctx.attr.python_version python_version_info = python_version.split(".") release_filename = rctx.attr.release_filename - version_suffix = "t" if "freethreaded" in release_filename else "" + version_suffix = "t" if FREETHREADED in release_filename else "" python_short_version = "{0}.{1}{suffix}".format( suffix = version_suffix, *python_version_info diff --git a/python/versions.bzl b/python/versions.bzl index 1b61d2dd18..774c24d1b9 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -724,11 +724,11 @@ def _generate_platforms(): os_name = v.os_name, arch = v.arch, ) + for p, v in platforms.items() for suffix, freethreaded_value in { "": "no", - "-freethreaded": "yes", + "-" + FREETHREADED: "yes", }.items() - for p, v in platforms.items() } PLATFORMS = _generate_platforms() @@ -761,18 +761,21 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U release_filename = None rendered_urls = [] for u in url: - p, _, _ = platform.partition("-freethreaded") - - if "freethreaded" in platform: - build = "freethreaded+{}-full".format({ - "aarch64-apple-darwin": "pgo+lto", - "aarch64-unknown-linux-gnu": "lto", - "ppc64le-unknown-linux-gnu": "lto", - "s390x-unknown-linux-gnu": "lto", - "x86_64-apple-darwin": "pgo+lto", - "x86_64-pc-windows-msvc": "pgo", - "x86_64-unknown-linux-gnu": "pgo+lto", - }[p]) + p, _, _ = platform.partition("-" + FREETHREADED) + + if FREETHREADED in platform: + build = "{}+{}-full".format( + FREETHREADED, + { + "aarch64-apple-darwin": "pgo+lto", + "aarch64-unknown-linux-gnu": "lto", + "ppc64le-unknown-linux-gnu": "lto", + "s390x-unknown-linux-gnu": "lto", + "x86_64-apple-darwin": "pgo+lto", + "x86_64-pc-windows-msvc": "pgo", + "x86_64-unknown-linux-gnu": "pgo+lto", + }[p], + ) else: build = "install_only" From a90dd54edb8ce77c677ce5907d9b603d6f1d9c1d Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 21:57:34 +0900 Subject: [PATCH 22/24] add docs --- docs/api/rules_python/python/config_settings/index.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/api/rules_python/python/config_settings/index.md b/docs/api/rules_python/python/config_settings/index.md index 7c7421bf7e..ef829bab76 100644 --- a/docs/api/rules_python/python/config_settings/index.md +++ b/docs/api/rules_python/python/config_settings/index.md @@ -149,6 +149,16 @@ Values: ::: :::: +::::{bzl:flag} py_freethreaded +Set whether to use an interpreter with the experimental freethreaded option set to true. + +Values: +* `no`: Use regular Python toolchains, default. +* `yes`: Use the experimental Python toolchain with freethreaded compile option enabled. +:::{versionadded} 0.38.0 +::: +:::: + ::::{bzl:flag} pip_whl Set what distributions are used in the `pip` integration. From c2663ba400e2a2e54819a63c7a317da9cb8d8d4b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Nov 2024 22:29:14 +0900 Subject: [PATCH 23/24] chore: add coverage deps --- python/private/coverage_deps.bzl | 12 ++++++++++ .../update_deps/update_coverage_deps.py | 22 ++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/python/private/coverage_deps.bzl b/python/private/coverage_deps.bzl index d3a6d96664..e80e8ee910 100644 --- a/python/private/coverage_deps.bzl +++ b/python/private/coverage_deps.bzl @@ -80,11 +80,23 @@ _coverage_deps = { "https://files.pythonhosted.org/packages/b9/67/e1413d5a8591622a46dd04ff80873b04c849268831ed5c304c16433e7e30/coverage-7.6.1-cp313-cp313-macosx_11_0_arm64.whl", "a6d3adcf24b624a7b778533480e32434a39ad8fa30c315208f6d3e5542aeb6e9", ), + "aarch64-apple-darwin-freethreaded": ( + "https://files.pythonhosted.org/packages/c4/ae/b5d58dff26cade02ada6ca612a76447acd69dccdbb3a478e9e088eb3d4b9/coverage-7.6.1-cp313-cp313t-macosx_11_0_arm64.whl", + "502753043567491d3ff6d08629270127e0c31d4184c4c8d98f92c26f65019962", + ), "aarch64-unknown-linux-gnu": ( + "https://files.pythonhosted.org/packages/14/5b/9dec847b305e44a5634d0fb8498d135ab1d88330482b74065fcec0622224/coverage-7.6.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", + "d0c212c49b6c10e6951362f7c6df3329f04c2b1c28499563d4035d964ab8e08c", + ), + "aarch64-unknown-linux-gnu-freethreaded": ( "https://files.pythonhosted.org/packages/b8/d7/62095e355ec0613b08dfb19206ce3033a0eedb6f4a67af5ed267a8800642/coverage-7.6.1-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", "6a89ecca80709d4076b95f89f308544ec8f7b4727e8a547913a35f16717856cb", ), "x86_64-unknown-linux-gnu": ( + "https://files.pythonhosted.org/packages/f7/95/d2fd31f1d638df806cae59d7daea5abf2b15b5234016a5ebb502c2f3f7ee/coverage-7.6.1-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", + "78b260de9790fd81e69401c2dc8b17da47c8038176a79092a89cb2b7d945d060", + ), + "x86_64-unknown-linux-gnu-freethreaded": ( "https://files.pythonhosted.org/packages/8b/61/a7a6a55dd266007ed3b1df7a3386a0d760d014542d72f7c2c6938483b7bd/coverage-7.6.1-cp313-cp313t-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", "13b0a73a0896988f053e4fbb7de6d93388e6dd292b0d87ee51d106f2c11b465b", ), diff --git a/tools/private/update_deps/update_coverage_deps.py b/tools/private/update_deps/update_coverage_deps.py index a856b7acba..bbff67e927 100755 --- a/tools/private/update_deps/update_coverage_deps.py +++ b/tools/private/update_deps/update_coverage_deps.py @@ -42,6 +42,10 @@ "manylinux2014_aarch64": "aarch64-unknown-linux-gnu", "macosx_11_0_arm64": "aarch64-apple-darwin", "macosx_10_9_x86_64": "x86_64-apple-darwin", + ("t", "manylinux2014_x86_64"): "x86_64-unknown-linux-gnu-freethreaded", + ("t", "manylinux2014_aarch64"): "aarch64-unknown-linux-gnu-freethreaded", + ("t", "macosx_11_0_arm64"): "aarch64-apple-darwin-freethreaded", + ("t", "macosx_10_9_x86_64"): "x86_64-apple-darwin-freethreaded", } @@ -87,10 +91,18 @@ def __repr__(self): return "{{\n{}\n}}".format(textwrap.indent("\n".join(parts), prefix=" ")) -def _get_platforms(filename: str, name: str, version: str, python_version: str): - return filename[ - len(f"{name}-{version}-{python_version}-{python_version}-") : -len(".whl") - ].split(".") +def _get_platforms(filename: str, python_version: str): + name, _, tail = filename.partition("-") + version, _, tail = tail.partition("-") + got_python_version, _, tail = tail.partition("-") + if python_version != got_python_version: + return [] + abi, _, tail = tail.partition("-") + + platforms, _, tail = tail.rpartition(".") + platforms = platforms.split(".") + + return [("t", p) for p in platforms] if abi.endswith("t") else platforms def _map( @@ -172,8 +184,6 @@ def main(): platforms = _get_platforms( u["filename"], - args.name, - args.version, u["python_version"], ) From f9609e9437edb18947daaa57b9e038d453ee3339 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:47:16 +0900 Subject: [PATCH 24/24] comment: improve docs and add suggestions from rickeylev --- CHANGELOG.md | 4 +++- python/private/hermetic_runtime_repo_setup.bzl | 3 --- python/private/python_register_toolchains.bzl | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59ecc2c200..f3b2465c07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,9 @@ A brief description of the categories of changes: {#v0-0-0-added} ### Added -* (toolchain) The support for freethreaded Python toolchains is now available. +* (toolchain) Support for freethreaded Python toolchains is now available. Use + the config flag `//python/config_settings:py_freethreaded` to toggle the + selection of the free-threaded toolchains. {#v0-0-0-removed} ### Removed diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index 35308cfb06..3f7bb5d773 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -105,9 +105,6 @@ def define_hermetic_runtime_toolchain_impl( ] + select({ _IS_FREETHREADED: [ "include/python{major}.{minor}t".format(**version_dict), - # FIXME @aignas 2024-11-05: the following looks fishy - should - # we have a config setting for `m` (i.e. DEBUG builds)? - "include/python{major}.{minor}tm".format(**version_dict), ], "//conditions:default": [ "include/python{major}.{minor}".format(**version_dict), diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl index 637b457d81..98c8e5bfc3 100644 --- a/python/private/python_register_toolchains.bzl +++ b/python/private/python_register_toolchains.bzl @@ -75,7 +75,7 @@ def python_register_toolchains( **kwargs: passed to each {obj}`python_repository` call. Returns: - on bzlmod this returns the loaded platform labels. + On bzlmod this returns the loaded platform labels. Otherwise None. """ bzlmod_toolchain_call = kwargs.pop("_internal_bzlmod_toolchain_call", False) if bzlmod_toolchain_call: