1- """Toolchain for making toolchains"""
1+ """Utilities for making toolchains"""
2+ load ("//tools:integrity.bzl" , "RELEASED_BINARY_INTEGRITY" )
3+ load ("//tools:version.bzl" , "VERSION" )
24
3- load ("//tools:version.bzl" , "IS_PRERELEASE" )
5+ # The expected config for each tool, whether it runs in an action or at runtime
6+ RUST_BIN_CFG = {
7+ "unpack" : "exec" ,
8+ "venv" : "target" ,
9+ }
410
511TOOLCHAIN_PLATFORMS = {
612 "darwin_amd64" : struct (
7- release_platform = "macos-amd64" ,
13+ arch = "x86_64" ,
14+ vendor_os_abi = "apple-darwin" ,
815 compatible_with = [
916 "@platforms//os:macos" ,
1017 "@platforms//cpu:x86_64" ,
1118 ],
1219 ),
1320 "darwin_arm64" : struct (
14- release_platform = "macos-arm64" ,
21+ arch = "aarch64" ,
22+ vendor_os_abi = "apple-darwin" ,
1523 compatible_with = [
1624 "@platforms//os:macos" ,
1725 "@platforms//cpu:aarch64" ,
1826 ],
1927 ),
2028 "linux_amd64" : struct (
21- release_platform = "linux-amd64" ,
29+ arch = "x86_64" ,
30+ vendor_os_abi = "unknown-linux-gnu" ,
2231 compatible_with = [
2332 "@platforms//os:linux" ,
2433 "@platforms//cpu:x86_64" ,
2534 ],
2635 ),
2736 "linux_arm64" : struct (
28- release_platform = "linux-arm64" ,
37+ arch = "aarch64" ,
38+ vendor_os_abi = "unknown-linux-gnu" ,
2939 compatible_with = [
3040 "@platforms//os:linux" ,
3141 "@platforms//cpu:aarch64" ,
@@ -56,7 +66,7 @@ def _toolchain_impl(ctx):
5666
5767 return [toolchain_info , default_info , template_variables ]
5868
59- _toolchain = rule (
69+ py_tool_toolchain = rule (
6070 implementation = _toolchain_impl ,
6171 attrs = {
6272 "bin" : attr .label (
@@ -69,49 +79,97 @@ _toolchain = rule(
6979 },
7080)
7181
72- def _make_toolchain_name (name , platform ):
73- return "{}_{}_toolchain" .format (name , platform )
74-
75- def make_toolchain (name , toolchain_type , tools , cfg = "exec" ):
82+ def source_toolchain (name , toolchain_type , bin ):
7683 """Makes vtool toolchain and repositories
7784
7885 Args:
7986 name: Override the prefix for the generated toolchain repositories.
8087 toolchain_type: Toolchain type reference.
81- tools: Mapping of tool binary to platform.
82- cfg: Generate a toolchain for the target or exec config.
88+ bin: the rust_binary target
8389 """
8490
85- if IS_PRERELEASE :
86- toolchain_rule = "{}_toolchain_source" .format (name )
87- _toolchain (
88- name = toolchain_rule ,
89- bin = tools ["from-source" ],
90- template_var = "{}_BIN" .format (name .upper ()),
91- )
92- native .toolchain (
93- name = _make_toolchain_name (name , "source" ),
94- toolchain = toolchain_rule ,
95- toolchain_type = toolchain_type ,
91+ toolchain_rule = "{}_toolchain_source" .format (name )
92+ py_tool_toolchain (
93+ name = toolchain_rule ,
94+ bin = bin ,
95+ template_var = "{}_BIN" .format (name .upper ()),
96+ )
97+ native .toolchain (
98+ name = "{}_source_toolchain" .format (name ),
99+ toolchain = toolchain_rule ,
100+ toolchain_type = toolchain_type ,
101+ )
102+
103+ def _tool_repo_impl (rctx ):
104+ build_content = """\
105+ # Generated by @aspect_rules_py//py/private/toolchain:tools.bzl
106+ load("@aspect_rules_py//py/private/toolchain:tools.bzl", "py_tool_toolchain")
107+
108+ package(default_visibility = ["//visibility:public"])
109+ """
110+ # For manual testing, override these environment variables
111+ # TODO: use rctx.getenv when available, see https://github.com/bazelbuild/bazel/pull/20944
112+ release_fork = "aspect-build"
113+ release_version = VERSION
114+ if "RULES_PY_RELEASE_FORK" in rctx .os .environ :
115+ release_fork = rctx .os .environ ["RULES_PY_RELEASE_FORK" ]
116+ if "RULES_PY_RELEASE_VERSION" in rctx .os .environ :
117+ release_version = rctx .os .environ ["RULES_PY_RELEASE_VERSION" ]
118+
119+ for tool , cfg in RUST_BIN_CFG .items ():
120+ filename = "-" .join ([
121+ tool ,
122+ TOOLCHAIN_PLATFORMS [rctx .attr .platform ].arch ,
123+ TOOLCHAIN_PLATFORMS [rctx .attr .platform ].vendor_os_abi ,
124+ ])
125+ url = "https://github.com/{}/rules_py/releases/download/v{}/{}" .format (
126+ release_fork ,
127+ release_version ,
128+ filename
96129 )
97- return
98-
99- for [platform , meta ] in TOOLCHAIN_PLATFORMS .items ():
100- toolchain_rule = "{}_toolchain_{}" .format (name , platform )
101- _toolchain (
102- name = toolchain_rule ,
103- bin = tools [meta .release_platform ],
104- template_var = "{}_BIN" .format (name .upper ()),
130+ rctx .download (
131+ url = url ,
132+ sha256 = RELEASED_BINARY_INTEGRITY [filename ],
133+ executable = True ,
134+ output = tool ,
105135 )
136+ build_content += """\
137+ py_tool_toolchain(name = "concrete_{tool}_toolchain", bin = "{tool}", template_var = "{tool_upper}_BIN")
138+
139+ toolchain(
140+ name = "{tool}_toolchain",
141+ {cfg}_compatible_with = {compatible_with},
142+ toolchain = "concrete_{tool}_toolchain",
143+ toolchain_type = "@aspect_rules_py//py/private/toolchain/{tool}:toolchain_type",
144+ )
145+ """ .format (
146+ cfg = cfg ,
147+ compatible_with = TOOLCHAIN_PLATFORMS [rctx .attr .platform ].compatible_with ,
148+ tool = tool ,
149+ tool_upper = tool .upper (),
150+ )
151+
152+ rctx .file ("BUILD.bazel" , build_content )
106153
107- args = dict ({
108- "name" : _make_toolchain_name (name , platform ),
109- "toolchain" : toolchain_rule ,
110- "toolchain_type" : toolchain_type ,
111- })
112- if cfg == "exec" :
113- args .update ({"exec_compatible_with" : meta .compatible_with })
114- else :
115- args .update ({"target_compatible_with" : meta .compatible_with })
116-
117- native .toolchain (** args )
154+ _tool_repo = repository_rule (
155+ doc = "Download pre-built binary tools and create toolchains for them" ,
156+ implementation = _tool_repo_impl ,
157+ attrs = {
158+ "platform" : attr .string (mandatory = True , values = TOOLCHAIN_PLATFORMS .keys ()),
159+ }
160+ )
161+
162+ def binary_tool_repos (name ):
163+ """Create a downloaded toolchain for every tool under every supported platform.
164+
165+ Args:
166+ name: prefix used in created repositories
167+ Returns:
168+ list of toolchain targets to register
169+ """
170+ result = []
171+ for platform in TOOLCHAIN_PLATFORMS .keys ():
172+ plat_repo_name = "." .join ([name , platform ])
173+ result .append ("@{}//:all" .format (plat_repo_name ))
174+ _tool_repo (name = plat_repo_name , platform = platform )
175+ return result
0 commit comments