Skip to content

Commit

Permalink
Ask rust_toolchain for rustc_srcs in rust_analyzer.bzl
Browse files Browse the repository at this point in the history
  • Loading branch information
hlopko committed May 11, 2021
1 parent 613f947 commit 25f034d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace(name = "rules_rust")

load("@rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()
rust_repositories(include_rustc_src = True)

load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")

Expand Down
3 changes: 3 additions & 0 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ deps()
load(":examples_transitive_deps.bzl", "transitive_deps")

transitive_deps()

load("@rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories(include_rustc_src = True, version = "nightly", edition="2018")
11 changes: 10 additions & 1 deletion rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,16 @@ def create_crate(ctx, info, crate_mapping):
# TODO(djmarcin): Run the cargo_build_scripts to gather env vars correctly.
def _rust_analyzer_impl(ctx):
rust_toolchain = find_toolchain(ctx)
sysroot_src = _exec_root_tmpl + rust_toolchain.rust_lib.label.workspace_root + "/lib/rustlib/src/library"

if not rust_toolchain.rustc_src:
fail(
"Current Rust toolchain doesn't contain rustc sources in `rustc_src` attribute.",
"These are needed by rust analyzer.",
"If you are using the default Rust toolchain, add `rust_repositories(include_rustc_src = True, ...).` to your WORKSPACE file.",
)
sysroot_src = rust_toolchain.rustc_src.label.package + "/library"
if rust_toolchain.rustc_src.label.workspace_root:
sysroot_src = _exec_root_tmpl + rust_toolchain.rustc_src.label.workspace_root + "/" + sysroot_src

# Gather all crates and their dependencies into an array.
# Dependencies are referenced by index, so leaves should come first.
Expand Down
39 changes: 13 additions & 26 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,6 @@ def BUILD_for_rustfmt(target_triple):
binary_ext = system_to_binary_ext(system),
)

_build_file_for_rustc_src = """\
load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
filegroup(
name = "rustc_src",
srcs = glob(
[
"lib/rustlib/src/**/*.rs",
],
),
visibility = ["//visibility:public"],
)
"""

def BUILD_for_rustc_src():
"""Emits a BUILD file for the rustc src extracted files."""

return _build_file_for_rustc_src

_build_file_for_clippy_template = """\
load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
Expand Down Expand Up @@ -331,7 +312,7 @@ def BUILD_for_rust_toolchain(

rustc_src = "None"
if include_rustc_src:
rustc_src = "\"@{workspace_name}//:rustc_src\"".format(workspace_name = workspace_name)
rustc_src = "\"@{workspace_name}//lib/rustlib/src:rustc_src\"".format(workspace_name = workspace_name)

return _build_file_for_rust_toolchain_template.format(
toolchain_name = name,
Expand Down Expand Up @@ -495,8 +476,6 @@ def _load_rust_src(ctx):
Args:
ctx (ctx): A repository_ctx.
Returns:
string: The BUILD file contents for the rust source code
"""
tool_suburl = produce_tool_suburl("rustc", "src", ctx.attr.version, ctx.attr.iso_date)
static_rust = ctx.os.environ.get("STATIC_RUST_URL", "https://static.rust-lang.org")
Expand All @@ -514,7 +493,15 @@ def _load_rust_src(ctx):
output = "lib/rustlib/src",
stripPrefix = tool_path,
)
return BUILD_for_rustc_src()
ctx.file(
"lib/rustlib/src/BUILD.bazel",
"""\
filegroup(
name = "rustc_src",
srcs = glob(["**/*"]),
visibility = ["//visibility:public"],
)""",
)

def _load_rust_stdlib(ctx, target_triple):
"""Loads a rust standard library and yields corresponding BUILD for it
Expand Down Expand Up @@ -599,10 +586,10 @@ def _rust_toolchain_repository_impl(ctx):

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

build_components = [_load_rust_compiler(ctx)]

if ctx.attr.include_rustc_src:
build_components.append(_load_rust_src(ctx))
_load_rust_src(ctx)

build_components = [_load_rust_compiler(ctx)]

if ctx.attr.rustfmt_version:
build_components.append(_load_rustfmt(ctx))
Expand Down

0 comments on commit 25f034d

Please sign in to comment.