@@ -796,3 +796,75 @@ if CONFIG.BAZEL_COMPATIBILITY:
796796 py_library = python_library
797797 py_binary = python_binary
798798 py_test = python_test
799+
800+
801+ def python_toolchain(name:str, version:str, release:str, arch:str=None,
802+ vendor:str=None, os:str=None, build:str=None,
803+ hashes:list=None, visibility:list=None):
804+ """Defines download of a CPython interpreter for use within the repo.
805+
806+ These are taken from https://github.com/indygreg/python-build-standalone; see
807+ https://gregoryszorc.com/docs/python-build-standalone/main/running.html for more info
808+ about the various os/arch/build options available.
809+ At minimum the Python version and release datestamp must be specified; other fields
810+ will attempt to autodetect based on the _host_ architecture. It may be useful to specify
811+ these in some cases where we aren't able to select the name they would use.
812+
813+ Args:
814+ name: Name of the target
815+ version: Python version to download, e.g. "3.10.16"
816+ release: Release datestamp, e.g. "20241206"
817+ arch: Architecture to download for, e.g. "aarch64" or "x86_64".
818+ vendor: Vendor to target, e.g. "unknown" (for linux) or "apple" (for macos).
819+ os: OS to target, e.g. "linux" or "darwin".
820+ build: The build type, one of "debug", "noopt", "lto", "pgo" or "pgo+lto".
821+ By default will choose debug for dbg / cover builds and pgo+lto otherwise.
822+ hashes: List of hashes that the downloaded tarball must match.
823+ visibility: Visibility of the toolchain.
824+ """
825+ # TODO(peterebden): The URLs are going to change in like two days, once that happens
826+ # we should update to point to appropriate new locations.
827+ if not arch:
828+ if CONFIG.HOSTARCH == "amd64":
829+ arch = "x86_64_v2"
830+ elif CONFIG.HOSTARCH == "arm64":
831+ arch = "aarch64"
832+ else:
833+ fail(f"Unsure what arch to use for {CONFIG.HOSTARCH}, please specify")
834+ if not vendor:
835+ if CONFIG.HOSTOS == "linux":
836+ vendor = "unknown"
837+ elif CONFIG.HOSTOS == "darwin":
838+ vendor = "apple"
839+ elif CONFIG.HOSTOS == "windows":
840+ vendor = "windows"
841+ else:
842+ fail(f"Unsure what vendor to use for {CONFIG.HOSTOS}, please specify")
843+ if not os:
844+ if CONFIG.HOSTOS == "linux":
845+ os = "linux-gnu"
846+ elif CONFIG.HOSTOS == "darwin":
847+ os = "darwin"
848+ elif CONFIG.HOSTOS == "windows":
849+ os = "msvc"
850+ else:
851+ fail(f"Unsure what OS to use for {CONFIG.HOSTOS}, please specify")
852+ if not build:
853+ build = "debug" if (CONFIG.BUILD_CONFIG == "dbg" or CONFIG.BUILD_CONFIG == "cover") else "pgo+lto"
854+
855+ download = remote_file(
856+ name = f"_{name}#download",
857+ url = f"https://github.com/indygreg/python-build-standalone/releases/download/{release}/cpython-{version}+{release}-{arch}-{vendor}-{os}-{build}-full.tar.zst",
858+ hashes = hashes,
859+ )
860+ return build_rule(
861+ name = name,
862+ srcs = [download],
863+ outs = [f"python-{version}"], # We might want this to be more specific so you could mix multiple in a directory?
864+ cmd = "tar --strip-components=1 -xf $SRCS && mv install $OUTS",
865+ binary = True,
866+ entry_points = {
867+ "python": f"python-{version}/bin/python3",
868+ },
869+ visibility = visibility,
870+ )
0 commit comments