Skip to content

Commit e461396

Browse files
committed
Add python_toolchain
1 parent 397ea0c commit e461396

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

build_defs/python.build_defs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
)

third_party/cpython/BUILD

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
version = "3.12.7"
2-
release = "20241016"
1+
subinclude("//build_defs:python")
32

4-
remote_file(
5-
name = "download",
6-
url = f"https://github.com/indygreg/python-build-standalone/releases/download/{release}/cpython-{version}+{release}-x86_64_v2-unknown-linux-gnu-pgo+lto-full.tar.zst",
7-
hashes = ["98f64550d63fa2f5468b28482b1fed8aef33a70d81f1549b7a8be1a59db41d98"],
8-
)
9-
10-
genrule(
3+
python_toolchain(
114
name = "cpython",
12-
srcs = [":download"],
13-
outs = [f"python-{version}"],
14-
cmd = [
15-
"tar --strip-components=1 -xf $SRCS",
16-
"mv install $OUTS",
5+
version = "3.12.7",
6+
release = "20241016",
7+
hashes = [
8+
"0f089b56633c90757bd7027ea2548f024dd187a08c1702d8bd76ff9c507723b4", # linux dbg
9+
"98f64550d63fa2f5468b28482b1fed8aef33a70d81f1549b7a8be1a59db41d98", # linux opt
1710
],
18-
binary = True,
19-
entry_points = {
20-
"python": f"python-{version}/bin/python3",
21-
},
2211
visibility = ["PUBLIC"],
2312
)

0 commit comments

Comments
 (0)