Skip to content

Commit

Permalink
Added a toolchain_tool rule for fetching rust tools from a toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jun 25, 2021
1 parent 4efd1f7 commit d80501a
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ PAGES = dict([
"rust_toolchain",
"rust_toolchain_repository",
"rust_toolchain_repository_proxy",
"toolchain_tool",
],
),
page(
Expand Down
2 changes: 2 additions & 0 deletions docs/symbols.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ load(
load(
"@rules_rust//rust:toolchain.bzl",
_rust_toolchain = "rust_toolchain",
_toolchain_tool = "toolchain_tool",
)
load(
"@rules_rust//wasm_bindgen:repositories.bzl",
Expand Down Expand Up @@ -96,6 +97,7 @@ rust_bindgen_repositories = _rust_bindgen_repositories
rust_toolchain = _rust_toolchain
rust_proto_toolchain = _rust_proto_toolchain
rust_proto_repositories = _rust_proto_repositories
toolchain_tool = _toolchain_tool

cargo_build_script = _cargo_build_script

Expand Down
167 changes: 167 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,170 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
with the actual binaries and libraries.
""",
)

def _cargo_tool_impl(ctx):
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]

# Executables must be produced within the rule marked executable. To
# satisfy this, a symlink is created.
cargo = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = cargo,
target_file = toolchain.cargo,
is_executable = True,
)

return [DefaultInfo(
files = depset([toolchain.cargo]),
executable = cargo,
)]

_cargo_tool = rule(
doc = "A rule for fetching a `cargo` binary from a rust toolchain.",
implementation = _cargo_tool_impl,
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
executable = True,
)

def _clippy_tool_impl(ctx):
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]

# Executables must be produced within the rule marked executable. To
# satisfy this, a symlink is created.
clippy_driver = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = clippy_driver,
target_file = toolchain.clippy_driver,
is_executable = True,
)

return [DefaultInfo(
files = depset([toolchain.clippy_driver]),
executable = clippy_driver,
)]

_clippy_tool = rule(
doc = "A rule for fetching a `clippy` binary from a rust toolchain.",
implementation = _clippy_tool_impl,
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
executable = True,
)

def _rustc_tool_impl(ctx):
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]

# Executables must be produced within the rule marked executable. To
# satisfy this, a symlink is created.
rustc = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = rustc,
target_file = toolchain.rustc,
is_executable = True,
)

return [DefaultInfo(
files = depset([toolchain.rustc]),
runfiles = ctx.runfiles(transitive_files = toolchain.rustc_lib[DefaultInfo].files),
executable = rustc,
)]

_rustc_tool = rule(
doc = "A rule for fetching a `rustc` binary from a rust toolchain.",
implementation = _rustc_tool_impl,
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
executable = True,
)

def _rustc_srcs_tool_impl(ctx):
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]
srcs = []
if toolchain.rustc_srcs:
srcs = toolchain.rustc_srcs[DefaultInfo].files
return [DefaultInfo(
files = depset(srcs),
)]

_rustc_srcs_tool = rule(
doc = (
"A rule for fetching a `rustc-src` artifact from a rust toolchain. " +
"This can optionally return no files depending on whtether or not the " +
"toolchain was setup using `include_rustc_srcs = True`."
),
implementation = _rustc_srcs_tool_impl,
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
)

def _rustfmt_tool_impl(ctx):
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]

# Executables must be produced within the rule marked executable. To
# satisfy this, a symlink is created.
rustfmt = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = rustfmt,
target_file = toolchain.rustfmt,
is_executable = True,
)

return [DefaultInfo(
files = depset([toolchain.rustfmt]),
executable = rustfmt,
)]

_rustfmt_tool = rule(
doc = "A rule for fetching a `rustfmt` binary from a rust toolchain.",
implementation = _rustfmt_tool_impl,
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
executable = True,
)

def _rustdoc_tool_impl(ctx):
toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]

# Executables must be produced within the rule marked executable. To
# satisfy this, a symlink is created.
rustdoc = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = rustdoc,
target_file = toolchain.rust_doc,
is_executable = True,
)

return [DefaultInfo(
files = depset([toolchain.rust_doc]),
executable = rustdoc,
)]

_rustdoc_tool = rule(
doc = "A rule for fetching a `rustdoc` binary from a rust toolchain.",
implementation = _rustdoc_tool_impl,
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
executable = True,
)

toolchain_tool = struct(
cargo = _cargo_tool,
clippy = _clippy_tool,
rustc_srcs = _rustc_srcs_tool,
rustc = _rustc_tool,
rustdoc = _rustdoc_tool,
rustfmt = _rustfmt_tool,
)
25 changes: 25 additions & 0 deletions test/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("//rust:toolchain.bzl", "toolchain_tool")

toolchain_tool.cargo(
name = "cargo",
)

toolchain_tool.clippy(
name = "clippy",
)

toolchain_tool.rustc_srcs(
name = "rustc_srcs",
)

toolchain_tool.rustc(
name = "rustc",
)

toolchain_tool.rustdoc(
name = "rustdoc",
)

toolchain_tool.rustfmt(
name = "rustfmt",
)
10 changes: 2 additions & 8 deletions tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library")
load("//rust:toolchain.bzl", "toolchain_tool")

package(default_visibility = ["//visibility:public"])

exports_files(["rustfmt.toml"])

alias(
toolchain_tool.rustfmt(
name = "rustfmt_bin",
actual = select({
"@rules_rust//rust/platform:aarch64-apple-darwin": "@rust_darwin_aarch64//:rustfmt_bin",
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": "@rust_linux_aarch64//:rustfmt_bin",
"@rules_rust//rust/platform:x86_64-apple-darwin": "@rust_darwin_x86_64//:rustfmt_bin",
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": "@rust_windows_x86_64//:rustfmt_bin",
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": "@rust_linux_x86_64//:rustfmt_bin",
}),
)

rust_library(
Expand Down

0 comments on commit d80501a

Please sign in to comment.