From 2567774183cbe09807bf53cd6e94d43d512d3dc0 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Fri, 25 Jun 2021 10:38:25 -0700 Subject: [PATCH] Added standalone targets for `rust_toolchain` components --- crate_universe/private/bootstrap/BUILD.bazel | 26 +----- rust/private/toolchain_utils.bzl | 81 +++++++++++++++++++ rust/toolchain/BUILD.bazel | 43 ++++++++++ test/current_toolchain_tools/BUILD.bazel | 43 ++++++++++ .../current_exec_files_test.sh | 28 +++++++ tools/rustfmt/BUILD.bazel | 15 +--- 6 files changed, 199 insertions(+), 37 deletions(-) create mode 100644 rust/private/toolchain_utils.bzl create mode 100644 rust/toolchain/BUILD.bazel create mode 100644 test/current_toolchain_tools/BUILD.bazel create mode 100755 test/current_toolchain_tools/current_exec_files_test.sh diff --git a/crate_universe/private/bootstrap/BUILD.bazel b/crate_universe/private/bootstrap/BUILD.bazel index 9138c38e23..d181ab61c8 100644 --- a/crate_universe/private/bootstrap/BUILD.bazel +++ b/crate_universe/private/bootstrap/BUILD.bazel @@ -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({ diff --git a/rust/private/toolchain_utils.bzl b/rust/private/toolchain_utils.bzl new file mode 100644 index 0000000000..f26acf298b --- /dev/null +++ b/rust/private/toolchain_utils.bzl @@ -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, +) diff --git a/rust/toolchain/BUILD.bazel b/rust/toolchain/BUILD.bazel new file mode 100644 index 0000000000..daccd08b3f --- /dev/null +++ b/rust/toolchain/BUILD.bazel @@ -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", +) diff --git a/test/current_toolchain_tools/BUILD.bazel b/test/current_toolchain_tools/BUILD.bazel new file mode 100644 index 0000000000..e617417b2c --- /dev/null +++ b/test/current_toolchain_tools/BUILD.bazel @@ -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 = { + "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() +] diff --git a/test/current_toolchain_tools/current_exec_files_test.sh b/test/current_toolchain_tools/current_exec_files_test.sh new file mode 100755 index 0000000000..aa55c4b25b --- /dev/null +++ b/test/current_toolchain_tools/current_exec_files_test.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -xeuo pipefail + +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 diff --git a/tools/rustfmt/BUILD.bazel b/tools/rustfmt/BUILD.bazel index 40fa906acb..93abad4398 100644 --- a/tools/rustfmt/BUILD.bazel +++ b/tools/rustfmt/BUILD.bazel @@ -4,17 +4,6 @@ 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( @@ -22,12 +11,12 @@ rust_library( 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)", }, )