1717For historic reasons, pip_repositories() is defined in //python:pip.bzl.
1818"""
1919
20- load ("//python/private:toolchains_repo.bzl" , "PLATFORMS" , "toolchains_repo" )
21- load ("//python/private:versions.bzl" ,
22- "get_release_url " ,
20+ load ("//python/private:toolchains_repo.bzl" , "PLATFORMS" , "host_platform" , " toolchains_repo" )
21+ load (
22+ "//python/private:versions.bzl " ,
2323 "MINOR_MAPPING" ,
2424 "TOOL_VERSIONS" ,
25+ "get_release_url" ,
2526)
2627
2728def py_repositories ():
@@ -117,20 +118,16 @@ py_runtime_pair(
117118 rctx .file ("BUILD.bazel" , build_content )
118119
119120 return {
120- "sha256" : download_result .sha256 ,
121121 "name" : rctx .attr .name ,
122122 "platform" : platform ,
123123 "python_version" : python_version ,
124+ "sha256" : download_result .sha256 ,
124125 }
125126
126127python_repository = repository_rule (
127128 _python_repository_impl ,
128129 doc = "Fetches the external tools needed for the Python toolchain." ,
129130 attrs = {
130- "sha256" : attr .string (
131- doc = "The SHA256 integrity hash for the Python interpreter tarball." ,
132- mandatory = True ,
133- ),
134131 "platform" : attr .string (
135132 doc = "The platform name for the Python interpreter tarball." ,
136133 mandatory = True ,
@@ -141,6 +138,48 @@ python_repository = repository_rule(
141138 mandatory = True ,
142139 values = TOOL_VERSIONS .keys () + MINOR_MAPPING .keys (),
143140 ),
141+ "sha256" : attr .string (
142+ doc = "The SHA256 integrity hash for the Python interpreter tarball." ,
143+ mandatory = True ,
144+ ),
145+ },
146+ )
147+
148+ def _host_os_alias_impl (repository_ctx ):
149+ # Base BUILD file for this repository.
150+ repository_ctx .file ("BUILD.bazel" , """\
151+ # Generated by python/repositories.bzl
152+ package(default_visibility = ["//visibility:public"])
153+ alias(name = "files", actual = "@{py_repository}_{host_platform}//:files")
154+ alias(name = "py3_runtime", actual = "@{py_repository}_{host_platform}//:py3_runtime")
155+ alias(name = "python_runtimes", actual = "@{py_repository}_{host_platform}//:python_runtimes")
156+ alias(name = "python3", actual = "@{py_repository}_{host_platform}//:bin/python3")
157+ """ .format (
158+ py_repository = repository_ctx .attr .user_repository_name ,
159+ host_platform = host_platform (repository_ctx ),
160+ ))
161+
162+ # Expose a Starlark file so rules can know what host platform we used and where to find an interpreter
163+ # when using repository_ctx.path, which doesn't understand aliases.
164+ repository_ctx .file ("defs.bzl" , content = """\
165+ # Generated by python/repositories.bzl
166+ host_platform="{host_platform}"
167+ interpreter="@{py_repository}_{host_platform}//:bin/python3"
168+ """ .format (
169+ py_repository = repository_ctx .attr .user_repository_name ,
170+ host_platform = host_platform (repository_ctx ),
171+ ))
172+
173+ _host_os_alias = repository_rule (
174+ _host_os_alias_impl ,
175+ doc = """Creates a repository with a shorter name meant for the host platform, which contains
176+ a BUILD.bazel file declaring aliases to the host platform's targets.
177+ """ ,
178+ attrs = {
179+ "user_repository_name" : attr .string (
180+ mandatory = True ,
181+ doc = "The base name for all created repositories, like 'python38'." ,
182+ ),
144183 },
145184)
146185
@@ -150,8 +189,6 @@ def python_register_toolchains(name, python_version, **kwargs):
150189
151190 - Create a repository for each built-in platform like "python_linux_amd64" -
152191 this repository is lazily fetched when Python is needed for that platform.
153- - TODO(f0rmiga): create a convenience repository for the host platform like
154- "python_host".
155192 - Create a repository exposing toolchains for each platform like
156193 "python_platforms".
157194 - Register a toolchain pointing at each platform.
@@ -185,6 +222,10 @@ def python_register_toolchains(name, python_version, **kwargs):
185222 platform = platform ,
186223 ))
187224
225+ _host_os_alias (
226+ name = "{name}_host" .format (name = name ),
227+ user_repository_name = name ,
228+ )
188229 toolchains_repo (
189230 name = "{name}_toolchains" .format (name = name ),
190231 user_repository_name = name ,
0 commit comments