diff --git a/docs/flatten.md b/docs/flatten.md index 2626106f2d..ea5766fa90 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1144,8 +1144,8 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
 rust_toolchain_repository(name, dev_components, edition, exec_triple, extra_target_triples,
-                          iso_date, repo_mapping, rustfmt_version, sha256s, toolchain_name_prefix,
-                          urls, version)
+                          include_rustc_src, iso_date, repo_mapping, rustfmt_version, sha256s,
+                          toolchain_name_prefix, urls, version)
 
Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms. @@ -1162,6 +1162,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito | edition | The rust edition to be used by default. | String | optional | "2015" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | | extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] | +| include_rustc_src | 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 | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | | rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | "" | @@ -1531,7 +1532,8 @@ Declare dependencies needed for proto compilation. ## rust_repositories
-rust_repositories(version, iso_date, rustfmt_version, edition, dev_components, sha256s, urls)
+rust_repositories(version, iso_date, rustfmt_version, edition, dev_components, sha256s,
+                  include_rustc_src, urls)
 
Emits a default set of toolchains for Linux, MacOS, and Freebsd @@ -1562,6 +1564,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | edition | The rust edition to be used by default (2015 (default) or 2018) | None | | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | False | | sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | None | +| include_rustc_src | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | False | | 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'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | @@ -1570,8 +1573,8 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai ## rust_repository_set
-rust_repository_set(name, version, exec_triple, extra_target_triples, iso_date, rustfmt_version,
-                    edition, dev_components, sha256s, urls)
+rust_repository_set(name, version, exec_triple, include_rustc_src, extra_target_triples, iso_date,
+                    rustfmt_version, edition, dev_components, sha256s, urls)
 
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -1587,6 +1590,7 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with | name | The name of the generated repository | none | | version | The version of the tool among "nightly", "beta', or an exact version. | none | | exec_triple | The Rust-style target that this compiler runs on | none | +| include_rustc_src | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | False | | extra_target_triples | Additional rust-style targets that this set of toolchains should support. Defaults to []. | [] | | iso_date | The date of the tool. Defaults to None. | None | | rustfmt_version | The version of rustfmt to be associated with the toolchain. Defaults to None. | None | diff --git a/docs/rust_analyzer.md b/docs/rust_analyzer.md index 661c4fec92..bd7964d1b5 100644 --- a/docs/rust_analyzer.md +++ b/docs/rust_analyzer.md @@ -21,6 +21,8 @@ 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`. + Then, add a rule to the root `BUILD` file like the following. ```python diff --git a/docs/rust_analyzer.vm b/docs/rust_analyzer.vm index 7d05f6b0ab..e8c1d13118 100644 --- a/docs/rust_analyzer.vm +++ b/docs/rust_analyzer.vm @@ -16,6 +16,8 @@ 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`. + Then, add a rule to the root `BUILD` file like the following. ```python diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index d93995364c..46adaa3309 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -90,8 +90,8 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
 rust_toolchain_repository(name, dev_components, edition, exec_triple, extra_target_triples,
-                          iso_date, repo_mapping, rustfmt_version, sha256s, toolchain_name_prefix,
-                          urls, version)
+                          include_rustc_src, iso_date, repo_mapping, rustfmt_version, sha256s,
+                          toolchain_name_prefix, urls, version)
 
Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms. @@ -108,6 +108,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito | edition | The rust edition to be used by default. | String | optional | "2015" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | | extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] | +| include_rustc_src | 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 | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | | rustfmt_version | The version of the tool among "nightly", "beta", or an exact version. | String | optional | "" | @@ -146,7 +147,8 @@ Generates a toolchain-bearing repository that declares the toolchains from some ## rust_repositories
-rust_repositories(version, iso_date, rustfmt_version, edition, dev_components, sha256s, urls)
+rust_repositories(version, iso_date, rustfmt_version, edition, dev_components, sha256s,
+                  include_rustc_src, urls)
 
