Skip to content

Commit 82b650d

Browse files
authored
Allow auth to be used for downloading rust repos (#972)
1 parent cdca6ed commit 82b650d

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

docs/flatten.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
11921192
## rust_toolchain_repository
11931193

11941194
<pre>
1195-
rust_toolchain_repository(<a href="#rust_toolchain_repository-name">name</a>, <a href="#rust_toolchain_repository-dev_components">dev_components</a>, <a href="#rust_toolchain_repository-edition">edition</a>, <a href="#rust_toolchain_repository-exec_triple">exec_triple</a>, <a href="#rust_toolchain_repository-extra_target_triples">extra_target_triples</a>,
1195+
rust_toolchain_repository(<a href="#rust_toolchain_repository-name">name</a>, <a href="#rust_toolchain_repository-auth">auth</a>, <a href="#rust_toolchain_repository-dev_components">dev_components</a>, <a href="#rust_toolchain_repository-edition">edition</a>, <a href="#rust_toolchain_repository-exec_triple">exec_triple</a>, <a href="#rust_toolchain_repository-extra_target_triples">extra_target_triples</a>,
11961196
<a href="#rust_toolchain_repository-include_rustc_srcs">include_rustc_srcs</a>, <a href="#rust_toolchain_repository-iso_date">iso_date</a>, <a href="#rust_toolchain_repository-repo_mapping">repo_mapping</a>, <a href="#rust_toolchain_repository-rustfmt_version">rustfmt_version</a>, <a href="#rust_toolchain_repository-sha256s">sha256s</a>,
11971197
<a href="#rust_toolchain_repository-toolchain_name_prefix">toolchain_name_prefix</a>, <a href="#rust_toolchain_repository-urls">urls</a>, <a href="#rust_toolchain_repository-version">version</a>)
11981198
</pre>
@@ -1207,6 +1207,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito
12071207
| Name | Description | Type | Mandatory | Default |
12081208
| :------------- | :------------- | :------------- | :------------- | :------------- |
12091209
| <a id="rust_toolchain_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
1210+
| <a id="rust_toolchain_repository-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download for more details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
12101211
| <a id="rust_toolchain_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False |
12111212
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2018" |
12121213
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
@@ -1797,7 +1798,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
17971798

17981799
<pre>
17991800
rust_repository_set(<a href="#rust_repository_set-name">name</a>, <a href="#rust_repository_set-version">version</a>, <a href="#rust_repository_set-exec_triple">exec_triple</a>, <a href="#rust_repository_set-include_rustc_srcs">include_rustc_srcs</a>, <a href="#rust_repository_set-extra_target_triples">extra_target_triples</a>, <a href="#rust_repository_set-iso_date">iso_date</a>,
1800-
<a href="#rust_repository_set-rustfmt_version">rustfmt_version</a>, <a href="#rust_repository_set-edition">edition</a>, <a href="#rust_repository_set-dev_components">dev_components</a>, <a href="#rust_repository_set-sha256s">sha256s</a>, <a href="#rust_repository_set-urls">urls</a>)
1801+
<a href="#rust_repository_set-rustfmt_version">rustfmt_version</a>, <a href="#rust_repository_set-edition">edition</a>, <a href="#rust_repository_set-dev_components">dev_components</a>, <a href="#rust_repository_set-sha256s">sha256s</a>, <a href="#rust_repository_set-urls">urls</a>, <a href="#rust_repository_set-auth">auth</a>)
18011802
</pre>
18021803

18031804
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.
@@ -1821,6 +1822,7 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with
18211822
| <a id="rust_repository_set-dev_components"></a>dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | <code>False</code> |
18221823
| <a id="rust_repository_set-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | <code>None</code> |
18231824
| <a id="rust_repository_set-urls"></a>urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | <code>["https://static.rust-lang.org/dist/{}.tar.gz"]</code> |
1825+
| <a id="rust_repository_set-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download for more details. | <code>None</code> |
18241826

18251827

18261828
<a id="#rust_test_suite"></a>

docs/rust_repositories.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
112112
## rust_toolchain_repository
113113

114114
<pre>
115-
rust_toolchain_repository(<a href="#rust_toolchain_repository-name">name</a>, <a href="#rust_toolchain_repository-dev_components">dev_components</a>, <a href="#rust_toolchain_repository-edition">edition</a>, <a href="#rust_toolchain_repository-exec_triple">exec_triple</a>, <a href="#rust_toolchain_repository-extra_target_triples">extra_target_triples</a>,
115+
rust_toolchain_repository(<a href="#rust_toolchain_repository-name">name</a>, <a href="#rust_toolchain_repository-auth">auth</a>, <a href="#rust_toolchain_repository-dev_components">dev_components</a>, <a href="#rust_toolchain_repository-edition">edition</a>, <a href="#rust_toolchain_repository-exec_triple">exec_triple</a>, <a href="#rust_toolchain_repository-extra_target_triples">extra_target_triples</a>,
116116
<a href="#rust_toolchain_repository-include_rustc_srcs">include_rustc_srcs</a>, <a href="#rust_toolchain_repository-iso_date">iso_date</a>, <a href="#rust_toolchain_repository-repo_mapping">repo_mapping</a>, <a href="#rust_toolchain_repository-rustfmt_version">rustfmt_version</a>, <a href="#rust_toolchain_repository-sha256s">sha256s</a>,
117117
<a href="#rust_toolchain_repository-toolchain_name_prefix">toolchain_name_prefix</a>, <a href="#rust_toolchain_repository-urls">urls</a>, <a href="#rust_toolchain_repository-version">version</a>)
118118
</pre>
@@ -127,6 +127,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito
127127
| Name | Description | Type | Mandatory | Default |
128128
| :------------- | :------------- | :------------- | :------------- | :------------- |
129129
| <a id="rust_toolchain_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
130+
| <a id="rust_toolchain_repository-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download for more details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
130131
| <a id="rust_toolchain_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False |
131132
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2018" |
132133
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
@@ -212,7 +213,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
212213

213214
<pre>
214215
rust_repository_set(<a href="#rust_repository_set-name">name</a>, <a href="#rust_repository_set-version">version</a>, <a href="#rust_repository_set-exec_triple">exec_triple</a>, <a href="#rust_repository_set-include_rustc_srcs">include_rustc_srcs</a>, <a href="#rust_repository_set-extra_target_triples">extra_target_triples</a>, <a href="#rust_repository_set-iso_date">iso_date</a>,
215-
<a href="#rust_repository_set-rustfmt_version">rustfmt_version</a>, <a href="#rust_repository_set-edition">edition</a>, <a href="#rust_repository_set-dev_components">dev_components</a>, <a href="#rust_repository_set-sha256s">sha256s</a>, <a href="#rust_repository_set-urls">urls</a>)
216+
<a href="#rust_repository_set-rustfmt_version">rustfmt_version</a>, <a href="#rust_repository_set-edition">edition</a>, <a href="#rust_repository_set-dev_components">dev_components</a>, <a href="#rust_repository_set-sha256s">sha256s</a>, <a href="#rust_repository_set-urls">urls</a>, <a href="#rust_repository_set-auth">auth</a>)
216217
</pre>
217218

218219
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.
@@ -236,5 +237,6 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with
236237
| <a id="rust_repository_set-dev_components"></a>dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | <code>False</code> |
237238
| <a id="rust_repository_set-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | <code>None</code> |
238239
| <a id="rust_repository_set-urls"></a>urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | <code>["https://static.rust-lang.org/dist/{}.tar.gz"]</code> |
240+
| <a id="rust_repository_set-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download for more details. | <code>None</code> |
239241

240242

rust/private/repository_utils.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def load_rust_src(ctx):
323323
url,
324324
output = archive_path,
325325
sha256 = ctx.attr.sha256s.get(tool_suburl) or FILE_KEY_TO_SHA.get(tool_suburl) or "",
326+
auth = _make_auth_dict(ctx, [url]),
326327
)
327328
ctx.extract(
328329
archive_path,
@@ -536,10 +537,20 @@ def load_arbitrary_tool(ctx, tool_name, tool_subdirectories, version, iso_date,
536537
sha256 = getattr(ctx.attr, "sha256s", dict()).get(tool_suburl) or
537538
FILE_KEY_TO_SHA.get(tool_suburl) or
538539
sha256,
540+
auth = _make_auth_dict(ctx, urls),
539541
)
540542
for subdirectory in tool_subdirectories:
541543
ctx.extract(
542544
archive_path,
543545
output = "",
544546
stripPrefix = "{}/{}".format(tool_path, subdirectory),
545547
)
548+
549+
def _make_auth_dict(ctx, urls):
550+
auth = getattr(ctx.attr, "auth", {})
551+
if not auth:
552+
return {}
553+
ret = {}
554+
for url in urls:
555+
ret[url] = auth
556+
return ret

rust/repositories.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ rust_toolchain_repository = repository_rule(
170170
"selection from toolchain fetching."
171171
),
172172
attrs = {
173+
"auth": attr.string_dict(
174+
doc = (
175+
"Auth object compatible with repository_ctx.download to use when downloading files. " +
176+
"See https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download for more details."
177+
),
178+
),
173179
"dev_components": attr.bool(
174180
doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
175181
default = False,
@@ -255,7 +261,8 @@ def rust_repository_set(
255261
edition = None,
256262
dev_components = False,
257263
sha256s = None,
258-
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES):
264+
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
265+
auth = None):
259266
"""Assembles a remote repository for the given toolchain params, produces a proxy repository \
260267
to contain the toolchain declaration, and registers the toolchains.
261268
@@ -278,6 +285,8 @@ def rust_repository_set(
278285
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
279286
[rust_repositories](#rust_repositories) for more details.
280287
urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz']
288+
auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
289+
See https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download for more details.
281290
"""
282291

283292
rust_toolchain_repository(
@@ -293,6 +302,7 @@ def rust_repository_set(
293302
dev_components = dev_components,
294303
sha256s = sha256s,
295304
urls = urls,
305+
auth = auth,
296306
)
297307

298308
rust_toolchain_repository_proxy(

0 commit comments

Comments
 (0)