Skip to content

Commit

Permalink
Support all toolchain parameters in module extension
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron-martin committed Feb 18, 2023
1 parent 53b6b54 commit 15a4b35
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
31 changes: 29 additions & 2 deletions extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
"Module extensions for using rules_rust with bzlmod"

load("//rust:repositories.bzl", "rust_register_toolchains")
load(
"//rust/private:repository_utils.bzl",
"DEFAULT_EXTRA_TARGET_TRIPLES",
"DEFAULT_NIGHTLY_VERSION",
"DEFAULT_STATIC_RUST_URL_TEMPLATES",
)

def _toolchains_impl(ctx):
mod = ctx.modules[0]
for toolchain in mod.tags.toolchain:
rust_register_toolchains(edition = toolchain.edition, register_toolchains = False)
rust_register_toolchains(
dev_components = toolchain.dev_components,
edition = toolchain.edition,
allocator_library = toolchain.allocator_library,
rustfmt_version = toolchain.rustfmt_version,
rust_analyzer_version = toolchain.rust_analyzer_version,
sha256s = toolchain.sha256s,
extra_target_triples = toolchain.extra_target_triples,
urls = toolchain.urls,
versions = toolchain.versions,
register_toolchains = False,
)

toolchains_toolchain = tag_class(attrs = {"edition": attr.string()})
toolchains_toolchain = tag_class(attrs = {
"dev_components": attr.bool(default = False),
"edition": attr.string(),
"allocator_library": attr.string(),
"rustfmt_version": attr.string(default = DEFAULT_NIGHTLY_VERSION),
"rust_analyzer_version": attr.string(),
"sha256s": attr.string_dict(),
"extra_target_triples": attr.string_list(default = DEFAULT_EXTRA_TARGET_TRIPLES),
"urls": attr.string_list(default = DEFAULT_STATIC_RUST_URL_TEMPLATES),
"versions": attr.string_list(default = []),
})
toolchains = module_extension(
implementation = _toolchains_impl,
tag_classes = {"toolchain": toolchains_toolchain},
Expand Down
3 changes: 3 additions & 0 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utility macros for use in rules_rust repository rules"""

load("//rust/private:common.bzl", "DEFAULT_NIGHTLY_ISO_DATE")
load("//rust:known_shas.bzl", "FILE_KEY_TO_SHA")
load(
"//rust/platform:triple_mappings.bzl",
Expand All @@ -11,6 +12,8 @@ load(

DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"
DEFAULT_STATIC_RUST_URL_TEMPLATES = ["https://static.rust-lang.org/dist/{}.tar.gz"]
DEFAULT_NIGHTLY_VERSION = "nightly/{}".format(DEFAULT_NIGHTLY_ISO_DATE)
DEFAULT_EXTRA_TARGET_TRIPLES = ["wasm32-unknown-unknown", "wasm32-wasi"]

_build_file_for_compiler_template = """\
filegroup(
Expand Down
10 changes: 5 additions & 5 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ load(
"BUILD_for_rust_toolchain",
"BUILD_for_rustfmt_toolchain",
"BUILD_for_toolchain",
"DEFAULT_EXTRA_TARGET_TRIPLES",
"DEFAULT_NIGHTLY_VERSION",
"DEFAULT_STATIC_RUST_URL_TEMPLATES",
"check_version_valid",
"includes_rust_analyzer_proc_macro_srv",
Expand Down Expand Up @@ -91,11 +93,9 @@ def rules_rust_dependencies():
build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel",
)

_DEFAULT_NIGHTLY_VERSION = "nightly/{}".format(DEFAULT_NIGHTLY_ISO_DATE)

_RUST_TOOLCHAIN_VERSIONS = [
rust_common.default_version,
_DEFAULT_NIGHTLY_VERSION,
DEFAULT_NIGHTLY_VERSION,
]

# buildifier: disable=unnamed-macro
Expand All @@ -105,10 +105,10 @@ def rust_register_toolchains(
allocator_library = None,
iso_date = None,
register_toolchains = True,
rustfmt_version = _DEFAULT_NIGHTLY_VERSION,
rustfmt_version = DEFAULT_NIGHTLY_VERSION,
rust_analyzer_version = None,
sha256s = None,
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
version = None,
versions = []):
Expand Down

0 comments on commit 15a4b35

Please sign in to comment.