Emits a default set of toolchains for Linux, MacOS, and Freebsd @@ -177,6 +179,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | edition | The rust edition to be used by default (2015 (default) or 2018) | None | | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | False | | sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | None | +| include_rustc_src | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | False | | 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'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | @@ -185,8 +188,8 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai ## rust_repository_set
-rust_repository_set(name, version, exec_triple, extra_target_triples, iso_date, rustfmt_version,
-                    edition, dev_components, sha256s, urls)
+rust_repository_set(name, version, exec_triple, include_rustc_src, extra_target_triples, iso_date,
+                    rustfmt_version, edition, dev_components, sha256s, urls)
 
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -202,6 +205,7 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with | name | The name of the generated repository | none | | version | The version of the tool among "nightly", "beta', or an exact version. | none | | exec_triple | The Rust-style target that this compiler runs on | none | +| include_rustc_src | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | False | | extra_target_triples | Additional rust-style targets that this set of toolchains should support. Defaults to []. | [] | | iso_date | The date of the tool. Defaults to None. | None | | rustfmt_version | The version of rustfmt to be associated with the toolchain. Defaults to None. | None | diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 4c7e52b9eb..9ce8397d83 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -34,6 +34,7 @@ def rust_repositories( edition = None, dev_components = False, sha256s = None, + include_rustc_src = False, urls = DEFAULT_STATIC_RUST_URL_TEMPLATES): """Emits a default set of toolchains for Linux, MacOS, and Freebsd @@ -60,6 +61,7 @@ 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_src (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. 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'] """ @@ -99,6 +101,7 @@ def rust_repositories( edition = edition, dev_components = dev_components, sha256s = sha256s, + include_rustc_src = include_rustc_src, urls = urls, ) @@ -285,7 +288,7 @@ rust_toolchain( cargo = "@{workspace_name}//:cargo", clippy_driver = "@{workspace_name}//:clippy_driver_bin", rustc_lib = "@{workspace_name}//:rustc_lib", - rustc_src = "@{workspace_name}//:rustc_src", + rustc_src = {rustc_src}, binary_ext = "{binary_ext}", staticlib_ext = "{staticlib_ext}", dylib_ext = "{dylib_ext}", @@ -303,6 +306,7 @@ def BUILD_for_rust_toolchain( name, exec_triple, target_triple, + include_rustc_src, stdlib_linkflags = None, default_edition = "2015"): """Emits a toolchain declaration to match an existing compiler and stdlib. @@ -312,6 +316,7 @@ def BUILD_for_rust_toolchain( name (str): The name of the toolchain declaration exec_triple (str): The rust-style target that this compiler runs on target_triple (str): The rust-style target triple of the tool + include_rustc_src (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. stdlib_linkflags (list, optional): Overriden flags needed for linking to rust stdlib, akin to BAZEL_LINKLIBS. Defaults to None. @@ -324,12 +329,17 @@ def BUILD_for_rust_toolchain( if stdlib_linkflags == None: stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(system)]) + rustc_src = "None" + if include_rustc_src: + rustc_src = "\"@{workspace_name}//:rustc_src\"".format(workspace_name = workspace_name) + return _build_file_for_rust_toolchain_template.format( toolchain_name = name, workspace_name = workspace_name, binary_ext = system_to_binary_ext(system), staticlib_ext = system_to_staticlib_ext(system), dylib_ext = system_to_dylib_ext(system), + rustc_src = rustc_src, stdlib_linkflags = stdlib_linkflags, system = system, default_edition = default_edition, @@ -538,6 +548,7 @@ def _load_rust_stdlib(ctx, target_triple): target_triple = target_triple, ), exec_triple = ctx.attr.exec_triple, + include_rustc_src = ctx.attr.include_rustc_src, target_triple = target_triple, stdlib_linkflags = stdlib_linkflags, workspace_name = ctx.attr.name, @@ -588,7 +599,10 @@ def _rust_toolchain_repository_impl(ctx): _check_version_valid(ctx.attr.version, ctx.attr.iso_date) - build_components = [_load_rust_compiler(ctx), _load_rust_src(ctx)] + build_components = [_load_rust_compiler(ctx)] + + if ctx.attr.include_rustc_src: + build_components.append(_load_rust_src(ctx)) if ctx.attr.rustfmt_version: build_components.append(_load_rustfmt(ctx)) @@ -648,6 +662,10 @@ rust_toolchain_repository = repository_rule( "extra_target_triples": attr.string_list( doc = "Additional rust-style targets that this set of toolchains should support.", ), + "include_rustc_src": 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.", + default = False, + ), "iso_date": attr.string( doc = "The date of the tool (or None, if the version is a specific version).", ), @@ -700,6 +718,7 @@ def rust_repository_set( name, version, exec_triple, + include_rustc_src = False, extra_target_triples = [], iso_date = None, rustfmt_version = None, @@ -717,6 +736,7 @@ def rust_repository_set( name (str): The name of the generated repository version (str): The version of the tool among "nightly", "beta', or an exact version. exec_triple (str): The Rust-style target that this compiler runs on + include_rustc_src (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. extra_target_triples (list, optional): Additional rust-style targets that this set of toolchains should support. Defaults to []. iso_date (str, optional): The date of the tool. Defaults to None. @@ -733,6 +753,7 @@ def rust_repository_set( rust_toolchain_repository( name = name, exec_triple = exec_triple, + include_rustc_src = include_rustc_src, extra_target_triples = extra_target_triples, iso_date = iso_date, toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX,