Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support downloading nightly rustc-dev components #349

Merged
merged 4 commits into from
Jun 23, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"

def rust_repositories(version = "1.44.0", iso_date = None, rustfmt_version = "1.4.8", edition = None):
def rust_repositories(version = "1.44.0", iso_date = None, rustfmt_version = "1.4.8", edition = None, dev_components = False):
"""Emits a default set of toolchains for Linux, OSX, and Freebsd

Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for
Expand All @@ -16,8 +16,12 @@ def rust_repositories(version = "1.44.0", iso_date = None, rustfmt_version = "1.
rustfmt_version: The version of rustfmt. Either "nightly", "beta", or an exact version.
iso_date: The date of the nightly or beta release (or None, if the version is a specific version).
edition: The rust edition to be used by default (2015 (default) or 2018)
dev_components: Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
"""

if dev_components and version != "nightly":
fail("Rust version must be set to \"nightly\" to enable rustc-dev components")

maybe(
http_archive,
name = "rules_cc",
Expand All @@ -35,6 +39,7 @@ def rust_repositories(version = "1.44.0", iso_date = None, rustfmt_version = "1.
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
)

rust_repository_set(
Expand All @@ -45,6 +50,7 @@ def rust_repositories(version = "1.44.0", iso_date = None, rustfmt_version = "1.
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
)

rust_repository_set(
Expand All @@ -55,6 +61,7 @@ def rust_repositories(version = "1.44.0", iso_date = None, rustfmt_version = "1.
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
)

def _check_version_valid(version, iso_date, param_prefix = ""):
Expand Down Expand Up @@ -382,6 +389,25 @@ def _load_rust_stdlib(ctx, target_triple):

return stdlib_BUILD + toolchain_BUILD

def _load_rustc_dev_nightly(ctx, target_triple):
"""Loads the nightly rustc dev component

Args:
ctx: A repository_ctx.
target_triple: The rust-style target triple of the tool
"""

load_arbitrary_tool(
ctx,
iso_date = ctx.attr.iso_date,
target_triple = target_triple,
tool_name = "rustc-dev",
tool_subdirectories = ["rustc-dev-{}".format(target_triple)],
version = ctx.attr.version,
)

return

def _rust_toolchain_repository_impl(ctx):
"""The implementation of the rust toolchain repository rule."""

Expand All @@ -392,7 +418,14 @@ def _rust_toolchain_repository_impl(ctx):
if ctx.attr.rustfmt_version:
BUILD_components.append(_load_rustfmt(ctx))

for target_triple in [ctx.attr.exec_triple] + ctx.attr.extra_target_triples:
for target_triple in [ctx.attr.exec_triple]:
BUILD_components.append(_load_rust_stdlib(ctx, target_triple))
if ctx.attr.dev_components:
# No BUILD file is needed because components will match the stdlib
# BUILD file globs
Arm1stice marked this conversation as resolved.
Show resolved Hide resolved
_load_rustc_dev_nightly(ctx, target_triple)

for target_triple in ctx.attr.extra_target_triples:
BUILD_components.append(_load_rust_stdlib(ctx, target_triple))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx.attr.extra_target_triples contains the wasm target. While there is a stdlib package, there are no rustc-dev components shipped for this target. Because of this, I split this into two different loops. This means the if statement in the first loop only has to check if dev_components is True. Another option might be to keep one loop, and have the if statement be:

if ctx.attr.dev_components and target_triple not in ctx.attr.extra_target_triples:

I prefer the two loops personally, but I can also switch to the longer if statement if there is a strong preference for it by the maintainers.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading the code, I think one loop will make reading the code easier, and adding a comment on why excluding extra_target_triples make sense (e.g. "it contains wasm and the likes, which does not have a dev component")


ctx.file("WORKSPACE", "")
Expand Down Expand Up @@ -441,6 +474,7 @@ rust_toolchain_repository = repository_rule(
"extra_target_triples": attr.string_list(),
"toolchain_name_prefix": attr.string(),
"edition": attr.string(default = "2015"),
"dev_components": attr.bool(default = False),
},
implementation = _rust_toolchain_repository_impl,
)
Expand Down Expand Up @@ -474,7 +508,8 @@ def rust_repository_set(
extra_target_triples = [],
iso_date = None,
rustfmt_version = None,
edition = None):
edition = None,
dev_components = False):
"""Assembles a remote repository for the given toolchain params, produces a proxy repository
to contain the toolchain declaration, and registers the toolchains.

Expand All @@ -490,6 +525,8 @@ def rust_repository_set(
should support.
rustfmt_version: The version of rustfmt to be associated with the toolchain.
edition: The rust edition to be used by default (2015 (default) or 2018)
dev_components: Whether to download the rustc-dev components (defaults to False).
Requires version to be "nightly".
"""

rust_toolchain_repository(
Expand All @@ -501,6 +538,7 @@ def rust_repository_set(
version = version,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components
)

rust_toolchain_repository_proxy(
Expand Down