Skip to content

Commit

Permalink
Added environment variable to control downloading rustc sources (#737)
Browse files Browse the repository at this point in the history
* Added environment variable to control downloading rustc sources

* Regenerate documentation
  • Loading branch information
UebelAndre committed May 13, 2021
1 parent 714a4d4 commit 76cfae4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2015" |
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
| <a id="rust_toolchain_repository-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] |
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. | Boolean | optional | False |
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. An environment variable <code>RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS</code> can also be used to control this attribute. This variable will take precedence over the hard coded attribute. Setting it to <code>true</code> to activates this attribute where all other values deactivate it. | Boolean | optional | False |
| <a id="rust_toolchain_repository-iso_date"></a>iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" |
| <a id="rust_toolchain_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
| <a id="rust_toolchain_repository-rustfmt_version"></a>rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | "" |
Expand Down Expand Up @@ -1564,7 +1564,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
| <a id="rust_repositories-edition"></a>edition | The rust edition to be used by default (2015 (default) or 2018) | <code>None</code> |
| <a id="rust_repositories-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | <code>False</code> |
| <a id="rust_repositories-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | <code>None</code> |
| <a id="rust_repositories-include_rustc_srcs"></a>include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | <code>False</code> |
| <a id="rust_repositories-include_rustc_srcs"></a>include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details | <code>False</code> |
| <a id="rust_repositories-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> |


Expand Down
12 changes: 10 additions & 2 deletions docs/rust_analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_deps")
rust_analyzer_deps()
```

You'll also need to ensure any `rust_repository` or `rust_repositories` calls in your `WORKSPACE` file set the arg `include_rustc_src` to `True`.
Next, add the following lines to the `.bazelrc` file of your workspace:
```
build --repo_env=RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS=true
```

This will ensure rust source code is available to `rust-analyzer`. Users
can also set `include_rustc_srcs = True` on any `rust_repository` or
`rust_repositories` calls in the workspace but the environment variable
has higher priority and can override the attribute.

Then, add a rule to the root `BUILD` file like the following.
Finally, add a rule to the root `BUILD` file like the following.

```python
load("@rules_rust//rust:defs.bzl", "rust_analyzer")
Expand Down
12 changes: 10 additions & 2 deletions docs/rust_analyzer.vm
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_deps")
rust_analyzer_deps()
```

You'll also need to ensure any `rust_repository` or `rust_repositories` calls in your `WORKSPACE` file set the arg `include_rustc_src` to `True`.
Next, add the following lines to the `.bazelrc` file of your workspace:
```
build --repo_env=RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS=true
```

This will ensure rust source code is available to `rust-analyzer`. Users
can also set `include_rustc_srcs = True` on any `rust_repository` or
`rust_repositories` calls in the workspace but the environment variable
has higher priority and can override the attribute.

Then, add a rule to the root `BUILD` file like the following.
Finally, add a rule to the root `BUILD` file like the following.

```python
load("@rules_rust//rust:defs.bzl", "rust_analyzer")
Expand Down
4 changes: 2 additions & 2 deletions docs/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2015" |
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
| <a id="rust_toolchain_repository-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] |
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. | Boolean | optional | False |
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. An environment variable <code>RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS</code> can also be used to control this attribute. This variable will take precedence over the hard coded attribute. Setting it to <code>true</code> to activates this attribute where all other values deactivate it. | Boolean | optional | False |
| <a id="rust_toolchain_repository-iso_date"></a>iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" |
| <a id="rust_toolchain_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
| <a id="rust_toolchain_repository-rustfmt_version"></a>rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | "" |
Expand Down Expand Up @@ -179,7 +179,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
| <a id="rust_repositories-edition"></a>edition | The rust edition to be used by default (2015 (default) or 2018) | <code>None</code> |
| <a id="rust_repositories-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | <code>False</code> |
| <a id="rust_repositories-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | <code>None</code> |
| <a id="rust_repositories-include_rustc_srcs"></a>include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | <code>False</code> |
| <a id="rust_repositories-include_rustc_srcs"></a>include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details | <code>False</code> |
| <a id="rust_repositories-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> |


Expand Down
22 changes: 19 additions & 3 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def rust_repositories(
edition (str, optional): The rust edition to be used by default (2015 (default) or 2018)
dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. Defaults to None.
include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.
include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details
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']
"""

Expand Down Expand Up @@ -586,7 +587,15 @@ def _rust_toolchain_repository_impl(ctx):

_check_version_valid(ctx.attr.version, ctx.attr.iso_date)

if ctx.attr.include_rustc_srcs:
# Determing whether or not to include rustc sources in the toolchain. The environment
# variable will always take precedence over the attribute.
include_rustc_srcs_env = ctx.os.environ.get("RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS")
if include_rustc_srcs_env != None:
include_rustc_srcs = include_rustc_srcs_env.lower() in ["true", "1"]
else:
include_rustc_srcs = ctx.attr.include_rustc_srcs

if include_rustc_srcs:
_load_rust_src(ctx)

build_components = [_load_rust_compiler(ctx)]
Expand Down Expand Up @@ -650,7 +659,13 @@ rust_toolchain_repository = repository_rule(
doc = "Additional rust-style targets that this set of toolchains should support.",
),
"include_rustc_srcs": attr.bool(
doc = "Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer.",
doc = (
"Whether to download and unpack the rustc source files. These are very large, and " +
"slow to unpack, but are required to support rust analyzer. An environment variable " +
"`RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS` can also be used to control this attribute. " +
"This variable will take precedence over the hard coded attribute. Setting it to `true` to " +
"activates this attribute where all other values deactivate it."
),
default = False,
),
"iso_date": attr.string(
Expand All @@ -675,6 +690,7 @@ rust_toolchain_repository = repository_rule(
),
},
implementation = _rust_toolchain_repository_impl,
environ = ["RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS"],
)

rust_toolchain_repository_proxy = repository_rule(
Expand Down

0 comments on commit 76cfae4

Please sign in to comment.