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

Added standalone targets for rust_toolchain components. #792

Merged
merged 3 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
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
26 changes: 2 additions & 24 deletions crate_universe/private/bootstrap/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
alias(
name = "cargo",
actual = select({
"@rules_rust//rust/platform:aarch64-apple-darwin": "@rust_darwin_aarch64//:cargo",
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": "@rust_linux_aarch64//:cargo",
"@rules_rust//rust/platform:x86_64-apple-darwin": "@rust_darwin_x86_64//:cargo",
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": "@rust_windows_x86_64//:cargo",
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": "@rust_linux_x86_64//:cargo",
}),
)

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

_COMMON_ENV = {
"CARGO": "$${PWD}/$(execpath :cargo)",
"CARGO": "$${PWD}/$(execpath //rust/toolchain:current_exec_cargo_files)",
"DETECT_CHANGES": "true",
"RUSTC": "$${PWD}/$(execpath :rustc)",
"RUSTC": "$${PWD}/$(execpath //rust/toolchain:current_exec_rustc_files)",
}

_ENV = select({
Expand Down
81 changes: 81 additions & 0 deletions rust/private/toolchain_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""A module defining toolchain utilities"""

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

runfiles = None
if ctx.attr.tool == "cargo":
files = depset([toolchain.cargo])
runfiles = ctx.runfiles(
files = [
toolchain.cargo,
toolchain.rustc,
],
transitive_files = toolchain.rustc_lib.files,
)
elif ctx.attr.tool == "clippy":
files = depset([toolchain.clippy_driver])
runfiles = ctx.runfiles(
files = [
toolchain.clippy_driver,
toolchain.rustc,
],
transitive_files = toolchain.rustc_lib.files,
)
elif ctx.attr.tool == "rustc":
files = depset([toolchain.rustc])
runfiles = ctx.runfiles(
files = [toolchain.rustc],
transitive_files = toolchain.rustc_lib.files,
)
elif ctx.attr.tool == "rustdoc":
files = depset([toolchain.rust_doc])
runfiles = ctx.runfiles(
files = [toolchain.rust_doc],
transitive_files = toolchain.rustc_lib.files,
)
elif ctx.attr.tool == "rustfmt":
files = depset([toolchain.rustfmt])
runfiles = ctx.runfiles(
files = [toolchain.rustfmt],
transitive_files = toolchain.rustc_lib.files,
)
elif ctx.attr.tool == "rustc_lib":
files = toolchain.rustc_lib.files
elif ctx.attr.tool == "rustc_srcs":
files = toolchain.rustc_srcs.files
elif ctx.attr.tool == "rust_lib" or ctx.attr.tool == "rust_stdlib":
files = toolchain.rust_lib.files
else:
fail("Unsupported tool: ", ctx.attr.tool)

return [DefaultInfo(
files = files,
runfiles = runfiles,
)]

toolchain_files = rule(
doc = "A rule for fetching files from a rust toolchain.",
implementation = _toolchain_files_impl,
attrs = {
"tool": attr.string(
doc = "The desired tool to get form the current rust_toolchain",
values = [
"cargo",
"clippy",
"rustc",
"rustdoc",
"rustfmt",
"rustc_lib",
"rustc_srcs",
"rust_lib",
"rust_stdlib",
],
mandatory = True,
),
},
toolchains = [
str(Label("//rust:toolchain")),
],
incompatible_use_toolchain_transition = True,
)
43 changes: 43 additions & 0 deletions rust/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load("//rust/private:toolchain_utils.bzl", "toolchain_files")

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

toolchain_files(
name = "current_exec_cargo_files",
tool = "cargo",
)

toolchain_files(
name = "current_exec_clippy_files",
tool = "clippy",
)

toolchain_files(
name = "current_exec_rustc_files",
tool = "rustc",
)

toolchain_files(
name = "current_exec_rustdoc_files",
tool = "rustdoc",
)

toolchain_files(
name = "current_exec_rustfmt_files",
tool = "rustfmt",
)

toolchain_files(
name = "current_exec_rustc_lib_files",
tool = "rustc_lib",
)

toolchain_files(
name = "current_exec_rustc_srcs_files",
tool = "rustc_srcs",
)

toolchain_files(
name = "current_exec_rust_stdlib_files",
tool = "rust_stdlib",
)
43 changes: 43 additions & 0 deletions test/current_toolchain_tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Executable targets will output a pattern similar to the following
# cargo 1.53.0 (4369396ce 2021-04-27)
# Also Note, rustc_srcs is too big for this test
_FILES = {
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
"cargo": ("--executable", r"^cargo [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
"clippy": ("--executable", r"^clippy [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
"rust_stdlib": ("--files", r"\.rlib"),
"rustc": ("--executable", r"^rustc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
"rustc_lib": ("--files", r"rustc_driver"),
"rustdoc": ("--executable", r"^rustdoc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
"rustfmt": ("--executable", r"^rustfmt [0-9\.]\+\-stable ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
}

# Generate a list manifest for all files in the filegroup
[
genrule(
name = "{}_manifest_genrule".format(files),
srcs = ["//rust/toolchain:current_exec_{}_files".format(files)],
outs = ["{}_manifest".format(files)],
cmd = "for file in $(rootpaths //rust/toolchain:current_exec_{}_files); do echo $$file >> $@; done".format(files),
)
for files in _FILES
if "--files" in _FILES[files]
]

# Test that all toolchain tools are executable targets
[
sh_test(
name = tool + "_test",
srcs = ["current_exec_files_test.sh"],
args = [
"$(rootpath //rust/toolchain:current_exec_{}_files)".format(tool) if "--executable" == arg else "$(rootpath {}_manifest)".format(tool),
arg,
"'{}'".format(pattern),
],
data = [
"//rust/toolchain:current_exec_{}_files".format(tool),
] + (
["{}_manifest".format(tool)] if "--files" == arg else []
),
)
for tool, (arg, pattern) in _FILES.items()
]
28 changes: 28 additions & 0 deletions test/current_toolchain_tools/current_exec_files_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -xeuo pipefail
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved

TARGET="$1"
OPTION="$2"

# To parse this argument on windows it must be wrapped in quotes but
# these quotes should not be passed to grep. Remove them here.
PATTERN="$(echo -n "$3" | sed "s/'//g")"

if [[ "${OPTION}" == "--executable" ]]; then
# Clippy requires this environment variable is set
export SYSROOT=""

"${TARGET}" --version
"${TARGET}" --version | grep "${PATTERN}"
exit 0
fi

if [[ "${OPTION}" == "--files" ]]; then
cat "${TARGET}"
grep "${PATTERN}" "${TARGET}"
exit 0
fi

echo "Unexpected option: ${OPTION}"
exit 1
15 changes: 2 additions & 13 deletions tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,19 @@ package(default_visibility = ["//visibility:public"])

exports_files(["rustfmt.toml"])

alias(
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(
name = "rustfmt_lib",
srcs = glob(
["srcs/**/*.rs"],
exclude = ["srcs/**/*main.rs"],
),
data = [
":rustfmt_bin",
"//:rustfmt.toml",
"//rust/toolchain:current_exec_rustfmt_files",
],
edition = "2018",
rustc_env = {
"RUSTFMT": "$(rootpath :rustfmt_bin)",
"RUSTFMT": "$(rootpath //rust/toolchain:current_exec_rustfmt_files)",
"RUSTFMT_CONFIG": "$(rootpath //:rustfmt.toml)",
},
)
Expand Down